-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnaga_test.go
More file actions
600 lines (524 loc) · 16.6 KB
/
Copy pathnaga_test.go
File metadata and controls
600 lines (524 loc) · 16.6 KB
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
package naga
import (
"testing"
"github.com/gogpu/naga/spirv"
)
// TestCompileSimpleVertexShader tests compilation of a basic vertex shader.
func TestCompileSimpleVertexShader(t *testing.T) {
// Skip validation for this test as the shader is minimal
source := `
@vertex
fn main(@builtin(vertex_index) idx: u32) -> @builtin(position) vec4<f32> {
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
`
opts := CompileOptions{
Debug: false,
Validate: false, // Skip validation for minimal shader
}
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}
// Check SPIR-V magic number (little-endian: 0x07230203)
if len(spirvBytes) < 4 {
t.Fatal("Output too short")
}
magic := uint32(spirvBytes[0]) | uint32(spirvBytes[1])<<8 | uint32(spirvBytes[2])<<16 | uint32(spirvBytes[3])<<24
expectedMagic := uint32(0x07230203)
if magic != expectedMagic {
t.Errorf("Invalid SPIR-V magic: got 0x%08x, want 0x%08x", magic, expectedMagic)
}
t.Logf("Generated %d bytes of SPIR-V", len(spirvBytes))
}
// TestCompileFragmentShader tests compilation of a fragment shader.
func TestCompileFragmentShader(t *testing.T) {
source := `
@fragment
fn main(@location(0) color: vec4<f32>) -> @location(0) vec4<f32> {
return color;
}
`
opts := CompileOptions{Validate: false} // Skip validation for minimal shader
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}
// Verify SPIR-V header
if len(spirvBytes) < 20 {
t.Fatal("SPIR-V output too short (should have at least 5-word header)")
}
// Check magic number
magic := uint32(spirvBytes[0]) | uint32(spirvBytes[1])<<8 | uint32(spirvBytes[2])<<16 | uint32(spirvBytes[3])<<24
if magic != 0x07230203 {
t.Errorf("Invalid SPIR-V magic: got 0x%08x, want 0x07230203", magic)
}
t.Logf("Generated %d bytes of SPIR-V", len(spirvBytes))
}
// TestCompileWithMathFunctions tests compilation with built-in math functions.
func TestCompileWithMathFunctions(t *testing.T) {
source := `
@fragment
fn main(@location(0) v: vec3<f32>) -> @location(0) vec4<f32> {
let n = normalize(v);
let len = length(v);
return vec4<f32>(n, len);
}
`
opts := CompileOptions{Validate: false} // Skip validation for test
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}
// Just verify we got valid output
if len(spirvBytes) < 20 {
t.Fatal("Output too short")
}
t.Logf("Generated %d bytes of SPIR-V", len(spirvBytes))
}
// TestCompileComputeShader tests compilation of a compute shader.
func TestCompileComputeShader(t *testing.T) {
source := `
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
// Compute work here
}
`
opts := CompileOptions{Validate: false} // Skip validation for test
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}
// Verify SPIR-V magic
if len(spirvBytes) < 4 {
t.Fatal("Output too short")
}
magic := uint32(spirvBytes[0]) | uint32(spirvBytes[1])<<8 | uint32(spirvBytes[2])<<16 | uint32(spirvBytes[3])<<24
if magic != 0x07230203 {
t.Errorf("Invalid SPIR-V magic: got 0x%08x, want 0x07230203", magic)
}
t.Logf("Generated %d bytes of SPIR-V", len(spirvBytes))
}
// TestCompileWithOptions tests compilation with custom options.
func TestCompileWithOptions(t *testing.T) {
source := `
@vertex
fn main() -> @builtin(position) vec4<f32> {
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
`
opts := CompileOptions{
Debug: true,
Validate: false, // Skip validation for minimal shader
}
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("CompileWithOptions failed: %v", err)
}
if len(spirvBytes) < 20 {
t.Fatal("Output too short")
}
t.Logf("Generated %d bytes of SPIR-V (with debug info)", len(spirvBytes))
}
// TestCompileInvalidShader tests error handling for invalid shaders.
func TestCompileInvalidShader(t *testing.T) {
source := `
@vertex
fn main() -> vec4<f32> {
return vec4<f32>(0.0, 0.0); // Wrong number of components
}
`
_, err := Compile(source)
if err == nil {
t.Fatal("Expected compilation error for invalid shader, got nil")
}
t.Logf("Got expected error: %v", err)
}
// TestParseSyntaxError tests error handling for syntax errors.
func TestParseSyntaxError(t *testing.T) {
source := `
@vertex
fn main( { // Missing closing parenthesis
return vec4<f32>(0.0);
}
`
_, err := Parse(source)
if err == nil {
t.Fatal("Expected parse error for syntax error, got nil")
}
t.Logf("Got expected parse error: %v", err)
}
// TestParseAndLowerPipeline tests the individual stages of compilation.
func TestParseAndLowerPipeline(t *testing.T) {
source := `
@vertex
fn main(@builtin(vertex_index) idx: u32) -> @builtin(position) vec4<f32> {
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
`
// Stage 1: Parse
ast, err := Parse(source)
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
// Stage 2: Lower
module, err := Lower(ast)
if err != nil {
t.Fatalf("Lower failed: %v", err)
}
if len(module.EntryPoints) != 1 {
t.Errorf("Expected 1 entry point, got %d", len(module.EntryPoints))
}
// Stage 3: Validate (expect validation errors for minimal shader)
errors, err := Validate(module)
if err != nil {
t.Fatalf("Validate failed: %v", err)
}
// Note: Minimal shader has validation errors (missing bindings), which is expected
if len(errors) == 0 {
t.Log("No validation errors (shader is valid)")
} else {
t.Logf("Expected validation errors for minimal shader: %v", errors[0])
}
t.Log("Successfully parsed, lowered, and validated shader")
}
// TestCompileTriangleShader tests a complete triangle rendering shader.
func TestCompileTriangleShader(t *testing.T) {
// Note: Array initialization syntax not yet fully supported
// TODO: Re-enable when array initialization is implemented
// t.Skip("Array initialization syntax not yet fully supported")
source := `
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) color: vec4<f32>,
}
@vertex
fn vs_main(@builtin(vertex_index) idx: u32) -> VertexOutput {
var out: VertexOutput;
// Triangle vertices
var pos = array<vec2<f32>, 3>(
vec2<f32>(0.0, 0.5),
vec2<f32>(-0.5, -0.5),
vec2<f32>(0.5, -0.5)
);
out.position = vec4<f32>(pos[idx], 0.0, 1.0);
out.color = vec4<f32>(1.0, 0.0, 0.0, 1.0);
return out;
}
@fragment
fn fs_main(@location(0) color: vec4<f32>) -> @location(0) vec4<f32> {
return color;
}
`
opts := CompileOptions{Validate: false} // Skip validation for test
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}
// Verify SPIR-V output
if len(spirvBytes) < 20 {
t.Fatal("Output too short")
}
t.Logf("Generated %d bytes of SPIR-V for triangle shader", len(spirvBytes))
}
// TestIntegrationVertexFragment tests the full pipeline for vertex and fragment shaders.
func TestIntegrationVertexFragment(t *testing.T) {
source := `
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) color: vec3<f32>,
}
@vertex
fn vs_main(@location(0) pos: vec3<f32>, @location(1) col: vec3<f32>) -> VertexOutput {
var output: VertexOutput;
output.position = vec4<f32>(pos.x, pos.y, pos.z, 1.0);
output.color = col;
return output;
}
@fragment
fn fs_main(@location(0) color: vec3<f32>) -> @location(0) vec4<f32> {
return vec4<f32>(color.x, color.y, color.z, 1.0);
}
`
opts := CompileOptions{Validate: false} // Skip validation for integration test
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}
// Verify SPIR-V magic number
if len(spirvBytes) < 20 {
t.Fatal("SPIR-V binary too short")
}
magic := uint32(spirvBytes[0]) | uint32(spirvBytes[1])<<8 | uint32(spirvBytes[2])<<16 | uint32(spirvBytes[3])<<24
if magic != 0x07230203 {
t.Errorf("Invalid SPIR-V magic: got 0x%08x, want 0x07230203", magic)
}
// Verify version (if set - can be 0 in some cases)
version := uint32(spirvBytes[4]) | uint32(spirvBytes[5])<<8 | uint32(spirvBytes[6])<<16 | uint32(spirvBytes[7])<<24
if version != 0 && (version < 0x00010000 || version > 0x00010600) {
t.Errorf("Invalid SPIR-V version: 0x%08x", version)
}
t.Logf("Successfully compiled vertex+fragment shader: %d bytes", len(spirvBytes))
}
// TestIntegrationComputeWithStorage tests compute shader with storage buffers.
func TestIntegrationComputeWithStorage(t *testing.T) {
// Note: Runtime-sized arrays not yet fully supported
// This test uses a simplified compute shader
source := `
@compute @workgroup_size(64, 1, 1)
fn main(@builtin(global_invocation_id) id: vec3<u32>) {
var temp: u32 = id.x * 2u;
}
`
opts := CompileOptions{Validate: false} // Skip validation for integration test
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}
// Verify SPIR-V header
if len(spirvBytes) < 20 {
t.Fatal("SPIR-V binary too short")
}
magic := uint32(spirvBytes[0]) | uint32(spirvBytes[1])<<8 | uint32(spirvBytes[2])<<16 | uint32(spirvBytes[3])<<24
if magic != 0x07230203 {
t.Errorf("Invalid SPIR-V magic: got 0x%08x, want 0x07230203", magic)
}
// Verify bound is reasonable (should have multiple IDs allocated)
bound := uint32(spirvBytes[12]) | uint32(spirvBytes[13])<<8 | uint32(spirvBytes[14])<<16 | uint32(spirvBytes[15])<<24
if bound < 10 {
t.Errorf("SPIR-V bound too small: %d (expected at least 10)", bound)
}
t.Logf("Successfully compiled compute shader with storage: %d bytes, bound=%d", len(spirvBytes), bound)
}
// TestIntegrationWithUniforms tests shader with uniform buffers.
func TestIntegrationWithUniforms(t *testing.T) {
// Note: Matrix multiplication not yet implemented
source := `
struct Camera {
view_proj: mat4x4<f32>,
}
@group(0) @binding(0) var<uniform> camera: Camera;
@vertex
fn main(@location(0) position: vec3<f32>) -> @builtin(position) vec4<f32> {
return vec4<f32>(position.x, position.y, position.z, 1.0);
}
`
opts := CompileOptions{Validate: false} // Skip validation for integration test
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}
// Verify SPIR-V is valid
if len(spirvBytes) < 20 {
t.Fatal("SPIR-V binary too short")
}
magic := uint32(spirvBytes[0]) | uint32(spirvBytes[1])<<8 | uint32(spirvBytes[2])<<16 | uint32(spirvBytes[3])<<24
if magic != 0x07230203 {
t.Errorf("Invalid SPIR-V magic: got 0x%08x, want 0x07230203", magic)
}
t.Logf("Successfully compiled shader with uniform buffer: %d bytes", len(spirvBytes))
}
// TestIntegrationPipelineAPI tests the individual pipeline stages.
func TestIntegrationPipelineAPI(t *testing.T) {
source := `
@vertex
fn main(@location(0) pos: vec3<f32>) -> @builtin(position) vec4<f32> {
return vec4<f32>(pos.x, pos.y, pos.z, 1.0);
}
`
// Test Parse stage
ast, err := Parse(source)
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
// Test Lower stage
module, err := Lower(ast)
if err != nil {
t.Fatalf("Lower failed: %v", err)
}
if len(module.EntryPoints) != 1 {
t.Errorf("Expected 1 entry point, got %d", len(module.EntryPoints))
}
// Test Validate stage
errors, err := Validate(module)
if err != nil {
t.Fatalf("Validate failed: %v", err)
}
// Note: Validation may report warnings for minimal shader
t.Logf("Validation completed with %d issues", len(errors))
// Test GenerateSPIRV stage
spirvOpts := spirv.Options{
Version: spirv.Version1_3,
Debug: false,
}
spirvBytes, err := GenerateSPIRV(module, spirvOpts)
if err != nil {
t.Fatalf("GenerateSPIRV failed: %v", err)
}
if len(spirvBytes) < 20 {
t.Fatal("SPIR-V output too short")
}
t.Logf("Pipeline test successful: %d bytes SPIR-V", len(spirvBytes))
}
// TestIntegrationErrorHandling tests error handling in the compilation pipeline.
func TestIntegrationErrorHandling(t *testing.T) {
tests := []struct {
name string
source string
expectError bool
skipValidation bool
}{
{
name: "valid shader",
source: `
@vertex
fn main() -> @builtin(position) vec4<f32> {
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
`,
expectError: false,
skipValidation: false, // Validation should pass with correct return type binding
},
{
name: "syntax error - missing parenthesis",
source: `
@vertex
fn main( -> @builtin(position) vec4<f32> {
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
`,
expectError: true,
skipValidation: false,
},
// NOTE: Component count validation for vector constructors is not yet implemented.
// The following test case would require semantic validation of constructor arguments.
// When implemented, uncomment this test:
// {
// name: "semantic error - wrong component count",
// source: `
// @vertex
// fn main() -> @builtin(position) vec4<f32> {
// return vec4<f32>(0.0, 0.0);
// }
// `,
// expectError: true,
// skipValidation: false,
// },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var err error
if tt.skipValidation {
opts := CompileOptions{Validate: false}
_, err = CompileWithOptions(tt.source, opts)
} else {
_, err = Compile(tt.source)
}
if tt.expectError && err == nil {
t.Error("Expected error but got nil")
}
if !tt.expectError && err != nil {
t.Errorf("Expected no error but got: %v", err)
}
})
}
}
// TestCompileSwitchStatement tests compilation of switch statements (NAGA-002).
func TestCompileSwitchStatement(t *testing.T) {
source := `
@fragment
fn main(@location(0) idx: u32) -> @location(0) vec4<f32> {
var color: vec4<f32>;
switch idx {
case 0u: { color = vec4<f32>(1.0, 0.0, 0.0, 1.0); }
case 1u: { color = vec4<f32>(0.0, 1.0, 0.0, 1.0); }
default: { color = vec4<f32>(0.0, 0.0, 1.0, 1.0); }
}
return color;
}
`
opts := CompileOptions{Validate: false}
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}
// Check SPIR-V magic number
if len(spirvBytes) < 4 {
t.Fatal("Output too short")
}
magic := uint32(spirvBytes[0]) | uint32(spirvBytes[1])<<8 | uint32(spirvBytes[2])<<16 | uint32(spirvBytes[3])<<24
if magic != uint32(0x07230203) {
t.Errorf("Invalid SPIR-V magic: got 0x%08x, want 0x%08x", magic, uint32(0x07230203))
}
t.Logf("Generated %d bytes of SPIR-V for switch statement", len(spirvBytes))
}
// TestCompileLocalConst tests compilation of local const declarations (NAGA-002).
func TestCompileLocalConst(t *testing.T) {
source := `
@vertex
fn main(@builtin(vertex_index) idx: u32) -> @builtin(position) vec4<f32> {
const PI = 3.14159;
let x = PI * 2.0;
return vec4<f32>(x, 0.0, 0.0, 1.0);
}
`
opts := CompileOptions{Validate: false}
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}
if len(spirvBytes) < 4 {
t.Fatal("Output too short")
}
magic := uint32(spirvBytes[0]) | uint32(spirvBytes[1])<<8 | uint32(spirvBytes[2])<<16 | uint32(spirvBytes[3])<<24
if magic != uint32(0x07230203) {
t.Errorf("Invalid SPIR-V magic: got 0x%08x, want 0x%08x", magic, uint32(0x07230203))
}
t.Logf("Generated %d bytes of SPIR-V for local const", len(spirvBytes))
}
// TestCompileSwizzleAssignment tests that swizzle assignment decomposes
// correctly and produces valid SPIR-V through all backends.
func TestCompileSwizzleAssignment(t *testing.T) {
source := `enable swizzle_assignment;
@compute @workgroup_size(1)
fn main() {
var v = vec4<f32>(1.0, 2.0, 3.0, 4.0);
v.xz = vec2<f32>(10.0, 30.0);
v.yw += vec2<f32>(1.0, 1.0);
v.rgb = vec3<f32>(0.5, 0.6, 0.7);
}
`
opts := CompileOptions{Validate: false}
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("SPIR-V compile failed: %v", err)
}
if len(spirvBytes) < 4 {
t.Fatal("Output too short")
}
magic := uint32(spirvBytes[0]) | uint32(spirvBytes[1])<<8 | uint32(spirvBytes[2])<<16 | uint32(spirvBytes[3])<<24
if magic != uint32(0x07230203) {
t.Errorf("Invalid SPIR-V magic: got 0x%08x, want 0x%08x", magic, uint32(0x07230203))
}
t.Logf("Generated %d bytes of SPIR-V for swizzle assignment", len(spirvBytes))
}
// TestCompileSwizzleAssignmentCompound tests compound swizzle assignment to SPIR-V.
func TestCompileSwizzleAssignmentCompound(t *testing.T) {
source := `enable swizzle_assignment;
@compute @workgroup_size(1)
fn main() {
var v = vec4<f32>(1.0, 2.0, 3.0, 4.0);
var w = vec3<f32>(10.0, 20.0, 30.0);
v.ywx *= w;
}
`
opts := CompileOptions{Validate: false}
spirvBytes, err := CompileWithOptions(source, opts)
if err != nil {
t.Fatalf("SPIR-V compile failed: %v", err)
}
if len(spirvBytes) < 20 {
t.Fatal("Output too short")
}
t.Logf("Generated %d bytes of SPIR-V for compound swizzle assignment", len(spirvBytes))
}