Debian Codegraph @

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

raw
1 #pragma once
2
3 #include "../RhiTypes.h"
4 #include "../../../Primitives/Rect.h"
5 #include "../../../Primitives/Colorf.h"
6
7 namespace nCine::RHI::GL
8 {
9 /**
10 @brief Pipeline-state and draw-call facade of the OpenGL backend
11
12 Exposes everything the render pipeline sets between draws — blending, depth, scissor, viewport,
13 clear — plus the draw calls themselves, with backend-neutral signatures (aliased as `RHI::Device`).
14 Delegates to the individual OpenGL state-cache wrappers, so redundant state changes keep being
15 filtered exactly as before.
16 */
17 class GLDevice
18 {
19 public:
20 GLDevice() = delete;
21 ~GLDevice() = delete;
22
23 /**
24 * @brief Snapshot of the scissor test state, used to save and restore it around a draw
25 */
26 struct ScissorState
27 {
28 /** @brief Whether the scissor test is enabled */
29 bool Enabled = false;
30 /** @brief Scissor rectangle */
31 Recti Rect = Recti(0, 0, 0, 0);
32 };
33
34 /**
35 * @brief Snapshot of the blending state, used to save and restore it around a draw
36 */
37 struct BlendingState
38 {
39 /** @brief Whether blending is enabled */
40 bool Enabled = false;
41 /** @brief Source RGB blend factor */
42 BlendingFactor SrcRgb = BlendingFactor::One;
43 /** @brief Destination RGB blend factor */
44 BlendingFactor DstRgb = BlendingFactor::Zero;
45 /** @brief Source alpha blend factor */
46 BlendingFactor SrcAlpha = BlendingFactor::One;
47 /** @brief Destination alpha blend factor */
48 BlendingFactor DstAlpha = BlendingFactor::Zero;
49 };
50
51 /**
52 * @brief Snapshot of the depth test state, used to save and restore it around a draw
53 */
54 struct DepthTestState
55 {
56 /** @brief Whether the depth test is enabled */
57 bool TestEnabled = false;
58 /** @brief Whether writing to the depth buffer is enabled */
59 bool MaskEnabled = true;
60 };
61
62 /**
63 * @brief Snapshot of the face culling state, used to save and restore it around a draw
64 */
65 struct CullFaceState
66 {
67 /** @brief Whether face culling is enabled */
68 bool Enabled = false;
69 /** @brief Culled face mode */
70 CullFaceMode Mode = CullFaceMode::Back;
71 };
72
73 /** @brief Enables or disables blending */
74 static void SetBlendingEnabled(bool enabled);
75 /** @brief Sets separate blending factors for color and alpha */
76 static void SetBlendingFactors(BlendingFactor srcRgb, BlendingFactor dstRgb, BlendingFactor srcAlpha, BlendingFactor dstAlpha);
77 /** @brief Returns the whole blending state */
78 static BlendingState GetBlendingState();
79 /** @brief Restores the whole blending state */
80 static void SetBlendingState(const BlendingState& state);
81
82 /** @brief Enables or disables the depth test */
83 static void SetDepthTestEnabled(bool enabled);
84 /** @brief Enables or disables writing to the depth buffer */
85 static void SetDepthMaskEnabled(bool enabled);
86 /** @brief Returns the whole depth test state */
87 static DepthTestState GetDepthTestState();
88 /** @brief Restores the whole depth test state */
89 static void SetDepthTestState(const DepthTestState& state);
90
91 /** @brief Enables or disables face culling */
92 static void SetCullFaceEnabled(bool enabled);
93 /** @brief Returns the whole face culling state */
94 static CullFaceState GetCullFaceState();
95 /** @brief Restores the whole face culling state */
96 static void SetCullFaceState(const CullFaceState& state);
97
98 /** @brief Returns the whole scissor test state */
99 static ScissorState GetScissorState();
100 /** @brief Restores the whole scissor test state */
101 static void SetScissorState(const ScissorState& state);
102 /** @brief Enables the scissor test with the specified rectangle */
103 static void SetScissor(const Recti& rect);
104 /** @brief Enables (reusing the last rectangle) or disables the scissor test */
105 static void SetScissorTestEnabled(bool enabled);
106
107 /** @brief Returns the current viewport rectangle */
108 static Recti GetViewport();
109 /** @brief Sets the viewport rectangle */
110 static void SetViewport(const Recti& rect);
111 /** @brief Initializes the cached viewport rectangle right after context creation */
112 static void InitViewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height);
113
114 /** @brief Returns the current clear color */
115 static Colorf GetClearColor();
116 /** @brief Sets the clear color */
117 static void SetClearColor(const Colorf& color);
118 /** @brief Clears the specified buffers of the current render target */
119 static void Clear(ClearFlags flags);
120
121 /** @brief Draws non-indexed geometry */
122 static void DrawArrays(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices);
123 /** @brief Draws non-indexed geometry with instancing */
124 static void DrawArraysInstanced(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices, std::int32_t numInstances);
125 /**
126 * @brief Draws indexed geometry
127 *
128 * @param indexOffset Byte offset of the first index within the bound index buffer
129 * @param baseVertex Added to each index — ignored by backends without base-vertex support,
130 * where the caller offsets the vertex format instead
131 */
132 static void DrawElements(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t baseVertex);
133 /** @brief Draws indexed geometry with 16-bit indices */
134 static inline void DrawElements(PrimitiveType primitive, std::uint32_t numIndices, std::uintptr_t indexOffset, std::int32_t baseVertex) {
135 DrawElements(primitive, numIndices, IndexFormat::UInt16, indexOffset, baseVertex);
136 }
137 /** @brief Draws indexed geometry with instancing */
138 static void DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex);
139 /** @brief Draws indexed geometry (16-bit indices) with instancing */
140 static inline void DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex) {
141 DrawElementsInstanced(primitive, numIndices, IndexFormat::UInt16, indexOffset, numInstances, baseVertex);
142 }
143
144 /** @brief Inserts a fence that signals once all previously submitted commands complete */
145 static FenceHandle InsertFence();
146 /** @brief Deletes a fence and resets the handle to `nullptr` (accepts a null handle) */
147 static void DeleteFence(FenceHandle& fence);
148 /** @brief Blocks until the fence signals, flushing pending commands; returns `false` on timeout or failure */
149 static bool ClientWaitFence(FenceHandle fence, std::uint64_t timeoutNs);
150
151 /** @brief Applies the initial pipeline state once after context creation */
152 static void SetupInitialState();
153
154 /** @brief Re-synchronizes the driver with every cached GL state after external code (e.g. a libretro
155 frontend sharing the context) changed bindings and fixed-function state behind the caches */
156 static void ResyncExternalStateChanges();
157
158 // -- Swap-chain / presentation surface (uniform across every backend) --
159 // The OpenGL backend renders through a GL context the WINDOW backend owns (SDL / GLFW / EGL), which
160 // also performs the buffer swap (e.g. SDL_GL_SwapWindow) - so this whole quartet is inert here. It
161 // exists so window backends and shared code can drive any backend through the same calls instead of
162 // branching on WITH_RHI_* (the D3D11 / Vulkan devices own a real swap chain behind the same surface).
163
164 /** @brief No-op (the window backend owns the GL context; there is no backend swap chain) */
165 static inline bool CreateSwapchain(void* windowHandle, std::int32_t width, std::int32_t height, bool vsync) {
166 static_cast<void>(windowHandle);
167 static_cast<void>(width);
168 static_cast<void>(height);
169 static_cast<void>(vsync);
170 return true;
171 }
172 /** @brief No-op */
173 static inline void DestroySwapchain() {}
174 /** @brief No-op (the GL default framebuffer resizes with the window) */
175 static inline void ResizeSwapchain(std::int32_t width, std::int32_t height) {
176 static_cast<void>(width);
177 static_cast<void>(height);
178 }
179 /** @brief No-op (the window backend performs the buffer swap) */
180 static inline void PresentFrame() {}
181 };
182 }