Elffuss BraidHub

kikocis/ncnn public

master · 6.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, imfmtc1) writeonly uniform unfp image3D top_blob_3d;
45#else
46layout (binding = 0) readonly buffer bottom_blob { sfpvec8 bottom_blob_data[]; };
47layout (binding = 1) writeonly buffer top_blob { sfp top_blob_data[]; };
48#endif
49
50layout (push_constant) uniform parameter
51{
52 int dims;
53 int w;
54 int h;
55 int d;
56 int c;
57 int cstep;
58
59 int outdims;
60 int outw;
61 int outh;
62 int outd;
63 int outc;
64 int outcstep;
65} p;
66
67void main()
68{
69 int gx = int(gl_GlobalInvocationID.x);
70 int gy = int(gl_GlobalInvocationID.y);
71 int gz = int(gl_GlobalInvocationID.z);
72
73 if (gx >= psc(w) || gy >= psc(h) * psc(d) || gz >= psc(c))
74 return;
75
76 ivec4 i4;
77 ivec4 ii4;
78 if (psc(dims) == 1)
79 {
80 i4 = gx * 8 + ivec4(0, 1, 2, 3);
81 ii4 = i4 + 4;
82 }
83 else if (psc(dims) == 2)
84 {
85 i4 = (gy * 8) * psc(w) + gx + ivec4(0, 1, 2, 3) * psc(w);
86 ii4 = i4 + 4 * psc(w);
87 }
88 else if (psc(dims) == 3)
89 {
90 i4 = (gz * 8) * psc(h) * psc(w) + gy * psc(w) + gx + ivec4(0, 1, 2, 3) * psc(h) * psc(w);
91 ii4 = i4 + 4 * psc(h) * psc(w);
92 }
93 else // if (psc(dims) == 4)
94 {
95 i4 = (gz * 8) * psc(d) * psc(h) * psc(w) + gy * psc(w) + gx + ivec4(0, 1, 2, 3) * psc(d) * psc(h) * psc(w);
96 ii4 = i4 + 4 * psc(d) * psc(h) * psc(w);
97 }
98
99#if NCNN_image_shader
100 ivec4 x4;
101 ivec4 xx4;
102 ivec4 y4;
103 ivec4 yy4;
104 ivec4 z4;
105 ivec4 zz4;
106
107 if (ndim == 1)
108 {
109 z4 = ivec4(0);
110 y4 = ivec4(0);
111 x4 = i4;
112 zz4 = ivec4(0);
113 yy4 = ivec4(0);
114 xx4 = ii4;
115 }
116 if (ndim == 2)
117 {
118 z4 = ivec4(0);
119 y4 = i4 / psc(outw);
120 x4 = i4 % psc(outw);
121 zz4 = ivec4(0);
122 yy4 = ii4 / psc(outw);
123 xx4 = ii4 % psc(outw);
124 }
125 if (ndim == 3)
126 {
127 int size = psc(outw) * psc(outh);
128
129 z4 = i4 / size;
130 y4 = i4 % size / psc(outw);
131 x4 = i4 % size % psc(outw);
132 zz4 = ii4 / size;
133 yy4 = ii4 % size / psc(outw);
134 xx4 = ii4 % size % psc(outw);
135 }
136 if (ndim == 4)
137 {
138 int size = psc(outw) * psc(outh) * psc(outd);
139 int dsize = psc(outw) * psc(outh);
140
141 z4 = i4 / size;
142 ivec4 yd4 = i4 % size / dsize;
143 ivec4 yh4 = i4 % size % dsize / psc(outw);
144 x4 = i4 % size % dsize % psc(outw);
145 zz4 = ii4 / size;
146 ivec4 yyd4 = ii4 % size / dsize;
147 ivec4 yyh4 = ii4 % size % dsize / psc(outw);
148 xx4 = ii4 % size % dsize % psc(outw);
149
150 y4 = yd4 * psc(outh) + yh4;
151 yy4 = yyd4 * psc(outh) + yyh4;
152 }
153
154 afpvec8 v;
155 if (psc(dims) == 1)
156 {
157 v = image3d_ld8(bottom_blob_3d, ivec3(gx, 0, 0));
158 }
159 else if (psc(dims) == 2)
160 {
161 v = image3d_ld8(bottom_blob_3d, ivec3(gx, gy, 0));
162 }
163 else // if (psc(dims) == 3 || psc(dims) == 4)
164 {
165 v = image3d_ld8(bottom_blob_3d, ivec3(gx, gy, gz));
166 }
167
168 image3d_st1(top_blob_3d, ivec3(x4.r, y4.r, z4.r), v[0].r);
169 image3d_st1(top_blob_3d, ivec3(x4.g, y4.g, z4.g), v[0].g);
170 image3d_st1(top_blob_3d, ivec3(x4.b, y4.b, z4.b), v[0].b);
171 image3d_st1(top_blob_3d, ivec3(x4.a, y4.a, z4.a), v[0].a);
172 image3d_st1(top_blob_3d, ivec3(xx4.r, yy4.r, zz4.r), v[1].r);
173 image3d_st1(top_blob_3d, ivec3(xx4.g, yy4.g, zz4.g), v[1].g);
174 image3d_st1(top_blob_3d, ivec3(xx4.b, yy4.b, zz4.b), v[1].b);
175 image3d_st1(top_blob_3d, ivec3(xx4.a, yy4.a, zz4.a), v[1].a);
176#else
177 ivec4 v_offset;
178 ivec4 vv_offset;
179
180 if (ndim == 1)
181 {
182 v_offset = i4;
183 vv_offset = ii4;
184 }
185 if (ndim == 2)
186 {
187 ivec4 y4 = i4 / psc(outw);
188 ivec4 x4 = i4 % psc(outw);
189 ivec4 yy4 = ii4 / psc(outw);
190 ivec4 xx4 = ii4 % psc(outw);
191
192 v_offset = y4 * psc(outw) + x4;
193 vv_offset = yy4 * psc(outw) + xx4;
194 }
195 if (ndim == 3)
196 {
197 int size = psc(outw) * psc(outh);
198
199 ivec4 z4 = i4 / size;
200 ivec4 y4 = i4 % size / psc(outw);
201 ivec4 x4 = i4 % size % psc(outw);
202 ivec4 zz4 = ii4 / size;
203 ivec4 yy4 = ii4 % size / psc(outw);
204 ivec4 xx4 = ii4 % size % psc(outw);
205
206 v_offset = z4 * psc(outcstep) + y4 * psc(outw) + x4;
207 vv_offset = zz4 * psc(outcstep) + yy4 * psc(outw) + xx4;
208 }
209 if (ndim == 4)
210 {
211 int size = psc(outw) * psc(outh) * psc(outd);
212 int dsize = psc(outw) * psc(outh);
213
214 ivec4 z4 = i4 / size;
215 ivec4 yd4 = i4 % size / dsize;
216 ivec4 yh4 = i4 % size % dsize / psc(outw);
217 ivec4 x4 = i4 % size % dsize % psc(outw);
218 ivec4 zz4 = ii4 / size;
219 ivec4 yyd4 = ii4 % size / dsize;
220 ivec4 yyh4 = ii4 % size % dsize / psc(outw);
221 ivec4 xx4 = ii4 % size % dsize % psc(outw);
222
223 ivec4 y4 = yd4 * psc(outh) + yh4;
224 ivec4 yy4 = yyd4 * psc(outh) + yyh4;
225
226 v_offset = z4 * psc(outcstep) + y4 * psc(outw) + x4;
227 vv_offset = zz4 * psc(outcstep) + yy4 * psc(outw) + xx4;
228 }
229
230 int gi = gz * psc(cstep) + gy * psc(w) + gx;
231
232 buffer_cp8to1(top_blob_data, v_offset, vv_offset, bottom_blob_data, gi);
233#endif
234}