128 lines
5.0 KiB
TypeScript
128 lines
5.0 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// a render region keeps its sections' solid and cutout quads in one buffer per layer, by face group, and puts it
|
|
// together again when they change, see client/level/render_region.ts
|
|
import { assert, assertEquals } from "@std/assert";
|
|
import { type RegionBuffers, RenderRegion } from "@/client/level/render_region.ts";
|
|
import type { RenderSection } from "@/client/level/client_level.ts";
|
|
import { FACE_GROUPS, type FaceGroup, TERRAIN_QUAD_BYTES } from "@/client/workers/chunk_messages.ts";
|
|
|
|
// buffers in memory, keeping what was written to them
|
|
interface FakeBuffer {
|
|
bytes: Uint8Array;
|
|
destroyed: boolean;
|
|
}
|
|
|
|
function fake_buffers() {
|
|
const written = { bytes: 0 };
|
|
const buffers: RegionBuffers = {
|
|
create: (size) => ({ bytes: new Uint8Array(size), destroyed: false }) as unknown as GPUBuffer,
|
|
write: (buffer, offset, data, first, size) => {
|
|
written.bytes += size;
|
|
(buffer as unknown as FakeBuffer).bytes.set(data.subarray(first, first + size), offset);
|
|
},
|
|
copy: (from, to, ranges) => {
|
|
const source = from as unknown as FakeBuffer;
|
|
assert(!source.destroyed);
|
|
for (let i = 0; i < ranges.length; i += 3) {
|
|
(to as unknown as FakeBuffer).bytes.set(
|
|
source.bytes.subarray(ranges[i], ranges[i] + ranges[i + 2]),
|
|
ranges[i + 1],
|
|
);
|
|
}
|
|
},
|
|
destroy: (buffer) => (buffer as unknown as FakeBuffer).destroyed = true,
|
|
};
|
|
return { buffers, written };
|
|
}
|
|
|
|
// a section's solid quads as the chunk worker sends them, counts[g] in each group, each quad's bytes all a tag that
|
|
// says which mesh, group and quad it is
|
|
function mesh(region: RenderRegion, section: RenderSection, mesh: number, counts: number[]) {
|
|
const groups: FaceGroup[] = [];
|
|
const tags: number[][] = [];
|
|
let first = 0;
|
|
for (let g = 0; g < FACE_GROUPS; g++) {
|
|
groups.push({ first, count: counts[g], min: 0, max: 0 });
|
|
tags.push(Array.from({ length: counts[g] }, (_, q) => mesh * 40 + g * 5 + q));
|
|
first += counts[g];
|
|
}
|
|
const vertices = new Uint8Array(first * TERRAIN_QUAD_BYTES);
|
|
tags.flat().forEach((tag, q) => vertices.fill(tag, q * TERRAIN_QUAD_BYTES, (q + 1) * TERRAIN_QUAD_BYTES));
|
|
section.meshes = { solid: { quad_count: first, groups } };
|
|
region.meshed("solid", section, vertices);
|
|
return tags;
|
|
}
|
|
|
|
const new_section = () => ({ meshes: {} }) as RenderSection;
|
|
|
|
// the tags of a section's quads in each group, as they are in the region's buffer
|
|
function in_buffer(region: RenderRegion, section: RenderSection) {
|
|
const bytes = (region.layers.solid.buffer as unknown as FakeBuffer).bytes;
|
|
return section.meshes.solid!.groups!.map(({ first, count }) =>
|
|
Array.from({ length: count }, (_, q) => bytes[(first + q) * TERRAIN_QUAD_BYTES])
|
|
);
|
|
}
|
|
|
|
// each group's quads are the sections' one after another, so the ones drawn together are one draw call
|
|
function assert_by_group(region: RenderRegion) {
|
|
let end = 0;
|
|
for (let g = 0; g < FACE_GROUPS; g++) {
|
|
for (const section of region.layers.solid.sections) {
|
|
const group = section.meshes.solid!.groups![g];
|
|
assertEquals(group.first, end);
|
|
end += group.count;
|
|
}
|
|
}
|
|
}
|
|
|
|
Deno.test("a region's sections' quads are in its buffer group by group", () => {
|
|
const { buffers } = fake_buffers();
|
|
const region = new RenderRegion(buffers);
|
|
const sections = [new_section(), new_section(), new_section()];
|
|
const tags = [
|
|
mesh(region, sections[0], 0, [2, 0, 1, 3, 0, 0, 1]),
|
|
mesh(region, sections[1], 1, [0, 4, 1, 0, 2, 1, 0]),
|
|
mesh(region, sections[2], 2, [1, 1, 1, 1, 1, 1, 1]),
|
|
];
|
|
region.update();
|
|
assertEquals(sections.map((section) => in_buffer(region, section)), tags);
|
|
assert_by_group(region);
|
|
assertEquals(region.layers.cutout.buffer, undefined);
|
|
});
|
|
|
|
Deno.test("a region keeps what didn't change when its sections are meshed again, lose their quads or come", () => {
|
|
const { buffers, written } = fake_buffers();
|
|
const region = new RenderRegion(buffers);
|
|
const sections = [new_section(), new_section(), new_section(), new_section()];
|
|
const tags = [
|
|
mesh(region, sections[0], 0, [2, 0, 1, 3, 0, 0, 1]),
|
|
mesh(region, sections[1], 1, [0, 4, 1, 0, 2, 1, 0]),
|
|
mesh(region, sections[2], 2, [1, 1, 1, 1, 1, 1, 1]),
|
|
];
|
|
region.update();
|
|
const old = region.layers.solid.buffer as unknown as FakeBuffer;
|
|
|
|
// the first loses its quads, the second is meshed again, a fourth comes
|
|
region.removed("solid", sections[0]);
|
|
sections[0].meshes = {};
|
|
tags[1] = mesh(region, sections[1], 3, [1, 0, 0, 0, 0, 0, 2]);
|
|
tags[3] = mesh(region, sections[3], 4, [0, 0, 3, 0, 0, 0, 0]);
|
|
written.bytes = 0;
|
|
region.update();
|
|
|
|
assert(old.destroyed);
|
|
assertEquals(region.layers.solid.sections.length, 3);
|
|
assertEquals([1, 2, 3].map((i) => in_buffer(region, sections[i])), [tags[1], tags[2], tags[3]]);
|
|
assert_by_group(region);
|
|
assertEquals(written.bytes, 6 * TERRAIN_QUAD_BYTES, "only the new quads are written, the rest are copied");
|
|
|
|
// a layer that didn't change stays as it is
|
|
const solid = region.layers.solid.buffer;
|
|
region.update();
|
|
assertEquals(region.layers.solid.buffer, solid);
|
|
region.dispose();
|
|
assert((solid as unknown as FakeBuffer).destroyed);
|
|
});
|