Compare commits
2 Commits
ddb9b0598f
...
eaff99a5b6
Author | SHA1 | Date | |
---|---|---|---|
eaff99a5b6
|
|||
c1b9c0d17e
|
@ -22,12 +22,12 @@
|
||||
extern PFNGLDRAWELEMENTSINSTANCEDBASEINSTANCEPROC rk_DrawElementsInstancedBaseInstance;
|
||||
extern PFNGLMULTIDRAWELEMENTSINDIRECTPROC rk_MultiDrawElementsIndirect;
|
||||
|
||||
RK_EXPORT rk_window_t rk_create_context(
|
||||
extern rk_window_t rk_create_context(
|
||||
char const * name,
|
||||
rk_uint width,
|
||||
rk_uint height);
|
||||
unsigned width,
|
||||
unsigned height);
|
||||
|
||||
RK_EXPORT void rk_swap_buffers();
|
||||
RK_EXPORT void rk_destroy_context();
|
||||
extern void rk_swap_buffers();
|
||||
extern void rk_destroy_context();
|
||||
|
||||
#endif // _RK_ENGINE_RENDER_CONTEXT_H
|
||||
|
@ -91,8 +91,8 @@ static int rk_error_handler(
|
||||
|
||||
rk_window_t rk_create_context(
|
||||
char const * name,
|
||||
rk_uint width,
|
||||
rk_uint height) {
|
||||
unsigned width,
|
||||
unsigned height) {
|
||||
rk_display = XOpenDisplay(nullptr);
|
||||
if (!rk_display) {
|
||||
rk_printf("Failed to open X display.");
|
||||
@ -162,7 +162,7 @@ rk_window_t rk_create_context(
|
||||
char const * const glx_exts = glXQueryExtensionsString(rk_display, DefaultScreen(rk_display));
|
||||
glXCreateContextAttribsARBProc const glXCreateContextAttribsARB =
|
||||
reinterpret_cast<glXCreateContextAttribsARBProc>(
|
||||
glXGetProcAddressARB(reinterpret_cast<const GLubyte *>("glXCreateContextAttribsARB")));
|
||||
glXGetProcAddressARB(reinterpret_cast<GLubyte const *>("glXCreateContextAttribsARB")));
|
||||
|
||||
rk_error_occured = false;
|
||||
int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&rk_error_handler);
|
||||
@ -190,13 +190,13 @@ rk_window_t rk_create_context(
|
||||
if (rk_extension_supported(gl_exts, "GL_EXT_base_instance")) {
|
||||
rk_DrawElementsInstancedBaseInstance =
|
||||
reinterpret_cast<PFNGLDRAWELEMENTSINSTANCEDBASEINSTANCEPROC>(
|
||||
glXGetProcAddressARB(reinterpret_cast<const GLubyte *>("DrawElementsInstancedBaseInstance")));
|
||||
glXGetProcAddressARB(reinterpret_cast<GLubyte const *>("DrawElementsInstancedBaseInstance")));
|
||||
if (rk_DrawElementsInstancedBaseInstance) {
|
||||
rk_printf("Using extension GL_EXT_base_instance::DrawElementsInstancedBaseInstance.");
|
||||
if (rk_extension_supported(gl_exts, "GL_EXT_multi_draw_indirect")) {
|
||||
rk_MultiDrawElementsIndirect =
|
||||
reinterpret_cast<PFNGLMULTIDRAWELEMENTSINDIRECTPROC>(
|
||||
glXGetProcAddressARB(reinterpret_cast<const GLubyte *>("MultiDrawElementsIndirectEXT")));
|
||||
glXGetProcAddressARB(reinterpret_cast<GLubyte const *>("MultiDrawElementsIndirectEXT")));
|
||||
if (rk_MultiDrawElementsIndirect) {
|
||||
rk_printf("Using extension GL_EXT_multi_draw_indirect::MultiDrawElementsIndirectEXT.");
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ rk_window_t rk_initialize(
|
||||
}
|
||||
|
||||
static void rk_print_shader_infolog(GLuint shader) {
|
||||
int length;
|
||||
GLsizei length;
|
||||
char infolog[1024];
|
||||
glGetShaderInfoLog(shader, sizeof(infolog), &length, infolog);
|
||||
if (length > 0) {
|
||||
@ -75,7 +75,7 @@ static void rk_print_shader_infolog(GLuint shader) {
|
||||
}
|
||||
|
||||
static void rk_print_program_infolog(GLuint program) {
|
||||
int length;
|
||||
GLsizei length;
|
||||
char infolog[1024];
|
||||
glGetProgramInfoLog(program, sizeof(infolog), &length, infolog);
|
||||
if (length > 0) {
|
||||
@ -258,7 +258,7 @@ rk_vertices_t rk_create_vertices(
|
||||
if (!format || !nvertices || !_vertices || !nindices || !indices) {
|
||||
return nullptr;
|
||||
}
|
||||
rk_uint vertex_size = 0;
|
||||
unsigned vertex_size = 0;
|
||||
for (rk_vertex_format const * f = format; *f; ++f) {
|
||||
switch (*f & RK_VERTEX_FORMAT_MASK) {
|
||||
case RK_VERTEX_FORMAT_VEC3_FLOAT:
|
||||
@ -289,7 +289,7 @@ rk_vertices_t rk_create_vertices(
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
vertices->vertex_size = vertex_size;
|
||||
vertices->layout = 0;
|
||||
rk_uint offset = 0;
|
||||
unsigned offset = 0;
|
||||
for (rk_vertex_format const * f = format; *f; ++f, ++vertices->layout) {
|
||||
glEnableVertexAttribArray(vertices->layout);
|
||||
GLboolean const normalize = (*f & RK_VERTEX_FORMAT_NORMALIZE) != 0;
|
||||
@ -317,35 +317,35 @@ rk_vertices_t rk_create_vertices(
|
||||
return vertices;
|
||||
}
|
||||
|
||||
static void rk_pack_vec3_float(
|
||||
rk_vec3 * const __restrict dst,
|
||||
rk_vec3 const * const __restrict src) {
|
||||
*dst = *src;
|
||||
static void rk_pack_vec3(
|
||||
rk_pack_dst dst,
|
||||
rk_pack_src src) {
|
||||
*dst.vec3_ptr = *src.vec3_ptr;
|
||||
}
|
||||
|
||||
static void rk_pack_vec3_short(
|
||||
rk_vec3_short * const __restrict dst,
|
||||
rk_vec3 const * const __restrict src) {
|
||||
dst->x = static_cast<rk_short>(src->x);
|
||||
dst->y = static_cast<rk_short>(src->y);
|
||||
dst->z = static_cast<rk_short>(src->z);
|
||||
static void rk_pack_vec3s(
|
||||
rk_pack_dst dst,
|
||||
rk_pack_src src) {
|
||||
dst.vec3s_ptr->x = static_cast<rk_short>(src.vec3_ptr->x);
|
||||
dst.vec3s_ptr->y = static_cast<rk_short>(src.vec3_ptr->y);
|
||||
dst.vec3s_ptr->z = static_cast<rk_short>(src.vec3_ptr->z);
|
||||
}
|
||||
|
||||
static void rk_pack_vec3_short_norm(
|
||||
rk_vec3_short * const __restrict dst,
|
||||
rk_vec3 const * const __restrict src) {
|
||||
static void rk_pack_vec3s_norm(
|
||||
rk_pack_dst dst,
|
||||
rk_pack_src src) {
|
||||
#define _convert(s) (static_cast<rk_short>((s) * ((s) < 0.f ? 32768.f : 32767.f)))
|
||||
dst->x = _convert(src->x);
|
||||
dst->y = _convert(src->y);
|
||||
dst->z = _convert(src->z);
|
||||
dst.vec3s_ptr->x = _convert(src.vec3_ptr->x);
|
||||
dst.vec3s_ptr->y = _convert(src.vec3_ptr->y);
|
||||
dst.vec3s_ptr->z = _convert(src.vec3_ptr->z);
|
||||
#undef _convert
|
||||
}
|
||||
|
||||
static void rk_pack_vec3_int10(
|
||||
rk_int * const __restrict dst,
|
||||
rk_vec3 const * const __restrict src) {
|
||||
rk_pack_dst dst,
|
||||
rk_pack_src src) {
|
||||
#define _convert(s) (static_cast<rk_int>((s) * ((s) < 0.f ? 512.f : 511.f)) & 1023)
|
||||
*dst = _convert(src->x) | (_convert(src->y) << 10) | (_convert(src->z) << 20);
|
||||
*dst.int_ptr = _convert(src.vec3_ptr->x) | (_convert(src.vec3_ptr->y) << 10) | (_convert(src.vec3_ptr->z) << 20);
|
||||
#undef _convert
|
||||
}
|
||||
|
||||
@ -359,9 +359,9 @@ rk_batch_t rk_create_batch(
|
||||
rk_printf("rk_create_batch(): invalid parameters.");
|
||||
return nullptr;
|
||||
}
|
||||
rk_uint nparams = 0;
|
||||
rk_uint params_size = 0;
|
||||
rk_uint packed_size = 0;
|
||||
unsigned nparams = 0;
|
||||
unsigned params_size = 0;
|
||||
unsigned packed_size = 0;
|
||||
for (rk_param_format const * f = params_format; *f; ++f, ++nparams) {
|
||||
switch (*f & RK_PARAM_FORMAT_MASK) {
|
||||
case RK_PARAM_FORMAT_VEC3_FLOAT:
|
||||
@ -370,7 +370,7 @@ rk_batch_t rk_create_batch(
|
||||
break;
|
||||
case RK_PARAM_FORMAT_VEC3_SHORT:
|
||||
params_size += sizeof(rk_vec3);
|
||||
packed_size += sizeof(rk_vec3_short);
|
||||
packed_size += sizeof(rk_vec3s);
|
||||
break;
|
||||
case RK_PARAM_FORMAT_VEC3_INT10:
|
||||
params_size += sizeof(rk_vec3);
|
||||
@ -393,9 +393,9 @@ rk_batch_t rk_create_batch(
|
||||
batch->packers = new rk_packer[nparams];
|
||||
batch->params = new rk_ubyte[max_size * packed_size];
|
||||
glGenBuffers(1, &batch->params_buffer);
|
||||
rk_uint layout = vertices->layout;
|
||||
unsigned layout = vertices->layout;
|
||||
rk_packer * packer = batch->packers;
|
||||
rk_uint offset = 0;
|
||||
unsigned offset = 0;
|
||||
glBindVertexArray(vertices->array);
|
||||
for (rk_param_format const * f = params_format; *f; ++f, ++layout, ++packer) {
|
||||
GLboolean const normalize = (*f & RK_PARAM_FORMAT_NORMALIZE) != 0;
|
||||
@ -403,23 +403,23 @@ rk_batch_t rk_create_batch(
|
||||
switch (*f & RK_PARAM_FORMAT_MASK) {
|
||||
case RK_PARAM_FORMAT_VEC3_FLOAT:
|
||||
glVertexAttribFormat(layout, 3, GL_FLOAT, normalize, offset);
|
||||
packer->pack = reinterpret_cast<rk_packer_fn>(rk_pack_vec3_float);
|
||||
packer->pack = rk_pack_vec3;
|
||||
packer->dst_incr = sizeof(rk_vec3);
|
||||
packer->src_incr = sizeof(rk_vec3);
|
||||
break;
|
||||
case RK_PARAM_FORMAT_VEC3_SHORT:
|
||||
glVertexAttribFormat(layout, 3, GL_SHORT, normalize, offset);
|
||||
if (normalize) {
|
||||
packer->pack = reinterpret_cast<rk_packer_fn>(rk_pack_vec3_short_norm);
|
||||
packer->pack = rk_pack_vec3s_norm;
|
||||
} else {
|
||||
packer->pack = reinterpret_cast<rk_packer_fn>(rk_pack_vec3_short);
|
||||
packer->pack = rk_pack_vec3s;
|
||||
}
|
||||
packer->dst_incr = sizeof(rk_vec3_short);
|
||||
packer->dst_incr = sizeof(rk_vec3s);
|
||||
packer->src_incr = sizeof(rk_vec3);
|
||||
break;
|
||||
case RK_PARAM_FORMAT_VEC3_INT10:
|
||||
glVertexAttribFormat(layout, 4, GL_INT_2_10_10_10_REV, normalize, offset);
|
||||
packer->pack = reinterpret_cast<rk_packer_fn>(rk_pack_vec3_int10);
|
||||
packer->pack = rk_pack_vec3_int10;
|
||||
packer->dst_incr = sizeof(rk_int);
|
||||
packer->src_incr = sizeof(rk_vec3);
|
||||
break;
|
||||
@ -531,22 +531,22 @@ void rk_select_vertices(
|
||||
}
|
||||
}
|
||||
|
||||
static rk_uint rk_batch_filter(
|
||||
static unsigned rk_batch_filter(
|
||||
rk_batch & batch,
|
||||
rk_uint const size,
|
||||
unsigned const size,
|
||||
rk_instance_flags const * flags) {
|
||||
rk_ushort * indices = batch.indices;
|
||||
for (rk_ushort index = 0; index < size; ++index, ++flags) {
|
||||
for (unsigned index = 0; index < size; ++index, ++flags) {
|
||||
if ((*flags & RK_INSTANCE_FLAGS_SPAWNED_VISIBLE) == RK_INSTANCE_FLAGS_SPAWNED_VISIBLE) {
|
||||
*indices++ = index;
|
||||
*indices++ = static_cast<rk_ushort>(index);
|
||||
}
|
||||
}
|
||||
return indices - batch.indices;
|
||||
}
|
||||
|
||||
static rk_uint rk_batch_build_commands(
|
||||
static unsigned rk_batch_build_commands(
|
||||
rk_batch & batch,
|
||||
rk_uint const ninstances,
|
||||
unsigned const ninstances,
|
||||
rk_mesh const * const meshes) {
|
||||
rk_command * commands = batch.commands;
|
||||
rk_ushort * base = batch.indices;
|
||||
@ -556,10 +556,10 @@ static rk_uint rk_batch_build_commands(
|
||||
for ( ; first < last && meshes[*first].packed == mesh.packed; ++first) {
|
||||
}
|
||||
for (rk_ushort * second = first; second < last; ++second) {
|
||||
rk_ushort const index = *second;
|
||||
unsigned const index = *second;
|
||||
if (meshes[index].packed == mesh.packed) {
|
||||
*second = *first;
|
||||
*first++ = index;
|
||||
*first++ = static_cast<rk_ushort>(index);
|
||||
}
|
||||
}
|
||||
commands->nvertices = static_cast<GLuint>(mesh.ntriangles) * 3;
|
||||
@ -571,19 +571,19 @@ static rk_uint rk_batch_build_commands(
|
||||
return commands - batch.commands;
|
||||
}
|
||||
|
||||
static void rk_batch_convert_params(
|
||||
static void rk_batch_pack(
|
||||
rk_batch & batch,
|
||||
rk_uint const ninstances,
|
||||
unsigned const ninstances,
|
||||
rk_ubyte const * const params) {
|
||||
rk_ubyte * __restrict dst = batch.params;
|
||||
rk_pack_dst dst(batch.params);
|
||||
rk_ushort const * const last_index = batch.indices + ninstances;
|
||||
rk_packer const * const last_packer = batch.packers + batch.nparams;
|
||||
for (rk_ushort const * index = batch.indices; index < last_index; ++index) {
|
||||
rk_ubyte const * __restrict src = ¶ms[batch.params_size * (*index)];
|
||||
rk_pack_src src(¶ms[batch.params_size * (*index)]);
|
||||
for (rk_packer const * packer = batch.packers; packer < last_packer; ++packer) {
|
||||
packer->pack(dst, src);
|
||||
dst += packer->dst_incr;
|
||||
src += packer->src_incr;
|
||||
dst.ptr += packer->dst_incr;
|
||||
src.ptr += packer->src_incr;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -598,17 +598,17 @@ void rk_draw_batch(
|
||||
if (!size || size > batch.size || !flags || !meshes || !rk_current_shader || !rk_current_vertices) {
|
||||
return;
|
||||
}
|
||||
rk_uint const ninstances = rk_batch_filter(batch, size, flags);
|
||||
unsigned const ninstances = rk_batch_filter(batch, size, flags);
|
||||
if (!ninstances) {
|
||||
return;
|
||||
}
|
||||
rk_uint const ncommands = rk_batch_build_commands(batch, ninstances, meshes);
|
||||
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) {
|
||||
rk_batch_convert_params(batch, ninstances, params);
|
||||
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);
|
||||
@ -627,7 +627,7 @@ void rk_draw_batch(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
rk_uint params_offset = 0;
|
||||
unsigned params_offset = 0;
|
||||
rk_command const * const last_command = batch.commands + ncommands;
|
||||
for (rk_command const * command = batch.commands; command < last_command; ++command) {
|
||||
if (batch.nparams) {
|
||||
|
@ -22,7 +22,7 @@
|
||||
#include <GLES3/gl3ext.h>
|
||||
#include <GLES3/gl3platform.h>
|
||||
|
||||
enum : rk_uint {
|
||||
enum : GLuint {
|
||||
RK_VERTICES_BINDING = 0,
|
||||
RK_PARAMS_BINDING = 1
|
||||
};
|
||||
@ -34,30 +34,55 @@ struct rk_shader {
|
||||
};
|
||||
|
||||
struct rk_texture {
|
||||
rk_uint nlevels;
|
||||
unsigned nlevels;
|
||||
GLuint texture;
|
||||
};
|
||||
|
||||
struct rk_triangles {
|
||||
rk_uint size;
|
||||
unsigned size;
|
||||
GLuint array;
|
||||
GLuint vertices;
|
||||
};
|
||||
|
||||
struct rk_vertices {
|
||||
rk_uint vertex_size;
|
||||
rk_uint layout;
|
||||
unsigned vertex_size;
|
||||
unsigned layout;
|
||||
GLuint array;
|
||||
GLuint vertices;
|
||||
GLuint indices;
|
||||
};
|
||||
|
||||
typedef void (*rk_packer_fn)(rk_ubyte * const __restrict, rk_ubyte const * const __restrict);
|
||||
struct rk_vec3s {
|
||||
rk_short x;
|
||||
rk_short y;
|
||||
rk_short z;
|
||||
rk_short pad;
|
||||
};
|
||||
|
||||
union rk_pack_src {
|
||||
rk_ubyte const * __restrict ptr;
|
||||
rk_vec3 const * __restrict vec3_ptr;
|
||||
|
||||
inline rk_pack_src() {}
|
||||
inline rk_pack_src(rk_ubyte const * const __restrict src) : ptr(src) {}
|
||||
};
|
||||
|
||||
union rk_pack_dst {
|
||||
rk_ubyte * __restrict ptr;
|
||||
rk_vec3 * __restrict vec3_ptr;
|
||||
rk_vec3s * __restrict vec3s_ptr;
|
||||
rk_int * __restrict int_ptr;
|
||||
|
||||
inline rk_pack_dst() {}
|
||||
inline rk_pack_dst(rk_ubyte * const __restrict dst) : ptr(dst) {}
|
||||
};
|
||||
|
||||
typedef void (*rk_packer_fn)(rk_pack_dst, rk_pack_src);
|
||||
|
||||
struct rk_packer {
|
||||
rk_packer_fn pack;
|
||||
rk_uint dst_incr;
|
||||
rk_uint src_incr;
|
||||
unsigned dst_incr;
|
||||
unsigned src_incr;
|
||||
};
|
||||
|
||||
struct rk_command {
|
||||
@ -69,10 +94,10 @@ struct rk_command {
|
||||
};
|
||||
|
||||
struct rk_batch {
|
||||
rk_uint size;
|
||||
rk_uint nparams;
|
||||
rk_uint params_size;
|
||||
rk_uint packed_size;
|
||||
unsigned size;
|
||||
unsigned nparams;
|
||||
unsigned params_size;
|
||||
unsigned packed_size;
|
||||
rk_ushort * indices;
|
||||
rk_command * commands;
|
||||
rk_packer * packers;
|
||||
@ -81,11 +106,4 @@ struct rk_batch {
|
||||
GLuint commands_buffer;
|
||||
};
|
||||
|
||||
struct rk_vec3_short {
|
||||
rk_short x;
|
||||
rk_short y;
|
||||
rk_short z;
|
||||
rk_short pad;
|
||||
};
|
||||
|
||||
#endif // _RK_ENGINE_RENDER_OPENGLES_H
|
||||
|
Reference in New Issue
Block a user