Skip to content

Instantly share code, notes, and snippets.

@SausCode
Last active November 28, 2019 20:59
Show Gist options
  • Save SausCode/31510eaeaf57e5340b0f2173a5f70a69 to your computer and use it in GitHub Desktop.
Save SausCode/31510eaeaf57e5340b0f2173a5f70a69 to your computer and use it in GitHub Desktop.
SPV Diss Texture Binding Issues
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wmissing-braces"
#pragma clang diagnostic ignored "-Wunused-variable"
#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_atomic>
using namespace metal;
template<typename T, size_t Num>
struct spvUnsafeArray
{
T elements[Num ? Num : 1];
thread T& operator [] (size_t pos) thread
{
return elements[pos];
}
constexpr const thread T& operator [] (size_t pos) const thread
{
return elements[pos];
}
device T& operator [] (size_t pos) device
{
return elements[pos];
}
constexpr const device T& operator [] (size_t pos) const device
{
return elements[pos];
}
constexpr const constant T& operator [] (size_t pos) const constant
{
return elements[pos];
}
threadgroup T& operator [] (size_t pos) threadgroup
{
return elements[pos];
}
constexpr const threadgroup T& operator [] (size_t pos) const threadgroup
{
return elements[pos];
}
};
struct _17
{
float4 m[1];
};
constant bool _24_tmp [[function_constant(961)]];
constant bool _24 = is_function_constant_defined(_24_tmp) ? _24_tmp : true;
constant bool _28_tmp [[function_constant(962)]];
constant bool _28 = is_function_constant_defined(_28_tmp) ? _28_tmp : true;
constant bool _32_tmp [[function_constant(963)]];
constant bool _32 = is_function_constant_defined(_32_tmp) ? _32_tmp : true;
constant bool _36_tmp [[function_constant(964)]];
constant bool _36 = is_function_constant_defined(_36_tmp) ? _36_tmp : true;
struct main0_in
{
float4 m_38 [[user(locn1)]];
};
// Returns 2D texture coords corresponding to 1D texel buffer coords
static inline __attribute__((always_inline))
uint2 spvTexelBufferCoord(uint tc)
{
return uint2(tc % 4096, tc / 4096);
}
// Returns buffer coords corresponding to 2D texture coords for emulating 2D texture atomics
#define spvImage2DAtomicCoord(tc, tex) (((tex).get_width() * (tc).x) + (tc).y)
static inline __attribute__((always_inline))
void ps_main(thread spvUnsafeArray<float4, 2> (&shader_in), constant _17& v_19, thread texture2d<uint> v_22, device atomic_uint* v_22_atomic, thread texture2d<float, access::write> v_27, thread texture2d<uint, access::write> v_31, thread texture2d<uint> v_35, thread float4& v_48, thread float4& v_65)
{
float2 _49 = as_type<float2>(uint2(shader_in[0].xy));
v_48 = float4(_49.x, _49.y, v_48.z, v_48.w);
if (_24)
{
uint _64 = atomic_fetch_add_explicit((device atomic_uint*)&v_22_atomic[spvImage2DAtomicCoord(as_type<int4>(v_48.xyxx).xy, v_22)], 1u, memory_order_relaxed);
float4 _68 = v_65;
_68.x = as_type<float>(_64);
v_65 = _68;
}
float4 _85 = v_48;
_85.x = as_type<float>((as_type<int>(v_48.y) * as_type<int>(v_19.m[0].x)) + as_type<int>(v_48.x));
v_48 = _85;
float4 _94 = v_48;
_94.y = as_type<float>(as_type<int>(v_48.x) + int(4294967295u));
v_48 = _94;
float4 _105 = v_48;
_105.y = as_type<float>(v_35.read(spvTexelBufferCoord(as_type<int4>(v_48.yyyy).x)).x);
v_48 = _105;
float4 _115 = v_48;
_115.y = as_type<float>(as_type<int>(v_48.y) + as_type<int>(v_65.x));
v_48 = _115;
float4 _126 = v_48;
_126.x = (as_type<uint>(v_48.x) != 0u) ? v_48.y : v_65.x;
v_48 = _126;
if (_28)
{
v_27.write(shader_in[0].zzzz, spvTexelBufferCoord(as_type<int4>(v_48.xxxx).x));
}
v_65 = precise::clamp(shader_in[1], float4(0.0), float4(1.0));
v_65 *= as_type<float4>(uint4(1132396544u));
v_65 = as_type<float4>(uint4(v_65));
if (_32)
{
v_31.write(as_type<uint4>(v_65), spvTexelBufferCoord(as_type<int4>(v_48.xxxx).x));
}
}
fragment void main0(main0_in in [[stage_in]], constant _17& v_19 [[buffer(0)]], texture2d<float, access::write> v_27 [[texture(0)]], texture2d<uint> v_22 [[texture(1)]], device atomic_uint* v_22_atomic [[buffer(1)]], texture2d<uint, access::write> v_31 [[texture(1)]], texture2d<uint> v_35 [[texture(2)]], float4 gl_FragCoord [[position]])
{
spvUnsafeArray<float4, 2> shader_in;
shader_in[1] = in.m_38;
float4 _169 = gl_FragCoord;
_169.w = 1.0 / gl_FragCoord.w;
shader_in[0] = float4(_169.xyz.x, _169.xyz.y, _169.xyz.z, shader_in[0].w);
float4 v_48;
float4 v_65;
ps_main(shader_in, v_19, v_22, v_22_atomic, v_27, v_31, v_35, v_48, v_65);
}
; SPIR-V
; Version: 1.3
; Generator: Khronos; 0
; Bound: 176
; Schema: 0
OpCapability Shader
OpCapability ImageQuery
OpCapability DerivativeControl
OpCapability StorageImageWriteWithoutFormat
OpCapability ImageBuffer
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %__9 %ps_frag_coord
OpExecutionMode %main OriginUpperLeft
OpSource Unknown 0 %3
OpName %shader_in "shader_in"
OpName %ps_main "ps_main"
OpName %_ ""
OpMemberName %_ 0 "m"
OpName %__0 ""
OpName %__1 ""
OpName %__2 ""
OpName %__3 ""
OpName %__4 ""
OpName %__5 ""
OpName %__6 ""
OpName %__7 ""
OpName %__8 ""
OpName %__9 ""
OpName %__10 ""
OpName %__11 ""
OpName %ps_frag_coord "ps_frag_coord"
OpName %main "main"
OpDecorate %_arr_v4float_uint_1 ArrayStride 16
OpDecorate %_ Block
OpMemberDecorate %_ 0 Offset 0
OpDecorate %__0 DescriptorSet 0
OpDecorate %__0 Binding 0
OpDecorate %__1 DescriptorSet 0
OpDecorate %__1 Binding 961
OpDecorate %__2 SpecId 961
OpDecorate %__3 DescriptorSet 0
OpDecorate %__3 Binding 962
OpDecorate %__3 NonReadable
OpDecorate %__4 SpecId 962
OpDecorate %__5 DescriptorSet 0
OpDecorate %__5 Binding 963
OpDecorate %__5 NonReadable
OpDecorate %__6 SpecId 963
OpDecorate %__7 DescriptorSet 0
OpDecorate %__7 Binding 964
OpDecorate %__8 SpecId 964
OpDecorate %__9 Location 1
OpDecorate %ps_frag_coord BuiltIn FragCoord
%uint = OpTypeInt 32 0
%uint_2 = OpConstant %uint 2
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_arr_v4float_uint_2 = OpTypeArray %v4float %uint_2
%_ptr_Private__arr_v4float_uint_2 = OpTypePointer Private %_arr_v4float_uint_2
%void = OpTypeVoid
%13 = OpTypeFunction %void
%uint_1 = OpConstant %uint 1
%_arr_v4float_uint_1 = OpTypeArray %v4float %uint_1
%_ = OpTypeStruct %_arr_v4float_uint_1
%_ptr_Uniform__ = OpTypePointer Uniform %_
%20 = OpTypeImage %uint 2D 0 0 0 2 R32ui
%_ptr_UniformConstant_20 = OpTypePointer UniformConstant %20
%bool = OpTypeBool
%__2 = OpSpecConstantTrue %bool
%25 = OpTypeImage %float Buffer 0 0 0 2 Unknown
%_ptr_UniformConstant_25 = OpTypePointer UniformConstant %25
%__4 = OpSpecConstantTrue %bool
%29 = OpTypeImage %uint Buffer 0 0 0 2 Unknown
%_ptr_UniformConstant_29 = OpTypePointer UniformConstant %29
%__6 = OpSpecConstantTrue %bool
%33 = OpTypeImage %uint Buffer 0 0 0 2 R32ui
%_ptr_UniformConstant_33 = OpTypePointer UniformConstant %33
%__8 = OpSpecConstantTrue %bool
%_ptr_Input_v4float = OpTypePointer Input %v4float
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%_ptr_Private_v4float = OpTypePointer Private %v4float
%v2float = OpTypeVector %float 2
%v2uint = OpTypeVector %uint 2
%v4int = OpTypeVector %int 4
%v2int = OpTypeVector %int 2
%_ptr_Image_uint = OpTypePointer Image %uint
%uint_0 = OpConstant %uint 0
%uint_2056 = OpConstant %uint 2056
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
%_ptr_Uniform_float = OpTypePointer Uniform %float
%uint_4294967295 = OpConstant %uint 4294967295
%v4uint = OpTypeVector %uint 4
%int_1 = OpConstant %int 1
%float_0 = OpConstant %float 0
%141 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
%float_1 = OpConstant %float 1
%143 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
%uint_1132396544 = OpConstant %uint 1132396544
%147 = OpConstantComposite %v4uint %uint_1132396544 %uint_1132396544 %uint_1132396544 %uint_1132396544
%v3float = OpTypeVector %float 3
%shader_in = OpVariable %_ptr_Private__arr_v4float_uint_2 Private
%__0 = OpVariable %_ptr_Uniform__ Uniform
%__1 = OpVariable %_ptr_UniformConstant_20 UniformConstant
%__3 = OpVariable %_ptr_UniformConstant_25 UniformConstant
%__5 = OpVariable %_ptr_UniformConstant_29 UniformConstant
%__7 = OpVariable %_ptr_UniformConstant_33 UniformConstant
%__9 = OpVariable %_ptr_Input_v4float Input
%__10 = OpVariable %_ptr_Private_v4float Private
%__11 = OpVariable %_ptr_Private_v4float Private
%ps_frag_coord = OpVariable %_ptr_Input_v4float Input
%ps_main = OpFunction %void None %13
%14 = OpLabel
%42 = OpAccessChain %_ptr_Private_v4float %shader_in %int_0
%43 = OpLoad %v4float %42
%45 = OpVectorShuffle %v2float %43 %43 0 1
%47 = OpConvertFToU %v2uint %45
%49 = OpBitcast %v2float %47
%50 = OpLoad %v4float %__10
%51 = OpVectorShuffle %v4float %50 %49 4 5 2 3
OpStore %__10 %51
OpSelectionMerge %53 None
OpBranchConditional %__2 %52 %53
%52 = OpLabel
%54 = OpLoad %v4float %__10
%55 = OpVectorShuffle %v4float %54 %54 0 1 0 0
%57 = OpBitcast %v4int %55
%59 = OpVectorShuffle %v2int %57 %57 0 1
%62 = OpImageTexelPointer %_ptr_Image_uint %__1 %59 %uint_0
%64 = OpAtomicIAdd %uint %62 %uint_1 %uint_2056 %uint_1
%66 = OpBitcast %float %64
%67 = OpLoad %v4float %__11
%68 = OpCompositeInsert %v4float %66 %67 0
OpStore %__11 %68
OpBranch %53
%53 = OpLabel
%69 = OpLoad %v4float %__10
%70 = OpCompositeExtract %float %69 1
%71 = OpBitcast %int %70
%73 = OpAccessChain %_ptr_Uniform_v4float %__0 %int_0 %int_0
%75 = OpAccessChain %_ptr_Uniform_float %73 %uint_0
%76 = OpLoad %float %75
%77 = OpBitcast %int %76
%78 = OpLoad %v4float %__10
%79 = OpCompositeExtract %float %78 0
%80 = OpBitcast %int %79
%81 = OpIMul %int %71 %77
%82 = OpIAdd %int %81 %80
%83 = OpBitcast %float %82
%84 = OpLoad %v4float %__10
%85 = OpCompositeInsert %v4float %83 %84 0
OpStore %__10 %85
%86 = OpLoad %v4float %__10
%87 = OpCompositeExtract %float %86 0
%88 = OpBitcast %int %87
%90 = OpBitcast %int %uint_4294967295
%91 = OpIAdd %int %88 %90
%92 = OpBitcast %float %91
%93 = OpLoad %v4float %__10
%94 = OpCompositeInsert %v4float %92 %93 1
OpStore %__10 %94
%95 = OpLoad %v4float %__10
%96 = OpVectorShuffle %v4float %95 %95 1 1 1 1
%97 = OpBitcast %v4int %96
%98 = OpCompositeExtract %int %97 0
%100 = OpLoad %33 %__7
%101 = OpImageRead %v4uint %100 %98
%102 = OpCompositeExtract %uint %101 0
%103 = OpBitcast %float %102
%104 = OpLoad %v4float %__10
%105 = OpCompositeInsert %v4float %103 %104 1
OpStore %__10 %105
%106 = OpLoad %v4float %__10
%107 = OpCompositeExtract %float %106 1
%108 = OpBitcast %int %107
%109 = OpLoad %v4float %__11
%110 = OpCompositeExtract %float %109 0
%111 = OpBitcast %int %110
%112 = OpIAdd %int %108 %111
%113 = OpBitcast %float %112
%114 = OpLoad %v4float %__10
%115 = OpCompositeInsert %v4float %113 %114 1
OpStore %__10 %115
%116 = OpLoad %v4float %__10
%117 = OpCompositeExtract %float %116 0
%118 = OpBitcast %uint %117
%119 = OpLoad %v4float %__10
%120 = OpCompositeExtract %float %119 1
%121 = OpLoad %v4float %__11
%122 = OpCompositeExtract %float %121 0
%123 = OpINotEqual %bool %118 %uint_0
%124 = OpSelect %float %123 %120 %122
%125 = OpLoad %v4float %__10
%126 = OpCompositeInsert %v4float %124 %125 0
OpStore %__10 %126
OpSelectionMerge %128 None
OpBranchConditional %__4 %127 %128
%127 = OpLabel
%129 = OpLoad %v4float %__10
%130 = OpVectorShuffle %v4float %129 %129 0 0 0 0
%131 = OpBitcast %v4int %130
%132 = OpCompositeExtract %int %131 0
%133 = OpAccessChain %_ptr_Private_v4float %shader_in %int_0
%134 = OpLoad %v4float %133
%135 = OpVectorShuffle %v4float %134 %134 2 2 2 2
%136 = OpLoad %25 %__3
OpImageWrite %136 %132 %135
OpBranch %128
%128 = OpLabel
%138 = OpAccessChain %_ptr_Private_v4float %shader_in %int_1
%139 = OpLoad %v4float %138
%144 = OpExtInst %v4float %1 NClamp %139 %141 %143
OpStore %__11 %144
%145 = OpLoad %v4float %__11
%148 = OpBitcast %v4float %147
%149 = OpFMul %v4float %145 %148
OpStore %__11 %149
%150 = OpLoad %v4float %__11
%151 = OpConvertFToU %v4uint %150
%152 = OpBitcast %v4float %151
OpStore %__11 %152
OpSelectionMerge %154 None
OpBranchConditional %__6 %153 %154
%153 = OpLabel
%155 = OpLoad %v4float %__10
%156 = OpVectorShuffle %v4float %155 %155 0 0 0 0
%157 = OpBitcast %v4int %156
%158 = OpCompositeExtract %int %157 0
%159 = OpLoad %v4float %__11
%160 = OpBitcast %v4uint %159
%161 = OpLoad %29 %__5
OpImageWrite %161 %158 %160
OpBranch %154
%154 = OpLabel
OpReturn
OpFunctionEnd
%main = OpFunction %void None %13
%162 = OpLabel
%163 = OpLoad %v4float %__9
%164 = OpAccessChain %_ptr_Private_v4float %shader_in %int_1
OpStore %164 %163
%166 = OpLoad %v4float %ps_frag_coord
%167 = OpCompositeExtract %float %166 3
%168 = OpFDiv %float %float_1 %167
%169 = OpCompositeInsert %v4float %168 %166 3
%171 = OpVectorShuffle %v3float %169 %169 0 1 2
%172 = OpAccessChain %_ptr_Private_v4float %shader_in %int_0
%173 = OpLoad %v4float %172
%174 = OpVectorShuffle %v4float %173 %171 4 5 6 3
OpStore %172 %174
%175 = OpFunctionCall %void %ps_main
OpReturn
OpFunctionEnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment