Skip to content

Commit

Permalink
gfx: be more careful with cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Oct 24, 2024
1 parent 6a3d249 commit 85327e6
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 20 deletions.
30 changes: 20 additions & 10 deletions src/libtrx/gfx/gl/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,70 @@

void GFX_GL_Buffer_Init(GFX_GL_BUFFER *buf, GLenum target)
{
assert(buf);
assert(buf != NULL);
buf->target = target;
glGenBuffers(1, &buf->id);
GFX_GL_CheckError();
buf->initialized = true;
}

void GFX_GL_Buffer_Close(GFX_GL_BUFFER *buf)
{
assert(buf);
glDeleteBuffers(1, &buf->id);
GFX_GL_CheckError();
assert(buf != NULL);
if (buf->initialized) {
glDeleteBuffers(1, &buf->id);
GFX_GL_CheckError();
}
buf->initialized = false;
}

void GFX_GL_Buffer_Bind(GFX_GL_BUFFER *buf)
{
assert(buf);
assert(buf != NULL);
assert(buf->initialized);
glBindBuffer(buf->target, buf->id);
GFX_GL_CheckError();
}

void GFX_GL_Buffer_Data(
GFX_GL_BUFFER *buf, GLsizei size, const void *data, GLenum usage)
{
assert(buf);
assert(buf != NULL);
assert(buf->initialized);
glBufferData(buf->target, size, data, usage);
GFX_GL_CheckError();
}

void GFX_GL_Buffer_SubData(
GFX_GL_BUFFER *buf, GLsizei offset, GLsizei size, const void *data)
{
assert(buf);
assert(buf != NULL);
assert(buf->initialized);
glBufferSubData(buf->target, offset, size, data);
GFX_GL_CheckError();
}

void *GFX_GL_Buffer_Map(GFX_GL_BUFFER *buf, GLenum access)
{
assert(buf);
assert(buf != NULL);
assert(buf->initialized);
void *ret = glMapBuffer(buf->target, access);
GFX_GL_CheckError();
return ret;
}

void GFX_GL_Buffer_Unmap(GFX_GL_BUFFER *buf)
{
assert(buf);
assert(buf != NULL);
assert(buf->initialized);
glUnmapBuffer(buf->target);
GFX_GL_CheckError();
}

