skia-rs-gpu 0.2.1

GPU backends for skia-rs
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
//! GPU Shader Debugging Tools
//!
//! This module provides comprehensive tools for debugging GPU shaders in skia-rs.
//! It includes shader inspection, validation, profiling, and diagnostic utilities.
//!
//! # Overview
//!
//! The shader debugging system provides:
//! - **Shader Source Inspection**: View generated shader source code
//! - **Shader Validation**: Validate shaders against GLSL/WGSL specifications
//! - **Performance Profiling**: Measure shader compilation and execution time
//! - **Error Diagnostics**: Detailed error messages with line/column information
//! - **Shader Statistics**: Instruction counts, register usage, etc.
//!
//! # Example
//!
//! ```ignore
//! use skia_rs_gpu::debug::{ShaderDebugger, ShaderType};
//!
//! let debugger = ShaderDebugger::new();
//!
//! // Inspect a shader
//! let info = debugger.inspect_shader(shader_id)?;
//! println!("Shader type: {:?}", info.shader_type);
//! println!("Source:\n{}", info.source);
//!
//! // Validate shader
//! let validation = debugger.validate_shader(&source, ShaderType::Fragment)?;
//! if !validation.is_valid {
//!     for error in &validation.errors {
//!         eprintln!("Error at {}:{}: {}", error.line, error.column, error.message);
//!     }
//! }
//! ```

use std::collections::HashMap;
use std::fmt;
use std::time::{Duration, Instant};

// =============================================================================
// Shader Types and Enums
// =============================================================================

/// Type of shader
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ShaderType {
    /// Vertex shader
    Vertex,
    /// Fragment/pixel shader
    Fragment,
    /// Compute shader
    Compute,
    /// Geometry shader (if supported)
    Geometry,
    /// Tessellation control shader
    TessControl,
    /// Tessellation evaluation shader
    TessEval,
}

impl fmt::Display for ShaderType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ShaderType::Vertex => write!(f, "Vertex"),
            ShaderType::Fragment => write!(f, "Fragment"),
            ShaderType::Compute => write!(f, "Compute"),
            ShaderType::Geometry => write!(f, "Geometry"),
            ShaderType::TessControl => write!(f, "TessControl"),
            ShaderType::TessEval => write!(f, "TessEval"),
        }
    }
}

/// Shader language
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ShaderLanguage {
    /// OpenGL Shading Language
    Glsl,
    /// WebGPU Shading Language
    Wgsl,
    /// SPIR-V binary
    SpirV,
    /// Metal Shading Language
    Msl,
    /// High Level Shading Language (DirectX)
    Hlsl,
}

impl fmt::Display for ShaderLanguage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ShaderLanguage::Glsl => write!(f, "GLSL"),
            ShaderLanguage::Wgsl => write!(f, "WGSL"),
            ShaderLanguage::SpirV => write!(f, "SPIR-V"),
            ShaderLanguage::Msl => write!(f, "MSL"),
            ShaderLanguage::Hlsl => write!(f, "HLSL"),
        }
    }
}

// =============================================================================
// Shader Information
// =============================================================================

/// Information about a compiled shader
#[derive(Debug, Clone)]
pub struct ShaderInfo {
    /// Unique identifier for this shader
    pub id: u64,
    /// Type of shader
    pub shader_type: ShaderType,
    /// Source language
    pub language: ShaderLanguage,
    /// Original source code
    pub source: String,
    /// Compiled binary (if available)
    pub binary: Option<Vec<u8>>,
    /// Compilation time
    pub compile_time: Duration,
    /// Shader statistics
    pub stats: ShaderStats,
    /// Entry point name
    pub entry_point: String,
    /// Input/output bindings
    pub bindings: Vec<ShaderBinding>,
}

/// Statistics about a shader
#[derive(Debug, Clone, Default)]
pub struct ShaderStats {
    /// Estimated instruction count
    pub instruction_count: u32,
    /// Estimated register usage
    pub register_count: u32,
    /// Number of texture samplers used
    pub sampler_count: u32,
    /// Number of uniform buffers
    pub uniform_buffer_count: u32,
    /// Number of storage buffers
    pub storage_buffer_count: u32,
    /// Number of input attributes
    pub input_count: u32,
    /// Number of output attributes
    pub output_count: u32,
    /// Whether the shader uses derivatives
    pub uses_derivatives: bool,
    /// Whether the shader uses discard
    pub uses_discard: bool,
}

