60 static glm::ivec3
bricksFor(
const glm::ivec3& resolution) {
69 per_object_draws_ = 0;
73 void noteDraw(
bool perPixel)
const { ++(perPixel ? per_pixel_draws_ : per_object_draws_); }
86 atlas_width_ = atlas_height_ = atlas_depth_ = 0;
88 for (
Volume& v : volumes_) v.indirection = glm::ivec3(-1);
93 void add(
const glm::vec3& min,
const glm::vec3& max,
const ProbeVolume* grid) {
94 if (!grid || !grid->
isValid())
return;
95 if (max.x <= min.x || max.y <= min.y || max.z <= min.z)
return;
96 volumes_.push_back({min, max, grid});
99 bool empty()
const {
return volumes_.empty(); }
109 for (
const Volume& v : volumes_) {
110 mix(&v.min,
sizeof(v.min));
111 mix(&v.max,
sizeof(v.max));
112 mix(&v.grid,
sizeof(v.grid));
113 mix(&v.grid->revision,
sizeof(v.grid->revision));
115 if (revision_ == 0 || h != revision_hash_) {
131 const Volume* best =
nullptr;
133 for (
const Volume& v : volumes_) {
134 if (p.x < v.min.x || p.x > v.max.x || p.y < v.min.y || p.y > v.max.y
135 || p.z < v.min.z || p.z > v.max.z)
continue;
136 const glm::vec3 e = v.max - v.min;
137 const f32 size = e.x * e.y * e.z;
138 if (!best || size < bestSize) { best = &v; bestSize = size; }
140 if (!best)
return false;
143 if (perPixel && best->
indirection.x >= 0 && atlas_texture_ != 0) {
144 writeGridRun(*best, out);
148 sampleBricks(*best, p, out);
151 i32 low[3] = {0, 0, 0};
152 i32 high[3] = {0, 0, 0};
153 f32 frac[3] = {0.0f, 0.0f, 0.0f};
154 for (
u32 a = 0; a < 3; ++a) {
156 if (n <= 1)
continue;
157 const f32 span = best->
max[
static_cast<int>(a)] - best->
min[
static_cast<int>(a)];
158 f32 u = (p[
static_cast<int>(a)] - best->
min[
static_cast<int>(a)]) / span
159 *
static_cast<f32>(n - 1);
160 u = std::fmin(std::fmax(u, 0.0f),
static_cast<f32>(n - 1));
161 i32 i0 =
static_cast<i32>(std::floor(u));
162 if (i0 > n - 2) i0 = n - 2;
165 frac[a] = u -
static_cast<f32>(i0);
172 for (
u32 corner = 0; corner < 8; ++corner) {
173 const f32 w = ((corner & 1u) ? frac[0] : 1.0f - frac[0])
174 * ((corner & 2u) ? frac[1] : 1.0f - frac[1])
175 * ((corner & 4u) ? frac[2] : 1.0f - frac[2]);
176 if (w <= 0.0f)
continue;
177 const i32 x = (corner & 1u) ? high[0] : low[0];
178 const i32 y = (corner & 2u) ? high[1] : low[1];
179 const i32 z = (corner & 4u) ? high[2] : low[2];
180 const usize base =
static_cast<usize>(x + y * strideY + z * strideZ) * 9;
181 for (
u32 c = 0; c < 9; ++c) {
206 if (
sample(centre, out, perPixel))
return true;
207 const Volume* best =
nullptr;
209 for (
const Volume& v : volumes_) {
210 const glm::vec3 vc = (v.min + v.max) * 0.5f;
211 const glm::vec3 vh = (v.max - v.min) * 0.5f;
212 const glm::vec3 gap = glm::abs(centre - vc) - (half + vh);
213 if (gap.x > 0.0f || gap.y > 0.0f || gap.z > 0.0f)
continue;
214 const f32 size = vh.x * vh.y * vh.z;
215 if (!best || size < bestSize) { best = &v; bestSize = size; }
217 if (!best)
return false;
218 return sample(glm::clamp(centre, best->
min, best->
max), out, perPixel);
231 const i32 side =
static_cast<i32>(maxSide);
234 std::vector<u64> key;
235 key.reserve(volumes_.size() * 2);
238 glm::ivec3 extent{0};
239 for (
usize i = 0; i < volumes_.size(); ++i) {
243 for (
usize j = 0; j < i; ++j) {
244 if (volumes_[j].grid == v.
grid) { shared =
true;
break; }
246 if (shared)
continue;
250 const glm::ivec3 grown{std::max(extent.x, n.x), std::max(extent.y, n.y), extent.z + n.z};
251 const Shape shape = shapeFor(bricks +
count, maxSlotsX, maxSlotsYZ, grown);
252 if (shape.slots.x == 0 || shape.pageWidth *
kAtlasPages > side || shape.height > side || shape.depth > side) {
255 placed_.push_back({v.
grid, bricks, n, glm::ivec3(0, 0, extent.z),
count});
259 key.push_back(
static_cast<u64>(extent.z));
261 shape_ = placed_.empty() ? Shape{} : shapeFor(bricks, maxSlotsX, maxSlotsYZ, extent);
263 for (Placed& p : placed_) p.indirection.x = (
kAtlasPages - 1) * shape_.pageWidth;
264 for (
Volume& v : volumes_) {
265 for (
const Placed& p : placed_) {
266 if (p.grid == v.grid) { v.indirection = p.indirection;
break; }
269 const u32 width = placed_.empty() ? 0u :
static_cast<u32>(shape_.pageWidth *
kAtlasPages);
270 const u32 height = placed_.empty() ? 0u :
static_cast<u32>(shape_.height);
271 const u32 depth = placed_.empty() ? 0u :
static_cast<u32>(shape_.depth);
272 const bool changed = key != atlas_key_ || width != atlas_width_ || height != atlas_height_ || depth != atlas_depth_;
273 atlas_key_ = std::move(key);
274 atlas_width_ = width;
275 atlas_height_ = height;
276 atlas_depth_ = depth;
285 return static_cast<u64>(atlas_width_) * atlas_height_ * atlas_depth_ * 4u *
sizeof(
u16);
292 for (
const Placed& p : placed_) n +=
static_cast<u32>(p.count);
298 const i32 sx = shape_.slots.x;
299 const i32 sy = shape_.slots.y;
300 return glm::ivec3(index % sx, (index / sx) % sy, index / (sx * sy)) *
kBrickTexels;
310 const usize w = atlas_width_;
311 const usize h = atlas_height_;
312 out.assign(w * h * atlas_depth_ * 4, 0);
314 return &out[((
static_cast<usize>(z) * h +
static_cast<usize>(y)) * w +
static_cast<usize>(x)) * 4];
316 const i32 page = shape_.pageWidth;
317 for (
const Placed& p : placed_) {
322 const auto put = [&](
u16* t,
usize probe) {
330 const glm::ivec4 k = g.
bricks[b];
331 const glm::ivec3 o =
slotOrigin(p.firstBrick +
static_cast<i32>(b));
335 for (
i32 c = 0; c < 9; ++c) put(texel(c * page + o.x + tx, o.y + ty, o.z + tz), probe +
static_cast<usize>(c));
338 for (
i32 z = k.z; z < std::min(k.z + k.w, p.bricks.z); ++z) {
339 for (
i32 y = k.y; y < std::min(k.y + k.w, p.bricks.y); ++y) {
340 for (
i32 x = k.x; x < std::min(k.x + k.w, p.bricks.x); ++x) {
341 u16* ind = texel(p.indirection.x + x, p.indirection.y + y, p.indirection.z + z);
352 for (
i32 bz = 0; bz < p.bricks.z; ++bz) {
353 for (
i32 by = 0; by < p.bricks.y; ++by) {
354 for (
i32 bx = 0; bx < p.bricks.x; ++bx) {
355 const i32 index = p.firstBrick + bx + p.bricks.x * (by + p.bricks.y * bz);
357 u16* ind = texel(p.indirection.x + bx, p.indirection.y + by, p.indirection.z + bz);
368 const usize probe =
static_cast<usize>(px + r.x * (py + r.y * pz)) * 9;
369 for (
i32 c = 0; c < 9; ++c) {
370 u16* t = texel(c * page + o.x + tx, o.y + ty, o.z + tz);
400 const glm::vec3 e = v.max - v.min;
404 out.
irradiance[1] = glm::vec4(v.min, r.x - 1.0f);
405 out.
irradiance[2] = glm::vec4(r.x > 1.0f ? (r.x - 1.0f) / e.x : 0.0f,
406 r.y > 1.0f ? (r.y - 1.0f) / e.y : 0.0f,
407 r.z > 1.0f ? (r.z - 1.0f) / e.z : 0.0f, r.y - 1.0f);
408 out.
irradiance[3] = glm::vec4(glm::vec3(v.indirection), r.z - 1.0f);
409 out.
irradiance[4] = glm::vec4(1.0f /
static_cast<f32>(atlas_width_), 1.0f /
static_cast<f32>(atlas_height_),
410 1.0f /
static_cast<f32>(atlas_depth_),
static_cast<f32>(shape_.pageWidth));
424 const ProbeVolume* grid =
nullptr;
426 glm::ivec3 bricks{1};
427 glm::ivec3 indirection{0};
435 void sampleBricks(
const Volume& v,
const glm::vec3& p, ProbeConstants& out)
const {
436 const ProbeVolume& g = *v.grid;
437 const glm::ivec3 cells = g.
cells();
438 glm::vec3 grid{0.0f};
439 for (
int a = 0; a < 3; ++a) {
440 const i32 n = g.resolution[a];
441 if (n <= 1)
continue;
442 const f32 u = (p[a] - v.min[a]) / (v.max[a] - v.min[a]) *
static_cast<f32>(n - 1);
443 grid[a] = std::fmin(std::fmax(u, 0.0f),
static_cast<f32>(n - 1));
446 for (
int a = 0; a < 3; ++a) cell[a] = std::min(static_cast<i32>(std::floor(grid[a] /
kBrickCells)), cells[a] - 1);
447 const i32 b = g.cellBrick[
static_cast<usize>(cell.x) +
static_cast<usize>(cells.x)
448 * (
static_cast<usize>(cell.y) +
static_cast<usize>(cells.y) *
static_cast<usize>(cell.z))];
449 const glm::ivec4 k = g.bricks[
static_cast<usize>(b)];
452 for (
int a = 0; a < 3; ++a) {
454 low[a] = std::min(std::max(
static_cast<i32>(std::floor(local)), 0),
kBrickCells - 1);
455 frac[a] = std::fmin(std::fmax(local -
static_cast<f32>(low[a]), 0.0f), 1.0f);
457 const bool sky = g.hasSkyVisibility();
458 out = ProbeConstants{};
459 for (
u32 corner = 0; corner < 8; ++corner) {
460 const f32 w = ((corner & 1u) ? frac[0] : 1.0f - frac[0])
461 * ((corner & 2u) ? frac[1] : 1.0f - frac[1])
462 * ((corner & 4u) ? frac[2] : 1.0f - frac[2]);
463 if (w <= 0.0f)
continue;
464 const i32 t = (low[0] + ((corner & 1u) ? 1 : 0))
467 for (
u32 c = 0; c < 9; ++c) {
468 const glm::vec3& sh = g.irradiance[base + c];
469 out.irradiance[c].x += sh.x * w;
470 out.irradiance[c].y += sh.y * w;
471 out.irradiance[c].z += sh.z * w;
474 out.reflection.y += g.skyVisibility[base] * w;
475 for (
u32 c = 1; c < 9; ++c) out.irradiance[c].w += g.skyVisibility[base + c] * w;
478 out.irradiance[0].w = 1.0f;
479 if (sky) out.reflection.z = 1.0f;
488 static Shape shapeFor(
i32 bricks,
i32 maxSlotsX,
i32 maxSlotsYZ,
const glm::ivec3& extent) {
490 if (bricks <= 0 || maxSlotsX <= 0 || maxSlotsYZ <= 0)
return best;
492 for (
i32 sx = 1; sx <= std::min(bricks, maxSlotsX); ++sx) {
493 for (
i32 sy = 1; sy <= std::min((bricks + sx - 1) / sx, maxSlotsYZ); ++sy) {
494 const i32 sz = (bricks + sx * sy - 1) / (sx * sy);
495 if (sz > maxSlotsYZ)
continue;
497 s.slots = {sx, sy, sz};
501 const i64 texels =
static_cast<i64>(s.pageWidth) * s.height * s.depth;
502 if (best.slots.x == 0 || texels < bestTexels) { best = s; bestTexels = texels; }
508 std::vector<Volume> volumes_;
509 mutable u64 revision_ = 0;
510 mutable u64 revision_hash_ = 0;
511 std::vector<u64> atlas_key_;
512 std::vector<Placed> placed_;
514 u32 atlas_width_ = 0;
515 u32 atlas_height_ = 0;
516 u32 atlas_depth_ = 0;
517 u32 atlas_texture_ = 0;
518 mutable u32 per_pixel_draws_ = 0;
519 mutable u32 per_object_draws_ = 0;
IEEE binary32 → binary16, for the data the GPU takes at half width.
The indirect light where ONE draw stands, when a grid was baked for it.
A baked environment sampled on a grid, rather than held at one value.
What a resource's content is, as a number that changes whenever it does.
Core type definitions and utilities for ESEngine.
The frame's probe volumes, and the trilinear read through them.
static constexpr i32 kBrickCells
glm::ivec3 slotOrigin(i32 index) const
Where brick slot index's first texel sits in page 0.
u32 brickCount() const
Bricks the atlas holds, across every grid placed.
void add(const glm::vec3 &min, const glm::vec3 &max, const ProbeVolume *grid)
Takes a volume, or ignores one no point can be inside. A grid whose coefficients do not match its res...
static constexpr i32 kBrickTexels
bool layoutAtlas(u32 maxSide)
Places every volume's grid in the atlas. True when the atlas's texels changed and want uploading.
bool sampleBox(const glm::vec3 ¢re, const glm::vec3 &half, ProbeConstants &out, bool perPixel=false) const
The volume a whole object stands in, by its world box rather than its origin.
bool sample(const glm::vec3 &p, ProbeConstants &out, bool perPixel=false) const
The irradiance at p, read trilinearly. False = no volume holds it.
void noteDraw(bool perPixel) const
Counts a draw that read a volume this frame, per pixel or per object: what the stats say a frame paid...
void fillAtlas(std::vector< u16 > &out) const
The atlas's texels, RGBA half floats, slice after slice: a coefficient's rgb and the sky's visibility...
u64 revision() const
What the frame's probe volumes say, as a number that changes whenever it does (see core/Revision....
u32 pageWidth() const
Texels across one page: the x distance from a coefficient's texel to the next one's.
u32 perObjectDraws() const
static constexpr i32 kAtlasPages
Pages across the atlas: the nine coefficients', then the indirection's.
static constexpr u32 kReadsPerLookup
Texture reads one per-pixel lookup costs: the indirection cell, then a filtered read per coefficient.
u64 atlasBytes() const
What the atlas takes on the GPU: four half floats a texel, no mips. What a sparse grid saves is here.
void forgetAtlas()
Drops the atlas's layout, so the next layoutAtlas places everything again and says it changed....
void setAtlasTexture(u32 id)
The atlas texture this frame's per-pixel runs point into; 0 = none, and every draw reads per object.
static glm::ivec3 bricksFor(const glm::ivec3 &resolution)
Bricks along each axis of a grid: enough cells for every probe interval, one on a flat axis.
u32 perPixelDraws() const
Irradiance at a grid of points — an Environment sampled, not held.
u64 revision
Changes whenever the coefficients do (a new grid, its sky visibility set): what the frame's probe atl...
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)...
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
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...
u64 fnv1aBytes(u64 h, const void *data, usize bytes)
constexpr u64 kFnv64Offset
std::int32_t i32
Signed 32-bit integer.
std::uint64_t u64
Unsigned 64-bit integer.
u16 packHalf(f32 value)
IEEE binary32 to binary16.
std::size_t usize
Size type for indexing and sizes.
std::uint32_t u32
Unsigned 32-bit integer.
std::int64_t i64
Signed 64-bit integer.
std::uint16_t u16
Unsigned 16-bit integer.
u64 nextRevision()
A revision no content has had before. Never 0, which reads as "none".
float f32
32-bit floating point
The indirect light one instance stands in — both halves of it.
One volume: a world box, and the grid solved inside it.
glm::ivec3 indirection
Where its grid's indirection cells start in the atlas (a texel of the indirection page)....