$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 void glColorMaski(GLuint index, GLboolean r, GLboolean g, GLboolean b,
106  GLboolean a);
107 #endif
108 
109 #ifndef GL_COMMON_NO_GLFW
110 #include <GLFW/glfw3.h>
111 #endif
112 #include <stddef.h>
113 #include <stdint.h>
114 
117 
119 enum { INSTANCE_ATTR_START = 2 };
120 
124 enum { SYNC_ATTR_START = 9 };
125 
129 
134 enum { SIMD_ALIGNMENT = 64 };
135 
143 enum { GL_UBO_ALIGNMENT = 32 };
144 
149 #define GL_UBO_ALIGNED __attribute__((aligned(GL_UBO_ALIGNMENT)))
150 
156 #define GL_ASSERT_UBO_ALIGNMENT(type) \
157  _Static_assert(_Alignof(type) >= GL_UBO_ALIGNMENT, \
158  #type " must be >= 32-byte aligned for AVX (cglm)")
159 
164 #define GL_DEBUG_PUSH(name) \
165  glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, name)
166 
168 #define GL_DEBUG_POP() glPopDebugGroup()
169 
173 static inline void cleanup_gl_debug_group(const char** dummy)
174 {
175  (void)dummy;
176  glPopDebugGroup();
177 }
178 
183 #define GL_SCOPE_DEBUG_GROUP(name) \
184  glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, name); \
185  const char* _gl_dbg_##__LINE__ \
186  __attribute__((cleanup(cleanup_gl_debug_group))) \
187  __attribute__((unused)) = name
188 
192 static inline void cleanup_gl_use_program(const GLuint* dummy)
193 {
194  (void)dummy;
195  glUseProgram(0);
196 }
197 
203 #define GL_SCOPE_USE_PROGRAM(prog) \
204  glUseProgram(prog); \
205  GLuint _gl_prog_##__LINE__ \
206  __attribute__((cleanup(cleanup_gl_use_program))) \
207  __attribute__((unused)) = prog
208 
213 static inline void cleanup_gl_texture(GLuint* tex)
214 {
215  if (tex && *tex) {
216  glDeleteTextures(1, tex);
217  }
218 }
219 
221 #define CLEANUP_TEXTURE __attribute__((cleanup(cleanup_gl_texture)))
222 
229 #define GL_SAFE_DELETE_TEXTURE(tex) \
230  do { \
231  if ((tex) != 0) { \
232  glDeleteTextures(1, &(tex)); \
233  (tex) = 0; \
234  } \
235  } while (0)
236 
237 #define GL_SAFE_DELETE_BUFFER(buf) \
238  do { \
239  if ((buf) != 0) { \
240  glDeleteBuffers(1, &(buf)); \
241  (buf) = 0; \
242  } \
243  } while (0)
244 
245 #define GL_SAFE_DELETE_BUFFERS(count, ids) \
246  do { \
247  if ((ids) != NULL) { \
248  glDeleteBuffers(count, ids); \
249  for (int i = 0; i < (count); i++) { \
250  (ids)[i] = 0; \
251  } \
252  } \
253  } while (0)
254 
255 #define GL_SAFE_DELETE_VAO(vao) \
256  do { \
257  if ((vao) != 0) { \
258  glDeleteVertexArrays(1, &(vao)); \
259  (vao) = 0; \
260  } \
261  } while (0)
262 
263 #define GL_SAFE_DELETE_VAOS(count, vaos) \
264  do { \
265  if ((vaos) != NULL) { \
266  glDeleteVertexArrays(count, vaos); \
267  for (int i = 0; i < (count); i++) { \
268  (vaos)[i] = 0; \
269  } \
270  } \
271  } while (0)
272 
273 #define GL_SAFE_DELETE_PROGRAM(prog) \
274  do { \
275  if ((prog) != 0) { \
276  glDeleteProgram(prog); \
277  (prog) = 0; \
278  } \
279  } while (0)
280 
281 #define GL_SAFE_DELETE_FRAMEBUFFER(fbo) \
282  do { \
283  if ((fbo) != 0) { \
284  glDeleteFramebuffers(1, &(fbo)); \
285  (fbo) = 0; \
286  } \
287  } while (0)
288 
289 #endif /* GL_COMMON_H */
@ SYNC_ATTR_START
Definition: gl_common.h:124
@ SCREEN_QUAD_VERTEX_COUNT
Definition: gl_common.h:128
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:213
@ GL_UBO_ALIGNMENT
Definition: gl_common.h:143
@ SIMD_ALIGNMENT
Definition: gl_common.h:134
@ MAX_VERTEX_ATTRIBS_BASELINE
Definition: gl_common.h:116
static void cleanup_gl_debug_group(const char **dummy)
RAII-style cleanup for OpenGL debug groups.
Definition: gl_common.h:173
@ INSTANCE_ATTR_START
Definition: gl_common.h:119
static void cleanup_gl_use_program(const GLuint *dummy)
RAII-style cleanup for OpenGL shader program binding.
Definition: gl_common.h:192