139 lines
5.8 KiB
TypeScript
139 lines
5.8 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// sections are drawn by region, like sodium's RenderRegion: the solid and cutout quads of 8x4x8 sections share one
|
|
// buffer per layer, so drawing them binds it once. webgpu has no multi-draw, so the buffer is laid out to need fewer
|
|
// draws instead: by face group, every section's quads facing up, then every section's facing down, and so on (see
|
|
// FACE_GROUPS). the sections in view whose group faces the camera are then mostly next to each other, and a run of
|
|
// them is one draw
|
|
import { copy_buffer, create_copyable_vertex_buffer, destroy_buffer, write_buffer } from "@/client/renderer/mod.ts";
|
|
import { FACE_GROUPS, TERRAIN_QUAD_BYTES } from "@/client/workers/chunk_messages.ts";
|
|
import type { RenderSection } from "./client_level.ts";
|
|
|
|
export const REGION_WIDTH = 8;
|
|
export const REGION_HEIGHT = 4;
|
|
|
|
export type RegionLayer = "solid" | "cutout";
|
|
export const REGION_LAYERS: RegionLayer[] = ["solid", "cutout"];
|
|
|
|
// a region by the chunk and section index of any section in it. a chunk coordinate is under 2^19, so a region's x and
|
|
// z are each under 2^16 either way, and a world is at most 256 sections tall, so its regions under 2^8
|
|
export function region_key(chunk_x: number, index: number, chunk_z: number) {
|
|
return ((chunk_x >> 3) + 65536) * 2 ** 25 + ((chunk_z >> 3) + 65536) * 2 ** 8 + (index >> 2);
|
|
}
|
|
|
|
// what a region does with its buffers, apart so tests can see what's in them without a gpu
|
|
export interface RegionBuffers {
|
|
create(size: number): GPUBuffer;
|
|
write(buffer: GPUBuffer, offset: number, data: Uint8Array<ArrayBuffer>, first: number, size: number): void;
|
|
// as from offset, to offset and size in bytes, three numbers each
|
|
copy(from: GPUBuffer, to: GPUBuffer, ranges: number[]): void;
|
|
destroy(buffer: GPUBuffer): void;
|
|
}
|
|
|
|
const GPU_BUFFERS: RegionBuffers = {
|
|
create: create_copyable_vertex_buffer,
|
|
write: write_buffer,
|
|
copy: copy_buffer,
|
|
destroy: destroy_buffer,
|
|
};
|
|
|
|
interface LayerBuffer {
|
|
buffer: GPUBuffer | undefined;
|
|
// its sections in the order their quads are in each group's part of the buffer
|
|
sections: RenderSection[];
|
|
// meshed since it was last put together, with their quads as the chunk worker sent them
|
|
meshed: Map<RenderSection, Uint8Array<ArrayBuffer>>;
|
|
// whether any of its sections were meshed or lost their quads since
|
|
changed: boolean;
|
|
}
|
|
|
|
const empty_layer = (): LayerBuffer => ({ buffer: undefined, sections: [], meshed: new Map(), changed: false });
|
|
|
|
export class RenderRegion {
|
|
layers: Record<RegionLayer, LayerBuffer> = { solid: empty_layer(), cutout: empty_layer() };
|
|
// the loaded chunks' sections in it, so it goes when they do
|
|
section_count = 0;
|
|
|
|
constructor(readonly buffers = GPU_BUFFERS) {}
|
|
|
|
// a section's new quads in a layer, put in the buffer by the next update. section.meshes[layer].groups say where
|
|
// each group's quads are in vertices, and after the update where they are in the buffer
|
|
meshed(layer: RegionLayer, section: RenderSection, vertices: Uint8Array<ArrayBuffer>) {
|
|
this.layers[layer].meshed.set(section, vertices);
|
|
this.layers[layer].changed = true;
|
|
}
|
|
|
|
// a section's quads in a layer went, the next update takes them out
|
|
removed(layer: RegionLayer, section: RenderSection) {
|
|
this.layers[layer].meshed.delete(section);
|
|
this.layers[layer].changed = true;
|
|
}
|
|
|
|
// puts each layer's buffer together again: a new one, with the quads of the sections that didn't change copied over
|
|
// from the old one on the gpu, and the new quads written to it. a section with no quads in a layer anymore isn't
|
|
// in it. only for regions whose sections changed since, see ClientLevel.update_regions
|
|
update() {
|
|
for (const layer of REGION_LAYERS) {
|
|
const old = this.layers[layer];
|
|
if (!old.changed) continue;
|
|
const sections = old.sections.filter((section) => section.meshes[layer] && !old.meshed.has(section));
|
|
sections.push(...old.meshed.keys());
|
|
|
|
// where each group of each section goes, group after group
|
|
const firsts = new Uint32Array(sections.length * FACE_GROUPS);
|
|
let quads = 0;
|
|
for (let g = 0; g < FACE_GROUPS; g++) {
|
|
for (let i = 0; i < sections.length; i++) {
|
|
firsts[i * FACE_GROUPS + g] = quads;
|
|
quads += sections[i].meshes[layer]!.groups![g].count;
|
|
}
|
|
}
|
|
|
|
const buffer = quads > 0 ? this.buffers.create(quads * TERRAIN_QUAD_BYTES) : undefined;
|
|
// copies from the old buffer, one per run of quads that's together in both
|
|
const copies: number[] = [];
|
|
for (let g = 0; g < FACE_GROUPS && buffer; g++) {
|
|
for (let i = 0; i < sections.length; i++) {
|
|
const group = sections[i].meshes[layer]!.groups![g];
|
|
if (group.count === 0) continue;
|
|
const from = group.first * TERRAIN_QUAD_BYTES;
|
|
const to = firsts[i * FACE_GROUPS + g] * TERRAIN_QUAD_BYTES;
|
|
const size = group.count * TERRAIN_QUAD_BYTES;
|
|
const vertices = old.meshed.get(sections[i]);
|
|
if (vertices) {
|
|
this.buffers.write(buffer, to, vertices, from, size);
|
|
continue;
|
|
}
|
|
const last = copies.length - 3;
|
|
if (
|
|
last >= 0 && copies[last] + copies[last + 2] === from &&
|
|
copies[last + 1] + copies[last + 2] === to
|
|
) {
|
|
copies[last + 2] += size;
|
|
} else {
|
|
copies.push(from, to, size);
|
|
}
|
|
}
|
|
}
|
|
if (old.buffer) {
|
|
if (buffer) this.buffers.copy(old.buffer, buffer, copies);
|
|
this.buffers.destroy(old.buffer);
|
|
}
|
|
for (let i = 0; i < sections.length; i++) {
|
|
const mesh = sections[i].meshes[layer]!;
|
|
for (let g = 0; g < FACE_GROUPS; g++) mesh.groups![g].first = firsts[i * FACE_GROUPS + g];
|
|
mesh.slot = i;
|
|
}
|
|
this.layers[layer] = { buffer, sections, meshed: new Map(), changed: false };
|
|
}
|
|
}
|
|
|
|
dispose() {
|
|
for (const layer of REGION_LAYERS) {
|
|
if (this.layers[layer].buffer) this.buffers.destroy(this.layers[layer].buffer!);
|
|
this.layers[layer] = empty_layer();
|
|
}
|
|
}
|
|
}
|