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