ultrafast-gpu 0.1.1

Thin native GPU layer for ultrafast (Metal today, Vulkan next)
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
//! Metal backend (macOS, Apple silicon first).
//!
//! Kernels come from MSL produced by the Slang compiler. The binding layout is
//! fixed for every kernel ultrafast generates:
//!
//! | binding      | contents                                        |
//! |--------------|-------------------------------------------------|
//! | texture(0)   | output                                          |
//! | texture(1..) | inputs                                          |
//! | sampler(0)   | bilinear, clamp-to-zero (transparent outside)   |
//! | buffer(0)    | params (float array), from the per-frame arena  |
//! | buffer(1..)  | extra storage buffers (geometry, accumulators)  |

use crate::stats::Counters;
use crate::{GpuError, GpuTiming, PixelFormat, TextureDesc, TransferStats};
use objc2::rc::Retained;
use objc2::runtime::ProtocolObject;
use objc2_foundation::NSString;
use objc2_metal::{
    MTLBlendFactor, MTLBlendOperation, MTLBlitCommandEncoder, MTLBuffer, MTLClearColor, MTLCommandBuffer,
    MTLCommandBufferStatus, MTLCommandEncoder, MTLCommandQueue, MTLCompareFunction, MTLComputeCommandEncoder,
    MTLComputePipelineState, MTLCreateSystemDefaultDevice, MTLCullMode, MTLDepthStencilDescriptor,
    MTLDepthStencilState, MTLDevice, MTLIndexType, MTLLibrary, MTLLoadAction, MTLOrigin, MTLPixelFormat,
    MTLPrimitiveType, MTLRegion, MTLRenderCommandEncoder, MTLRenderPassDescriptor, MTLRenderPipelineDescriptor,
    MTLRenderPipelineState, MTLResourceOptions, MTLSamplerAddressMode, MTLSamplerDescriptor, MTLSamplerMinMagFilter,
    MTLSamplerState, MTLSize, MTLStorageMode, MTLStoreAction, MTLTexture, MTLTextureDescriptor, MTLTextureType,
    MTLTextureUsage, MTLViewport,
};
use std::ptr::NonNull;
use std::sync::{Arc, Mutex};

/// Metal objects that Apple documents as thread-safe (device, queue,
/// libraries, pipeline states, sampler states, resources) are shared across
/// the render and compile threads through this wrapper.
struct Shared<T>(T);
// SAFETY: only used for Metal objects documented as safe to use from any
// thread; mutation of resource contents is ordered by command buffers.
unsafe impl<T> Send for Shared<T> {}
unsafe impl<T> Sync for Shared<T> {}

impl<T> std::ops::Deref for Shared<T> {
    type Target = T;
    fn deref(&self) -> &T {
        &self.0
    }
}

type P<T> = Retained<ProtocolObject<T>>;

/// Frames that may be encoded while earlier ones still execute.
const FRAMES_IN_FLIGHT: usize = 3;
/// Param arena per in-flight frame.
const ARENA_BYTES: usize = 32 << 20;
const ARENA_ALIGN: usize = 256;

struct RingSlot {
    arena: Shared<P<dyn MTLBuffer>>,
    pending: Option<Shared<P<dyn MTLCommandBuffer>>>,
    /// A frame is being recorded into it (from `begin` until it is
    /// submitted or dropped): another thread must not take it.
    recording: bool,
}

struct Inner {
    device: Shared<P<dyn MTLDevice>>,
    queue: Shared<P<dyn MTLCommandQueue>>,
    sampler: Shared<P<dyn MTLSamplerState>>,
    ring: Mutex<(usize, Vec<RingSlot>)>,
    counters: Counters,
}

/// Handle to the GPU. Cheap to clone and safe to share across threads.
#[derive(Clone)]
pub struct Device {
    inner: Arc<Inner>,
}

/// A compiled module holding one or more kernels.
#[derive(Clone)]
pub struct Library {
    raw: Arc<Shared<P<dyn MTLLibrary>>>,
    device: Device,
}

/// A compiled compute pipeline. Cheap to clone.
#[derive(Clone)]
pub struct Kernel {
    name: Arc<str>,
    pipeline: Arc<Shared<P<dyn MTLComputePipelineState>>>,
}

impl Kernel {
    pub fn name(&self) -> &str {
        &self.name
    }
}

/// A GPU texture. Cheap to clone (reference counted).
#[derive(Clone)]
pub struct Texture {
    raw: Arc<Shared<P<dyn MTLTexture>>>,
    desc: TextureDesc,
}

impl Texture {
    pub fn desc(&self) -> TextureDesc {
        self.desc
    }

    /// Identity for caching: two handles to the same GPU texture compare equal.
    pub fn id(&self) -> usize {
        Arc::as_ptr(&self.raw) as usize
    }

    pub fn raw(&self) -> &ProtocolObject<dyn MTLTexture> {
        &self.raw
    }
}

/// A GPU storage buffer.
#[derive(Clone)]
pub struct Buffer {
    raw: Arc<Shared<P<dyn MTLBuffer>>>,
    len: usize,
}

impl Buffer {
    pub fn len(&self) -> usize {
        self.len
    }

    /// Whether this handle is the buffer's only one (nothing else, a frame
    /// in flight included, still holds it).
    pub fn is_unique(&self) -> bool {
        Arc::strong_count(&self.raw) == 1
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// The buffer's memory, for filling it from the CPU in place. Writers
    /// report the bytes with [`Device::note_upload`].
    ///
    /// # Safety
    /// No GPU work may access the buffer, and no other slice of it may be
    /// alive, while the returned slice is.
    #[allow(clippy::mut_from_ref)]
    pub unsafe fn mapped(&self) -> &mut [u8] {
        unsafe { std::slice::from_raw_parts_mut(self.raw.0.contents().as_ptr().cast::<u8>(), self.len) }
    }
}

fn mtl_format(f: PixelFormat) -> MTLPixelFormat {
    match f {
        PixelFormat::Rgba16Float => MTLPixelFormat::RGBA16Float,
        PixelFormat::Rgba8Unorm => MTLPixelFormat::RGBA8Unorm,
        PixelFormat::Bgra8Unorm => MTLPixelFormat::BGRA8Unorm,
        PixelFormat::R32Float => MTLPixelFormat::R32Float,
        PixelFormat::R16Float => MTLPixelFormat::R16Float,
        PixelFormat::Rgba32Float => MTLPixelFormat::RGBA32Float,
        PixelFormat::Depth32Float => MTLPixelFormat::Depth32Float,
    }
}

impl Device {
    pub fn new() -> Result<Self, GpuError> {
        let device = MTLCreateSystemDefaultDevice().ok_or(GpuError::NoDevice)?;
        let queue = device.newCommandQueue().ok_or(GpuError::NoDevice)?;
        let sd = MTLSamplerDescriptor::new();
        sd.setMinFilter(MTLSamplerMinMagFilter::Linear);
        sd.setMagFilter(MTLSamplerMinMagFilter::Linear);
        sd.setSAddressMode(MTLSamplerAddressMode::ClampToZero);
        sd.setTAddressMode(MTLSamplerAddressMode::ClampToZero);
        let sampler = device.newSamplerStateWithDescriptor(&sd).ok_or(GpuError::NoDevice)?;
        let mut slots = Vec::new();
        for _ in 0..FRAMES_IN_FLIGHT {
            let arena = device
                .newBufferWithLength_options(ARENA_BYTES, MTLResourceOptions::StorageModeShared)
                .ok_or(GpuError::NoDevice)?;
            slots.push(RingSlot {
                arena: Shared(arena),
                pending: None,
                recording: false,
            });
        }
        Ok(Self {
            inner: Arc::new(Inner {
                device: Shared(device),
                queue: Shared(queue),
                sampler: Shared(sampler),
                ring: Mutex::new((0, slots)),
                counters: Counters::default(),
            }),
        })
    }

    pub fn name(&self) -> String {
        self.inner.device.name().to_string()
    }

    pub fn backend(&self) -> &'static str {
        "metal"
    }

    /// A handle for a renderer on another thread (Metal queues are shared
    /// safely; same handle).
    pub fn fork(&self) -> Result<Device, GpuError> {
        Ok(self.clone())
    }

    /// GPU memory this device has allocated.
    pub fn memory_in_use(&self) -> u64 {
        self.inner.device.currentAllocatedSize() as u64
    }

    /// Metal command buffers release what they used when they complete.
    pub fn reclaim(&self) {}

    /// Pipelines loaded / compiled (Metal keeps compiled pipelines in the
    /// system's own shader cache; not counted).
    pub fn pipeline_stats(&self) -> (u64, u64, f64) {
        (0, 0, 0.0)
    }

    /// The largest groups of live allocations (not tracked on Metal).
    pub fn memory_breakdown(&self, _top: usize) -> String {
        String::new()
    }

    /// (bytes in live allocations, bytes reserved for them).
    pub fn memory_allocated(&self) -> (u64, u64) {
        let n = self.memory_in_use();
        (n, n)
    }

    /// GPU memory the process can use without hurting performance.
    pub fn memory_bytes(&self) -> u64 {
        if let Some(mb) = std::env::var("ULTRAFAST_GPU_MEMORY_MB")
            .ok()
            .and_then(|v| v.parse::<u64>().ok())
        {
            // Pretend the GPU is this size (budget testing).
            return mb << 20;
        }
        self.inner.device.recommendedMaxWorkingSetSize()
    }

