Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLVertexArrayObject.cpp

raw
1 #include "GLVertexArrayObject.h"
2 #include "GLDebug.h"
3
4 #if defined(RHI_GL_PROFILE_ES2) && !defined(DEATH_TARGET_VITA)
5 // Vertex array objects are not ES 2.0 core - this profile relies on GL_OES_vertex_array_object, whose entry
6 // points carry the OES suffix on a true ES2 context. Being extension functions, they are not necessarily
7 // *exported* by the client library either (ANGLE exports them, Mesa's libGLESv2 does not), so they are
8 // resolved once through eglGetProcAddress() - the ES profiles always create their context through EGL. The
9 // extension is required by this engine's render loop, which binds all vertex state through VAOs. PS Vita's
10 // vitaGL is the exception: it exports the unsuffixed entry points directly, so nothing is remapped there.
11 # include <EGL/egl.h>
12
13 namespace nCine::RHI::GL
14 {
15 namespace
16 {
17 void (GL_APIENTRY* _glGenVertexArrays)(GLsizei, GLuint*) = nullptr;
18 void (GL_APIENTRY* _glDeleteVertexArrays)(GLsizei, const GLuint*) = nullptr;
19 void (GL_APIENTRY* _glBindVertexArray)(GLuint) = nullptr;
20
21 void ResolveVertexArrayEntryPoints()
22 {
23 if DEATH_LIKELY(_glGenVertexArrays != nullptr) {
24 return;
25 }
26
27 _glGenVertexArrays = reinterpret_cast<void (GL_APIENTRY*)(GLsizei, GLuint*)>(eglGetProcAddress("glGenVertexArraysOES"));
28 _glDeleteVertexArrays = reinterpret_cast<void (GL_APIENTRY*)(GLsizei, const GLuint*)>(eglGetProcAddress("glDeleteVertexArraysOES"));
29 _glBindVertexArray = reinterpret_cast<void (GL_APIENTRY*)(GLuint)>(eglGetProcAddress("glBindVertexArrayOES"));
30
31 FATAL_ASSERT_MSG(_glGenVertexArrays != nullptr && _glDeleteVertexArrays != nullptr && _glBindVertexArray != nullptr,
32 "GL_OES_vertex_array_object is required by the OpenGL|ES 2.0 profile but not provided by this context");
33 }
34 }
35 }
36
37 # define glGenVertexArrays _glGenVertexArrays
38 # define glDeleteVertexArrays _glDeleteVertexArrays
39 # define glBindVertexArray _glBindVertexArray
40 #endif
41
42 namespace nCine::RHI::GL
43 {
44 unsigned int GLVertexArrayObject::_boundVAO = 0;
45
46 GLVertexArrayObject::GLVertexArrayObject()
47 : _glHandle(0)
48 {
49 #if defined(RHI_GL_PROFILE_ES2) && !defined(DEATH_TARGET_VITA)
50 ResolveVertexArrayEntryPoints();
51 #endif
52 glGenVertexArrays(1, &_glHandle);
53 GL_LOG_ERRORS();
54 }
55
56 GLVertexArrayObject::~GLVertexArrayObject()
57 {
58 if (_boundVAO == _glHandle) {
59 Unbind();
60 }
61
62 glDeleteVertexArrays(1, &_glHandle);
63 GL_LOG_ERRORS();
64 }
65
66 bool GLVertexArrayObject::Bind() const
67 {
68 if (_boundVAO != _glHandle) {
69 glBindVertexArray(_glHandle);
70 GL_LOG_ERRORS();
71 _boundVAO = _glHandle;
72 return true;
73 }
74 return false;
75 }
76
77 bool GLVertexArrayObject::Unbind()
78 {
79 if (_boundVAO != 0) {
80 glBindVertexArray(0);
81 GL_LOG_ERRORS();
82 _boundVAO = 0;
83 return true;
84 }
85 return false;
86 }
87
88 void GLVertexArrayObject::SetObjectLabel(StringView label)
89 {
90 GLDebug::SetObjectLabel(GLDebug::LabelTypes::VertexArray, _glHandle, label);
91 }
92 }