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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! GPU-accelerated content bounds computation.
//!
//! A compute shader scans a texture and produces the tight bounding rect of
//! all non-transparent pixels using atomic min/max. The result is 16 bytes
//! (4× u32) read back asynchronously — no full-texture readback required.
//!
//! The compositor owns a [`ContentBoundsPass`] and exposes cached per-layer
//! bounds. Bounds are invalidated on [`mark_dirty`](super::compositor::Compositor::mark_dirty)
//! and recomputed lazily when a consumer requests them.
use crate::layer::LayerId;
use std::collections::HashMap;
/// Initial values for the atomic bounds buffer: min = MAX, max = 0.
/// If min_x > max_x after dispatch, the texture is fully transparent.
const BOUNDS_INIT: [u32; 4] = [u32::MAX, u32::MAX, 0, 0];
/// GPU compute pipeline + per-layer cache for content bounds.
pub struct ContentBoundsPass {
pipeline: wgpu::ComputePipeline,
bind_group_layout: wgpu::BindGroupLayout,
/// Cached content bounds per layer: `[x, y, w, h]`.
cached: HashMap<LayerId, [u32; 4]>,
/// Generation counter per layer — incremented on invalidation.
/// Pending results whose generation doesn't match are discarded.
generation: HashMap<LayerId, u64>,
/// In-flight compute dispatches awaiting buffer mapping.
pending: Vec<PendingBounds>,
}
struct PendingBounds {
layer_id: LayerId,
gen: u64,
staging: wgpu::Buffer,
rx: Option<std::sync::mpsc::Receiver<Result<(), wgpu::BufferAsyncError>>>,
}
/// Uniform buffer layout matching the shader's `Params` struct.
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Params {
width: u32,
height: u32,
use_r_channel: u32,
_pad: u32,
}
impl ContentBoundsPass {
pub fn new(device: &wgpu::Device) -> Self {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("content-bounds-shader"),
source: wgpu::ShaderSource::Wgsl(
include_str!("../../shaders/content_bounds.wgsl").into(),
),
});
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("content-bounds-bgl"),
entries: &[
// binding 0: source texture
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: false },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
// binding 1: atomic bounds storage buffer
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
// binding 2: params uniform
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("content-bounds-pipeline-layout"),
bind_group_layouts: &[Some(&bind_group_layout)],
immediate_size: 0,
});
let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some("content-bounds-pipeline"),
layout: Some(&pipeline_layout),
module: &shader,
entry_point: Some("main"),
compilation_options: Default::default(),
cache: None,
});
ContentBoundsPass {
pipeline,
bind_group_layout,
cached: HashMap::new(),
generation: HashMap::new(),
pending: Vec::new(),
}
}
/// Return cached content bounds for a layer, if available.
/// Returns `[x, y, w, h]` or `None` if not yet computed or invalidated.
pub fn get(&self, layer_id: LayerId) -> Option<[u32; 4]> {
self.cached.get(&layer_id).copied()
}
/// True if a bounds computation is in flight for this layer.
pub fn is_pending(&self, layer_id: LayerId) -> bool {
let gen = self.generation.get(&layer_id).copied().unwrap_or(0);
self.pending
.iter()
.any(|p| p.layer_id == layer_id && p.gen == gen)
}
/// Invalidate cached bounds for a specific layer.
pub fn invalidate(&mut self, layer_id: LayerId) {
self.cached.remove(&layer_id);
*self.generation.entry(layer_id).or_insert(0) += 1;
}
/// Invalidate cached bounds for all layers.
pub fn invalidate_all(&mut self) {
self.cached.clear();
for gen in self.generation.values_mut() {
*gen += 1;
}
}
/// Remove all state for a layer (when it's deleted).
pub fn remove_layer(&mut self, layer_id: LayerId) {
self.cached.remove(&layer_id);
self.generation.remove(&layer_id);
}
/// True if any results are pending.
pub fn has_pending(&self) -> bool {
!self.pending.is_empty()
}
/// Dispatch a compute shader to calculate content bounds for a node's
/// texture.
///
/// `r_channel` selects which texel channel is treated as coverage: alpha
/// for RGBA targets, red for R8 targets. Driven by the texture's format,
/// not by node kind.
///
/// Results arrive asynchronously — call [`poll`] each frame.
pub fn request(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
texture_view: &wgpu::TextureView,
width: u32,
height: u32,
r_channel: bool,
layer_id: LayerId,
) {
let gen = self.generation.get(&layer_id).copied().unwrap_or(0);
// Don't queue duplicate requests for the same generation.
if self
.pending
.iter()
.any(|p| p.layer_id == layer_id && p.gen == gen)
{
return;
}
// Storage buffer for atomic results (16 bytes).
let storage_buf = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("content-bounds-storage"),
size: 16,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: true,
});
// Initialize: min = MAX, max = 0.
{
let mut mapping = storage_buf.slice(..).get_mapped_range_mut();
mapping.copy_from_slice(bytemuck::bytes_of(&BOUNDS_INIT));
}
storage_buf.unmap();
// Staging buffer for CPU readback (16 bytes).
let staging_buf = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("content-bounds-staging"),
size: 16,
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
// Params uniform.
let params = Params {
width,
height,
use_r_channel: if r_channel { 1 } else { 0 },
_pad: 0,
};
let param_buf = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("content-bounds-params"),
size: std::mem::size_of::<Params>() as u64,
usage: wgpu::BufferUsages::UNIFORM,
mapped_at_creation: true,
});
{
let mut mapping = param_buf.slice(..).get_mapped_range_mut();
mapping.copy_from_slice(bytemuck::bytes_of(¶ms));
}
param_buf.unmap();
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("content-bounds-bg"),
layout: &self.bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(texture_view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: storage_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: param_buf.as_entire_binding(),
},
],
});
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("content-bounds-compute"),
});
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("content-bounds"),
timestamp_writes: None,
});
pass.set_pipeline(&self.pipeline);
pass.set_bind_group(0, Some(&bind_group), &[]);
let wg_x = width.div_ceil(16);
let wg_y = height.div_ceil(16);
pass.dispatch_workgroups(wg_x, wg_y, 1);
}
// Copy storage → staging for CPU readback.
encoder.copy_buffer_to_buffer(&storage_buf, 0, &staging_buf, 0, 16);
queue.submit([encoder.finish()]);
self.pending.push(PendingBounds {
layer_id,
gen,
staging: staging_buf,
rx: None,
});
}
/// Poll pending computations. Call once per frame.
///
/// Returns the list of layer IDs whose bounds just became available.
pub fn poll(&mut self, device: &wgpu::Device) -> Vec<LayerId> {
// Begin mapping for newly submitted requests.
for p in &mut self.pending {
if p.rx.is_none() {
let slice = p.staging.slice(..);
let (tx, rx) = std::sync::mpsc::sync_channel(1);
slice.map_async(wgpu::MapMode::Read, move |result| {
let _ = tx.send(result);
});
p.rx = Some(rx);
}
}
if !self.pending.is_empty() {
let _ = device.poll(wgpu::PollType::Poll);
}
let mut completed = Vec::new();
let mut i = 0;
while i < self.pending.len() {
let ready = self.pending[i]
.rx
.as_ref()
.and_then(|rx| rx.try_recv().ok());
match ready {
Some(Ok(())) => {
let p = self.pending.swap_remove(i);
let current_gen = self.generation.get(&p.layer_id).copied().unwrap_or(0);
if p.gen == current_gen {
// Read the 4× u32 result.
let slice = p.staging.slice(..);
let mapped = slice.get_mapped_range();
let raw: [u32; 4] = *bytemuck::from_bytes(&mapped[..16]);
drop(mapped);
p.staging.unmap();
let [min_x, min_y, max_x, max_y] = raw;
if min_x <= max_x && min_y <= max_y {
let bounds = [min_x, min_y, max_x - min_x + 1, max_y - min_y + 1];
self.cached.insert(p.layer_id, bounds);
}
// If min_x > max_x: fully transparent — no cached entry.
completed.push(p.layer_id);
} else {
// Stale result — generation changed since dispatch.
p.staging.unmap();
}
}
Some(Err(e)) => {
log::error!("content bounds buffer mapping failed: {e}");
self.pending.swap_remove(i);
}
None => {
i += 1;
}
}
}
completed
}
}