Improve typing in render.

This commit is contained in:
Roz K 2023-01-04 12:41:05 +01:00
parent d6ec77207f
commit 59d13684be
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
3 changed files with 30 additions and 30 deletions

View File

@ -86,16 +86,21 @@ enum : rk_uint {
typedef rk_ushort rk_vertex_index; typedef rk_ushort rk_vertex_index;
typedef rk_ushort rk_mesh_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 { struct rk_mesh {
rk_uint base_index; rk_uint base_index;
rk_uint ntriangles; rk_uint ntriangles;
}; };
// param input types must be size compatible with an array of rk_param_input
typedef rk_uint rk_param_input;
template<typename _input_type>
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_EXPORT void rk_render_initialize(
rk_bool debug); rk_bool debug);

View File

@ -13,6 +13,7 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "../render.hpp"
#include "render_opengles.hpp" #include "render_opengles.hpp"
#include "../display/display_glx.hpp" #include "../display/display_glx.hpp"
#include "../cmp_memcpy.hpp" #include "../cmp_memcpy.hpp"
@ -688,8 +689,8 @@ rk_batch_t rk_create_batch(
param->offset = offset; param->offset = offset;
switch (*f & RK_PARAM_FORMAT_MASK) { switch (*f & RK_PARAM_FORMAT_MASK) {
case RK_PARAM_FORMAT_VEC3_FLOAT: case RK_PARAM_FORMAT_VEC3_FLOAT:
param->src_size = sizeof(rk_vec3); rk_param_get_input_size<rk_vec3>(param->src_size, param->src_len);
param->dst_size = sizeof(rk_vec3_float); rk_param_get_output_size<rk_vec3_float>(param->dst_size, param->dst_len);
param->packer = rk_pack_vec3_float; param->packer = rk_pack_vec3_float;
glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size); glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size);
glEnableVertexAttribArray(attrib); glEnableVertexAttribArray(attrib);
@ -697,8 +698,8 @@ rk_batch_t rk_create_batch(
glVertexAttribBinding(attrib++, binding); glVertexAttribBinding(attrib++, binding);
break; break;
case RK_PARAM_FORMAT_VEC3_SHORT: case RK_PARAM_FORMAT_VEC3_SHORT:
param->src_size = sizeof(rk_vec3); rk_param_get_input_size<rk_vec3>(param->src_size, param->src_len);
param->dst_size = sizeof(rk_vec3_short); rk_param_get_output_size<rk_vec3_short>(param->dst_size, param->dst_len);
param->packer = norm ? rk_pack_vec3_short_norm : rk_pack_vec3_short; param->packer = norm ? rk_pack_vec3_short_norm : rk_pack_vec3_short;
glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size); glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size);
glEnableVertexAttribArray(attrib); glEnableVertexAttribArray(attrib);
@ -706,8 +707,8 @@ rk_batch_t rk_create_batch(
glVertexAttribBinding(attrib++, binding); glVertexAttribBinding(attrib++, binding);
break; break;
case RK_PARAM_FORMAT_VEC3_INT10: case RK_PARAM_FORMAT_VEC3_INT10:
param->src_size = sizeof(rk_vec3); rk_param_get_input_size<rk_vec3>(param->src_size, param->src_len);
param->dst_size = sizeof(rk_vec3_int10); rk_param_get_output_size<rk_vec3_int10>(param->dst_size, param->dst_len);
param->packer = norm ? rk_pack_vec3_int10_norm : rk_pack_vec3_int10; param->packer = norm ? rk_pack_vec3_int10_norm : rk_pack_vec3_int10;
glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size); glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size);
glEnableVertexAttribArray(attrib); glEnableVertexAttribArray(attrib);
@ -715,8 +716,8 @@ rk_batch_t rk_create_batch(
glVertexAttribBinding(attrib++, binding); glVertexAttribBinding(attrib++, binding);
break; break;
case RK_PARAM_FORMAT_MAT3_FLOAT: case RK_PARAM_FORMAT_MAT3_FLOAT:
param->src_size = sizeof(rk_mat3); rk_param_get_input_size<rk_mat3>(param->src_size, param->src_len);
param->dst_size = sizeof(rk_mat3_float); rk_param_get_output_size<rk_mat3_float>(param->dst_size, param->dst_len);
param->packer = rk_pack_mat3_float; param->packer = rk_pack_mat3_float;
glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size); glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size);
glEnableVertexAttribArray(attrib); glEnableVertexAttribArray(attrib);
@ -730,8 +731,8 @@ rk_batch_t rk_create_batch(
glVertexAttribBinding(attrib++, binding); glVertexAttribBinding(attrib++, binding);
break; break;
case RK_PARAM_FORMAT_MAT3_INT10: case RK_PARAM_FORMAT_MAT3_INT10:
param->src_size = sizeof(rk_mat3); rk_param_get_input_size<rk_mat3>(param->src_size, param->src_len);
param->dst_size = sizeof(rk_mat3_int10); rk_param_get_output_size<rk_mat3_int10>(param->dst_size, param->dst_len);
param->packer = norm ? rk_pack_mat3_int10_norm : rk_pack_mat3_int10; param->packer = norm ? rk_pack_mat3_int10_norm : rk_pack_mat3_int10;
glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size); glBindVertexBuffer(binding, batch->params_buffer, param->offset, param->dst_size);
glEnableVertexAttribArray(attrib); glEnableVertexAttribArray(attrib);
@ -746,8 +747,6 @@ rk_batch_t rk_create_batch(
break; break;
} }
glVertexBindingDivisor(binding, 1); 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]; param->source = new rk_param_input[max_size * param->src_len];
memset(param->source, 0xFF, max_size * param->src_size); memset(param->source, 0xFF, max_size * param->src_size);
offset += max_size * param->dst_size; offset += max_size * param->dst_size;

View File

@ -16,7 +16,8 @@
#ifndef _RK_ENGINE_RENDER_OPENGLES_H #ifndef _RK_ENGINE_RENDER_OPENGLES_H
#define _RK_ENGINE_RENDER_OPENGLES_H #define _RK_ENGINE_RENDER_OPENGLES_H
#include "../render.hpp" #include "../types.hpp"
#include "../math.hpp"
#include <GLES3/gl32.h> #include <GLES3/gl32.h>
#include <GLES3/gl3ext.h> #include <GLES3/gl3ext.h>
#include <GLES3/gl3platform.h> #include <GLES3/gl3platform.h>
@ -61,7 +62,12 @@ struct rk_command {
// param output types must be size compatible with an array of rk_param_output // param output types must be size compatible with an array of rk_param_output
typedef rk_uint 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<typename _output_type>
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 { struct rk_vec3_float {
float x; float x;
@ -95,16 +101,6 @@ struct rk_mat3_int10 {
rk_vec3_int10 z; 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)( typedef void (*rk_packer)(
unsigned const, // count unsigned const, // count
rk_instance_index const * const, // indices rk_instance_index const * const, // indices
@ -116,8 +112,8 @@ struct rk_parameter {
unsigned binding; unsigned binding;
unsigned offset; unsigned offset;
unsigned src_size; unsigned src_size;
unsigned dst_size;
unsigned src_len; unsigned src_len;
unsigned dst_size;
unsigned dst_len; unsigned dst_len;
rk_param_input * source; rk_param_input * source;
rk_packer packer; rk_packer packer;