Debian Codegraph @

third_party/spirv-cross/reference/shaders-msl-no-opt/comp/array-copy-threadgroup-memory.comp

raw
1 #pragma clang diagnostic ignored "-Wmissing-prototypes"
2 #pragma clang diagnostic ignored "-Wmissing-braces"
3
4 #include <metal_stdlib>
5 #include <simd/simd.h>
6
7 using namespace metal;
8
9 template<typename T, size_t Num>
10 struct spvUnsafeArray
11 {
12 T elements[Num ? Num : 1];
13
14 thread T& operator [] (size_t pos) thread
15 {
16 return elements[pos];
17 }
18 constexpr const thread T& operator [] (size_t pos) const thread
19 {
20 return elements[pos];
21 }
22
23 device T& operator [] (size_t pos) device
24 {
25 return elements[pos];
26 }
27 constexpr const device T& operator [] (size_t pos) const device
28 {
29 return elements[pos];
30 }
31
32 constexpr const constant T& operator [] (size_t pos) const constant
33 {
34 return elements[pos];
35 }
36
37 threadgroup T& operator [] (size_t pos) threadgroup
38 {
39 return elements[pos];
40 }
41 constexpr const threadgroup T& operator [] (size_t pos) const threadgroup
42 {
43 return elements[pos];
44 }
45 };
46
47 constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(8u, 1u, 1u);
48
49 template<typename T, uint A>
50 inline void spvArrayCopyFromConstantToStack1(thread T (&dst)[A], constant T (&src)[A])
51 {
52 for (uint i = 0; i < A; i++)
53 {
54 dst[i] = src[i];
55 }
56 }
57
58 template<typename T, uint A>
59 inline void spvArrayCopyFromConstantToThreadGroup1(threadgroup T (&dst)[A], constant T (&src)[A])
60 {
61 for (uint i = 0; i < A; i++)
62 {
63 dst[i] = src[i];
64 }
65 }
66
67 template<typename T, uint A>
68 inline void spvArrayCopyFromStackToStack1(thread T (&dst)[A], thread const T (&src)[A])
69 {
70 for (uint i = 0; i < A; i++)
71 {
72 dst[i] = src[i];
73 }
74 }
75
76 template<typename T, uint A>
77 inline void spvArrayCopyFromStackToThreadGroup1(threadgroup T (&dst)[A], thread const T (&src)[A])
78 {
79 for (uint i = 0; i < A; i++)
80 {
81 dst[i] = src[i];
82 }
83 }
84
85 template<typename T, uint A>
86 inline void spvArrayCopyFromThreadGroupToStack1(thread T (&dst)[A], threadgroup const T (&src)[A])
87 {
88 for (uint i = 0; i < A; i++)
89 {
90 dst[i] = src[i];
91 }
92 }
93
94 template<typename T, uint A>
95 inline void spvArrayCopyFromThreadGroupToThreadGroup1(threadgroup T (&dst)[A], threadgroup const T (&src)[A])
96 {
97 for (uint i = 0; i < A; i++)
98 {
99 dst[i] = src[i];
100 }
101 }
102
103 template<typename T, uint A>
104 inline void spvArrayCopyFromDeviceToDevice1(device T (&dst)[A], device const T (&src)[A])
105 {
106 for (uint i = 0; i < A; i++)
107 {
108 dst[i] = src[i];
109 }
110 }
111
112 template<typename T, uint A>
113 inline void spvArrayCopyFromConstantToDevice1(device T (&dst)[A], constant T (&src)[A])
114 {
115 for (uint i = 0; i < A; i++)
116 {
117 dst[i] = src[i];
118 }
119 }
120
121 template<typename T, uint A>
122 inline void spvArrayCopyFromStackToDevice1(device T (&dst)[A], thread const T (&src)[A])
123 {
124 for (uint i = 0; i < A; i++)
125 {
126 dst[i] = src[i];
127 }
128 }
129
130 template<typename T, uint A>
131 inline void spvArrayCopyFromThreadGroupToDevice1(device T (&dst)[A], threadgroup const T (&src)[A])
132 {
133 for (uint i = 0; i < A; i++)
134 {
135 dst[i] = src[i];
136 }
137 }
138
139 template<typename T, uint A>
140 inline void spvArrayCopyFromDeviceToStack1(thread T (&dst)[A], device const T (&src)[A])
141 {
142 for (uint i = 0; i < A; i++)
143 {
144 dst[i] = src[i];
145 }
146 }
147
148 template<typename T, uint A>
149 inline void spvArrayCopyFromDeviceToThreadGroup1(threadgroup T (&dst)[A], device const T (&src)[A])
150 {
151 for (uint i = 0; i < A; i++)
152 {
153 dst[i] = src[i];
154 }
155 }
156
157 kernel void main0(uint gl_LocalInvocationIndex [[thread_index_in_threadgroup]])
158 {
159 threadgroup float shared_group[8][8];
160 threadgroup float shared_group_alt[8][8];
161 spvUnsafeArray<float, 8> blob;
162 for (int i = 0; i < 8; i++)
163 {
164 blob[i] = float(i);
165 }
166 spvArrayCopyFromStackToThreadGroup1(shared_group[gl_LocalInvocationIndex], blob.elements);
167 threadgroup_barrier(mem_flags::mem_threadgroup);
168 spvUnsafeArray<float, 8> copied_blob;
169 spvArrayCopyFromThreadGroupToStack1(copied_blob.elements, shared_group[gl_LocalInvocationIndex ^ 1u]);
170 spvArrayCopyFromThreadGroupToThreadGroup1(shared_group_alt[gl_LocalInvocationIndex], shared_group[gl_LocalInvocationIndex]);
171 }
172