lampshade 0.13.0

Fast, composable GPU primitives for Rust applications using wgpu and WGSL.
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
use std::sync::Mutex;

use wgpu::util::DeviceExt;

use crate::common::buffers::BufferRange;
use crate::{Context, Error, common};

use super::eight_bit::NativeKeyValueSoaSorter;
use super::key_value_sorter::KeyValueSorter;
use super::pipeline::{
    EIGHT_BIT_BLOCK_SIZE, EIGHT_BIT_WORKGROUP_STORAGE_BYTES, RadixVariant, SortItemKind,
};

const U32_SIZE_BYTES: u64 = size_of::<u32>() as u64;
const KEY_VALUE_SIZE_BYTES: u64 = 2 * U32_SIZE_BYTES;
const BRIDGE_BLOCK_SIZE: u32 = 256;
const MAX_WORKGROUPS_X: u32 = 65_535;
const WORKSPACE_GROWTH_BYTES: u64 = 16 * 1024 * 1024;

/// Device features and limits that enable Lampshade's accelerated SoA sorter.
///
/// When `accelerated` is false, request the application's normal features and
/// limits; [`KeyValueSoaSorter`] will use its portable backend.
#[derive(Clone, Debug)]
pub struct KeyValueSoaRequirements {
    additional_features: wgpu::Features,
    min_compute_invocations: u32,
    min_compute_workgroup_size_x: u32,
    min_compute_workgroup_storage_size: u32,
    /// Whether requesting these additions enables the native SoA backend.
    pub accelerated: bool,
}

impl KeyValueSoaRequirements {
    /// Adds the optional features required by the native SoA backend.
    pub fn features(&self, existing: wgpu::Features) -> wgpu::Features {
        existing | self.additional_features
    }

    /// Raises only the limits required by the native SoA backend.
    ///
    /// All unrelated application limits, including storage-buffer sizes, are
    /// preserved.
    pub fn limits(&self, mut existing: wgpu::Limits) -> wgpu::Limits {
        existing.max_compute_invocations_per_workgroup = existing
            .max_compute_invocations_per_workgroup
            .max(self.min_compute_invocations);
        existing.max_compute_workgroup_size_x = existing
            .max_compute_workgroup_size_x
            .max(self.min_compute_workgroup_size_x);
        existing.max_compute_workgroup_storage_size = existing
            .max_compute_workgroup_storage_size
            .max(self.min_compute_workgroup_storage_size);
        existing
    }
}

/// Stable in-place radix sort for separate `u32` key and value buffers.
///
/// [`Self::new`] selects the accelerated native-SoA implementation when the
/// enabled device supports it and otherwise bridges through Lampshade's
/// portable key/value radix sorter. Keys and values remain in separate caller
/// buffers in both cases.
pub struct KeyValueSoaSorter {
    backend: SoaBackend,
    fixed: Option<FixedPlan>,
}

enum SoaBackend {
    Native(Box<NativeKeyValueSoaSorter>),
    Portable(Box<PortableKeyValueSoaSorter>),
}

struct FixedPlan {
    keys: wgpu::Buffer,
    values: wgpu::Buffer,
    count: wgpu::Buffer,
    num_items: u32,
}

impl KeyValueSoaSorter {
    /// Returns the additions to a default device request that enable the
    /// accelerated native-SoA backend on this adapter.
    pub fn requirements(adapter: &wgpu::Adapter) -> KeyValueSoaRequirements {
        let adapter_info = adapter.get_info();
        let adapter_limits = adapter.limits();
        let accelerated = RadixVariant::for_adapter(
            SortItemKind::KeyValue,
            &adapter_info,
            adapter.features(),
            &adapter_limits,
        )
        .uses_eight_bit_pipeline();
        KeyValueSoaRequirements {
            additional_features: if accelerated {
                wgpu::Features::SUBGROUP
            } else {
                wgpu::Features::empty()
            },
            min_compute_invocations: if accelerated { EIGHT_BIT_BLOCK_SIZE } else { 0 },
            min_compute_workgroup_size_x: if accelerated { EIGHT_BIT_BLOCK_SIZE } else { 0 },
            min_compute_workgroup_storage_size: if accelerated {
                EIGHT_BIT_WORKGROUP_STORAGE_BYTES
            } else {
                0
            },
            accelerated,
        }
    }

