|
| 1 | +package codegen |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gaarutyunov/guix/pkg/parser" |
| 8 | +) |
| 9 | + |
| 10 | +func TestE2EWGSLGeneration(t *testing.T) { |
| 11 | + input := `package shaders |
| 12 | +
|
| 13 | +@gpu type Uniforms struct { |
| 14 | + viewportSize vec2 |
| 15 | + color vec4 |
| 16 | +} |
| 17 | +
|
| 18 | +@binding(0, 0) @uniform var uniforms Uniforms |
| 19 | +
|
| 20 | +@vertex |
| 21 | +func vsMain(@builtin(vertex_index) idx uint32) vec4 { |
| 22 | + return vec4(0.0, 0.0, 0.0, 1.0) |
| 23 | +} |
| 24 | +
|
| 25 | +@fragment |
| 26 | +func fsMain(@location(0) color vec4) vec4 { |
| 27 | + return color |
| 28 | +}` |
| 29 | + |
| 30 | + // Parse |
| 31 | + p, err := parser.New() |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("Failed to create parser: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + file, err := p.ParseString(input) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("Failed to parse: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + // Verify parsing |
| 42 | + if len(file.GPUStructs) != 1 { |
| 43 | + t.Errorf("Expected 1 GPU struct, got %d", len(file.GPUStructs)) |
| 44 | + } |
| 45 | + |
| 46 | + if len(file.GPUBindings) != 1 { |
| 47 | + t.Errorf("Expected 1 GPU binding, got %d", len(file.GPUBindings)) |
| 48 | + } |
| 49 | + |
| 50 | + if len(file.GPUFunctions) != 2 { |
| 51 | + t.Errorf("Expected 2 GPU functions, got %d", len(file.GPUFunctions)) |
| 52 | + } |
| 53 | + |
| 54 | + // Generate WGSL |
| 55 | + wgslGen := NewWGSLGenerator() |
| 56 | + wgslOutput, err := wgslGen.Generate(file) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("Failed to generate WGSL: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + wgslCode := string(wgslOutput) |
| 62 | + t.Logf("Generated WGSL:\n%s", wgslCode) |
| 63 | + |
| 64 | + // Verify WGSL output contains expected elements |
| 65 | + expectedStrings := []string{ |
| 66 | + "struct Uniforms {", |
| 67 | + "viewportSize: vec2<f32>,", |
| 68 | + "color: vec4<f32>,", |
| 69 | + "@binding(0, 0)", |
| 70 | + "@uniform", |
| 71 | + "var<uniform> uniforms: Uniforms;", |
| 72 | + "@vertex", |
| 73 | + "fn vsMain(", |
| 74 | + "@builtin(vertex_index) idx: u32", |
| 75 | + "-> vec4<f32>", |
| 76 | + "@fragment", |
| 77 | + "fn fsMain(", |
| 78 | + "@location(0) color: vec4<f32>", |
| 79 | + } |
| 80 | + |
| 81 | + for _, expected := range expectedStrings { |
| 82 | + if !strings.Contains(wgslCode, expected) { |
| 83 | + t.Errorf("Expected WGSL to contain %q, but it didn't", expected) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Generate GPU Go code |
| 88 | + gpuGoGen := NewGPUGoGenerator(file.Package) |
| 89 | + gpuGoOutput, err := gpuGoGen.Generate(file) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("Failed to generate GPU Go code: %v", err) |
| 92 | + } |
| 93 | + |
| 94 | + goCode := string(gpuGoOutput) |
| 95 | + t.Logf("Generated Go code:\n%s", goCode) |
| 96 | + |
| 97 | + // Verify Go output contains expected elements |
| 98 | + expectedGoStrings := []string{ |
| 99 | + "type Uniforms struct {", |
| 100 | + "ViewportSize", |
| 101 | + "[2]float32", |
| 102 | + "Color", |
| 103 | + "[4]float32", |
| 104 | + "func (s *Uniforms) ToBytes() []byte", |
| 105 | + "//go:embed shaders.wgsl", |
| 106 | + "var ShaderSource string", |
| 107 | + } |
| 108 | + |
| 109 | + for _, expected := range expectedGoStrings { |
| 110 | + if !strings.Contains(goCode, expected) { |
| 111 | + t.Errorf("Expected Go code to contain %q, but it didn't", expected) |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestE2ECompleteShader(t *testing.T) { |
| 117 | + input := `package candle |
| 118 | +
|
| 119 | +@gpu type ChartUniforms struct { |
| 120 | + viewportSize vec2 |
| 121 | + dataRange vec4 |
| 122 | + candleWidth float32 |
| 123 | + upColor vec4 |
| 124 | + downColor vec4 |
| 125 | +} |
| 126 | +
|
| 127 | +@gpu type Candle struct { |
| 128 | + timestamp float32 |
| 129 | + open float32 |
| 130 | + high float32 |
| 131 | + low float32 |
| 132 | + close float32 |
| 133 | +} |
| 134 | +
|
| 135 | +@binding(0, 0) @uniform var uniforms ChartUniforms |
| 136 | +@binding(0, 1) @storage var candles []Candle |
| 137 | +
|
| 138 | +@vertex |
| 139 | +func vsCandle(@builtin(vertex_index) vid uint32, @builtin(instance_index) iid uint32) vec4 { |
| 140 | + c := candles[iid] |
| 141 | + bullish := c.close >= c.open |
| 142 | + x := (c.timestamp - uniforms.dataRange.x) / (uniforms.dataRange.z - uniforms.dataRange.x) |
| 143 | + return vec4(x, 0.0, 0.0, 1.0) |
| 144 | +} |
| 145 | +
|
| 146 | +@fragment |
| 147 | +func fsCandle(@location(0) color vec4) vec4 { |
| 148 | + return color |
| 149 | +}` |
| 150 | + |
| 151 | + // Parse |
| 152 | + p, err := parser.New() |
| 153 | + if err != nil { |
| 154 | + t.Fatalf("Failed to create parser: %v", err) |
| 155 | + } |
| 156 | + |
| 157 | + file, err := p.ParseString(input) |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("Failed to parse: %v", err) |
| 160 | + } |
| 161 | + |
| 162 | + // Generate WGSL |
| 163 | + wgslGen := NewWGSLGenerator() |
| 164 | + wgslOutput, err := wgslGen.Generate(file) |
| 165 | + if err != nil { |
| 166 | + t.Fatalf("Failed to generate WGSL: %v", err) |
| 167 | + } |
| 168 | + |
| 169 | + wgslCode := string(wgslOutput) |
| 170 | + t.Logf("Generated complete WGSL:\n%s", wgslCode) |
| 171 | + |
| 172 | + // Verify complex features |
| 173 | + if !strings.Contains(wgslCode, "array<Candle>") { |
| 174 | + t.Error("Expected array type for candles slice") |
| 175 | + } |
| 176 | + |
| 177 | + if !strings.Contains(wgslCode, "let c = candles[iid];") { |
| 178 | + t.Error("Expected array indexing in generated code") |
| 179 | + } |
| 180 | + |
| 181 | + if !strings.Contains(wgslCode, "c.close >= c.open") { |
| 182 | + t.Error("Expected comparison expression") |
| 183 | + } |
| 184 | + |
| 185 | + if !strings.Contains(wgslCode, "uniforms.dataRange.x") { |
| 186 | + t.Error("Expected field access") |
| 187 | + } |
| 188 | + |
| 189 | + // Generate Go code |
| 190 | + gpuGoGen := NewGPUGoGenerator(file.Package) |
| 191 | + gpuGoOutput, err := gpuGoGen.Generate(file) |
| 192 | + if err != nil { |
| 193 | + t.Fatalf("Failed to generate GPU Go code: %v", err) |
| 194 | + } |
| 195 | + |
| 196 | + goCode := string(gpuGoOutput) |
| 197 | + |
| 198 | + // Verify Go struct generation |
| 199 | + if !strings.Contains(goCode, "type ChartUniforms struct") { |
| 200 | + t.Error("Expected ChartUniforms struct") |
| 201 | + } |
| 202 | + |
| 203 | + if !strings.Contains(goCode, "type Candle struct") { |
| 204 | + t.Error("Expected Candle struct") |
| 205 | + } |
| 206 | +} |
0 commit comments