torsh-core 0.1.2

Core types and traits for ToRSh deep learning framework
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
// Copyright (c) 2025 ToRSh Contributors
//
// WebGPU Compute Shader Integration
//
// This module provides abstractions for WebGPU compute shaders, enabling
// high-performance tensor operations in web browsers and native applications.
//
// # Key Features
//
// - **WGSL Shader Compilation**: Compile and manage WebGPU Shading Language shaders
// - **Compute Pipeline Management**: Efficient pipeline creation and caching
// - **Buffer Management**: Optimized GPU buffer allocation and transfer
// - **Workgroup Optimization**: Automatic workgroup size calculation
// - **Cross-Platform**: Works in browsers (via wasm) and native applications
//
// # Design Principles
//
// 1. **Zero-Copy Transfer**: Minimize data transfer between CPU and GPU
// 2. **Pipeline Caching**: Reuse compiled pipelines for performance
// 3. **Memory Efficiency**: Efficient buffer pooling and management
// 4. **Shader Composition**: Modular shader building blocks
//
// # Examples
//
// ```rust
// use torsh_core::webgpu::{WGSLShader, ComputePipeline, GPUBuffer};
//
// // Define a WGSL compute shader
// let shader = WGSLShader::new(r#"
//     @compute @workgroup_size(64)
//     fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
//         // Compute shader code
//     }
// "#);
//
// // Create a compute pipeline
// let pipeline = ComputePipeline::new(shader);
//
// // Allocate GPU buffers
// let input_buffer = GPUBuffer::storage(data.len());
// let output_buffer = GPUBuffer::storage(data.len());
// ```

use core::fmt;

/// WebGPU compute shader in WGSL (WebGPU Shading Language)
///
/// This struct represents a compute shader written in WGSL that can be
/// compiled and executed on the GPU.
#[derive(Debug, Clone)]
pub struct WGSLShader {
    /// Shader source code in WGSL
    source: String,
    /// Entry point function name
    entry_point: String,
    /// Workgroup size (x, y, z)
    workgroup_size: (u32, u32, u32),
}

impl WGSLShader {
    /// Create a new WGSL shader
    pub fn new(source: impl Into<String>) -> Self {
        Self {
            source: source.into(),
            entry_point: "main".to_string(),
            workgroup_size: (64, 1, 1), // Default workgroup size
        }
    }

    /// Create a shader with custom entry point
    pub fn with_entry_point(source: impl Into<String>, entry_point: impl Into<String>) -> Self {
        Self {
            source: source.into(),
            entry_point: entry_point.into(),
            workgroup_size: (64, 1, 1),
        }
    }

    /// Set workgroup size
    pub fn with_workgroup_size(mut self, x: u32, y: u32, z: u32) -> Self {
        self.workgroup_size = (x, y, z);
        self
    }

    /// Get shader source
    pub fn source(&self) -> &str {
        &self.source
    }

    /// Get entry point
    pub fn entry_point(&self) -> &str {
        &self.entry_point
    }

    /// Get workgroup size
    pub fn workgroup_size(&self) -> (u32, u32, u32) {
        self.workgroup_size
    }

    /// Validate shader syntax (basic validation)
    pub fn validate(&self) -> Result<(), ShaderError> {
        if self.source.is_empty() {
            return Err(ShaderError::EmptyShader);
        }
        if !self.source.contains("@compute") {
            return Err(ShaderError::MissingComputeAttribute);
        }
        Ok(())
    }
}

/// Shader compilation and validation errors
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ShaderError {
    /// Shader source is empty
    EmptyShader,
    /// Missing @compute attribute
    MissingComputeAttribute,
    /// Syntax error in shader
    SyntaxError(String),
    /// Compilation failed
    CompilationFailed(String),
}

impl fmt::Display for ShaderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ShaderError::EmptyShader => write!(f, "Shader source is empty"),
            ShaderError::MissingComputeAttribute => write!(f, "Missing @compute attribute"),
            ShaderError::SyntaxError(msg) => write!(f, "Syntax error: {}", msg),
            ShaderError::CompilationFailed(msg) => write!(f, "Compilation failed: {}", msg),
        }
    }
}

