Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
LightConstants.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
13#pragma once
14
15#include "../../core/Types.hpp"
17#include "./TiledLights.hpp"
18
19#include <algorithm>
20#include <cmath>
21#include <glm/glm.hpp>
22
23namespace esengine {
24
26inline constexpr u32 LIGHT_CONSTANTS_BINDING = 2;
27
29inline constexpr const char* LIGHT_CONSTANTS_BLOCK = "LightConstants";
30
36inline constexpr u32 MAX_LIGHTS = 16;
37
39inline constexpr u32 MAX_SHAPE_2D_LIGHTS = 4;
40
42inline constexpr u32 SHADOW_2D_MASK_CHANNELS = 4;
43
51inline constexpr u32 MAX_SHADOW_2D_LIGHTS = 8;
52
59inline constexpr u32 MAX_SHADOW_CASCADES = 4;
60
66inline constexpr u32 SHADOW_CUBE_FACES = 6;
67
75inline constexpr u32 MAX_SHADOW_TILES = 16;
76
77inline f32 packShadowBias(f32 texels, f32 slopeFloor) {
78 return std::floor(std::clamp(texels, 0.0f, 16.0f) * 64.0f + 0.5f) + std::clamp(slopeFloor, 0.01f, 0.999f);
79}
80
87inline constexpr u32 MAX_REFLECTION_PROBES = 15;
88
94inline constexpr f32 LIGHT_TILE_BITS = 8.0f;
95
97inline f32 packLightLayerRange(i32 layerMin, i32 layerMax) {
98 const i32 lo = std::clamp(layerMin, -128, 127) + 128;
99 const i32 hi = 127 - std::clamp(layerMax, -128, 127);
100 return static_cast<f32>(lo + 256 * hi);
101}
102
104inline constexpr f32 SPRITE_LAYER_BIAS = 129.0f;
105
111struct GpuLight {
115 glm::vec4 posDir{0.0f};
117 glm::vec4 color{0.0f};
121 glm::vec4 spot{0.0f};
125 glm::vec4 shadow{0.0f};
128 glm::vec4 shadowMap{0.0f, 0.0f, 0.0f, -1.0f};
132 glm::vec4 falloff{0.0f, 1.0f, 1.0f, -1.0f};
133};
134
142 glm::vec4 ambient{0.0f};
147 glm::vec4 mask2DRect{0.0f};
158 glm::vec4 shadowParams{0.0f};
162 glm::vec4 envIrradiance[9]{};
165 glm::vec4 envParams{0.0f};
169 glm::vec4 envTint{0.0f};
174 glm::vec4 reflParams{1.0f, 1.0f, 1.0f, 0.0f};
181 glm::vec4 bakedTint{1.0f};
190 glm::vec4 tileInfo{0.0f};
194 glm::vec4 ambientGround{0.0f};
200 glm::mat4 tileView{1.0f};
201 // Render-relative optical window and depth decoder. A zero span disables it.
202 glm::vec4 waterRect{};
203 glm::vec4 waterDepth{}; // surface datum, top margin, full depth span, slice spacing
204 glm::vec4 waterSun{}; // mean refracted X/Z slopes, selected light slot, reserved
205};
206
215inline constexpr f32 SPRITE_LIGHT_REACH_STEP = 512.0f;
216inline constexpr u32 SPRITE_LIGHT_REACH_BITS = 15;
217
224inline u32 lightReachMask2D(const GpuLight* lights, u32 count, const glm::vec2& lo, const glm::vec2& hi) {
225 u32 mask = 0;
226 const u32 n = std::min(count, SPRITE_LIGHT_REACH_BITS);
227 for (u32 i = 0; i < n; ++i) {
228 const GpuLight& light = lights[i];
229 if (light.color.a == 0.0f) continue;
230 const bool directional = light.posDir.z >= 0.5f && light.posDir.z < 1.5f;
231 if (!directional) {
232 const f32 dx = std::max(std::max(lo.x - light.posDir.x, light.posDir.x - hi.x), 0.0f);
233 const f32 dy = std::max(std::max(lo.y - light.posDir.y, light.posDir.y - hi.y), 0.0f);
234 const f32 reach = std::max(light.posDir.w, 0.0001f) * 1.001f + 0.001f;
235 if (dx * dx + dy * dy >= reach * reach) continue;
236 }
237 mask |= 1u << i;
238 }
239 return mask;
240}
241
243inline f32 packSpriteLightLane(i32 layer, u32 reachMask) {
244 const f32 layerLane = static_cast<f32>(std::clamp(layer, -128, 127)) + SPRITE_LAYER_BIAS;
245 const u32 reach = reachMask & ((1u << SPRITE_LIGHT_REACH_BITS) - 1u);
246 return -(layerLane + SPRITE_LIGHT_REACH_STEP * static_cast<f32>(reach));
247}
248
249static_assert(sizeof(GpuLight) == 96, "GpuLight must be std140-tight (six vec4s)");
250static_assert(sizeof(LightConstants) <= 16384, "the block has to fit the 16 KB every device guarantees a uniform block");
251static_assert(sizeof(LightConstants) == 16 + 96 * MAX_LIGHTS + 16
253 + 16 + 16 * 9 + 16 + 16
254 + 16 + 32 * MAX_REFLECTION_PROBES + 16
255 + 96 * MAX_TILED_LIGHTS + 16 * TILE_COLUMNS * TILE_ROWS + 16 + 16 + 64 + 48,
256 "LightConstants must match the std140 GLSL block layout");
257
258} // namespace esengine
The samplers the engine injects into a shader, and the texture unit each is pinned to.
Which screen tiles a light can reach — the half of tiled forward lighting that is decided on the CPU.
Core type definitions and utilities for ESEngine.
constexpr u32 MAX_LIGHTS
Max simultaneous 2D lights packed into the UBO. The injected fragment loop is a fixed bound; inactive...
constexpr u32 SPRITE_LIGHT_REACH_BITS
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
constexpr u32 MAX_SHADOW_TILES
Max shadow tiles one frame's atlas hands out — the shader's array bound.
f32 packShadowBias(f32 texels, f32 slopeFloor)
constexpr u32 SHADOW_CUBE_FACES
Faces a point light's map is rendered as. Six cones at right angles cover every direction from a poin...
constexpr u32 MAX_SHADOW_2D_LIGHTS
Channels of the 2D shadow mask: the lights that MEET one another and cast at once (see ShadowChannels...
constexpr f32 SPRITE_LIGHT_REACH_STEP
Which of the loop's lights can reach a lit quad rides in the same vertex lane as its layer: -(layer +...
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...
f32 packLightLayerRange(i32 layerMin, i32 layerMax)
The range of sprite layers a light reaches, as the number that rides in shadowMap....
constexpr u32 LIGHT_CONSTANTS_BINDING
Indexed UBO binding point for the per-frame LightConstants block.
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 f32 SPRITE_LAYER_BIAS
What a sprite's layer is written as in the vertex lane the lit fragment reads: the layer plus 129,...
constexpr const char * LIGHT_CONSTANTS_BLOCK
GLSL block name; must match ShaderParser's injected block + Shader::compile lookup.
constexpr u32 MAX_TILED_LIGHTS
Lights a frame can carry through tiles, past the sixteen the loop reads directly. Three words of mask...
u32 lightReachMask2D(const GpuLight *lights, u32 count, const glm::vec2 &lo, const glm::vec2 &hi)
Bit i set when loop light i can put any light on the box lo .. hi (the plane's XY,...
constexpr u32 SHADOW_2D_MASK_CHANNELS
Channels one 2D shadow mask carries: an RGBA8 target has four.
f32 packSpriteLightLane(i32 layer, u32 reachMask)
The lane a lit quad carries: its layer and the lights that reach it (lightReachMask2D).
constexpr u32 TILE_ROWS
constexpr u32 TILE_COLUMNS
float f32
32-bit floating point
Definition Types.hpp:59
constexpr u32 MAX_SHADOW_CASCADES
Slices one directional shadow map is split into, each covering a stretch of the view — so the near on...
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::mat4 shadowMatrix[MAX_SHADOW_TILES]
glm::vec4 shadowTile[MAX_SHADOW_TILES]
GpuLight lights[MAX_LIGHTS]
glm::vec4 reflBox[2 *MAX_REFLECTION_PROBES]
glm::uvec4 tileMask[TILE_COLUMNS *TILE_ROWS]