Debian Codegraph @

third_party/rust/naga/src/front/glsl/parser_tests.rs

raw
1 use alloc::{borrow::ToOwned, vec};
2
3 use pp_rs::token::PreprocessorError;
4
5 use super::{
6 ast::Profile,
7 error::ExpectedToken,
8 error::{Error, ErrorKind, ParseErrors},
9 token::TokenValue,
10 Frontend, Options, Span,
11 };
12 use crate::ShaderStage;
13
14 #[cfg(test)]
15 use std::println;
16
17 #[test]
18 fn version() {
19 let mut frontend = Frontend::default();
20
21 // invalid versions
22 assert_eq!(
23 frontend
24 .parse(
25 &Options::from(ShaderStage::Vertex),
26 "#version 99000\n void main(){}",
27 )
28 .err()
29 .unwrap(),
30 ParseErrors {
31 errors: vec![Error {
32 kind: ErrorKind::InvalidVersion(99000),
33 meta: Span::new(9, 14)
34 }],
35 },
36 );
37
38 assert_eq!(
39 frontend
40 .parse(
41 &Options::from(ShaderStage::Vertex),
42 "#version 449\n void main(){}",
43 )
44 .err()
45 .unwrap(),
46 ParseErrors {
47 errors: vec![Error {
48 kind: ErrorKind::InvalidVersion(449),
49 meta: Span::new(9, 12)
50 }]
51 },
52 );
53
54 assert_eq!(
55 frontend
56 .parse(
57 &Options::from(ShaderStage::Vertex),
58 "#version 450 smart\n void main(){}",
59 )
60 .err()
61 .unwrap(),
62 ParseErrors {
63 errors: vec![Error {
64 kind: ErrorKind::InvalidProfile("smart".into()),
65 meta: Span::new(13, 18),
66 }]
67 },
68 );
69
70 assert_eq!(
71 frontend
72 .parse(
73 &Options::from(ShaderStage::Vertex),
74 "#version 450\nvoid main(){} #version 450",
75 )
76 .err()
77 .unwrap(),
78 ParseErrors {
79 errors: vec![
80 Error {
81 kind: ErrorKind::PreprocessorError(PreprocessorError::UnexpectedHash,),
82 meta: Span::new(27, 28),
83 },
84 Error {
85 kind: ErrorKind::InvalidToken(
86 TokenValue::Identifier("version".into()),
87 vec![ExpectedToken::Eof]
88 ),
89 meta: Span::new(28, 35)
90 }
91 ]
92 },
93 );
94
95 // valid versions
96 frontend
97 .parse(
98 &Options::from(ShaderStage::Vertex),
99 " # version 450\nvoid main() {}",
100 )
101 .unwrap();
102 assert_eq!(
103 (frontend.metadata().version, frontend.metadata().profile),
104 (450, Profile::Core)
105 );
106
107 frontend
108 .parse(
109 &Options::from(ShaderStage::Vertex),
110 "#version 450\nvoid main() {}",
111 )
112 .unwrap();
113 assert_eq!(
114 (frontend.metadata().version, frontend.metadata().profile),
115 (450, Profile::Core)
116 );
117
118 frontend
119 .parse(
120 &Options::from(ShaderStage::Vertex),
121 "#version 450 core\nvoid main(void) {}",
122 )
123 .unwrap();
124 assert_eq!(
125 (frontend.metadata().version, frontend.metadata().profile),
126 (450, Profile::Core)
127 );
128 }
129
130 #[test]
131 fn control_flow() {
132 let mut frontend = Frontend::default();
133
134 frontend
135 .parse(
136 &Options::from(ShaderStage::Vertex),
137 r#"
138 # version 450
139 void main() {
140 if (true) {
141 return 1;
142 } else {
143 return 2;
144 }
145 }
146 "#,
147 )
148 .unwrap();
149
150 frontend
151 .parse(
152 &Options::from(ShaderStage::Vertex),
153 r#"
154 # version 450
155 void main() {
156 if (true) {
157 return 1;
158 }
159 }
160 "#,
161 )
162 .unwrap();
163
164 frontend
165 .parse(
166 &Options::from(ShaderStage::Vertex),
167 r#"
168 # version 450
169 void main() {
170 int x;
171 int y = 3;
172 switch (5) {
173 case 2:
174 x = 2;
175 case 5:
176 x = 5;
177 y = 2;
178 break;
179 default:
180 x = 0;
181 }
182 }
183 "#,
184 )
185 .unwrap();
186
187 frontend
188 .parse(
189 &Options::from(ShaderStage::Vertex),
190 r#"
191 # version 450
192 void main() {
193 int x = 0;
194 while(x < 5) {
195 x = x + 1;
196 }
197 do {
198 x = x - 1;
199 } while(x >= 4)
200 }
201 "#,
202 )
203 .unwrap();
204
205 frontend
206 .parse(
207 &Options::from(ShaderStage::Vertex),
208 r#"
209 # version 450
210 void main() {
211 int x = 0;
212 for(int i = 0; i < 10;) {
213 x = x + 2;
214 }
215 for(;;);
216 return x;
217 }
218 "#,
219 )
220 .unwrap();
221 }
222
223 #[test]
224 fn declarations() {
225 let mut frontend = Frontend::default();
226
227 frontend
228 .parse(
229 &Options::from(ShaderStage::Vertex),
230 r#"
231 #version 450
232 layout(location = 0) in vec2 v_uv;
233 layout(location = 0) out vec4 o_color;
234 layout(set = 1, binding = 1) uniform texture2D tex;
235 layout(set = 1, binding = 2) uniform sampler tex_sampler;
236
237 layout(early_fragment_tests) in;
238
239 void main() {}
240 "#,
241 )
242 .unwrap();
243
244 frontend
245 .parse(
246 &Options::from(ShaderStage::Vertex),
247 r#"
248 #version 450
249 layout(std140, set = 2, binding = 0)
250 uniform u_locals {
251 vec3 model_offs;
252 float load_time;
253 ivec4 atlas_offs;
254 };
255
256 void main() {}
257 "#,
258 )
259 .unwrap();
260
261 frontend
262 .parse(
263 &Options::from(ShaderStage::Vertex),
264 r#"
265 #version 450
266 layout(push_constant)
267 uniform u_locals {
268 vec3 model_offs;
269 float load_time;
270 ivec4 atlas_offs;
271 };
272
273 void main() {}
274 "#,
275 )
276 .unwrap();
277
278 frontend
279 .parse(
280 &Options::from(ShaderStage::Vertex),
281 r#"
282 #version 450
283 layout(std430, set = 2, binding = 0)
284 uniform u_locals {
285 vec3 model_offs;
286 float load_time;
287 ivec4 atlas_offs;
288 };
289
290 void main() {}
291 "#,
292 )
293 .unwrap();
294
295 frontend
296 .parse(
297 &Options::from(ShaderStage::Vertex),
298 r#"
299 #version 450
300 layout(std140, set = 2, binding = 0)
301 uniform u_locals {
302 vec3 model_offs;
303 float load_time;
304 } block_var;
305
306 void main() {
307 load_time * model_offs;
308 block_var.load_time * block_var.model_offs;
309 }
310 "#,
311 )
312 .unwrap();
313
314 frontend
315 .parse(
316 &Options::from(ShaderStage::Vertex),
317 r#"
318 #version 450
319 float vector = vec4(1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0);
320
321 void main() {}
322 "#,
323 )
324 .unwrap();
325
326 frontend
327 .parse(
328 &Options::from(ShaderStage::Vertex),
329 r#"
330 #version 450
331 precision highp float;
332
333 void main() {}
334 "#,
335 )
336 .unwrap();
337 }
338
339 #[test]
340 fn textures() {
341 let mut frontend = Frontend::default();
342
343 frontend
344 .parse(
345 &Options::from(ShaderStage::Vertex),
346 r#"
347 #version 450
348 layout(location = 0) in vec2 v_uv;
349 layout(location = 0) out vec4 o_color;
350 layout(set = 1, binding = 1) uniform texture2D tex;
351 layout(set = 1, binding = 2) uniform sampler tex_sampler;
352 void main() {
353 o_color = texture(sampler2D(tex, tex_sampler), v_uv);
354 o_color.a = texture(sampler2D(tex, tex_sampler), v_uv, 2.0).a;
355 }
356 "#,
357 )
358 .unwrap();
359 }
360
361 #[test]
362 fn functions() {
363 let mut frontend = Frontend::default();
364
365 frontend
366 .parse(
367 &Options::from(ShaderStage::Vertex),
368 r#"
369 # version 450
370 void test1(float);
371 void test1(float) {}
372
373 void main() {}
374 "#,
375 )
376 .unwrap();
377
378 frontend
379 .parse(
380 &Options::from(ShaderStage::Vertex),
381 r#"
382 # version 450
383 void test2(float a) {}
384 void test3(float a, float b) {}
385 void test4(float, float) {}
386
387 void main() {}
388 "#,
389 )
390 .unwrap();
391
392 frontend
393 .parse(
394 &Options::from(ShaderStage::Vertex),
395 r#"
396 # version 450
397 float test(float a) { return a; }
398
399 void main() {}
400 "#,
401 )
402 .unwrap();
403
404 frontend
405 .parse(
406 &Options::from(ShaderStage::Vertex),
407 r#"
408 # version 450
409 float test(vec4 p) {
410 return p.x;
411 }
412
413 void main() {}
414 "#,
415 )
416 .unwrap();
417
418 // Function overloading
419 frontend
420 .parse(
421 &Options::from(ShaderStage::Vertex),
422 r#"
423 # version 450
424 float test(vec2 p);
425 float test(vec3 p);
426 float test(vec4 p);
427
428 float test(vec2 p) {
429 return p.x;
430 }
431
432 float test(vec3 p) {
433 return p.x;
434 }
435
436 float test(vec4 p) {
437 return p.x;
438 }
439
440 void main() {}
441 "#,
442 )
443 .unwrap();
444
445 assert_eq!(
446 frontend
447 .parse(
448 &Options::from(ShaderStage::Vertex),
449 r#"
450 # version 450
451 int test(vec4 p) {
452 return p.x;
453 }
454
455 float test(vec4 p) {
456 return p.x;
457 }
458
459 void main() {}
460 "#,
461 )
462 .err()
463 .unwrap(),
464 ParseErrors {
465 errors: vec![Error {
466 kind: ErrorKind::SemanticError("Function already defined".into()),
467 meta: Span::new(134, 152),
468 }]
469 },
470 );
471
472 println!();
473
474 frontend
475 .parse(
476 &Options::from(ShaderStage::Vertex),
477 r#"
478 # version 450
479 float callee(uint q) {
480 return float(q);
481 }
482
483 float caller() {
484 callee(1u);
485 }
486
487 void main() {}
488 "#,
489 )
490 .unwrap();
491
492 // Nested function call
493 frontend
494 .parse(
495 &Options::from(ShaderStage::Vertex),
496 r#"
497 # version 450
498 layout(set = 0, binding = 1) uniform texture2D t_noise;
499 layout(set = 0, binding = 2) uniform sampler s_noise;
500
501 void main() {
502 textureLod(sampler2D(t_noise, s_noise), vec2(1.0), 0);
503 }
504 "#,
505 )
506 .unwrap();
507
508 frontend
509 .parse(
510 &Options::from(ShaderStage::Vertex),
511 r#"
512 # version 450
513 void fun(vec2 in_parameter, out float out_parameter) {
514 ivec2 _ = ivec2(in_parameter);
515 }
516
517 void main() {
518 float a;
519 fun(vec2(1.0), a);
520 }
521 "#,
522 )
523 .unwrap();
524 }
525
526 #[test]
527 fn constants() {
528 use crate::{Constant, Expression, Type, TypeInner};
529
530 let mut frontend = Frontend::default();
531
532 let module = frontend
533 .parse(
534 &Options::from(ShaderStage::Vertex),
535 r#"
536 # version 450
537 const float a = 1.0;
538 float global = a;
539 const float b = a;
540
541 void main() {}
542 "#,
543 )
544 .unwrap();
545
546 let mut types = module.types.iter();
547 let mut constants = module.constants.iter();
548 let mut global_expressions = module.global_expressions.iter();
549
550 let (ty_handle, ty) = types.next().unwrap();
551 assert_eq!(
552 ty,
553 &Type {
554 name: None,
555 inner: TypeInner::Scalar(crate::Scalar::F32)
556 }
557 );
558
559 let (init_handle, init) = global_expressions.next().unwrap();
560 assert_eq!(init, &Expression::Literal(crate::Literal::F32(1.0)));
561
562 assert_eq!(
563 constants.next().unwrap().1,
564 &Constant {
565 name: Some("a".to_owned()),
566 ty: ty_handle,
567 init: init_handle
568 }
569 );
570
571 assert_eq!(
572 constants.next().unwrap().1,
573 &Constant {
574 name: Some("b".to_owned()),
575 ty: ty_handle,
576 init: init_handle
577 }
578 );
579
580 assert!(constants.next().is_none());
581 }
582
583 #[test]
584 fn function_overloading() {
585 let mut frontend = Frontend::default();
586
587 frontend
588 .parse(
589 &Options::from(ShaderStage::Vertex),
590 r#"
591 # version 450
592
593 float saturate(float v) { return clamp(v, 0.0, 1.0); }
594 vec2 saturate(vec2 v) { return clamp(v, vec2(0.0), vec2(1.0)); }
595 vec3 saturate(vec3 v) { return clamp(v, vec3(0.0), vec3(1.0)); }
596 vec4 saturate(vec4 v) { return clamp(v, vec4(0.0), vec4(1.0)); }
597
598 void main() {
599 float v1 = saturate(1.5);
600 vec2 v2 = saturate(vec2(0.5, 1.5));
601 vec3 v3 = saturate(vec3(0.5, 1.5, 2.5));
602 vec3 v4 = saturate(vec4(0.5, 1.5, 2.5, 3.5));
603 }
604 "#,
605 )
606 .unwrap();
607 }
608
609 #[test]
610 fn implicit_conversions() {
611 let mut frontend = Frontend::default();
612
613 frontend
614 .parse(
615 &Options::from(ShaderStage::Vertex),
616 r#"
617 # version 450
618 void main() {
619 mat4 a = mat4(1);
620 float b = 1u;
621 float c = 1 + 2.0;
622 }
623 "#,
624 )
625 .unwrap();
626
627 assert_eq!(
628 frontend
629 .parse(
630 &Options::from(ShaderStage::Vertex),
631 r#"
632 # version 450
633 void test(int a) {}
634 void test(uint a) {}
635
636 void main() {
637 test(1.0);
638 }
639 "#,
640 )
641 .err()
642 .unwrap(),
643 ParseErrors {
644 errors: vec![Error {
645 kind: ErrorKind::SemanticError("Unknown function \'test\'".into()),
646 meta: Span::new(156, 165),
647 }]
648 },
649 );
650
651 assert_eq!(
652 frontend
653 .parse(
654 &Options::from(ShaderStage::Vertex),
655 r#"
656 # version 450
657 void test(float a) {}
658 void test(uint a) {}
659
660 void main() {
661 test(1);
662 }
663 "#,
664 )
665 .err()
666 .unwrap(),
667 ParseErrors {
668 errors: vec![Error {
669 kind: ErrorKind::SemanticError("Ambiguous best function for \'test\'".into()),
670 meta: Span::new(158, 165),
671 }]
672 }
673 );
674 }
675
676 #[test]
677 fn structs() {
678 let mut frontend = Frontend::default();
679
680 frontend
681 .parse(
682 &Options::from(ShaderStage::Vertex),
683 r#"
684 # version 450
685 Test {
686 vec4 pos;
687 } xx;
688
689 void main() {}
690 "#,
691 )
692 .unwrap_err();
693
694 frontend
695 .parse(
696 &Options::from(ShaderStage::Vertex),
697 r#"
698 # version 450
699 struct Test {
700 vec4 pos;
701 };
702
703 void main() {}
704 "#,
705 )
706 .unwrap();
707
708 frontend
709 .parse(
710 &Options::from(ShaderStage::Vertex),
711 r#"
712 # version 450
713 const int NUM_VECS = 42;
714 struct Test {
715 vec4 vecs[NUM_VECS];
716 };
717
718 void main() {}
719 "#,
720 )
721 .unwrap();
722
723 frontend
724 .parse(
725 &Options::from(ShaderStage::Vertex),
726 r#"
727 # version 450
728 struct Hello {
729 vec4 test;
730 } test() {
731 return Hello( vec4(1.0) );
732 }
733
734 void main() {}
735 "#,
736 )
737 .unwrap();
738
739 frontend
740 .parse(
741 &Options::from(ShaderStage::Vertex),
742 r#"
743 # version 450
744 struct Test {};
745
746 void main() {}
747 "#,
748 )
749 .unwrap_err();
750
751 frontend
752 .parse(
753 &Options::from(ShaderStage::Vertex),
754 r#"
755 # version 450
756 inout struct Test {
757 vec4 x;
758 };
759
760 void main() {}
761 "#,
762 )
763 .unwrap_err();
764 }
765
766 #[test]
767 fn swizzles() {
768 let mut frontend = Frontend::default();
769
770 frontend
771 .parse(
772 &Options::from(ShaderStage::Vertex),
773 r#"
774 # version 450
775 void main() {
776 vec4 v = vec4(1);
777 v.xyz = vec3(2);
778 v.x = 5.0;
779 v.xyz.zxy.yx.xy = vec2(5.0, 1.0);
780 }
781 "#,
782 )
783 .unwrap();
784
785 frontend
786 .parse(
787 &Options::from(ShaderStage::Vertex),
788 r#"
789 # version 450
790 void main() {
791 vec4 v = vec4(1);
792 v.xx = vec2(5.0);
793 }
794 "#,
795 )
796 .unwrap_err();
797
798 frontend
799 .parse(
800 &Options::from(ShaderStage::Vertex),
801 r#"
802 # version 450
803 void main() {
804 vec3 v = vec3(1);
805 v.w = 2.0;
806 }
807 "#,
808 )
809 .unwrap_err();
810 }
811
812 #[test]
813 fn expressions() {
814 let mut frontend = Frontend::default();
815
816 // Vector indexing
817 frontend
818 .parse(
819 &Options::from(ShaderStage::Vertex),
820 r#"
821 # version 450
822 float test(int index) {
823 vec4 v = vec4(1.0, 2.0, 3.0, 4.0);
824 return v[index] + 1.0;
825 }
826
827 void main() {}
828 "#,
829 )
830 .unwrap();
831
832 // Prefix increment/decrement
833 frontend
834 .parse(
835 &Options::from(ShaderStage::Vertex),
836 r#"
837 # version 450
838 void main() {
839 uint index = 0;
840
841 --index;
842 ++index;
843 }
844 "#,
845 )
846 .unwrap();
847
848 // Dynamic indexing of array
849 frontend
850 .parse(
851 &Options::from(ShaderStage::Vertex),
852 r#"
853 # version 450
854 void main() {
855 const vec4 positions[1] = { vec4(0) };
856
857 gl_Position = positions[gl_VertexIndex];
858 }
859 "#,
860 )
861 .unwrap();
862 }