Skip to content

Commit

Permalink
GPUDevice: Fix VRAM usage calculation for compressed textures
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Nov 25, 2024
1 parent 982dccb commit 4a650fc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
52 changes: 36 additions & 16 deletions src/util/gpu_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ u32 GPUTexture::GetBlockSize() const
u32 GPUTexture::GetBlockSize(Format format)
{
if (format >= Format::BC1 && format <= Format::BC7)
return 4;
return COMPRESSED_TEXTURE_BLOCK_SIZE;
else
return 1;
}
Expand All @@ -77,7 +77,7 @@ u32 GPUTexture::CalcUploadPitch(Format format, u32 width)
{
// convert to blocks
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);
}
Expand All @@ -96,7 +96,7 @@ u32 GPUTexture::CalcUploadRowLengthFromPitch(Format format, u32 pitch)
{
const u32 pixel_size = GetPixelSize(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
return pitch / pixel_size;
}
Expand Down Expand Up @@ -133,8 +133,8 @@ void GPUTexture::CopyTextureDataForUpload(u32 width, u32 height, Format format,
{
if (IsCompressedFormat(format))
{
const u32 blocks_wide = Common::AlignUpPow2(width, 4) / 4;
const u32 blocks_high = Common::AlignUpPow2(height, 4) / 4;
const u32 blocks_wide = Common::AlignUpPow2(width, COMPRESSED_TEXTURE_BLOCK_SIZE) / COMPRESSED_TEXTURE_BLOCK_SIZE;
const u32 blocks_high = Common::AlignUpPow2(height, COMPRESSED_TEXTURE_BLOCK_SIZE) / COMPRESSED_TEXTURE_BLOCK_SIZE;
const u32 block_size = GetPixelSize(format);
StringUtil::StrideMemCpy(dst, dst_pitch, src, src_pitch, block_size * blocks_wide, blocks_high);
}
Expand Down Expand Up @@ -208,21 +208,41 @@ std::array<float, 4> GPUTexture::GetUNormClearColor() 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;
u32 width = m_width;
u32 height = m_height;
size_t ts = 0;
for (u32 i = 0; i < m_levels; i++)
size_t mem;

// Max width/height is 65535, 65535*65535 as u32 is okay.
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;
height = (height > 1) ? (height / 2) : height;
mem += COMPRESSED_SIZE(width, height);
}

#undef COMPRESSED_SIZE
}
else
{
width = (width > 1) ? (width / 2) : width;
height = (height > 1) ? (height / 2) : height;
ts += static_cast<size_t>(width * height) * ps;
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 ts;
return mem;
}

u32 GPUTexture::GetPixelSize(GPUTexture::Format format)
Expand Down
2 changes: 2 additions & 0 deletions src/util/gpu_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ class GPUTexture
protected:
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_height = 0;
u8 m_layers = 0;
Expand Down

0 comments on commit 4a650fc

Please sign in to comment.