goldy 0.1.0

Goldy - Modern Graphics 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
//! Bind group management for uniform buffers and other shader resources.
//!
//! Bind groups are the mechanism to pass data to shaders beyond vertex data.
//! They contain references to buffers (uniform, storage), textures, and samplers.

use crate::backend::{
    BindGroupHandle, BindGroupLayoutHandle, BindGroupLayoutEntry, BindGroupEntry,
    BindingResource, GpuBackend,
};
use crate::buffer::Buffer;
use crate::device::Device;
use crate::sampler::Sampler;
use crate::texture::Texture;
use anyhow::Result;
use std::sync::{Arc, Mutex};

// Re-export backend types for convenience
pub use crate::backend::{BindingType, ShaderStages};

/// Description of a binding in a bind group layout.
#[derive(Debug, Clone)]
pub struct BindGroupLayoutBinding {
    /// Binding index (matches shader's `[[vk::binding(N, 0)]]`).
    pub binding: u32,
    /// Which shader stages can access this binding.
    pub visibility: ShaderStages,
    /// Type of resource at this binding.
    pub ty: BindingType,
}

impl BindGroupLayoutBinding {
    /// Create a uniform buffer binding visible to all graphics stages.
    pub fn uniform(binding: u32) -> Self {
        Self {
            binding,
            visibility: ShaderStages::ALL,
            ty: BindingType::UniformBuffer,
        }
    }

    /// Create a uniform buffer binding visible only to the vertex stage.
    pub fn uniform_vertex(binding: u32) -> Self {
        Self {
            binding,
            visibility: ShaderStages::VERTEX,
            ty: BindingType::UniformBuffer,
        }
    }

    /// Create a uniform buffer binding visible only to the fragment stage.
    pub fn uniform_fragment(binding: u32) -> Self {
        Self {
            binding,
            visibility: ShaderStages::FRAGMENT,
            ty: BindingType::UniformBuffer,
        }
    }

    /// Create a storage buffer binding.
    pub fn storage(binding: u32, read_only: bool) -> Self {
        Self {
            binding,
            visibility: ShaderStages::ALL,
            ty: BindingType::StorageBuffer { read_only },
        }
    }

    /// Create a sampled texture binding visible to the fragment stage.
    pub fn texture(binding: u32) -> Self {
        Self {
            binding,
            visibility: ShaderStages::FRAGMENT,
            ty: BindingType::Texture,
        }
    }

    /// Create a sampled texture binding visible to all graphics stages.
    pub fn texture_all(binding: u32) -> Self {
        Self {
            binding,
            visibility: ShaderStages::ALL,
            ty: BindingType::Texture,
        }
    }

    /// Create a sampler binding visible to the fragment stage.
    pub fn sampler(binding: u32) -> Self {
        Self {
            binding,
            visibility: ShaderStages::FRAGMENT,
            ty: BindingType::Sampler,
        }
    }

    /// Create a sampler binding visible to all graphics stages.
    pub fn sampler_all(binding: u32) -> Self {
        Self {
            binding,
            visibility: ShaderStages::ALL,
            ty: BindingType::Sampler,
        }
    }

    /// Create a storage texture binding (read-write in shader).
    pub fn storage_texture(binding: u32) -> Self {
        Self {
            binding,
            visibility: ShaderStages::ALL,
            ty: BindingType::StorageTexture,
        }
    }
}

/// A bind group layout defines the structure of a bind group.
///
/// This describes what types of resources will be bound and at which binding indices.
/// The layout must be created before creating bind groups or pipelines that use them.
pub struct BindGroupLayout {
    #[allow(dead_code)]
    backend: Arc<Mutex<Box<dyn GpuBackend>>>,
    pub(crate) handle: BindGroupLayoutHandle,
}

impl std::fmt::Debug for BindGroupLayout {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BindGroupLayout")
            .field("handle", &self.handle)
            .finish()
    }
}

impl BindGroupLayout {
    /// Create a new bind group layout.
    pub fn new(device: &Device, bindings: &[BindGroupLayoutBinding]) -> Result<Self> {
        let entries: Vec<BindGroupLayoutEntry> = bindings
            .iter()
            .map(|b| BindGroupLayoutEntry {
                binding: b.binding,
                visibility: b.visibility,
                ty: b.ty.clone(),
            })
            .collect();

        let mut backend = device.backend.lock().unwrap();
        let handle = backend.create_bind_group_layout(device.handle, &entries)?;

        Ok(Self {
            backend: Arc::clone(&device.backend),
            handle,
        })
    }

