Skip to content

Instantly share code, notes, and snippets.

@seece seece/gl_loader.cpp Secret
Last active Oct 5, 2019

Embed
What would you like to do?
Hacky OpenGL function pointer queries. Only a subset of the API functions are loaded to save space.
#include "gl_loader.h"
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
#include "Windows.h"
#include "../logger.h"
#include "../util.h"
void *GetAnyGLFuncAddress(const char *name)
{
void *p = (void *)wglGetProcAddress(name);
if (p == 0 ||
(p == (void*)0x1) || (p == (void*)0x2) || (p == (void*)0x3) ||
(p == (void*)-1))
{
HMODULE module = LoadLibraryA("opengl32.dll");
p = (void *)GetProcAddress(module, name);
}
return p;
}
const char* gl_function_names[] = {
"glActiveTexture",
"glDebugMessageCallbackAMD",
"glDebugMessageEnableAMD",
"glDebugMessageInsertAMD",
"glGetDebugMessageLogAMD",
"glDebugMessageCallback",
"glDebugMessageControl",
"glGetActiveUniformBlockName",
"glGetActiveUniformBlockiv",
"glGetActiveUniformsiv",
"glGetActiveUniformName",
"glCompileShader",
"glGetShaderiv",
"glGetShaderInfoLog",
"glCreateProgram",
"glProgramBinary",
"glCreateShader",
"glShaderSource",
"glDeleteShader",
"glGetProgramiv",
"glAttachShader",
"glProgramParameteri",
"glLinkProgram",
"glDeleteProgram",
"glUseProgram",
"glGetUniformLocation",
"glGetUniformBlockIndex",
"glGetProgramBinary",
"glGenVertexArrays",
"glBindVertexArray",
"glEnableVertexAttribArray",
"glGenBuffers",
"glBindBuffer",
"glBufferData",
"glVertexAttribPointer",
"glDeleteBuffers",
"glDeleteVertexArrays",
"glUniform1f",
"glUniform3fv",
"glUniform4fv",
"glUniformMatrix4fv",
"glBindBufferBase",
"glDispatchCompute",
"glMemoryBarrier",
"glMultiDrawArrays",
"glDrawBuffers",
"glGenFramebuffers",
"glFramebufferTexture2D",
"glCheckFramebufferStatus",
"glBindFramebuffer",
"glDeleteFramebuffers",
"glGetQueryObjectiv",
"glGetQueryObjectui64v",
"glTexImage2DMultisample",
"glGenerateMipmap",
"glUniform1i",
"glGetAttribLocation",
"glBufferSubData",
"glUniform4iv"
};
void* gl_function_pointers[sizeof(gl_function_names)/sizeof(const char*)];
#ifdef DEBUG
const char* gl_debug_function_names[] = {
"glGetTextureParameteriv",
"glDetachShader",
"glGenQueries",
"glEndQuery",
"glBeginQuery",
"glGetProgramiv",
"glMapBuffer",
"glUnmapBuffer"
};
void* gl_debug_function_pointers[sizeof(gl_function_names)/sizeof(const char*)];
#endif
int load_gl_functions() {
int failed = 0;
for (int i = 0; i < sizeof(gl_function_names) / sizeof(const char*); i++) {
const char* name = gl_function_names[i];
void* ptr = GetAnyGLFuncAddress(name);
gl_function_pointers[i] = ptr;
if (ptr == NULL) {
#ifdef DEBUG
trace("Failed to load GL func '%s'", name);
#endif
failed++;
}
}
#ifdef DEBUG
for (int i = 0; i < sizeof(gl_debug_function_names) / sizeof(const char*); i++) {
const char* name = gl_debug_function_names[i];
void* ptr = GetAnyGLFuncAddress(name);
gl_debug_function_pointers[i] = ptr;
if (ptr == NULL) {
trace("Failed to load debug GL func '%s'", name);
failed++;
}
}
#endif
return failed;
}
#pragma once
extern void* gl_function_pointers[];
/* Returns the number of functions that failed to load. */
int load_gl_functions();
#define glActiveTexture ((PFNGLACTIVETEXTUREPROC)gl_function_pointers[0])
#define glDebugMessageCallbackAMD ((PFNGLDEBUGMESSAGECALLBACKAMDPROC)gl_function_pointers[1])
#define glDebugMessageEnableAMD ((PFNGLDEBUGMESSAGEENABLEAMDPROC)gl_function_pointers[2])
#define glDebugMessageInsertAMD ((PFNGLDEBUGMESSAGEINSERTAMDPROC)gl_function_pointers[3])
#define glGetDebugMessageLogAMD ((PFNGLGETDEBUGMESSAGELOGAMDPROC)gl_function_pointers[4])
#define glDebugMessageCallback ((PFNGLDEBUGMESSAGECALLBACKPROC)gl_function_pointers[5])
#define glDebugMessageControl ((PFNGLDEBUGMESSAGECONTROLPROC)gl_function_pointers[6])
#define glGetActiveUniformBlockName ((PFNGLGETACTIVEUNIFORMBLOCKNAMEPROC)gl_function_pointers[7])
#define glGetActiveUniformBlockiv ((PFNGLGETACTIVEUNIFORMBLOCKIVPROC)gl_function_pointers[8])
#define glGetActiveUniformsiv ((PFNGLGETACTIVEUNIFORMSIVPROC)gl_function_pointers[9])
#define glGetActiveUniformName ((PFNGLGETACTIVEUNIFORMNAMEPROC)gl_function_pointers[10])
#define glCompileShader ((PFNGLCOMPILESHADERPROC)gl_function_pointers[11])
#define glGetShaderiv ((PFNGLGETSHADERIVPROC)gl_function_pointers[12])
#define glGetShaderInfoLog ((PFNGLGETSHADERINFOLOGPROC)gl_function_pointers[13])
#define glCreateProgram ((PFNGLCREATEPROGRAMPROC)gl_function_pointers[14])
#define glProgramBinary ((PFNGLPROGRAMBINARYPROC)gl_function_pointers[15])
#define glCreateShader ((PFNGLCREATESHADERPROC)gl_function_pointers[16])
#define glShaderSource ((PFNGLSHADERSOURCEPROC)gl_function_pointers[17])
#define glDeleteShader ((PFNGLDELETESHADERPROC)gl_function_pointers[18])
#define glGetProgramiv ((PFNGLGETPROGRAMIVPROC)gl_function_pointers[19])
#define glAttachShader ((PFNGLATTACHSHADERPROC)gl_function_pointers[20])
#define glProgramParameteri ((PFNGLPROGRAMPARAMETERIPROC)gl_function_pointers[21])
#define glLinkProgram ((PFNGLLINKPROGRAMPROC)gl_function_pointers[22])
#define glDeleteProgram ((PFNGLDELETEPROGRAMPROC)gl_function_pointers[23])
#define glUseProgram ((PFNGLUSEPROGRAMPROC)gl_function_pointers[24])
#define glGetUniformLocation ((PFNGLGETUNIFORMLOCATIONPROC)gl_function_pointers[25])
#define glGetUniformBlockIndex ((PFNGLGETUNIFORMBLOCKINDEXPROC)gl_function_pointers[26])
#define glGetProgramBinary ((PFNGLGETPROGRAMBINARYPROC)gl_function_pointers[27])
#define glGenVertexArrays ((PFNGLGENVERTEXARRAYSPROC)gl_function_pointers[28])
#define glBindVertexArray ((PFNGLBINDVERTEXARRAYPROC)gl_function_pointers[29])
#define glEnableVertexAttribArray ((PFNGLENABLEVERTEXATTRIBARRAYPROC)gl_function_pointers[30])
#define glGenBuffers ((PFNGLGENBUFFERSPROC)gl_function_pointers[31])
#define glBindBuffer ((PFNGLBINDBUFFERPROC)gl_function_pointers[32])
#define glBufferData ((PFNGLBUFFERDATAPROC)gl_function_pointers[33])
#define glVertexAttribPointer ((PFNGLVERTEXATTRIBPOINTERPROC)gl_function_pointers[34])
#define glDeleteBuffers ((PFNGLDELETEBUFFERSPROC)gl_function_pointers[35])
#define glDeleteVertexArrays ((PFNGLDELETEVERTEXARRAYSPROC)gl_function_pointers[36])
#define glUniform1f ((PFNGLUNIFORM1FPROC)gl_function_pointers[37])
#define glUniform3fv ((PFNGLUNIFORM3FVPROC)gl_function_pointers[38])
#define glUniform4fv ((PFNGLUNIFORM4FVPROC)gl_function_pointers[39])
#define glUniformMatrix4fv ((PFNGLUNIFORMMATRIX4FVPROC)gl_function_pointers[40])
#define glBindBufferBase ((PFNGLBINDBUFFERBASEPROC)gl_function_pointers[41])
#define glDispatchCompute ((PFNGLDISPATCHCOMPUTEPROC)gl_function_pointers[42])
#define glMemoryBarrier ((PFNGLMEMORYBARRIERPROC)gl_function_pointers[43])
#define glMultiDrawArrays ((PFNGLMULTIDRAWARRAYSPROC)gl_function_pointers[44])
#define glDrawBuffers ((PFNGLDRAWBUFFERSPROC)gl_function_pointers[45])
#define glGenFramebuffers ((PFNGLGENFRAMEBUFFERSPROC)gl_function_pointers[46])
#define glFramebufferTexture2D ((PFNGLFRAMEBUFFERTEXTURE2DPROC)gl_function_pointers[47])
#define glCheckFramebufferStatus ((PFNGLCHECKFRAMEBUFFERSTATUSPROC)gl_function_pointers[48])
#define glBindFramebuffer ((PFNGLBINDFRAMEBUFFERPROC)gl_function_pointers[49])
#define glDeleteFramebuffers ((PFNGLDELETEFRAMEBUFFERSPROC)gl_function_pointers[50])
#define glGetQueryObjectiv ((PFNGLGETQUERYOBJECTIVPROC)gl_function_pointers[51])
#define glGetQueryObjectui64v ((PFNGLGETQUERYOBJECTUI64VPROC)gl_function_pointers[52])
#define glTexImage2DMultisample ((PFNGLTEXIMAGE2DMULTISAMPLEPROC)gl_function_pointers[53])
#define glGenerateMipmap ((PFNGLGENERATEMIPMAPPROC)gl_function_pointers[54])
#define glUniform1i ((PFNGLUNIFORM1IPROC)gl_function_pointers[55])
#define glGetAttribLocation ((PFNGLGETATTRIBLOCATIONPROC)gl_function_pointers[56])
#define glBufferSubData ((PFNGLBUFFERSUBDATAPROC)gl_function_pointers[57])
#define glUniform4iv ((PFNGLUNIFORM4IVPROC)gl_function_pointers[58])
#ifdef DEBUG
extern void* gl_debug_function_pointers[];
#define glGetTextureParameteriv ((PFNGLGETTEXTUREPARAMETERIVPROC)gl_debug_function_pointers[0])
#define glDetachShader ((PFNGLDETACHSHADERPROC)gl_debug_function_pointers[1])
#define glGenQueries ((PFNGLGENQUERIESPROC)gl_debug_function_pointers[2])
#define glEndQuery ((PFNGLENDQUERYPROC)gl_debug_function_pointers[3])
#define glBeginQuery ((PFNGLBEGINQUERYPROC)gl_debug_function_pointers[4])
#define glGetProgramiv ((PFNGLGETPROGRAMIVPROC)gl_debug_function_pointers[5])
#define glMapBuffer ((PFNGLMAPBUFFERPROC)gl_debug_function_pointers[6])
#define glUnmapBuffer ((PFNGLUNMAPBUFFERPROC)gl_debug_function_pointers[7])
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.