/// A shader binding (uniform, texture, etc.)
#[derive(Debug, Clone)]
pub struct ShaderBinding {
    /// Binding name
    pub name: String,
    /// Binding location/index
    pub location: u32,
    /// Binding type
    pub binding_type: BindingType,
    /// Binding group (for WGSL)
    pub group: u32,
}

/// Type of shader binding
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BindingType {
    /// Uniform buffer
    UniformBuffer,
    /// Storage buffer
    StorageBuffer,
    /// Texture sampler
    Sampler,
    /// Texture
    Texture,
    /// Combined texture/sampler
    CombinedImageSampler,
    /// Storage image
    StorageImage,
    /// Input attribute
    Input,
    /// Output attribute
    Output,
}

// =============================================================================
// Validation Results
// =============================================================================

/// Result of shader validation
#[derive(Debug, Clone)]
pub struct ValidationResult {
    /// Whether the shader is valid
    pub is_valid: bool,
    /// Validation errors
    pub errors: Vec<ShaderError>,
    /// Validation warnings
    pub warnings: Vec<ShaderWarning>,
    /// Validation info messages
    pub info: Vec<String>,
}

impl ValidationResult {
    /// Create a valid result
    pub fn valid() -> Self {
        Self {
            is_valid: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            info: Vec::new(),
        }
    }

    /// Create an invalid result with a single error
    pub fn error(error: ShaderError) -> Self {
        Self {
            is_valid: false,
            errors: vec![error],
            warnings: Vec::new(),
            info: Vec::new(),
        }
    }
}

/// A shader compilation/validation error
#[derive(Debug, Clone)]
pub struct ShaderError {
    /// Error message
    pub message: String,
    /// Line number (1-indexed, 0 if unknown)
    pub line: u32,
    /// Column number (1-indexed, 0 if unknown)
    pub column: u32,
    /// Error code (if applicable)
    pub code: Option<String>,
    /// Source snippet around the error
    pub snippet: Option<String>,
}

impl fmt::Display for ShaderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.line > 0 {
            write!(f, "{}:{}: {}", self.line, self.column, self.message)
        } else {
            write!(f, "{}", self.message)
        }
    }
}

/// A shader compilation/validation warning
#[derive(Debug, Clone)]
pub struct ShaderWarning {
    /// Warning message
    pub message: String,
    /// Line number (1-indexed, 0 if unknown)
    pub line: u32,
    /// Column number (1-indexed, 0 if unknown)
    pub column: u32,
    /// Warning code (if applicable)
    pub code: Option<String>,
}

impl fmt::Display for ShaderWarning {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.line > 0 {
            write!(f, "{}:{}: warning: {}", self.line, self.column, self.message)
        } else {
            write!(f, "warning: {}", self.message)
        }
    }
}

// =============================================================================
// Shader Debugger
// =============================================================================

/// GPU shader debugging tool
///
/// Provides utilities for inspecting, validating, and profiling shaders.
#[derive(Debug)]
pub struct ShaderDebugger {
    /// Cached shader information
    shader_cache: HashMap<u64, ShaderInfo>,
    /// Next shader ID
    next_id: u64,
    /// Debug verbosity level
    verbosity: DebugVerbosity,
    /// Whether to capture shader source
    capture_source: bool,
    /// Whether to capture shader binaries
    capture_binary: bool,
}

/// Debug verbosity level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DebugVerbosity {
    /// Minimal output (errors only)
    #[default]
    Minimal,
    /// Normal output (errors and warnings)
    Normal,
    /// Verbose output (all messages)
    Verbose,
    /// Maximum verbosity (include debug info)
    Debug,
}

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