    /// Creates an adapter-selected sorter from an existing device and queue.
    ///
    /// The native backend is selected only if the device was created with all
    /// requirements returned by [`Self::requirements`].
    pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
        let adapter_info = device.adapter_info();
        let backend =
            if let Some(sorter) = NativeKeyValueSoaSorter::new_for_adapter(device, &adapter_info) {
                SoaBackend::Native(Box::new(sorter))
            } else {
                SoaBackend::Portable(Box::new(PortableKeyValueSoaSorter::new_for_adapter(
                    device,
                    queue,
                    &adapter_info,
                )))
            };
        Self {
            backend,
            fixed: None,
        }
    }

    /// Creates a sorter that always uses the portable SoA bridge.
    pub fn new_portable(device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
        Self {
            backend: SoaBackend::Portable(Box::new(PortableKeyValueSoaSorter::new_portable(
                device, queue,
            ))),
            fixed: None,
        }
    }

    /// Creates an adapter-selected sorter from Lampshade's convenience context.
    pub fn from_context(context: &Context) -> Self {
        Self::new(&context.device, &context.queue)
    }

    /// Creates the native backend only, returning `None` when unsupported.
    ///
    /// New code should generally use [`Self::new`] so unsupported devices fall
    /// back transparently.
    pub fn new_native_for_adapter(
        device: &wgpu::Device,
        adapter_info: &wgpu::AdapterInfo,
    ) -> Option<Self> {
        NativeKeyValueSoaSorter::new_for_adapter(device, adapter_info).map(|sorter| Self {
            backend: SoaBackend::Native(Box::new(sorter)),
            fixed: None,
        })
    }

    /// Native-only constructor retained for 0.10 API compatibility.
    pub fn new_for_adapter(
        device: &wgpu::Device,
        adapter_info: &wgpu::AdapterInfo,
    ) -> Option<Self> {
        Self::new_native_for_adapter(device, adapter_info)
    }

    /// Returns true when this instance uses the validated native SoA backend.
    pub const fn is_accelerated(&self) -> bool {
        matches!(&self.backend, SoaBackend::Native(_))
    }

    /// Reserves internal storage for fixed or GPU-counted sorts up to `capacity`.
    ///
    /// Call a `prepare_*` method afterward to bind a particular buffer set.
    pub fn reserve(&mut self, capacity: u32) -> Result<(), Error> {
        self.fixed = None;
        match &mut self.backend {
            SoaBackend::Native(sorter) => sorter.reserve(capacity),
            SoaBackend::Portable(sorter) => sorter.reserve(capacity),
        }
    }

    /// Prepares a reusable fixed-count binding plan.
    ///
    /// `keys` and `values` require `STORAGE` usage, must be distinct buffers,
    /// and must each contain at least `num_items` `u32` elements.
    pub fn prepare_sort(
        &mut self,
        keys: &wgpu::Buffer,
        values: &wgpu::Buffer,
        num_items: u32,
    ) -> Result<(), Error> {
        let count = self
            .fixed
            .as_ref()
            .and_then(|plan| (plan.num_items == num_items).then(|| plan.count.clone()));
        let count = count.unwrap_or_else(|| match &self.backend {
            SoaBackend::Native(sorter) => {
                sorter
                    .device()
                    .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                        label: Some("Fixed SoA Sort Count"),
                        contents: bytemuck::bytes_of(&num_items),
                        usage: wgpu::BufferUsages::STORAGE,
                    })
            }
            SoaBackend::Portable(sorter) => {
                sorter
                    .device
                    .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                        label: Some("Fixed SoA Sort Count"),
                        contents: bytemuck::bytes_of(&num_items),
                        usage: wgpu::BufferUsages::STORAGE,
                    })
            }
        });
        self.prepare_counted_from_word(keys, values, &count, 0, num_items)?;
        self.fixed = Some(FixedPlan {
            keys: keys.clone(),
            values: values.clone(),
            count,
            num_items,
        });
        Ok(())
    }

    /// Records a fixed-count stable in-place SoA sort.
    ///
    /// The first call for a buffer pair prepares its reusable bindings. Call
    /// [`Self::prepare_sort`] during setup to keep that work out of recording.
    pub fn record_sort(
        &mut self,
        encoder: &mut wgpu::CommandEncoder,
        keys: &wgpu::Buffer,
        values: &wgpu::Buffer,
        num_items: u32,
    ) -> Result<(), Error> {
        if !self.fixed_matches(keys, values, num_items) {
            self.prepare_sort(keys, values, num_items)?;
        }
        self.record_reserved_sort(encoder, keys, values, num_items)
    }

    /// Records a previously prepared fixed-count sort.
    ///
    /// On the native backend this performs no buffer, bind-group, or pipeline
    /// creation. The portable bridge may still prepare transient state inside
    /// its general-purpose radix sorter.
    pub fn record_reserved_sort(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        keys: &wgpu::Buffer,
        values: &wgpu::Buffer,
        num_items: u32,
    ) -> Result<(), Error> {
        let plan = self.fixed.as_ref().ok_or(Error::BufferTooSmall {
            name: "SoA fixed sort plan",
            required: 1,
            actual: 0,
        })?;
        if plan.keys != *keys || plan.values != *values || plan.num_items != num_items {
            return Err(Error::BufferTooSmall {
                name: "SoA fixed sort plan",
                required: 1,
                actual: 0,
            });
        }
        self.record_reserved_sort_counted_from_word(
            encoder,
            keys,
            values,
            &plan.count,
            0,
            num_items,
        )
    }

    /// Records a stable sort of a GPU-counted prefix using count word zero.
    ///
    /// The GPU count is clamped to `capacity`. Keys and values beyond that
    /// prefix are unspecified. All three buffers require `STORAGE` usage and
    /// must have distinct handles.
    pub fn record_sort_counted(
        &mut self,
        encoder: &mut wgpu::CommandEncoder,
        keys: &wgpu::Buffer,
        values: &wgpu::Buffer,
        count: &wgpu::Buffer,
        capacity: u32,
    ) -> Result<(), Error> {
        self.record_sort_counted_from_word(encoder, keys, values, count, 0, capacity)
    }

    /// Records a stable GPU-counted prefix sort from a metadata buffer.
    ///
    /// `count_word` is a `u32` index within `count`, not a byte offset. The
    /// selected count is clamped to `capacity`; output beyond it is unspecified.
    #[allow(clippy::too_many_arguments)]
    pub fn record_sort_counted_from_word(
        &mut self,
        encoder: &mut wgpu::CommandEncoder,
        keys: &wgpu::Buffer,
        values: &wgpu::Buffer,
        count: &wgpu::Buffer,
        count_word: u32,
        capacity: u32,
    ) -> Result<(), Error> {
        self.fixed = None;
        match &mut self.backend {
            SoaBackend::Native(sorter) => sorter
                .record_sort_counted_from_word(encoder, keys, values, count, count_word, capacity),
            SoaBackend::Portable(sorter) => sorter
                .record_sort_counted_from_word(encoder, keys, values, count, count_word, capacity),
        }
    }

    /// Prepares buffers and binding state for a GPU-counted sort.
    ///
    /// Preparing a counted sort invalidates any fixed-count plan held by this
    /// sorter instance.
    #[allow(clippy::too_many_arguments)]
    pub fn prepare_counted_from_word(
        &mut self,
        keys: &wgpu::Buffer,
        values: &wgpu::Buffer,
        count: &wgpu::Buffer,
        count_word: u32,
        capacity: u32,
    ) -> Result<(), Error> {
        self.fixed = None;
        match &mut self.backend {
            SoaBackend::Native(sorter) => {
                sorter.prepare_counted_from_word(keys, values, count, count_word, capacity)
            }
            SoaBackend::Portable(sorter) => {
                sorter.prepare_counted_from_word(keys, values, count, count_word, capacity)
            }
        }
    }

    /// Records a previously prepared GPU-counted sort.
    ///
    /// Native recording is allocation-free. The portable backend preserves
    /// the same command-ordering contract but may build transient state inside
    /// the general-purpose radix sorter.
    #[allow(clippy::too_many_arguments)]
    pub fn record_reserved_sort_counted_from_word(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        keys: &wgpu::Buffer,
        values: &wgpu::Buffer,
        count: &wgpu::Buffer,
        count_word: u32,
        capacity: u32,
    ) -> Result<(), Error> {
        match &self.backend {
            SoaBackend::Native(sorter) => sorter.record_reserved_sort_counted_from_word(
                encoder, keys, values, count, count_word, capacity,
            ),
            SoaBackend::Portable(sorter) => sorter.record_reserved_sort_counted_from_word(
                encoder, keys, values, count, count_word, capacity,
            ),
        }
    }

    fn fixed_matches(&self, keys: &wgpu::Buffer, values: &wgpu::Buffer, num_items: u32) -> bool {
        self.fixed.as_ref().is_some_and(|plan| {
            plan.keys == *keys && plan.values == *values && plan.num_items == num_items
        })
    }
}

