// 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 .
#include "render_opengles.hpp"
#include
static rk_shader const * rk_current_shader = nullptr;
static rk_vertices const * rk_current_vertices = nullptr;
static void rk_printf(char const * messsage) {
printf("[RK_ENGINE] %s\n", messsage);
}
#define rk_error(message) { if (glGetError() != GL_NO_ERROR) { rk_printf(message); } }
static void rk_debug_message_callback(
GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
GLchar const * message,
void const * userParam) {
if (id == 131169 || id == 131185 || id == 131218 || id == 131204) {
return;
}
printf("[RK_ENGINE][GL] (id=%d) %s\n", id, message);
}
rk_window_t rk_initialize(
char const * name,
rk_uint width,
rk_uint height) {
rk_window_t const window = rk_create_context(name, width, height);
if (window) {
GLubyte const * const vendor = glGetString(GL_VENDOR);
GLubyte const * const renderer = glGetString(GL_RENDERER);
printf("[RK_ENGINE] vendor: %s, renderer: %s\n", vendor, renderer);
GLubyte const * const version = glGetString(GL_VERSION);
GLubyte const * const language = glGetString(GL_SHADING_LANGUAGE_VERSION);
printf("[RK_ENGINE] version: %s, language: %s\n", version, language);
GLint context_flags = 0;
glGetIntegerv(GL_CONTEXT_FLAGS, &context_flags);
if (context_flags & GL_CONTEXT_FLAG_DEBUG_BIT) {
printf("[RK_ENGINE] Debug context enabled\n");
glDebugMessageCallback(rk_debug_message_callback, nullptr);
glEnable(GL_DEBUG_OUTPUT);
} else {
glDisable(GL_DEBUG_OUTPUT);
}
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);
}
return window;
}
static void rk_print_shader_infolog(GLuint shader) {
int length;
char infolog[1024];
glGetShaderInfoLog(shader, sizeof(infolog), &length, infolog);
if (length > 0) {
rk_printf(infolog);
}
}
static void rk_print_program_infolog(GLuint program) {
int length;
char infolog[1024];
glGetProgramInfoLog(program, sizeof(infolog), &length, infolog);
if (length > 0) {
rk_printf(infolog);
}
}
//TODO: external loading of shader sources
//TODO: error handling
rk_shader_t rk_load_shader(
char const * name) {
rk_shader * const shader = new rk_shader;
shader->vertex = glCreateShader(GL_VERTEX_SHADER);
shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
shader->program = glCreateProgram();
char vertex_name[256];
snprintf(vertex_name, sizeof(vertex_name), "%s_opengles.vert", name);
printf("[RK_ENGINE] Loading vertex shader %s...\n", vertex_name);
rk_uint vertex_length = 0;
char ** vertex_source = rk_load_shader_source(vertex_name, &vertex_length);
if (vertex_source) {
rk_printf("Compiling vertex shader...");
glShaderSource(shader->vertex, vertex_length, vertex_source, nullptr);
}
glCompileShader(shader->vertex);
rk_error("glCompileShader() failed.");
rk_print_shader_infolog(shader->vertex);
char fragment_name[256];
snprintf(fragment_name, sizeof(fragment_name), "%s_opengles.frag", name);
printf("[RK_ENGINE] Loading fragment shader %s...\n", fragment_name);
rk_uint fragment_length = 0;
char ** fragment_source = rk_load_shader_source(fragment_name, &fragment_length);
if (fragment_source) {
rk_printf("Compiling fragment shader...");
glShaderSource(shader->fragment, fragment_length, fragment_source, nullptr);
}
glCompileShader(shader->fragment);
rk_error("glCompileShader() failed.");
rk_print_shader_infolog(shader->fragment);
rk_printf("Linking program...");
glAttachShader(shader->program, shader->vertex);
glAttachShader(shader->program, shader->fragment);
glLinkProgram(shader->program);
rk_error("glLinkProgram() failed.");
rk_print_program_infolog(shader->program);
rk_printf("Done.");
glReleaseShaderCompiler();
if (vertex_source != nullptr) {
rk_free_shader_source(vertex_source, vertex_length);
}
if (fragment_source != nullptr) {
rk_free_shader_source(fragment_source, fragment_length);
}
return shader;
}
void rk_select_shader(
rk_shader_t _shader) {
rk_shader * const shader = reinterpret_cast(_shader);
if (shader) {
rk_current_shader = shader;
glUseProgram(shader->program);
}
}
rk_input_t rk_resolve_input(
char const * name) {
if (!rk_current_shader || !name) {
return nullptr;
}
GLint const uniform = glGetUniformLocation(rk_current_shader->program, name);
return reinterpret_cast(uniform + 1);
}
void rk_set_input_float(
rk_input_t _input,
float value) {
GLint const input = reinterpret_cast(_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(_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(_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(_input) - 1;
if (rk_current_shader && input > -1) {
glUniformMatrix4fv(input, 1, GL_FALSE, glm::value_ptr(value));
}
}
rk_texture_t rk_create_texture(
rk_uint slot,
char const * input,
rk_texture_format format,
rk_uint width,
rk_uint height,
rk_uint nlevels,
rk_texture_flags flags,
void const * pixels) {
if (!input || width == 0 || height == 0 || !pixels || !rk_current_shader) {
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;
case RK_TEXTURE_FORMAT_32F:
internal_format = GL_R32F;
source_format = GL_RED;
source_type = GL_FLOAT;
break;
default:
return nullptr;
break;
}
rk_texture * const texture = new rk_texture;
glActiveTexture(GL_TEXTURE0 + slot);
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->slot = slot;
texture->nlevels = nlevels;
texture->sampler = glGetUniformLocation(rk_current_shader->program, input);
if (texture->sampler == -1) {
printf("[RK_ENGINE] glGetUniformLocation(%s) failed\n", input);
}
glBindTexture(target, 0);
return texture;
}
rk_triangles_t rk_create_triangles(
rk_uint nvertices,
rk_vec3 const * vertices) {
if (nvertices == 0 || !vertices || !rk_current_shader) {
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) {
if (!format || !nvertices || !_vertices || !nindices || !indices || !rk_current_shader) {
return nullptr;
}
rk_uint vertex_size = 0;
for (rk_vertex_format const * f = format; *f; ++f) {
switch (*f) {
case RK_VERTEX_FORMAT_VEC2_FLOAT:
vertex_size += sizeof(float) * 2;
break;
case RK_VERTEX_FORMAT_VEC2_USHORT:
vertex_size += sizeof(rk_ushort) * 2;
break;
case RK_VERTEX_FORMAT_VEC3_FLOAT:
vertex_size += sizeof(float) * 3;
break;
case RK_VERTEX_FORMAT_VEC3_INT10:
vertex_size += sizeof(rk_uint);
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;
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);
vertices->vertex_size = vertex_size;
vertices->layout = 0;
rk_uint offset = 0;
for (rk_vertex_format const * f = format; *f; ++f, ++vertices->layout) {
glEnableVertexAttribArray(vertices->layout);
switch (*f) {
case RK_VERTEX_FORMAT_VEC2_FLOAT:
glVertexAttribFormat(vertices->layout, 2, GL_FLOAT, GL_FALSE, offset);
glVertexAttribBinding(vertices->layout, RK_VERTICES_BINDING);
offset += sizeof(float) * 2;
break;
case RK_VERTEX_FORMAT_VEC2_USHORT:
glVertexAttribFormat(vertices->layout, 2, GL_UNSIGNED_SHORT, GL_TRUE, offset);
glVertexAttribBinding(vertices->layout, RK_VERTICES_BINDING);
offset += sizeof(rk_ushort) * 2;
break;
case RK_VERTEX_FORMAT_VEC3_FLOAT:
glVertexAttribFormat(vertices->layout, 3, GL_FLOAT, GL_FALSE, offset);
glVertexAttribBinding(vertices->layout, RK_VERTICES_BINDING);
offset += sizeof(float) * 3;
break;
case RK_VERTEX_FORMAT_VEC3_INT10:
glVertexAttribFormat(vertices->layout, GL_BGRA, GL_INT_2_10_10_10_REV, GL_TRUE, offset);
glVertexAttribBinding(vertices->layout, RK_VERTICES_BINDING);
offset += sizeof(rk_uint);
break;
default:
break;
}
}
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);
glBindVertexArray(0);
return vertices;
}
//TODO: support for mat3 orientations with packing into int10 * 3
// - maybe from quaternions inputs
// - maybe it's possible to implement efficient quaternions in glsl?
rk_batch_t rk_create_batch(
rk_uint max_size,
rk_batch_translation_format translation_format,
rk_batch_orientation_format orientation_format) {
if (!max_size || max_size > RK_BATCH_MAX_SIZE || !rk_current_shader || !rk_current_vertices) {
return nullptr;
}
rk_uint translation_size = 0;
switch (translation_format) {
case RK_BATCH_TRANSLATION_FORMAT_FLOAT:
translation_size = sizeof(float) * 4;
break;
case RK_BATCH_TRANSLATION_FORMAT_SHORT:
translation_size = sizeof(rk_short) * 4;
break;
default:
rk_printf("rk_create_batch(): invalid translation format.");
return nullptr;
break;
}
rk_uint orientation_size = 0;
switch (orientation_format) {
case RK_BATCH_ORIENTATION_FORMAT_NONE:
orientation_size = 0;
break;
case RK_BATCH_ORIENTATION_FORMAT_FLOAT:
orientation_size = sizeof(float) * 3;
break;
case RK_BATCH_ORIENTATION_FORMAT_INT10:
orientation_size = sizeof(rk_uint);
break;
default:
rk_printf("rk_create_batch(): invalid orientation format.");
return nullptr;
break;
}
rk_uint const params_size = translation_size + orientation_size;
rk_batch * batch = new rk_batch;
batch->size = max_size;
batch->translation_format = translation_format;
batch->orientation_format = orientation_format;
batch->params_size = params_size;
batch->indices = new rk_ushort[batch->size];
batch->params = new rk_ubyte[batch->size * params_size];
batch->commands = new rk_command[batch->size * sizeof(rk_command)];
glGenBuffers(1, &batch->params_buffer);
rk_uint const translation_layout = rk_current_vertices->layout;
glEnableVertexAttribArray(translation_layout);
switch (translation_format) {
case RK_BATCH_TRANSLATION_FORMAT_FLOAT:
glVertexAttribFormat(translation_layout, 4, GL_FLOAT, GL_FALSE, 0);
break;
case RK_BATCH_TRANSLATION_FORMAT_SHORT:
glVertexAttribFormat(translation_layout, 4, GL_SHORT, GL_FALSE, 0);
break;
}
glVertexAttribBinding(translation_layout, RK_PARAMS_BINDING);
rk_uint const orientation_layout = rk_current_vertices->layout + 1;
switch (orientation_format) {
case RK_BATCH_ORIENTATION_FORMAT_NONE:
break;
case RK_BATCH_ORIENTATION_FORMAT_FLOAT:
glEnableVertexAttribArray(orientation_layout);
glVertexAttribFormat(orientation_layout, 3, GL_FLOAT, GL_FALSE, translation_size);
glVertexAttribBinding(orientation_layout, RK_PARAMS_BINDING);
break;
case RK_BATCH_ORIENTATION_FORMAT_INT10:
glEnableVertexAttribArray(orientation_layout);
glVertexAttribFormat(orientation_layout, GL_BGRA, GL_INT_2_10_10_10_REV, GL_TRUE, translation_size);
glVertexAttribBinding(orientation_layout, RK_PARAMS_BINDING);
break;
}
glVertexBindingDivisor(RK_PARAMS_BINDING, 1);
glBindVertexBuffer(RK_PARAMS_BINDING, batch->params_buffer, 0, batch->params_size);
if (rk_MultiDrawElementsIndirect) {
glGenBuffers(1, &batch->commands_buffer);
}
return batch;
}
void rk_begin_frame() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
void rk_select_texture(
rk_texture_t _texture) {
rk_texture const * const texture = reinterpret_cast(_texture);
if (texture) {
glActiveTexture(GL_TEXTURE0 + texture->slot);
if (texture->nlevels) {
glBindTexture(GL_TEXTURE_2D_ARRAY, texture->texture);
} else {
glBindTexture(GL_TEXTURE_2D, texture->texture);
}
if (texture->sampler > -1) {
glUniform1i(texture->sampler, texture->slot);
}
}
}
RK_EXPORT void rk_draw_triangles(
rk_triangles_t _triangles) {
rk_triangles const * const triangles = reinterpret_cast(_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(_vertices);
if (vertices && rk_current_shader) {
glBindVertexArray(vertices->array);
rk_current_vertices = vertices;
}
}
static rk_uint rk_batch_filter(
rk_uint const size,
rk_ushort * const _indices,
rk_instance_flags const * flags) {
rk_ushort * indices = _indices;
for (rk_ushort index = 0; index < size; ++index, ++flags) {
if ((*flags & RK_INSTANCE_FLAGS_SPAWNED_VISIBLE) == RK_INSTANCE_FLAGS_SPAWNED_VISIBLE) {
*indices++ = index;
}
}
return indices - _indices;
}
static rk_uint rk_batch_build_commands(
rk_uint const count,
rk_ushort * const indices,
rk_command * const _commands,
rk_mesh const * const meshes) {
rk_command * commands = _commands;
rk_ushort * base = indices;
rk_ushort * const last = indices + count;
for (rk_ushort * first = indices; first < last; base = first, ++commands) {
rk_mesh const & mesh = meshes[*first++];
for ( ; first < last && meshes[*first].packed == mesh.packed; ++first) {
}
for (rk_ushort * second = first; second < last; ++second) {
rk_ushort const index = *second;
if (meshes[index].packed == mesh.packed) {
*second = *first;
*first++ = index;
}
}
commands->count = static_cast(mesh.count) * 3;
commands->ninstances = first - base;
commands->base_index = mesh.offset;
commands->base_vertex = 0;
commands->base_instance = base - indices;
}
return commands - _commands;
}
template < typename _instance_params_t >
static void rk_batch_convert_params(
rk_uint const count,
rk_ushort const * const _indices,
rk_ubyte * const _params,
rk_vec3 const * const translations,
rk_ushort const * const texlevels,
rk_vec3 const * const orientations) {
_instance_params_t * params = reinterpret_cast<_instance_params_t *>(_params);
rk_ushort const * const last = _indices + count;
for (rk_ushort const * indices = _indices; indices < last; ++indices, ++params) {
rk_uint const index = *indices;
params->set(translations[index], texlevels[index], orientations[index]);
}
}
template < typename _instance_params_t >
static void rk_batch_convert_params(
rk_uint const count,
rk_ushort const * const _indices,
rk_ubyte * const _params,
rk_vec3 const * const translations,
rk_ushort const * const texlevels) {
_instance_params_t * params = reinterpret_cast<_instance_params_t *>(_params);
rk_ushort const * const last = _indices + count;
for (rk_ushort const * indices = _indices; indices < last; ++indices, ++params) {
rk_uint const index = *indices;
params->set(translations[index], texlevels[index]);
}
}
void rk_draw_batch(
rk_batch_t _batch,
rk_uint size,
rk_instance_flags const * flags,
rk_ushort const * texlevels,
rk_mesh const * meshes,
rk_vec3 const * translations,
rk_vec3 const * orientations) {
rk_batch & batch = *reinterpret_cast(_batch);
if (!size || size > batch.size || !flags || !texlevels || !meshes || !translations ||
!rk_current_shader || !rk_current_vertices) {
return;
}
if (batch.orientation_format != RK_BATCH_ORIENTATION_FORMAT_NONE && !orientations) {
return;
}
rk_uint const count = rk_batch_filter(size, batch.indices, flags);
if (!count) {
return;
}
rk_uint const ncommands = rk_batch_build_commands(count, batch.indices, batch.commands, 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);
}
switch (batch.translation_format) {
case RK_BATCH_TRANSLATION_FORMAT_FLOAT:
switch (batch.orientation_format) {
case RK_BATCH_ORIENTATION_FORMAT_NONE:
rk_batch_convert_params(
count, batch.indices, batch.params, translations, texlevels);
break;
case RK_BATCH_ORIENTATION_FORMAT_FLOAT:
rk_batch_convert_params(
count, batch.indices, batch.params, translations, texlevels, orientations);
break;
case RK_BATCH_ORIENTATION_FORMAT_INT10:
rk_batch_convert_params(
count, batch.indices, batch.params, translations, texlevels, orientations);
break;
}
break;
case RK_BATCH_TRANSLATION_FORMAT_SHORT:
switch (batch.orientation_format) {
case RK_BATCH_ORIENTATION_FORMAT_NONE:
rk_batch_convert_params(
count, batch.indices, batch.params, translations, texlevels);
break;
case RK_BATCH_ORIENTATION_FORMAT_FLOAT:
rk_batch_convert_params(
count, batch.indices, batch.params, translations, texlevels, orientations);
break;
case RK_BATCH_ORIENTATION_FORMAT_INT10:
rk_batch_convert_params(
count, batch.indices, batch.params, translations, texlevels, orientations);
break;
}
break;
}
glBindBuffer(GL_ARRAY_BUFFER, batch.params_buffer);
glBufferData(GL_ARRAY_BUFFER, count * batch.params_size, batch.params, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
if (batch.orientation_format == RK_BATCH_ORIENTATION_FORMAT_NONE) {
rk_vec3 const forward(0.f, 1.f, 0.f);
glVertexAttrib3fv(rk_current_vertices->layout + 1, glm::value_ptr(forward));
}
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) {
rk_DrawElementsInstancedBaseInstance(
GL_TRIANGLES, command->count, GL_UNSIGNED_SHORT,
reinterpret_cast(command->base_index << 1),
command->ninstances, command->base_instance);
}
}
} else {
rk_uint params_offset = 0;
rk_command const * const last_command = batch.commands + ncommands;
for (rk_command const * command = batch.commands; command < last_command; ++command) {
glBindVertexBuffer(RK_PARAMS_BINDING, batch.params_buffer, params_offset, batch.params_size);
params_offset += command->ninstances * batch.params_size;
glDrawElementsInstanced(
GL_TRIANGLES, command->count, GL_UNSIGNED_SHORT,
reinterpret_cast(command->base_index << 1),
command->ninstances);
}
}
}
void rk_unselect_vertices(
rk_vertices_t _vertices) {
rk_current_vertices = nullptr;
glBindVertexArray(0);
}
void rk_unselect_texture(
rk_texture_t _texture) {
rk_texture const * const texture = reinterpret_cast(_texture);
if (texture) {
glActiveTexture(GL_TEXTURE0 + texture->slot);
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() {
rk_swap_buffers();
}
void rk_destroy_batch(
rk_batch_t _batch) {
rk_batch * const batch = reinterpret_cast(_batch);
if (batch) {
delete[] batch->indices;
delete[] batch->params;
delete[] batch->commands;
glDeleteBuffers(1, &batch->params_buffer);
if (rk_MultiDrawElementsIndirect) {
glDeleteBuffers(1, &batch->commands_buffer);
}
delete batch;
}
}
void rk_destroy_triangles(
rk_triangles_t _triangles) {
rk_triangles * const triangles = reinterpret_cast(_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(_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(_texture);
if (texture) {
glDeleteTextures(1, &texture->texture);
delete texture;
}
}
void rk_destroy_shader(
rk_shader_t _shader) {
rk_shader * const shader = reinterpret_cast(_shader);
if (shader) {
glDeleteShader(shader->vertex);
glDeleteShader(shader->fragment);
glDeleteProgram(shader->program);
delete shader;
}
}
void rk_terminate() {
rk_destroy_context();
}