Enigma  3.2.0
A Simple, Reliable and Efficient Encryption Tool
OpenGLUtils.hpp
Go to the documentation of this file.
1 #pragma once
2 #ifndef ENIGMA_OPENGL_UTILS_H
3 #define ENIGMA_OPENGL_UTILS_H
4 #include <glad/glad.h>
5 #include <Core/Core.hpp>
6 #include <cstring>
7 
8 // NVX_gpu_memory_info extension defines from https://www.khronos.org/registry/OpenGL/extensions/NVX/NVX_gpu_memory_info.tx
9 #define GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX 0x9047
10 #define GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX 0x9048
11 #define GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX 0x9049
12 #define GL_GPU_MEMORY_INFO_EVICTION_COUNT_NVX 0x904A
13 #define GL_GPU_MEMORY_INFO_EVICTED_MEMORY_NVX 0x904B
14 
15 
17 
18 class OpenGLUtils final {
19  ENIGMA_STATIC_CLASS(OpenGLUtils);
20 
21  public:
22  /*
23  * Checks wether an extension available for example: GL_NVX_gpu_memory_info
24  * returns extension index of glGetStringi() if found, -1 otherwise
25  */
26  static const GLint IsExtensionSupported(const char *extension_name) noexcept {
27  GLint ext_count{};
28  glGetIntegerv(GL_NUM_EXTENSIONS, &ext_count);
29  if (ext_count < 1)
30  return -1;
31 
32  for (GLint i = 0; i < ext_count; ++i) {
33  const char *extension = reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i));
34  if (std::strcmp(extension, extension_name) == 0) {
35  // Found
36  return i;
37  }
38  }
39  // Not found
40  return -1;
41  }
42 
43  /*
44  * Convert GL Error Enum macro name to a std::string, returns "Unknown GLenum" on non detected macros
45  */
46  static constexpr const char *GetGLErrorEnumString(const GLenum& _enum) noexcept {
47 #define CASE_ENUM(e) \
48  case e: \
49  return #e
50  switch (_enum) {
51  // errors
52  CASE_ENUM(GL_NONE);
53  CASE_ENUM(GL_INVALID_ENUM);
54  CASE_ENUM(GL_INVALID_VALUE);
55  CASE_ENUM(GL_INVALID_OPERATION);
56  CASE_ENUM(GL_INVALID_FRAMEBUFFER_OPERATION);
57  CASE_ENUM(GL_OUT_OF_MEMORY);
58  default:
59  return "Unknown GLenum";
60  };
61 #undef CASE_ENUM
62  }
63 };
64 
65 
67 #define glAssert(call) \
68  do { \
69  (call); \
70  const GLenum err = glGetError(); \
71  if (err != GL_NO_ERROR) { \
72  ENIGMA_ASSERT(false, OpenGLUtils::GetGLErrorEnumString(err)); \
73  } \
74  } while (false)
76 
78 
79 #endif // !ENIGMA_OPENGL_UTILS_H
#define NS_ENIGMA_BEGIN
Enable/Disable Assertions.
Definition: Macros.hpp:13
#define NS_ENIGMA_END
Definition: Macros.hpp:14
#define CASE_ENUM(e)
static constexpr const char * GetGLErrorEnumString(const GLenum &_enum) noexcept
Definition: OpenGLUtils.hpp:46
static const GLint IsExtensionSupported(const char *extension_name) noexcept
Definition: OpenGLUtils.hpp:26