Cleanup params packing.

This commit is contained in:
Roz K 2022-12-20 06:33:31 +01:00
parent ddb9b0598f
commit c1b9c0d17e
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
2 changed files with 57 additions and 39 deletions

View File

@ -317,35 +317,35 @@ rk_vertices_t rk_create_vertices(
return vertices; return vertices;
} }
static void rk_pack_vec3_float( static void rk_pack_vec3(
rk_vec3 * const __restrict dst, rk_pack_dst dst,
rk_vec3 const * const __restrict src) { rk_pack_src src) {
*dst = *src; *dst.vec3_ptr = *src.vec3_ptr;
} }
static void rk_pack_vec3_short( static void rk_pack_vec3s(
rk_vec3_short * const __restrict dst, rk_pack_dst dst,
rk_vec3 const * const __restrict src) { rk_pack_src src) {
dst->x = static_cast<rk_short>(src->x); dst.vec3s_ptr->x = static_cast<rk_short>(src.vec3_ptr->x);
dst->y = static_cast<rk_short>(src->y); dst.vec3s_ptr->y = static_cast<rk_short>(src.vec3_ptr->y);
dst->z = static_cast<rk_short>(src->z); dst.vec3s_ptr->z = static_cast<rk_short>(src.vec3_ptr->z);
} }
static void rk_pack_vec3_short_norm( static void rk_pack_vec3s_norm(
rk_vec3_short * const __restrict dst, rk_pack_dst dst,
rk_vec3 const * const __restrict src) { rk_pack_src src) {
#define _convert(s) (static_cast<rk_short>((s) * ((s) < 0.f ? 32768.f : 32767.f))) #define _convert(s) (static_cast<rk_short>((s) * ((s) < 0.f ? 32768.f : 32767.f)))
dst->x = _convert(src->x); dst.vec3s_ptr->x = _convert(src.vec3_ptr->x);
dst->y = _convert(src->y); dst.vec3s_ptr->y = _convert(src.vec3_ptr->y);
dst->z = _convert(src->z); dst.vec3s_ptr->z = _convert(src.vec3_ptr->z);
#undef _convert #undef _convert
} }
static void rk_pack_vec3_int10( static void rk_pack_vec3_int10(
rk_int * const __restrict dst, rk_pack_dst dst,
rk_vec3 const * const __restrict src) { rk_pack_src src) {
#define _convert(s) (static_cast<rk_int>((s) * ((s) < 0.f ? 512.f : 511.f)) & 1023) #define _convert(s) (static_cast<rk_int>((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 #undef _convert
} }
@ -370,7 +370,7 @@ rk_batch_t rk_create_batch(
break; break;
case RK_PARAM_FORMAT_VEC3_SHORT: case RK_PARAM_FORMAT_VEC3_SHORT:
params_size += sizeof(rk_vec3); params_size += sizeof(rk_vec3);
packed_size += sizeof(rk_vec3_short); packed_size += sizeof(rk_vec3s);
break; break;
case RK_PARAM_FORMAT_VEC3_INT10: case RK_PARAM_FORMAT_VEC3_INT10:
params_size += sizeof(rk_vec3); params_size += sizeof(rk_vec3);
@ -403,23 +403,23 @@ rk_batch_t rk_create_batch(
switch (*f & RK_PARAM_FORMAT_MASK) { switch (*f & RK_PARAM_FORMAT_MASK) {
case RK_PARAM_FORMAT_VEC3_FLOAT: case RK_PARAM_FORMAT_VEC3_FLOAT:
glVertexAttribFormat(layout, 3, GL_FLOAT, normalize, offset); glVertexAttribFormat(layout, 3, GL_FLOAT, normalize, offset);
packer->pack = reinterpret_cast<rk_packer_fn>(rk_pack_vec3_float); packer->pack = rk_pack_vec3;
packer->dst_incr = sizeof(rk_vec3); packer->dst_incr = sizeof(rk_vec3);
packer->src_incr = sizeof(rk_vec3); packer->src_incr = sizeof(rk_vec3);
break; break;
case RK_PARAM_FORMAT_VEC3_SHORT: case RK_PARAM_FORMAT_VEC3_SHORT:
glVertexAttribFormat(layout, 3, GL_SHORT, normalize, offset); glVertexAttribFormat(layout, 3, GL_SHORT, normalize, offset);
if (normalize) { if (normalize) {
packer->pack = reinterpret_cast<rk_packer_fn>(rk_pack_vec3_short_norm); packer->pack = rk_pack_vec3s_norm;
} else { } else {
packer->pack = reinterpret_cast<rk_packer_fn>(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); packer->src_incr = sizeof(rk_vec3);
break; break;
case RK_PARAM_FORMAT_VEC3_INT10: case RK_PARAM_FORMAT_VEC3_INT10:
glVertexAttribFormat(layout, 4, GL_INT_2_10_10_10_REV, normalize, offset); glVertexAttribFormat(layout, 4, GL_INT_2_10_10_10_REV, normalize, offset);
packer->pack = reinterpret_cast<rk_packer_fn>(rk_pack_vec3_int10); packer->pack = rk_pack_vec3_int10;
packer->dst_incr = sizeof(rk_int); packer->dst_incr = sizeof(rk_int);
packer->src_incr = sizeof(rk_vec3); packer->src_incr = sizeof(rk_vec3);
break; break;
@ -571,19 +571,19 @@ static rk_uint rk_batch_build_commands(
return commands - batch.commands; return commands - batch.commands;
} }
static void rk_batch_convert_params( static void rk_batch_pack(
rk_batch & batch, rk_batch & batch,
rk_uint const ninstances, rk_uint const ninstances,
rk_ubyte const * const params) { 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_ushort const * const last_index = batch.indices + ninstances;
rk_packer const * const last_packer = batch.packers + batch.nparams; rk_packer const * const last_packer = batch.packers + batch.nparams;
for (rk_ushort const * index = batch.indices; index < last_index; ++index) { for (rk_ushort const * index = batch.indices; index < last_index; ++index) {
rk_ubyte const * __restrict src = &params[batch.params_size * (*index)]; rk_pack_src src(&params[batch.params_size * (*index)]);
for (rk_packer const * packer = batch.packers; packer < last_packer; ++packer) { for (rk_packer const * packer = batch.packers; packer < last_packer; ++packer) {
packer->pack(dst, src); packer->pack(dst, src);
dst += packer->dst_incr; dst.ptr += packer->dst_incr;
src += packer->src_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); glBufferData(GL_DRAW_INDIRECT_BUFFER, ncommands * sizeof(rk_command), batch.commands, GL_STREAM_DRAW);
} }
if (batch.nparams) { if (batch.nparams) {
rk_batch_convert_params(batch, ninstances, params); rk_batch_pack(batch, ninstances, params);
glBindBuffer(GL_ARRAY_BUFFER, batch.params_buffer); glBindBuffer(GL_ARRAY_BUFFER, batch.params_buffer);
glBufferData(GL_ARRAY_BUFFER, ninstances * batch.packed_size, batch.params, GL_STREAM_DRAW); glBufferData(GL_ARRAY_BUFFER, ninstances * batch.packed_size, batch.params, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);

View File

@ -52,7 +52,32 @@ struct rk_vertices {
GLuint indices; 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 { struct rk_packer {
rk_packer_fn pack; rk_packer_fn pack;
@ -81,11 +106,4 @@ struct rk_batch {
GLuint commands_buffer; 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 #endif // _RK_ENGINE_RENDER_OPENGLES_H