| 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 <gccore.h> |
| 11 |
|
| 12 |
namespace nCine::RHI::GX |
| 13 |
{ |
| 14 |
class GxShaderProgram; |
| 15 |
class GxRenderTarget; |
| 16 |
class GxTexture; |
| 17 |
// Defined by the generated GxGeneratedEffects.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 GX backend (aliased as `RHI::Device`) |
| 23 |
|
| 24 |
Mirrors the software device's surface, but instead of rasterizing on the CPU each draw decodes the |
| 25 |
bound program's instance block(s) exactly like `SwDevice::Dispatch` and emits fixed-function GX |
| 26 |
quads: the four sprite corners are CPU-transformed to logical screen pixels (the same math as the |
| 27 |
software `FetchVertex`), an orthographic projection over the logical resolution maps them onto the |
| 28 |
full EFB (which is how the logical-resolution scene is upscaled to the display for free), and the |
| 29 |
effect selected from the program label picks the texture/TEV configuration: |
| 30 |
- `DefaultSprite` family: textured quad, `GX_MODULATE` with the instance color |
| 31 |
- `PaletteRemap` family: CI8 texture with the TLUT of the instance's `palOffset` row; RG8 index+alpha |
| 32 |
textures use a per-row CPU-baked RGBA copy (see @ref GxTexture::EnsureBakedRgba) |
| 33 |
- `NoTexture` family: flat color quad (`GX_PASSCLR`) |
| 34 |
- `TileMapMesh` family: a whole tile layer arrives as one triangle-list mesh (see |
| 35 |
@ref DispatchTileMesh) and is submitted as batched `GX_QUADS` runs sharing one texture/TEV setup |
| 36 |
- `TexturedBackground` family: rendered as a plain textured quad (the per-pixel tunnel warp needs |
| 37 |
shaders; the flat fallback keeps those layers visible) |
| 38 |
- `Combine`: intercepted - applies the queued CPU lightmap as a multiply quad plus the water tint |
| 39 |
bands (the direct-tier lighting contract shared with the software backend) |
| 40 |
Unknown effects log once and skip their draws. |
| 41 |
|
| 42 |
Render targets are EFB copies: drawing into a bound @ref GxRenderTarget renders to the EFB with the |
| 43 |
target's dimensions and is copied out (`GX_CopyTex`) into the attached texture's tiled store when the |
| 44 |
target is switched away, so the textured-background passes work. Render-target passes must precede |
| 45 |
the screen pass within a frame (the engine's viewport chain already orders them that way). |
| 46 |
*/ |
| 47 |
class GxDevice |
| 48 |
{ |
| 49 |
public: |
| 50 |
/** @brief Monotonic count of presented frames, used to detect "still referenced by the in-flight FIFO" resources */ |
| 51 |
static std::uint32_t GetFrameCounter() { |
| 52 |
return _frameCounter; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
@brief Returns the generated fixed-function effect of a (program, variant) key, or `nullptr` |
| 57 |
|
| 58 |
Scans the table transpiled from the shaders' `fixed_function` blocks |
| 59 |
(`Shaders/Generated/GxGeneratedEffects.h`). Called once per program load from |
| 60 |
@ref GxShaderProgram::SetObjectLabel(), which maps the object label onto the key |
| 61 |
through its exact-name table - the draw path only ever reads the stored pointer. |
| 62 |
*/ |
| 63 |
static const FixedFunctionGeneratedEffect* FindGeneratedEffect(const char* program, const char* variant); |
| 64 |
|
| 65 |
GxDevice() = delete; |
| 66 |
~GxDevice() = delete; |
| 67 |
|
| 68 |
struct ScissorState |
| 69 |
{ |
| 70 |
bool Enabled = false; |
| 71 |
Recti Rect = Recti(0, 0, 0, 0); |
| 72 |
}; |
| 73 |
|
| 74 |
struct BlendingState |
| 75 |
{ |
| 76 |
bool Enabled = false; |
| 77 |
nCine::BlendingFactor SrcRgb = nCine::BlendingFactor::One; |
| 78 |
nCine::BlendingFactor DstRgb = nCine::BlendingFactor::Zero; |
| 79 |
nCine::BlendingFactor SrcAlpha = nCine::BlendingFactor::One; |
| 80 |
nCine::BlendingFactor DstAlpha = nCine::BlendingFactor::Zero; |
| 81 |
}; |
| 82 |
|
| 83 |
struct DepthTestState |
| 84 |
{ |
| 85 |
bool TestEnabled = false; |
| 86 |
bool MaskEnabled = true; |
| 87 |
}; |
| 88 |
|
| 89 |
struct CullFaceState |
| 90 |
{ |
| 91 |
bool Enabled = false; |
| 92 |
CullFaceMode Mode = CullFaceMode::Back; |
| 93 |
}; |
| 94 |
|
| 95 |
static void SetBlendingEnabled(bool enabled); |
| 96 |
static void SetBlendingFactors(nCine::BlendingFactor srcRgb, nCine::BlendingFactor dstRgb, nCine::BlendingFactor srcAlpha, nCine::BlendingFactor dstAlpha); |
| 97 |
static BlendingState GetBlendingState(); |
| 98 |
static void SetBlendingState(const BlendingState& state); |
| 99 |
|
| 100 |
static void SetDepthTestEnabled(bool enabled); |
| 101 |
static void SetDepthMaskEnabled(bool enabled); |
| 102 |
static DepthTestState GetDepthTestState(); |
| 103 |
static void SetDepthTestState(const DepthTestState& state); |
| 104 |
|
| 105 |
static void SetCullFaceEnabled(bool enabled); |
| 106 |
static CullFaceState GetCullFaceState(); |
| 107 |
static void SetCullFaceState(const CullFaceState& state); |
| 108 |
|
| 109 |
static ScissorState GetScissorState(); |
| 110 |
static void SetScissorState(const ScissorState& state); |
| 111 |
static void SetScissor(const Recti& rect); |
| 112 |
static void SetScissorTestEnabled(bool enabled); |
| 113 |
|
| 114 |
static Recti GetViewport(); |
| 115 |
static void SetViewport(const Recti& rect); |
| 116 |
static void InitViewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height); |
| 117 |
|
| 118 |
static Colorf GetClearColor(); |
| 119 |
static void SetClearColor(const Colorf& color); |
| 120 |
static void Clear(ClearFlags flags); |
| 121 |
|
| 122 |
static void DrawArrays(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices); |
| 123 |
static void DrawArraysInstanced(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices, std::int32_t numInstances); |
| 124 |
static void DrawElements(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t baseVertex); |
| 125 |
static inline void DrawElements(PrimitiveType primitive, std::uint32_t numIndices, std::uintptr_t indexOffset, std::int32_t baseVertex) { |
| 126 |
DrawElements(primitive, numIndices, IndexFormat::UInt16, indexOffset, baseVertex); |
| 127 |
} |
| 128 |
static void DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex); |
| 129 |
static inline void DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex) { |
| 130 |
DrawElementsInstanced(primitive, numIndices, IndexFormat::UInt16, indexOffset, numInstances, baseVertex); |
| 131 |
} |
| 132 |
|
| 133 |
static FenceHandle InsertFence(); |
| 134 |
static void DeleteFence(FenceHandle& fence); |
| 135 |
static bool ClientWaitFence(FenceHandle fence, std::uint64_t timeoutNs); |
| 136 |
|
| 137 |
static void SetupInitialState(); |
| 138 |
|
| 139 |
// -- Swap-chain / presentation surface (uniform across every backend) -- |
| 140 |
// The Ogc window backend owns VIDEO/XFB and drives the GX session through InitializeGx() and |
| 141 |
// PresentToXfb() below, so the neutral quartet is inert here (mirrors the software backend). |
| 142 |
|
| 143 |
/** @brief No-op (the window backend owns the presentation path) */ |
| 144 |
static inline bool CreateSwapchain(void* windowHandle, std::int32_t width, std::int32_t height, bool vsync) { |
| 145 |
static_cast<void>(windowHandle); |
| 146 |
static_cast<void>(width); |
| 147 |
static_cast<void>(height); |
| 148 |
static_cast<void>(vsync); |
| 149 |
return true; |
| 150 |
} |
| 151 |
/** @brief No-op */ |
| 152 |
static inline void DestroySwapchain() {} |
| 153 |
/** @brief No-op (the logical resolution is driven by ResizeScreenFramebuffer() from the render pipeline) */ |
| 154 |
static inline void ResizeSwapchain(std::int32_t width, std::int32_t height) { |
| 155 |
static_cast<void>(width); |
| 156 |
static_cast<void>(height); |
| 157 |
} |
| 158 |
/** @brief No-op (the window backend calls PresentToXfb() itself) */ |
| 159 |
static inline void PresentFrame() {} |
| 160 |
|
| 161 |
/** @brief Returns the maximum supported texture dimension (drives the tileset chunking) */ |
| 162 |
static inline std::int32_t GetMaxTextureDimension() { |
| 163 |
return 1024; |
| 164 |
} |
| 165 |
|
| 166 |
// -- Direct-tier presentation contract (see RhiFwd.h) -- |
| 167 |
|
| 168 |
/** |
| 169 |
@brief Sets the logical resolution the scene is rendered at |
| 170 |
|
| 171 |
Called by the render pipeline (UpscaleRenderPass) on the direct tier. Draw coordinates arrive in |
| 172 |
this logical space; the orthographic projection maps it onto the full EFB, whose display copy the |
| 173 |
video hardware scales to the TV - the logical-to-display upscale costs nothing. |
| 174 |
*/ |
| 175 |
static void ResizeScreenFramebuffer(std::int32_t width, std::int32_t height); |
| 176 |
|
| 177 |
// -- GX session, driven by the Ogc window backend -- |
| 178 |
|
| 179 |
/** @brief Brings up the GX pipe (FIFO, vertex descriptors, TEV baseline) for the given render mode */ |
| 180 |
static void InitializeGx(GXRModeObj* rmode); |
| 181 |
/** @brief Drains the GX pipe and releases the FIFO, so no GP work is left pending at exit */ |
| 182 |
static void ShutdownGx(); |
| 183 |
/** @brief Finishes the frame's draws, copies the EFB to the given external framebuffer and flushes */ |
| 184 |
static void PresentToXfb(void* xfb); |
| 185 |
|
| 186 |
// -- GX backend extensions (called by the resource types and read by the draw dispatch) -- |
| 187 |
|
| 188 |
/** @brief Records the currently bound shader program */ |
| 189 |
static void BindProgram(GxShaderProgram* program); |
| 190 |
/** @brief Returns the currently bound shader program */ |
| 191 |
static GxShaderProgram* CurrentProgram(); |
| 192 |
/** @brief Records the texture bound to a texture unit */ |
| 193 |
static void BindTexture(std::uint32_t unit, const GxTexture* texture); |
| 194 |
/** @brief Clears a texture from every unit it is bound to (called from ~GxTexture) */ |
| 195 |
static void UnbindTexture(const GxTexture* texture); |
| 196 |
/** @brief Returns the texture bound to a texture unit */ |
| 197 |
static const GxTexture* GetBoundTexture(std::uint32_t unit); |
| 198 |
/** @brief Records the host data range bound to a uniform binding point */ |
| 199 |
static void BindUniformRange(std::uint32_t index, const std::uint8_t* data, std::uint32_t size); |
| 200 |
/** @brief Records the current draw render target (draws render to the EFB and are copied out on switch) */ |
| 201 |
static void SetRenderTarget(GxRenderTarget* renderTarget); |
| 202 |
/** @brief Clears a render target from the device if it is the current one (called from ~GxRenderTarget) */ |
| 203 |
static void UnbindRenderTarget(const GxRenderTarget* renderTarget); |
| 204 |
|
| 205 |
/** @brief Registers the intercepted shared palette texture (rows become TLUTs) */ |
| 206 |
static void RegisterPaletteTexture(GxTexture* texture); |
| 207 |
/** @brief Invalidates the TLUTs (and RG8 bakes) of the given palette rows after an upload */ |
| 208 |
static void NotifyPaletteTextureChanged(GxTexture* texture, std::int32_t firstRow, std::int32_t rowCount); |
| 209 |
|
| 210 |
/** |
| 211 |
@brief Queues the CPU lightmap/water combine for the next `Combine` draw |
| 212 |
|
| 213 |
The direct-tier lighting contract shared with the software backend (see CombineRenderer): the GX |
| 214 |
device consumes each entry by drawing the lightmap as a multiply quad plus the water tint bands. |
| 215 |
*/ |
| 216 |
static void SetPendingSoftwareLighting(const float* lightmap, std::int32_t lmW, std::int32_t lmH, std::int32_t scale, |
| 217 |
std::int32_t vpX, std::int32_t vpY, std::int32_t vpW, std::int32_t vpH, float ambR, float ambG, float ambB, |
| 218 |
bool waterActive = false, float waterLevelPx = 0.0f, float waterTime = 0.0f, float waterCamY = 0.0f); |
| 219 |
/** @brief Drops any lighting entries not consumed this frame (called by the window backend at present) */ |
| 220 |
static void EndFrame(); |
| 221 |
|
| 222 |
private: |
| 223 |
static constexpr std::uint32_t MaxTextureUnits = 8; |
| 224 |
static constexpr std::uint32_t MaxUniformBindings = 8; |
| 225 |
// GX_TLUT0..15 are the 16 low-bank 256-entry TLUT slots; palette rows are mapped onto them LRU |
| 226 |
static constexpr std::uint32_t MaxTlutSlots = 16; |
| 227 |
|
| 228 |
struct UniformRange |
| 229 |
{ |
| 230 |
const std::uint8_t* Data = nullptr; |
| 231 |
std::uint32_t Size = 0; |
| 232 |
}; |
| 233 |
|
| 234 |
struct PendingSoftwareLight |
| 235 |
{ |
| 236 |
const float* Lightmap = nullptr; |
| 237 |
std::int32_t LmW = 0, LmH = 0, Scale = 1; |
| 238 |
std::int32_t VpX = 0, VpY = 0, VpW = 0, VpH = 0; |
| 239 |
float AmbR = 0.0f, AmbG = 0.0f, AmbB = 0.0f; |
| 240 |
bool WaterActive = false; |
| 241 |
float WaterLevelPx = 0.0f, WaterTime = 0.0f, WaterCamY = 0.0f; |
| 242 |
}; |
| 243 |
|
| 244 |
struct TlutSlot |
| 245 |
{ |
| 246 |
std::int32_t PaletteOffset = -1; |
| 247 |
const GxTexture* Palette = nullptr; |
| 248 |
std::uint32_t PaletteVersion = 0; |
| 249 |
std::uint32_t LastUse = 0; |
| 250 |
std::uint16_t* Data = nullptr; // 256 RGB5A3 entries, 32-byte aligned |
| 251 |
}; |
| 252 |
|
| 253 |
static BlendingState _blending; |
| 254 |
static DepthTestState _depthTest; |
| 255 |
static CullFaceState _cullFace; |
| 256 |
static ScissorState _scissor; |
| 257 |
static Recti _viewport; |
| 258 |
static Colorf _clearColor; |
| 259 |
|
| 260 |
static GxShaderProgram* _currentProgram; |
| 261 |
static const GxTexture* _boundTextures[MaxTextureUnits]; |
| 262 |
static UniformRange _boundUniformRanges[MaxUniformBindings]; |
| 263 |
static GxRenderTarget* _currentRenderTarget; |
| 264 |
|
| 265 |
static GXRModeObj* _rmode; |
| 266 |
static void* _gxFifo; |
| 267 |
static bool _gxInitialized; |
| 268 |
static std::int32_t _logicalWidth; |
| 269 |
static std::int32_t _logicalHeight; |
| 270 |
|
| 271 |
static GxTexture* _paletteTexture; |
| 272 |
static std::uint32_t _paletteGeneration; |
| 273 |
static TlutSlot _tlutSlots[MaxTlutSlots]; |
| 274 |
static std::uint32_t _tlutUseCounter; |
| 275 |
static std::uint32_t _frameCounter; |
| 276 |
|
| 277 |
static std::vector<PendingSoftwareLight> _pendingSoftwareLights; |
| 278 |
|
| 279 |
// Lightmap combine texture (rebuilt per Combine draw from the queued float lightmap) |
| 280 |
static std::uint8_t* _lightmapStore; |
| 281 |
static std::size_t _lightmapStoreSize; |
| 282 |
static GXTexObj _lightmapTexObj; |
| 283 |
// Linear staging buffer the lightmap texels are combined into before tiling; persists across |
| 284 |
// frames and grows monotonically, because reallocating (and zero-filling) it per lit frame is |
| 285 |
// wasted work - every byte is overwritten anyway |
| 286 |
static std::uint8_t* _lightmapLinear; |
| 287 |
static std::size_t _lightmapLinearSize; |
| 288 |
|
| 289 |
static void Dispatch(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices); |
| 290 |
static void DispatchTileMesh(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices); |
| 291 |
// Draws a vertex-fed textured line strip (the weapon wheel) through the GP's native line primitive |
| 292 |
static void DispatchLineStrip(std::int32_t firstVertex, std::int32_t numVertices); |
| 293 |
static void ApplyPendingSoftwareLighting(); |
| 294 |
static void ApplyRenderState(); |
| 295 |
static void ApplyProjection(); |
| 296 |
static void FlushCurrentRenderTarget(); |
| 297 |
static std::int32_t AcquireTlutForRow(const GxTexture* palette, std::int32_t paletteRow); |
| 298 |
}; |
| 299 |
} |