Skip to main content

repose_render_wgpu/
lib.rs

1use std::borrow::Cow;
2use std::collections::HashMap;
3#[cfg(feature = "winit-surface")]
4use std::sync::Arc;
5#[cfg(feature = "winit-surface")]
6use std::panic::{AssertUnwindSafe, catch_unwind};
7use std::ops::{Deref, DerefMut};
8
9use repose_core::color::{ChromaSiting, ColorInfo, PixelFormat};
10use repose_core::request_frame;
11use repose_core::{
12    Brush, FontStyle, GlyphRasterConfig, RenderBackend, Scene, SceneNode, StrokeCap, Transform,
13};
14use wgpu::Instance;
15
16mod slug;
17
18#[derive(Clone)]
19struct UploadRing {
20    buf: wgpu::Buffer,
21    cap: u64,
22    head: u64,
23}
24
25impl UploadRing {
26    fn new(device: &wgpu::Device, label: &str, cap: u64) -> Self {
27        let buf = device.create_buffer(&wgpu::BufferDescriptor {
28            label: Some(label),
29            size: cap,
30            usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
31            mapped_at_creation: false,
32        });
33        Self { buf, cap, head: 0 }
34    }
35
36    fn reset(&mut self) {
37        self.head = 0;
38    }
39
40    fn grow_to_fit(&mut self, device: &wgpu::Device, needed: u64) {
41        let start = (self.head + 3) & !3;
42        if start + needed <= self.cap {
43            return;
44        }
45        let new_cap = (start + needed).next_power_of_two();
46        self.buf = device.create_buffer(&wgpu::BufferDescriptor {
47            label: Some("upload ring (grown)"),
48            size: new_cap,
49            usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
50            mapped_at_creation: false,
51        });
52        self.cap = new_cap;
53    }
54
55    fn alloc_write(&mut self, queue: &wgpu::Queue, bytes: &[u8]) -> (u64, u64) {
56        let len = bytes.len() as u64;
57        let start = (self.head + 3) & !3; // align to 4
58        let end = start + len;
59        assert!(end <= self.cap, "ring overflow - call grow_to_fit first");
60        queue.write_buffer(&self.buf, start, bytes);
61        self.head = end;
62        (start, len)
63    }
64}
65
66struct InstancedPipe<I: bytemuck::Pod> {
67    ring: UploadRing,
68    _marker: std::marker::PhantomData<I>,
69}
70
71impl<I: bytemuck::Pod> InstancedPipe<I> {
72    fn new(ring: UploadRing) -> Self {
73        Self {
74            ring,
75            _marker: std::marker::PhantomData,
76        }
77    }
78
79    fn upload(
80        &mut self,
81        device: &wgpu::Device,
82        queue: &wgpu::Queue,
83        data: &[I],
84    ) -> Option<(u64, u32)> {
85        if data.is_empty() {
86            return None;
87        }
88        let bytes = bytemuck::cast_slice(data);
89        self.ring.grow_to_fit(device, bytes.len() as u64);
90        let (off, wrote) = self.ring.alloc_write(queue, bytes);
91        debug_assert_eq!(wrote as usize, bytes.len());
92        Some((off, data.len() as u32))
93    }
94
95    fn reset(&mut self) {
96        self.ring.reset();
97    }
98}
99
100#[repr(C)]
101#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
102struct Globals {
103    ndc_to_px: [f32; 2],
104    _pad: [f32; 2],
105}
106
107fn make_globals(target_w: f32, target_h: f32) -> Globals {
108    Globals {
109        ndc_to_px: [target_w * 0.5, target_h * 0.5],
110        _pad: [0.0, 0.0],
111    }
112}
113
114pub struct WgpuSceneRenderer {
115    pub device: wgpu::Device,
116    pub queue: wgpu::Queue,
117    pub output_format: wgpu::TextureFormat,
118    pub output_width: u32,
119    pub output_height: u32,
120
121    // Render pipelines. Two sets: one for the MSAA surface pass, one for
122    // graphics-layer render-to-texture passes (sample_count = 1).
123    surface_pipes: Pipelines,
124    layer_pipes: Pipelines,
125
126    // Instanced draw rings
127    rects: InstancedPipe<RectInstance>,
128    borders: InstancedPipe<BorderInstance>,
129    ellipses: InstancedPipe<EllipseInstance>,
130    ellipse_borders: InstancedPipe<EllipseBorderInstance>,
131    arcs: InstancedPipe<ArcInstance>,
132    glyph_mask: InstancedPipe<GlyphInstance>,
133    glyph_color: InstancedPipe<GlyphInstance>,
134
135    // Image bind layouts and shared sampler
136    image_bind_layout_rgba: wgpu::BindGroupLayout,
137    image_bind_layout_nv12: wgpu::BindGroupLayout,
138    image_sampler: wgpu::Sampler,
139    layer_sampler: wgpu::Sampler,
140    layer_sampler_linear: wgpu::Sampler,
141
142    // Blur composite ring (for graphics-layer drop shadows)
143    blur_ring: UploadRing,
144
145    text_bind_layout: wgpu::BindGroupLayout,
146
147    // Stencil clip ring
148    clip_ring: UploadRing,
149
150    // Tessellated vector glyph pipeline (always enabled)
151    slug_enabled: bool,
152    slug_ring: UploadRing,
153    slug_cache: slug::GlyphSlugCache,
154
155    // Instanced NV12 ring
156    nv12: InstancedPipe<Nv12Instance>,
157
158    msaa_samples: u32,
159
160    // Depth-stencil target
161    depth_stencil_tex: wgpu::Texture,
162    depth_stencil_view: wgpu::TextureView,
163
164    // Optional MSAA color target
165    msaa_tex: Option<wgpu::Texture>,
166    msaa_view: Option<wgpu::TextureView>,
167
168    globals_layout: wgpu::BindGroupLayout,
169    globals_buf: wgpu::Buffer,
170    globals_bind: wgpu::BindGroup,
171
172    // Glyph atlas
173    atlas_mask: AtlasA8,
174    atlas_color: AtlasRGBA,
175
176    // Image management
177    next_image_handle: u64,
178    images: HashMap<u64, ImageTex>,
179
180    // Eviction stats
181    frame_index: u64,
182    image_bytes_total: u64,
183    image_evict_after_frames: u64,
184    image_budget_bytes: u64,
185
186    // Graphics layer pool. Maps `SceneNode::BeginLayer::layer_id` to a
187    // cached offscreen render target.
188    layer_pool: HashMap<u32, LayerTarget>,
189
190    // Linear working-space mode (default off -> fast playback path).
191    // When enabled, the scene is rendered into an Rgba16Float intermediate
192    // texture, then a final full-screen pass applies the display OETF.
193    working_space: bool,
194    ws_tex: Option<wgpu::Texture>,
195    ws_view: Option<wgpu::TextureView>,
196    ws_bind: Option<wgpu::BindGroup>,
197    display_pipeline: Option<wgpu::RenderPipeline>,
198    display_layout: Option<wgpu::BindGroupLayout>,
199}
200
201pub struct WgpuSurfaceBackend {
202    pub surface: Option<wgpu::Surface<'static>>,
203    pub surface_config: Option<wgpu::SurfaceConfiguration>,
204    pub renderer: WgpuSceneRenderer,
205}
206
207impl std::ops::Deref for WgpuSurfaceBackend {
208    type Target = WgpuSceneRenderer;
209    fn deref(&self) -> &Self::Target { &self.renderer }
210}
211impl std::ops::DerefMut for WgpuSurfaceBackend {
212    fn deref_mut(&mut self) -> &mut Self::Target { &mut self.renderer }
213}
214
215#[cfg(feature = "winit-surface")]
216pub type WgpuBackend = WgpuSurfaceBackend;
217
218impl Drop for WgpuSceneRenderer {
219    fn drop(&mut self) {
220        let _ = self.device.poll(wgpu::PollType::wait_indefinitely());
221    }
222}
223
224#[derive(Clone)]
225struct LayerTarget {
226    texture: wgpu::Texture,
227    view: wgpu::TextureView,
228    bind: wgpu::BindGroup,
229    bind_linear: wgpu::BindGroup,
230    depth_stencil_tex: wgpu::Texture,
231    depth_stencil_view: wgpu::TextureView,
232    width: u32,
233    height: u32,
234    rect_px: (f32, f32, f32, f32),
235}
236
237/// Identifies which render target a `Pass` draws into.
238#[derive(Clone, Copy)]
239enum PassTarget {
240    Surface,
241    Layer(u32),
242}
243
244/// A bundle of render pipelines for a single sample-count target. Created
245/// twice: once with `sample_count = msaa_samples` for the surface pass, and
246/// once with `sample_count = 1` for graphics-layer render-to-texture passes
247/// (where MSAA is wasted).
248struct Pipelines {
249    rects: wgpu::RenderPipeline,
250    borders: wgpu::RenderPipeline,
251    ellipses: wgpu::RenderPipeline,
252    ellipse_borders: wgpu::RenderPipeline,
253    arcs: wgpu::RenderPipeline,
254    text_mask: wgpu::RenderPipeline,
255    text_color: wgpu::RenderPipeline,
256    image_rgba: wgpu::RenderPipeline,
257    image_nv12: wgpu::RenderPipeline,
258    blur: wgpu::RenderPipeline,
259    blur_content: wgpu::RenderPipeline,
260    clip_a2c: wgpu::RenderPipeline,
261    clip_bin: wgpu::RenderPipeline,
262    clip_dec: wgpu::RenderPipeline,
263    slug: Option<wgpu::RenderPipeline>,
264}
265
266impl Pipelines {
267    fn create(
268        device: &wgpu::Device,
269        format: wgpu::TextureFormat,
270        sample_count: u32,
271        globals_layout: &wgpu::BindGroupLayout,
272        text_bind_layout: &wgpu::BindGroupLayout,
273        image_bind_layout_nv12: &wgpu::BindGroupLayout,
274        clip_pipeline_layout: &wgpu::PipelineLayout,
275        stencil_for_content: &wgpu::DepthStencilState,
276        stencil_for_clip_inc: &wgpu::DepthStencilState,
277        stencil_for_clip_dec: &wgpu::DepthStencilState,
278        clip_color_target: &wgpu::ColorTargetState,
279        clip_vertex_layout: &wgpu::VertexBufferLayout,
280    ) -> Self {
281        let msaa_state = wgpu::MultisampleState {
282            count: sample_count,
283            mask: !0,
284            alpha_to_coverage_enabled: false,
285        };
286
287        macro_rules! make_content_pipeline {
288            ($name:ident, $shader:literal, $inst_type:ty, $attrs:expr) => {
289                let shader_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
290                    label: Some(concat!($shader, ".wgsl")),
291                    source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(concat!(
292                        "shaders/", $shader, ".wgsl"
293                    )))),
294                });
295                let pipeline_layout =
296                    device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
297                        label: Some(concat!($shader, " pipeline layout")),
298                        bind_group_layouts: &[Some(globals_layout)],
299                        immediate_size: 0,
300                    });
301                let $name = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
302                    label: Some(concat!($shader, " pipeline")),
303                    layout: Some(&pipeline_layout),
304                    vertex: wgpu::VertexState {
305                        module: &shader_module,
306                        entry_point: Some("vs_main"),
307                        buffers: &[Some(wgpu::VertexBufferLayout {
308                            array_stride: std::mem::size_of::<$inst_type>() as u64,
309                            step_mode: wgpu::VertexStepMode::Instance,
310                            attributes: $attrs,
311                        })],
312                        compilation_options: wgpu::PipelineCompilationOptions::default(),
313                    },
314                    fragment: Some(wgpu::FragmentState {
315                        module: &shader_module,
316                        entry_point: Some("fs_main"),
317                        targets: &[Some(wgpu::ColorTargetState {
318                            format,
319                            blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
320                            write_mask: wgpu::ColorWrites::ALL,
321                        })],
322                        compilation_options: wgpu::PipelineCompilationOptions::default(),
323                    }),
324                    primitive: wgpu::PrimitiveState::default(),
325                    depth_stencil: Some(stencil_for_content.clone()),
326                    multisample: msaa_state,
327                    multiview_mask: None,
328                    cache: None,
329                });
330            };
331        }
332
333        let rect_attrs: &[wgpu::VertexAttribute] = &[
334            wgpu::VertexAttribute {
335                shader_location: 0,
336                offset: 0,
337                format: wgpu::VertexFormat::Float32x4,
338            },
339            wgpu::VertexAttribute {
340                shader_location: 1,
341                offset: 16,
342                format: wgpu::VertexFormat::Float32x4,
343            },
344            wgpu::VertexAttribute {
345                shader_location: 2,
346                offset: 32,
347                format: wgpu::VertexFormat::Uint32,
348            },
349            wgpu::VertexAttribute {
350                shader_location: 3,
351                offset: 48,
352                format: wgpu::VertexFormat::Float32x4,
353            },
354            wgpu::VertexAttribute {
355                shader_location: 4,
356                offset: 64,
357                format: wgpu::VertexFormat::Float32x4,
358            },
359            wgpu::VertexAttribute {
360                shader_location: 5,
361                offset: 80,
362                format: wgpu::VertexFormat::Float32x2,
363            },
364            wgpu::VertexAttribute {
365                shader_location: 6,
366                offset: 88,
367                format: wgpu::VertexFormat::Float32x2,
368            },
369            wgpu::VertexAttribute {
370                shader_location: 7,
371                offset: 96,
372                format: wgpu::VertexFormat::Float32x2,
373            },
374        ];
375        let border_attrs: &[wgpu::VertexAttribute] = &[
376            wgpu::VertexAttribute {
377                shader_location: 0,
378                offset: 0,
379                format: wgpu::VertexFormat::Float32x4,
380            },
381            wgpu::VertexAttribute {
382                shader_location: 1,
383                offset: 16,
384                format: wgpu::VertexFormat::Float32x4,
385            },
386            wgpu::VertexAttribute {
387                shader_location: 2,
388                offset: 32,
389                format: wgpu::VertexFormat::Float32,
390            },
391            wgpu::VertexAttribute {
392                shader_location: 3,
393                offset: 36,
394                format: wgpu::VertexFormat::Float32x4,
395            },
396            wgpu::VertexAttribute {
397                shader_location: 4,
398                offset: 52,
399                format: wgpu::VertexFormat::Float32x2,
400            },
401        ];
402        let ellipse_attrs: &[wgpu::VertexAttribute] = &[
403            wgpu::VertexAttribute {
404                shader_location: 0,
405                offset: 0,
406                format: wgpu::VertexFormat::Float32x4,
407            },
408            wgpu::VertexAttribute {
409                shader_location: 1,
410                offset: 16,
411                format: wgpu::VertexFormat::Float32x4,
412            },
413            wgpu::VertexAttribute {
414                shader_location: 2,
415                offset: 32,
416                format: wgpu::VertexFormat::Float32x2,
417            },
418        ];
419        let ellipse_border_attrs: &[wgpu::VertexAttribute] = &[
420            wgpu::VertexAttribute {
421                shader_location: 0,
422                offset: 0,
423                format: wgpu::VertexFormat::Float32x4,
424            },
425            wgpu::VertexAttribute {
426                shader_location: 1,
427                offset: 16,
428                format: wgpu::VertexFormat::Float32,
429            },
430            wgpu::VertexAttribute {
431                shader_location: 2,
432                offset: 20,
433                format: wgpu::VertexFormat::Float32,
434            },
435            wgpu::VertexAttribute {
436                shader_location: 3,
437                offset: 24,
438                format: wgpu::VertexFormat::Float32x4,
439            },
440            wgpu::VertexAttribute {
441                shader_location: 4,
442                offset: 40,
443                format: wgpu::VertexFormat::Float32x2,
444            },
445        ];
446
447        make_content_pipeline!(rects, "rect", RectInstance, rect_attrs);
448        make_content_pipeline!(borders, "border", BorderInstance, border_attrs);
449        make_content_pipeline!(ellipses, "ellipse", EllipseInstance, ellipse_attrs);
450        make_content_pipeline!(
451            ellipse_borders,
452            "ellipse_border",
453            EllipseBorderInstance,
454            ellipse_border_attrs
455        );
456
457        let arc_attrs: &[wgpu::VertexAttribute] = &[
458            wgpu::VertexAttribute {
459                shader_location: 0,
460                offset: 0,
461                format: wgpu::VertexFormat::Float32x4,
462            },
463            wgpu::VertexAttribute {
464                shader_location: 1,
465                offset: 16,
466                format: wgpu::VertexFormat::Float32,
467            },
468            wgpu::VertexAttribute {
469                shader_location: 2,
470                offset: 20,
471                format: wgpu::VertexFormat::Float32,
472            },
473            wgpu::VertexAttribute {
474                shader_location: 3,
475                offset: 24,
476                format: wgpu::VertexFormat::Float32,
477            },
478            wgpu::VertexAttribute {
479                shader_location: 4,
480                offset: 28,
481                format: wgpu::VertexFormat::Float32,
482            },
483            wgpu::VertexAttribute {
484                shader_location: 5,
485                offset: 32,
486                format: wgpu::VertexFormat::Float32x4,
487            },
488            wgpu::VertexAttribute {
489                shader_location: 6,
490                offset: 48,
491                format: wgpu::VertexFormat::Float32x2,
492            },
493            wgpu::VertexAttribute {
494                shader_location: 7,
495                offset: 56,
496                format: wgpu::VertexFormat::Float32,
497            },
498        ];
499
500        make_content_pipeline!(arcs, "arc", ArcInstance, arc_attrs);
501
502        // Text (mask)
503        let text_mask_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
504            label: Some("text.wgsl"),
505            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shaders/text.wgsl"))),
506        });
507        // Text (color)
508        let text_color_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
509            label: Some("text_color.wgsl"),
510            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(
511                "shaders/text_color.wgsl"
512            ))),
513        });
514        let text_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
515            label: Some("text pipeline layout"),
516            bind_group_layouts: &[Some(globals_layout), Some(text_bind_layout)],
517            immediate_size: 0,
518        });
519        let glyph_vertex = wgpu::VertexBufferLayout {
520            array_stride: std::mem::size_of::<GlyphInstance>() as u64,
521            step_mode: wgpu::VertexStepMode::Instance,
522            attributes: &[
523                wgpu::VertexAttribute {
524                    shader_location: 0,
525                    offset: 0,
526                    format: wgpu::VertexFormat::Float32x4,
527                },
528                wgpu::VertexAttribute {
529                    shader_location: 1,
530                    offset: 16,
531                    format: wgpu::VertexFormat::Float32x4,
532                },
533                wgpu::VertexAttribute {
534                    shader_location: 2,
535                    offset: 32,
536                    format: wgpu::VertexFormat::Float32x4,
537                },
538                wgpu::VertexAttribute {
539                    shader_location: 3,
540                    offset: 48,
541                    format: wgpu::VertexFormat::Float32x2,
542                },
543            ],
544        };
545        let text_mask = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
546            label: Some("text pipeline (mask)"),
547            layout: Some(&text_pipeline_layout),
548            vertex: wgpu::VertexState {
549                module: &text_mask_shader,
550                entry_point: Some("vs_main"),
551                buffers: &[Some(glyph_vertex.clone())],
552                compilation_options: wgpu::PipelineCompilationOptions::default(),
553            },
554            fragment: Some(wgpu::FragmentState {
555                module: &text_mask_shader,
556                entry_point: Some("fs_main"),
557                targets: &[Some(wgpu::ColorTargetState {
558                    format,
559                    blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
560                    write_mask: wgpu::ColorWrites::ALL,
561                })],
562                compilation_options: wgpu::PipelineCompilationOptions::default(),
563            }),
564            primitive: wgpu::PrimitiveState::default(),
565            depth_stencil: Some(stencil_for_content.clone()),
566            multisample: msaa_state,
567            multiview_mask: None,
568            cache: None,
569        });
570        let text_color = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
571            label: Some("text pipeline (color)"),
572            layout: Some(&text_pipeline_layout),
573            vertex: wgpu::VertexState {
574                module: &text_color_shader,
575                entry_point: Some("vs_main"),
576                buffers: &[Some(glyph_vertex)],
577                compilation_options: wgpu::PipelineCompilationOptions::default(),
578            },
579            fragment: Some(wgpu::FragmentState {
580                module: &text_color_shader,
581                entry_point: Some("fs_main"),
582                targets: &[Some(wgpu::ColorTargetState {
583                    format,
584                    blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
585                    write_mask: wgpu::ColorWrites::ALL,
586                })],
587                compilation_options: wgpu::PipelineCompilationOptions::default(),
588            }),
589            primitive: wgpu::PrimitiveState::default(),
590            depth_stencil: Some(stencil_for_content.clone()),
591            multisample: msaa_state,
592            multiview_mask: None,
593            cache: None,
594        });
595        // image_rgba reuses the text color pipeline (same vertex/bindings).
596        let image_rgba = text_color.clone();
597
598        // Blur composite pipeline (graphics-layer drop shadow)
599        let blur_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
600            label: Some("blur_shadow.wgsl"),
601            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(
602                "shaders/blur_shadow.wgsl"
603            ))),
604        });
605        let blur_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
606            label: Some("blur pipeline layout"),
607            bind_group_layouts: &[Some(globals_layout), Some(text_bind_layout)],
608            immediate_size: 0,
609        });
610        let blur = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
611            label: Some("blur pipeline"),
612            layout: Some(&blur_pipeline_layout),
613            vertex: wgpu::VertexState {
614                module: &blur_shader,
615                entry_point: Some("vs_main"),
616                buffers: &[Some(wgpu::VertexBufferLayout {
617                    array_stride: std::mem::size_of::<BlurInstance>() as u64,
618                    step_mode: wgpu::VertexStepMode::Instance,
619                    attributes: &[
620                        wgpu::VertexAttribute {
621                            shader_location: 0,
622                            offset: 0,
623                            format: wgpu::VertexFormat::Float32x4,
624                        },
625                        wgpu::VertexAttribute {
626                            shader_location: 1,
627                            offset: 16,
628                            format: wgpu::VertexFormat::Float32x4,
629                        },
630                        wgpu::VertexAttribute {
631                            shader_location: 2,
632                            offset: 32,
633                            format: wgpu::VertexFormat::Float32x4,
634                        },
635                        wgpu::VertexAttribute {
636                            shader_location: 3,
637                            offset: 48,
638                            format: wgpu::VertexFormat::Float32x2,
639                        },
640                        wgpu::VertexAttribute {
641                            shader_location: 4,
642                            offset: 56,
643                            format: wgpu::VertexFormat::Float32x2,
644                        },
645                    ],
646                })],
647                compilation_options: wgpu::PipelineCompilationOptions::default(),
648            },
649            fragment: Some(wgpu::FragmentState {
650                module: &blur_shader,
651                entry_point: Some("fs_main"),
652                targets: &[Some(wgpu::ColorTargetState {
653                    format,
654                    blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
655                    write_mask: wgpu::ColorWrites::ALL,
656                })],
657                compilation_options: wgpu::PipelineCompilationOptions::default(),
658            }),
659            primitive: wgpu::PrimitiveState::default(),
660            depth_stencil: Some(stencil_for_content.clone()),
661            multisample: msaa_state,
662            multiview_mask: None,
663            cache: None,
664        });
665
666        // Content blur pipeline (full RGBA gaussian blur)
667        let blur_content_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
668            label: Some("blur_content.wgsl"),
669            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(
670                "shaders/blur_content.wgsl"
671            ))),
672        });
673        let blur_content = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
674            label: Some("blur content pipeline"),
675            layout: Some(&blur_pipeline_layout),
676            vertex: wgpu::VertexState {
677                module: &blur_content_shader,
678                entry_point: Some("vs_main"),
679                buffers: &[Some(wgpu::VertexBufferLayout {
680                    array_stride: std::mem::size_of::<BlurInstance>() as u64,
681                    step_mode: wgpu::VertexStepMode::Instance,
682                    attributes: &[
683                        wgpu::VertexAttribute {
684                            shader_location: 0,
685                            offset: 0,
686                            format: wgpu::VertexFormat::Float32x4,
687                        },
688                        wgpu::VertexAttribute {
689                            shader_location: 1,
690                            offset: 16,
691                            format: wgpu::VertexFormat::Float32x4,
692                        },
693                        wgpu::VertexAttribute {
694                            shader_location: 2,
695                            offset: 32,
696                            format: wgpu::VertexFormat::Float32x4,
697                        },
698                        wgpu::VertexAttribute {
699                            shader_location: 3,
700                            offset: 48,
701                            format: wgpu::VertexFormat::Float32x2,
702                        },
703                        wgpu::VertexAttribute {
704                            shader_location: 4,
705                            offset: 56,
706                            format: wgpu::VertexFormat::Float32x2,
707                        },
708                    ],
709                })],
710                compilation_options: wgpu::PipelineCompilationOptions::default(),
711            },
712            fragment: Some(wgpu::FragmentState {
713                module: &blur_content_shader,
714                entry_point: Some("fs_main"),
715                targets: &[Some(wgpu::ColorTargetState {
716                    format,
717                    blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
718                    write_mask: wgpu::ColorWrites::ALL,
719                })],
720                compilation_options: wgpu::PipelineCompilationOptions::default(),
721            }),
722            primitive: wgpu::PrimitiveState::default(),
723            depth_stencil: Some(stencil_for_content.clone()),
724            multisample: msaa_state,
725            multiview_mask: None,
726            cache: None,
727        });
728
729        // NV12 Image Pipeline
730        let image_nv12_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
731            label: Some("image_nv12.wgsl"),
732            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(
733                "shaders/image_nv12.wgsl"
734            ))),
735        });
736        let image_nv12_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
737            label: Some("image nv12 pipeline layout"),
738            bind_group_layouts: &[Some(globals_layout), Some(image_bind_layout_nv12)],
739            immediate_size: 0,
740        });
741        let image_nv12 = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
742            label: Some("image nv12 pipeline"),
743            layout: Some(&image_nv12_layout),
744            vertex: wgpu::VertexState {
745                module: &image_nv12_shader,
746                entry_point: Some("vs_main"),
747                buffers: &[Some(wgpu::VertexBufferLayout {
748                    array_stride: std::mem::size_of::<Nv12Instance>() as u64,
749                    step_mode: wgpu::VertexStepMode::Instance,
750                    attributes: &[
751                        wgpu::VertexAttribute {
752                            shader_location: 0,
753                            offset: 0,
754                            format: wgpu::VertexFormat::Float32x4,
755                        },
756                        wgpu::VertexAttribute {
757                            shader_location: 1,
758                            offset: 16,
759                            format: wgpu::VertexFormat::Float32x4,
760                        },
761                        wgpu::VertexAttribute {
762                            shader_location: 2,
763                            offset: 32,
764                            format: wgpu::VertexFormat::Float32x4,
765                        },
766                        wgpu::VertexAttribute {
767                            shader_location: 3,
768                            offset: 48,
769                            format: wgpu::VertexFormat::Float32,
770                        },
771                        wgpu::VertexAttribute {
772                            shader_location: 4,
773                            offset: 52,
774                            format: wgpu::VertexFormat::Float32x2,
775                        },
776                    ],
777                })],
778                compilation_options: wgpu::PipelineCompilationOptions::default(),
779            },
780            fragment: Some(wgpu::FragmentState {
781                module: &image_nv12_shader,
782                entry_point: Some("fs_main"),
783                targets: &[Some(wgpu::ColorTargetState {
784                    format,
785                    blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
786                    write_mask: wgpu::ColorWrites::ALL,
787                })],
788                compilation_options: wgpu::PipelineCompilationOptions::default(),
789            }),
790            primitive: wgpu::PrimitiveState::default(),
791            depth_stencil: Some(stencil_for_content.clone()),
792            multisample: msaa_state,
793            multiview_mask: None,
794            cache: None,
795        });
796
797        // Clipping
798        let clip_shader_a2c = device.create_shader_module(wgpu::ShaderModuleDescriptor {
799            label: Some("clip_round_rect_a2c.wgsl"),
800            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(
801                "shaders/clip_round_rect_a2c.wgsl"
802            ))),
803        });
804        let clip_shader_bin = device.create_shader_module(wgpu::ShaderModuleDescriptor {
805            label: Some("clip_round_rect_bin.wgsl"),
806            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(
807                "shaders/clip_round_rect_bin.wgsl"
808            ))),
809        });
810        let clip_a2c = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
811            label: Some("clip pipeline (a2c)"),
812            layout: Some(clip_pipeline_layout),
813            vertex: wgpu::VertexState {
814                module: &clip_shader_a2c,
815                entry_point: Some("vs_main"),
816                buffers: &[Some(clip_vertex_layout.clone())],
817                compilation_options: wgpu::PipelineCompilationOptions::default(),
818            },
819            fragment: Some(wgpu::FragmentState {
820                module: &clip_shader_a2c,
821                entry_point: Some("fs_main"),
822                targets: &[Some(clip_color_target.clone())],
823                compilation_options: wgpu::PipelineCompilationOptions::default(),
824            }),
825            primitive: wgpu::PrimitiveState::default(),
826            depth_stencil: Some(stencil_for_clip_inc.clone()),
827            multisample: wgpu::MultisampleState {
828                count: sample_count,
829                mask: !0,
830                alpha_to_coverage_enabled: sample_count > 1,
831            },
832            multiview_mask: None,
833            cache: None,
834        });
835        let clip_bin = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
836            label: Some("clip pipeline (bin)"),
837            layout: Some(clip_pipeline_layout),
838            vertex: wgpu::VertexState {
839                module: &clip_shader_bin,
840                entry_point: Some("vs_main"),
841                buffers: &[Some(clip_vertex_layout.clone())],
842                compilation_options: wgpu::PipelineCompilationOptions::default(),
843            },
844            fragment: Some(wgpu::FragmentState {
845                module: &clip_shader_bin,
846                entry_point: Some("fs_main"),
847                targets: &[Some(clip_color_target.clone())],
848                compilation_options: wgpu::PipelineCompilationOptions::default(),
849            }),
850            primitive: wgpu::PrimitiveState::default(),
851            depth_stencil: Some(stencil_for_clip_inc.clone()),
852            multisample: wgpu::MultisampleState {
853                count: sample_count,
854                mask: !0,
855                alpha_to_coverage_enabled: false,
856            },
857            multiview_mask: None,
858            cache: None,
859        });
860        let clip_dec = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
861            label: Some("clip pipeline (dec)"),
862            layout: Some(clip_pipeline_layout),
863            vertex: wgpu::VertexState {
864                module: &clip_shader_bin,
865                entry_point: Some("vs_main"),
866                buffers: &[Some(clip_vertex_layout.clone())],
867                compilation_options: wgpu::PipelineCompilationOptions::default(),
868            },
869            fragment: Some(wgpu::FragmentState {
870                module: &clip_shader_bin,
871                entry_point: Some("fs_main"),
872                targets: &[Some(clip_color_target.clone())],
873                compilation_options: wgpu::PipelineCompilationOptions::default(),
874            }),
875            primitive: wgpu::PrimitiveState::default(),
876            depth_stencil: Some(stencil_for_clip_dec.clone()),
877            multisample: wgpu::MultisampleState {
878                count: sample_count,
879                mask: !0,
880                alpha_to_coverage_enabled: false,
881            },
882            multiview_mask: None,
883            cache: None,
884        });
885
886        let slug = Some(slug::create_pipeline(
887            device,
888            format,
889            sample_count,
890            stencil_for_content,
891        ));
892
893        Self {
894            rects,
895            borders,
896            ellipses,
897            ellipse_borders,
898            arcs,
899            text_mask,
900            text_color,
901            image_rgba,
902            image_nv12,
903            blur,
904            blur_content,
905            clip_a2c,
906            clip_bin,
907            clip_dec,
908            slug,
909        }
910    }
911}
912
913/// A segment of the frame that draws into a single render target.
914struct Pass {
915    target: PassTarget,
916    /// The initial scissor to apply to the rpass when it is opened.
917    initial_scissor: (u32, u32, u32, u32),
918    /// `None` means `LoadOp::Load` (resume existing content);
919    /// `Some(c)` means `LoadOp::Clear(c)`.
920    clear_color: Option<[f32; 4]>,
921    cmds: Vec<Cmd>,
922}
923
924#[allow(non_snake_case)]
925enum Cmd {
926    ClipPush {
927        off: u64,
928        cnt: u32,
929        scissor: (u32, u32, u32, u32),
930        difference: bool,
931        rounded: bool,
932    },
933    ClipPop {
934        scissor: (u32, u32, u32, u32),
935    },
936    Rect {
937        off: u64,
938        cnt: u32,
939    },
940    Border {
941        off: u64,
942        cnt: u32,
943    },
944    Ellipse {
945        off: u64,
946        cnt: u32,
947    },
948    EllipseBorder {
949        off: u64,
950        cnt: u32,
951    },
952    Arc {
953        off: u64,
954        cnt: u32,
955    },
956    GlyphsMask {
957        off: u64,
958        cnt: u32,
959    },
960    GlyphsColor {
961        off: u64,
962        cnt: u32,
963    },
964    GlyphsVector {
965        off: u64,
966        cnt: u32,
967    },
968    ImageRgba {
969        off: u64,
970        cnt: u32,
971        handle: u64,
972    },
973    ImageNv12 {
974        off: u64,
975        cnt: u32,
976        handle: u64,
977    },
978    PushTransform(Transform),
979    PopTransform,
980    /// Composite a previously-rendered graphics layer back into the
981    /// current target as a textured quad. The quad's vertex buffer
982    /// lives in `self.glyph_color.ring` (a `GlyphInstance`).
983    CompositeLayer {
984        off: u64,
985        cnt: u32,
986        layer_id: u32,
987        alpha: f32,
988    },
989    /// Composite a blurred drop shadow of a previously-rendered graphics
990    /// layer. The quad's vertex buffer lives in `self.blur_ring` (a
991    /// `BlurInstance`).
992    CompositeShadow {
993        off: u64,
994        cnt: u32,
995        layer_id: u32,
996    },
997    /// Apply gaussian blur to a layer and composite the blurred result.
998    /// Uses the `blur_content` pipeline (full RGBA blur).
999    CompositeBlur {
1000        off: u64,
1001        cnt: u32,
1002        layer_id: u32,
1003    },
1004}
1005
1006enum ImageTex {
1007    Rgba {
1008        tex: wgpu::Texture,
1009        view: wgpu::TextureView,
1010        bind: wgpu::BindGroup,
1011        w: u32,
1012        h: u32,
1013        format: wgpu::TextureFormat,
1014        last_used_frame: u64,
1015        bytes: u64,
1016    },
1017    Nv12 {
1018        tex_y: wgpu::Texture,
1019        view_y: wgpu::TextureView,
1020        tex_uv: wgpu::Texture,
1021        view_uv: wgpu::TextureView,
1022        bind: wgpu::BindGroup,
1023        yuv_buf: wgpu::Buffer,
1024        w: u32,
1025        h: u32,
1026        color_info: ColorInfo,
1027        last_used_frame: u64,
1028        bytes: u64,
1029    },
1030}
1031
1032struct AtlasA8 {
1033    tex: wgpu::Texture,
1034    view: wgpu::TextureView,
1035    sampler: wgpu::Sampler,
1036    size: u32,
1037    next_x: u32,
1038    next_y: u32,
1039    row_h: u32,
1040    map: HashMap<(repose_text::GlyphKey, u32), GlyphInfo>,
1041}
1042
1043struct AtlasRGBA {
1044    tex: wgpu::Texture,
1045    view: wgpu::TextureView,
1046    sampler: wgpu::Sampler,
1047    size: u32,
1048    next_x: u32,
1049    next_y: u32,
1050    row_h: u32,
1051    map: HashMap<(repose_text::GlyphKey, u32), GlyphInfo>,
1052}
1053
1054#[derive(Clone, Copy)]
1055struct GlyphInfo {
1056    u0: f32,
1057    v0: f32,
1058    u1: f32,
1059    v1: f32,
1060    w: f32,
1061    h: f32,
1062    bearing_x: f32,
1063    bearing_y: f32,
1064    advance: f32,
1065}
1066
1067#[repr(C)]
1068#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
1069struct RectInstance {
1070    xywh: [f32; 4],
1071    radii: [f32; 4],
1072    brush_type: u32,
1073    _pad: [f32; 3],
1074    color0: [f32; 4],
1075    color1: [f32; 4],
1076    grad_start: [f32; 2],
1077    grad_end: [f32; 2],
1078    sin_cos: [f32; 2],
1079}
1080
1081#[repr(C)]
1082#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
1083struct BorderInstance {
1084    xywh: [f32; 4],
1085    radii: [f32; 4],
1086    stroke: f32,
1087    color: [f32; 4],
1088    sin_cos: [f32; 2],
1089}
1090
1091#[repr(C)]
1092#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
1093struct EllipseInstance {
1094    xywh: [f32; 4],
1095    color: [f32; 4],
1096    sin_cos: [f32; 2],
1097}
1098
1099#[repr(C)]
1100#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
1101struct EllipseBorderInstance {
1102    xywh: [f32; 4],
1103    stroke: f32,
1104    pad: f32,
1105    color: [f32; 4],
1106    sin_cos: [f32; 2],
1107}
1108
1109#[repr(C)]
1110#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
1111struct ArcInstance {
1112    xywh: [f32; 4],
1113    start_angle: f32,
1114    sweep_angle: f32,
1115    stroke: f32,
1116    pad: f32,
1117    color: [f32; 4],
1118    sin_cos: [f32; 2],
1119    cap: f32, // 0=Butt, 1=Round, 2=Square
1120}
1121
1122#[repr(C)]
1123#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
1124struct GlyphInstance {
1125    xywh: [f32; 4],
1126    uv: [f32; 4],
1127    color: [f32; 4],
1128    sin_cos: [f32; 2],
1129}
1130
1131#[repr(C)]
1132#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
1133struct BlurInstance {
1134    xywh: [f32; 4],
1135    uv: [f32; 4],
1136    color: [f32; 4],
1137    blur_uv: [f32; 2],
1138    sin_cos: [f32; 2],
1139}
1140
1141/// CPU-computed Y′CbCr → R′G′B′ transform uploaded as a uniform buffer.
1142/// Layout matches the WGSL `YuvTransform` struct (4 × vec4<f32>).
1143#[repr(C)]
1144#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
1145struct YuvTransformRaw {
1146    row0: [f32; 4],
1147    row1: [f32; 4],
1148    row2: [f32; 4],
1149    b: [f32; 4],
1150}
1151
1152#[repr(C)]
1153#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
1154struct Nv12Instance {
1155    xywh: [f32; 4],
1156    uv: [f32; 4],
1157    color: [f32; 4], // tint
1158    uv_x_offset: f32,
1159    sin_cos: [f32; 2],
1160    _pad: [f32; 1],
1161}
1162
1163#[repr(C)]
1164#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
1165struct ClipInstance {
1166    xywh: [f32; 4],
1167    radii: [f32; 4],
1168    sin_cos: [f32; 2],
1169}
1170
1171fn swash_to_a8_coverage(content: repose_text::SwashContent, data: &[u8]) -> Option<Vec<u8>> {
1172    match content {
1173        repose_text::SwashContent::Mask => Some(data.to_vec()),
1174        repose_text::SwashContent::SubpixelMask => {
1175            let mut out = Vec::with_capacity(data.len() / 4);
1176            for px in data.chunks_exact(4) {
1177                let r = px[0];
1178                let g = px[1];
1179                let b = px[2];
1180                out.push(r.max(g).max(b));
1181            }
1182            Some(out)
1183        }
1184        repose_text::SwashContent::Color => None,
1185    }
1186}
1187
1188impl WgpuSceneRenderer {
1189    pub fn from_device(
1190        device: wgpu::Device,
1191        queue: wgpu::Queue,
1192        output_format: wgpu::TextureFormat,
1193        msaa_samples: u32,
1194    ) -> Self {
1195        let globals_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
1196            label: Some("globals layout"),
1197            entries: &[wgpu::BindGroupLayoutEntry {
1198                binding: 0,
1199                visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
1200                ty: wgpu::BindingType::Buffer {
1201                    ty: wgpu::BufferBindingType::Uniform,
1202                    has_dynamic_offset: false,
1203                    min_binding_size: None,
1204                },
1205                count: None,
1206            }],
1207        });
1208
1209        let globals_buf = device.create_buffer(&wgpu::BufferDescriptor {
1210            label: Some("globals buf"),
1211            size: std::mem::size_of::<Globals>() as u64,
1212            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
1213            mapped_at_creation: false,
1214        });
1215
1216        let globals_bind = device.create_bind_group(&wgpu::BindGroupDescriptor {
1217            label: Some("globals bind"),
1218            layout: &globals_layout,
1219            entries: &[wgpu::BindGroupEntry {
1220                binding: 0,
1221                resource: globals_buf.as_entire_binding(),
1222            }],
1223        });
1224
1225
1226        let ds_format = wgpu::TextureFormat::Depth24PlusStencil8;
1227
1228        let stencil_for_content = wgpu::DepthStencilState {
1229            format: ds_format,
1230            depth_write_enabled: Some(false),
1231            depth_compare: Some(wgpu::CompareFunction::Always),
1232            stencil: wgpu::StencilState {
1233                front: wgpu::StencilFaceState {
1234                    compare: wgpu::CompareFunction::LessEqual,
1235                    fail_op: wgpu::StencilOperation::Keep,
1236                    depth_fail_op: wgpu::StencilOperation::Keep,
1237                    pass_op: wgpu::StencilOperation::Keep,
1238                },
1239                back: wgpu::StencilFaceState {
1240                    compare: wgpu::CompareFunction::LessEqual,
1241                    fail_op: wgpu::StencilOperation::Keep,
1242                    depth_fail_op: wgpu::StencilOperation::Keep,
1243                    pass_op: wgpu::StencilOperation::Keep,
1244                },
1245                read_mask: 0xFF,
1246                write_mask: 0x00,
1247            },
1248            bias: wgpu::DepthBiasState::default(),
1249        };
1250
1251        let stencil_for_clip_inc = wgpu::DepthStencilState {
1252            format: ds_format,
1253            depth_write_enabled: Some(false),
1254            depth_compare: Some(wgpu::CompareFunction::Always),
1255            stencil: wgpu::StencilState {
1256                front: wgpu::StencilFaceState {
1257                    compare: wgpu::CompareFunction::Equal,
1258                    fail_op: wgpu::StencilOperation::Keep,
1259                    depth_fail_op: wgpu::StencilOperation::Keep,
1260                    pass_op: wgpu::StencilOperation::IncrementClamp,
1261                },
1262                back: wgpu::StencilFaceState {
1263                    compare: wgpu::CompareFunction::Equal,
1264                    fail_op: wgpu::StencilOperation::Keep,
1265                    depth_fail_op: wgpu::StencilOperation::Keep,
1266                    pass_op: wgpu::StencilOperation::IncrementClamp,
1267                },
1268                read_mask: 0xFF,
1269                write_mask: 0xFF,
1270            },
1271            bias: wgpu::DepthBiasState::default(),
1272        };
1273
1274        let stencil_for_clip_dec = wgpu::DepthStencilState {
1275            format: ds_format,
1276            depth_write_enabled: Some(false),
1277            depth_compare: Some(wgpu::CompareFunction::Always),
1278            stencil: wgpu::StencilState {
1279                front: wgpu::StencilFaceState {
1280                    compare: wgpu::CompareFunction::Equal,
1281                    fail_op: wgpu::StencilOperation::Keep,
1282                    depth_fail_op: wgpu::StencilOperation::Keep,
1283                    pass_op: wgpu::StencilOperation::DecrementClamp,
1284                },
1285                back: wgpu::StencilFaceState {
1286                    compare: wgpu::CompareFunction::Equal,
1287                    fail_op: wgpu::StencilOperation::Keep,
1288                    depth_fail_op: wgpu::StencilOperation::Keep,
1289                    pass_op: wgpu::StencilOperation::DecrementClamp,
1290                },
1291                read_mask: 0xFF,
1292                write_mask: 0xFF,
1293            },
1294            bias: wgpu::DepthBiasState::default(),
1295        };
1296
1297        let _multisample_state = wgpu::MultisampleState {
1298            count: msaa_samples,
1299            mask: !0,
1300            alpha_to_coverage_enabled: false,
1301        };
1302
1303        // PIPELINES
1304
1305        // Single shared sampler for images/text
1306        let image_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
1307            label: Some("image/text sampler"),
1308            address_mode_u: wgpu::AddressMode::ClampToEdge,
1309            address_mode_v: wgpu::AddressMode::ClampToEdge,
1310            mag_filter: wgpu::FilterMode::Linear,
1311            min_filter: wgpu::FilterMode::Linear,
1312            mipmap_filter: wgpu::MipmapFilterMode::Linear,
1313            ..Default::default()
1314        });
1315
1316        // linear filtering only blurs them; nearest keeps the blit crisp.
1317        let layer_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
1318            label: Some("layer nearest sampler"),
1319            address_mode_u: wgpu::AddressMode::ClampToEdge,
1320            address_mode_v: wgpu::AddressMode::ClampToEdge,
1321            mag_filter: wgpu::FilterMode::Nearest,
1322            min_filter: wgpu::FilterMode::Nearest,
1323            mipmap_filter: wgpu::MipmapFilterMode::Nearest,
1324            ..Default::default()
1325        });
1326
1327        // Linear taps for Gaussian blur/shadow passes; nearest is kept for
1328        // the sharp 1:1 layer composite.
1329        let layer_sampler_linear = device.create_sampler(&wgpu::SamplerDescriptor {
1330            label: Some("layer linear sampler"),
1331            address_mode_u: wgpu::AddressMode::ClampToEdge,
1332            address_mode_v: wgpu::AddressMode::ClampToEdge,
1333            mag_filter: wgpu::FilterMode::Linear,
1334            min_filter: wgpu::FilterMode::Linear,
1335            mipmap_filter: wgpu::MipmapFilterMode::Linear,
1336            ..Default::default()
1337        });
1338
1339        // Layout for Text / RGBA Images (Texture + Sampler)
1340        let text_bind_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
1341            label: Some("text/rgba bind layout"),
1342            entries: &[
1343                wgpu::BindGroupLayoutEntry {
1344                    binding: 0,
1345                    visibility: wgpu::ShaderStages::FRAGMENT,
1346                    ty: wgpu::BindingType::Texture {
1347                        multisampled: false,
1348                        view_dimension: wgpu::TextureViewDimension::D2,
1349                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
1350                    },
1351                    count: None,
1352                },
1353                wgpu::BindGroupLayoutEntry {
1354                    binding: 1,
1355                    visibility: wgpu::ShaderStages::FRAGMENT,
1356                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
1357                    count: None,
1358                },
1359            ],
1360        });
1361        // We reuse this for RGBA images for simplicity, or create a distinct one
1362        let image_bind_layout_rgba = text_bind_layout.clone();
1363
1364        // Layout for NV12 Images (TextureY + TextureUV + Sampler + YuvTransform uniform)
1365        let image_bind_layout_nv12 =
1366            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
1367                label: Some("image bind layout nv12"),
1368                entries: &[
1369                    // Y plane
1370                    wgpu::BindGroupLayoutEntry {
1371                        binding: 0,
1372                        visibility: wgpu::ShaderStages::FRAGMENT,
1373                        ty: wgpu::BindingType::Texture {
1374                            multisampled: false,
1375                            view_dimension: wgpu::TextureViewDimension::D2,
1376                            sample_type: wgpu::TextureSampleType::Float { filterable: true },
1377                        },
1378                        count: None,
1379                    },
1380                    // UV plane
1381                    wgpu::BindGroupLayoutEntry {
1382                        binding: 1,
1383                        visibility: wgpu::ShaderStages::FRAGMENT,
1384                        ty: wgpu::BindingType::Texture {
1385                            multisampled: false,
1386                            view_dimension: wgpu::TextureViewDimension::D2,
1387                            sample_type: wgpu::TextureSampleType::Float { filterable: true },
1388                        },
1389                        count: None,
1390                    },
1391                    // Sampler
1392                    wgpu::BindGroupLayoutEntry {
1393                        binding: 2,
1394                        visibility: wgpu::ShaderStages::FRAGMENT,
1395                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
1396                        count: None,
1397                    },
1398                    // YUV transform uniform buffer
1399                    wgpu::BindGroupLayoutEntry {
1400                        binding: 3,
1401                        visibility: wgpu::ShaderStages::FRAGMENT,
1402                        ty: wgpu::BindingType::Buffer {
1403                            ty: wgpu::BufferBindingType::Uniform,
1404                            has_dynamic_offset: false,
1405                            min_binding_size: None,
1406                        },
1407                        count: None,
1408                    },
1409                ],
1410            });
1411
1412        // Clipping layout
1413        let clip_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
1414            label: Some("clip pipeline layout"),
1415            bind_group_layouts: &[Some(&globals_layout)],
1416            immediate_size: 0,
1417        });
1418        let clip_vertex_layout = wgpu::VertexBufferLayout {
1419            array_stride: std::mem::size_of::<ClipInstance>() as u64,
1420            step_mode: wgpu::VertexStepMode::Instance,
1421            attributes: &[
1422                wgpu::VertexAttribute {
1423                    shader_location: 0,
1424                    offset: 0,
1425                    format: wgpu::VertexFormat::Float32x4,
1426                },
1427                wgpu::VertexAttribute {
1428                    shader_location: 1,
1429                    offset: 16,
1430                    format: wgpu::VertexFormat::Float32x4,
1431                },
1432                wgpu::VertexAttribute {
1433                    shader_location: 2,
1434                    offset: 32,
1435                    format: wgpu::VertexFormat::Float32x2,
1436                },
1437            ],
1438        };
1439        let clip_color_target = wgpu::ColorTargetState {
1440            format: output_format,
1441            blend: None,
1442            write_mask: wgpu::ColorWrites::empty(),
1443        };
1444
1445        // Two sets of pipelines: one for the MSAA surface pass, one for layer
1446        // render-to-texture passes (sample_count = 1).
1447        let surface_pipes = Pipelines::create(
1448            &device,
1449            output_format,
1450            msaa_samples,
1451            &globals_layout,
1452            &text_bind_layout,
1453            &image_bind_layout_nv12,
1454            &clip_pipeline_layout,
1455            &stencil_for_content,
1456            &stencil_for_clip_inc,
1457            &stencil_for_clip_dec,
1458            &clip_color_target,
1459            &clip_vertex_layout,
1460        );
1461        let layer_pipes = Pipelines::create(
1462            &device,
1463            output_format,
1464            1,
1465            &globals_layout,
1466            &text_bind_layout,
1467            &image_bind_layout_nv12,
1468            &clip_pipeline_layout,
1469            &stencil_for_content,
1470            &stencil_for_clip_inc,
1471            &stencil_for_clip_dec,
1472            &clip_color_target,
1473            &clip_vertex_layout,
1474        );
1475
1476        // Vector glyph rendering always available with tessellation+MSAA approach.
1477        let slug_enabled = true;
1478
1479        // Blur composite ring (for graphics-layer drop shadows)
1480        let blur_ring = UploadRing::new(&device, "blur ring", 1024 * 1024);
1481
1482        // Atlases
1483        let atlas_mask = init_atlas_mask(&device);
1484        let atlas_color = init_atlas_color(&device);
1485
1486        // Upload rings
1487        let ring_rect = UploadRing::new(&device, "ring rect", 1 << 20);
1488        let ring_border = UploadRing::new(&device, "ring border", 1 << 20);
1489        let ring_ellipse = UploadRing::new(&device, "ring ellipse", 1 << 20);
1490        let ring_ellipse_border = UploadRing::new(&device, "ring ellipse border", 1 << 20);
1491        let ring_arc = UploadRing::new(&device, "ring arc", 1 << 20);
1492        let ring_glyph_mask = UploadRing::new(&device, "ring glyph mask", 1 << 20);
1493        let ring_glyph_color = UploadRing::new(&device, "ring glyph color", 1 << 20);
1494        let ring_slug = UploadRing::new(&device, "ring slug", 1 << 22);
1495        let ring_clip = UploadRing::new(&device, "ring clip", 1 << 16);
1496        let ring_nv12 = UploadRing::new(&device, "ring nv12", 1 << 20);
1497
1498        // Placeholder textures
1499        let depth_stencil_tex = device.create_texture(&wgpu::TextureDescriptor {
1500            label: Some("temp ds"),
1501            size: wgpu::Extent3d {
1502                width: 1,
1503                height: 1,
1504                depth_or_array_layers: 1,
1505            },
1506            mip_level_count: 1,
1507            sample_count: 1,
1508            dimension: wgpu::TextureDimension::D2,
1509            format: wgpu::TextureFormat::Depth24PlusStencil8,
1510            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
1511            view_formats: &[],
1512        });
1513        let depth_stencil_view =
1514            depth_stencil_tex.create_view(&wgpu::TextureViewDescriptor::default());
1515
1516        let mut renderer = WgpuSceneRenderer {
1517            device,
1518            queue,
1519            output_format,
1520            output_width: 0,
1521            output_height: 0,
1522
1523            surface_pipes,
1524            layer_pipes,
1525
1526            rects: InstancedPipe::new(ring_rect),
1527            borders: InstancedPipe::new(ring_border),
1528            ellipses: InstancedPipe::new(ring_ellipse),
1529            ellipse_borders: InstancedPipe::new(ring_ellipse_border),
1530            arcs: InstancedPipe::new(ring_arc),
1531            glyph_mask: InstancedPipe::new(ring_glyph_mask),
1532            glyph_color: InstancedPipe::new(ring_glyph_color),
1533
1534            text_bind_layout,
1535
1536            image_bind_layout_rgba,
1537            image_bind_layout_nv12,
1538            image_sampler,
1539            layer_sampler,
1540            layer_sampler_linear,
1541
1542            blur_ring,
1543
1544            slug_enabled,
1545            slug_ring: ring_slug,
1546            slug_cache: slug::GlyphSlugCache::new(),
1547
1548            clip_ring: ring_clip,
1549
1550            nv12: InstancedPipe::new(ring_nv12),
1551
1552            msaa_samples,
1553            depth_stencil_tex,
1554            depth_stencil_view,
1555            msaa_tex: None,
1556            msaa_view: None,
1557            globals_bind,
1558            globals_buf,
1559            globals_layout,
1560
1561            atlas_mask,
1562            atlas_color,
1563
1564            next_image_handle: 1,
1565            images: HashMap::new(),
1566
1567            frame_index: 0,
1568            image_bytes_total: 0,
1569            image_evict_after_frames: 600,         // ~10s @ 60fps
1570            image_budget_bytes: 512 * 1024 * 1024, // 512 MB
1571            layer_pool: HashMap::new(),
1572
1573            working_space: false,
1574            ws_tex: None,
1575            ws_view: None,
1576            ws_bind: None,
1577            display_pipeline: None,
1578            display_layout: None,
1579        };
1580
1581        renderer.recreate_msaa_and_depth_stencil();
1582        renderer
1583    }
1584}
1585
1586impl WgpuSurfaceBackend {
1587    #[cfg(feature = "winit-surface")]
1588    pub async fn new_async(window: Arc<winit::window::Window>) -> anyhow::Result<WgpuSurfaceBackend> {
1589        let instance: Instance;
1590
1591        if cfg!(target_arch = "wasm32") {
1592            let mut desc = wgpu::InstanceDescriptor::new_without_display_handle();
1593            desc.backends = wgpu::Backends::BROWSER_WEBGPU | wgpu::Backends::GL;
1594            instance = wgpu::util::new_instance_with_webgpu_detection(desc).await;
1595        } else {
1596            instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());
1597        };
1598
1599        let surface = instance.create_surface(window.clone())?;
1600
1601        let adapter = instance
1602            .request_adapter(&wgpu::RequestAdapterOptions {
1603                power_preference: wgpu::PowerPreference::HighPerformance,
1604                compatible_surface: Some(&surface),
1605                force_fallback_adapter: false,
1606                apply_limit_buckets: false,
1607            })
1608            .await
1609            .map_err(|e| anyhow::anyhow!("No suitable adapter: {e:?}"))?;
1610
1611        let limits = adapter.limits();
1612
1613        #[cfg(target_os = "linux")]
1614        let features = {
1615            let af = adapter.features();
1616            let mut f = wgpu::Features::empty();
1617            if af.contains(wgpu::Features::VULKAN_EXTERNAL_MEMORY_FD) {
1618                f |= wgpu::Features::VULKAN_EXTERNAL_MEMORY_FD;
1619            }
1620            if af.contains(wgpu::Features::VULKAN_EXTERNAL_MEMORY_DMA_BUF) {
1621                f |= wgpu::Features::VULKAN_EXTERNAL_MEMORY_DMA_BUF;
1622            }
1623            f
1624        };
1625        #[cfg(not(target_os = "linux"))]
1626        let features = wgpu::Features::empty();
1627
1628        let (device, queue) = adapter
1629            .request_device(&wgpu::DeviceDescriptor {
1630                label: Some("repose-rs device"),
1631                required_features: features,
1632                required_limits: limits,
1633                experimental_features: wgpu::ExperimentalFeatures::disabled(),
1634                memory_hints: wgpu::MemoryHints::default(),
1635                trace: wgpu::Trace::Off,
1636            })
1637            .await
1638            .map_err(|e| anyhow::anyhow!("request_device failed: {e:?}"))?;
1639
1640        let size = window.inner_size();
1641
1642        let caps = surface.get_capabilities(&adapter);
1643        let format = caps
1644            .formats
1645            .iter()
1646            .copied()
1647            .find(|f| f.is_srgb())
1648            .unwrap_or(caps.formats[0]);
1649        let present_mode = caps
1650            .present_modes
1651            .iter()
1652            .copied()
1653            .find(|m| *m == wgpu::PresentMode::Fifo)
1654            .or_else(|| caps.present_modes.iter().copied().find(|m| *m == wgpu::PresentMode::Mailbox))
1655            .unwrap_or(wgpu::PresentMode::Immediate);
1656        let alpha_mode = caps.alpha_modes[0];
1657
1658        // Pick MSAA sample count
1659        let fmt_features = adapter.get_texture_format_features(format);
1660        let msaa_samples = if fmt_features.flags.sample_count_supported(4)
1661            && fmt_features
1662                .flags
1663                .contains(wgpu::TextureFormatFeatureFlags::MULTISAMPLE_RESOLVE)
1664        {
1665            4
1666        } else {
1667            1
1668        };
1669
1670        let renderer = WgpuSceneRenderer::from_device(device, queue, format, msaa_samples);
1671
1672        let config = wgpu::SurfaceConfiguration {
1673            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
1674            format,
1675            width: size.width.max(1),
1676            height: size.height.max(1),
1677            present_mode,
1678            alpha_mode,
1679            color_space: wgpu::SurfaceColorSpace::Auto,
1680            view_formats: vec![],
1681            desired_maximum_frame_latency: 1,
1682        };
1683        surface.configure(&renderer.device, &config);
1684
1685        Ok(WgpuSurfaceBackend { surface: Some(surface), surface_config: Some(config), renderer })
1686    }
1687
1688    #[cfg(all(feature = "winit-surface", not(target_arch = "wasm32")))]
1689    pub fn new(window: Arc<winit::window::Window>) -> anyhow::Result<WgpuSurfaceBackend> {
1690        pollster::block_on(Self::new_async(window))
1691    }
1692
1693    #[cfg(all(feature = "winit-surface", target_arch = "wasm32"))]
1694    pub fn new(_window: Arc<winit::window::Window>) -> anyhow::Result<WgpuSurfaceBackend> {
1695        anyhow::bail!("Use WgpuSurfaceBackend::new_async(window).await on wasm32")
1696    }
1697}
1698
1699impl WgpuSceneRenderer {
1700    // Image API
1701
1702    pub fn set_image_from_bytes(
1703        &mut self,
1704        handle: u64,
1705        data: &[u8],
1706        srgb: bool,
1707    ) -> anyhow::Result<()> {
1708        let img = image::load_from_memory(data)?;
1709        let rgba = img.to_rgba8();
1710        let (w, h) = rgba.dimensions();
1711        self.set_image_rgba8(handle, w, h, &rgba, srgb)
1712    }
1713
1714    pub fn set_image_rgba8(
1715        &mut self,
1716        handle: u64,
1717        w: u32,
1718        h: u32,
1719        rgba: &[u8],
1720        srgb: bool,
1721    ) -> anyhow::Result<()> {
1722        let expected = (w as usize) * (h as usize) * 4;
1723        if rgba.len() < expected {
1724            return Err(anyhow::anyhow!(
1725                "RGBA buffer too small: {} < {}",
1726                rgba.len(),
1727                expected
1728            ));
1729        }
1730
1731        let format = if srgb {
1732            wgpu::TextureFormat::Rgba8UnormSrgb
1733        } else {
1734            wgpu::TextureFormat::Rgba8Unorm
1735        };
1736
1737        let needs_recreate = match self.images.get(&handle) {
1738            Some(ImageTex::Rgba {
1739                w: cw,
1740                h: ch,
1741                format: cf,
1742                ..
1743            }) => *cw != w || *ch != h || *cf != format,
1744            _ => true,
1745        };
1746
1747        if needs_recreate {
1748            // Remove old to track budget correctly
1749            self.remove_image(handle);
1750
1751            let tex = self.device.create_texture(&wgpu::TextureDescriptor {
1752                label: Some("user image rgba"),
1753                size: wgpu::Extent3d {
1754                    width: w,
1755                    height: h,
1756                    depth_or_array_layers: 1,
1757                },
1758                mip_level_count: 1,
1759                sample_count: 1,
1760                dimension: wgpu::TextureDimension::D2,
1761                format,
1762                usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
1763                view_formats: &[],
1764            });
1765            let view = tex.create_view(&wgpu::TextureViewDescriptor::default());
1766
1767            let bind = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
1768                label: Some("image bind rgba"),
1769                layout: &self.image_bind_layout_rgba,
1770                entries: &[
1771                    wgpu::BindGroupEntry {
1772                        binding: 0,
1773                        resource: wgpu::BindingResource::TextureView(&view),
1774                    },
1775                    wgpu::BindGroupEntry {
1776                        binding: 1,
1777                        resource: wgpu::BindingResource::Sampler(&self.image_sampler),
1778                    },
1779                ],
1780            });
1781
1782            let bytes = (w as u64) * (h as u64) * 4;
1783            self.image_bytes_total += bytes;
1784
1785            self.images.insert(
1786                handle,
1787                ImageTex::Rgba {
1788                    tex,
1789                    view,
1790                    bind,
1791                    w,
1792                    h,
1793                    format,
1794                    last_used_frame: self.frame_index,
1795                    bytes,
1796                },
1797            );
1798        }
1799
1800        let tex = match self.images.get(&handle) {
1801            Some(ImageTex::Rgba { tex, .. }) => tex,
1802            _ => unreachable!(),
1803        };
1804
1805        self.queue.write_texture(
1806            wgpu::TexelCopyTextureInfo {
1807                texture: tex,
1808                mip_level: 0,
1809                origin: wgpu::Origin3d::ZERO,
1810                aspect: wgpu::TextureAspect::All,
1811            },
1812            &rgba[..expected],
1813            wgpu::TexelCopyBufferLayout {
1814                offset: 0,
1815                bytes_per_row: Some(4 * w),
1816                rows_per_image: Some(h),
1817            },
1818            wgpu::Extent3d {
1819                width: w,
1820                height: h,
1821                depth_or_array_layers: 1,
1822            },
1823        );
1824
1825        // Ensure budget limits
1826        self.evict_budget_excess();
1827
1828        Ok(())
1829    }
1830
1831    pub fn set_image_nv12(
1832        &mut self,
1833        handle: u64,
1834        w: u32,
1835        h: u32,
1836        y: &[u8],
1837        uv: &[u8],
1838        color_info: ColorInfo,
1839    ) -> anyhow::Result<()> {
1840        let y_expected = (w as usize) * (h as usize);
1841        let uv_w = w.div_ceil(2);
1842        let uv_h = h.div_ceil(2);
1843        let uv_expected = (uv_w as usize) * (uv_h as usize) * 2;
1844
1845        if y.len() < y_expected {
1846            return Err(anyhow::anyhow!("Y plane too small"));
1847        }
1848        if uv.len() < uv_expected {
1849            return Err(anyhow::anyhow!("UV plane too small"));
1850        }
1851
1852        let needs_recreate = match self.images.get(&handle) {
1853            Some(ImageTex::Nv12 { w: ww, h: hh, .. }) => *ww != w || *hh != h,
1854            _ => true,
1855        };
1856
1857        // Compute the YUV→RGB transform on the CPU.
1858        let yuv = color_info.to_yuv_transform();
1859        let yuv_raw = YuvTransformRaw {
1860            row0: [yuv.m[0][0], yuv.m[0][1], yuv.m[0][2], 0.0],
1861            row1: [yuv.m[1][0], yuv.m[1][1], yuv.m[1][2], 0.0],
1862            row2: [yuv.m[2][0], yuv.m[2][1], yuv.m[2][2], 0.0],
1863            b: [yuv.b[0], yuv.b[1], yuv.b[2], 0.0],
1864        };
1865
1866        if needs_recreate {
1867            self.remove_image(handle);
1868
1869            let tex_y = self.device.create_texture(&wgpu::TextureDescriptor {
1870                label: Some("nv12 Y"),
1871                size: wgpu::Extent3d {
1872                    width: w,
1873                    height: h,
1874                    depth_or_array_layers: 1,
1875                },
1876                mip_level_count: 1,
1877                sample_count: 1,
1878                dimension: wgpu::TextureDimension::D2,
1879                format: wgpu::TextureFormat::R8Unorm,
1880                usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
1881                view_formats: &[],
1882            });
1883            let view_y = tex_y.create_view(&wgpu::TextureViewDescriptor::default());
1884
1885            let tex_uv = self.device.create_texture(&wgpu::TextureDescriptor {
1886                label: Some("nv12 UV"),
1887                size: wgpu::Extent3d {
1888                    width: uv_w,
1889                    height: uv_h,
1890                    depth_or_array_layers: 1,
1891                },
1892                mip_level_count: 1,
1893                sample_count: 1,
1894                dimension: wgpu::TextureDimension::D2,
1895                format: wgpu::TextureFormat::Rg8Unorm,
1896                usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
1897                view_formats: &[],
1898            });
1899            let view_uv = tex_uv.create_view(&wgpu::TextureViewDescriptor::default());
1900
1901            // Create a uniform buffer for the YUV transform (per-image).
1902            let yuv_buf = self.device.create_buffer(&wgpu::BufferDescriptor {
1903                label: Some("nv12 yuv transform"),
1904                size: std::mem::size_of::<YuvTransformRaw>() as u64,
1905                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
1906                mapped_at_creation: false,
1907            });
1908
1909            // Write initial transform.
1910            self.queue
1911                .write_buffer(&yuv_buf, 0, bytemuck::bytes_of(&yuv_raw));
1912
1913            let bind = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
1914                label: Some("nv12 bind"),
1915                layout: &self.image_bind_layout_nv12,
1916                entries: &[
1917                    wgpu::BindGroupEntry {
1918                        binding: 0,
1919                        resource: wgpu::BindingResource::TextureView(&view_y),
1920                    },
1921                    wgpu::BindGroupEntry {
1922                        binding: 1,
1923                        resource: wgpu::BindingResource::TextureView(&view_uv),
1924                    },
1925                    wgpu::BindGroupEntry {
1926                        binding: 2,
1927                        resource: wgpu::BindingResource::Sampler(&self.image_sampler),
1928                    },
1929                    wgpu::BindGroupEntry {
1930                        binding: 3,
1931                        resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
1932                            buffer: &yuv_buf,
1933                            offset: 0,
1934                            size: None,
1935                        }),
1936                    },
1937                ],
1938            });
1939
1940            let bytes = (w as u64) * (h as u64)
1941                + (uv_w as u64) * (uv_h as u64) * 2
1942                + std::mem::size_of::<YuvTransformRaw>() as u64;
1943            self.image_bytes_total += bytes;
1944
1945            self.images.insert(
1946                handle,
1947                ImageTex::Nv12 {
1948                    tex_y,
1949                    view_y,
1950                    tex_uv,
1951                    view_uv,
1952                    bind,
1953                    yuv_buf,
1954                    w,
1955                    h,
1956                    color_info,
1957                    last_used_frame: self.frame_index,
1958                    bytes,
1959                },
1960            );
1961        } else {
1962            // Re-use existing textures; just update the YUV transform if needed.
1963            if let Some(ImageTex::Nv12 { yuv_buf, .. }) = self.images.get(&handle) {
1964                self.queue
1965                    .write_buffer(yuv_buf, 0, bytemuck::bytes_of(&yuv_raw));
1966            }
1967        }
1968
1969        let (tex_y, tex_uv, _bind) = match self.images.get(&handle) {
1970            Some(ImageTex::Nv12 {
1971                tex_y,
1972                tex_uv,
1973                bind,
1974                ..
1975            }) => (tex_y, tex_uv, bind),
1976            _ => return Err(anyhow::anyhow!("Handle is not NV12")),
1977        };
1978
1979        self.queue.write_texture(
1980            wgpu::TexelCopyTextureInfo {
1981                texture: tex_y,
1982                mip_level: 0,
1983                origin: wgpu::Origin3d::ZERO,
1984                aspect: wgpu::TextureAspect::All,
1985            },
1986            &y[..y_expected],
1987            wgpu::TexelCopyBufferLayout {
1988                offset: 0,
1989                bytes_per_row: Some(w),
1990                rows_per_image: Some(h),
1991            },
1992            wgpu::Extent3d {
1993                width: w,
1994                height: h,
1995                depth_or_array_layers: 1,
1996            },
1997        );
1998
1999        self.queue.write_texture(
2000            wgpu::TexelCopyTextureInfo {
2001                texture: tex_uv,
2002                mip_level: 0,
2003                origin: wgpu::Origin3d::ZERO,
2004                aspect: wgpu::TextureAspect::All,
2005            },
2006            &uv[..uv_expected],
2007            wgpu::TexelCopyBufferLayout {
2008                offset: 0,
2009                bytes_per_row: Some(2 * uv_w),
2010                rows_per_image: Some(uv_h),
2011            },
2012            wgpu::Extent3d {
2013                width: uv_w,
2014                height: uv_h,
2015                depth_or_array_layers: 1,
2016            },
2017        );
2018
2019        self.evict_budget_excess();
2020        Ok(())
2021    }
2022
2023    pub fn set_image_planes(
2024        &mut self,
2025        handle: u64,
2026        w: u32,
2027        h: u32,
2028        pixel_format: PixelFormat,
2029        planes: &[&[u8]],
2030        color_info: ColorInfo,
2031    ) -> anyhow::Result<()> {
2032        match pixel_format {
2033            PixelFormat::Nv12 => {
2034                let y = planes.first().ok_or(anyhow::anyhow!("missing Y plane"))?;
2035                let uv = planes.get(1).ok_or(anyhow::anyhow!("missing UV plane"))?;
2036                self.set_image_nv12(handle, w, h, y, uv, color_info)
2037            }
2038            PixelFormat::P010 => {
2039                let y = planes.first().ok_or(anyhow::anyhow!("missing Y plane"))?;
2040                let uv = planes.get(1).ok_or(anyhow::anyhow!("missing UV plane"))?;
2041                self.set_image_p010(handle, w, h, y, uv, color_info)
2042            }
2043            PixelFormat::I420 | PixelFormat::I444 => Err(anyhow::anyhow!(
2044                "I420/I444 not implemented and unlikely -> cheap to convert to NV12 (better for the GPU too)"
2045            )),
2046            PixelFormat::Rgba => {
2047                let rgba = planes
2048                    .first()
2049                    .ok_or(anyhow::anyhow!("missing RGBA plane"))?;
2050                self.set_image_rgba8(handle, w, h, rgba, false)
2051            }
2052        }
2053    }
2054
2055    fn set_image_p010(
2056        &mut self,
2057        handle: u64,
2058        w: u32,
2059        h: u32,
2060        y: &[u8],
2061        uv: &[u8],
2062        color_info: ColorInfo,
2063    ) -> anyhow::Result<()> {
2064        let uv_w = w.div_ceil(2);
2065        let uv_h = h.div_ceil(2);
2066
2067        let y_expected = (w as usize) * 2;
2068        let uv_expected = (uv_w as usize) * (uv_h as usize) * 4;
2069
2070        if y.len() < y_expected {
2071            return Err(anyhow::anyhow!("P010 Y plane too small"));
2072        }
2073        if uv.len() < uv_expected {
2074            return Err(anyhow::anyhow!("P010 UV plane too small"));
2075        }
2076
2077        // P010 reuses the NV12 pipeline (same bind group layout -> wgpu
2078        // abstracts the storage format so R16Unorm/Rg16Unorm are
2079        // filterable float textures just like R8Unorm/Rg8Unorm).
2080        let needs_recreate = match self.images.get(&handle) {
2081            Some(ImageTex::Nv12 { w: ww, h: hh, .. }) => *ww != w || *hh != h,
2082            _ => true,
2083        };
2084
2085        let yuv = color_info.to_yuv_transform();
2086        let yuv_raw = YuvTransformRaw {
2087            row0: [yuv.m[0][0], yuv.m[0][1], yuv.m[0][2], 0.0],
2088            row1: [yuv.m[1][0], yuv.m[1][1], yuv.m[1][2], 0.0],
2089            row2: [yuv.m[2][0], yuv.m[2][1], yuv.m[2][2], 0.0],
2090            b: [yuv.b[0], yuv.b[1], yuv.b[2], 0.0],
2091        };
2092
2093        if needs_recreate {
2094            self.remove_image(handle);
2095
2096            let tex_y = self.device.create_texture(&wgpu::TextureDescriptor {
2097                label: Some("p010 Y"),
2098                size: wgpu::Extent3d {
2099                    width: w,
2100                    height: h,
2101                    depth_or_array_layers: 1,
2102                },
2103                mip_level_count: 1,
2104                sample_count: 1,
2105                dimension: wgpu::TextureDimension::D2,
2106                format: wgpu::TextureFormat::R16Unorm,
2107                usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
2108                view_formats: &[],
2109            });
2110            let view_y = tex_y.create_view(&wgpu::TextureViewDescriptor::default());
2111
2112            let tex_uv = self.device.create_texture(&wgpu::TextureDescriptor {
2113                label: Some("p010 UV"),
2114                size: wgpu::Extent3d {
2115                    width: uv_w,
2116                    height: uv_h,
2117                    depth_or_array_layers: 1,
2118                },
2119                mip_level_count: 1,
2120                sample_count: 1,
2121                dimension: wgpu::TextureDimension::D2,
2122                format: wgpu::TextureFormat::Rg16Unorm,
2123                usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
2124                view_formats: &[],
2125            });
2126            let view_uv = tex_uv.create_view(&wgpu::TextureViewDescriptor::default());
2127
2128            let yuv_buf = self.device.create_buffer(&wgpu::BufferDescriptor {
2129                label: Some("p010 yuv transform"),
2130                size: std::mem::size_of::<YuvTransformRaw>() as u64,
2131                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
2132                mapped_at_creation: false,
2133            });
2134            self.queue
2135                .write_buffer(&yuv_buf, 0, bytemuck::bytes_of(&yuv_raw));
2136
2137            let bind = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
2138                label: Some("p010 bind"),
2139                layout: &self.image_bind_layout_nv12,
2140                entries: &[
2141                    wgpu::BindGroupEntry {
2142                        binding: 0,
2143                        resource: wgpu::BindingResource::TextureView(&view_y),
2144                    },
2145                    wgpu::BindGroupEntry {
2146                        binding: 1,
2147                        resource: wgpu::BindingResource::TextureView(&view_uv),
2148                    },
2149                    wgpu::BindGroupEntry {
2150                        binding: 2,
2151                        resource: wgpu::BindingResource::Sampler(&self.image_sampler),
2152                    },
2153                    wgpu::BindGroupEntry {
2154                        binding: 3,
2155                        resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
2156                            buffer: &yuv_buf,
2157                            offset: 0,
2158                            size: None,
2159                        }),
2160                    },
2161                ],
2162            });
2163
2164            let bytes = (w as u64) * 2
2165                + (uv_w as u64) * (uv_h as u64) * 4
2166                + std::mem::size_of::<YuvTransformRaw>() as u64;
2167            self.image_bytes_total += bytes;
2168
2169            self.images.insert(
2170                handle,
2171                ImageTex::Nv12 {
2172                    tex_y,
2173                    view_y,
2174                    tex_uv,
2175                    view_uv,
2176                    bind,
2177                    yuv_buf,
2178                    w,
2179                    h,
2180                    color_info,
2181                    last_used_frame: self.frame_index,
2182                    bytes,
2183                },
2184            );
2185        } else {
2186            if let Some(ImageTex::Nv12 { yuv_buf, .. }) = self.images.get(&handle) {
2187                self.queue
2188                    .write_buffer(yuv_buf, 0, bytemuck::bytes_of(&yuv_raw));
2189            }
2190        }
2191
2192        let (tex_y, tex_uv, _bind) = match self.images.get(&handle) {
2193            Some(ImageTex::Nv12 {
2194                tex_y,
2195                tex_uv,
2196                bind,
2197                ..
2198            }) => (tex_y, tex_uv, bind),
2199            _ => return Err(anyhow::anyhow!("Handle is not P010/NV12")),
2200        };
2201
2202        self.queue.write_texture(
2203            wgpu::TexelCopyTextureInfo {
2204                texture: tex_y,
2205                mip_level: 0,
2206                origin: wgpu::Origin3d::ZERO,
2207                aspect: wgpu::TextureAspect::All,
2208            },
2209            &y[..y_expected],
2210            wgpu::TexelCopyBufferLayout {
2211                offset: 0,
2212                bytes_per_row: Some(w * 2),
2213                rows_per_image: Some(h),
2214            },
2215            wgpu::Extent3d {
2216                width: w,
2217                height: h,
2218                depth_or_array_layers: 1,
2219            },
2220        );
2221        self.queue.write_texture(
2222            wgpu::TexelCopyTextureInfo {
2223                texture: tex_uv,
2224                mip_level: 0,
2225                origin: wgpu::Origin3d::ZERO,
2226                aspect: wgpu::TextureAspect::All,
2227            },
2228            &uv[..uv_expected],
2229            wgpu::TexelCopyBufferLayout {
2230                offset: 0,
2231                bytes_per_row: Some(uv_w * 4),
2232                rows_per_image: Some(uv_h),
2233            },
2234            wgpu::Extent3d {
2235                width: uv_w,
2236                height: uv_h,
2237                depth_or_array_layers: 1,
2238            },
2239        );
2240
2241        self.evict_budget_excess();
2242        Ok(())
2243    }
2244
2245    #[cfg(target_os = "linux")]
2246    pub fn set_image_dmabuf(
2247        &mut self,
2248        handle: u64,
2249        w: u32,
2250        h: u32,
2251        fds: Vec<std::os::unix::io::OwnedFd>,
2252        modifier: u64,
2253        strides: Vec<u32>,
2254        offsets: Vec<u64>,
2255        color_info: ColorInfo,
2256    ) -> anyhow::Result<()> {
2257        log::info!("set_image_dmabuf handle={handle} {}x{} fds={} modifier=0x{modifier:x}", w, h, fds.len());
2258
2259        self.remove_image(handle);
2260
2261        let yuv = color_info.to_yuv_transform();
2262        let yuv_raw = YuvTransformRaw {
2263            row0: [yuv.m[0][0], yuv.m[0][1], yuv.m[0][2], 0.0],
2264            row1: [yuv.m[1][0], yuv.m[1][1], yuv.m[1][2], 0.0],
2265            row2: [yuv.m[2][0], yuv.m[2][1], yuv.m[2][2], 0.0],
2266            b: [yuv.b[0], yuv.b[1], yuv.b[2], 0.0],
2267        };
2268
2269        if fds.len() != 2 {
2270            return Err(anyhow::anyhow!(
2271                "unsupported fd count {} - need exactly 2 for separate Y/UV planes",
2272                fds.len()
2273            ));
2274        }
2275
2276        let uv_w = w.div_ceil(2);
2277        let uv_h = h.div_ceil(2);
2278
2279        let hal_y_desc = wgpu::hal::TextureDescriptor {
2280            label: Some("dmabuf y"),
2281            size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },
2282            mip_level_count: 1,
2283            sample_count: 1,
2284            dimension: wgpu::TextureDimension::D2,
2285            format: wgpu::TextureFormat::R8Unorm,
2286            usage: wgpu::wgt::TextureUses::RESOURCE,
2287            memory_flags: wgpu::hal::MemoryFlags::empty(),
2288            view_formats: vec![],
2289        };
2290        let hal_uv_desc = wgpu::hal::TextureDescriptor {
2291            label: Some("dmabuf uv"),
2292            size: wgpu::Extent3d { width: uv_w, height: uv_h, depth_or_array_layers: 1 },
2293            mip_level_count: 1,
2294            sample_count: 1,
2295            dimension: wgpu::TextureDimension::D2,
2296            format: wgpu::TextureFormat::Rg8Unorm,
2297            usage: wgpu::wgt::TextureUses::RESOURCE,
2298            memory_flags: wgpu::hal::MemoryFlags::empty(),
2299            view_formats: vec![],
2300        };
2301
2302        let wgpu_y_desc = wgpu::TextureDescriptor {
2303            label: Some("dmabuf y"),
2304            size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },
2305            mip_level_count: 1,
2306            sample_count: 1,
2307            dimension: wgpu::TextureDimension::D2,
2308            format: wgpu::TextureFormat::R8Unorm,
2309            usage: wgpu::TextureUsages::TEXTURE_BINDING,
2310            view_formats: &[],
2311        };
2312        let wgpu_uv_desc = wgpu::TextureDescriptor {
2313            label: Some("dmabuf uv"),
2314            size: wgpu::Extent3d { width: uv_w, height: uv_h, depth_or_array_layers: 1 },
2315            mip_level_count: 1,
2316            sample_count: 1,
2317            dimension: wgpu::TextureDimension::D2,
2318            format: wgpu::TextureFormat::Rg8Unorm,
2319            usage: wgpu::TextureUsages::TEXTURE_BINDING,
2320            view_formats: &[],
2321        };
2322
2323        let (tex_y, view_y, tex_uv, view_uv) = unsafe {
2324            let mut hal_guard = self.device.as_hal::<wgpu::hal::vulkan::Api>()
2325                .ok_or_else(|| {
2326                    log::warn!("as_hal::<vulkan::Api> returned None");
2327                    anyhow::anyhow!("Device is not Vulkan")
2328                })?;
2329
2330            let mut fds = fds;
2331            let uv_fd = fds.remove(1);
2332            let y_fd = fds.remove(0);
2333
2334            let yt = hal_guard
2335                .texture_from_dmabuf_fd(y_fd, &hal_y_desc, modifier, strides[0] as u64, offsets[0] as u64)
2336                .map_err(|e| anyhow::anyhow!("import Y dmabuf: {e:?}"))?;
2337            log::info!("imported Y dmabuf OK");
2338
2339            let uvt = hal_guard
2340                .texture_from_dmabuf_fd(uv_fd, &hal_uv_desc, modifier, strides[1] as u64, offsets[1] as u64)
2341                .map_err(|e| anyhow::anyhow!("import UV dmabuf: {e:?}"))?;
2342            log::info!("imported UV dmabuf OK");
2343
2344            drop(hal_guard);
2345
2346            let tex_y = self.device.create_texture_from_hal::<wgpu::hal::vulkan::Api>(
2347                yt,
2348                &wgpu_y_desc,
2349                wgpu::wgt::TextureUses::UNINITIALIZED,
2350            );
2351            let view_y = tex_y.create_view(&wgpu::TextureViewDescriptor::default());
2352
2353            let tex_uv = self.device.create_texture_from_hal::<wgpu::hal::vulkan::Api>(
2354                uvt,
2355                &wgpu_uv_desc,
2356                wgpu::wgt::TextureUses::UNINITIALIZED,
2357            );
2358            let view_uv = tex_uv.create_view(&wgpu::TextureViewDescriptor::default());
2359
2360            (tex_y, view_y, tex_uv, view_uv)
2361        };
2362
2363        let yuv_buf = self.device.create_buffer(&wgpu::BufferDescriptor {
2364            label: Some("dmabuf yuv transform"),
2365            size: std::mem::size_of::<YuvTransformRaw>() as u64,
2366            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
2367            mapped_at_creation: false,
2368        });
2369        self.queue.write_buffer(&yuv_buf, 0, bytemuck::bytes_of(&yuv_raw));
2370
2371        let bind = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
2372            label: Some("dmabuf nv12 bind"),
2373            layout: &self.image_bind_layout_nv12,
2374            entries: &[
2375                wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&view_y) },
2376                wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(&view_uv) },
2377                wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::Sampler(&self.image_sampler) },
2378                wgpu::BindGroupEntry {
2379                    binding: 3,
2380                    resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
2381                        buffer: &yuv_buf,
2382                        offset: 0,
2383                        size: None,
2384                    }),
2385                },
2386            ],
2387        });
2388
2389        let bytes = (w as u64) * (h as u64)
2390            + (uv_w as u64) * (uv_h as u64) * 2
2391            + std::mem::size_of::<YuvTransformRaw>() as u64;
2392
2393        self.images.insert(
2394            handle,
2395            ImageTex::Nv12 {
2396                tex_y,
2397                view_y,
2398                tex_uv,
2399                view_uv,
2400                bind,
2401                yuv_buf,
2402                w,
2403                h,
2404                color_info,
2405                last_used_frame: self.frame_index,
2406                bytes,
2407            },
2408        );
2409
2410        self.evict_budget_excess();
2411        Ok(())
2412    }
2413
2414    pub fn remove_image(&mut self, handle: u64) {
2415        if let Some(img) = self.images.remove(&handle) {
2416            let b = match &img {
2417                ImageTex::Rgba { bytes, .. } => *bytes,
2418                ImageTex::Nv12 { bytes, .. } => *bytes,
2419            };
2420            self.image_bytes_total = self.image_bytes_total.saturating_sub(b);
2421        }
2422    }
2423
2424    // Legacy support from Step 1 instructions (temporary until platform render logic is fully swapped)
2425    pub fn register_image_from_bytes(&mut self, data: &[u8], srgb: bool) -> u64 {
2426        let handle = self.next_image_handle;
2427        self.next_image_handle += 1;
2428        if let Err(e) = self.set_image_from_bytes(handle, data, srgb) {
2429            log::error!("Failed to register image: {e}");
2430        }
2431        handle
2432    }
2433
2434    fn evict_unused_images(&mut self) {
2435        let now = self.frame_index;
2436        let evict_after = self.image_evict_after_frames;
2437
2438        // Time based eviction
2439        let mut to_remove = Vec::new();
2440        for (h, t) in self.images.iter() {
2441            let last = match t {
2442                ImageTex::Rgba {
2443                    last_used_frame, ..
2444                } => *last_used_frame,
2445                ImageTex::Nv12 {
2446                    last_used_frame, ..
2447                } => *last_used_frame,
2448            };
2449            if now.saturating_sub(last) > evict_after {
2450                to_remove.push(*h);
2451            }
2452        }
2453        for h in to_remove {
2454            self.remove_image(h);
2455        }
2456
2457        self.evict_budget_excess();
2458    }
2459
2460    fn evict_budget_excess(&mut self) {
2461        if self.image_bytes_total <= self.image_budget_bytes {
2462            return;
2463        }
2464        // Collect (handle, last_used, bytes)
2465        let mut candidates: Vec<(u64, u64, u64)> = self
2466            .images
2467            .iter()
2468            .map(|(h, t)| {
2469                let (last, bytes) = match t {
2470                    ImageTex::Rgba {
2471                        last_used_frame,
2472                        bytes,
2473                        ..
2474                    } => (*last_used_frame, *bytes),
2475                    ImageTex::Nv12 {
2476                        last_used_frame,
2477                        bytes,
2478                        ..
2479                    } => (*last_used_frame, *bytes),
2480                };
2481                (*h, last, bytes)
2482            })
2483            .collect();
2484
2485        // Sort by last_used ascending (LRU first)
2486        candidates.sort_by_key(|k| k.1);
2487
2488        let now = self.frame_index;
2489        for (h, last, _bytes) in candidates {
2490            if self.image_bytes_total <= self.image_budget_bytes {
2491                break;
2492            }
2493            // Don't evict something used this frame
2494            if last == now {
2495                continue;
2496            }
2497            self.remove_image(h);
2498        }
2499    }
2500
2501    /// Enable or disable linear working-space rendering.
2502    /// When enabled, the scene is rendered into an Rgba16Float intermediate
2503    /// and a final full-screen pass applies the display OETF.
2504    pub fn set_working_space(&mut self, enabled: bool) {
2505        if enabled == self.working_space {
2506            return;
2507        }
2508        self.working_space = enabled;
2509        if enabled {
2510            self.ensure_display_pipeline();
2511            self.recreate_working_space_texture();
2512        } else {
2513            self.ws_tex = None;
2514            self.ws_view = None;
2515            self.ws_bind = None;
2516        }
2517    }
2518
2519    fn ensure_display_pipeline(&mut self) {
2520        if self.display_pipeline.is_some() {
2521            return;
2522        }
2523
2524        let layout = self
2525            .device
2526            .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
2527                label: Some("display transform layout"),
2528                entries: &[
2529                    wgpu::BindGroupLayoutEntry {
2530                        binding: 0,
2531                        visibility: wgpu::ShaderStages::FRAGMENT,
2532                        ty: wgpu::BindingType::Texture {
2533                            multisampled: false,
2534                            view_dimension: wgpu::TextureViewDimension::D2,
2535                            sample_type: wgpu::TextureSampleType::Float { filterable: true },
2536                        },
2537                        count: None,
2538                    },
2539                    wgpu::BindGroupLayoutEntry {
2540                        binding: 1,
2541                        visibility: wgpu::ShaderStages::FRAGMENT,
2542                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
2543                        count: None,
2544                    },
2545                ],
2546            });
2547        self.display_layout = Some(layout);
2548
2549        let shader = self
2550            .device
2551            .create_shader_module(wgpu::ShaderModuleDescriptor {
2552                label: Some("display_transform.wgsl"),
2553                source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(
2554                    "shaders/display_transform.wgsl"
2555                ))),
2556            });
2557
2558        let pipeline_layout = self
2559            .device
2560            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
2561                label: Some("display transform pipeline layout"),
2562                bind_group_layouts: &[None, self.display_layout.as_ref()],
2563                immediate_size: 0,
2564            });
2565
2566        let pipeline = self
2567            .device
2568            .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
2569                label: Some("display transform pipeline"),
2570                layout: Some(&pipeline_layout),
2571                vertex: wgpu::VertexState {
2572                    module: &shader,
2573                    entry_point: Some("vs_main"),
2574                    buffers: &[],
2575                    compilation_options: wgpu::PipelineCompilationOptions::default(),
2576                },
2577                fragment: Some(wgpu::FragmentState {
2578                    module: &shader,
2579                    entry_point: Some("fs_main"),
2580                    targets: &[Some(wgpu::ColorTargetState {
2581                        format: self.output_format,
2582                        blend: None,
2583                        write_mask: wgpu::ColorWrites::ALL,
2584                    })],
2585                    compilation_options: wgpu::PipelineCompilationOptions::default(),
2586                }),
2587                primitive: wgpu::PrimitiveState::default(),
2588                depth_stencil: None,
2589                multisample: wgpu::MultisampleState::default(),
2590                multiview_mask: None,
2591                cache: None,
2592            });
2593        self.display_pipeline = Some(pipeline);
2594    }
2595
2596    /// Resize the render target dimensions.
2597    ///
2598    /// Recreates MSAA, depth-stencil, and working-space textures to match the
2599    /// new size..
2600    pub fn resize(&mut self, width: u32, height: u32) {
2601        self.output_width = width;
2602        self.output_height = height;
2603        self.recreate_msaa_and_depth_stencil();
2604        self.recreate_working_space_texture();
2605    }
2606
2607    fn recreate_working_space_texture(&mut self) {
2608        if !self.working_space {
2609            return;
2610        }
2611        let w = self.output_width.max(1);
2612        let h = self.output_height.max(1);
2613
2614        let tex = self.device.create_texture(&wgpu::TextureDescriptor {
2615            label: Some("working space"),
2616            size: wgpu::Extent3d {
2617                width: w,
2618                height: h,
2619                depth_or_array_layers: 1,
2620            },
2621            mip_level_count: 1,
2622            sample_count: 1,
2623            dimension: wgpu::TextureDimension::D2,
2624            format: wgpu::TextureFormat::Rgba16Float,
2625            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
2626            view_formats: &[],
2627        });
2628        let view = tex.create_view(&wgpu::TextureViewDescriptor::default());
2629
2630        let bind = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
2631            label: Some("working space bind"),
2632            layout: self.display_layout.as_ref().unwrap(),
2633            entries: &[
2634                wgpu::BindGroupEntry {
2635                    binding: 0,
2636                    resource: wgpu::BindingResource::TextureView(&view),
2637                },
2638                wgpu::BindGroupEntry {
2639                    binding: 1,
2640                    resource: wgpu::BindingResource::Sampler(&self.image_sampler),
2641                },
2642            ],
2643        });
2644
2645        self.ws_tex = Some(tex);
2646        self.ws_view = Some(view);
2647        self.ws_bind = Some(bind);
2648    }
2649
2650    fn recreate_msaa_and_depth_stencil(&mut self) {
2651        if self.msaa_samples > 1 {
2652            let tex = self.device.create_texture(&wgpu::TextureDescriptor {
2653                label: Some("msaa color"),
2654                size: wgpu::Extent3d {
2655                    width: self.output_width.max(1),
2656                    height: self.output_height.max(1),
2657                    depth_or_array_layers: 1,
2658                },
2659                mip_level_count: 1,
2660                sample_count: self.msaa_samples,
2661                dimension: wgpu::TextureDimension::D2,
2662                format: self.output_format,
2663                usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
2664                view_formats: &[],
2665            });
2666            let view = tex.create_view(&wgpu::TextureViewDescriptor::default());
2667            self.msaa_tex = Some(tex);
2668            self.msaa_view = Some(view);
2669        } else {
2670            self.msaa_tex = None;
2671            self.msaa_view = None;
2672        }
2673
2674        self.depth_stencil_tex = self.device.create_texture(&wgpu::TextureDescriptor {
2675            label: Some("depth-stencil (stencil clips)"),
2676            size: wgpu::Extent3d {
2677                width: self.output_width.max(1),
2678                height: self.output_height.max(1),
2679                depth_or_array_layers: 1,
2680            },
2681            mip_level_count: 1,
2682            sample_count: self.msaa_samples,
2683            dimension: wgpu::TextureDimension::D2,
2684            format: wgpu::TextureFormat::Depth24PlusStencil8,
2685            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
2686            view_formats: &[],
2687        });
2688        self.depth_stencil_view = self
2689            .depth_stencil_tex
2690            .create_view(&wgpu::TextureViewDescriptor::default());
2691    }
2692
2693
2694
2695    fn get_or_create_layer(
2696        &mut self,
2697        layer_id: u32,
2698        width: u32,
2699        height: u32,
2700        rect: repose_core::Rect,
2701    ) {
2702        let needs_alloc = match self.layer_pool.get(&layer_id) {
2703            Some(lt) => lt.width != width || lt.height != height,
2704            None => true,
2705        };
2706        if !needs_alloc {
2707            if let Some(lt) = self.layer_pool.get_mut(&layer_id) {
2708                lt.rect_px = (rect.x, rect.y, rect.w, rect.h);
2709            }
2710            return;
2711        }
2712        let tex = self.device.create_texture(&wgpu::TextureDescriptor {
2713            label: Some("graphics layer"),
2714            size: wgpu::Extent3d {
2715                width: width.max(1),
2716                height: height.max(1),
2717                depth_or_array_layers: 1,
2718            },
2719            mip_level_count: 1,
2720            sample_count: 1,
2721            dimension: wgpu::TextureDimension::D2,
2722            format: self.output_format,
2723            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
2724            view_formats: &[],
2725        });
2726        let view = tex.create_view(&wgpu::TextureViewDescriptor::default());
2727        let bind = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
2728            label: Some("layer bind"),
2729            layout: &self.image_bind_layout_rgba,
2730            entries: &[
2731                wgpu::BindGroupEntry {
2732                    binding: 0,
2733                    resource: wgpu::BindingResource::TextureView(&view),
2734                },
2735                wgpu::BindGroupEntry {
2736                    binding: 1,
2737                    resource: wgpu::BindingResource::Sampler(&self.layer_sampler),
2738                },
2739            ],
2740        });
2741        let bind_linear = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
2742            label: Some("layer bind linear"),
2743            layout: &self.image_bind_layout_rgba,
2744            entries: &[
2745                wgpu::BindGroupEntry {
2746                    binding: 0,
2747                    resource: wgpu::BindingResource::TextureView(&view),
2748                },
2749                wgpu::BindGroupEntry {
2750                    binding: 1,
2751                    resource: wgpu::BindingResource::Sampler(&self.layer_sampler_linear),
2752                },
2753            ],
2754        });
2755        let depth_stencil_tex = self.device.create_texture(&wgpu::TextureDescriptor {
2756            label: Some("graphics layer depth-stencil"),
2757            size: wgpu::Extent3d {
2758                width: width.max(1),
2759                height: height.max(1),
2760                depth_or_array_layers: 1,
2761            },
2762            mip_level_count: 1,
2763            sample_count: 1,
2764            dimension: wgpu::TextureDimension::D2,
2765            format: wgpu::TextureFormat::Depth24PlusStencil8,
2766            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
2767            view_formats: &[],
2768        });
2769        let depth_stencil_view =
2770            depth_stencil_tex.create_view(&wgpu::TextureViewDescriptor::default());
2771        self.layer_pool.insert(
2772            layer_id,
2773            LayerTarget {
2774                texture: tex,
2775                view,
2776                bind,
2777                bind_linear,
2778                depth_stencil_tex,
2779                depth_stencil_view,
2780                width,
2781                height,
2782                rect_px: (rect.x, rect.y, rect.w, rect.h),
2783            },
2784        );
2785    }
2786
2787    fn atlas_bind_group_mask(&self) -> wgpu::BindGroup {
2788        self.device.create_bind_group(&wgpu::BindGroupDescriptor {
2789            label: Some("atlas bind"),
2790            layout: &self.text_bind_layout,
2791            entries: &[
2792                wgpu::BindGroupEntry {
2793                    binding: 0,
2794                    resource: wgpu::BindingResource::TextureView(&self.atlas_mask.view),
2795                },
2796                wgpu::BindGroupEntry {
2797                    binding: 1,
2798                    resource: wgpu::BindingResource::Sampler(&self.atlas_mask.sampler),
2799                },
2800            ],
2801        })
2802    }
2803
2804    fn atlas_bind_group_color(&self) -> wgpu::BindGroup {
2805        self.device.create_bind_group(&wgpu::BindGroupDescriptor {
2806            label: Some("atlas bind color"),
2807            layout: &self.text_bind_layout,
2808            entries: &[
2809                wgpu::BindGroupEntry {
2810                    binding: 0,
2811                    resource: wgpu::BindingResource::TextureView(&self.atlas_color.view),
2812                },
2813                wgpu::BindGroupEntry {
2814                    binding: 1,
2815                    resource: wgpu::BindingResource::Sampler(&self.atlas_color.sampler),
2816                },
2817            ],
2818        })
2819    }
2820
2821    fn upload_glyph_mask(&mut self, key: repose_text::GlyphKey, px: f32) -> Option<GlyphInfo> {
2822        let keyp = (key, px.to_bits());
2823        if let Some(info) = self.atlas_mask.map.get(&keyp) {
2824            return Some(*info);
2825        }
2826
2827        let gb = repose_text::rasterize(key, px)?;
2828        if gb.w == 0 || gb.h == 0 || gb.data.is_empty() {
2829            return None;
2830        }
2831
2832        let coverage = swash_to_a8_coverage(gb.content, &gb.data)?;
2833
2834        let w = gb.w.max(1);
2835        let h = gb.h.max(1);
2836
2837        if !self.alloc_space_mask(w, h) {
2838            self.grow_mask_and_rebuild();
2839        }
2840        if !self.alloc_space_mask(w, h) {
2841            return None;
2842        }
2843        let x = self.atlas_mask.next_x;
2844        let y = self.atlas_mask.next_y;
2845        self.atlas_mask.next_x += w + 1;
2846        self.atlas_mask.row_h = self.atlas_mask.row_h.max(h + 1);
2847
2848        let layout = wgpu::TexelCopyBufferLayout {
2849            offset: 0,
2850            bytes_per_row: Some(w),
2851            rows_per_image: Some(h),
2852        };
2853        let size = wgpu::Extent3d {
2854            width: w,
2855            height: h,
2856            depth_or_array_layers: 1,
2857        };
2858        self.queue.write_texture(
2859            wgpu::TexelCopyTextureInfoBase {
2860                texture: &self.atlas_mask.tex,
2861                mip_level: 0,
2862                origin: wgpu::Origin3d { x, y, z: 0 },
2863                aspect: wgpu::TextureAspect::All,
2864            },
2865            &coverage,
2866            layout,
2867            size,
2868        );
2869
2870        let info = GlyphInfo {
2871            u0: x as f32 / self.atlas_mask.size as f32,
2872            v0: y as f32 / self.atlas_mask.size as f32,
2873            u1: (x + w) as f32 / self.atlas_mask.size as f32,
2874            v1: (y + h) as f32 / self.atlas_mask.size as f32,
2875            w: w as f32,
2876            h: h as f32,
2877            bearing_x: 0.0,
2878            bearing_y: 0.0,
2879            advance: 0.0,
2880        };
2881        self.atlas_mask.map.insert(keyp, info);
2882        Some(info)
2883    }
2884
2885    fn upload_glyph_color(&mut self, key: repose_text::GlyphKey, px: f32) -> Option<GlyphInfo> {
2886        let keyp = (key, px.to_bits());
2887        if let Some(info) = self.atlas_color.map.get(&keyp) {
2888            return Some(*info);
2889        }
2890        let gb = repose_text::rasterize(key, px)?;
2891        if !matches!(gb.content, repose_text::SwashContent::Color) {
2892            return None;
2893        }
2894        let w = gb.w.max(1);
2895        let h = gb.h.max(1);
2896        if !self.alloc_space_color(w, h) {
2897            self.grow_color_and_rebuild();
2898        }
2899        if !self.alloc_space_color(w, h) {
2900            return None;
2901        }
2902        let x = self.atlas_color.next_x;
2903        let y = self.atlas_color.next_y;
2904        self.atlas_color.next_x += w + 1;
2905        self.atlas_color.row_h = self.atlas_color.row_h.max(h + 1);
2906
2907        let layout = wgpu::TexelCopyBufferLayout {
2908            offset: 0,
2909            bytes_per_row: Some(w * 4),
2910            rows_per_image: Some(h),
2911        };
2912        let size = wgpu::Extent3d {
2913            width: w,
2914            height: h,
2915            depth_or_array_layers: 1,
2916        };
2917        self.queue.write_texture(
2918            wgpu::TexelCopyTextureInfoBase {
2919                texture: &self.atlas_color.tex,
2920                mip_level: 0,
2921                origin: wgpu::Origin3d { x, y, z: 0 },
2922                aspect: wgpu::TextureAspect::All,
2923            },
2924            &gb.data,
2925            layout,
2926            size,
2927        );
2928        let info = GlyphInfo {
2929            u0: x as f32 / self.atlas_color.size as f32,
2930            v0: y as f32 / self.atlas_color.size as f32,
2931            u1: (x + w) as f32 / self.atlas_color.size as f32,
2932            v1: (y + h) as f32 / self.atlas_color.size as f32,
2933            w: w as f32,
2934            h: h as f32,
2935            bearing_x: 0.0,
2936            bearing_y: 0.0,
2937            advance: 0.0,
2938        };
2939        self.atlas_color.map.insert(keyp, info);
2940        Some(info)
2941    }
2942
2943    fn alloc_space_mask(&mut self, w: u32, h: u32) -> bool {
2944        if self.atlas_mask.next_x + w + 1 >= self.atlas_mask.size {
2945            self.atlas_mask.next_x = 1;
2946            self.atlas_mask.next_y += self.atlas_mask.row_h + 1;
2947            self.atlas_mask.row_h = 0;
2948        }
2949        if self.atlas_mask.next_y + h + 1 >= self.atlas_mask.size {
2950            return false;
2951        }
2952        true
2953    }
2954
2955    fn grow_mask_and_rebuild(&mut self) {
2956        let new_size = (self.atlas_mask.size * 2).min(4096);
2957        if new_size == self.atlas_mask.size {
2958            return;
2959        }
2960        let tex = self.device.create_texture(&wgpu::TextureDescriptor {
2961            label: Some("glyph atlas A8 (grown)"),
2962            size: wgpu::Extent3d {
2963                width: new_size,
2964                height: new_size,
2965                depth_or_array_layers: 1,
2966            },
2967            mip_level_count: 1,
2968            sample_count: 1,
2969            dimension: wgpu::TextureDimension::D2,
2970            format: wgpu::TextureFormat::R8Unorm,
2971            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
2972            view_formats: &[],
2973        });
2974        self.atlas_mask.tex = tex;
2975        self.atlas_mask.view = self
2976            .atlas_mask
2977            .tex
2978            .create_view(&wgpu::TextureViewDescriptor::default());
2979        self.atlas_mask.size = new_size;
2980        self.atlas_mask.next_x = 1;
2981        self.atlas_mask.next_y = 1;
2982        self.atlas_mask.row_h = 0;
2983        let keys: Vec<(repose_text::GlyphKey, u32)> = self.atlas_mask.map.keys().copied().collect();
2984        self.atlas_mask.map.clear();
2985        for (k, px_bits) in keys {
2986            let _ = self.upload_glyph_mask(k, f32::from_bits(px_bits));
2987        }
2988    }
2989
2990    fn alloc_space_color(&mut self, w: u32, h: u32) -> bool {
2991        if self.atlas_color.next_x + w + 1 >= self.atlas_color.size {
2992            self.atlas_color.next_x = 1;
2993            self.atlas_color.next_y += self.atlas_color.row_h + 1;
2994            self.atlas_color.row_h = 0;
2995        }
2996        if self.atlas_color.next_y + h + 1 >= self.atlas_color.size {
2997            return false;
2998        }
2999        true
3000    }
3001
3002    fn grow_color_and_rebuild(&mut self) {
3003        let new_size = (self.atlas_color.size * 2).min(4096);
3004        if new_size == self.atlas_color.size {
3005            return;
3006        }
3007        let tex = self.device.create_texture(&wgpu::TextureDescriptor {
3008            label: Some("glyph atlas RGBA (grown)"),
3009            size: wgpu::Extent3d {
3010                width: new_size,
3011                height: new_size,
3012                depth_or_array_layers: 1,
3013            },
3014            mip_level_count: 1,
3015            sample_count: 1,
3016            dimension: wgpu::TextureDimension::D2,
3017            format: wgpu::TextureFormat::Rgba8UnormSrgb,
3018            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
3019            view_formats: &[],
3020        });
3021        self.atlas_color.tex = tex;
3022        self.atlas_color.view = self
3023            .atlas_color
3024            .tex
3025            .create_view(&wgpu::TextureViewDescriptor::default());
3026        self.atlas_color.size = new_size;
3027        self.atlas_color.next_x = 1;
3028        self.atlas_color.next_y = 1;
3029        self.atlas_color.row_h = 0;
3030        let keys: Vec<(repose_text::GlyphKey, u32)> =
3031            self.atlas_color.map.keys().copied().collect();
3032        self.atlas_color.map.clear();
3033        for (k, px_bits) in keys {
3034            let _ = self.upload_glyph_color(k, f32::from_bits(px_bits));
3035        }
3036    }
3037}
3038
3039fn brush_to_instance_fields(brush: &Brush) -> (u32, [f32; 4], [f32; 4], [f32; 2], [f32; 2]) {
3040    match brush {
3041        Brush::Solid(c) => (
3042            0u32,
3043            c.to_linear(),
3044            [0.0, 0.0, 0.0, 0.0],
3045            [0.0, 0.0],
3046            [0.0, 1.0],
3047        ),
3048        Brush::Linear {
3049            start,
3050            end,
3051            start_color,
3052            end_color,
3053        } => (
3054            1u32,
3055            start_color.to_linear(),
3056            end_color.to_linear(),
3057            [start.x, start.y],
3058            [end.x, end.y],
3059        ),
3060        _ => (0u32, [0.0; 4], [0.0; 4], [0.0; 2], [0.0; 2]),
3061    }
3062}
3063
3064fn brush_to_solid_color(brush: &Brush) -> [f32; 4] {
3065    match brush {
3066        Brush::Solid(c) => c.to_linear(),
3067        Brush::Linear { start_color, .. } => start_color.to_linear(),
3068        _ => [0.0; 4],
3069    }
3070}
3071
3072fn init_atlas_mask(device: &wgpu::Device) -> AtlasA8 {
3073    let size = 1024u32;
3074    let tex = device.create_texture(&wgpu::TextureDescriptor {
3075        label: Some("glyph atlas A8"),
3076        size: wgpu::Extent3d {
3077            width: size,
3078            height: size,
3079            depth_or_array_layers: 1,
3080        },
3081        mip_level_count: 1,
3082        sample_count: 1,
3083        dimension: wgpu::TextureDimension::D2,
3084        format: wgpu::TextureFormat::R8Unorm,
3085        usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
3086        view_formats: &[],
3087    });
3088    let view = tex.create_view(&wgpu::TextureViewDescriptor::default());
3089    let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
3090        label: Some("glyph atlas sampler A8"),
3091        address_mode_u: wgpu::AddressMode::ClampToEdge,
3092        address_mode_v: wgpu::AddressMode::ClampToEdge,
3093        address_mode_w: wgpu::AddressMode::ClampToEdge,
3094        mag_filter: wgpu::FilterMode::Linear,
3095        min_filter: wgpu::FilterMode::Linear,
3096        mipmap_filter: wgpu::MipmapFilterMode::Linear,
3097        ..Default::default()
3098    });
3099
3100    AtlasA8 {
3101        tex,
3102        view,
3103        sampler,
3104        size,
3105        next_x: 1,
3106        next_y: 1,
3107        row_h: 0,
3108        map: HashMap::new(),
3109    }
3110}
3111
3112fn init_atlas_color(device: &wgpu::Device) -> AtlasRGBA {
3113    let size = 1024u32;
3114    let tex = device.create_texture(&wgpu::TextureDescriptor {
3115        label: Some("glyph atlas RGBA"),
3116        size: wgpu::Extent3d {
3117            width: size,
3118            height: size,
3119            depth_or_array_layers: 1,
3120        },
3121        mip_level_count: 1,
3122        sample_count: 1,
3123        dimension: wgpu::TextureDimension::D2,
3124        format: wgpu::TextureFormat::Rgba8UnormSrgb,
3125        usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
3126        view_formats: &[],
3127    });
3128    let view = tex.create_view(&wgpu::TextureViewDescriptor::default());
3129    let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
3130        label: Some("glyph atlas sampler RGBA"),
3131        address_mode_u: wgpu::AddressMode::ClampToEdge,
3132        address_mode_v: wgpu::AddressMode::ClampToEdge,
3133        address_mode_w: wgpu::AddressMode::ClampToEdge,
3134        mag_filter: wgpu::FilterMode::Linear,
3135        min_filter: wgpu::FilterMode::Linear,
3136        mipmap_filter: wgpu::MipmapFilterMode::Linear,
3137        ..Default::default()
3138    });
3139    AtlasRGBA {
3140        tex,
3141        view,
3142        sampler,
3143        size,
3144        next_x: 1,
3145        next_y: 1,
3146        row_h: 0,
3147        map: HashMap::new(),
3148    }
3149}
3150
3151#[cfg(feature = "winit-surface")]
3152impl RenderBackend for WgpuSurfaceBackend {
3153    fn configure_surface(&mut self, width: u32, height: u32) {
3154        if width == 0 || height == 0 {
3155            return;
3156        }
3157        self.renderer.output_width = width;
3158        self.renderer.output_height = height;
3159        if let Some(ref mut config) = self.surface_config {
3160            config.width = width;
3161            config.height = height;
3162        }
3163        if let (Some(surface), Some(config)) = (self.surface.as_ref(), self.surface_config.as_ref()) {
3164            surface.configure(&self.renderer.device, config);
3165        }
3166        self.renderer.recreate_msaa_and_depth_stencil();
3167        self.renderer.recreate_working_space_texture();
3168    }
3169
3170    fn frame(&mut self, scene: &Scene, _glyph_cfg: GlyphRasterConfig) {
3171        let surface = self.surface.as_ref().expect("WgpuSurfaceBackend::frame() requires a surface (use from_device + render_to_view instead)");
3172        let surface_config = self.surface_config.as_ref().expect("surface_config required for frame()");
3173
3174        self.renderer.frame_index = self.renderer.frame_index.wrapping_add(1);
3175        self.renderer.slug_cache.next_frame();
3176
3177        if self.renderer.output_width == 0 || self.renderer.output_height == 0 {
3178            return;
3179        }
3180
3181        let mut retries = 0u32;
3182        const MAX_RETRIES: u32 = 4;
3183        let frame = loop {
3184            match surface.get_current_texture() {
3185                wgpu::CurrentSurfaceTexture::Success(f) => break f,
3186                wgpu::CurrentSurfaceTexture::Suboptimal(f) => {
3187                    log::warn!("suboptimal surface; reconfiguring");
3188                    surface.configure(&self.renderer.device, surface_config);
3189                    break f;
3190                }
3191                wgpu::CurrentSurfaceTexture::Outdated => {
3192                    retries += 1;
3193                    if retries >= MAX_RETRIES {
3194                        log::warn!("surface outdated persisted after {MAX_RETRIES} retries; skipping frame");
3195                        return;
3196                    }
3197                    log::warn!("surface outdated; reconfiguring");
3198                    surface.configure(&self.renderer.device, surface_config);
3199                }
3200                wgpu::CurrentSurfaceTexture::Lost => {
3201                    retries += 1;
3202                    if retries >= MAX_RETRIES {
3203                        log::warn!("surface lost persisted after {MAX_RETRIES} retries; skipping frame");
3204                        return;
3205                    }
3206                    log::warn!("surface lost; reconfiguring");
3207                    surface.configure(&self.renderer.device, surface_config);
3208                }
3209                wgpu::CurrentSurfaceTexture::Timeout | wgpu::CurrentSurfaceTexture::Occluded => {
3210                    request_frame();
3211                    return;
3212                }
3213                wgpu::CurrentSurfaceTexture::Validation => {
3214                    retries += 1;
3215                    if retries >= MAX_RETRIES {
3216                        log::warn!("surface validation persisted after {MAX_RETRIES} retries; skipping frame");
3217                        return;
3218                    }
3219                    surface.configure(&self.renderer.device, surface_config);
3220                }
3221            }
3222        };
3223
3224        let swap_view = frame.texture.create_view(&wgpu::TextureViewDescriptor::default());
3225        let mut encoder = self.renderer.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
3226            label: Some("frame encoder"),
3227        });
3228
3229        let clear_color = Some([
3230            scene.clear_color.0 as f64 / 255.0,
3231            scene.clear_color.1 as f64 / 255.0,
3232            scene.clear_color.2 as f64 / 255.0,
3233            scene.clear_color.3 as f64 / 255.0,
3234        ]);
3235
3236        self.renderer.render_scene_to_encoder(scene, &mut encoder, &swap_view, clear_color);
3237
3238        self.renderer.queue.submit(std::iter::once(encoder.finish()));
3239        if let Err(e) = catch_unwind(AssertUnwindSafe(|| self.renderer.queue.present(frame))) {
3240            log::warn!("queue.present panicked: {:?}", e);
3241        }
3242    }
3243}
3244
3245impl WgpuSceneRenderer {
3246    pub fn render_scene_to_encoder(
3247        &mut self,
3248        scene: &Scene,
3249        encoder: &mut wgpu::CommandEncoder,
3250        target_view: &wgpu::TextureView,
3251        clear_color_override: Option<[f64; 4]>,
3252    ) {
3253        fn to_ndc(x: f32, y: f32, w: f32, h: f32, fb_w: f32, fb_h: f32) -> [f32; 4] {
3254            let x0 = (x / fb_w) * 2.0 - 1.0;
3255            let y0 = 1.0 - (y / fb_h) * 2.0;
3256            let x1 = ((x + w) / fb_w) * 2.0 - 1.0;
3257            let y1 = 1.0 - ((y + h) / fb_h) * 2.0;
3258            let min_x = x0.min(x1);
3259            let min_y = y0.min(y1);
3260            let w_ndc = (x1 - x0).abs();
3261            let h_ndc = (y1 - y0).abs();
3262            [min_x, min_y, w_ndc, h_ndc]
3263        }
3264
3265        /// Convert a local-space rect + transform to NDC center-based position+size and rotation.
3266        fn rect_to_instance_ndc(
3267            rect: repose_core::Rect,
3268            transform: &Transform,
3269            fb_w: f32,
3270            fb_h: f32,
3271        ) -> ([f32; 4], [f32; 2]) {
3272            let cx = rect.x + rect.w * 0.5;
3273            let cy = rect.y + rect.h * 0.5;
3274
3275            // Apply full transform to center
3276            let sx = cx * transform.scale_x;
3277            let sy = cy * transform.scale_y;
3278            let cos_a = transform.rotate.cos();
3279            let sin_a = transform.rotate.sin();
3280            let tx = sx * cos_a - sy * sin_a + transform.translate_x;
3281            let ty = sx * sin_a + sy * cos_a + transform.translate_y;
3282
3283            // NDC center
3284            let ndc_cx = (tx / fb_w) * 2.0 - 1.0;
3285            let ndc_cy = 1.0 - (ty / fb_h) * 2.0;
3286            // NDC size (after scale only, no rotation - rotation is done in shader)
3287            let ndc_w = (rect.w * transform.scale_x / fb_w) * 2.0;
3288            let ndc_h = (rect.h * transform.scale_y / fb_h) * 2.0;
3289
3290            ([ndc_cx, ndc_cy, ndc_w, ndc_h], [cos_a, sin_a])
3291        }
3292
3293        fn to_scissor(r: &repose_core::Rect, fb_w: u32, fb_h: u32) -> (u32, u32, u32, u32) {
3294            let mut x = r.x.floor() as i64;
3295            let mut y = r.y.floor() as i64;
3296            let fb_wi = fb_w as i64;
3297            let fb_hi = fb_h as i64;
3298            x = x.clamp(0, fb_wi.saturating_sub(1));
3299            y = y.clamp(0, fb_hi.saturating_sub(1));
3300            let w_req = r.w.ceil().max(1.0) as i64;
3301            let h_req = r.h.ceil().max(1.0) as i64;
3302            let w = (w_req).min(fb_wi - x).max(1);
3303            let h = (h_req).min(fb_hi - y).max(1);
3304            (x as u32, y as u32, w as u32, h as u32)
3305        }
3306
3307        let fb_w = self.output_width as f32;
3308        let fb_h = self.output_height as f32;
3309
3310        let mut passes: Vec<Pass> = Vec::with_capacity(1);
3311        let clear_color = clear_color_override.unwrap_or_else(|| {
3312            [
3313                scene.clear_color.0 as f64 / 255.0,
3314                scene.clear_color.1 as f64 / 255.0,
3315                scene.clear_color.2 as f64 / 255.0,
3316                scene.clear_color.3 as f64 / 255.0,
3317            ]
3318        });
3319        let mut current_pass: Pass = Pass {
3320            target: PassTarget::Surface,
3321            initial_scissor: (0, 0, self.output_width, self.output_height),
3322            clear_color: Some([
3323                clear_color[0] as f32,
3324                clear_color[1] as f32,
3325                clear_color[2] as f32,
3326                clear_color[3] as f32,
3327            ]),
3328            cmds: Vec::with_capacity(scene.nodes.len()),
3329        };
3330        let mut target_stack: Vec<PassTarget> = Vec::new();
3331        let mut layer_alphas: Vec<(u32, f32, (u32, u32, u32, u32))> = Vec::new();
3332        let mut layer_blurs: Vec<(u32, f32, f32)> = Vec::new();
3333        let mut current_target_size: (f32, f32) = (fb_w, fb_h);
3334
3335        struct Batch {
3336            rects: Vec<RectInstance>,
3337            borders: Vec<BorderInstance>,
3338            ellipses: Vec<EllipseInstance>,
3339            e_borders: Vec<EllipseBorderInstance>,
3340            arcs: Vec<ArcInstance>,
3341            masks: Vec<GlyphInstance>,
3342            colors: Vec<GlyphInstance>,
3343            nv12s: Vec<Nv12Instance>,
3344        }
3345
3346        impl Batch {
3347            fn new() -> Self {
3348                Self {
3349                    rects: vec![],
3350                    borders: vec![],
3351                    ellipses: vec![],
3352                    e_borders: vec![],
3353                    arcs: vec![],
3354                    masks: vec![],
3355                    colors: vec![],
3356                    nv12s: vec![],
3357                }
3358            }
3359
3360            fn is_empty(&self) -> bool {
3361                self.rects.is_empty()
3362                    && self.borders.is_empty()
3363                    && self.ellipses.is_empty()
3364                    && self.e_borders.is_empty()
3365                    && self.arcs.is_empty()
3366                    && self.masks.is_empty()
3367                    && self.colors.is_empty()
3368                    && self.nv12s.is_empty()
3369            }
3370
3371            fn flush(
3372                &mut self,
3373                pipes: (
3374                    &mut InstancedPipe<RectInstance>,
3375                    &mut InstancedPipe<BorderInstance>,
3376                    &mut InstancedPipe<EllipseInstance>,
3377                    &mut InstancedPipe<EllipseBorderInstance>,
3378                    &mut InstancedPipe<ArcInstance>,
3379                ),
3380                glyph_pipes: (
3381                    &mut InstancedPipe<GlyphInstance>,
3382                    &mut InstancedPipe<GlyphInstance>,
3383                ),
3384                nv12_pipe: &mut InstancedPipe<Nv12Instance>,
3385                device: &wgpu::Device,
3386                queue: &wgpu::Queue,
3387                cmds: &mut Vec<Cmd>,
3388            ) {
3389                let (rects, borders, ellipses, e_borders, arcs) = pipes;
3390                let (masks, colors) = glyph_pipes;
3391
3392                macro_rules! flush_one {
3393                    ($buf:ident, $pipe:expr, $variant:ident) => {
3394                        if !self.$buf.is_empty() {
3395                            if let Some((off, cnt)) = $pipe.upload(device, queue, &self.$buf) {
3396                                cmds.push(Cmd::$variant { off, cnt });
3397                            }
3398                            self.$buf.clear();
3399                        }
3400                    };
3401                }
3402
3403                flush_one!(rects, rects, Rect);
3404                flush_one!(borders, borders, Border);
3405                flush_one!(ellipses, ellipses, Ellipse);
3406                flush_one!(e_borders, e_borders, EllipseBorder);
3407                flush_one!(arcs, arcs, Arc);
3408                flush_one!(masks, masks, GlyphsMask);
3409                flush_one!(colors, colors, GlyphsColor);
3410
3411                if !self.nv12s.is_empty() {
3412                    if let Some((off, cnt)) = nv12_pipe.upload(device, queue, &self.nv12s) {
3413                        let _ = (off, cnt);
3414                    }
3415                    self.nv12s.clear();
3416                }
3417            }
3418        }
3419
3420        self.rects.reset();
3421        self.borders.reset();
3422        self.ellipses.reset();
3423        self.ellipse_borders.reset();
3424        self.arcs.reset();
3425        self.glyph_mask.reset();
3426        self.glyph_color.reset();
3427        self.clip_ring.reset();
3428        self.blur_ring.reset();
3429        self.nv12.reset();
3430
3431        self.slug_ring.reset();
3432        let mut batch = Batch::new();
3433        let mut slug_verts_local: Vec<slug::TessVertex> = Vec::new();
3434        let mut transform_stack: Vec<Transform> = vec![Transform::identity()];
3435        let mut scissor_stack: Vec<repose_core::Rect> = Vec::with_capacity(8);
3436        let root_clip_rect = repose_core::Rect {
3437            x: 0.0,
3438            y: 0.0,
3439            w: fb_w,
3440            h: fb_h,
3441        };
3442
3443        let mut current_prim: Option<&'static str> = None;
3444
3445        macro_rules! flush_if_prim_changed {
3446            ($prim:literal, $pipe:expr) => {
3447                if current_prim != Some($prim) {
3448                    flush_batch!();
3449                    current_prim = Some($prim);
3450                }
3451            };
3452        }
3453
3454        macro_rules! flush_batch {
3455            () => {
3456                if !batch.is_empty() {
3457                    batch.flush(
3458                        (
3459                            &mut self.rects,
3460                            &mut self.borders,
3461                            &mut self.ellipses,
3462                            &mut self.ellipse_borders,
3463                            &mut self.arcs,
3464                        ),
3465                        (&mut self.glyph_mask, &mut self.glyph_color),
3466                        &mut self.nv12,
3467                        &self.device,
3468                        &self.queue,
3469                        &mut current_pass.cmds,
3470                    )
3471                }
3472            };
3473        }
3474        for node in &scene.nodes {
3475            let t_identity = Transform::identity();
3476            let current_transform = transform_stack.last().unwrap_or(&t_identity);
3477
3478            match node {
3479                SceneNode::Rect {
3480                    rect,
3481                    brush,
3482                    radius,
3483                } => {
3484                    flush_if_prim_changed!("rect", &self.rects);
3485                    let (ndc, sin_cos) = rect_to_instance_ndc(
3486                        *rect,
3487                        current_transform,
3488                        current_target_size.0,
3489                        current_target_size.1,
3490                    );
3491                    let (brush_type, color0, color1, grad_start, grad_end) =
3492                        brush_to_instance_fields(brush);
3493                    batch.rects.push(RectInstance {
3494                        xywh: ndc,
3495                        radii: *radius,
3496                        brush_type,
3497                        _pad: [0.0; 3],
3498                        color0,
3499                        color1,
3500                        grad_start,
3501                        grad_end,
3502                        sin_cos,
3503                    });
3504                }
3505                SceneNode::Border {
3506                    rect,
3507                    color,
3508                    width,
3509                    radius,
3510                } => {
3511                    flush_if_prim_changed!("border", &self.borders);
3512                    let (ndc, sin_cos) = rect_to_instance_ndc(
3513                        *rect,
3514                        current_transform,
3515                        current_target_size.0,
3516                        current_target_size.1,
3517                    );
3518                    batch.borders.push(BorderInstance {
3519                        xywh: ndc,
3520                        radii: *radius,
3521                        stroke: *width,
3522                        color: color.to_linear(),
3523                        sin_cos,
3524                    });
3525                }
3526                SceneNode::Ellipse { rect, brush } => {
3527                    flush_if_prim_changed!("ellipse", &self.ellipses);
3528                    let (ndc, sin_cos) = rect_to_instance_ndc(
3529                        *rect,
3530                        current_transform,
3531                        current_target_size.0,
3532                        current_target_size.1,
3533                    );
3534                    let color = brush_to_solid_color(brush);
3535                    batch.ellipses.push(EllipseInstance {
3536                        xywh: ndc,
3537                        color,
3538                        sin_cos,
3539                    });
3540                }
3541                SceneNode::EllipseBorder { rect, color, width } => {
3542                    flush_if_prim_changed!("ellipse_border", &self.ellipse_borders);
3543                    let (ndc, sin_cos) = rect_to_instance_ndc(
3544                        *rect,
3545                        current_transform,
3546                        current_target_size.0,
3547                        current_target_size.1,
3548                    );
3549                    let pad_px = *width * 0.5 + 2.0;
3550                    let pad = (pad_px / current_target_size.0) * 2.0;
3551                    batch.e_borders.push(EllipseBorderInstance {
3552                        xywh: ndc,
3553                        stroke: *width,
3554                        pad,
3555                        color: color.to_linear(),
3556                        sin_cos,
3557                    });
3558                }
3559                SceneNode::Arc {
3560                    rect,
3561                    start_angle,
3562                    sweep_angle,
3563                    stroke_width,
3564                    color,
3565                    cap,
3566                } => {
3567                    flush_if_prim_changed!("arc", &self.arcs);
3568                    let (ndc, sin_cos) = rect_to_instance_ndc(
3569                        *rect,
3570                        current_transform,
3571                        current_target_size.0,
3572                        current_target_size.1,
3573                    );
3574                    let pad_px = *stroke_width * 0.5 + 2.0;
3575                    let pad = (pad_px / current_target_size.0) * 2.0;
3576                    let cap_val = match cap {
3577                        StrokeCap::Butt => 0.0,
3578                        StrokeCap::Round => 1.0,
3579                        StrokeCap::Square => 2.0,
3580                    };
3581                    batch.arcs.push(ArcInstance {
3582                        xywh: ndc,
3583                        start_angle: *start_angle,
3584                        sweep_angle: *sweep_angle,
3585                        stroke: *stroke_width,
3586                        pad,
3587                        color: color.to_linear(),
3588                        sin_cos,
3589                        cap: cap_val,
3590                    });
3591                }
3592                SceneNode::Text {
3593                    rect,
3594                    text,
3595                    color,
3596                    size,
3597                    font_family,
3598                    text_align: _,
3599                    font_weight,
3600                    font_style,
3601                    text_decoration,
3602                    letter_spacing,
3603                    line_height: _,
3604                    extra_style,
3605                    url: _,
3606                    font_variation_settings,
3607                } => {
3608                    flush_batch!(); // flush any prior primitives
3609
3610                    let px = *size;
3611                    let lh_ratio = rect.h / px;
3612                    let fw = font_weight.0;
3613                    let fs = if *font_style == FontStyle::Italic {
3614                        1
3615                    } else {
3616                        0
3617                    };
3618                    let shaped = repose_text::shape_line(
3619                        text.as_ref(),
3620                        px,
3621                        lh_ratio,
3622                        *font_family,
3623                        fw,
3624                        fs,
3625                        *letter_spacing,
3626                        font_variation_settings.as_deref(),
3627                    );
3628                    let baseline_y = shaped.first().map(|g| rect.y + g.y);
3629
3630                    let cos_a = current_transform.rotate.cos();
3631                    let sin_a = current_transform.rotate.sin();
3632                    let has_rotation = current_transform.rotate != 0.0;
3633
3634                    // For rotated text, the pivot is the center of the text rect.
3635                    let pivot_x = rect.x + rect.w * 0.5;
3636                    let pivot_y = rect.y + rect.h * 0.5;
3637
3638                    // Helper: compute NDC for a glyph rect, handling rotation correctly.
3639                    let make_glyph_instance =
3640                        |gx: f32, gy: f32, gw: f32, gh: f32| -> ([f32; 4], [f32; 2]) {
3641                            if has_rotation {
3642                                let corners =
3643                                    [(gx, gy), (gx + gw, gy), (gx + gw, gy + gh), (gx, gy + gh)];
3644                                let mut min_x = f32::MAX;
3645                                let mut max_x = f32::MIN;
3646                                let mut min_y = f32::MAX;
3647                                let mut max_y = f32::MIN;
3648                                for &(x, y) in &corners {
3649                                    let dx = x - pivot_x;
3650                                    let dy = y - pivot_y;
3651                                    let rx = pivot_x + dx * cos_a - dy * sin_a;
3652                                    let ry = pivot_y + dx * sin_a + dy * cos_a;
3653                                    min_x = min_x.min(rx);
3654                                    max_x = max_x.max(rx);
3655                                    min_y = min_y.min(ry);
3656                                    max_y = max_y.max(ry);
3657                                }
3658                                let bb_w = max_x - min_x;
3659                                let bb_h = max_y - min_y;
3660                                let ndc_tl = to_ndc(
3661                                    min_x,
3662                                    min_y,
3663                                    bb_w,
3664                                    bb_h,
3665                                    current_target_size.0,
3666                                    current_target_size.1,
3667                                );
3668                                let ndc = [
3669                                    ndc_tl[0] + ndc_tl[2] * 0.5,
3670                                    ndc_tl[1] + ndc_tl[3] * 0.5,
3671                                    ndc_tl[2],
3672                                    ndc_tl[3],
3673                                ];
3674                                (ndc, [cos_a, sin_a])
3675                            } else {
3676                                // Only safe at 1:1 scale (no zoom animations active).
3677                                let (sx, sy) = if current_transform.scale_x == 1.0
3678                                    && current_transform.scale_y == 1.0
3679                                {
3680                                    (gx.round(), gy.round())
3681                                } else {
3682                                    (gx, gy)
3683                                };
3684                                rect_to_instance_ndc(
3685                                    repose_core::Rect {
3686                                        x: sx,
3687                                        y: sy,
3688                                        w: gw,
3689                                        h: gh,
3690                                    },
3691                                    current_transform,
3692                                    current_target_size.0,
3693                                    current_target_size.1,
3694                                )
3695                            }
3696                        };
3697
3698                    let baseline_shift_y: f32 = px * extra_style.baseline_shift.0;
3699
3700                    let (
3701                        is_stroke,
3702                        stroke_width,
3703                        stroke_cap,
3704                        stroke_join,
3705                        stroke_miter,
3706                        stroke_path_effect,
3707                    ) = match &extra_style.draw_style {
3708                        repose_core::DrawStyle::Stroke {
3709                            width,
3710                            cap,
3711                            join,
3712                            miter,
3713                            path_effect,
3714                        } => (true, *width, *cap, *join, *miter, path_effect.clone()),
3715                        _ => (
3716                            false,
3717                            0.0,
3718                            repose_core::StrokeCap::Butt,
3719                            repose_core::StrokeJoin::Miter,
3720                            4.0,
3721                            None,
3722                        ),
3723                    };
3724                    let stroke_tess_key = if is_stroke {
3725                        Some(slug::StrokeTessKey::new(
3726                            stroke_width,
3727                            stroke_cap,
3728                            stroke_join,
3729                            stroke_miter,
3730                            &stroke_path_effect,
3731                        ))
3732                    } else {
3733                        None
3734                    };
3735
3736                    for sg in shaped {
3737                        let gx = rect.x + sg.x + sg.bearing_x;
3738                        let gy = rect.y + sg.y - sg.bearing_y + baseline_shift_y;
3739
3740                        // Vector glyph path: tessellated geometry with MSAA.
3741                        if self.slug_enabled {
3742                            let ck = repose_text::lookup_cache_key(sg.key, sg.px);
3743                            if let Some(ref ck) = ck {
3744                                // Check if cached.
3745                                let need_tessellate = self.slug_cache.get(ck).map_or(true, |g| {
3746                                    if is_stroke {
3747                                        let key = stroke_tess_key.as_ref().unwrap();
3748                                        !g.stroke_variants.contains_key(key)
3749                                    } else {
3750                                        g.fill_vertices.is_none()
3751                                    }
3752                                });
3753                                if need_tessellate {
3754                                    if let Some((ck2, commands)) =
3755                                        repose_text::lookup_and_extract_outline(sg.key, sg.px)
3756                                    {
3757                                        let font_size_px = f32::from_bits(ck2.font_size_bits);
3758                                        if is_stroke {
3759                                            self.slug_cache.get_or_insert_stroke(
3760                                                ck2,
3761                                                font_size_px,
3762                                                &commands,
3763                                                stroke_width,
3764                                                stroke_cap,
3765                                                stroke_join,
3766                                                stroke_miter,
3767                                                &stroke_path_effect,
3768                                            );
3769                                        } else {
3770                                            self.slug_cache.get_or_insert(
3771                                                ck2,
3772                                                font_size_px,
3773                                                &commands,
3774                                            );
3775                                        }
3776                                    }
3777                                } else {
3778                                    self.slug_cache.touch(ck);
3779                                }
3780                            }
3781                            if let Some(entry) = ck.as_ref().and_then(|ck| self.slug_cache.get(ck))
3782                            {
3783                                let ox = rect.x + sg.x;
3784                                let oy = rect.y + sg.y + baseline_shift_y;
3785                                let scx = current_transform.scale_x;
3786                                let scy = current_transform.scale_y;
3787                                let ttx = current_transform.translate_x;
3788                                let tty = current_transform.translate_y;
3789
3790                                let tf = |x: f32, y: f32| -> (f32, f32) {
3791                                    if has_rotation {
3792                                        let dx = x - pivot_x;
3793                                        let dy = y - pivot_y;
3794                                        let rx = pivot_x + dx * cos_a - dy * sin_a;
3795                                        let ry = pivot_y + dx * sin_a + dy * cos_a;
3796                                        (rx, ry)
3797                                    } else {
3798                                        (x * scx + ttx, y * scy + tty)
3799                                    }
3800                                };
3801
3802                                let tw = current_target_size.0;
3803                                let th = current_target_size.1;
3804
3805                                let verts = if is_stroke {
3806                                    let key = stroke_tess_key.as_ref().unwrap();
3807                                    entry
3808                                        .stroke_variants
3809                                        .get(key)
3810                                        .map(|v| v.as_slice())
3811                                        .unwrap_or(&[])
3812                                } else {
3813                                    entry.fill_vertices.as_deref().unwrap_or(&[])
3814                                };
3815
3816                                for &v in verts {
3817                                    let (sx, sy) = tf(ox + v[0] * px, oy - v[1] * px);
3818                                    let ndc_x = sx / tw * 2.0 - 1.0;
3819                                    let ndc_y = -(sy / th) * 2.0 + 1.0;
3820                                    slug_verts_local.push(slug::TessVertex {
3821                                        ndc_pos: [ndc_x, ndc_y],
3822                                        color: color.to_linear(),
3823                                    });
3824                                }
3825
3826                                if is_stroke {
3827                                    // Stroke glyphs cannot use atlas fallback...
3828                                    continue;
3829                                }
3830                                continue;
3831                            }
3832                        }
3833
3834                        // Don't use atlas fallback for strokes too
3835                        if is_stroke {
3836                            continue;
3837                        }
3838
3839                        // Atlas fallback: color emoji + failed slug extraction
3840                        if let Some(info) = self.upload_glyph_color(sg.key, sg.px) {
3841                            let (ndc, sin_cos) = make_glyph_instance(gx, gy, info.w, info.h);
3842                            batch.colors.push(GlyphInstance {
3843                                xywh: ndc,
3844                                uv: [info.u0, info.v1, info.u1, info.v0],
3845                                color: color.to_linear(),
3846                                sin_cos,
3847                            });
3848                        } else if let Some(info) = self.upload_glyph_mask(sg.key, sg.px) {
3849                            let (ndc, sin_cos) = make_glyph_instance(gx, gy, info.w, info.h);
3850                            batch.masks.push(GlyphInstance {
3851                                xywh: ndc,
3852                                uv: [info.u0, info.v1, info.u1, info.v0],
3853                                color: color.to_linear(),
3854                                sin_cos,
3855                            });
3856                        }
3857                    }
3858
3859                    // Upload slug vertices if any
3860                    if !slug_verts_local.is_empty() {
3861                        let bytes = bytemuck::cast_slice(&slug_verts_local);
3862                        self.slug_ring.grow_to_fit(&self.device, bytes.len() as u64);
3863                        let (off, _) = self.slug_ring.alloc_write(&self.queue, bytes);
3864                        current_pass.cmds.push(Cmd::GlyphsVector {
3865                            off,
3866                            cnt: slug_verts_local.len() as u32,
3867                        });
3868                        slug_verts_local.clear();
3869                    }
3870
3871                    // Text decoration: underline / strikethrough
3872                    if (text_decoration.underline || text_decoration.strikethrough)
3873                        && let Some(baseline_y) = baseline_y
3874                    {
3875                        flush_batch!();
3876                        current_prim = Some("rect");
3877                        let deco_color = text_decoration.color.unwrap_or(*color);
3878                        let thickness = (px * 0.07).max(1.0);
3879
3880                        if text_decoration.underline {
3881                            let dy = baseline_y + px * 0.1;
3882                            let (ndc, sin_cos) = rect_to_instance_ndc(
3883                                repose_core::Rect {
3884                                    x: rect.x,
3885                                    y: dy,
3886                                    w: rect.w,
3887                                    h: thickness,
3888                                },
3889                                current_transform,
3890                                current_target_size.0,
3891                                current_target_size.1,
3892                            );
3893                            batch.rects.push(RectInstance {
3894                                xywh: ndc,
3895                                radii: [0.0; 4],
3896                                brush_type: 0,
3897                                _pad: [0.0; 3],
3898                                color0: deco_color.to_linear(),
3899                                color1: [0.0; 4],
3900                                grad_start: [0.0; 2],
3901                                grad_end: [0.0; 2],
3902                                sin_cos,
3903                            });
3904                        }
3905                        if text_decoration.strikethrough {
3906                            let sy = baseline_y - px * 0.3;
3907                            let (ndc, sin_cos) = rect_to_instance_ndc(
3908                                repose_core::Rect {
3909                                    x: rect.x,
3910                                    y: sy,
3911                                    w: rect.w,
3912                                    h: thickness,
3913                                },
3914                                current_transform,
3915                                current_target_size.0,
3916                                current_target_size.1,
3917                            );
3918                            batch.rects.push(RectInstance {
3919                                xywh: ndc,
3920                                radii: [0.0; 4],
3921                                brush_type: 0,
3922                                _pad: [0.0; 3],
3923                                color0: deco_color.to_linear(),
3924                                color1: [0.0; 4],
3925                                grad_start: [0.0; 2],
3926                                grad_end: [0.0; 2],
3927                                sin_cos,
3928                            });
3929                        }
3930                    }
3931                }
3932                SceneNode::Image {
3933                    rect,
3934                    handle,
3935                    tint,
3936                    fit,
3937                } => {
3938                    flush_batch!();
3939
3940                    // Update usage timestamp for eviction
3941                    let (img_w, img_h, is_nv12) = if let Some(t) = self.images.get_mut(handle) {
3942                        match t {
3943                            ImageTex::Rgba {
3944                                w,
3945                                h,
3946                                last_used_frame,
3947                                ..
3948                            } => {
3949                                *last_used_frame = self.frame_index;
3950                                (*w, *h, false)
3951                            }
3952                            ImageTex::Nv12 {
3953                                w,
3954                                h,
3955                                last_used_frame,
3956                                ..
3957                            } => {
3958                                *last_used_frame = self.frame_index;
3959                                (*w, *h, true)
3960                            }
3961                        }
3962                    } else {
3963                        log::warn!("Image handle {} not found", handle);
3964                        continue;
3965                    };
3966
3967                    let src_w = img_w as f32;
3968                    let src_h = img_h as f32;
3969                    let transformed = current_transform.apply_to_rect(*rect);
3970                    let dst_w = transformed.w.max(0.0);
3971                    let dst_h = transformed.h.max(0.0);
3972                    if dst_w <= 0.0 || dst_h <= 0.0 {
3973                        continue;
3974                    }
3975
3976                    let (xywh_ndc, uv_rect) = match fit {
3977                        repose_core::view::ImageFit::Contain => {
3978                            let scale = (dst_w / src_w).min(dst_h / src_h);
3979                            let w = src_w * scale;
3980                            let h = src_h * scale;
3981                            let x = transformed.x + (dst_w - w) * 0.5;
3982                            let y = transformed.y + (dst_h - h) * 0.5;
3983                            (
3984                                to_ndc(x, y, w, h, current_target_size.0, current_target_size.1),
3985                                [0.0, 1.0, 1.0, 0.0],
3986                            )
3987                        }
3988                        repose_core::view::ImageFit::Cover => {
3989                            let scale = (dst_w / src_w).max(dst_h / src_h);
3990                            let content_w = src_w * scale;
3991                            let content_h = src_h * scale;
3992                            let overflow_x = (content_w - dst_w) * 0.5;
3993                            let overflow_y = (content_h - dst_h) * 0.5;
3994                            let u0 = (overflow_x / content_w).clamp(0.0, 1.0);
3995                            let v0 = (overflow_y / content_h).clamp(0.0, 1.0);
3996                            let u1 = ((overflow_x + dst_w) / content_w).clamp(0.0, 1.0);
3997                            let v1 = ((overflow_y + dst_h) / content_h).clamp(0.0, 1.0);
3998                            (
3999                                to_ndc(
4000                                    transformed.x,
4001                                    transformed.y,
4002                                    dst_w,
4003                                    dst_h,
4004                                    current_target_size.0,
4005                                    current_target_size.1,
4006                                ),
4007                                [u0, 1.0 - v1, u1, 1.0 - v0],
4008                            )
4009                        }
4010                        repose_core::view::ImageFit::FitWidth => {
4011                            let scale = dst_w / src_w;
4012                            let w = dst_w;
4013                            let h = src_h * scale;
4014                            let y = transformed.y + (dst_h - h) * 0.5;
4015                            (
4016                                to_ndc(
4017                                    transformed.x,
4018                                    y,
4019                                    w,
4020                                    h,
4021                                    current_target_size.0,
4022                                    current_target_size.1,
4023                                ),
4024                                [0.0, 1.0, 1.0, 0.0],
4025                            )
4026                        }
4027                        repose_core::view::ImageFit::FitHeight => {
4028                            let scale = dst_h / src_h;
4029                            let w = src_w * scale;
4030                            let h = dst_h;
4031                            let x = transformed.x + (dst_w - w) * 0.5;
4032                            (
4033                                to_ndc(
4034                                    x,
4035                                    transformed.y,
4036                                    w,
4037                                    h,
4038                                    current_target_size.0,
4039                                    current_target_size.1,
4040                                ),
4041                                [0.0, 1.0, 1.0, 0.0],
4042                            )
4043                        }
4044                        _ => ([0.0; 4], [0.0; 4]),
4045                    };
4046
4047                    // Convert top-left based NDC to center-based for shader
4048                    let ndc_center = [
4049                        xywh_ndc[0] + xywh_ndc[2] * 0.5,
4050                        xywh_ndc[1] + xywh_ndc[3] * 0.5,
4051                        xywh_ndc[2],
4052                        xywh_ndc[3],
4053                    ];
4054
4055                    if is_nv12 {
4056                        let uv_x_offset = if let Some(ImageTex::Nv12 { w, color_info, .. }) =
4057                            self.images.get(handle)
4058                        {
4059                            match color_info.chroma_siting {
4060                                ChromaSiting::Center | ChromaSiting::TopLeft => 0.0,
4061                                ChromaSiting::Left => -1.0 / *w as f32,
4062                            }
4063                        } else {
4064                            0.0
4065                        };
4066
4067                        let inst = Nv12Instance {
4068                            xywh: ndc_center,
4069                            uv: uv_rect,
4070                            color: tint.to_linear(),
4071                            uv_x_offset,
4072                            sin_cos: [1.0, 0.0],
4073                            _pad: [0.0],
4074                        };
4075                        if let Some((off, _)) = self.nv12.upload(&self.device, &self.queue, &[inst])
4076                        {
4077                            current_pass.cmds.push(Cmd::ImageNv12 {
4078                                off,
4079                                cnt: 1,
4080                                handle: *handle,
4081                            });
4082                        }
4083                    } else {
4084                        // RGBA uses GlyphInstance struct (reused pipeline)
4085                        let inst = GlyphInstance {
4086                            xywh: ndc_center,
4087                            uv: uv_rect,
4088                            color: tint.to_linear(),
4089                            sin_cos: [1.0, 0.0],
4090                        };
4091                        if let Some((off, _)) =
4092                            self.glyph_color.upload(&self.device, &self.queue, &[inst])
4093                        {
4094                            current_pass.cmds.push(Cmd::ImageRgba {
4095                                off,
4096                                cnt: 1,
4097                                handle: *handle,
4098                            });
4099                        }
4100                    }
4101                }
4102                SceneNode::PushClip { rect, radius, op } => {
4103                    flush_batch!(); // flush content before entering clip
4104
4105                    let is_diff = matches!(op, repose_core::ClipOp::Difference);
4106
4107                    let t_identity = Transform::identity();
4108                    let current_transform = transform_stack.last().unwrap_or(&t_identity);
4109                    let transformed = current_transform.apply_to_rect(*rect);
4110
4111                    let top = scissor_stack.last().copied().unwrap_or(root_clip_rect);
4112                    let next_scissor = if is_diff {
4113                        top
4114                    } else {
4115                        intersect(top, transformed)
4116                    };
4117                    scissor_stack.push(next_scissor);
4118                    let scissor = to_scissor(
4119                        &next_scissor,
4120                        current_target_size.0 as u32,
4121                        current_target_size.1 as u32,
4122                    );
4123
4124                    let clip_ndc_tl = to_ndc(
4125                        transformed.x,
4126                        transformed.y,
4127                        transformed.w,
4128                        transformed.h,
4129                        current_target_size.0,
4130                        current_target_size.1,
4131                    );
4132                    let inst = ClipInstance {
4133                        xywh: [
4134                            clip_ndc_tl[0] + clip_ndc_tl[2] * 0.5,
4135                            clip_ndc_tl[1] + clip_ndc_tl[3] * 0.5,
4136                            clip_ndc_tl[2],
4137                            clip_ndc_tl[3],
4138                        ],
4139                        radii: *radius,
4140                        sin_cos: [1.0, 0.0],
4141                    };
4142                    let bytes = bytemuck::bytes_of(&inst);
4143                    self.clip_ring.grow_to_fit(&self.device, bytes.len() as u64);
4144                    let (off, _) = self.clip_ring.alloc_write(&self.queue, bytes);
4145
4146                    let rounded = radius.iter().any(|&r| r > 0.5);
4147
4148                    current_pass.cmds.push(Cmd::ClipPush {
4149                        off,
4150                        cnt: 1,
4151                        scissor,
4152                        difference: is_diff,
4153                        rounded,
4154                    });
4155                }
4156                SceneNode::PopClip => {
4157                    flush_batch!();
4158
4159                    if !scissor_stack.is_empty() {
4160                        scissor_stack.pop();
4161                    } else {
4162                        log::warn!("PopClip with empty stack");
4163                    }
4164
4165                    let top = scissor_stack.last().copied().unwrap_or(root_clip_rect);
4166                    let scissor = to_scissor(
4167                        &top,
4168                        current_target_size.0 as u32,
4169                        current_target_size.1 as u32,
4170                    );
4171                    current_pass.cmds.push(Cmd::ClipPop { scissor });
4172                }
4173                SceneNode::Shadow {
4174                    rect,
4175                    radius,
4176                    elevation: _,
4177                    color,
4178                } => {
4179                    flush_if_prim_changed!("rect", &self.rects);
4180                    let (ndc, sin_cos) = rect_to_instance_ndc(
4181                        *rect,
4182                        current_transform,
4183                        current_target_size.0,
4184                        current_target_size.1,
4185                    );
4186                    let (brush_type, color0, _color1, _grad_start, _grad_end) =
4187                        brush_to_instance_fields(&Brush::Solid(*color));
4188                    batch.rects.push(RectInstance {
4189                        xywh: ndc,
4190                        radii: *radius,
4191                        brush_type,
4192                        _pad: [0.0; 3],
4193                        color0,
4194                        color1: [0.0; 4],
4195                        grad_start: [0.0; 2],
4196                        grad_end: [0.0; 2],
4197                        sin_cos,
4198                    });
4199                }
4200                SceneNode::PushTransform { transform } => {
4201                    flush_batch!(); // flush before transform change
4202                    let combined = current_transform.combine(transform);
4203                    transform_stack.push(combined);
4204                }
4205                SceneNode::PopTransform => {
4206                    flush_batch!(); // flush before transform change
4207                    transform_stack.pop();
4208                }
4209                SceneNode::BeginLayer {
4210                    rect,
4211                    layer_id,
4212                    alpha,
4213                    blur_radius_x,
4214                    blur_radius_y,
4215                    rectangle_edge: _,
4216                } => {
4217                    flush_batch!();
4218                    // Layer rect is already snapped to whole pixels in layout;
4219                    // round() keeps any bypass of that snap consistent.
4220                    let w = (rect.w.round().max(1.0)) as u32;
4221                    let h = (rect.h.round().max(1.0)) as u32;
4222                    // Close out the current pass, start a new one for the layer.
4223                    let prev_target = current_pass.target;
4224                    let prev_scissor = current_pass.initial_scissor;
4225                    let saved = std::mem::replace(
4226                        &mut current_pass,
4227                        Pass {
4228                            target: PassTarget::Layer(*layer_id),
4229                            initial_scissor: (0, 0, w, h),
4230                            clear_color: Some([0.0, 0.0, 0.0, 0.0]),
4231                            cmds: Vec::new(),
4232                        },
4233                    );
4234                    passes.push(saved);
4235                    target_stack.push(prev_target);
4236                    let _ = prev_scissor; // initial_scissor of resumed pass is restored at EndLayer
4237                    // Get or create the layer's offscreen texture now so that
4238                    // subsequent scissor ops / draws have a valid target.
4239                    self.get_or_create_layer(*layer_id, w, h, *rect);
4240                    current_target_size = (w as f32, h as f32);
4241                    layer_alphas.push((*layer_id, *alpha, current_pass.initial_scissor));
4242                    // Store blur info for post-processing after EndLayer
4243                    if *blur_radius_x > 0.0 || *blur_radius_y > 0.0 {
4244                        layer_blurs.push((*layer_id, *blur_radius_x, *blur_radius_y));
4245                    }
4246                }
4247                SceneNode::EndLayer { layer_id } => {
4248                    flush_batch!();
4249                    // Finish the layer's pass, start a new one on the previous target.
4250                    let saved = std::mem::replace(
4251                        &mut current_pass,
4252                        Pass {
4253                            target: target_stack.pop().unwrap_or(PassTarget::Surface),
4254                            initial_scissor: (0, 0, self.output_width, self.output_height),
4255                            clear_color: None, // LoadOp::Load - don't wipe earlier surface content
4256                            cmds: Vec::new(),
4257                        },
4258                    );
4259                    passes.push(saved);
4260                    current_target_size = (fb_w, fb_h);
4261                    // Issue a composite quad for the just-finished layer in the new pass.
4262                    if let Some((_, layer_alpha, _)) = layer_alphas
4263                        .iter()
4264                        .find(|(id, _, _)| id == layer_id)
4265                        .copied()
4266                    {
4267                        let layer = self.layer_pool.get(layer_id).expect("layer target");
4268                        let ndc_tl = to_ndc(
4269                            layer.rect_px.0,
4270                            layer.rect_px.1,
4271                            layer.rect_px.2,
4272                            layer.rect_px.3,
4273                            fb_w,
4274                            fb_h,
4275                        );
4276                        let uv_u1 = layer.rect_px.2 / layer.width.max(1) as f32;
4277                        let uv_v1 = layer.rect_px.3 / layer.height.max(1) as f32;
4278                        // Check if this layer needs content blur
4279                        let blur_px_val = layer_blurs
4280                            .iter()
4281                            .find(|(id, _, _)| id == layer_id)
4282                            .map(|(_, bx, by)| (*bx, *by));
4283                        if let Some((blur_x, blur_y)) =
4284                            blur_px_val.filter(|(bx, by)| *bx > 0.0 || *by > 0.0)
4285                        {
4286                            // Content blur: draw blurred version using the blur_content pipeline
4287                            let bw_uv = (blur_x * 1.5) / layer.width.max(1) as f32;
4288                            let bh_uv = (blur_y * 1.5) / layer.height.max(1) as f32;
4289                            let inst = BlurInstance {
4290                                xywh: [
4291                                    ndc_tl[0] + ndc_tl[2] * 0.5,
4292                                    ndc_tl[1] + ndc_tl[3] * 0.5,
4293                                    ndc_tl[2],
4294                                    ndc_tl[3],
4295                                ],
4296                                uv: [0.0, 0.0, uv_u1, uv_v1],
4297                                color: [1.0, 1.0, 1.0, layer_alpha],
4298                                blur_uv: [bw_uv, bh_uv],
4299                                sin_cos: [1.0, 0.0],
4300                            };
4301                            self.blur_ring.grow_to_fit(
4302                                &self.device,
4303                                std::mem::size_of::<BlurInstance>() as u64,
4304                            );
4305                            let bytes = bytemuck::bytes_of(&inst);
4306                            let (off, _) = self.blur_ring.alloc_write(&self.queue, bytes);
4307                            current_pass.cmds.push(Cmd::CompositeBlur {
4308                                off,
4309                                cnt: 1,
4310                                layer_id: *layer_id,
4311                            });
4312                        } else {
4313                            // Normal sharp composite
4314                            let inst = GlyphInstance {
4315                                xywh: [
4316                                    ndc_tl[0] + ndc_tl[2] * 0.5,
4317                                    ndc_tl[1] + ndc_tl[3] * 0.5,
4318                                    ndc_tl[2],
4319                                    ndc_tl[3],
4320                                ],
4321                                uv: [0.0, uv_v1, uv_u1, 0.0],
4322                                color: [1.0, 1.0, 1.0, layer_alpha],
4323                                sin_cos: [1.0, 0.0],
4324                            };
4325                            if let Some((off, cnt)) =
4326                                self.glyph_color.upload(&self.device, &self.queue, &[inst])
4327                            {
4328                                current_pass.cmds.push(Cmd::CompositeLayer {
4329                                    off,
4330                                    cnt,
4331                                    layer_id: *layer_id,
4332                                    alpha: layer_alpha,
4333                                });
4334                            }
4335                        }
4336                    }
4337                }
4338                SceneNode::CompositeShadow {
4339                    layer_id,
4340                    blur_px,
4341                    offset_px,
4342                    color,
4343                } => {
4344                    flush_batch!();
4345                    if let Some(layer) = self.layer_pool.get(layer_id).cloned() {
4346                        // Shadow rect = layer rect + offset.
4347                        let sx = layer.rect_px.0 + offset_px.0;
4348                        let sy = layer.rect_px.1 + offset_px.1;
4349                        let sw = layer.rect_px.2;
4350                        let sh = layer.rect_px.3;
4351                        // The blur in UV space is 1.5 * blur_px / texture_size
4352                        // (the 1.5 matches the 3x3 Gaussian span).
4353                        let bw_uv = (blur_px * 1.5) / layer.width.max(1) as f32;
4354                        let bh_uv = (blur_px * 1.5) / layer.height.max(1) as f32;
4355                        let shadow_u1 = layer.rect_px.2 / layer.width.max(1) as f32;
4356                        let shadow_v1 = layer.rect_px.3 / layer.height.max(1) as f32;
4357                        let ndc_tl = to_ndc(sx, sy, sw, sh, fb_w, fb_h);
4358                        let inst = BlurInstance {
4359                            xywh: [
4360                                ndc_tl[0] + ndc_tl[2] * 0.5,
4361                                ndc_tl[1] + ndc_tl[3] * 0.5,
4362                                ndc_tl[2],
4363                                ndc_tl[3],
4364                            ],
4365                            uv: [0.0, 0.0, shadow_u1, shadow_v1],
4366                            color: [
4367                                color.0 as f32 / 255.0,
4368                                color.1 as f32 / 255.0,
4369                                color.2 as f32 / 255.0,
4370                                color.3 as f32 / 255.0,
4371                            ],
4372                            blur_uv: [bw_uv, bh_uv],
4373                            sin_cos: [1.0, 0.0],
4374                        };
4375                        self.blur_ring
4376                            .grow_to_fit(&self.device, std::mem::size_of::<BlurInstance>() as u64);
4377                        let bytes = bytemuck::bytes_of(&inst);
4378                        let (off, _) = self.blur_ring.alloc_write(&self.queue, bytes);
4379                        current_pass.cmds.push(Cmd::CompositeShadow {
4380                            off,
4381                            cnt: 1,
4382                            layer_id: *layer_id,
4383                        });
4384                    }
4385                }
4386                _ => {}
4387            }
4388        }
4389
4390        flush_batch!();
4391
4392        // Push the final pass.
4393        passes.push(current_pass);
4394
4395        let globals_bytes = std::mem::size_of::<Globals>() as u64;
4396        let globals_staging = self.device.create_buffer(&wgpu::BufferDescriptor {
4397            label: Some("globals staging"),
4398            size: (passes.len().max(1) as u64) * globals_bytes,
4399            usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC,
4400            mapped_at_creation: false,
4401        });
4402        for (i, pass) in passes.iter().enumerate() {
4403            let (target_w, target_h) = match pass.target {
4404                PassTarget::Surface => (fb_w, fb_h),
4405                PassTarget::Layer(layer_id) => {
4406                    let lt = self.layer_pool.get(&layer_id);
4407                    (
4408                        lt.map_or(fb_w, |l| l.width as f32),
4409                        lt.map_or(fb_h, |l| l.height as f32),
4410                    )
4411                }
4412            };
4413            self.queue.write_buffer(
4414                &globals_staging,
4415                (i as u64) * globals_bytes,
4416                bytemuck::bytes_of(&make_globals(target_w, target_h)),
4417            );
4418        }
4419
4420        let bind_mask = self.atlas_bind_group_mask();
4421        let bind_color = self.atlas_bind_group_color();
4422        let mut clip_depth: u32 = 0;
4423
4424        for (pass_index, pass) in std::mem::take(&mut passes).into_iter().enumerate() {
4425            let (color_view, resolve_target, depth_stencil_view, is_layer) = match pass.target {
4426                PassTarget::Surface => {
4427                    let swap_view = target_view.clone();
4428                    let use_ws = self.working_space && self.ws_view.is_some();
4429                    let (color, resolve) = if use_ws {
4430                        let ws_view = self.ws_view.as_ref().unwrap();
4431                        if let Some(msaa_view) = &self.msaa_view {
4432                            // MSAA resolves to working-space texture
4433                            (msaa_view.clone(), Some(ws_view.clone()))
4434                        } else {
4435                            // Direct render to working-space texture
4436                            (ws_view.clone(), None)
4437                        }
4438                    } else if let Some(msaa_view) = &self.msaa_view {
4439                        (msaa_view.clone(), Some(swap_view))
4440                    } else {
4441                        (swap_view, None)
4442                    };
4443                    (color, resolve, self.depth_stencil_view.clone(), false)
4444                }
4445                PassTarget::Layer(layer_id) => {
4446                    if let Some(lt) = self.layer_pool.get(&layer_id) {
4447                        (lt.view.clone(), None, lt.depth_stencil_view.clone(), true)
4448                    } else {
4449                        log::warn!("missing layer target {layer_id}");
4450                        continue;
4451                    }
4452                }
4453            };
4454
4455            encoder.copy_buffer_to_buffer(
4456                &globals_staging,
4457                (pass_index as u64) * globals_bytes,
4458                &self.globals_buf,
4459                0,
4460                globals_bytes,
4461            );
4462
4463            if is_layer {
4464                clip_depth = 0;
4465            }
4466
4467            let pipes: &Pipelines = if is_layer {
4468                &self.layer_pipes
4469            } else {
4470                &self.surface_pipes
4471            };
4472
4473            let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
4474                label: Some("pass"),
4475                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
4476                    view: &color_view,
4477                    resolve_target: resolve_target.as_ref(),
4478                    ops: wgpu::Operations {
4479                        load: match pass.clear_color {
4480                            Some(c) => wgpu::LoadOp::Clear(wgpu::Color {
4481                                r: c[0] as f64,
4482                                g: c[1] as f64,
4483                                b: c[2] as f64,
4484                                a: c[3] as f64,
4485                            }),
4486                            None => wgpu::LoadOp::Load,
4487                        },
4488                        store: wgpu::StoreOp::Store,
4489                    },
4490                    depth_slice: None,
4491                })],
4492                depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
4493                    view: &depth_stencil_view,
4494                    depth_ops: None,
4495                    stencil_ops: Some(wgpu::Operations {
4496                        load: if is_layer || pass.clear_color.is_some() {
4497                            wgpu::LoadOp::Clear(0)
4498                        } else {
4499                            wgpu::LoadOp::Load
4500                        },
4501                        store: wgpu::StoreOp::Store,
4502                    }),
4503                }),
4504                timestamp_writes: None,
4505                occlusion_query_set: None,
4506                multiview_mask: None,
4507            });
4508
4509            rpass.set_bind_group(0, &self.globals_bind, &[]);
4510            rpass.set_stencil_reference(clip_depth);
4511            rpass.set_scissor_rect(
4512                pass.initial_scissor.0,
4513                pass.initial_scissor.1,
4514                pass.initial_scissor.2,
4515                pass.initial_scissor.3,
4516            );
4517
4518            macro_rules! draw_simple {
4519                ($pipeline:expr, $ring:expr, $inst:ty, $off:ident, $n:ident) => {{
4520                    rpass.set_pipeline($pipeline);
4521                    let bytes = ($n as u64) * std::mem::size_of::<$inst>() as u64;
4522                    rpass.set_vertex_buffer(0, $ring.buf.slice($off..$off + bytes));
4523                    rpass.draw(0..6, 0..$n);
4524                }};
4525            }
4526
4527            macro_rules! draw_with_bind {
4528                ($pipeline:expr, $ring:expr, $inst:ty, $bind:expr, $off:ident, $n:ident) => {{
4529                    rpass.set_pipeline($pipeline);
4530                    rpass.set_bind_group(1, $bind, &[]);
4531                    let bytes = ($n as u64) * std::mem::size_of::<$inst>() as u64;
4532                    rpass.set_vertex_buffer(0, $ring.buf.slice($off..$off + bytes));
4533                    rpass.draw(0..6, 0..$n);
4534                }};
4535            }
4536
4537            for cmd in pass.cmds {
4538                match cmd {
4539                    Cmd::ClipPush {
4540                        off,
4541                        cnt: n,
4542                        scissor,
4543                        difference,
4544                        rounded,
4545                    } => {
4546                        rpass.set_scissor_rect(scissor.0, scissor.1, scissor.2, scissor.3);
4547                        rpass.set_stencil_reference(clip_depth);
4548
4549                        if difference {
4550                            rpass.set_pipeline(&pipes.clip_dec);
4551                        } else if self.msaa_samples > 1 && !is_layer && rounded {
4552                            rpass.set_pipeline(&pipes.clip_a2c);
4553                        } else {
4554                            rpass.set_pipeline(&pipes.clip_bin);
4555                        }
4556
4557                        let bytes = (n as u64) * std::mem::size_of::<ClipInstance>() as u64;
4558                        rpass.set_vertex_buffer(0, self.clip_ring.buf.slice(off..off + bytes));
4559                        rpass.draw(0..6, 0..n);
4560
4561                        if !difference {
4562                            clip_depth = (clip_depth + 1).min(255);
4563                            rpass.set_stencil_reference(clip_depth);
4564                        }
4565                    }
4566
4567                    Cmd::ClipPop { scissor } => {
4568                        clip_depth = clip_depth.saturating_sub(1);
4569                        rpass.set_stencil_reference(clip_depth);
4570                        rpass.set_scissor_rect(scissor.0, scissor.1, scissor.2, scissor.3);
4571                    }
4572
4573                    Cmd::Rect { off, cnt: n } => {
4574                        draw_simple!(&pipes.rects, self.rects.ring, RectInstance, off, n);
4575                    }
4576
4577                    Cmd::Border { off, cnt: n } => {
4578                        draw_simple!(&pipes.borders, self.borders.ring, BorderInstance, off, n);
4579                    }
4580
4581                    Cmd::GlyphsMask { off, cnt: n } => {
4582                        draw_with_bind!(
4583                            &pipes.text_mask,
4584                            self.glyph_mask.ring,
4585                            GlyphInstance,
4586                            &bind_mask,
4587                            off,
4588                            n
4589                        );
4590                    }
4591
4592                    Cmd::GlyphsColor { off, cnt: n } => {
4593                        draw_with_bind!(
4594                            &pipes.text_color,
4595                            self.glyph_color.ring,
4596                            GlyphInstance,
4597                            &bind_color,
4598                            off,
4599                            n
4600                        );
4601                    }
4602
4603                    Cmd::GlyphsVector { off, cnt: n } => {
4604                        if let Some(ref slug_pipe) = pipes.slug.as_ref() {
4605                            rpass.set_pipeline(slug_pipe);
4606                            let bytes = (n as u64) * std::mem::size_of::<slug::TessVertex>() as u64;
4607                            rpass.set_vertex_buffer(0, self.slug_ring.buf.slice(off..off + bytes));
4608                            rpass.draw(0..n, 0..1);
4609                        }
4610                    }
4611
4612                    Cmd::ImageRgba {
4613                        off,
4614                        cnt: n,
4615                        handle,
4616                    } => {
4617                        if let Some(ImageTex::Rgba { bind, .. }) = self.images.get(&handle) {
4618                            draw_with_bind!(
4619                                &pipes.image_rgba,
4620                                self.glyph_color.ring,
4621                                GlyphInstance,
4622                                bind,
4623                                off,
4624                                n
4625                            );
4626                        }
4627                    }
4628
4629                    Cmd::ImageNv12 {
4630                        off,
4631                        cnt: n,
4632                        handle,
4633                    } => {
4634                        if let Some(ImageTex::Nv12 { bind, .. }) = self.images.get(&handle) {
4635                            draw_with_bind!(
4636                                &pipes.image_nv12,
4637                                self.nv12.ring,
4638                                Nv12Instance,
4639                                bind,
4640                                off,
4641                                n
4642                            );
4643                        }
4644                    }
4645
4646                    Cmd::Ellipse { off, cnt: n } => {
4647                        draw_simple!(&pipes.ellipses, self.ellipses.ring, EllipseInstance, off, n);
4648                    }
4649
4650                    Cmd::EllipseBorder { off, cnt: n } => {
4651                        draw_simple!(
4652                            &pipes.ellipse_borders,
4653                            self.ellipse_borders.ring,
4654                            EllipseBorderInstance,
4655                            off,
4656                            n
4657                        );
4658                    }
4659
4660                    Cmd::Arc { off, cnt: n } => {
4661                        draw_simple!(&pipes.arcs, self.arcs.ring, ArcInstance, off, n);
4662                    }
4663
4664                    Cmd::PushTransform(_) => {}
4665                    Cmd::PopTransform => {}
4666                    Cmd::CompositeLayer {
4667                        off,
4668                        cnt: n,
4669                        layer_id,
4670                        alpha: _,
4671                    } => {
4672                        if let Some(lt) = self.layer_pool.get(&layer_id).cloned() {
4673                            draw_with_bind!(
4674                                &pipes.image_rgba,
4675                                self.glyph_color.ring,
4676                                GlyphInstance,
4677                                &lt.bind,
4678                                off,
4679                                n
4680                            );
4681                        }
4682                    }
4683                    Cmd::CompositeShadow {
4684                        off,
4685                        cnt: n,
4686                        layer_id,
4687                    } => {
4688                        if let Some(lt) = self.layer_pool.get(&layer_id).cloned() {
4689                            draw_with_bind!(
4690                                &pipes.blur,
4691                                self.blur_ring,
4692                                BlurInstance,
4693                                &lt.bind_linear,
4694                                off,
4695                                n
4696                            );
4697                        }
4698                    }
4699                    Cmd::CompositeBlur {
4700                        off,
4701                        cnt: n,
4702                        layer_id,
4703                    } => {
4704                        if let Some(lt) = self.layer_pool.get(&layer_id).cloned() {
4705                            draw_with_bind!(
4706                                &pipes.blur_content,
4707                                self.blur_ring,
4708                                BlurInstance,
4709                                &lt.bind_linear,
4710                                off,
4711                                n
4712                            );
4713                        }
4714                    }
4715                }
4716            }
4717        }
4718
4719        // Display pass: linear working space → sRGB OETF → swapchain
4720        if self.working_space {
4721            if let (Some(_ws_view), Some(ws_bind), Some(display_pipeline)) =
4722                (&self.ws_view, &self.ws_bind, &self.display_pipeline)
4723            {
4724                let swap_view = target_view.clone();
4725                let mut display_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
4726                    label: Some("display transform"),
4727                    color_attachments: &[Some(wgpu::RenderPassColorAttachment {
4728                        view: &swap_view,
4729                        resolve_target: None,
4730                        ops: wgpu::Operations {
4731                            load: wgpu::LoadOp::Load,
4732                            store: wgpu::StoreOp::Store,
4733                        },
4734                        depth_slice: None,
4735                    })],
4736                    depth_stencil_attachment: None,
4737                    timestamp_writes: None,
4738                    occlusion_query_set: None,
4739                    multiview_mask: None,
4740                });
4741                display_pass.set_pipeline(display_pipeline);
4742                display_pass.set_bind_group(1, ws_bind, &[]);
4743                display_pass.draw(0..3, 0..1);
4744            }
4745        }
4746
4747
4748        // Frame end maintenance: Evict unused images
4749        self.evict_unused_images();
4750    }
4751
4752    /// Render a scene into an externally-provided texture view.
4753    /// Use this when embedding Repose in a host that owns the GPU.
4754    /// The host is responsible for submitting the encoder and handling present.
4755    pub fn render_to_view(
4756        &mut self,
4757        scene: &Scene,
4758        encoder: &mut wgpu::CommandEncoder,
4759        target_view: &wgpu::TextureView,
4760        width: u32,
4761        height: u32,
4762        clear_color: Option<[f64; 4]>,
4763    ) {
4764        self.resize(width, height);
4765
4766        self.frame_index = self.frame_index.wrapping_add(1);
4767        self.slug_cache.next_frame();
4768
4769        if width == 0 || height == 0 {
4770            return;
4771        }
4772
4773        self.render_scene_to_encoder(scene, encoder, target_view, clear_color);
4774    }
4775}
4776
4777
4778fn intersect(a: repose_core::Rect, b: repose_core::Rect) -> repose_core::Rect {
4779    let x0 = a.x.max(b.x);
4780    let y0 = a.y.max(b.y);
4781    let x1 = (a.x + a.w).min(b.x + b.w);
4782    let y1 = (a.y + a.h).min(b.y + b.h);
4783    repose_core::Rect {
4784        x: x0,
4785        y: y0,
4786        w: (x1 - x0).max(0.0),
4787        h: (y1 - y0).max(0.0),
4788    }
4789}