Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ShadowPlan.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
16#pragma once
17
18#include "../../core/Types.hpp"
19#include "./ShadowAtlas.hpp"
20
21#include <vector>
22
23namespace esengine {
24
42
43 bool denied() const { return granted == 0; }
44 bool reduced() const { return granted > 0 && granted < requested; }
45};
46
53 std::vector<ShadowGrant> grants;
54
55 void clear() {
57 grantedTiles = 0;
58 grants.clear();
59 }
60
62 u32 n = 0;
63 for (const ShadowGrant& g : grants) n += g.denied() ? 1u : 0u;
64 return n;
65 }
66
68 const ShadowGrant* grantFor(Entity entity) const {
69 for (const ShadowGrant& g : grants) {
70 if (g.light.raw == entity.raw) return &g;
71 }
72 return nullptr;
73 }
74
76 bool hasSameDenial(const ShadowGrant& claim) const {
77 const ShadowGrant* previous = grantFor(claim.light);
78 return claim.denied() && previous && previous->denied()
79 && previous->requested == claim.requested && previous->refusal == claim.refusal;
80 }
81
83 u32 n = 0;
84 for (const ShadowGrant& g : grants) n += g.reduced() ? 1u : 0u;
85 return n;
86 }
87};
88
93
103inline ShadowGrant claimTiles(ShadowAtlas& atlas, Entity light, u32 want, u32 cells,
104 i32& outFirst, ShadowClaim policy = ShadowClaim::AllOrNothing) {
105 ShadowGrant grant{light, want, 0, AtlasRefusal::None};
106 outFirst = -1;
107 for (u32 take = want; take > 0; --take) {
109 const i32 at = atlas.allocate(take, cells, why);
110 if (at >= 0) {
111 outFirst = at;
112 grant.granted = take;
113 return grant;
114 }
115 // The first refusal is the one that answers the question a reader has.
116 if (take == want) grant.refusal = why;
117 if (policy == ShadowClaim::AllOrNothing) break;
118 }
119 return grant;
120}
121
122} // namespace esengine
Who owns which square of the one shadow texture.
Core type definitions and utilities for ESEngine.
A square texture divided into a grid of cells, handed out as square blocks.
i32 allocate(u32 count, u32 cells, AtlasRefusal &why)
Claims count tiles of cells x cells cells each.
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
ShadowGrant claimTiles(ShadowAtlas &atlas, Entity light, u32 want, u32 cells, i32 &outFirst, ShadowClaim policy=ShadowClaim::AllOrNothing)
Claim a complete shadow map, or explicitly allow fewer cascades.
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
AtlasRefusal
Why a claim was turned down. Three different answers on purpose: raising the atlas size fixes NoSpace...
Entity identifier with packed index + generation.
Definition Types.hpp:135
One caster's claim on the atlas: what it wanted, what it kept, and why the difference.
u32 requested
Tiles asked for: a sun's cascade set, a cube's six faces, or one.
u32 granted
Tiles kept. 0 means this light casts no shadow at all this frame.
Entity light
The light that asked. Carried so a reader can name it rather than an index.
The frame's whole shadow allocation, as numbers something outside it can check.
const ShadowGrant * grantFor(Entity entity) const
What entity asked for and kept, or null when it asked nothing.
bool hasSameDenial(const ShadowGrant &claim) const
Whether this report already contains the same denied claim.
std::vector< ShadowGrant > grants