| 1 |
#pragma once |
| 2 |
|
| 3 |
#ifndef DOXYGEN_GENERATING_OUTPUT |
| 4 |
#define NCINE_INCLUDE_OPENGL |
| 5 |
#include "../../../CommonHeaders.h" |
| 6 |
#endif |
| 7 |
|
| 8 |
#include "../../../../Main.h" |
| 9 |
|
| 10 |
#include <Containers/StringView.h> |
| 11 |
|
| 12 |
/** |
| 13 |
* @brief Logs any pending OpenGL error |
| 14 |
* |
| 15 |
* Expands to a `glGetError()` check that logs a warning when an error is pending. |
| 16 |
* Compiles to a no-op unless both `DEATH_TRACE` and `DEATH_TRACE_VERBOSE_GL` are defined. |
| 17 |
*/ |
| 18 |
#if defined(DEATH_TRACE) && defined(DEATH_TRACE_VERBOSE_GL) |
| 19 |
# define GL_LOG_ERRORS() \ |
| 20 |
do { \ |
| 21 |
GLenum __err = glGetError(); \ |
| 22 |
if (__err != GL_NO_ERROR) { \ |
| 23 |
LOGW("OpenGL returned error: {}", __err); \ |
| 24 |
} \ |
| 25 |
} while (false) |
| 26 |
#else |
| 27 |
# define GL_LOG_ERRORS() do {} while (false) |
| 28 |
#endif |
| 29 |
|
| 30 |
using namespace Death::Containers; |
| 31 |
|
| 32 |
namespace nCine::RHI |
| 33 |
{ |
| 34 |
class IRhiCapabilities; |
| 35 |
} |
| 36 |
|
| 37 |
// Some OpenGL|ES 3.0/3.1 headers (notably aarch64 Mesa) ship a <GLES2/gl2ext.h> that omits the KHR_debug |
| 38 |
// object-type enums, and their unsuffixed 3.2 core spellings are only available via <GLES3/gl32.h>. Define the |
| 39 |
// _KHR names from their fixed, spec-mandated KHR_debug values so the LabelTypes enum below compiles on such |
| 40 |
// headers; the #ifndef guards make this a no-op wherever the GL/ES headers already provide them. (This avoids |
| 41 |
// pulling <GLES3/gl32.h> into the header, which would define GL_ES_VERSION_3_2 mid-translation-unit and flip |
| 42 |
// GL_ES_VERSION_3_2-gated declarations, e.g. GLBufferObject::TexBuffer.) |
| 43 |
#if defined(RHI_GL_PROFILE_ES) && GL_ES_VERSION_3_0 |
| 44 |
# ifndef GL_BUFFER_KHR |
| 45 |
# define GL_BUFFER_KHR 0x82E0 |
| 46 |
# endif |
| 47 |
# ifndef GL_SHADER_KHR |
| 48 |
# define GL_SHADER_KHR 0x82E1 |
| 49 |
# endif |
| 50 |
# ifndef GL_PROGRAM_KHR |
| 51 |
# define GL_PROGRAM_KHR 0x82E2 |
| 52 |
# endif |
| 53 |
# ifndef GL_VERTEX_ARRAY_KHR |
| 54 |
# define GL_VERTEX_ARRAY_KHR 0x8074 |
| 55 |
# endif |
| 56 |
# ifndef GL_QUERY_KHR |
| 57 |
# define GL_QUERY_KHR 0x82E3 |
| 58 |
# endif |
| 59 |
# ifndef GL_PROGRAM_PIPELINE_KHR |
| 60 |
# define GL_PROGRAM_PIPELINE_KHR 0x82E4 |
| 61 |
# endif |
| 62 |
# ifndef GL_SAMPLER_KHR |
| 63 |
# define GL_SAMPLER_KHR 0x82E6 |
| 64 |
# endif |
| 65 |
#endif |
| 66 |
|
| 67 |
namespace nCine::RHI::GL |
| 68 |
{ |
| 69 |
/** |
| 70 |
@brief Wrapper around OpenGL debug output and object labelling |
| 71 |
|
| 72 |
All-static helper built on top of the `KHR_debug` functionality. Provides |
| 73 |
debug message groups, message insertion and object labels, all of which |
| 74 |
become no-ops when a debug context is not available. |
| 75 |
*/ |
| 76 |
class GLDebug |
| 77 |
{ |
| 78 |
public: |
| 79 |
/** |
| 80 |
* @brief OpenGL object types that can be labelled |
| 81 |
* |
| 82 |
* Maps to the OpenGL object identifier enums accepted by `glObjectLabel()`. |
| 83 |
*/ |
| 84 |
enum class LabelTypes |
| 85 |
{ |
| 86 |
// Apple's OpenGL headers don't expose the KHR_debug object-type enums, and PS Vita's vitaGL has no |
| 87 |
// KHR_debug at all (object labelling is compiled out entirely in GLDebug.cpp - GL_DEBUG_SUPPORTED is |
| 88 |
// never defined there for Vita). On both, these values are never passed to the driver, so use plain |
| 89 |
// sequential enumerators instead of the GL_* object-identifier tokens. |
| 90 |
#if defined(DEATH_TARGET_APPLE) || defined(DEATH_TARGET_VITA) |
| 91 |
Buffer, |
| 92 |
Shader, |
| 93 |
Program, |
| 94 |
VertexArray, |
| 95 |
Query, |
| 96 |
ProgramPipeline, |
| 97 |
TransformFeedback, |
| 98 |
Sampler, |
| 99 |
Texture, |
| 100 |
RenderBuffer, |
| 101 |
FrameBuffer |
| 102 |
#else |
| 103 |
TransformFeedback = GL_TRANSFORM_FEEDBACK, |
| 104 |
Texture = GL_TEXTURE, |
| 105 |
RenderBuffer = GL_RENDERBUFFER, |
| 106 |
FrameBuffer = GL_FRAMEBUFFER, |
| 107 |
# if ((defined(DEATH_TARGET_ANDROID) && __ANDROID_API__ >= 21) || (!defined(DEATH_TARGET_ANDROID) && defined(RHI_GL_PROFILE_ES))) && (GL_ES_VERSION_3_0 || (defined(RHI_GL_PROFILE_ES2) && !defined(DEATH_TARGET_VITA))) |
| 108 |
Buffer = GL_BUFFER_KHR, |
| 109 |
Shader = GL_SHADER_KHR, |
| 110 |
Program = GL_PROGRAM_KHR, |
| 111 |
VertexArray = GL_VERTEX_ARRAY_KHR, |
| 112 |
Query = GL_QUERY_KHR, |
| 113 |
ProgramPipeline = GL_PROGRAM_PIPELINE_KHR, |
| 114 |
Sampler = GL_SAMPLER_KHR |
| 115 |
# else |
| 116 |
Buffer = GL_BUFFER, |
| 117 |
Shader = GL_SHADER, |
| 118 |
Program = GL_PROGRAM, |
| 119 |
VertexArray = GL_VERTEX_ARRAY, |
| 120 |
Query = GL_QUERY, |
| 121 |
ProgramPipeline = GL_PROGRAM_PIPELINE, |
| 122 |
Sampler = GL_SAMPLER |
| 123 |
# endif |
| 124 |
#endif |
| 125 |
}; |
| 126 |
|
| 127 |
/** |
| 128 |
* @brief RAII scope for an OpenGL debug message group |
| 129 |
* |
| 130 |
* Pushes a debug group on construction and pops it on destruction. |
| 131 |
*/ |
| 132 |
class ScopedGroup |
| 133 |
{ |
| 134 |
public: |
| 135 |
/** @brief Pushes a debug group with the given message */ |
| 136 |
explicit ScopedGroup(StringView message) { |
| 137 |
PushGroup(message); |
| 138 |
} |
| 139 |
~ScopedGroup() { |
| 140 |
PopGroup(); |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
/** |
| 145 |
* @brief Queries debug capabilities and enables debug output if available |
| 146 |
* |
| 147 |
* @param caps Graphics capabilities used to detect `KHR_debug` support |
| 148 |
*/ |
| 149 |
static void Init(const IRhiCapabilities& caps); |
| 150 |
/** @brief Resets the running debug group id counter */ |
| 151 |
static inline void Reset() { |
| 152 |
_debugGroupId = 0; |
| 153 |
} |
| 154 |
|
| 155 |
/** @brief Returns whether OpenGL debug output is available */ |
| 156 |
static inline bool IsAvailable() { |
| 157 |
return _debugAvailable; |
| 158 |
} |
| 159 |
|
| 160 |
/** @brief Pushes a named debug group */ |
| 161 |
static void PushGroup(StringView message); |
| 162 |
/** @brief Pops the most recently pushed debug group */ |
| 163 |
static void PopGroup(); |
| 164 |
/** @brief Inserts a one-off debug marker message */ |
| 165 |
static void MessageInsert(StringView message); |
| 166 |
|
| 167 |
/** |
| 168 |
* @brief Sets a human-readable label on an OpenGL object |
| 169 |
* |
| 170 |
* @param identifier Type of the object being labelled |
| 171 |
* @param name Name (id) of the object |
| 172 |
* @param label Label to assign |
| 173 |
*/ |
| 174 |
static void SetObjectLabel(LabelTypes identifier, GLuint name, StringView label); |
| 175 |
/** |
| 176 |
* @brief Retrieves the label of an OpenGL object |
| 177 |
* |
| 178 |
* @param identifier Type of the object being queried |
| 179 |
* @param name Name (id) of the object |
| 180 |
* @param bufSize Size of the destination buffer |
| 181 |
* @param length Receives the number of characters written (may be `nullptr`) |
| 182 |
* @param label Destination buffer for the label |
| 183 |
*/ |
| 184 |
static void GetObjectLabel(LabelTypes identifier, GLuint name, GLsizei bufSize, GLsizei* length, char* label); |
| 185 |
|
| 186 |
/** @brief Returns the maximum supported object label length */ |
| 187 |
static inline std::int32_t GetMaxLabelLength() { |
| 188 |
return _maxLabelLength; |
| 189 |
} |
| 190 |
|
| 191 |
private: |
| 192 |
static bool _debugAvailable; |
| 193 |
static GLuint _debugGroupId; |
| 194 |
static std::int32_t _maxLabelLength; |
| 195 |
|
| 196 |
/** |
| 197 |
* @brief Enables OpenGL debug output and registers the logging callback |
| 198 |
*/ |
| 199 |
static void EnableDebugOutput(); |
| 200 |
}; |
| 201 |
|
| 202 |
} |