/// GPU buffer types for WebGPU
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BufferUsage {
    /// Storage buffer (read/write)
    Storage,
    /// Uniform buffer (read-only, small data)
    Uniform,
    /// Staging buffer (CPU-GPU transfer)
    Staging,
    /// Vertex buffer
    Vertex,
    /// Index buffer
    Index,
}

/// GPU buffer descriptor
///
/// Describes a GPU buffer for allocation and management.
#[derive(Debug, Clone)]
pub struct GPUBuffer {
    /// Buffer size in bytes
    size: usize,
    /// Buffer usage
    usage: BufferUsage,
    /// Whether buffer is mapped for CPU access
    mapped: bool,
    /// Buffer label for debugging
    label: Option<String>,
}

impl GPUBuffer {
    /// Create a storage buffer
    pub fn storage(size: usize) -> Self {
        Self {
            size,
            usage: BufferUsage::Storage,
            mapped: false,
            label: None,
        }
    }

    /// Create a uniform buffer
    pub fn uniform(size: usize) -> Self {
        Self {
            size,
            usage: BufferUsage::Uniform,
            mapped: false,
            label: None,
        }
    }

    /// Create a staging buffer
    pub fn staging(size: usize) -> Self {
        Self {
            size,
            usage: BufferUsage::Staging,
            mapped: true,
            label: None,
        }
    }

    /// Set buffer label
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Get buffer size
    pub fn size(&self) -> usize {
        self.size
    }

    /// Get buffer usage
    pub fn usage(&self) -> BufferUsage {
        self.usage
    }

    /// Check if buffer is mapped
    pub fn is_mapped(&self) -> bool {
        self.mapped
    }

    /// Get buffer label
    pub fn label(&self) -> Option<&str> {
        self.label.as_deref()
    }
}

/// Compute pipeline for executing shaders
///
/// Represents a compiled compute pipeline that can be executed on the GPU.
#[derive(Debug, Clone)]
pub struct ComputePipeline {
    /// Associated shader
    shader: WGSLShader,
    /// Bind group layouts
    bind_groups: Vec<BindGroupLayout>,
    /// Pipeline label
    label: Option<String>,
}

impl ComputePipeline {
    /// Create a new compute pipeline
    pub fn new(shader: WGSLShader) -> Self {
        Self {
            shader,
            bind_groups: Vec::new(),
            label: None,
        }
    }

    /// Add a bind group layout
    pub fn with_bind_group(mut self, layout: BindGroupLayout) -> Self {
        self.bind_groups.push(layout);
        self
    }

    /// Set pipeline label
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Get shader
    pub fn shader(&self) -> &WGSLShader {
        &self.shader
    }

    /// Get bind groups
    pub fn bind_groups(&self) -> &[BindGroupLayout] {
        &self.bind_groups
    }

    /// Get pipeline label
    pub fn label(&self) -> Option<&str> {
        self.label.as_deref()
    }

    /// Calculate optimal dispatch size for a given data size
    pub fn optimal_dispatch_size(&self, data_size: usize) -> (u32, u32, u32) {
        let (wg_x, wg_y, wg_z) = self.shader.workgroup_size();
        let workgroup_count = (data_size as u32 + wg_x - 1) / wg_x;
        (workgroup_count, wg_y, wg_z)
    }
}

/// Bind group layout entry
///
/// Describes a binding in a bind group (buffer, texture, sampler, etc.)
#[derive(Debug, Clone)]
pub struct BindGroupEntry {
    /// Binding index
    binding: u32,
    /// Resource type
    resource_type: ResourceType,
    /// Shader visibility
    visibility: ShaderStage,
}

impl BindGroupEntry {
    /// Create a new bind group entry
    pub fn new(binding: u32, resource_type: ResourceType, visibility: ShaderStage) -> Self {
        Self {
            binding,
            resource_type,
            visibility,
        }
    }

    /// Get binding index
    pub fn binding(&self) -> u32 {
        self.binding
    }

    /// Get resource type
    pub fn resource_type(&self) -> ResourceType {
        self.resource_type
    }

    /// Get visibility
    pub fn visibility(&self) -> ShaderStage {
        self.visibility
    }
}

