@@ -12,54 +12,24 @@ impl<'a> std::fmt::Debug for PrettyString<'a> {
12
12
}
13
13
}
14
14
15
- fn assemble_spirv ( spirv : & str ) -> Vec < u8 > {
16
- use std:: process:: Command ;
17
- use tempfile:: tempdir;
15
+ fn assemble_spirv ( spirv : & str ) -> Result < Vec < u8 > > {
16
+ use spirv_tools:: assembler;
18
17
19
- let temp = tempdir ( ) . expect ( "Unable to create temp dir" ) ;
20
- let input = temp. path ( ) . join ( "code.txt" ) ;
21
- let output = temp. path ( ) . join ( "code.spv" ) ;
18
+ let assembler = assembler:: Assembler :: new ( spirv_tools:: default_target_env ( ) ) ;
22
19
23
- std :: fs :: write ( & input , spirv ) . unwrap ( ) ;
20
+ let spv_binary = assembler . assemble ( spirv , assembler :: AssemblerOptions :: default ( ) ) ? ;
24
21
25
- let process = Command :: new ( "spirv-as" )
26
- . arg ( input. to_str ( ) . unwrap ( ) )
27
- . arg ( "-o" )
28
- . arg ( output. to_str ( ) . unwrap ( ) )
29
- . output ( )
30
- . expect ( "failed to execute process" ) ;
31
-
32
- println ! ( "status: {}" , process. status) ;
33
- println ! ( "stdout: {}" , String :: from_utf8_lossy( & process. stdout) ) ;
34
- println ! ( "stderr: {}" , String :: from_utf8_lossy( & process. stderr) ) ;
35
-
36
- assert ! ( process. status. success( ) ) ;
37
-
38
- std:: fs:: read ( & output) . unwrap ( )
22
+ let contents: & [ u8 ] = spv_binary. as_ref ( ) ;
23
+ Ok ( contents. to_vec ( ) )
39
24
}
40
25
41
26
#[ allow( unused) ]
42
27
fn validate ( spirv : & [ u32 ] ) {
43
- use std:: process:: Command ;
44
- use tempfile:: tempdir;
45
-
46
- let temp = tempdir ( ) . expect ( "Unable to create temp dir" ) ;
47
- let input = temp. path ( ) . join ( "code.spv" ) ;
48
-
49
- let spirv = unsafe { std:: slice:: from_raw_parts ( spirv. as_ptr ( ) as * const u8 , spirv. len ( ) * 4 ) } ;
50
-
51
- std:: fs:: write ( & input, spirv) . unwrap ( ) ;
52
-
53
- let process = Command :: new ( "spirv-val.exe" )
54
- . arg ( input. to_str ( ) . unwrap ( ) )
55
- . output ( )
56
- . expect ( "failed to execute process" ) ;
28
+ let validator = spirv_tools:: val:: Validator :: new ( spirv_tools:: default_target_env ( ) ) ;
57
29
58
- println ! ( "status: {}" , process. status) ;
59
- println ! ( "stdout: {}" , String :: from_utf8_lossy( & process. stdout) ) ;
60
- println ! ( "stderr: {}" , String :: from_utf8_lossy( & process. stderr) ) ;
61
-
62
- assert ! ( process. status. success( ) ) ;
30
+ validator
31
+ . validate ( spirv, & spirv_tools:: val:: ValidatorOptions :: default ( ) )
32
+ . expect ( "validation error occurred" ) ;
63
33
}
64
34
65
35
fn load ( bytes : & [ u8 ] ) -> Module {
@@ -126,7 +96,7 @@ fn standard() -> Result<()> {
126
96
%2 = OpTypeFloat 32
127
97
%1 = OpVariable %2 Uniform
128
98
%3 = OpVariable %2 Input"# ,
129
- ) ;
99
+ ) ? ;
130
100
131
101
let b = assemble_spirv (
132
102
r#"OpCapability Linkage
@@ -135,7 +105,7 @@ fn standard() -> Result<()> {
135
105
%3 = OpConstant %2 42
136
106
%1 = OpVariable %2 Uniform %3
137
107
"# ,
138
- ) ;
108
+ ) ? ;
139
109
140
110
let result = assemble_and_link ( & [ & a, & b] ) ?;
141
111
let expect = r#"OpModuleProcessed "Linked by rspirv-linker"
@@ -155,7 +125,7 @@ fn not_a_lib_extra_exports() -> Result<()> {
155
125
OpDecorate %1 LinkageAttributes "foo" Export
156
126
%2 = OpTypeFloat 32
157
127
%1 = OpVariable %2 Uniform"# ,
158
- ) ;
128
+ ) ? ;
159
129
160
130
let result = assemble_and_link ( & [ & a] ) ?;
161
131
let expect = r#"OpModuleProcessed "Linked by rspirv-linker"
@@ -194,9 +164,9 @@ fn unresolved_symbol() -> Result<()> {
194
164
OpDecorate %1 LinkageAttributes "foo" Import
195
165
%2 = OpTypeFloat 32
196
166
%1 = OpVariable %2 Uniform"# ,
197
- ) ;
167
+ ) ? ;
198
168
199
- let b = assemble_spirv ( "OpCapability Linkage" ) ;
169
+ let b = assemble_spirv ( "OpCapability Linkage" ) ? ;
200
170
201
171
let result = assemble_and_link ( & [ & a, & b] ) ;
202
172
@@ -216,7 +186,7 @@ fn type_mismatch() -> Result<()> {
216
186
%2 = OpTypeFloat 32
217
187
%1 = OpVariable %2 Uniform
218
188
%3 = OpVariable %2 Input"# ,
219
- ) ;
189
+ ) ? ;
220
190
221
191
let b = assemble_spirv (
222
192
r#"OpCapability Linkage
@@ -225,7 +195,7 @@ fn type_mismatch() -> Result<()> {
225
195
%3 = OpConstant %2 42
226
196
%1 = OpVariable %2 Uniform %3
227
197
"# ,
228
- ) ;
198
+ ) ? ;
229
199
230
200
let result = assemble_and_link ( & [ & a, & b] ) ;
231
201
assert_eq ! (
@@ -247,7 +217,7 @@ fn multiple_definitions() -> Result<()> {
247
217
%2 = OpTypeFloat 32
248
218
%1 = OpVariable %2 Uniform
249
219
%3 = OpVariable %2 Input"# ,
250
- ) ;
220
+ ) ? ;
251
221
252
222
let b = assemble_spirv (
253
223
r#"OpCapability Linkage
@@ -256,15 +226,15 @@ fn multiple_definitions() -> Result<()> {
256
226
%2 = OpTypeFloat 32
257
227
%3 = OpConstant %2 42
258
228
%1 = OpVariable %2 Uniform %3"# ,
259
- ) ;
229
+ ) ? ;
260
230
261
231
let c = assemble_spirv (
262
232
r#"OpCapability Linkage
263
233
OpDecorate %1 LinkageAttributes "foo" Export
264
234
%2 = OpTypeFloat 32
265
235
%3 = OpConstant %2 -1
266
236
%1 = OpVariable %2 Uniform %3"# ,
267
- ) ;
237
+ ) ? ;
268
238
269
239
let result = assemble_and_link ( & [ & a, & b, & c] ) ;
270
240
assert_eq ! (
@@ -282,7 +252,7 @@ fn multiple_definitions_different_types() -> Result<()> {
282
252
%2 = OpTypeFloat 32
283
253
%1 = OpVariable %2 Uniform
284
254
%3 = OpVariable %2 Input"# ,
285
- ) ;
255
+ ) ? ;
286
256
287
257
let b = assemble_spirv (
288
258
r#"OpCapability Linkage
@@ -291,15 +261,15 @@ fn multiple_definitions_different_types() -> Result<()> {
291
261
%2 = OpTypeInt 32 0
292
262
%3 = OpConstant %2 42
293
263
%1 = OpVariable %2 Uniform %3"# ,
294
- ) ;
264
+ ) ? ;
295
265
296
266
let c = assemble_spirv (
297
267
r#"OpCapability Linkage
298
268
OpDecorate %1 LinkageAttributes "foo" Export
299
269
%2 = OpTypeFloat 32
300
270
%3 = OpConstant %2 12
301
271
%1 = OpVariable %2 Uniform %3"# ,
302
- ) ;
272
+ ) ? ;
303
273
304
274
let result = assemble_and_link ( & [ & a, & b, & c] ) ;
305
275
assert_eq ! (
@@ -348,7 +318,7 @@ fn func_ctrl() -> Result<()> {
348
318
%5 = OpVariable %4 Uniform
349
319
%1 = OpFunction %2 None %3
350
320
OpFunctionEnd"# ,
351
- ) ;
321
+ ) ? ;
352
322
353
323
let b = assemble_spirv (
354
324
r#"OpCapability Linkage
@@ -359,7 +329,7 @@ fn func_ctrl() -> Result<()> {
359
329
%4 = OpLabel
360
330
OpReturn
361
331
OpFunctionEnd"# ,
362
- ) ;
332
+ ) ? ;
363
333
364
334
let result = assemble_and_link ( & [ & a, & b] ) ?;
365
335
@@ -396,7 +366,7 @@ fn use_exported_func_param_attr() -> Result<()> {
396
366
%4 = OpFunctionParameter %6
397
367
OpFunctionEnd
398
368
"# ,
399
- ) ;
369
+ ) ? ;
400
370
401
371
let b = assemble_spirv (
402
372
r#"OpCapability Kernel
@@ -412,7 +382,7 @@ fn use_exported_func_param_attr() -> Result<()> {
412
382
OpReturn
413
383
OpFunctionEnd
414
384
"# ,
415
- ) ;
385
+ ) ? ;
416
386
417
387
let result = assemble_and_link ( & [ & a, & b] ) ?;
418
388
@@ -461,7 +431,7 @@ fn names_and_decorations() -> Result<()> {
461
431
%4 = OpFunctionParameter %9
462
432
OpFunctionEnd
463
433
"# ,
464
- ) ;
434
+ ) ? ;
465
435
466
436
let b = assemble_spirv (
467
437
r#"OpCapability Kernel
@@ -480,7 +450,7 @@ fn names_and_decorations() -> Result<()> {
480
450
OpReturn
481
451
OpFunctionEnd
482
452
"# ,
483
- ) ;
453
+ ) ? ;
484
454
485
455
let result = assemble_and_link ( & [ & a, & b] ) ?;
486
456
0 commit comments