diff --git a/__init__.py b/__init__.py index d3070a9..5547944 100644 --- a/__init__.py +++ b/__init__.py @@ -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 diff --git a/cpp/render.hpp b/cpp/render.hpp index c33de92..1b38e06 100644 --- a/cpp/render.hpp +++ b/cpp/render.hpp @@ -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(); diff --git a/cpp/render/render_opengles.cpp b/cpp/render/render_opengles.cpp index bf60410..614257e 100644 --- a/cpp/render/render_opengles.cpp +++ b/cpp/render/render_opengles.cpp @@ -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(_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(index); } } - commands->nvertices = static_cast(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(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( diff --git a/cpp/render/render_opengles.hpp b/cpp/render/render_opengles.hpp index e51e2e0..892b9ff 100644 --- a/cpp/render/render_opengles.hpp +++ b/cpp/render/render_opengles.hpp @@ -101,6 +101,7 @@ struct rk_parameter { struct rk_batch { unsigned max_size; + unsigned max_meshes; unsigned nparams; unsigned commands_size; unsigned packed_size;