$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
gl_common.h File Reference

Common OpenGL definitions, RAII helpers, and utilities. More...

#include "glad/glad.h"
#include <GLFW/glfw3.h>
#include <stddef.h>
#include <stdint.h>
Include dependency graph for gl_common.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define GL_UBO_ALIGNED   __attribute__((aligned(GL_UBO_ALIGNMENT)))
 Attribute to apply on UBO typedef to enforce AVX alignment. Usage: } GL_UBO_ALIGNED MyUBOType;. More...
 
#define GL_ASSERT_UBO_ALIGNMENT(type)
 Compile-time assertion that a UBO struct meets AVX alignment. Place after the typedef to catch misconfigurations at build time. More...
 
#define GL_DEBUG_PUSH(name)    glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, name)
 Pushes a debug group to the OpenGL command stream for debugging tools. More...
 
#define GL_DEBUG_POP()   glPopDebugGroup()
 Pops the current debug group. More...
 
#define GL_SCOPE_DEBUG_GROUP(name)
 Scoped OpenGL debug group. Automatically pops when leaving scope. More...
 
#define GL_SCOPE_USE_PROGRAM(prog)
 Scoped shader program binding. Unbinds (glUseProgram(0)) when leaving scope. More...
 
#define CLEANUP_TEXTURE   __attribute__((cleanup(cleanup_gl_texture)))
 Attribute to automatically delete an OpenGL texture on scope exit. More...
 
#define GL_SAFE_DELETE_TEXTURE(tex)
 Idempotent OpenGL resource deletion macros. These macros check if the resource handle is non-zero, delete the resource, and set the handle to 0 to prevent double-deletion. More...
 
#define GL_SAFE_DELETE_BUFFER(buf)
 
#define GL_SAFE_DELETE_BUFFERS(count, ids)
 
#define GL_SAFE_DELETE_VAO(vao)
 
#define GL_SAFE_DELETE_VAOS(count, vaos)
 
#define GL_SAFE_DELETE_PROGRAM(prog)
 
#define GL_SAFE_DELETE_FRAMEBUFFER(fbo)
 

Enumerations

enum  { MAX_VERTEX_ATTRIBS_BASELINE = 16 }
 Minimum number of vertex attributes guaranteed by OpenGL 3.3+. More...
 
enum  { INSTANCE_ATTR_START = 2 }
 Starting index for instanced vertex attributes. More...
 
enum  { SYNC_ATTR_START = 8 }
 Starting index for synchronization vertex attributes (motion blur). More...
 
enum  { SCREEN_QUAD_VERTEX_COUNT = 6 }
 Number of vertices in a standard screen-filling quad (2 triangles). More...
 
enum  { SIMD_ALIGNMENT = 64 }
 Memory alignment for SIMD/AVX (64-byte is AVX-512 safe and L1 cache line aligned). More...
 
enum  { GL_UBO_ALIGNMENT = 32 }
 Required alignment for UBO structs used with cglm (AVX mat4 ops). More...
 

Functions

static void cleanup_gl_debug_group (const char **dummy)
 RAII-style cleanup for OpenGL debug groups. More...
 
static void cleanup_gl_use_program (const GLuint *dummy)
 RAII-style cleanup for OpenGL shader program binding. More...
 
static void cleanup_gl_texture (GLuint *tex)
 RAII-style cleanup for OpenGL textures. Deletes the texture if the handle is non-zero. More...
 

Detailed Description

Common OpenGL definitions, RAII helpers, and utilities.

This header ensures correct inclusion order for GLAD and GLFW, and provides C-style RAII macros using the __cleanup__ attribute.

Macro Definition Documentation

◆ CLEANUP_TEXTURE

#define CLEANUP_TEXTURE   __attribute__((cleanup(cleanup_gl_texture)))

Attribute to automatically delete an OpenGL texture on scope exit.

◆ GL_ASSERT_UBO_ALIGNMENT

#define GL_ASSERT_UBO_ALIGNMENT (   type)
Value:
_Static_assert(_Alignof(type) >= GL_UBO_ALIGNMENT, \
#type " must be >= 32-byte aligned for AVX (cglm)")
@ GL_UBO_ALIGNMENT
Definition: gl_common.h:140
_Static_assert(_Alignof(PostProcessUBO) >=GL_UBO_ALIGNMENT, "PostProcessUBO" " must be >= 32-byte aligned for AVX (cglm)")

Compile-time assertion that a UBO struct meets AVX alignment. Place after the typedef to catch misconfigurations at build time.

Parameters
typeThe UBO struct type name.

◆ GL_DEBUG_POP

#define GL_DEBUG_POP ( )    glPopDebugGroup()

Pops the current debug group.

◆ GL_DEBUG_PUSH

#define GL_DEBUG_PUSH (   name)     glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, name)

Pushes a debug group to the OpenGL command stream for debugging tools.

Parameters
nameString label for the group.

◆ GL_SAFE_DELETE_BUFFER

#define GL_SAFE_DELETE_BUFFER (   buf)
Value:
do { \
if ((buf) != 0) { \
glDeleteBuffers(1, &(buf)); \
(buf) = 0; \
} \
} while (0)

◆ GL_SAFE_DELETE_BUFFERS

#define GL_SAFE_DELETE_BUFFERS (   count,
  ids 
)
Value:
do { \
if ((ids) != NULL) { \
glDeleteBuffers(count, ids); \
for (int i = 0; i < (count); i++) { \
(ids)[i] = 0; \
} \
} \
} while (0)

