From 74dcc41b3877c838fe4a72944333de459291feeb Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Wed, 16 Jan 2019 19:30:21 -0400 Subject: [PATCH] Cleaned up love.graphics Image code for determining whether the image's pixel format is supported. --- src/modules/graphics/Graphics.h | 2 +- src/modules/graphics/Image.cpp | 17 ++++++++++++++-- src/modules/graphics/opengl/Graphics.cpp | 4 ++-- src/modules/graphics/opengl/Graphics.h | 2 +- src/modules/graphics/opengl/Image.cpp | 25 +++--------------------- src/modules/graphics/opengl/Image.h | 3 +-- 6 files changed, 23 insertions(+), 30 deletions(-) diff --git a/src/modules/graphics/Graphics.h b/src/modules/graphics/Graphics.h index 3fb7413c6..7ae831431 100644 --- a/src/modules/graphics/Graphics.h +++ b/src/modules/graphics/Graphics.h @@ -829,7 +829,7 @@ public: **/ virtual bool isCanvasFormatSupported(PixelFormat format) const = 0; virtual bool isCanvasFormatSupported(PixelFormat format, bool readable) const = 0; - virtual bool isImageFormatSupported(PixelFormat format) const = 0; + virtual bool isImageFormatSupported(PixelFormat format, bool sRGB = false) const = 0; /** * Gets the renderer used by love.graphics. diff --git a/src/modules/graphics/Image.cpp b/src/modules/graphics/Image.cpp index fbef8a5e3..d9414b568 100644 --- a/src/modules/graphics/Image.cpp +++ b/src/modules/graphics/Image.cpp @@ -43,8 +43,6 @@ Image::Image(const Slices &data, const Settings &settings, bool validatedata) { if (validatedata && data.validate() == MIPMAPS_DATA) mipmapsType = MIPMAPS_DATA; - - ++imageCount; } Image::Image(TextureType textype, PixelFormat format, int width, int height, int slices, const Settings &settings) @@ -80,6 +78,19 @@ Image::~Image() void Image::init(PixelFormat fmt, int w, int h, const Settings &settings) { + Graphics *gfx = Module::getInstance(Module::M_GRAPHICS); + if (gfx != nullptr && !gfx->isImageFormatSupported(fmt, sRGB)) + { + const char *str; + if (love::getConstant(fmt, str)) + { + throw love::Exception("Cannot create image: " + "%s%s images are not supported on this system.", sRGB ? "sRGB " : "", str); + } + else + throw love::Exception("cannot create image: format is not supported on this system."); + } + pixelWidth = w; pixelHeight = h; @@ -97,6 +108,8 @@ void Image::init(PixelFormat fmt, int w, int h, const Settings &settings) filter.mipmap = defaultMipmapFilter; initQuad(); + + ++imageCount; } void Image::uploadImageData(love::image::ImageDataBase *d, int level, int slice, int x, int y) diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index 3c003b9b3..af28cd551 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -1398,9 +1398,9 @@ bool Graphics::isCanvasFormatSupported(PixelFormat format, bool readable) const return Canvas::isFormatSupported(format, readable); } -bool Graphics::isImageFormatSupported(PixelFormat format) const +bool Graphics::isImageFormatSupported(PixelFormat format, bool sRGB) const { - return Image::isFormatSupported(format); + return Image::isFormatSupported(format, sRGB); } Shader::Language Graphics::getShaderLanguageTarget() const diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index e918ca751..760437f58 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -106,7 +106,7 @@ public: bool isCanvasFormatSupported(PixelFormat format) const override; bool isCanvasFormatSupported(PixelFormat format, bool readable) const override; - bool isImageFormatSupported(PixelFormat format) const override; + bool isImageFormatSupported(PixelFormat format, bool sRGB) const override; Renderer getRenderer() const override; RendererInfo getRendererInfo() const override; diff --git a/src/modules/graphics/opengl/Image.cpp b/src/modules/graphics/opengl/Image.cpp index bb537faf3..ca85b80b0 100644 --- a/src/modules/graphics/opengl/Image.cpp +++ b/src/modules/graphics/opengl/Image.cpp @@ -185,22 +185,8 @@ bool Image::loadVolatile() OpenGL::TempDebugGroup debuggroup("Image load"); - if (!OpenGL::isPixelFormatSupported(format, false, true, sRGB)) + if (!isCompressed()) { - const char *str; - if (love::getConstant(format, str)) - { - throw love::Exception("Cannot create image: " - "%s%s images are not supported on this system.", sRGB ? "sRGB " : "", str); - } - else - throw love::Exception("cannot create image: format is not supported on this system."); - } - else if (!isCompressed()) - { - if (sRGB && !hasSRGBSupport()) - throw love::Exception("sRGB images are not supported on this system."); - // GL_EXT_sRGB doesn't support glGenerateMipmap for sRGB textures. if (sRGB && (GLAD_ES_VERSION_2_0 && GLAD_EXT_sRGB && !GLAD_ES_VERSION_3_0) && mipmapsType != MIPMAPS_DATA) @@ -365,14 +351,9 @@ bool Image::setMipmapSharpness(float sharpness) return true; } -bool Image::isFormatSupported(PixelFormat pixelformat) +bool Image::isFormatSupported(PixelFormat pixelformat, bool sRGB) { - return OpenGL::isPixelFormatSupported(pixelformat, false, true, false); -} - -bool Image::hasSRGBSupport() -{ - return GLAD_ES_VERSION_3_0 || GLAD_EXT_sRGB || GLAD_VERSION_2_1 || GLAD_EXT_texture_sRGB; + return OpenGL::isPixelFormatSupported(pixelformat, false, true, sRGB); } } // opengl diff --git a/src/modules/graphics/opengl/Image.h b/src/modules/graphics/opengl/Image.h index f90af7f4b..765ddd57b 100644 --- a/src/modules/graphics/opengl/Image.h +++ b/src/modules/graphics/opengl/Image.h @@ -54,8 +54,7 @@ public: bool setMipmapSharpness(float sharpness) override; - static bool isFormatSupported(PixelFormat pixelformat); - static bool hasSRGBSupport(); + static bool isFormatSupported(PixelFormat pixelformat, bool sRGB); private: