1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::utils::{load_glsl, ShaderStage};
use nalgebra_glm as glm;
use rand::Rng;
use wgpu;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct SsaoGlobals {
pub projection: [f32; 16],
pub samples: [f32; 64 * 4],
pub noise: [f32; 4 * 4 * 4],
}
unsafe impl bytemuck::Zeroable for SsaoGlobals {}
unsafe impl bytemuck::Pod for SsaoGlobals {}
fn lerp(a: f32, b: f32, f: f32) -> f32 {
a + f * (b - a)
}
impl Default for SsaoGlobals {
fn default() -> Self {
let mut rng = rand::thread_rng();
let mut samples = [0.0; 64 * 4];
for i in 0..64 {
let mut sample = glm::vec3(
rng.gen_range(0.0, 1.0) * 2.0 - 1.0,
rng.gen_range(0.0, 1.0) * 2.0 - 1.0,
rng.gen_range(0.0, 1.0),
);
sample = glm::normalize(&sample);
sample *= rng.gen_range(0.0, 1.0);
let mut scale = i as f32 / 64.0;
scale = lerp(0.1, 1.0, scale * scale);
sample *= scale;
samples[i * 4] = sample.x;
samples[i * 4 + 1] = sample.y;
samples[i * 4 + 2] = sample.z;
}
let mut noise = [0.0; 4 * 4 * 4];
for i in 0..16 {
let sample = glm::vec3(rng.gen_range(0.0, 1.0) * 2.0 - 1.0, rng.gen_range(0.0, 1.0) * 2.0 - 1.0, 0.0);
noise[i * 4] = sample.x;
noise[i * 4 + 1] = sample.y;
noise[i * 4 + 2] = sample.z;
}
Self {
projection: [0.0; 16],
samples,
noise,
}
}
}
pub struct SsaoPipeline {
pub pipeline: wgpu::ComputePipeline,
pub bind_group_layout: wgpu::BindGroupLayout,
}
impl SsaoPipeline {
pub fn new(device: &wgpu::Device) -> Self {
let cs_bytes = load_glsl(include_str!("ssao.comp"), ShaderStage::Compute);
let cs_module = device.create_shader_module(&cs_bytes);
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("SSAO bind group layout"),
bindings: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::UniformBuffer { dynamic: false },
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::SampledTexture {
component_type: wgpu::TextureComponentType::Float,
multisampled: false,
dimension: wgpu::TextureViewDimension::D2,
},
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::SampledTexture {
component_type: wgpu::TextureComponentType::Float,
multisampled: false,
dimension: wgpu::TextureViewDimension::D2,
},
},
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::StorageTexture {
dimension: wgpu::TextureViewDimension::D2,
component_type: wgpu::TextureComponentType::Float,
format: wgpu::TextureFormat::Rgba32Float,
readonly: false,
},
},
],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout],
});
let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
layout: &pipeline_layout,
compute_stage: wgpu::ProgrammableStageDescriptor {
module: &cs_module,
entry_point: "main",
},
});
Self {
pipeline,
bind_group_layout,
}
}
}