GLint GFX_GL_Buffer_Parameter(GFX_GL_BUFFER *buf, GLenum pname)
{
assert(buf);
assert(buf != NULL);
assert(buf->initialized);
GLint params = 0;
glGetBufferParameteriv(buf->target, pname, &params);
GFX_GL_CheckError();
Expand Down
14 changes: 14 additions & 0 deletions src/libtrx/gfx/gl/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bool GFX_GL_Program_Init(GFX_GL_PROGRAM *program)

void GFX_GL_Program_Close(GFX_GL_PROGRAM *program)
{
assert(program != NULL);
if (program->id) {
glDeleteProgram(program->id);
GFX_GL_CheckError();
Expand All @@ -33,13 +34,16 @@ void GFX_GL_Program_Close(GFX_GL_PROGRAM *program)

void GFX_GL_Program_Bind(GFX_GL_PROGRAM *program)
{
assert(program != NULL);
glUseProgram(program->id);
GFX_GL_CheckError();
}

char *GFX_GL_Program_PreprocessShader(
const char *content, GLenum type, GFX_GL_BACKEND backend)
{
assert(content != NULL);

const char *version_ogl21 =
"#version 120\n"
"#extension GL_ARB_explicit_attrib_location: enable\n"
Expand Down Expand Up @@ -85,6 +89,9 @@ char *GFX_GL_Program_PreprocessShader(
void GFX_GL_Program_AttachShader(
GFX_GL_PROGRAM *program, GLenum type, const char *path)
{
assert(program != NULL);
assert(path != NULL);

GLuint shader_id = glCreateShader(type);
GFX_GL_CheckError();
if (!shader_id) {
Expand Down Expand Up @@ -137,6 +144,7 @@ void GFX_GL_Program_AttachShader(

void GFX_GL_Program_Link(GFX_GL_PROGRAM *program)
{
assert(program != NULL);
glLinkProgram(program->id);
GFX_GL_CheckError();

Expand All @@ -160,12 +168,14 @@ void GFX_GL_Program_Link(GFX_GL_PROGRAM *program)

void GFX_GL_Program_FragmentData(GFX_GL_PROGRAM *program, const char *name)
{
assert(program != NULL);
glBindFragDataLocation(program->id, 0, name);
GFX_GL_CheckError();
}

GLint GFX_GL_Program_UniformLocation(GFX_GL_PROGRAM *program, const char *name)
{
assert(program != NULL);
GLint location = glGetUniformLocation(program->id, name);
GFX_GL_CheckError();
if (location == -1) {
Expand All @@ -177,6 +187,7 @@ GLint GFX_GL_Program_UniformLocation(GFX_GL_PROGRAM *program, const char *name)
void GFX_GL_Program_Uniform3f(
GFX_GL_PROGRAM *program, GLint loc, GLfloat v0, GLfloat v1, GLfloat v2)
{
assert(program != NULL);
glUniform3f(loc, v0, v1, v2);
GFX_GL_CheckError();
}
Expand All @@ -185,12 +196,14 @@ void GFX_GL_Program_Uniform4f(
GFX_GL_PROGRAM *program, GLint loc, GLfloat v0, GLfloat v1, GLfloat v2,
GLfloat v3)
{
assert(program != NULL);
glUniform4f(loc, v0, v1, v2, v3);
GFX_GL_CheckError();
}

void GFX_GL_Program_Uniform1i(GFX_GL_PROGRAM *program, GLint loc, GLint v0)
{
assert(program != NULL);
glUniform1i(loc, v0);
GFX_GL_CheckError();
}
Expand All @@ -199,6 +212,7 @@ void GFX_GL_Program_UniformMatrix4fv(
GFX_GL_PROGRAM *program, GLint loc, GLsizei count, GLboolean transpose,
const GLfloat *value)
{
assert(program != NULL);
glUniformMatrix4fv(loc, count, transpose, value);
GFX_GL_CheckError();
}
18 changes: 16 additions & 2 deletions src/libtrx/gfx/gl/sampler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,48 @@

#include "gfx/gl/utils.h"

#include <assert.h>

void GFX_GL_Sampler_Init(GFX_GL_SAMPLER *sampler)
{
assert(sampler != NULL);
glGenSamplers(1, &sampler->id);
GFX_GL_CheckError();
sampler->initialized = true;
}

void GFX_GL_Sampler_Close(GFX_GL_SAMPLER *sampler)
{
glDeleteSamplers(1, &sampler->id);
GFX_GL_CheckError();
assert(sampler != NULL);
if (sampler->initialized) {
glDeleteSamplers(1, &sampler->id);
GFX_GL_CheckError();
}
sampler->initialized = false;
}

void GFX_GL_Sampler_Bind(GFX_GL_SAMPLER *sampler, GLuint unit)
{
assert(sampler != NULL);
assert(sampler->initialized);
glBindSampler(unit, sampler->id);
GFX_GL_CheckError();
}

void GFX_GL_Sampler_Parameteri(
GFX_GL_SAMPLER *sampler, GLenum pname, GLint param)
{
assert(sampler != NULL);
assert(sampler->initialized);
glSamplerParameteri(sampler->id, pname, param);
GFX_GL_CheckError();
}

void GFX_GL_Sampler_Parameterf(
GFX_GL_SAMPLER *sampler, GLenum pname, GLfloat param)
{
assert(sampler != NULL);
assert(sampler->initialized);
glSamplerParameterf(sampler->id, pname, param);
GFX_GL_CheckError();
}
11 changes: 9 additions & 2 deletions src/libtrx/gfx/gl/texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@ void GFX_GL_Texture_Init(GFX_GL_TEXTURE *texture, GLenum target)
texture->target = target;
glGenTextures(1, &texture->id);
GFX_GL_CheckError();
texture->initialized = true;
}

void GFX_GL_Texture_Close(GFX_GL_TEXTURE *texture)
{
assert(texture != NULL);
glDeleteTextures(1, &texture->id);
GFX_GL_CheckError();
if (texture->initialized) {
glDeleteTextures(1, &texture->id);
GFX_GL_CheckError();
}
texture->initialized = false;
}

void GFX_GL_Texture_Bind(GFX_GL_TEXTURE *texture)
{
assert(texture != NULL);
assert(texture->initialized);
glBindTexture(texture->target, texture->id);
GFX_GL_CheckError();
}
Expand All @@ -48,6 +53,7 @@ void GFX_GL_Texture_Load(
GLint internal_format, GLint format)
{
assert(texture != NULL);
assert(texture->initialized);

GFX_GL_Texture_Bind(texture);

Expand All @@ -65,6 +71,7 @@ void GFX_GL_Texture_Load(
void GFX_GL_Texture_LoadFromBackBuffer(GFX_GL_TEXTURE *const texture)
{
assert(texture != NULL);
assert(texture->initialized);

GFX_GL_Texture_Bind(texture);

Expand Down
18 changes: 12 additions & 6 deletions src/libtrx/gfx/gl/vertex_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@

void GFX_GL_VertexArray_Init(GFX_GL_VERTEX_ARRAY *array)
{
assert(array);
assert(array != NULL);
glGenVertexArrays(1, &array->id);
GFX_GL_CheckError();
array->initialized = true;
}

void GFX_GL_VertexArray_Close(GFX_GL_VERTEX_ARRAY *array)
{
assert(array);
glDeleteVertexArrays(1, &array->id);
GFX_GL_CheckError();
assert(array != NULL);
if (array->initialized) {
glDeleteVertexArrays(1, &array->id);
GFX_GL_CheckError();
}
array->initialized = false;
}

void GFX_GL_VertexArray_Bind(GFX_GL_VERTEX_ARRAY *array)
{
assert(array);
assert(array != NULL);
assert(array->initialized);
glBindVertexArray(array->id);
GFX_GL_CheckError();
}
Expand All @@ -30,7 +35,8 @@ void GFX_GL_VertexArray_Attribute(
GFX_GL_VERTEX_ARRAY *array, GLuint index, GLint size, GLenum type,
GLboolean normalized, GLsizei stride, GLsizei offset)
{
assert(array);
assert(array != NULL);
assert(array->initialized);
glEnableVertexAttribArray(index);
GFX_GL_CheckError();

Expand Down
3 changes: 3 additions & 0 deletions src/libtrx/include/libtrx/gfx/gl/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#include "gl_core_3_3.h"

#include <stdbool.h>

typedef struct {
bool initialized;
GLuint id;
GLenum target;
} GFX_GL_BUFFER;
Expand Down
1 change: 1 addition & 0 deletions src/libtrx/include/libtrx/gfx/gl/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdbool.h>

typedef struct {
bool initialized;
GLuint id;
} GFX_GL_PROGRAM;

Expand Down
3 changes: 3 additions & 0 deletions src/libtrx/include/libtrx/gfx/gl/sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#include "../gl/gl_core_3_3.h"

#include <stdbool.h>

typedef struct {
bool initialized;
GLuint id;
} GFX_GL_SAMPLER;

Expand Down
3 changes: 3 additions & 0 deletions src/libtrx/include/libtrx/gfx/gl/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#include "../gl/gl_core_3_3.h"

#include <stdbool.h>

typedef struct {
bool initialized;
GLuint id;
GLenum target;
} GFX_GL_TEXTURE;
Expand Down
3 changes: 3 additions & 0 deletions src/libtrx/include/libtrx/gfx/gl/vertex_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#include "../gl/gl_core_3_3.h"

#include <stdbool.h>

typedef struct {
bool initialized;
GLuint id;
} GFX_GL_VERTEX_ARRAY;

Expand Down

0 comments on commit 85327e6

Please sign in to comment.