Skip to content

Commit

Permalink
Implement constant buffers for OpenGL
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Oct 4, 2023
1 parent ed3194a commit 71850f7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <kinc/backend/graphics4/vertexbuffer.h>

#include <kinc/graphics4/constantbuffer.h>
#include <kinc/graphics4/indexbuffer.h>
#include <kinc/graphics4/pipeline.h>
#include <kinc/graphics4/rendertarget.h>
Expand Down Expand Up @@ -1244,3 +1245,9 @@ bool kinc_g4_supports_non_pow2_textures() {
bool kinc_g4_render_targets_inverted_y(void) {
return true;
}

#ifdef KINC_KONG
void kinc_g4_set_constant_buffer(uint32_t id, struct kinc_g4_constant_buffer *buffer) {
glBindBufferBase(GL_UNIFORM_BUFFER, id, buffer->impl.buffer);
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifdef KINC_KONG

#include <kinc/graphics4/constantbuffer.h>

void kinc_g4_constant_buffer_init(kinc_g4_constant_buffer *buffer, size_t size) {
buffer->impl.size = size;
buffer->impl.last_start = 0;
buffer->impl.last_size = size;

buffer->impl.data = malloc(size);

buffer->impl.buffer = 0;
glGenBuffers(1, &buffer->impl.buffer);
glBindBuffer(GL_UNIFORM_BUFFER, buffer->impl.buffer);
glBufferData(GL_UNIFORM_BUFFER, size, NULL, GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}

void kinc_g4_constant_buffer_destroy(kinc_g4_constant_buffer *buffer) {
free(buffer->impl.data);
buffer->impl.data = NULL;

glDeleteBuffers(1, &buffer->impl.buffer);
buffer->impl.buffer = 0;
}

uint8_t *kinc_g4_constant_buffer_lock_all(kinc_g4_constant_buffer *buffer) {
return kinc_g4_constant_buffer_lock(buffer, 0, kinc_g4_constant_buffer_size(buffer));
}

uint8_t *kinc_g4_constant_buffer_lock(kinc_g4_constant_buffer *buffer, size_t start, size_t size) {
buffer->impl.last_start = start;
buffer->impl.last_size = size;

uint8_t *data = (uint8_t *)buffer->impl.data;
return &data[start];
}

void kinc_g4_constant_buffer_unlock_all(kinc_g4_constant_buffer *buffer) {
kinc_g4_constant_buffer_unlock(buffer, buffer->impl.last_size);
}

void kinc_g4_constant_buffer_unlock(kinc_g4_constant_buffer *buffer, size_t count) {
glBindBuffer(GL_UNIFORM_BUFFER, buffer->impl.buffer);
glBufferSubData(GL_UNIFORM_BUFFER, buffer->impl.last_start, buffer->impl.last_size, buffer->impl.data);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}

size_t kinc_g4_constant_buffer_size(kinc_g4_constant_buffer *buffer) {
return buffer->impl.size;
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#ifdef KINC_KONG

typedef struct kinc_g4_constant_buffer_impl {
unsigned buffer;
void *data;
size_t size;
size_t last_start;
size_t last_size;
} kinc_g4_constant_buffer_impl;

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ static int kinc_internal_opengl_max_vertex_attribute_arrays = 0;
#include "OpenGLWindow.c.h"
#include "ShaderStorageBufferImpl.c.h"
#include "VrInterface.c.h"
#include "constantbuffer.c.h"
#include "indexbuffer.c.h"
#include "pipeline.c.h"
#include "rendertarget.c.h"
Expand Down

0 comments on commit 71850f7

Please sign in to comment.