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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Floating-content preview methods on [`Compositor`].
//!
//! The floating preview is a *derived view* of the target node's texture:
//! when a transform is active, the compositor maintains a per-target
//! preview texture rebuilt on every matrix update, holding "what the
//! target would look like if commit ran right now". The render walk's
//! `effective_node_view` and `effective_mask_bind_group` accessors swap
//! the live view / mask bind group for the preview equivalents when the
//! floating target is encountered, so the host's normal blend pass
//! renders the preview without any extra render pass — and isolation,
//! grouping, and other branch-free render concerns compose with it
//! automatically.
//!
//! The compositor exposes primitives (set/clear floating content, update
//! uniforms + preview, commit to live target). The engine drives them by
//! calling `update_floating_preview` after each matrix change and on
//! `setup_transform`.
use crate::gpu::compositor::{BlendUniforms, Compositor};
use crate::layer::LayerId;
impl Compositor {
/// Allocate the per-target preview texture (and, when target is R8, a
/// mask-shape bind group sampling it) plus the canvas-aligned blend
/// uniform buffer the host's blend pass reads when this layer is the
/// floating target.
///
/// Preview is canvas-sized (not live-sized) so a translate that moves
/// content past the live layer's bounding box still has room on the
/// preview to write — clipped at canvas bounds, which is all the
/// viewport renders anyway. Commit's `grow_node_to_fit` separately
/// expands the live layer so off-canvas pixels survive the commit.
fn allocate_preview_resources(
&self,
device: &wgpu::Device,
format: wgpu::TextureFormat,
) -> (
wgpu::Texture,
wgpu::TextureView,
Option<wgpu::BindGroup>,
wgpu::Buffer,
) {
let preview = device.create_texture(&wgpu::TextureDescriptor {
label: Some("floating-preview"),
size: wgpu::Extent3d {
width: self.canvas_width.max(1),
height: self.canvas_height.max(1),
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
// COPY_SRC needed because `render_commit` runs
// `copy_for_compositing` against the render target before its
// shader pass — when the target is the preview texture, that
// copy reads from preview.
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_SRC
| wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
});
let view = preview.create_view(&wgpu::TextureViewDescriptor::default());
let mask_bg = if format == wgpu::TextureFormat::R8Unorm {
Some(device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("floating-preview-mask-bg"),
layout: &self.blend_pipelines.mask_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&view),
}],
}))
} else {
None
};
let blend_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("floating-preview-blend-uniforms"),
size: std::mem::size_of::<BlendUniforms>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
(preview, view, mask_bg, blend_uniform_buf)
}
/// Look up the target's format + dimensions. Falls back to canvas-sized
/// RGBA8 when the node texture hasn't been allocated yet (paste-as-
/// floating creates the layer before its texture).
fn target_format_and_dims(&self, target_layer: LayerId) -> (wgpu::TextureFormat, u32, u32) {
match self.node_textures.get(&target_layer) {
Some(t) => {
let ext = t.layer_extent();
(t.format(), ext.width, ext.height)
}
None => (
wgpu::TextureFormat::Rgba8Unorm,
self.canvas_width,
self.canvas_height,
),
}
}
/// Set up floating content for GPU preview from straight-alpha RGBA
/// pixel data (used by the paste paths). The target's preview texture
/// is allocated alongside.
pub fn set_floating_content(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
rgba_data: &[u8],
_source_origin: (i32, i32),
source_width: u32,
source_height: u32,
target_layer: LayerId,
) {
let (target_format, _tw, _th) = self.target_format_and_dims(target_layer);
let (preview_texture, preview_view, preview_mask_bg, preview_blend_uniform_buf) =
self.allocate_preview_resources(device, target_format);
self.transform_pass.set_floating_content(
device,
queue,
&self.sampler,
rgba_data,
source_width,
source_height,
target_layer,
target_format,
preview_texture,
preview_view,
preview_mask_bg,
preview_blend_uniform_buf,
);
// Seed the preview's blend uniforms from the live layer's cached
// props now that the floating session is active.
self.write_preview_blend_uniforms_if_active(queue, target_layer);
self.mark_dirty();
}
/// Set floating content by copying directly from a node's GPU texture.
/// GPU→GPU copy — no CPU tiles involved. Format dispatch (R8 mask vs RGBA
/// layer) is driven by the texture's own format from the unified node pool.
pub fn set_floating_content_from_gpu(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
source_origin: (i32, i32),
source_width: u32,
source_height: u32,
target_layer: LayerId,
) {
let layer = match self.node_textures.get(&target_layer) {
Some(t) => t,
None => return,
};
let target_format = layer.format();
let (preview_texture, preview_view, preview_mask_bg, preview_blend_uniform_buf) =
self.allocate_preview_resources(device, target_format);
// Re-borrow `layer` after `allocate_preview_resources` — the helper
// doesn't take `&mut self`, but rust-analyzer prefers the explicit
// re-fetch over keeping the borrow live across the helper call.
let layer = self
.node_textures
.get(&target_layer)
.expect("layer present after preview allocation");
self.transform_pass.set_floating_content_from_gpu(
device,
queue,
encoder,
&self.sampler,
layer,
source_origin,
source_width,
source_height,
target_layer,
target_format,
preview_texture,
preview_view,
preview_mask_bg,
preview_blend_uniform_buf,
);
// Seed the preview's blend uniforms from the live layer's cached
// props now that the floating session is active.
self.write_preview_blend_uniforms_if_active(queue, target_layer);
self.mark_dirty();
}
/// Update the floating preview: write the current matrix uniforms, copy
/// live target → preview texture, apply the engine-side `clear_shape`
/// (None for paste mode, `Some` for transform mode), then run the
/// commit shader into the preview. The resulting preview texture is
/// what the host's blend pass reads through `effective_node_view` /
/// `effective_mask_bind_group` for the rest of the frame.
///
/// Called by the engine on `setup_transform` (initial preview) and
/// `update_floating_matrix` (per drag tick).
#[allow(clippy::too_many_arguments)]
pub fn update_floating_preview(
&self,
device: &wgpu::Device,
queue: &wgpu::Queue,
paint_pipelines: &crate::gpu::paint_target::PaintPipelines,
matrix: &crate::gpu::transform::Affine2D,
source_origin: (i32, i32),
source_width: u32,
source_height: u32,
clear_shape: Option<&crate::gpu::transform::ClearShape>,
) {
let Some(state) = self.transform_pass.active.as_ref() else {
return;
};
let live = match self.node_textures.get(&state.target_layer) {
Some(t) => t,
None => return,
};
// The preview is a window-local surface anchored at `canvas_origin`:
// its texel (0,0) maps to plane `canvas_origin` (that is how the host's
// blend composites it — `layer_offset = canvas_origin` in
// `write_preview_blend_uniforms_if_active`). So the transform shader's
// `target_offset` — "where on the canvas the target's pixels live" —
// must be `canvas_origin`, not (0,0); otherwise source content drawn at
// plane `C` lands in texel `C` and then composites at plane
// `canvas_origin + C`, shifting the whole preview by the crop offset.
let origin = self.canvas_origin;
self.transform_pass.update_uniforms(
queue,
matrix,
source_origin,
source_width,
source_height,
(origin.x, origin.y),
self.canvas_width,
self.canvas_height,
self.canvas_width,
self.canvas_height,
);
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("floating-preview-build"),
});
// 0. Reset the whole preview to transparent. The copy below only
// repaints the canvas portion live actually covers, and the
// commit shader discards outside the transformed source bounds —
// so canvas pixels outside live's extent would otherwise retain
// previous-frame transform writes (ghost trails).
crate::gpu::clear_view_transparent(
&mut encoder,
&state.preview_view,
"floating-preview-clear",
);
// 1. Copy live → preview so non-source-rect pixels are preserved.
// Live texture sits at its plane extent; clip the copy to the
// on-canvas (window) portion. The preview is window-local anchored at
// `canvas_origin`, so intersect against the plane canvas window
// (`canvas_rect()`, at `canvas_origin`) and write to the window-local
// destination texel `visible - canvas_origin`. Off-window pixels are
// not in the preview — the viewport never renders them, and commit's
// `grow_node_to_fit` preserves them on the live texture.
let canvas_rect = self.canvas_rect();
let live_canvas_extent = live.canvas_extent();
if let Some(visible) = live_canvas_extent.intersect(canvas_rect) {
// Translate the visible canvas slice into the live texture's
// layer-local coordinate frame for the GPU copy origin.
let visible_layer = live
.canvas_to_layer_rect(visible)
.expect("intersect with live's extent yields a layer-local rect");
let dst_x = (visible.x0() - origin.x) as u32;
let dst_y = (visible.y0() - origin.y) as u32;
encoder.copy_texture_to_texture(
wgpu::TexelCopyTextureInfo {
texture: live.texture(),
mip_level: 0,
origin: wgpu::Origin3d {
x: visible_layer.x0(),
y: visible_layer.y0(),
z: 0,
},
aspect: wgpu::TextureAspect::All,
},
wgpu::TexelCopyTextureInfo {
texture: &state.preview_texture,
mip_level: 0,
origin: wgpu::Origin3d {
x: dst_x,
y: dst_y,
z: 0,
},
aspect: wgpu::TextureAspect::All,
},
wgpu::Extent3d {
width: visible.width,
height: visible.height,
depth_or_array_layers: 1,
},
);
}
// 2. Apply the source-rect clear (transform mode only — paste mode
// leaves the preview as a copy of the live target so the commit
// shader composites over the existing pixels). The preview is
// canvas-aligned, so the paint target reports canvas dims/offset.
if let Some(cs) = clear_shape {
let preview_target = crate::gpu::paint_target::GpuPaintTarget::from_canvas_texture(
&state.preview_texture,
&state.preview_view,
state.target_format,
self.canvas_rect(),
);
match cs {
crate::gpu::transform::ClearShape::Rect(rect) => {
preview_target.clear_rect(&mut encoder, paint_pipelines, queue, *rect);
}
crate::gpu::transform::ClearShape::Selection { mask_bind_group } => {
preview_target.erase_with_selection(
&mut encoder,
paint_pipelines,
queue,
mask_bind_group,
);
}
}
}
// 3. Run the commit shader into the preview at the current matrix.
self.transform_pass.render_commit(
device,
&mut encoder,
&state.preview_texture,
&state.preview_view,
);
queue.submit(std::iter::once(encoder.finish()));
}
/// Render the transform directly into the live target texture.
/// Used by `commit_floating()` after the engine has applied the
/// `clear_shape` to the live target.
pub fn commit_floating_to_texture(
&mut self,
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
queue: &wgpu::Queue,
matrix: &crate::gpu::transform::Affine2D,
source_origin: (i32, i32),
source_width: u32,
source_height: u32,
) {
let Some(state) = self.transform_pass.active.as_ref() else {
return;
};
let live = match self.node_textures.get(&state.target_layer) {
Some(t) => t,
None => return,
};
let live_extent = live.canvas_extent();
self.transform_pass.update_uniforms(
queue,
matrix,
source_origin,
source_width,
source_height,
(live_extent.x0(), live_extent.y0()),
live_extent.width,
live_extent.height,
self.canvas_width,
self.canvas_height,
);
self.transform_pass
.render_commit(device, encoder, live.texture(), live.view());
}
/// Remove floating content GPU state.
pub fn clear_floating_content(&mut self) {
self.transform_pass.clear();
self.mark_dirty();
}
/// Get a reference to the transform source texture and its view.
/// Returns None if no floating content is active.
pub fn transform_source_texture(&self) -> Option<(&wgpu::Texture, &wgpu::TextureView)> {
self.transform_pass
.active
.as_ref()
.map(|s| (&s.source_texture, &s.source_view))
}
/// Write the floating preview's canvas-aligned blend uniforms using the
/// given layer's cached blend props. No-op when there is no active
/// floating, or when the active floating's target is not `layer_id`.
/// Called from both `update_layer_uniforms` (on prop change) and the
/// floating setup paths (to seed the buffer at session start).
pub(super) fn write_preview_blend_uniforms_if_active(
&self,
queue: &wgpu::Queue,
layer_id: LayerId,
) {
let state = match self.transform_pass.active.as_ref() {
Some(s) if s.target_layer == layer_id => s,
_ => return,
};
let cache = match self.layer_cache.get(&layer_id) {
Some(c) => c,
None => return,
};
let uniforms = BlendUniforms {
opacity: cache.opacity,
blend_mode: cache.blend_mode,
isolated: cache.isolated as u32,
_pad1: 0.0,
// The preview texture is window-sized and window-anchored, so it
// composites as a "layer" occupying the canvas window in the plane:
// `layer_offset = canvas_origin`, `layer_size = canvas_size` makes
// the shared-canvas plane round-trip collapse to identity.
layer_offset: [self.canvas_origin.x as f32, self.canvas_origin.y as f32],
layer_size: [self.canvas_width as f32, self.canvas_height as f32],
};
queue.write_buffer(
&state.preview_blend_uniform_buf,
0,
bytemuck::bytes_of(&uniforms),
);
}
}