Add gfx memory usage utilities
This commit is contained in:
parent
6d5a01921c
commit
696ed71090
5 changed files with 103 additions and 0 deletions
|
|
@ -180,4 +180,10 @@ namespace psemek::gfx
|
||||||
using buffer = basic_buffer<gl::ARRAY_BUFFER>;
|
using buffer = basic_buffer<gl::ARRAY_BUFFER>;
|
||||||
using uniform_buffer = basic_buffer<gl::UNIFORM_BUFFER>;
|
using uniform_buffer = basic_buffer<gl::UNIFORM_BUFFER>;
|
||||||
|
|
||||||
|
template <GLenum Target>
|
||||||
|
std::size_t memory_usage(basic_buffer<Target> const & buffer)
|
||||||
|
{
|
||||||
|
return buffer.size();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -363,4 +363,9 @@ namespace psemek::gfx
|
||||||
|
|
||||||
imported_mesh load_mesh(std::string_view data);
|
imported_mesh load_mesh(std::string_view data);
|
||||||
|
|
||||||
|
inline std::size_t memory_usage(mesh const & mesh)
|
||||||
|
{
|
||||||
|
return memory_usage(mesh.vertex_buffer()) + memory_usage(mesh.index_buffer()) + memory_usage(mesh.instance_buffer());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -453,4 +453,6 @@ namespace psemek::gfx
|
||||||
static constexpr GLenum type = gl::UNSIGNED_BYTE;
|
static constexpr GLenum type = gl::UNSIGNED_BYTE;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::size_t pixel_size(GLint internal_format);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,8 @@ namespace psemek::gfx
|
||||||
void bind() const;
|
void bind() const;
|
||||||
void bind(int texture_unit) const;
|
void bind(int texture_unit) const;
|
||||||
|
|
||||||
|
GLint internal_format() const { return format_; }
|
||||||
|
|
||||||
geom::vector<std::size_t, D> size() const { return size_; }
|
geom::vector<std::size_t, D> size() const { return size_; }
|
||||||
|
|
||||||
std::size_t width() const;
|
std::size_t width() const;
|
||||||
|
|
@ -82,6 +84,7 @@ namespace psemek::gfx
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
GLuint id_ = 0;
|
GLuint id_ = 0;
|
||||||
|
GLint format_ = 0;
|
||||||
geom::vector<std::size_t, D> size_ = geom::vector<std::size_t, D>::zero();
|
geom::vector<std::size_t, D> size_ = geom::vector<std::size_t, D>::zero();
|
||||||
|
|
||||||
explicit basic_texture(std::nullptr_t);
|
explicit basic_texture(std::nullptr_t);
|
||||||
|
|
@ -250,9 +253,11 @@ namespace psemek::gfx
|
||||||
template <std::size_t D, GLenum Target>
|
template <std::size_t D, GLenum Target>
|
||||||
basic_texture<D, Target>::basic_texture(basic_texture && other)
|
basic_texture<D, Target>::basic_texture(basic_texture && other)
|
||||||
: id_{other.id_}
|
: id_{other.id_}
|
||||||
|
, format_{other.format_}
|
||||||
, size_{other.size_}
|
, size_{other.size_}
|
||||||
{
|
{
|
||||||
other.id_ = 0;
|
other.id_ = 0;
|
||||||
|
other.format_ = 0;
|
||||||
other.size_ = other.size_.zero();
|
other.size_ = other.size_.zero();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -263,6 +268,7 @@ namespace psemek::gfx
|
||||||
|
|
||||||
reset();
|
reset();
|
||||||
std::swap(id_, other.id_);
|
std::swap(id_, other.id_);
|
||||||
|
std::swap(format_, other.format_);
|
||||||
std::swap(size_, other.size_);
|
std::swap(size_, other.size_);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
@ -285,6 +291,7 @@ namespace psemek::gfx
|
||||||
if (id_ != 0)
|
if (id_ != 0)
|
||||||
gl::DeleteTextures(1, &id_);
|
gl::DeleteTextures(1, &id_);
|
||||||
id_ = 0;
|
id_ = 0;
|
||||||
|
format_ = 0;
|
||||||
size_ = size_.zero();
|
size_ = size_.zero();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -351,6 +358,7 @@ namespace psemek::gfx
|
||||||
gl::TexImage3D(Target, 0, internal_format, size[0], size[1], size[2], 0, format, type, data);
|
gl::TexImage3D(Target, 0, internal_format, size[0], size[1], size[2], 0, format, type, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
format_ = internal_format;
|
||||||
size_ = size;
|
size_ = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -598,4 +606,10 @@ namespace psemek::gfx
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <std::size_t D, GLenum Target>
|
||||||
|
std::size_t memory_usage(basic_texture<D, Target> const & texture)
|
||||||
|
{
|
||||||
|
return texture.width() * texture.height() * texture.depth() * pixel_size(texture.internal_format());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
76
libs/gfx/source/pixel.cpp
Normal file
76
libs/gfx/source/pixel.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
#include <psemek/gfx/pixel.hpp>
|
||||||
|
#include <psemek/util/to_string.hpp>
|
||||||
|
|
||||||
|
namespace psemek::gfx
|
||||||
|
{
|
||||||
|
|
||||||
|
std::size_t pixel_size(GLint internal_format)
|
||||||
|
{
|
||||||
|
switch (internal_format)
|
||||||
|
{
|
||||||
|
case gl::RGBA32F: return 16;
|
||||||
|
case gl::RGBA32I: return 16;
|
||||||
|
case gl::RGBA32UI: return 16;
|
||||||
|
case gl::RGBA16: return 8;
|
||||||
|
case gl::RGBA16F: return 8;
|
||||||
|
case gl::RGBA16I: return 8;
|
||||||
|
case gl::RGBA16UI: return 8;
|
||||||
|
case gl::RGBA8: return 4;
|
||||||
|
case gl::RGBA8UI: return 4;
|
||||||
|
case gl::SRGB8_ALPHA8: return 4;
|
||||||
|
case gl::RGB10_A2: return 4;
|
||||||
|
case gl::RGB10_A2UI: return 4;
|
||||||
|
case gl::R11F_G11F_B10F: return 4;
|
||||||
|
case gl::RG32F: return 8;
|
||||||
|
case gl::RG32I: return 8;
|
||||||
|
case gl::RG32UI: return 8;
|
||||||
|
case gl::RG16: return 4;
|
||||||
|
case gl::RG16F: return 4;
|
||||||
|
case gl::RGB16I: return 4;
|
||||||
|
case gl::RGB16UI: return 4;
|
||||||
|
case gl::RG8: return 2;
|
||||||
|
case gl::RG8I: return 2;
|
||||||
|
case gl::RG8UI: return 2;
|
||||||
|
case gl::R32F: return 4;
|
||||||
|
case gl::R32I: return 4;
|
||||||
|
case gl::R32UI: return 4;
|
||||||
|
case gl::R16F: return 2;
|
||||||
|
case gl::R16I: return 2;
|
||||||
|
case gl::R16UI: return 2;
|
||||||
|
case gl::R8: return 1;
|
||||||
|
case gl::R8I: return 1;
|
||||||
|
case gl::R8UI: return 1;
|
||||||
|
case gl::RGBA16_SNORM: return 8;
|
||||||
|
case gl::RGBA8_SNORM: return 4;
|
||||||
|
case gl::RGB32F: return 12;
|
||||||
|
case gl::RGB32I: return 12;
|
||||||
|
case gl::RGB32UI: return 12;
|
||||||
|
case gl::RGB16_SNORM: return 6;
|
||||||
|
case gl::RGB16F: return 6;
|
||||||
|
case gl::RGB16: return 6;
|
||||||
|
case gl::RGB8_SNORM: return 3;
|
||||||
|
case gl::RGB8: return 3;
|
||||||
|
case gl::RGB8I: return 3;
|
||||||
|
case gl::RGB8UI: return 3;
|
||||||
|
case gl::SRGB8: return 3;
|
||||||
|
case gl::RGB9_E5: return 4;
|
||||||
|
case gl::RG16_SNORM: return 4;
|
||||||
|
case gl::RG8_SNORM: return 2;
|
||||||
|
case gl::R16_SNORM: return 2;
|
||||||
|
case gl::R8_SNORM: return 1;
|
||||||
|
case gl::DEPTH_COMPONENT32F: return 4;
|
||||||
|
case gl::DEPTH_COMPONENT24: return 3;
|
||||||
|
case gl::DEPTH_COMPONENT16: return 2;
|
||||||
|
case gl::DEPTH32F_STENCIL8: return 5;
|
||||||
|
case gl::DEPTH24_STENCIL8: return 4;
|
||||||
|
// Assume uncompressed size as an upper bound
|
||||||
|
case gl::COMPRESSED_RG_RGTC2: return 2;
|
||||||
|
case gl::COMPRESSED_SIGNED_RG_RGTC2: return 2;
|
||||||
|
case gl::COMPRESSED_RED_RGTC1: return 1;
|
||||||
|
case gl::COMPRESSED_SIGNED_RED_RGTC1: return 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
throw std::runtime_error(util::to_string("unknown pixel format: 0x", std::hex, internal_format));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue