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
19
struct sfpvec8 { f16vec4 abcd; f16vec4 efgh; };
21
#if NCNN_fp16_arithmetic
22
#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
25
layout (constant_id = 0) const int order_type = 0;
26
layout (constant_id = 1) const int bugihfa = 0;
28
#define shape_constant_id_offset 2
29
layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
30
layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
31
layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
32
layout (constant_id = shape_constant_id_offset + 3) const int d = 0;
33
layout (constant_id = shape_constant_id_offset + 4) const int c = 0;
34
layout (constant_id = shape_constant_id_offset + 5) const int cstep = 0;
36
layout (constant_id = shape_constant_id_offset + 6) const int outdims = 0;
37
layout (constant_id = shape_constant_id_offset + 7) const int outw = 0;
38
layout (constant_id = shape_constant_id_offset + 8) const int outh = 0;
39
layout (constant_id = shape_constant_id_offset + 9) const int outd = 0;
40
layout (constant_id = shape_constant_id_offset + 10) const int outc = 0;
41
layout (constant_id = shape_constant_id_offset + 11) const int outcstep = 0;
44
layout (binding = 0) uniform unfp sampler3D bottom_blob_3d;
45
layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob_3d;
48
layout (binding = 0) readonly buffer bottom_blob { sfpvec2 bottom_blob_data[]; };
50
layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
52
layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };
55
layout (push_constant) uniform parameter
74
int gx = int(gl_GlobalInvocationID.x);
75
int gy = int(gl_GlobalInvocationID.y);
76
int gz = int(gl_GlobalInvocationID.z);
78
if (gx >= psc(outw) || gy >= psc(outh) * psc(outd) || gz >= psc(outc))
102
y4 = gy * 8 + ivec4(0, 1, 2, 3);
107
x4 = gy * 8 + ivec4(0, 1, 2, 3);
113
else if (psc(dims) == 3)
129
z4 = gz * 8 + ivec4(0, 1, 2, 3);
138
z4 = gz * 8 + ivec4(0, 1, 2, 3);
145
y4 = gz * 8 + ivec4(0, 1, 2, 3);
154
y4 = gz * 8 + ivec4(0, 1, 2, 3);
161
x4 = gz * 8 + ivec4(0, 1, 2, 3);
170
x4 = gz * 8 + ivec4(0, 1, 2, 3);
178
else // if (psc(dims) == 4)
206
int yd = gy / psc(outh);
207
int yh = gy % psc(outh);
213
y4 = ivec4(yd * psc(h) + yh);
215
z4 = gz * 8 + ivec4(0, 1, 2, 3);
222
y4 = ivec4(yd * psc(h) + gx);
224
z4 = gz * 8 + ivec4(0, 1, 2, 3);
231
y4 = ivec4(yh * psc(h) + yd);
233
z4 = gz * 8 + ivec4(0, 1, 2, 3);
240
y4 = ivec4(gx * psc(h) + yd);
242
z4 = gz * 8 + ivec4(0, 1, 2, 3);
249
y4 = ivec4(yh * psc(h) + gx);
251
z4 = gz * 8 + ivec4(0, 1, 2, 3);
258
y4 = ivec4(gx * psc(h) + yh);
260
z4 = gz * 8 + ivec4(0, 1, 2, 3);
267
y4 = (gz * 8 + ivec4(0, 1, 2, 3)) * psc(h) + yh;
268
yy4 = y4 + 4 * psc(h);
276
y4 = (gz * 8 + ivec4(0, 1, 2, 3)) * psc(h) + gx;
277
yy4 = y4 + 4 * psc(h);
285
y4 = (gz * 8 + ivec4(0, 1, 2, 3)) * psc(h) + yd;
286
yy4 = y4 + 4 * psc(h);
294
y4 = (gz * 8 + ivec4(0, 1, 2, 3)) * psc(h) + yd;
295
yy4 = y4 + 4 * psc(h);
299
if (order_type == 10)
303
y4 = (gz * 8 + ivec4(0, 1, 2, 3)) * psc(h) + gx;
304
yy4 = y4 + 4 * psc(h);
308
if (order_type == 11)
312
y4 = (gz * 8 + ivec4(0, 1, 2, 3)) * psc(h) + yh;
313
yy4 = y4 + 4 * psc(h);
317
if (order_type == 12)
321
y4 = yh * psc(h) + gz * 8 + ivec4(0, 1, 2, 3);
326
if (order_type == 13)
330
y4 = gx * psc(h) + gz * 8 + ivec4(0, 1, 2, 3);
335
if (order_type == 14)
339
y4 = yd * psc(h) + gz * 8 + ivec4(0, 1, 2, 3);
344
if (order_type == 15)
348
y4 = yd * psc(h) + gz * 8 + ivec4(0, 1, 2, 3);
353
if (order_type == 16)
357
y4 = gx * psc(h) + gz * 8 + ivec4(0, 1, 2, 3);
362
if (order_type == 17)
366
y4 = yh * psc(h) + gz * 8 + ivec4(0, 1, 2, 3);
371
if (order_type == 18)
373
x4 = gz * 8 + ivec4(0, 1, 2, 3);
375
y4 = ivec4(yh * psc(h) + gx);
380
if (order_type == 19)
382
x4 = gz * 8 + ivec4(0, 1, 2, 3);
384
y4 = ivec4(gx * psc(h) + yh);
389
if (order_type == 20)
391
x4 = gz * 8 + ivec4(0, 1, 2, 3);
393
y4 = ivec4(yd * psc(h) + gx);
398
if (order_type == 21)
400
x4 = gz * 8 + ivec4(0, 1, 2, 3);
402
y4 = ivec4(yd * psc(h) + yh);
407
if (order_type == 22)
409
x4 = gz * 8 + ivec4(0, 1, 2, 3);
411
y4 = ivec4(gx * psc(h) + yd);
416
if (order_type == 23)
418
x4 = gz * 8 + ivec4(0, 1, 2, 3);
420
y4 = ivec4(yh * psc(h) + yd);
428
ivec4 i4 = z4 * psc(w) * psc(h) * psc(d) + y4 * psc(w) + x4;
429
ivec4 ii4 = zz4 * psc(w) * psc(h) * psc(d) + yy4 * psc(w) + xx4;
435
ivec4 i4 = z4 * psc(cstep) + y4 * psc(w) + x4;
436
ivec4 ii4 = zz4 * psc(cstep) + yy4 * psc(w) + xx4;
453
lane2 = (y4 % 8) / 4;
454
lane2_1 = (yy4 % 8) / 4;
462
v_offset = ((y4 / 8) * psc(w) + x4) * 4 + (y4 % 8) / 2;
464
vv_offset = ((yy4 / 8) * psc(w) + xx4) * 4 + (yy4 % 8) / 2;
467
v_offset = ((y4 / 8) * psc(w) + x4) * 8 + y4 % 8;
468
vv_offset = ((yy4 / 8) * psc(w) + xx4) * 8 + yy4 % 8;
472
else if (psc(dims) == 3)
474
int size = psc(w) * psc(h);
477
y4 = i4 % size / psc(w);
478
x4 = i4 % size % psc(w);
480
yy4 = ii4 % size / psc(w);
481
xx4 = ii4 % size % psc(w);
484
lane2 = (z4 % 8) / 4;
485
lane2_1 = (zz4 % 8) / 4;
493
v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 4 + (z4 % 8) / 2;
495
vv_offset = ((zz4 / 8) * psc(cstep) + yy4 * psc(w) + xx4) * 4 + (zz4 % 8) / 2;
498
v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 8 + z4 % 8;
499
vv_offset = ((zz4 / 8) * psc(cstep) + yy4 * psc(w) + xx4) * 8 + zz4 % 8;
503
else // if (psc(dims) == 4)
505
int size = psc(w) * psc(h) * psc(d);
506
int dsize = psc(w) * psc(h);
509
ivec4 yd4 = i4 % size / dsize;
510
ivec4 yh4 = i4 % size % dsize / psc(w);
511
x4 = i4 % size % psc(w);
513
ivec4 yyd4 = ii4 % size / dsize;
514
ivec4 yyh4 = ii4 % size % dsize / psc(w);
515
xx4 = ii4 % size % psc(w);
517
y4 = yd4 * psc(h) + yh4;
518
yy4 = yyd4 * psc(h) + yyh4;
521
lane2 = (z4 % 8) / 4;
522
lane2_1 = (zz4 % 8) / 4;
530
v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 4 + (z4 % 8) / 2;
532
vv_offset = ((zz4 / 8) * psc(cstep) + yy4 * psc(w) + xx4) * 4 + (zz4 % 8) / 2;
535
v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 8 + z4 % 8;
536
vv_offset = ((zz4 / 8) * psc(cstep) + yy4 * psc(w) + xx4) * 8 + zz4 % 8;
542
afpvec8 v0 = image3d_ld8(bottom_blob_3d, ivec3(x4.r, y4.r, z4.r));
543
afpvec8 v1 = image3d_ld8(bottom_blob_3d, ivec3(x4.g, y4.g, z4.g));
544
afpvec8 v2 = image3d_ld8(bottom_blob_3d, ivec3(x4.b, y4.b, z4.b));
545
afpvec8 v3 = image3d_ld8(bottom_blob_3d, ivec3(x4.a, y4.a, z4.a));
546
afpvec8 v4 = image3d_ld8(bottom_blob_3d, ivec3(xx4.r, yy4.r, zz4.r));
547
afpvec8 v5 = image3d_ld8(bottom_blob_3d, ivec3(xx4.g, yy4.g, zz4.g));
548
afpvec8 v6 = image3d_ld8(bottom_blob_3d, ivec3(xx4.b, yy4.b, zz4.b));
549
afpvec8 v7 = image3d_ld8(bottom_blob_3d, ivec3(xx4.a, yy4.a, zz4.a));
552
v[0].r = v0[lane2.r][lane4.r];
553
v[0].g = v1[lane2.g][lane4.g];
554
v[0].b = v2[lane2.b][lane4.b];
555
v[0].a = v3[lane2.a][lane4.a];
556
v[1].r = v4[lane2_1.r][lane4_1.r];
557
v[1].g = v5[lane2_1.g][lane4_1.g];
558
v[1].b = v6[lane2_1.b][lane4_1.b];
559
v[1].a = v7[lane2_1.a][lane4_1.a];
561
image3d_st8(top_blob_3d, ivec3(gx, gy, gz), v);
563
int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
566
afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r);
567
afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g);
568
afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b);
569
afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a);
571
afpvec2 vvr = buffer_ld2(bottom_blob_data, vv_offset.r);
572
afpvec2 vvg = buffer_ld2(bottom_blob_data, vv_offset.g);
573
afpvec2 vvb = buffer_ld2(bottom_blob_data, vv_offset.b);
574
afpvec2 vva = buffer_ld2(bottom_blob_data, vv_offset.a);
576
afpvec8 v = afpvec8(vr[lane2.r], vg[lane2.g], vb[lane2.b], va[lane2.a], vvr[lane4.r], vvg[lane4.g], vvb[lane4.b], vva[lane4.a]);
578
buffer_st8(top_blob_data, gi, v);
580
buffer_cp1to8(top_blob_data, gi, bottom_blob_data, v_offset, vv_offset);