Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
Loading...
Searching...
No Matches
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" // IWYU pragma: export
14#else
15#ifndef GLFW_INCLUDE_NONE
16#define GLFW_INCLUDE_NONE
17#endif
18/* Minimal GL types fallback */
19typedef unsigned int GLuint;
20typedef int GLint;
21typedef unsigned int GLenum;
22typedef int GLsizei;
23typedef float GLfloat;
24typedef unsigned char GLboolean;
25typedef long GLsizeiptr;
26typedef long GLintptr;
27typedef void* GLsync;
28typedef unsigned long long GLuint64;
29typedef char GLchar;
30typedef 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_RGB32F 0x8815
49#define GL_RGB16F 0x881B
50#define GL_RGB 0x1907
51#define GL_RGBA 0x1908
52#define GL_FLOAT 0x1406
53#define GL_HALF_FLOAT 0x140B
54#define GL_UNSIGNED_BYTE 0x1401
55#define GL_UNSIGNED_INT 0x1405
56#define GL_PIXEL_UNPACK_BUFFER 0x88EC
57#define GL_STREAM_DRAW 0x88E0
58#define GL_WRITE_ONLY 0x88B9
59#define GL_UNPACK_ALIGNMENT 0x0CF5
60#define GL_NO_ERROR 0
61#define GL_DEBUG_SOURCE_APPLICATION 0x824A
62
63#define GL_MAP_UNSYNCHRONIZED_BIT 0x0020
64#define GL_MAP_WRITE_BIT 0x0010
65#define GL_MAP_INVALIDATE_BUFFER_BIT 0x0008
66#define GL_TIMESTAMP 0x8E28
67#define GL_QUERY_RESULT 0x8866
68#define GL_QUERY_RESULT_AVAILABLE 0x8867
69#define GL_TIME_ELAPSED 0x88BF
70
71/* Prototype declarations for mocks */
72GLenum glGetError(void);
73void glGenTextures(GLsizei n, GLuint* ids);
74void glBindTexture(GLenum target, GLuint id);
75void glPixelStorei(GLenum pname, GLint param);
76void glTexStorage2D(GLenum target, GLsizei levels, GLenum internalformat,
77 GLsizei width, GLsizei height);
78void glTexImage2D(GLenum t, GLint l, GLint i, GLsizei w, GLsizei h, GLint b,
79 GLenum f, GLenum ty, const void* p);
80void glTexSubImage2D(GLenum t, GLint l, GLint x, GLint y, GLsizei w, GLsizei h,
81 GLenum f, GLenum ty, const void* p);
82/* Prototype pour garantir la compilation de texture.c dans les tests unitaires
83 */
84#undef glCompressedTexSubImage2D
85void glCompressedTexSubImage2D(unsigned int target, int level, int xoffset,
86 int yoffset, int width, int height,
87 unsigned int format, int imageSize,
88 const void* data);
89void glTexParameteri(GLenum target, GLenum pname, GLint param);
90void glGenerateMipmap(GLenum target);
91void glDeleteTextures(GLsizei n, const GLuint* ids);
92void glActiveTexture(GLenum texture);
93void glGenBuffers(GLsizei n, GLuint* buffers);
94void glBindBuffer(GLenum target, GLuint buffer);
95void glBufferData(GLenum target, GLsizeiptr size, const void* data,
96 GLenum usage);
97void glDeleteBuffers(GLsizei n, const GLuint* buffers);
98void* glMapBuffer(GLenum target, GLenum access);
99void* glMapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
100 GLbitfield access);
101GLboolean glUnmapBuffer(GLenum target);
102void glGetTexLevelParameteriv(GLenum target, GLint level, GLenum pname,
103 GLint* params);
104void glGenQueries(GLsizei n, GLuint* ids);
105void glDeleteQueries(GLsizei n, const GLuint* ids);
106void glQueryCounter(GLuint id, GLenum target);
107void glGetQueryObjectiv(GLuint id, GLenum pname, GLint* params);
108void glGetQueryObjectui64v(GLuint id, GLenum pname, GLuint64* params);
109void glFlush(void);
110void glFinish(void);
111void glUseProgram(GLuint program);
112void glPopDebugGroup(void);
113void glPushDebugGroup(GLenum source, GLuint id, GLsizei length,
114 const GLchar* message);
115void glColorMaski(GLuint index, GLboolean r, GLboolean g, GLboolean b,
116 GLboolean a);
117void glVertexAttribFormat(GLuint attribindex, GLint size, GLenum type,
118 GLboolean normalized, GLuint relativeoffset);
119void glVertexAttribBinding(GLuint attribindex, GLuint bindingindex);
120void glBindVertexBuffer(GLuint bindingindex, GLuint buffer, GLintptr offset,
121 GLsizei stride);
122void glVertexBindingDivisor(GLuint bindingindex, GLuint divisor);
123#endif
124
125#ifndef GL_COMMON_NO_GLFW
126#include <GLFW/glfw3.h> // IWYU pragma: export
127#endif
128#include <stddef.h> // IWYU pragma: export
129#include <stdint.h> // IWYU pragma: export
130
133
136
140enum { SYNC_ATTR_START = 9 };
141
145
150enum { SIMD_ALIGNMENT = 64 };
151
159enum { GL_UBO_ALIGNMENT = 32 };
160
165#define GL_UBO_ALIGNED __attribute__((aligned(GL_UBO_ALIGNMENT)))
166
172#define GL_ASSERT_UBO_ALIGNMENT(type) \
173 _Static_assert(_Alignof(type) >= GL_UBO_ALIGNMENT, \
174 #type " must be >= 32-byte aligned for AVX (cglm)")
175
180#define GL_DEBUG_PUSH(name) \
181 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, name)
182
184#define GL_DEBUG_POP() glPopDebugGroup()
185
189static inline void cleanup_gl_debug_group(const char** dummy)
190{
191 (void)dummy;
192 glPopDebugGroup();
193}
194
199#define GL_SCOPE_DEBUG_GROUP(name) \
200 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, name); \
201 const char* _gl_dbg_##__LINE__ \
202 __attribute__((cleanup(cleanup_gl_debug_group))) \
203 __attribute__((unused)) = name
204
208static inline void cleanup_gl_use_program(const GLuint* dummy)
209{
210 (void)dummy;
211 glUseProgram(0);
212}
213
219#define GL_SCOPE_USE_PROGRAM(prog) \
220 glUseProgram(prog); \
221 GLuint _gl_prog_##__LINE__ \
222 __attribute__((cleanup(cleanup_gl_use_program))) \
223 __attribute__((unused)) = prog
224
229static inline void cleanup_gl_texture(GLuint* tex)
230{
231 if (tex && *tex) {
232 glDeleteTextures(1, tex);
233 }
234}
235
237#define CLEANUP_TEXTURE __attribute__((cleanup(cleanup_gl_texture)))
238
245#define GL_SAFE_DELETE_TEXTURE(tex) \
246 do { \
247 if ((tex) != 0) { \
248 glDeleteTextures(1, &(tex)); \
249 (tex) = 0; \
250 } \
251 } while (0)
252
253#define GL_SAFE_DELETE_BUFFER(buf) \
254 do { \
255 if ((buf) != 0) { \
256 glDeleteBuffers(1, &(buf)); \
257 (buf) = 0; \
258 } \
259 } while (0)
260
261#define GL_SAFE_DELETE_BUFFERS(count, ids) \
262 do { \
263 if ((ids) != NULL) { \
264 glDeleteBuffers(count, ids); \
265 for (int i = 0; i < (count); i++) { \
266 (ids)[i] = 0; \
267 } \
268 } \
269 } while (0)
270
271#define GL_SAFE_DELETE_VAO(vao) \
272 do { \
273 if ((vao) != 0) { \
274 glDeleteVertexArrays(1, &(vao)); \
275 (vao) = 0; \
276 } \
277 } while (0)
278
279#define GL_SAFE_DELETE_VAOS(count, vaos) \
280 do { \
281 if ((vaos) != NULL) { \
282 glDeleteVertexArrays(count, vaos); \
283 for (int i = 0; i < (count); i++) { \
284 (vaos)[i] = 0; \
285 } \
286 } \
287 } while (0)
288
289#define GL_SAFE_DELETE_PROGRAM(prog) \
290 do { \
291 if ((prog) != 0) { \
292 glDeleteProgram(prog); \
293 (prog) = 0; \
294 } \
295 } while (0)
296
297#define GL_SAFE_DELETE_FRAMEBUFFER(fbo) \
298 do { \
299 if ((fbo) != 0) { \
300 glDeleteFramebuffers(1, &(fbo)); \
301 (fbo) = 0; \
302 } \
303 } while (0)
304
305#endif /* GL_COMMON_H */
@ INSTANCE_ATTR_START
Definition gl_common.h:135
@ SYNC_ATTR_START
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:229
@ GL_UBO_ALIGNMENT
Definition gl_common.h:159
@ SIMD_ALIGNMENT
Definition gl_common.h:150
@ SCREEN_QUAD_VERTEX_COUNT
Definition gl_common.h:144
static void cleanup_gl_debug_group(const char **dummy)
RAII-style cleanup for OpenGL debug groups.
Definition gl_common.h:189
@ MAX_VERTEX_ATTRIBS_BASELINE
Definition gl_common.h:132
static void cleanup_gl_use_program(const GLuint *dummy)
RAII-style cleanup for OpenGL shader program binding.
Definition gl_common.h:208