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