| 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 |
namespace nCine::RHI::Software |
| 11 |
{ |
| 12 |
class SwShaderProgram; |
| 13 |
class SwRenderTarget; |
| 14 |
class SwTexture; |
| 15 |
|
| 16 |
/** |
| 17 |
@brief Destination framebuffer the device presents and resolves draws into |
| 18 |
|
| 19 |
A caller-owned, tightly- or loosely-packed RGBA8 surface. The bytes are laid out as |
| 20 |
`[R, G, B, A]` per texel, rows are stored top to bottom and @ref strideBytes is the byte |
| 21 |
distance between two consecutive rows (allowing padded rows or rendering into a sub-window of a |
| 22 |
larger surface). The device never allocates or frees this memory (except the screen back-buffer it |
| 23 |
owns through @ref SwDevice::ResizeScreenFramebuffer()). With the optional `RHI_USE_FB16` mode |
| 24 |
the screen back-buffer instead stores native-endian RGB565 (2 bytes per texel, `strideBytes` follows); |
| 25 |
render-target surfaces stay RGBA8 - see the staging helpers in SwRaster.h. |
| 26 |
*/ |
| 27 |
struct Framebuffer |
| 28 |
{ |
| 29 |
/** @brief Pointer to the first byte (top-left texel) of the surface */ |
| 30 |
std::uint8_t* pixels = nullptr; |
| 31 |
/** @brief Width of the surface in pixels */ |
| 32 |
std::int32_t width = 0; |
| 33 |
/** @brief Height of the surface in pixels */ |
| 34 |
std::int32_t height = 0; |
| 35 |
/** @brief Byte distance between two consecutive rows (usually `width * 4`) */ |
| 36 |
std::int32_t strideBytes = 0; |
| 37 |
}; |
| 38 |
|
| 39 |
/** |
| 40 |
@brief Pipeline-state and draw-call facade of the software backend (aliased as `RHI::Device`) |
| 41 |
|
| 42 |
Mirrors the OpenGL device's surface (blending, depth, cull, scissor, viewport, clear and the draw |
| 43 |
calls) but instead of talking to a GPU it is a small immediate-mode state machine: `Bind*` calls |
| 44 |
record the current program, textures and uniform ranges, `Set*` calls record blend/scissor/viewport |
| 45 |
state, and each `Draw*` call gathers all of it and runs the hand-written C++ effect selected by the |
| 46 |
bound program, rasterizing into the current render target's color texture with @ref SwRaster. |
| 47 |
Depth is a no-op (the renderer is 2D) and fences signal immediately. |
| 48 |
|
| 49 |
The blend factors on this API are the pipeline-neutral `nCine::BlendingFactor` (fully qualified so |
| 50 |
they are never shadowed by the rasterizer's identically-named mirror enum). |
| 51 |
*/ |
| 52 |
class SwDevice |
| 53 |
{ |
| 54 |
public: |
| 55 |
SwDevice() = delete; |
| 56 |
~SwDevice() = delete; |
| 57 |
|
| 58 |
struct ScissorState |
| 59 |
{ |
| 60 |
bool Enabled = false; |
| 61 |
Recti Rect = Recti(0, 0, 0, 0); |
| 62 |
}; |
| 63 |
|
| 64 |
struct BlendingState |
| 65 |
{ |
| 66 |
bool Enabled = false; |
| 67 |
nCine::BlendingFactor SrcRgb = nCine::BlendingFactor::One; |
| 68 |
nCine::BlendingFactor DstRgb = nCine::BlendingFactor::Zero; |
| 69 |
nCine::BlendingFactor SrcAlpha = nCine::BlendingFactor::One; |
| 70 |
nCine::BlendingFactor DstAlpha = nCine::BlendingFactor::Zero; |
| 71 |
}; |
| 72 |
|
| 73 |
struct DepthTestState |
| 74 |
{ |
| 75 |
bool TestEnabled = false; |
| 76 |
bool MaskEnabled = true; |
| 77 |
}; |
| 78 |
|
| 79 |
struct CullFaceState |
| 80 |
{ |
| 81 |
bool Enabled = false; |
| 82 |
CullFaceMode Mode = CullFaceMode::Back; |
| 83 |
}; |
| 84 |
|
| 85 |
static void SetBlendingEnabled(bool enabled); |
| 86 |
static void SetBlendingFactors(nCine::BlendingFactor srcRgb, nCine::BlendingFactor dstRgb, nCine::BlendingFactor srcAlpha, nCine::BlendingFactor dstAlpha); |
| 87 |
static BlendingState GetBlendingState(); |
| 88 |
static void SetBlendingState(const BlendingState& state); |
| 89 |
|
| 90 |
static void SetDepthTestEnabled(bool enabled); |
| 91 |
static void SetDepthMaskEnabled(bool enabled); |
| 92 |
static DepthTestState GetDepthTestState(); |
| 93 |
static void SetDepthTestState(const DepthTestState& state); |
| 94 |
|
| 95 |
static void SetCullFaceEnabled(bool enabled); |
| 96 |
static CullFaceState GetCullFaceState(); |
| 97 |
static void SetCullFaceState(const CullFaceState& state); |
| 98 |
|
| 99 |
static ScissorState GetScissorState(); |
| 100 |
static void SetScissorState(const ScissorState& state); |
| 101 |
static void SetScissor(const Recti& rect); |
| 102 |
static void SetScissorTestEnabled(bool enabled); |
| 103 |
|
| 104 |
static Recti GetViewport(); |
| 105 |
static void SetViewport(const Recti& rect); |
| 106 |
static void InitViewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height); |
| 107 |
|
| 108 |
static Colorf GetClearColor(); |
| 109 |
static void SetClearColor(const Colorf& color); |
| 110 |
static void Clear(ClearFlags flags); |
| 111 |
|
| 112 |
static void DrawArrays(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices); |
| 113 |
static void DrawArraysInstanced(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices, std::int32_t numInstances); |
| 114 |
static void DrawElements(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t baseVertex); |
| 115 |
static inline void DrawElements(PrimitiveType primitive, std::uint32_t numIndices, std::uintptr_t indexOffset, std::int32_t baseVertex) { |
| 116 |
DrawElements(primitive, numIndices, IndexFormat::UInt16, indexOffset, baseVertex); |
| 117 |
} |
| 118 |
static void DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex); |
| 119 |
static inline void DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex) { |
| 120 |
DrawElementsInstanced(primitive, numIndices, IndexFormat::UInt16, indexOffset, numInstances, baseVertex); |
| 121 |
} |
| 122 |
|
| 123 |
static FenceHandle InsertFence(); |
| 124 |
static void DeleteFence(FenceHandle& fence); |
| 125 |
static bool ClientWaitFence(FenceHandle fence, std::uint64_t timeoutNs); |
| 126 |
|
| 127 |
static void SetupInitialState(); |
| 128 |
|
| 129 |
// -- Swap-chain / presentation surface (uniform across every backend) -- |
| 130 |
// The software backend presents through the WINDOW backend's CPU blit (SDL_Renderer streaming |
| 131 |
// texture): the window backend calls @ref FlushSoftwareRenderer() + @ref GetScreenFramebuffer() + |
| 132 |
// @ref EndFrame() itself, so this quartet is inert here. It exists so window backends and shared |
| 133 |
// code can drive any backend through the same calls instead of branching on WITH_RHI_*. |
| 134 |
|
| 135 |
/** @brief No-op (the window backend owns the presentation path) */ |
| 136 |
static inline bool CreateSwapchain(void* windowHandle, std::int32_t width, std::int32_t height, bool vsync) { |
| 137 |
static_cast<void>(windowHandle); |
| 138 |
static_cast<void>(width); |
| 139 |
static_cast<void>(height); |
| 140 |
static_cast<void>(vsync); |
| 141 |
return true; |
| 142 |
} |
| 143 |
/** @brief No-op */ |
| 144 |
static inline void DestroySwapchain() {} |
| 145 |
/** @brief No-op (the CPU screen buffer is sized by @ref ResizeScreenFramebuffer() from the render pipeline, not by the window) */ |
| 146 |
static inline void ResizeSwapchain(std::int32_t width, std::int32_t height) { |
| 147 |
static_cast<void>(width); |
| 148 |
static_cast<void>(height); |
| 149 |
} |
| 150 |
/** @brief No-op (the window backend blits @ref GetScreenFramebuffer() itself) */ |
| 151 |
static inline void PresentFrame() {} |
| 152 |
|
| 153 |
// -- Software backend extensions (called by the resource types and read by the effects) -- |
| 154 |
|
| 155 |
/** @brief Records the currently bound shader program */ |
| 156 |
static void BindProgram(SwShaderProgram* program); |
| 157 |
/** @brief Returns the currently bound shader program */ |
| 158 |
static SwShaderProgram* CurrentProgram(); |
| 159 |
/** @brief Records the texture bound to a texture unit */ |
| 160 |
static void BindTexture(std::uint32_t unit, const SwTexture* texture); |
| 161 |
/** @brief Clears a texture from every unit it is bound to (called from ~SwTexture to avoid a dangling pointer) */ |
| 162 |
static void UnbindTexture(const SwTexture* texture); |
| 163 |
/** @brief Returns the texture bound to a texture unit */ |
| 164 |
static const SwTexture* GetBoundTexture(std::uint32_t unit); |
| 165 |
/** @brief Records the host data range bound to a uniform binding point */ |
| 166 |
static void BindUniformRange(std::uint32_t index, const std::uint8_t* data, std::uint32_t size); |
| 167 |
/** @brief Records the current draw render target (its color attachment 0 receives the pixels) */ |
| 168 |
static void SetRenderTarget(SwRenderTarget* renderTarget); |
| 169 |
/** @brief Clears a render target from the device if it is the current one (called from ~SwRenderTarget) */ |
| 170 |
static void UnbindRenderTarget(const SwRenderTarget* renderTarget); |
| 171 |
/** @brief Sets the framebuffer used when no render target is bound (present/back-buffer path) */ |
| 172 |
static void SetDefaultFramebuffer(const Framebuffer& framebuffer); |
| 173 |
|
| 174 |
/** |
| 175 |
@brief Resizes the owned screen back-buffer to the window size and installs it as the default framebuffer |
| 176 |
|
| 177 |
The root screen viewport binds no render target, so its clears and draws land in the default |
| 178 |
framebuffer. On desktop the window backend calls this once at window creation and on every resize |
| 179 |
to give that framebuffer a CPU surface sized to the drawable, then reads it back each frame with |
| 180 |
@ref GetScreenFramebuffer() to present it (the software counterpart of the GL default framebuffer + |
| 181 |
buffer swap). The backend owns the pixel memory; it stays valid until the next resize. |
| 182 |
*/ |
| 183 |
static void ResizeScreenFramebuffer(std::int32_t width, std::int32_t height); |
| 184 |
/** @brief Returns the owned screen back-buffer (pixels/size/stride) for the window backend to present */ |
| 185 |
static Framebuffer GetScreenFramebuffer(); |
| 186 |
|
| 187 |
/** |
| 188 |
@brief Renders any draws the tile renderer has deferred into the current color buffer |
| 189 |
|
| 190 |
The window backend calls this before reading @ref GetScreenFramebuffer() to present, so every |
| 191 |
queued draw has landed in the screen buffer. Forwards to the rasterizer's deferred-layer flush, |
| 192 |
which waits for all worker threads to finish; a no-op when nothing is queued. |
| 193 |
*/ |
| 194 |
static void FlushSoftwareRenderer(); |
| 195 |
|
| 196 |
/** |
| 197 |
@brief Ends the presented frame, dropping any per-frame device state |
| 198 |
|
| 199 |
The window backend calls this after presenting. Discards software-lighting entries left in the queue |
| 200 |
(their Combine draw was culled or never dispatched, e.g. an off-screen viewport): without this the |
| 201 |
leftovers would survive into later frames, permanently shifting the viewport-to-lightmap pairing in |
| 202 |
splitscreen — and their caller-owned lightmap pointers would dangle past the compositor's frame. |
| 203 |
*/ |
| 204 |
static void EndFrame(); |
| 205 |
|
| 206 |
/** |
| 207 |
@brief Queues a dynamic lightmap to be composited over a viewport during the next matching Combine draw |
| 208 |
|
| 209 |
Software-lighting entry point used by the viewport compositor (@c Jazz2::Rendering::CombineRenderer). The |
| 210 |
scene renders straight to the screen buffer with no shader post-processing, so the dynamic lighting is |
| 211 |
applied on the CPU here instead: the compositor builds the half-resolution lightmap in the Visit |
| 212 |
(queue-building) phase and calls this, then the Combine draw it queues is intercepted in @ref Dispatch() |
| 213 |
during the later Draw phase - after the scene has been rasterized into the screen buffer and before the |
| 214 |
HUD - where the map is blended in place over the viewport rectangle. Entries are consumed |
| 215 |
first-in-first-out, one per Combine draw, so splitscreen viewports each receive their own map in |
| 216 |
submission order. |
| 217 |
|
| 218 |
@param lightmap Half-resolution accumulation buffer, 2 floats per texel (R = intensity incl. the ambient |
| 219 |
base, G = brightness core), row-major. Caller-owned; must stay valid until the matching |
| 220 |
Combine draw is dispatched (the compositor keeps it in a per-viewport member). |
| 221 |
@param lmW Lightmap width in texels |
| 222 |
@param lmH Lightmap height in texels |
| 223 |
@param scale Lightmap-to-screen downscale factor (a screen pixel samples texel `pixel / scale`) |
| 224 |
@param vpX Viewport left in screen-buffer pixels |
| 225 |
@param vpY Viewport top in screen-buffer pixels |
| 226 |
@param vpW Viewport width in screen-buffer pixels (clamped against the buffer at apply time) |
| 227 |
@param vpH Viewport height in screen-buffer pixels (clamped against the buffer at apply time) |
| 228 |
@param ambR Ambient colour red the unlit scene is blended toward |
| 229 |
@param ambG Ambient colour green the unlit scene is blended toward |
| 230 |
@param ambB Ambient colour blue the unlit scene is blended toward |
| 231 |
@param waterActive Whether the waterline is inside this viewport - enables the lightweight per-row |
| 232 |
water effect (the CPU replacement of the CombineWithWater shader variants). With |
| 233 |
water active, @p lightmap may be `nullptr` for a water-only combine (fully lit scene). |
| 234 |
@param waterLevelPx Waterline position in viewport-local pixels from the TOP edge (the shader path's |
| 235 |
`viewWaterLevel`); may be negative when the whole viewport is underwater |
| 236 |
@param waterTime Wave animation time, the shader path's `uTime` (elapsed frames * 0.0018) |
| 237 |
@param waterCamY Camera world-space Y, anchors the per-row wave phase to the world while scrolling |
| 238 |
*/ |
| 239 |
static void SetPendingSoftwareLighting(const float* lightmap, std::int32_t lmW, std::int32_t lmH, std::int32_t scale, |
| 240 |
std::int32_t vpX, std::int32_t vpY, std::int32_t vpW, std::int32_t vpH, float ambR, float ambG, float ambB, |
| 241 |
bool waterActive = false, float waterLevelPx = 0.0f, float waterTime = 0.0f, float waterCamY = 0.0f); |
| 242 |
|
| 243 |
private: |
| 244 |
static constexpr std::uint32_t MaxTextureUnits = 8; |
| 245 |
static constexpr std::uint32_t MaxUniformBindings = 8; |
| 246 |
|
| 247 |
struct UniformRange |
| 248 |
{ |
| 249 |
const std::uint8_t* Data = nullptr; |
| 250 |
std::uint32_t Size = 0; |
| 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 SwShaderProgram* _currentProgram; |
| 261 |
static const SwTexture* _boundTextures[MaxTextureUnits]; |
| 262 |
static UniformRange _boundUniformRanges[MaxUniformBindings]; |
| 263 |
static SwRenderTarget* _currentRenderTarget; |
| 264 |
static std::uint8_t* _defaultFbPixels; |
| 265 |
static std::int32_t _defaultFbWidth; |
| 266 |
static std::int32_t _defaultFbHeight; |
| 267 |
static std::int32_t _defaultFbStride; |
| 268 |
/** @brief Backend-owned pixel store for the screen back-buffer (only used by the present path) */ |
| 269 |
static std::vector<std::uint8_t> _screenPixels; |
| 270 |
|
| 271 |
/** @brief One queued software-lighting/water combine, submitted by the compositor and applied at the next Combine draw */ |
| 272 |
struct PendingSoftwareLight |
| 273 |
{ |
| 274 |
const float* Lightmap = nullptr; |
| 275 |
std::int32_t LmW = 0, LmH = 0, Scale = 1; |
| 276 |
std::int32_t VpX = 0, VpY = 0, VpW = 0, VpH = 0; |
| 277 |
float AmbR = 0.0f, AmbG = 0.0f, AmbB = 0.0f; |
| 278 |
bool WaterActive = false; |
| 279 |
float WaterLevelPx = 0.0f, WaterTime = 0.0f, WaterCamY = 0.0f; |
| 280 |
}; |
| 281 |
/** @brief FIFO of pending software-lighting combines (one per viewport, in submission order) */ |
| 282 |
static std::vector<PendingSoftwareLight> _pendingSoftwareLights; |
| 283 |
|
| 284 |
/** @brief Resolves the color framebuffer that draws and clears write into (RT color 0, else default) */ |
| 285 |
static bool ResolveFramebuffer(Framebuffer& out); |
| 286 |
/** @brief Runs the correct C++ effect for the bound program over the given draw range (@p firstVertex indexes the bound vertex buffer; only the vertex-attribute mesh path consumes it) */ |
| 287 |
static void Dispatch(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices); |
| 288 |
/** @brief Consumes the front queued software combine (lightmap and/or water effect) and blends it in place over its viewport rectangle */ |
| 289 |
static void ApplyPendingSoftwareLighting(); |
| 290 |
}; |
| 291 |
} |