Debian Codegraph @

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

raw
1 #include "GLDevice.h"
2 #include "GLBlending.h"
3 #include "GLDepthTest.h"
4 #include "GLCullFace.h"
5 #include "GLScissorTest.h"
6 #include "GLClearColor.h"
7 #include "GLViewport.h"
8 #include "GLFramebuffer.h"
9 #include "GLBufferObject.h"
10 #include "GLShaderProgram.h"
11 #include "GLTexture.h"
12 #include "GLVertexArrayObject.h"
13
14 namespace nCine::RHI::GL
15 {
16 // The backend-neutral enums promise GL-compatible numeric values, so this backend can translate with a plain cast
17 static_assert(static_cast<GLenum>(PrimitiveType::Points) == GL_POINTS);
18 static_assert(static_cast<GLenum>(PrimitiveType::Lines) == GL_LINES);
19 static_assert(static_cast<GLenum>(PrimitiveType::LineLoop) == GL_LINE_LOOP);
20 static_assert(static_cast<GLenum>(PrimitiveType::LineStrip) == GL_LINE_STRIP);
21 static_assert(static_cast<GLenum>(PrimitiveType::Triangles) == GL_TRIANGLES);
22 static_assert(static_cast<GLenum>(PrimitiveType::TriangleStrip) == GL_TRIANGLE_STRIP);
23 static_assert(static_cast<GLenum>(PrimitiveType::TriangleFan) == GL_TRIANGLE_FAN);
24 static_assert(static_cast<GLenum>(BufferUsage::StreamDraw) == GL_STREAM_DRAW);
25 static_assert(static_cast<GLenum>(BufferUsage::StaticDraw) == GL_STATIC_DRAW);
26 static_assert(static_cast<GLenum>(BufferUsage::DynamicDraw) == GL_DYNAMIC_DRAW);
27 static_assert(static_cast<GLenum>(BlendingFactor::Zero) == GL_ZERO);
28 static_assert(static_cast<GLenum>(BlendingFactor::One) == GL_ONE);
29 static_assert(static_cast<GLenum>(BlendingFactor::SrcColor) == GL_SRC_COLOR);
30 static_assert(static_cast<GLenum>(BlendingFactor::OneMinusSrcColor) == GL_ONE_MINUS_SRC_COLOR);
31 static_assert(static_cast<GLenum>(BlendingFactor::SrcAlpha) == GL_SRC_ALPHA);
32 static_assert(static_cast<GLenum>(BlendingFactor::OneMinusSrcAlpha) == GL_ONE_MINUS_SRC_ALPHA);
33 static_assert(static_cast<GLenum>(BlendingFactor::DstAlpha) == GL_DST_ALPHA);
34 static_assert(static_cast<GLenum>(BlendingFactor::OneMinusDstAlpha) == GL_ONE_MINUS_DST_ALPHA);
35 static_assert(static_cast<GLenum>(BlendingFactor::DstColor) == GL_DST_COLOR);
36 static_assert(static_cast<GLenum>(BlendingFactor::OneMinusDstColor) == GL_ONE_MINUS_DST_COLOR);
37 static_assert(static_cast<GLenum>(BlendingFactor::SrcAlphaSaturate) == GL_SRC_ALPHA_SATURATE);
38 #if !defined(DEATH_TARGET_VITA)
39 // vitaGL declares none of the constant-colour blend factors, ConstantColor/-Alpha blending is unused there
40 static_assert(static_cast<GLenum>(BlendingFactor::ConstantColor) == GL_CONSTANT_COLOR);
41 static_assert(static_cast<GLenum>(BlendingFactor::OneMinusConstantColor) == GL_ONE_MINUS_CONSTANT_COLOR);
42 static_assert(static_cast<GLenum>(BlendingFactor::ConstantAlpha) == GL_CONSTANT_ALPHA);
43 static_assert(static_cast<GLenum>(BlendingFactor::OneMinusConstantAlpha) == GL_ONE_MINUS_CONSTANT_ALPHA);
44 #endif
45 static_assert(static_cast<GLenum>(BufferTarget::Vertex) == GL_ARRAY_BUFFER);
46 static_assert(static_cast<GLenum>(BufferTarget::Index) == GL_ELEMENT_ARRAY_BUFFER);
47 static_assert(static_cast<GLenum>(BufferTarget::Uniform) == GL_UNIFORM_BUFFER);
48 static_assert(static_cast<GLbitfield>(MapFlags::Write) == GL_MAP_WRITE_BIT);
49 static_assert(static_cast<GLbitfield>(MapFlags::InvalidateRange) == GL_MAP_INVALIDATE_RANGE_BIT);
50 static_assert(static_cast<GLbitfield>(MapFlags::InvalidateBuffer) == GL_MAP_INVALIDATE_BUFFER_BIT);
51 static_assert(static_cast<GLbitfield>(MapFlags::FlushExplicit) == GL_MAP_FLUSH_EXPLICIT_BIT);
52 static_assert(static_cast<GLbitfield>(MapFlags::Unsynchronized) == GL_MAP_UNSYNCHRONIZED_BIT);
53 #if defined(GL_MAP_PERSISTENT_BIT)
54 static_assert(static_cast<GLbitfield>(MapFlags::Persistent) == GL_MAP_PERSISTENT_BIT);
55 static_assert(static_cast<GLbitfield>(MapFlags::Coherent) == GL_MAP_COHERENT_BIT);
56 #endif
57 static_assert(static_cast<GLenum>(IndexFormat::UInt16) == GL_UNSIGNED_SHORT);
58 static_assert(static_cast<GLenum>(IndexFormat::UInt32) == GL_UNSIGNED_INT);
59 static_assert(static_cast<GLenum>(CullFaceMode::Front) == GL_FRONT);
60 static_assert(static_cast<GLenum>(CullFaceMode::Back) == GL_BACK);
61 static_assert(static_cast<GLenum>(CullFaceMode::FrontAndBack) == GL_FRONT_AND_BACK);
62
63 void GLDevice::SetBlendingEnabled(bool enabled)
64 {
65 if (enabled) {
66 GLBlending::Enable();
67 } else {
68 GLBlending::Disable();
69 }
70 }
71
72 void GLDevice::SetBlendingFactors(BlendingFactor srcRgb, BlendingFactor dstRgb, BlendingFactor srcAlpha, BlendingFactor dstAlpha)
73 {
74 GLBlending::SetBlendFunc(static_cast<GLenum>(srcRgb), static_cast<GLenum>(dstRgb), static_cast<GLenum>(srcAlpha), static_cast<GLenum>(dstAlpha));
75 }
76
77 GLDevice::BlendingState GLDevice::GetBlendingState()
78 {
79 const GLBlending::State state = GLBlending::GetState();
80 return { state.enabled, BlendingFactor(state.srcRgb), BlendingFactor(state.dstRgb), BlendingFactor(state.srcAlpha), BlendingFactor(state.dstAlpha) };
81 }
82
83 void GLDevice::SetBlendingState(const BlendingState& state)
84 {
85 GLBlending::SetState({ state.Enabled, static_cast<GLenum>(state.SrcRgb), static_cast<GLenum>(state.DstRgb), static_cast<GLenum>(state.SrcAlpha), static_cast<GLenum>(state.DstAlpha) });
86 }
87
88 void GLDevice::SetDepthTestEnabled(bool enabled)
89 {
90 if (enabled) {
91 GLDepthTest::Enable();
92 } else {
93 GLDepthTest::Disable();
94 }
95 }
96
97 void GLDevice::SetDepthMaskEnabled(bool enabled)
98 {
99 if (enabled) {
100 GLDepthTest::EnableDepthMask();
101 } else {
102 GLDepthTest::DisableDepthMask();
103 }
104 }
105
106 GLDevice::DepthTestState GLDevice::GetDepthTestState()
107 {
108 const GLDepthTest::State state = GLDepthTest::GetState();
109 return { state.enabled, state.depthMaskEnabled };
110 }
111
112 void GLDevice::SetDepthTestState(const DepthTestState& state)
113 {
114 GLDepthTest::SetState({ state.TestEnabled, state.MaskEnabled });
115 }
116
117 void GLDevice::SetCullFaceEnabled(bool enabled)
118 {
119 if (enabled) {
120 GLCullFace::Enable();
121 } else {
122 GLCullFace::Disable();
123 }
124 }
125
126 GLDevice::CullFaceState GLDevice::GetCullFaceState()
127 {
128 const GLCullFace::State state = GLCullFace::GetState();
129 return { state.enabled, CullFaceMode(state.mode) };
130 }
131
132 void GLDevice::SetCullFaceState(const CullFaceState& state)
133 {
134 GLCullFace::SetState({ state.Enabled, static_cast<GLenum>(state.Mode) });
135 }
136
137 GLDevice::ScissorState GLDevice::GetScissorState()
138 {
139 const GLScissorTest::State state = GLScissorTest::GetState();
140 return { state.enabled, state.rect };
141 }
142
143 void GLDevice::SetScissorState(const ScissorState& state)
144 {
145 GLScissorTest::SetState({ state.Enabled, state.Rect });
146 }
147
148 void GLDevice::SetScissor(const Recti& rect)
149 {
150 GLScissorTest::Enable(rect);
151 }
152
153 void GLDevice::SetScissorTestEnabled(bool enabled)
154 {
155 if (enabled) {
156 GLScissorTest::Enable();
157 } else {
158 GLScissorTest::Disable();
159 }
160 }
161
162 Recti GLDevice::GetViewport()
163 {
164 return GLViewport::GetRect();
165 }
166
167 void GLDevice::SetViewport(const Recti& rect)
168 {
169 GLViewport::SetRect(rect);
170 }
171
172 void GLDevice::InitViewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height)
173 {
174 GLViewport::InitRect(x, y, width, height);
175 }
176
177 Colorf GLDevice::GetClearColor()
178 {
179 return GLClearColor::GetColor();
180 }
181
182 void GLDevice::SetClearColor(const Colorf& color)
183 {
184 GLClearColor::SetColor(color);
185 }
186
187 void GLDevice::Clear(ClearFlags flags)
188 {
189 GLbitfield mask = 0;
190 if ((flags & ClearFlags::Color) == ClearFlags::Color) {
191 mask |= GL_COLOR_BUFFER_BIT;
192 }
193 if ((flags & ClearFlags::Depth) == ClearFlags::Depth) {
194 mask |= GL_DEPTH_BUFFER_BIT;
195 }
196 if ((flags & ClearFlags::Stencil) == ClearFlags::Stencil) {
197 mask |= GL_STENCIL_BUFFER_BIT;
198 }
199 if (mask != 0) {
200 glClear(mask);
201 }
202 }
203
204 void GLDevice::DrawArrays(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices)
205 {
206 glDrawArrays(static_cast<GLenum>(primitive), firstVertex, numVertices);
207 }
208
209 void GLDevice::DrawArraysInstanced(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices, std::int32_t numInstances)
210 {
211 #if defined(RHI_GL_PROFILE_ES2)
212 // ES2 has no instanced drawing; the profile never issues instanced commands (CPU sprite batching is
213 // disabled and every RenderCommand draws single-instance), so keeping the ES 3.0 symbol out of the
214 // build is both a spec guard and a link-time aid for true ES2 loaders
215 static_cast<void>(primitive); static_cast<void>(firstVertex); static_cast<void>(numVertices); static_cast<void>(numInstances);
216 FATAL_ASSERT_MSG(false, "Instanced drawing is not supported on OpenGL|ES 2.0");
217 #else
218 glDrawArraysInstanced(static_cast<GLenum>(primitive), firstVertex, numVertices, numInstances);
219 #endif
220 }
221
222 void GLDevice::DrawElements(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t baseVertex)
223 {
224 void* indexOffsetPtr = reinterpret_cast<void*>(indexOffset);
225 #if (defined(RHI_GL_PROFILE_ES) && !GL_ES_VERSION_3_2) || defined(DEATH_TARGET_EMSCRIPTEN)
226 // Base vertex is emulated by the caller offsetting the vertex format instead
227 static_cast<void>(baseVertex);
228 glDrawElements(static_cast<GLenum>(primitive), GLsizei(numIndices), static_cast<GLenum>(indexFormat), indexOffsetPtr);
229 #else
230 glDrawElementsBaseVertex(static_cast<GLenum>(primitive), GLsizei(numIndices), static_cast<GLenum>(indexFormat), indexOffsetPtr, baseVertex);
231 #endif
232 }
233
234 void GLDevice::DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex)
235 {
236 #if defined(RHI_GL_PROFILE_ES2)
237 // See DrawArraysInstanced() - unreachable on the ES2 profile
238 static_cast<void>(primitive); static_cast<void>(numIndices); static_cast<void>(indexFormat);
239 static_cast<void>(indexOffset); static_cast<void>(numInstances); static_cast<void>(baseVertex);
240 FATAL_ASSERT_MSG(false, "Instanced drawing is not supported on OpenGL|ES 2.0");
241 #else
242 void* indexOffsetPtr = reinterpret_cast<void*>(indexOffset);
243 #if (defined(RHI_GL_PROFILE_ES) && !GL_ES_VERSION_3_2) || defined(DEATH_TARGET_EMSCRIPTEN)
244 // Base vertex is emulated by the caller offsetting the vertex format instead
245 static_cast<void>(baseVertex);
246 glDrawElementsInstanced(static_cast<GLenum>(primitive), GLsizei(numIndices), static_cast<GLenum>(indexFormat), indexOffsetPtr, numInstances);
247 #else
248 glDrawElementsInstancedBaseVertex(static_cast<GLenum>(primitive), GLsizei(numIndices), static_cast<GLenum>(indexFormat), indexOffsetPtr, numInstances, baseVertex);
249 #endif
250 #endif
251 }
252
253 FenceHandle GLDevice::InsertFence()
254 {
255 #if defined(RHI_GL_PROFILE_ES2)
256 // Fence sync objects are ES 3.0; the only user is the persistent-mapping ring, which is compiled out
257 // on every OpenGL|ES configuration (NCINE_HAS_PERSISTENT_MAPPING excludes RHI_GL_PROFILE_ES)
258 return nullptr;
259 #else
260 return glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
261 #endif
262 }
263
264 void GLDevice::DeleteFence(FenceHandle& fence)
265 {
266 #if defined(RHI_GL_PROFILE_ES2)
267 fence = nullptr;
268 #else
269 if (fence != nullptr) {
270 glDeleteSync(static_cast<GLsync>(fence));
271 fence = nullptr;
272 }
273 #endif
274 }
275
276 bool GLDevice::ClientWaitFence(FenceHandle fence, std::uint64_t timeoutNs)
277 {
278 #if defined(RHI_GL_PROFILE_ES2)
279 static_cast<void>(fence); static_cast<void>(timeoutNs);
280 return true;
281 #else
282 const GLenum result = glClientWaitSync(static_cast<GLsync>(fence), GL_SYNC_FLUSH_COMMANDS_BIT, timeoutNs);
283 return (result == GL_ALREADY_SIGNALED || result == GL_CONDITION_SATISFIED);
284 #endif
285 }
286
287 void GLDevice::SetupInitialState()
288 {
289 #if !defined(DEATH_TARGET_VITA)
290 // vitaGL does not declare GL_DITHER, dithering is not part of its fixed-function state
291 glDisable(GL_DITHER);
292 #endif
293 GLBlending::SetBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
294 GLDepthTest::Enable();
295 }
296
297 void GLDevice::ResyncExternalStateChanges()
298 {
299 // Object bindings: drop the caches so the next bind of each object re-applies
300 GLFramebuffer::InvalidateCachedBindings();
301 GLBufferObject::InvalidateCachedBindings();
302 GLShaderProgram::InvalidateCachedBinding();
303 GLTexture::InvalidateCachedBindings();
304 GLVertexArrayObject::InvalidateCachedBinding();
305 // Fixed-function state: force the driver back to what the caches say
306 GLBlending::Reapply();
307 GLDepthTest::Reapply();
308 GLCullFace::Reapply();
309 GLScissorTest::Reapply();
310 GLClearColor::Reapply();
311 GLViewport::Reapply();
312 }
313 }