    /// Get the backend handle for this layout.
    ///
    /// This is used when creating pipelines that use this layout.
    pub fn handle(&self) -> BindGroupLayoutHandle {
        self.handle
    }
}

impl Drop for BindGroupLayout {
    fn drop(&mut self) {
        // Bind group layouts are typically long-lived and destroyed with the device
        // No explicit destruction needed as the device cleanup handles this
    }
}

/// Description of a buffer binding in a bind group.
#[derive(Clone)]
pub struct BufferBinding<'a> {
    /// Binding index (must match the layout).
    pub binding: u32,
    /// The buffer to bind.
    pub buffer: &'a Buffer,
    /// Offset into the buffer (usually 0).
    pub offset: u64,
    /// Size of the binding (None = entire buffer from offset).
    pub size: Option<u64>,
}

impl<'a> std::fmt::Debug for BufferBinding<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BufferBinding")
            .field("binding", &self.binding)
            .field("offset", &self.offset)
            .field("size", &self.size)
            .finish()
    }
}

impl<'a> BufferBinding<'a> {
    /// Create a buffer binding for the entire buffer.
    pub fn new(binding: u32, buffer: &'a Buffer) -> Self {
        Self {
            binding,
            buffer,
            offset: 0,
            size: None,
        }
    }

    /// Create a buffer binding with offset and size.
    pub fn with_range(binding: u32, buffer: &'a Buffer, offset: u64, size: u64) -> Self {
        Self {
            binding,
            buffer,
            offset,
            size: Some(size),
        }
    }
}

/// Description of a texture binding in a bind group.
#[derive(Clone)]
pub struct TextureBinding<'a> {
    /// Binding index (must match the layout).
    pub binding: u32,
    /// The texture to bind.
    pub texture: &'a Texture,
}

impl<'a> std::fmt::Debug for TextureBinding<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TextureBinding")
            .field("binding", &self.binding)
            .finish()
    }
}

impl<'a> TextureBinding<'a> {
    /// Create a texture binding.
    pub fn new(binding: u32, texture: &'a Texture) -> Self {
        Self { binding, texture }
    }
}

/// Description of a sampler binding in a bind group.
#[derive(Clone)]
pub struct SamplerBinding<'a> {
    /// Binding index (must match the layout).
    pub binding: u32,
    /// The sampler to bind.
    pub sampler: &'a Sampler,
}

impl<'a> std::fmt::Debug for SamplerBinding<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SamplerBinding")
            .field("binding", &self.binding)
            .finish()
    }
}

impl<'a> SamplerBinding<'a> {
    /// Create a sampler binding.
    pub fn new(binding: u32, sampler: &'a Sampler) -> Self {
        Self { binding, sampler }
    }
}

/// A bind group contains actual resource bindings matching a layout.
///
/// Bind groups are created from a layout and contain references to buffers
/// and other resources that shaders can access.
pub struct BindGroup {
    backend: Arc<Mutex<Box<dyn GpuBackend>>>,
    pub(crate) handle: BindGroupHandle,
}

impl BindGroup {
    /// Create a new bind group from a layout and buffer bindings.
    ///
    /// Use this when your bind group only contains buffer bindings (uniform, storage).
    /// For bind groups with textures and samplers, use `with_resources()`.
    pub fn new(
        device: &Device,
        layout: &BindGroupLayout,
        bindings: &[BufferBinding],
    ) -> Result<Self> {
        Self::with_resources(device, layout, bindings, &[], &[])
    }

    /// Create a new bind group with buffers, textures, and samplers.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use goldy::{Device, BindGroup, BindGroupLayout, BindGroupLayoutBinding, BufferBinding, TextureBinding, SamplerBinding};
    ///
    /// fn create_textured_bind_group(
    ///     device: &Device,
    ///     layout: &BindGroupLayout,
    ///     uniform_buffer: &goldy::Buffer,
    ///     texture: &goldy::Texture,
    ///     sampler: &goldy::Sampler,
    /// ) -> anyhow::Result<BindGroup> {
    ///     BindGroup::with_resources(
    ///         device,
    ///         layout,
    ///         &[BufferBinding::new(0, uniform_buffer)],
    ///         &[TextureBinding::new(1, texture)],
    ///         &[SamplerBinding::new(2, sampler)],
    ///     )
    /// }
    /// ```
    pub fn with_resources(
        device: &Device,
        layout: &BindGroupLayout,
        buffer_bindings: &[BufferBinding],
        texture_bindings: &[TextureBinding],
        sampler_bindings: &[SamplerBinding],
    ) -> Result<Self> {
        let mut entries: Vec<BindGroupEntry> = Vec::new();

        // Add buffer bindings
        for b in buffer_bindings {
            entries.push(BindGroupEntry {
                binding: b.binding,
                resource: BindingResource::Buffer {
                    buffer: b.buffer.handle,
                    offset: b.offset,
                    size: b.size.unwrap_or(b.buffer.size()),
                },
            });
        }

        // Add texture bindings
        for t in texture_bindings {
            entries.push(BindGroupEntry {
                binding: t.binding,
                resource: BindingResource::Texture(t.texture.handle),
            });
        }

        // Add sampler bindings
        for s in sampler_bindings {
            entries.push(BindGroupEntry {
                binding: s.binding,
                resource: BindingResource::Sampler(s.sampler.handle),
            });
        }

        let mut backend = device.backend.lock().unwrap();
        let handle = backend.create_bind_group(device.handle, layout.handle, &entries)?;

        Ok(Self {
            backend: Arc::clone(&device.backend),
            handle,
        })
    }
}

impl Drop for BindGroup {
    fn drop(&mut self) {
        let mut backend = self.backend.lock().unwrap();
        backend.destroy_bind_group(self.handle);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backend::mock::MockBackend;

    fn create_test_device() -> Device {
        Device::from_backend(Box::new(MockBackend::new())).unwrap()
    }

    // BindGroupLayoutBinding tests
    
    #[test]
    fn test_uniform_binding() {
        let binding = BindGroupLayoutBinding::uniform(0);
        assert_eq!(binding.binding, 0);
        assert_eq!(binding.visibility, ShaderStages::ALL);
        assert!(matches!(binding.ty, BindingType::UniformBuffer));
    }

    #[test]
    fn test_uniform_vertex_binding() {
        let binding = BindGroupLayoutBinding::uniform_vertex(1);
        assert_eq!(binding.binding, 1);
        assert_eq!(binding.visibility, ShaderStages::VERTEX);
        assert!(matches!(binding.ty, BindingType::UniformBuffer));
    }

    #[test]
    fn test_uniform_fragment_binding() {
        let binding = BindGroupLayoutBinding::uniform_fragment(2);
        assert_eq!(binding.binding, 2);
        assert_eq!(binding.visibility, ShaderStages::FRAGMENT);
        assert!(matches!(binding.ty, BindingType::UniformBuffer));
    }

    #[test]
    fn test_storage_binding_read_write() {
        let binding = BindGroupLayoutBinding::storage(0, false);
        assert_eq!(binding.binding, 0);
        assert_eq!(binding.visibility, ShaderStages::ALL);
        match binding.ty {
            BindingType::StorageBuffer { read_only } => assert!(!read_only),
            _ => panic!("Expected StorageBuffer"),
        }
    }

    #[test]
    fn test_storage_binding_read_only() {
        let binding = BindGroupLayoutBinding::storage(3, true);
        assert_eq!(binding.binding, 3);
        match binding.ty {
            BindingType::StorageBuffer { read_only } => assert!(read_only),
            _ => panic!("Expected StorageBuffer"),
        }
    }

    #[test]
    fn test_texture_binding() {
        let binding = BindGroupLayoutBinding::texture(0);
        assert_eq!(binding.binding, 0);
        assert_eq!(binding.visibility, ShaderStages::FRAGMENT);
        assert!(matches!(binding.ty, BindingType::Texture));
    }

    #[test]
    fn test_texture_all_binding() {
        let binding = BindGroupLayoutBinding::texture_all(1);
        assert_eq!(binding.binding, 1);
        assert_eq!(binding.visibility, ShaderStages::ALL);
        assert!(matches!(binding.ty, BindingType::Texture));
    }

    #[test]
    fn test_sampler_binding() {
        let binding = BindGroupLayoutBinding::sampler(0);
        assert_eq!(binding.binding, 0);
        assert_eq!(binding.visibility, ShaderStages::FRAGMENT);
        assert!(matches!(binding.ty, BindingType::Sampler));
    }

    #[test]
    fn test_sampler_all_binding() {
        let binding = BindGroupLayoutBinding::sampler_all(1);
        assert_eq!(binding.binding, 1);
        assert_eq!(binding.visibility, ShaderStages::ALL);
        assert!(matches!(binding.ty, BindingType::Sampler));
    }

    #[test]
    fn test_storage_texture_binding() {
        let binding = BindGroupLayoutBinding::storage_texture(0);
        assert_eq!(binding.binding, 0);
        assert_eq!(binding.visibility, ShaderStages::ALL);
        assert!(matches!(binding.ty, BindingType::StorageTexture));
    }

    // BindGroupLayout tests

    #[test]
    fn test_bind_group_layout_creation() {
        let device = create_test_device();
        
        let layout = BindGroupLayout::new(&device, &[
            BindGroupLayoutBinding::uniform(0),
        ]).unwrap();
        
        // Layout handle should be non-zero
        assert!(layout.handle() > 0);
    }

    #[test]
    fn test_bind_group_layout_multiple_bindings() {
        let device = create_test_device();
        
        let layout = BindGroupLayout::new(&device, &[
            BindGroupLayoutBinding::uniform(0),
            BindGroupLayoutBinding::storage(1, false),
            BindGroupLayoutBinding::texture(2),
            BindGroupLayoutBinding::sampler(3),
        ]).unwrap();
        
        assert!(layout.handle() > 0);
    }

    #[test]
    fn test_bind_group_layout_empty() {
        let device = create_test_device();
        
        // Empty layout should work
        let layout = BindGroupLayout::new(&device, &[]).unwrap();
        assert!(layout.handle() > 0);
    }

    // BufferBinding tests

    #[test]
    fn test_buffer_binding_new() {
        let device = create_test_device();
        let buffer = Buffer::new(&device, 256, crate::types::BufferUsage::UNIFORM).unwrap();
        
        let binding = BufferBinding::new(0, &buffer);
        assert_eq!(binding.binding, 0);
        assert_eq!(binding.offset, 0);
        assert!(binding.size.is_none());
    }

    #[test]
    fn test_buffer_binding_with_range() {
        let device = create_test_device();
        let buffer = Buffer::new(&device, 1024, crate::types::BufferUsage::UNIFORM).unwrap();
        
        let binding = BufferBinding::with_range(0, &buffer, 256, 512);
        assert_eq!(binding.binding, 0);
        assert_eq!(binding.offset, 256);
        assert_eq!(binding.size, Some(512));
    }

    // BindGroup tests

    #[test]
    fn test_bind_group_creation() {
        let device = create_test_device();
        
        let layout = BindGroupLayout::new(&device, &[
            BindGroupLayoutBinding::uniform(0),
        ]).unwrap();
        
        let buffer = Buffer::new(&device, 256, crate::types::BufferUsage::UNIFORM).unwrap();
        
        let bind_group = BindGroup::new(&device, &layout, &[
            BufferBinding::new(0, &buffer),
        ]).unwrap();
        
        // Just verify creation succeeded
        assert!(bind_group.handle > 0);
    }

    #[test]
    fn test_bind_group_multiple_buffers() {
        let device = create_test_device();
        
        let layout = BindGroupLayout::new(&device, &[
            BindGroupLayoutBinding::uniform(0),
            BindGroupLayoutBinding::uniform(1),
            BindGroupLayoutBinding::storage(2, false),
        ]).unwrap();
        
        let uniform1 = Buffer::new(&device, 256, crate::types::BufferUsage::UNIFORM).unwrap();
        let uniform2 = Buffer::new(&device, 128, crate::types::BufferUsage::UNIFORM).unwrap();
        let storage = Buffer::new(&device, 1024, crate::types::BufferUsage::STORAGE).unwrap();
        
        let bind_group = BindGroup::new(&device, &layout, &[
            BufferBinding::new(0, &uniform1),
            BufferBinding::new(1, &uniform2),
            BufferBinding::new(2, &storage),
        ]).unwrap();
        
        assert!(bind_group.handle > 0);
    }

    #[test]
    fn test_bind_group_with_resources() {
        let device = create_test_device();
        
        // Layout with buffer, texture, and sampler
        let layout = BindGroupLayout::new(&device, &[
            BindGroupLayoutBinding::uniform(0),
            BindGroupLayoutBinding::texture(1),
            BindGroupLayoutBinding::sampler(2),
        ]).unwrap();
        
        let buffer = Buffer::new(&device, 256, crate::types::BufferUsage::UNIFORM).unwrap();
        let texture = crate::texture::Texture::new(
            &device, 
            64, 64, 
            crate::types::TextureFormat::Rgba8Unorm,
            crate::types::TextureUsage::SAMPLED,
        ).unwrap();
        let sampler = crate::sampler::Sampler::new(&device, &crate::types::SamplerDesc::default()).unwrap();
        
        let bind_group = BindGroup::with_resources(
            &device,
            &layout,
            &[BufferBinding::new(0, &buffer)],
            &[TextureBinding::new(1, &texture)],
            &[SamplerBinding::new(2, &sampler)],
        ).unwrap();
        
        assert!(bind_group.handle > 0);
    }

    #[test]
    fn test_bind_group_empty_bindings() {
        let device = create_test_device();
        
        let layout = BindGroupLayout::new(&device, &[]).unwrap();
        
        // Empty bind group should work
        let bind_group = BindGroup::new(&device, &layout, &[]).unwrap();
        assert!(bind_group.handle > 0);
    }

    #[test]
    fn test_texture_binding_struct() {
        let device = create_test_device();
        let texture = crate::texture::Texture::new(
            &device, 
            64, 64, 
            crate::types::TextureFormat::Rgba8Unorm,
            crate::types::TextureUsage::SAMPLED,
        ).unwrap();
        
        let binding = TextureBinding::new(0, &texture);
        assert_eq!(binding.binding, 0);
    }

    #[test]
    fn test_sampler_binding_struct() {
        let device = create_test_device();
        let sampler = crate::sampler::Sampler::new(&device, &crate::types::SamplerDesc::default()).unwrap();
        
        let binding = SamplerBinding::new(0, &sampler);
        assert_eq!(binding.binding, 0);
    }

    // Debug trait tests

    #[test]
    fn test_bind_group_layout_debug() {
        let device = create_test_device();
        let layout = BindGroupLayout::new(&device, &[
            BindGroupLayoutBinding::uniform(0),
        ]).unwrap();
        
        let debug_str = format!("{:?}", layout);
        assert!(debug_str.contains("BindGroupLayout"));
    }

    #[test]
    fn test_buffer_binding_debug() {
        let device = create_test_device();
        let buffer = Buffer::new(&device, 256, crate::types::BufferUsage::UNIFORM).unwrap();
        let binding = BufferBinding::new(0, &buffer);
        
        let debug_str = format!("{:?}", binding);
        assert!(debug_str.contains("BufferBinding"));
        assert!(debug_str.contains("binding"));
    }

    #[test]
    fn test_texture_binding_debug() {
        let device = create_test_device();
        let texture = crate::texture::Texture::new(
            &device, 
            64, 64, 
            crate::types::TextureFormat::Rgba8Unorm,
            crate::types::TextureUsage::SAMPLED,
        ).unwrap();
        let binding = TextureBinding::new(0, &texture);
        
        let debug_str = format!("{:?}", binding);
        assert!(debug_str.contains("TextureBinding"));
    }

    #[test]
    fn test_sampler_binding_debug() {
        let device = create_test_device();
        let sampler = crate::sampler::Sampler::new(&device, &crate::types::SamplerDesc::default()).unwrap();
        let binding = SamplerBinding::new(0, &sampler);
        
        let debug_str = format!("{:?}", binding);
        assert!(debug_str.contains("SamplerBinding"));
    }

    #[test]
    fn test_bind_group_layout_binding_clone() {
        let binding = BindGroupLayoutBinding::uniform(5);
        let cloned = binding.clone();
        
        assert_eq!(cloned.binding, 5);
        assert_eq!(cloned.visibility, ShaderStages::ALL);
    }
}