    pub fn stats(&self) -> TransferStats {
        self.inner.counters.snapshot()
    }

    /// Counts bytes the CPU wrote into GPU-visible memory directly
    /// ([`Buffer::mapped`]) as an upload.
    pub fn note_upload(&self, bytes: usize) {
        Counters::add(&self.inner.counters.uploads, 1);
        Counters::add(&self.inner.counters.upload_bytes, bytes as u64);
    }

    /// Compiles MSL source (as produced by Slang) into a library. Safe to
    /// call from any thread.
    pub fn library(&self, msl: &[u8]) -> Result<Library, GpuError> {
        let msl = std::str::from_utf8(msl).map_err(|e| GpuError::ShaderCompile(e.to_string()))?;
        let raw = self
            .inner
            .device
            .newLibraryWithSource_options_error(&NSString::from_str(msl), None)
            .map_err(|e| GpuError::ShaderCompile(e.localizedDescription().to_string()))?;
        Ok(Library {
            raw: Arc::new(Shared(raw)),
            device: self.clone(),
        })
    }

    pub fn create_texture(&self, desc: TextureDesc) -> Texture {
        let raw = unsafe {
            let d = MTLTextureDescriptor::texture2DDescriptorWithPixelFormat_width_height_mipmapped(
                mtl_format(desc.format),
                desc.width.max(1) as usize,
                desc.height.max(1) as usize,
                false,
            );
            // Render target too, so 3D passes can resolve into any texture.
            d.setUsage(MTLTextureUsage::ShaderRead | MTLTextureUsage::ShaderWrite | MTLTextureUsage::RenderTarget);
            let (storage, options) = if desc.cpu_readable {
                (MTLStorageMode::Shared, MTLResourceOptions::StorageModeShared)
            } else {
                (MTLStorageMode::Private, MTLResourceOptions::StorageModePrivate)
            };
            d.setStorageMode(storage);
            d.setResourceOptions(options);
            self.inner
                .device
                .newTextureWithDescriptor(&d)
                .expect("texture allocation")
        };
        Counters::add(&self.inner.counters.textures_created, 1);
        Texture {
            raw: Arc::new(Shared(raw)),
            desc,
        }
    }

    /// Creates a GPU-private texture filled with `bytes` (tightly packed rows).
    /// Counted as an upload.
    /// Like [`upload_texture`](Self::upload_texture) from bytes already in
    /// a buffer (rows tightly packed).
    pub fn upload_texture_from(&self, desc: TextureDesc, buffer: &Buffer) -> Texture {
        let len = desc.width as usize * desc.height as usize * desc.format.bytes_per_pixel();
        // SAFETY: the buffer was filled before this call and is only read.
        let bytes = unsafe { &buffer.mapped()[..len] };
        self.upload_texture(desc, bytes)
    }

    pub fn upload_texture(&self, desc: TextureDesc, bytes: &[u8]) -> Texture {
        let row = desc.width as usize * desc.format.bytes_per_pixel();
        assert_eq!(bytes.len(), row * desc.height as usize, "upload size mismatch");
        let staging = self.create_texture(TextureDesc {
            cpu_readable: true,
            ..desc
        });
        unsafe {
            staging.raw.replaceRegion_mipmapLevel_withBytes_bytesPerRow(
                region(desc.width, desc.height),
                0,
                NonNull::new(bytes.as_ptr() as *mut _).unwrap(),
                row,
            );
        }
        let tex = self.create_texture(TextureDesc {
            cpu_readable: false,
            ..desc
        });
        let cb = self.inner.queue.commandBuffer().expect("command buffer");
        let blit = cb.blitCommandEncoder().expect("blit encoder");
        unsafe { blit.copyFromTexture_toTexture(&staging.raw, &tex.raw) };
        blit.endEncoding();
        cb.commit();
        Counters::add(&self.inner.counters.uploads, 1);
        Counters::add(&self.inner.counters.upload_bytes, bytes.len() as u64);
        tex
    }

    /// Creates a shared storage buffer. With `init`, contents are copied in
    /// and the copy is counted as an upload.
    pub fn create_buffer(&self, len: usize, init: Option<&[u8]>) -> Buffer {
        let len = len.max(16);
        let raw = self
            .inner
            .device
            .newBufferWithLength_options(len, MTLResourceOptions::StorageModeShared)
            .expect("buffer allocation");
        if let Some(data) = init {
            unsafe {
                std::ptr::copy_nonoverlapping(data.as_ptr(), raw.contents().as_ptr().cast(), data.len().min(len));
            }
            Counters::add(&self.inner.counters.uploads, 1);
            Counters::add(&self.inner.counters.upload_bytes, data.len() as u64);
        }
        Buffer {
            raw: Arc::new(Shared(raw)),
            len,
        }
    }

    /// A buffer only the GPU reads and writes (accumulators, simulation
    /// state). `read_buffer` / `write_buffer` must not be used on it.
    pub fn create_private_buffer(&self, len: usize) -> Buffer {
        let len = len.max(16);
        let raw = self
            .inner
            .device
            .newBufferWithLength_options(len, MTLResourceOptions::StorageModePrivate)
            .expect("buffer allocation");
        Buffer {
            raw: Arc::new(Shared(raw)),
            len,
        }
    }

    /// Overwrites the start of a shared buffer. The caller must ensure the GPU
    /// is not reading it. Counted as an upload.
    /// Copies the first `len` bytes of a buffer to the CPU (the GPU must be
    /// done writing it). Counted as a readback.
    pub fn read_buffer(&self, buf: &Buffer, len: usize) -> Vec<u8> {
        let len = len.min(buf.len);
        let mut out = vec![0u8; len];
        unsafe { std::ptr::copy_nonoverlapping(buf.raw.contents().as_ptr().cast::<u8>(), out.as_mut_ptr(), len) };
        Counters::add(&self.inner.counters.readbacks, 1);
        Counters::add(&self.inner.counters.readback_bytes, len as u64);
        out
    }

    pub fn write_buffer(&self, buf: &Buffer, data: &[u8]) {
        assert!(data.len() <= buf.len, "write past end of buffer");
        unsafe {
            std::ptr::copy_nonoverlapping(data.as_ptr(), buf.raw.contents().as_ptr().cast(), data.len());
        }
        Counters::add(&self.inner.counters.uploads, 1);
        Counters::add(&self.inner.counters.upload_bytes, data.len() as u64);
    }

    /// Starts recording a frame. Blocks only if the frame that last used this
    /// ring slot (FRAMES_IN_FLIGHT frames ago) is still executing. Threads
    /// recording at once each get a slot of their own (the ring grows when
    /// more record than it has slots).
    pub fn begin(&self) -> CommandList {
        let (slot, arena) = {
            let mut ring = self.inner.ring.lock().unwrap();
            let n = ring.1.len();
            let free = (0..n).map(|k| (ring.0 + k) % n).find(|&i| !ring.1[i].recording);
            let idx = match free {
                Some(i) => i,
                None => {
                    let arena = self
                        .inner
                        .device
                        .newBufferWithLength_options(ARENA_BYTES, MTLResourceOptions::StorageModeShared)
                        .expect("param arena");
                    ring.1.push(RingSlot {
                        arena: Shared(arena),
                        pending: None,
                        recording: false,
                    });
                    n
                }
            };
            ring.0 = (idx + 1) % ring.1.len();
            let s = &mut ring.1[idx];
            s.recording = true;
            if let Some(cb) = s.pending.take() {
                cb.waitUntilCompleted();
            }
            (idx, s.arena.0.clone())
        };
        let cb = self.inner.queue.commandBuffer().expect("command buffer");
        let enc = cb.computeCommandEncoder().expect("compute encoder");
        CommandList {
            device: self.clone(),
            cb,
            enc,
            slot,
            arena,
            arena_used: 0,
            submitted: false,
        }
    }

    /// Copies a CPU-readable texture to CPU memory, tightly packed. Counted
    /// as a readback.
    pub fn readback(&self, tex: &Texture) -> Vec<u8> {
        assert!(tex.desc.cpu_readable, "readback of a GPU-private texture");
        let (w, h) = (tex.desc.width as usize, tex.desc.height as usize);
        let row = w * tex.desc.format.bytes_per_pixel();
        let mut out = vec![0u8; row * h];
        unsafe {
            tex.raw.getBytes_bytesPerRow_fromRegion_mipmapLevel(
                NonNull::new(out.as_mut_ptr().cast()).unwrap(),
                row,
                region(tex.desc.width, tex.desc.height),
                0,
            );
        }
        Counters::add(&self.inner.counters.readbacks, 1);
        Counters::add(&self.inner.counters.readback_bytes, out.len() as u64);
        out
    }

    /// Raw Metal device, for platform interop (IOSurface, CoreVideo).
    pub fn raw_device(&self) -> &ProtocolObject<dyn MTLDevice> {
        &self.inner.device
    }

    /// Wraps an externally created Metal texture (for example one backed by
    /// an IOSurface from VideoToolbox). No copy.
    pub fn wrap_texture(&self, raw: P<dyn MTLTexture>, format: PixelFormat) -> Texture {
        let desc = TextureDesc {
            width: raw.width() as u32,
            height: raw.height() as u32,
            format,
            cpu_readable: false,
        };
        Texture {
            raw: Arc::new(Shared(raw)),
            desc,
        }
    }
}

impl Library {
    pub fn kernel(&self, name: &str) -> Result<Kernel, GpuError> {
        let func = self
            .raw
            .newFunctionWithName(&NSString::from_str(name))
            .ok_or_else(|| GpuError::NoSuchKernel(name.to_string()))?;
        let pipeline = self
            .device
            .inner
            .device
            .newComputePipelineStateWithFunction_error(&func)
            .map_err(|e| GpuError::Pipeline(name.to_string(), e.localizedDescription().to_string()))?;
        Ok(Kernel {
            name: name.into(),
            pipeline: Arc::new(Shared(pipeline)),
        })
    }
}

/// A graphics pipeline for 3D passes: vertex pulling from a storage buffer,
/// RGBA16F color, 32-bit depth, optional MSAA. Draw ABI (both stages): params
/// buffer(0), vertices buffer(1), texture(0), sampler(0).
#[derive(Clone)]
pub struct DrawPipeline {
    state: Arc<Shared<P<dyn MTLRenderPipelineState>>>,
    depth: Option<Arc<Shared<P<dyn MTLDepthStencilState>>>>,
    pub samples: u32,
}

/// Attachments of a 3D pass. `color` is the multisampled target (None for
/// single-sample passes, which then render straight into `resolve`).
pub struct DrawTarget<'a> {
    pub resolve: &'a Texture,
    pub color: Option<&'a Texture>,
    pub depth: Option<&'a Texture>,
    pub clear: [f32; 4],
}

/// One indexed draw.
pub struct Draw<'a> {
    pub pipeline: &'a DrawPipeline,
    pub params: &'a [f32],
    pub vertices: BufferBinding<'a>,
    pub indices: &'a Buffer,
    pub index_count: u32,
    pub texture: &'a Texture,
    /// Bound at texture(1) (shadow maps); pass any texture when unused.
    pub texture2: &'a Texture,
    /// Bound at texture(2) (environment maps); pass any texture when unused.
    pub texture3: &'a Texture,
    /// Bound at texture(3) (prefiltered environment); any texture when unused.
    pub texture4: &'a Texture,
}

impl Device {
    pub fn draw_pipeline(
        &self,
        library: &Library,
        vertex: &str,
        fragment: &str,
        samples: u32,
        depth: bool,
        blend: crate::Blend,
        color: PixelFormat,
    ) -> Result<DrawPipeline, GpuError> {
        let vf = library
            .raw
            .newFunctionWithName(&NSString::from_str(vertex))
            .ok_or_else(|| GpuError::NoSuchKernel(vertex.to_string()))?;
        let ff = library
            .raw
            .newFunctionWithName(&NSString::from_str(fragment))
            .ok_or_else(|| GpuError::NoSuchKernel(fragment.to_string()))?;
        let d = MTLRenderPipelineDescriptor::new();
        d.setVertexFunction(Some(&vf));
        d.setFragmentFunction(Some(&ff));
        unsafe {
            let c = d.colorAttachments().objectAtIndexedSubscript(0);
            c.setPixelFormat(mtl_format(color));
            // Premultiplied blending: src + dst * (1 - src.a), or additive.
            if blend != crate::Blend::Opaque {
                c.setBlendingEnabled(true);
                c.setRgbBlendOperation(MTLBlendOperation::Add);
                c.setAlphaBlendOperation(MTLBlendOperation::Add);
                c.setSourceRGBBlendFactor(MTLBlendFactor::One);
                c.setSourceAlphaBlendFactor(MTLBlendFactor::One);
                // Additive light adds color but composites alpha as over, so
                // coverage never exceeds 1.
                let dst = if blend == crate::Blend::Add {
                    MTLBlendFactor::One
                } else {
                    MTLBlendFactor::OneMinusSourceAlpha
                };
                c.setDestinationRGBBlendFactor(dst);
                c.setDestinationAlphaBlendFactor(MTLBlendFactor::OneMinusSourceAlpha);
            }
            d.setRasterSampleCount(samples as usize);
        }
        if depth {
            d.setDepthAttachmentPixelFormat(MTLPixelFormat::Depth32Float);
        }
        let state = self
            .inner
            .device
            .newRenderPipelineStateWithDescriptor_error(&d)
            .map_err(|e| GpuError::Pipeline(vertex.to_string(), e.localizedDescription().to_string()))?;
        let depth = if depth {
            let dd = MTLDepthStencilDescriptor::new();
            dd.setDepthCompareFunction(MTLCompareFunction::Less);
            dd.setDepthWriteEnabled(true);
            Some(Arc::new(Shared(
                self.inner
                    .device
                    .newDepthStencilStateWithDescriptor(&dd)
                    .ok_or(GpuError::NoDevice)?,
            )))
        } else {
            None
        };
        Ok(DrawPipeline {
            state: Arc::new(Shared(state)),
            depth,
            samples,
        })
    }

    /// A render attachment: multisampled color, or depth. GPU-only memory
    /// (memoryless on Apple silicon: it never leaves tile memory).
    pub fn create_attachment(&self, width: u32, height: u32, format: PixelFormat, samples: u32) -> Texture {
        let raw = unsafe {
            let d = MTLTextureDescriptor::texture2DDescriptorWithPixelFormat_width_height_mipmapped(
                mtl_format(format),
                width.max(1) as usize,
                height.max(1) as usize,
                false,
            );
            if samples > 1 {
                d.setTextureType(MTLTextureType::Type2DMultisample);
                d.setSampleCount(samples as usize);
            }
            d.setUsage(MTLTextureUsage::RenderTarget);
            d.setStorageMode(MTLStorageMode::Memoryless);
            self.inner
                .device
                .newTextureWithDescriptor(&d)
                .expect("attachment allocation")
        };
        Counters::add(&self.inner.counters.textures_created, 1);
        Texture {
            raw: Arc::new(Shared(raw)),
            desc: TextureDesc {
                width,
                height,
                format,
                cpu_readable: false,
            },
        }
    }
}

fn region(w: u32, h: u32) -> MTLRegion {
    MTLRegion {
        origin: MTLOrigin { x: 0, y: 0, z: 0 },
        size: MTLSize {
            width: w as usize,
            height: h as usize,
            depth: 1,
        },
    }
}

/// Records dispatches for one frame into one command buffer.
pub struct CommandList {
    device: Device,
    cb: P<dyn MTLCommandBuffer>,
    enc: P<dyn MTLComputeCommandEncoder>,
    slot: usize,
    arena: P<dyn MTLBuffer>,
    arena_used: usize,
    submitted: bool,
}

impl Drop for CommandList {
    /// A list abandoned mid-frame (an error while encoding) is ended and
    /// discarded instead of tripping Metal's encoder assertions.
    fn drop(&mut self) {
        if !self.submitted {
            self.enc.endEncoding();
            self.device.inner.ring.lock().unwrap().1[self.slot].recording = false;
        }
    }
}

/// A storage buffer binding: a persistent buffer, or bytes copied into the
/// frame's arena (per-frame data such as dynamic geometry).
pub enum BufferBinding<'a> {
    Buffer(&'a Buffer),
    Data(&'a [u8]),
}

/// Extra bindings for kernels that need more than textures and params.
#[derive(Default)]
pub struct Bindings<'a> {
    /// Bound at buffer(1..).
    pub buffers: &'a [BufferBinding<'a>],
    /// Overrides the dispatch grid in threads (default: output texture size).
    pub grid: Option<(u32, u32)>,
    /// Overrides the threadgroup size (needed by kernels using groupshared
    /// memory).
    pub threadgroup: Option<(u32, u32)>,
}

impl CommandList {
    /// Runs `kernel` over every pixel of `out`.
    pub fn dispatch(&mut self, kernel: &Kernel, out: &Texture, inputs: &[&Texture], params: &[f32]) {
        self.dispatch_ex(kernel, out, inputs, params, Bindings::default());
    }

    pub fn dispatch_ex(&mut self, kernel: &Kernel, out: &Texture, inputs: &[&Texture], params: &[f32], b: Bindings) {
        self.enc.setComputePipelineState(&kernel.pipeline);
        unsafe {
            self.enc.setTexture_atIndex(Some(&out.raw), 0);
            for (i, t) in inputs.iter().enumerate() {
                self.enc.setTexture_atIndex(Some(&t.raw), i + 1);
            }
            self.enc.setSamplerState_atIndex(Some(&self.device.inner.sampler), 0);
        }
        for (i, binding) in b.buffers.iter().enumerate() {
            match binding {
                BufferBinding::Buffer(buf) => unsafe { self.enc.setBuffer_offset_atIndex(Some(&buf.raw), 0, i + 1) },
                BufferBinding::Data(data) => {
                    let offset = self.arena_write(data);
                    unsafe { self.enc.setBuffer_offset_atIndex(Some(&self.arena), offset, i + 1) };
                    Counters::add(&self.device.inner.counters.upload_bytes, data.len() as u64);
                    Counters::add(&self.device.inner.counters.uploads, 1);
                }
            }
        }
        // Params go into the frame's arena: no allocation, one write into
        // shared memory.
        let bytes = unsafe { std::slice::from_raw_parts(params.as_ptr().cast::<u8>(), std::mem::size_of_val(params)) };
        let offset = self.arena_write(bytes);
        unsafe { self.enc.setBuffer_offset_atIndex(Some(&self.arena), offset, 0) };
        Counters::add(&self.device.inner.counters.uniform_bytes, bytes.len() as u64);

        let (gw, gh) = b.grid.unwrap_or((out.desc.width, out.desc.height));
        let (tw, th) = match b.threadgroup {
            Some((x, y)) => (x as usize, y as usize),
            None => {
                let w = kernel.pipeline.threadExecutionWidth();
                (w, (kernel.pipeline.maxTotalThreadsPerThreadgroup() / w).clamp(1, 16))
            }
        };
        self.enc.dispatchThreads_threadsPerThreadgroup(
            MTLSize {
                width: gw.max(1) as usize,
                height: gh.max(1) as usize,
                depth: 1,
            },
            MTLSize {
                width: tw,
                height: th,
                depth: 1,
            },
        );
        Counters::add(&self.device.inner.counters.dispatches, 1);
    }

    /// Copies bytes into the frame arena; returns the offset into
    /// `self.arena`. A frame that outgrows the arena (dense geometry under a
    /// deep zoom, many motion-blur samples) continues in a fresh overflow
    /// buffer, kept alive by the command buffer that binds it; read
    /// `self.arena` after each write.
    fn arena_write(&mut self, data: &[u8]) -> usize {
        let bytes = data.len().max(16);
        if self.arena_used + bytes > self.arena.length() {
            let len = ARENA_BYTES.max(bytes.next_multiple_of(ARENA_ALIGN));
            self.arena = self
                .device
                .inner
                .device
                .newBufferWithLength_options(len, MTLResourceOptions::StorageModeShared)
                .expect("overflow arena");
            self.arena_used = 0;
        }
        let offset = self.arena_used;
        unsafe {
            let dst = self.arena.contents().as_ptr().cast::<u8>().add(offset);
            std::ptr::copy_nonoverlapping(data.as_ptr(), dst, data.len());
        }
        self.arena_used = (offset + bytes).next_multiple_of(ARENA_ALIGN);
        offset
    }

    /// Zeroes `len` bytes of a buffer starting at `offset` (GPU side).
    pub fn clear_buffer_range(&mut self, buf: &Buffer, offset: usize, len: usize) {
        let len = len.min(buf.len.saturating_sub(offset));
        if len == 0 {
            return;
        }
        self.enc.endEncoding();
        let blit = self.cb.blitCommandEncoder().expect("blit encoder");
        blit.fillBuffer_range_value(&buf.raw, objc2_foundation::NSRange::new(offset, len), 0);
        blit.endEncoding();
        self.enc = self.cb.computeCommandEncoder().expect("compute encoder");
    }

    /// Runs a 3D pass: clears, draws, and resolves into `target.resolve`.
    pub fn draw_pass(&mut self, target: DrawTarget, draws: &[Draw]) {
        self.enc.endEncoding();
        let pass = MTLRenderPassDescriptor::new();
        unsafe {
            let c = pass.colorAttachments().objectAtIndexedSubscript(0);
            let [r, g, b, a] = target.clear;
            c.setClearColor(MTLClearColor {
                red: r as f64,
                green: g as f64,
                blue: b as f64,
                alpha: a as f64,
            });
            c.setLoadAction(MTLLoadAction::Clear);
            match target.color {
                Some(msaa) => {
                    c.setTexture(Some(&msaa.raw));
                    c.setResolveTexture(Some(&target.resolve.raw));
                    c.setStoreAction(MTLStoreAction::MultisampleResolve);
                }
                None => {
                    c.setTexture(Some(&target.resolve.raw));
                    c.setStoreAction(MTLStoreAction::Store);
                }
            }
            if let Some(depth) = target.depth {
                let d = pass.depthAttachment();
                d.setTexture(Some(&depth.raw));
                d.setClearDepth(1.0);
                d.setLoadAction(MTLLoadAction::Clear);
                d.setStoreAction(MTLStoreAction::DontCare);
            }
        }
        let enc = self
            .cb
            .renderCommandEncoderWithDescriptor(&pass)
            .expect("render encoder");
        let (w, h) = (target.resolve.desc.width as f64, target.resolve.desc.height as f64);
        enc.setViewport(MTLViewport {
            originX: 0.0,
            originY: 0.0,
            width: w,
            height: h,
            znear: 0.0,
            zfar: 1.0,
        });
        enc.setCullMode(MTLCullMode::None);
        for d in draws {
            enc.setRenderPipelineState(&d.pipeline.state);
            if let Some(ds) = &d.pipeline.depth {
                enc.setDepthStencilState(Some(ds));
            }
            let bytes =
                unsafe { std::slice::from_raw_parts(d.params.as_ptr().cast::<u8>(), std::mem::size_of_val(d.params)) };
            let off = self.arena_write(bytes);
            let pbuf = self.arena.clone();
            Counters::add(&self.device.inner.counters.uniform_bytes, bytes.len() as u64);
            // Vertices: a persistent buffer, or per-frame data in the arena.
            let (vbuf, voff) = match &d.vertices {
                BufferBinding::Buffer(b) => ((*b.raw).clone(), 0),
                BufferBinding::Data(data) => {
                    let o = self.arena_write(data);
                    Counters::add(&self.device.inner.counters.uploads, 1);
                    Counters::add(&self.device.inner.counters.upload_bytes, data.len() as u64);
                    (self.arena.clone(), o)
                }
            };
            unsafe {
                enc.setVertexBuffer_offset_atIndex(Some(&pbuf), off, 0);
                enc.setFragmentBuffer_offset_atIndex(Some(&pbuf), off, 0);
                enc.setVertexBuffer_offset_atIndex(Some(&vbuf), voff, 1);
                enc.setFragmentBuffer_offset_atIndex(Some(&vbuf), voff, 1);
                enc.setFragmentTexture_atIndex(Some(&d.texture.raw), 0);
                enc.setFragmentTexture_atIndex(Some(&d.texture2.raw), 1);
                enc.setFragmentTexture_atIndex(Some(&d.texture3.raw), 2);
                enc.setFragmentTexture_atIndex(Some(&d.texture4.raw), 3);
                enc.setFragmentSamplerState_atIndex(Some(&self.device.inner.sampler), 0);
                enc.drawIndexedPrimitives_indexCount_indexType_indexBuffer_indexBufferOffset(
                    MTLPrimitiveType::Triangle,
                    d.index_count as usize,
                    MTLIndexType::UInt32,
                    &d.indices.raw,
                    0,
                );
            }
            Counters::add(&self.device.inner.counters.dispatches, 1);
        }
        enc.endEncoding();
        self.enc = self.cb.computeCommandEncoder().expect("compute encoder");
    }

    /// Fills a buffer with zeros (GPU side, no transfer).
    pub fn clear_buffer(&mut self, buf: &Buffer) {
        // Compute encoders cannot fill; end, blit, and resume.
        self.enc.endEncoding();
        let blit = self.cb.blitCommandEncoder().expect("blit encoder");
        blit.fillBuffer_range_value(&buf.raw, objc2_foundation::NSRange::new(0, buf.len), 0);
        blit.endEncoding();
        self.enc = self.cb.computeCommandEncoder().expect("compute encoder");
    }

    /// Copies one texture to another of identical size and format (GPU side).
    pub fn copy_texture(&mut self, src: &Texture, dst: &Texture) {
        self.enc.endEncoding();
        let blit = self.cb.blitCommandEncoder().expect("blit encoder");
        unsafe { blit.copyFromTexture_toTexture(&src.raw, &dst.raw) };
        blit.endEncoding();
        self.enc = self.cb.computeCommandEncoder().expect("compute encoder");
    }

    /// Schedules `drawable` to be shown when this frame completes.
    pub fn present(&mut self, drawable: &crate::surface::Drawable) {
        self.cb.presentDrawable(drawable.as_mtl());
    }

    pub fn submit(mut self) -> Submitted {
        self.enc.endEncoding();
        self.submitted = true;
        self.cb.commit();
        let mut ring = self.device.inner.ring.lock().unwrap();
        ring.1[self.slot].pending = Some(Shared(self.cb.clone()));
        ring.1[self.slot].recording = false;
        Submitted { cb: self.cb.clone() }
    }
}

pub struct Submitted {
    cb: P<dyn MTLCommandBuffer>,
}

impl Submitted {
    /// GPU timing if the frame has finished, without blocking.
    pub fn try_timing(&self) -> Option<Result<GpuTiming, GpuError>> {
        match self.cb.status() {
            MTLCommandBufferStatus::Completed => Some(Ok(GpuTiming {
                gpu_ms: (self.cb.GPUEndTime() - self.cb.GPUStartTime()) * 1000.0,
            })),
            MTLCommandBufferStatus::Error => Some(Err(GpuError::Execution(
                self.cb
                    .error()
                    .map(|e| e.localizedDescription().to_string())
                    .unwrap_or_default(),
            ))),
            _ => None,
        }
    }

    pub fn wait(self) -> Result<GpuTiming, GpuError> {
        self.cb.waitUntilCompleted();
        if self.cb.status() == MTLCommandBufferStatus::Error {
            let msg = self
                .cb
                .error()
                .map(|e| e.localizedDescription().to_string())
                .unwrap_or_default();
            return Err(GpuError::Execution(msg));
        }
        let gpu_ms = (self.cb.GPUEndTime() - self.cb.GPUStartTime()) * 1000.0;
        Ok(GpuTiming { gpu_ms })
    }
}