-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgraphics_vk_spirv_selftest.cpp
More file actions
613 lines (582 loc) · 21.8 KB
/
Copy pathgraphics_vk_spirv_selftest.cpp
File metadata and controls
613 lines (582 loc) · 21.8 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
601
602
603
604
605
606
607
608
609
610
611
612
613
#include "subsystems/graphics/graphics_vk_spirv.h"
#include "arch/x86_64/serial.h"
#include "core/panic.h"
#include "util/soft_float.h"
/*
* DuetOS — SPIR-V interpreter boot self-test.
*
* Verifies the interpreter against a small set of hand-crafted
* SPIR-V modules. Each module is the exact word stream a glslang
* compile would emit for a trivial GLSL shader; the bytes are
* baked here so the self-test doesn't depend on a runtime
* compiler or a filesystem fetch.
*
* Cases:
* 1. `const_color_frag` — a fragment shader that writes
* `vec4(0.5, 0.25, 0.75, 1.0)` to Location 0. Exercises
* OpConstant + OpConstantComposite + OpStore via a
* Location-decorated Output.
* 2. `add_two_floats` — a vertex-shader-shaped module with a
* single function that loads two scalar Input floats by
* Location, adds them via OpFAdd, and stores the sum to a
* Location-decorated Output. Confirms the Sf32 add path
* threads through the executor end-to-end.
* 3. `mul_vec3_scalar` — Output[0..2] = Input * 2.0. Exercises
* OpVectorTimesScalar + vector-form Output read.
*
* Each module's bytes are commented inline so a maintainer can
* verify them against the SPIR-V spec word-by-word without
* re-running glslang.
*/
namespace duetos::subsystems::graphics::spirv
{
namespace
{
using ::duetos::core::Sf32FromBits;
using ::duetos::core::Sf32ToBits;
void Fail(const char* msg, u32 v)
{
arch::SerialWrite("[subsys/graphics/spirv] FAIL ");
arch::SerialWrite(msg);
arch::SerialWrite(" v=");
arch::SerialWriteHex(v);
arch::SerialWrite("\n");
::duetos::core::Panic("subsys/graphics/spirv", "interpreter self-test regression");
}
// ------------------------------------------------------------------
// Module 1: fragment shader emitting a constant vec4 colour.
//
// GLSL equivalent:
// #version 450
// layout(location = 0) out vec4 outColor;
// void main() { outColor = vec4(0.5, 0.25, 0.75, 1.0); }
//
// SPIR-V (assembled by hand from SPIRV-Headers numbering):
// Header: magic, version 1.0 (0x00010000), generator 0,
// bound 17, schema 0.
// 1: OpCapability Shader
// 3: OpMemoryModel Logical GLSL450
// 4: OpEntryPoint Fragment %4 "main" %outColor
// 2: OpExecutionMode %4 OriginUpperLeft
// 3: OpDecorate %outColor Location 0
// 2: OpTypeVoid -> %2
// 3: OpTypeFunction %2 -> %3
// 3: OpTypeFloat 32 -> %6
// 4: OpTypeVector %6 4 -> %7
// 4: OpTypePointer Output %7 -> %8
// 4: OpVariable %8 Output -> %outColor (%9)
// 4: OpConstant %6 0.5 -> %10
// 4: OpConstant %6 0.25 -> %11
// 4: OpConstant %6 0.75 -> %12
// 4: OpConstant %6 1.0 -> %13
// 7: OpConstantComposite %7 %10 %11 %12 %13 -> %14
// 5: OpFunction %2 None %3 -> %4 (main)
// 2: OpLabel -> %15
// 3: OpStore %9 %14
// 1: OpReturn
// 1: OpFunctionEnd
constexpr u32 const_color_frag[] = {
0x07230203,
0x00010000,
0x00000000,
17u,
0u,
// OpCapability Shader (wc=2, op=17): 17|2<<16 = 0x00020011
(2u << 16) | 17u,
1u, // Shader = 1
// OpMemoryModel Logical(0) GLSL450(1) (wc=3, op=14)
(3u << 16) | 14u,
0u,
1u,
// OpEntryPoint Fragment(4) %4 "main" %9 (wc=5: opcode word + 3 fixed + 1 interface)
// String "main" packed: 'm','a','i','n','\0','\0','\0','\0' -> 0x6E69616D 0x00000000
(6u << 16) | 15u,
4u,
4u,
0x6E69616Du,
0x00000000u,
9u,
// OpExecutionMode %4 OriginUpperLeft(7) (wc=3, op=16)
(3u << 16) | 16u,
4u,
7u,
// OpDecorate %9 Location(30) 0 (wc=4, op=71)
(4u << 16) | 71u,
9u,
30u,
0u,
// OpTypeVoid %2 (wc=2, op=19)
(2u << 16) | 19u,
2u,
// OpTypeFunction %3 %2 (wc=3, op=33)
(3u << 16) | 33u,
3u,
2u,
// OpTypeFloat %6 32 (wc=3, op=22)
(3u << 16) | 22u,
6u,
32u,
// OpTypeVector %7 %6 4 (wc=4, op=23)
(4u << 16) | 23u,
7u,
6u,
4u,
// OpTypePointer %8 Output(3) %7 (wc=4, op=32)
(4u << 16) | 32u,
8u,
3u,
7u,
// OpVariable %8 %9 Output(3) (wc=4, op=59)
(4u << 16) | 59u,
8u,
9u,
3u,
// OpConstant %6 %10 0.5 (= 0x3F000000) (wc=4, op=43)
(4u << 16) | 43u,
6u,
10u,
0x3F000000u,
// OpConstant %6 %11 0.25 (= 0x3E800000)
(4u << 16) | 43u,
6u,
11u,
0x3E800000u,
// OpConstant %6 %12 0.75 (= 0x3F400000)
(4u << 16) | 43u,
6u,
12u,
0x3F400000u,
// OpConstant %6 %13 1.0 (= 0x3F800000)
(4u << 16) | 43u,
6u,
13u,
0x3F800000u,
// OpConstantComposite %7 %14 %10 %11 %12 %13 (wc=7, op=44)
(7u << 16) | 44u,
7u,
14u,
10u,
11u,
12u,
13u,
// OpFunction %2 %4 None(0) %3 (wc=5, op=54)
(5u << 16) | 54u,
2u,
4u,
0u,
3u,
// OpLabel %15 (wc=2, op=248)
(2u << 16) | 248u,
15u,
// OpStore %9 %14 (wc=3, op=62)
(3u << 16) | 62u,
9u,
14u,
// OpReturn (wc=1, op=253)
(1u << 16) | 253u,
// OpFunctionEnd (wc=1, op=56)
(1u << 16) | 56u,
};
// Run the const-color test against a Program parsed from the
// bytes above.
void TestConstColor()
{
static Program prog;
const u32 wc = sizeof(const_color_frag) / sizeof(const_color_frag[0]);
if (!Parse(const_color_frag, wc, &prog))
Fail("const_color: Parse rejected", wc);
if (prog.entry_point_count != 1)
Fail("const_color: wrong entry point count", prog.entry_point_count);
if (prog.entry_points[0].execution_model != execution_models::kFragment)
Fail("const_color: wrong execution model", prog.entry_points[0].execution_model);
if (prog.function_count != 1)
Fail("const_color: wrong function count", prog.function_count);
ResetIO(&prog);
if (!ExecuteEntryPoint(&prog, "main"))
Fail("const_color: ExecuteEntryPoint returned false", 0);
u32 out_color[4] = {0, 0, 0, 0};
if (!ReadOutputLocation(&prog, 0, out_color, sizeof(out_color)))
Fail("const_color: ReadOutputLocation 0 missing", 0);
if (out_color[0] != 0x3F000000u)
Fail("const_color: r mismatch", out_color[0]);
if (out_color[1] != 0x3E800000u)
Fail("const_color: g mismatch", out_color[1]);
if (out_color[2] != 0x3F400000u)
Fail("const_color: b mismatch", out_color[2]);
if (out_color[3] != 0x3F800000u)
Fail("const_color: a mismatch", out_color[3]);
}
// ------------------------------------------------------------------
// Module 2: add two scalar float inputs into a scalar output.
//
// GLSL equivalent:
// #version 450
// layout(location = 0) in float a;
// layout(location = 1) in float b;
// layout(location = 0) out float r;
// void main() { r = a + b; }
constexpr u32 add_two_floats[] = {
0x07230203, 0x00010000, 0u, 20u, 0u, (2u << 16) | 17u, 1u, // Capability Shader
(3u << 16) | 14u, 0u, 1u, // MemoryModel Logical GLSL450
// EntryPoint Vertex(0) %4 "main" %a %b %r — interface 3 vars (a=10, b=11, r=12)
(8u << 16) | 15u, 0u, 4u, 0x6E69616Du, 0u, 10u, 11u, 12u, (4u << 16) | 71u, 10u, 30u, 0u, // Decorate %a Location 0
(4u << 16) | 71u, 11u, 30u, 1u, // Decorate %b Location 1
(4u << 16) | 71u, 12u, 30u, 0u, // Decorate %r Location 0
(2u << 16) | 19u, 2u, // TypeVoid
(3u << 16) | 33u, 3u, 2u, // TypeFunction () -> void
(3u << 16) | 22u, 6u, 32u, // TypeFloat 32
(4u << 16) | 32u, 7u, 1u, 6u, // TypePointer Input %6
(4u << 16) | 32u, 8u, 3u, 6u, // TypePointer Output %6
(4u << 16) | 59u, 7u, 10u, 1u, // Variable %a Input
(4u << 16) | 59u, 7u, 11u, 1u, // Variable %b Input
(4u << 16) | 59u, 8u, 12u, 3u, // Variable %r Output
(5u << 16) | 54u, 2u, 4u, 0u, 3u, // Function main
(2u << 16) | 248u, 15u, // Label
(4u << 16) | 61u, 6u, 16u, 10u, // %16 = Load %a
(4u << 16) | 61u, 6u, 17u, 11u, // %17 = Load %b
(5u << 16) | 129u, 6u, 18u, 16u, 17u, // %18 = FAdd %16 %17
(3u << 16) | 62u, 12u, 18u, // Store %r %18
(1u << 16) | 253u, // Return
(1u << 16) | 56u, // FunctionEnd
};
void TestAddTwoFloats()
{
static Program prog;
const u32 wc = sizeof(add_two_floats) / sizeof(add_two_floats[0]);
if (!Parse(add_two_floats, wc, &prog))
Fail("add_floats: Parse rejected", wc);
// Inputs: a=1.5 (0x3FC00000), b=2.25 (0x40100000). Expected r=3.75
// (0x40700000).
ResetIO(&prog);
const u32 a_bits = 0x3FC00000u;
const u32 b_bits = 0x40100000u;
if (!WriteInputLocation(&prog, 0, &a_bits, sizeof(a_bits)))
Fail("add_floats: WriteInputLocation 0 missing", 0);
if (!WriteInputLocation(&prog, 1, &b_bits, sizeof(b_bits)))
Fail("add_floats: WriteInputLocation 1 missing", 0);
if (!ExecuteEntryPoint(&prog, "main"))
Fail("add_floats: ExecuteEntryPoint returned false", 0);
u32 r_bits = 0;
if (!ReadOutputLocation(&prog, 0, &r_bits, sizeof(r_bits)))
Fail("add_floats: ReadOutputLocation 0 missing", 0);
// Tolerate any IEEE 754 form of 3.75 — the Sf32 implementation
// is exact for representable values, so a strict bit compare
// is the right check.
if (r_bits != 0x40700000u)
Fail("add_floats: 1.5+2.25 should be 3.75", r_bits);
}
// ------------------------------------------------------------------
// Module 3: vec3 * scalar.
//
// GLSL equivalent:
// #version 450
// layout(location = 0) in vec3 v;
// layout(location = 0) out vec3 r;
// void main() { r = v * 2.0; }
constexpr u32 mul_vec3_scalar[] = {
0x07230203,
0x00010000,
0u,
25u,
0u,
(2u << 16) | 17u,
1u,
(3u << 16) | 14u,
0u,
1u,
(7u << 16) | 15u,
0u,
4u,
0x6E69616Du,
0u,
10u,
11u, // EntryPoint Vertex main %v %r
(4u << 16) | 71u,
10u,
30u,
0u, // Decorate %v Location 0
(4u << 16) | 71u,
11u,
30u,
0u, // Decorate %r Location 0
(2u << 16) | 19u,
2u, // TypeVoid
(3u << 16) | 33u,
3u,
2u, // TypeFunction () -> void
(3u << 16) | 22u,
6u,
32u, // TypeFloat 32
(4u << 16) | 23u,
7u,
6u,
3u, // TypeVector vec3
(4u << 16) | 32u,
8u,
1u,
7u, // TypePointer Input vec3
(4u << 16) | 32u,
9u,
3u,
7u, // TypePointer Output vec3
(4u << 16) | 59u,
8u,
10u,
1u, // Variable %v Input
(4u << 16) | 59u,
9u,
11u,
3u, // Variable %r Output
(4u << 16) | 43u,
6u,
12u,
0x40000000u, // Constant 2.0
(5u << 16) | 54u,
2u,
4u,
0u,
3u, // Function main
(2u << 16) | 248u,
15u, // Label
(4u << 16) | 61u,
7u,
16u,
10u, // %16 = Load %v
(5u << 16) | 142u,
7u,
17u,
16u,
12u, // %17 = VectorTimesScalar %16 %12
(3u << 16) | 62u,
11u,
17u, // Store %r %17
(1u << 16) | 253u, // Return
(1u << 16) | 56u, // FunctionEnd
};
void TestMulVec3Scalar()
{
static Program prog;
const u32 wc = sizeof(mul_vec3_scalar) / sizeof(mul_vec3_scalar[0]);
if (!Parse(mul_vec3_scalar, wc, &prog))
Fail("mul_vec3: Parse rejected", wc);
// Input v = (1.0, 2.0, 3.0). Expected r = (2.0, 4.0, 6.0).
ResetIO(&prog);
const u32 v_bits[3] = {0x3F800000u, 0x40000000u, 0x40400000u};
if (!WriteInputLocation(&prog, 0, v_bits, sizeof(v_bits)))
Fail("mul_vec3: WriteInputLocation 0 missing", 0);
if (!ExecuteEntryPoint(&prog, "main"))
Fail("mul_vec3: ExecuteEntryPoint returned false", 0);
u32 r_bits[3] = {0, 0, 0};
if (!ReadOutputLocation(&prog, 0, r_bits, sizeof(r_bits)))
Fail("mul_vec3: ReadOutputLocation 0 missing", 0);
if (r_bits[0] != 0x40000000u)
Fail("mul_vec3: r.x should be 2.0", r_bits[0]);
if (r_bits[1] != 0x40800000u)
Fail("mul_vec3: r.y should be 4.0", r_bits[1]);
if (r_bits[2] != 0x40C00000u)
Fail("mul_vec3: r.z should be 6.0", r_bits[2]);
}
// ------------------------------------------------------------------
// Module 4: compute shader with declared LocalSize.
//
// GLSL equivalent:
// #version 450
// layout(local_size_x = 4, local_size_y = 2, local_size_z = 1) in;
// void main() { }
//
// Validates that the parser recognises ExecutionModel=Compute (5),
// captures OpExecutionMode LocalSize 4 2 1 into the EntryPointRecord,
// and the executor can step through a trivial empty body. Tests for
// the dispatch-side workgroup loop are deferred to runtime (would
// need a full pipeline scaffold).
constexpr u32 compute_local_size[] = {
0x07230203,
0x00010000,
0u,
10u,
0u,
(2u << 16) | 17u,
1u, // Capability Shader
(3u << 16) | 14u,
0u,
1u, // MemoryModel Logical GLSL450
(5u << 16) | 15u,
5u,
4u,
0x6E69616Du,
0u, // EntryPoint GLCompute(5) %4 "main" (no interface vars)
(6u << 16) | 16u,
4u,
17u,
4u,
2u,
1u, // ExecutionMode %4 LocalSize 4 2 1
(2u << 16) | 19u,
2u, // TypeVoid
(3u << 16) | 33u,
3u,
2u, // TypeFunction () -> void
(5u << 16) | 54u,
2u,
4u,
0u,
3u, // Function main
(2u << 16) | 248u,
5u, // Label
(1u << 16) | 253u, // Return
(1u << 16) | 56u, // FunctionEnd
};
void TestComputeLocalSize()
{
static Program prog;
const u32 wc = sizeof(compute_local_size) / sizeof(compute_local_size[0]);
if (!Parse(compute_local_size, wc, &prog))
Fail("compute: Parse rejected", wc);
if (prog.entry_point_count != 1)
Fail("compute: wrong entry point count", prog.entry_point_count);
if (prog.entry_points[0].execution_model != execution_models::kGLCompute)
Fail("compute: wrong execution model", prog.entry_points[0].execution_model);
if (prog.entry_points[0].local_size_x != 4)
Fail("compute: local_size_x mismatch", prog.entry_points[0].local_size_x);
if (prog.entry_points[0].local_size_y != 2)
Fail("compute: local_size_y mismatch", prog.entry_points[0].local_size_y);
if (prog.entry_points[0].local_size_z != 1)
Fail("compute: local_size_z mismatch", prog.entry_points[0].local_size_z);
ResetIO(&prog);
if (!ExecuteEntryPoint(&prog, "main"))
Fail("compute: ExecuteEntryPoint returned false", 0);
}
void TestDescriptorBinding()
{
// Exercises the per-Program descriptor binding API without
// dragging in a full SPIR-V module — just creates a bare
// Program with parse_ok=true to satisfy the lookups, sets a
// binding, reads it back, validates the (set, binding) bounds
// checks behave.
static Program prog;
// Zero-initialise via the public Parse path against a minimal
// valid module (reuse the const-color frag — already verified
// to parse cleanly above).
const u32 wc = sizeof(const_color_frag) / sizeof(const_color_frag[0]);
if (!Parse(const_color_frag, wc, &prog))
Fail("descriptors: Parse failed", 0);
// Initial state: every binding zero.
if (LookupDescriptor(&prog, 0, 0) != 0)
Fail("descriptors: initial slot non-zero", static_cast<u32>(LookupDescriptor(&prog, 0, 0)));
// Set and read back.
BindDescriptor(&prog, 0, 0, 0xDEADBEEFCAFEBABEull);
if (LookupDescriptor(&prog, 0, 0) != 0xDEADBEEFCAFEBABEull)
Fail("descriptors: round-trip mismatch", 0);
// Out-of-bounds set/binding -> 0.
BindDescriptor(&prog, 99, 0, 0xAAAA); // bounds-check no-op
if (LookupDescriptor(&prog, 99, 0) != 0)
Fail("descriptors: OOB set should return 0", 0);
BindDescriptor(&prog, 0, 99, 0xBBBB);
if (LookupDescriptor(&prog, 0, 99) != 0)
Fail("descriptors: OOB binding should return 0", 0);
// Different set untouched by binding to set 0.
if (LookupDescriptor(&prog, 1, 0) != 0)
Fail("descriptors: cross-set leak", 0);
// Sampler binding rides on a parallel array — same round-trip
// shape, independent of the image-handle bindings above. The
// executor's OpImageSample path reads this to pick the
// configured sampler addressing mode.
if (LookupSampler(&prog, 0, 0) != 0)
Fail("samplers: initial slot non-zero", 0);
BindSampler(&prog, 0, 0, 0x12345678ABCDEFull);
if (LookupSampler(&prog, 0, 0) != 0x12345678ABCDEFull)
Fail("samplers: round-trip mismatch", 0);
if (LookupDescriptor(&prog, 0, 0) != 0xDEADBEEFCAFEBABEull)
Fail("samplers: image-handle clobbered by sampler write", 0);
BindSampler(&prog, 99, 0, 0x9999);
if (LookupSampler(&prog, 99, 0) != 0)
Fail("samplers: OOB set should return 0", 0);
}
// ------------------------------------------------------------------
// Module 6: OpFunctionCall with a real two-parameter helper.
//
// GLSL equivalent:
// #version 450
// layout(location = 0) in float a;
// layout(location = 1) in float b;
// layout(location = 0) out float r;
// float add(float x, float y) { return x + y; }
// void main() { r = add(a, b); }
//
// Exercises the real call path: argument->parameter binding
// (positional, two params), callee basic-block execution, and the
// OpReturnValue -> call-result copy. Inputs reuse module 2's
// known-good constants so the expected sum is identical (3.75).
constexpr u32 call_add_floats[] = {
0x07230203, 0x00010000, 0u, 26u, 0u, // header: bound 26
(2u << 16) | 17u, 1u, // Capability Shader
(3u << 16) | 14u, 0u, 1u, // MemoryModel Logical GLSL450
(8u << 16) | 15u, 0u, 4u, 0x6E69616Du, 0u, 10u, 11u, 12u, // EntryPoint Vertex %4 "main" %a %b %r
(4u << 16) | 71u, 10u, 30u, 0u, // Decorate %a Location 0
(4u << 16) | 71u, 11u, 30u, 1u, // Decorate %b Location 1
(4u << 16) | 71u, 12u, 30u, 0u, // Decorate %r Location 0
(2u << 16) | 19u, 2u, // TypeVoid %2
(3u << 16) | 33u, 3u, 2u, // TypeFunction %3 = void()
(3u << 16) | 22u, 6u, 32u, // TypeFloat 32 %6
(5u << 16) | 33u, 21u, 6u, 6u, 6u, // TypeFunction %21 = float(float,float)
(4u << 16) | 32u, 7u, 1u, 6u, // TypePointer Input %6 -> %7
(4u << 16) | 32u, 8u, 3u, 6u, // TypePointer Output %6 -> %8
(4u << 16) | 59u, 7u, 10u, 1u, // Variable %a Input
(4u << 16) | 59u, 7u, 11u, 1u, // Variable %b Input
(4u << 16) | 59u, 8u, 12u, 3u, // Variable %r Output
// main: r = add(a, b)
(5u << 16) | 54u, 2u, 4u, 0u, 3u, // Function %4 main
(2u << 16) | 248u, 15u, // Label %15
(4u << 16) | 61u, 6u, 16u, 10u, // %16 = Load %a
(4u << 16) | 61u, 6u, 17u, 11u, // %17 = Load %b
(6u << 16) | 57u, 6u, 18u, 20u, 16u, 17u, // %18 = FunctionCall %add %16 %17
(3u << 16) | 62u, 12u, 18u, // Store %r %18
(1u << 16) | 253u, // Return
(1u << 16) | 56u, // FunctionEnd
// float add(float x, float y) { return x + y; }
(5u << 16) | 54u, 6u, 20u, 0u, 21u, // Function %20 add -> float, type %21
(3u << 16) | 55u, 6u, 22u, // FunctionParameter %6 %x
(3u << 16) | 55u, 6u, 23u, // FunctionParameter %6 %y
(2u << 16) | 248u, 24u, // Label %24
(5u << 16) | 129u, 6u, 25u, 22u, 23u, // %25 = FAdd %x %y
(2u << 16) | 254u, 25u, // ReturnValue %25
(1u << 16) | 56u, // FunctionEnd
};
void TestCallAddFloats()
{
static Program prog;
const u32 wc = sizeof(call_add_floats) / sizeof(call_add_floats[0]);
if (!Parse(call_add_floats, wc, &prog))
Fail("call_add: Parse rejected", wc);
if (prog.function_count != 2)
Fail("call_add: expected 2 functions", prog.function_count);
ResetIO(&prog);
const u32 a_bits = 0x3FC00000u; // 1.5
const u32 b_bits = 0x40100000u; // 2.25
if (!WriteInputLocation(&prog, 0, &a_bits, sizeof(a_bits)))
Fail("call_add: WriteInputLocation 0 missing", 0);
if (!WriteInputLocation(&prog, 1, &b_bits, sizeof(b_bits)))
Fail("call_add: WriteInputLocation 1 missing", 0);
if (!ExecuteEntryPoint(&prog, "main"))
Fail("call_add: ExecuteEntryPoint returned false", 0);
u32 r_bits = 0;
if (!ReadOutputLocation(&prog, 0, &r_bits, sizeof(r_bits)))
Fail("call_add: ReadOutputLocation 0 missing", 0);
// add(1.5, 2.25) must be 3.75 — proves the call bound both params
// and copied the callee's OpReturnValue back. A regressed call
// path (the old no-op-returns-0 stub) writes 0 here.
if (r_bits != 0x40700000u)
Fail("call_add: add(1.5,2.25) should be 3.75", r_bits);
}
} // namespace
void SelfTest()
{
TestConstColor();
TestAddTwoFloats();
TestMulVec3Scalar();
TestComputeLocalSize();
TestDescriptorBinding();
TestCallAddFloats();
arch::SerialWrite("[subsys/graphics/spirv] self-test PASS (6 modules executed)\n");
}
} // namespace duetos::subsystems::graphics::spirv