Batch commands memory optim.

This commit is contained in:
Roz K 2022-12-30 14:49:00 +01:00
parent 628f8bcaa4
commit 3b21e1610a
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
4 changed files with 16 additions and 11 deletions

View File

@ -355,6 +355,7 @@ create_batch.restype = ctypes.c_void_p
create_batch.argtypes = (
ctypes.c_void_p, # vertices
ctypes.c_uint, # max_size
ctypes.c_uint, # max_meshes
ctypes.c_char_p) # params_format
begin_frame = _engine.rk_begin_frame

View File

@ -121,6 +121,7 @@ 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_uint max_meshes,
rk_param_format const * params_format);
RK_EXPORT void rk_begin_frame();

View File

@ -484,6 +484,7 @@ static void rk_pack_mat3_int10_norm(
rk_batch_t rk_create_batch(
rk_vertices_t _vertices,
rk_uint max_size,
rk_uint max_meshes,
rk_param_format const * params_format) {
rk_vertices const * const vertices = reinterpret_cast<rk_vertices const *>(_vertices);
if (!vertices || !max_size || max_size > RK_BATCH_MAX_SIZE) {
@ -496,6 +497,7 @@ rk_batch_t rk_create_batch(
}
rk_batch * batch = new rk_batch;
batch->max_size = max_size;
batch->max_meshes = max_meshes;
batch->nparams = nparams;
batch->commands_size = 0;
batch->packed_size = 0;
@ -542,11 +544,10 @@ rk_batch_t rk_create_batch(
packed_size += max_size * param->size;
}
}
batch->commands_size = max_size * sizeof(rk_command);
batch->commands_size = max_meshes * sizeof(rk_command);
batch->packed_size = packed_size;
batch->indices = new rk_ushort[max_size];
batch->commands = new rk_command[max_size];
memset(batch->commands, 0, batch->commands_size);
batch->commands = new rk_command[max_meshes];
if (rk_MultiDrawElementsIndirect) {
glGenBuffers(1, &batch->commands_buffer);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, batch->commands_buffer);
@ -733,10 +734,11 @@ static unsigned rk_batch_build_commands(
rk_batch & batch,
unsigned const ninstances,
rk_mesh const * const meshes) {
rk_command * commands = batch.commands;
rk_command * const last_command = batch.commands + batch.max_meshes;
rk_command * command = batch.commands;
rk_ushort * base = batch.indices;
rk_ushort * const last = batch.indices + ninstances;
for (rk_ushort * first = batch.indices; first < last; base = first, ++commands) {
for (rk_ushort * first = batch.indices; first < last && command < last_command; base = first, ++command) {
rk_mesh const & mesh = meshes[*first++];
for ( ; first < last && meshes[*first].packed == mesh.packed; ++first) {
}
@ -747,13 +749,13 @@ static unsigned rk_batch_build_commands(
*first++ = static_cast<rk_ushort>(index);
}
}
commands->nvertices = static_cast<GLuint>(mesh.ntriangles) * 3;
commands->ninstances = first - base;
commands->base_index = mesh.base_index;
commands->base_vertex = 0;
commands->base_instance = base - batch.indices;
command->nvertices = static_cast<GLuint>(mesh.ntriangles) * 3;
command->ninstances = first - base;
command->base_index = mesh.base_index;
command->base_vertex = 0;
command->base_instance = base - batch.indices;
}
return commands - batch.commands;
return command - batch.commands;
}
static void rk_batch_pack(

View File

@ -101,6 +101,7 @@ struct rk_parameter {
struct rk_batch {
unsigned max_size;
unsigned max_meshes;
unsigned nparams;
unsigned commands_size;
unsigned packed_size;