diff --git a/cpp/render/render_opengles.cpp b/cpp/render/render_opengles.cpp index 6a36b29..08ae93a 100644 --- a/cpp/render/render_opengles.cpp +++ b/cpp/render/render_opengles.cpp @@ -351,6 +351,8 @@ rk_vertices_t rk_create_vertices( memcpy(vertices->vertices, _vertices, nvertices * vertex_size); vertices->indices = new rk_ushort[nindices]; memcpy(vertices->indices, indices, nindices * sizeof(rk_ushort)); + vertices->vertices_buffer = 0; + vertices->indices_buffer = 0; return reinterpret_cast(vertices); } @@ -490,7 +492,7 @@ rk_batch_t rk_create_batch( rk_uint max_size, rk_uint max_meshes, rk_param_format const * params_format) { - rk_vertices const * const vertices = reinterpret_cast(_vertices); + rk_vertices * const vertices = reinterpret_cast(_vertices); if (!vertices || !max_size || max_size > RK_BATCH_MAX_SIZE) { rk_printf("rk_create_batch(): invalid params."); return RK_INVALID_HANDLE; @@ -555,13 +557,19 @@ rk_batch_t rk_create_batch( } glGenVertexArrays(1, &batch->vertex_array); glBindVertexArray(batch->vertex_array); - glGenBuffers(1, &batch->vertices_buffer); - glBindBuffer(GL_ARRAY_BUFFER, batch->vertices_buffer); - glBufferData(GL_ARRAY_BUFFER, vertices->nvertices * vertex_size, vertices->vertices, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glGenBuffers(1, &batch->indices_buffer); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch->indices_buffer); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertices->nindices * sizeof(rk_ushort), vertices->indices, GL_STATIC_DRAW); + if (!vertices->vertices_buffer) { + glGenBuffers(1, &vertices->vertices_buffer); + glBindBuffer(GL_ARRAY_BUFFER, vertices->vertices_buffer); + glBufferData(GL_ARRAY_BUFFER, vertices->nvertices * vertex_size, vertices->vertices, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } + if (vertices->indices_buffer) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertices->indices_buffer); + } else { + glGenBuffers(1, &vertices->indices_buffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertices->indices_buffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertices->nindices * sizeof(rk_ushort), vertices->indices, GL_STATIC_DRAW); + } if (rk_MultiDrawElementsIndirect) { glGenBuffers(1, &batch->commands_buffer); glBindBuffer(GL_DRAW_INDIRECT_BUFFER, batch->commands_buffer); @@ -577,7 +585,7 @@ rk_batch_t rk_create_batch( unsigned binding = 0; unsigned attrib = 0; unsigned offset = 0; - glBindVertexBuffer(binding, batch->vertices_buffer, 0, vertex_size); + glBindVertexBuffer(binding, vertices->vertices_buffer, 0, vertex_size); for (rk_vertex_format const * f = vertices->format; *f; ++f) { GLboolean const norm = (*f & RK_VERTEX_FORMAT_NORMALIZE) != 0; switch (*f & RK_VERTEX_FORMAT_MASK) { @@ -1029,8 +1037,6 @@ void rk_destroy_batch( delete[] batch->params; glDeleteBuffers(1, &batch->params_buffer); } - glDeleteBuffers(1, &batch->indices_buffer); - glDeleteBuffers(1, &batch->vertices_buffer); glDeleteVertexArrays(1, &batch->vertex_array); delete batch; } @@ -1053,6 +1059,12 @@ void rk_destroy_vertices( delete[] vertices->format; delete[] vertices->vertices; delete[] vertices->indices; + if (vertices->vertices_buffer) { + glDeleteBuffers(1, &vertices->vertices_buffer); + } + if (vertices->indices_buffer) { + glDeleteBuffers(1, &vertices->indices_buffer); + } delete vertices; } } diff --git a/cpp/render/render_opengles.hpp b/cpp/render/render_opengles.hpp index 7906238..8d66bbf 100644 --- a/cpp/render/render_opengles.hpp +++ b/cpp/render/render_opengles.hpp @@ -44,6 +44,8 @@ struct rk_vertices { rk_vertex_format * format; rk_ubyte * vertices; rk_ushort * indices; + GLuint vertices_buffer; + GLuint indices_buffer; }; struct rk_command { @@ -119,8 +121,6 @@ struct rk_batch { rk_command * commands; rk_parameter * params; GLuint vertex_array; - GLuint vertices_buffer; - GLuint indices_buffer; GLuint commands_buffer; GLuint params_buffer; };