perf(render): keep solid meshes resident

Keep all solid geometry GPU-resident during navigation. Use vertex-rate instancing, compact untextured vertices, vertex-cache ordering, and cached silhouette sources without culling geometry.
This commit is contained in:
Hakan Seven 2026-08-12 16:02:10 +03:00
commit 5e8221f8f1
9 changed files with 756 additions and 1012 deletions

63
Cargo.lock generated
View file

@ -31,6 +31,7 @@ dependencies = [
"js-sys",
"log",
"lyon_tessellation",
"meshopt",
"naga 27.0.3",
"ocs_plugin_api",
"open",
@ -152,7 +153,7 @@ version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
dependencies = [
"memchr 2.8.3",
"memchr",
]
[[package]]
@ -1071,7 +1072,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd"
dependencies = [
"bytes",
"memchr 2.8.3",
"memchr",
]
[[package]]
@ -1694,6 +1695,15 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4"
[[package]]
name = "float-cmp"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b09cf3155332e944990140d967ff5eceb70df778b34f77d8075db46e4704e6d8"
dependencies = [
"num-traits",
]
[[package]]
name = "float_next_after"
version = "1.0.0"
@ -1741,7 +1751,7 @@ version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54f0d287c53ffd184d04d8677f590f4ac5379785529e5e08b1c8083acdd5c198"
dependencies = [
"memchr 2.8.3",
"memchr",
"thiserror 2.0.18",
]
@ -1927,7 +1937,7 @@ dependencies = [
"futures-macro",
"futures-sink",
"futures-task",
"memchr 2.8.3",
"memchr",
"pin-project-lite",
"slab",
]
@ -3055,15 +3065,6 @@ dependencies = [
"digest 0.10.7",
]
[[package]]
name = "memchr"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a"
dependencies = [
"libc",
]
[[package]]
name = "memchr"
version = "2.8.3"
@ -3088,6 +3089,18 @@ dependencies = [
"autocfg",
]
[[package]]
name = "meshopt"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01e77ead21976b3a9f01ec1724f766923da74f0726364e2b0f425658935d71a"
dependencies = [
"bitflags 2.13.1",
"cc",
"float-cmp 0.10.0",
"thiserror 2.0.18",
]
[[package]]
name = "mime"
version = "0.3.17"
@ -3254,7 +3267,7 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418abd1b6d34fbf6cae440dc874771b0525a604428704c76e48b29a5e67b8003"
dependencies = [
"memchr 2.8.3",
"memchr",
]
[[package]]
@ -3263,7 +3276,7 @@ version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr 2.8.3",
"memchr",
"minimal-lexical",
]
@ -3273,7 +3286,7 @@ version = "8.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405"
dependencies = [
"memchr 2.8.3",
"memchr",
]
[[package]]
@ -3283,7 +3296,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b577e2d69827c4740cba2b52efaad1c4cc7c73042860b199710b3575c68438d"
dependencies = [
"bytecount",
"memchr 1.0.2",
"memchr",
"nom 8.0.0",
]
@ -4225,7 +4238,7 @@ checksum = "f86ba2052aebccc42cbbb3ed234b8b13ce76f75c3551a303cb2bcffcff12bb14"
dependencies = [
"bitflags 2.13.1",
"getopts",
"memchr 2.8.3",
"memchr",
"pulldown-cmark-escape",
"unicase",
]
@ -4286,7 +4299,7 @@ version = "0.39.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdcc8dd4e2f670d309a5f0e83fe36dfdc05af317008fea29144da1a2ac858e5e"
dependencies = [
"memchr 2.8.3",
"memchr",
]
[[package]]
@ -4532,7 +4545,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
dependencies = [
"aho-corasick",
"memchr 2.8.3",
"memchr",
"regex-automata",
"regex-syntax",
]
@ -4544,7 +4557,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fcfdb36bda0c880c5931cdc7a2bcdc8ba4556847b9d912bca70bc94708711ad"
dependencies = [
"aho-corasick",
"memchr 2.8.3",
"memchr",
"regex-syntax",
]
@ -4963,7 +4976,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
dependencies = [
"itoa",
"memchr 2.8.3",
"memchr",
"serde",
"serde_core",
"zmij",
@ -5260,7 +5273,7 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
dependencies = [
"float-cmp",
"float-cmp 0.9.0",
]
[[package]]
@ -5629,7 +5642,7 @@ version = "3.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8765b90061cba6c22b5831f675da109ae5561588290f9fa2317adab2714d5a6"
dependencies = [
"memchr 2.8.3",
"memchr",
"nom 8.0.0",
"petgraph",
]
@ -6699,7 +6712,7 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81"
dependencies = [
"memchr 2.8.3",
"memchr",
]
[[package]]

View file

@ -67,6 +67,7 @@ iced_widget = { git = "https://github.com/iced-rs/iced.git", rev = "23604ff22ab0
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ocs_plugin_api = { path = "crates/ocs_plugin_api", features = ["host"] }
meshopt = "0.6.2"
i18n-embed = { version = "0.16", features = ["desktop-requester"] }
rayon = "1"
open = "5"

View file

@ -20,10 +20,6 @@ impl DeviceCapabilities {
/// instances, boundaries, families, dashes, and visibility.
const HATCH_STORAGE_BINDINGS: u32 = 5;
/// Mesh compute culling reads one item buffer and writes four indirect
/// command buffers.
const MESH_CULL_BINDINGS: u32 = 5;
pub fn detect(device: &wgpu::Device) -> Self {
Self::from_limits(&device.limits())
}
@ -36,11 +32,6 @@ impl DeviceCapabilities {
}
}
/// Mesh instancing needs one read-only storage buffer.
pub fn supports_mesh_storage_instancing(self) -> bool {
self.max_storage_buffers_per_shader_stage >= 1
}
pub fn supports_wire_storage(self) -> bool {
self.max_storage_buffers_per_shader_stage >= Self::WIRE_STORAGE_BINDINGS
}
@ -49,10 +40,6 @@ impl DeviceCapabilities {
self.max_storage_buffers_per_shader_stage >= Self::HATCH_STORAGE_BINDINGS
}
/// WebGL2 reports zero storage bindings and stays on CPU mesh culling.
pub fn supports_mesh_compute_culling(self) -> bool {
self.max_storage_buffers_per_shader_stage >= Self::MESH_CULL_BINDINGS
}
}
#[cfg(test)]
@ -63,19 +50,15 @@ mod tests {
#[test]
fn webgl_limits_select_compatibility_paths() {
let caps = DeviceCapabilities::from_limits(&wgpu::Limits::downlevel_webgl2_defaults());
assert!(!caps.supports_mesh_storage_instancing());
assert!(!caps.supports_wire_storage());
assert!(!caps.supports_batched_hatch());
assert!(!caps.supports_mesh_compute_culling());
}
#[test]
fn default_limits_select_storage_paths() {
let caps = DeviceCapabilities::from_limits(&wgpu::Limits::default());
assert!(caps.supports_mesh_storage_instancing());
assert!(caps.supports_wire_storage());
assert!(caps.supports_batched_hatch());
assert!(caps.supports_mesh_compute_culling());
}
#[test]
@ -86,8 +69,6 @@ mod tests {
max_vertex_attributes: 16,
};
assert!(caps.supports_wire_storage());
assert!(caps.supports_mesh_storage_instancing());
assert!(!caps.supports_batched_hatch());
assert!(!caps.supports_mesh_compute_culling());
}
}

