use glam::Mat4;
use std::num::NonZeroU64;
use wgpu::util::DeviceExt;
#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
#[allow(clippy::pub_underscore_fields)]
pub struct SsaoUniforms {
pub proj: [[f32; 4]; 4],
pub inv_proj: [[f32; 4]; 4],
pub radius: f32,
pub bias: f32,
pub intensity: f32,
pub sample_count: u32,
pub screen_width: f32,
pub screen_height: f32,
pub _padding: [f32; 2],
}
impl Default for SsaoUniforms {
fn default() -> Self {
Self {
proj: Mat4::IDENTITY.to_cols_array_2d(),
inv_proj: Mat4::IDENTITY.to_cols_array_2d(),
radius: 0.5,
bias: 0.025,
intensity: 1.0,
sample_count: 16,
screen_width: 1280.0,
screen_height: 720.0,
_padding: [0.0; 2],
}
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
#[allow(clippy::pub_underscore_fields)]
pub struct SsaoBlurUniforms {
pub texel_size: [f32; 2],
pub blur_scale: f32,
pub blur_sharpness: f32,
}
pub struct SsaoPass {
ssao_pipeline: wgpu::RenderPipeline,
ssao_bind_group_layout: wgpu::BindGroupLayout,
ssao_uniform_buffer: wgpu::Buffer,
blur_pipeline: wgpu::RenderPipeline,
blur_bind_group_layout: wgpu::BindGroupLayout,
blur_uniform_buffer: wgpu::Buffer,
ssao_texture: wgpu::Texture,
ssao_view: wgpu::TextureView,
sampler: wgpu::Sampler,
}
impl SsaoPass {
#[must_use]
pub fn new(device: &wgpu::Device, width: u32, height: u32) -> Self {
let ssao_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("SSAO Shader"),
source: wgpu::ShaderSource::Wgsl(include_str!("shaders/ssao.wgsl").into()),
});
let blur_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("SSAO Blur Shader"),
source: wgpu::ShaderSource::Wgsl(include_str!("shaders/ssao_blur.wgsl").into()),
});
let ssao_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("SSAO Bind Group Layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Depth,
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 4,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: NonZeroU64::new(160),
},
count: None,
},
],
});
let ssao_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("SSAO Pipeline Layout"),
bind_group_layouts: &[&ssao_bind_group_layout],
push_constant_ranges: &[],
});
let ssao_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("SSAO Pipeline"),
layout: Some(&ssao_pipeline_layout),
vertex: wgpu::VertexState {
module: &ssao_shader,
entry_point: Some("vs_main"),
buffers: &[],
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState {
module: &ssao_shader,
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::R8Unorm, blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: wgpu::PipelineCompilationOptions::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview: None,
cache: None,
});
let blur_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("SSAO Blur Bind Group Layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Depth,
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: NonZeroU64::new(16),
},
count: None,
},
],
});
let blur_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("SSAO Blur Pipeline Layout"),
bind_group_layouts: &[&blur_bind_group_layout],
push_constant_ranges: &[],
});
let blur_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("SSAO Blur Pipeline"),
layout: Some(&blur_pipeline_layout),
vertex: wgpu::VertexState {
module: &blur_shader,
entry_point: Some("vs_main"),
buffers: &[],
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState {
module: &blur_shader,
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::R8Unorm,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: wgpu::PipelineCompilationOptions::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview: None,
cache: None,
});
let ssao_uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("SSAO Uniform Buffer"),
contents: bytemuck::cast_slice(&[SsaoUniforms::default()]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let blur_uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("SSAO Blur Uniform Buffer"),
contents: bytemuck::cast_slice(&[SsaoBlurUniforms {
texel_size: [1.0 / width as f32, 1.0 / height as f32],
blur_scale: 1.0,
blur_sharpness: 50.0, }]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let ssao_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("SSAO Texture"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R8Unorm,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
});
let ssao_view = ssao_texture.create_view(&wgpu::TextureViewDescriptor::default());
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("SSAO Sampler"),
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
..Default::default()
});
Self {
ssao_pipeline,
ssao_bind_group_layout,
ssao_uniform_buffer,
blur_pipeline,
blur_bind_group_layout,
blur_uniform_buffer,
ssao_texture,
ssao_view,
sampler,
}
}
pub fn resize(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, width: u32, height: u32) {
self.ssao_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("SSAO Texture"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R8Unorm,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
});
self.ssao_view = self
.ssao_texture
.create_view(&wgpu::TextureViewDescriptor::default());
queue.write_buffer(
&self.blur_uniform_buffer,
0,
bytemuck::cast_slice(&[SsaoBlurUniforms {
texel_size: [1.0 / width as f32, 1.0 / height as f32],
blur_scale: 1.0,
blur_sharpness: 50.0,
}]),
);
}
pub fn update_uniforms(
&self,
queue: &wgpu::Queue,
proj: Mat4,
inv_proj: Mat4,
radius: f32,
bias: f32,
intensity: f32,
sample_count: u32,
width: f32,
height: f32,
) {
let uniforms = SsaoUniforms {
proj: proj.to_cols_array_2d(),
inv_proj: inv_proj.to_cols_array_2d(),
radius,
bias,
intensity,
sample_count,
screen_width: width,
screen_height: height,
_padding: [0.0; 2],
};
queue.write_buffer(
&self.ssao_uniform_buffer,
0,
bytemuck::cast_slice(&[uniforms]),
);
}
#[must_use]
pub fn create_ssao_bind_group(
&self,
device: &wgpu::Device,
depth_view: &wgpu::TextureView,
normal_view: &wgpu::TextureView,
noise_view: &wgpu::TextureView,
) -> wgpu::BindGroup {
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("SSAO Bind Group"),
layout: &self.ssao_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(depth_view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(normal_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::TextureView(noise_view),
},
wgpu::BindGroupEntry {
binding: 3,
resource: wgpu::BindingResource::Sampler(&self.sampler),
},
wgpu::BindGroupEntry {
binding: 4,
resource: self.ssao_uniform_buffer.as_entire_binding(),
},
],
})
}
#[must_use]
pub fn create_blur_bind_group(
&self,
device: &wgpu::Device,
depth_view: &wgpu::TextureView,
) -> wgpu::BindGroup {
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("SSAO Blur Bind Group"),
layout: &self.blur_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&self.ssao_view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(depth_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&self.sampler),
},
wgpu::BindGroupEntry {
binding: 3,
resource: self.blur_uniform_buffer.as_entire_binding(),
},
],
})
}
pub fn render_ssao(&self, encoder: &mut wgpu::CommandEncoder, bind_group: &wgpu::BindGroup) {
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("SSAO Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &self.ssao_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::WHITE),
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
..Default::default()
});
render_pass.set_pipeline(&self.ssao_pipeline);
render_pass.set_bind_group(0, bind_group, &[]);
render_pass.draw(0..3, 0..1);
}
pub fn render_blur(
&self,
encoder: &mut wgpu::CommandEncoder,
output_view: &wgpu::TextureView,
bind_group: &wgpu::BindGroup,
) {
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("SSAO Blur Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: output_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::WHITE),
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
..Default::default()
});
render_pass.set_pipeline(&self.blur_pipeline);
render_pass.set_bind_group(0, bind_group, &[]);
render_pass.draw(0..3, 0..1);
}
#[must_use]
pub fn ssao_view(&self) -> &wgpu::TextureView {
&self.ssao_view
}
}