Browse Source

metal: fix texture memory tracking

Sasha Szpakowski 1 year ago
parent
commit
304bf8bb83

+ 26 - 5
src/modules/graphics/Texture.cpp

@@ -450,7 +450,7 @@ Texture::Texture(Graphics *gfx, Texture *base, const ViewSettings &viewsettings)
 
 Texture::~Texture()
 {
-	setGraphicsMemorySize(0);
+	updateGraphicsMemorySize(false);
 
 	if (this == rootView.texture)
 		--textureCount;
@@ -461,13 +461,34 @@ Texture::~Texture()
 		parentView.texture->release();
 }
 
-void Texture::setGraphicsMemorySize(int64 bytes)
+void Texture::updateGraphicsMemorySize(bool loaded)
 {
+	int64 memsize = 0;
+
+	if (loaded)
+	{
+		for (int mip = 0; mip < getMipmapCount(); mip++)
+		{
+			int w = getPixelWidth(mip);
+			int h = getPixelHeight(mip);
+			int slices = getDepth(mip) * layers * (texType == TEXTURE_CUBE ? 6 : 1);
+			memsize += getPixelFormatSliceSize(format, w, h) * slices;
+		}
+
+		if (getMSAA() > 1 && isReadable())
+		{
+			int slices = depth * layers * (texType == TEXTURE_CUBE ? 6 : 1);
+			memsize += getPixelFormatSliceSize(format, pixelWidth, pixelHeight) * slices * getMSAA();
+		}
+		else if (getMSAA() > 1)
+			memsize *= getMSAA();
+	}
+
 	totalGraphicsMemory = std::max(totalGraphicsMemory - graphicsMemorySize, (int64) 0);
 
-	bytes = std::max(bytes, (int64) 0);
-	graphicsMemorySize = bytes;
-	totalGraphicsMemory += bytes;
+	memsize = std::max(memsize, (int64) 0);
+	graphicsMemorySize = memsize;
+	totalGraphicsMemory += memsize;
 }
 
 void Texture::draw(Graphics *gfx, const Matrix4 &m)

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

@@ -337,7 +337,7 @@ protected:
 	Texture(Graphics *gfx, Texture *base, const ViewSettings &viewsettings);
 	virtual ~Texture();
 
-	void setGraphicsMemorySize(int64 size);
+	void updateGraphicsMemorySize(bool loaded);
 
 	void uploadImageData(love::image::ImageDataBase *d, int level, int slice, int x, int y);
 	virtual void uploadByteData(const void *data, size_t size, int level, int slice, const Rect &r) = 0;

+ 2 - 0
src/modules/graphics/metal/Texture.mm

@@ -209,6 +209,8 @@ Texture::Texture(love::graphics::Graphics *gfxbase, id<MTLDevice> device, const
 		}
 	}
 
+	updateGraphicsMemorySize(true);
+
 	// Non-readable textures can't have mipmaps (enforced in the base class),
 	// so generateMipmaps here is fine - when they aren't already initialized.
 	if (shouldgeneratemips)

+ 3 - 21
src/modules/graphics/opengl/Texture.cpp

@@ -426,26 +426,6 @@ bool Texture::loadVolatile()
 		return false;
 	}
 
-	int64 memsize = 0;
-
-	for (int mip = 0; mip < getMipmapCount(); mip++)
-	{
-		int w = getPixelWidth(mip);
-		int h = getPixelHeight(mip);
-		int slices = getDepth(mip) * layers * (texType == TEXTURE_CUBE ? 6 : 1);
-		memsize += getPixelFormatSliceSize(format, w, h) * slices;
-	}
-
-	if (actualSamples > 1 && isReadable())
-	{
-		int slices = depth * layers * (texType == TEXTURE_CUBE ? 6 : 1);
-		memsize += getPixelFormatSliceSize(format, pixelWidth, pixelHeight) * slices * actualSamples;
-	}
-	else if (actualSamples > 1)
-		memsize *= actualSamples;
-
-	setGraphicsMemorySize(memsize);
-
 	if (!debugName.empty() && (GLAD_VERSION_4_3 || GLAD_ES_VERSION_3_2))
 	{
 		if (texture)
@@ -460,6 +440,8 @@ bool Texture::loadVolatile()
 		}
 	}
 
+	updateGraphicsMemorySize(true);
+
 	return true;
 }
 
@@ -487,7 +469,7 @@ void Texture::unloadVolatile()
 	renderbuffer = 0;
 	texture = 0;
 
-	setGraphicsMemorySize(0);
+	updateGraphicsMemorySize(false);
 }
 
 void Texture::uploadByteData(const void *data, size_t size, int level, int slice, const Rect &r)

+ 3 - 18
src/modules/graphics/vulkan/Texture.cpp

@@ -237,23 +237,6 @@ bool Texture::loadVolatile()
 		}
 	}
 
-	int64 memsize = 0;
-
-	if (root)
-	{
-		for (int mip = 0; mip < getMipmapCount(); mip++)
-		{
-			int w = getPixelWidth(mip);
-			int h = getPixelHeight(mip);
-			int slices = getDepth(mip) * layerCount;
-			memsize += getPixelFormatSliceSize(format, w, h) * slices;
-		}
-
-		memsize *= static_cast<int>(msaaSamples);
-	}
-
-	setGraphicsMemorySize(memsize);
-
 	if (!debugName.empty())
 	{
 		if (vgfx->getEnabledOptionalInstanceExtensions().debugInfo)
@@ -275,6 +258,8 @@ bool Texture::loadVolatile()
 		}
 	}
 
+	updateGraphicsMemorySize(true);
+
 	return true;
 }
 
@@ -301,7 +286,7 @@ void Texture::unloadVolatile()
 	textureImage = VK_NULL_HANDLE;
 	textureImageAllocation = VK_NULL_HANDLE;
 
-	setGraphicsMemorySize(0);
+	updateGraphicsMemorySize(false);
 }
 
 Texture::~Texture()