From 4fb392adae2fd43525f0dfa171fc30ddc00d8b0e Mon Sep 17 00:00:00 2001 From: Paula Date: Fri, 2 Oct 2026 22:25:47 -0300 Subject: [PATCH] A little more optimization --- client/gui/debug_overlay.ts | 35 +++++++++++++++++------- client/level/client_level.ts | 52 +++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 26 deletions(-) diff --git a/client/gui/debug_overlay.ts b/client/gui/debug_overlay.ts index 1e94080..2a901de 100644 --- a/client/gui/debug_overlay.ts +++ b/client/gui/debug_overlay.ts @@ -5,8 +5,15 @@ import { profiler, type SectionStats } from "$/client/profiler.ts"; import { canvas, gpu_timer, render_debug, render_stats } from "$/client/renderer/mod.ts"; import { RENDER_LAYERS } from "$/common/everything_registry.ts"; +// how often the totals that go through every chunk are worked out again, so the overlay doesn't slow the frames it's +// measuring +const CHUNK_STATS_MS = 1000; + // f3: every entity's fields, editable, and what frames cost export class DebugOverlay { + #chunk_stats = { meshed: 0, bytes: 0 }; + #chunk_stats_time = -Infinity; + render(client: Client) { this.#render_performance(client); @@ -73,17 +80,9 @@ export class DebugOverlay { for (const layer of RENDER_LAYERS) text(` ${layer}: ${quads[layer].toLocaleString()} quads`); const level = client.level; const level_renderer = client.game_renderer.level_renderer; - let meshed = 0; - let waiting = 0; - let bytes = 0; - for (const chunk of level.chunks.values()) { - const meshes = Object.values(chunk.meshes); - if (meshes.length > 0) meshed += 1; - if (chunk.dirty) waiting += 1; - for (const mesh of meshes) bytes += mesh.vertex_buffer.size + (mesh.index_buffer?.size ?? 0); - } + const { meshed, bytes } = this.#count_chunks(client); text(`chunks: ${level.chunks.size} loaded, ${meshed} meshed, ${level_renderer.visible_chunks} in view`); - text(` ${waiting} waiting to mesh, ${level.meshes_in_flight} meshing`); + text(` ${level.dirty_count} waiting to mesh, ${level.meshes_in_flight} meshing`); text(` meshes: ${(bytes / 1024 / 1024).toFixed(1)} MB`); separator(); @@ -99,6 +98,22 @@ export class DebugOverlay { } } + // meshed chunks and what their meshes take on the gpu, once a second + #count_chunks(client: Client) { + const now = performance.now(); + if (now - this.#chunk_stats_time < CHUNK_STATS_MS) return this.#chunk_stats; + this.#chunk_stats_time = now; + let meshed = 0; + let bytes = 0; + for (const chunk of client.level.chunks.values()) { + const meshes = Object.values(chunk.meshes); + if (meshes.length > 0) meshed += 1; + for (const mesh of meshes) bytes += mesh.vertex_buffer.size + (mesh.index_buffer?.size ?? 0); + } + this.#chunk_stats = { meshed, bytes }; + return this.#chunk_stats; + } + // deno-lint-ignore no-explicit-any #render_fields(object: any) { for (const key in object) { diff --git a/client/level/client_level.ts b/client/level/client_level.ts index 693abdf..df3ce50 100644 --- a/client/level/client_level.ts +++ b/client/level/client_level.ts @@ -96,6 +96,9 @@ export const FULL_SKY_LIGHT = 15; export class ClientLevel { image: Texture = AssetManager.instance.get("bworld:textures"); chunks = new Map(); + // the chunks request_meshes and update_translucent_sorting look at, so they don't go through every chunk each frame + #dirty = new Set(); + #sorted = new Set(); second_timer = 0; tick_timer = 0; // world time in ticks, counted here between the server's updates. see common/time.ts @@ -176,9 +179,9 @@ export class ClientLevel { // its old mesh stays up until the new one is ready chunk.blocks = blocks; chunk.biome_colors = biome_colors; - chunk.dirty = true; + this.#mark_dirty(chunk); } else { - this.chunks.set(chunk_key(x, z), { + const chunk: Chunk = { x, z, blocks, @@ -188,13 +191,15 @@ export class ClientLevel { meshes: {}, min_y: 0, max_y: 0, - }); + }; + this.chunks.set(chunk_key(x, z), chunk); + this.#dirty.add(chunk); } // their border faces and light depend on this one for (const [dx, dz] of ALL_NEIGHBOR_OFFSETS) { const neighbor = this.get_chunk(x + dx, z + dz); if (neighbor) { - neighbor.dirty = true; + this.#mark_dirty(neighbor); } } } @@ -222,6 +227,7 @@ export class ClientLevel { this.delete_chunk_mesh(chunk); } this.chunks.clear(); + this.#dirty.clear(); } get_chunk(x: number, z: number) { @@ -247,7 +253,7 @@ export class ClientLevel { const old_nid = chunk.blocks[index] & ID_MASK; chunk.blocks[index] = state === undefined ? default_block_value(nid, info) : block_value(nid, state); - chunk.dirty = true; + this.#mark_dirty(chunk); this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, nid); } @@ -325,7 +331,7 @@ export class ClientLevel { const index = ly * CHUNK_SIZE * CHUNK_SIZE + lz * CHUNK_SIZE + lx; const old_nid = chunk.blocks[index] & ID_MASK; chunk.blocks[index] = AIR; - chunk.dirty = true; + this.#mark_dirty(chunk); this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, AIR); } @@ -348,7 +354,7 @@ export class ClientLevel { for (const [dx, dz] of ALL_NEIGHBOR_OFFSETS) { const n = this.get_chunk(block_chunk_x + dx, block_chunk_z + dz); if (n) { - n.dirty = true; + this.#mark_dirty(n); } } return; @@ -357,23 +363,23 @@ export class ClientLevel { if (lx === 0) { const n = this.get_chunk(block_chunk_x - 1, block_chunk_z); if (n) { - n.dirty = true; + this.#mark_dirty(n); } } else if (lx === CHUNK_SIZE - 1) { const n = this.get_chunk(block_chunk_x + 1, block_chunk_z); if (n) { - n.dirty = true; + this.#mark_dirty(n); } } if (lz === 0) { const n = this.get_chunk(block_chunk_x, block_chunk_z - 1); if (n) { - n.dirty = true; + this.#mark_dirty(n); } } else if (lz === CHUNK_SIZE - 1) { const n = this.get_chunk(block_chunk_x, block_chunk_z + 1); if (n) { - n.dirty = true; + this.#mark_dirty(n); } } } @@ -419,6 +425,7 @@ export class ClientLevel { } this.delete_chunk_mesh(chunk); this.chunks.delete(key); + this.#dirty.delete(chunk); this.block_items.delete(key); } @@ -455,11 +462,12 @@ export class ClientLevel { // sends every dirty chunk that can be meshed to the workers request_meshes(camera: Camera) { - for (const chunk of this.chunks.values()) { - if (!chunk.dirty || !this.can_mesh(chunk)) { + for (const chunk of this.#dirty) { + if (!this.can_mesh(chunk)) { continue; } chunk.dirty = false; + this.#dirty.delete(chunk); chunk.mesh_version += 1; // copies, the worker lights the whole 3x3 area const chunks: (Uint32Array | null)[] = []; @@ -488,9 +496,9 @@ export class ClientLevel { // planes they lie on, so that's the only time they get sorted again update_translucent_sorting(camera: Camera) { const position = [camera.x, camera.y, camera.z]; - for (const chunk of this.chunks.values()) { - const sort = chunk.translucent_sort; - if (!sort || !crosses_planes(sort.planes, sort.camera, position)) { + for (const chunk of this.#sorted) { + const sort = chunk.translucent_sort!; + if (!crosses_planes(sort.planes, sort.camera, position)) { continue; } sort.camera = position; @@ -542,6 +550,7 @@ export class ClientLevel { } chunk.meshes.translucent!.index_buffer = create_index_buffer(translucent.indices); if (translucent.sort_type === "dynamic") { + this.#sorted.add(chunk); chunk.translucent_sort = { mesh_version: message.version, centers: translucent.centers, @@ -577,6 +586,17 @@ export class ClientLevel { } chunk.meshes = {}; chunk.translucent_sort = undefined; + this.#sorted.delete(chunk); + } + + // chunks that need meshing, some waiting on their neighbors to load + get dirty_count() { + return this.#dirty.size; + } + + #mark_dirty(chunk: Chunk) { + chunk.dirty = true; + this.#dirty.add(chunk); } // the closest entity whose box is along the view of something at x/y/z looking at yaw/pitch, within max_distance.