Compare commits

..

2 Commits

Author SHA1 Message Date
7384a014ff
Improve enum declaration. 2023-01-06 18:42:58 +01:00
cb763962fd
Merge vertex and param formats. 2023-01-06 18:40:58 +01:00
5 changed files with 56 additions and 75 deletions

View File

@ -268,34 +268,28 @@ TEXTURE_FLAG_MAG_NEAREST = 0
TEXTURE_FLAG_MAG_LINEAR = _flag(3) TEXTURE_FLAG_MAG_LINEAR = _flag(3)
VERTEX_FORMAT_VEC3_FLOAT = 1 VERTEX_FORMAT_VEC3_FLOAT = 1
VERTEX_FORMAT_VEC3_INT10 = 2 VERTEX_FORMAT_VEC3_SHORT = 2
VERTEX_FORMAT_VEC3_UINT10 = 3 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_NORMALIZE = _flag(7)
_VERTEX_FORMAT_MASK = VERTEX_FORMAT_NORMALIZE - 1
def vertex_format(*format): def vertex_format(*format):
return array('B', format).tobytes() return array('B', format).tobytes()
PARAM_FORMAT_VEC3_FLOAT = 1 _VERTEX_TYPES = (
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 = (
None, None,
vec3, # PARAM_FORMAT_VEC3_FLOAT vec3, # VERTEX_FORMAT_VEC3_FLOAT
vec3, # PARAM_FORMAT_VEC3_SHORT vec3, # VERTEX_FORMAT_VEC3_SHORT
vec3, # PARAM_FORMAT_VEC3_INT10 vec3, # VERTEX_FORMAT_VEC3_INT10
mat3, # PARAM_FORMAT_MAT3_FLOAT vec3, # VERTEX_FORMAT_VEC3_UINT10
mat3) # PARAM_FORMAT_MAT3_INT10 mat3, # VERTEX_FORMAT_MAT3_FLOAT
mat3) # VERTEX_FORMAT_MAT3_INT10
def param_type(format): def vertex_type(format):
return _PARAMS_TYPES[format & _PARAM_FORMAT_MASK] return _VERTEX_TYPES[format & _VERTEX_FORMAT_MASK]
def params_format(*format):
return array('B', format).tobytes()
INSTANCE_FLAG_SPAWNED = _flag(0) INSTANCE_FLAG_SPAWNED = _flag(0)
INSTANCE_FLAG_VISIBLE = _flag(1) INSTANCE_FLAG_VISIBLE = _flag(1)

View File

