Elffuss BraidHub

kikocis/ncnn public

master · 6.4 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 axis = 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 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_top_blob_3d;
42layout (binding = 1, imfmtc4) writeonly uniform unfp image3D sum_workspace_3d;
43#else
44layout (binding = 0) readonly buffer bottom_top_blob { sfpvec8 bottom_top_blob_data[]; };
45layout (binding = 1) writeonly buffer sum_workspace { sfpvec8 sum_workspace_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} p;
62
63void main()
64{
65 int gx = int(gl_GlobalInvocationID.x);
66 int gy = int(gl_GlobalInvocationID.y);
67 int gz = int(gl_GlobalInvocationID.z);
68
69 if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
70 return;
71
72 int positive_axis = axis < 0 ? psc(dims) + axis : axis;
73 afpvec8 sum_value = afpvec8(afpvec4(0.f), afpvec4(0.f));
74
75 if (psc(dims) == 1) // positive_axis == 0
76 {
77 for (int i = 0; i < psc(w); i++)
78 {
79#if NCNN_image_shader
80 afpvec8 v = image3d_ld8(bottom_top_blob_3d, ivec3(i, 0, 0));
81#else
82 afpvec8 v = buffer_ld8(bottom_top_blob_data, i);
83#endif
84 sum_value += v;
85 }
86 afpvec4 sum4 = sum_value[0] + sum_value[1];
87 afpvec2 sum2 = sum4.rg + sum4.ba;
88 afp sum1 = sum2.r + sum2.g;
89 sum_value = afpvec8(afpvec4(sum1), afpvec4(sum1));
90#if NCNN_image_shader
91 image3d_st8(sum_workspace_3d, ivec3(0, 0, 0), sum_value);
92#else
93 buffer_st8(sum_workspace_data, 0, sum_value);
94#endif
95 return;
96 }
97
98 if (psc(dims) == 2 && positive_axis == 0)
99 {
100 for (int i = 0; i < psc(h); i++)
101 {
102#if NCNN_image_shader
103 afpvec8 v = image3d_ld8(bottom_top_blob_3d, ivec3(gx, i, 0));
104#else
105 int v_offset = i * psc(w) + gx;
106 afpvec8 v = buffer_ld8(bottom_top_blob_data, v_offset);
107#endif
108 sum_value += v;
109 }
110 afpvec4 sum4 = sum_value[0] + sum_value[1];
111 afpvec2 sum2 = sum4.rg + sum4.ba;
112 afp sum1 = sum2.r + sum2.g;
113 sum_value = afpvec8(afpvec4(sum1), afpvec4(sum1));
114#if NCNN_image_shader
115 image3d_st8(sum_workspace_3d, ivec3(gx, 0, 0), sum_value);
116#else
117 buffer_st8(sum_workspace_data, gx, sum_value);
118#endif
119 return;
120 }
121
122 if (psc(dims) == 2 && positive_axis == 1)
123 {
124 for (int i = 0; i < psc(w); i++)
125 {
126#if NCNN_image_shader
127 afpvec8 v = image3d_ld8(bottom_top_blob_3d, ivec3(i, gx, 0));
128#else
129 int v_offset = gx * psc(w) + i;
130 afpvec8 v = buffer_ld8(bottom_top_blob_data, v_offset);
131#endif
132 sum_value += v;
133 }
134#if NCNN_image_shader
135 image3d_st8(sum_workspace_3d, ivec3(gx, 0, 0), sum_value);
136#else
137 buffer_st8(sum_workspace_data, gx, sum_value);
138#endif
139 return;
140 }
141
142 if (psc(dims) == 3 && positive_axis == 0)
143 {
144 for (int i = 0; i < psc(c); i++)
145 {
146#if NCNN_image_shader
147 afpvec8 v = image3d_ld8(bottom_top_blob_3d, ivec3(gx, gy, i));
148#else
149 int v_offset = i * psc(cstep) + gy * psc(w) + gx;
150 afpvec8 v = buffer_ld8(bottom_top_blob_data, v_offset);
151#endif
152 sum_value += v;
153 }
154 afpvec4 sum4 = sum_value[0] + sum_value[1];
155 afpvec2 sum2 = sum4.rg + sum4.ba;
156 afp sum1 = sum2.r + sum2.g;
157 sum_value = afpvec8(afpvec4(sum1), afpvec4(sum1));
158#if NCNN_image_shader
159 image3d_st8(sum_workspace_3d, ivec3(gx, gy, 0), sum_value);
160#else
161 buffer_st8(sum_workspace_data, gy * psc(w) + gx, sum_value);
162#endif
163 return;
164 }
165
166 if (psc(dims) == 3 && positive_axis == 1)
167 {
168 for (int i = 0; i < psc(h); i++)
169 {
170#if NCNN_image_shader
171 afpvec8 v = image3d_ld8(bottom_top_blob_3d, ivec3(gx, i, gy));
172#else
173 int v_offset = gy * psc(cstep) + i * psc(w) + gx;
174 afpvec8 v = buffer_ld8(bottom_top_blob_data, v_offset);
175#endif
176 sum_value += v;
177 }
178#if NCNN_image_shader
179 image3d_st8(sum_workspace_3d, ivec3(gx, gy, 0), sum_value);
180#else
181 buffer_st8(sum_workspace_data, gy * psc(w) + gx, sum_value);
182#endif
183 return;
184 }
185
186 if (psc(dims) == 3 && positive_axis == 2)
187 {
188 for (int i = 0; i < psc(w); i++)
189 {
190#if NCNN_image_shader
191 afpvec8 v = image3d_ld8(bottom_top_blob_3d, ivec3(i, gx, gy));
192#else
193 int v_offset = gy * psc(cstep) + gx * psc(w) + i;
194 afpvec8 v = buffer_ld8(bottom_top_blob_data, v_offset);
195#endif
196 sum_value += v;
197 }
198#if NCNN_image_shader
199 image3d_st8(sum_workspace_3d, ivec3(gx, gy, 0), sum_value);
200#else
201 buffer_st8(sum_workspace_data, gy * psc(h) + gx, sum_value);
202#endif
203 return;
204 }
205}