Refactor mesh vertex size check

This commit is contained in:
Nikita Lisitsa 2020-10-02 19:13:17 +03:00
parent 9e79bd1c40
commit d4a7611ee5
2 changed files with 26 additions and 8 deletions

View file

@ -384,6 +384,9 @@ namespace psemek::gfx
template <typename T> template <typename T>
static constexpr GLenum gl_type_v = gl_type<T>::value; static constexpr GLenum gl_type_v = gl_type<T>::value;
void check_vertex_size(std::size_t vertex_size, std::size_t stride);
void check_instance_size(std::size_t instance_size, std::size_t stride);
} }
struct mesh struct mesh
@ -412,8 +415,7 @@ namespace psemek::gfx
template <typename Vertex> template <typename Vertex>
void load(Vertex const * vertices, std::size_t count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW) void load(Vertex const * vertices, std::size_t count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW)
{ {
if (sizeof(Vertex) != stride_) detail::check_vertex_size(sizeof(Vertex), stride_);
throw std::runtime_error("Vertex size not equal to sum of attribute sizes");
switch (primitive_type) switch (primitive_type)
{ {
@ -532,8 +534,7 @@ namespace psemek::gfx
template <typename Vertex, typename Index> template <typename Vertex, typename Index>
void load(Vertex const * vertices, std::size_t vertex_count, Index const * indices, std::size_t index_count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW) void load(Vertex const * vertices, std::size_t vertex_count, Index const * indices, std::size_t index_count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW)
{ {
if (sizeof(Vertex) != stride_) detail::check_vertex_size(sizeof(Vertex), stride_);
throw std::runtime_error("Vertex size not equal to sum of attribute sizes");
switch (primitive_type) switch (primitive_type)
{ {
@ -658,8 +659,7 @@ namespace psemek::gfx
template <typename Vertex> template <typename Vertex>
void load(Vertex const * vertices, std::size_t count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW) void load(Vertex const * vertices, std::size_t count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW)
{ {
if (sizeof(Vertex) != stride_) detail::check_vertex_size(sizeof(Vertex), stride_);
throw std::runtime_error("Vertex size not equal to sum of attribute sizes");
switch (primitive_type) switch (primitive_type)
{ {
@ -722,8 +722,7 @@ namespace psemek::gfx
template <typename Instance> template <typename Instance>
void load_instance(Instance const * instances, std::size_t count, GLenum usage = gl::STREAM_DRAW) void load_instance(Instance const * instances, std::size_t count, GLenum usage = gl::STREAM_DRAW)
{ {
if (sizeof(Instance) != instance_stride_) detail::check_instance_size(sizeof(Instance), instance_stride_);
throw std::runtime_error("Instance size not equal to sum of instanced attribute sizes");
gl::BindBuffer(gl::ARRAY_BUFFER, instance_buffer_); gl::BindBuffer(gl::ARRAY_BUFFER, instance_buffer_);
gl::BufferData(gl::ARRAY_BUFFER, count * sizeof(Instance), instances, usage); gl::BufferData(gl::ARRAY_BUFFER, count * sizeof(Instance), instances, usage);

View file

@ -1,8 +1,27 @@
#include <psemek/gfx/mesh.hpp> #include <psemek/gfx/mesh.hpp>
#include <psemek/util/to_string.hpp>
namespace psemek::gfx namespace psemek::gfx
{ {
namespace detail
{
void check_vertex_size(std::size_t vertex_size, std::size_t stride)
{
if (vertex_size != stride)
throw std::runtime_error(util::to_string("Vertex size (", vertex_size, ") not equal to sum of attribute sizes (", stride, ")"));
}
void check_instance_size(std::size_t instance_size, std::size_t stride)
{
if (instance_size != stride)
throw std::runtime_error(util::to_string("Instance size (", instance_size, ") not equal to sum of instanced attribute sizes (", stride, ")"));
}
}
mesh mesh::null() mesh mesh::null()
{ {
return mesh(0); return mesh(0);