Render regions, disabled by default
This commit is contained in:
@@ -17,7 +17,6 @@ import {
|
||||
EverythingRegistry,
|
||||
is_fluid_block,
|
||||
light_tables,
|
||||
RENDER_LAYERS,
|
||||
RenderLayer,
|
||||
} from "@/common/everything_registry.ts";
|
||||
import { AIR, CHUNK_AREA, CHUNK_SIZE, Faces, ID_MASK, VOID } from "../../common/constants.ts";
|
||||
@@ -26,9 +25,10 @@ import { ChunkColumn, column_heights, section_index, type SectionBiomes, type Se
|
||||
import { AssetManager } from "../assets.ts";
|
||||
import { ChunkWorkerPool } from "../chunk_workers.ts";
|
||||
import type { FaceGroup, FromChunkWorker, SectionLight } from "../workers/chunk_messages.ts";
|
||||
import { create_index_buffer, create_vertex_buffer, destroy_buffer, Texture } from "../renderer/mod.ts";
|
||||
import { create_index_buffer, create_vertex_buffer, destroy_buffer, render_debug, Texture } from "../renderer/mod.ts";
|
||||
import { crosses_planes } from "../workers/translucent_sort.ts";
|
||||
import { ALL_VISIBLE } from "../workers/visibility.ts";
|
||||
import { region_key, REGION_LAYERS, type RegionLayer, RenderRegion } from "./render_region.ts";
|
||||
import { Camera } from "../camera.ts";
|
||||
import type { Entity } from "../entity/entity.ts";
|
||||
import type { ModelJson } from "@/common/block_models.ts";
|
||||
@@ -79,15 +79,20 @@ export interface RenderSection {
|
||||
// client/rendering/occlusion.ts
|
||||
search: number;
|
||||
incoming: number;
|
||||
// where its solid and cutout quads are, see render_region.ts
|
||||
region: RenderRegion;
|
||||
}
|
||||
|
||||
export interface ChunkMesh {
|
||||
vertex_buffer: GPUBuffer;
|
||||
quad_count: number;
|
||||
// the translucent layer's quads sorted back to front, the others are drawn in order
|
||||
index_buffer?: GPUBuffer;
|
||||
// solid and cutout quads by the way they face, see FACE_GROUPS in chunk_messages.ts
|
||||
groups?: FaceGroup[];
|
||||
// the section's own buffer, the translucent layer's quads sorted back to front by index_buffer. solid and cutout
|
||||
// quads are in its region's buffer instead with the render_regions switch on, see render_region.ts, and slot is
|
||||
// the section's place in the order the region's sections are in it
|
||||
vertex_buffer?: GPUBuffer;
|
||||
index_buffer?: GPUBuffer;
|
||||
slot?: number;
|
||||
}
|
||||
|
||||
interface TranslucentSort {
|
||||
@@ -142,6 +147,11 @@ export class ClientLevel {
|
||||
#sorted = new Set<RenderSection>();
|
||||
// the sections with quads or visuals, the only ones there are to draw
|
||||
drawn = new Set<RenderSection>();
|
||||
// by region_key, and the ones whose sections' meshes changed since update_regions
|
||||
regions = new Map<number, RenderRegion>();
|
||||
#changed_regions = new Set<RenderRegion>();
|
||||
// whether solid and cutout quads go in their regions' buffers, see use_render_regions
|
||||
#render_regions = render_debug.render_regions;
|
||||
// bumped whenever a chunk comes or goes or a section is meshed, which can change what the camera sees
|
||||
sections_version = 0;
|
||||
second_timer = 0;
|
||||
@@ -250,6 +260,13 @@ export class ClientLevel {
|
||||
} else {
|
||||
const chunk: Chunk = { x, z, column, sky, biome_colors, sections: [] };
|
||||
for (let index = 0; index < column.sections.length; index++) {
|
||||
const key = region_key(x, index, z);
|
||||
let region = this.regions.get(key);
|
||||
if (!region) {
|
||||
region = new RenderRegion();
|
||||
this.regions.set(key, region);
|
||||
}
|
||||
region.section_count += 1;
|
||||
chunk.sections.push({
|
||||
chunk,
|
||||
index,
|
||||
@@ -260,6 +277,7 @@ export class ClientLevel {
|
||||
visibility: ALL_VISIBLE,
|
||||
search: 0,
|
||||
incoming: 0,
|
||||
region,
|
||||
});
|
||||
}
|
||||
this.chunks.set(chunk_key(x, z), chunk);
|
||||
@@ -292,6 +310,9 @@ export class ClientLevel {
|
||||
for (const section of chunk.sections) this.#delete_mesh(section);
|
||||
}
|
||||
this.visuals.dispose();
|
||||
for (const region of this.regions.values()) region.dispose();
|
||||
this.regions.clear();
|
||||
this.#changed_regions.clear();
|
||||
this.chunks.clear();
|
||||
this.#dirty.clear();
|
||||
this.drawn.clear();
|
||||
@@ -528,6 +549,12 @@ export class ClientLevel {
|
||||
for (const section of chunk.sections) {
|
||||
this.#delete_mesh(section);
|
||||
this.drawn.delete(section);
|
||||
section.region.section_count -= 1;
|
||||
if (section.region.section_count === 0) {
|
||||
section.region.dispose();
|
||||
this.regions.delete(region_key(cx, section.index, cz));
|
||||
this.#changed_regions.delete(section.region);
|
||||
}
|
||||
}
|
||||
this.#dirty.delete(chunk);
|
||||
this.chunks.delete(key);
|
||||
@@ -715,19 +742,29 @@ export class ClientLevel {
|
||||
else this.drawn.delete(section);
|
||||
this.visuals.meshed(chunk.x, chunk.z, this.shape.min_y + section.index * SECTION_HEIGHT, mesh.visuals);
|
||||
|
||||
for (const layer of RENDER_LAYERS) {
|
||||
for (const layer of REGION_LAYERS) {
|
||||
const { vertices, quad_count, groups } = mesh[layer];
|
||||
if (quad_count === 0) {
|
||||
continue;
|
||||
}
|
||||
section.meshes[layer] = { vertex_buffer: create_vertex_buffer(vertices), quad_count, groups };
|
||||
if (this.#render_regions) {
|
||||
section.meshes[layer] = { quad_count, groups };
|
||||
section.region.meshed(layer, section, vertices);
|
||||
this.#changed_regions.add(section.region);
|
||||
} else {
|
||||
section.meshes[layer] = { quad_count, groups, vertex_buffer: create_vertex_buffer(vertices) };
|
||||
}
|
||||
}
|
||||
|
||||
const translucent = mesh.translucent;
|
||||
if (translucent.quad_count === 0) {
|
||||
continue;
|
||||
}
|
||||
section.meshes.translucent!.index_buffer = create_index_buffer(translucent.indices);
|
||||
section.meshes.translucent = {
|
||||
quad_count: translucent.quad_count,
|
||||
vertex_buffer: create_vertex_buffer(translucent.vertices),
|
||||
index_buffer: create_index_buffer(translucent.indices),
|
||||
};
|
||||
if (translucent.sort_type === "dynamic") {
|
||||
this.#sorted.add(section);
|
||||
section.translucent_sort = {
|
||||
@@ -758,10 +795,13 @@ export class ClientLevel {
|
||||
}
|
||||
|
||||
#delete_mesh(section: RenderSection) {
|
||||
for (const mesh of Object.values(section.meshes)) {
|
||||
destroy_buffer(mesh.vertex_buffer);
|
||||
if (mesh.index_buffer) {
|
||||
destroy_buffer(mesh.index_buffer);
|
||||
for (const [layer, mesh] of Object.entries(section.meshes)) {
|
||||
if (mesh.vertex_buffer) {
|
||||
destroy_buffer(mesh.vertex_buffer);
|
||||
if (mesh.index_buffer) destroy_buffer(mesh.index_buffer);
|
||||
} else {
|
||||
section.region.removed(layer as RegionLayer, section);
|
||||
this.#changed_regions.add(section.region);
|
||||
}
|
||||
}
|
||||
section.meshes = {};
|
||||
@@ -769,6 +809,21 @@ export class ClientLevel {
|
||||
this.#sorted.delete(section);
|
||||
}
|
||||
|
||||
// render regions draw solid and cutout terrain in about a fifth of the draw calls, but in firefox that didn't make
|
||||
// frames any faster and the gpu took longer, so they're the f3 window's render_regions switch, off by default.
|
||||
// switching meshes everything again, each section's quads going in its own buffer or its region's as they come back
|
||||
use_render_regions(on: boolean) {
|
||||
if (on === this.#render_regions) return;
|
||||
this.#render_regions = on;
|
||||
this.remesh_all();
|
||||
}
|
||||
|
||||
// puts the buffers of the regions whose sections' solid or cutout quads changed together again, before they're drawn
|
||||
update_regions() {
|
||||
for (const region of this.#changed_regions) region.update();
|
||||
this.#changed_regions.clear();
|
||||
}
|
||||
|
||||
// sections that need meshing, some waiting on their chunk's neighbors to load
|
||||
get dirty_count() {
|
||||
let count = 0;
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user