nngn
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#ifndef NNGN_GRAPHICS_OPENGL_UTILS_H
2#define NNGN_GRAPHICS_OPENGL_UTILS_H
3
4#include <string_view>
5
6#include "os/platform.h"
7#include "utils/log.h"
8#include "utils/utils.h"
9
10#include "opengl.h"
11
12#ifdef GL_VERSION_4_3
13constexpr GLenum NNGN_GL_BUFFER = GL_BUFFER;
14constexpr GLenum NNGN_GL_PROGRAM = GL_PROGRAM;
15constexpr GLenum NNGN_GL_SHADER = GL_SHADER;
16constexpr GLenum NNGN_GL_VERTEX_ARRAY = GL_VERTEX_ARRAY;
17#else
18constexpr GLenum NNGN_GL_BUFFER = 0;
19constexpr GLenum NNGN_GL_PROGRAM = 0;
20constexpr GLenum NNGN_GL_SHADER = 0;
21constexpr GLenum NNGN_GL_VERTEX_ARRAY = 0;
22#endif
23
24#define LOG_RESULT(f, ...) (f(__VA_ARGS__), nngn::gl_check_result(#f))
25#define CHECK_RESULT(f, ...) \
26 do { f(__VA_ARGS__); if(!nngn::gl_check_result(#f)) return false; } while(0)
27
28namespace nngn {
29
30const char *gl_strerror(GLenum error);
31const char *gl_enum_str(GLenum e);
32bool gl_check_result(const char *func_name);
33bool gl_set_obj_name(GLenum type, GLuint obj, std::string_view name);
34
38 GLDebugGroup([[maybe_unused]]std::string_view s) {
39#ifdef GL_EXT_debug_marker
40 LOG_RESULT(glPushGroupMarkerEXT,
41 static_cast<GLsizei>(s.size()), s.data());
42#endif
43 }
44 ~GLDebugGroup(void);
45};
46
48#ifdef GL_EXT_debug_marker
49 glPopGroupMarkerEXT();
50 gl_check_result("glPopGroupMarkerEXT");
51#endif
52}
53
54inline bool gl_check_result([[maybe_unused]] const char *func_name) {
55 if constexpr(Platform::emscripten)
56 /*
57 * XXX WebGLRenderingContext.getError has recently (?) become extremely
58 * expensive for whatever reason (~200 -> 40 FPS), even though this has
59 * an imperceptible effect on regular builds.
60 */
61 return true;
62 const auto err = glGetError();
63 if(err == GL_NO_ERROR)
64 return true;
65 nngn::Log::l() << func_name << ": " << gl_strerror(err) << std::endl;
66 return false;
67}
68
69inline bool gl_set_obj_name(
70 [[maybe_unused]] GLenum type, [[maybe_unused]] GLuint obj,
71 [[maybe_unused]] std::string_view name
72) {
73#ifdef GL_VERSION_4_3
74 CHECK_RESULT(glObjectLabel,
75 type, obj, static_cast<GLsizei>(name.size()), name.data());
76#endif
77 return true;
78}
79
80}
81
82#endif
name
Definition cathedral.lua:11
static std::ostream & l()
Definition log.cpp:56
Definition audio.cpp:7
bool gl_set_obj_name(GLenum type, GLuint obj, std::string_view name)
Definition utils.h:69
const char * gl_enum_str(GLenum e)
const char * gl_strerror(GLenum error)
Definition utils.cpp:8
bool gl_check_result(const char *func_name)
Definition utils.h:54
Definition debug.h:13
#define LOG_RESULT(f,...)
Definition opencl.cpp:264
#define CHECK_RESULT(f,...)
Definition opencl.cpp:265
constexpr GLenum NNGN_GL_BUFFER
Definition utils.h:18
constexpr GLenum NNGN_GL_VERTEX_ARRAY
Definition utils.h:21
constexpr GLenum NNGN_GL_SHADER
Definition utils.h:20
constexpr GLenum NNGN_GL_PROGRAM
Definition utils.h:19
#define NNGN_MOVE_ONLY(x)
Definition utils.h:39
local error
Definition strict.lua:2
RAII-based debug group manager.
Definition utils.h:36
~GLDebugGroup(void)
Definition utils.h:47
static constexpr bool emscripten
Definition platform.h:38
local e
Definition math.lua:4
std::chrono::seconds s
Definition timing.cpp:6