Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_anti_alias/src/fxaa/node.rs
6596 views
1
use std::sync::Mutex;
2
3
use crate::fxaa::{CameraFxaaPipeline, Fxaa, FxaaPipeline};
4
use bevy_ecs::{prelude::*, query::QueryItem};
5
use bevy_render::{
6
diagnostic::RecordDiagnostics,
7
render_graph::{NodeRunError, RenderGraphContext, ViewNode},
8
render_resource::{
9
BindGroup, BindGroupEntries, Operations, PipelineCache, RenderPassColorAttachment,
10
RenderPassDescriptor, TextureViewId,
11
},
12
renderer::RenderContext,
13
view::ViewTarget,
14
};
15
16
#[derive(Default)]
17
pub struct FxaaNode {
18
cached_texture_bind_group: Mutex<Option<(TextureViewId, BindGroup)>>,
19
}
20
21
impl ViewNode for FxaaNode {
22
type ViewQuery = (
23
&'static ViewTarget,
24
&'static CameraFxaaPipeline,
25
&'static Fxaa,
26
);
27
28
fn run(
29
&self,
30
_graph: &mut RenderGraphContext,
31
render_context: &mut RenderContext,
32
(target, pipeline, fxaa): QueryItem<Self::ViewQuery>,
33
world: &World,
34
) -> Result<(), NodeRunError> {
35
let pipeline_cache = world.resource::<PipelineCache>();
36
let fxaa_pipeline = world.resource::<FxaaPipeline>();
37
38
if !fxaa.enabled {
39
return Ok(());
40
};
41
42
let Some(pipeline) = pipeline_cache.get_render_pipeline(pipeline.pipeline_id) else {
43
return Ok(());
44
};
45
46
let diagnostics = render_context.diagnostic_recorder();
47
48
let post_process = target.post_process_write();
49
let source = post_process.source;
50
let destination = post_process.destination;
51
let mut cached_bind_group = self.cached_texture_bind_group.lock().unwrap();
52
let bind_group = match &mut *cached_bind_group {
53
Some((id, bind_group)) if source.id() == *id => bind_group,
54
cached_bind_group => {
55
let bind_group = render_context.render_device().create_bind_group(
56
None,
57
&fxaa_pipeline.texture_bind_group,
58
&BindGroupEntries::sequential((source, &fxaa_pipeline.sampler)),
59
);
60
61
let (_, bind_group) = cached_bind_group.insert((source.id(), bind_group));
62
bind_group
63
}
64
};
65
66
let pass_descriptor = RenderPassDescriptor {
67
label: Some("fxaa"),
68
color_attachments: &[Some(RenderPassColorAttachment {
69
view: destination,
70
depth_slice: None,
71
resolve_target: None,
72
ops: Operations::default(),
73
})],
74
depth_stencil_attachment: None,
75
timestamp_writes: None,
76
occlusion_query_set: None,
77
};
78
79
let mut render_pass = render_context
80
.command_encoder()
81
.begin_render_pass(&pass_descriptor);
82
let pass_span = diagnostics.pass_span(&mut render_pass, "fxaa");
83
84
render_pass.set_pipeline(pipeline);
85
render_pass.set_bind_group(0, bind_group, &[]);
86
render_pass.draw(0..3, 0..1);
87
88
pass_span.end(&mut render_pass);
89
90
Ok(())
91
}
92
}
93
94