diff --git a/crates/bevy_core_pipeline/src/core_2d/mod.rs b/crates/bevy_core_pipeline/src/core_2d/mod.rs index 2cfe3b8e18..869356c502 100644 --- a/crates/bevy_core_pipeline/src/core_2d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_2d/mod.rs @@ -20,6 +20,7 @@ pub mod graph { Bloom, Tonemapping, Fxaa, + Smaa, Upscaling, ContrastAdaptiveSharpening, EndMainPassPostProcessing, diff --git a/crates/bevy_core_pipeline/src/core_3d/mod.rs b/crates/bevy_core_pipeline/src/core_3d/mod.rs index ddc07f4951..a847607911 100644 --- a/crates/bevy_core_pipeline/src/core_3d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_3d/mod.rs @@ -32,6 +32,7 @@ pub mod graph { DepthOfField, Tonemapping, Fxaa, + Smaa, Upscaling, ContrastAdaptiveSharpening, EndMainPassPostProcessing, diff --git a/crates/bevy_core_pipeline/src/lib.rs b/crates/bevy_core_pipeline/src/lib.rs index dffeff4fc4..fec38e432b 100644 --- a/crates/bevy_core_pipeline/src/lib.rs +++ b/crates/bevy_core_pipeline/src/lib.rs @@ -21,6 +21,7 @@ pub mod motion_blur; pub mod msaa_writeback; pub mod prepass; mod skybox; +pub mod smaa; mod taa; pub mod tonemapping; pub mod upscaling; @@ -60,6 +61,7 @@ use crate::{ motion_blur::MotionBlurPlugin, msaa_writeback::MsaaWritebackPlugin, prepass::{DeferredPrepass, DepthPrepass, MotionVectorPrepass, NormalPrepass}, + smaa::SmaaPlugin, tonemapping::TonemappingPlugin, upscaling::UpscalingPlugin, }; @@ -96,6 +98,7 @@ impl Plugin for CorePipelinePlugin { CASPlugin, MotionBlurPlugin, DepthOfFieldPlugin, + SmaaPlugin, )); } } diff --git a/crates/bevy_core_pipeline/src/smaa/SMAAAreaLUT.ktx2 b/crates/bevy_core_pipeline/src/smaa/SMAAAreaLUT.ktx2 new file mode 100644 index 0000000000..e7d160f62e Binary files /dev/null and b/crates/bevy_core_pipeline/src/smaa/SMAAAreaLUT.ktx2 differ diff --git a/crates/bevy_core_pipeline/src/smaa/SMAASearchLUT.ktx2 b/crates/bevy_core_pipeline/src/smaa/SMAASearchLUT.ktx2 new file mode 100644 index 0000000000..29089f1e90 Binary files /dev/null and b/crates/bevy_core_pipeline/src/smaa/SMAASearchLUT.ktx2 differ diff --git a/crates/bevy_core_pipeline/src/smaa/mod.rs b/crates/bevy_core_pipeline/src/smaa/mod.rs new file mode 100644 index 0000000000..96721abdd9 --- /dev/null +++ b/crates/bevy_core_pipeline/src/smaa/mod.rs @@ -0,0 +1,1070 @@ +//! Subpixel morphological antialiasing (SMAA). +//! +//! [SMAA] is a 2011 antialiasing technique that takes an aliased image and +//! smooths out the *jaggies*, making edges smoother. It's been used in numerous +//! games and has become a staple postprocessing technique. Compared to MSAA, +//! SMAA has the advantage of compatibility with deferred rendering and +//! reduction of GPU memory bandwidth. Compared to FXAA, SMAA has the advantage +//! of improved quality, but the disadvantage of reduced performance. Compared +//! to TAA, SMAA has the advantage of stability and lack of *ghosting* +//! artifacts, but has the disadvantage of not supporting temporal accumulation, +//! which have made SMAA less popular when advanced photorealistic rendering +//! features are used in recent years. +//! +//! To use SMAA, add [`SmaaSettings`] to a [`bevy_render::camera::Camera`]. In a +//! pinch, you can simply use the default settings (via the [`Default`] trait) +//! for a high-quality, high-performance appearance. When using SMAA, you will +//! likely want to turn the default MSAA off by inserting the +//! [`bevy_render::Msaa::Off`] resource into the [`App`]. +//! +//! Those who have used SMAA in other engines should be aware that Bevy doesn't +//! yet support the following more advanced features of SMAA: +//! +//! * The temporal variant. +//! +//! * Depth- and chroma-based edge detection. +//! +//! * Predicated thresholding. +//! +//! * Compatibility with SSAA and MSAA. +//! +//! [SMAA]: https://www.iryoku.com/smaa/ + +use bevy_app::{App, Plugin}; +use bevy_asset::{load_internal_asset, load_internal_binary_asset, Handle}; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::{ + component::Component, + entity::Entity, + query::{QueryItem, With}, + reflect::ReflectComponent, + schedule::IntoSystemConfigs as _, + system::{lifetimeless::Read, Commands, Query, Res, ResMut, Resource}, + world::{FromWorld, World}, +}; +use bevy_math::{vec4, Vec4}; +use bevy_reflect::{std_traits::ReflectDefault, Reflect}; +use bevy_render::{ + camera::ExtractedCamera, + extract_component::{ExtractComponent, ExtractComponentPlugin}, + render_asset::{RenderAssetUsages, RenderAssets}, + render_graph::{ + NodeRunError, RenderGraphApp as _, RenderGraphContext, ViewNode, ViewNodeRunner, + }, + render_resource::{ + binding_types::{sampler, texture_2d, uniform_buffer}, + AddressMode, BindGroup, BindGroupEntries, BindGroupLayout, BindGroupLayoutEntries, + CachedRenderPipelineId, ColorTargetState, ColorWrites, CompareFunction, DepthStencilState, + DynamicUniformBuffer, Extent3d, FilterMode, FragmentState, LoadOp, MultisampleState, + Operations, PipelineCache, PrimitiveState, RenderPassColorAttachment, + RenderPassDepthStencilAttachment, RenderPassDescriptor, RenderPipeline, + RenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor, Shader, ShaderDefVal, + ShaderStages, ShaderType, SpecializedRenderPipeline, SpecializedRenderPipelines, + StencilFaceState, StencilOperation, StencilState, StoreOp, TextureDescriptor, + TextureDimension, TextureFormat, TextureSampleType, TextureUsages, TextureView, + VertexState, + }, + renderer::{RenderContext, RenderDevice, RenderQueue}, + texture::{ + BevyDefault, CachedTexture, CompressedImageFormats, GpuImage, Image, ImageFormat, + ImageSampler, ImageType, TextureCache, + }, + view::{ExtractedView, ViewTarget}, + Render, RenderApp, RenderSet, +}; +use bevy_utils::prelude::default; + +use crate::{ + core_2d::graph::{Core2d, Node2d}, + core_3d::graph::{Core3d, Node3d}, +}; + +/// The handle of the `smaa.wgsl` shader. +const SMAA_SHADER_HANDLE: Handle = Handle::weak_from_u128(12247928498010601081); +/// The handle of the area LUT, a KTX2 format texture that SMAA uses internally. +const SMAA_AREA_LUT_TEXTURE_HANDLE: Handle = Handle::weak_from_u128(15283551734567401670); +/// The handle of the search LUT, a KTX2 format texture that SMAA uses internally. +const SMAA_SEARCH_LUT_TEXTURE_HANDLE: Handle = Handle::weak_from_u128(3187314362190283210); + +/// Adds support for subpixel morphological antialiasing, or SMAA. +pub struct SmaaPlugin; + +/// Add this component to a [`bevy_render::camera::Camera`] to enable subpixel +/// morphological antialiasing (SMAA). +#[derive(Clone, Copy, Default, Component, Reflect, ExtractComponent)] +#[reflect(Component, Default)] +pub struct SmaaSettings { + /// A predefined set of SMAA parameters: i.e. a quality level. + /// + /// Generally, you can leave this at its default level. + pub preset: SmaaPreset, +} + +/// A preset quality level for SMAA. +/// +/// Higher values are slower but result in a higher-quality image. +/// +/// The default value is *high*. +#[derive(Clone, Copy, Reflect, Default, PartialEq, Eq, Hash)] +#[reflect(Default)] +pub enum SmaaPreset { + /// Four search steps; no diagonal or corner detection. + Low, + + /// Eight search steps; no diagonal or corner detection. + Medium, + + /// Sixteen search steps, 8 diagonal search steps, and corner detection. + /// + /// This is the default. + #[default] + High, + + /// Thirty-two search steps, 8 diagonal search steps, and corner detection. + Ultra, +} + +/// A render world resource that holds all render pipeline data needed for SMAA. +/// +/// There are three separate passes, so we need three separate pipelines. +#[derive(Resource)] +pub struct SmaaPipelines { + /// Pass 1: Edge detection. + edge_detection: SmaaEdgeDetectionPipeline, + /// Pass 2: Blending weight calculation. + blending_weight_calculation: SmaaBlendingWeightCalculationPipeline, + /// Pass 3: Neighborhood blending. + neighborhood_blending: SmaaNeighborhoodBlendingPipeline, +} + +/// The pipeline data for phase 1 of SMAA: edge detection. +struct SmaaEdgeDetectionPipeline { + /// The bind group layout common to all passes. + postprocess_bind_group_layout: BindGroupLayout, + /// The bind group layout for data specific to this pass. + edge_detection_bind_group_layout: BindGroupLayout, +} + +/// The pipeline data for phase 2 of SMAA: blending weight calculation. +struct SmaaBlendingWeightCalculationPipeline { + /// The bind group layout common to all passes. + postprocess_bind_group_layout: BindGroupLayout, + /// The bind group layout for data specific to this pass. + blending_weight_calculation_bind_group_layout: BindGroupLayout, +} + +/// The pipeline data for phase 3 of SMAA: neighborhood blending. +struct SmaaNeighborhoodBlendingPipeline { + /// The bind group layout common to all passes. + postprocess_bind_group_layout: BindGroupLayout, + /// The bind group layout for data specific to this pass. + neighborhood_blending_bind_group_layout: BindGroupLayout, +} + +/// A unique identifier for a set of SMAA pipelines. +#[derive(Clone, PartialEq, Eq, Hash)] +pub struct SmaaNeighborhoodBlendingPipelineKey { + /// The format of the framebuffer. + texture_format: TextureFormat, + /// The quality preset. + preset: SmaaPreset, +} + +/// A render world component that holds the pipeline IDs for the SMAA passes. +/// +/// There are three separate SMAA passes, each with a different shader and bind +/// group layout, so we need three pipeline IDs. +#[derive(Component)] +pub struct ViewSmaaPipelines { + /// The pipeline ID for edge detection (phase 1). + edge_detection_pipeline_id: CachedRenderPipelineId, + /// The pipeline ID for blending weight calculation (phase 2). + blending_weight_calculation_pipeline_id: CachedRenderPipelineId, + /// The pipeline ID for neighborhood blending (phase 3). + neighborhood_blending_pipeline_id: CachedRenderPipelineId, +} + +/// The render graph node that performs subpixel morphological antialiasing +/// (SMAA). +#[derive(Default)] +pub struct SmaaNode; + +/// Values supplied to the GPU for SMAA. +/// +/// Currently, this just contains the render target metrics and values derived +/// from them. These could be computed by the shader itself, but the original +/// SMAA HLSL code supplied them in a uniform, so we do the same for +/// consistency. +#[derive(Clone, Copy, ShaderType)] +pub struct SmaaInfoUniform { + /// Information about the width and height of the framebuffer. + /// + /// * *x*: The reciprocal pixel width of the framebuffer. + /// + /// * *y*: The reciprocal pixel height of the framebuffer. + /// + /// * *z*: The pixel width of the framebuffer. + /// + /// * *w*: The pixel height of the framebuffer. + pub rt_metrics: Vec4, +} + +/// A render world component that stores the offset of each [`SmaaInfoUniform`] +/// within the [`SmaaInfoUniformBuffer`] for each view. +#[derive(Clone, Copy, Deref, DerefMut, Component)] +pub struct SmaaInfoUniformOffset(pub u32); + +/// The GPU buffer that holds all [`SmaaInfoUniform`]s for all views. +/// +/// This is a resource stored in the render world. +#[derive(Resource, Default, Deref, DerefMut)] +pub struct SmaaInfoUniformBuffer(pub DynamicUniformBuffer); + +/// A render world component that holds the intermediate textures necessary to +/// perform SMAA. +/// +/// This is stored on each view that has enabled SMAA. +#[derive(Component)] +pub struct SmaaTextures { + /// The two-channel texture that stores the output from the first pass (edge + /// detection). + /// + /// The second pass (blending weight calculation) reads this texture to do + /// its work. + pub edge_detection_color_texture: CachedTexture, + + /// The 8-bit stencil texture that records which pixels the first pass + /// touched, so that the second pass doesn't have to examine other pixels. + /// + /// Each texel will contain a 0 if the first pass didn't touch the + /// corresponding pixel or a 1 if the first pass did touch that pixel. + pub edge_detection_stencil_texture: CachedTexture, + + /// A four-channel RGBA texture that stores the output from the second pass + /// (blending weight calculation). + /// + /// The final pass (neighborhood blending) reads this texture to do its + /// work. + pub blend_texture: CachedTexture, +} + +/// A render world component that stores the bind groups necessary to perform +/// SMAA. +/// +/// This is stored on each view. +#[derive(Component)] +pub struct SmaaBindGroups { + /// The bind group for the first pass (edge detection). + pub edge_detection_bind_group: BindGroup, + /// The bind group for the second pass (blending weight calculation). + pub blending_weight_calculation_bind_group: BindGroup, + /// The bind group for the final pass (neighborhood blending). + pub neighborhood_blending_bind_group: BindGroup, +} + +/// Stores the specialized render pipelines for SMAA. +/// +/// Because SMAA uses three passes, we need three separate render pipeline +/// stores. +#[derive(Resource, Default)] +pub struct SmaaSpecializedRenderPipelines { + /// Specialized render pipelines for the first phase (edge detection). + edge_detection: SpecializedRenderPipelines, + + /// Specialized render pipelines for the second phase (blending weight + /// calculation). + blending_weight_calculation: SpecializedRenderPipelines, + + /// Specialized render pipelines for the third phase (neighborhood + /// blending). + neighborhood_blending: SpecializedRenderPipelines, +} + +impl Plugin for SmaaPlugin { + fn build(&self, app: &mut App) { + // Load the shader. + load_internal_asset!(app, SMAA_SHADER_HANDLE, "smaa.wgsl", Shader::from_wgsl); + + // Load the two lookup textures. These are compressed textures in KTX2 + // format. + load_internal_binary_asset!( + app, + SMAA_AREA_LUT_TEXTURE_HANDLE, + "SMAAAreaLUT.ktx2", + |bytes, _: String| Image::from_buffer( + #[cfg(all(debug_assertions, feature = "dds"))] + "SMAAAreaLUT".to_owned(), + bytes, + ImageType::Format(ImageFormat::Ktx2), + CompressedImageFormats::NONE, + false, + ImageSampler::Default, + RenderAssetUsages::RENDER_WORLD, + ) + .expect("Failed to load SMAA area LUT") + ); + + load_internal_binary_asset!( + app, + SMAA_SEARCH_LUT_TEXTURE_HANDLE, + "SMAASearchLUT.ktx2", + |bytes, _: String| Image::from_buffer( + #[cfg(all(debug_assertions, feature = "dds"))] + "SMAASearchLUT".to_owned(), + bytes, + ImageType::Format(ImageFormat::Ktx2), + CompressedImageFormats::NONE, + false, + ImageSampler::Default, + RenderAssetUsages::RENDER_WORLD, + ) + .expect("Failed to load SMAA search LUT") + ); + + app.add_plugins(ExtractComponentPlugin::::default()) + .register_type::(); + + let Some(render_app) = app.get_sub_app_mut(RenderApp) else { + return; + }; + + render_app + .init_resource::() + .init_resource::() + .add_systems( + Render, + ( + prepare_smaa_pipelines.in_set(RenderSet::Prepare), + prepare_smaa_uniforms.in_set(RenderSet::PrepareResources), + prepare_smaa_textures.in_set(RenderSet::PrepareResources), + prepare_smaa_bind_groups.in_set(RenderSet::PrepareBindGroups), + ), + ) + .add_render_graph_node::>(Core3d, Node3d::Smaa) + .add_render_graph_edges( + Core3d, + ( + Node3d::Tonemapping, + Node3d::Smaa, + Node3d::EndMainPassPostProcessing, + ), + ) + .add_render_graph_node::>(Core2d, Node2d::Smaa) + .add_render_graph_edges( + Core2d, + ( + Node2d::Tonemapping, + Node2d::Smaa, + Node2d::EndMainPassPostProcessing, + ), + ); + } + + fn finish(&self, app: &mut App) { + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app.init_resource::(); + } + } +} + +impl FromWorld for SmaaPipelines { + fn from_world(world: &mut World) -> Self { + let render_device = world.resource::(); + + // Create the postprocess bind group layout (all passes, bind group 0). + let postprocess_bind_group_layout = render_device.create_bind_group_layout( + "SMAA postprocess bind group layout", + &BindGroupLayoutEntries::sequential( + ShaderStages::FRAGMENT, + ( + texture_2d(TextureSampleType::Float { filterable: true }), + uniform_buffer::(true) + .visibility(ShaderStages::VERTEX_FRAGMENT), + ), + ), + ); + + // Create the edge detection bind group layout (pass 1, bind group 1). + let edge_detection_bind_group_layout = render_device.create_bind_group_layout( + "SMAA edge detection bind group layout", + &BindGroupLayoutEntries::sequential( + ShaderStages::FRAGMENT, + (sampler(SamplerBindingType::Filtering),), + ), + ); + + // Create the blending weight calculation bind group layout (pass 2, bind group 1). + let blending_weight_calculation_bind_group_layout = render_device.create_bind_group_layout( + "SMAA blending weight calculation bind group layout", + &BindGroupLayoutEntries::sequential( + ShaderStages::FRAGMENT, + ( + texture_2d(TextureSampleType::Float { filterable: true }), // edges texture + sampler(SamplerBindingType::Filtering), // edges sampler + texture_2d(TextureSampleType::Float { filterable: true }), // search texture + texture_2d(TextureSampleType::Float { filterable: true }), // area texture + ), + ), + ); + + // Create the neighborhood blending bind group layout (pass 3, bind group 1). + let neighborhood_blending_bind_group_layout = render_device.create_bind_group_layout( + "SMAA neighborhood blending bind group layout", + &BindGroupLayoutEntries::sequential( + ShaderStages::FRAGMENT, + ( + texture_2d(TextureSampleType::Float { filterable: true }), + sampler(SamplerBindingType::Filtering), + ), + ), + ); + + SmaaPipelines { + edge_detection: SmaaEdgeDetectionPipeline { + postprocess_bind_group_layout: postprocess_bind_group_layout.clone(), + edge_detection_bind_group_layout, + }, + blending_weight_calculation: SmaaBlendingWeightCalculationPipeline { + postprocess_bind_group_layout: postprocess_bind_group_layout.clone(), + blending_weight_calculation_bind_group_layout, + }, + neighborhood_blending: SmaaNeighborhoodBlendingPipeline { + postprocess_bind_group_layout, + neighborhood_blending_bind_group_layout, + }, + } + } +} + +// Phase 1: edge detection. +impl SpecializedRenderPipeline for SmaaEdgeDetectionPipeline { + type Key = SmaaPreset; + + fn specialize(&self, preset: Self::Key) -> RenderPipelineDescriptor { + let shader_defs = vec!["SMAA_EDGE_DETECTION".into(), preset.shader_def()]; + + // We mark the pixels that we touched with a 1 so that the blending + // weight calculation (phase 2) will only consider those. This reduces + // the overhead of phase 2 considerably. + let stencil_face_state = StencilFaceState { + compare: CompareFunction::Always, + fail_op: StencilOperation::Replace, + depth_fail_op: StencilOperation::Replace, + pass_op: StencilOperation::Replace, + }; + + RenderPipelineDescriptor { + label: Some("SMAA edge detection".into()), + layout: vec![ + self.postprocess_bind_group_layout.clone(), + self.edge_detection_bind_group_layout.clone(), + ], + vertex: VertexState { + shader: SMAA_SHADER_HANDLE, + shader_defs: shader_defs.clone(), + entry_point: "edge_detection_vertex_main".into(), + buffers: vec![], + }, + fragment: Some(FragmentState { + shader: SMAA_SHADER_HANDLE, + shader_defs, + entry_point: "luma_edge_detection_fragment_main".into(), + targets: vec![Some(ColorTargetState { + format: TextureFormat::Rg8Unorm, + blend: None, + write_mask: ColorWrites::ALL, + })], + }), + push_constant_ranges: vec![], + primitive: PrimitiveState::default(), + depth_stencil: Some(DepthStencilState { + format: TextureFormat::Stencil8, + depth_write_enabled: false, + depth_compare: CompareFunction::Always, + stencil: StencilState { + front: stencil_face_state, + back: stencil_face_state, + read_mask: 1, + write_mask: 1, + }, + bias: default(), + }), + multisample: MultisampleState::default(), + } + } +} + +// Phase 2: blending weight calculation. +impl SpecializedRenderPipeline for SmaaBlendingWeightCalculationPipeline { + type Key = SmaaPreset; + + fn specialize(&self, preset: Self::Key) -> RenderPipelineDescriptor { + let shader_defs = vec![ + "SMAA_BLENDING_WEIGHT_CALCULATION".into(), + preset.shader_def(), + ]; + + // Only consider the pixels that were touched in phase 1. + let stencil_face_state = StencilFaceState { + compare: CompareFunction::Equal, + fail_op: StencilOperation::Keep, + depth_fail_op: StencilOperation::Keep, + pass_op: StencilOperation::Keep, + }; + + RenderPipelineDescriptor { + label: Some("SMAA blending weight calculation".into()), + layout: vec![ + self.postprocess_bind_group_layout.clone(), + self.blending_weight_calculation_bind_group_layout.clone(), + ], + vertex: VertexState { + shader: SMAA_SHADER_HANDLE, + shader_defs: shader_defs.clone(), + entry_point: "blending_weight_calculation_vertex_main".into(), + buffers: vec![], + }, + fragment: Some(FragmentState { + shader: SMAA_SHADER_HANDLE, + shader_defs, + entry_point: "blending_weight_calculation_fragment_main".into(), + targets: vec![Some(ColorTargetState { + format: TextureFormat::Rgba8Unorm, + blend: None, + write_mask: ColorWrites::ALL, + })], + }), + push_constant_ranges: vec![], + primitive: PrimitiveState::default(), + depth_stencil: Some(DepthStencilState { + format: TextureFormat::Stencil8, + depth_write_enabled: false, + depth_compare: CompareFunction::Always, + stencil: StencilState { + front: stencil_face_state, + back: stencil_face_state, + read_mask: 1, + write_mask: 1, + }, + bias: default(), + }), + multisample: MultisampleState::default(), + } + } +} + +impl SpecializedRenderPipeline for SmaaNeighborhoodBlendingPipeline { + type Key = SmaaNeighborhoodBlendingPipelineKey; + + fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor { + let shader_defs = vec!["SMAA_NEIGHBORHOOD_BLENDING".into(), key.preset.shader_def()]; + + RenderPipelineDescriptor { + label: Some("SMAA neighborhood blending".into()), + layout: vec![ + self.postprocess_bind_group_layout.clone(), + self.neighborhood_blending_bind_group_layout.clone(), + ], + vertex: VertexState { + shader: SMAA_SHADER_HANDLE, + shader_defs: shader_defs.clone(), + entry_point: "neighborhood_blending_vertex_main".into(), + buffers: vec![], + }, + fragment: Some(FragmentState { + shader: SMAA_SHADER_HANDLE, + shader_defs, + entry_point: "neighborhood_blending_fragment_main".into(), + targets: vec![Some(ColorTargetState { + format: key.texture_format, + blend: None, + write_mask: ColorWrites::ALL, + })], + }), + push_constant_ranges: vec![], + primitive: PrimitiveState::default(), + depth_stencil: None, + multisample: MultisampleState::default(), + } + } +} + +/// A system, part of the render app, that specializes the three pipelines +/// needed for SMAA according to each view's SMAA settings. +fn prepare_smaa_pipelines( + mut commands: Commands, + pipeline_cache: Res, + mut specialized_render_pipelines: ResMut, + smaa_pipelines: Res, + view_targets: Query<(Entity, &ExtractedView, &SmaaSettings)>, +) { + for (entity, view, settings) in &view_targets { + let edge_detection_pipeline_id = specialized_render_pipelines.edge_detection.specialize( + &pipeline_cache, + &smaa_pipelines.edge_detection, + settings.preset, + ); + + let blending_weight_calculation_pipeline_id = specialized_render_pipelines + .blending_weight_calculation + .specialize( + &pipeline_cache, + &smaa_pipelines.blending_weight_calculation, + settings.preset, + ); + + let neighborhood_blending_pipeline_id = specialized_render_pipelines + .neighborhood_blending + .specialize( + &pipeline_cache, + &smaa_pipelines.neighborhood_blending, + SmaaNeighborhoodBlendingPipelineKey { + texture_format: if view.hdr { + ViewTarget::TEXTURE_FORMAT_HDR + } else { + TextureFormat::bevy_default() + }, + preset: settings.preset, + }, + ); + + commands.entity(entity).insert(ViewSmaaPipelines { + edge_detection_pipeline_id, + blending_weight_calculation_pipeline_id, + neighborhood_blending_pipeline_id, + }); + } +} + +/// A system, part of the render app, that builds the [`SmaaInfoUniform`] data +/// for each view with SMAA enabled and writes the resulting data to GPU memory. +fn prepare_smaa_uniforms( + mut commands: Commands, + render_device: Res, + render_queue: Res, + view_targets: Query<(Entity, &ExtractedView), With>, + mut smaa_info_buffer: ResMut, +) { + smaa_info_buffer.clear(); + for (entity, view) in &view_targets { + let offset = smaa_info_buffer.push(&SmaaInfoUniform { + rt_metrics: vec4( + 1.0 / view.viewport.z as f32, + 1.0 / view.viewport.w as f32, + view.viewport.z as f32, + view.viewport.w as f32, + ), + }); + commands + .entity(entity) + .insert(SmaaInfoUniformOffset(offset)); + } + + smaa_info_buffer.write_buffer(&render_device, &render_queue); +} + +/// A system, part of the render app, that builds the intermediate textures for +/// each view with SMAA enabled. +/// +/// Phase 1 (edge detection) needs a two-channel RG texture and an 8-bit stencil +/// texture; phase 2 (blend weight calculation) needs a four-channel RGBA +/// texture. +fn prepare_smaa_textures( + mut commands: Commands, + render_device: Res, + mut texture_cache: ResMut, + view_targets: Query<(Entity, &ExtractedCamera), (With, With)>, +) { + for (entity, camera) in &view_targets { + let Some(texture_size) = camera.physical_target_size else { + continue; + }; + + let texture_size = Extent3d { + width: texture_size.x, + height: texture_size.y, + depth_or_array_layers: 1, + }; + + // Create the two-channel RG texture for phase 1 (edge detection). + let edge_detection_color_texture = texture_cache.get( + &render_device, + TextureDescriptor { + label: Some("SMAA edge detection color texture"), + size: texture_size, + mip_level_count: 1, + sample_count: 1, + dimension: TextureDimension::D2, + format: TextureFormat::Rg8Unorm, + usage: TextureUsages::TEXTURE_BINDING | TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }, + ); + + // Create the stencil texture for phase 1 (edge detection). + let edge_detection_stencil_texture = texture_cache.get( + &render_device, + TextureDescriptor { + label: Some("SMAA edge detection stencil texture"), + size: texture_size, + mip_level_count: 1, + sample_count: 1, + dimension: TextureDimension::D2, + format: TextureFormat::Stencil8, + usage: TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }, + ); + + // Create the four-channel RGBA texture for phase 2 (blending weight + // calculation). + let blend_texture = texture_cache.get( + &render_device, + TextureDescriptor { + label: Some("SMAA blend texture"), + size: texture_size, + mip_level_count: 1, + sample_count: 1, + dimension: TextureDimension::D2, + format: TextureFormat::Rgba8Unorm, + usage: TextureUsages::TEXTURE_BINDING | TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }, + ); + + commands.entity(entity).insert(SmaaTextures { + edge_detection_color_texture, + edge_detection_stencil_texture, + blend_texture, + }); + } +} + +/// A system, part of the render app, that builds the SMAA bind groups for each +/// view with SMAA enabled. +fn prepare_smaa_bind_groups( + mut commands: Commands, + render_device: Res, + smaa_pipelines: Res, + images: Res>, + view_targets: Query<(Entity, &SmaaTextures), (With, With)>, +) { + // Fetch the two lookup textures. These are bundled in this library. + let (Some(search_texture), Some(area_texture)) = ( + images.get(&SMAA_SEARCH_LUT_TEXTURE_HANDLE), + images.get(&SMAA_AREA_LUT_TEXTURE_HANDLE), + ) else { + return; + }; + + for (entity, smaa_textures) in &view_targets { + // We use the same sampler settings for all textures, so we can build + // only one and reuse it. + let sampler = render_device.create_sampler(&SamplerDescriptor { + label: Some("SMAA sampler"), + address_mode_u: AddressMode::ClampToEdge, + address_mode_v: AddressMode::ClampToEdge, + address_mode_w: AddressMode::ClampToEdge, + mag_filter: FilterMode::Linear, + min_filter: FilterMode::Linear, + ..default() + }); + + commands.entity(entity).insert(SmaaBindGroups { + edge_detection_bind_group: render_device.create_bind_group( + Some("SMAA edge detection bind group"), + &smaa_pipelines + .edge_detection + .edge_detection_bind_group_layout, + &BindGroupEntries::sequential((&sampler,)), + ), + blending_weight_calculation_bind_group: render_device.create_bind_group( + Some("SMAA blending weight calculation bind group"), + &smaa_pipelines + .blending_weight_calculation + .blending_weight_calculation_bind_group_layout, + &BindGroupEntries::sequential(( + &smaa_textures.edge_detection_color_texture.default_view, + &sampler, + &search_texture.texture_view, + &area_texture.texture_view, + )), + ), + neighborhood_blending_bind_group: render_device.create_bind_group( + Some("SMAA neighborhood blending bind group"), + &smaa_pipelines + .neighborhood_blending + .neighborhood_blending_bind_group_layout, + &BindGroupEntries::sequential(( + &smaa_textures.blend_texture.default_view, + &sampler, + )), + ), + }); + } +} + +impl ViewNode for SmaaNode { + type ViewQuery = ( + Read, + Read, + Read, + Read, + Read, + ); + + fn run<'w>( + &self, + _: &mut RenderGraphContext, + render_context: &mut RenderContext<'w>, + ( + view_target, + view_pipelines, + view_smaa_uniform_offset, + smaa_textures, + view_smaa_bind_groups, + ): QueryItem<'w, Self::ViewQuery>, + world: &'w World, + ) -> Result<(), NodeRunError> { + let pipeline_cache = world.resource::(); + let smaa_pipelines = world.resource::(); + let smaa_info_uniform_buffer = world.resource::(); + + // Fetch the render pipelines. + let ( + Some(edge_detection_pipeline), + Some(blending_weight_calculation_pipeline), + Some(neighborhood_blending_pipeline), + ) = ( + pipeline_cache.get_render_pipeline(view_pipelines.edge_detection_pipeline_id), + pipeline_cache + .get_render_pipeline(view_pipelines.blending_weight_calculation_pipeline_id), + pipeline_cache.get_render_pipeline(view_pipelines.neighborhood_blending_pipeline_id), + ) + else { + return Ok(()); + }; + + // Fetch the framebuffer textures. + let postprocess = view_target.post_process_write(); + let (source, destination) = (postprocess.source, postprocess.destination); + + // Stage 1: Edge detection pass. + perform_edge_detection( + render_context, + smaa_pipelines, + smaa_textures, + view_smaa_bind_groups, + smaa_info_uniform_buffer, + view_smaa_uniform_offset, + edge_detection_pipeline, + source, + ); + + // Stage 2: Blending weight calculation pass. + perform_blending_weight_calculation( + render_context, + smaa_pipelines, + smaa_textures, + view_smaa_bind_groups, + smaa_info_uniform_buffer, + view_smaa_uniform_offset, + blending_weight_calculation_pipeline, + source, + ); + + // Stage 3: Neighborhood blending pass. + perform_neighborhood_blending( + render_context, + smaa_pipelines, + view_smaa_bind_groups, + smaa_info_uniform_buffer, + view_smaa_uniform_offset, + neighborhood_blending_pipeline, + source, + destination, + ); + + Ok(()) + } +} + +/// Performs edge detection (phase 1). +/// +/// This runs as part of the [`SmaaNode`]. It reads from the source texture and +/// writes to the two-channel RG edges texture. Additionally, it ensures that +/// all pixels it didn't touch are stenciled out so that phase 2 won't have to +/// examine them. +#[allow(clippy::too_many_arguments)] +fn perform_edge_detection( + render_context: &mut RenderContext, + smaa_pipelines: &SmaaPipelines, + smaa_textures: &SmaaTextures, + view_smaa_bind_groups: &SmaaBindGroups, + smaa_info_uniform_buffer: &SmaaInfoUniformBuffer, + view_smaa_uniform_offset: &SmaaInfoUniformOffset, + edge_detection_pipeline: &RenderPipeline, + source: &TextureView, +) { + // Create the edge detection bind group. + let postprocess_bind_group = render_context.render_device().create_bind_group( + None, + &smaa_pipelines.edge_detection.postprocess_bind_group_layout, + &BindGroupEntries::sequential((source, &**smaa_info_uniform_buffer)), + ); + + // Create the edge detection pass descriptor. + let pass_descriptor = RenderPassDescriptor { + label: Some("SMAA edge detection pass"), + color_attachments: &[Some(RenderPassColorAttachment { + view: &smaa_textures.edge_detection_color_texture.default_view, + resolve_target: None, + ops: default(), + })], + depth_stencil_attachment: Some(RenderPassDepthStencilAttachment { + view: &smaa_textures.edge_detection_stencil_texture.default_view, + depth_ops: None, + stencil_ops: Some(Operations { + load: LoadOp::Clear(0), + store: StoreOp::Store, + }), + }), + timestamp_writes: None, + occlusion_query_set: None, + }; + + // Run the actual render pass. + let mut render_pass = render_context + .command_encoder() + .begin_render_pass(&pass_descriptor); + render_pass.set_pipeline(edge_detection_pipeline); + render_pass.set_bind_group(0, &postprocess_bind_group, &[**view_smaa_uniform_offset]); + render_pass.set_bind_group(1, &view_smaa_bind_groups.edge_detection_bind_group, &[]); + render_pass.set_stencil_reference(1); + render_pass.draw(0..3, 0..1); +} + +/// Performs blending weight calculation (phase 2). +/// +/// This runs as part of the [`SmaaNode`]. It reads the edges texture and writes +/// to the blend weight texture, using the stencil buffer to avoid processing +/// pixels it doesn't need to examine. +#[allow(clippy::too_many_arguments)] +fn perform_blending_weight_calculation( + render_context: &mut RenderContext, + smaa_pipelines: &SmaaPipelines, + smaa_textures: &SmaaTextures, + view_smaa_bind_groups: &SmaaBindGroups, + smaa_info_uniform_buffer: &SmaaInfoUniformBuffer, + view_smaa_uniform_offset: &SmaaInfoUniformOffset, + blending_weight_calculation_pipeline: &RenderPipeline, + source: &TextureView, +) { + // Create the blending weight calculation bind group. + let postprocess_bind_group = render_context.render_device().create_bind_group( + None, + &smaa_pipelines + .blending_weight_calculation + .postprocess_bind_group_layout, + &BindGroupEntries::sequential((source, &**smaa_info_uniform_buffer)), + ); + + // Create the blending weight calculation pass descriptor. + let pass_descriptor = RenderPassDescriptor { + label: Some("SMAA blending weight calculation pass"), + color_attachments: &[Some(RenderPassColorAttachment { + view: &smaa_textures.blend_texture.default_view, + resolve_target: None, + ops: default(), + })], + depth_stencil_attachment: Some(RenderPassDepthStencilAttachment { + view: &smaa_textures.edge_detection_stencil_texture.default_view, + depth_ops: None, + stencil_ops: Some(Operations { + load: LoadOp::Load, + store: StoreOp::Discard, + }), + }), + timestamp_writes: None, + occlusion_query_set: None, + }; + + // Run the actual render pass. + let mut render_pass = render_context + .command_encoder() + .begin_render_pass(&pass_descriptor); + render_pass.set_pipeline(blending_weight_calculation_pipeline); + render_pass.set_bind_group(0, &postprocess_bind_group, &[**view_smaa_uniform_offset]); + render_pass.set_bind_group( + 1, + &view_smaa_bind_groups.blending_weight_calculation_bind_group, + &[], + ); + render_pass.set_stencil_reference(1); + render_pass.draw(0..3, 0..1); +} + +/// Performs blending weight calculation (phase 3). +/// +/// This runs as part of the [`SmaaNode`]. It reads from the blend weight +/// texture. It's the only phase that writes to the postprocessing destination. +#[allow(clippy::too_many_arguments)] +fn perform_neighborhood_blending( + render_context: &mut RenderContext, + smaa_pipelines: &SmaaPipelines, + view_smaa_bind_groups: &SmaaBindGroups, + smaa_info_uniform_buffer: &SmaaInfoUniformBuffer, + view_smaa_uniform_offset: &SmaaInfoUniformOffset, + neighborhood_blending_pipeline: &RenderPipeline, + source: &TextureView, + destination: &TextureView, +) { + let postprocess_bind_group = render_context.render_device().create_bind_group( + None, + &smaa_pipelines + .neighborhood_blending + .postprocess_bind_group_layout, + &BindGroupEntries::sequential((source, &**smaa_info_uniform_buffer)), + ); + + let pass_descriptor = RenderPassDescriptor { + label: Some("SMAA neighborhood blending pass"), + color_attachments: &[Some(RenderPassColorAttachment { + view: destination, + resolve_target: None, + ops: default(), + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + }; + + let mut neighborhood_blending_render_pass = render_context + .command_encoder() + .begin_render_pass(&pass_descriptor); + neighborhood_blending_render_pass.set_pipeline(neighborhood_blending_pipeline); + neighborhood_blending_render_pass.set_bind_group( + 0, + &postprocess_bind_group, + &[**view_smaa_uniform_offset], + ); + neighborhood_blending_render_pass.set_bind_group( + 1, + &view_smaa_bind_groups.neighborhood_blending_bind_group, + &[], + ); + neighborhood_blending_render_pass.draw(0..3, 0..1); +} + +impl SmaaPreset { + /// Returns the `#define` in the shader corresponding to this quality + /// preset. + fn shader_def(&self) -> ShaderDefVal { + match *self { + SmaaPreset::Low => "SMAA_PRESET_LOW".into(), + SmaaPreset::Medium => "SMAA_PRESET_MEDIUM".into(), + SmaaPreset::High => "SMAA_PRESET_HIGH".into(), + SmaaPreset::Ultra => "SMAA_PRESET_ULTRA".into(), + } + } +} diff --git a/crates/bevy_core_pipeline/src/smaa/smaa.wgsl b/crates/bevy_core_pipeline/src/smaa/smaa.wgsl new file mode 100644 index 0000000000..f8f23c987b --- /dev/null +++ b/crates/bevy_core_pipeline/src/smaa/smaa.wgsl @@ -0,0 +1,1106 @@ +/** + * Copyright (C) 2013 Jorge Jimenez (jorge@iryoku.com) + * Copyright (C) 2013 Jose I. Echevarria (joseignacioechevarria@gmail.com) + * Copyright (C) 2013 Belen Masia (bmasia@unizar.es) + * Copyright (C) 2013 Fernando Navarro (fernandn@microsoft.com) + * Copyright (C) 2013 Diego Gutierrez (diegog@unizar.es) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to + * do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. As clarification, there + * is no requirement that the copyright notice and permission be included in + * binary distributions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * _______ ___ ___ ___ ___ + * / || \/ | / \ / \ + * | (---- | \ / | / ^ \ / ^ \ + * \ \ | |\/| | / /_\ \ / /_\ \ + * ----) | | | | | / _____ \ / _____ \ + * |_______/ |__| |__| /__/ \__\ /__/ \__\ + * + * E N H A N C E D + * S U B P I X E L M O R P H O L O G I C A L A N T I A L I A S I N G + * + * http://www.iryoku.com/smaa/ + * + * Hi, welcome aboard! + * + * Here you'll find instructions to get the shader up and running as fast as + * possible. + * + * IMPORTANTE NOTICE: when updating, remember to update both this file and the + * precomputed textures! They may change from version to version. + * + * The shader has three passes, chained together as follows: + * + * |input|------------------� + * v | + * [ SMAA*EdgeDetection ] | + * v | + * |edgesTex| | + * v | + * [ SMAABlendingWeightCalculation ] | + * v | + * |blendTex| | + * v | + * [ SMAANeighborhoodBlending ] <------� + * v + * |output| + * + * Note that each [pass] has its own vertex and pixel shader. Remember to use + * oversized triangles instead of quads to avoid overshading along the + * diagonal. + * + * You've three edge detection methods to choose from: luma, color or depth. + * They represent different quality/performance and anti-aliasing/sharpness + * tradeoffs, so our recommendation is for you to choose the one that best + * suits your particular scenario: + * + * - Depth edge detection is usually the fastest but it may miss some edges. + * + * - Luma edge detection is usually more expensive than depth edge detection, + * but catches visible edges that depth edge detection can miss. + * + * - Color edge detection is usually the most expensive one but catches + * chroma-only edges. + * + * For quickstarters: just use luma edge detection. + * + * The general advice is to not rush the integration process and ensure each + * step is done correctly (don't try to integrate SMAA T2x with predicated edge + * detection from the start!). Ok then, let's go! + * + * 1. The first step is to create two RGBA temporal render targets for holding + * |edgesTex| and |blendTex|. + * + * In DX10 or DX11, you can use a RG render target for the edges texture. + * In the case of NVIDIA GPUs, using RG render targets seems to actually be + * slower. + * + * On the Xbox 360, you can use the same render target for resolving both + * |edgesTex| and |blendTex|, as they aren't needed simultaneously. + * + * 2. Both temporal render targets |edgesTex| and |blendTex| must be cleared + * each frame. Do not forget to clear the alpha channel! + * + * 3. The next step is loading the two supporting precalculated textures, + * 'areaTex' and 'searchTex'. You'll find them in the 'Textures' folder as + * C++ headers, and also as regular DDS files. They'll be needed for the + * 'SMAABlendingWeightCalculation' pass. + * + * If you use the C++ headers, be sure to load them in the format specified + * inside of them. + * + * You can also compress 'areaTex' and 'searchTex' using BC5 and BC4 + * respectively, if you have that option in your content processor pipeline. + * When compressing then, you get a non-perceptible quality decrease, and a + * marginal performance increase. + * + * 4. All samplers must be set to linear filtering and clamp. + * + * After you get the technique working, remember that 64-bit inputs have + * half-rate linear filtering on GCN. + * + * If SMAA is applied to 64-bit color buffers, switching to point filtering + * when accessing them will increase the performance. Search for + * 'SMAASamplePoint' to see which textures may benefit from point + * filtering, and where (which is basically the color input in the edge + * detection and resolve passes). + * + * 5. All texture reads and buffer writes must be non-sRGB, with the exception + * of the input read and the output write in + * 'SMAANeighborhoodBlending' (and only in this pass!). If sRGB reads in + * this last pass are not possible, the technique will work anyway, but + * will perform antialiasing in gamma space. + * + * IMPORTANT: for best results the input read for the color/luma edge + * detection should *NOT* be sRGB. + * + * 6. Before including SMAA.h you'll have to setup the render target metrics, + * the target and any optional configuration defines. Optionally you can + * use a preset. + * + * You have the following targets available: + * SMAA_HLSL_3 + * SMAA_HLSL_4 + * SMAA_HLSL_4_1 + * SMAA_GLSL_3 * + * SMAA_GLSL_4 * + * + * * (See SMAA_INCLUDE_VS and SMAA_INCLUDE_PS below). + * + * And four presets: + * SMAA_PRESET_LOW (%60 of the quality) + * SMAA_PRESET_MEDIUM (%80 of the quality) + * SMAA_PRESET_HIGH (%95 of the quality) + * SMAA_PRESET_ULTRA (%99 of the quality) + * + * For example: + * #define SMAA_RT_METRICS float4(1.0 / 1280.0, 1.0 / 720.0, 1280.0, 720.0) + * #define SMAA_HLSL_4 + * #define SMAA_PRESET_HIGH + * #include "SMAA.h" + * + * Note that SMAA_RT_METRICS doesn't need to be a macro, it can be a + * uniform variable. The code is designed to minimize the impact of not + * using a constant value, but it is still better to hardcode it. + * + * Depending on how you encoded 'areaTex' and 'searchTex', you may have to + * add (and customize) the following defines before including SMAA.h: + * #define SMAA_AREATEX_SELECT(sample) sample.rg + * #define SMAA_SEARCHTEX_SELECT(sample) sample.r + * + * If your engine is already using porting macros, you can define + * SMAA_CUSTOM_SL, and define the porting functions by yourself. + * + * 7. Then, you'll have to setup the passes as indicated in the scheme above. + * You can take a look into SMAA.fx, to see how we did it for our demo. + * Checkout the function wrappers, you may want to copy-paste them! + * + * 8. It's recommended to validate the produced |edgesTex| and |blendTex|. + * You can use a screenshot from your engine to compare the |edgesTex| + * and |blendTex| produced inside of the engine with the results obtained + * with the reference demo. + * + * 9. After you get the last pass to work, it's time to optimize. You'll have + * to initialize a stencil buffer in the first pass (discard is already in + * the code), then mask execution by using it the second pass. The last + * pass should be executed in all pixels. + * + * + * After this point you can choose to enable predicated thresholding, + * temporal supersampling and motion blur integration: + * + * a) If you want to use predicated thresholding, take a look into + * SMAA_PREDICATION; you'll need to pass an extra texture in the edge + * detection pass. + * + * b) If you want to enable temporal supersampling (SMAA T2x): + * + * 1. The first step is to render using subpixel jitters. I won't go into + * detail, but it's as simple as moving each vertex position in the + * vertex shader, you can check how we do it in our DX10 demo. + * + * 2. Then, you must setup the temporal resolve. You may want to take a look + * into SMAAResolve for resolving 2x modes. After you get it working, you'll + * probably see ghosting everywhere. But fear not, you can enable the + * CryENGINE temporal reprojection by setting the SMAA_REPROJECTION macro. + * Check out SMAA_DECODE_VELOCITY if your velocity buffer is encoded. + * + * 3. The next step is to apply SMAA to each subpixel jittered frame, just as + * done for 1x. + * + * 4. At this point you should already have something usable, but for best + * results the proper area textures must be set depending on current jitter. + * For this, the parameter 'subsampleIndices' of + * 'SMAABlendingWeightCalculationPS' must be set as follows, for our T2x + * mode: + * + * @SUBSAMPLE_INDICES + * + * | S# | Camera Jitter | subsampleIndices | + * +----+------------------+---------------------+ + * | 0 | ( 0.25, -0.25) | float4(1, 1, 1, 0) | + * | 1 | (-0.25, 0.25) | float4(2, 2, 2, 0) | + * + * These jitter positions assume a bottom-to-top y axis. S# stands for the + * sample number. + * + * More information about temporal supersampling here: + * http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf + * + * c) If you want to enable spatial multisampling (SMAA S2x): + * + * 1. The scene must be rendered using MSAA 2x. The MSAA 2x buffer must be + * created with: + * - DX10: see below (*) + * - DX10.1: D3D10_STANDARD_MULTISAMPLE_PATTERN or + * - DX11: D3D11_STANDARD_MULTISAMPLE_PATTERN + * + * This allows to ensure that the subsample order matches the table in + * @SUBSAMPLE_INDICES. + * + * (*) In the case of DX10, we refer the reader to: + * - SMAA::detectMSAAOrder and + * - SMAA::msaaReorder + * + * These functions allow to match the standard multisample patterns by + * detecting the subsample order for a specific GPU, and reordering + * them appropriately. + * + * 2. A shader must be run to output each subsample into a separate buffer + * (DX10 is required). You can use SMAASeparate for this purpose, or just do + * it in an existing pass (for example, in the tone mapping pass, which has + * the advantage of feeding tone mapped subsamples to SMAA, which will yield + * better results). + * + * 3. The full SMAA 1x pipeline must be run for each separated buffer, storing + * the results in the final buffer. The second run should alpha blend with + * the existing final buffer using a blending factor of 0.5. + * 'subsampleIndices' must be adjusted as in the SMAA T2x case (see point + * b). + * + * d) If you want to enable temporal supersampling on top of SMAA S2x + * (which actually is SMAA 4x): + * + * 1. SMAA 4x consists on temporally jittering SMAA S2x, so the first step is + * to calculate SMAA S2x for current frame. In this case, 'subsampleIndices' + * must be set as follows: + * + * | F# | S# | Camera Jitter | Net Jitter | subsampleIndices | + * +----+----+--------------------+-------------------+----------------------+ + * | 0 | 0 | ( 0.125, 0.125) | ( 0.375, -0.125) | float4(5, 3, 1, 3) | + * | 0 | 1 | ( 0.125, 0.125) | (-0.125, 0.375) | float4(4, 6, 2, 3) | + * +----+----+--------------------+-------------------+----------------------+ + * | 1 | 2 | (-0.125, -0.125) | ( 0.125, -0.375) | float4(3, 5, 1, 4) | + * | 1 | 3 | (-0.125, -0.125) | (-0.375, 0.125) | float4(6, 4, 2, 4) | + * + * These jitter positions assume a bottom-to-top y axis. F# stands for the + * frame number. S# stands for the sample number. + * + * 2. After calculating SMAA S2x for current frame (with the new subsample + * indices), previous frame must be reprojected as in SMAA T2x mode (see + * point b). + * + * e) If motion blur is used, you may want to do the edge detection pass + * together with motion blur. This has two advantages: + * + * 1. Pixels under heavy motion can be omitted from the edge detection process. + * For these pixels we can just store "no edge", as motion blur will take + * care of them. + * 2. The center pixel tap is reused. + * + * Note that in this case depth testing should be used instead of stenciling, + * as we have to write all the pixels in the motion blur pass. + * + * That's it! + */ + +struct SmaaInfo { + rt_metrics: vec4, +} + +struct VertexVaryings { + clip_coord: vec2, + tex_coord: vec2, +} + +struct EdgeDetectionVaryings { + @builtin(position) position: vec4, + @location(0) offset_0: vec4, + @location(1) offset_1: vec4, + @location(2) offset_2: vec4, + @location(3) tex_coord: vec2, +} + +struct BlendingWeightCalculationVaryings { + @builtin(position) position: vec4, + @location(0) offset_0: vec4, + @location(1) offset_1: vec4, + @location(2) offset_2: vec4, + @location(3) tex_coord: vec2, +} + +struct NeighborhoodBlendingVaryings { + @builtin(position) position: vec4, + @location(0) offset: vec4, + @location(1) tex_coord: vec2, +} + +@group(0) @binding(0) var color_texture: texture_2d; +@group(0) @binding(1) var smaa_info: SmaaInfo; + +#ifdef SMAA_EDGE_DETECTION +@group(1) @binding(0) var color_sampler: sampler; +#endif // SMAA_EDGE_DETECTION + +#ifdef SMAA_BLENDING_WEIGHT_CALCULATION +@group(1) @binding(0) var edges_texture: texture_2d; +@group(1) @binding(1) var edges_sampler: sampler; +@group(1) @binding(2) var search_texture: texture_2d; +@group(1) @binding(3) var area_texture: texture_2d; +#endif // SMAA_BLENDING_WEIGHT_CALCULATION + +#ifdef SMAA_NEIGHBORHOOD_BLENDING +@group(1) @binding(0) var blend_texture: texture_2d; +@group(1) @binding(1) var blend_sampler: sampler; +#endif // SMAA_NEIGHBORHOOD_BLENDING + +//----------------------------------------------------------------------------- +// SMAA Presets + +#ifdef SMAA_PRESET_LOW +const SMAA_THRESHOLD: f32 = 0.15; +const SMAA_MAX_SEARCH_STEPS: u32 = 4u; +#define SMAA_DISABLE_DIAG_DETECTION +#define SMAA_DISABLE_CORNER_DETECTION +#else ifdef SMAA_PRESET_MEDIUM // SMAA_PRESET_LOW +const SMAA_THRESHOLD: f32 = 0.1; +const SMAA_MAX_SEARCH_STEPS: u32 = 8u; +#define SMAA_DISABLE_DIAG_DETECTION +#define SMAA_DISABLE_CORNER_DETECTION +#else ifdef SMAA_PRESET_HIGH // SMAA_PRESET_MEDIUM +const SMAA_THRESHOLD: f32 = 0.1; +const SMAA_MAX_SEARCH_STEPS: u32 = 16u; +const SMAA_MAX_SEARCH_STEPS_DIAG: u32 = 8u; +const SMAA_CORNER_ROUNDING: u32 = 25u; +#else ifdef SMAA_PRESET_ULTRA // SMAA_PRESET_HIGH +const SMAA_THRESHOLD: f32 = 0.05; +const SMAA_MAX_SEARCH_STEPS: u32 = 32u; +const SMAA_MAX_SEARCH_STEPS_DIAG: u32 = 16u; +const SMAA_CORNER_ROUNDING: u32 = 25u; +#else // SMAA_PRESET_ULTRA +const SMAA_THRESHOLD: f32 = 0.1; +const SMAA_MAX_SEARCH_STEPS: u32 = 16u; +const SMAA_MAX_SEARCH_STEPS_DIAG: u32 = 8u; +const SMAA_CORNER_ROUNDING: u32 = 25u; +#endif // SMAA_PRESET_ULTRA + +//----------------------------------------------------------------------------- +// Configurable Defines + +/** + * SMAA_THRESHOLD specifies the threshold or sensitivity to edges. + * Lowering this value you will be able to detect more edges at the expense of + * performance. + * + * Range: [0, 0.5] + * 0.1 is a reasonable value, and allows to catch most visible edges. + * 0.05 is a rather overkill value, that allows to catch 'em all. + * + * If temporal supersampling is used, 0.2 could be a reasonable value, as low + * contrast edges are properly filtered by just 2x. + */ +// (In the WGSL version of this shader, `SMAA_THRESHOLD` is set above, in "SMAA +// Presets".) + +/** + * SMAA_MAX_SEARCH_STEPS specifies the maximum steps performed in the + * horizontal/vertical pattern searches, at each side of the pixel. + * + * In number of pixels, it's actually the double. So the maximum line length + * perfectly handled by, for example 16, is 64 (by perfectly, we meant that + * longer lines won't look as good, but still antialiased). + * + * Range: [0, 112] + */ +// (In the WGSL version of this shader, `SMAA_MAX_SEARCH_STEPS` is set above, in +// "SMAA Presets".) + +/** + * SMAA_MAX_SEARCH_STEPS_DIAG specifies the maximum steps performed in the + * diagonal pattern searches, at each side of the pixel. In this case we jump + * one pixel at time, instead of two. + * + * Range: [0, 20] + * + * On high-end machines it is cheap (between a 0.8x and 0.9x slower for 16 + * steps), but it can have a significant impact on older machines. + * + * Define SMAA_DISABLE_DIAG_DETECTION to disable diagonal processing. + */ +// (In the WGSL version of this shader, `SMAA_MAX_SEARCH_STEPS_DIAG` is set +// above, in "SMAA Presets".) + +/** + * SMAA_CORNER_ROUNDING specifies how much sharp corners will be rounded. + * + * Range: [0, 100] + * + * Define SMAA_DISABLE_CORNER_DETECTION to disable corner processing. + */ +// (In the WGSL version of this shader, `SMAA_CORNER_ROUNDING` is set above, in +// "SMAA Presets".) + +/** + * If there is an neighbor edge that has SMAA_LOCAL_CONTRAST_FACTOR times + * bigger contrast than current edge, current edge will be discarded. + * + * This allows to eliminate spurious crossing edges, and is based on the fact + * that, if there is too much contrast in a direction, that will hide + * perceptually contrast in the other neighbors. + */ +const SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR: f32 = 2.0; + +//----------------------------------------------------------------------------- +// Non-Configurable Defines + +const SMAA_AREATEX_MAX_DISTANCE: f32 = 16.0; +const SMAA_AREATEX_MAX_DISTANCE_DIAG: f32 = 20.0; +const SMAA_AREATEX_PIXEL_SIZE: vec2 = (1.0 / vec2(160.0, 560.0)); +const SMAA_AREATEX_SUBTEX_SIZE: f32 = (1.0 / 7.0); +const SMAA_SEARCHTEX_SIZE: vec2 = vec2(66.0, 33.0); +const SMAA_SEARCHTEX_PACKED_SIZE: vec2 = vec2(64.0, 16.0); + +#ifndef SMAA_DISABLE_CORNER_DETECTION +const SMAA_CORNER_ROUNDING_NORM: f32 = f32(SMAA_CORNER_ROUNDING) / 100.0; +#endif // SMAA_DISABLE_CORNER_DETECTION + +//----------------------------------------------------------------------------- +// WGSL-Specific Functions + +// This vertex shader produces the following, when drawn using indices 0..3: +// +// 1 | 0-----x.....2 +// 0 | | s | . ´ +// -1 | x_____x´ +// -2 | : .´ +// -3 | 1´ +// +--------------- +// -1 0 1 2 3 +// +// The axes are clip-space x and y. The region marked s is the visible region. +// The digits in the corners of the right-angled triangle are the vertex +// indices. +// +// The top-left has UV 0,0, the bottom-left has 0,2, and the top-right has 2,0. +// This means that the UV gets interpolated to 1,1 at the bottom-right corner +// of the clip-space rectangle that is at 1,-1 in clip space. +fn calculate_vertex_varyings(vertex_index: u32) -> VertexVaryings { + // See the explanation above for how this works + let uv = vec2(f32(vertex_index >> 1u), f32(vertex_index & 1u)) * 2.0; + let clip_position = vec2(uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0)); + + return VertexVaryings(clip_position, uv); +} + +//----------------------------------------------------------------------------- +// Vertex Shaders + +#ifdef SMAA_EDGE_DETECTION + +/** + * Edge Detection Vertex Shader + */ +@vertex +fn edge_detection_vertex_main(@builtin(vertex_index) vertex_index: u32) -> EdgeDetectionVaryings { + let varyings = calculate_vertex_varyings(vertex_index); + + var edge_detection_varyings = EdgeDetectionVaryings(); + edge_detection_varyings.position = vec4(varyings.clip_coord, 0.0, 1.0); + edge_detection_varyings.tex_coord = varyings.tex_coord; + + edge_detection_varyings.offset_0 = smaa_info.rt_metrics.xyxy * vec4(-1.0, 0.0, 0.0, -1.0) + + varyings.tex_coord.xyxy; + edge_detection_varyings.offset_1 = smaa_info.rt_metrics.xyxy * vec4(1.0, 0.0, 0.0, 1.0) + + varyings.tex_coord.xyxy; + edge_detection_varyings.offset_2 = smaa_info.rt_metrics.xyxy * vec4(-2.0, 0.0, 0.0, -2.0) + + varyings.tex_coord.xyxy; + + return edge_detection_varyings; +} + +#endif // SMAA_EDGE_DETECTION + +#ifdef SMAA_BLENDING_WEIGHT_CALCULATION + +/** + * Blend Weight Calculation Vertex Shader + */ +@vertex +fn blending_weight_calculation_vertex_main(@builtin(vertex_index) vertex_index: u32) + -> BlendingWeightCalculationVaryings { + let varyings = calculate_vertex_varyings(vertex_index); + + var weight_varyings = BlendingWeightCalculationVaryings(); + weight_varyings.position = vec4(varyings.clip_coord, 0.0, 1.0); + weight_varyings.tex_coord = varyings.tex_coord; + + // We will use these offsets for the searches later on (see @PSEUDO_GATHER4): + weight_varyings.offset_0 = smaa_info.rt_metrics.xyxy * vec4(-0.25, -0.125, 1.25, -0.125) + + varyings.tex_coord.xyxy; + weight_varyings.offset_1 = smaa_info.rt_metrics.xyxy * vec4(-0.125, -0.25, -0.125, 1.25) + + varyings.tex_coord.xyxy; + + // And these for the searches, they indicate the ends of the loops: + weight_varyings.offset_2 = + smaa_info.rt_metrics.xxyy * vec4(-2.0, 2.0, -2.0, 2.0) * f32(SMAA_MAX_SEARCH_STEPS) + + vec4(weight_varyings.offset_0.xz, weight_varyings.offset_1.yw); + + return weight_varyings; +} + +#endif // SMAA_BLENDING_WEIGHT_CALCULATION + +#ifdef SMAA_NEIGHBORHOOD_BLENDING + +/** + * Neighborhood Blending Vertex Shader + */ +@vertex +fn neighborhood_blending_vertex_main(@builtin(vertex_index) vertex_index: u32) + -> NeighborhoodBlendingVaryings { + let varyings = calculate_vertex_varyings(vertex_index); + let offset = smaa_info.rt_metrics.xyxy * vec4(1.0, 0.0, 0.0, 1.0) + varyings.tex_coord.xyxy; + return NeighborhoodBlendingVaryings( + vec4(varyings.clip_coord, 0.0, 1.0), + offset, + varyings.tex_coord + ); +} + +#endif // SMAA_NEIGHBORHOOD_BLENDING + +//----------------------------------------------------------------------------- +// Edge Detection Pixel Shaders (First Pass) + +#ifdef SMAA_EDGE_DETECTION + +/** + * Luma Edge Detection + * + * IMPORTANT NOTICE: luma edge detection requires gamma-corrected colors, and + * thus 'color_texture' should be a non-sRGB texture. + */ +@fragment +fn luma_edge_detection_fragment_main(in: EdgeDetectionVaryings) -> @location(0) vec4 { + // Calculate the threshold: + // TODO: Predication. + let threshold = vec2(SMAA_THRESHOLD); + + // Calculate luma: + let weights = vec3(0.2126, 0.7152, 0.0722); + let L = dot(textureSample(color_texture, color_sampler, in.tex_coord).rgb, weights); + + let Lleft = dot(textureSample(color_texture, color_sampler, in.offset_0.xy).rgb, weights); + let Ltop = dot(textureSample(color_texture, color_sampler, in.offset_0.zw).rgb, weights); + + // We do the usual threshold: + var delta: vec4 = vec4(abs(L - vec2(Lleft, Ltop)), 0.0, 0.0); + var edges = step(threshold, delta.xy); + + // Then discard if there is no edge: + if (dot(edges, vec2(1.0)) == 0.0) { + discard; + } + + // Calculate right and bottom deltas: + let Lright = dot(textureSample(color_texture, color_sampler, in.offset_1.xy).rgb, weights); + let Lbottom = dot(textureSample(color_texture, color_sampler, in.offset_1.zw).rgb, weights); + delta = vec4(delta.xy, abs(L - vec2(Lright, Lbottom))); + + // Calculate the maximum delta in the direct neighborhood: + var max_delta = max(delta.xy, delta.zw); + + // Calculate left-left and top-top deltas: + let Lleftleft = dot(textureSample(color_texture, color_sampler, in.offset_2.xy).rgb, weights); + let Ltoptop = dot(textureSample(color_texture, color_sampler, in.offset_2.zw).rgb, weights); + delta = vec4(delta.xy, abs(vec2(Lleft, Ltop) - vec2(Lleftleft, Ltoptop))); + + // Calculate the final maximum delta: + max_delta = max(max_delta.xy, delta.zw); + let final_delta = max(max_delta.x, max_delta.y); + + // Local contrast adaptation: + edges *= step(vec2(final_delta), SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy); + + return vec4(edges, 0.0, 1.0); +} + +#endif // SMAA_EDGE_DETECTION + +#ifdef SMAA_BLENDING_WEIGHT_CALCULATION + +//----------------------------------------------------------------------------- +// Diagonal Search Functions + +#ifndef SMAA_DISABLE_DIAG_DETECTION + +/** + * Allows to decode two binary values from a bilinear-filtered access. + */ +fn decode_diag_bilinear_access_2(in_e: vec2) -> vec2 { + // Bilinear access for fetching 'e' have a 0.25 offset, and we are + // interested in the R and G edges: + // + // +---G---+-------+ + // | x o R x | + // +-------+-------+ + // + // Then, if one of these edge is enabled: + // Red: (0.75 * X + 0.25 * 1) => 0.25 or 1.0 + // Green: (0.75 * 1 + 0.25 * X) => 0.75 or 1.0 + // + // This function will unpack the values (mad + mul + round): + // wolframalpha.com: round(x * abs(5 * x - 5 * 0.75)) plot 0 to 1 + var e = in_e; + e.r = e.r * abs(5.0 * e.r - 5.0 * 0.75); + return round(e); +} + +fn decode_diag_bilinear_access_4(e: vec4) -> vec4 { + let e_rb = e.rb * abs(5.0 * e.rb - 5.0 * 0.75); + return round(vec4(e_rb.x, e.g, e_rb.y, e.a)); +} + +/** + * These functions allows to perform diagonal pattern searches. + */ +fn search_diag_1(tex_coord: vec2, dir: vec2, e: ptr>) -> vec2 { + var coord = vec4(tex_coord, -1.0, 1.0); + let t = vec3(smaa_info.rt_metrics.xy, 1.0); + while (coord.z < f32(SMAA_MAX_SEARCH_STEPS_DIAG - 1u) && coord.w > 0.9) { + coord = vec4(t * vec3(dir, 1.0) + coord.xyz, coord.w); + *e = textureSampleLevel(edges_texture, edges_sampler, coord.xy, 0.0).rg; + coord.w = dot(*e, vec2(0.5)); + } + return coord.zw; +} + +fn search_diag_2(tex_coord: vec2, dir: vec2, e: ptr>) -> vec2 { + var coord = vec4(tex_coord, -1.0, 1.0); + coord.x += 0.25 * smaa_info.rt_metrics.x; // See @SearchDiag2Optimization + let t = vec3(smaa_info.rt_metrics.xy, 1.0); + while (coord.z < f32(SMAA_MAX_SEARCH_STEPS_DIAG - 1u) && coord.w > 0.9) { + coord = vec4(t * vec3(dir, 1.0) + coord.xyz, coord.w); + + // @SearchDiag2Optimization + // Fetch both edges at once using bilinear filtering: + *e = textureSampleLevel(edges_texture, edges_sampler, coord.xy, 0.0).rg; + *e = decode_diag_bilinear_access_2(*e); + + // Non-optimized version: + // e.g = SMAASampleLevelZero(edgesTex, coord.xy).g; + // e.r = SMAASampleLevelZeroOffset(edgesTex, coord.xy, int2(1, 0)).r; + + coord.w = dot(*e, vec2(0.5)); + } + return coord.zw; +} + +/** + * Similar to SMAAArea, this calculates the area corresponding to a certain + * diagonal distance and crossing edges 'e'. + */ +fn area_diag(dist: vec2, e: vec2, offset: f32) -> vec2 { + var tex_coord = vec2(SMAA_AREATEX_MAX_DISTANCE_DIAG) * e + dist; + + // We do a scale and bias for mapping to texel space: + tex_coord = SMAA_AREATEX_PIXEL_SIZE * tex_coord + 0.5 * SMAA_AREATEX_PIXEL_SIZE; + + // Diagonal areas are on the second half of the texture: + tex_coord.x += 0.5; + + // Move to proper place, according to the subpixel offset: + tex_coord.y += SMAA_AREATEX_SUBTEX_SIZE * offset; + + // Do it! + return textureSampleLevel(area_texture, edges_sampler, tex_coord, 0.0).rg; +} + +/** + * This searches for diagonal patterns and returns the corresponding weights. + */ +fn calculate_diag_weights(tex_coord: vec2, e: vec2, subsample_indices: vec4) + -> vec2 { + var weights = vec2(0.0, 0.0); + + // Search for the line ends: + var d = vec4(0.0); + var end = vec2(0.0); + if (e.r > 0.0) { + let d_xz = search_diag_1(tex_coord, vec2(-1.0, 1.0), &end); + d = vec4(d_xz.x, d.y, d_xz.y, d.w); + d.x += f32(end.y > 0.9); + } else { + d = vec4(0.0, d.y, 0.0, d.w); + } + let d_yw = search_diag_1(tex_coord, vec2(1.0, -1.0), &end); + d = vec4(d.x, d_yw.x, d.y, d_yw.y); + + if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3 + // Fetch the crossing edges: + let coords = vec4(-d.x + 0.25, d.x, d.y, -d.y - 0.25) * smaa_info.rt_metrics.xyxy + + tex_coord.xyxy; + var c = vec4( + textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0, vec2(-1, 0)).rg, + textureSampleLevel(edges_texture, edges_sampler, coords.zw, 0.0, vec2( 1, 0)).rg, + ); + let c_yxwz = decode_diag_bilinear_access_4(c.xyzw); + c = c_yxwz.yxwz; + + // Non-optimized version: + // float4 coords = mad(float4(-d.x, d.x, d.y, -d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy); + // float4 c; + // c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).g; + // c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, 0)).r; + // c.z = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).g; + // c.w = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, -1)).r; + + // Merge crossing edges at each side into a single value: + var cc = vec2(2.0) * c.xz + c.yw; + + // Remove the crossing edge if we didn't found the end of the line: + cc = select(cc, vec2(0.0, 0.0), vec2(step(vec2(0.9), d.zw))); + + // Fetch the areas for this line: + weights += area_diag(d.xy, cc, subsample_indices.z); + } + + // Search for the line ends: + let d_xz = search_diag_2(tex_coord, vec2(-1.0, -1.0), &end); + if (textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0, vec2(1, 0)).r > 0.0) { + let d_yw = search_diag_2(tex_coord, vec2(1.0, 1.0), &end); + d = vec4(d.x, d_yw.x, d.z, d_yw.y); + d.y += f32(end.y > 0.9); + } else { + d = vec4(d.x, 0.0, d.z, 0.0); + } + + if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3 + // Fetch the crossing edges: + let coords = vec4(-d.x, -d.x, d.y, d.y) * smaa_info.rt_metrics.xyxy + tex_coord.xyxy; + let c = vec4( + textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0, vec2(-1, 0)).g, + textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0, vec2( 0, -1)).r, + textureSampleLevel(edges_texture, edges_sampler, coords.zw, 0.0, vec2( 1, 0)).gr, + ); + var cc = vec2(2.0) * c.xz + c.yw; + + // Remove the crossing edge if we didn't found the end of the line: + cc = select(cc, vec2(0.0, 0.0), vec2(step(vec2(0.9), d.zw))); + + // Fetch the areas for this line: + weights += area_diag(d.xy, cc, subsample_indices.w).gr; + } + + return weights; +} + +#endif // SMAA_DISABLE_DIAG_DETECTION + +//----------------------------------------------------------------------------- +// Horizontal/Vertical Search Functions + +/** + * This allows to determine how much length should we add in the last step + * of the searches. It takes the bilinearly interpolated edge (see + * @PSEUDO_GATHER4), and adds 0, 1 or 2, depending on which edges and + * crossing edges are active. + */ +fn search_length(e: vec2, offset: f32) -> f32 { + // The texture is flipped vertically, with left and right cases taking half + // of the space horizontally: + var scale = SMAA_SEARCHTEX_SIZE * vec2(0.5, -1.0); + var bias = SMAA_SEARCHTEX_SIZE * vec2(offset, 1.0); + + // Scale and bias to access texel centers: + scale += vec2(-1.0, 1.0); + bias += vec2( 0.5, -0.5); + + // Convert from pixel coordinates to texcoords: + // (We use SMAA_SEARCHTEX_PACKED_SIZE because the texture is cropped) + scale *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE; + bias *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE; + + // Lookup the search texture: + return textureSampleLevel(search_texture, edges_sampler, scale * e + bias, 0.0).r; +} + +/** + * Horizontal/vertical search functions for the 2nd pass. + */ +fn search_x_left(in_tex_coord: vec2, end: f32) -> f32 { + var tex_coord = in_tex_coord; + + /** + * @PSEUDO_GATHER4 + * This texcoord has been offset by (-0.25, -0.125) in the vertex shader to + * sample between edge, thus fetching four edges in a row. + * Sampling with different offsets in each direction allows to disambiguate + * which edges are active from the four fetched ones. + */ + var e = vec2(0.0, 1.0); + while (tex_coord.x > end && + e.g > 0.8281 && // Is there some edge not activated? + e.r == 0.0) { // Or is there a crossing edge that breaks the line? + e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg; + tex_coord += -vec2(2.0, 0.0) * smaa_info.rt_metrics.xy; + } + let offset = -(255.0 / 127.0) * search_length(e, 0.0) + 3.25; + return smaa_info.rt_metrics.x * offset + tex_coord.x; +} + +fn search_x_right(in_tex_coord: vec2, end: f32) -> f32 { + var tex_coord = in_tex_coord; + + var e = vec2(0.0, 1.0); + while (tex_coord.x < end && + e.g > 0.8281 && // Is there some edge not activated? + e.r == 0.0) { // Or is there a crossing edge that breaks the line? + e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg; + tex_coord += vec2(2.0, 0.0) * smaa_info.rt_metrics.xy; + } + let offset = -(255.0 / 127.0) * search_length(e, 0.5) + 3.25; + return -smaa_info.rt_metrics.x * offset + tex_coord.x; +} + +fn search_y_up(in_tex_coord: vec2, end: f32) -> f32 { + var tex_coord = in_tex_coord; + + var e = vec2(1.0, 0.0); + while (tex_coord.y > end && + e.r > 0.8281 && // Is there some edge not activated? + e.g == 0.0) { // Or is there a crossing edge that breaks the line? + e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg; + tex_coord += -vec2(0.0, 2.0) * smaa_info.rt_metrics.xy; + } + let offset = -(255.0 / 127.0) * search_length(e.gr, 0.0) + 3.25; + return smaa_info.rt_metrics.y * offset + tex_coord.y; +} + +fn search_y_down(in_tex_coord: vec2, end: f32) -> f32 { + var tex_coord = in_tex_coord; + + var e = vec2(1.0, 0.0); + while (tex_coord.y < end && + e.r > 0.8281 && // Is there some edge not activated? + e.g == 0.0) { // Or is there a crossing edge that breaks the line? + e = textureSampleLevel(edges_texture, edges_sampler, tex_coord, 0.0).rg; + tex_coord += vec2(0.0, 2.0) * smaa_info.rt_metrics.xy; + } + let offset = -(255.0 / 127.0) * search_length(e.gr, 0.5) + 3.25; + return -smaa_info.rt_metrics.y * offset + tex_coord.y; +} + +/** + * Ok, we have the distance and both crossing edges. So, what are the areas + * at each side of current edge? + */ +fn area(dist: vec2, e1: f32, e2: f32, offset: f32) -> vec2 { + // Rounding prevents precision errors of bilinear filtering: + var tex_coord = SMAA_AREATEX_MAX_DISTANCE * round(4.0 * vec2(e1, e2)) + dist; + + // We do a scale and bias for mapping to texel space: + tex_coord = SMAA_AREATEX_PIXEL_SIZE * tex_coord + 0.5 * SMAA_AREATEX_PIXEL_SIZE; + + // Move to proper place, according to the subpixel offset: + tex_coord.y += SMAA_AREATEX_SUBTEX_SIZE * offset; + + // Do it! + return textureSample(area_texture, edges_sampler, tex_coord).rg; +} + +//----------------------------------------------------------------------------- +// Corner Detection Functions + +fn detect_horizontal_corner_pattern(weights: vec2, tex_coord: vec4, d: vec2) + -> vec2 { +#ifndef SMAA_DISABLE_CORNER_DETECTION + let left_right = step(d.xy, d.yx); + var rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * left_right; + + rounding /= left_right.x + left_right.y; // Reduce blending for pixels in the center of a line. + + var factor = vec2(1.0, 1.0); + factor.x -= rounding.x * + textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2(0, 1)).r; + factor.x -= rounding.y * + textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2(1, 1)).r; + factor.y -= rounding.x * + textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2(0, -2)).r; + factor.y -= rounding.y * + textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2(1, -2)).r; + + return weights * saturate(factor); +#else // SMAA_DISABLE_CORNER_DETECTION + return weights; +#endif // SMAA_DISABLE_CORNER_DETECTION +} + +fn detect_vertical_corner_pattern(weights: vec2, tex_coord: vec4, d: vec2) + -> vec2 { +#ifndef SMAA_DISABLE_CORNER_DETECTION + let left_right = step(d.xy, d.yx); + var rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * left_right; + + rounding /= left_right.x + left_right.y; + + var factor = vec2(1.0, 1.0); + factor.x -= rounding.x * + textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2( 1, 0)).g; + factor.x -= rounding.y * + textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2( 1, 1)).g; + factor.y -= rounding.x * + textureSampleLevel(edges_texture, edges_sampler, tex_coord.xy, 0.0, vec2(-2, 0)).g; + factor.y -= rounding.y * + textureSampleLevel(edges_texture, edges_sampler, tex_coord.zw, 0.0, vec2(-2, 1)).g; + + return weights * saturate(factor); +#else // SMAA_DISABLE_CORNER_DETECTION + return weights; +#endif // SMAA_DISABLE_CORNER_DETECTION +} + +//----------------------------------------------------------------------------- +// Blending Weight Calculation Pixel Shader (Second Pass) + +@fragment +fn blending_weight_calculation_fragment_main(in: BlendingWeightCalculationVaryings) + -> @location(0) vec4 { + let subsample_indices = vec4(0.0); // Just pass zero for SMAA 1x, see @SUBSAMPLE_INDICES. + + var weights = vec4(0.0); + + var e = textureSample(edges_texture, edges_sampler, in.tex_coord).rg; + + if (e.g > 0.0) { // Edge at north +#ifndef SMAA_DISABLE_DIAG_DETECTION + // Diagonals have both north and west edges, so searching for them in + // one of the boundaries is enough. + weights = vec4(calculate_diag_weights(in.tex_coord, e, subsample_indices), weights.ba); + + // We give priority to diagonals, so if we find a diagonal we skip + // horizontal/vertical processing. + if (weights.r + weights.g != 0.0) { + return weights; + } +#endif // SMAA_DISABLE_DIAG_DETECTION + + var d: vec2; + + // Find the distance to the left: + var coords: vec3; + coords.x = search_x_left(in.offset_0.xy, in.offset_2.x); + // in.offset_1.y = in.tex_coord.y - 0.25 * smaa_info.rt_metrics.y (@CROSSING_OFFSET) + coords.y = in.offset_1.y; + d.x = coords.x; + + // Now fetch the left crossing edges, two at a time using bilinear + // filtering. Sampling at -0.25 (see @CROSSING_OFFSET) enables to + // discern what value each edge has: + let e1 = textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0).r; + + // Find the distance to the right: + coords.z = search_x_right(in.offset_0.zw, in.offset_2.y); + d.y = coords.z; + + // We want the distances to be in pixel units (doing this here allow to + // better interleave arithmetic and memory accesses): + d = abs(round(smaa_info.rt_metrics.zz * d - in.position.xx)); + + // SMAAArea below needs a sqrt, as the areas texture is compressed + // quadratically: + let sqrt_d = sqrt(d); + + // Fetch the right crossing edges: + let e2 = textureSampleLevel( + edges_texture, edges_sampler, coords.zy, 0.0, vec2(1, 0)).r; + + // Ok, we know how this pattern looks like, now it is time for getting + // the actual area: + weights = vec4(area(sqrt_d, e1, e2, subsample_indices.y), weights.ba); + + // Fix corners: + coords.y = in.tex_coord.y; + weights = vec4( + detect_horizontal_corner_pattern(weights.rg, coords.xyzy, d), + weights.ba + ); + } + + if (e.r > 0.0) { // Edge at west + var d: vec2; + + // Find the distance to the top: + var coords: vec3; + coords.y = search_y_up(in.offset_1.xy, in.offset_2.z); + // in.offset_1.x = in.tex_coord.x - 0.25 * smaa_info.rt_metrics.x + coords.x = in.offset_0.x; + d.x = coords.y; + + // Fetch the top crossing edges: + let e1 = textureSampleLevel(edges_texture, edges_sampler, coords.xy, 0.0).g; + + // Find the distance to the bottom: + coords.z = search_y_down(in.offset_1.zw, in.offset_2.w); + d.y = coords.z; + + // We want the distances to be in pixel units: + d = abs(round(smaa_info.rt_metrics.ww * d - in.position.yy)); + + // SMAAArea below needs a sqrt, as the areas texture is compressed + // quadratically: + let sqrt_d = sqrt(d); + + // Fetch the bottom crossing edges: + let e2 = textureSampleLevel( + edges_texture, edges_sampler, coords.xz, 0.0, vec2(0, 1)).g; + + // Get the area for this direction: + weights = vec4(weights.rg, area(sqrt_d, e1, e2, subsample_indices.x)); + + // Fix corners: + coords.x = in.tex_coord.x; + weights = vec4(weights.rg, detect_vertical_corner_pattern(weights.ba, coords.xyxz, d)); + } + + return weights; +} + +#endif // SMAA_BLENDING_WEIGHT_CALCULATION + +#ifdef SMAA_NEIGHBORHOOD_BLENDING + +//----------------------------------------------------------------------------- +// Neighborhood Blending Pixel Shader (Third Pass) + +@fragment +fn neighborhood_blending_fragment_main(in: NeighborhoodBlendingVaryings) -> @location(0) vec4 { + // Fetch the blending weights for current pixel: + let a = vec4( + textureSample(blend_texture, blend_sampler, in.offset.xy).a, // Right + textureSample(blend_texture, blend_sampler, in.offset.zw).g, // Top + textureSample(blend_texture, blend_sampler, in.tex_coord).zx, // Bottom / Left + ); + + // Is there any blending weight with a value greater than 0.0? + if (dot(a, vec4(1.0)) < 1.0e-5) { + let color = textureSampleLevel(color_texture, blend_sampler, in.tex_coord, 0.0); + // TODO: Reprojection + return color; + } else { + let h = max(a.x, a.z) > max(a.y, a.w); // max(horizontal) > max(vertical) + + // Calculate the blending offsets: + var blending_offset = vec4(0.0, a.y, 0.0, a.w); + var blending_weight = a.yw; + blending_offset = select(blending_offset, vec4(a.x, 0.0, a.z, 0.0), h); + blending_weight = select(blending_weight, a.xz, h); + blending_weight /= dot(blending_weight, vec2(1.0)); + + // Calculate the texture coordinates: + let blending_coord = + blending_offset * vec4(smaa_info.rt_metrics.xy, -smaa_info.rt_metrics.xy) + + in.tex_coord.xyxy; + + // We exploit bilinear filtering to mix current pixel with the chosen + // neighbor: + var color = blending_weight.x * + textureSampleLevel(color_texture, blend_sampler, blending_coord.xy, 0.0); + color += blending_weight.y * + textureSampleLevel(color_texture, blend_sampler, blending_coord.zw, 0.0); + + // TODO: Reprojection + + return color; + } +} + +#endif // SMAA_NEIGHBORHOOD_BLENDING diff --git a/examples/3d/anti_aliasing.rs b/examples/3d/anti_aliasing.rs index 81c2156a55..7b04a499a0 100644 --- a/examples/3d/anti_aliasing.rs +++ b/examples/3d/anti_aliasing.rs @@ -1,6 +1,7 @@ //! This example compares MSAA (Multi-Sample Anti-aliasing), FXAA (Fast Approximate Anti-aliasing), and TAA (Temporal Anti-aliasing). use std::f32::consts::PI; +use std::fmt::Write; use bevy::{ core_pipeline::{ @@ -9,6 +10,7 @@ use bevy::{ TemporalAntiAliasBundle, TemporalAntiAliasPlugin, TemporalAntiAliasSettings, }, fxaa::{Fxaa, Sensitivity}, + smaa::{SmaaPreset, SmaaSettings}, }, pbr::CascadeShadowConfigBuilder, prelude::*, @@ -34,6 +36,7 @@ fn modify_aa( ( Entity, Option<&mut Fxaa>, + Option<&mut SmaaSettings>, Option<&TemporalAntiAliasSettings>, ), With, @@ -41,19 +44,21 @@ fn modify_aa( mut msaa: ResMut, mut commands: Commands, ) { - let (camera_entity, fxaa, taa) = camera.single_mut(); + let (camera_entity, fxaa, smaa, taa) = camera.single_mut(); let mut camera = commands.entity(camera_entity); // No AA if keys.just_pressed(KeyCode::Digit1) { *msaa = Msaa::Off; camera.remove::(); + camera.remove::(); camera.remove::(); } // MSAA if keys.just_pressed(KeyCode::Digit2) && *msaa == Msaa::Off { camera.remove::(); + camera.remove::(); camera.remove::(); *msaa = Msaa::Sample4; @@ -75,6 +80,7 @@ fn modify_aa( // FXAA if keys.just_pressed(KeyCode::Digit3) && fxaa.is_none() { *msaa = Msaa::Off; + camera.remove::(); camera.remove::(); camera.insert(Fxaa::default()); @@ -104,10 +110,36 @@ fn modify_aa( } } - // TAA - if keys.just_pressed(KeyCode::Digit4) && taa.is_none() { + // SMAA + if keys.just_pressed(KeyCode::Digit4) && smaa.is_none() { *msaa = Msaa::Off; camera.remove::(); + camera.remove::(); + + camera.insert(SmaaSettings::default()); + } + + // SMAA Settings + if let Some(mut smaa) = smaa { + if keys.just_pressed(KeyCode::KeyQ) { + smaa.preset = SmaaPreset::Low; + } + if keys.just_pressed(KeyCode::KeyW) { + smaa.preset = SmaaPreset::Medium; + } + if keys.just_pressed(KeyCode::KeyE) { + smaa.preset = SmaaPreset::High; + } + if keys.just_pressed(KeyCode::KeyR) { + smaa.preset = SmaaPreset::Ultra; + } + } + + // TAA + if keys.just_pressed(KeyCode::Digit5) && taa.is_none() { + *msaa = Msaa::Off; + camera.remove::(); + camera.remove::(); camera.insert(TemporalAntiAliasBundle::default()); } @@ -141,6 +173,7 @@ fn update_ui( camera: Query< ( Option<&Fxaa>, + Option<&SmaaSettings>, Option<&TemporalAntiAliasSettings>, &ContrastAdaptiveSharpeningSettings, ), @@ -149,104 +182,67 @@ fn update_ui( msaa: Res, mut ui: Query<&mut Text>, ) { - let (fxaa, taa, cas_settings) = camera.single(); + let (fxaa, smaa, taa, cas_settings) = camera.single(); let mut ui = ui.single_mut(); let ui = &mut ui.sections[0].value; *ui = "Antialias Method\n".to_string(); - if *msaa == Msaa::Off && fxaa.is_none() && taa.is_none() { - ui.push_str("(1) *No AA*\n"); - } else { - ui.push_str("(1) No AA\n"); - } + draw_selectable_menu_item( + ui, + "No AA", + '1', + *msaa == Msaa::Off && fxaa.is_none() && taa.is_none() && smaa.is_none(), + ); + draw_selectable_menu_item(ui, "MSAA", '2', *msaa != Msaa::Off); + draw_selectable_menu_item(ui, "FXAA", '3', fxaa.is_some()); + draw_selectable_menu_item(ui, "SMAA", '4', smaa.is_some()); + draw_selectable_menu_item(ui, "TAA", '5', taa.is_some()); if *msaa != Msaa::Off { - ui.push_str("(2) *MSAA*\n"); - } else { - ui.push_str("(2) MSAA\n"); - } - - if fxaa.is_some() { - ui.push_str("(3) *FXAA*\n"); - } else { - ui.push_str("(3) FXAA\n"); - } - - if taa.is_some() { - ui.push_str("(4) *TAA*"); - } else { - ui.push_str("(4) TAA"); - } - - if *msaa != Msaa::Off { - ui.push_str("\n\n----------\n\nSample Count\n"); - - if *msaa == Msaa::Sample2 { - ui.push_str("(Q) *2*\n"); - } else { - ui.push_str("(Q) 2\n"); - } - if *msaa == Msaa::Sample4 { - ui.push_str("(W) *4*\n"); - } else { - ui.push_str("(W) 4\n"); - } - if *msaa == Msaa::Sample8 { - ui.push_str("(E) *8*"); - } else { - ui.push_str("(E) 8"); - } + ui.push_str("\n----------\n\nSample Count\n"); + draw_selectable_menu_item(ui, "2", 'Q', *msaa == Msaa::Sample2); + draw_selectable_menu_item(ui, "4", 'W', *msaa == Msaa::Sample4); + draw_selectable_menu_item(ui, "8", 'E', *msaa == Msaa::Sample8); } if let Some(fxaa) = fxaa { - ui.push_str("\n\n----------\n\nSensitivity\n"); - - if fxaa.edge_threshold == Sensitivity::Low { - ui.push_str("(Q) *Low*\n"); - } else { - ui.push_str("(Q) Low\n"); - } - - if fxaa.edge_threshold == Sensitivity::Medium { - ui.push_str("(W) *Medium*\n"); - } else { - ui.push_str("(W) Medium\n"); - } - - if fxaa.edge_threshold == Sensitivity::High { - ui.push_str("(E) *High*\n"); - } else { - ui.push_str("(E) High\n"); - } - - if fxaa.edge_threshold == Sensitivity::Ultra { - ui.push_str("(R) *Ultra*\n"); - } else { - ui.push_str("(R) Ultra\n"); - } - - if fxaa.edge_threshold == Sensitivity::Extreme { - ui.push_str("(T) *Extreme*"); - } else { - ui.push_str("(T) Extreme"); - } + ui.push_str("\n----------\n\nSensitivity\n"); + draw_selectable_menu_item(ui, "Low", 'Q', fxaa.edge_threshold == Sensitivity::Low); + draw_selectable_menu_item( + ui, + "Medium", + 'W', + fxaa.edge_threshold == Sensitivity::Medium, + ); + draw_selectable_menu_item(ui, "High", 'E', fxaa.edge_threshold == Sensitivity::High); + draw_selectable_menu_item(ui, "Ultra", 'R', fxaa.edge_threshold == Sensitivity::Ultra); + draw_selectable_menu_item( + ui, + "Extreme", + 'T', + fxaa.edge_threshold == Sensitivity::Extreme, + ); } + if let Some(smaa) = smaa { + ui.push_str("\n----------\n\nQuality\n"); + draw_selectable_menu_item(ui, "Low", 'Q', smaa.preset == SmaaPreset::Low); + draw_selectable_menu_item(ui, "Medium", 'W', smaa.preset == SmaaPreset::Medium); + draw_selectable_menu_item(ui, "High", 'E', smaa.preset == SmaaPreset::High); + draw_selectable_menu_item(ui, "Ultra", 'R', smaa.preset == SmaaPreset::Ultra); + } + + ui.push_str("\n----------\n\n"); + draw_selectable_menu_item(ui, "Sharpening", '0', cas_settings.enabled); + if cas_settings.enabled { - ui.push_str("\n\n----------\n\n(0) Sharpening (Enabled)\n"); ui.push_str(&format!( "(-/+) Strength: {:.1}\n", cas_settings.sharpening_strength )); - if cas_settings.denoise { - ui.push_str("(D) Denoising (Enabled)\n"); - } else { - ui.push_str("(D) Denoising (Disabled)\n"); - } - } else { - ui.push_str("\n\n----------\n\n(0) Sharpening (Disabled)\n"); + draw_selectable_menu_item(ui, "Denoising", 'D', cas_settings.denoise); } } @@ -350,6 +346,12 @@ fn setup( ); } +/// Writes a simple menu item that can be on or off. +fn draw_selectable_menu_item(ui: &mut String, label: &str, shortcut: char, enabled: bool) { + let star = if enabled { "*" } else { "" }; + let _ = writeln!(*ui, "({}) {}{}{}", shortcut, star, label, star); +} + /// Creates a colorful test pattern fn uv_debug_texture() -> Image { const TEXTURE_SIZE: usize = 8; diff --git a/typos.toml b/typos.toml index ce87fc5f04..bf338fb5c9 100644 --- a/typos.toml +++ b/typos.toml @@ -12,11 +12,13 @@ extend-exclude = [ # Match Whole Word - Case Sensitive [default.extend-identifiers] -iy = "iy" # Variable name used in bevy_gizmos. Probably stands for "y-axis index", as it's being used in loops. -ser = "ser" # ron::ser - Serializer -SME = "SME" # Subject Matter Expert -Sur = "Sur" # macOS Big Sur - South -Ba = "Ba" # Bitangent for Anisotropy +iy = "iy" # Variable name used in bevy_gizmos. Probably stands for "y-axis index", as it's being used in loops. +ser = "ser" # ron::ser - Serializer +SME = "SME" # Subject Matter Expert +Sur = "Sur" # macOS Big Sur - South +Masia = "Masia" # The surname of one of the authors of SMAA +Ba = "Ba" # Bitangent for Anisotropy +ba = "ba" # Part of an accessor in WGSL - color.ba # Match Inside a Word - Case Insensitive [default.extend-words]