Elffuss BraidHub

kikocis/ncnn public

master · 8.9 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 ndim = 0;
26
27#define shape_constant_id_offset 1
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 { sfpvec8 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 i4;
81 ivec4 ii4;
82 if (ndim == 1)
83 {
84 i4 = gx * 8 + ivec4(0, 1, 2, 3);
85 ii4 = i4 + 4;
86 }
87 if (ndim == 2)
88 {
89 i4 = (gy * 8) * psc(outw) + gx + ivec4(0, 1, 2, 3) * psc(outw);
90 ii4 = i4 + 4 * psc(outw);
91 }
92 if (ndim == 3)
93 {
94 i4 = (gz * 8) * psc(outh) * psc(outw) + gy * psc(outw) + gx + ivec4(0, 1, 2, 3) * psc(outh) * psc(outw);
95 ii4 = i4 + 4 * psc(outh) * psc(outw);
96 }
97 if (ndim == 4)
98 {
99 i4 = (gz * 8) * psc(outd) * psc(outh) * psc(outw) + gy * psc(outw) + gx + ivec4(0, 1, 2, 3) * psc(outd) * psc(outh) * psc(outw);
100 ii4 = i4 + 4 * psc(outd) * psc(outh) * psc(outw);
101 }
102
103 ivec4 x4;
104 ivec4 xx4;
105 ivec4 y4;
106 ivec4 yy4;
107 ivec4 z4;
108 ivec4 zz4;
109
110#if NCNN_image_shader
111 ivec4 lane2;
112 ivec4 lane2_1;
113 ivec4 lane4;
114 ivec4 lane4_1;
115#else
116 ivec4 v_offset;
117 ivec4 vv_offset;
118#if NCNN_fp16_packed
119 ivec4 lane2;
120 ivec4 lane4;
121#endif
122#endif
123
124 if (psc(dims) == 1)
125 {
126 z4 = ivec4(0);
127 y4 = ivec4(0);
128 x4 = i4;
129 zz4 = ivec4(0);
130 yy4 = ivec4(0);
131 xx4 = ii4;
132
133#if NCNN_image_shader
134 lane2 = (x4 % 8) / 4;
135 lane2_1 = (xx4 % 8) / 4;
136 lane4 = x4 % 4;
137 lane4_1 = xx4 % 4;
138 x4 = x4 / 8;
139 xx4 = xx4 / 8;
140#else
141#if NCNN_fp16_packed
142 v_offset = i4 / 2;
143 lane2 = i4 % 2;
144 vv_offset = ii4 / 2;
145 lane4 = ii4 % 2;
146#else
147 v_offset = i4;
148 vv_offset = ii4;
149#endif
150#endif
151 }
152 else if (psc(dims) == 2)
153 {
154 z4 = ivec4(0);
155 y4 = i4 / psc(w);
156 x4 = i4 % psc(w);
157 zz4 = ivec4(0);
158 yy4 = ii4 / psc(w);
159 xx4 = ii4 % psc(w);
160
161#if NCNN_image_shader
162 lane2 = (y4 % 8) / 4;
163 lane2_1 = (yy4 % 8) / 4;
164 lane4 = y4 % 4;
165 lane4_1 = yy4 % 4;
166 y4 = y4 / 8;
167 yy4 = yy4 / 8;
168#else
169#if NCNN_fp16_packed
170 v_offset = ((y4 / 8) * psc(w) + x4) * 4 + (y4 % 8) / 2;
171 lane2 = y4 % 2;
172 vv_offset = ((yy4 / 8) * psc(w) + xx4) * 4 + (yy4 % 8) / 2;
173 lane4 = yy4 % 2;
174#else
175 v_offset = ((y4 / 8) * psc(w) + x4) * 8 + y4 % 8;
176 vv_offset = ((yy4 / 8) * psc(w) + xx4) * 8 + yy4 % 8;
177#endif
178#endif
179 }
180 else if (psc(dims) == 3)
181 {
182 int size = psc(w) * psc(h);
183
184 z4 = i4 / size;
185 y4 = i4 % size / psc(w);
186 x4 = i4 % size % psc(w);
187 zz4 = ii4 / size;
188 yy4 = ii4 % size / psc(w);
189 xx4 = ii4 % size % psc(w);
190
191#if NCNN_image_shader
192 lane2 = (z4 % 8) / 4;
193 lane2_1 = (zz4 % 8) / 4;
194 lane4 = z4 % 4;
195 lane4_1 = zz4 % 4;
196 z4 = z4 / 8;
197 zz4 = zz4 / 8;
198#else
199#if NCNN_fp16_packed
200 v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 4 + (z4 % 8) / 2;
201 lane2 = z4 % 2;
202 vv_offset = ((zz4 / 8) * psc(cstep) + yy4 * psc(w) + xx4) * 4 + (zz4 % 8) / 2;
203 lane4 = zz4 % 2;
204#else
205 v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 8 + z4 % 8;
206 vv_offset = ((zz4 / 8) * psc(cstep) + yy4 * psc(w) + xx4) * 8 + zz4 % 8;
207#endif
208#endif
209 }
210 else // if (psc(dims) == 4)
211 {
212 int size = psc(w) * psc(h) * psc(d);
213 int dsize = psc(w) * psc(h);
214
215 z4 = i4 / size;
216 ivec4 yd4 = i4 % size / dsize;
217 ivec4 yh4 = i4 % size % dsize / psc(w);
218 x4 = i4 % size % dsize % psc(w);
219 zz4 = ii4 / size;
220 ivec4 yyd4 = ii4 % size / dsize;
221 ivec4 yyh4 = ii4 % size % dsize / psc(w);
222 xx4 = ii4 % size % dsize % psc(w);
223
224 y4 = yd4 * psc(h) + yh4;
225 yy4 = yyd4 * psc(h) + yyh4;
226
227#if NCNN_image_shader
228 lane2 = (z4 % 8) / 4;
229 lane2_1 = (zz4 % 8) / 4;
230 lane4 = z4 % 4;
231 lane4_1 = zz4 % 4;
232 z4 = z4 / 8;
233 zz4 = zz4 / 8;
234#else
235#if NCNN_fp16_packed
236 v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 4 + (z4 % 8) / 2;
237 lane2 = z4 % 2;
238 vv_offset = ((zz4 / 8) * psc(cstep) + yy4 * psc(w) + xx4) * 4 + (zz4 % 8) / 2;
239 lane4 = zz4 % 2;
240#else
241 v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 8 + z4 % 8;
242 vv_offset = ((zz4 / 8) * psc(cstep) + yy4 * psc(w) + xx4) * 8 + zz4 % 8;
243#endif
244#endif
245 }
246
247#if NCNN_image_shader
248 afpvec8 v0 = image3d_ld8(bottom_blob_3d, ivec3(x4.r, y4.r, z4.r));
249 afpvec8 v1 = image3d_ld8(bottom_blob_3d, ivec3(x4.g, y4.g, z4.g));
250 afpvec8 v2 = image3d_ld8(bottom_blob_3d, ivec3(x4.b, y4.b, z4.b));
251 afpvec8 v3 = image3d_ld8(bottom_blob_3d, ivec3(x4.a, y4.a, z4.a));
252 afpvec8 v4 = image3d_ld8(bottom_blob_3d, ivec3(xx4.r, yy4.r, zz4.r));
253 afpvec8 v5 = image3d_ld8(bottom_blob_3d, ivec3(xx4.g, yy4.g, zz4.g));
254 afpvec8 v6 = image3d_ld8(bottom_blob_3d, ivec3(xx4.b, yy4.b, zz4.b));
255 afpvec8 v7 = image3d_ld8(bottom_blob_3d, ivec3(xx4.a, yy4.a, zz4.a));
256
257 afpvec8 v;
258 v[0].r = v0[lane2.r][lane4.r];
259 v[0].g = v1[lane2.g][lane4.g];
260 v[0].b = v2[lane2.b][lane4.b];
261 v[0].a = v3[lane2.a][lane4.a];
262 v[1].r = v4[lane2_1.r][lane4_1.r];
263 v[1].g = v5[lane2_1.g][lane4_1.g];
264 v[1].b = v6[lane2_1.b][lane4_1.b];
265 v[1].a = v7[lane2_1.a][lane4_1.a];
266
267 if (ndim == 1) image3d_st8(top_blob_3d, ivec3(gx, 0, 0), v);
268 if (ndim == 2) image3d_st8(top_blob_3d, ivec3(gx, gy, 0), v);
269 if (ndim == 3 || ndim == 4) image3d_st8(top_blob_3d, ivec3(gx, gy, gz), v);
270#else
271 int gi;
272 if (ndim == 1) gi = gx;
273 if (ndim == 2) gi = gy * psc(outw) + gx;
274 if (ndim == 3 || ndim == 4) gi = gz * psc(outcstep) + gy * psc(outw) + gx;
275
276#if NCNN_fp16_packed
277 afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r);
278 afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g);
279 afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b);
280 afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a);
281
282 afpvec2 vvr = buffer_ld2(bottom_blob_data, vv_offset.r);
283 afpvec2 vvg = buffer_ld2(bottom_blob_data, vv_offset.g);
284 afpvec2 vvb = buffer_ld2(bottom_blob_data, vv_offset.b);
285 afpvec2 vva = buffer_ld2(bottom_blob_data, vv_offset.a);
286
287 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]);
288
289 buffer_st8(top_blob_data, gi, v);
290#else
291 buffer_cp1to8(top_blob_data, gi, bottom_blob_data, v_offset, vv_offset);
292#endif
293#endif
294}