/// Bind group layout
///
/// Describes the layout of bindings in a bind group.
#[derive(Debug, Clone)]
pub struct BindGroupLayout {
    /// Entries in this bind group
    entries: Vec<BindGroupEntry>,
    /// Layout label
    label: Option<String>,
}

impl BindGroupLayout {
    /// Create a new bind group layout
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
            label: None,
        }
    }

    /// Add an entry
    pub fn with_entry(mut self, entry: BindGroupEntry) -> Self {
        self.entries.push(entry);
        self
    }

    /// Set label
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Get entries
    pub fn entries(&self) -> &[BindGroupEntry] {
        &self.entries
    }

    /// Get label
    pub fn label(&self) -> Option<&str> {
        self.label.as_deref()
    }
}

impl Default for BindGroupLayout {
    fn default() -> Self {
        Self::new()
    }
}

/// Resource types for bind group entries
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResourceType {
    /// Storage buffer (read/write)
    StorageBuffer,
    /// Uniform buffer (read-only)
    UniformBuffer,
    /// Read-only storage buffer
    ReadOnlyStorageBuffer,
    /// Texture (2D/3D)
    Texture,
    /// Sampler
    Sampler,
}

/// Shader stage visibility
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShaderStage {
    /// Vertex shader
    Vertex,
    /// Fragment shader
    Fragment,
    /// Compute shader
    Compute,
    /// All stages
    All,
}

/// Workgroup size optimizer
///
/// Automatically calculates optimal workgroup sizes based on data dimensions
/// and GPU capabilities.
#[derive(Debug, Clone, Copy)]
pub struct WorkgroupOptimizer {
    /// Maximum workgroup size (device-dependent)
    max_workgroup_size: u32,
    /// Preferred workgroup size for 1D operations
    preferred_1d: u32,
    /// Preferred workgroup size for 2D operations
    preferred_2d: (u32, u32),
}

impl WorkgroupOptimizer {
    /// Create a new workgroup optimizer
    pub fn new(max_workgroup_size: u32) -> Self {
        Self {
            max_workgroup_size,
            preferred_1d: 256,
            preferred_2d: (16, 16),
        }
    }

    /// Calculate optimal workgroup size for 1D data
    pub fn optimize_1d(&self, data_size: usize) -> u32 {
        let size = data_size as u32;
        if size <= 64 {
            64
        } else if size <= self.preferred_1d {
            self.preferred_1d
        } else {
            self.max_workgroup_size.min(512)
        }
    }

    /// Calculate optimal workgroup size for 2D data
    pub fn optimize_2d(&self, width: usize, height: usize) -> (u32, u32) {
        let (w, h) = (width as u32, height as u32);
        if w <= 16 && h <= 16 {
            (8, 8)
        } else if w <= 32 && h <= 32 {
            (16, 16)
        } else {
            self.preferred_2d
        }
    }

    /// Calculate optimal workgroup size for 3D data
    pub fn optimize_3d(&self, width: usize, height: usize, depth: usize) -> (u32, u32, u32) {
        let (w, h, d) = (width as u32, height as u32, depth as u32);
        if w <= 8 && h <= 8 && d <= 8 {
            (4, 4, 4)
        } else {
            (8, 8, 4)
        }
    }

    /// Get maximum workgroup size
    pub fn max_workgroup_size(&self) -> u32 {
        self.max_workgroup_size
    }
}

impl Default for WorkgroupOptimizer {
    fn default() -> Self {
        Self::new(256) // Common default
    }
}

/// Pipeline cache for reusing compiled pipelines
///
/// Caches compiled compute pipelines to avoid recompilation.
#[derive(Debug, Clone)]
pub struct PipelineCache {
    /// Cached pipelines (shader source hash -> pipeline)
    cache: Vec<(u64, ComputePipeline)>,
    /// Maximum cache size
    max_size: usize,
}

impl PipelineCache {
    /// Create a new pipeline cache
    pub fn new(max_size: usize) -> Self {
        Self {
            cache: Vec::new(),
            max_size,
        }
    }

