Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ProbeVolume.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
7#pragma once
8
9#include "../core/Types.hpp"
10#include "../core/Revision.hpp"
11#include "../math/Math.hpp"
12
13#include <algorithm>
14#include <atomic>
15#include <vector>
16
17namespace esengine {
18
31public:
33 static constexpr i32 kBrickCells = 4;
34 static constexpr i32 kBrickSide = kBrickCells + 1;
36
40 glm::ivec3 resolution{0};
41
44 std::vector<glm::vec3> irradiance;
45
50 std::vector<f32> skyVisibility;
51
54 std::vector<glm::ivec4> bricks;
55
57 std::vector<i32> cellBrick;
58
63
65
66 bool sparse() const { return !bricks.empty(); }
67
69 static glm::ivec3 cellsFor(const glm::ivec3& res) {
70 glm::ivec3 n{1};
71 for (int a = 0; a < 3; ++a) n[a] = res[a] > 1 ? (res[a] - 1 + kBrickCells - 1) / kBrickCells : 1;
72 return n;
73 }
74 glm::ivec3 cells() const { return cellsFor(resolution); }
75
76 bool hasSkyVisibility() const {
77 return !skyVisibility.empty() && skyVisibility.size() == static_cast<usize>(probeCount()) * 9;
78 }
79
81 u32 probeCount() const {
82 if (resolution.x <= 0 || resolution.y <= 0 || resolution.z <= 0) return 0;
83 if (sparse()) return static_cast<u32>(bricks.size()) * static_cast<u32>(kBrickProbes);
84 return static_cast<u32>(resolution.x) * static_cast<u32>(resolution.y)
85 * static_cast<u32>(resolution.z);
86 }
87
88 bool isValid() const {
89 const u32 probes = probeCount();
90 if (probes == 0 || irradiance.size() != static_cast<usize>(probes) * 9) return false;
91 if (!sparse()) return true;
92 const glm::ivec3 c = cells();
93 return cellBrick.size() == static_cast<usize>(c.x) * static_cast<usize>(c.y) * static_cast<usize>(c.z);
94 }
95
101 bool setBricks(std::vector<glm::ivec4> list) {
102 bricks.clear();
103 cellBrick.clear();
104 const glm::ivec3 c = cells();
105 if (list.empty() || resolution.x <= 0 || resolution.y <= 0 || resolution.z <= 0) return false;
106 std::vector<i32> owner(static_cast<usize>(c.x) * static_cast<usize>(c.y) * static_cast<usize>(c.z), -1);
107 for (usize b = 0; b < list.size(); ++b) {
108 const glm::ivec4 k = list[b];
109 const i32 span = k.w;
110 if (span <= 0 || (span & (span - 1)) != 0) return false;
111 for (int a = 0; a < 3; ++a) {
112 if (k[a] < 0 || k[a] >= c[a] || k[a] % span != 0) return false;
113 }
114 for (i32 z = k.z; z < std::min(k.z + span, c.z); ++z) {
115 for (i32 y = k.y; y < std::min(k.y + span, c.y); ++y) {
116 for (i32 x = k.x; x < std::min(k.x + span, c.x); ++x) {
117 i32& o = owner[static_cast<usize>(x) + static_cast<usize>(c.x) * (static_cast<usize>(y) + static_cast<usize>(c.y) * static_cast<usize>(z))];
118 if (o >= 0) return false;
119 o = static_cast<i32>(b);
120 }
121 }
122 }
123 }
124 for (const i32 o : owner) if (o < 0) return false;
125 bricks = std::move(list);
126 cellBrick = std::move(owner);
127 return true;
128 }
129};
130
131} // namespace esengine
Math utilities and GLM configuration for ESEngine.
What a resource's content is, as a number that changes whenever it does.
Core type definitions and utilities for ESEngine.
Irradiance at a grid of points — an Environment sampled, not held.
glm::ivec3 cells() const
u64 revision
Changes whenever the coefficients do (a new grid, its sky visibility set): what the frame's probe atl...
static u64 nextRevision()
static constexpr i32 kBrickSide
static constexpr i32 kBrickCells
Cells a brick spans along each axis at its own spacing, and the probes it carries (its far faces too)...
static glm::ivec3 cellsFor(const glm::ivec3 &res)
Finest-brick cells along each axis: enough for every probe interval, one on a flat axis.
u32 probeCount() const
The probes the coefficients hold: the grid's for a dense one, kBrickProbes a brick for a sparse one.
std::vector< glm::ivec4 > bricks
Sparse only: each brick's first finest-brick cell (xyz) and its span in those cells (w)....
std::vector< f32 > skyVisibility
How much of the sky each probe sees: nine coefficients per probe, cosine-convolved like the irradianc...
std::vector< glm::vec3 > irradiance
Nine RGB coefficients per probe: in grid order when dense, brick after brick (each brick's probes x f...
bool hasSkyVisibility() const
bool setBricks(std::vector< glm::ivec4 > list)
Makes the grid sparse with these bricks. False, and the grid left dense, unless they tile it: every s...
static constexpr i32 kBrickProbes
glm::ivec3 resolution
Probes along each axis of the finest grid. One on an axis is a constant along it — which is what an E...
std::vector< i32 > cellBrick
Sparse only: which brick covers each finest-brick cell, x fastest. Built by setBricks.
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
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
u64 nextRevision()
A revision no content has had before. Never 0, which reads as "none".
Definition Revision.hpp:24