Path: blob/main/crates/bevy_anti_alias/src/fxaa/node.rs
6596 views
use std::sync::Mutex;12use crate::fxaa::{CameraFxaaPipeline, Fxaa, FxaaPipeline};3use bevy_ecs::{prelude::*, query::QueryItem};4use bevy_render::{5diagnostic::RecordDiagnostics,6render_graph::{NodeRunError, RenderGraphContext, ViewNode},7render_resource::{8BindGroup, BindGroupEntries, Operations, PipelineCache, RenderPassColorAttachment,9RenderPassDescriptor, TextureViewId,10},11renderer::RenderContext,12view::ViewTarget,13};1415#[derive(Default)]16pub struct FxaaNode {17cached_texture_bind_group: Mutex<Option<(TextureViewId, BindGroup)>>,18}1920impl ViewNode for FxaaNode {21type ViewQuery = (22&'static ViewTarget,23&'static CameraFxaaPipeline,24&'static Fxaa,25);2627fn run(28&self,29_graph: &mut RenderGraphContext,30render_context: &mut RenderContext,31(target, pipeline, fxaa): QueryItem<Self::ViewQuery>,32world: &World,33) -> Result<(), NodeRunError> {34let pipeline_cache = world.resource::<PipelineCache>();35let fxaa_pipeline = world.resource::<FxaaPipeline>();3637if !fxaa.enabled {38return Ok(());39};4041let Some(pipeline) = pipeline_cache.get_render_pipeline(pipeline.pipeline_id) else {42return Ok(());43};4445let diagnostics = render_context.diagnostic_recorder();4647let post_process = target.post_process_write();48let source = post_process.source;49let destination = post_process.destination;50let mut cached_bind_group = self.cached_texture_bind_group.lock().unwrap();51let bind_group = match &mut *cached_bind_group {52Some((id, bind_group)) if source.id() == *id => bind_group,53cached_bind_group => {54let bind_group = render_context.render_device().create_bind_group(55None,56&fxaa_pipeline.texture_bind_group,57&BindGroupEntries::sequential((source, &fxaa_pipeline.sampler)),58);5960let (_, bind_group) = cached_bind_group.insert((source.id(), bind_group));61bind_group62}63};6465let pass_descriptor = RenderPassDescriptor {66label: Some("fxaa"),67color_attachments: &[Some(RenderPassColorAttachment {68view: destination,69depth_slice: None,70resolve_target: None,71ops: Operations::default(),72})],73depth_stencil_attachment: None,74timestamp_writes: None,75occlusion_query_set: None,76};7778let mut render_pass = render_context79.command_encoder()80.begin_render_pass(&pass_descriptor);81let pass_span = diagnostics.pass_span(&mut render_pass, "fxaa");8283render_pass.set_pipeline(pipeline);84render_pass.set_bind_group(0, bind_group, &[]);85render_pass.draw(0..3, 0..1);8687pass_span.end(&mut render_pass);8889Ok(())90}91}929394