1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
18
#extension GL_EXT_shader_16bit_storage: require
20
#if NCNN_fp16_arithmetic
21
#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
24
layout (constant_id = 0) const int order_type = 0;
25
layout (constant_id = 1) const int bugihfa = 0;
27
#define shape_constant_id_offset 2
28
layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
29
layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
30
layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
31
layout (constant_id = shape_constant_id_offset + 3) const int d = 0;
32
layout (constant_id = shape_constant_id_offset + 4) const int c = 0;
33
layout (constant_id = shape_constant_id_offset + 5) const int cstep = 0;
35
layout (constant_id = shape_constant_id_offset + 6) const int outdims = 0;
36
layout (constant_id = shape_constant_id_offset + 7) const int outw = 0;
37
layout (constant_id = shape_constant_id_offset + 8) const int outh = 0;
38
layout (constant_id = shape_constant_id_offset + 9) const int outd = 0;
39
layout (constant_id = shape_constant_id_offset + 10) const int outc = 0;
40
layout (constant_id = shape_constant_id_offset + 11) const int outcstep = 0;
43
layout (binding = 0) uniform unfp sampler3D bottom_blob_3d;
44
layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob_3d;
47
layout (binding = 0) readonly buffer bottom_blob { sfpvec2 bottom_blob_data[]; };
49
layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
51
layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
54
layout (push_constant) uniform parameter
73
int gx = int(gl_GlobalInvocationID.x);
74
int gy = int(gl_GlobalInvocationID.y);
75
int gz = int(gl_GlobalInvocationID.z);
77
if (gx >= psc(outw) || gy >= psc(outh) * psc(outd) || gz >= psc(outc))
96
y4 = gy * 4 + ivec4(0, 1, 2, 3);
100
x4 = gy * 4 + ivec4(0, 1, 2, 3);
104
else if (psc(dims) == 3)
118
z4 = gz * 4 + ivec4(0, 1, 2, 3);
124
z4 = gz * 4 + ivec4(0, 1, 2, 3);
129
y4 = gz * 4 + ivec4(0, 1, 2, 3);
135
y4 = gz * 4 + ivec4(0, 1, 2, 3);
140
x4 = gz * 4 + ivec4(0, 1, 2, 3);
146
x4 = gz * 4 + ivec4(0, 1, 2, 3);
151
else // if (psc(dims) == 4)
179
int yd = gy / psc(outh);
180
int yh = gy % psc(outh);
185
y4 = ivec4(yd * psc(h) + yh);
186
z4 = gz * 4 + ivec4(0, 1, 2, 3);
191
y4 = ivec4(yd * psc(h) + gx);
192
z4 = gz * 4 + ivec4(0, 1, 2, 3);
197
y4 = ivec4(yh * psc(h) + yd);
198
z4 = gz * 4 + ivec4(0, 1, 2, 3);
203
y4 = ivec4(gx * psc(h) + yd);
204
z4 = gz * 4 + ivec4(0, 1, 2, 3);
209
y4 = ivec4(yh * psc(h) + gx);
210
z4 = gz * 4 + ivec4(0, 1, 2, 3);
215
y4 = ivec4(gx * psc(h) + yh);
216
z4 = gz * 4 + ivec4(0, 1, 2, 3);
221
y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yh;
227
y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + gx;
233
y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yd;
239
y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yd;
242
if (order_type == 10)
245
y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + gx;
248
if (order_type == 11)
251
y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yh;
254
if (order_type == 12)
257
y4 = yh * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
260
if (order_type == 13)
263
y4 = gx * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
266
if (order_type == 14)
269
y4 = yd * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
272
if (order_type == 15)
275
y4 = yd * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
278
if (order_type == 16)
281
y4 = gx * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
284
if (order_type == 17)
287
y4 = yh * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
290
if (order_type == 18)
292
x4 = gz * 4 + ivec4(0, 1, 2, 3);
293
y4 = ivec4(yh * psc(h) + gx);
296
if (order_type == 19)
298
x4 = gz * 4 + ivec4(0, 1, 2, 3);
299
y4 = ivec4(gx * psc(h) + yh);
302
if (order_type == 20)
304
x4 = gz * 4 + ivec4(0, 1, 2, 3);
305
y4 = ivec4(yd * psc(h) + gx);
308
if (order_type == 21)
310
x4 = gz * 4 + ivec4(0, 1, 2, 3);
311
y4 = ivec4(yd * psc(h) + yh);
314
if (order_type == 22)
316
x4 = gz * 4 + ivec4(0, 1, 2, 3);
317
y4 = ivec4(gx * psc(h) + yd);
320
if (order_type == 23)
322
x4 = gz * 4 + ivec4(0, 1, 2, 3);
323
y4 = ivec4(yh * psc(h) + yd);
329
ivec4 i4 = z4 * psc(w) * psc(h) * psc(d) + y4 * psc(w) + x4;
332
ivec4 i4 = z4 * psc(cstep) + y4 * psc(w) + x4;
349
v_offset = ((y4 / 4) * psc(w) + x4) * 2 + (y4 % 4) / 2;
352
v_offset = ((y4 / 4) * psc(w) + x4) * 4 + y4 % 4;
356
else if (psc(dims) == 3)
358
int size = psc(w) * psc(h);
361
y4 = i4 % size / psc(w);
362
x4 = i4 % size % psc(w);
369
v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 2 + (z4 % 4) / 2;
372
v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 4 + z4 % 4;
376
else // if (psc(dims) == 4)
378
int size = psc(w) * psc(h) * psc(d);
379
int dsize = psc(w) * psc(h);
382
ivec4 yd4 = i4 % size / dsize;
383
ivec4 yh4 = i4 % size % dsize / psc(w);
384
x4 = i4 % size % dsize % psc(w);
386
y4 = yd4 * psc(h) + yh4;
393
v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 2 + (z4 % 4) / 2;
396
v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 4 + z4 % 4;
402
afpvec4 vr = image3d_ld4(bottom_blob_3d, ivec3(x4.r, y4.r, z4.r));
403
afpvec4 vg = image3d_ld4(bottom_blob_3d, ivec3(x4.g, y4.g, z4.g));
404
afpvec4 vb = image3d_ld4(bottom_blob_3d, ivec3(x4.b, y4.b, z4.b));
405
afpvec4 va = image3d_ld4(bottom_blob_3d, ivec3(x4.a, y4.a, z4.a));
408
#if NCNN_fp16_arithmetic
411
if (lane4.r == 0) v.r = vr.r;
412
if (lane4.r == 1) v.r = vr.g;
413
if (lane4.r == 2) v.r = vr.b;
414
if (lane4.r == 3) v.r = vr.a;
415
if (lane4.g == 0) v.g = vg.r;
416
if (lane4.g == 1) v.g = vg.g;
417
if (lane4.g == 2) v.g = vg.b;
418
if (lane4.g == 3) v.g = vg.a;
419
if (lane4.b == 0) v.b = vb.r;
420
if (lane4.b == 1) v.b = vb.g;
421
if (lane4.b == 2) v.b = vb.b;
422
if (lane4.b == 3) v.b = vb.a;
423
if (lane4.a == 0) v.a = va.r;
424
if (lane4.a == 1) v.a = va.g;
425
if (lane4.a == 2) v.a = va.b;
426
if (lane4.a == 3) v.a = va.a;
431
v = afpvec4(vr[lane4.r], vg[lane4.g], vb[lane4.b], va[lane4.a]);
434
image3d_st4(top_blob_3d, ivec3(gx, gy, gz), v);
436
int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
439
afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r);
440
afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g);
441
afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b);
442
afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a);
444
afpvec4 v = afpvec4(vr[lane2.r], vg[lane2.g], vb[lane2.b], va[lane2.a]);
446
buffer_st4(top_blob_data, gi, v);
448
buffer_cp1to4(top_blob_data, gi, bottom_blob_data, v_offset);