-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathcustom_render_phase.rs
More file actions
672 lines (620 loc) · 24.7 KB
/
custom_render_phase.rs
File metadata and controls
672 lines (620 loc) · 24.7 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! This example demonstrates how to write a custom phase
//!
//! Render phases in bevy are used whenever you need to draw a group of meshes in a specific way.
//! For example, bevy's main pass has an opaque phase, a transparent phase for both 2d and 3d.
//! Sometimes, you may want to only draw a subset of meshes before or after the builtin phase. In
//! those situations you need to write your own phase.
//!
//! This example showcases how writing a custom phase to draw a stencil of a bevy mesh could look
//! like. Some shortcuts have been used for simplicity.
//!
//! This example was made for 3d, but a 2d equivalent would be almost identical.
use std::ops::Range;
use bevy::camera::Viewport;
use bevy::core_pipeline::core_3d::TransparentSortingInfo3d;
use bevy::math::Affine3Ext;
use bevy::pbr::{MeshPipelineSet, SetMeshViewEmptyBindGroup, ViewKeyCache};
use bevy::{
camera::MainPassResolutionOverride,
core_pipeline::{core_3d::main_opaque_pass_3d, schedule::Core3d, Core3dSystems},
ecs::{
entity::EntityHash,
system::{lifetimeless::SRes, SystemParamItem},
},
math::FloatOrd,
mesh::MeshVertexBufferLayoutRef,
pbr::{
DrawMesh, MeshInputUniform, MeshPipeline, MeshPipelineKey, MeshPipelineViewLayoutKey,
MeshUniform, RenderMeshInstances, SetMeshBindGroup, SetMeshViewBindGroup,
},
platform::collections::HashSet,
prelude::*,
render::{
batching::{
gpu_preprocessing::{
batch_and_prepare_sorted_render_phase, IndirectParametersCpuMetadata,
UntypedPhaseIndirectParametersBuffers,
},
GetBatchData, GetFullBatchData,
},
camera::{DirtySpecializations, ExtractedCamera, PendingQueues},
extract_component::{ExtractComponent, ExtractComponentPlugin},
mesh::{allocator::MeshAllocator, RenderMesh},
render_asset::RenderAssets,
render_phase::{
sort_phase_system, AddRenderCommand, CachedRenderPipelinePhaseItem, DrawFunctionId,
DrawFunctions, PhaseItem, PhaseItemExtraIndex, SetItemPipeline, SortedPhaseItem,
SortedRenderPhasePlugin, ViewSortedRenderPhases,
},
render_resource::{
CachedRenderPipelineId, ColorTargetState, ColorWrites, Face, FragmentState,
PipelineCache, PrimitiveState, RenderPassDescriptor, RenderPipelineDescriptor,
SpecializedMeshPipeline, SpecializedMeshPipelineError, SpecializedMeshPipelines,
TextureFormat, VertexState,
},
renderer::{RenderContext, ViewQuery},
sync_world::MainEntity,
view::{ExtractedView, RenderVisibleEntities, RetainedViewEntity, ViewTarget},
Extract, Render, RenderApp, RenderDebugFlags, RenderStartup, RenderSystems,
},
};
use indexmap::IndexMap;
use nonmax::NonMaxU32;
const SHADER_ASSET_PATH: &str = "shaders/custom_stencil.wgsl";
fn main() {
App::new()
.add_plugins((DefaultPlugins, MeshStencilPhasePlugin))
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// circular base
commands.spawn((
Mesh3d(meshes.add(Circle::new(4.0))),
MeshMaterial3d(materials.add(Color::WHITE)),
Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
));
// cube
// This cube will be rendered by the main pass, but it will also be rendered by our custom
// pass. This should result in an unlit red cube
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
Transform::from_xyz(0.0, 0.5, 0.0),
// This marker component is used to identify which mesh will be used in our custom pass
// The circle doesn't have it so it won't be rendered in our pass
DrawStencil,
));
// light
commands.spawn((
PointLight {
shadow_maps_enabled: true,
..default()
},
Transform::from_xyz(4.0, 8.0, 4.0),
));
// camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(-2.0, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
// disable msaa for simplicity
Msaa::Off,
));
}
#[derive(Component, ExtractComponent, Clone, Copy, Default)]
struct DrawStencil;
struct MeshStencilPhasePlugin;
impl Plugin for MeshStencilPhasePlugin {
fn build(&self, app: &mut App) {
app.add_plugins((
ExtractComponentPlugin::<DrawStencil>::default(),
SortedRenderPhasePlugin::<Stencil3d, MeshPipeline>::new(RenderDebugFlags::default()),
));
// We need to get the render app from the main app
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};
render_app
.init_resource::<SpecializedMeshPipelines<StencilPipeline>>()
.init_resource::<DrawFunctions<Stencil3d>>()
.add_render_command::<Stencil3d, DrawMesh3dStencil>()
.init_resource::<ViewSortedRenderPhases<Stencil3d>>()
.init_resource::<PendingCustomMeshQueues>()
.add_systems(RenderStartup, init_stencil_pipeline.after(MeshPipelineSet))
.add_systems(ExtractSchedule, extract_camera_phases)
.add_systems(
Render,
(
queue_custom_meshes.in_set(RenderSystems::QueueMeshes),
sort_phase_system::<Stencil3d>.in_set(RenderSystems::PhaseSort),
batch_and_prepare_sorted_render_phase::<Stencil3d, StencilPipeline>
.in_set(RenderSystems::PrepareResources),
),
)
.add_systems(
Core3d,
custom_draw_system
.after(main_opaque_pass_3d)
.in_set(Core3dSystems::MainPass),
);
}
}
#[derive(Resource)]
struct StencilPipeline {
/// The base mesh pipeline defined by bevy
///
/// Since we want to draw a stencil of an existing bevy mesh we want to reuse the default
/// pipeline as much as possible
mesh_pipeline: MeshPipeline,
/// Stores the shader used for this pipeline directly on the pipeline.
/// This isn't required, it's only done like this for simplicity.
shader_handle: Handle<Shader>,
}
fn init_stencil_pipeline(
mut commands: Commands,
mesh_pipeline: Res<MeshPipeline>,
asset_server: Res<AssetServer>,
) {
commands.insert_resource(StencilPipeline {
mesh_pipeline: mesh_pipeline.clone(),
shader_handle: asset_server.load(SHADER_ASSET_PATH),
});
}
// For more information on how SpecializedMeshPipeline work, please look at the
// specialized_mesh_pipeline example
impl SpecializedMeshPipeline for StencilPipeline {
type Key = MeshPipelineKey;
fn specialize(
&self,
key: Self::Key,
layout: &MeshVertexBufferLayoutRef,
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
// We will only use the position of the mesh in our shader so we only need to specify that
let mut vertex_attributes = Vec::new();
if layout.0.contains(Mesh::ATTRIBUTE_POSITION) {
// Make sure this matches the shader location
vertex_attributes.push(Mesh::ATTRIBUTE_POSITION.at_shader_location(0));
}
// This will automatically generate the correct `VertexBufferLayout` based on the vertex attributes
let vertex_buffer_layout = layout.0.get_layout(&vertex_attributes)?;
let view_layout = self
.mesh_pipeline
.get_view_layout(MeshPipelineViewLayoutKey::from(key));
Ok(RenderPipelineDescriptor {
label: Some("Specialized Mesh Pipeline".into()),
// We want to reuse the data from bevy so we use the same bind groups as the default
// mesh pipeline
layout: vec![
// Bind group 0 is the view uniform
view_layout.main_layout.clone(),
// Bind group 1 is empty
view_layout.empty_layout.clone(),
// Bind group 2 is the mesh uniform
self.mesh_pipeline.mesh_layouts.model_only.clone(),
],
vertex: VertexState {
shader: self.shader_handle.clone(),
buffers: vec![vertex_buffer_layout],
..default()
},
fragment: Some(FragmentState {
shader: self.shader_handle.clone(),
targets: vec![Some(ColorTargetState {
format: TextureFormat::bevy_default(),
blend: None,
write_mask: ColorWrites::ALL,
})],
..default()
}),
primitive: PrimitiveState {
topology: key.primitive_topology(),
strip_index_format: key.strip_index_format(),
cull_mode: Some(Face::Back),
..default()
},
// It's generally recommended to specialize your pipeline for MSAA,
// but it's not always possible
..default()
})
}
}
// We will reuse render commands already defined by bevy to draw a 3d mesh
type DrawMesh3dStencil = (
SetItemPipeline,
// This will set the view bindings in group 0
SetMeshViewBindGroup<0>,
// This will set an empty bind group in group 1
SetMeshViewEmptyBindGroup<1>,
// This will set the mesh bindings in group 2
SetMeshBindGroup<2>,
// This will draw the mesh
DrawMesh,
);
// This is the data required per entity drawn in a custom phase in bevy. More specifically this is the
// data required when using a ViewSortedRenderPhase. This would look differently if we wanted a
// batched render phase. Sorted phases are a bit easier to implement, but a batched phase would
// look similar.
//
// If you want to see how a batched phase implementation looks, you should look at the Opaque2d
// phase.
struct Stencil3d {
/// Information needed to sort the objects in the phase by distance to the
/// view.
pub sorting_info: TransparentSortingInfo3d,
pub distance: FloatOrd,
pub entity: (Entity, MainEntity),
pub pipeline: CachedRenderPipelineId,
pub draw_function: DrawFunctionId,
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
/// Whether the mesh in question is indexed (uses an index buffer in
/// addition to its vertex buffer).
pub indexed: bool,
}
// For more information about writing a phase item, please look at the custom_phase_item example
impl PhaseItem for Stencil3d {
#[inline]
fn entity(&self) -> Entity {
self.entity.0
}
#[inline]
fn main_entity(&self) -> MainEntity {
self.entity.1
}
#[inline]
fn draw_function(&self) -> DrawFunctionId {
self.draw_function
}
#[inline]
fn batch_range(&self) -> &Range<u32> {
&self.batch_range
}
#[inline]
fn batch_range_mut(&mut self) -> &mut Range<u32> {
&mut self.batch_range
}
#[inline]
fn extra_index(&self) -> PhaseItemExtraIndex {
self.extra_index.clone()
}
#[inline]
fn batch_range_and_extra_index_mut(&mut self) -> (&mut Range<u32>, &mut PhaseItemExtraIndex) {
(&mut self.batch_range, &mut self.extra_index)
}
}
impl SortedPhaseItem for Stencil3d {
type SortKey = FloatOrd;
#[inline]
fn sort_key(&self) -> Self::SortKey {
self.distance
}
#[inline]
fn sort(items: &mut IndexMap<(Entity, MainEntity), Stencil3d, EntityHash>) {
items.sort_by_key(|_, phase_item: &Stencil3d| phase_item.distance);
}
fn recalculate_sort_keys(
items: &mut IndexMap<(Entity, MainEntity), Self, EntityHash>,
view: &ExtractedView,
) {
// Determine the distance to the view for each phase item.
let rangefinder = view.rangefinder3d();
for item in items.values_mut() {
item.distance = FloatOrd(item.sorting_info.sort_distance(&rangefinder));
}
}
#[inline]
fn indexed(&self) -> bool {
self.indexed
}
}
impl CachedRenderPipelinePhaseItem for Stencil3d {
#[inline]
fn cached_pipeline(&self) -> CachedRenderPipelineId {
self.pipeline
}
}
impl GetBatchData for StencilPipeline {
type Param = (
SRes<RenderMeshInstances>,
SRes<RenderAssets<RenderMesh>>,
SRes<MeshAllocator>,
);
// Placing `AssetId<Mesh>` in the batch set compare data prevents Bevy from
// trying to multi-draw items with different meshes together. This is fine
// for this simple example.
type BatchSetCompareData = AssetId<Mesh>;
type BatchCompareData = ();
type BufferData = MeshUniform;
fn get_batch_data(
(mesh_instances, _render_assets, mesh_allocator): &SystemParamItem<Self::Param>,
(_entity, main_entity): (Entity, MainEntity),
) -> Option<(
Self::BufferData,
Option<(Self::BatchSetCompareData, Self::BatchCompareData)>,
)> {
let RenderMeshInstances::CpuBuilding(ref mesh_instances) = **mesh_instances else {
error!(
"`get_batch_data` should never be called in GPU mesh uniform \
building mode"
);
return None;
};
let mesh_instance = mesh_instances.get(&main_entity)?;
let first_vertex_index =
match mesh_allocator.mesh_vertex_slice(&mesh_instance.mesh_asset_id()) {
Some(mesh_vertex_slice) => mesh_vertex_slice.range.start,
None => 0,
};
let mesh_uniform = {
let mesh_transforms = &mesh_instance.transforms;
let (local_from_world_transpose_a, local_from_world_transpose_b) =
mesh_transforms.world_from_local.inverse_transpose_3x3();
MeshUniform {
world_from_local: mesh_transforms.world_from_local.to_transpose(),
previous_world_from_local: mesh_transforms.previous_world_from_local.to_transpose(),
lightmap_uv_rect: UVec2::ZERO,
local_from_world_transpose_a,
local_from_world_transpose_b,
flags: mesh_transforms.flags,
first_vertex_index,
current_skin_index: u32::MAX,
material_and_lightmap_bind_group_slot: 0,
tag: 0,
morph_descriptor_index: u32::MAX,
}
};
Some((mesh_uniform, None))
}
}
impl GetFullBatchData for StencilPipeline {
type BufferInputData = MeshInputUniform;
fn get_index_and_compare_data(
(mesh_instances, _, _): &SystemParamItem<Self::Param>,
main_entity: MainEntity,
) -> Option<(
NonMaxU32,
Option<(Self::BatchSetCompareData, Self::BatchCompareData)>,
)> {
// This should only be called during GPU building.
let RenderMeshInstances::GpuBuilding(ref mesh_instances) = **mesh_instances else {
error!(
"`get_index_and_compare_data` should never be called in CPU mesh uniform building \
mode"
);
return None;
};
let mesh_instance = mesh_instances.get(&main_entity)?;
Some((
NonMaxU32::new(mesh_instance.gpu_specific.current_uniform_index())?,
mesh_instance
.should_batch()
.then_some((mesh_instance.mesh_asset_id(), ())),
))
}
fn get_binned_batch_data(
(mesh_instances, _render_assets, mesh_allocator): &SystemParamItem<Self::Param>,
main_entity: MainEntity,
) -> Option<Self::BufferData> {
let RenderMeshInstances::CpuBuilding(ref mesh_instances) = **mesh_instances else {
error!(
"`get_binned_batch_data` should never be called in GPU mesh uniform building mode"
);
return None;
};
let mesh_instance = mesh_instances.get(&main_entity)?;
let first_vertex_index =
match mesh_allocator.mesh_vertex_slice(&mesh_instance.mesh_asset_id()) {
Some(mesh_vertex_slice) => mesh_vertex_slice.range.start,
None => 0,
};
Some(MeshUniform::new(
&mesh_instance.transforms,
first_vertex_index,
mesh_instance.material_bindings_index().slot,
None,
None,
None,
None,
))
}
fn write_batch_indirect_parameters_metadata(
indexed: bool,
base_output_index: u32,
batch_set_index: Option<NonMaxU32>,
indirect_parameters_buffers: &mut UntypedPhaseIndirectParametersBuffers,
indirect_parameters_offset: u32,
) {
// Note that `IndirectParameters` covers both of these structures, even
// though they actually have distinct layouts. See the comment above that
// type for more information.
let indirect_parameters = IndirectParametersCpuMetadata {
base_output_index,
batch_set_index: match batch_set_index {
None => !0,
Some(batch_set_index) => u32::from(batch_set_index),
},
};
if indexed {
indirect_parameters_buffers
.indexed
.set(indirect_parameters_offset, indirect_parameters);
} else {
indirect_parameters_buffers
.non_indexed
.set(indirect_parameters_offset, indirect_parameters);
}
}
fn get_binned_index(
_param: &SystemParamItem<Self::Param>,
_query_item: MainEntity,
) -> Option<NonMaxU32> {
None
}
}
// When defining a phase, we need to extract it from the main world and add it to a resource
// that will be used by the render world. We need to give that resource all views that will use
// that phase
fn extract_camera_phases(
mut stencil_phases: ResMut<ViewSortedRenderPhases<Stencil3d>>,
cameras: Extract<Query<(Entity, &Camera), With<Camera3d>>>,
mut live_entities: Local<HashSet<RetainedViewEntity>>,
) {
live_entities.clear();
for (main_entity, camera) in &cameras {
if !camera.is_active {
continue;
}
// This is the main camera, so we use the first subview index (0)
let retained_view_entity = RetainedViewEntity::new(main_entity.into(), None, 0);
stencil_phases.prepare_for_new_frame(retained_view_entity);
live_entities.insert(retained_view_entity);
}
// Clear out all dead views.
stencil_phases.retain(|camera_entity, _| live_entities.contains(camera_entity));
}
/// A resource that stores meshes that couldn't be specialized yet because their
/// materials hadn't loaded.
///
/// See the documentation for [`PendingQueues`] for more information.
#[derive(Default, Deref, DerefMut, Resource)]
struct PendingCustomMeshQueues(pub PendingQueues);
// This is a very important step when writing a custom phase.
//
// This system determines which meshes will be added to the phase.
fn queue_custom_meshes(
custom_draw_functions: Res<DrawFunctions<Stencil3d>>,
mut pipelines: ResMut<SpecializedMeshPipelines<StencilPipeline>>,
pipeline_cache: Res<PipelineCache>,
custom_draw_pipeline: Res<StencilPipeline>,
render_meshes: Res<RenderAssets<RenderMesh>>,
render_mesh_instances: Res<RenderMeshInstances>,
mut custom_render_phases: ResMut<ViewSortedRenderPhases<Stencil3d>>,
mut views: Query<(&ExtractedView, &RenderVisibleEntities)>,
view_key_cache: Res<ViewKeyCache>,
dirty_specializations: Res<DirtySpecializations>,
mut pending_custom_mesh_queues: ResMut<PendingCustomMeshQueues>,
has_marker: Query<(), With<DrawStencil>>,
) {
for (view, visible_entities) in &mut views {
let Some(custom_phase) = custom_render_phases.get_mut(&view.retained_view_entity) else {
continue;
};
let draw_custom = custom_draw_functions.read().id::<DrawMesh3dStencil>();
let Some(&view_key) = view_key_cache.get(&view.retained_view_entity) else {
continue;
};
// Since our phase can work on any 3d mesh we can reuse the default mesh 3d filter
let Some(render_visible_mesh_entities) = visible_entities.get::<Mesh3d>() else {
continue;
};
let view_pending_custom_mesh_queues =
pending_custom_mesh_queues.prepare_for_new_frame(view.retained_view_entity);
// First, remove meshes that need to be respecialized, and those that were removed, from the bins.
for &main_entity in dirty_specializations
.iter_to_dequeue(view.retained_view_entity, render_visible_mesh_entities)
{
custom_phase.remove(Entity::PLACEHOLDER, main_entity);
}
for (render_entity, visible_entity) in dirty_specializations.iter_to_queue(
view.retained_view_entity,
render_visible_mesh_entities,
&view_pending_custom_mesh_queues.prev_frame,
) {
// We only want meshes with the marker component to be queued to our phase.
if has_marker.get(*render_entity).is_err() {
continue;
}
let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(*visible_entity)
else {
// We couldn't fetch the mesh, probably because it hasn't been
// loaded yet. Add the entity to the list of pending custom mesh
// queues and bail.
view_pending_custom_mesh_queues
.current_frame
.insert((*render_entity, *visible_entity));
continue;
};
let Some(mesh) = render_meshes.get(mesh_instance.mesh_asset_id()) else {
continue;
};
// Specialize the key for the current mesh entity
// For this example we only specialize based on the mesh topology
// but you could have more complex keys and that's where you'd need to create those keys
let mut mesh_key = view_key;
mesh_key |= MeshPipelineKey::from_primitive_topology_and_strip_index(
mesh.primitive_topology(),
mesh.index_format(),
);
let pipeline_id = pipelines.specialize(
&pipeline_cache,
&custom_draw_pipeline,
mesh_key,
&mesh.layout,
);
let pipeline_id = match pipeline_id {
Ok(id) => id,
Err(err) => {
error!("{}", err);
continue;
}
};
// At this point we have all the data we need to create a phase item and add it to our
// phase
custom_phase.add(Stencil3d {
sorting_info: TransparentSortingInfo3d::Sorted {
mesh_center: mesh_instance.center,
depth_bias: 0.0,
},
distance: FloatOrd(0.0),
entity: (Entity::PLACEHOLDER, *visible_entity),
pipeline: pipeline_id,
draw_function: draw_custom,
// Sorted phase items aren't batched
batch_range: 0..1,
extra_index: PhaseItemExtraIndex::None,
indexed: mesh.indexed(),
});
}
}
}
fn custom_draw_system(
world: &World,
view: ViewQuery<(
&ExtractedCamera,
&ExtractedView,
&ViewTarget,
Option<&MainPassResolutionOverride>,
)>,
stencil_phases: Res<ViewSortedRenderPhases<Stencil3d>>,
mut ctx: RenderContext,
) {
let view_entity = view.entity();
let (camera, extracted_view, target, resolution_override) = view.into_inner();
let Some(stencil_phase) = stencil_phases.get(&extracted_view.retained_view_entity) else {
return;
};
let mut render_pass = ctx.begin_tracked_render_pass(RenderPassDescriptor {
label: Some("stencil pass"),
// For the purpose of the example, we will write directly to the view target. A real
// stencil pass would write to a custom texture and that texture would be used in later
// passes to render custom effects using it.
color_attachments: &[Some(target.get_color_attachment())],
// We don't bind any depth buffer for this pass
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
if let Some(viewport) =
Viewport::from_viewport_and_override(camera.viewport.as_ref(), resolution_override)
{
render_pass.set_camera_viewport(&viewport);
}
if let Err(err) = stencil_phase.render(&mut render_pass, world, view_entity) {
error!("Error encountered while rendering the stencil phase {err:?}");
}
}