psemek/libs/wgpu/source/texture.cpp

200 lines
8.3 KiB
C++

#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
{
texture_view texture::create_view(texture_view::descriptor const & desc)
{
WGPUTextureViewDescriptor descriptor = {};
descriptor.nextInChain = (WGPUChainedStruct const *)detail::fill_chain(desc.chain);
descriptor.label = detail::to_string_view(desc.label);
descriptor.format = (WGPUTextureFormat)desc.format;
descriptor.dimension = (WGPUTextureViewDimension)desc.dimension;
descriptor.baseMipLevel = desc.base_mip_level;
descriptor.mipLevelCount = desc.mip_level_count;
descriptor.baseArrayLayer = desc.base_array_layer;
descriptor.arrayLayerCount = desc.array_layer_count;
descriptor.aspect = (WGPUTextureAspect)desc.aspect;
return texture_view(wgpuTextureCreateView((WGPUTexture)get(), &descriptor));
}
void texture::destroy()
{
wgpuTextureDestroy((WGPUTexture)get());
}
std::uint32_t texture::get_width() const
{
return wgpuTextureGetWidth((WGPUTexture)get());
}
std::uint32_t texture::get_height() const
{
return wgpuTextureGetHeight((WGPUTexture)get());
}
std::uint32_t texture::get_depth() const
{
return wgpuTextureGetDepthOrArrayLayers((WGPUTexture)get());
}
texture::dimension texture::get_dimension() const
{
return (dimension)wgpuTextureGetDimension((WGPUTexture)get());
}
texture::format texture::get_format() const
{
return (format)wgpuTextureGetFormat((WGPUTexture)get());
}
std::uint32_t texture::get_mip_level_count() const
{
return wgpuTextureGetMipLevelCount((WGPUTexture)get());
}
texture::usage texture::get_usage() const
{
return (usage)wgpuTextureGetUsage((WGPUTexture)get());
}
void texture::set_label(std::string const & label)
{
wgpuTextureSetLabel((WGPUTexture)get(), detail::to_string_view(label));
}
void texture::reference(void * ptr)
{
wgpuTextureAddRef((WGPUTexture)ptr);
}
void texture::release(void * ptr)
{
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());
}
}