2022-08-28 04:23:13 +02:00
|
|
|
// Copyright (C) 2022 RozK
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-12-24 11:27:53 +01:00
|
|
|
#include "../render.hpp"
|
2022-08-28 04:23:13 +02:00
|
|
|
#include "render_opengles.hpp"
|
2022-12-24 11:27:53 +01:00
|
|
|
#include "../display/display_glx.hpp"
|
2022-08-28 04:23:13 +02:00
|
|
|
#include <cstdio>
|
2022-12-27 06:27:08 +01:00
|
|
|
#include <cstring>
|
2022-08-28 04:23:13 +02:00
|
|
|
|
2022-12-29 16:20:42 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
static rk_DrawElementsInstancedBaseInstanceFunc rk_DrawElementsInstancedBaseInstance = nullptr;
|
|
|
|
static rk_MultiDrawElementsIndirectFunc rk_MultiDrawElementsIndirect = nullptr;
|
|
|
|
|
2022-08-28 04:23:13 +02:00
|
|
|
static rk_shader const * rk_current_shader = nullptr;
|
|
|
|
static rk_vertices const * rk_current_vertices = nullptr;
|
|
|
|
|
2022-12-29 16:20:42 +01:00
|
|
|
static void rk_gl_printf(char const * message) {
|
|
|
|
printf("[GL] %s\n", message);
|
|
|
|
}
|
|
|
|
|
2022-12-24 11:27:53 +01:00
|
|
|
static void rk_printf(char const * message) {
|
|
|
|
printf("[RK] %s\n", message);
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
|
|
|
|
2022-12-24 11:27:53 +01:00
|
|
|
#define rk_gl_error(_message) { if (glGetError() != GL_NO_ERROR) { printf("[GL] %s\n", (_message)); } }
|
2022-08-28 04:23:13 +02:00
|
|
|
|
|
|
|
static void rk_debug_message_callback(
|
|
|
|
GLenum source,
|
|
|
|
GLenum type,
|
|
|
|
GLuint id,
|
|
|
|
GLenum severity,
|
|
|
|
GLsizei length,
|
|
|
|
GLchar const * message,
|
|
|
|
void const * userParam) {
|
2022-12-24 11:27:53 +01:00
|
|
|
printf("[GL] (id=%d) %s\n", id, message);
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
|
|
|
|
2022-12-24 11:27:53 +01:00
|
|
|
void rk_render_initialize() {
|
|
|
|
GLubyte const * const vendor = glGetString(GL_VENDOR);
|
|
|
|
GLubyte const * const renderer = glGetString(GL_RENDERER);
|
2022-12-29 16:20:42 +01:00
|
|
|
printf("[GL] vendor: %s, renderer: %s\n", vendor, renderer);
|
2022-12-24 11:27:53 +01:00
|
|
|
GLubyte const * const version = glGetString(GL_VERSION);
|
|
|
|
GLubyte const * const language = glGetString(GL_SHADING_LANGUAGE_VERSION);
|
2022-12-29 16:20:42 +01:00
|
|
|
printf("[GL] version: %s, language: %s\n", version, language);
|
2022-12-24 11:27:53 +01:00
|
|
|
|
|
|
|
glDebugMessageCallback(rk_debug_message_callback, nullptr);
|
|
|
|
glEnable(GL_DEBUG_OUTPUT);
|
|
|
|
|
2022-12-29 16:20:42 +01:00
|
|
|
char const * const gl_exts = reinterpret_cast<char const *>(glGetString(GL_EXTENSIONS));
|
|
|
|
// printf("[GL] %s\n", gl_exts);
|
|
|
|
rk_DrawElementsInstancedBaseInstance = reinterpret_cast<rk_DrawElementsInstancedBaseInstanceFunc>(
|
|
|
|
rk_resolve_extension(gl_exts, "GL_EXT_base_instance", "DrawElementsInstancedBaseInstance"));
|
|
|
|
if (rk_DrawElementsInstancedBaseInstance) {
|
|
|
|
rk_gl_printf("Using extension GL_EXT_base_instance::DrawElementsInstancedBaseInstance.");
|
|
|
|
rk_MultiDrawElementsIndirect = reinterpret_cast<rk_MultiDrawElementsIndirectFunc>(
|
|
|
|
rk_resolve_extension(gl_exts, "GL_EXT_multi_draw_indirect", "MultiDrawElementsIndirectEXT"));
|
|
|
|
if (rk_MultiDrawElementsIndirect) {
|
|
|
|
rk_gl_printf("Using extension GL_EXT_multi_draw_indirect::MultiDrawElementsIndirectEXT.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-24 11:27:53 +01:00
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glEnable(GL_DITHER);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glDisable(GL_STENCIL_TEST);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glFrontFace(GL_CCW);
|
|
|
|
glCullFace(GL_BACK);
|
|
|
|
glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_render_terminate() {
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rk_print_shader_infolog(GLuint shader) {
|
2022-12-20 07:01:58 +01:00
|
|
|
GLsizei length;
|
2022-08-28 04:23:13 +02:00
|
|
|
char infolog[1024];
|
|
|
|
glGetShaderInfoLog(shader, sizeof(infolog), &length, infolog);
|
|
|
|
if (length > 0) {
|
|
|
|
rk_printf(infolog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rk_print_program_infolog(GLuint program) {
|
2022-12-20 07:01:58 +01:00
|
|
|
GLsizei length;
|
2022-08-28 04:23:13 +02:00
|
|
|
char infolog[1024];
|
|
|
|
glGetProgramInfoLog(program, sizeof(infolog), &length, infolog);
|
|
|
|
if (length > 0) {
|
|
|
|
rk_printf(infolog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: error handling
|
|
|
|
rk_shader_t rk_load_shader(
|
2022-12-04 01:43:35 +01:00
|
|
|
rk_uint const vert_nlines,
|
|
|
|
char const ** const vert_lines,
|
|
|
|
rk_uint const frag_nlines,
|
|
|
|
char const ** const frag_lines) {
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_shader * const shader = new rk_shader;
|
|
|
|
shader->vertex = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
shader->program = glCreateProgram();
|
2022-12-17 04:58:19 +01:00
|
|
|
if (!vert_nlines || !vert_lines) {
|
2022-12-04 01:43:35 +01:00
|
|
|
rk_printf("Missing vertex shader.");
|
|
|
|
return nullptr;
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
2022-12-17 04:58:19 +01:00
|
|
|
if (!frag_nlines || !frag_lines) {
|
2022-12-04 01:43:35 +01:00
|
|
|
rk_printf("Missing fragment shader.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
rk_printf("Compiling vertex shader...");
|
|
|
|
glShaderSource(shader->vertex, vert_nlines, vert_lines, nullptr);
|
2022-08-28 04:23:13 +02:00
|
|
|
glCompileShader(shader->vertex);
|
2022-12-24 11:27:53 +01:00
|
|
|
rk_gl_error("glCompileShader() failed.");
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_print_shader_infolog(shader->vertex);
|
2022-12-04 01:43:35 +01:00
|
|
|
rk_printf("Compiling fragment shader...");
|
|
|
|
glShaderSource(shader->fragment, frag_nlines, frag_lines, nullptr);
|
2022-08-28 04:23:13 +02:00
|
|
|
glCompileShader(shader->fragment);
|
2022-12-24 11:27:53 +01:00
|
|
|
rk_gl_error("glCompileShader() failed.");
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_print_shader_infolog(shader->fragment);
|
|
|
|
rk_printf("Linking program...");
|
|
|
|
glAttachShader(shader->program, shader->vertex);
|
|
|
|
glAttachShader(shader->program, shader->fragment);
|
|
|
|
glLinkProgram(shader->program);
|
2022-12-24 11:27:53 +01:00
|
|
|
rk_gl_error("glLinkProgram() failed.");
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_print_program_infolog(shader->program);
|
|
|
|
rk_printf("Done.");
|
|
|
|
glReleaseShaderCompiler();
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
rk_input_t rk_resolve_input(
|
2022-12-18 03:39:34 +01:00
|
|
|
rk_shader_t _shader,
|
2022-08-28 04:23:13 +02:00
|
|
|
char const * name) {
|
2022-12-18 03:39:34 +01:00
|
|
|
rk_shader const * const shader = reinterpret_cast<rk_shader const *>(_shader);
|
|
|
|
if (!shader || !name) {
|
2022-08-28 04:23:13 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-12-18 03:39:34 +01:00
|
|
|
GLint const uniform = glGetUniformLocation(shader->program, name);
|
2022-08-28 04:23:13 +02:00
|
|
|
return reinterpret_cast<rk_input_t>(uniform + 1);
|
|
|
|
}
|
|
|
|
|
2022-12-17 16:50:41 +01:00
|
|
|
rk_param_t rk_resolve_param(
|
2022-12-18 03:39:34 +01:00
|
|
|
rk_shader_t _shader,
|
2022-12-17 16:50:41 +01:00
|
|
|
char const * name) {
|
2022-12-18 03:39:34 +01:00
|
|
|
rk_shader const * const shader = reinterpret_cast<rk_shader const *>(_shader);
|
|
|
|
if (!shader || !name) {
|
2022-12-17 16:50:41 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-12-18 03:39:34 +01:00
|
|
|
GLint const location = glGetAttribLocation(shader->program, name);
|
2022-12-17 16:50:41 +01:00
|
|
|
return reinterpret_cast<rk_param_t>(location + 1);
|
|
|
|
}
|
|
|
|
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_texture_t rk_create_texture(
|
|
|
|
rk_texture_format format,
|
|
|
|
rk_uint width,
|
|
|
|
rk_uint height,
|
|
|
|
rk_uint nlevels,
|
|
|
|
rk_texture_flags flags,
|
|
|
|
void const * pixels) {
|
2022-12-18 03:39:34 +01:00
|
|
|
if (!width || !height || !pixels) {
|
2022-08-28 04:23:13 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
GLint internal_format;
|
|
|
|
GLenum source_format;
|
|
|
|
GLenum source_type;
|
|
|
|
switch (format) {
|
|
|
|
case RK_TEXTURE_FORMAT_SRGB8_A8:
|
|
|
|
internal_format = GL_SRGB8_ALPHA8;
|
|
|
|
source_format = GL_RGBA;
|
|
|
|
source_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
case RK_TEXTURE_FORMAT_RGBA8:
|
|
|
|
internal_format = GL_RGBA8;
|
|
|
|
source_format = GL_RGBA;
|
|
|
|
source_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
case RK_TEXTURE_FORMAT_RGB10_A2:
|
|
|
|
internal_format = GL_RGB10_A2;
|
|
|
|
source_format = GL_RGBA;
|
|
|
|
source_type = GL_UNSIGNED_INT_2_10_10_10_REV;
|
|
|
|
break;
|
2022-12-25 04:20:19 +01:00
|
|
|
case RK_TEXTURE_FORMAT_FLOAT_32:
|
2022-08-28 04:23:13 +02:00
|
|
|
internal_format = GL_R32F;
|
|
|
|
source_format = GL_RED;
|
|
|
|
source_type = GL_FLOAT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rk_texture * const texture = new rk_texture;
|
|
|
|
glGenTextures(1, &texture->texture);
|
|
|
|
GLenum target;
|
|
|
|
if (nlevels) {
|
|
|
|
if (flags & RK_TEXTURE_FLAG_3D) {
|
|
|
|
target = GL_TEXTURE_3D;
|
|
|
|
} else {
|
|
|
|
target = GL_TEXTURE_2D_ARRAY;
|
|
|
|
}
|
|
|
|
glBindTexture(target, texture->texture);
|
|
|
|
//TODO: glTexStorage3D
|
|
|
|
glTexImage3D(target, 0, internal_format, width, height, nlevels, 0, source_format, source_type, pixels);
|
|
|
|
} else {
|
|
|
|
target = GL_TEXTURE_2D;
|
|
|
|
glBindTexture(target, texture->texture);
|
|
|
|
//TODO: glTexStorage2D
|
|
|
|
glTexImage2D(target, 0, internal_format, width, height, 0, source_format, source_type, pixels);
|
|
|
|
}
|
|
|
|
if (flags & RK_TEXTURE_FLAG_MIPMAPS) {
|
|
|
|
if (flags & RK_TEXTURE_FLAG_MIN_LINEAR) {
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
|
|
} else {
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (flags & RK_TEXTURE_FLAG_MIN_LINEAR) {
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
} else {
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags & RK_TEXTURE_FLAG_MAG_LINEAR) {
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
} else {
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
}
|
|
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
|
|
|
|
if (flags & RK_TEXTURE_FLAG_MIPMAPS) {
|
|
|
|
glGenerateMipmap(target);
|
|
|
|
}
|
|
|
|
texture->nlevels = nlevels;
|
|
|
|
glBindTexture(target, 0);
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
rk_triangles_t rk_create_triangles(
|
|
|
|
rk_uint nvertices,
|
|
|
|
rk_vec3 const * vertices) {
|
2022-12-18 03:39:34 +01:00
|
|
|
if (!nvertices || !vertices) {
|
2022-08-28 04:23:13 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
rk_triangles * const triangles = new rk_triangles;
|
|
|
|
triangles->size = nvertices;
|
|
|
|
glGenVertexArrays(1, &triangles->array);
|
|
|
|
glBindVertexArray(triangles->array);
|
|
|
|
glGenBuffers(1, &triangles->vertices);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, triangles->vertices);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, nvertices * sizeof(rk_vec3), vertices, GL_STATIC_DRAW);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
return triangles;
|
|
|
|
}
|
|
|
|
|
|
|
|
rk_vertices_t rk_create_vertices(
|
|
|
|
rk_vertex_format const * format,
|
|
|
|
rk_uint nvertices,
|
|
|
|
void const * _vertices,
|
|
|
|
rk_uint nindices,
|
|
|
|
rk_ushort const * indices) {
|
2022-12-18 03:39:34 +01:00
|
|
|
if (!format || !nvertices || !_vertices || !nindices || !indices) {
|
2022-08-28 04:23:13 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-12-20 07:01:58 +01:00
|
|
|
unsigned vertex_size = 0;
|
2022-12-27 06:27:08 +01:00
|
|
|
unsigned nattribs = 0;
|
2022-08-28 04:23:13 +02:00
|
|
|
for (rk_vertex_format const * f = format; *f; ++f) {
|
2022-12-05 05:50:04 +01:00
|
|
|
switch (*f & RK_VERTEX_FORMAT_MASK) {
|
2022-08-28 04:23:13 +02:00
|
|
|
case RK_VERTEX_FORMAT_VEC3_FLOAT:
|
2022-12-27 06:27:08 +01:00
|
|
|
vertex_size += sizeof(rk_vec3_float);
|
|
|
|
nattribs += 1;
|
2022-08-28 04:23:13 +02:00
|
|
|
break;
|
|
|
|
case RK_VERTEX_FORMAT_VEC3_INT10:
|
2022-12-27 06:27:08 +01:00
|
|
|
vertex_size += sizeof(rk_vec3_int10);
|
|
|
|
nattribs += 1;
|
2022-12-05 05:50:04 +01:00
|
|
|
break;
|
|
|
|
case RK_VERTEX_FORMAT_VEC3_UINT10:
|
2022-12-27 06:27:08 +01:00
|
|
|
vertex_size += sizeof(rk_vec3_uint10);
|
|
|
|
nattribs += 1;
|
2022-08-28 04:23:13 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rk_printf("rk_create_vertices(): invalid format.");
|
|
|
|
return nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!vertex_size) {
|
|
|
|
rk_printf("rk_create_vertices(): empty format.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
rk_vertices * const vertices = new rk_vertices;
|
2022-12-27 06:27:08 +01:00
|
|
|
vertices->vertex_size = vertex_size;
|
2022-08-28 04:23:13 +02:00
|
|
|
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);
|
2022-12-27 06:27:08 +01:00
|
|
|
glBindVertexBuffer(RK_VERTICES_BINDING, vertices->vertices, 0, vertices->vertex_size);
|
|
|
|
for (unsigned attrib = 0; attrib < nattribs; ++attrib) {
|
|
|
|
glEnableVertexAttribArray(attrib);
|
|
|
|
}
|
2022-08-28 04:23:13 +02:00
|
|
|
vertices->layout = 0;
|
2022-12-20 07:01:58 +01:00
|
|
|
unsigned offset = 0;
|
2022-12-27 06:27:08 +01:00
|
|
|
for (rk_vertex_format const * f = format; *f; ++f) {
|
2022-12-26 14:05:06 +01:00
|
|
|
GLboolean const norm = (*f & RK_VERTEX_FORMAT_NORMALIZE) != 0;
|
2022-12-05 05:50:04 +01:00
|
|
|
switch (*f & RK_VERTEX_FORMAT_MASK) {
|
2022-08-28 04:23:13 +02:00
|
|
|
case RK_VERTEX_FORMAT_VEC3_FLOAT:
|
2022-12-27 06:27:08 +01:00
|
|
|
glVertexAttribFormat(vertices->layout++, 3, GL_FLOAT, GL_FALSE, offset);
|
|
|
|
offset += sizeof(rk_vec3_float);
|
2022-08-28 04:23:13 +02:00
|
|
|
break;
|
|
|
|
case RK_VERTEX_FORMAT_VEC3_INT10:
|
2022-12-27 06:27:08 +01:00
|
|
|
glVertexAttribFormat(vertices->layout++, 4, GL_INT_2_10_10_10_REV, norm, offset);
|
|
|
|
offset += sizeof(rk_vec3_int10);
|
2022-12-05 05:50:04 +01:00
|
|
|
break;
|
|
|
|
case RK_VERTEX_FORMAT_VEC3_UINT10:
|
2022-12-27 06:27:08 +01:00
|
|
|
glVertexAttribFormat(vertices->layout++, 4, GL_UNSIGNED_INT_2_10_10_10_REV, norm, offset);
|
|
|
|
offset += sizeof(rk_vec3_uint10);
|
2022-08-28 04:23:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 06:27:08 +01:00
|
|
|
for (unsigned attrib = 0; attrib < nattribs; ++attrib) {
|
|
|
|
glVertexAttribBinding(attrib, RK_VERTICES_BINDING);
|
|
|
|
}
|
2022-08-28 04:23:13 +02:00
|
|
|
glGenBuffers(1, &vertices->indices);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertices->indices);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, nindices * sizeof(rk_ushort), indices, GL_STATIC_DRAW);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
return vertices;
|
|
|
|
}
|
|
|
|
|
2022-12-26 14:05:06 +01:00
|
|
|
static void rk_pack_vec3_float(
|
|
|
|
rk_pack_dst const dst,
|
|
|
|
rk_pack_src const src) {
|
|
|
|
*dst.vec3_float = *src.vec3_float;
|
2022-12-17 16:50:41 +01:00
|
|
|
}
|
|
|
|
|
2022-12-26 14:05:06 +01:00
|
|
|
static void rk_pack_vec3_short(
|
|
|
|
rk_pack_dst const dst,
|
|
|
|
rk_pack_src const src) {
|
|
|
|
dst.vec3_short->x = static_cast<rk_short>(src.vec3_float->x);
|
|
|
|
dst.vec3_short->y = static_cast<rk_short>(src.vec3_float->y);
|
|
|
|
dst.vec3_short->z = static_cast<rk_short>(src.vec3_float->z);
|
2022-12-17 16:50:41 +01:00
|
|
|
}
|
|
|
|
|
2022-12-26 14:05:06 +01:00
|
|
|
static void rk_pack_vec3_short_norm(
|
|
|
|
rk_pack_dst const dst,
|
|
|
|
rk_pack_src const src) {
|
2022-12-17 04:58:19 +01:00
|
|
|
#define _convert(s) (static_cast<rk_short>((s) * ((s) < 0.f ? 32768.f : 32767.f)))
|
2022-12-26 14:05:06 +01:00
|
|
|
dst.vec3_short->x = _convert(src.vec3_float->x);
|
|
|
|
dst.vec3_short->y = _convert(src.vec3_float->y);
|
|
|
|
dst.vec3_short->z = _convert(src.vec3_float->z);
|
2022-12-17 04:58:19 +01:00
|
|
|
#undef _convert
|
|
|
|
}
|
|
|
|
|
2022-12-17 16:50:41 +01:00
|
|
|
static void rk_pack_vec3_int10(
|
2022-12-26 14:05:06 +01:00
|
|
|
rk_pack_dst const dst,
|
|
|
|
rk_pack_src const src) {
|
|
|
|
#define _convert(s) (static_cast<rk_int>((s)) & 1023)
|
|
|
|
*dst.vec3_int10 =
|
|
|
|
_convert(src.vec3_float->x) | (_convert(src.vec3_float->y) << 10) | (_convert(src.vec3_float->z) << 20);
|
|
|
|
#undef _convert
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rk_pack_vec3_int10_norm(
|
|
|
|
rk_pack_dst const dst,
|
|
|
|
rk_pack_src const src) {
|
2022-12-17 04:58:19 +01:00
|
|
|
#define _convert(s) (static_cast<rk_int>((s) * ((s) < 0.f ? 512.f : 511.f)) & 1023)
|
2022-12-26 14:05:06 +01:00
|
|
|
*dst.vec3_int10 =
|
|
|
|
_convert(src.vec3_float->x) | (_convert(src.vec3_float->y) << 10) | (_convert(src.vec3_float->z) << 20);
|
|
|
|
#undef _convert
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rk_pack_mat3_float(
|
|
|
|
rk_pack_dst const dst,
|
|
|
|
rk_pack_src const src) {
|
|
|
|
*dst.mat3_float = *src.mat3_float;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rk_pack_mat3_int10(
|
|
|
|
rk_pack_dst const dst,
|
|
|
|
rk_pack_src const src) {
|
|
|
|
#define _convert(s) (static_cast<rk_int>((s)) & 1023)
|
|
|
|
dst.mat3_int10->x =
|
|
|
|
_convert(src.mat3_float->x.x) | (_convert(src.mat3_float->x.y) << 10) | (_convert(src.mat3_float->x.z) << 20);
|
|
|
|
dst.mat3_int10->y =
|
|
|
|
_convert(src.mat3_float->y.x) | (_convert(src.mat3_float->y.y) << 10) | (_convert(src.mat3_float->y.z) << 20);
|
|
|
|
dst.mat3_int10->z =
|
|
|
|
_convert(src.mat3_float->z.x) | (_convert(src.mat3_float->z.y) << 10) | (_convert(src.mat3_float->z.z) << 20);
|
|
|
|
#undef _convert
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rk_pack_mat3_int10_norm(
|
|
|
|
rk_pack_dst const dst,
|
|
|
|
rk_pack_src const src) {
|
|
|
|
#define _convert(s) (static_cast<rk_int>((s) * ((s) < 0.f ? 512.f : 511.f)) & 1023)
|
|
|
|
dst.mat3_int10->x =
|
|
|
|
_convert(src.mat3_float->x.x) | (_convert(src.mat3_float->x.y) << 10) | (_convert(src.mat3_float->x.z) << 20);
|
|
|
|
dst.mat3_int10->y =
|
|
|
|
_convert(src.mat3_float->y.x) | (_convert(src.mat3_float->y.y) << 10) | (_convert(src.mat3_float->y.z) << 20);
|
|
|
|
dst.mat3_int10->z =
|
|
|
|
_convert(src.mat3_float->z.x) | (_convert(src.mat3_float->z.y) << 10) | (_convert(src.mat3_float->z.z) << 20);
|
2022-12-17 04:58:19 +01:00
|
|
|
#undef _convert
|
|
|
|
}
|
2022-08-28 04:23:13 +02:00
|
|
|
|
2022-12-18 03:39:34 +01:00
|
|
|
//TODO: multiple batches per vertices
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_batch_t rk_create_batch(
|
2022-12-18 03:39:34 +01:00
|
|
|
rk_vertices_t _vertices,
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_uint max_size,
|
2022-12-17 04:58:19 +01:00
|
|
|
rk_param_format const * params_format) {
|
2022-12-18 03:39:34 +01:00
|
|
|
rk_vertices const * const vertices = reinterpret_cast<rk_vertices const *>(_vertices);
|
|
|
|
if (!vertices || !max_size || !params_format || max_size > RK_BATCH_MAX_SIZE) {
|
2022-12-17 04:58:19 +01:00
|
|
|
rk_printf("rk_create_batch(): invalid parameters.");
|
2022-08-28 04:23:13 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-12-20 07:01:58 +01:00
|
|
|
unsigned nparams = 0;
|
|
|
|
unsigned params_size = 0;
|
|
|
|
unsigned packed_size = 0;
|
2022-12-27 06:27:08 +01:00
|
|
|
unsigned nattribs = 0;
|
2022-12-17 04:58:19 +01:00
|
|
|
for (rk_param_format const * f = params_format; *f; ++f, ++nparams) {
|
|
|
|
switch (*f & RK_PARAM_FORMAT_MASK) {
|
|
|
|
case RK_PARAM_FORMAT_VEC3_FLOAT:
|
2022-12-26 14:05:06 +01:00
|
|
|
params_size += sizeof(rk_vec3_float);
|
|
|
|
packed_size += sizeof(rk_vec3_float);
|
2022-12-27 06:27:08 +01:00
|
|
|
nattribs += 1;
|
2022-12-17 04:58:19 +01:00
|
|
|
break;
|
|
|
|
case RK_PARAM_FORMAT_VEC3_SHORT:
|
2022-12-26 14:05:06 +01:00
|
|
|
params_size += sizeof(rk_vec3_float);
|
|
|
|
packed_size += sizeof(rk_vec3_short);
|
2022-12-27 06:27:08 +01:00
|
|
|
nattribs += 1;
|
2022-12-17 04:58:19 +01:00
|
|
|
break;
|
|
|
|
case RK_PARAM_FORMAT_VEC3_INT10:
|
2022-12-26 14:05:06 +01:00
|
|
|
params_size += sizeof(rk_vec3_float);
|
|
|
|
packed_size += sizeof(rk_vec3_int10);
|
2022-12-27 06:27:08 +01:00
|
|
|
nattribs += 1;
|
2022-12-26 14:05:06 +01:00
|
|
|
break;
|
|
|
|
case RK_PARAM_FORMAT_MAT3_FLOAT:
|
|
|
|
params_size += sizeof(rk_mat3_float);
|
|
|
|
packed_size += sizeof(rk_mat3_float);
|
2022-12-27 06:27:08 +01:00
|
|
|
nattribs += 3;
|
2022-12-26 14:05:06 +01:00
|
|
|
break;
|
|
|
|
case RK_PARAM_FORMAT_MAT3_INT10:
|
|
|
|
params_size += sizeof(rk_mat3_float);
|
|
|
|
packed_size += sizeof(rk_mat3_int10);
|
2022-12-27 06:27:08 +01:00
|
|
|
nattribs += 3;
|
2022-12-17 04:58:19 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rk_printf("rk_create_batch(): invalid param format.");
|
|
|
|
return nullptr;
|
|
|
|
break;
|
|
|
|
}
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
2022-12-27 06:27:08 +01:00
|
|
|
glBindVertexArray(vertices->array);
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_batch * batch = new rk_batch;
|
|
|
|
batch->size = max_size;
|
2022-12-17 04:58:19 +01:00
|
|
|
batch->nparams = nparams;
|
2022-08-28 04:23:13 +02:00
|
|
|
batch->params_size = params_size;
|
2022-12-17 16:50:41 +01:00
|
|
|
batch->packed_size = packed_size;
|
2022-12-17 04:58:19 +01:00
|
|
|
batch->indices = new rk_ushort[max_size];
|
|
|
|
batch->commands = new rk_command[max_size * sizeof(rk_command)];
|
2022-12-27 06:27:08 +01:00
|
|
|
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;
|
2022-12-17 04:58:19 +01:00
|
|
|
if (nparams) {
|
2022-12-17 16:50:41 +01:00
|
|
|
batch->packers = new rk_packer[nparams];
|
|
|
|
batch->params = new rk_ubyte[max_size * packed_size];
|
2022-12-27 06:27:08 +01:00
|
|
|
memset(batch->params, 0, max_size * packed_size);
|
2022-12-17 04:58:19 +01:00
|
|
|
glGenBuffers(1, &batch->params_buffer);
|
2022-12-27 06:27:08 +01:00
|
|
|
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);
|
|
|
|
}
|
2022-12-17 16:50:41 +01:00
|
|
|
rk_packer * packer = batch->packers;
|
2022-12-26 14:05:06 +01:00
|
|
|
unsigned layout = vertices->layout;
|
2022-12-20 07:01:58 +01:00
|
|
|
unsigned offset = 0;
|
2022-12-26 14:05:06 +01:00
|
|
|
for (rk_param_format const * f = params_format; *f; ++f, ++packer) {
|
|
|
|
GLboolean const norm = (*f & RK_PARAM_FORMAT_NORMALIZE) != 0;
|
2022-12-17 04:58:19 +01:00
|
|
|
switch (*f & RK_PARAM_FORMAT_MASK) {
|
|
|
|
case RK_PARAM_FORMAT_VEC3_FLOAT:
|
2022-12-26 14:05:06 +01:00
|
|
|
glVertexAttribFormat(layout++, 3, GL_FLOAT, GL_FALSE, offset);
|
|
|
|
packer->pack = rk_pack_vec3_float;
|
|
|
|
packer->src_incr = sizeof(rk_vec3_float);
|
|
|
|
packer->dst_incr = sizeof(rk_vec3_float);
|
2022-12-17 04:58:19 +01:00
|
|
|
break;
|
|
|
|
case RK_PARAM_FORMAT_VEC3_SHORT:
|
2022-12-26 14:05:06 +01:00
|
|
|
glVertexAttribFormat(layout++, 3, GL_SHORT, norm, offset);
|
|
|
|
if (norm) {
|
|
|
|
packer->pack = rk_pack_vec3_short_norm;
|
2022-12-17 16:50:41 +01:00
|
|
|
} else {
|
2022-12-26 14:05:06 +01:00
|
|
|
packer->pack = rk_pack_vec3_short;
|
2022-12-17 04:58:19 +01:00
|
|
|
}
|
2022-12-26 14:05:06 +01:00
|
|
|
packer->src_incr = sizeof(rk_vec3_float);
|
|
|
|
packer->dst_incr = sizeof(rk_vec3_short);
|
2022-12-17 04:58:19 +01:00
|
|
|
break;
|
|
|
|
case RK_PARAM_FORMAT_VEC3_INT10:
|
2022-12-26 14:05:06 +01:00
|
|
|
glVertexAttribFormat(layout++, 4, GL_INT_2_10_10_10_REV, norm, offset);
|
|
|
|
if (norm) {
|
|
|
|
packer->pack = rk_pack_vec3_int10_norm;
|
|
|
|
} else {
|
|
|
|
packer->pack = rk_pack_vec3_int10;
|
|
|
|
}
|
|
|
|
packer->src_incr = sizeof(rk_vec3_float);
|
|
|
|
packer->dst_incr = sizeof(rk_vec3_int10);
|
|
|
|
break;
|
|
|
|
case RK_PARAM_FORMAT_MAT3_FLOAT:
|
|
|
|
glVertexAttribFormat(layout++, 3, GL_FLOAT, GL_FALSE, offset + offsetof(rk_mat3_float, x));
|
|
|
|
glVertexAttribFormat(layout++, 3, GL_FLOAT, GL_FALSE, offset + offsetof(rk_mat3_float, y));
|
|
|
|
glVertexAttribFormat(layout++, 3, GL_FLOAT, GL_FALSE, offset + offsetof(rk_mat3_float, z));
|
|
|
|
packer->pack = rk_pack_mat3_float;
|
|
|
|
packer->src_incr = sizeof(rk_mat3_float);
|
|
|
|
packer->dst_incr = sizeof(rk_mat3_float);
|
|
|
|
break;
|
|
|
|
case RK_PARAM_FORMAT_MAT3_INT10:
|
|
|
|
glVertexAttribFormat(layout++, 4, GL_INT_2_10_10_10_REV, norm, offset + offsetof(rk_mat3_int10, x));
|
|
|
|
glVertexAttribFormat(layout++, 4, GL_INT_2_10_10_10_REV, norm, offset + offsetof(rk_mat3_int10, y));
|
|
|
|
glVertexAttribFormat(layout++, 4, GL_INT_2_10_10_10_REV, norm, offset + offsetof(rk_mat3_int10, z));
|
|
|
|
if (norm) {
|
|
|
|
packer->pack = rk_pack_mat3_int10_norm;
|
|
|
|
} else {
|
|
|
|
packer->pack = rk_pack_mat3_int10;
|
|
|
|
}
|
|
|
|
packer->src_incr = sizeof(rk_mat3_float);
|
|
|
|
packer->dst_incr = sizeof(rk_mat3_int10);
|
2022-12-17 04:58:19 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-12-17 16:50:41 +01:00
|
|
|
offset += packer->dst_incr;
|
2022-12-26 14:05:06 +01:00
|
|
|
}
|
2022-12-27 06:27:08 +01:00
|
|
|
for (unsigned attrib = vertices->layout; attrib < vertices->layout + nattribs; ++attrib) {
|
2022-12-26 14:05:06 +01:00
|
|
|
glVertexAttribBinding(attrib, RK_PARAMS_BINDING);
|
2022-12-17 04:58:19 +01:00
|
|
|
}
|
|
|
|
glVertexBindingDivisor(RK_PARAMS_BINDING, 1);
|
2022-12-27 06:27:08 +01:00
|
|
|
} else {
|
|
|
|
batch->params_buffer = 0;
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
2022-12-27 06:27:08 +01:00
|
|
|
glBindVertexArray(0);
|
2022-08-28 04:23:13 +02:00
|
|
|
return batch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_begin_frame() {
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
2022-12-18 03:39:34 +01:00
|
|
|
void rk_select_shader(
|
|
|
|
rk_shader_t _shader) {
|
|
|
|
rk_shader * const shader = reinterpret_cast<rk_shader *>(_shader);
|
|
|
|
if (shader) {
|
|
|
|
rk_current_shader = shader;
|
|
|
|
glUseProgram(shader->program);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_set_input_float(
|
|
|
|
rk_input_t _input,
|
|
|
|
float value) {
|
|
|
|
GLint const input = reinterpret_cast<intptr_t>(_input) - 1;
|
|
|
|
if (rk_current_shader && input > -1) {
|
|
|
|
glUniform1f(input, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_set_input_vec3(
|
|
|
|
rk_input_t _input,
|
|
|
|
rk_vec3 const & value) {
|
|
|
|
GLint const input = reinterpret_cast<intptr_t>(_input) - 1;
|
|
|
|
if (rk_current_shader && input > -1) {
|
|
|
|
glUniform3fv(input, 1, glm::value_ptr(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_set_input_mat3(
|
|
|
|
rk_input_t _input,
|
|
|
|
rk_mat3 const & value) {
|
|
|
|
GLint const input = reinterpret_cast<intptr_t>(_input) - 1;
|
|
|
|
if (rk_current_shader && input > -1) {
|
|
|
|
glUniformMatrix3fv(input, 1, GL_FALSE, glm::value_ptr(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_set_input_mat4(
|
|
|
|
rk_input_t _input,
|
|
|
|
rk_mat4 const & value) {
|
|
|
|
GLint const input = reinterpret_cast<intptr_t>(_input) - 1;
|
|
|
|
if (rk_current_shader && input > -1) {
|
|
|
|
glUniformMatrix4fv(input, 1, GL_FALSE, glm::value_ptr(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_set_param_vec3(
|
|
|
|
rk_param_t _param,
|
|
|
|
rk_vec3 const & value) {
|
|
|
|
GLint const param = reinterpret_cast<intptr_t>(_param) - 1;
|
|
|
|
if (rk_current_shader && param > -1) {
|
|
|
|
glVertexAttrib3fv(param, glm::value_ptr(value));
|
|
|
|
}
|
2022-12-27 06:20:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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]));
|
|
|
|
}
|
2022-12-18 03:39:34 +01:00
|
|
|
}
|
|
|
|
|
2022-08-28 04:23:13 +02:00
|
|
|
void rk_select_texture(
|
2022-12-19 06:27:34 +01:00
|
|
|
rk_uint slot,
|
2022-12-18 03:39:34 +01:00
|
|
|
rk_texture_t _texture,
|
|
|
|
rk_input_t _sampler) {
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_texture const * const texture = reinterpret_cast<rk_texture const *>(_texture);
|
2022-12-18 03:39:34 +01:00
|
|
|
GLint const sampler = reinterpret_cast<intptr_t>(_sampler) - 1;
|
|
|
|
if (texture && sampler > -1 && rk_current_shader) {
|
2022-12-19 06:27:34 +01:00
|
|
|
glActiveTexture(GL_TEXTURE0 + slot);
|
2022-08-28 04:23:13 +02:00
|
|
|
if (texture->nlevels) {
|
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, texture->texture);
|
|
|
|
} else {
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->texture);
|
|
|
|
}
|
2022-12-19 06:27:34 +01:00
|
|
|
glUniform1i(sampler, slot);
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RK_EXPORT void rk_draw_triangles(
|
|
|
|
rk_triangles_t _triangles) {
|
|
|
|
rk_triangles const * const triangles = reinterpret_cast<rk_triangles const *>(_triangles);
|
|
|
|
if (triangles && rk_current_shader && !rk_current_vertices) {
|
|
|
|
glBindVertexArray(triangles->array);
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, triangles->size);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_select_vertices(
|
|
|
|
rk_vertices_t _vertices) {
|
|
|
|
rk_vertices * const vertices = reinterpret_cast<rk_vertices *>(_vertices);
|
2022-12-18 03:39:34 +01:00
|
|
|
if (vertices) {
|
2022-08-28 04:23:13 +02:00
|
|
|
glBindVertexArray(vertices->array);
|
|
|
|
rk_current_vertices = vertices;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 07:01:58 +01:00
|
|
|
static unsigned rk_batch_filter(
|
2022-12-17 16:50:41 +01:00
|
|
|
rk_batch & batch,
|
2022-12-20 07:01:58 +01:00
|
|
|
unsigned const size,
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_instance_flags const * flags) {
|
2022-12-17 16:50:41 +01:00
|
|
|
rk_ushort * indices = batch.indices;
|
2022-12-20 07:01:58 +01:00
|
|
|
for (unsigned index = 0; index < size; ++index, ++flags) {
|
2022-08-28 04:23:13 +02:00
|
|
|
if ((*flags & RK_INSTANCE_FLAGS_SPAWNED_VISIBLE) == RK_INSTANCE_FLAGS_SPAWNED_VISIBLE) {
|
2022-12-20 07:01:58 +01:00
|
|
|
*indices++ = static_cast<rk_ushort>(index);
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-17 16:50:41 +01:00
|
|
|
return indices - batch.indices;
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 07:01:58 +01:00
|
|
|
static unsigned rk_batch_build_commands(
|
2022-12-17 16:50:41 +01:00
|
|
|
rk_batch & batch,
|
2022-12-20 07:01:58 +01:00
|
|
|
unsigned const ninstances,
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_mesh const * const meshes) {
|
2022-12-17 16:50:41 +01:00
|
|
|
rk_command * commands = batch.commands;
|
|
|
|
rk_ushort * base = batch.indices;
|
|
|
|
rk_ushort * const last = batch.indices + ninstances;
|
|
|
|
for (rk_ushort * first = batch.indices; first < last; base = first, ++commands) {
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_mesh const & mesh = meshes[*first++];
|
|
|
|
for ( ; first < last && meshes[*first].packed == mesh.packed; ++first) {
|
|
|
|
}
|
|
|
|
for (rk_ushort * second = first; second < last; ++second) {
|
2022-12-20 07:01:58 +01:00
|
|
|
unsigned const index = *second;
|
2022-08-28 04:23:13 +02:00
|
|
|
if (meshes[index].packed == mesh.packed) {
|
|
|
|
*second = *first;
|
2022-12-20 07:01:58 +01:00
|
|
|
*first++ = static_cast<rk_ushort>(index);
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-17 16:50:41 +01:00
|
|
|
commands->nvertices = static_cast<GLuint>(mesh.ntriangles) * 3;
|
2022-08-28 04:23:13 +02:00
|
|
|
commands->ninstances = first - base;
|
2022-12-17 16:50:41 +01:00
|
|
|
commands->base_index = mesh.base_index;
|
2022-08-28 04:23:13 +02:00
|
|
|
commands->base_vertex = 0;
|
2022-12-17 16:50:41 +01:00
|
|
|
commands->base_instance = base - batch.indices;
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
2022-12-17 16:50:41 +01:00
|
|
|
return commands - batch.commands;
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 06:33:31 +01:00
|
|
|
static void rk_batch_pack(
|
2022-12-17 04:58:19 +01:00
|
|
|
rk_batch & batch,
|
2022-12-20 07:01:58 +01:00
|
|
|
unsigned const ninstances,
|
2022-12-17 16:50:41 +01:00
|
|
|
rk_ubyte const * const params) {
|
2022-12-20 06:33:31 +01:00
|
|
|
rk_pack_dst dst(batch.params);
|
2022-12-17 16:50:41 +01:00
|
|
|
rk_ushort const * const last_index = batch.indices + ninstances;
|
|
|
|
rk_packer const * const last_packer = batch.packers + batch.nparams;
|
2022-12-17 04:58:19 +01:00
|
|
|
for (rk_ushort const * index = batch.indices; index < last_index; ++index) {
|
2022-12-20 06:33:31 +01:00
|
|
|
rk_pack_src src(¶ms[batch.params_size * (*index)]);
|
2022-12-17 16:50:41 +01:00
|
|
|
for (rk_packer const * packer = batch.packers; packer < last_packer; ++packer) {
|
|
|
|
packer->pack(dst, src);
|
2022-12-20 06:33:31 +01:00
|
|
|
src.ptr += packer->src_incr;
|
2022-12-26 14:05:06 +01:00
|
|
|
dst.ptr += packer->dst_incr;
|
2022-12-17 04:58:19 +01:00
|
|
|
}
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_draw_batch(
|
|
|
|
rk_batch_t _batch,
|
|
|
|
rk_uint size,
|
|
|
|
rk_instance_flags const * flags,
|
|
|
|
rk_mesh const * meshes,
|
2022-12-17 16:50:41 +01:00
|
|
|
rk_ubyte const * params) {
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_batch & batch = *reinterpret_cast<rk_batch *>(_batch);
|
2022-12-17 04:58:19 +01:00
|
|
|
if (!size || size > batch.size || !flags || !meshes || !rk_current_shader || !rk_current_vertices) {
|
2022-08-28 04:23:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-12-20 07:01:58 +01:00
|
|
|
unsigned const ninstances = rk_batch_filter(batch, size, flags);
|
2022-12-17 16:50:41 +01:00
|
|
|
if (!ninstances) {
|
2022-08-28 04:23:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rk_MultiDrawElementsIndirect) {
|
|
|
|
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, batch.commands_buffer);
|
|
|
|
}
|
2022-12-17 04:58:19 +01:00
|
|
|
if (batch.nparams) {
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, batch.params_buffer);
|
2022-12-27 06:27:08 +01:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0, ninstances * batch.packed_size, batch.params);
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
|
|
|
if (rk_DrawElementsInstancedBaseInstance) {
|
|
|
|
if (rk_MultiDrawElementsIndirect) {
|
|
|
|
rk_MultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_SHORT, nullptr, ncommands, sizeof(rk_command));
|
|
|
|
} else {
|
|
|
|
rk_command const * const last_command = batch.commands + ncommands;
|
|
|
|
for (rk_command const * command = batch.commands; command < last_command; ++command) {
|
|
|
|
rk_DrawElementsInstancedBaseInstance(
|
2022-12-17 16:50:41 +01:00
|
|
|
GL_TRIANGLES, command->nvertices, GL_UNSIGNED_SHORT,
|
2022-08-28 04:23:13 +02:00
|
|
|
reinterpret_cast<void const *>(command->base_index << 1),
|
|
|
|
command->ninstances, command->base_instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-20 07:01:58 +01:00
|
|
|
unsigned params_offset = 0;
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_command const * const last_command = batch.commands + ncommands;
|
|
|
|
for (rk_command const * command = batch.commands; command < last_command; ++command) {
|
2022-12-17 04:58:19 +01:00
|
|
|
if (batch.nparams) {
|
2022-12-17 16:50:41 +01:00
|
|
|
glBindVertexBuffer(RK_PARAMS_BINDING, batch.params_buffer, params_offset, batch.packed_size);
|
|
|
|
params_offset += command->ninstances * batch.packed_size;
|
2022-12-17 04:58:19 +01:00
|
|
|
}
|
2022-08-28 04:23:13 +02:00
|
|
|
glDrawElementsInstanced(
|
2022-12-17 16:50:41 +01:00
|
|
|
GL_TRIANGLES, command->nvertices, GL_UNSIGNED_SHORT,
|
2022-08-28 04:23:13 +02:00
|
|
|
reinterpret_cast<void const *>(command->base_index << 1),
|
|
|
|
command->ninstances);
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 06:27:08 +01:00
|
|
|
if (rk_MultiDrawElementsIndirect) {
|
|
|
|
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
|
|
|
|
}
|
|
|
|
if (batch.nparams) {
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
}
|
2022-08-28 04:23:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void rk_unselect_vertices(
|
|
|
|
rk_vertices_t _vertices) {
|
|
|
|
rk_current_vertices = nullptr;
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_unselect_texture(
|
2022-12-19 06:27:34 +01:00
|
|
|
rk_uint slot,
|
2022-08-28 04:23:13 +02:00
|
|
|
rk_texture_t _texture) {
|
|
|
|
rk_texture const * const texture = reinterpret_cast<rk_texture const *>(_texture);
|
|
|
|
if (texture) {
|
2022-12-19 06:27:34 +01:00
|
|
|
glActiveTexture(GL_TEXTURE0 + slot);
|
2022-08-28 04:23:13 +02:00
|
|
|
if (texture->nlevels) {
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
} else {
|
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_unselect_shader(
|
|
|
|
rk_shader_t _shader) {
|
|
|
|
rk_current_shader = nullptr;
|
|
|
|
glUseProgram(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_end_frame() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_destroy_batch(
|
|
|
|
rk_batch_t _batch) {
|
|
|
|
rk_batch * const batch = reinterpret_cast<rk_batch *>(_batch);
|
|
|
|
if (batch) {
|
|
|
|
delete[] batch->indices;
|
|
|
|
delete[] batch->commands;
|
2022-12-17 04:58:19 +01:00
|
|
|
if (batch->nparams) {
|
2022-12-17 16:50:41 +01:00
|
|
|
delete[] batch->packers;
|
2022-12-17 04:58:19 +01:00
|
|
|
delete[] batch->params;
|
|
|
|
glDeleteBuffers(1, &batch->params_buffer);
|
|
|
|
}
|
2022-08-28 04:23:13 +02:00
|
|
|
if (rk_MultiDrawElementsIndirect) {
|
|
|
|
glDeleteBuffers(1, &batch->commands_buffer);
|
|
|
|
}
|
|
|
|
delete batch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_destroy_triangles(
|
|
|
|
rk_triangles_t _triangles) {
|
|
|
|
rk_triangles * const triangles = reinterpret_cast<rk_triangles *>(_triangles);
|
|
|
|
if (triangles) {
|
|
|
|
glDeleteBuffers(1, &triangles->vertices);
|
|
|
|
glDeleteVertexArrays(1, &triangles->array);
|
|
|
|
delete triangles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_destroy_vertices(
|
|
|
|
rk_vertices_t _vertices) {
|
|
|
|
rk_vertices * const vertices = reinterpret_cast<rk_vertices *>(_vertices);
|
|
|
|
if (vertices) {
|
|
|
|
glDeleteBuffers(1, &vertices->indices);
|
|
|
|
glDeleteBuffers(1, &vertices->vertices);
|
|
|
|
glDeleteVertexArrays(1, &vertices->array);
|
|
|
|
delete vertices;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_destroy_texture(
|
|
|
|
rk_texture_t _texture) {
|
|
|
|
rk_texture * const texture = reinterpret_cast<rk_texture *>(_texture);
|
|
|
|
if (texture) {
|
|
|
|
glDeleteTextures(1, &texture->texture);
|
|
|
|
delete texture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rk_destroy_shader(
|
|
|
|
rk_shader_t _shader) {
|
|
|
|
rk_shader * const shader = reinterpret_cast<rk_shader *>(_shader);
|
|
|
|
if (shader) {
|
|
|
|
glDeleteShader(shader->vertex);
|
|
|
|
glDeleteShader(shader->fragment);
|
|
|
|
glDeleteProgram(shader->program);
|
|
|
|
delete shader;
|
|
|
|
}
|
|
|
|
}
|