$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
gl_common.h
Go to the documentation of this file.
1 
9 #ifndef GL_COMMON_H
10 #define GL_COMMON_H
11 
12 #ifndef GL_COMMON_NO_GLAD
13 #include "glad/glad.h"
14 #else
15 #ifndef GLFW_INCLUDE_NONE
16 #define GLFW_INCLUDE_NONE
17 #endif
18 /* Minimal GL types fallback */
19 typedef unsigned int GLuint;
20 typedef int GLint;
21 typedef unsigned int GLenum;
22 typedef int GLsizei;
23 typedef float GLfloat;
24 typedef unsigned char GLboolean;
25 typedef long GLsizeiptr;
26 typedef long GLintptr;
27 typedef void* GLsync;
28 typedef unsigned long long GLuint64;
29 typedef char GLchar;
30 typedef unsigned int GLbitfield;
31 
32 #define GL_TRUE 1
33 #define GL_FALSE 0
34 #define GL_TEXTURE_2D 0x0DE1
35 #define GL_TEXTURE_MIN_FILTER 0x2801
36 #define GL_TEXTURE_MAG_FILTER 0x2800
37 #define GL_TEXTURE_WRAP_S 0x2802
38 #define GL_TEXTURE_WRAP_T 0x2803
39 #define GL_TEXTURE_WIDTH 0x1000
40 #define GL_TEXTURE_HEIGHT 0x1001
41 #define GL_TEXTURE_INTERNAL_FORMAT 0x1003
42 #define GL_LINEAR 0x2601
43 #define GL_LINEAR_MIPMAP_LINEAR 0x2703
44 #define GL_CLAMP_TO_EDGE 0x812F
45 #define GL_REPEAT 0x2901
46 #define GL_RGBA32F 0x8814
47 #define GL_RGBA16F 0x881A
48 #define GL_RGBA 0x1908
49 #define GL_FLOAT 0x1406
50 #define GL_HALF_FLOAT 0x140B
51 #define GL_UNSIGNED_BYTE 0x1401
52 #define GL_UNSIGNED_INT 0x1405
53 #define GL_PIXEL_UNPACK_BUFFER 0x88EC
54 #define GL_STREAM_DRAW 0x88E0
55 #define GL_WRITE_ONLY 0x88B9
56 #define GL_UNPACK_ALIGNMENT 0x0CF5
57 #define GL_NO_ERROR 0
58 #define GL_DEBUG_SOURCE_APPLICATION 0x824A
59 
60 #define GL_MAP_UNSYNCHRONIZED_BIT 0x0020
61 #define GL_MAP_WRITE_BIT 0x0010
62 #define GL_MAP_INVALIDATE_BUFFER_BIT 0x0008
63 #define GL_TIMESTAMP 0x8E28
64 #define GL_QUERY_RESULT 0x8866
65 #define GL_QUERY_RESULT_AVAILABLE 0x8867
66 #define GL_TIME_ELAPSED 0x88BF
67 
68 /* Prototype declarations for mocks */
69 GLenum glGetError(void);
70 void glGenTextures(GLsizei n, GLuint* ids);
71 void glBindTexture(GLenum target, GLuint id);
72 void glPixelStorei(GLenum pname, GLint param);
73 void glTexStorage2D(GLenum target, GLsizei levels, GLenum internalformat,
74  GLsizei width, GLsizei height);
75 void glTexImage2D(GLenum t, GLint l, GLint i, GLsizei w, GLsizei h, GLint b,
76  GLenum f, GLenum ty, const void* p);
77 void glTexSubImage2D(GLenum t, GLint l, GLint x, GLint y, GLsizei w, GLsizei h,
78  GLenum f, GLenum ty, const void* p);
79 void glTexParameteri(GLenum target, GLenum pname, GLint param);
80 void glGenerateMipmap(GLenum target);
81 void glDeleteTextures(GLsizei n, const GLuint* ids);
82 void glActiveTexture(GLenum texture);
83 void glGenBuffers(GLsizei n, GLuint* buffers);
84 void glBindBuffer(GLenum target, GLuint buffer);
85 void glBufferData(GLenum target, GLsizeiptr size, const void* data,
86  GLenum usage);
87 void glDeleteBuffers(GLsizei n, const GLuint* buffers);
88 void* glMapBuffer(GLenum target, GLenum access);
89 void* glMapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
90  GLbitfield access);
91 GLboolean glUnmapBuffer(GLenum target);
92 void glGetTexLevelParameteriv(GLenum target, GLint level, GLenum pname,
93  GLint* params);
94 void glGenQueries(GLsizei n, GLuint* ids);
95 void glDeleteQueries(GLsizei n, const GLuint* ids);
96 void glQueryCounter(GLuint id, GLenum target);
97 void glGetQueryObjectiv(GLuint id, GLenum pname, GLint* params);
98 void glGetQueryObjectui64v(GLuint id, GLenum pname, GLuint64* params);
99 void glFlush(void);
100 void glFinish(void);
101 void glUseProgram(GLuint program);
102 void glPopDebugGroup(void);
103 void glPushDebugGroup(GLenum source, GLuint id, GLsizei length,
104  const GLchar* message);
105 #endif
106 
107 #ifndef GL_COMMON_NO_GLFW
108 #include <GLFW/glfw3.h>
109 #endif
110 #include <stddef.h>
111 #include <stdint.h>
112 
115 
117 enum { INSTANCE_ATTR_START = 2 };
118 
121 enum { SYNC_ATTR_START = 8 };
122 
126 
131 enum { SIMD_ALIGNMENT = 64 };
132 
140 enum { GL_UBO_ALIGNMENT = 32 };
141 
146 #define GL_UBO_ALIGNED __attribute__((aligned(GL_UBO_ALIGNMENT)))
147 
153 #define GL_ASSERT_UBO_ALIGNMENT(type) \
154  _Static_assert(_Alignof(type) >= GL_UBO_ALIGNMENT, \
155  #type " must be >= 32-byte aligned for AVX (cglm)")
156 
161 #define GL_DEBUG_PUSH(name) \
162  glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, name)
163 
165 #define GL_DEBUG_POP() glPopDebugGroup()
166 
170 static inline void cleanup_gl_debug_group(const char** dummy)
171 {
172  (void)dummy;
173  glPopDebugGroup();
174 }
175 
180 #define GL_SCOPE_DEBUG_GROUP(name) \
181  glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, name); \
182  const char* _gl_dbg_##__LINE__ \
183  __attribute__((cleanup(cleanup_gl_debug_group))) \
184  __attribute__((unused)) = name
185 
189 static inline void cleanup_gl_use_program(const GLuint* dummy)
190 {
191  (void)dummy;
192  glUseProgram(0);
193 }
194 
200 #define GL_SCOPE_USE_PROGRAM(prog) \
201  glUseProgram(prog); \
202  GLuint _gl_prog_##__LINE__ \
203  __attribute__((cleanup(cleanup_gl_use_program))) \
204  __attribute__((unused)) = prog
205 
210 static inline void cleanup_gl_texture(GLuint* tex)
211 {
212  if (tex && *tex) {
213  glDeleteTextures(1, tex);
214  }
215 }
216 
218 #define CLEANUP_TEXTURE __attribute__((cleanup(cleanup_gl_texture)))
219 
226 #define GL_SAFE_DELETE_TEXTURE(tex) \
227  do { \
228  if ((tex) != 0) { \
229  glDeleteTextures(1, &(tex)); \
230  (tex) = 0; \
231  } \
232  } while (0)
233 
234 #define GL_SAFE_DELETE_BUFFER(buf) \
235  do { \
236  if ((buf) != 0) { \
237  glDeleteBuffers(1, &(buf)); \
238  (buf) = 0; \
239  } \
240  } while (0)
241 
242 #define GL_SAFE_DELETE_BUFFERS(count, ids) \
243  do { \
244  if ((ids) != NULL) { \
245  glDeleteBuffers(count, ids); \
246  for (int i = 0; i < (count); i++) { \
247  (ids)[i] = 0; \
248  } \
249  } \
250  } while (0)
251 
252 #define GL_SAFE_DELETE_VAO(vao) \
253  do { \
254  if ((vao) != 0) { \
255  glDeleteVertexArrays(1, &(vao)); \
256  (vao) = 0; \
257  } \
258  } while (0)
259 
260 #define GL_SAFE_DELETE_VAOS(count, vaos) \
261  do { \
262  if ((vaos) != NULL) { \
263  glDeleteVertexArrays(count, vaos); \
264  for (int i = 0; i < (count); i++) { \
265  (vaos)[i] = 0; \
266  } \
267  } \
268  } while (0)
269 
270 #define GL_SAFE_DELETE_PROGRAM(prog) \
271  do { \
272  if ((prog) != 0) { \
273  glDeleteProgram(prog); \
274  (prog) = 0; \
275  } \
276  } while (0)
277 
278 #define GL_SAFE_DELETE_FRAMEBUFFER(fbo) \
279  do { \
280  if ((fbo) != 0) { \
281  glDeleteFramebuffers(1, &(fbo)); \
282  (fbo) = 0; \
283  } \
284  } while (0)
285 
286 #endif /* GL_COMMON_H */
@ SIMD_ALIGNMENT
Definition: gl_common.h:131
@ GL_UBO_ALIGNMENT
Definition: gl_common.h:140
static void cleanup_gl_texture(GLuint *tex)
RAII-style cleanup for OpenGL textures. Deletes the texture if the handle is non-zero.
Definition: gl_common.h:210
@ INSTANCE_ATTR_START
Definition: gl_common.h:117
@ MAX_VERTEX_ATTRIBS_BASELINE
Definition: gl_common.h:114
@ SYNC_ATTR_START
Definition: gl_common.h:121
static void cleanup_gl_debug_group(const char **dummy)
RAII-style cleanup for OpenGL debug groups.
Definition: gl_common.h:170
@ SCREEN_QUAD_VERTEX_COUNT
Definition: gl_common.h:125
static void cleanup_gl_use_program(const GLuint *dummy)
RAII-style cleanup for OpenGL shader program binding.
Definition: gl_common.h:189