Browse Source

Improve setCanvas validation of volume texture layer + mipmap values.

Alex Szpakowski 3 years ago
parent
commit
d96e1636b1

+ 3 - 3
src/modules/graphics/Graphics.cpp

@@ -779,7 +779,7 @@ void Graphics::setRenderTargets(const RenderTargets &rts)
 	if (firsttarget.mipmap < 0 || firsttarget.mipmap >= firsttex->getMipmapCount())
 	if (firsttarget.mipmap < 0 || firsttarget.mipmap >= firsttex->getMipmapCount())
 		throw love::Exception("Invalid mipmap level %d.", firsttarget.mipmap + 1);
 		throw love::Exception("Invalid mipmap level %d.", firsttarget.mipmap + 1);
 
 
-	if (!firsttex->isValidSlice(firsttarget.slice))
+	if (!firsttex->isValidSlice(firsttarget.slice, firsttarget.mipmap))
 		throw love::Exception("Invalid slice index: %d.", firsttarget.slice + 1);
 		throw love::Exception("Invalid slice index: %d.", firsttarget.slice + 1);
 
 
 	bool hasSRGBtexture = isPixelFormatSRGB(firstcolorformat);
 	bool hasSRGBtexture = isPixelFormatSRGB(firstcolorformat);
@@ -800,7 +800,7 @@ void Graphics::setRenderTargets(const RenderTargets &rts)
 		if (mip < 0 || mip >= c->getMipmapCount())
 		if (mip < 0 || mip >= c->getMipmapCount())
 			throw love::Exception("Invalid mipmap level %d.", mip + 1);
 			throw love::Exception("Invalid mipmap level %d.", mip + 1);
 
 
-		if (!c->isValidSlice(slice))
+		if (!c->isValidSlice(slice, mip))
 			throw love::Exception("Invalid slice index: %d.", slice + 1);
 			throw love::Exception("Invalid slice index: %d.", slice + 1);
 
 
 		if (c->getPixelWidth(mip) != pixelw || c->getPixelHeight(mip) != pixelh)
 		if (c->getPixelWidth(mip) != pixelw || c->getPixelHeight(mip) != pixelh)
@@ -840,7 +840,7 @@ void Graphics::setRenderTargets(const RenderTargets &rts)
 		if (mip < 0 || mip >= c->getMipmapCount())
 		if (mip < 0 || mip >= c->getMipmapCount())
 			throw love::Exception("Invalid mipmap level %d.", mip + 1);
 			throw love::Exception("Invalid mipmap level %d.", mip + 1);
 
 
-		if (!c->isValidSlice(slice))
+		if (!c->isValidSlice(slice, mip))
 			throw love::Exception("Invalid slice index: %d.", slice + 1);
 			throw love::Exception("Invalid slice index: %d.", slice + 1);
 	}
 	}
 
 

+ 13 - 12
src/modules/graphics/Texture.cpp

@@ -609,21 +609,22 @@ bool Texture::isFormatLinear() const
 	return isGammaCorrect() && !sRGB && !isPixelFormatSRGB(format);
 	return isGammaCorrect() && !sRGB && !isPixelFormatSRGB(format);
 }
 }
 
 
-bool Texture::isValidSlice(int slice) const
+bool Texture::isValidSlice(int slice, int mip) const
 {
 {
-	if (slice < 0)
-		return false;
+	return slice >= 0 && slice < getSliceCount(mip);
+}
 
 
-	if (texType == TEXTURE_CUBE)
-		return slice < 6;
-	else if (texType == TEXTURE_VOLUME)
-		return slice < depth;
+int Texture::getSliceCount(int mip) const
+{
+	if (texType == TEXTURE_2D)
+		return 1;
+	else if (texType == TEXTURE_CUBE)
+		return 6;
 	else if (texType == TEXTURE_2D_ARRAY)
 	else if (texType == TEXTURE_2D_ARRAY)
-		return slice < layers;
-	else if (slice > 0)
-		return false;
-
-	return true;
+		return layers;
+	else if (texType == TEXTURE_VOLUME)
+		return getDepth(mip);
+	return 1;
 }
 }
 
 
 int Texture::getWidth(int mip) const
 int Texture::getWidth(int mip) const

+ 4 - 1
src/modules/graphics/Texture.h

@@ -264,7 +264,10 @@ public:
 	bool isCompressed() const;
 	bool isCompressed() const;
 	bool isFormatLinear() const;
 	bool isFormatLinear() const;
 
 
-	bool isValidSlice(int slice) const;
+	bool isValidSlice(int slice, int mip) const;
+
+	// Number of array layers, cube faces, or volume layers for the given mip.
+	int getSliceCount(int mip) const;
 
 
 	int getWidth(int mip = 0) const;
 	int getWidth(int mip = 0) const;
 	int getHeight(int mip = 0) const;
 	int getHeight(int mip = 0) const;