From 6d1ddad428f87798e014869646b42fcee34249e3 Mon Sep 17 00:00:00 2001 From: Redacted Date: Thu, 6 Feb 2025 10:26:34 -0500 Subject: [PATCH] Add a way to query the max texture size. --- include/JGL/types/RenderTarget.h | 5 +++-- include/JGL/types/Texture.h | 5 ++++- src/types/RenderTarget.cpp | 4 ++++ src/types/Texture.cpp | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/include/JGL/types/RenderTarget.h b/include/JGL/types/RenderTarget.h index 25503ef..e012cbd 100644 --- a/include/JGL/types/RenderTarget.h +++ b/include/JGL/types/RenderTarget.h @@ -91,10 +91,11 @@ public: /// @returns The color that should be used to clear this Render Target. [[nodiscard]] Color4 GetClearColor() const; - /// @returns The color information for this Render Target. - /// @note Both the CPU & GPU cannot do anything while this takes place. It's very slow. + /// @note The CPU thread this is called from & the GPU cannot do anything while this takes place. It's very slow. [[nodiscard]] std::vector GetPixels() const; + + [[nodiscard]] static Vector2i MaximimSize(); public: /// Create a Render Target from a Render Target that already exists. /** @note Render Targets that are copies of another will copy the Texture. diff --git a/include/JGL/types/Texture.h b/include/JGL/types/Texture.h index 183efc7..0948034 100644 --- a/include/JGL/types/Texture.h +++ b/include/JGL/types/Texture.h @@ -27,7 +27,6 @@ namespace JGL { class Texture; } -/// TODO handle the case of a texture being loaded that exceeds the max texture size. /// Represents texture data loaded on the GPU. Contains a handle that can be passed to OpenGL draw calls. class JGL::Texture { protected: @@ -40,6 +39,7 @@ protected: void load(const unsigned char* pixels); std::vector png(const std::filesystem::path& file); std::vector bmp(const std::filesystem::path& file); + [[nodiscard]] bool SizeExceedsMaximum(const Vector2i& size); public: /// @returns A handle used to identify this texture. [[nodiscard]] unsigned int GetHandle() const; @@ -56,6 +56,9 @@ public: /// @returns The raw pixels this texture is made up of. /// @note This will read-back from the GPU. Slow. [[nodiscard]] std::vector GetPixelData() const; + /// @returns The biggest size for a texture on this system. + /// @note on modern systems this is *usually* ridiculous. + [[nodiscard]] static Vector2i MaximumSize(); public: /// Load a texture from a file, explicit Texture(const std::filesystem::path& file, FilteringMode filtering_mode = FilteringMode::BILINEAR, WrappingMode wrapping_mode = WrappingMode::CLAMP_TO_EDGE, bool invert_y = true); diff --git a/src/types/RenderTarget.cpp b/src/types/RenderTarget.cpp index 86a18df..cad7803 100644 --- a/src/types/RenderTarget.cpp +++ b/src/types/RenderTarget.cpp @@ -381,3 +381,7 @@ JGL::RenderTarget::RenderTarget(const JGL::RenderTarget& rhs) { operator delete(this_render_target); } + +Vector2i JGL::RenderTarget::MaximimSize() { + return Texture::MaximumSize(); +} diff --git a/src/types/Texture.cpp b/src/types/Texture.cpp index 6269bc6..0b2e161 100644 --- a/src/types/Texture.cpp +++ b/src/types/Texture.cpp @@ -99,6 +99,8 @@ std::vector Texture::bmp(const std::filesystem::path& file) { } Texture::Texture(const Vector2i& size) : invert_y(true), format(ColorFormat::RGBA), size(size), filtering_mode(FilteringMode::NEAREST), wrapping_mode(WrappingMode::CLAMP_TO_EDGE) { + if (SizeExceedsMaximum(size)) + Logger::Error("Creating a texture where the size is bigger than the maximum for this system."); GLuint previous_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*) &previous_texture); @@ -116,6 +118,9 @@ Texture::Texture(const Vector2i& size) : invert_y(true), format(ColorFormat::RGB } void Texture::load(const unsigned char* pixels) { + if (SizeExceedsMaximum(size)) + Logger::Error("Creating a texture where the size is bigger than the maximum for this system."); + GLuint previous_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*) &previous_texture); @@ -272,3 +277,16 @@ Texture::Texture(const Texture* textures, const size_t& texture_count) { next_x += textures[i].GetDimensions().x; } } + +bool Texture::SizeExceedsMaximum(const Vector2i& s) { + + auto max_size = Texture::MaximumSize(); + return s.x > max_size.x || s.y > max_size.y; +} + +Vector2i Texture::MaximumSize() { + GLint max_size; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_size); + + return { max_size, max_size }; +}