SPIRV-Cross/shaders-msl/asm/comp/variable-pointers-2.asm.comp
Chip Davis 3bfb2f94d4 MSL: Support SPV_KHR_variable_pointers.
This allows shaders to declare and use pointer-type variables. Pointers
may be loaded and stored, be the result of an `OpSelect`, be passed to
and returned from functions, and even be passed as inputs to the `OpPhi`
instruction. All types of pointers may be used as variable pointers.
Variable pointers to storage buffers and workgroup memory may even be
loaded from and stored to, as though they were ordinary variables. In
addition, this enables using an interior pointer to an array as though
it were an array pointer itself using the `OpPtrAccessChain`
instruction.

This is a rather large and involved change, mostly because this is
somewhat complicated with a lot of moving parts. It's a wonder
SPIRV-Cross's output is largely unchanged. Indeed, many of these changes
are to accomplish exactly that! Perhaps the largest source of changes
was the violation of the assumption that, when emitting types, the
pointer type didn't matter.

One of the test cases added by the change doesn't optimize very well;
the output of `spirv-opt` here is invalid SPIR-V. I need to file a bug
with SPIRV-Tools about this.

I wanted to test that variable pointers to images worked too, but I
couldn't figure out how to propagate the access qualifier properly--in
MSL, it's part of the type, so getting this right is important. I've
punted on that for now.
2019-01-07 11:19:10 -06:00

118 lines
5.0 KiB
Plaintext

; SPIR-V
; Version: 1.3
; Generator: Khronos SPIR-V Tools Assembler; 0
; Bound: 65
; Schema: 0
OpCapability Shader
OpCapability VariablePointers
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main" %gl_GlobalInvocationID %gl_LocalInvocationID
OpExecutionMode %main LocalSize 1 1 1
OpSource GLSL 450
OpName %main "main"
OpName %foo "foo"
OpMemberName %foo 0 "a"
OpMemberName %foo 1 "b"
OpMemberName %foo 2 "c"
OpName %bar "bar"
OpMemberName %bar 0 "d"
OpName %buf "buf"
OpName %cb "cb"
OpName %select_buffer "select_buffer"
OpName %select_input "select_input"
OpName %a "a"
OpMemberDecorate %foo 0 Offset 0
OpMemberDecorate %foo 1 Offset 512
OpMemberDecorate %foo 2 Offset 520
OpMemberDecorate %bar 0 Offset 0
OpDecorate %foo Block
OpDecorate %bar Block
OpDecorate %buf DescriptorSet 0
OpDecorate %buf Binding 0
OpDecorate %cb DescriptorSet 0
OpDecorate %cb Binding 1
OpDecorate %_ptr_StorageBuffer_int ArrayStride 4
OpDecorate %_arr_int_uint_128 ArrayStride 4
OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
OpDecorate %gl_LocalInvocationID BuiltIn LocalInvocationId
%void = OpTypeVoid
%15 = OpTypeFunction %void
%int = OpTypeInt 32 1
%uint = OpTypeInt 32 0
%v3uint = OpTypeVector %uint 3
%_ptr_Input_v3uint = OpTypePointer Input %v3uint
%gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
%gl_LocalInvocationID = OpVariable %_ptr_Input_v3uint Input
%uint_128 = OpConstant %uint 128
%_arr_int_uint_128 = OpTypeArray %int %uint_128
%float = OpTypeFloat 32
%v2float = OpTypeVector %float 2
%foo = OpTypeStruct %_arr_int_uint_128 %uint %v2float
%_ptr_StorageBuffer_foo = OpTypePointer StorageBuffer %foo
%buf = OpVariable %_ptr_StorageBuffer_foo StorageBuffer
%bar = OpTypeStruct %int
%_ptr_Uniform_bar = OpTypePointer Uniform %bar
%cb = OpVariable %_ptr_Uniform_bar Uniform
%uint_0 = OpConstant %uint 0
%bool = OpTypeBool
%_ptr_Uniform_int = OpTypePointer Uniform %int
%28 = OpTypeFunction %_ptr_StorageBuffer_foo %_ptr_StorageBuffer_foo
%int_0 = OpConstant %int 0
%uint_1 = OpConstant %uint 1
%31 = OpConstantNull %_ptr_StorageBuffer_foo
%32 = OpTypeFunction %_ptr_Input_v3uint
%_ptr_StorageBuffer_int = OpTypePointer StorageBuffer %int
%_ptr_Function__ptr_StorageBuffer_foo = OpTypePointer Function %_ptr_StorageBuffer_foo
%select_buffer = OpFunction %_ptr_StorageBuffer_foo None %28
%a = OpFunctionParameter %_ptr_StorageBuffer_foo
%33 = OpLabel
%34 = OpAccessChain %_ptr_Uniform_int %cb %uint_0
%35 = OpLoad %int %34
%36 = OpINotEqual %bool %35 %int_0
%37 = OpSelect %_ptr_StorageBuffer_foo %36 %a %31
OpReturnValue %37
OpFunctionEnd
%select_input = OpFunction %_ptr_Input_v3uint None %32
%38 = OpLabel
%39 = OpAccessChain %_ptr_Uniform_int %cb %uint_0
%40 = OpLoad %int %39
%41 = OpINotEqual %bool %40 %int_0
%42 = OpSelect %_ptr_Input_v3uint %41 %gl_GlobalInvocationID %gl_LocalInvocationID
OpReturnValue %42
OpFunctionEnd
%main = OpFunction %void None %15
%43 = OpLabel
%65 = OpVariable %_ptr_Function__ptr_StorageBuffer_foo Function
%44 = OpFunctionCall %_ptr_StorageBuffer_foo %select_buffer %buf
OpStore %65 %44
%45 = OpFunctionCall %_ptr_Input_v3uint %select_input
%66 = OpLoad %_ptr_StorageBuffer_foo %65
%46 = OpAccessChain %_ptr_StorageBuffer_int %66 %uint_0 %uint_0
%47 = OpAccessChain %_ptr_StorageBuffer_int %buf %uint_0 %uint_0
OpBranch %48
%48 = OpLabel
%49 = OpPhi %_ptr_StorageBuffer_int %46 %43 %50 %51
%52 = OpPhi %_ptr_StorageBuffer_int %47 %43 %53 %51
%54 = OpLoad %int %49
%55 = OpLoad %int %52
%56 = OpINotEqual %bool %54 %55
OpLoopMerge %58 %51 None
OpBranchConditional %56 %57 %58
%57 = OpLabel
%59 = OpIAdd %int %54 %55
%60 = OpLoad %v3uint %45
%61 = OpCompositeExtract %uint %60 0
%62 = OpBitcast %int %61
%63 = OpIAdd %int %59 %62
OpStore %49 %63
OpStore %52 %63
OpBranch %51
%51 = OpLabel
%50 = OpPtrAccessChain %_ptr_StorageBuffer_int %49 %uint_1
%53 = OpPtrAccessChain %_ptr_StorageBuffer_int %52 %uint_1
OpBranch %48
%58 = OpLabel
OpReturn
OpFunctionEnd