Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
LightStore.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
18#pragma once
19
20#include "../rhi/GfxEnums.hpp"
21#include "./LightConstants.hpp"
22
23#include <algorithm>
24#include <bit>
25#include <cmath>
26#include <glm/glm.hpp>
27
28namespace esengine {
29
30class GfxDevice;
31
37public:
39 void setDevice(GfxDevice* device) { device_ = device; }
40
43 void clear() {
44 data_ = LightConstants{};
45 count_ = 0;
46 tiledCount_ = 0;
47 loopMarked_ = false;
48 loopTileBits_ = 0;
49 loopTileBits3D_ = 0;
50 hasEnvironment_ = false;
51 dirty_ = true;
52 }
53
56 void addAmbient(const glm::vec3& color) {
57 data_.ambient.x += color.x;
58 data_.ambient.y += color.y;
59 data_.ambient.z += color.z;
60 dirty_ = true;
61 }
62
65 void addAmbientGround(const glm::vec3& delta) {
66 data_.ambientGround.x += delta.x;
67 data_.ambientGround.y += delta.y;
68 data_.ambientGround.z += delta.z;
69 dirty_ = true;
70 }
71
74 void addLight(const GpuLight& light) {
75 if (count_ >= MAX_LIGHTS) return;
76 data_.lights[count_++] = light;
77 data_.ambient.w = static_cast<f32>(count_);
78 dirty_ = true;
79 }
80
83 bool addTiledLight(const GpuLight& light, const glm::mat4& viewProjection) {
84 if (tiledCount_ >= MAX_TILED_LIGHTS) return false;
85 data_.tiledLights[tiledCount_] = light;
86 markTiledLight(tiledCount_, viewProjection);
87 ++tiledCount_;
88 data_.tileInfo = glm::vec4(static_cast<f32>(tiledCount_), static_cast<f32>(TILE_COLUMNS), static_cast<f32>(TILE_ROWS), data_.tileInfo.w);
89 dirty_ = true;
90 return true;
91 }
92
95 void setTiledView(const glm::mat4& viewProjection) {
96 if (tiledCount_ == 0 && !loopMarked_) return;
97 std::fill_n(data_.tileMask, TILE_COLUMNS * TILE_ROWS, glm::uvec4(0u));
98 for (u32 i = 0; i < tiledCount_; ++i) markTiledLight(i, viewProjection);
99 if (loopMarked_) markLoopLights(viewProjection);
100 dirty_ = true;
101 }
102
119 void markLoopLights(const glm::mat4& viewProjection) {
120 auto* masks = reinterpret_cast<u32(*)[4]>(data_.tileMask);
121 for (u32 t = 0; t < TILE_COLUMNS * TILE_ROWS; ++t) masks[t][3] = 0u;
122 const bool flatView = viewProjection[2][0] == 0.0f && viewProjection[2][1] == 0.0f
123 && viewProjection[0][3] == 0.0f && viewProjection[1][3] == 0.0f && viewProjection[2][3] == 0.0f;
124 for (u32 i = 0; i < count_; ++i) {
125 const GpuLight& light = data_.lights[i];
126 const bool directional = light.posDir.z >= 0.5f && light.posDir.z < 1.5f;
127 // In space, first: a directional light reaches every tile, a dark slot none, a point or a spot its sphere.
128 if (directional) {
129 for (u32 t = 0; t < TILE_COLUMNS * TILE_ROWS; ++t) masks[t][3] |= 1u << (16u + i);
130 } else if (light.color.a != 0.0f) {
131 const f32 radius = std::max(light.posDir.w, 0.0001f) * 1.001f + 0.001f;
132 markLightTiles(viewProjection, glm::vec3(light.posDir.x, light.posDir.y, light.shadow.z), radius, 96u + 16u + i, masks);
133 }
134 if (!flatView || directional) {
135 for (u32 t = 0; t < TILE_COLUMNS * TILE_ROWS; ++t) masks[t][3] |= 1u << i;
136 continue;
137 }
138 if (light.color.a == 0.0f) continue;
139 // A hair wide, as lightReachMask2D is: the fragment's tile comes from an interpolated position.
140 const f32 reach = std::max(light.posDir.w, 0.0001f) * 1.001f + 0.001f;
141 markLightTiles(viewProjection, glm::vec3(light.posDir.x, light.posDir.y, 0.0f), reach, 96u + i, masks);
142 }
143 data_.tileView = viewProjection;
144 data_.tileInfo.y = static_cast<f32>(TILE_COLUMNS);
145 data_.tileInfo.z = static_cast<f32>(TILE_ROWS);
146 data_.tileInfo.w = 1.0f;
147 loopTileBits_ = 0;
148 loopTileBits3D_ = 0;
149 for (u32 t = 0; t < TILE_COLUMNS * TILE_ROWS; ++t) {
150 loopTileBits_ += static_cast<u32>(std::popcount(masks[t][3] & 0xFFFFu));
151 loopTileBits3D_ += static_cast<u32>(std::popcount(masks[t][3] >> 16));
152 }
153 loopMarked_ = true;
154 dirty_ = true;
155 }
156
157 u32 tiledCount() const { return tiledCount_; }
158
161 u32 loopTileBits() const { return loopTileBits_; }
163 u32 loopTileBits3D() const { return loopTileBits3D_; }
164
168 void setShadowTiles(const glm::mat4* matrices, const glm::vec4* tiles, u32 count,
169 const glm::vec4& params) {
170 for (u32 i = 0; i < MAX_SHADOW_TILES; ++i) {
171 data_.shadowMatrix[i] = i < count ? matrices[i] : glm::mat4(1.0f);
172 data_.shadowTile[i] = i < count ? tiles[i] : glm::vec4(0.0f);
173 }
174 data_.shadowParams = params;
175 dirty_ = true;
176 }
177
181
182 void setLightCloudTile(u32 slot, u32 tile) {
183 if (slot >= count_) return;
184 data_.lights[slot].spot.x = static_cast<f32>(tile + 1);
185 dirty_ = true;
186 }
187
188 void setLightShadowTiles(u32 slot, u32 first, u32 tiles) {
189 if (slot >= count_) return;
190 // The two the atlas decides, and nothing else: z is the light's own source size,
191 // written when it was collected and not this pass's to overwrite.
192 // The layer range this light carries above the count is not the atlas's to overwrite either.
193 const f32 range = std::floor(data_.lights[slot].shadowMap.y / LIGHT_TILE_BITS);
194 data_.lights[slot].shadowMap.x = static_cast<f32>(first);
195 data_.lights[slot].shadowMap.y = static_cast<f32>(tiles) + LIGHT_TILE_BITS * range;
196 dirty_ = true;
197 }
198
203 bool setEnvironment(const glm::vec3* irradiance, const glm::vec4& params,
204 const glm::vec3& tint, f32 yaw) {
205 if (hasEnvironment_) return false;
206 for (usize i = 0; i < 9; ++i) data_.envIrradiance[i] = glm::vec4(irradiance[i], 0.0f);
207 data_.envParams = params;
208 data_.envTint = glm::vec4(tint, yaw);
209 hasEnvironment_ = true;
210 dirty_ = true;
211 return true;
212 }
213
214 bool hasEnvironment() const { return hasEnvironment_; }
215
224 void setReflections(const glm::vec4& params, const glm::vec4* boxes, u32 count) {
225 data_.envParams = params;
226 data_.reflParams.x = static_cast<f32>(count) + 1.0f;
227 for (u32 i = 0; i < count * 2 && i < 2 * MAX_REFLECTION_PROBES; ++i) {
228 data_.reflBox[i] = boxes[i];
229 }
230 dirty_ = true;
231 }
232
234 void setReflectionTone(f32 scale, f32 saturation) {
235 data_.reflParams.y = scale;
236 data_.reflParams.z = saturation;
237 dirty_ = true;
238 }
239
241
248 void isolate(bool direct) {
249 if (direct) {
250 data_.ambient = glm::vec4(0.0f, 0.0f, 0.0f, data_.ambient.w);
251 data_.ambientGround = glm::vec4(0.0f);
252 for (auto& c : data_.envIrradiance) c = glm::vec4(0.0f);
253 data_.envTint = glm::vec4(0.0f);
254 data_.reflParams.y = 0.0f;
255 data_.bakedTint = glm::vec4(0.0f);
256 } else {
257 count_ = 0;
258 tiledCount_ = 0;
259 data_.ambient.w = 0.0f;
260 }
261 dirty_ = true;
262 }
263
264 void setBakedTint(const glm::vec3& tint) {
265 data_.bakedTint = glm::vec4(tint, 1.0f);
266 dirty_ = true;
267 }
268
272 void setLightShadow2DChannel(u32 slot, u32 channel) {
273 if (slot >= count_ || channel >= MAX_SHADOW_2D_LIGHTS) return;
274 data_.lights[slot].shadowMap.w = static_cast<f32>(channel);
275 dirty_ = true;
276 }
277
279 void setTiledLightShadow2DChannel(u32 index, u32 channel) {
280 if (index >= tiledCount_ || channel >= MAX_SHADOW_2D_LIGHTS) return;
281 data_.tiledLights[index].shadowMap.w = static_cast<f32>(channel);
282 dirty_ = true;
283 }
284
287 void setLightShape2DChannel(u32 slot, u32 channel) {
288 if (slot >= count_ || channel >= MAX_SHAPE_2D_LIGHTS) return;
289 data_.lights[slot].falloff.w = static_cast<f32>(channel);
290 dirty_ = true;
291 }
292
296 void setMask2DRect(const glm::vec4& rect) {
297 data_.mask2DRect = rect;
298 dirty_ = true;
299 }
300
301 void setWaterSun(const glm::vec4& rect, const glm::vec4& depth, const glm::vec4& sun) {
302 if (data_.waterRect == rect && data_.waterDepth == depth && data_.waterSun == sun) return;
303 data_.waterRect = rect;
304 data_.waterDepth = depth;
305 data_.waterSun = sun;
306 dirty_ = true;
307 }
308
311 void uploadAndBind();
312
314 void free();
315
316 u32 count() const { return count_; }
317 const LightConstants& data() const { return data_; }
318
319private:
320 void markTiledLight(u32 index, const glm::mat4& viewProjection) {
321 const auto& light = data_.tiledLights[index];
322 const glm::vec3 position(light.posDir.x, light.posDir.y, light.shadow.z);
323 markLightTiles(viewProjection, position, std::max(light.posDir.w, 0.0f), index,
324 reinterpret_cast<u32(*)[4]>(data_.tileMask));
325 }
326
327 LightConstants data_{};
328 u32 count_ = 0;
329 u32 tiledCount_ = 0;
330 bool loopMarked_ = false;
331 u32 loopTileBits_ = 0;
332 u32 loopTileBits3D_ = 0;
333 bool hasEnvironment_ = false;
335 bool dirty_ = true;
336 GfxDevice* device_ = nullptr;
337};
338
339} // namespace esengine
Backend-agnostic graphics enumerations.
Per-frame 2D lighting constants — the third tier of the engine's constant-buffer layering (after per-...
Abstract graphics device interface.
Definition GfxDevice.hpp:75
Holds the per-frame 2D lights and their GPU UBO. Mirrors MaterialStore's UBO lifecycle (lazy create,...
void free()
Frees the GPU UBO. Call while the device is still valid (RenderContext::shutdown).
void addLight(const GpuLight &light)
void setTiledLightShadow2DChannel(u32 index, u32 channel)
The same for the index'th light of the tiled list, the ones past the loop's sixteen.
void setBakedTint(const glm::vec3 &tint)
void addAmbientGround(const glm::vec3 &delta)
void setShadowTiles(const glm::mat4 *matrices, const glm::vec4 *tiles, u32 count, const glm::vec4 &params)
void setReflections(const glm::vec4 &params, const glm::vec4 *boxes, u32 count)
Points the frame at a BAKED atlas: its format, and a box per column.
void setDevice(GfxDevice *device)
The render path uses this device to create/upload/delete the lighting UBO.
bool hasEnvironment() const
void setLightShadowTiles(u32 slot, u32 first, u32 tiles)
u32 loopTileBits3D() const
The tiles the loop's lights reach a 3D surface on, summed over lights (the masks' upper half).
void setLightShape2DChannel(u32 slot, u32 channel)
void setTiledView(const glm::mat4 &viewProjection)
bool addTiledLight(const GpuLight &light, const glm::mat4 &viewProjection)
void addAmbient(const glm::vec3 &color)
const LightConstants & data() const
u32 loopTileBits() const
void setWaterSun(const glm::vec4 &rect, const glm::vec4 &depth, const glm::vec4 &sun)
void setReflectionTone(f32 scale, f32 saturation)
How baked reflection columns are shown this frame: their brightness and the colour they keep.
void setLightCloudTile(u32 slot, u32 tile)
Says the directional light in slot reads the sky's cloud shadow from atlas tile tile (GpuLight::spot....
bool setEnvironment(const glm::vec3 *irradiance, const glm::vec4 &params, const glm::vec3 &tint, f32 yaw)
void setMask2DRect(const glm::vec4 &rect)
void isolate(bool direct)
What baked indirect light is multiplied by this frame: the day's or the weather's say on a bake of on...
void markLoopLights(const glm::mat4 &viewProjection)
Says, per screen tile, which of the LOOP's sixteen lights can reach a flat lit surface there: the mas...
void setLightShadow2DChannel(u32 slot, u32 channel)
constexpr u32 MAX_LIGHTS
Max simultaneous 2D lights packed into the UBO. The injected fragment loop is a fixed bound; inactive...
constexpr u32 MAX_SHADOW_TILES
Max shadow tiles one frame's atlas hands out — the shader's array bound.
void markLightTiles(const glm::mat4 &viewProjection, const glm::vec3 &center, f32 radius, u32 index, u32(*masks)[4])
constexpr u32 MAX_SHADOW_2D_LIGHTS
Channels of the 2D shadow mask: the lights that MEET one another and cast at once (see ShadowChannels...
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
constexpr u32 MAX_SHAPE_2D_LIGHTS
Lights whose SHAPE one frame can carry — the shape mask's four channels.
constexpr f32 LIGHT_TILE_BITS
The sprite layers a light reaches ride in GpuLight::shadowMap.y above the atlas tile count: tiles + 8...
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
constexpr u32 MAX_REFLECTION_PROBES
Columns of the reflection atlas one frame can address — column 0 the environment's,...
constexpr u32 MAX_TILED_LIGHTS
Lights a frame can carry through tiles, past the sixteen the loop reads directly. Three words of mask...
constexpr u32 TILE_ROWS
constexpr u32 TILE_COLUMNS
float f32
32-bit floating point
Definition Types.hpp:59
One 2D light, std140-packed (six vec4s, 96 bytes, 16-aligned).
glm::vec4 color
rgb = colour, a = intensity.
CPU mirror of the GLSL LightConstants block (std140).
GpuLight tiledLights[MAX_TILED_LIGHTS]
The lights past the loop's sixteen (point and spot, no shadow map), read through the tile masks below...
glm::uvec4 tileMask[TILE_COLUMNS *TILE_ROWS]