Elffuss BraidHub

kikocis/ncnn public

master · 4.2 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
19struct sfpvec8 { f16vec4 abcd; f16vec4 efgh; };
20#endif
21#if NCNN_fp16_arithmetic
22#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
23#endif
24
25layout (constant_id = 0) const int upscale_factor = 0;
26layout (constant_id = 1) const int mode = 0;
27
28#define shape_constant_id_offset 2
29layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
30layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
31layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
32layout (constant_id = shape_constant_id_offset + 3) const int c = 0;
33layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;
34
35layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0;
36layout (constant_id = shape_constant_id_offset + 6) const int outw = 0;
37layout (constant_id = shape_constant_id_offset + 7) const int outh = 0;
38layout (constant_id = shape_constant_id_offset + 8) const int outc = 0;
39layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0;
40
41#if NCNN_image_shader
42layout (binding = 0) uniform unfp sampler3D bottom_blob;
43layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
44#else
45#if NCNN_fp16_packed
46layout (binding = 0) readonly buffer bottom_blob { sfpvec2 bottom_blob_data[]; };
47#else
48layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
49#endif
50layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
51#endif
52
53layout (push_constant) uniform parameter
54{
55 int dims;
56 int w;
57 int h;
58 int c;
59 int cstep;
60
61 int outdims;
62 int outw;
63 int outh;
64 int outc;
65 int outcstep;
66} p;
67
68void main()
69{
70 int gx = int(gl_GlobalInvocationID.x);
71 int gy = int(gl_GlobalInvocationID.y);
72 int gz = int(gl_GlobalInvocationID.z);
73
74 if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
75 return;
76
77 ivec4 gz4 = gz * 4 + ivec4(0, 1, 2, 3);
78
79 int x = gx / upscale_factor;
80 int y = gy / upscale_factor;
81 ivec4 z4;
82 if (mode == 0)
83 {
84 z4 = gz4 * upscale_factor * upscale_factor + (gy % upscale_factor) * upscale_factor + gx % upscale_factor;
85 }
86 else // if (mode == 1)
87 {
88 z4 = ((gy % upscale_factor) * upscale_factor + gx % upscale_factor) * psc(outc) * 4 + gz4;
89 }
90
91#if NCNN_image_shader
92 afpvec8 v0 = image3d_ld8(bottom_blob, ivec3(x, y, z4.r / 8));
93 afpvec8 v1 = image3d_ld8(bottom_blob, ivec3(x, y, z4.g / 8));
94 afpvec8 v2 = image3d_ld8(bottom_blob, ivec3(x, y, z4.b / 8));
95 afpvec8 v3 = image3d_ld8(bottom_blob, ivec3(x, y, z4.a / 8));
96
97 afpvec4 v;
98 v.r = v0[(z4.r % 8) / 4][z4.r % 4];
99 v.g = v1[(z4.g % 8) / 4][z4.g % 4];
100 v.b = v2[(z4.b % 8) / 4][z4.b % 4];
101 v.a = v3[(z4.a % 8) / 4][z4.a % 4];
102
103 image3d_st4(top_blob, ivec3(gx, gy, gz), v);
104#else
105 int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
106
107#if NCNN_fp16_packed
108 ivec4 v_offset = ((z4 / 8) * psc(cstep) + y * psc(w) + x) * 4 + (z4 % 8) / 2;
109 ivec4 lane2 = z4 % 2;
110
111 afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r);
112 afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g);
113 afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b);
114 afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a);
115
116 afpvec4 v = afpvec4(vr[lane2.r], vg[lane2.g], vb[lane2.b], va[lane2.a]);
117
118 buffer_st4(top_blob_data, gi, v);
119#else
120 ivec4 v_offset = ((z4 / 8) * psc(cstep) + y * psc(w) + x) * 8 + z4 % 8;
121
122 buffer_cp1to4(top_blob_data, gi, bottom_blob_data, v_offset);
123#endif
124#endif
125}