Elffuss BraidHub

kikocis/ncnn public

master · 5.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 op_type = 0;
26layout (constant_id = 1) const int with_scalar = 0;
27layout (constant_id = 2) const float const_b = 0;
28
29#define shape_constant_id_offset 3
30layout (constant_id = shape_constant_id_offset + 0) const int adims = 0;
31layout (constant_id = shape_constant_id_offset + 1) const int aw = 0;
32layout (constant_id = shape_constant_id_offset + 2) const int ah = 0;
33layout (constant_id = shape_constant_id_offset + 3) const int ac = 0;
34layout (constant_id = shape_constant_id_offset + 4) const int acstep = 0;
35
36layout (constant_id = shape_constant_id_offset + 5) const int bdims = 0;
37layout (constant_id = shape_constant_id_offset + 6) const int bw = 0;
38layout (constant_id = shape_constant_id_offset + 7) const int bh = 0;
39layout (constant_id = shape_constant_id_offset + 8) const int bc = 0;
40layout (constant_id = shape_constant_id_offset + 9) const int bcstep = 0;
41
42layout (constant_id = shape_constant_id_offset + 10) const int outdims = 0;
43layout (constant_id = shape_constant_id_offset + 11) const int outw = 0;
44layout (constant_id = shape_constant_id_offset + 12) const int outh = 0;
45layout (constant_id = shape_constant_id_offset + 13) const int outc = 0;
46layout (constant_id = shape_constant_id_offset + 14) const int outcstep = 0;
47
48#if NCNN_image_shader
49layout (binding = 0) uniform unfp sampler3D a_blob_3d;
50layout (binding = 1) uniform unfp sampler3D b_blob_3d;
51layout (binding = 2, imfmtc4) writeonly uniform unfp image3D top_blob_3d;
52#else
53layout (binding = 0) buffer a_blob { sfpvec8 a_blob_data[]; };
54layout (binding = 1) readonly buffer b_blob { sfpvec8 b_blob_data[]; };
55layout (binding = 2) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };
56#endif
57
58layout (push_constant) uniform parameter
59{
60 int adims;
61 int aw;
62 int ah;
63 int ac;
64 int acstep;
65
66 int bdims;
67 int bw;
68 int bh;
69 int bc;
70 int bcstep;
71
72 int outdims;
73 int outw;
74 int outh;
75 int outc;
76 int outcstep;
77} p;
78
79void main()
80{
81 int gx = int(gl_GlobalInvocationID.x);
82 int gy = int(gl_GlobalInvocationID.y);
83 int gz = int(gl_GlobalInvocationID.z);
84
85 if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
86 return;
87
88#if NCNN_image_shader
89 afpvec8 v1 = image3d_ld8(a_blob_3d, ivec3(gx, gy, gz));
90#else
91 const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
92
93 afpvec8 v1 = buffer_ld8(a_blob_data, gi);
94#endif
95
96 afpvec8 v2;
97
98 if (with_scalar == 1)
99 {
100 // scalar
101 v2[0] = afpvec4(afp(const_b));
102 v2[1] = afpvec4(afp(const_b));
103 }
104 else
105 {
106 // no broadcast
107#if NCNN_image_shader
108 v2 = image3d_ld8(b_blob_3d, ivec3(gx, gy, gz));
109#else
110 v2 = buffer_ld8(b_blob_data, gi);
111#endif
112 }
113
114 afpvec8 res;
115
116 if (op_type == 0)
117 {
118 res[0] = v1[0] + v2[0];
119 res[1] = v1[1] + v2[1];
120 }
121 if (op_type == 1)
122 {
123 res[0] = v1[0] - v2[0];
124 res[1] = v1[1] - v2[1];
125 }
126 if (op_type == 2)
127 {
128 res[0] = v1[0] * v2[0];
129 res[1] = v1[1] * v2[1];
130 }
131 if (op_type == 3)
132 {
133 res[0] = v1[0] / v2[0];
134 res[1] = v1[1] / v2[1];
135 }
136 if (op_type == 4)
137 {
138 res[0] = max(v1[0], v2[0]);
139 res[1] = max(v1[1], v2[1]);
140 }
141 if (op_type == 5)
142 {
143 res[0] = min(v1[0], v2[0]);
144 res[1] = min(v1[1], v2[1]);
145 }
146 if (op_type == 6)
147 {
148 res[0] = pow(v1[0], v2[0]);
149 res[1] = pow(v1[1], v2[1]);
150 }
151 if (op_type == 7)
152 {
153 res[0] = v2[0] - v1[0];
154 res[1] = v2[1] - v1[1];
155 }
156 if (op_type == 8)
157 {
158 res[0] = v2[0] / v1[0];
159 res[1] = v2[1] / v1[1];
160 }
161 if (op_type == 9)
162 {
163 res[0] = pow(v2[0], v1[0]);
164 res[1] = pow(v2[1], v1[1]);
165 }
166 if (op_type == 10)
167 {
168#if NCNN_moltenvk
169 res[0] = afpvec4(atan(vec4(v1[0]), vec4(v2[0])));
170 res[1] = afpvec4(atan(vec4(v1[1]), vec4(v2[1])));
171#else
172 res[0] = atan(v1[0], v2[0]);
173 res[1] = atan(v1[1], v2[1]);
174#endif
175 }
176 if (op_type == 11)
177 {
178#if NCNN_moltenvk
179 res[0] = afpvec4(atan(vec4(v2[0]), vec4(v1[0])));
180 res[1] = afpvec4(atan(vec4(v2[1]), vec4(v1[1])));
181#else
182 res[0] = atan(v2[0], v1[0]);
183 res[1] = atan(v2[1], v1[1]);
184#endif
185 }
186
187#if NCNN_image_shader
188 image3d_st8(top_blob_3d, ivec3(gx, gy, gz), res);
189#else
190 buffer_st8(top_blob_data, gi, res);
191#endif
192}