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) 2019 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
19#endif
20#if NCNN_fp16_arithmetic
21#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
22#endif
23
24layout (constant_id = 0) const int resize_type = 0;
25layout (constant_id = 1) const int align_corner = 0;
26
27#define shape_constant_id_offset 2
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 c = 0;
32layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;
33
34layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0;
35layout (constant_id = shape_constant_id_offset + 6) const int outw = 0;
36layout (constant_id = shape_constant_id_offset + 7) const int outh = 0;
37layout (constant_id = shape_constant_id_offset + 8) const int outc = 0;
38layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0;
39
40#if NCNN_image_shader
41layout (binding = 0) uniform unfp sampler3D bottom_blob;
42layout (binding = 1, imfmtc1) writeonly uniform unfp image3D top_blob;
43#else
44layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
45layout (binding = 1) writeonly buffer top_blob { sfp top_blob_data[]; };
46#endif
47
48layout (push_constant) uniform parameter
49{
50 int dims;
51 int w;
52 int h;
53 int c;
54 int cstep;
55
56 int outdims;
57 int outw;
58 int outh;
59 int outc;
60 int outcstep;
61
62 float scale_x;
63 float scale_y;
64} p;
65
66void main()
67{
68 int gx = int(gl_GlobalInvocationID.x);
69 int gy = int(gl_GlobalInvocationID.y);
70 int gz = int(gl_GlobalInvocationID.z);
71
72 if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
73 return;
74
75 if (psc(dims) == 1)
76 {
77#if NCNN_image_shader
78 image3d_cp1(top_blob, ivec3(gx, gy, gz), bottom_blob, ivec3(gz, 0, 0));
79#else
80 const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
81
82 buffer_cp1(top_blob_data, gi, bottom_blob_data, gz);
83#endif
84
85 return;
86 }
87
88 if (psc(dims) == 2)
89 {
90 if (resize_type == 1) // nearest
91 {
92 int sx = min(int(floor(afp(gx) * afp(p.scale_x))), psc(w) - 1);
93
94#if NCNN_image_shader
95 image3d_cp1(top_blob, ivec3(gx, gy, gz), bottom_blob, ivec3(sx, gy, gz));
96#else
97 int v_offset = gz * psc(cstep) + gy * psc(w) + sx;
98
99 const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
100
101 buffer_cp1(top_blob_data, gi, bottom_blob_data, v_offset);
102#endif
103 }
104 if (resize_type == 2) // bilinear
105 {
106 afp fx;
107 if (align_corner == 1)
108 {
109 fx = afp(gx) * afp(p.scale_x);
110 }
111 else
112 {
113 fx = (afp(gx) + afp(0.5f)) * afp(p.scale_x) - afp(0.5f);
114 }
115
116 int sx = int(floor(fx));
117
118 fx -= afp(sx);
119
120 int sx_max = psc(w) - 2;
121
122 if (sx < 0)
123 {
124 sx = 0;
125 fx = afp(0.f);
126 }
127 else if (sx > sx_max)
128 {
129 sx = sx_max;
130 fx = afp(1.f);
131 }
132
133#if NCNN_image_shader
134 afp a0 = image3d_ld1(bottom_blob, ivec3(sx, gy, gz));
135 afp a1 = image3d_ld1(bottom_blob, ivec3(sx + 1, gy, gz));
136#else
137 int v_offset_0 = gz * psc(cstep) + gy * psc(w) + sx;
138
139 afp a0 = buffer_ld1(bottom_blob_data, v_offset_0);
140 afp a1 = buffer_ld1(bottom_blob_data, v_offset_0 + 1);
141#endif
142
143 afp res = a0 * (afp(1.f) - fx) + a1 * fx;
144
145#if NCNN_image_shader
146 image3d_st1(top_blob, ivec3(gx, gy, gz), res);
147#else
148 const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
149
150 buffer_st1(top_blob_data, gi, res);
151#endif
152 }
153
154 return;
155 }
156
157 if (resize_type == 1) // nearest
158 {
159 afpvec2 gxy = afpvec2(gx, gy);
160 ivec2 sxy_max = ivec2(psc(w) - 1, psc(h) - 1);
161 ivec2 sxy = min(ivec2(floor(gxy * afpvec2(p.scale_x, p.scale_y))), sxy_max);
162
163 int sx = sxy.r;
164 int sy = sxy.g;
165
166#if NCNN_image_shader
167 image3d_cp1(top_blob, ivec3(gx, gy, gz), bottom_blob, ivec3(sx, sy, gz));
168#else
169 int v_offset = gz * psc(cstep) + sy * psc(w) + sx;
170
171 const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
172
173 buffer_cp1(top_blob_data, gi, bottom_blob_data, v_offset);
174#endif
175 }
176 if (resize_type == 2) // bilinear
177 {
178 afpvec2 gxy = afpvec2(gx, gy);
179 afpvec2 fxy;
180 if (align_corner == 1)
181 {
182 fxy = gxy * afpvec2(p.scale_x, p.scale_y);
183 }
184 else
185 {
186 fxy = (gxy + afp(0.5f)) * afpvec2(p.scale_x, p.scale_y) - afp(0.5f);
187 }
188
189 ivec2 sxy = ivec2(floor(fxy));
190
191 fxy -= afpvec2(sxy);
192
193 ivec2 sxy_max = ivec2(psc(w) - 2, psc(h) - 2);
194
195 bvec2 underflow = lessThan(sxy, ivec2(0));
196 bvec2 overflow = greaterThan(sxy, sxy_max);
197
198 sxy = clamp(sxy, ivec2(0), sxy_max);
199
200 fxy = mix(fxy, afpvec2(0.f), underflow);
201 fxy = mix(fxy, afpvec2(1.f), overflow);
202
203 int sx = sxy.r;
204 int sy = sxy.g;
205
206#if NCNN_image_shader
207 afp a0 = image3d_ld1(bottom_blob, ivec3(sx, sy, gz));
208 afp a1 = image3d_ld1(bottom_blob, ivec3(sx + 1, sy, gz));
209 afp b0 = image3d_ld1(bottom_blob, ivec3(sx, sy + 1, gz));
210 afp b1 = image3d_ld1(bottom_blob, ivec3(sx + 1, sy + 1, gz));
211#else
212 int v_offset_0 = gz * psc(cstep) + sy * psc(w) + sx;
213 int v_offset_1 = gz * psc(cstep) + (sy + 1) * psc(w) + sx;
214
215 afp a0 = buffer_ld1(bottom_blob_data, v_offset_0);
216 afp a1 = buffer_ld1(bottom_blob_data, v_offset_0 + 1);
217 afp b0 = buffer_ld1(bottom_blob_data, v_offset_1);
218 afp b1 = buffer_ld1(bottom_blob_data, v_offset_1 + 1);
219#endif
220
221 afp fx = fxy.r;
222 afp fy = fxy.g;
223
224 afpvec2 ab = afpvec2(a0, b0) * (afp(1.f) - fx) + afpvec2(a1, b1) * fx;
225
226 afp res = ab.r * (afp(1.f) - fy) + ab.g * fy;
227
228#if NCNN_image_shader
229 image3d_st1(top_blob, ivec3(gx, gy, gz), res);
230#else
231 const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
232
233 buffer_st1(top_blob_data, gi, res);
234#endif
235 }
236}