@ -29,7 +29,7 @@ typedef rk_handle_t rk_batch_t;
typedef rk_uint rk_texture_format; typedef rk_uint rk_texture_format;
enum : rk_uint { enum : rk_texture_format {
RK_TEXTURE_FORMAT_SRGB8_A8 = 0, RK_TEXTURE_FORMAT_SRGB8_A8 = 0,
RK_TEXTURE_FORMAT_RGBA8 = 1, RK_TEXTURE_FORMAT_RGBA8 = 1,
RK_TEXTURE_FORMAT_RGB10_A2 = 2, RK_TEXTURE_FORMAT_RGB10_A2 = 2,
@ -38,7 +38,7 @@ enum : rk_uint {
typedef rk_uint rk_texture_flags; typedef rk_uint rk_texture_flags;
enum : rk_uint { enum : rk_texture_flags {
RK_TEXTURE_FLAG_3D = RK_FLAG(0), RK_TEXTURE_FLAG_3D = RK_FLAG(0),
RK_TEXTURE_FLAG_MIPMAPS = RK_FLAG(1), RK_TEXTURE_FLAG_MIPMAPS = RK_FLAG(1),
RK_TEXTURE_FLAG_MIN_NEAREST = 0, RK_TEXTURE_FLAG_MIN_NEAREST = 0,
@ -49,45 +49,34 @@ enum : rk_uint {
typedef rk_ubyte rk_vertex_format; typedef rk_ubyte rk_vertex_format;
enum : rk_ubyte { enum : rk_vertex_format {
RK_VERTEX_FORMAT_VEC3_FLOAT = 1, RK_VERTEX_FORMAT_VEC3_FLOAT = 1,
RK_VERTEX_FORMAT_VEC3_INT10 = 2, RK_VERTEX_FORMAT_VEC3_SHORT = 2,
RK_VERTEX_FORMAT_VEC3_UINT10 = 3, 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_NORMALIZE = RK_FLAG(7),
RK_VERTEX_FORMAT_MASK = RK_VERTEX_FORMAT_NORMALIZE - 1 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; typedef rk_ubyte rk_instance_flags;
enum : rk_ubyte { enum : rk_instance_flags {
RK_INSTANCE_FLAG_SPAWNED = RK_FLAG(0), RK_INSTANCE_FLAG_SPAWNED = RK_FLAG(0),
RK_INSTANCE_FLAG_VISIBLE = RK_FLAG(1), RK_INSTANCE_FLAG_VISIBLE = RK_FLAG(1),
RK_INSTANCE_FLAGS_SPAWNED_VISIBLE = RK_INSTANCE_FLAG_SPAWNED | RK_INSTANCE_FLAG_VISIBLE RK_INSTANCE_FLAGS_SPAWNED_VISIBLE = RK_INSTANCE_FLAG_SPAWNED | RK_INSTANCE_FLAG_VISIBLE
}; };
typedef rk_ushort rk_instance_index;
enum : rk_uint { 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_vertex_index;
typedef rk_ushort rk_mesh_index; typedef rk_ushort rk_mesh_index;
typedef rk_ushort rk_instance_index;
typedef rk_uint rk_vertex_input; typedef rk_uint rk_vertex_input;
typedef rk_uint rk_vertex_output; typedef rk_uint rk_vertex_output;
typedef rk_vertex_input rk_param_input;
struct rk_mesh { struct rk_mesh {
rk_uint base_index; 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_EXPORT rk_batch_t rk_create_batch(
rk_vertices_t vertices, rk_vertices_t vertices,
rk_uint max_size, rk_uint max_size,
rk_param_format const * params_format); rk_vertex_format const * params_format);
RK_EXPORT void rk_fill_batch( RK_EXPORT void rk_fill_batch(
rk_batch_t batch, rk_batch_t batch,
rk_uint count, rk_uint count,
rk_instance_flags const * flags, rk_instance_flags const * flags,
rk_mesh_index const * meshes, rk_mesh_index const * meshes,
rk_param_input const * const * params); rk_vertex_input const * const * params);
RK_EXPORT void rk_clear_buffer( RK_EXPORT void rk_clear_buffer(
rk_bool pixels, rk_bool pixels,

View File

@ -424,7 +424,7 @@ static void rk_buckets_alloc(
rk_batch_t rk_create_batch( rk_batch_t rk_create_batch(
rk_vertices_t _vertices, rk_vertices_t _vertices,
rk_uint max_size, rk_uint max_size,
rk_param_format const * params_format) { rk_vertex_format const * params_format) {
rk_vertices * const vertices = reinterpret_cast<rk_vertices *>(_vertices); rk_vertices * const vertices = reinterpret_cast<rk_vertices *>(_vertices);
if (!vertices || !max_size || max_size > RK_BATCH_MAX_SIZE) { if (!vertices || !max_size || max_size > RK_BATCH_MAX_SIZE) {
rk_printf("rk_create_batch(): invalid params."); rk_printf("rk_create_batch(): invalid params.");
@ -447,21 +447,21 @@ rk_batch_t rk_create_batch(
unsigned nparams = 0; unsigned nparams = 0;
unsigned params_size = 0; unsigned params_size = 0;
if (params_format) { if (params_format) {
for (rk_param_format const * f = params_format; *f; ++f, ++nparams) { for (rk_vertex_format const * f = params_format; *f; ++f, ++nparams) {
switch (*f & RK_PARAM_FORMAT_MASK) { switch (*f & RK_VERTEX_FORMAT_MASK) {
case RK_PARAM_FORMAT_VEC3_FLOAT: case RK_VERTEX_FORMAT_VEC3_FLOAT:
params_size += rk_vec3_float::get_output_size(); params_size += rk_vec3_float::get_output_size();
break; break;
case RK_PARAM_FORMAT_VEC3_SHORT: case RK_VERTEX_FORMAT_VEC3_SHORT:
params_size += rk_vec3_short::get_output_size(); params_size += rk_vec3_short::get_output_size();
break; break;
case RK_PARAM_FORMAT_VEC3_INT10: case RK_VERTEX_FORMAT_VEC3_INT10:
params_size += rk_vec3_int10::get_output_size(); params_size += rk_vec3_int10::get_output_size();
break; break;
case RK_PARAM_FORMAT_MAT3_FLOAT: case RK_VERTEX_FORMAT_MAT3_FLOAT:
params_size += rk_mat3_float::get_output_size(); params_size += rk_mat3_float::get_output_size();
break; break;
case RK_PARAM_FORMAT_MAT3_INT10: case RK_VERTEX_FORMAT_MAT3_INT10:
params_size += rk_mat3_int10::get_output_size(); params_size += rk_mat3_int10::get_output_size();
break; break;
default: default:
@ -551,13 +551,13 @@ rk_batch_t rk_create_batch(
if (nparams) { if (nparams) {
offset = 0; offset = 0;
rk_parameter * param = batch->params; rk_parameter * param = batch->params;
for (rk_param_format const * f = params_format; *f; ++f, ++param, ++binding) { for (rk_vertex_format const * f = params_format; *f; ++f, ++param, ++binding) {
GLboolean const norm = (*f & RK_PARAM_FORMAT_NORMALIZE) != 0; GLboolean const norm = (*f & RK_VERTEX_FORMAT_NORMALIZE) != 0;
param->dirty = false; param->dirty = false;
param->binding = binding; param->binding = binding;
param->offset = offset; param->offset = offset;
switch (*f & RK_PARAM_FORMAT_MASK) { switch (*f & RK_VERTEX_FORMAT_MASK) {
case RK_PARAM_FORMAT_VEC3_FLOAT: case RK_VERTEX_FORMAT_VEC3_FLOAT:
param->src_size = rk_vec3_float::get_input_size(); param->src_size = rk_vec3_float::get_input_size();
param->dst_size = rk_vec3_float::get_output_size(); param->dst_size = rk_vec3_float::get_output_size();
param->packer = rk_vec3_float::param_packer; 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); glVertexAttribFormat(attrib, 3, GL_FLOAT, GL_FALSE, 0);
glVertexAttribBinding(attrib++, binding); glVertexAttribBinding(attrib++, binding);
break; break;
case RK_PARAM_FORMAT_VEC3_SHORT: case RK_VERTEX_FORMAT_VEC3_SHORT:
param->src_size = rk_vec3_short::get_input_size(); param->src_size = rk_vec3_short::get_input_size();
param->dst_size = rk_vec3_short::get_output_size(); param->dst_size = rk_vec3_short::get_output_size();
param->packer = norm ? rk_vec3_short_norm::param_packer : rk_vec3_short::param_packer; 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); glVertexAttribFormat(attrib, 3, GL_SHORT, norm, 0);
glVertexAttribBinding(attrib++, binding); glVertexAttribBinding(attrib++, binding);
break; break;
case RK_PARAM_FORMAT_VEC3_INT10: case RK_VERTEX_FORMAT_VEC3_INT10:
param->src_size = rk_vec3_int10::get_input_size(); param->src_size = rk_vec3_int10::get_input_size();
param->dst_size = rk_vec3_int10::get_output_size(); param->dst_size = rk_vec3_int10::get_output_size();
param->packer = norm ? rk_vec3_int10_norm::param_packer : rk_vec3_int10::param_packer; 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); glVertexAttribFormat(attrib, 4, GL_INT_2_10_10_10_REV, norm, 0);
glVertexAttribBinding(attrib++, binding); glVertexAttribBinding(attrib++, binding);
break; break;
case RK_PARAM_FORMAT_MAT3_FLOAT: case RK_VERTEX_FORMAT_MAT3_FLOAT:
param->src_size = rk_mat3_float::get_input_size(); param->src_size = rk_mat3_float::get_input_size();
param->dst_size = rk_mat3_float::get_output_size(); param->dst_size = rk_mat3_float::get_output_size();
param->packer = rk_mat3_float::param_packer; 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)); glVertexAttribFormat(attrib, 3, GL_FLOAT, GL_FALSE, rk_mat3_float::get_output_offset(2));
glVertexAttribBinding(attrib++, binding); glVertexAttribBinding(attrib++, binding);
break; break;
case RK_PARAM_FORMAT_MAT3_INT10: case RK_VERTEX_FORMAT_MAT3_INT10:
param->src_size = rk_mat3_int10::get_input_size(); param->src_size = rk_mat3_int10::get_input_size();
param->dst_size = rk_mat3_int10::get_output_size(); param->dst_size = rk_mat3_int10::get_output_size();
param->packer = norm ? rk_mat3_int10_norm::param_packer : rk_mat3_int10::param_packer; 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; break;
} }
glVertexBindingDivisor(binding, 1); glVertexBindingDivisor(binding, 1);
param->src_len = param->src_size / sizeof(rk_param_output); param->src_len = param->src_size / sizeof(rk_vertex_output);
param->dst_len = param->dst_size / sizeof(rk_param_output); param->dst_len = param->dst_size / sizeof(rk_vertex_output);
param->source = new rk_param_input[max_size * param->src_len]; param->source = new rk_vertex_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;
} }
@ -674,7 +674,7 @@ static void rk_pack_batch(
if (param->dirty) { if (param->dirty) {
param->dirty = false; param->dirty = false;
if (batch.ninstances) { if (batch.ninstances) {
rk_param_output * const dst = reinterpret_cast<rk_param_output *>( rk_vertex_output * const dst = reinterpret_cast<rk_vertex_output *>(
glMapBufferRange(GL_ARRAY_BUFFER, param->offset, batch.ninstances * param->dst_size, glMapBufferRange(GL_ARRAY_BUFFER, param->offset, batch.ninstances * param->dst_size,
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT)); GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
if (dst) { if (dst) {
@ -695,7 +695,7 @@ void rk_fill_batch(
rk_uint count, rk_uint count,
rk_instance_flags const * flags, rk_instance_flags const * flags,
rk_mesh_index const * meshes, rk_mesh_index const * meshes,
rk_param_input const * const * params) { rk_vertex_input const * const * params) {
rk_batch const * const batch = reinterpret_cast<rk_batch const *>(_batch); rk_batch const * const batch = reinterpret_cast<rk_batch const *>(_batch);
if (!batch || !count || count > batch->max_size) { if (!batch || !count || count > batch->max_size) {
rk_printf("rk_fill_batch(): invalid params."); rk_printf("rk_fill_batch(): invalid params.");
@ -706,7 +706,7 @@ void rk_fill_batch(
if (batch->nparams) { if (batch->nparams) {
got_all_params = (params != nullptr); got_all_params = (params != nullptr);
if (params) { 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); bool const got_param = (*param != nullptr);
got_any_params |= got_param; got_any_params |= got_param;
got_all_params &= got_param; got_all_params &= got_param;
@ -730,7 +730,7 @@ void rk_fill_batch(
if (batch->nparams) { if (batch->nparams) {
rk_parameter const * const last_param = batch->params + batch->nparams; rk_parameter const * const last_param = batch->params + batch->nparams;
if (got_any_params) { 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) { for (rk_parameter const * param = batch->params; param < last_param; ++param, ++src) {
param->dirty = param->dirty =
((*src && rk_cmp_memcpy(param->source, *src, batch->count * param->src_len)) || need_sorting); ((*src && rk_cmp_memcpy(param->source, *src, batch->count * param->src_len)) || need_sorting);

View File

@ -23,8 +23,6 @@
static_assert(sizeof(rk_vertex_output) == 4); static_assert(sizeof(rk_vertex_output) == 4);
typedef rk_vertex_output rk_param_output;
struct rk_shader { struct rk_shader {
GLuint vertex; GLuint vertex;
GLuint fragment; GLuint fragment;
@ -65,8 +63,8 @@ struct rk_command {
typedef void (*rk_packer)( typedef void (*rk_packer)(
unsigned const, // count unsigned const, // count
rk_instance_index const * const __restrict, // indices rk_instance_index const * const __restrict, // indices
rk_param_output * __restrict, // dst rk_vertex_output * __restrict, // dst
rk_param_input const * const __restrict); // src rk_vertex_input const * const __restrict); // src
struct rk_parameter { struct rk_parameter {
mutable bool dirty; mutable bool dirty;
@ -76,7 +74,7 @@ struct rk_parameter {
unsigned src_len; unsigned src_len;
unsigned dst_size; unsigned dst_size;
unsigned dst_len; unsigned dst_len;
rk_param_input * source; rk_vertex_input * source;
rk_packer packer; rk_packer packer;
}; };

View File

@ -231,8 +231,8 @@ struct rk_format {
static void param_packer( static void param_packer(
unsigned const count, unsigned const count,
rk_instance_index const * const __restrict indices, rk_instance_index const * const __restrict indices,
rk_param_output * __restrict _dst, rk_vertex_output * __restrict _dst,
rk_param_input const * const __restrict _src) { rk_vertex_input const * const __restrict _src) {
rk_instance_index const * const last_index = indices + count; rk_instance_index const * const last_index = indices + count;
output_format * __restrict dst = reinterpret_cast<output_format *>(_dst); output_format * __restrict dst = reinterpret_cast<output_format *>(_dst);
input_format const * const __restrict src = reinterpret_cast<input_format const *>(_src); input_format const * const __restrict src = reinterpret_cast<input_format const *>(_src);