Browse Source

Cleaned up love.graphics Image code for determining whether the image's pixel format is supported.

Alex Szpakowski 6 years ago
parent
commit
74dcc41b38

+ 1 - 1
src/modules/graphics/Graphics.h

@@ -829,7 +829,7 @@ public:
 	 **/
 	 **/
 	virtual bool isCanvasFormatSupported(PixelFormat format) const = 0;
 	virtual bool isCanvasFormatSupported(PixelFormat format) const = 0;
 	virtual bool isCanvasFormatSupported(PixelFormat format, bool readable) 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.
 	 * Gets the renderer used by love.graphics.

+ 15 - 2
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)
 	if (validatedata && data.validate() == MIPMAPS_DATA)
 		mipmapsType = MIPMAPS_DATA;
 		mipmapsType = MIPMAPS_DATA;
-
-	++imageCount;
 }
 }
 
 
 Image::Image(TextureType textype, PixelFormat format, int width, int height, int slices, const Settings &settings)
 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)
 void Image::init(PixelFormat fmt, int w, int h, const Settings &settings)
 {
 {
+	Graphics *gfx = Module::getInstance<Graphics>(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;
 	pixelWidth = w;
 	pixelHeight = h;
 	pixelHeight = h;
 
 
@@ -97,6 +108,8 @@ void Image::init(PixelFormat fmt, int w, int h, const Settings &settings)
 		filter.mipmap = defaultMipmapFilter;
 		filter.mipmap = defaultMipmapFilter;
 
 
 	initQuad();
 	initQuad();
+
+	++imageCount;
 }
 }
 
 
 void Image::uploadImageData(love::image::ImageDataBase *d, int level, int slice, int x, int y)
 void Image::uploadImageData(love::image::ImageDataBase *d, int level, int slice, int x, int y)

+ 2 - 2
src/modules/graphics/opengl/Graphics.cpp

@@ -1398,9 +1398,9 @@ bool Graphics::isCanvasFormatSupported(PixelFormat format, bool readable) const
 	return Canvas::isFormatSupported(format, readable);
 	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
 Shader::Language Graphics::getShaderLanguageTarget() const

+ 1 - 1
src/modules/graphics/opengl/Graphics.h

@@ -106,7 +106,7 @@ public:
 
 
 	bool isCanvasFormatSupported(PixelFormat format) const override;
 	bool isCanvasFormatSupported(PixelFormat format) const override;
 	bool isCanvasFormatSupported(PixelFormat format, bool readable) 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;
 	Renderer getRenderer() const override;
 	RendererInfo getRendererInfo() const override;
 	RendererInfo getRendererInfo() const override;
 
 

+ 3 - 22
src/modules/graphics/opengl/Image.cpp

@@ -185,22 +185,8 @@ bool Image::loadVolatile()
 
 
 	OpenGL::TempDebugGroup debuggroup("Image load");
 	OpenGL::TempDebugGroup debuggroup("Image load");
 
 
-	if (!OpenGL::isPixelFormatSupported(format, false, true, sRGB))
-	{
-		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 (!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.
 		// 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)
 		if (sRGB && (GLAD_ES_VERSION_2_0 && GLAD_EXT_sRGB && !GLAD_ES_VERSION_3_0)
 			&& mipmapsType != MIPMAPS_DATA)
 			&& mipmapsType != MIPMAPS_DATA)
@@ -365,14 +351,9 @@ bool Image::setMipmapSharpness(float sharpness)
 	return true;
 	return true;
 }
 }
 
 
-bool Image::isFormatSupported(PixelFormat pixelformat)
-{
-	return OpenGL::isPixelFormatSupported(pixelformat, false, true, false);
-}
-
-bool Image::hasSRGBSupport()
+bool Image::isFormatSupported(PixelFormat pixelformat, bool sRGB)
 {
 {
-	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
 } // opengl

+ 1 - 2
src/modules/graphics/opengl/Image.h

@@ -54,8 +54,7 @@ public:
 
 
 	bool setMipmapSharpness(float sharpness) override;
 	bool setMipmapSharpness(float sharpness) override;
 
 
-	static bool isFormatSupported(PixelFormat pixelformat);
-	static bool hasSRGBSupport();
+	static bool isFormatSupported(PixelFormat pixelformat, bool sRGB);
 
 
 private:
 private: