Skip to content

Commit abc0b30

Browse files
Imberflurteoxoy
authored andcommitted
Add test case for builtin only accessed in function parsed after the entry point function
1 parent 8b818a2 commit abc0b30

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Binary file not shown.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
;; Ensure builtin binding isn't removed by unused gl_PerVertex builtin culling when
2+
;; the builtin is used in a function defined after (in the SPIRV) the entry point.
3+
;;
4+
;; Generated from the following glsl via `glslc` (without `-O` flag):
5+
;;
6+
;; ```glsl
7+
;; #version 450
8+
;;
9+
;; void builtin_usage() {
10+
;; gl_Position = vec4(
11+
;; (gl_VertexIndex == 0) ? -4.0 : 1.0,
12+
;; (gl_VertexIndex == 2) ? 4.0 : -1.0,
13+
;; 0.0,
14+
;; 1.0
15+
;; );
16+
;; }
17+
;;
18+
;; void main()
19+
;; {
20+
;; builtin_usage();
21+
;; }
22+
;; ```
23+
;;
24+
; SPIR-V
25+
; Version: 1.0
26+
; Generator: Google Shaderc over Glslang; 11
27+
; Bound: 37
28+
; Schema: 0
29+
OpCapability Shader
30+
%1 = OpExtInstImport "GLSL.std.450"
31+
OpMemoryModel Logical GLSL450
32+
OpEntryPoint Vertex %main "main" %_ %gl_VertexIndex
33+
OpSource GLSL 450
34+
OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
35+
OpSourceExtension "GL_GOOGLE_include_directive"
36+
OpName %main "main"
37+
OpName %builtin_usage_ "builtin_usage("
38+
OpName %gl_PerVertex "gl_PerVertex"
39+
OpMemberName %gl_PerVertex 0 "gl_Position"
40+
OpMemberName %gl_PerVertex 1 "gl_PointSize"
41+
OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
42+
OpMemberName %gl_PerVertex 3 "gl_CullDistance"
43+
OpName %_ ""
44+
OpName %gl_VertexIndex "gl_VertexIndex"
45+
OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
46+
OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
47+
OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
48+
OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
49+
OpDecorate %gl_PerVertex Block
50+
OpDecorate %gl_VertexIndex BuiltIn VertexIndex
51+
%void = OpTypeVoid
52+
%3 = OpTypeFunction %void
53+
%float = OpTypeFloat 32
54+
%v4float = OpTypeVector %float 4
55+
%uint = OpTypeInt 32 0
56+
%uint_1 = OpConstant %uint 1
57+
%_arr_float_uint_1 = OpTypeArray %float %uint_1
58+
%gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
59+
%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
60+
%_ = OpVariable %_ptr_Output_gl_PerVertex Output
61+
%int = OpTypeInt 32 1
62+
%int_0 = OpConstant %int 0
63+
%_ptr_Input_int = OpTypePointer Input %int
64+
%gl_VertexIndex = OpVariable %_ptr_Input_int Input
65+
%bool = OpTypeBool
66+
%float_n4 = OpConstant %float -4
67+
%float_1 = OpConstant %float 1
68+
%int_2 = OpConstant %int 2
69+
%float_4 = OpConstant %float 4
70+
%float_n1 = OpConstant %float -1
71+
%float_0 = OpConstant %float 0
72+
%_ptr_Output_v4float = OpTypePointer Output %v4float
73+
%main = OpFunction %void None %3
74+
%5 = OpLabel
75+
%36 = OpFunctionCall %void %builtin_usage_
76+
OpReturn
77+
OpFunctionEnd
78+
%builtin_usage_ = OpFunction %void None %3
79+
%7 = OpLabel
80+
%20 = OpLoad %int %gl_VertexIndex
81+
%22 = OpIEqual %bool %20 %int_0
82+
%25 = OpSelect %float %22 %float_n4 %float_1
83+
%26 = OpLoad %int %gl_VertexIndex
84+
%28 = OpIEqual %bool %26 %int_2
85+
%31 = OpSelect %float %28 %float_4 %float_n1
86+
%33 = OpCompositeConstruct %v4float %25 %31 %float_0 %float_1
87+
%35 = OpAccessChain %_ptr_Output_v4float %_ %int_0
88+
OpStore %35 %33
89+
OpReturn
90+
OpFunctionEnd
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
struct gl_PerVertex {
2+
@builtin(position) gl_Position: vec4<f32>,
3+
gl_PointSize: f32,
4+
gl_ClipDistance: array<f32, 1>,
5+
gl_CullDistance: array<f32, 1>,
6+
}
7+
8+
var<private> unnamed: gl_PerVertex = gl_PerVertex(vec4<f32>(0f, 0f, 0f, 1f), 1f, array<f32, 1>(), array<f32, 1>());
9+
var<private> gl_VertexIndex_1: i32;
10+
11+
fn builtin_usage() {
12+
let _e9 = gl_VertexIndex_1;
13+
let _e12 = gl_VertexIndex_1;
14+
unnamed.gl_Position = vec4<f32>(select(1f, -4f, (_e9 == 0i)), select(-1f, 4f, (_e12 == 2i)), 0f, 1f);
15+
return;
16+
}
17+
18+
fn main_1() {
19+
builtin_usage();
20+
return;
21+
}
22+
23+
@vertex
24+
fn main(@builtin(vertex_index) gl_VertexIndex: u32) -> @builtin(position) vec4<f32> {
25+
gl_VertexIndex_1 = i32(gl_VertexIndex);
26+
main_1();
27+
let _e6 = unnamed.gl_Position.y;
28+
unnamed.gl_Position.y = -(_e6);
29+
let _e8 = unnamed.gl_Position;
30+
return _e8;
31+
}

naga/tests/snapshots.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ fn convert_spv_all() {
891891
true,
892892
Targets::METAL | Targets::GLSL | Targets::HLSL | Targets::WGSL,
893893
);
894+
convert_spv("builtin-accessed-outside-entrypoint", true, Targets::WGSL);
894895
}
895896

896897
#[cfg(feature = "glsl-in")]

0 commit comments

Comments
 (0)