feather-ui 0.4.0

Feather UI library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2025 Fundament Research Institute <https://fundament.institute>

use crate::color::sRGB32;
use crate::graphics::{Driver, Vec2f};
use crate::render::atlas::PxBox;
use crate::{PxDim, PxPoint, PxRect, PxVector, RelDim, SourceID};
use derive_where::derive_where;
use num_traits::Zero;
use smallvec::SmallVec;
use std::collections::HashMap;
use std::num::NonZero;
use std::sync::Arc;
use wgpu::wgt::SamplerDescriptor;
use wgpu::{
    BindGroupEntry, BindGroupLayoutEntry, Buffer, BufferDescriptor, BufferUsages, TextureView,
};

use parking_lot::RwLock;

use super::atlas::Atlas;

#[derive(Debug)]
/// Shared resources that are the same between all windows
pub struct Shared {
    layout: wgpu::PipelineLayout,
    shader: wgpu::ShaderModule,
    sampler: wgpu::Sampler,
    pipelines: RwLock<HashMap<wgpu::TextureFormat, wgpu::RenderPipeline>>,
    layers: RwLock<HashMap<Arc<SourceID>, Layer>>,
}

pub const TARGET_BLEND: wgpu::ColorTargetState = wgpu::ColorTargetState {
    format: crate::render::atlas::ATLAS_FORMAT,
    blend: Some(wgpu::BlendState::REPLACE),
    write_mask: wgpu::ColorWrites::ALL,
};

impl Shared {
    pub fn new(device: &wgpu::Device) -> Self {
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Compositor"),
            source: wgpu::ShaderSource::Wgsl(include_str!("../shaders/compositor.wgsl").into()),
        });

        let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("Compositor Bind Group"),
            entries: &[
                BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::VERTEX,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: NonZero::new(size_of::<crate::Mat4x4>() as u64),
                    },
                    count: None,
                },
                BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Storage { read_only: true },
                        has_dynamic_offset: true,
                        min_binding_size: None,
                    },
                    count: None,
                },
                BindGroupLayoutEntry {
                    binding: 2,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Storage { read_only: true },
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                },
                BindGroupLayoutEntry {
                    binding: 3,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                },
                BindGroupLayoutEntry {
                    binding: 4,
                    visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        multisampled: false,
                        view_dimension: wgpu::TextureViewDimension::D2Array,
                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
                    },
                    count: None,
                },
                BindGroupLayoutEntry {
                    binding: 5,
                    visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        multisampled: false,
                        view_dimension: wgpu::TextureViewDimension::D2Array,
                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
                    },
                    count: None,
                },
            ],
        });

        let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("Compositor Pipeline"),
            bind_group_layouts: &[&bind_group_layout],
            push_constant_ranges: &[],
        });

        let sampler = device.create_sampler(&SamplerDescriptor {
            label: Some("Compositor Sampler"),
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            mipmap_filter: wgpu::FilterMode::Linear,
            border_color: Some(wgpu::SamplerBorderColor::TransparentBlack),
            ..Default::default()
        });

        Self {
            layout,
            shader,
            sampler,
            pipelines: HashMap::new().into(),
            layers: HashMap::new().into(),
        }
    }

    pub fn access_layers(
        &self,
    ) -> parking_lot::lock_api::RwLockReadGuard<
        '_,
        parking_lot::RawRwLock,
        HashMap<Arc<SourceID>, Layer>,
    > {
        self.layers.read()
    }

    fn get_pipeline(
        &self,
        device: &wgpu::Device,
        format: wgpu::TextureFormat,
    ) -> wgpu::RenderPipeline {
        self.pipelines
            .write()
            .entry(format)
            .or_insert_with(|| {
                device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
                    label: None,
                    layout: Some(&self.layout),
                    vertex: wgpu::VertexState {
                        module: &self.shader,
                        entry_point: Some("vs_main"),
                        buffers: &[],
                        compilation_options: Default::default(),
                    },
                    fragment: Some(wgpu::FragmentState {
                        module: &self.shader,
                        entry_point: Some("fs_main"),
                        compilation_options: Default::default(),
                        targets: &[Some(wgpu::ColorTargetState {
                            format,
                            blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
                            write_mask: wgpu::ColorWrites::ALL,
                        })],
                    }),
                    primitive: wgpu::PrimitiveState {
                        front_face: wgpu::FrontFace::Cw,
                        topology: wgpu::PrimitiveTopology::TriangleList,
                        ..Default::default()
                    },
                    depth_stencil: None,
                    multisample: wgpu::MultisampleState::default(),
                    multiview: None,
                    cache: None,
                })
            })
            .clone()
    }

    pub fn create_layer(
        &self,
        _device: &wgpu::Device,
        id: Arc<SourceID>,
        mut area: PxRect,
        dest: Option<PxRect>,
        color: sRGB32,
        rotation: f32,
        force: bool,
    ) -> Option<Layer> {
        // Snap the layer area to the nearest pixel. This is necessary because the layer
        // is treated as a compositing target, which is always assumed to be on
        // a pixel grid.
        let array = area.v.as_array_mut();
        array[0] = array[0].floor();
        array[1] = array[1].floor();
        array[2] = array[2].ceil();
        array[3] = array[3].ceil();

        let dest = dest.unwrap_or(area);

        // If true, this is a clipping layer, not a texture-backed one
        let target = if color == sRGB32::white() && rotation.is_zero() && !force && dest == area {
            None
        } else {
            Some(RwLock::new(LayerTarget {
                dependents: Default::default(),
            }))
        };

        let layer = Layer {
            area,
            dest,
            color,
            rotation,
            target,
        };

        if let Some(prev) = self.layers.read().get(&id)
            && *prev == layer
        {
            return None;
        }

        self.layers.write().insert(id, layer)
    }
}

