1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Renderable collection and draw helpers.
use awsm_renderer_core::{command::render_pass::RenderPassEncoder, pipeline::primitive::CullMode};
use glam::Mat4;
use crate::{
bounds::Aabb,
error::AwsmError,
frustum::Frustum,
materials::MaterialKey,
meshes::MeshKey,
pipelines::{compute_pipeline::ComputePipelineKey, render_pipeline::RenderPipelineKey},
render::RenderContext,
render_passes::geometry::bind_group::GeometryBindGroups,
scene_spatial::NodeFilter,
AwsmRenderer,
};
/// Reusable scratch space for the per-frame renderable collection.
///
/// Held on [`AwsmRenderer`] (not on each frame's [`Renderables`]) so
/// the Vecs survive across frames and `clear_in_place` reuses the
/// existing allocation. For 10K-mesh scenes that's ~24 KB of avoided
/// allocator churn per frame. Frame-to-frame the Vec contents are
/// invalid (capacity is preserved, length is reset to 0 by `prepare`).
#[derive(Default)]
pub struct RenderablePool {
opaque: Vec<Renderable>,
transparent: Vec<Renderable>,
hud: Vec<Renderable>,
}
impl RenderablePool {
/// Drops the prior frame's content while keeping the underlying
/// allocations. Called at the top of each frame's
/// `collect_renderables`.
fn clear(&mut self) {
self.opaque.clear();
self.transparent.clear();
self.hud.clear();
}
}
/// Per-frame borrowed view over the [`RenderablePool`]'s populated
/// slices. Constructed by [`AwsmRenderer::collect_renderables`] and
/// passed to the per-pass `render` functions.
#[derive(Copy, Clone)]
pub struct Renderables<'r> {
pub opaque: &'r [Renderable],
pub transparent: &'r [Renderable],
pub hud: &'r [Renderable],
}
impl Renderables<'_> {
/// Returns true if there are no renderables.
pub fn is_empty(&self) -> bool {
self.opaque.is_empty() && self.transparent.is_empty() && self.hud.is_empty()
}
/// Returns the total number of renderables.
pub fn len(&self) -> usize {
self.opaque.len() + self.transparent.len() + self.hud.len()
}
}
impl AwsmRenderer {
/// Returns a borrowed view over the [`RenderablePool`]. Cheap;
/// callers can re-call this on every pass without repopulating.
pub fn renderables(&self) -> Renderables<'_> {
Renderables {
opaque: &self.renderable_pool.opaque,
transparent: &self.renderable_pool.transparent,
hud: &self.renderable_pool.hud,
}
}
/// Populates the per-frame [`RenderablePool`] from the renderer's
/// current state. Clears the pool in-place and refills it.
/// Callers read the populated slices via [`Self::renderables`].
///
/// Borrows `&mut self` only for the duration of population — once
/// the function returns, `self` is no longer mutably borrowed,
/// so the caller can construct a [`RenderContext`] over `&self`.
pub fn collect_renderables(&mut self) -> Result<()> {
let _maybe_span_guard = if self.logging.render_timings.sub_frame() {
Some(tracing::span!(tracing::Level::INFO, "Collect renderables").entered())
} else {
None
};
// Take the pool out by std::mem::take so we can populate
// without holding a `&mut self` borrow during the read-only
// queries below. The pool gets put back at the end.
let mut pool = std::mem::take(&mut self.renderable_pool);
pool.clear();
let frustum = self
.camera
.last_matrices
.as_ref()
.map(|matrices| Frustum::from_view_projection(matrices.view_projection()));
// Pre-size the visible scratch to the upper-bound mesh count.
// BVH culling usually returns far fewer, but this avoids any
// realloc-during-push on the conservative tail-walk path.
let mesh_upper_bound = self.meshes.len();
let mut visible: Vec<(MeshKey, &crate::meshes::mesh::Mesh)> =
Vec::with_capacity(mesh_upper_bound);
// Build the visible mesh-key set from the BVH instead of walking
// every mesh. The previous linear scan tested every mesh's cached
// `world_aabb` against the frustum on every frame; the BVH path
// descends hierarchically and surfaces only the surviving leaves.
// Meshes without a world AABB (procedural / mid-load) aren't in
// the index — fall back to a tail-walk of those so they still
// draw conservatively.
match &frustum {
Some(f) => {
visible.extend(
self.scene_spatial
.query_frustum(f, NodeFilter::camera_default())
.filter_map(|node| {
self.meshes
.get(node.mesh_key)
.ok()
.map(|m| (node.mesh_key, m))
}),
);
// Conservative fallback: any mesh without a world AABB
// can't be tested by the BVH; keep it in the visible set.
visible.extend(
self.meshes
.iter()
.filter(|(_, m)| !m.hidden && m.world_aabb.is_none()),
);
}
None => {
visible.extend(self.meshes.iter().filter(|(_, m)| !m.hidden));
}
}
for (mesh_key, mesh) in visible {
// Route by the authored `material_key`: `MaterialMeshMeta`
// is still packed from `mesh.material_key` (see
// `meshes::meta`), so routing by `effective_material_key`
// would pick a pipeline that doesn't match the data the
// shader reads. `effective_material_key` stays available
// on `Mesh` for a future cheap-material LOD wiring once
// the cheap material's offset is also plumbed into meta.
let routing_material = mesh.material_key;
// The opaque compute pipeline is specialized per
// `MaterialShaderId` (PBR / Unlit / Toon). Look up the
// routing material's shader_id so the pipeline matches
// the data the shader will read.
let shader_id = self.materials.shader_id(routing_material);
let cull_mode = if mesh.double_sided {
CullMode::None
} else {
CullMode::Back
};
let renderable = Renderable {
key: mesh_key,
world_aabb: mesh.world_aabb.clone(),
instanced: mesh.instanced,
double_sided: mesh.double_sided,
cull_mode,
hud: mesh.hud,
material_key: routing_material,
geometry_render_pipeline_key: self
.render_passes
.geometry
.pipelines
.get_render_pipeline_key(
crate::render_passes::geometry::pipeline::GeometryRenderPipelineKeyOpts {
anti_aliasing: &self.anti_aliasing,
instancing: mesh.instanced,
cull_mode,
// Mirrors the runtime branch in
// `meshes/mesh.rs::push_geometry_pass_commands`.
meta_storage_array: !mesh.instanced
&& self.features.indirect_first_instance_enabled(),
},
)
.inspect_err(|err| {
// Log the *actual* failure reason at collection
// time — `Renderable` only stores
// `Option<RenderPipelineKey>`, so the geometry
// pass's `None` path can only emit a generic
// "missing pipeline" warning. Surface the
// structured error here while we still have it.
tracing::warn!(
"geometry pipeline key lookup failed for mesh {mesh_key:?}: {err:?}"
);
})
.ok(),
// Masked (alpha-tested) variant: only for non-instanced glTF
// MASK meshes whose per-shader-id masked pipeline is compiled.
// `None` → falls back to the plain (solid) geometry pipeline.
geometry_masked_render_pipeline_key: if !mesh.instanced
&& self.materials.alpha_cutoff(routing_material).is_some()
{
let msaa = match self.anti_aliasing.msaa_sample_count {
Some(4) => Some(4u32),
_ => None,
};
// Key on the CANONICAL id (not the resolved variant id) —
// the masked fragment is variant-independent.
let masked_shader_id = self.materials.canonical_shader_id(routing_material);
self.render_passes.geometry.masked_pipelines.get(
msaa,
masked_shader_id,
cull_mode,
)
} else {
None
},
material_opaque_compute_pipeline_key: self
.render_passes
.material_opaque
.pipelines
.get_compute_pipeline_key(&self.anti_aliasing, shader_id),
material_transparent_render_pipeline_key: self
.render_passes
.material_transparent
.pipelines
.get_render_pipeline_key(mesh_key),
};
if mesh.hud {
pool.hud.push(renderable);
} else if self.materials.is_transparency_pass(routing_material) {
pool.transparent.push(renderable);
} else {
pool.opaque.push(renderable);
}
}
if let Some(camera_matrices) = self.camera.last_matrices.as_ref() {
let view_proj = camera_matrices.view_projection();
pool.opaque
.sort_by(|a, b| geometry_sort_renderable(a, b, &view_proj, false));
pool.transparent
.sort_by(|a, b| geometry_sort_renderable(a, b, &view_proj, true));
pool.hud
.sort_by(|a, b| geometry_sort_renderable(a, b, &view_proj, true));
}
// Put the populated pool back; the mutable borrow ends here.
self.renderable_pool = pool;
Ok(())
}
}
fn geometry_sort_renderable(
a: &Renderable,
b: &Renderable,
view_proj: &Mat4,
transparent: bool,
) -> std::cmp::Ordering {
// For the OPAQUE pass we group by render_pipeline_key first so the
// GPU can avoid pipeline switches across consecutive draws — the
// depth buffer handles overlap so any in-group order works. For
// the TRANSPARENT pass that grouping is *unsafe*: alpha compositing
// requires strict back-to-front draw order across *every* renderable,
// not within a pipeline group, otherwise a particle in front of a
// dome pane (or vice versa) will draw in the wrong order and one
// will incorrectly occlude / show-through the other. Skip the
// pipeline grouping in the transparent case and let depth alone
// decide.
if !transparent {
match (
a.geometry_render_pipeline_key,
b.geometry_render_pipeline_key,
) {
(None, None) => return std::cmp::Ordering::Equal,
(None, Some(_)) => return std::cmp::Ordering::Greater,
(Some(_), None) => return std::cmp::Ordering::Less,
(Some(key_a), Some(key_b)) => {
let pipeline_ordering = key_a.cmp(&key_b);
if pipeline_ordering != std::cmp::Ordering::Equal {
return pipeline_ordering;
}
}
}
}
match (a.world_aabb.as_ref(), b.world_aabb.as_ref()) {
(Some(a_world_aabb), Some(b_world_aabb)) => {
let a_min_z = view_proj.transform_point3(a_world_aabb.min).z;
let a_max_z = view_proj.transform_point3(a_world_aabb.max).z;
let b_min_z = view_proj.transform_point3(b_world_aabb.min).z;
let b_max_z = view_proj.transform_point3(b_world_aabb.max).z;
let a_closest_depth = a_min_z.min(a_max_z);
let b_closest_depth = b_min_z.min(b_max_z);
if transparent {
// Sort back-to-front for transparent objects.
// (larger z is further away, and we want that to come first)
b_closest_depth.total_cmp(&a_closest_depth)
} else {
// Sort front-to-back for opaque objects.
// (smaller z is closer, and we want that to come first)
a_closest_depth.total_cmp(&b_closest_depth)
}
}
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => std::cmp::Ordering::Equal,
}
}
/// Single renderable entity. No lifetime — all fields are owned or
/// `Copy`. The [`RenderablePool`] on [`AwsmRenderer`] stores these
/// frame-to-frame and clears in place.
#[derive(Debug, Clone)]
pub struct Renderable {
/// Stable mesh identity; pass-time lookups (`ctx.meshes.get(key)`)
/// retrieve any field this struct doesn't already cache.
pub key: MeshKey,
/// Snapshot of the mesh's world AABB at the moment of
/// collection. Used by depth sorting and as the cull-pass instance
/// bounds. Cloned (24 B) so the renderable doesn't borrow from
/// `meshes`.
pub world_aabb: Option<Aabb>,
pub instanced: bool,
pub double_sided: bool,
pub cull_mode: CullMode,
pub hud: bool,
pub material_key: MaterialKey,
/// Precomputed at collection time so the per-frame sort
/// comparator stays free of `RenderContext` access (which lets
/// `collect_renderables` populate the pool before `ctx` is built).
pub geometry_render_pipeline_key: Option<RenderPipelineKey>,
/// Set for glTF `MASK` meshes whose masked (alpha-tested) variant has been
/// compiled. When `Some`, the geometry pass draws this mesh with the masked
/// pipeline + augmented group-0 (cutoff `discard`); when `None` the mesh
/// falls back to `geometry_render_pipeline_key` (renders solid).
pub geometry_masked_render_pipeline_key: Option<RenderPipelineKey>,
pub material_opaque_compute_pipeline_key: Option<ComputePipelineKey>,
pub material_transparent_render_pipeline_key: Option<RenderPipelineKey>,
}
impl Renderable {
/// Returns the geometry render pipeline key.
pub fn geometry_render_pipeline_key(&self) -> Option<RenderPipelineKey> {
self.geometry_render_pipeline_key
}
/// Returns the opaque compute pipeline key, if any.
pub fn material_opaque_compute_pipeline_key(&self) -> Option<ComputePipelineKey> {
self.material_opaque_compute_pipeline_key
}
/// Returns the transparent render pipeline key, if any.
pub fn material_transparent_render_pipeline_key(&self) -> Option<RenderPipelineKey> {
self.material_transparent_render_pipeline_key
}
/// Returns the material key for this renderable.
pub fn material_key(&self) -> MaterialKey {
self.material_key
}
/// Returns the world-space AABB snapshot, if present.
pub fn world_aabb(&self) -> Option<&Aabb> {
self.world_aabb.as_ref()
}
/// Pushes geometry pass commands for this renderable. `masked` selects the
/// alpha-tested draw path (forces the non-instanced uniform-meta CPU draw,
/// matching the masked pipeline's compiled shape).
pub fn push_geometry_pass_commands(
&self,
ctx: &RenderContext,
render_pass: &RenderPassEncoder,
geometry_bind_groups: &GeometryBindGroups,
masked: bool,
) -> Result<()> {
let mesh = ctx.meshes.get(self.key)?;
mesh.push_geometry_pass_commands(ctx, self.key, render_pass, geometry_bind_groups, masked)
}
/// Returns the masked (alpha-tested) geometry pipeline key, if this mesh's
/// material is glTF `MASK` and its masked variant has been compiled.
pub fn geometry_masked_render_pipeline_key(&self) -> Option<RenderPipelineKey> {
self.geometry_masked_render_pipeline_key
}
/// Pushes transparent material pass commands for this renderable.
pub fn push_material_transparent_pass_commands(
&self,
ctx: &RenderContext,
render_pass: &RenderPassEncoder,
mesh_material_bind_group: &web_sys::GpuBindGroup,
) -> Result<()> {
let mesh = ctx.meshes.get(self.key)?;
mesh.push_material_transparent_pass_commands(
ctx,
self.key,
render_pass,
mesh_material_bind_group,
)
}
}
type Result<T> = std::result::Result<T, AwsmError>;