Compare commits

..

No commits in common. "628f8bcaa4eb3c4da5a534b55d58218b894b9822" and "7db5304d40013c7e4631b66ab5199f9bbd109285" have entirely different histories.

4 changed files with 26 additions and 41 deletions

View File

@ -279,9 +279,6 @@ INSTANCE_FLAG_VISIBLE = _flag(1)
BATCH_MAX_SIZE = 65536
render_initialize = _engine.rk_render_initialize
render_initialize.argtypes = (
ctypes.c_bool,) # debug
render_terminate = _engine.rk_render_terminate
_load_shader = _engine.rk_load_shader

View File

@ -80,8 +80,7 @@ union rk_mesh {
};
};
RK_EXPORT void rk_render_initialize(
rk_bool debug);
RK_EXPORT void rk_render_initialize();
RK_EXPORT void rk_render_terminate();

View File

@ -17,6 +17,7 @@
#include "render_opengles.hpp"
#include "../display/display_glx.hpp"
#include <cstdio>
#include <cstring>
typedef void (*rk_DrawElementsInstancedBaseInstanceFunc)(rk_uint, rk_uint, rk_uint, const void *, rk_uint, rk_uint);
typedef void (*rk_MultiDrawElementsIndirectFunc)(rk_uint, rk_uint, const void *, rk_uint, rk_uint);
@ -48,8 +49,7 @@ static void rk_debug_message_callback(
printf("[GL] (id=%d) %s\n", id, message);
}
void rk_render_initialize(
rk_bool debug) {
void rk_render_initialize() {
GLubyte const * const vendor = glGetString(GL_VENDOR);
GLubyte const * const renderer = glGetString(GL_RENDERER);
printf("[GL] vendor: %s, renderer: %s\n", vendor, renderer);
@ -57,10 +57,8 @@ void rk_render_initialize(
GLubyte const * const language = glGetString(GL_SHADING_LANGUAGE_VERSION);
printf("[GL] version: %s, language: %s\n", version, language);
if (debug) {
glDebugMessageCallback(rk_debug_message_callback, nullptr);
glEnable(GL_DEBUG_OUTPUT);
}
glDebugMessageCallback(rk_debug_message_callback, nullptr);
glEnable(GL_DEBUG_OUTPUT);
char const * const gl_exts = reinterpret_cast<char const *>(glGetString(GL_EXTENSIONS));
// printf("[GL] %s\n", gl_exts);
@ -375,7 +373,6 @@ static void rk_pack_vec3_short(
dst->x = static_cast<rk_short>(input.x);
dst->y = static_cast<rk_short>(input.y);
dst->z = static_cast<rk_short>(input.z);
dst->pad = 0;
}
}
@ -393,7 +390,6 @@ static void rk_pack_vec3_short_norm(
dst->x = _convert(input.x);
dst->y = _convert(input.y);
dst->z = _convert(input.z);
dst->pad = 0;
}
#undef _convert
}
@ -497,19 +493,18 @@ rk_batch_t rk_create_batch(
rk_batch * batch = new rk_batch;
batch->max_size = max_size;
batch->nparams = nparams;
batch->commands_size = 0;
batch->packed_size = 0;
batch->indices = nullptr;
batch->commands = nullptr;
batch->params = nullptr;
batch->commands_buffer = 0;
batch->packed_params = nullptr;
batch->indirect_buffer = 0;
batch->params_array = 0;
unsigned packed_size = 0;
unsigned params_size = 0;
if (nparams) {
batch->params = new rk_parameter[nparams];
rk_parameter * param = batch->params;
for (rk_param_format const * f = params_format; *f; ++f, ++param) {
param->offset = packed_size;
param->offset = params_size;
bool const norm = (*f & RK_PARAM_FORMAT_NORMALIZE) != 0;
switch (*f & RK_PARAM_FORMAT_MASK) {
case RK_PARAM_FORMAT_VEC3_FLOAT:
@ -539,25 +534,26 @@ rk_batch_t rk_create_batch(
return nullptr;
break;
}
packed_size += max_size * param->size;
params_size += max_size * param->size;
}
}
batch->commands_size = max_size * 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);
memset(batch->commands, 0, max_size * sizeof(rk_command));
batch->indirect_buffer = 0;
if (rk_MultiDrawElementsIndirect) {
glGenBuffers(1, &batch->commands_buffer);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, batch->commands_buffer);
glBufferData(GL_DRAW_INDIRECT_BUFFER, batch->commands_size, nullptr, GL_STREAM_DRAW);
glGenBuffers(1, &batch->indirect_buffer);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, batch->indirect_buffer);
glBufferData(GL_DRAW_INDIRECT_BUFFER, max_size * sizeof(rk_command), nullptr, GL_DYNAMIC_DRAW);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
}
if (nparams) {
batch->packed_params = new rk_ubyte[params_size];
memset(batch->packed_params, 0, params_size);
glBindVertexArray(vertices->array);
glGenBuffers(1, &batch->params_array);
glBindBuffer(GL_ARRAY_BUFFER, batch->params_array);
glBufferData(GL_ARRAY_BUFFER, batch->packed_size, nullptr, GL_STREAM_DRAW);
glBufferData(GL_ARRAY_BUFFER, params_size, nullptr, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
rk_parameter const * param = batch->params;
unsigned binding = RK_PARAMS_BINDING_BASE;
@ -750,7 +746,6 @@ static unsigned rk_batch_build_commands(
commands->nvertices = static_cast<GLuint>(mesh.ntriangles) * 3;
commands->ninstances = first - base;
commands->base_index = mesh.base_index;
commands->base_vertex = 0;
commands->base_instance = base - batch.indices;
}
return commands - batch.commands;
@ -764,13 +759,9 @@ static void rk_batch_pack(
for (rk_parameter const * param = batch.params; param < last_param; ++param) {
rk_ubyte const * const src = *srcs++;
if (src) {
rk_ubyte * const dst = reinterpret_cast<rk_ubyte *>(
glMapBufferRange(GL_ARRAY_BUFFER, param->offset, ninstances * param->size,
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
if (dst) {
param->packer(ninstances, batch.indices, dst, src);
glUnmapBuffer(GL_ARRAY_BUFFER);
}
rk_ubyte * const dst = batch.packed_params + param->offset;
param->packer(ninstances, batch.indices, dst, src);
glBufferSubData(GL_ARRAY_BUFFER, param->offset, ninstances * param->size, dst);
}
}
}
@ -791,13 +782,11 @@ void rk_draw_batch(
}
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, batch.commands_size, nullptr, GL_STREAM_DRAW);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, batch.indirect_buffer);
glBufferSubData(GL_DRAW_INDIRECT_BUFFER, 0, ncommands * sizeof(rk_command), batch.commands);
}
if (batch.nparams && params) {
glBindBuffer(GL_ARRAY_BUFFER, batch.params_array);
glBufferData(GL_ARRAY_BUFFER, batch.packed_size, nullptr, GL_STREAM_DRAW);
rk_batch_pack(batch, ninstances, params);
}
if (rk_DrawElementsInstancedBaseInstance) {
@ -873,10 +862,11 @@ void rk_destroy_batch(
delete[] batch->indices;
delete[] batch->commands;
if (rk_MultiDrawElementsIndirect) {
glDeleteBuffers(1, &batch->commands_buffer);
glDeleteBuffers(1, &batch->indirect_buffer);
}
if (batch->nparams) {
delete[] batch->params;
delete[] batch->packed_params;
glDeleteBuffers(1, &batch->params_array);
}
delete batch;

View File

@ -102,12 +102,11 @@ struct rk_parameter {
struct rk_batch {
unsigned max_size;
unsigned nparams;
unsigned commands_size;
unsigned packed_size;
rk_ushort * indices;
rk_command * commands;
rk_parameter * params;
GLuint commands_buffer;
rk_ubyte * packed_params;
GLuint indirect_buffer;
GLuint params_array;
};