impl ShaderDebugger {
    /// Create a new shader debugger
    pub fn new() -> Self {
        Self {
            shader_cache: HashMap::new(),
            next_id: 1,
            verbosity: DebugVerbosity::Normal,
            capture_source: true,
            capture_binary: false,
        }
    }

    /// Set debug verbosity level
    pub fn set_verbosity(&mut self, verbosity: DebugVerbosity) {
        self.verbosity = verbosity;
    }

    /// Enable/disable source capture
    pub fn set_capture_source(&mut self, capture: bool) {
        self.capture_source = capture;
    }

    /// Enable/disable binary capture
    pub fn set_capture_binary(&mut self, capture: bool) {
        self.capture_binary = capture;
    }

    /// Register a shader for debugging
    pub fn register_shader(
        &mut self,
        source: &str,
        shader_type: ShaderType,
        language: ShaderLanguage,
    ) -> u64 {
        let id = self.next_id;
        self.next_id += 1;

        let start = Instant::now();
        let stats = Self::analyze_shader(source, shader_type, language);
        let compile_time = start.elapsed();

        let info = ShaderInfo {
            id,
            shader_type,
            language,
            source: if self.capture_source {
                source.to_string()
            } else {
                String::new()
            },
            binary: None,
            compile_time,
            stats,
            entry_point: "main".to_string(),
            bindings: Self::extract_bindings(source, language),
        };

        self.shader_cache.insert(id, info);
        id
    }

    /// Get information about a registered shader
    pub fn get_shader_info(&self, id: u64) -> Option<&ShaderInfo> {
        self.shader_cache.get(&id)
    }

    /// Validate a shader
    pub fn validate_shader(
        &self,
        source: &str,
        shader_type: ShaderType,
        language: ShaderLanguage,
    ) -> ValidationResult {
        match language {
            ShaderLanguage::Glsl => self.validate_glsl(source, shader_type),
            ShaderLanguage::Wgsl => self.validate_wgsl(source, shader_type),
            ShaderLanguage::SpirV => ValidationResult::valid(), // Binary validation not implemented
            ShaderLanguage::Msl => self.validate_msl(source, shader_type),
            ShaderLanguage::Hlsl => self.validate_hlsl(source, shader_type),
        }
    }

    /// Analyze shader and extract statistics
    fn analyze_shader(
        source: &str,
        _shader_type: ShaderType,
        language: ShaderLanguage,
    ) -> ShaderStats {
        let mut stats = ShaderStats::default();

        // Count lines (rough instruction estimate)
        let lines: Vec<&str> = source.lines().collect();
        stats.instruction_count = lines
            .iter()
            .filter(|l| {
                let trimmed = l.trim();
                !trimmed.is_empty() && !trimmed.starts_with("//") && !trimmed.starts_with('#')
            })
            .count() as u32;

        // Count specific patterns based on language
        match language {
            ShaderLanguage::Glsl => {
                stats.sampler_count = source.matches("sampler").count() as u32;
                stats.uniform_buffer_count = source.matches("uniform ").count() as u32;
                stats.uses_derivatives = source.contains("dFdx") || source.contains("dFdy");
                stats.uses_discard = source.contains("discard");
            }
            ShaderLanguage::Wgsl => {
                stats.sampler_count = source.matches("sampler").count() as u32;
                stats.uniform_buffer_count = source.matches("@group").count() as u32;
                stats.uses_derivatives = source.contains("dpdx") || source.contains("dpdy");
                stats.uses_discard = source.contains("discard");
            }
            _ => {}
        }

        stats
    }

    /// Extract bindings from shader source
    fn extract_bindings(source: &str, language: ShaderLanguage) -> Vec<ShaderBinding> {
        let mut bindings = Vec::new();

        match language {
            ShaderLanguage::Glsl => {
                // Extract GLSL uniforms
                for (idx, line) in source.lines().enumerate() {
                    let line = line.trim();
                    if line.starts_with("uniform ") {
                        if let Some(name) = Self::extract_glsl_uniform_name(line) {
                            bindings.push(ShaderBinding {
                                name,
                                location: idx as u32,
                                binding_type: BindingType::UniformBuffer,
                                group: 0,
                            });
                        }
                    }
                }
            }
            ShaderLanguage::Wgsl => {
                // Extract WGSL bindings
                for line in source.lines() {
                    if line.contains("@group") && line.contains("@binding") {
                        if let Some(binding) = Self::parse_wgsl_binding(line) {
                            bindings.push(binding);
                        }
                    }
                }
            }
            _ => {}
        }

        bindings
    }

    fn extract_glsl_uniform_name(line: &str) -> Option<String> {
        // Simple parser for "uniform type name;"
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() >= 3 {
            let name = parts[2].trim_end_matches(';').trim_end_matches('[');
            return Some(name.to_string());
        }
        None
    }

    fn parse_wgsl_binding(line: &str) -> Option<ShaderBinding> {
        // Parse @group(N) @binding(M) var<...> name: type;
        let group = Self::extract_number_from_attr(line, "@group(")?;
        let binding = Self::extract_number_from_attr(line, "@binding(")?;

        // Find var name
        let var_idx = line.find("var")?;
        let after_var = &line[var_idx..];
        let name_start = after_var.find(' ')? + 1;
        let name_end = after_var[name_start..].find(':')?;
        let name = after_var[name_start..name_start + name_end].trim().to_string();

        let binding_type = if line.contains("sampler") {
            BindingType::Sampler
        } else if line.contains("texture") {
            BindingType::Texture
        } else if line.contains("<uniform>") {
            BindingType::UniformBuffer
        } else if line.contains("<storage") {
            BindingType::StorageBuffer
        } else {
            BindingType::UniformBuffer
        };

        Some(ShaderBinding {
            name,
            location: binding,
            binding_type,
            group,
        })
    }

    fn extract_number_from_attr(line: &str, prefix: &str) -> Option<u32> {
        let start = line.find(prefix)? + prefix.len();
        let end = line[start..].find(')')?;
        line[start..start + end].parse().ok()
    }

    // Validation implementations
    fn validate_glsl(&self, source: &str, shader_type: ShaderType) -> ValidationResult {
        let mut result = ValidationResult::valid();

        // Check for version directive
        if !source.contains("#version") {
            result.warnings.push(ShaderWarning {
                message: "Missing #version directive".to_string(),
                line: 1,
                column: 1,
                code: Some("W001".to_string()),
            });
        }

        // Check for main function
        if !source.contains("void main()") && !source.contains("void main(void)") {
            result.errors.push(ShaderError {
                message: "Missing main() function".to_string(),
                line: 0,
                column: 0,
                code: Some("E001".to_string()),
                snippet: None,
            });
            result.is_valid = false;
        }

        // Check for required outputs based on shader type
        match shader_type {
            ShaderType::Vertex => {
                if !source.contains("gl_Position") {
                    result.warnings.push(ShaderWarning {
                        message: "Vertex shader does not write to gl_Position".to_string(),
                        line: 0,
                        column: 0,
                        code: Some("W002".to_string()),
                    });
                }
            }
            ShaderType::Fragment => {
                // Fragment shaders should have an output
                if !source.contains("out ") && !source.contains("gl_FragColor") {
                    result.warnings.push(ShaderWarning {
                        message: "Fragment shader has no output variable".to_string(),
                        line: 0,
                        column: 0,
                        code: Some("W003".to_string()),
                    });
                }
            }
            _ => {}
        }

        // Check for mismatched braces
        let open_braces = source.matches('{').count();
        let close_braces = source.matches('}').count();
        if open_braces != close_braces {
            result.errors.push(ShaderError {
                message: format!(
                    "Mismatched braces: {} open, {} close",
                    open_braces, close_braces
                ),
                line: 0,
                column: 0,
                code: Some("E002".to_string()),
                snippet: None,
            });
            result.is_valid = false;
        }

        result
    }

    fn validate_wgsl(&self, source: &str, shader_type: ShaderType) -> ValidationResult {
        let mut result = ValidationResult::valid();

        // Check for entry point
        let entry_point = match shader_type {
            ShaderType::Vertex => "@vertex",
            ShaderType::Fragment => "@fragment",
            ShaderType::Compute => "@compute",
            _ => "",
        };

        if !entry_point.is_empty() && !source.contains(entry_point) {
            result.errors.push(ShaderError {
                message: format!("Missing {} entry point annotation", entry_point),
                line: 0,
                column: 0,
                code: Some("E001".to_string()),
                snippet: None,
            });
            result.is_valid = false;
        }

        // Check for fn main or named entry point
        if !source.contains("fn ") {
            result.errors.push(ShaderError {
                message: "No function definitions found".to_string(),
                line: 0,
                column: 0,
                code: Some("E002".to_string()),
                snippet: None,
            });
            result.is_valid = false;
        }

        result
    }

    fn validate_msl(&self, source: &str, _shader_type: ShaderType) -> ValidationResult {
        let mut result = ValidationResult::valid();

        // Check for metal stdlib
        if !source.contains("#include <metal_stdlib>") {
            result.warnings.push(ShaderWarning {
                message: "Missing #include <metal_stdlib>".to_string(),
                line: 1,
                column: 1,
                code: Some("W001".to_string()),
            });
        }

        result
    }

    fn validate_hlsl(&self, source: &str, _shader_type: ShaderType) -> ValidationResult {
        let mut result = ValidationResult::valid();

        // Basic HLSL validation
        if !source.contains("main") && !source.contains("Main") {
            result.warnings.push(ShaderWarning {
                message: "No main entry point found".to_string(),
                line: 0,
                column: 0,
                code: Some("W001".to_string()),
            });
        }

        result
    }

    /// Dump shader information for debugging
    pub fn dump_shader(&self, id: u64) -> String {
        if let Some(info) = self.shader_cache.get(&id) {
            let mut output = String::new();
            output.push_str(&format!("=== Shader {} ===\n", id));
            output.push_str(&format!("Type: {}\n", info.shader_type));
            output.push_str(&format!("Language: {}\n", info.language));
            output.push_str(&format!("Entry Point: {}\n", info.entry_point));
            output.push_str(&format!("Compile Time: {:?}\n", info.compile_time));
            output.push_str("\n--- Statistics ---\n");
            output.push_str(&format!(
                "Instructions: ~{}\n",
                info.stats.instruction_count
            ));
            output.push_str(&format!("Registers: ~{}\n", info.stats.register_count));
            output.push_str(&format!("Samplers: {}\n", info.stats.sampler_count));
            output.push_str(&format!(
                "Uniform Buffers: {}\n",
                info.stats.uniform_buffer_count
            ));
            output.push_str(&format!(
                "Uses Derivatives: {}\n",
                info.stats.uses_derivatives
            ));
            output.push_str(&format!("Uses Discard: {}\n", info.stats.uses_discard));

            if !info.bindings.is_empty() {
                output.push_str("\n--- Bindings ---\n");
                for binding in &info.bindings {
                    output.push_str(&format!(
                        "  {} (group={}, binding={}, type={:?})\n",
                        binding.name, binding.group, binding.location, binding.binding_type
                    ));
                }
            }

            if !info.source.is_empty() {
                output.push_str("\n--- Source ---\n");
                for (i, line) in info.source.lines().enumerate() {
                    output.push_str(&format!("{:4} | {}\n", i + 1, line));
                }
            }

            output
        } else {
            format!("Shader {} not found", id)
        }
    }

    /// Get all registered shader IDs
    pub fn get_shader_ids(&self) -> Vec<u64> {
        self.shader_cache.keys().copied().collect()
    }

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

// =============================================================================
// Shader Profiler
// =============================================================================

/// Shader performance profiler
#[derive(Debug, Default)]
pub struct ShaderProfiler {
    /// Compilation time samples
    compile_times: HashMap<u64, Vec<Duration>>,
    /// Execution time samples (if available)
    execution_times: HashMap<u64, Vec<Duration>>,
}

impl ShaderProfiler {
    /// Create a new shader profiler
    pub fn new() -> Self {
        Self::default()
    }

    /// Record compilation time for a shader
    pub fn record_compile_time(&mut self, shader_id: u64, duration: Duration) {
        self.compile_times
            .entry(shader_id)
            .or_default()
            .push(duration);
    }

