Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ShadowAtlas.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
14#pragma once
15
16#include "../../core/Types.hpp"
17#include "./LightConstants.hpp"
18
19#include <glm/glm.hpp>
20
21#include <algorithm>
22#include <vector>
23
24namespace esengine {
25
39
41inline const char* refusalName(AtlasRefusal why) {
42 switch (why) {
43 case AtlasRefusal::TileBudget: return "tile budget";
44 case AtlasRefusal::TooLarge: return "tile larger than the atlas";
45 case AtlasRefusal::NoSpace: return "atlas full";
46 case AtlasRefusal::None: break;
47 }
48 return "none";
49}
50
52struct ShadowTile {
53 u32 x = 0;
54 u32 y = 0;
55 u32 size = 0;
56};
57
66public:
67 ShadowAtlas(u32 atlasSize, u32 cellSize)
68 : size_(atlasSize), cell_(cellSize > 0 ? cellSize : 1),
69 cols_(atlasSize / (cellSize > 0 ? cellSize : 1)) {
70 used_.assign(static_cast<usize>(cols_) * cols_, false);
71 }
72
74 void reset() {
75 tiles_.clear();
76 used_.assign(used_.size(), false);
77 }
78
91 i32 allocate(u32 count, u32 cells, AtlasRefusal& why) {
93 if (count == 0 || cells == 0 || cells > cols_) {
95 return -1;
96 }
97 if (tiles_.size() + count > tile_budget_) {
99 return -1;
100 }
101 const usize before = tiles_.size();
102 for (u32 i = 0; i < count; ++i) {
103 if (!claim(cells)) {
104 tiles_.resize(before);
105 rebuildUsed();
107 return -1;
108 }
109 }
110 return static_cast<i32>(before);
111 }
112
113 void setTileBudget(u32 tiles) { tile_budget_ = std::clamp(tiles, 1u, MAX_SHADOW_TILES); }
114 u32 tileBudget() const { return tile_budget_; }
115
116 u32 tileCount() const { return static_cast<u32>(tiles_.size()); }
117 const ShadowTile& tile(u32 index) const { return tiles_[index]; }
118
119 // Fit storage to occupied texels without changing tile placement or resolution.
121 u32 extent = 0;
122 for (const ShadowTile& t : tiles_) {
123 extent = std::max(extent, std::max(t.x, t.y) + t.size);
124 }
125 if (extent == 0) {
126 return 0;
127 }
128 u32 size = cell_;
129 while (size < extent) {
130 size = size > size_ / 2 ? size_ : size * 2;
131 }
132 return size;
133 }
134
142 glm::vec4 unitRect(u32 index, u32 textureSize = 0) const {
143 const ShadowTile& t = tiles_[index];
144 const f32 inv = 1.0f / static_cast<f32>(textureSize > 0 ? textureSize : size_);
145 return {static_cast<f32>(t.x) * inv, static_cast<f32>(t.y) * inv,
146 static_cast<f32>(t.size) * inv, 0.0f};
147 }
148
149private:
150 bool claim(u32 cells) {
151 for (u32 cy = 0; cy + cells <= cols_; cy += cells) {
152 for (u32 cx = 0; cx + cells <= cols_; cx += cells) {
153 if (!free(cx, cy, cells)) continue;
154 mark(cx, cy, cells, true);
155 tiles_.push_back({cx * cell_, cy * cell_, cells * cell_});
156 return true;
157 }
158 }
159 return false;
160 }
161
162 bool free(u32 cx, u32 cy, u32 cells) const {
163 for (u32 y = cy; y < cy + cells; ++y) {
164 for (u32 x = cx; x < cx + cells; ++x) {
165 if (used_[static_cast<usize>(y) * cols_ + x]) return false;
166 }
167 }
168 return true;
169 }
170
171 void mark(u32 cx, u32 cy, u32 cells, bool value) {
172 for (u32 y = cy; y < cy + cells; ++y) {
173 for (u32 x = cx; x < cx + cells; ++x) {
174 used_[static_cast<usize>(y) * cols_ + x] = value;
175 }
176 }
177 }
178
179 void rebuildUsed() {
180 used_.assign(used_.size(), false);
181 for (const ShadowTile& t : tiles_) {
182 mark(t.x / cell_, t.y / cell_, t.size / cell_, true);
183 }
184 }
185
186 u32 size_;
187 u32 cell_;
188 u32 cols_;
189 std::vector<ShadowTile> tiles_;
190 std::vector<bool> used_;
191 u32 tile_budget_ = MAX_SHADOW_TILES;
192};
193
194} // namespace esengine
Per-frame 2D lighting constants — the third tier of the engine's constant-buffer layering (after per-...
Core type definitions and utilities for ESEngine.
ShadowAtlas(u32 atlasSize, u32 cellSize)
void reset()
Gives the whole atlas back. Called once per frame, before any claim.
i32 allocate(u32 count, u32 cells, AtlasRefusal &why)
Claims count tiles of cells x cells cells each.
const ShadowTile & tile(u32 index) const
void setTileBudget(u32 tiles)
glm::vec4 unitRect(u32 index, u32 textureSize=0) const
Tile index as a fraction of the atlas: (origin.x, origin.y, side, 0).
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.
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
const char * refusalName(AtlasRefusal why)
The refusal as a word, for a log line and, later, for a reader.
float f32
32-bit floating point
Definition Types.hpp:59
AtlasRefusal
Why a claim was turned down. Three different answers on purpose: raising the atlas size fixes NoSpace...
@ TileBudget
More tiles than the shader's arrays are sized for (MAX_SHADOW_TILES).
@ NoSpace
The atlas has no free block that size left. The one raising the size fixes.
@ TooLarge
One tile is wider than the whole atlas — the request cannot fit at any size.
One tile's square, in atlas texels.