Cleanup vertex and params creation, and switch commands and params buffers to dynamic draw.
This commit is contained in:
parent
af0edd5e6d
commit
269d7821c8
@ -17,6 +17,7 @@
|
|||||||
#include "render_opengles.hpp"
|
#include "render_opengles.hpp"
|
||||||
#include "../display/display_glx.hpp"
|
#include "../display/display_glx.hpp"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
static rk_shader const * rk_current_shader = nullptr;
|
static rk_shader const * rk_current_shader = nullptr;
|
||||||
static rk_vertices const * rk_current_vertices = nullptr;
|
static rk_vertices const * rk_current_vertices = nullptr;
|
||||||
@ -257,16 +258,20 @@ rk_vertices_t rk_create_vertices(
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
unsigned vertex_size = 0;
|
unsigned vertex_size = 0;
|
||||||
|
unsigned nattribs = 0;
|
||||||
for (rk_vertex_format const * f = format; *f; ++f) {
|
for (rk_vertex_format const * f = format; *f; ++f) {
|
||||||
switch (*f & RK_VERTEX_FORMAT_MASK) {
|
switch (*f & RK_VERTEX_FORMAT_MASK) {
|
||||||
case RK_VERTEX_FORMAT_VEC3_FLOAT:
|
case RK_VERTEX_FORMAT_VEC3_FLOAT:
|
||||||
vertex_size += sizeof(rk_vec3);
|
vertex_size += sizeof(rk_vec3_float);
|
||||||
|
nattribs += 1;
|
||||||
break;
|
break;
|
||||||
case RK_VERTEX_FORMAT_VEC3_INT10:
|
case RK_VERTEX_FORMAT_VEC3_INT10:
|
||||||
vertex_size += sizeof(rk_int);
|
vertex_size += sizeof(rk_vec3_int10);
|
||||||
|
nattribs += 1;
|
||||||
break;
|
break;
|
||||||
case RK_VERTEX_FORMAT_VEC3_UINT10:
|
case RK_VERTEX_FORMAT_VEC3_UINT10:
|
||||||
vertex_size += sizeof(rk_uint);
|
vertex_size += sizeof(rk_vec3_uint10);
|
||||||
|
nattribs += 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
rk_printf("rk_create_vertices(): invalid format.");
|
rk_printf("rk_create_vertices(): invalid format.");
|
||||||
@ -279,35 +284,39 @@ rk_vertices_t rk_create_vertices(
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
rk_vertices * const vertices = new rk_vertices;
|
rk_vertices * const vertices = new rk_vertices;
|
||||||
|
vertices->vertex_size = vertex_size;
|
||||||
glGenVertexArrays(1, &vertices->array);
|
glGenVertexArrays(1, &vertices->array);
|
||||||
glBindVertexArray(vertices->array);
|
glBindVertexArray(vertices->array);
|
||||||
glGenBuffers(1, &vertices->vertices);
|
glGenBuffers(1, &vertices->vertices);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertices->vertices);
|
glBindBuffer(GL_ARRAY_BUFFER, vertices->vertices);
|
||||||
glBufferData(GL_ARRAY_BUFFER, nvertices * vertex_size, _vertices, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, nvertices * vertex_size, _vertices, GL_STATIC_DRAW);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
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;
|
vertices->layout = 0;
|
||||||
unsigned offset = 0;
|
unsigned offset = 0;
|
||||||
for (rk_vertex_format const * f = format; *f; ++f, ++vertices->layout) {
|
for (rk_vertex_format const * f = format; *f; ++f) {
|
||||||
glEnableVertexAttribArray(vertices->layout);
|
|
||||||
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) {
|
||||||
case RK_VERTEX_FORMAT_VEC3_FLOAT:
|
case RK_VERTEX_FORMAT_VEC3_FLOAT:
|
||||||
glVertexAttribFormat(vertices->layout, 3, GL_FLOAT, norm, offset);
|
glVertexAttribFormat(vertices->layout++, 3, GL_FLOAT, GL_FALSE, offset);
|
||||||
offset += sizeof(rk_vec3);
|
offset += sizeof(rk_vec3_float);
|
||||||
break;
|
break;
|
||||||
case RK_VERTEX_FORMAT_VEC3_INT10:
|
case RK_VERTEX_FORMAT_VEC3_INT10:
|
||||||
glVertexAttribFormat(vertices->layout, 4, GL_INT_2_10_10_10_REV, norm, offset);
|
glVertexAttribFormat(vertices->layout++, 4, GL_INT_2_10_10_10_REV, norm, offset);
|
||||||
offset += sizeof(rk_int);
|
offset += sizeof(rk_vec3_int10);
|
||||||
break;
|
break;
|
||||||
case RK_VERTEX_FORMAT_VEC3_UINT10:
|
case RK_VERTEX_FORMAT_VEC3_UINT10:
|
||||||
glVertexAttribFormat(vertices->layout, 4, GL_UNSIGNED_INT_2_10_10_10_REV, norm, offset);
|
glVertexAttribFormat(vertices->layout++, 4, GL_UNSIGNED_INT_2_10_10_10_REV, norm, offset);
|
||||||
offset += sizeof(rk_uint);
|
offset += sizeof(rk_vec3_uint10);
|
||||||
break;
|
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);
|
glGenBuffers(1, &vertices->indices);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertices->indices);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertices->indices);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, nindices * sizeof(rk_ushort), indices, GL_STATIC_DRAW);
|
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 nparams = 0;
|
||||||
unsigned params_size = 0;
|
unsigned params_size = 0;
|
||||||
unsigned packed_size = 0;
|
unsigned packed_size = 0;
|
||||||
|
unsigned nattribs = 0;
|
||||||
for (rk_param_format const * f = params_format; *f; ++f, ++nparams) {
|
for (rk_param_format const * f = params_format; *f; ++f, ++nparams) {
|
||||||
switch (*f & RK_PARAM_FORMAT_MASK) {
|
switch (*f & RK_PARAM_FORMAT_MASK) {
|
||||||
case RK_PARAM_FORMAT_VEC3_FLOAT:
|
case RK_PARAM_FORMAT_VEC3_FLOAT:
|
||||||
params_size += sizeof(rk_vec3_float);
|
params_size += sizeof(rk_vec3_float);
|
||||||
packed_size += sizeof(rk_vec3_float);
|
packed_size += sizeof(rk_vec3_float);
|
||||||
|
nattribs += 1;
|
||||||
break;
|
break;
|
||||||
case RK_PARAM_FORMAT_VEC3_SHORT:
|
case RK_PARAM_FORMAT_VEC3_SHORT:
|
||||||
params_size += sizeof(rk_vec3_float);
|
params_size += sizeof(rk_vec3_float);
|
||||||
packed_size += sizeof(rk_vec3_short);
|
packed_size += sizeof(rk_vec3_short);
|
||||||
|
nattribs += 1;
|
||||||
break;
|
break;
|
||||||
case RK_PARAM_FORMAT_VEC3_INT10:
|
case RK_PARAM_FORMAT_VEC3_INT10:
|
||||||
params_size += sizeof(rk_vec3_float);
|
params_size += sizeof(rk_vec3_float);
|
||||||
packed_size += sizeof(rk_vec3_int10);
|
packed_size += sizeof(rk_vec3_int10);
|
||||||
|
nattribs += 1;
|
||||||
break;
|
break;
|
||||||
case RK_PARAM_FORMAT_MAT3_FLOAT:
|
case RK_PARAM_FORMAT_MAT3_FLOAT:
|
||||||
params_size += sizeof(rk_mat3_float);
|
params_size += sizeof(rk_mat3_float);
|
||||||
packed_size += sizeof(rk_mat3_float);
|
packed_size += sizeof(rk_mat3_float);
|
||||||
|
nattribs += 3;
|
||||||
break;
|
break;
|
||||||
case RK_PARAM_FORMAT_MAT3_INT10:
|
case RK_PARAM_FORMAT_MAT3_INT10:
|
||||||
params_size += sizeof(rk_mat3_float);
|
params_size += sizeof(rk_mat3_float);
|
||||||
packed_size += sizeof(rk_mat3_int10);
|
packed_size += sizeof(rk_mat3_int10);
|
||||||
|
nattribs += 3;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
rk_printf("rk_create_batch(): invalid param format.");
|
rk_printf("rk_create_batch(): invalid param format.");
|
||||||
@ -430,6 +445,7 @@ rk_batch_t rk_create_batch(
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
glBindVertexArray(vertices->array);
|
||||||
rk_batch * batch = new rk_batch;
|
rk_batch * batch = new rk_batch;
|
||||||
batch->size = max_size;
|
batch->size = max_size;
|
||||||
batch->nparams = nparams;
|
batch->nparams = nparams;
|
||||||
@ -437,14 +453,32 @@ rk_batch_t rk_create_batch(
|
|||||||
batch->packed_size = packed_size;
|
batch->packed_size = packed_size;
|
||||||
batch->indices = new rk_ushort[max_size];
|
batch->indices = new rk_ushort[max_size];
|
||||||
batch->commands = new rk_command[max_size * sizeof(rk_command)];
|
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) {
|
if (nparams) {
|
||||||
batch->packers = new rk_packer[nparams];
|
batch->packers = new rk_packer[nparams];
|
||||||
batch->params = new rk_ubyte[max_size * packed_size];
|
batch->params = new rk_ubyte[max_size * packed_size];
|
||||||
|
memset(batch->params, 0, max_size * packed_size);
|
||||||
glGenBuffers(1, &batch->params_buffer);
|
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;
|
rk_packer * packer = batch->packers;
|
||||||
unsigned layout = vertices->layout;
|
unsigned layout = vertices->layout;
|
||||||
unsigned offset = 0;
|
unsigned offset = 0;
|
||||||
glBindVertexArray(vertices->array);
|
|
||||||
for (rk_param_format const * f = params_format; *f; ++f, ++packer) {
|
for (rk_param_format const * f = params_format; *f; ++f, ++packer) {
|
||||||
GLboolean const norm = (*f & RK_PARAM_FORMAT_NORMALIZE) != 0;
|
GLboolean const norm = (*f & RK_PARAM_FORMAT_NORMALIZE) != 0;
|
||||||
switch (*f & RK_PARAM_FORMAT_MASK) {
|
switch (*f & RK_PARAM_FORMAT_MASK) {
|
||||||
@ -497,17 +531,14 @@ rk_batch_t rk_create_batch(
|
|||||||
}
|
}
|
||||||
offset += packer->dst_incr;
|
offset += packer->dst_incr;
|
||||||
}
|
}
|
||||||
for (unsigned attrib = vertices->layout; attrib < layout; ++attrib) {
|
for (unsigned attrib = vertices->layout; attrib < vertices->layout + nattribs; ++attrib) {
|
||||||
glEnableVertexAttribArray(attrib);
|
|
||||||
glVertexAttribBinding(attrib, RK_PARAMS_BINDING);
|
glVertexAttribBinding(attrib, RK_PARAMS_BINDING);
|
||||||
}
|
}
|
||||||
glVertexBindingDivisor(RK_PARAMS_BINDING, 1);
|
glVertexBindingDivisor(RK_PARAMS_BINDING, 1);
|
||||||
glBindVertexBuffer(RK_PARAMS_BINDING, batch->params_buffer, 0, batch->packed_size);
|
} else {
|
||||||
glBindVertexArray(0);
|
batch->params_buffer = 0;
|
||||||
}
|
|
||||||
if (rk_MultiDrawElementsIndirect) {
|
|
||||||
glGenBuffers(1, &batch->commands_buffer);
|
|
||||||
}
|
}
|
||||||
|
glBindVertexArray(0);
|
||||||
return batch;
|
return batch;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -687,21 +718,23 @@ void rk_draw_batch(
|
|||||||
if (!ninstances) {
|
if (!ninstances) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
unsigned const ncommands = rk_batch_build_commands(batch, ninstances, meshes);
|
|
||||||
if (rk_MultiDrawElementsIndirect) {
|
if (rk_MultiDrawElementsIndirect) {
|
||||||
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, batch.commands_buffer);
|
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) {
|
if (batch.nparams) {
|
||||||
rk_batch_pack(batch, ninstances, params);
|
rk_batch_pack(batch, ninstances, params);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, batch.params_buffer);
|
glBufferSubData(GL_ARRAY_BUFFER, 0, ninstances * batch.packed_size, batch.params);
|
||||||
glBufferData(GL_ARRAY_BUFFER, ninstances * batch.packed_size, batch.params, GL_STREAM_DRAW);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
||||||
}
|
}
|
||||||
if (rk_DrawElementsInstancedBaseInstance) {
|
if (rk_DrawElementsInstancedBaseInstance) {
|
||||||
if (rk_MultiDrawElementsIndirect) {
|
if (rk_MultiDrawElementsIndirect) {
|
||||||
rk_MultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_SHORT, nullptr, ncommands, sizeof(rk_command));
|
rk_MultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_SHORT, nullptr, ncommands, sizeof(rk_command));
|
||||||
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
|
|
||||||
} else {
|
} else {
|
||||||
rk_command const * const last_command = batch.commands + ncommands;
|
rk_command const * const last_command = batch.commands + ncommands;
|
||||||
for (rk_command const * command = batch.commands; command < last_command; ++command) {
|
for (rk_command const * command = batch.commands; command < last_command; ++command) {
|
||||||
@ -725,6 +758,12 @@ void rk_draw_batch(
|
|||||||
command->ninstances);
|
command->ninstances);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (rk_MultiDrawElementsIndirect) {
|
||||||
|
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
|
||||||
|
}
|
||||||
|
if (batch.nparams) {
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void rk_unselect_vertices(
|
void rk_unselect_vertices(
|
||||||
|
@ -65,6 +65,7 @@ struct rk_vec3_short {
|
|||||||
};
|
};
|
||||||
|
|
||||||
typedef rk_int rk_vec3_int10;
|
typedef rk_int rk_vec3_int10;
|
||||||
|
typedef rk_uint rk_vec3_uint10;
|
||||||
|
|
||||||
struct rk_mat3_float {
|
struct rk_mat3_float {
|
||||||
rk_vec3_float x;
|
rk_vec3_float x;
|
||||||
@ -124,8 +125,8 @@ struct rk_batch {
|
|||||||
rk_command * commands;
|
rk_command * commands;
|
||||||
rk_packer * packers;
|
rk_packer * packers;
|
||||||
rk_ubyte * params;
|
rk_ubyte * params;
|
||||||
GLuint params_buffer;
|
|
||||||
GLuint commands_buffer;
|
GLuint commands_buffer;
|
||||||
|
GLuint params_buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _RK_ENGINE_RENDER_OPENGLES_H
|
#endif // _RK_ENGINE_RENDER_OPENGLES_H
|
||||||
|
Loading…
Reference in New Issue
Block a user