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;
|
||||
|
||||
Reference in New Issue
Block a user