From cb763962fd3154b29ffecf4d97b0b829cdf54a18 Mon Sep 17 00:00:00 2001 From: Roz K Date: Fri, 6 Jan 2023 18:40:58 +0100 Subject: [PATCH] Merge vertex and param formats. --- __init__.py | 36 +++++++++------------ cpp/render.hpp | 31 ++++++------------ cpp/render/render_opengles.cpp | 46 +++++++++++++-------------- cpp/render/render_opengles.hpp | 8 ++--- cpp/render/vertex_format_opengles.hpp | 4 +-- 5 files changed, 53 insertions(+), 72 deletions(-) diff --git a/__init__.py b/__init__.py index 582003f..5d70e79 100644 --- a/__init__.py +++ b/__init__.py @@ -268,34 +268,28 @@ TEXTURE_FLAG_MAG_NEAREST = 0 TEXTURE_FLAG_MAG_LINEAR = _flag(3) VERTEX_FORMAT_VEC3_FLOAT = 1 -VERTEX_FORMAT_VEC3_INT10 = 2 -VERTEX_FORMAT_VEC3_UINT10 = 3 +VERTEX_FORMAT_VEC3_SHORT = 2 +VERTEX_FORMAT_VEC3_INT10 = 3 +VERTEX_FORMAT_VEC3_UINT10 = 4 +VERTEX_FORMAT_MAT3_FLOAT = 5 +VERTEX_FORMAT_MAT3_INT10 = 6 VERTEX_FORMAT_NORMALIZE = _flag(7) +_VERTEX_FORMAT_MASK = VERTEX_FORMAT_NORMALIZE - 1 def vertex_format(*format): return array('B', format).tobytes() -PARAM_FORMAT_VEC3_FLOAT = 1 -PARAM_FORMAT_VEC3_SHORT = 2 -PARAM_FORMAT_VEC3_INT10 = 3 -PARAM_FORMAT_MAT3_FLOAT = 4 -PARAM_FORMAT_MAT3_INT10 = 5 -PARAM_FORMAT_NORMALIZE = _flag(7) -_PARAM_FORMAT_MASK = PARAM_FORMAT_NORMALIZE - 1 - -_PARAMS_TYPES = ( +_VERTEX_TYPES = ( None, - vec3, # PARAM_FORMAT_VEC3_FLOAT - vec3, # PARAM_FORMAT_VEC3_SHORT - vec3, # PARAM_FORMAT_VEC3_INT10 - mat3, # PARAM_FORMAT_MAT3_FLOAT - mat3) # PARAM_FORMAT_MAT3_INT10 + vec3, # VERTEX_FORMAT_VEC3_FLOAT + vec3, # VERTEX_FORMAT_VEC3_SHORT + vec3, # VERTEX_FORMAT_VEC3_INT10 + vec3, # VERTEX_FORMAT_VEC3_UINT10 + mat3, # VERTEX_FORMAT_MAT3_FLOAT + mat3) # VERTEX_FORMAT_MAT3_INT10 -def param_type(format): - return _PARAMS_TYPES[format & _PARAM_FORMAT_MASK] - -def params_format(*format): - return array('B', format).tobytes() +def vertex_type(format): + return _VERTEX_TYPES[format & _VERTEX_FORMAT_MASK] INSTANCE_FLAG_SPAWNED = _flag(0) INSTANCE_FLAG_VISIBLE = _flag(1) diff --git a/cpp/render.hpp b/cpp/render.hpp index 32193d7..8114e5e 100644 --- a/cpp/render.hpp +++ b/cpp/render.hpp @@ -49,26 +49,17 @@ enum : rk_uint { typedef rk_ubyte rk_vertex_format; -enum : rk_ubyte { +enum : rk_vertex_format { RK_VERTEX_FORMAT_VEC3_FLOAT = 1, - RK_VERTEX_FORMAT_VEC3_INT10 = 2, - RK_VERTEX_FORMAT_VEC3_UINT10 = 3, + RK_VERTEX_FORMAT_VEC3_SHORT = 2, + RK_VERTEX_FORMAT_VEC3_INT10 = 3, + RK_VERTEX_FORMAT_VEC3_UINT10 = 4, + RK_VERTEX_FORMAT_MAT3_FLOAT = 5, + RK_VERTEX_FORMAT_MAT3_INT10 = 6, RK_VERTEX_FORMAT_NORMALIZE = RK_FLAG(7), RK_VERTEX_FORMAT_MASK = RK_VERTEX_FORMAT_NORMALIZE - 1 }; -typedef rk_ubyte rk_param_format; - -enum : rk_ubyte { - RK_PARAM_FORMAT_VEC3_FLOAT = 1, - RK_PARAM_FORMAT_VEC3_SHORT = 2, - RK_PARAM_FORMAT_VEC3_INT10 = 3, - RK_PARAM_FORMAT_MAT3_FLOAT = 4, - RK_PARAM_FORMAT_MAT3_INT10 = 5, - RK_PARAM_FORMAT_NORMALIZE = RK_FLAG(7), - RK_PARAM_FORMAT_MASK = RK_PARAM_FORMAT_NORMALIZE - 1 -}; - typedef rk_ubyte rk_instance_flags; enum : rk_ubyte { @@ -77,17 +68,15 @@ enum : rk_ubyte { RK_INSTANCE_FLAGS_SPAWNED_VISIBLE = RK_INSTANCE_FLAG_SPAWNED | RK_INSTANCE_FLAG_VISIBLE }; -typedef rk_ushort rk_instance_index; - enum : rk_uint { - RK_BATCH_MAX_SIZE = 1 << (sizeof(rk_instance_index) * 8) + RK_BATCH_MAX_SIZE = 65536 }; typedef rk_ushort rk_vertex_index; typedef rk_ushort rk_mesh_index; +typedef rk_ushort rk_instance_index; typedef rk_uint rk_vertex_input; typedef rk_uint rk_vertex_output; -typedef rk_vertex_input rk_param_input; struct rk_mesh { rk_uint base_index; @@ -137,14 +126,14 @@ RK_EXPORT rk_vertices_t rk_create_vertices( RK_EXPORT rk_batch_t rk_create_batch( rk_vertices_t vertices, rk_uint max_size, - rk_param_format const * params_format); + rk_vertex_format const * params_format); RK_EXPORT void rk_fill_batch( rk_batch_t batch, rk_uint count, rk_instance_flags const * flags, rk_mesh_index const * meshes, - rk_param_input const * const * params); + rk_vertex_input const * const * params); RK_EXPORT void rk_clear_buffer( rk_bool pixels, diff --git a/cpp/render/render_opengles.cpp b/cpp/render/render_opengles.cpp index b3e011c..1a51592 100644 --- a/cpp/render/render_opengles.cpp +++ b/cpp/render/render_opengles.cpp @@ -424,7 +424,7 @@ static void rk_buckets_alloc( rk_batch_t rk_create_batch( rk_vertices_t _vertices, rk_uint max_size, - rk_param_format const * params_format) { + rk_vertex_format const * params_format) { rk_vertices * const vertices = reinterpret_cast(_vertices); if (!vertices || !max_size || max_size > RK_BATCH_MAX_SIZE) { rk_printf("rk_create_batch(): invalid params."); @@ -447,21 +447,21 @@ rk_batch_t rk_create_batch( unsigned nparams = 0; unsigned params_size = 0; if (params_format) { - for (rk_param_format const * f = params_format; *f; ++f, ++nparams) { - switch (*f & RK_PARAM_FORMAT_MASK) { - case RK_PARAM_FORMAT_VEC3_FLOAT: + for (rk_vertex_format const * f = params_format; *f; ++f, ++nparams) { + switch (*f & RK_VERTEX_FORMAT_MASK) { + case RK_VERTEX_FORMAT_VEC3_FLOAT: params_size += rk_vec3_float::get_output_size(); break; - case RK_PARAM_FORMAT_VEC3_SHORT: + case RK_VERTEX_FORMAT_VEC3_SHORT: params_size += rk_vec3_short::get_output_size(); break; - case RK_PARAM_FORMAT_VEC3_INT10: + case RK_VERTEX_FORMAT_VEC3_INT10: params_size += rk_vec3_int10::get_output_size(); break; - case RK_PARAM_FORMAT_MAT3_FLOAT: + case RK_VERTEX_FORMAT_MAT3_FLOAT: params_size += rk_mat3_float::get_output_size(); break; - case RK_PARAM_FORMAT_MAT3_INT10: + case RK_VERTEX_FORMAT_MAT3_INT10: params_size += rk_mat3_int10::get_output_size(); break; default: @@ -551,13 +551,13 @@ rk_batch_t rk_create_batch( if (nparams) { offset = 0; rk_parameter * param = batch->params; - for (rk_param_format const * f = params_format; *f; ++f, ++param, ++binding) { - GLboolean const norm = (*f & RK_PARAM_FORMAT_NORMALIZE) != 0; + for (rk_vertex_format const * f = params_format; *f; ++f, ++param, ++binding) { + GLboolean const norm = (*f & RK_VERTEX_FORMAT_NORMALIZE) != 0; param->dirty = false; param->binding = binding; param->offset = offset; - switch (*f & RK_PARAM_FORMAT_MASK) { - case RK_PARAM_FORMAT_VEC3_FLOAT: + switch (*f & RK_VERTEX_FORMAT_MASK) { + case RK_VERTEX_FORMAT_VEC3_FLOAT: param->src_size = rk_vec3_float::get_input_size(); param->dst_size = rk_vec3_float::get_output_size(); param->packer = rk_vec3_float::param_packer; @@ -566,7 +566,7 @@ rk_batch_t rk_create_batch( glVertexAttribFormat(attrib, 3, GL_FLOAT, GL_FALSE, 0); glVertexAttribBinding(attrib++, binding); break; - case RK_PARAM_FORMAT_VEC3_SHORT: + case RK_VERTEX_FORMAT_VEC3_SHORT: param->src_size = rk_vec3_short::get_input_size(); param->dst_size = rk_vec3_short::get_output_size(); param->packer = norm ? rk_vec3_short_norm::param_packer : rk_vec3_short::param_packer; @@ -575,7 +575,7 @@ rk_batch_t rk_create_batch( glVertexAttribFormat(attrib, 3, GL_SHORT, norm, 0); glVertexAttribBinding(attrib++, binding); break; - case RK_PARAM_FORMAT_VEC3_INT10: + case RK_VERTEX_FORMAT_VEC3_INT10: param->src_size = rk_vec3_int10::get_input_size(); param->dst_size = rk_vec3_int10::get_output_size(); param->packer = norm ? rk_vec3_int10_norm::param_packer : rk_vec3_int10::param_packer; @@ -584,7 +584,7 @@ rk_batch_t rk_create_batch( glVertexAttribFormat(attrib, 4, GL_INT_2_10_10_10_REV, norm, 0); glVertexAttribBinding(attrib++, binding); break; - case RK_PARAM_FORMAT_MAT3_FLOAT: + case RK_VERTEX_FORMAT_MAT3_FLOAT: param->src_size = rk_mat3_float::get_input_size(); param->dst_size = rk_mat3_float::get_output_size(); param->packer = rk_mat3_float::param_packer; @@ -599,7 +599,7 @@ rk_batch_t rk_create_batch( glVertexAttribFormat(attrib, 3, GL_FLOAT, GL_FALSE, rk_mat3_float::get_output_offset(2)); glVertexAttribBinding(attrib++, binding); break; - case RK_PARAM_FORMAT_MAT3_INT10: + case RK_VERTEX_FORMAT_MAT3_INT10: param->src_size = rk_mat3_int10::get_input_size(); param->dst_size = rk_mat3_int10::get_output_size(); param->packer = norm ? rk_mat3_int10_norm::param_packer : rk_mat3_int10::param_packer; @@ -616,9 +616,9 @@ rk_batch_t rk_create_batch( break; } glVertexBindingDivisor(binding, 1); - param->src_len = param->src_size / sizeof(rk_param_output); - param->dst_len = param->dst_size / sizeof(rk_param_output); - param->source = new rk_param_input[max_size * param->src_len]; + param->src_len = param->src_size / sizeof(rk_vertex_output); + param->dst_len = param->dst_size / sizeof(rk_vertex_output); + param->source = new rk_vertex_input[max_size * param->src_len]; memset(param->source, 0xFF, max_size * param->src_size); offset += max_size * param->dst_size; } @@ -674,7 +674,7 @@ static void rk_pack_batch( if (param->dirty) { param->dirty = false; if (batch.ninstances) { - rk_param_output * const dst = reinterpret_cast( + rk_vertex_output * const dst = reinterpret_cast( glMapBufferRange(GL_ARRAY_BUFFER, param->offset, batch.ninstances * param->dst_size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT)); if (dst) { @@ -695,7 +695,7 @@ void rk_fill_batch( rk_uint count, rk_instance_flags const * flags, rk_mesh_index const * meshes, - rk_param_input const * const * params) { + rk_vertex_input const * const * params) { rk_batch const * const batch = reinterpret_cast(_batch); if (!batch || !count || count > batch->max_size) { rk_printf("rk_fill_batch(): invalid params."); @@ -706,7 +706,7 @@ void rk_fill_batch( if (batch->nparams) { got_all_params = (params != nullptr); if (params) { - for (rk_param_input const * const * param = params; param < params + batch->nparams; ++param) { + for (rk_vertex_input const * const * param = params; param < params + batch->nparams; ++param) { bool const got_param = (*param != nullptr); got_any_params |= got_param; got_all_params &= got_param; @@ -730,7 +730,7 @@ void rk_fill_batch( if (batch->nparams) { rk_parameter const * const last_param = batch->params + batch->nparams; if (got_any_params) { - rk_param_input const * const * src = params; + rk_vertex_input const * const * src = params; for (rk_parameter const * param = batch->params; param < last_param; ++param, ++src) { param->dirty = ((*src && rk_cmp_memcpy(param->source, *src, batch->count * param->src_len)) || need_sorting); diff --git a/cpp/render/render_opengles.hpp b/cpp/render/render_opengles.hpp index b367a40..f97c4b5 100644 --- a/cpp/render/render_opengles.hpp +++ b/cpp/render/render_opengles.hpp @@ -23,8 +23,6 @@ static_assert(sizeof(rk_vertex_output) == 4); -typedef rk_vertex_output rk_param_output; - struct rk_shader { GLuint vertex; GLuint fragment; @@ -65,8 +63,8 @@ struct rk_command { typedef void (*rk_packer)( unsigned const, // count rk_instance_index const * const __restrict, // indices - rk_param_output * __restrict, // dst - rk_param_input const * const __restrict); // src + rk_vertex_output * __restrict, // dst + rk_vertex_input const * const __restrict); // src struct rk_parameter { mutable bool dirty; @@ -76,7 +74,7 @@ struct rk_parameter { unsigned src_len; unsigned dst_size; unsigned dst_len; - rk_param_input * source; + rk_vertex_input * source; rk_packer packer; }; diff --git a/cpp/render/vertex_format_opengles.hpp b/cpp/render/vertex_format_opengles.hpp index 4b5247a..a484a96 100644 --- a/cpp/render/vertex_format_opengles.hpp +++ b/cpp/render/vertex_format_opengles.hpp @@ -231,8 +231,8 @@ struct rk_format { static void param_packer( unsigned const count, rk_instance_index const * const __restrict indices, - rk_param_output * __restrict _dst, - rk_param_input const * const __restrict _src) { + rk_vertex_output * __restrict _dst, + rk_vertex_input const * const __restrict _src) { rk_instance_index const * const last_index = indices + count; output_format * __restrict dst = reinterpret_cast(_dst); input_format const * const __restrict src = reinterpret_cast(_src);