Debian Codegraph @

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

raw
1 #include "GLVertexFormat.h"
2 #include "GLBufferObject.h"
3 #include "../IRhiCapabilities.h"
4 #include "../../../ServiceLocator.h"
5 #include "../../../../Main.h"
6
7 namespace nCine::RHI::GL
8 {
9 GLVertexFormat::Attribute::Attribute()
10 : _vbo(nullptr), _pointer(nullptr), _index(0), _size(-1), _type(GL_FLOAT), _stride(0), _baseOffset(0), _enabled(false), _normalized(GL_FALSE)
11 {
12 }
13
14 GLVertexFormat::GLVertexFormat()
15 : _ibo(nullptr), _attributes(MaxAttributes)
16 {
17 }
18
19 bool GLVertexFormat::Attribute::operator==(const Attribute& other) const
20 {
21 return ((other._enabled == false && _enabled == false) ||
22 ((other._enabled == true && _enabled == true) &&
23 (other._vbo && _vbo && other._vbo->GetGLHandle() == _vbo->GetGLHandle()) &&
24 other._index == _index &&
25 other._size == _size &&
26 other._type == _type &&
27 other._normalized == _normalized &&
28 other._stride == _stride &&
29 other._pointer == _pointer &&
30 other._baseOffset == _baseOffset));
31 }
32
33 bool GLVertexFormat::Attribute::operator!=(const Attribute& other) const
34 {
35 return !operator==(other);
36 }
37
38 void GLVertexFormat::Attribute::Init(std::uint32_t index, GLint size, GLenum type)
39 {
40 _enabled = true;
41 _vbo = nullptr;
42 _index = index;
43 _size = size;
44 _type = type;
45 _normalized = GL_FALSE;
46 _stride = 0;
47 _pointer = nullptr;
48 _baseOffset = 0;
49 }
50
51 void GLVertexFormat::Attribute::SetVboParameters(GLsizei stride, const GLvoid* pointer)
52 {
53 #if !defined(DEATH_TARGET_EMSCRIPTEN) && !(defined(DEATH_TARGET_APPLE) && defined(DEATH_TARGET_ARM)) && (defined(RHI_GL_PROFILE_CORE) || GL_ES_VERSION_3_1)
54 static const std::int32_t MaxVertexAttribStride = theServiceLocator().GetRhiCapabilities().GetValue(IRhiCapabilities::IntValues::MaxVertexAttribStride);
55
56 if (stride > MaxVertexAttribStride) {
57 _stride = MaxVertexAttribStride;
58 LOGW("Vertex attribute stride ({}) is bigger than the maximum value supported ({})", stride, MaxVertexAttribStride);
59 } else
60 #endif
61 {
62 _stride = stride;
63 }
64
65 _pointer = pointer;
66 }
67
68 void GLVertexFormat::Define()
69 {
70 for (std::uint32_t i = 0; i < MaxAttributes; i++) {
71 if (_attributes[i]._enabled) {
72 _attributes[i]._vbo->Bind();
73 glEnableVertexAttribArray(_attributes[i]._index);
74
75 #if (defined(RHI_GL_PROFILE_ES) && !GL_ES_VERSION_3_2) || defined(DEATH_TARGET_EMSCRIPTEN)
76 const GLubyte* initialPointer = reinterpret_cast<const GLubyte*>(_attributes[i]._pointer);
77 const GLvoid* pointer = reinterpret_cast<const GLvoid*>(initialPointer + _attributes[i]._baseOffset);
78 #else
79 const GLvoid* pointer = _attributes[i]._pointer;
80 #endif
81
82 #if defined(RHI_GL_PROFILE_ES2)
83 // ES2 has no integer vertex attributes (glVertexAttribIPointer is ES 3.0) and no GL_(UNSIGNED_)INT
84 // attribute data type at all; the ESSL 100 emitter already declares every attribute float-typed and
85 // the only integer-typed streams belong to the batched-mesh programs this profile never compiles
86 glVertexAttribPointer(_attributes[i]._index, _attributes[i]._size, _attributes[i]._type, _attributes[i]._normalized, _attributes[i]._stride, pointer);
87 #else
88 switch (_attributes[i]._type) {
89 case GL_BYTE:
90 case GL_UNSIGNED_BYTE:
91 case GL_SHORT:
92 case GL_UNSIGNED_SHORT:
93 case GL_INT:
94 case GL_UNSIGNED_INT:
95 if (_attributes[i]._normalized) {
96 glVertexAttribPointer(_attributes[i]._index, _attributes[i]._size, _attributes[i]._type, GL_TRUE, _attributes[i]._stride, pointer);
97 } else {
98 glVertexAttribIPointer(_attributes[i]._index, _attributes[i]._size, _attributes[i]._type, _attributes[i]._stride, pointer);
99 }
100 break;
101 default:
102 glVertexAttribPointer(_attributes[i]._index, _attributes[i]._size, _attributes[i]._type, _attributes[i]._normalized, _attributes[i]._stride, pointer);
103 break;
104 }
105 #endif
106 }
107 }
108
109 if (_ibo) {
110 _ibo->Bind();
111 }
112 }
113
114 void GLVertexFormat::Reset()
115 {
116 for (std::uint32_t i = 0; i < MaxAttributes; i++) {
117 _attributes[i]._enabled = false;
118 }
119 _ibo = nullptr;
120 }
121
122 namespace
123 {
124 inline std::uint64_t hashCombine(std::uint64_t h, std::uint64_t v)
125 {
126 // One splitmix64 step over the previous hash and the new value
127 h += 0x9E3779B97F4A7C15ull + v;
128 h = (h ^ (h >> 30)) * 0xBF58476D1CE4E5B9ull;
129 h = (h ^ (h >> 27)) * 0x94D049BB133111EBull;
130 return h ^ (h >> 31);
131 }
132 }
133
134 std::uint64_t GLVertexFormat::CalculateFingerprint() const
135 {
136 // Mirrors operator==(): only enabled attributes and the IBO identity contribute
137 std::uint64_t hash = std::uint64_t(std::uintptr_t(_ibo));
138 for (std::uint32_t i = 0; i < MaxAttributes; i++) {
139 const Attribute& attribute = _attributes[i];
140 if (!attribute._enabled) {
141 continue;
142 }
143 hash = hashCombine(hash, (std::uint64_t(attribute._index) << 32) | (attribute._vbo != nullptr ? attribute._vbo->GetGLHandle() : 0));
144 hash = hashCombine(hash, (std::uint64_t(std::uint32_t(attribute._size)) << 32) | attribute._type);
145 hash = hashCombine(hash, (std::uint64_t(std::uint32_t(attribute._stride)) << 33) | (std::uint64_t(attribute._baseOffset) << 1) | (attribute._normalized ? 1u : 0u));
146 hash = hashCombine(hash, std::uint64_t(std::uintptr_t(attribute._pointer)));
147 }
148 return hash;
149 }
150
151 bool GLVertexFormat::operator==(const GLVertexFormat& other) const
152 {
153 bool areEqual = (other._ibo == _ibo && other._attributes.size() == _attributes.size());
154
155 // If indices are the same then check attributes too
156 if (areEqual) {
157 for (std::uint32_t i = 0; i < _attributes.size(); i++) {
158 if (other._attributes[i] != _attributes[i]) {
159 areEqual = false;
160 break;
161 }
162 }
163 }
164
165 return areEqual;
166 }
167
168 bool GLVertexFormat::operator!=(const GLVertexFormat& other) const
169 {
170 return !operator==(other);
171 }
172 }