Move vertices and indicies buffers to vertices.

This commit is contained in:
Roz K 2023-01-03 11:38:19 +01:00
parent a91a852887
commit 66980e6ea9
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
2 changed files with 25 additions and 13 deletions

View File

@ -351,6 +351,8 @@ rk_vertices_t rk_create_vertices(
memcpy(vertices->vertices, _vertices, nvertices * vertex_size); memcpy(vertices->vertices, _vertices, nvertices * vertex_size);
vertices->indices = new rk_ushort[nindices]; vertices->indices = new rk_ushort[nindices];
memcpy(vertices->indices, indices, nindices * sizeof(rk_ushort)); memcpy(vertices->indices, indices, nindices * sizeof(rk_ushort));
vertices->vertices_buffer = 0;
vertices->indices_buffer = 0;
return reinterpret_cast<rk_vertices_t>(vertices); return reinterpret_cast<rk_vertices_t>(vertices);
} }
@ -490,7 +492,7 @@ rk_batch_t rk_create_batch(
rk_uint max_size, rk_uint max_size,
rk_uint max_meshes, rk_uint max_meshes,
rk_param_format const * params_format) { rk_param_format const * params_format) {
rk_vertices const * const vertices = reinterpret_cast<rk_vertices const *>(_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.");
return RK_INVALID_HANDLE; return RK_INVALID_HANDLE;
@ -555,13 +557,19 @@ rk_batch_t rk_create_batch(
} }
glGenVertexArrays(1, &batch->vertex_array); glGenVertexArrays(1, &batch->vertex_array);
glBindVertexArray(batch->vertex_array); glBindVertexArray(batch->vertex_array);
glGenBuffers(1, &batch->vertices_buffer); if (!vertices->vertices_buffer) {
glBindBuffer(GL_ARRAY_BUFFER, batch->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); glBufferData(GL_ARRAY_BUFFER, vertices->nvertices * vertex_size, vertices->vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &batch->indices_buffer); }
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch->indices_buffer); 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); glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertices->nindices * sizeof(rk_ushort), vertices->indices, GL_STATIC_DRAW);
}
if (rk_MultiDrawElementsIndirect) { if (rk_MultiDrawElementsIndirect) {
glGenBuffers(1, &batch->commands_buffer); glGenBuffers(1, &batch->commands_buffer);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 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 binding = 0;
unsigned attrib = 0; unsigned attrib = 0;
unsigned offset = 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) { for (rk_vertex_format const * f = vertices->format; *f; ++f) {
GLboolean const norm = (*f & RK_VERTEX_FORMAT_NORMALIZE) != 0; GLboolean const norm = (*f & RK_VERTEX_FORMAT_NORMALIZE) != 0;
switch (*f & RK_VERTEX_FORMAT_MASK) { switch (*f & RK_VERTEX_FORMAT_MASK) {
@ -1029,8 +1037,6 @@ void rk_destroy_batch(
delete[] batch->params; delete[] batch->params;
glDeleteBuffers(1, &batch->params_buffer); glDeleteBuffers(1, &batch->params_buffer);
} }
glDeleteBuffers(1, &batch->indices_buffer);
glDeleteBuffers(1, &batch->vertices_buffer);
glDeleteVertexArrays(1, &batch->vertex_array); glDeleteVertexArrays(1, &batch->vertex_array);
delete batch; delete batch;
} }
@ -1053,6 +1059,12 @@ void rk_destroy_vertices(
delete[] vertices->format; delete[] vertices->format;
delete[] vertices->vertices; delete[] vertices->vertices;
delete[] vertices->indices; delete[] vertices->indices;
if (vertices->vertices_buffer) {
glDeleteBuffers(1, &vertices->vertices_buffer);
}
if (vertices->indices_buffer) {
glDeleteBuffers(1, &vertices->indices_buffer);
}
delete vertices; delete vertices;
} }
} }

View File

@ -44,6 +44,8 @@ struct rk_vertices {
rk_vertex_format * format; rk_vertex_format * format;
rk_ubyte * vertices; rk_ubyte * vertices;
rk_ushort * indices; rk_ushort * indices;
GLuint vertices_buffer;
GLuint indices_buffer;
}; };
struct rk_command { struct rk_command {
@ -119,8 +121,6 @@ struct rk_batch {
rk_command * commands; rk_command * commands;
rk_parameter * params; rk_parameter * params;
GLuint vertex_array; GLuint vertex_array;
GLuint vertices_buffer;
GLuint indices_buffer;
GLuint commands_buffer; GLuint commands_buffer;
GLuint params_buffer; GLuint params_buffer;
}; };