    /// Get or create a pipeline
    pub fn get_or_create<F>(&mut self, shader: &WGSLShader, create_fn: F) -> &ComputePipeline
    where
        F: FnOnce(&WGSLShader) -> ComputePipeline,
    {
        let hash = self.hash_shader(shader);

        // Check if already cached
        if let Some(index) = self.cache.iter().position(|(h, _)| *h == hash) {
            return &self.cache[index].1;
        }

        // Create new pipeline
        let pipeline = create_fn(shader);
        self.cache.push((hash, pipeline));

        // Evict oldest if cache is full
        if self.cache.len() > self.max_size {
            self.cache.remove(0);
        }

        &self
            .cache
            .last()
            .expect("cache should have at least one entry after push")
            .1
    }

    /// Simple hash function for shader source
    fn hash_shader(&self, shader: &WGSLShader) -> u64 {
        // Simple hash based on source length and first few characters
        let src = shader.source();
        let mut hash = src.len() as u64;
        for (i, byte) in src.bytes().take(16).enumerate() {
            hash = hash
                .wrapping_mul(31)
                .wrapping_add(byte as u64 * (i as u64 + 1));
        }
        hash
    }

    /// Clear the cache
    pub fn clear(&mut self) {
        self.cache.clear();
    }

    /// Get cache size
    pub fn size(&self) -> usize {
        self.cache.len()
    }

    /// Get maximum cache size
    pub fn max_size(&self) -> usize {
        self.max_size
    }
}

/// Common WGSL shader templates
pub mod shaders {
    use super::*;

