Add wgpu::memory_usage for textures and texture formats

This commit is contained in:
Nikita Lisitsa 2026-05-11 13:47:35 +03:00
parent 074cbb18ba
commit 0731dd9216
2 changed files with 124 additions and 0 deletions

View file

@ -220,4 +220,7 @@ namespace psemek::wgpu
return static_cast<texture::usage>(static_cast<unsigned int>(u1) | static_cast<unsigned int>(u2));
}
std::size_t memory_usage(std::uint32_t width, std::uint32_t height, std::uint32_t depth, texture::format format);
std::size_t memory_usage(texture const & texture);
}

View file

@ -1,6 +1,7 @@
#include <psemek/wgpu/texture.hpp>
#include <psemek/wgpu/detail/string_view.hpp>
#include <psemek/wgpu/external/webgpu.h>
#include <psemek/util/enum.hpp>
namespace psemek::wgpu
{
@ -76,4 +77,124 @@ namespace psemek::wgpu
wgpuTextureRelease((WGPUTexture)ptr);
}
std::size_t memory_usage(std::uint32_t width, std::uint32_t height, std::uint32_t depth, texture::format format)
{
std::size_t const pixels = width * height * depth;
switch (format)
{
case texture::format::undefined: throw util::unknown_enum_value_exception(format);
case texture::format::r8unorm: return 1 * pixels;
case texture::format::r8snorm: return 1 * pixels;
case texture::format::r8uint: return 1 * pixels;
case texture::format::r8sint: return 1 * pixels;
case texture::format::r16uint: return 2 * pixels;
case texture::format::r16sint: return 2 * pixels;
case texture::format::r16float: return 2 * pixels;
case texture::format::rg8unorm: return 2 * pixels;
case texture::format::rg8snorm: return 2 * pixels;
case texture::format::rg8uint: return 2 * pixels;
case texture::format::rg8sint: return 2 * pixels;
case texture::format::r32float: return 4 * pixels;
case texture::format::r32uint: return 4 * pixels;
case texture::format::r32sint: return 4 * pixels;
case texture::format::rg16uint: return 4 * pixels;
case texture::format::rg16sint: return 4 * pixels;
case texture::format::rg16float: return 4 * pixels;
case texture::format::rgba8unorm: return 4 * pixels;
case texture::format::rgba8unorm_srgb: return 4 * pixels;
case texture::format::rgba8snorm: return 4 * pixels;
case texture::format::rgba8uint: return 4 * pixels;
case texture::format::rgba8sint: return 4 * pixels;
case texture::format::bgra8unorm: return 4 * pixels;
case texture::format::bgra8unorm_srgb: return 4 * pixels;
case texture::format::rgb10a2uint: return 4 * pixels;
case texture::format::rgb10a2unorm: return 4 * pixels;
case texture::format::rg11b10ufloat: return 4 * pixels;
case texture::format::rgb9e5ufloat: return 4 * pixels;
case texture::format::rg32float: return 8 * pixels;
case texture::format::rg32uint: return 8 * pixels;
case texture::format::rg32sint: return 8 * pixels;
case texture::format::rgba16uint: return 8 * pixels;
case texture::format::rgba16sint: return 8 * pixels;
case texture::format::rgba16float: return 8 * pixels;
case texture::format::rgba32float: return 16 * pixels;
case texture::format::rgba32uint: return 16 * pixels;
case texture::format::rgba32sint: return 16 * pixels;
case texture::format::stencil8: return 1 * pixels;
case texture::format::depth16unorm: return 2 * pixels;
case texture::format::depth24plus: return 3 * pixels;
case texture::format::depth24plusstencil8: return 4 * pixels;
case texture::format::depth32float: return 4 * pixels;
case texture::format::depth32floatstencil8: return 5 * pixels;
case texture::format::bc1rgbaunorm: return pixels / 2;
case texture::format::bc1rgbaunorm_srgb: return pixels / 2;
case texture::format::bc2rgbaunorm: return pixels;
case texture::format::bc2rgbaunorm_srgb: return pixels;
case texture::format::bc3rgbaunorm: return pixels;
case texture::format::bc3rgbaunorm_srgb: return pixels;
case texture::format::bc4runorm: return pixels / 2;
case texture::format::bc4rsnorm: return pixels / 2;
case texture::format::bc5rgunorm: return pixels;
case texture::format::bc5rgsnorm: return pixels;
case texture::format::bc6hrgbufloat: return pixels;
case texture::format::bc6hrgbfloat: return pixels;
case texture::format::bc7rgbaunorm: return pixels;
case texture::format::bc7rgbaunorm_srgb: return pixels;
case texture::format::etc2rgb8unorm: return pixels / 2;
case texture::format::etc2rgb8unorm_srgb: return pixels / 2;
case texture::format::etc2rgb8a1unorm: return pixels / 2;
case texture::format::etc2rgb8a1unorm_srgb: return pixels / 2;
case texture::format::etc2rgba8unorm: return pixels;
case texture::format::etc2rgba8unorm_srgb: return pixels;
case texture::format::eacr11unorm: return pixels / 2;
case texture::format::eacr11snorm: return pixels / 2;
case texture::format::eacrg11unorm: return pixels;
case texture::format::eacrg11snorm: return pixels;
case texture::format::astc4x4unorm: return pixels;
case texture::format::astc4x4unorm_srgb: return pixels;
case texture::format::astc5x4unorm: return pixels * 0.8f;
case texture::format::astc5x4unorm_srgb: return pixels * 0.8f;
case texture::format::astc5x5unorm: return pixels * 0.64f;
case texture::format::astc5x5unorm_srgb: return pixels * 0.64f;
case texture::format::astc6x5unorm: return pixels * 0.53375f;
case texture::format::astc6x5unorm_srgb: return pixels * 0.53375f;
case texture::format::astc6x6unorm: return pixels * 0.445f;
case texture::format::astc6x6unorm_srgb: return pixels * 0.445f;
case texture::format::astc8x5unorm: return pixels * 0.4f;
case texture::format::astc8x5unorm_srgb: return pixels * 0.4f;
case texture::format::astc8x6unorm: return pixels * 0.33375f;
case texture::format::astc8x6unorm_srgb: return pixels * 0.33375f;
case texture::format::astc8x8unorm: return pixels * 0.25f;
case texture::format::astc8x8unorm_srgb: return pixels * 0.25f;
case texture::format::astc10x5unorm: return pixels * 0.32f;
case texture::format::astc10x5unorm_srgb: return pixels * 0.32f;
case texture::format::astc10x6unorm: return pixels * 0.26625f;
case texture::format::astc10x6unorm_srgb: return pixels * 0.26625f;
case texture::format::astc10x8unorm: return pixels * 0.2f;
case texture::format::astc10x8unorm_srgb: return pixels * 0.2f;
case texture::format::astc10x10unorm: return pixels * 0.16f;
case texture::format::astc10x10unorm_srgb: return pixels * 0.16f;
case texture::format::astc12x10unorm: return pixels * 0.13375f;
case texture::format::astc12x10unorm_srgb: return pixels * 0.13375f;
case texture::format::astc12x12unorm: return pixels * 0.11125f;
case texture::format::astc12x12unorm_srgb: return pixels * 0.11125f;
case texture::format::r16unorm: return 2 * pixels;
case texture::format::r16snorm: return 2 * pixels;
case texture::format::rg16unorm: return 4 * pixels;
case texture::format::rg16snorm: return 4 * pixels;
case texture::format::rgba16unorm: return 8 * pixels;
case texture::format::rgba16snorm: return 8 * pixels;
case texture::format::nv12: return pixels * 1.5f;
case texture::format::p010: return pixels * 1.5f;
}
throw util::unknown_enum_value_exception(format);
}
std::size_t memory_usage(texture const & texture)
{
return memory_usage(texture.get_width(), texture.get_height(), texture.get_depth(), texture.get_format());
}
}