GPUDevice: Fix VRAM usage calculation for compressed textures

This commit is contained in:
Stenzek 2024-11-25 18:44:33 +10:00
parent 982dccb990
commit 4a650fcce2
No known key found for this signature in database
2 changed files with 38 additions and 16 deletions

View File

@ -68,7 +68,7 @@ u32 GPUTexture::GetBlockSize() const
u32 GPUTexture::GetBlockSize(Format format) u32 GPUTexture::GetBlockSize(Format format)
{ {
if (format >= Format::BC1 && format <= Format::BC7) if (format >= Format::BC1 && format <= Format::BC7)
return 4; return COMPRESSED_TEXTURE_BLOCK_SIZE;
else else
return 1; return 1;
} }
@ -77,7 +77,7 @@ u32 GPUTexture::CalcUploadPitch(Format format, u32 width)
{ {
// convert to blocks // convert to blocks
if (format >= Format::BC1 && format <= Format::BC7) if (format >= Format::BC1 && format <= Format::BC7)
width = Common::AlignUpPow2(width, 4) / 4; width = Common::AlignUpPow2(width, COMPRESSED_TEXTURE_BLOCK_SIZE) / COMPRESSED_TEXTURE_BLOCK_SIZE;
return width * GetPixelSize(format); return width * GetPixelSize(format);
} }
@ -96,7 +96,7 @@ u32 GPUTexture::CalcUploadRowLengthFromPitch(Format format, u32 pitch)
{ {
const u32 pixel_size = GetPixelSize(format); const u32 pixel_size = GetPixelSize(format);
if (IsCompressedFormat(format)) if (IsCompressedFormat(format))
return (Common::AlignUpPow2(pitch, pixel_size) / pixel_size) * 4; return (Common::AlignUpPow2(pitch, pixel_size) / pixel_size) * COMPRESSED_TEXTURE_BLOCK_SIZE;
else else
return pitch / pixel_size; return pitch / pixel_size;
} }
@ -133,8 +133,8 @@ void GPUTexture::CopyTextureDataForUpload(u32 width, u32 height, Format format,
{ {
if (IsCompressedFormat(format)) if (IsCompressedFormat(format))
{ {
const u32 blocks_wide = Common::AlignUpPow2(width, 4) / 4; const u32 blocks_wide = Common::AlignUpPow2(width, COMPRESSED_TEXTURE_BLOCK_SIZE) / COMPRESSED_TEXTURE_BLOCK_SIZE;
const u32 blocks_high = Common::AlignUpPow2(height, 4) / 4; const u32 blocks_high = Common::AlignUpPow2(height, COMPRESSED_TEXTURE_BLOCK_SIZE) / COMPRESSED_TEXTURE_BLOCK_SIZE;
const u32 block_size = GetPixelSize(format); const u32 block_size = GetPixelSize(format);
StringUtil::StrideMemCpy(dst, dst_pitch, src, src_pitch, block_size * blocks_wide, blocks_high); StringUtil::StrideMemCpy(dst, dst_pitch, src, src_pitch, block_size * blocks_wide, blocks_high);
} }
@ -208,21 +208,41 @@ std::array<float, 4> GPUTexture::GetUNormClearColor() const
size_t GPUTexture::GetVRAMUsage() const size_t GPUTexture::GetVRAMUsage() const
{ {
if (m_levels == 1) [[likely]]
return ((static_cast<size_t>(m_width * m_height) * GetPixelSize(m_format)) * m_layers * m_samples);
const size_t ps = GetPixelSize(m_format) * m_layers * m_samples; const size_t ps = GetPixelSize(m_format) * m_layers * m_samples;
u32 width = m_width; size_t mem;
u32 height = m_height;
size_t ts = 0; // Max width/height is 65535, 65535*65535 as u32 is okay.
for (u32 i = 0; i < m_levels; i++) if (IsCompressedFormat())
{
#define COMPRESSED_SIZE(width, height) \
(static_cast<size_t>((Common::AlignUpPow2(width, COMPRESSED_TEXTURE_BLOCK_SIZE) / COMPRESSED_TEXTURE_BLOCK_SIZE) * \
(Common::AlignUpPow2(height, COMPRESSED_TEXTURE_BLOCK_SIZE) / COMPRESSED_TEXTURE_BLOCK_SIZE)) * \
ps)
u32 width = m_width, height = m_height;
mem = COMPRESSED_SIZE(width, height);
for (u32 i = 1; i < m_levels; i++)
{ {
width = (width > 1) ? (width / 2) : width; width = (width > 1) ? (width / 2) : width;
height = (height > 1) ? (height / 2) : height; height = (height > 1) ? (height / 2) : height;
ts += static_cast<size_t>(width * height) * ps; mem += COMPRESSED_SIZE(width, height);
} }
return ts; #undef COMPRESSED_SIZE
}
else
{
u32 width = m_width, height = m_height;
mem = static_cast<size_t>(width * height) * ps;
for (u32 i = 1; i < m_levels; i++)
{
width = (width > 1) ? (width / 2) : width;
height = (height > 1) ? (height / 2) : height;
mem += static_cast<size_t>(width * height) * ps;
}
}
return mem;
} }
u32 GPUTexture::GetPixelSize(GPUTexture::Format format) u32 GPUTexture::GetPixelSize(GPUTexture::Format format)

View File

@ -188,6 +188,8 @@ public:
protected: protected:
GPUTexture(u16 width, u16 height, u8 layers, u8 levels, u8 samples, Type type, Format format, Flags flags); GPUTexture(u16 width, u16 height, u8 layers, u8 levels, u8 samples, Type type, Format format, Flags flags);
static constexpr u32 COMPRESSED_TEXTURE_BLOCK_SIZE = 4;
u16 m_width = 0; u16 m_width = 0;
u16 m_height = 0; u16 m_height = 0;
u8 m_layers = 0; u8 m_layers = 0;