struct PortableKeyValueSoaSorter {
    device: wgpu::Device,
    sorter: Mutex<KeyValueSorter>,
    pack_layout: wgpu::BindGroupLayout,
    unpack_layout: wgpu::BindGroupLayout,
    pack: wgpu::ComputePipeline,
    unpack: wgpu::ComputePipeline,
    workspace: Option<PortableWorkspace>,
    bindings: Option<PortableBindings>,
}

struct PortableWorkspace {
    capacity_bytes: u64,
    packed_input: wgpu::Buffer,
    packed_output: wgpu::Buffer,
    clamped_count: wgpu::Buffer,
}

struct PortableBindings {
    keys: wgpu::Buffer,
    values: wgpu::Buffer,
    count: wgpu::Buffer,
    count_word: u32,
    capacity: u32,
    workspace_capacity: u64,
    _config: wgpu::Buffer,
    pack: wgpu::BindGroup,
    unpack: wgpu::BindGroup,
}

impl PortableKeyValueSoaSorter {
    fn new_for_adapter(
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        adapter_info: &wgpu::AdapterInfo,
    ) -> Self {
        Self::with_sorter(
            device,
            KeyValueSorter::new_for_adapter(device, queue, adapter_info),
        )
    }

    fn new_portable(device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
        Self::with_sorter(device, KeyValueSorter::new(device, queue))
    }

