Compare commits

..

No commits in common. "269d7821c82757cf994370f9ed4b2a6d36e34983" and "6b33f8285cd37f9aca381c3800d21a26435b7cfb" have entirely different histories.

4 changed files with 49 additions and 93 deletions

View File

@ -100,10 +100,26 @@ class mat4(ctypes.Structure):
self.up = up
self.origin = origin
def set_vec3(self, right, forward, up, origin):
self.right.set(*right, 0.0)
self.forward.set(*forward, 0.0)
self.up.set(*up, 0.0)
def set_mat3_vec3(self, axes, origin):
self.right.set(*axes.right, 0.0)
self.forward.set(*axes.forward, 0.0)
self.up.set(*axes.up, 0.0)
self.origin.set(*origin, 1.0)
def set_axes(self, right, forward, up):
self.right = right
self.forward = forward
self.up = up
def set_axes_mat3(self, axes):
self.right.set(*axes.right, 0.0)
self.forward.set(*axes.forward, 0.0)
self.up.set(*axes.up, 0.0)
def set_origin(self, origin):
self.origin = origin
def set_origin_vec3(self, origin):
self.origin.set(*origin, 1.0)
assert ctypes.sizeof(vec3) == ctypes.sizeof(ctypes.c_float) * 3
@ -365,11 +381,6 @@ set_param_vec3.argtypes = (
ctypes.c_uint, # layout
_vec3_p) # value
set_param_mat3 = _engine.rk_set_param_mat3
set_param_mat3.argtypes = (
ctypes.c_uint, # layout
_mat3_p) # value
select_texture = _engine.rk_select_texture
select_texture.argtypes = (
ctypes.c_uint, # slot

View File

@ -147,10 +147,6 @@ RK_EXPORT void rk_set_param_vec3(
rk_param_t param,
rk_vec3 const & value);
RK_EXPORT void rk_set_param_mat3(
rk_param_t param,
rk_mat3 const & value);
RK_EXPORT void rk_select_texture(
rk_uint slot,
rk_texture_t texture,

View File

@ -17,7 +17,6 @@
#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;
@ -258,20 +257,16 @@ 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_float);
nattribs += 1;
vertex_size += sizeof(rk_vec3);
break;
case RK_VERTEX_FORMAT_VEC3_INT10:
vertex_size += sizeof(rk_vec3_int10);
nattribs += 1;
vertex_size += sizeof(rk_int);
break;
case RK_VERTEX_FORMAT_VEC3_UINT10:
vertex_size += sizeof(rk_vec3_uint10);
nattribs += 1;
vertex_size += sizeof(rk_uint);
break;
default:
rk_printf("rk_create_vertices(): invalid format.");
@ -284,39 +279,35 @@ 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);
glBindVertexBuffer(RK_VERTICES_BINDING, vertices->vertices, 0, vertices->vertex_size);
for (unsigned attrib = 0; attrib < nattribs; ++attrib) {
glEnableVertexAttribArray(attrib);
}
vertices->vertex_size = vertex_size;
vertices->layout = 0;
unsigned offset = 0;
for (rk_vertex_format const * f = format; *f; ++f) {
for (rk_vertex_format const * f = format; *f; ++f, ++vertices->layout) {
glEnableVertexAttribArray(vertices->layout);
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, GL_FALSE, offset);
offset += sizeof(rk_vec3_float);
glVertexAttribFormat(vertices->layout, 3, GL_FLOAT, norm, offset);
offset += sizeof(rk_vec3);
break;
case RK_VERTEX_FORMAT_VEC3_INT10:
glVertexAttribFormat(vertices->layout++, 4, GL_INT_2_10_10_10_REV, norm, offset);
offset += sizeof(rk_vec3_int10);
glVertexAttribFormat(vertices->layout, 4, GL_INT_2_10_10_10_REV, norm, offset);
offset += sizeof(rk_int);
break;
case RK_VERTEX_FORMAT_VEC3_UINT10:
glVertexAttribFormat(vertices->layout++, 4, GL_UNSIGNED_INT_2_10_10_10_REV, norm, offset);
offset += sizeof(rk_vec3_uint10);
glVertexAttribFormat(vertices->layout, 4, GL_UNSIGNED_INT_2_10_10_10_REV, norm, offset);
offset += sizeof(rk_uint);
break;
}
glVertexAttribBinding(vertices->layout, RK_VERTICES_BINDING);
}
for (unsigned attrib = 0; attrib < nattribs; ++attrib) {
glVertexAttribBinding(attrib, RK_VERTICES_BINDING);
}
glBindVertexBuffer(RK_VERTICES_BINDING, vertices->vertices, 0, vertices->vertex_size);
glGenBuffers(1, &vertices->indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertices->indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, nindices * sizeof(rk_ushort), indices, GL_STATIC_DRAW);
@ -411,33 +402,27 @@ 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.");
@ -445,7 +430,6 @@ rk_batch_t rk_create_batch(
break;
}
}
glBindVertexArray(vertices->array);
rk_batch * batch = new rk_batch;
batch->size = max_size;
batch->nparams = nparams;
@ -453,32 +437,14 @@ 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) {
@ -531,14 +497,17 @@ rk_batch_t rk_create_batch(
}
offset += packer->dst_incr;
}
for (unsigned attrib = vertices->layout; attrib < vertices->layout + nattribs; ++attrib) {
for (unsigned attrib = vertices->layout; attrib < layout; ++attrib) {
glEnableVertexAttribArray(attrib);
glVertexAttribBinding(attrib, RK_PARAMS_BINDING);
}
glVertexBindingDivisor(RK_PARAMS_BINDING, 1);
} else {
batch->params_buffer = 0;
glBindVertexBuffer(RK_PARAMS_BINDING, batch->params_buffer, 0, batch->packed_size);
glBindVertexArray(0);
}
if (rk_MultiDrawElementsIndirect) {
glGenBuffers(1, &batch->commands_buffer);
}
glBindVertexArray(0);
return batch;
}
@ -600,17 +569,6 @@ void rk_set_param_vec3(
}
}
void rk_set_param_mat3(
rk_param_t _param,
rk_mat3 const & value) {
GLint const param = reinterpret_cast<intptr_t>(_param) - 1;
if (rk_current_shader && param > -1) {
glVertexAttrib3fv(param + 0, glm::value_ptr(value[0]));
glVertexAttrib3fv(param + 1, glm::value_ptr(value[1]));
glVertexAttrib3fv(param + 2, glm::value_ptr(value[2]));
}
}
void rk_select_texture(
rk_uint slot,
rk_texture_t _texture,
@ -718,23 +676,21 @@ void rk_draw_batch(
if (!ninstances) {
return;
}
if (rk_MultiDrawElementsIndirect) {
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, batch.commands_buffer);
}
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);
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) {
rk_batch_pack(batch, ninstances, params);
glBufferSubData(GL_ARRAY_BUFFER, 0, ninstances * batch.packed_size, batch.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);
}
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) {
@ -758,12 +714,6 @@ 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,7 +65,6 @@ struct rk_vec3_short {
};
typedef rk_int rk_vec3_int10;
typedef rk_uint rk_vec3_uint10;
struct rk_mat3_float {
rk_vec3_float x;
@ -125,8 +124,8 @@ struct rk_batch {
rk_command * commands;
rk_packer * packers;
rk_ubyte * params;
GLuint commands_buffer;
GLuint params_buffer;
GLuint commands_buffer;
};
#endif // _RK_ENGINE_RENDER_OPENGLES_H