A little more optimization
This commit is contained in:
+25
-10
@@ -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 { canvas, gpu_timer, render_debug, render_stats } from "$/client/renderer/mod.ts";
|
||||||
import { RENDER_LAYERS } from "$/common/everything_registry.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
|
// f3: every entity's fields, editable, and what frames cost
|
||||||
export class DebugOverlay {
|
export class DebugOverlay {
|
||||||
|
#chunk_stats = { meshed: 0, bytes: 0 };
|
||||||
|
#chunk_stats_time = -Infinity;
|
||||||
|
|
||||||
render(client: Client) {
|
render(client: Client) {
|
||||||
this.#render_performance(client);
|
this.#render_performance(client);
|
||||||
|
|
||||||
@@ -73,17 +80,9 @@ export class DebugOverlay {
|
|||||||
for (const layer of RENDER_LAYERS) text(` ${layer}: ${quads[layer].toLocaleString()} quads`);
|
for (const layer of RENDER_LAYERS) text(` ${layer}: ${quads[layer].toLocaleString()} quads`);
|
||||||
const level = client.level;
|
const level = client.level;
|
||||||
const level_renderer = client.game_renderer.level_renderer;
|
const level_renderer = client.game_renderer.level_renderer;
|
||||||
let meshed = 0;
|
const { meshed, bytes } = this.#count_chunks(client);
|
||||||
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);
|
|
||||||
}
|
|
||||||
text(`chunks: ${level.chunks.size} loaded, ${meshed} meshed, ${level_renderer.visible_chunks} in view`);
|
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`);
|
text(` meshes: ${(bytes / 1024 / 1024).toFixed(1)} MB`);
|
||||||
separator();
|
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
|
// deno-lint-ignore no-explicit-any
|
||||||
#render_fields(object: any) {
|
#render_fields(object: any) {
|
||||||
for (const key in object) {
|
for (const key in object) {
|
||||||
|
|||||||
@@ -96,6 +96,9 @@ export const FULL_SKY_LIGHT = 15;
|
|||||||
export class ClientLevel {
|
export class ClientLevel {
|
||||||
image: Texture = AssetManager.instance.get("bworld:textures");
|
image: Texture = AssetManager.instance.get("bworld:textures");
|
||||||
chunks = new Map<number, Chunk>();
|
chunks = new Map<number, Chunk>();
|
||||||
|
// the chunks request_meshes and update_translucent_sorting look at, so they don't go through every chunk each frame
|
||||||
|
#dirty = new Set<Chunk>();
|
||||||
|
#sorted = new Set<Chunk>();
|
||||||
second_timer = 0;
|
second_timer = 0;
|
||||||
tick_timer = 0;
|
tick_timer = 0;
|
||||||
// world time in ticks, counted here between the server's updates. see common/time.ts
|
// 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
|
// its old mesh stays up until the new one is ready
|
||||||
chunk.blocks = blocks;
|
chunk.blocks = blocks;
|
||||||
chunk.biome_colors = biome_colors;
|
chunk.biome_colors = biome_colors;
|
||||||
chunk.dirty = true;
|
this.#mark_dirty(chunk);
|
||||||
} else {
|
} else {
|
||||||
this.chunks.set(chunk_key(x, z), {
|
const chunk: Chunk = {
|
||||||
x,
|
x,
|
||||||
z,
|
z,
|
||||||
blocks,
|
blocks,
|
||||||
@@ -188,13 +191,15 @@ export class ClientLevel {
|
|||||||
meshes: {},
|
meshes: {},
|
||||||
min_y: 0,
|
min_y: 0,
|
||||||
max_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
|
// their border faces and light depend on this one
|
||||||
for (const [dx, dz] of ALL_NEIGHBOR_OFFSETS) {
|
for (const [dx, dz] of ALL_NEIGHBOR_OFFSETS) {
|
||||||
const neighbor = this.get_chunk(x + dx, z + dz);
|
const neighbor = this.get_chunk(x + dx, z + dz);
|
||||||
if (neighbor) {
|
if (neighbor) {
|
||||||
neighbor.dirty = true;
|
this.#mark_dirty(neighbor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -222,6 +227,7 @@ export class ClientLevel {
|
|||||||
this.delete_chunk_mesh(chunk);
|
this.delete_chunk_mesh(chunk);
|
||||||
}
|
}
|
||||||
this.chunks.clear();
|
this.chunks.clear();
|
||||||
|
this.#dirty.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
get_chunk(x: number, z: number) {
|
get_chunk(x: number, z: number) {
|
||||||
@@ -247,7 +253,7 @@ export class ClientLevel {
|
|||||||
|
|
||||||
const old_nid = chunk.blocks[index] & ID_MASK;
|
const old_nid = chunk.blocks[index] & ID_MASK;
|
||||||
chunk.blocks[index] = state === undefined ? default_block_value(nid, info) : block_value(nid, state);
|
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);
|
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 index = ly * CHUNK_SIZE * CHUNK_SIZE + lz * CHUNK_SIZE + lx;
|
||||||
const old_nid = chunk.blocks[index] & ID_MASK;
|
const old_nid = chunk.blocks[index] & ID_MASK;
|
||||||
chunk.blocks[index] = AIR;
|
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);
|
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) {
|
for (const [dx, dz] of ALL_NEIGHBOR_OFFSETS) {
|
||||||
const n = this.get_chunk(block_chunk_x + dx, block_chunk_z + dz);
|
const n = this.get_chunk(block_chunk_x + dx, block_chunk_z + dz);
|
||||||
if (n) {
|
if (n) {
|
||||||
n.dirty = true;
|
this.#mark_dirty(n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -357,23 +363,23 @@ export class ClientLevel {
|
|||||||
if (lx === 0) {
|
if (lx === 0) {
|
||||||
const n = this.get_chunk(block_chunk_x - 1, block_chunk_z);
|
const n = this.get_chunk(block_chunk_x - 1, block_chunk_z);
|
||||||
if (n) {
|
if (n) {
|
||||||
n.dirty = true;
|
this.#mark_dirty(n);
|
||||||
}
|
}
|
||||||
} else if (lx === CHUNK_SIZE - 1) {
|
} else if (lx === CHUNK_SIZE - 1) {
|
||||||
const n = this.get_chunk(block_chunk_x + 1, block_chunk_z);
|
const n = this.get_chunk(block_chunk_x + 1, block_chunk_z);
|
||||||
if (n) {
|
if (n) {
|
||||||
n.dirty = true;
|
this.#mark_dirty(n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (lz === 0) {
|
if (lz === 0) {
|
||||||
const n = this.get_chunk(block_chunk_x, block_chunk_z - 1);
|
const n = this.get_chunk(block_chunk_x, block_chunk_z - 1);
|
||||||
if (n) {
|
if (n) {
|
||||||
n.dirty = true;
|
this.#mark_dirty(n);
|
||||||
}
|
}
|
||||||
} else if (lz === CHUNK_SIZE - 1) {
|
} else if (lz === CHUNK_SIZE - 1) {
|
||||||
const n = this.get_chunk(block_chunk_x, block_chunk_z + 1);
|
const n = this.get_chunk(block_chunk_x, block_chunk_z + 1);
|
||||||
if (n) {
|
if (n) {
|
||||||
n.dirty = true;
|
this.#mark_dirty(n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -419,6 +425,7 @@ export class ClientLevel {
|
|||||||
}
|
}
|
||||||
this.delete_chunk_mesh(chunk);
|
this.delete_chunk_mesh(chunk);
|
||||||
this.chunks.delete(key);
|
this.chunks.delete(key);
|
||||||
|
this.#dirty.delete(chunk);
|
||||||
this.block_items.delete(key);
|
this.block_items.delete(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -455,11 +462,12 @@ export class ClientLevel {
|
|||||||
|
|
||||||
// sends every dirty chunk that can be meshed to the workers
|
// sends every dirty chunk that can be meshed to the workers
|
||||||
request_meshes(camera: Camera) {
|
request_meshes(camera: Camera) {
|
||||||
for (const chunk of this.chunks.values()) {
|
for (const chunk of this.#dirty) {
|
||||||
if (!chunk.dirty || !this.can_mesh(chunk)) {
|
if (!this.can_mesh(chunk)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
chunk.dirty = false;
|
chunk.dirty = false;
|
||||||
|
this.#dirty.delete(chunk);
|
||||||
chunk.mesh_version += 1;
|
chunk.mesh_version += 1;
|
||||||
// copies, the worker lights the whole 3x3 area
|
// copies, the worker lights the whole 3x3 area
|
||||||
const chunks: (Uint32Array | null)[] = [];
|
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
|
// planes they lie on, so that's the only time they get sorted again
|
||||||
update_translucent_sorting(camera: Camera) {
|
update_translucent_sorting(camera: Camera) {
|
||||||
const position = [camera.x, camera.y, camera.z];
|
const position = [camera.x, camera.y, camera.z];
|
||||||
for (const chunk of this.chunks.values()) {
|
for (const chunk of this.#sorted) {
|
||||||
const sort = chunk.translucent_sort;
|
const sort = chunk.translucent_sort!;
|
||||||
if (!sort || !crosses_planes(sort.planes, sort.camera, position)) {
|
if (!crosses_planes(sort.planes, sort.camera, position)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
sort.camera = position;
|
sort.camera = position;
|
||||||
@@ -542,6 +550,7 @@ export class ClientLevel {
|
|||||||
}
|
}
|
||||||
chunk.meshes.translucent!.index_buffer = create_index_buffer(translucent.indices);
|
chunk.meshes.translucent!.index_buffer = create_index_buffer(translucent.indices);
|
||||||
if (translucent.sort_type === "dynamic") {
|
if (translucent.sort_type === "dynamic") {
|
||||||
|
this.#sorted.add(chunk);
|
||||||
chunk.translucent_sort = {
|
chunk.translucent_sort = {
|
||||||
mesh_version: message.version,
|
mesh_version: message.version,
|
||||||
centers: translucent.centers,
|
centers: translucent.centers,
|
||||||
@@ -577,6 +586,17 @@ export class ClientLevel {
|
|||||||
}
|
}
|
||||||
chunk.meshes = {};
|
chunk.meshes = {};
|
||||||
chunk.translucent_sort = undefined;
|
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.
|
// the closest entity whose box is along the view of something at x/y/z looking at yaw/pitch, within max_distance.
|
||||||
|
|||||||
Reference in New Issue
Block a user