Cleanup vertex and params creation, and switch commands and params buffers to dynamic draw.

This commit is contained in:
Roz K 2022-12-27 06:27:08 +01:00
parent af0edd5e6d
commit 269d7821c8
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
2 changed files with 69 additions and 29 deletions

View File

@ -17,6 +17,7 @@
#include "render_opengles.hpp"
#include "../display/display_glx.hpp"
#include <cstdio>
#include <cstring>
static rk_shader const * rk_current_shader = nullptr;
static rk_vertices const * rk_current_vertices = nullptr;
@ -257,16 +258,20 @@ rk_vertices_t rk_create_vertices(
return nullptr;
}
unsigned vertex_size = 0;
unsigned nattribs = 0;
for (rk_vertex_format const * f = format; *f; ++f) {
switch (*f & RK_VERTEX_FORMAT_MASK) {
case RK_VERTEX_FORMAT_VEC3_FLOAT:
vertex_size += sizeof(rk_vec3);
vertex_size += sizeof(rk_vec3_float);
nattribs += 1;
break;
case RK_VERTEX_FORMAT_VEC3_INT10:
vertex_size += sizeof(rk_int);
vertex_size += sizeof(rk_vec3_int10);
nattribs += 1;
break;
case RK_VERTEX_FORMAT_VEC3_UINT10:
vertex_size += sizeof(rk_uint);
vertex_size += sizeof(rk_vec3_uint10);
nattribs += 1;
break;
default:
rk_printf("rk_create_vertices(): invalid format.");
@ -279,35 +284,39 @@ rk_vertices_t rk_create_vertices(
return nullptr;
}
rk_vertices * const vertices = new rk_vertices;
vertices->vertex_size = vertex_size;
glGenVertexArrays(1, &vertices->array);
glBindVertexArray(vertices->array);
glGenBuffers(1, &vertices->vertices);
glBindBuffer(GL_ARRAY_BUFFER, vertices->vertices);
glBufferData(GL_ARRAY_BUFFER, nvertices * vertex_size, _vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
vertices->vertex_size = vertex_size;
glBindVertexBuffer(RK_VERTICES_BINDING, vertices->vertices, 0, vertices->vertex_size);
for (unsigned attrib = 0; attrib < nattribs; ++attrib) {
glEnableVertexAttribArray(attrib);
}
vertices->layout = 0;
unsigned offset = 0;
for (rk_vertex_format const * f = format; *f; ++f, ++vertices->layout) {
glEnableVertexAttribArray(vertices->layout);
for (rk_vertex_format const * f = format; *f; ++f) {
GLboolean const norm = (*f & RK_VERTEX_FORMAT_NORMALIZE) != 0;
switch (*f & RK_VERTEX_FORMAT_MASK) {
case RK_VERTEX_FORMAT_VEC3_FLOAT:
glVertexAttribFormat(vertices->layout, 3, GL_FLOAT, norm, offset);
offset += sizeof(rk_vec3);
glVertexAttribFormat(vertices->layout++, 3, GL_FLOAT, GL_FALSE, offset);
offset += sizeof(rk_vec3_float);
break;
case RK_VERTEX_FORMAT_VEC3_INT10:
glVertexAttribFormat(vertices->layout, 4, GL_INT_2_10_10_10_REV, norm, offset);
offset += sizeof(rk_int);
glVertexAttribFormat(vertices->layout++, 4, GL_INT_2_10_10_10_REV, norm, offset);
offset += sizeof(rk_vec3_int10);
break;
case RK_VERTEX_FORMAT_VEC3_UINT10:
glVertexAttribFormat(vertices->layout, 4, GL_UNSIGNED_INT_2_10_10_10_REV, norm, offset);
offset += sizeof(rk_uint);
glVertexAttribFormat(vertices->layout++, 4, GL_UNSIGNED_INT_2_10_10_10_REV, norm, offset);
offset += sizeof(rk_vec3_uint10);
break;
}
glVertexAttribBinding(vertices->layout, RK_VERTICES_BINDING);
}
glBindVertexBuffer(RK_VERTICES_BINDING, vertices->vertices, 0, vertices->vertex_size);
for (unsigned attrib = 0; attrib < nattribs; ++attrib) {
glVertexAttribBinding(attrib, RK_VERTICES_BINDING);
}
glGenBuffers(1, &vertices->indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertices->indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, nindices * sizeof(rk_ushort), indices, GL_STATIC_DRAW);
@ -402,27 +411,33 @@ rk_batch_t rk_create_batch(
unsigned nparams = 0;
unsigned params_size = 0;
unsigned packed_size = 0;
unsigned nattribs = 0;
for (rk_param_format const * f = params_format; *f; ++f, ++nparams) {
switch (*f & RK_PARAM_FORMAT_MASK) {
case RK_PARAM_FORMAT_VEC3_FLOAT:
params_size += sizeof(rk_vec3_float);
packed_size += sizeof(rk_vec3_float);
nattribs += 1;
break;
case RK_PARAM_FORMAT_VEC3_SHORT:
params_size += sizeof(rk_vec3_float);
packed_size += sizeof(rk_vec3_short);
nattribs += 1;
break;
case RK_PARAM_FORMAT_VEC3_INT10:
params_size += sizeof(rk_vec3_float);
packed_size += sizeof(rk_vec3_int10);
nattribs += 1;
break;
case RK_PARAM_FORMAT_MAT3_FLOAT:
params_size += sizeof(rk_mat3_float);
packed_size += sizeof(rk_mat3_float);
nattribs += 3;
break;
case RK_PARAM_FORMAT_MAT3_INT10:
params_size += sizeof(rk_mat3_float);
packed_size += sizeof(rk_mat3_int10);
nattribs += 3;
break;
default:
rk_printf("rk_create_batch(): invalid param format.");
@ -430,6 +445,7 @@ rk_batch_t rk_create_batch(
break;
}
}
glBindVertexArray(vertices->array);
rk_batch * batch = new rk_batch;
batch->size = max_size;
batch->nparams = nparams;
@ -437,14 +453,32 @@ rk_batch_t rk_create_batch(
batch->packed_size = packed_size;
batch->indices = new rk_ushort[max_size];
batch->commands = new rk_command[max_size * sizeof(rk_command)];
memset(batch->commands, 0, max_size * sizeof(rk_command));
if (rk_MultiDrawElementsIndirect) {
glGenBuffers(1, &batch->commands_buffer);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, batch->commands_buffer);
glBufferData(GL_DRAW_INDIRECT_BUFFER, max_size * sizeof(rk_command), batch->commands, GL_DYNAMIC_DRAW);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
} else {
batch->commands_buffer = 0;
}
batch->packers = nullptr;
batch->params = nullptr;
if (nparams) {
batch->packers = new rk_packer[nparams];
batch->params = new rk_ubyte[max_size * packed_size];
memset(batch->params, 0, max_size * packed_size);
glGenBuffers(1, &batch->params_buffer);
glBindBuffer(GL_ARRAY_BUFFER, batch->params_buffer);
glBufferData(GL_ARRAY_BUFFER, max_size * batch->packed_size, batch->params, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexBuffer(RK_PARAMS_BINDING, batch->params_buffer, 0, batch->packed_size);
for (unsigned attrib = vertices->layout; attrib < vertices->layout + nattribs; ++attrib) {
glEnableVertexAttribArray(attrib);
}
rk_packer * packer = batch->packers;
unsigned layout = vertices->layout;
unsigned offset = 0;
glBindVertexArray(vertices->array);
for (rk_param_format const * f = params_format; *f; ++f, ++packer) {
GLboolean const norm = (*f & RK_PARAM_FORMAT_NORMALIZE) != 0;
switch (*f & RK_PARAM_FORMAT_MASK) {
@ -497,17 +531,14 @@ rk_batch_t rk_create_batch(
}
offset += packer->dst_incr;
}
for (unsigned attrib = vertices->layout; attrib < layout; ++attrib) {
glEnableVertexAttribArray(attrib);
for (unsigned attrib = vertices->layout; attrib < vertices->layout + nattribs; ++attrib) {
glVertexAttribBinding(attrib, RK_PARAMS_BINDING);
}
glVertexBindingDivisor(RK_PARAMS_BINDING, 1);
glBindVertexBuffer(RK_PARAMS_BINDING, batch->params_buffer, 0, batch->packed_size);
glBindVertexArray(0);
}
if (rk_MultiDrawElementsIndirect) {
glGenBuffers(1, &batch->commands_buffer);
} else {
batch->params_buffer = 0;
}
glBindVertexArray(0);
return batch;
}
@ -687,21 +718,23 @@ void rk_draw_batch(
if (!ninstances) {
return;
}
unsigned const ncommands = rk_batch_build_commands(batch, ninstances, meshes);
if (rk_MultiDrawElementsIndirect) {
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, batch.commands_buffer);
glBufferData(GL_DRAW_INDIRECT_BUFFER, ncommands * sizeof(rk_command), batch.commands, GL_STREAM_DRAW);
}
if (batch.nparams) {
glBindBuffer(GL_ARRAY_BUFFER, batch.params_buffer);
}
unsigned const ncommands = rk_batch_build_commands(batch, ninstances, meshes);
if (rk_MultiDrawElementsIndirect) {
glBufferSubData(GL_DRAW_INDIRECT_BUFFER, 0, ncommands * sizeof(rk_command), batch.commands);
}
if (batch.nparams) {
rk_batch_pack(batch, ninstances, params);
glBindBuffer(GL_ARRAY_BUFFER, batch.params_buffer);
glBufferData(GL_ARRAY_BUFFER, ninstances * batch.packed_size, batch.params, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBufferSubData(GL_ARRAY_BUFFER, 0, ninstances * batch.packed_size, batch.params);
}
if (rk_DrawElementsInstancedBaseInstance) {
if (rk_MultiDrawElementsIndirect) {
rk_MultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_SHORT, nullptr, ncommands, sizeof(rk_command));
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
} else {
rk_command const * const last_command = batch.commands + ncommands;
for (rk_command const * command = batch.commands; command < last_command; ++command) {
@ -725,6 +758,12 @@ void rk_draw_batch(
command->ninstances);
}
}
if (rk_MultiDrawElementsIndirect) {
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
}
if (batch.nparams) {
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
void rk_unselect_vertices(

View File

@ -65,6 +65,7 @@ struct rk_vec3_short {
};
typedef rk_int rk_vec3_int10;
typedef rk_uint rk_vec3_uint10;
struct rk_mat3_float {
rk_vec3_float x;
@ -124,8 +125,8 @@ struct rk_batch {
rk_command * commands;
rk_packer * packers;
rk_ubyte * params;
GLuint params_buffer;
GLuint commands_buffer;
GLuint params_buffer;
};
#endif // _RK_ENGINE_RENDER_OPENGLES_H