// This holds the information for rendering to a layer, which can only be done
// if the layer is texture-backed.
#[derive(Debug)]
pub struct LayerTarget {
    pub dependents: Vec<std::sync::Weak<SourceID>>, /* Layers that draw on to this one (does not
                                                     * include fake layers) */
}

#[derive(Debug)]
pub struct Layer {
    // Renderable area representing what children draw onto. This corresponds to this layer's
    // compositor viewport, if it has one.
    pub area: PxRect,
    // destination area that the layer is composited onto. Usually this is the same as area, but
    // can be different when scaling down
    dest: PxRect,
    color: sRGB32,
    rotation: f32,
    // Layers aren't always texture-backed so this may not exist
    pub target: Option<RwLock<LayerTarget>>,
}

impl PartialEq for Layer {
    fn eq(&self, other: &Self) -> bool {
        self.area == other.area
            && self.dest == other.dest
            && self.color == other.color
            && self.rotation == other.rotation
    }
}

type DeferFn = dyn FnOnce(&Driver, &mut Data) + Send + Sync;
type CustomDrawFn = dyn FnMut(&Driver, &mut wgpu::RenderPass<'_>, PxVector) + Send + Sync;

/// Fundamentally, the compositor works on a massive set of pre-allocated
/// vertices that it assembles into quads in the vertex shader, which then moves
/// them into position and assigns them UV coordinates. Then the pixel shader
/// checks if it must do per-pixel clipping and discards the pixel if it's out
/// of bounds, then samples a texture from the provided texture bank, does color
/// modulation if applicable, then draws the final pixel to the screen using
/// pre-multiplied alpha blending. This allows the compositor to avoid
/// allocating a vertex buffer, instead using a SSBO (or webgpu storage buffer)
/// to store the per-quad data, which it then accesses from the built-in vertex
/// index.
///
/// The compositor can accept GPU generated instructions written directly into
/// it's buffer using a compute shader, if desired.
///
/// The compositor can also accept custom draw calls that break up the batched
/// compositor instructions, which is intended for situations where rendering to
/// the texture atlas is either impractical, or a different blending operation
/// is required (such as subpixel blended text, which requires the SRC1
/// dual-source blending mode, instead of standard pre-multiplied alpha).
#[derive(Debug)]
pub struct Compositor {
    pipeline: wgpu::RenderPipeline,
    mvp: Buffer,
    clip: Buffer,
    clipdata: Vec<PxRect>, // Clipping Rectangles
    pub(crate) segments: SmallVec<[HashMap<u8, Segment>; 1]>,
    view: std::sync::Weak<TextureView>,
    layer_view: std::sync::Weak<TextureView>,
    layer: bool, // Tells us which layer atlas to use (the first or second)
}

/// This stores the compositing data for a single render pass. The window
/// compositor only ever has one segment, but the compositors for the layer
/// caches can have many different segments for each dependency layer and each
/// target slice in the layer atlas for that dependency layer. Each segment
/// contains a set of CPU-side copy-regions to enable GPU generation of
/// compositing data, it's own deferred rendering queue, and its own list of
/// custom draw commands.
#[derive_where(Debug)]
pub struct Segment {
    group: wgpu::BindGroup,
    buffer: Buffer,
    data: Vec<Data>,
    regions: Vec<std::ops::Range<u32>>,
    #[derive_where(skip)]
    defer: HashMap<u32, (Box<DeferFn>, PxRect, PxVector)>,
    #[derive_where(skip)]
    custom: Vec<(u32, Box<CustomDrawFn>, PxVector)>,
}

impl Compositor {
    fn reserve(&mut self, driver: &Driver, pass: u8, slice: u8) {
        self.segments.resize_with(pass as usize + 1, HashMap::new);
        self.segments[pass as usize]
            .entry(slice)
            .or_insert_with(|| {
                let buffer = driver.device.create_buffer(&BufferDescriptor {
                    label: Some("Compositor Data"),
                    size: 32 * size_of::<Data>() as u64,
                    usage: BufferUsages::STORAGE | BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                });

                let group = Self::gen_binding(
                    &self.mvp,
                    &buffer,
                    &self.clip,
                    &driver.shared,
                    &driver.device,
                    &driver.atlas.read().view,
                    &driver.layer_atlas[self.layer as usize].read().view,
                    &self.pipeline.get_bind_group_layout(0),
                );

                #[allow(clippy::single_range_in_vec_init)]
                Segment {
                    group,
                    buffer,
                    data: Vec::new(),
                    regions: vec![0..0],
                    defer: HashMap::new(),
                    custom: Vec::new(),
                }
            });
    }

    fn gen_binding(
        mvp: &Buffer,
        buffer: &Buffer,
        clip: &Buffer,
        shared: &Shared,
        device: &wgpu::Device,
        atlasview: &TextureView,
        layerview: &TextureView,
        layout: &wgpu::BindGroupLayout,
    ) -> wgpu::BindGroup {
        let bindings = [
            BindGroupEntry {
                binding: 0,
                resource: mvp.as_entire_binding(),
            },
            BindGroupEntry {
                binding: 1,
                resource: buffer.as_entire_binding(),
            },
            BindGroupEntry {
                binding: 2,
                resource: clip.as_entire_binding(),
            },
            BindGroupEntry {
                binding: 3,
                resource: wgpu::BindingResource::Sampler(&shared.sampler),
            },
            BindGroupEntry {
                binding: 4,
                resource: wgpu::BindingResource::TextureView(atlasview),
            },
            BindGroupEntry {
                binding: 5,
                resource: wgpu::BindingResource::TextureView(layerview),
            },
        ];

        device.create_bind_group(&wgpu::BindGroupDescriptor {
            layout,
            entries: &bindings,
            label: None,
        })
    }

    // This cannot take a Driver because we have to create two Compositors before
    // the Driver object is made
    pub fn new(
        device: &wgpu::Device,
        shared: &Shared,
        atlasview: &Arc<TextureView>,
        layerview: &Arc<TextureView>,
        format: wgpu::TextureFormat,
        layer: bool,
    ) -> Self {
        let pipeline = shared.get_pipeline(device, format);

        let mvp = device.create_buffer(&BufferDescriptor {
            label: Some("MVP"),
            size: std::mem::size_of::<crate::Mat4x4>() as u64,
            usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let clip = device.create_buffer(&BufferDescriptor {
            label: Some("Compositor Clip Data"),
            size: 4 * size_of::<PxRect>() as u64,
            usage: BufferUsages::STORAGE | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let buffer = device.create_buffer(&BufferDescriptor {
            label: Some("Compositor Data"),
            size: 32 * size_of::<Data>() as u64,
            usage: BufferUsages::STORAGE | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let group = Self::gen_binding(
            &mvp,
            &buffer,
            &clip,
            shared,
            device,
            atlasview,
            layerview,
            &pipeline.get_bind_group_layout(0),
        );

        #[allow(clippy::single_range_in_vec_init)]
        let segment = Segment {
            group,
            buffer,
            data: Vec::new(),
            regions: vec![0..0],
            defer: HashMap::new(),
            custom: Vec::new(),
        };

        Self {
            pipeline,
            mvp,
            clip,
            clipdata: vec![PxRect::zero()],
            segments: SmallVec::from_buf([HashMap::from_iter([(0, segment)])]),
            view: Arc::downgrade(atlasview),
            layer_view: Arc::downgrade(layerview),
            layer,
        }
    }

    /// Should be called when any external or internal buffer gets invalidated
    /// (such as atlas views)
    fn rebind(&mut self, shared: &Shared, device: &wgpu::Device, atlas: &Atlas, layers: &Atlas) {
        self.view = Arc::downgrade(&atlas.view);
        self.layer_view = Arc::downgrade(&layers.view);

        for slices in &mut self.segments {
            for segment in slices.values_mut() {
                segment.group = Self::gen_binding(
                    &self.mvp,
                    &segment.buffer,
                    &self.clip,
                    shared,
                    device,
                    &atlas.view,
                    &layers.view,
                    &self.pipeline.get_bind_group_layout(0),
                );
            }
        }
    }

    fn check_data(
        mvp: &Buffer,
        clip: &Buffer,
        layout: &wgpu::BindGroupLayout,
        segment: &mut Segment,
        shared: &Shared,
        device: &wgpu::Device,
        atlas: &Atlas,
        layers: &Atlas,
    ) {
        let size = segment.data.len() * size_of::<Data>();
        if (segment.buffer.size() as usize) < size {
            segment.buffer.destroy();
            segment.buffer = device.create_buffer(&BufferDescriptor {
                label: Some("Compositor Data"),
                size: size.next_power_of_two() as u64,
                usage: BufferUsages::STORAGE | BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });

            segment.group = Self::gen_binding(
                mvp,
                &segment.buffer,
                clip,
                shared,
                device,
                &atlas.view,
                &layers.view,
                layout,
            );
        }
    }

    fn check_clip(
        &mut self,
        shared: &Shared,
        device: &wgpu::Device,
        atlas: &Atlas,
        layers: &Atlas,
    ) {
        let size = self.clipdata.len() * size_of::<PxRect>();
        if (self.clip.size() as usize) < size {
            self.clip.destroy();
            self.clip = device.create_buffer(&BufferDescriptor {
                label: Some("Compositor Clip Data"),
                size: size.next_power_of_two() as u64,
                usage: BufferUsages::STORAGE | BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            self.rebind(shared, device, atlas, layers);
        }
    }

    #[inline]
    fn scissor_check(dim: &PxDim, clip: PxRect) -> Result<PxRect, PxRect> {
        if PxRect::new(0.0, 0.0, dim.width, dim.height) == clip {
            Err(clip)
        } else {
            Ok(clip)
        }
    }

    #[inline]
    fn clip_data(
        clipdata: &mut Vec<PxRect>,
        cliprect: Result<PxRect, PxRect>,
        offset: PxVector,
        mut data: Data,
    ) -> Option<Data> {
        match cliprect {
            Ok(clip) => {
                if data.rotation.is_zero() {
                    let (mut x, mut y, mut w, mut h) =
                        (data.pos.0[0], data.pos.0[1], data.dim.0[0], data.dim.0[1]);

                    // If the whole rect is outside the cliprect, don't render it at all.
                    if !clip.collides(&PxRect::new(x, y, x + w, y + h)) {
                        // TODO: When we start reserving slots, this will need to instead insert a
                        // special zero size rect.
                        return None;
                    }

                    let (mut u, mut v, mut uw, mut vh) =
                        (data.uv.0[0], data.uv.0[1], data.uvdim.0[0], data.uvdim.0[1]);

                    // If rotation is zero, we don't need to do per-pixel clipping, we can just
                    // modify the rect itself.
                    let bounds = clip.v.as_array_ref();
                    let (min_x, min_y, max_x, max_y) = (bounds[0], bounds[1], bounds[2], bounds[3]);

                    // Get the ratio from our target rect to the source UV sampler rect
                    let uv_ratio = RelDim::new(uw / w, vh / h);

                    // Clip left edge
                    if x < min_x {
                        let right_shift = min_x - x;

                        x += right_shift;
                        u += right_shift * uv_ratio.width;
                        w -= right_shift;
                        uw -= right_shift * uv_ratio.width;
                    }

                    // Clip right edge
                    if x + w > max_x {
                        let right_shift = max_x - (x + w);
                        w += right_shift;
                        uw += right_shift * uv_ratio.width;
                    }

                    // Clip top edge
                    if y < min_y {
                        let bottom_shift = min_y - y;

                        y += bottom_shift;
                        v += bottom_shift * uv_ratio.height;
                        h -= bottom_shift;
                        vh -= bottom_shift * uv_ratio.height;
                    }

                    // Clip bottom edge
                    if y + h > max_y {
                        let bottom_shift = max_y - (y + h);
                        h += bottom_shift;
                        vh += bottom_shift * uv_ratio.height;
                    }

                    Some(Data {
                        pos: [x + offset.x, y + offset.y].into(),
                        dim: [w, h].into(),
                        uv: [u, v].into(),
                        uvdim: [uw, vh].into(),
                        color: data.color,
                        rotation: data.rotation,
                        flags: data.flags,
                        _padding: data._padding,
                    })
                } else {
                    // TODO: Beyond some size, like 32, skip all elements except the last N clipping
                    // rects and only check those
                    let idx = if let Some((idx, _)) =
                        clipdata.iter().enumerate().find(|(_, r)| **r == clip)
                    {
                        idx
                    } else {
                        clipdata.push(clip);
                        clipdata.len() - 1
                    };

                    debug_assert!(idx < 0xFFFF);
                    data.flags = DataFlags::from_bits(data.flags)
                        .with_clip(idx as u16)
                        .into();
                    data.pos.0[0] += offset.x;
                    data.pos.0[1] += offset.y;
                    Some(data)
                }
            }
            Err(clip) => {
                // If the current cliprect is just the scissor rect, we do NOT add a custom
                // clipping rect or do further clipping, but we do check to see
                // if we need to bother rendering this at all.
                if data.rotation.is_zero() {
                    let (x, y, w, h) = (data.pos.0[0], data.pos.0[1], data.dim.0[0], data.dim.0[1]);
                    if !clip.collides(&PxRect::new(x, y, x + w, y + h)) {
                        // TODO: When we start reserving slots, this will need to instead insert a
                        // special zero size rect.
                        return None;
                    }
                }
                data.pos.0[0] += offset.x;
                data.pos.0[1] += offset.y;
                Some(data)
            }
        }
    }

    pub fn prepare(&mut self, driver: &Driver, _: &mut wgpu::CommandEncoder, surface_dim: PxDim) {
        // Check to see if we need to rebind either atlas view
        if self.view.strong_count() == 0 || self.layer_view.strong_count() == 0 {
            self.rebind(
                &driver.shared,
                &driver.device,
                &driver.atlas.read(),
                &driver.layer_atlas[self.layer as usize].read(),
            );
        }

        // Resolve all defers
        for slices in &mut self.segments {
            for segment in slices.values_mut() {
                for (idx, (f, clip, offset)) in segment.defer.drain() {
                    f(driver, &mut segment.data[idx as usize]);
                    segment.data[idx as usize].flags =
                        DataFlags::from_bits(segment.data[idx as usize].flags).into();
                    segment.data[idx as usize] = Self::clip_data(
                        &mut self.clipdata,
                        Self::scissor_check(&surface_dim, clip),
                        offset,
                        segment.data[idx as usize],
                    )
                    .unwrap_or_default();
                }

                if !segment.data.is_empty() {
                    Self::check_data(
                        &self.mvp,
                        &self.clip,
                        &self.pipeline.get_bind_group_layout(0),
                        segment,
                        &driver.shared,
                        &driver.device,
                        &driver.atlas.read(),
                        &driver.layer_atlas[self.layer as usize].read(),
                    );

                    // TODO turn into write_buffer_with (is that actually going to be faster?)
                    let mut offset = 0;
                    for range in &segment.regions {
                        let len = range.end - range.start;
                        driver.queue.write_buffer(
                            &segment.buffer,
                            range.start as u64,
                            bytemuck::cast_slice(
                                &segment.data.as_slice()[offset as usize..(offset + len) as usize],
                            ),
                        );
                        offset += len;
                    }
                }
            }
        }

        driver.queue.write_buffer(
            &self.mvp,
            0,
            bytemuck::cast_slice(
                &crate::graphics::mat4_proj(
                    0.0,
                    surface_dim.height,
                    surface_dim.width,
                    -(surface_dim.height),
                    0.2,
                    10000.0,
                )
                .to_array(),
            ),
        );

        // Very important that we do this AFTER resolving all defers, since those can
        // add cliprects
        if !self.clipdata.is_empty() {
            self.check_clip(
                &driver.shared,
                &driver.device,
                &driver.atlas.read(),
                &driver.layer_atlas[self.layer as usize].read(),
            );
            driver.queue.write_buffer(
                &self.clip,
                0,
                bytemuck::cast_slice(self.clipdata.as_slice()),
            );
        }
    }

    #[inline]
    fn append_internal(
        &mut self,
        clipstack: &[PxRect],
        surface_dim: PxDim,
        layer_offset: PxVector,
        pos: PxPoint,
        dim: PxDim,
        uv: PxPoint,
        uvdim: PxDim,
        color: u32,
        rotation: f32,
        tex: u8,
        pass: u8,
        slice: u8,
        raw: bool,
        layer: bool,
    ) -> u32 {
        let data = Data {
            pos: pos.to_array().into(),
            dim: dim.to_array().into(),
            uv: uv.to_array().into(),
            uvdim: uvdim.to_array().into(),
            color,
            rotation,
            flags: DataFlags::new()
                .with_tex(tex)
                .with_raw(raw)
                .with_layer(layer)
                .into(),
            ..Default::default()
        };

        if let Some(d) = Self::clip_data(
            &mut self.clipdata,
            clipstack.last().ok_or(surface_dim.into()).copied(),
            layer_offset,
            data,
        ) {
            self.preprocessed(d, pass, slice)
        } else {
            u32::MAX // TODO: Once we start reserving slots, we will always need to return a valid one by inserting an empty rect in clip_data
        }
    }

    #[inline]
    fn preprocessed(&mut self, data: Data, index: u8, slice: u8) -> u32 {
        let segment = &mut self.segments[index as usize].get_mut(&slice).unwrap();
        let region = segment.regions.last_mut().unwrap();
        if region.end == u32::MAX {
            panic!(
                "Still processing a compute operation! Finish it by calling set_compute_buffer() first."
            );
        }

        let idx = region.end;
        segment.data.push(data);
        region.end += 1;
        idx
    }

    pub fn draw(&mut self, driver: &Driver, pass: &mut wgpu::RenderPass<'_>, index: u8, slice: u8) {
        let segment = &mut self.segments[index as usize].get_mut(&slice).unwrap();

        let mut last_index = 0;
        for (i, f, draw_offset) in &mut segment.custom {
            if last_index < *i {
                pass.set_pipeline(&self.pipeline);
                pass.set_bind_group(0, &segment.group, &[0]);
                pass.draw(last_index..*i, 0..1);
            }
            last_index = *i;
            f(driver, pass, *draw_offset);
        }

        pass.set_pipeline(&self.pipeline);
        pass.set_bind_group(0, &segment.group, &[0]);
        pass.draw(last_index..(segment.regions.last().unwrap().end * 6), 0..1);
    }

    pub fn cleanup(&mut self) {
        for slices in &mut self.segments {
            for segment in slices.values_mut() {
                segment.data.clear();
                segment.regions.clear();
                segment.regions.push(0..0);
            }
        }

        self.clipdata.clear();
        self.clipdata.push(PxRect::zero());
    }
}

/// A Compositor is associated with a render target, which is usually a window,
/// but can also be an intermediate buffer, used for Layers. As a result, a
/// compositor does not control the clipping rect stack, the window itself does.
/// This associates a compositor with a clipstack and any other information it
/// might need to properly append data during a render, such as the offset.
/// Because of this auxillairy information, you cannot append directly to a
/// compositor, only to a CompositorView.
pub struct CompositorView<'a> {
    pub index: u8, /* While we carry mutable references of all 3 possible compositors, this
                    * tells us which we're currently using */
    pub window: &'a mut Compositor, // index 0
    pub layer0: &'a mut Compositor, // index 1
    pub layer1: &'a mut Compositor, // index 2
    pub clipstack: &'a mut Vec<PxRect>,
    pub offset: PxVector,
    pub surface_dim: PxDim, // Dimension of the top-level window surface.
    pub pass: u8,
    pub slice: u8, // This is the atlas slice index that this is being rendered to
}

impl<'a> CompositorView<'a> {
    #[inline]
    pub fn with_clip<T>(
        &mut self,
        clip: PxRect,
        f: impl FnOnce(&mut Self) -> Result<T, crate::Error>,
    ) -> Result<T, crate::Error> {
        if let Some(prev) = self.clipstack.last() {
            self.clipstack.push(clip.intersect(*prev));
        } else {
            self.clipstack.push(clip);
        }
        let r = f(self);
        self.clipstack
            .pop()
            .expect("Tried to pop a clipping rect but the stack was empty!");
        r
    }

    #[inline]
    pub fn current_clip(&self) -> PxRect {
        *self.clipstack.last().unwrap_or(&self.surface_dim.into())
    }

    #[inline]
    pub fn segment(&mut self) -> &mut Segment {
        let pass = self.pass;
        let slice = self.slice;
        self.compositor().segments[pass as usize]
            .get_mut(&slice)
            .unwrap()
    }

    #[inline]
    pub fn compositor(&mut self) -> &mut Compositor {
        match self.index {
            0 => self.window,
            1 => self.layer0,
            2 => self.layer1,
            _ => panic!("Illegal compositor index!"),
        }
    }

    #[inline]
    pub fn append_data(
        &mut self,
        pos: PxPoint,
        dim: PxDim,
        uv: PxPoint,
        uvdim: PxDim,
        color: u32,
        rotation: f32,
        tex: u8,
        raw: bool,
    ) -> u32 {
        // I really wish rust had partial borrows
        let compositor = match self.index {
            0 => &mut self.window,
            1 => &mut self.layer0,
            2 => &mut self.layer1,
            _ => panic!("Illegal compositor index!"),
        };
        compositor.append_internal(
            self.clipstack,
            self.surface_dim,
            self.offset,
            pos,
            dim,
            uv,
            uvdim,
            color,
            rotation,
            tex,
            self.pass,
            self.slice,
            raw,
            false,
        )
    }

    #[inline]
    pub(crate) fn append_layer(&mut self, layer: &Layer, parent_pos: PxPoint, uv: PxBox) -> u32 {
        // I really wish rust had partial borrows
        let compositor = match self.index {
            0 => &mut self.window,
            1 => &mut self.layer0,
            2 => &mut self.layer1,
            _ => panic!("Illegal compositor index!"),
        };
        compositor.append_internal(
            self.clipstack,
            self.surface_dim,
            self.offset,
            layer.dest.topleft() + parent_pos.to_vector(),
            layer.dest.dim(),
            uv.min.to_f32().to_array().into(),
            uv.size().to_f32().to_array().into(),
            layer.color.rgba,
            layer.rotation,
            0,
            self.pass,
            self.slice,
            false,
            true,
        )
    }

    #[inline]
    pub fn preprocessed(&mut self, mut data: Data) -> u32 {
        data.pos.0[0] += self.offset.x;
        data.pos.0[1] += self.offset.y;
        data.flags = DataFlags::from_bits(data.flags).into();
        let pass = self.pass;
        let slice = self.slice;
        self.compositor().preprocessed(data, pass, slice)
    }

    #[inline]
    pub fn defer(&mut self, f: impl FnOnce(&Driver, &mut Data) + Send + Sync + 'static) {
        let clip = self.current_clip();
        let offset = self.offset;
        let segment = self.segment();
        let region = segment.regions.last_mut().unwrap();
        if region.end == u32::MAX {
            panic!(
                "Still processing a compute operation! Finish it by calling set_compute_buffer() first."
            );
        }

        let idx = region.end;
        segment.data.push(Default::default());
        segment.defer.insert(idx, (Box::new(f), clip, offset));
        region.end += 1;
    }

    pub fn append_custom(
        &mut self,
        f: impl FnMut(&Driver, &mut wgpu::RenderPass<'_>, PxVector) + Send + Sync + 'static,
    ) {
        let index = self.segment().regions.last().unwrap().end;
        if index == u32::MAX {
            panic!(
                "Still processing a compute operation! Finish it by calling set_compute_buffer() first."
            );
        }
        let offset = self.offset;
        self.segment().custom.push((index, Box::new(f), offset));
    }

    /// Returns the GPU buffer and the current offset, which allows a compute
    /// shader to accumulate commands in the GPU buffer directly, provided
    /// it calls set_compute_buffer afterwards with the command count.
    /// Attempting to insert a non-GPU command before calling set_compute_buffer
    /// will panic.
    pub fn get_compute_buffer(&mut self) -> (&Buffer, u32) {
        let offset = self.segment().regions.last().unwrap().end;
        if offset == u32::MAX {
            panic!(
                "Still processing a compute operation! Finish it by calling set_compute_buffer() first."
            );
        }
        self.segment().regions.push(offset..u32::MAX);
        (&self.segment().buffer, offset)
    }

    /// After executing a compute shader that added a series of compositor
    /// commands to the command buffer, this must be called with the number
    /// of commands that were contiguously inserted into the buffer.
    pub fn set_compute_buffer(&mut self, count: u32) {
        let region = self.segment().regions.last_mut().unwrap();
        region.start += count;
        region.end = region.start;
    }

    pub fn reserve(&mut self, driver: &Driver) {
        let pass = self.pass;
        let slice = self.slice;
        self.compositor().reserve(driver, pass, slice);
    }
}

#[bitfield_struct::bitfield(u32)]
pub struct DataFlags {
    #[bits(16)]
    pub clip: u16,
    #[bits(8)]
    pub tex: u8,
    #[bits(6)]
    pub __: u8,
    pub layer: bool,
    pub raw: bool,
}

// Renderdoc Format:
// struct Data {
// 	float pos[2];
// 	float dim[2];
//  float uv[2];
//  float uvdim[2];
// 	uint32_t color;
// 	float rotation;
// 	uint32_t texclip;
//  char padding[4];
// };
// Data d[];

#[repr(C)]
#[derive(Debug, Clone, Copy, Default, PartialEq, bytemuck::NoUninit)]
pub struct Data {
    pub pos: Vec2f,
    pub dim: Vec2f,
    pub uv: Vec2f,
    pub uvdim: Vec2f,
    pub color: u32, // Encoded as a non-linear, non-premultiplied sRGB32 color
    pub rotation: f32,
    pub flags: u32,
    pub _padding: [u8; 4], // We have to manually specify this to satisfy bytemuck
}

static_assertions::const_assert_eq!(std::mem::size_of::<Data>(), 48);

// Our shader will assemble a rotation based on this matrix, but transposed:
// [ cos(r) -sin(r) 0 (x - x*cos(r) + y*sin(r)) ]
// [ sin(r)  cos(r) 0 (y - x*sin(r) - y*cos(r)) ]
// [ 0       0      1 0 ]
// [ 0       0      0 1 ] ^ -1