    fn with_sorter(device: &wgpu::Device, sorter: KeyValueSorter) -> Self {
        let pack_layout = bridge_layout(device, "SoA Pack Layout", true);
        let unpack_layout = bridge_layout(device, "SoA Unpack Layout", false);
        let pack = bridge_pipeline(
            device,
            &pack_layout,
            include_str!("soa_pack.wgsl"),
            "SoA Pack Pipeline",
        );
        let unpack = bridge_pipeline(
            device,
            &unpack_layout,
            include_str!("soa_unpack.wgsl"),
            "SoA Unpack Pipeline",
        );
        Self {
            device: device.clone(),
            sorter: Mutex::new(sorter),
            pack_layout,
            unpack_layout,
            pack,
            unpack,
            workspace: None,
            bindings: None,
        }
    }

    fn reserve(&mut self, capacity: u32) -> Result<(), Error> {
        if capacity == 0 {
            return Ok(());
        }
        let packed_bytes =
            common::math::checked_byte_size(u64::from(capacity), KEY_VALUE_SIZE_BYTES)?;
        self.ensure_workspace(packed_bytes)?;
        self.sorter
            .get_mut()
            .expect("portable SoA sorter lock is not poisoned")
            .reserve_counted(capacity)
    }

    #[allow(clippy::too_many_arguments)]
    fn record_sort_counted_from_word(
        &mut self,
        encoder: &mut wgpu::CommandEncoder,
        keys: &wgpu::Buffer,
        values: &wgpu::Buffer,
        count: &wgpu::Buffer,
        count_word: u32,
        capacity: u32,
    ) -> Result<(), Error> {
        self.prepare_counted_from_word(keys, values, count, count_word, capacity)?;
        self.record_reserved_sort_counted_from_word(
            encoder, keys, values, count, count_word, capacity,
        )
    }