View file

@ -94,6 +94,41 @@ impl MeshVertex {
}
}
#[repr(C)]
#[derive(Copy, Clone, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct MeshPlainVertex {
pub position: [f32; 3],
pub normal: [f32; 3],
pub position_low: [f32; 3],
}
impl MeshPlainVertex {
pub fn layout<'a>() -> wgpu::VertexBufferLayout<'a> {
const ATTRS: &[wgpu::VertexAttribute] = &[
wgpu::VertexAttribute {
offset: std::mem::offset_of!(MeshPlainVertex, position) as u64,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: std::mem::offset_of!(MeshPlainVertex, normal) as u64,
shader_location: 1,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: std::mem::offset_of!(MeshPlainVertex, position_low) as u64,
shader_location: 3,
format: wgpu::VertexFormat::Float32x3,
},
];
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Self>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: ATTRS,
}
}
}
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct MeshEdgeVertex {
@ -240,6 +275,51 @@ impl MeshInstanceGpu {
normal_row_2: [n[2] as f32, n[5] as f32, n[8] as f32, 0.0],
}
}
pub fn layout<'a>() -> wgpu::VertexBufferLayout<'a> {
const ATTRS: &[wgpu::VertexAttribute] = &[
wgpu::VertexAttribute {
offset: std::mem::offset_of!(MeshInstanceGpu, model_row_0) as u64,
shader_location: 4,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: std::mem::offset_of!(MeshInstanceGpu, model_row_1) as u64,
shader_location: 5,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: std::mem::offset_of!(MeshInstanceGpu, model_row_2) as u64,
shader_location: 7,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: std::mem::offset_of!(MeshInstanceGpu, translation_low) as u64,
shader_location: 8,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: std::mem::offset_of!(MeshInstanceGpu, normal_row_0) as u64,
shader_location: 9,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: std::mem::offset_of!(MeshInstanceGpu, normal_row_1) as u64,
shader_location: 13,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: std::mem::offset_of!(MeshInstanceGpu, normal_row_2) as u64,
shader_location: 14,
format: wgpu::VertexFormat::Float32x4,
},
];
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Self>() as u64,
step_mode: wgpu::VertexStepMode::Instance,
attributes: ATTRS,
}
}
}
// ── Batched mesh buffers ──────────────────────────────────────────────────
@ -254,6 +334,7 @@ impl MeshInstanceGpu {
pub struct MeshBatchChunk {
pub vertex_buffer: wgpu::Buffer,
pub compact_vertices: bool,
/// Opaque triangle indices (mesh colour alpha ≈ 1). Drawn with depth write.
pub index_buffer: wgpu::Buffer,
pub index_count: u32,
@ -274,8 +355,6 @@ pub struct MeshBatchChunk {
pub instance_count: u32,
pub highlight_ranges: Vec<MeshBatchRange>,
pub handles: rustc_hash::FxHashSet<acadrust::Handle>,
pub world_aabb: [f32; 6],
pub visible: bool,
pub material: Option<crate::scene::model::material_model::MeshMaterial>,
pub face_color: [f32; 4],
pub material_bind_group: Option<wgpu::BindGroup>,
@ -326,7 +405,7 @@ fn make_chunk(
highlight_ranges: &[MeshBatchRange],
instances: &[MeshInstanceGpu],
handles: &rustc_hash::FxHashSet<acadrust::Handle>,
bounds_override: Option<[f32; 6]>,
_bounds_override: Option<[f32; 6]>,
material: Option<&crate::scene::model::material_model::MeshMaterial>,
face_color: [f32; 4],
vertex_buffer_override: Option<wgpu::Buffer>,
@ -358,6 +437,28 @@ fn make_chunk(
queue.write_buffer(&buffer, 0, bytemuck::cast_slice(data));
buffer
};
let compact_vertices = !material_has_textures(material) && wire_indices.is_empty();
let mk_plain_vertex = |data: &[MeshVertex]| {
if data.is_empty() {
return stubs.vertex.clone();
}
let compact: Vec<MeshPlainVertex> = data
.iter()
.map(|vertex| MeshPlainVertex {
position: vertex.position,
normal: vertex.normal,
position_low: vertex.position_low,
})
.collect();
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("mesh.batch.plain_vbuf"),
size: std::mem::size_of_val(compact.as_slice()) as u64,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
queue.write_buffer(&buffer, 0, bytemuck::cast_slice(&compact));
buffer
};
let mk_edge_vertex = |data: &[MeshEdgeVertex], label: &'static str| {
if data.is_empty() {
return stubs.vertex.clone();
@ -377,46 +478,15 @@ fn make_chunk(
} else {
instances
};
let instance_usage =
if device.limits().max_storage_buffers_per_shader_stage > 0 {
wgpu::BufferUsages::STORAGE
} else {
wgpu::BufferUsages::UNIFORM
};
let mut computed_aabb = [
f32::INFINITY,
f32::INFINITY,
f32::INFINITY,
f32::NEG_INFINITY,
f32::NEG_INFINITY,
f32::NEG_INFINITY,
];
for vertex in verts {
let point = [
vertex.position[0] + vertex.position_low[0],
vertex.position[1] + vertex.position_low[1],
vertex.position[2] + vertex.position_low[2],
];
for axis in 0..3 {
computed_aabb[axis] = computed_aabb[axis].min(point[axis]);
computed_aabb[axis + 3] = computed_aabb[axis + 3].max(point[axis]);
}
}
for vertex in edge_verts {
let point = [
vertex.position[0] + vertex.position_low[0],
vertex.position[1] + vertex.position_low[1],
vertex.position[2] + vertex.position_low[2],
];
for axis in 0..3 {
computed_aabb[axis] = computed_aabb[axis].min(point[axis]);
computed_aabb[axis + 3] = computed_aabb[axis + 3].max(point[axis]);
}
}
let world_aabb = bounds_override.unwrap_or(computed_aabb);
MeshBatchChunk {
vertex_buffer: vertex_buffer_override
.unwrap_or_else(|| mk_vertex(verts, "mesh.batch.vbuf")),
vertex_buffer: vertex_buffer_override.unwrap_or_else(|| {
if compact_vertices {
mk_plain_vertex(verts)
} else {
mk_vertex(verts, "mesh.batch.vbuf")
}
}),
compact_vertices,
index_buffer: mk_index(indices, "mesh.batch.ibuf"),
index_count: indices.len() as u32,
transp_index_buffer: mk_index(transp_indices, "mesh.batch.transp_ibuf"),
@ -430,7 +500,7 @@ fn make_chunk(
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("mesh.batch.instances"),
size: std::mem::size_of_val(instance_data) as u64,
usage: instance_usage | wgpu::BufferUsages::COPY_DST,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
queue.write_buffer(&buffer, 0, bytemuck::cast_slice(instance_data));
@ -439,8 +509,6 @@ fn make_chunk(
instance_count: instance_data.len() as u32,
highlight_ranges: highlight_ranges.to_vec(),
handles: handles.clone(),
world_aabb,
visible: true,
material: material.cloned(),
face_color,
material_bind_group: None,
@ -727,29 +795,9 @@ pub fn create_material_bind_group_from_resources(
device: &wgpu::Device,
layout: &wgpu::BindGroupLayout,
resources: &MeshMaterialResources,
instance_buffer: Option<&wgpu::Buffer>,
material: Option<&crate::scene::model::material_model::MeshMaterial>,
face_color: [f32; 4],
) -> wgpu::BindGroup {
let fallback_instances;
let instance_buffer = match instance_buffer {
Some(buffer) => buffer,
None => {
let usage =
if device.limits().max_storage_buffers_per_shader_stage > 0 {
wgpu::BufferUsages::STORAGE
} else {
wgpu::BufferUsages::UNIFORM
};
fallback_instances =
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("mesh.instances.identity"),
contents: bytemuck::bytes_of(&MeshInstanceGpu::identity()),
usage,
});
&fallback_instances
}
};
let (material_params, specular, ambient, advanced, flags) =
material_vertex_params(material);
let surface = MeshSurfaceParams {
@ -829,10 +877,6 @@ pub fn create_material_bind_group_from_resources(
binding: 14,
resource: resources.params_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 15,
resource: instance_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 16,
resource: surface_buffer.as_entire_binding(),
@ -846,7 +890,6 @@ pub fn create_material_bind_group(
queue: &wgpu::Queue,
layout: &wgpu::BindGroupLayout,
material: Option<&crate::scene::model::material_model::MeshMaterial>,
instance_buffer: Option<&wgpu::Buffer>,
) -> wgpu::BindGroup {
let resources = create_material_resources(device, queue, material);
let color = material.map_or([0.8, 0.8, 0.8, 1.0], |material| material.diffuse);
@ -854,7 +897,6 @@ pub fn create_material_bind_group(
device,
layout,
&resources,
instance_buffer,
material,
color,
)
@ -879,7 +921,6 @@ pub fn upload_chunk_material_bind_groups(
device,
layout,
&resources,
Some(&chunk.instance_buffer),
chunk.material.as_ref(),
chunk.face_color,
));
@ -894,7 +935,6 @@ pub fn upload_chunk_material_bind_groups(
device,
layout,
resources,
Some(&chunk.instance_buffer),
chunk.material.as_ref(),
chunk.face_color,
));
@ -1025,6 +1065,18 @@ fn index_hash(indices: &[u32]) -> u64 {
hasher.finish()
}
fn optimize_triangle_indices(indices: &[u32], vertex_count: usize) -> std::sync::Arc<[u32]> {
#[cfg(not(target_arch = "wasm32"))]
{
return meshopt::optimize::optimize_vertex_cache(indices, vertex_count).into();
}
#[cfg(target_arch = "wasm32")]
{
let _ = vertex_count;
indices.into()
}
}
fn visual_style_partition_key(
style: Option<&crate::scene::model::visual_style_model::MeshVisualStyle>,
) -> [u64; 12] {
@ -1061,6 +1113,7 @@ struct InstancedBuildProfile {
struct InstancedVertexKey {
source: u64,
material: u64,
compact: bool,
}
#[derive(Default)]
@ -1102,6 +1155,8 @@ fn build_instanced_chunk(
let material = first.material;
let color = first.color;
let source_handle = source.handle.value();
let compact_vertices = !material_has_textures(material)
&& (!first.include_edges || !source.edge_verts.is_empty());
let material_identity = if material_has_textures(material) {
material.map_or(0, |material| {
material
@ -1114,6 +1169,7 @@ fn build_instanced_chunk(
let vertex_key = InstancedVertexKey {
source: source_handle,
material: material_identity,
compact: compact_vertices,
};
let shared_vertex_buffer = buffers.vertices.get(&vertex_key).cloned();
let has_normals = mesh.normals.len() == mesh.verts.len();
@ -1574,7 +1630,7 @@ pub fn build_mesh_batch_filtered(
let (shared_indices, shared_hash) = source_indices
.entry(source_identity)
.or_insert_with(|| {
let indices = std::sync::Arc::<[u32]>::from(mesh.indices.as_slice());
let indices = optimize_triangle_indices(&mesh.indices, mesh.verts.len());
let hash = index_hash(indices.as_ref());
(indices, hash)
})
@ -1655,7 +1711,7 @@ pub fn build_mesh_batch_filtered(
let parts: Vec<_> = groups
.into_values()
.map(|(material, color, indices)| {
let indices = std::sync::Arc::<[u32]>::from(indices);
let indices = optimize_triangle_indices(&indices, mesh.verts.len());
let index_hash = index_hash(indices.as_ref());
CachedFacePart {
material,
@ -1711,7 +1767,6 @@ pub fn build_mesh_batch_filtered(
mesh_spatial_key(part.set, spatial_bounds),
)
});
let storage_instancing = device.limits().max_storage_buffers_per_shader_stage > 0;
let mut instance_groups: std::collections::BTreeMap<
InstanceGroupKey,
Vec<MeshBatchPart<'_>>,
@ -2173,48 +2228,14 @@ pub fn build_mesh_batch_filtered(
let mut instanced_profile = InstancedBuildProfile::default();
let mut instanced_buffers = InstancedBufferCache::default();
for parts in instance_groups.values() {
let Some(first) = parts.first() else {
continue;
};
let source_bytes = first
.mesh
.verts
.len()
.saturating_mul(std::mem::size_of::<MeshVertex>())
.saturating_add(first.indices.len().saturating_mul(12))
.saturating_add(
first
.set
.instance_source
.as_ref()
.map_or(0, |source| {
source
.edge_verts
.len()
.saturating_mul(std::mem::size_of::<MeshEdgeVertex>())
}),
)
.max(1);
// Keep repeated geometry bounded while retaining useful culling
// granularity. Small block definitions get spatial clusters of roughly
// 64256 INSERTs; a huge source is duplicated only a few times.
let max_clusters = ((64 * 1024 * 1024) / source_bytes).clamp(1, 64);
let cluster_len = if storage_instancing {
parts.len().div_ceil(max_clusters).max(64)
} else {
1
};
for cluster in parts.chunks(cluster_len) {
if let Some((chunk, triangles, profile)) =
build_instanced_chunk(
if let Some((chunk, triangles, profile)) = build_instanced_chunk(
device,
queue,
&stubs,
&mut instanced_buffers,
cluster,
parts,
perf_started.is_some(),
)
{
) {
total_tris += triangles;
chunks.push(chunk);
instanced_profile.vertices += profile.vertices;
@ -2227,7 +2248,6 @@ pub fn build_mesh_batch_filtered(
instanced_profile.chunk_count += profile.chunk_count;
}
}
}
if let Some(started) = perf_started {
let done = std::time::Instant::now();
crate::perf_record!(

File diff suppressed because it is too large Load diff

View file

@ -390,8 +390,7 @@ impl shader::Primitive for Primitive {
inner.wire_cull_key = (u64::MAX, u64::MAX, 0, 0);
inner.hatch_lod_key = (usize::MAX, u64::MAX, 0, 0, false);
inner.wipeout_lod_key = (usize::MAX, u64::MAX, 0, 0, false);
inner.mesh_lod_key = (usize::MAX, u64::MAX, 0, 0);
inner.silhouette_key = (usize::MAX, u64::MAX, u64::MAX, false);
inner.silhouette_key = (usize::MAX, u64::MAX, [u32::MAX; 3], false);
inner.render_sig = u64::MAX;
}
// The MSAA / depth / resolve textures are always sized to the
@ -1039,13 +1038,14 @@ impl shader::Primitive for Primitive {
let silhouette_key = (
Arc::as_ptr(&vp.meshes) as usize,
vp.wire_content_id,
vp.camera_generation,
vp.view_dir.to_array().map(f32::to_bits),
silhouette_enabled,
);
if inner.silhouette_key != silhouette_key {
inner.upload_silhouettes(
device,
if silhouette_enabled { &vp.meshes[..] } else { &[] },
vp.wire_content_id,
vp.view_dir,
);
inner.silhouette_key = silhouette_key;
@ -1073,22 +1073,6 @@ impl shader::Primitive for Primitive {
inner.compute_wipeout_lod(view_rot, eye, clip_size.width, clip_size.height);
inner.wipeout_lod_key = wipeout_lod_key;
}
let mesh_lod_key = (
Arc::as_ptr(&vp.meshes) as usize,
vp.camera_generation,
clip_size.width,
clip_size.height,
);
if inner.mesh_lod_key != mesh_lod_key {
inner.compute_mesh_lod(
queue,
view_rot,
eye,
clip_size.width,
clip_size.height,
);
inner.mesh_lod_key = mesh_lod_key;
}
let cull_key = (
vp.wire_content_id,
vp.camera_generation,
@ -1298,6 +1282,7 @@ fn render_signature(vp: &ViewportData, clip_w: u32, clip_h: u32) -> u64 {
vp.show_2d_solid_fills.hash(&mut h);
vp.mesh_fill.hash(&mut h);
vp.show_3d_edges.hash(&mut h);
vp.display_silhouette.hash(&mut h);
vp.hidden_line.hash(&mut h);
// ViewCube visibility is excluded from the *scene* signature elsewhere only
// for the live-hover pass; here it MUST invalidate the cache so toggling the
@ -3701,6 +3686,7 @@ impl Scene {
} else {
Arc::new(vec![])
};
let navigating = !inst.paper_sheet && self.navigating_lod();
Some(ViewportData {
instance_id,
force_rasterize,
@ -3744,7 +3730,7 @@ impl Scene {
// actively moving; the scene-render cache holds the full-quality
// (hatched) frame once it settles. Only applied to the on-screen
// Model / paper content — the paper *sheet* keeps its fills.
skip_hatch: self.hatch_lod_enabled() && !inst.paper_sheet && self.navigating_lod(),
skip_hatch: self.hatch_lod_enabled() && navigating,
skip_background: !inst.paper_sheet && self.current_layout != "Model",
geometry_epoch: self.geometry_epoch,
camera_generation: self.camera_generation,

View file

@ -98,9 +98,6 @@ struct MeshInstance {
normal_row_2: vec4<f32>,
};
@group(1) @binding(15)
var<storage, read> mesh_instances: array<MeshInstance>;
struct VertexIn {
@location(0) position: vec3<f32>,
@location(1) normal: vec3<f32>,
@ -111,6 +108,22 @@ struct VertexIn {
@location(12) uv_refraction_normal: vec4<f32>,
};
struct PlainVertexIn {
@location(0) position: vec3<f32>,
@location(1) normal: vec3<f32>,
@location(3) position_low: vec3<f32>,
};
struct InstanceIn {
@location(4) model_row_0: vec4<f32>,
@location(5) model_row_1: vec4<f32>,
@location(7) model_row_2: vec4<f32>,
@location(8) translation_low: vec4<f32>,
@location(9) normal_row_0: vec4<f32>,
@location(13) normal_row_1: vec4<f32>,
@location(14) normal_row_2: vec4<f32>,
};
struct VertexOut {
@builtin(position) clip_pos: vec4<f32>,
@location(0) normal: vec3<f32>,
@ -153,13 +166,25 @@ fn relative_position(
return (world_high - u.eye_high) + (world_low - u.eye_low);
}
fn mesh_instance(input: InstanceIn) -> MeshInstance {
return MeshInstance(
input.model_row_0,
input.model_row_1,
input.model_row_2,
input.translation_low,
input.normal_row_0,
input.normal_row_1,
input.normal_row_2,
);
}
@vertex
fn vs_main(
v: VertexIn,
@builtin(instance_index) instance_index: u32,
i: InstanceIn,
) -> VertexOut {
var out: VertexOut;
let instance = mesh_instances[instance_index];
let instance = mesh_instance(i);
let rel = relative_position(v.position, v.position_low, instance);
out.clip_pos = u.view_rot * vec4<f32>(rel, 1.0);
out.normal = normalize(vec3<f32>(
@ -178,13 +203,38 @@ fn vs_main(
return out;
}
@vertex
fn vs_main_plain(
v: PlainVertexIn,
i: InstanceIn,
) -> VertexOut {
var out: VertexOut;
let instance = mesh_instance(i);
let rel = relative_position(v.position, v.position_low, instance);
out.clip_pos = u.view_rot * vec4<f32>(rel, 1.0);
out.normal = normalize(vec3<f32>(
dot(instance.normal_row_0.xyz, v.normal),
dot(instance.normal_row_1.xyz, v.normal),
dot(instance.normal_row_2.xyz, v.normal),
));
out.world_pos = rel;
out.uv_diffuse = vec2<f32>(0.0);
out.uv_specular = vec2<f32>(0.0);
out.uv_reflection = vec2<f32>(0.0);
out.uv_opacity = vec2<f32>(0.0);
out.uv_bump = vec2<f32>(0.0);
out.uv_refraction = vec2<f32>(0.0);
out.uv_normal = vec2<f32>(0.0);
return out;
}
@vertex
fn vs_edge(
v: EdgeVertexIn,
@builtin(instance_index) instance_index: u32,
i: InstanceIn,
) -> EdgeVertexOut {
var out: EdgeVertexOut;
let instance = mesh_instances[instance_index];
let instance = mesh_instance(i);
let rel = relative_position(v.position, v.position_low, instance);
out.clip_pos = u.view_rot * vec4<f32>(rel, 1.0);
out.color = v.color;

View file

@ -1,78 +0,0 @@
struct CullUniform {
view_rot: mat4x4<f32>,
eye: vec4<f32>,
count: vec4<u32>,
};
struct CullItem {
min: vec4<f32>,
max: vec4<f32>,
counts: vec4<u32>,
info: vec4<u32>,
};
struct DrawIndexed {
index_count: u32,
instance_count: u32,
first_index: u32,
base_vertex: u32,
first_instance: u32,
};
struct Draw {
vertex_count: u32,
instance_count: u32,
first_vertex: u32,
first_instance: u32,
};
@group(0) @binding(0) var<uniform> u: CullUniform;
@group(0) @binding(1) var<storage, read> items: array<CullItem>;
@group(0) @binding(2) var<storage, read_write> opaque: array<DrawIndexed>;
@group(0) @binding(3) var<storage, read_write> transparent: array<DrawIndexed>;
@group(0) @binding(4) var<storage, read_write> wire: array<DrawIndexed>;
@group(0) @binding(5) var<storage, read_write> edge: array<Draw>;
fn chunk_visible(item: CullItem) -> bool {
if (item.info.y == 0u) {
return false;
}
var min_ndc = vec2<f32>(1e30, 1e30);
var max_ndc = vec2<f32>(-1e30, -1e30);
for (var corner = 0u; corner < 8u; corner++) {
let point = vec3<f32>(
select(item.min.x, item.max.x, (corner & 1u) != 0u),
select(item.min.y, item.max.y, (corner & 2u) != 0u),
select(item.min.z, item.max.z, (corner & 4u) != 0u),
);
let clip = u.view_rot * vec4<f32>(point - u.eye.xyz, 1.0);
// A box touching or crossing the eye plane is conservatively retained.
if (clip.w <= 1e-6) {
return true;
}
let ndc = clip.xy / clip.w;
min_ndc = min(min_ndc, ndc);
max_ndc = max(max_ndc, ndc);
}
// 25% viewport margin on each side is 0.5 in NDC.
return !(
max_ndc.x < -1.5
|| min_ndc.x > 1.5
|| max_ndc.y < -1.5
|| min_ndc.y > 1.5
);
}
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) id: vec3<u32>) {
let index = id.x;
if (index >= u.count.x) {
return;
}
let item = items[index];
let instances = select(0u, item.info.x, chunk_visible(item));
opaque[index] = DrawIndexed(item.counts.x, instances, 0u, 0u, 0u);
transparent[index] = DrawIndexed(item.counts.y, instances, 0u, 0u, 0u);
wire[index] = DrawIndexed(item.counts.z, instances, 0u, 0u, 0u);
edge[index] = Draw(item.counts.w, instances, 0u, 0u);
}

View file

@ -51,19 +51,24 @@ struct MeshInstance {
};
@group(0) @binding(0) var<uniform> u: Uniforms;
@group(1) @binding(15) var<storage, read> mesh_instances: array<MeshInstance>;
struct VertexIn {
@location(0) position: vec3<f32>,
@location(3) position_low: vec3<f32>,
};
struct InstanceIn {
@location(4) model_row_0: vec4<f32>,
@location(5) model_row_1: vec4<f32>,
@location(7) model_row_2: vec4<f32>,
@location(8) translation_low: vec4<f32>,
};
@vertex
fn vs_main(
vertex: VertexIn,
@builtin(instance_index) instance_index: u32,
instance: InstanceIn,
) -> @builtin(position) vec4<f32> {
let instance = mesh_instances[instance_index];
let world_high = vec3<f32>(
dot(instance.model_row_0.xyz, vertex.position) + instance.model_row_0.w,
dot(instance.model_row_1.xyz, vertex.position) + instance.model_row_1.w,