◆ GL_SAFE_DELETE_FRAMEBUFFER

#define GL_SAFE_DELETE_FRAMEBUFFER (   fbo)
Value:
do { \
if ((fbo) != 0) { \
glDeleteFramebuffers(1, &(fbo)); \
(fbo) = 0; \
} \
} while (0)

◆ GL_SAFE_DELETE_PROGRAM

#define GL_SAFE_DELETE_PROGRAM (   prog)
Value:
do { \
if ((prog) != 0) { \
glDeleteProgram(prog); \
(prog) = 0; \
} \
} while (0)

◆ GL_SAFE_DELETE_TEXTURE

#define GL_SAFE_DELETE_TEXTURE (   tex)
Value:
do { \
if ((tex) != 0) { \
glDeleteTextures(1, &(tex)); \
(tex) = 0; \
} \
} while (0)

Idempotent OpenGL resource deletion macros. These macros check if the resource handle is non-zero, delete the resource, and set the handle to 0 to prevent double-deletion.

◆ GL_SAFE_DELETE_VAO

#define GL_SAFE_DELETE_VAO (   vao)
Value:
do { \
if ((vao) != 0) { \
glDeleteVertexArrays(1, &(vao)); \
(vao) = 0; \
} \
} while (0)

◆ GL_SAFE_DELETE_VAOS

#define GL_SAFE_DELETE_VAOS (   count,
  vaos 
)
Value:
do { \
if ((vaos) != NULL) { \
glDeleteVertexArrays(count, vaos); \
for (int i = 0; i < (count); i++) { \
(vaos)[i] = 0; \
} \
} \
} while (0)

◆ GL_SCOPE_DEBUG_GROUP

#define GL_SCOPE_DEBUG_GROUP (   name)
Value:
glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, name); \
const char* _gl_dbg_##__LINE__ \
__attribute__((cleanup(cleanup_gl_debug_group))) \
__attribute__((unused)) = name
static void cleanup_gl_debug_group(const char **dummy)
RAII-style cleanup for OpenGL debug groups.
Definition: gl_common.h:170

Scoped OpenGL debug group. Automatically pops when leaving scope.

Parameters
nameGroup label.

◆ GL_SCOPE_USE_PROGRAM

#define GL_SCOPE_USE_PROGRAM (   prog)
Value:
glUseProgram(prog); \
GLuint _gl_prog_##__LINE__ \
__attribute__((cleanup(cleanup_gl_use_program))) \
__attribute__((unused)) = prog
static void cleanup_gl_use_program(const GLuint *dummy)
RAII-style cleanup for OpenGL shader program binding.
Definition: gl_common.h:189

Scoped shader program binding. Unbinds (glUseProgram(0)) when leaving scope.

Parameters
progShader program handle.

◆ GL_UBO_ALIGNED

#define GL_UBO_ALIGNED   __attribute__((aligned(GL_UBO_ALIGNMENT)))

Attribute to apply on UBO typedef to enforce AVX alignment. Usage: } GL_UBO_ALIGNED MyUBOType;.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Starting index for instanced vertex attributes.

Enumerator
INSTANCE_ATTR_START 

◆ anonymous enum

anonymous enum

Starting index for synchronization vertex attributes (motion blur).

Enumerator
SYNC_ATTR_START 

◆ anonymous enum

anonymous enum

Number of vertices in a standard screen-filling quad (2 triangles).

Enumerator
SCREEN_QUAD_VERTEX_COUNT 

◆ anonymous enum

anonymous enum

Memory alignment for SIMD/AVX (64-byte is AVX-512 safe and L1 cache line aligned).

Enumerator
SIMD_ALIGNMENT 

◆ anonymous enum

anonymous enum

Required alignment for UBO structs used with cglm (AVX mat4 ops).

cglm's glm_mat4_copy uses AVX _mm256_store_ps which requires 32-byte alignment. Any UBO struct containing mat4 (float[16]) fields must be tagged with GL_UBO_ALIGNED to guarantee safe stack/heap allocation.

Enumerator
GL_UBO_ALIGNMENT 

◆ anonymous enum

anonymous enum

Minimum number of vertex attributes guaranteed by OpenGL 3.3+.

Enumerator
MAX_VERTEX_ATTRIBS_BASELINE 

Function Documentation

◆ cleanup_gl_debug_group()

static void cleanup_gl_debug_group ( const char **  dummy)
inlinestatic

RAII-style cleanup for OpenGL debug groups.

◆ cleanup_gl_texture()

static void cleanup_gl_texture ( GLuint *  tex)
inlinestatic

RAII-style cleanup for OpenGL textures. Deletes the texture if the handle is non-zero.

◆ cleanup_gl_use_program()

static void cleanup_gl_use_program ( const GLuint *  dummy)
inlinestatic

RAII-style cleanup for OpenGL shader program binding.