50 hasEnvironment_ =
false;
57 data_.ambient.x += color.x;
58 data_.ambient.y += color.y;
59 data_.ambient.z += color.z;
66 data_.ambientGround.x += delta.x;
67 data_.ambientGround.y += delta.y;
68 data_.ambientGround.z += delta.z;
76 data_.lights[count_++] = light;
77 data_.ambient.w =
static_cast<f32>(count_);
85 data_.tiledLights[tiledCount_] = light;
86 markTiledLight(tiledCount_, viewProjection);
96 if (tiledCount_ == 0 && !loopMarked_)
return;
98 for (
u32 i = 0; i < tiledCount_; ++i) markTiledLight(i, viewProjection);
120 auto* masks =
reinterpret_cast<u32(*)[4]
>(data_.tileMask);
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;
130 }
else if (light.
color.a != 0.0f) {
131 const f32 radius = std::max(light.
posDir.w, 0.0001f) * 1.001f + 0.001f;
134 if (!flatView || directional) {
138 if (light.
color.a == 0.0f)
continue;
140 const f32 reach = std::max(light.
posDir.w, 0.0001f) * 1.001f + 0.001f;
143 data_.tileView = viewProjection;
146 data_.tileInfo.w = 1.0f;
150 loopTileBits_ +=
static_cast<u32>(std::popcount(masks[t][3] & 0xFFFFu));
151 loopTileBits3D_ +=
static_cast<u32>(std::popcount(masks[t][3] >> 16));
169 const glm::vec4& params) {
171 data_.shadowMatrix[i] = i <
count ? matrices[i] : glm::mat4(1.0f);
172 data_.shadowTile[i] = i <
count ? tiles[i] : glm::vec4(0.0f);
174 data_.shadowParams = params;
183 if (slot >= count_)
return;
184 data_.lights[slot].spot.x =
static_cast<f32>(tile + 1);
189 if (slot >= count_)
return;
194 data_.lights[slot].shadowMap.x =
static_cast<f32>(first);
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;
225 data_.envParams = params;
226 data_.reflParams.x =
static_cast<f32>(
count) + 1.0f;
228 data_.reflBox[i] = boxes[i];
235 data_.reflParams.y = scale;
236 data_.reflParams.z = saturation;
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);
259 data_.ambient.w = 0.0f;
265 data_.bakedTint = glm::vec4(tint, 1.0f);
274 data_.lights[slot].shadowMap.w =
static_cast<f32>(channel);
281 data_.tiledLights[index].shadowMap.w =
static_cast<f32>(channel);
289 data_.lights[slot].falloff.w =
static_cast<f32>(channel);
297 data_.mask2DRect = rect;
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;
320 void markTiledLight(
u32 index,
const glm::mat4& viewProjection) {
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,
327 LightConstants data_{};
330 bool loopMarked_ =
false;
331 u32 loopTileBits_ = 0;
332 u32 loopTileBits3D_ = 0;
333 bool hasEnvironment_ =
false;
336 GfxDevice* device_ =
nullptr;
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.
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 ¶ms)
void setReflections(const glm::vec4 ¶ms, 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
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 ¶ms, 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 ¢er, 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.
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.
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_COLUMNS
float f32
32-bit floating point
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]