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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! Topic 43's render-scale row: the spatial upscale
//! that carries an internal render target to the caller's own extent.
//!
//! # It is the second pass in this crate that changes the frame's shape
//!
//! [`crate::fxaa`] is the first, and the argument is the same one: with render
//! scale at `1.0` the post chain writes the caller's target directly and this
//! pass does not exist, and below `1.0` every internal target in the frame is
//! smaller than the target and this pass is what reaches it. [`crate::forward`]
//! owns the choice, because it owns both ends.
//!
//! The two compose in one order and only one. The resolve runs at the internal
//! extent, before this pass, so FXAA filters the edges the renderer actually
//! drew; resolving after the upscale would be filtering an interpolation of
//! them. The UI goes the other way and composites onto the target after this
//! pass at native resolution, which is the whole reason a render-scale knob is
//! usable at all — text stays sharp while the 3D frame is cheap.
//!
//! # One pass, one sampler, and why it is linear
//!
//! `upscale.slang` computes its own sixteen weights, so a nearest sampler would
//! be the defensible choice. It is the wrong one at the border: every tap is
//! addressed by UV and the ring outside the source is what `ClampToEdge`
//! decides, and a linear sampler reaching a clamped edge returns the edge texel
//! exactly, which is what the filter wants there.
use crcbl_hal::{
BindGroupEntry, BindGroupHandle, BindGroupLayoutDesc, BindGroupLayoutEntry,
BindGroupLayoutHandle, BindingFlags, BindingKind, BindingResource, BufferDesc, BufferHandle,
BufferUsage, ClearValue, ColorTargetState, Device, FilterMode, Format, GraphicsPipelineHandle,
HalError, ImageViewHandle, ImageViewType, LoadOp, MemoryLocation, PipelineLayoutDesc,
PipelineLayoutHandle, SampleType, SamplerAddressMode, SamplerDesc, SamplerHandle, ShaderStages,
StoreOp, check_portable_storage_buffers,
};
use crcbl_shaders::{UPSCALE, upscale};
use crate::graph::{ImageId, RenderGraph};
use crate::ssao::cached_group;
/// Vertices in the over-sized full-screen triangle `upscale.slang` generates
/// from `SV_VertexID`. No geometry is bound anywhere.
const FULLSCREEN_VERTICES: u32 = 3;
/// Everything the upscale owns.
///
/// Built once by [`Upscale::new`] and released by [`Upscale::destroy`], which is
/// the shape every other resource group in this crate has — see [`crate::fxaa`].
#[derive(Debug)]
pub(crate) struct Upscale {
/// `[frame]`: `upscale.slang`'s uniform block, one per frame in flight for
/// the frame uniforms' reason exactly — the previous frame may still be
/// reading last frame's while this one is written.
uniforms: Vec<BufferHandle>,
layout: BindGroupLayoutHandle,
pipeline_layout: PipelineLayoutHandle,
pipeline: GraphicsPipelineHandle,
sampler: SamplerHandle,
/// `[frame]`: the group, cached against the source view. One per frame in
/// flight, because it names [`Upscale::uniforms`] as well as the transient.
groups: Vec<Option<(Vec<ImageViewHandle>, BindGroupHandle)>>,
}
impl Upscale {
/// Passes [`Upscale::add_pass`] adds to a frame.
pub(crate) const PASSES: u32 = 1;
/// Builds the pipeline, the sampler and the uniform ring.
///
/// `build_fullscreen` is handed in rather than duplicated, on
/// [`crate::fxaa::Fxaa::new`]'s terms exactly: it is [`crate::forward`]'s.
///
/// `target_format` is the format this pass writes — the caller's target —
/// and, because what it reads is a transient of that same description, also
/// the format it reads.
///
/// # Errors
///
/// [`HalError`] from any seam call. **Nothing is released on the failing
/// path**: the caller holds a rollback, and this is stored in it whole.
pub(crate) fn new(
device: &dyn Device,
frames: usize,
target_format: Format,
build_fullscreen: impl Fn(
&dyn Device,
&str,
&crcbl_shaders::Shader,
PipelineLayoutHandle,
&[ColorTargetState],
) -> Result<GraphicsPipelineHandle, HalError>,
) -> Result<Self, HalError> {
let entries = [
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::FRAGMENT,
kind: BindingKind::SampledImage {
view_type: ImageViewType::D2,
sample_type: SampleType::Float,
},
count: 1,
flags: BindingFlags::empty(),
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::FRAGMENT,
kind: BindingKind::Sampler { comparison: false },
count: 1,
flags: BindingFlags::empty(),
},
// A uniform buffer rather than a push constant, for the reason every
// post pass in this crate gives: WebGPU has no push constants, and
// one Slang entry point cannot read both a push-constant block and a
// bound one, so a range here would fork the shader.
BindGroupLayoutEntry {
binding: 2,
visibility: ShaderStages::FRAGMENT,
kind: BindingKind::UniformBuffer { dynamic: false },
count: 1,
flags: BindingFlags::empty(),
},
];
let desc = BindGroupLayoutDesc {
label: Some("upscale"),
entries: &entries,
};
check_portable_storage_buffers(Some("upscale"), &[&desc])?;
let layout = device.create_bind_group_layout(&desc)?;
let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDesc {
label: Some("upscale"),
bind_group_layouts: &[layout],
push_constants: None,
})?;
let pipeline = build_fullscreen(
device,
"upscale",
&UPSCALE,
pipeline_layout,
&[ColorTargetState::opaque(target_format)],
)?;
// **Linear, and only the border depends on it** — see the module docs.
let sampler = device.create_sampler(&SamplerDesc {
label: Some("upscale source"),
mag_filter: FilterMode::Linear,
min_filter: FilterMode::Linear,
mip_filter: FilterMode::Nearest,
address_mode: [SamplerAddressMode::ClampToEdge; 3],
..SamplerDesc::default()
})?;
let mut uniforms = Vec::with_capacity(frames);
for _ in 0..frames {
uniforms.push(device.create_buffer(&BufferDesc {
label: Some("upscale params"),
size: upscale::PARAMS_SIZE as u64,
usage: BufferUsage::UNIFORM,
memory: MemoryLocation::HostUpload,
})?);
}
Ok(Self {
uniforms,
layout,
pipeline_layout,
pipeline,
sampler,
groups: (0..frames).map(|_| None).collect(),
})
}
/// Writes this frame's block, where `source` is the **internal** render
/// extent and not the caller's target extent.
///
/// # Errors
///
/// [`HalError`] from the mapped write.
///
/// # Panics
///
/// If `frame` is not a slot this was built with.
pub(crate) fn begin_frame(
&self,
device: &dyn Device,
frame: usize,
source: (u32, u32),
) -> Result<(), HalError> {
let params = upscale::UpscaleParams::for_extent(source.0, source.1);
device.write_buffer(self.uniforms[frame], 0, ¶ms.to_bytes())
}
/// Adds the `upscale` pass, reading `source` and writing `target`.
///
/// # Panics
///
/// If `frame` is not a slot this was built with.
pub(crate) fn add_pass<'a>(
&'a mut self,
graph: &mut RenderGraph<'a>,
frame: usize,
source: ImageId,
target: ImageId,
) {
let pipeline = self.pipeline;
let pipeline_layout = self.pipeline_layout;
let layout = self.layout;
let sampler = self.sampler;
let uniforms = self.uniforms[frame];
let cached = &mut self.groups[frame];
graph
.add_render_pass("upscale")
// `DontCare`, not `Clear`: the full-screen triangle writes every
// pixel of the target, so loading or clearing it is pure bandwidth.
.color(
target,
LoadOp::DontCare,
StoreOp::Store,
ClearValue::default(),
)
.read_image(source)
.execute(move |ctx| {
let view = ctx.image_view(source);
let device = ctx.device();
let entries = [
BindGroupEntry {
binding: 0,
array_index: 0,
// Overwritten by `cached_group` with the realised view;
// written here so the list is a complete description of
// the layout rather than a list with a hole in it.
resource: BindingResource::ImageView(view),
},
BindGroupEntry {
binding: 1,
array_index: 0,
resource: BindingResource::Sampler(sampler),
},
BindGroupEntry {
binding: 2,
array_index: 0,
resource: BindingResource::whole_buffer(uniforms),
},
];
let Some(group) = cached_group(
cached,
device,
&[(0, view)],
"upscale source",
layout,
&entries,
) else {
return;
};
let encoder = ctx.encoder();
encoder.bind_graphics_pipeline(pipeline);
encoder.bind_group(0, group, &[], pipeline_layout);
encoder.draw(0..FULLSCREEN_VERTICES, 0..1);
});
}
/// Releases everything, in dependency order. The device must be idle.
pub(crate) fn destroy(self, device: &dyn Device) {
for cached in self.groups.into_iter().flatten() {
device.destroy_bind_group(cached.1);
}
device.destroy_sampler(self.sampler);
device.destroy_graphics_pipeline(self.pipeline);
device.destroy_pipeline_layout(self.pipeline_layout);
device.destroy_bind_group_layout(self.layout);
for buffer in self.uniforms {
device.destroy_buffer(buffer);
}
}
}