Elffuss BraidHub

kikocis/ncnn public

master · 11.5 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#version 450
16
17#if NCNN_fp16_storage
18#extension GL_EXT_shader_16bit_storage: require
19#endif
20#if NCNN_fp16_arithmetic
21#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
22#endif
23
24layout (constant_id = 0) const int order_type = 0;
25layout (constant_id = 1) const int bugihfa = 0;
26
27#define shape_constant_id_offset 2
28layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
29layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
30layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
31layout (constant_id = shape_constant_id_offset + 3) const int d = 0;
32layout (constant_id = shape_constant_id_offset + 4) const int c = 0;
33layout (constant_id = shape_constant_id_offset + 5) const int cstep = 0;
34
35layout (constant_id = shape_constant_id_offset + 6) const int outdims = 0;
36layout (constant_id = shape_constant_id_offset + 7) const int outw = 0;
37layout (constant_id = shape_constant_id_offset + 8) const int outh = 0;
38layout (constant_id = shape_constant_id_offset + 9) const int outd = 0;
39layout (constant_id = shape_constant_id_offset + 10) const int outc = 0;
40layout (constant_id = shape_constant_id_offset + 11) const int outcstep = 0;
41
42#if NCNN_image_shader
43layout (binding = 0) uniform unfp sampler3D bottom_blob_3d;
44layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob_3d;
45#else
46#if NCNN_fp16_packed
47layout (binding = 0) readonly buffer bottom_blob { sfpvec2 bottom_blob_data[]; };
48#else
49layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
50#endif
51layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
52#endif
53
54layout (push_constant) uniform parameter
55{
56 int dims;
57 int w;
58 int h;
59 int d;
60 int c;
61 int cstep;
62
63 int outdims;
64 int outw;
65 int outh;
66 int outd;
67 int outc;
68 int outcstep;
69} p;
70
71void main()
72{
73 int gx = int(gl_GlobalInvocationID.x);
74 int gy = int(gl_GlobalInvocationID.y);
75 int gz = int(gl_GlobalInvocationID.z);
76
77 if (gx >= psc(outw) || gy >= psc(outh) * psc(outd) || gz >= psc(outc))
78 return;
79
80 ivec4 x4;
81 ivec4 y4;
82 ivec4 z4;
83
84 if (psc(dims) == 2)
85 {
86 // order_type
87 // 0 = w h
88 // 1 = h w
89
90 gz = 0;
91 z4 = ivec4(0);
92
93 if (order_type == 0)
94 {
95 x4 = ivec4(gx);
96 y4 = gy * 4 + ivec4(0, 1, 2, 3);
97 }
98 if (order_type == 1)
99 {
100 x4 = gy * 4 + ivec4(0, 1, 2, 3);
101 y4 = ivec4(gx);
102 }
103 }
104 else if (psc(dims) == 3)
105 {
106 // order_type
107 // 0 = w h c
108 // 1 = h w c
109 // 2 = w c h
110 // 3 = c w h
111 // 4 = h c w
112 // 5 = c h w
113
114 if (order_type == 0)
115 {
116 x4 = ivec4(gx);
117 y4 = ivec4(gy);
118 z4 = gz * 4 + ivec4(0, 1, 2, 3);
119 }
120 if (order_type == 1)
121 {
122 x4 = ivec4(gy);
123 y4 = ivec4(gx);
124 z4 = gz * 4 + ivec4(0, 1, 2, 3);
125 }
126 if (order_type == 2)
127 {
128 x4 = ivec4(gx);
129 y4 = gz * 4 + ivec4(0, 1, 2, 3);
130 z4 = ivec4(gy);
131 }
132 if (order_type == 3)
133 {
134 x4 = ivec4(gy);
135 y4 = gz * 4 + ivec4(0, 1, 2, 3);
136 z4 = ivec4(gx);
137 }
138 if (order_type == 4)
139 {
140 x4 = gz * 4 + ivec4(0, 1, 2, 3);
141 y4 = ivec4(gx);
142 z4 = ivec4(gy);
143 }
144 if (order_type == 5)
145 {
146 x4 = gz * 4 + ivec4(0, 1, 2, 3);
147 y4 = ivec4(gy);
148 z4 = ivec4(gx);
149 }
150 }
151 else // if (psc(dims) == 4)
152 {
153 // order_type
154 // 0 = w h d c
155 // 1 = h w d c
156 // 2 = w d h c
157 // 3 = d w h c
158 // 4 = h d w c
159 // 5 = d h w c
160 // 6 = w h c d
161 // 7 = h w c d
162 // 8 = w c h d
163 // 9 = c w h d
164 //10 = h c w d
165 //11 = c h w d
166 //12 = w d c h
167 //13 = d w c h
168 //14 = w c d h
169 //15 = c w d h
170 //16 = d c w h
171 //17 = c d w h
172 //18 = h d c w
173 //19 = d h c w
174 //20 = h c d w
175 //21 = c h d w
176 //22 = d c h w
177 //23 = c d h w
178
179 int yd = gy / psc(outh);
180 int yh = gy % psc(outh);
181
182 if (order_type == 0)
183 {
184 x4 = ivec4(gx);
185 y4 = ivec4(yd * psc(h) + yh);
186 z4 = gz * 4 + ivec4(0, 1, 2, 3);
187 }
188 if (order_type == 1)
189 {
190 x4 = ivec4(yh);
191 y4 = ivec4(yd * psc(h) + gx);
192 z4 = gz * 4 + ivec4(0, 1, 2, 3);
193 }
194 if (order_type == 2)
195 {
196 x4 = ivec4(gx);
197 y4 = ivec4(yh * psc(h) + yd);
198 z4 = gz * 4 + ivec4(0, 1, 2, 3);
199 }
200 if (order_type == 3)
201 {
202 x4 = ivec4(yh);
203 y4 = ivec4(gx * psc(h) + yd);
204 z4 = gz * 4 + ivec4(0, 1, 2, 3);
205 }
206 if (order_type == 4)
207 {
208 x4 = ivec4(yd);
209 y4 = ivec4(yh * psc(h) + gx);
210 z4 = gz * 4 + ivec4(0, 1, 2, 3);
211 }
212 if (order_type == 5)
213 {
214 x4 = ivec4(yd);
215 y4 = ivec4(gx * psc(h) + yh);
216 z4 = gz * 4 + ivec4(0, 1, 2, 3);
217 }
218 if (order_type == 6)
219 {
220 x4 = ivec4(gx);
221 y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yh;
222 z4 = ivec4(yd);
223 }
224 if (order_type == 7)
225 {
226 x4 = ivec4(yh);
227 y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + gx;
228 z4 = ivec4(yd);
229 }
230 if (order_type == 8)
231 {
232 x4 = ivec4(gx);
233 y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yd;
234 z4 = ivec4(yh);
235 }
236 if (order_type == 9)
237 {
238 x4 = ivec4(yh);
239 y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yd;
240 z4 = ivec4(gx);
241 }
242 if (order_type == 10)
243 {
244 x4 = ivec4(yd);
245 y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + gx;
246 z4 = ivec4(yh);
247 }
248 if (order_type == 11)
249 {
250 x4 = ivec4(yd);
251 y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yh;
252 z4 = ivec4(gx);
253 }
254 if (order_type == 12)
255 {
256 x4 = ivec4(gx);
257 y4 = yh * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
258 z4 = ivec4(yd);
259 }
260 if (order_type == 13)
261 {
262 x4 = ivec4(yh);
263 y4 = gx * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
264 z4 = ivec4(yd);
265 }
266 if (order_type == 14)
267 {
268 x4 = ivec4(gx);
269 y4 = yd * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
270 z4 = ivec4(yh);
271 }
272 if (order_type == 15)
273 {
274 x4 = ivec4(yh);
275 y4 = yd * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
276 z4 = ivec4(gx);
277 }
278 if (order_type == 16)
279 {
280 x4 = ivec4(yd);
281 y4 = gx * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
282 z4 = ivec4(yh);
283 }
284 if (order_type == 17)
285 {
286 x4 = ivec4(yd);
287 y4 = yh * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
288 z4 = ivec4(gx);
289 }
290 if (order_type == 18)
291 {
292 x4 = gz * 4 + ivec4(0, 1, 2, 3);
293 y4 = ivec4(yh * psc(h) + gx);
294 z4 = ivec4(yd);
295 }
296 if (order_type == 19)
297 {
298 x4 = gz * 4 + ivec4(0, 1, 2, 3);
299 y4 = ivec4(gx * psc(h) + yh);
300 z4 = ivec4(yd);
301 }
302 if (order_type == 20)
303 {
304 x4 = gz * 4 + ivec4(0, 1, 2, 3);
305 y4 = ivec4(yd * psc(h) + gx);
306 z4 = ivec4(yh);
307 }
308 if (order_type == 21)
309 {
310 x4 = gz * 4 + ivec4(0, 1, 2, 3);
311 y4 = ivec4(yd * psc(h) + yh);
312 z4 = ivec4(gx);
313 }
314 if (order_type == 22)
315 {
316 x4 = gz * 4 + ivec4(0, 1, 2, 3);
317 y4 = ivec4(gx * psc(h) + yd);
318 z4 = ivec4(yh);
319 }
320 if (order_type == 23)
321 {
322 x4 = gz * 4 + ivec4(0, 1, 2, 3);
323 y4 = ivec4(yh * psc(h) + yd);
324 z4 = ivec4(gx);
325 }
326 }
327
328#if NCNN_image_shader
329 ivec4 i4 = z4 * psc(w) * psc(h) * psc(d) + y4 * psc(w) + x4;
330 ivec4 lane2;
331 ivec4 lane4;
332#else
333 ivec4 i4 = z4 * psc(cstep) + y4 * psc(w) + x4;
334 ivec4 v_offset;
335#if NCNN_fp16_packed
336 ivec4 lane2;
337#endif
338#endif
339
340 if (psc(dims) == 2)
341 {
342 y4 = i4 / psc(w);
343 x4 = i4 % psc(w);
344
345#if NCNN_image_shader
346 lane2 = (y4 % 8) / 4;
347 lane4 = y4 % 4;
348 y4 = y4 / 8;
349#else
350#if NCNN_fp16_packed
351 v_offset = ((y4 / 8) * psc(w) + x4) * 4 + (y4 % 8) / 2;
352 lane2 = y4 % 2;
353#else
354 v_offset = ((y4 / 8) * psc(w) + x4) * 8 + y4 % 8;
355#endif
356#endif
357 }
358 else if (psc(dims) == 3)
359 {
360 int size = psc(w) * psc(h);
361
362 z4 = i4 / size;
363 y4 = i4 % size / psc(w);
364 x4 = i4 % size % psc(w);
365
366#if NCNN_image_shader
367 lane2 = (z4 % 8) / 4;
368 lane4 = z4 % 4;
369 z4 = z4 / 8;
370#else
371#if NCNN_fp16_packed
372 v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 4 + (z4 % 8) / 2;
373 lane2 = z4 % 2;
374#else
375 v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 8 + z4 % 8;
376#endif
377#endif
378 }
379 else // if (psc(dims) == 4)
380 {
381 int size = psc(w) * psc(h) * psc(d);
382 int dsize = psc(w) * psc(h);
383
384 z4 = i4 / size;
385 ivec4 yd4 = i4 % size / dsize;
386 ivec4 yh4 = i4 % size % dsize / psc(w);
387 x4 = i4 % size % dsize % psc(w);
388
389 y4 = yd4 * psc(h) + yh4;
390
391#if NCNN_image_shader
392 lane2 = (z4 % 8) / 4;
393 lane4 = z4 % 4;
394 z4 = z4 / 8;
395#else
396#if NCNN_fp16_packed
397 v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 4 + (z4 % 8) / 2;
398 lane2 = z4 % 2;
399#else
400 v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 8 + z4 % 8;
401#endif
402#endif
403 }
404
405#if NCNN_image_shader
406 afpvec8 v0 = image3d_ld8(bottom_blob_3d, ivec3(x4.r, y4.r, z4.r));
407 afpvec8 v1 = image3d_ld8(bottom_blob_3d, ivec3(x4.g, y4.g, z4.g));
408 afpvec8 v2 = image3d_ld8(bottom_blob_3d, ivec3(x4.b, y4.b, z4.b));
409 afpvec8 v3 = image3d_ld8(bottom_blob_3d, ivec3(x4.a, y4.a, z4.a));
410
411 afpvec4 v;
412 v.r = v0[lane2.r][lane4.r];
413 v.g = v1[lane2.g][lane4.g];
414 v.b = v2[lane2.b][lane4.b];
415 v.a = v3[lane2.a][lane4.a];
416
417 image3d_st4(top_blob_3d, ivec3(gx, gy, gz), v);
418#else
419 int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
420
421#if NCNN_fp16_packed
422 afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r);
423 afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g);
424 afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b);
425 afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a);
426
427 afpvec4 v = afpvec4(vr[lane2.r], vg[lane2.g], vb[lane2.b], va[lane2.a]);
428
429 buffer_st4(top_blob_data, gi, v);
430#else
431 buffer_cp1to4(top_blob_data, gi, bottom_blob_data, v_offset);
432#endif
433#endif
434}