metal: fix texture memory tracking

This commit is contained in:
Sasha Szpakowski
2024-07-16 17:44:58 -03:00
parent 679f3bdaa9
commit 304bf8bb83
5 changed files with 35 additions and 45 deletions
+26 -5
View File
@@ -450,7 +450,7 @@ Texture::Texture(Graphics *gfx, Texture *base, const ViewSettings &viewsettings)
Texture::~Texture() Texture::~Texture()
{ {
setGraphicsMemorySize(0); updateGraphicsMemorySize(false);
if (this == rootView.texture) if (this == rootView.texture)
--textureCount; --textureCount;
@@ -461,13 +461,34 @@ Texture::~Texture()
parentView.texture->release(); 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); totalGraphicsMemory = std::max(totalGraphicsMemory - graphicsMemorySize, (int64) 0);
bytes = std::max(bytes, (int64) 0); memsize = std::max(memsize, (int64) 0);
graphicsMemorySize = bytes; graphicsMemorySize = memsize;
totalGraphicsMemory += bytes; totalGraphicsMemory += memsize;
} }
void Texture::draw(Graphics *gfx, const Matrix4 &m) void Texture::draw(Graphics *gfx, const Matrix4 &m)
+1 -1
View File
@@ -337,7 +337,7 @@ protected:
Texture(Graphics *gfx, Texture *base, const ViewSettings &viewsettings); Texture(Graphics *gfx, Texture *base, const ViewSettings &viewsettings);
virtual ~Texture(); virtual ~Texture();
void setGraphicsMemorySize(int64 size); void updateGraphicsMemorySize(bool loaded);
void uploadImageData(love::image::ImageDataBase *d, int level, int slice, int x, int y); 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; virtual void uploadByteData(const void *data, size_t size, int level, int slice, const Rect &r) = 0;
+2
View File
@@ -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), // Non-readable textures can't have mipmaps (enforced in the base class),
// so generateMipmaps here is fine - when they aren't already initialized. // so generateMipmaps here is fine - when they aren't already initialized.
if (shouldgeneratemips) if (shouldgeneratemips)
+3 -21
View File
@@ -426,26 +426,6 @@ bool Texture::loadVolatile()
return false; 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 (!debugName.empty() && (GLAD_VERSION_4_3 || GLAD_ES_VERSION_3_2))
{ {
if (texture) if (texture)
@@ -460,6 +440,8 @@ bool Texture::loadVolatile()
} }
} }
updateGraphicsMemorySize(true);
return true; return true;
} }
@@ -487,7 +469,7 @@ void Texture::unloadVolatile()
renderbuffer = 0; renderbuffer = 0;
texture = 0; texture = 0;
setGraphicsMemorySize(0); updateGraphicsMemorySize(false);
} }
void Texture::uploadByteData(const void *data, size_t size, int level, int slice, const Rect &r) void Texture::uploadByteData(const void *data, size_t size, int level, int slice, const Rect &r)
+3 -18
View File
@@ -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 (!debugName.empty())
{ {
if (vgfx->getEnabledOptionalInstanceExtensions().debugInfo) if (vgfx->getEnabledOptionalInstanceExtensions().debugInfo)
@@ -275,6 +258,8 @@ bool Texture::loadVolatile()
} }
} }
updateGraphicsMemorySize(true);
return true; return true;
} }
@@ -301,7 +286,7 @@ void Texture::unloadVolatile()
textureImage = VK_NULL_HANDLE; textureImage = VK_NULL_HANDLE;
textureImageAllocation = VK_NULL_HANDLE; textureImageAllocation = VK_NULL_HANDLE;
setGraphicsMemorySize(0); updateGraphicsMemorySize(false);
} }
Texture::~Texture() Texture::~Texture()