    /// Record execution time for a shader
    pub fn record_execution_time(&mut self, shader_id: u64, duration: Duration) {
        self.execution_times
            .entry(shader_id)
            .or_default()
            .push(duration);
    }

    /// Get average compilation time for a shader
    pub fn avg_compile_time(&self, shader_id: u64) -> Option<Duration> {
        self.compile_times.get(&shader_id).map(|times| {
            let total: Duration = times.iter().sum();
            total / times.len() as u32
        })
    }

    /// Get average execution time for a shader
    pub fn avg_execution_time(&self, shader_id: u64) -> Option<Duration> {
        self.execution_times.get(&shader_id).map(|times| {
            let total: Duration = times.iter().sum();
            total / times.len() as u32
        })
    }

    /// Generate a profiling report
    pub fn generate_report(&self) -> String {
        let mut output = String::new();
        output.push_str("=== Shader Profiling Report ===\n\n");

        output.push_str("Compilation Times:\n");
        for (id, times) in &self.compile_times {
            let avg = times.iter().sum::<Duration>() / times.len() as u32;
            let min = times.iter().min().unwrap();
            let max = times.iter().max().unwrap();
            output.push_str(&format!(
                "  Shader {}: avg={:?}, min={:?}, max={:?}, samples={}\n",
                id,
                avg,
                min,
                max,
                times.len()
            ));
        }

        if !self.execution_times.is_empty() {
            output.push_str("\nExecution Times:\n");
            for (id, times) in &self.execution_times {
                let avg = times.iter().sum::<Duration>() / times.len() as u32;
                let min = times.iter().min().unwrap();
                let max = times.iter().max().unwrap();
                output.push_str(&format!(
                    "  Shader {}: avg={:?}, min={:?}, max={:?}, samples={}\n",
                    id,
                    avg,
                    min,
                    max,
                    times.len()
                ));
            }
        }

        output
    }
}

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

    #[test]
    fn test_shader_debugger() {
        let mut debugger = ShaderDebugger::new();

        let glsl_source = r#"
#version 450
uniform mat4 uMVP;
in vec3 aPosition;
out vec4 vColor;
void main() {
    gl_Position = uMVP * vec4(aPosition, 1.0);
    vColor = vec4(1.0);
}
"#;

        let id = debugger.register_shader(glsl_source, ShaderType::Vertex, ShaderLanguage::Glsl);
        assert!(id > 0);

        let info = debugger.get_shader_info(id).unwrap();
        assert_eq!(info.shader_type, ShaderType::Vertex);
        assert_eq!(info.language, ShaderLanguage::Glsl);
    }

    #[test]
    fn test_glsl_validation() {
        let debugger = ShaderDebugger::new();

        // Valid shader
        let valid = r#"
#version 450
void main() {
    gl_Position = vec4(0.0);
}
"#;
        let result = debugger.validate_shader(valid, ShaderType::Vertex, ShaderLanguage::Glsl);
        assert!(result.is_valid);

        // Invalid shader (missing main)
        let invalid = r#"
#version 450
void notmain() { }
"#;
        let result = debugger.validate_shader(invalid, ShaderType::Vertex, ShaderLanguage::Glsl);
        assert!(!result.is_valid);
    }

    #[test]
    fn test_wgsl_validation() {
        let debugger = ShaderDebugger::new();

        let wgsl = r#"
@vertex
fn main(@location(0) position: vec3<f32>) -> @builtin(position) vec4<f32> {
    return vec4<f32>(position, 1.0);
}
"#;
        let result = debugger.validate_shader(wgsl, ShaderType::Vertex, ShaderLanguage::Wgsl);
        assert!(result.is_valid);
    }

    #[test]
    fn test_shader_profiler() {
        let mut profiler = ShaderProfiler::new();

        profiler.record_compile_time(1, Duration::from_millis(10));
        profiler.record_compile_time(1, Duration::from_millis(12));
        profiler.record_compile_time(1, Duration::from_millis(8));

        let avg = profiler.avg_compile_time(1).unwrap();
        assert_eq!(avg, Duration::from_millis(10));
    }
}