From 59d13684be90b9f942e68b478ab60755b528c8f5 Mon Sep 17 00:00:00 2001 From: Roz K Date: Wed, 4 Jan 2023 12:41:05 +0100 Subject: [PATCH] Improve typing in render. --- cpp/render.hpp | 15 ++++++++++----- cpp/render/render_opengles.cpp | 23 +++++++++++------------ cpp/render/render_opengles.hpp | 22 +++++++++------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/cpp/render.hpp b/cpp/render.hpp index 0fb5f17..461a4b5 100644 --- a/cpp/render.hpp +++ b/cpp/render.hpp @@ -86,16 +86,21 @@ enum : rk_uint { typedef rk_ushort rk_vertex_index; typedef rk_ushort rk_mesh_index; -// param input types must be size compatible with an array of rk_param_input -typedef rk_uint rk_param_input; - -#define RK_CHECK_PARAM_INPUT_TYPE(_t) static_assert(!(sizeof(_t) % sizeof(rk_param_input))) - struct rk_mesh { rk_uint base_index; rk_uint ntriangles; }; +// param input types must be size compatible with an array of rk_param_input +typedef rk_uint rk_param_input; + +template +void rk_param_get_input_size(unsigned & _size, unsigned & _len) { + static_assert((sizeof(_input_type) % sizeof(rk_param_input)) == 0); + _size = sizeof(_input_type); + _len = sizeof(_input_type) / sizeof(rk_param_input); +} + RK_EXPORT void rk_render_initialize( rk_bool debug); diff --git a/cpp/render/render_opengles.cpp b/cpp/render/render_opengles.cpp index d16945a..39c87c3 100644 --- a/cpp/render/render_opengles.cpp +++ b/cpp/render/render_opengles.cpp @@ -13,6 +13,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +#include "../render.hpp" #include "render_opengles.hpp" #include "../display/display_glx.hpp" #include "../cmp_memcpy.hpp" @@ -688,8 +689,8 @@ rk_batch_t rk_create_batch( param->offset = offset; switch (*f & RK_PARAM_FORMAT_MASK) { case RK_PARAM_FORMAT_VEC3_FLOAT: - param->src_size = sizeof(rk_vec3); - param->dst_size = sizeof(rk_vec3_float); + rk_param_get_input_size(param->src_size, param->src_len); + rk_param_get_output_size(param->dst_size, param->dst_len); param->packer = rk_pack_vec3_float; glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size); glEnableVertexAttribArray(attrib); @@ -697,8 +698,8 @@ rk_batch_t rk_create_batch( glVertexAttribBinding(attrib++, binding); break; case RK_PARAM_FORMAT_VEC3_SHORT: - param->src_size = sizeof(rk_vec3); - param->dst_size = sizeof(rk_vec3_short); + rk_param_get_input_size(param->src_size, param->src_len); + rk_param_get_output_size(param->dst_size, param->dst_len); param->packer = norm ? rk_pack_vec3_short_norm : rk_pack_vec3_short; glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size); glEnableVertexAttribArray(attrib); @@ -706,8 +707,8 @@ rk_batch_t rk_create_batch( glVertexAttribBinding(attrib++, binding); break; case RK_PARAM_FORMAT_VEC3_INT10: - param->src_size = sizeof(rk_vec3); - param->dst_size = sizeof(rk_vec3_int10); + rk_param_get_input_size(param->src_size, param->src_len); + rk_param_get_output_size(param->dst_size, param->dst_len); param->packer = norm ? rk_pack_vec3_int10_norm : rk_pack_vec3_int10; glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size); glEnableVertexAttribArray(attrib); @@ -715,8 +716,8 @@ rk_batch_t rk_create_batch( glVertexAttribBinding(attrib++, binding); break; case RK_PARAM_FORMAT_MAT3_FLOAT: - param->src_size = sizeof(rk_mat3); - param->dst_size = sizeof(rk_mat3_float); + rk_param_get_input_size(param->src_size, param->src_len); + rk_param_get_output_size(param->dst_size, param->dst_len); param->packer = rk_pack_mat3_float; glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size); glEnableVertexAttribArray(attrib); @@ -730,8 +731,8 @@ rk_batch_t rk_create_batch( glVertexAttribBinding(attrib++, binding); break; case RK_PARAM_FORMAT_MAT3_INT10: - param->src_size = sizeof(rk_mat3); - param->dst_size = sizeof(rk_mat3_int10); + rk_param_get_input_size(param->src_size, param->src_len); + rk_param_get_output_size(param->dst_size, param->dst_len); param->packer = norm ? rk_pack_mat3_int10_norm : rk_pack_mat3_int10; glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size); glEnableVertexAttribArray(attrib); @@ -746,8 +747,6 @@ rk_batch_t rk_create_batch( break; } glVertexBindingDivisor(binding, 1); - param->src_len = param->src_size / sizeof(rk_param_input); - param->dst_len = param->dst_size / sizeof(rk_param_output); param->source = new rk_param_input[max_size * param->src_len]; memset(param->source, 0xFF, max_size * param->src_size); offset += max_size * param->dst_size; diff --git a/cpp/render/render_opengles.hpp b/cpp/render/render_opengles.hpp index 02997b5..1f725b0 100644 --- a/cpp/render/render_opengles.hpp +++ b/cpp/render/render_opengles.hpp @@ -16,7 +16,8 @@ #ifndef _RK_ENGINE_RENDER_OPENGLES_H #define _RK_ENGINE_RENDER_OPENGLES_H -#include "../render.hpp" +#include "../types.hpp" +#include "../math.hpp" #include #include #include @@ -61,7 +62,12 @@ struct rk_command { // param output types must be size compatible with an array of rk_param_output typedef rk_uint rk_param_output; -#define RK_CHECK_PARAM_OUTPUT_TYPE(_t) static_assert(!(sizeof(_t) % sizeof(rk_param_output))) +template +void rk_param_get_output_size(unsigned & _size, unsigned & _len) { + static_assert((sizeof(_output_type) % sizeof(rk_param_output)) == 0); + _size = sizeof(_output_type); + _len = sizeof(_output_type) / sizeof(rk_param_output); +} struct rk_vec3_float { float x; @@ -95,16 +101,6 @@ struct rk_mat3_int10 { rk_vec3_int10 z; }; -RK_CHECK_PARAM_INPUT_TYPE(rk_vec3_float); -RK_CHECK_PARAM_INPUT_TYPE(rk_mat3_float); - -RK_CHECK_PARAM_OUTPUT_TYPE(rk_vec3_float); -RK_CHECK_PARAM_OUTPUT_TYPE(rk_vec3_short); -RK_CHECK_PARAM_OUTPUT_TYPE(rk_vec3_int10); -RK_CHECK_PARAM_OUTPUT_TYPE(rk_vec3_uint10); -RK_CHECK_PARAM_OUTPUT_TYPE(rk_mat3_float); -RK_CHECK_PARAM_OUTPUT_TYPE(rk_mat3_int10); - typedef void (*rk_packer)( unsigned const, // count rk_instance_index const * const, // indices @@ -116,8 +112,8 @@ struct rk_parameter { unsigned binding; unsigned offset; unsigned src_size; - unsigned dst_size; unsigned src_len; + unsigned dst_size; unsigned dst_len; rk_param_input * source; rk_packer packer;