    fn prepare_counted_from_word(
        &mut self,
        keys: &wgpu::Buffer,
        values: &wgpu::Buffer,
        count: &wgpu::Buffer,
        count_word: u32,
        capacity: u32,
    ) -> Result<(), Error> {
        let Some((keys_range, values_range, count_range, count_bytes)) =
            self.validate_inputs(keys, values, count, count_word, capacity)?
        else {
            self.bindings = None;
            return Ok(());
        };
        self.reserve(capacity)?;
        if self.bindings_match(keys, values, count, count_word, capacity) {
            return Ok(());
        }
        let workspace = self
            .workspace
            .as_ref()
            .expect("portable SoA workspace is prepared");
        let config = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Portable SoA Bridge Configuration"),
                contents: bytemuck::cast_slice(&[capacity, count_word, 0_u32, 0]),
                usage: wgpu::BufferUsages::UNIFORM,
            });
        let pack = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Portable SoA Pack Bind Group"),
            layout: &self.pack_layout,
            entries: &[
                bridge_entry(0, keys_range.binding(u64::from(capacity) * U32_SIZE_BYTES)),
                bridge_entry(
                    1,
                    values_range.binding(u64::from(capacity) * U32_SIZE_BYTES),
                ),
                bridge_entry(2, workspace.packed_input.as_entire_binding()),
                bridge_entry(3, count_range.binding(count_bytes)),
                bridge_entry(4, config.as_entire_binding()),
                bridge_entry(5, workspace.clamped_count.as_entire_binding()),
            ],
        });
        let unpack = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Portable SoA Unpack Bind Group"),
            layout: &self.unpack_layout,
            entries: &[
                bridge_entry(0, workspace.packed_output.as_entire_binding()),
                bridge_entry(1, keys_range.binding(u64::from(capacity) * U32_SIZE_BYTES)),
                bridge_entry(
                    2,
                    values_range.binding(u64::from(capacity) * U32_SIZE_BYTES),
                ),
                bridge_entry(3, count_range.binding(count_bytes)),
                bridge_entry(4, config.as_entire_binding()),
            ],
        });
        self.bindings = Some(PortableBindings {
            keys: keys.clone(),
            values: values.clone(),
            count: count.clone(),
            count_word,
            capacity,
            workspace_capacity: workspace.capacity_bytes,
            _config: config,
            pack,
            unpack,
        });
        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    fn record_reserved_sort_counted_from_word(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        keys: &wgpu::Buffer,
        values: &wgpu::Buffer,
        count: &wgpu::Buffer,
        count_word: u32,
        capacity: u32,
    ) -> Result<(), Error> {
        if self
            .validate_inputs(keys, values, count, count_word, capacity)?
            .is_none()
        {
            return Ok(());
        }
        if !self.bindings_match(keys, values, count, count_word, capacity) {
            return Err(Error::BufferTooSmall {
                name: "SoA sort binding plan",
                required: 1,
                actual: 0,
            });
        }
        let workspace = self
            .workspace
            .as_ref()
            .expect("portable SoA workspace is prepared");
        let bindings = self
            .bindings
            .as_ref()
            .expect("portable SoA bindings are prepared");
        let (groups_x, groups_y) = bridge_dispatch(capacity);
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Portable SoA Pack"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&self.pack);
            pass.set_bind_group(0, &bindings.pack, &[]);
            pass.dispatch_workgroups(groups_x, groups_y, 1);
        }
        self.sorter
            .lock()
            .expect("portable SoA sorter lock is not poisoned")
            .record_sort_counted(
                encoder,
                &workspace.packed_input,
                &workspace.packed_output,
                &workspace.clamped_count,
                capacity,
            )?;
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Portable SoA Unpack"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&self.unpack);
            pass.set_bind_group(0, &bindings.unpack, &[]);
            pass.dispatch_workgroups(groups_x, groups_y, 1);
        }
        Ok(())
    }

    fn validate_inputs<'a>(
        &self,
        keys: &'a wgpu::Buffer,
        values: &'a wgpu::Buffer,
        count: &'a wgpu::Buffer,
        count_word: u32,
        capacity: u32,
    ) -> Result<Option<(BufferRange<'a>, BufferRange<'a>, BufferRange<'a>, u64)>, Error> {
        for (first, first_name, second, second_name) in [
            (keys, "sort keys", values, "sort values"),
            (keys, "sort keys", count, "sort item count"),
            (values, "sort values", count, "sort item count"),
        ] {
            if first == second {
                return Err(Error::BufferAlias {
                    first: first_name,
                    second: second_name,
                });
            }
        }
        let count_bytes = u64::from(count_word)
            .checked_add(1)
            .and_then(|words| words.checked_mul(U32_SIZE_BYTES))
            .ok_or(Error::SizeOverflow)?;
        let item_bytes = common::math::checked_byte_size(u64::from(capacity), U32_SIZE_BYTES)?;
        let packed_bytes =
            common::math::checked_byte_size(u64::from(capacity), KEY_VALUE_SIZE_BYTES)?;
        let keys = BufferRange::whole(keys);
        let values = BufferRange::whole(values);
        let count = BufferRange::whole(count);
        keys.validate("sort keys", item_bytes, wgpu::BufferUsages::STORAGE)?;
        values.validate("sort values", item_bytes, wgpu::BufferUsages::STORAGE)?;
        count.validate("sort item count", count_bytes, wgpu::BufferUsages::STORAGE)?;
        for (range, name, size) in [
            (keys, "sort keys", item_bytes),
            (values, "sort values", item_bytes),
            (count, "sort item count", count_bytes),
        ] {
            range.validate_storage_offset(&self.device, name)?;
            range.validate_storage_binding_size(&self.device, size)?;
        }
        common::buffers::validate_storage_binding_size(&self.device, packed_bytes)?;
        if capacity == 0 {
            Ok(None)
        } else {
            Ok(Some((keys, values, count, count_bytes)))
        }
    }

    fn ensure_workspace(&mut self, requested_bytes: u64) -> Result<(), Error> {
        if self
            .workspace
            .as_ref()
            .is_some_and(|workspace| workspace.capacity_bytes >= requested_bytes)
        {
            return Ok(());
        }
        let capacity_bytes = workspace_capacity(requested_bytes)?;
        common::buffers::validate_storage_binding_size(&self.device, capacity_bytes)?;
        self.workspace = Some(PortableWorkspace {
            capacity_bytes,
            packed_input: common::buffers::create_empty_storage_buffer(
                &self.device,
                capacity_bytes,
            ),
            packed_output: common::buffers::create_empty_storage_buffer(
                &self.device,
                capacity_bytes,
            ),
            clamped_count: common::buffers::create_empty_storage_buffer(
                &self.device,
                U32_SIZE_BYTES,
            ),
        });
        self.bindings = None;
        Ok(())
    }

    fn bindings_match(
        &self,
        keys: &wgpu::Buffer,
        values: &wgpu::Buffer,
        count: &wgpu::Buffer,
        count_word: u32,
        capacity: u32,
    ) -> bool {
        let workspace_capacity = self
            .workspace
            .as_ref()
            .map_or(0, |workspace| workspace.capacity_bytes);
        self.bindings.as_ref().is_some_and(|bindings| {
            bindings.keys == *keys
                && bindings.values == *values
                && bindings.count == *count
                && bindings.count_word == count_word
                && bindings.capacity == capacity
                && bindings.workspace_capacity == workspace_capacity
        })
    }
}

