68 : size_(atlasSize), cell_(cellSize > 0 ? cellSize : 1),
69 cols_(atlasSize / (cellSize > 0 ? cellSize : 1)) {
70 used_.assign(
static_cast<usize>(cols_) * cols_,
false);
76 used_.assign(used_.size(),
false);
93 if (count == 0 || cells == 0 || cells > cols_) {
97 if (tiles_.size() + count > tile_budget_) {
101 const usize before = tiles_.size();
102 for (
u32 i = 0; i < count; ++i) {
104 tiles_.resize(before);
110 return static_cast<i32>(before);
123 extent = std::max(extent, std::max(t.x, t.y) + t.size);
129 while (size < extent) {
130 size = size > size_ / 2 ? size_ : size * 2;
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};
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_});
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;
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;
180 used_.assign(used_.size(),
false);
181 for (
const ShadowTile& t : tiles_) {
182 mark(t.x / cell_, t.y / cell_, t.size / cell_,
true);
189 std::vector<ShadowTile> tiles_;
190 std::vector<bool> used_;
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.
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.
std::uint32_t u32
Unsigned 32-bit integer.
std::uint8_t u8
Unsigned 8-bit integer.
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
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.