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
//! Per-element isolated backdrop blur pass.
//! Copies a scissored region from the scene texture into a
//! blur target the glass pass can sample, then runs a Kawase
//! downsample chain on the copied region.
use crate::kvasir::node::{ExecutionContext, KvasirNode};
use crate::kvasir::nodes::PassId;
use crate::kvasir::resource::ResourceId;
/// Copies a rectangular region from the scene texture into a
/// blur target resource, then runs a Kawase downsample chain.
pub struct BackdropRegionNode {
pub inputs: Vec<ResourceId>,
pub outputs: Vec<ResourceId>,
/// Region in logical pixels.
pub region: cvkg_core::Rect,
/// Output resource ID (allocated by the graph builder).
pub output_id: ResourceId,
}
impl BackdropRegionNode {
pub fn new(region: cvkg_core::Rect, output_id: ResourceId) -> Self {
Self {
inputs: vec![crate::kvasir::nodes::RES_SCENE],
outputs: vec![output_id],
region,
output_id,
}
}
}
impl KvasirNode for BackdropRegionNode {
fn label(&self) -> &'static str {
"Backdrop Region"
}
fn inputs(&self) -> &[ResourceId] {
&self.inputs
}
fn outputs(&self) -> &[ResourceId] {
&self.outputs
}
fn pass_id(&self) -> PassId {
PassId::BackdropRegion
}
fn execute(&self, ctx: &mut ExecutionContext) {
let scene_tex = ctx
.registry
.get_texture(crate::kvasir::nodes::RES_SCENE)
.expect("scene texture must exist");
let blur_tex = ctx
.registry
.get_texture(self.output_id)
.expect("blur target texture must exist");
let scale = ctx.scale_factor;
let rx = (self.region.x * scale) as u32;
let ry = (self.region.y * scale) as u32;
let rw = (self.region.width * scale) as u32;
let rh = (self.region.height * scale) as u32;
// Phase 1: GPU copy of the scissored region from scene to blur target.
// Uses copy_texture_to_texture for an actual pixel-exact copy (no shader needed).
let _src_extent = wgpu::Extent3d {
width: scene_tex.width(),
height: scene_tex.height(),
depth_or_array_layers: 1,
};
let dst_extent = wgpu::Extent3d {
width: rw,
height: rh,
depth_or_array_layers: 1,
};
ctx.encoder.copy_texture_to_texture(
wgpu::TexelCopyTextureInfo {
texture: &scene_tex,
mip_level: 0,
origin: wgpu::Origin3d { x: rx, y: ry, z: 0 },
aspect: wgpu::TextureAspect::All,
},
wgpu::TexelCopyTextureInfo {
texture: &blur_tex,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
dst_extent,
);
// Phase 2: Generate mips for the blurred backdrop region.
// This lets the glass shader sample at different blur levels.
// We do a simple blur via a Kawase-style approach on the copied region.
let mip_count = blur_tex.mip_level_count().min(4);
if mip_count >= 2 {
// Reuse persistent uniform buffer (avoids per-frame GPU allocation)
let kawase_uniform = &ctx.renderer.kawase_uniform;
for mip in 1..mip_count {
let src_view = {
let mut cache = ctx.renderer.texture_view_cache.lock().unwrap();
cache
.entry((self.output_id, (mip - 1)))
.or_insert_with(|| {
blur_tex.create_view(&wgpu::TextureViewDescriptor {
label: Some(&format!("blur_region_src_mip_{}", mip - 1)),
base_mip_level: mip - 1,
mip_level_count: Some(1),
..Default::default()
})
})
.clone()
};
let dst_view = {
let mut cache = ctx.renderer.texture_view_cache.lock().unwrap();
cache
.entry((self.output_id, mip))
.or_insert_with(|| {
blur_tex.create_view(&wgpu::TextureViewDescriptor {
label: Some(&format!("blur_region_dst_mip_{}", mip)),
base_mip_level: mip,
mip_level_count: Some(1),
..Default::default()
})
})
.clone()
};
let w = (rw >> mip).max(1);
let h = (rh >> mip).max(1);
let kernel = mip as f32;
let uniform_data: [f32; 8] = [
w as f32,
h as f32,
(mip - 1) as f32,
kernel,
0.0,
0.0,
0.0,
0.0,
];
ctx.queue
.write_buffer(kawase_uniform, 0, bytemuck::cast_slice(&uniform_data));
let src_bg = ctx.get_or_create_bind_group(
(self.output_id, mip, false),
&ctx.renderer.kawase_bind_group_layout,
&[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: kawase_uniform,
offset: 0,
size: wgpu::BufferSize::new(32),
}),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&src_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&ctx.renderer.sampler),
},
],
Some(&format!("blur_region_kawase_bg_{}", mip)),
);
let mut pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(&format!("Backdrop Region Blur {}", mip)),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &dst_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
..Default::default()
});
pass.set_viewport(0.0, 0.0, w as f32, h as f32, 0.0, 1.0);
pass.set_pipeline(&ctx.renderer.kawase_down_pipeline);
pass.set_bind_group(0, &src_bg, &[]);
pass.draw(0..3, 0..1);
}
// Upsample chain
for mip in (1..mip_count).rev() {
let src_view = {
let mut cache = ctx.renderer.texture_view_cache.lock().unwrap();
cache
.entry((self.output_id, mip))
.or_insert_with(|| {
blur_tex.create_view(&wgpu::TextureViewDescriptor {
label: Some(&format!("blur_region_src_mip_{}", mip)),
base_mip_level: mip,
mip_level_count: Some(1),
..Default::default()
})
})
.clone()
};
let dst_view = {
let mut cache = ctx.renderer.texture_view_cache.lock().unwrap();
cache
.entry((self.output_id, (mip - 1)))
.or_insert_with(|| {
blur_tex.create_view(&wgpu::TextureViewDescriptor {
label: Some(&format!("blur_region_dst_mip_{}", mip - 1)),
base_mip_level: mip - 1,
mip_level_count: Some(1),
..Default::default()
})
})
.clone()
};
let w = (rw >> (mip - 1)).max(1);
let h = (rh >> (mip - 1)).max(1);
let kernel = mip as f32;
let uniform_data: [f32; 8] =
[w as f32, h as f32, mip as f32, kernel, 0.0, 0.0, 0.0, 0.0];
ctx.queue
.write_buffer(kawase_uniform, 0, bytemuck::cast_slice(&uniform_data));
let src_bg = ctx.get_or_create_bind_group(
(self.output_id, mip, true),
&ctx.renderer.kawase_bind_group_layout,
&[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: kawase_uniform,
offset: 0,
size: wgpu::BufferSize::new(32),
}),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&src_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&ctx.renderer.sampler),
},
],
Some(&format!("blur_region_kawase_up_bg_{}", mip)),
);
// Clear the destination view on load to prevent additive compounding of light during the upsample chain
let mut pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(&format!("Backdrop Region Blur Up {}", mip)),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &dst_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
..Default::default()
});
pass.set_viewport(0.0, 0.0, w as f32, h as f32, 0.0, 1.0);
pass.set_pipeline(&ctx.renderer.kawase_up_pipeline);
pass.set_bind_group(0, &src_bg, &[]);
pass.draw(0..3, 0..1);
}
}
}
}