Elffuss BraidHub

kikocis/ncnn public

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