From c1b9c0d17eb9390a7c8002d520ddc01f1a56751d Mon Sep 17 00:00:00 2001 From: Roz K Date: Tue, 20 Dec 2022 06:33:31 +0100 Subject: [PATCH] Cleanup params packing. --- cpp/opengl/render_opengles.cpp | 62 +++++++++++++++++----------------- cpp/opengl/render_opengles.hpp | 34 ++++++++++++++----- 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/cpp/opengl/render_opengles.cpp b/cpp/opengl/render_opengles.cpp index ecbecd9..91d988b 100644 --- a/cpp/opengl/render_opengles.cpp +++ b/cpp/opengl/render_opengles.cpp @@ -317,35 +317,35 @@ rk_vertices_t rk_create_vertices( return vertices; } -static void rk_pack_vec3_float( - rk_vec3 * const __restrict dst, - rk_vec3 const * const __restrict src) { - *dst = *src; +static void rk_pack_vec3( + rk_pack_dst dst, + rk_pack_src src) { + *dst.vec3_ptr = *src.vec3_ptr; } -static void rk_pack_vec3_short( - rk_vec3_short * const __restrict dst, - rk_vec3 const * const __restrict src) { - dst->x = static_cast(src->x); - dst->y = static_cast(src->y); - dst->z = static_cast(src->z); +static void rk_pack_vec3s( + rk_pack_dst dst, + rk_pack_src src) { + dst.vec3s_ptr->x = static_cast(src.vec3_ptr->x); + dst.vec3s_ptr->y = static_cast(src.vec3_ptr->y); + dst.vec3s_ptr->z = static_cast(src.vec3_ptr->z); } -static void rk_pack_vec3_short_norm( - rk_vec3_short * const __restrict dst, - rk_vec3 const * const __restrict src) { +static void rk_pack_vec3s_norm( + rk_pack_dst dst, + rk_pack_src src) { #define _convert(s) (static_cast((s) * ((s) < 0.f ? 32768.f : 32767.f))) - dst->x = _convert(src->x); - dst->y = _convert(src->y); - dst->z = _convert(src->z); + dst.vec3s_ptr->x = _convert(src.vec3_ptr->x); + dst.vec3s_ptr->y = _convert(src.vec3_ptr->y); + dst.vec3s_ptr->z = _convert(src.vec3_ptr->z); #undef _convert } static void rk_pack_vec3_int10( - rk_int * const __restrict dst, - rk_vec3 const * const __restrict src) { + rk_pack_dst dst, + rk_pack_src src) { #define _convert(s) (static_cast((s) * ((s) < 0.f ? 512.f : 511.f)) & 1023) - *dst = _convert(src->x) | (_convert(src->y) << 10) | (_convert(src->z) << 20); + *dst.int_ptr = _convert(src.vec3_ptr->x) | (_convert(src.vec3_ptr->y) << 10) | (_convert(src.vec3_ptr->z) << 20); #undef _convert } @@ -370,7 +370,7 @@ rk_batch_t rk_create_batch( break; case RK_PARAM_FORMAT_VEC3_SHORT: params_size += sizeof(rk_vec3); - packed_size += sizeof(rk_vec3_short); + packed_size += sizeof(rk_vec3s); break; case RK_PARAM_FORMAT_VEC3_INT10: params_size += sizeof(rk_vec3); @@ -403,23 +403,23 @@ rk_batch_t rk_create_batch( switch (*f & RK_PARAM_FORMAT_MASK) { case RK_PARAM_FORMAT_VEC3_FLOAT: glVertexAttribFormat(layout, 3, GL_FLOAT, normalize, offset); - packer->pack = reinterpret_cast(rk_pack_vec3_float); + packer->pack = rk_pack_vec3; packer->dst_incr = sizeof(rk_vec3); packer->src_incr = sizeof(rk_vec3); break; case RK_PARAM_FORMAT_VEC3_SHORT: glVertexAttribFormat(layout, 3, GL_SHORT, normalize, offset); if (normalize) { - packer->pack = reinterpret_cast(rk_pack_vec3_short_norm); + packer->pack = rk_pack_vec3s_norm; } else { - packer->pack = reinterpret_cast(rk_pack_vec3_short); + packer->pack = rk_pack_vec3s; } - packer->dst_incr = sizeof(rk_vec3_short); + packer->dst_incr = sizeof(rk_vec3s); packer->src_incr = sizeof(rk_vec3); break; case RK_PARAM_FORMAT_VEC3_INT10: glVertexAttribFormat(layout, 4, GL_INT_2_10_10_10_REV, normalize, offset); - packer->pack = reinterpret_cast(rk_pack_vec3_int10); + packer->pack = rk_pack_vec3_int10; packer->dst_incr = sizeof(rk_int); packer->src_incr = sizeof(rk_vec3); break; @@ -571,19 +571,19 @@ static rk_uint rk_batch_build_commands( return commands - batch.commands; } -static void rk_batch_convert_params( +static void rk_batch_pack( rk_batch & batch, rk_uint const ninstances, rk_ubyte const * const params) { - rk_ubyte * __restrict dst = batch.params; + rk_pack_dst dst(batch.params); rk_ushort const * const last_index = batch.indices + ninstances; rk_packer const * const last_packer = batch.packers + batch.nparams; for (rk_ushort const * index = batch.indices; index < last_index; ++index) { - rk_ubyte const * __restrict src = ¶ms[batch.params_size * (*index)]; + rk_pack_src src(¶ms[batch.params_size * (*index)]); for (rk_packer const * packer = batch.packers; packer < last_packer; ++packer) { packer->pack(dst, src); - dst += packer->dst_incr; - src += packer->src_incr; + dst.ptr += packer->dst_incr; + src.ptr += packer->src_incr; } } } @@ -608,7 +608,7 @@ void rk_draw_batch( glBufferData(GL_DRAW_INDIRECT_BUFFER, ncommands * sizeof(rk_command), batch.commands, GL_STREAM_DRAW); } if (batch.nparams) { - rk_batch_convert_params(batch, ninstances, params); + rk_batch_pack(batch, ninstances, params); glBindBuffer(GL_ARRAY_BUFFER, batch.params_buffer); glBufferData(GL_ARRAY_BUFFER, ninstances * batch.packed_size, batch.params, GL_STREAM_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); diff --git a/cpp/opengl/render_opengles.hpp b/cpp/opengl/render_opengles.hpp index 24dac3c..3829415 100644 --- a/cpp/opengl/render_opengles.hpp +++ b/cpp/opengl/render_opengles.hpp @@ -52,7 +52,32 @@ struct rk_vertices { GLuint indices; }; -typedef void (*rk_packer_fn)(rk_ubyte * const __restrict, rk_ubyte const * const __restrict); +struct rk_vec3s { + rk_short x; + rk_short y; + rk_short z; + rk_short pad; +}; + +union rk_pack_src { + rk_ubyte const * __restrict ptr; + rk_vec3 const * __restrict vec3_ptr; + + inline rk_pack_src() {} + inline rk_pack_src(rk_ubyte const * const __restrict src) : ptr(src) {} +}; + +union rk_pack_dst { + rk_ubyte * __restrict ptr; + rk_vec3 * __restrict vec3_ptr; + rk_vec3s * __restrict vec3s_ptr; + rk_int * __restrict int_ptr; + + inline rk_pack_dst() {} + inline rk_pack_dst(rk_ubyte * const __restrict dst) : ptr(dst) {} +}; + +typedef void (*rk_packer_fn)(rk_pack_dst, rk_pack_src); struct rk_packer { rk_packer_fn pack; @@ -81,11 +106,4 @@ struct rk_batch { GLuint commands_buffer; }; -struct rk_vec3_short { - rk_short x; - rk_short y; - rk_short z; - rk_short pad; -}; - #endif // _RK_ENGINE_RENDER_OPENGLES_H