SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
ConstantBuffer.hpp
Go to the documentation of this file.
1#ifndef _CONSTANTBUFFER_H_
2#define _CONSTANTBUFFER_H_
3
4#include <Math/Matrix.hpp>
5#include <Math/Color.hpp>
6#include <cstdint>
7
8namespace Sleak {
9 namespace RenderEngine {
10
11 /// Well-known constant buffer slot identifiers.
12 enum class CBIds {
16 };
17
18 // Per-light GPU entry (64 bytes = 4 x 16-byte rows)
19 /// One light's packed GPU representation, laid out for std140/cbuffer alignment.
20 struct alignas(16) LightGPUEntry {
21 // Row 0: Position.xyz + Type
23 uint32_t Type; // 0=dir, 1=point, 2=spot, 3=area
24
25 // Row 1: Direction.xyz + Intensity
27 float Intensity;
28
29 // Row 2: Color.rgb + Range
31 float Range;
32
33 // Row 3: SpotInnerCos + SpotOuterCos + AreaWidth + AreaHeight
36 float AreaWidth;
38 };
39
40 // Full lighting constant buffer (64 + 64*16 + 32 trailing = 1120 bytes).
41 // The trailing fog gradient + height-fog block is appended AFTER the
42 // Lights[] array so that older shader UBO declarations (which stop at
43 // Lights[]) still read a valid prefix. Don't reorder.
44 static constexpr uint32_t MAX_LIGHTS = 16;
45
46 /// Full per-frame lighting UBO: camera, ambient, fog, and the packed light array.
47 struct alignas(16) LightCBData {
48 // Header Row 0: CameraPos.xyz + NumActiveLights
51
52 // Header Row 1: AmbientColor.rgb + AmbientIntensity
55
56 // Header Row 2: Fog color (HORIZON)
58
59 // Header Row 3: Fog distances + reserved
61 float _reserved[2];
62
63 // Per-light array
65
66 // ---- Trailing sky-matched + height fog block ----
67 float FogColorZenith[4]; // ZENITH color of fog gradient
68 float HeightFogTop; // world Y above which height fog goes to 0
69 float HeightFogDensity; // max density at low elevations [0..1]
70 float HeightFogFalloff; // exponential falloff per world unit
71 float HeightFogEnabled; // 0 = off, 1 = on
72 };
73
74 /// Interface for a CPU-side payload that can be uploaded to a GPU constant buffer.
76 public:
77 virtual void* GetData() const = 0;
78
79 virtual uint16_t GetSize() const = 0;
80 };
81
82 /// Per-draw world-view-projection and world matrices.
83 struct alignas(16) TransformBuffer : public ConstantBuffer {
84
85 /// Composes WVP from world/view/proj and keeps World separately for lighting.
87 const Sleak::Math::Matrix4& view,
88 const Sleak::Math::Matrix4& proj)
89 : WVP(world * view * proj), World(world) {
90 }
91
92 /// Takes a pre-composed WVP with an identity world matrix (e.g. skybox, UI).
94 : WVP(mvp), World(Sleak::Math::Matrix4::Identity()) {
95 }
96
97 virtual void* GetData() const override {
98 return (void*) &WVP;
99 }
100
101 virtual uint16_t GetSize() const override {
102 return sizeof(Sleak::Math::Matrix4) * 2;
103 }
104
105 private:
108 };
109
110 // GPU-aligned POD struct for material constant buffer data.
111 // Total: 128 bytes (8 x 16-byte rows), matches all shader backends.
112 /// Packed PBR material parameters uploaded to the material constant buffer.
113 struct alignas(16) MaterialGPUData {
114 // Row 0: Texture presence flags (16 bytes)
116 uint32_t HasNormalMap;
119
120 // Row 1: More texture flags (16 bytes)
122 uint32_t HasAOMap;
124 uint32_t _pad0;
125
126 // Row 2: Diffuse/Albedo color RGBA (16 bytes)
128
129 // Row 3: Specular color RGB + Shininess (16 bytes)
132
133 // Row 4: Emissive color RGB + Intensity (16 bytes)
136
137 // Row 5: PBR factors (16 bytes)
138 float Metallic;
140 float AO;
142
143 // Row 6: UV transform (16 bytes)
146
147 // Row 7: Alpha properties (16 bytes)
148 float Opacity;
150 float _pad1, _pad2;
151 };
152
153 /// ConstantBuffer wrapper around MaterialGPUData.
154 struct alignas(16) MaterialBuffer : public ConstantBuffer {
155
157
159 : gpuData(data) {}
160
161 virtual void* GetData() const override {
162 return (void*) &gpuData;
163 }
164
165 virtual uint16_t GetSize() const override {
166 return sizeof(MaterialGPUData);
167 }
168
170 };
171
172 // GPU-aligned struct for light + shadow data, bound via UBO (set 2, binding 0)
173 // NOTE: trailing fog/sky fields are appended in fixed order — do not
174 // reorder. Shader UBO declarations may stop short of the full struct
175 // (size mismatch in that direction is fine for std140/cbuffer reads).
176 /// Shadow-pass UBO: light/shadow parameters, fog, and extra fill lights (set 2, binding 0).
177 struct alignas(16) ShadowLightUBO {
178 float LightDir[4]; // xyz + normalBias
179 float LightColor[4]; // rgb + intensity
180 float Ambient[4]; // rgb + intensity
181 float CameraPos[4]; // xyz + sceneClock
182 float LightVP[16]; // mat4 (light view-projection)
186 float LightSize; // world-space light size for PCSS penumbra
187 float FogColor[4]; // rgb + a — HORIZON color of the fog gradient
188 float FogStart; // distance where fog begins
189 float FogEnd; // distance where fog fully obscures
190 float _fogPad[2];
191 float FogColorZenith[4]; // rgb + pad — ZENITH color of the fog gradient
192 float HeightFogTop; // world Y above which height fog density goes to 0
193 float HeightFogDensity; // max density at low elevations [0..1]
194 float HeightFogFalloff; // exponential falloff per world unit (e^-(top-y)*falloff)
195 float HeightFogEnabled; // 0 = off, 1 = on
196
197 // Extra directional lights (fill, rim, etc.) — no shadow casting
198 float ExtraLightDir[3][4]; // xyz + unused (w=0)
199 float ExtraLightColor[3][4]; // rgb + intensity
200 uint32_t NumExtraLights; // 0-3
201 float _extraPad[3];
202
203 // NDC -> shadow clip, composed on CPU (InvViewProj * LightVP).
204 // Shadow coords must use this — a per-fragment round trip
205 // through reconstructed world position shimmers under rotation.
206 float NdcToShadow[16];
207 };
208
209 // GPU-aligned POD struct for post-process settings constant buffer.
210 // Total: 16 bytes (1 x 16-byte row).
211 /// Tonemapping/exposure settings uploaded to the post-process pass.
212 struct alignas(16) PostProcessGPUData {
213 float Exposure; // HDR exposure multiplier (default 1.0)
214 float Gamma; // Gamma correction exponent (default 2.2)
215 uint32_t TonemapEnabled; // 0 = passthrough, 1 = ACES tonemap
216 uint32_t _ppPad0;
217 };
218
219 // GPU-aligned POD struct for SSAO settings constant buffer.
220 // Total: 64 bytes (4 x 16-byte rows).
221 static constexpr uint32_t SSAO_KERNEL_SIZE = 64;
222
223 /// SSAO pass parameters: kernel size, radius/bias/power, and screen/near-far metrics.
224 struct alignas(16) SSAOSettingsGPUData {
225 // Row 0
226 float Radius; // Sample hemisphere radius (default 0.5)
227 float Bias; // Depth bias to prevent self-occlusion (default 0.025)
228 float Power; // AO power exponent (default 2.0)
229 uint32_t KernelSize; // Number of samples (default 64)
230
231 // Row 1
234 uint32_t SSAOEnabled; // 0 = disabled, 1 = enabled
235 uint32_t _ssaoPad0;
236
237 // Row 2: near/far plane for linearizing depth
239 float FarPlane;
241
242 // Row 3: reserved
243 float _ssaoPad3[4];
244 };
245
246 // GPU-aligned POD struct for PCSS shadow settings (used in PBR shader, DX11/OpenGL).
247 // Total: 96 bytes (6 x 16-byte rows).
248 /// PCSS soft-shadow parameters for the DX11/OpenGL PBR shader path.
249 struct alignas(16) PCSSShadowGPUData {
250 // Row 0-3: Light view-projection matrix (64 bytes)
251 float LightVP[16];
252
253 // Row 4: Shadow parameters
258
259 // Row 5: Toggles
260 uint32_t PCSSEnabled;
263
264 // Row 6-9: NDC -> shadow clip (see ShadowLightUBO::NdcToShadow)
265 float NdcToShadow[16];
266 };
267
268 // GPU-aligned POD struct for SSAO composite settings (used in PBR shader).
269 // Total: 16 bytes (1 x 16-byte row).
270 /// Controls whether the forward PBR shader multiplies in the SSAO texture.
271 struct alignas(16) SSAOCompositeGPUData {
272 uint32_t SSAOCompositeEnabled; // 0 = disabled, 1 = multiply AO by SSAO texture
276 };
277
278 // GPU-aligned POD struct for IBL settings constant buffer.
279 // Total: 16 bytes (1 x 16-byte row).
280 /// Image-based lighting toggle, intensity, and prefiltered-map LOD range.
281 struct alignas(16) IBLSettingsGPUData {
282 uint32_t IBLEnabled; // 0 = disabled, 1 = enabled
283 float IBLIntensity; // multiplier for IBL contribution (default 1.0)
284 float MaxReflectionLOD; // max mip level for prefiltered env map (default 4.0)
285 uint32_t _iblPad0;
286 };
287
288 /// ConstantBuffer wrapper around LightCBData.
289 struct alignas(16) LightBuffer : public ConstantBuffer {
292 : gpuData(data) {}
293
294 virtual void* GetData() const override {
295 return (void*)&gpuData;
296 }
297
298 virtual uint16_t GetSize() const override {
299 return sizeof(LightCBData);
300 }
301
303 };
304
305 // Per-frame CB for the deferred lighting pass (bound at slot 3).
306 // Provides inverse view-projection for world-position reconstruction from depth.
307 // Total: 80 bytes (5 x 16-byte rows).
308 /// Per-frame deferred lighting pass CB: inverse view-projection plus screen/near-far metrics.
309 struct alignas(16) DeferredCBData {
310 // Rows 0-3: Inverse view-projection matrix (64 bytes)
311 float InvViewProj[16];
312
313 // Row 4: Screen size + near/far planes
317 float FarPlane;
318 };
319
320 /// ConstantBuffer wrapper around DeferredCBData.
321 struct alignas(16) DeferredBuffer : public ConstantBuffer {
323 DeferredBuffer(const DeferredCBData& data) : gpuData(data) {}
324
325 virtual void* GetData() const override { return (void*)&gpuData; }
326 virtual uint16_t GetSize() const override { return sizeof(DeferredCBData); }
327
329 };
330 }
331}
332
333#endif
Vectors, matrices, quaternions, colors, AABBs, and random helpers.
Matrix< float, 4, 4 > Matrix4
Definition Matrix.hpp:413
Backend-facing rendering layer shared by the four graphics backends.
static constexpr uint32_t SSAO_KERNEL_SIZE
static constexpr uint32_t MAX_LIGHTS
CBIds
Well-known constant buffer slot identifiers.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
Interface for a CPU-side payload that can be uploaded to a GPU constant buffer.
virtual uint16_t GetSize() const =0
virtual void * GetData() const =0
virtual uint16_t GetSize() const override
DeferredBuffer(const DeferredCBData &data)
virtual void * GetData() const override
Per-frame deferred lighting pass CB: inverse view-projection plus screen/near-far metrics.
Image-based lighting toggle, intensity, and prefiltered-map LOD range.
virtual uint16_t GetSize() const override
LightBuffer(const LightCBData &data)
virtual void * GetData() const override
Full per-frame lighting UBO: camera, ambient, fog, and the packed light array.
LightGPUEntry Lights[MAX_LIGHTS]
One light's packed GPU representation, laid out for std140/cbuffer alignment.
MaterialBuffer(const MaterialGPUData &data)
virtual void * GetData() const override
virtual uint16_t GetSize() const override
Packed PBR material parameters uploaded to the material constant buffer.
PCSS soft-shadow parameters for the DX11/OpenGL PBR shader path.
Tonemapping/exposure settings uploaded to the post-process pass.
Controls whether the forward PBR shader multiplies in the SSAO texture.
SSAO pass parameters: kernel size, radius/bias/power, and screen/near-far metrics.
Shadow-pass UBO: light/shadow parameters, fog, and extra fill lights (set 2, binding 0).
TransformBuffer(const Sleak::Math::Matrix4 &world, const Sleak::Math::Matrix4 &view, const Sleak::Math::Matrix4 &proj)
Composes WVP from world/view/proj and keeps World separately for lighting.
TransformBuffer(const Sleak::Math::Matrix4 &mvp)
Takes a pre-composed WVP with an identity world matrix (e.g. skybox, UI).
virtual uint16_t GetSize() const override
virtual void * GetData() const override