Elffuss BraidHub

kikocis/ncnn public

master · 4.4 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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 maxk = 1;
26
27#define shape_constant_id_offset 1
28layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
29layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
30layout (constant_id = shape_constant_id_offset + 2) const int c = 0;
31layout (constant_id = shape_constant_id_offset + 3) const int cstep = 0;
32
33layout (constant_id = shape_constant_id_offset + 4) const int outw = 0;
34layout (constant_id = shape_constant_id_offset + 5) const int outh = 0;
35
36#if NCNN_image_shader
37layout (binding = 0) uniform unfp sampler3D bottom_blob;
38layout (binding = 1, imfmtc4) writeonly uniform unfp image3D col_blob;
39layout (binding = 2) uniform unfp sampler3D weight_blob;
40#else
41layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
42layout (binding = 1) writeonly buffer col_blob { sfpvec8 col_blob_data[]; };
43layout (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; };
44#endif
45
46layout (push_constant) uniform parameter
47{
48 int w;
49 int h;
50 int c;
51 int cstep;
52
53 int outw;
54 int outh;
55} p;
56
57void main()
58{
59 int gx = int(gl_GlobalInvocationID.x) * 4;
60 int gy = int(gl_GlobalInvocationID.y);
61
62 if (gx >= psc(outw) || gy >= psc(outh))
63 return;
64
65 afpvec8 sum0 = afpvec8(afpvec4(0.f), afpvec4(0.f));
66 afpvec8 sum1 = afpvec8(afpvec4(0.f), afpvec4(0.f));
67 afpvec8 sum2 = afpvec8(afpvec4(0.f), afpvec4(0.f));
68 afpvec8 sum3 = afpvec8(afpvec4(0.f), afpvec4(0.f));
69
70#if NCNN_image_shader
71 ivec4 gx4 = gx + ivec4(0, 1, 2, 3);
72
73 ivec4 sy4 = gx4 / psc(w);
74 ivec4 sx4 = gx4 % psc(w);
75
76 for (int z = 0; z < psc(c); z++)
77 {
78 afp v0 = image3d_ld1(bottom_blob, ivec3(sx4.r, sy4.r, z));
79 afp v1 = image3d_ld1(bottom_blob, ivec3(sx4.g, sy4.g, z));
80 afp v2 = image3d_ld1(bottom_blob, ivec3(sx4.b, sy4.b, z));
81 afp v3 = image3d_ld1(bottom_blob, ivec3(sx4.a, sy4.a, z));
82
83 afpvec8 k = image3d_ld8(weight_blob, ivec3(z, gy, 0));
84
85 // sum += v * k;
86 sum0[0] += v0 * k[0];
87 sum0[1] += v0 * k[1];
88
89 sum1[0] += v1 * k[0];
90 sum1[1] += v1 * k[1];
91
92 sum2[0] += v2 * k[0];
93 sum2[1] += v2 * k[1];
94
95 sum3[0] += v3 * k[0];
96 sum3[1] += v3 * k[1];
97 }
98#else
99 int v_offset = gx;
100 int w_offset = gy * psc(c);
101
102 for (int z = 0; z < psc(c); z++)
103 {
104 afp v0 = buffer_ld1(bottom_blob_data, v_offset + 0);
105 afp v1 = buffer_ld1(bottom_blob_data, v_offset + 1);
106 afp v2 = buffer_ld1(bottom_blob_data, v_offset + 2);
107 afp v3 = buffer_ld1(bottom_blob_data, v_offset + 3);
108
109 afpvec8 k = buffer_ld8(weight_data, w_offset);
110
111 // sum += v * k;
112 sum0[0] += v0 * k[0];
113 sum0[1] += v0 * k[1];
114
115 sum1[0] += v1 * k[0];
116 sum1[1] += v1 * k[1];
117
118 sum2[0] += v2 * k[0];
119 sum2[1] += v2 * k[1];
120
121 sum3[0] += v3 * k[0];
122 sum3[1] += v3 * k[1];
123
124 v_offset += psc(cstep);
125 w_offset += 1;
126 }
127#endif
128
129#if NCNN_image_shader
130 image3d_st8(col_blob, ivec3(gx4.r, gy, 0), sum0);
131 image3d_st8(col_blob, ivec3(gx4.g, gy, 0), sum1);
132 image3d_st8(col_blob, ivec3(gx4.b, gy, 0), sum2);
133 image3d_st8(col_blob, ivec3(gx4.a, gy, 0), sum3);
134#else
135 const int gi = gy * psc(outw) + gx;
136
137 buffer_st8(col_blob_data, gi, sum0);
138 if (gx + 1 < psc(outw)) buffer_st8(col_blob_data, gi + 1, sum1);
139 if (gx + 2 < psc(outw)) buffer_st8(col_blob_data, gi + 2, sum2);
140 if (gx + 3 < psc(outw)) buffer_st8(col_blob_data, gi + 3, sum3);
141#endif
142}