Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
TiledLights.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
17#pragma once
18
19#include "../../core/Types.hpp"
20
21#include <glm/glm.hpp>
22
23#include <algorithm>
24#include <cmath>
25
26namespace esengine {
27
30inline constexpr u32 TILE_COLUMNS = 16;
31inline constexpr u32 TILE_ROWS = 12;
32
34inline constexpr u32 MAX_TILED_LIGHTS = 96;
35
42inline void markLightTiles(const glm::mat4& viewProjection, const glm::vec3& center, f32 radius, u32 index,
43 u32 (*masks)[4]) {
44 const u32 word = index >> 5;
45 const u32 bit = 1u << (index & 31u);
46 const auto markAll = [&]() {
47 for (u32 t = 0; t < TILE_COLUMNS * TILE_ROWS; ++t) masks[t][word] |= bit;
48 };
49 f32 minX = 1e30f, minY = 1e30f, maxX = -1e30f, maxY = -1e30f;
50 for (u32 corner = 0; corner < 8; ++corner) {
51 const glm::vec3 p = center + glm::vec3((corner & 1u) ? radius : -radius, (corner & 2u) ? radius : -radius,
52 (corner & 4u) ? radius : -radius);
53 const glm::vec4 clip = viewProjection * glm::vec4(p, 1.0f);
54 if (clip.w <= 1e-4f) {
55 markAll();
56 return;
57 }
58 const f32 x = clip.x / clip.w;
59 const f32 y = clip.y / clip.w;
60 minX = std::min(minX, x);
61 maxX = std::max(maxX, x);
62 minY = std::min(minY, y);
63 maxY = std::max(maxY, y);
64 }
65 if (maxX < -1.0f || minX > 1.0f || maxY < -1.0f || minY > 1.0f) return;
66 const auto tileOf = [](f32 ndc, u32 count) {
67 const f32 t = std::floor((ndc * 0.5f + 0.5f) * static_cast<f32>(count));
68 return static_cast<u32>(std::clamp(t, 0.0f, static_cast<f32>(count - 1)));
69 };
70 const u32 x0 = tileOf(minX, TILE_COLUMNS), x1 = tileOf(maxX, TILE_COLUMNS);
71 const u32 y0 = tileOf(minY, TILE_ROWS), y1 = tileOf(maxY, TILE_ROWS);
72 for (u32 y = y0; y <= y1; ++y) {
73 for (u32 x = x0; x <= x1; ++x) masks[y * TILE_COLUMNS + x][word] |= bit;
74 }
75}
76
77} // namespace esengine
Core type definitions and utilities for ESEngine.
void markLightTiles(const glm::mat4 &viewProjection, const glm::vec3 &center, f32 radius, u32 index, u32(*masks)[4])
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
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