    /// Element-wise addition shader
    pub fn elementwise_add() -> WGSLShader {
        WGSLShader::new(
            r#"
@group(0) @binding(0) var<storage, read> input_a: array<f32>;
@group(0) @binding(1) var<storage, read> input_b: array<f32>;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;

@compute @workgroup_size(256)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let index = global_id.x;
    if (index < arrayLength(&input_a)) {
        output[index] = input_a[index] + input_b[index];
    }
}
"#,
        )
    }

    /// Element-wise multiplication shader
    pub fn elementwise_mul() -> WGSLShader {
        WGSLShader::new(
            r#"
@group(0) @binding(0) var<storage, read> input_a: array<f32>;
@group(0) @binding(1) var<storage, read> input_b: array<f32>;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;

@compute @workgroup_size(256)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let index = global_id.x;
    if (index < arrayLength(&input_a)) {
        output[index] = input_a[index] * input_b[index];
    }
}
"#,
        )
    }

    /// Matrix multiplication shader (simple version)
    pub fn matrix_mul() -> WGSLShader {
        WGSLShader::new(
            r#"
struct Dimensions {
    m: u32,
    n: u32,
    k: u32,
}

@group(0) @binding(0) var<storage, read> matrix_a: array<f32>;
@group(0) @binding(1) var<storage, read> matrix_b: array<f32>;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;
@group(0) @binding(3) var<uniform> dims: Dimensions;

@compute @workgroup_size(16, 16)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let row = global_id.y;
    let col = global_id.x;

    if (row >= dims.m || col >= dims.n) {
        return;
    }

    var sum = 0.0;
    for (var i = 0u; i < dims.k; i++) {
        let a_index = row * dims.k + i;
        let b_index = i * dims.n + col;
        sum += matrix_a[a_index] * matrix_b[b_index];
    }

    let out_index = row * dims.n + col;
    output[out_index] = sum;
}
"#,
        )
        .with_workgroup_size(16, 16, 1)
    }

    /// Reduction (sum) shader
    pub fn reduce_sum() -> WGSLShader {
        WGSLShader::new(
            r#"
@group(0) @binding(0) var<storage, read> input: array<f32>;
@group(0) @binding(1) var<storage, read_write> output: array<f32>;

var<workgroup> shared_data: array<f32, 256>;

@compute @workgroup_size(256)
fn main(
    @builtin(global_invocation_id) global_id: vec3<u32>,
    @builtin(local_invocation_id) local_id: vec3<u32>,
) {
    let tid = local_id.x;
    let index = global_id.x;

    // Load data into shared memory
    if (index < arrayLength(&input)) {
        shared_data[tid] = input[index];
    } else {
        shared_data[tid] = 0.0;
    }

    workgroupBarrier();

    // Reduce in shared memory
    for (var s = 128u; s > 0u; s >>= 1u) {
        if (tid < s) {
            shared_data[tid] += shared_data[tid + s];
        }
        workgroupBarrier();
    }

    // Write result
    if (tid == 0u) {
        output[global_id.x / 256u] = shared_data[0];
    }
}
"#,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_wgsl_shader_creation() {
        let shader = WGSLShader::new("@compute @workgroup_size(64) fn main() {}");
        assert!(shader.source().contains("@compute"));
        assert_eq!(shader.entry_point(), "main");
        assert_eq!(shader.workgroup_size(), (64, 1, 1));
    }

    #[test]
    fn test_shader_with_entry_point() {
        let shader = WGSLShader::with_entry_point("code", "custom_entry");
        assert_eq!(shader.entry_point(), "custom_entry");
    }

    #[test]
    fn test_shader_workgroup_size() {
        let shader = WGSLShader::new("code").with_workgroup_size(16, 16, 1);
        assert_eq!(shader.workgroup_size(), (16, 16, 1));
    }

    #[test]
    fn test_shader_validation() {
        let shader = WGSLShader::new("@compute fn main() {}");
        assert!(shader.validate().is_ok());

        let empty_shader = WGSLShader::new("");
        assert_eq!(empty_shader.validate(), Err(ShaderError::EmptyShader));

        let invalid_shader = WGSLShader::new("fn main() {}");
        assert_eq!(
            invalid_shader.validate(),
            Err(ShaderError::MissingComputeAttribute)
        );
    }

    #[test]
    fn test_gpu_buffer_creation() {
        let storage = GPUBuffer::storage(1024);
        assert_eq!(storage.size(), 1024);
        assert_eq!(storage.usage(), BufferUsage::Storage);
        assert!(!storage.is_mapped());

        let uniform = GPUBuffer::uniform(256);
        assert_eq!(uniform.usage(), BufferUsage::Uniform);

        let staging = GPUBuffer::staging(512);
        assert_eq!(staging.usage(), BufferUsage::Staging);
        assert!(staging.is_mapped());
    }

    #[test]
    fn test_buffer_with_label() {
        let buffer = GPUBuffer::storage(1024).with_label("test_buffer");
        assert_eq!(buffer.label(), Some("test_buffer"));
    }

    #[test]
    fn test_compute_pipeline() {
        let shader = WGSLShader::new("@compute fn main() {}");
        let pipeline = ComputePipeline::new(shader.clone());
        assert_eq!(pipeline.shader().source(), shader.source());
        assert_eq!(pipeline.bind_groups().len(), 0);
    }

    #[test]
    fn test_pipeline_with_label() {
        let shader = WGSLShader::new("@compute fn main() {}");
        let pipeline = ComputePipeline::new(shader).with_label("test_pipeline");
        assert_eq!(pipeline.label(), Some("test_pipeline"));
    }

    #[test]
    fn test_optimal_dispatch_size() {
        let shader = WGSLShader::new("code").with_workgroup_size(64, 1, 1);
        let pipeline = ComputePipeline::new(shader);

        let (x, _, _) = pipeline.optimal_dispatch_size(1000);
        assert_eq!(x, 16); // ceil(1000 / 64) = 16
    }

    #[test]
    fn test_bind_group_entry() {
        let entry = BindGroupEntry::new(0, ResourceType::StorageBuffer, ShaderStage::Compute);
        assert_eq!(entry.binding(), 0);
        assert_eq!(entry.resource_type(), ResourceType::StorageBuffer);
        assert_eq!(entry.visibility(), ShaderStage::Compute);
    }

    #[test]
    fn test_bind_group_layout() {
        let entry = BindGroupEntry::new(0, ResourceType::StorageBuffer, ShaderStage::Compute);
        let layout = BindGroupLayout::new().with_entry(entry);
        assert_eq!(layout.entries().len(), 1);
    }

    #[test]
    fn test_workgroup_optimizer() {
        let optimizer = WorkgroupOptimizer::new(512);
        assert_eq!(optimizer.optimize_1d(50), 64); // size <= 64 -> 64
        assert_eq!(optimizer.optimize_1d(100), 256); // size <= 256 -> 256
        assert_eq!(optimizer.optimize_1d(500), 512); // size > 256 -> min(max, 512)

        let (w, h) = optimizer.optimize_2d(10, 10);
        assert_eq!((w, h), (8, 8));

        let (w, h, d) = optimizer.optimize_3d(10, 10, 10);
        assert_eq!((w, h, d), (8, 8, 4));
    }

    #[test]
    fn test_default_workgroup_optimizer() {
        let optimizer = WorkgroupOptimizer::default();
        assert_eq!(optimizer.max_workgroup_size(), 256);
    }

    #[test]
    fn test_pipeline_cache() {
        let mut cache = PipelineCache::new(10);
        assert_eq!(cache.size(), 0);

        let shader = WGSLShader::new("@compute fn main() {}");
        let _pipeline = cache.get_or_create(&shader, |s| ComputePipeline::new(s.clone()));
        assert_eq!(cache.size(), 1);

        cache.clear();
        assert_eq!(cache.size(), 0);
    }

    #[test]
    fn test_shader_templates() {
        let add_shader = shaders::elementwise_add();
        assert!(add_shader.source().contains("input_a"));
        assert!(add_shader.validate().is_ok());

        let mul_shader = shaders::elementwise_mul();
        assert!(mul_shader.source().contains("input_b"));
        assert!(mul_shader.validate().is_ok());

        let matmul_shader = shaders::matrix_mul();
        assert!(matmul_shader.source().contains("matrix_a"));
        assert_eq!(matmul_shader.workgroup_size(), (16, 16, 1));
        assert!(matmul_shader.validate().is_ok());

        let reduce_shader = shaders::reduce_sum();
        assert!(reduce_shader.source().contains("shared_data"));
        assert!(reduce_shader.validate().is_ok());
    }

    #[test]
    fn test_resource_types() {
        let _storage = ResourceType::StorageBuffer;
        let _uniform = ResourceType::UniformBuffer;
        let _readonly = ResourceType::ReadOnlyStorageBuffer;
        let _texture = ResourceType::Texture;
        let _sampler = ResourceType::Sampler;
    }

    #[test]
    fn test_shader_stages() {
        let _vertex = ShaderStage::Vertex;
        let _fragment = ShaderStage::Fragment;
        let _compute = ShaderStage::Compute;
        let _all = ShaderStage::All;
    }

    #[test]
    fn test_buffer_usage_types() {
        let _storage = BufferUsage::Storage;
        let _uniform = BufferUsage::Uniform;
        let _staging = BufferUsage::Staging;
        let _vertex = BufferUsage::Vertex;
        let _index = BufferUsage::Index;
    }

    #[test]
    fn test_shader_error_display() {
        let err = ShaderError::EmptyShader;
        assert_eq!(format!("{}", err), "Shader source is empty");

        let err = ShaderError::MissingComputeAttribute;
        assert_eq!(format!("{}", err), "Missing @compute attribute");

        let err = ShaderError::SyntaxError("test".to_string());
        assert_eq!(format!("{}", err), "Syntax error: test");

        let err = ShaderError::CompilationFailed("test".to_string());
        assert_eq!(format!("{}", err), "Compilation failed: test");
    }

    #[test]
    fn test_pipeline_with_bind_group() {
        let shader = WGSLShader::new("@compute fn main() {}");
        let layout = BindGroupLayout::new();
        let pipeline = ComputePipeline::new(shader).with_bind_group(layout);
        assert_eq!(pipeline.bind_groups().len(), 1);
    }

    #[test]
    fn test_bind_group_with_label() {
        let layout = BindGroupLayout::new().with_label("test_layout");
        assert_eq!(layout.label(), Some("test_layout"));
    }

    #[test]
    fn test_default_bind_group_layout() {
        let layout = BindGroupLayout::default();
        assert_eq!(layout.entries().len(), 0);
    }

    #[test]
    fn test_cache_max_size() {
        let cache = PipelineCache::new(5);
        assert_eq!(cache.max_size(), 5);
    }
}