fn bridge_layout(device: &wgpu::Device, label: &'static str, pack: bool) -> wgpu::BindGroupLayout {
    let mut entries = vec![
        common::buffers::bind_entry(0, true, false),
        common::buffers::bind_entry(1, pack, false),
        common::buffers::bind_entry(2, false, false),
        common::buffers::bind_entry(3, true, false),
        common::buffers::bind_entry(4, false, true),
    ];
    if pack {
        entries.push(common::buffers::bind_entry(5, false, false));
    }
    device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some(label),
        entries: &entries,
    })
}

fn bridge_pipeline(
    device: &wgpu::Device,
    layout: &wgpu::BindGroupLayout,
    source: &str,
    label: &'static str,
) -> wgpu::ComputePipeline {
    common::shader::create_compute_pipeline(device, layout, source, label, "main", None)
}

fn bridge_entry(binding: u32, resource: wgpu::BindingResource<'_>) -> wgpu::BindGroupEntry<'_> {
    wgpu::BindGroupEntry { binding, resource }
}

fn bridge_dispatch(capacity: u32) -> (u32, u32) {
    let groups = capacity.div_ceil(BRIDGE_BLOCK_SIZE);
    let groups_x = groups.min(MAX_WORKGROUPS_X);
    (groups_x, groups.div_ceil(groups_x))
}

fn workspace_capacity(requested: u64) -> Result<u64, Error> {
    if requested < WORKSPACE_GROWTH_BYTES {
        requested
            .max(KEY_VALUE_SIZE_BYTES)
            .checked_next_power_of_two()
            .ok_or(Error::SizeOverflow)
    } else {
        common::math::checked_align_to(requested, WORKSPACE_GROWTH_BYTES)
    }
}

#[cfg(test)]
mod tests {
    use super::{BRIDGE_BLOCK_SIZE, KeyValueSoaRequirements, bridge_dispatch, workspace_capacity};

    #[test]
    fn bridge_dispatch_covers_large_capacities() {
        assert_eq!(bridge_dispatch(1), (1, 1));
        assert_eq!(bridge_dispatch(BRIDGE_BLOCK_SIZE * 65_535), (65_535, 1));
        assert_eq!(bridge_dispatch(BRIDGE_BLOCK_SIZE * 65_535 + 1), (65_535, 2));
    }

    #[test]
    fn bridge_workspace_never_allocates_zero_bytes() {
        assert_eq!(workspace_capacity(0).unwrap(), 8);
        assert_eq!(workspace_capacity(9).unwrap(), 16);
    }

    #[test]
    fn requirements_merge_without_lowering_unrelated_application_limits() {
        let requirements = KeyValueSoaRequirements {
            additional_features: wgpu::Features::SUBGROUP,
            min_compute_invocations: 256,
            min_compute_workgroup_size_x: 256,
            min_compute_workgroup_storage_size: 16_388,
            accelerated: true,
        };
        let application_limits = wgpu::Limits {
            max_buffer_size: 987_654_321,
            max_compute_invocations_per_workgroup: 128,
            max_compute_workgroup_size_x: 128,
            max_compute_workgroup_storage_size: 16_384,
            ..wgpu::Limits::default()
        };

        let merged_features = requirements.features(wgpu::Features::TIMESTAMP_QUERY);
        let merged_limits = requirements.limits(application_limits);

        assert!(merged_features.contains(wgpu::Features::TIMESTAMP_QUERY));
        assert!(merged_features.contains(wgpu::Features::SUBGROUP));
        assert_eq!(merged_limits.max_buffer_size, 987_654_321);
        assert_eq!(merged_limits.max_compute_invocations_per_workgroup, 256);
        assert_eq!(merged_limits.max_compute_workgroup_size_x, 256);
        assert_eq!(merged_limits.max_compute_workgroup_storage_size, 16_388);
    }
}