Debian Codegraph @

Sources/nCine/Graphics/RHI/PVR/PvrDevice.h

raw
1 #pragma once
2
3 #include "../RhiTypes.h"
4 #include "../../../Primitives/Rect.h"
5 #include "../../../Primitives/Colorf.h"
6
7 #include <cstdint>
8 #include <vector>
9
10 #include <dc/pvr.h>
11
12 namespace nCine::RHI::PVR
13 {
14 class PvrShaderProgram;
15 class PvrRenderTarget;
16 class PvrTexture;
17 // Defined by the generated PvrGeneratedEffects.h inside the device translation unit; everyone
18 // else only ever holds an opaque entry pointer resolved at program load
19 struct FixedFunctionGeneratedEffect;
20
21 /**
22 @brief Pipeline-state and draw-call facade of the PVR backend (aliased as `RHI::Device`)
23
24 The Dreamcast twin of the GX device: each draw decodes the bound program's instance block(s)
25 exactly like `SwDevice::Dispatch`, CPU-transforms the four sprite corners to logical pixels and
26 submits them as PowerVR translucent-list quads in display coordinates (the logical-to-640x480
27 scale is folded into the vertex positions). The TA renders one scene per frame, so the device runs
28 a small scene state machine: the first draw of a frame opens the scene (`pvr_scene_begin` /
29 `pvr_scene_begin_txr` for a render target) and its translucent list; switching targets or
30 presenting finishes it. Autosort is disabled at init, so the translucent list preserves submission
31 order - the engine's painter's-order queue maps 1:1.
32
33 Per-effect texture selection mirrors the GX device: CI8-style draws pick one of the four hardware
34 palette banks from the instance's `palOffset` (LRU), RG8 index+alpha textures use the per-row
35 ARGB4444 bake, `NoTexture` draws submit untextured colored quads, and the `Combine` draw applies
36 the queued CPU lightmap as a `dst * src` multiply quad plus the water tint bands.
37 */
38 class PvrDevice
39 {
40 public:
41 /** @brief Monotonic count of finished scenes, used to detect "still referenced by the current scene" resources */
42 static std::uint32_t GetSceneCounter() {
43 return _sceneCounter;
44 }
45
46 /**
47 @brief Returns the generated fixed-function effect of a (program, variant) key, or `nullptr`
48
49 Scans the table transpiled from the shaders' `fixed_function` blocks
50 (`Shaders/Generated/PvrGeneratedEffects.h`). Called once per program load from
51 @ref PvrShaderProgram::SetObjectLabel(), which maps the object label onto the key
52 through its exact-name table - the draw path only ever reads the stored pointer.
53 */
54 static const FixedFunctionGeneratedEffect* FindGeneratedEffect(const char* program, const char* variant);
55
56 PvrDevice() = delete;
57 ~PvrDevice() = delete;
58
59 struct ScissorState
60 {
61 bool Enabled = false;
62 Recti Rect = Recti(0, 0, 0, 0);
63 };
64
65 struct BlendingState
66 {
67 bool Enabled = false;
68 nCine::BlendingFactor SrcRgb = nCine::BlendingFactor::One;
69 nCine::BlendingFactor DstRgb = nCine::BlendingFactor::Zero;
70 nCine::BlendingFactor SrcAlpha = nCine::BlendingFactor::One;
71 nCine::BlendingFactor DstAlpha = nCine::BlendingFactor::Zero;
72 };
73
74 struct DepthTestState
75 {
76 bool TestEnabled = false;
77 bool MaskEnabled = true;
78 };
79
80 struct CullFaceState
81 {
82 bool Enabled = false;
83 CullFaceMode Mode = CullFaceMode::Back;
84 };
85
86 static void SetBlendingEnabled(bool enabled);
87 static void SetBlendingFactors(nCine::BlendingFactor srcRgb, nCine::BlendingFactor dstRgb, nCine::BlendingFactor srcAlpha, nCine::BlendingFactor dstAlpha);
88 static BlendingState GetBlendingState();
89 static void SetBlendingState(const BlendingState& state);
90
91 static void SetDepthTestEnabled(bool enabled);
92 static void SetDepthMaskEnabled(bool enabled);
93 static DepthTestState GetDepthTestState();
94 static void SetDepthTestState(const DepthTestState& state);
95
96 static void SetCullFaceEnabled(bool enabled);
97 static CullFaceState GetCullFaceState();
98 static void SetCullFaceState(const CullFaceState& state);
99
100 static ScissorState GetScissorState();
101 static void SetScissorState(const ScissorState& state);
102 static void SetScissor(const Recti& rect);
103 static void SetScissorTestEnabled(bool enabled);
104
105 static Recti GetViewport();
106 static void SetViewport(const Recti& rect);
107 static void InitViewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height);
108
109 static Colorf GetClearColor();
110 static void SetClearColor(const Colorf& color);
111 static void Clear(ClearFlags flags);
112
113 static void DrawArrays(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices);
114 static void DrawArraysInstanced(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices, std::int32_t numInstances);
115 static void DrawElements(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t baseVertex);
116 static inline void DrawElements(PrimitiveType primitive, std::uint32_t numIndices, std::uintptr_t indexOffset, std::int32_t baseVertex) {
117 DrawElements(primitive, numIndices, IndexFormat::UInt16, indexOffset, baseVertex);
118 }
119 static void DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex);
120 static inline void DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex) {
121 DrawElementsInstanced(primitive, numIndices, IndexFormat::UInt16, indexOffset, numInstances, baseVertex);
122 }
123
124 static FenceHandle InsertFence();
125 static void DeleteFence(FenceHandle& fence);
126 static bool ClientWaitFence(FenceHandle fence, std::uint64_t timeoutNs);
127
128 static void SetupInitialState();
129
130 // -- Swap-chain / presentation surface (uniform across every backend) --
131
132 /** @brief No-op (the window backend owns the presentation path) */
133 static inline bool CreateSwapchain(void* windowHandle, std::int32_t width, std::int32_t height, bool vsync) {
134 static_cast<void>(windowHandle);
135 static_cast<void>(width);
136 static_cast<void>(height);
137 static_cast<void>(vsync);
138 return true;
139 }
140 /** @brief No-op */
141 static inline void DestroySwapchain() {}
142 /** @brief No-op (the logical resolution is driven by ResizeScreenFramebuffer() from the render pipeline) */
143 static inline void ResizeSwapchain(std::int32_t width, std::int32_t height) {
144 static_cast<void>(width);
145 static_cast<void>(height);
146 }
147 /** @brief Finishes the frame's TA scene (called by the Dc window backend once per frame) */
148 static void PresentFrame();
149
150 /** @brief Returns the maximum supported texture dimension (drives the tileset chunking) */
151 static inline std::int32_t GetMaxTextureDimension() {
152 return 1024;
153 }
154
155 // -- Direct-tier presentation contract (see RhiFwd.h) --
156
157 /** @brief Sets the logical resolution the scene is rendered at (scaled to 640x480 at submit) */
158 static void ResizeScreenFramebuffer(std::int32_t width, std::int32_t height);
159
160 // -- PVR session, driven by the Dc window backend --
161
162 /** @brief Brings up the PVR (pvr_init with autosort disabled, background color, palette format) */
163 static void InitializePvr();
164
165 // -- PVR backend extensions (called by the resource types and read by the draw dispatch) --
166
167 /** @brief Records the currently bound shader program */
168 static void BindProgram(PvrShaderProgram* program);
169 /** @brief Returns the currently bound shader program */
170 static PvrShaderProgram* CurrentProgram();
171 /** @brief Records the texture bound to a texture unit */
172 static void BindTexture(std::uint32_t unit, const PvrTexture* texture);
173 /** @brief Clears a texture from every unit it is bound to (called from ~PvrTexture) */
174 static void UnbindTexture(const PvrTexture* texture);
175 /** @brief Returns the texture bound to a texture unit */
176 static const PvrTexture* GetBoundTexture(std::uint32_t unit);
177 /** @brief Records the host data range bound to a uniform binding point */
178 static void BindUniformRange(std::uint32_t index, const std::uint8_t* data, std::uint32_t size);
179 /** @brief Records the current draw render target (draws open a render-to-texture scene) */
180 static void SetRenderTarget(PvrRenderTarget* renderTarget);
181 /** @brief Clears a render target from the device if it is the current one (called from ~PvrRenderTarget) */
182 static void UnbindRenderTarget(const PvrRenderTarget* renderTarget);
183
184 /** @brief Registers the intercepted shared palette texture (rows become palette banks) */
185 static void RegisterPaletteTexture(PvrTexture* texture);
186 /** @brief Invalidates the palette banks (and RG8 bakes) of the given palette rows after an upload */
187 static void NotifyPaletteTextureChanged(PvrTexture* texture, std::int32_t firstRow, std::int32_t rowCount);
188
189 /** @brief Queues the CPU lightmap/water combine for the next `Combine` draw (the direct-tier lighting contract) */
190 static void SetPendingSoftwareLighting(const float* lightmap, std::int32_t lmW, std::int32_t lmH, std::int32_t scale,
191 std::int32_t vpX, std::int32_t vpY, std::int32_t vpW, std::int32_t vpH, float ambR, float ambG, float ambB,
192 bool waterActive = false, float waterLevelPx = 0.0f, float waterTime = 0.0f, float waterCamY = 0.0f);
193 /** @brief Drops any lighting entries not consumed this frame (called by the window backend at present) */
194 static void EndFrame();
195
196 private:
197 static constexpr std::uint32_t MaxTextureUnits = 8;
198 static constexpr std::uint32_t MaxUniformBindings = 8;
199 // The PowerVR has 1024 palette entries = four 256-entry banks; rows are mapped onto them LRU
200 static constexpr std::uint32_t MaxPaletteBanks = 4;
201
202 enum class SceneTarget
203 {
204 None,
205 Screen,
206 RenderTexture
207 };
208
209 struct UniformRange
210 {
211 const std::uint8_t* Data = nullptr;
212 std::uint32_t Size = 0;
213 };
214
215 struct PendingSoftwareLight
216 {
217 const float* Lightmap = nullptr;
218 std::int32_t LmW = 0, LmH = 0, Scale = 1;
219 std::int32_t VpX = 0, VpY = 0, VpW = 0, VpH = 0;
220 float AmbR = 0.0f, AmbG = 0.0f, AmbB = 0.0f;
221 bool WaterActive = false;
222 float WaterLevelPx = 0.0f, WaterTime = 0.0f, WaterCamY = 0.0f;
223 };
224
225 struct PaletteBank
226 {
227 std::int32_t PaletteOffset = -1;
228 const PvrTexture* Palette = nullptr;
229 std::uint32_t PaletteVersion = 0;
230 std::uint32_t LastUse = 0;
231 };
232
233 static BlendingState _blending;
234 static DepthTestState _depthTest;
235 static CullFaceState _cullFace;
236 static ScissorState _scissor;
237 static Recti _viewport;
238 static Colorf _clearColor;
239
240 static PvrShaderProgram* _currentProgram;
241 static const PvrTexture* _boundTextures[MaxTextureUnits];
242 static UniformRange _boundUniformRanges[MaxUniformBindings];
243 static PvrRenderTarget* _currentRenderTarget;
244
245 static bool _pvrInitialized;
246 static std::int32_t _logicalWidth;
247 static std::int32_t _logicalHeight;
248 static SceneTarget _sceneTarget;
249 static PvrRenderTarget* _sceneRenderTarget;
250
251 static PvrTexture* _paletteTexture;
252 static std::uint32_t _paletteGeneration;
253 static PaletteBank _paletteBanks[MaxPaletteBanks];
254 static std::uint32_t _paletteUseCounter;
255 static std::uint32_t _sceneCounter;
256
257 static std::vector<PendingSoftwareLight> _pendingSoftwareLights;
258
259 // Lightmap combine texture (rebuilt per Combine draw from the queued float lightmap)
260 static pvr_ptr_t _lightmapVram;
261 static std::size_t _lightmapVramSize;
262 static std::int32_t _lightmapW;
263 static std::int32_t _lightmapH;
264
265 static void Dispatch(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices);
266 // Draws a whole tile-layer mesh (a triangle list of position/texcoord/colour vertices)
267 static void DispatchTileMesh(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices);
268 // Draws a vertex-fed textured line strip (the weapon wheel); the TA has no line primitive,
269 // so every segment goes out as a thin screen-space quad
270 static void DispatchLineStrip(std::int32_t firstVertex, std::int32_t numVertices);
271 static void ApplyPendingSoftwareLighting();
272 static void EnsureScene();
273 static void FinishScene();
274 static std::int32_t AcquirePaletteBankForRow(const PvrTexture* palette, std::int32_t paletteRow);
275 /** @brief Loads 256 entries into one of the hardware palette banks, reusing the bank if they are already there */
276 static std::int32_t AcquirePaletteBank(const PvrTexture* palette, std::int32_t paletteOffset,
277 std::uint32_t version, const std::uint32_t* entries);
278 static void GetTargetScale(float& scaleX, float& scaleY, float& offsetX, float& offsetY);
279 };
280 }