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 kernel_w = 1;
25layout (constant_id = 1) const int kernel_h = 1;
26layout (constant_id = 2) const int dilation_w = 1;
27layout (constant_id = 3) const int dilation_h = 1;
28layout (constant_id = 4) const int stride_w = 1;
29layout (constant_id = 5) const int stride_h = 1;
30layout (constant_id = 6) const int bias_term = 0;
31layout (constant_id = 7) const int activation_type = 0;
32layout (constant_id = 8) const float activation_param_0 = 0;
33layout (constant_id = 9) const float activation_param_1 = 0;
34
35#define shape_constant_id_offset 10
36layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
37layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
38layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
39layout (constant_id = shape_constant_id_offset + 3) const int c = 0;
40layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;
41
42layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0;
43layout (constant_id = shape_constant_id_offset + 6) const int outw = 0;
44layout (constant_id = shape_constant_id_offset + 7) const int outh = 0;
45layout (constant_id = shape_constant_id_offset + 8) const int outc = 0;
46layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0;
47
48#if NCNN_image_shader
49layout (binding = 0) uniform unfp sampler3D bottom_blob;
50layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
51layout (binding = 2) uniform unfp sampler3D weight_blob;
52layout (binding = 3) uniform unfp sampler3D bias_blob;
53#else
54layout (binding = 0) readonly buffer bottom_blob { sfpvec4 bottom_blob_data[]; };
55layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
56#if NCNN_fp16_packed || (NCNN_fp16_storage && !NCNN_fp16_arithmetic)
57// GL_EXT_shader_16bit_storage does not define f16mat4 type :(
58layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; };
59#else
60layout (binding = 2) readonly buffer weight_blob { sfpmat4 weight_data[]; };
61#endif
62layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; };
63#endif
64
65layout (push_constant) uniform parameter
66{
67    int dims;
68    int w;
69    int h;
70    int c;
71    int cstep;
72
73    int outdims;
74    int outw;
75    int outh;
76    int outc;
77    int outcstep;
78} p;
79
80void main()
81{
82    int gx = int(gl_GlobalInvocationID.x);
83    int gy = int(gl_GlobalInvocationID.y);
84    int gz = int(gl_GlobalInvocationID.z);
85
86    if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
87        return;
88
89    afpvec4 sum;
90
91    if (bias_term == 1)
92    {
93#if NCNN_image_shader
94        sum = image3d_ld4(bias_blob, ivec3(gz, 0, 0));
95#else
96        sum = buffer_ld4(bias_data, gz);
97#endif
98    }
99    else
100    {
101        sum = afpvec4(0.f);
102    }
103
104    const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
105    const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
106
107#if NCNN_image_shader
108    for (int y = 0; y < kernel_h; y++)
109    {
110        int sys = (gy + y * dilation_h - (kernel_extent_h - 1));
111        if (sys < 0 || sys % stride_h != 0)
112            continue;
113
114        int sy = sys / stride_h;
115        if (sy >= psc(h))
116            continue;
117
118        for (int x = 0; x < kernel_w; x++)
119        {
120            int sxs = (gx + x * dilation_w - (kernel_extent_w - 1));
121            if (sxs < 0 || sxs % stride_w != 0)
122                continue;
123
124            int sx = sxs / stride_w;
125            if (sx >= psc(w))
126                continue;
127
128            int wx = (y * kernel_w + x) * 4;
129
130            for (int z = 0; z < psc(c); z++)
131            {
132                afpvec4 v = image3d_ld4(bottom_blob, ivec3(sx, sy, z));
133
134                afpmat4 k = afpmat4(
135                    image3d_ld4(weight_blob, ivec3(wx + 0, z, gz)),
136                    image3d_ld4(weight_blob, ivec3(wx + 1, z, gz)),
137                    image3d_ld4(weight_blob, ivec3(wx + 2, z, gz)),
138                    image3d_ld4(weight_blob, ivec3(wx + 3, z, gz))
139                );
140
141                sum += v * k;
142            }
143        }
144    }
145#else
146    int w_offset_0 = gz * psc(c) * kernel_w * kernel_h;
147
148    for (int y = 0; y < kernel_h; y++)
149    {
150        int sys = (gy + y * dilation_h - (kernel_extent_h - 1));
151        if (sys < 0 || sys % stride_h != 0)
152            continue;
153
154        int sy = sys / stride_h;
155        if (sy >= psc(h))
156            continue;
157
158        for (int x = 0; x < kernel_w; x++)
159        {
160            int sxs = (gx + x * dilation_w - (kernel_extent_w - 1));
161            if (sxs < 0 || sxs % stride_w != 0)
162                continue;
163
164            int sx = sxs / stride_w;
165            if (sx >= psc(w))
166                continue;
167
168            int v_offset = sy * psc(w) + sx;
169            int w_offset = w_offset_0 + y * kernel_w + x;
170
171            for (int z = 0; z < psc(c); z++)
172            {
173                afpvec4 v = buffer_ld4(bottom_blob_data, v_offset);
174
175#if NCNN_fp16_packed || (NCNN_fp16_storage && !NCNN_fp16_arithmetic)
176                // GL_EXT_shader_16bit_storage does not define f16mat4 type :(
177                afpmat4 k = afpmat4(
178                    buffer_ld4(weight_data, w_offset * 4 + 0),
179                    buffer_ld4(weight_data, w_offset * 4 + 1),
180                    buffer_ld4(weight_data, w_offset * 4 + 2),
181                    buffer_ld4(weight_data, w_offset * 4 + 3)
182                );
183#else
184                afpmat4 k = afpmat4(weight_data[w_offset]);
185#endif
186
187                sum += v * k;
188
189                v_offset += psc(cstep);
190                w_offset += kernel_w * kernel_h;
191            }
192        }
193    }
194#endif
195
196    if (activation_type == 1)
197    {
198        sum = max(sum, afp(0.f));
199    }
200    if (activation_type == 2)
201    {
202        const afp slope = afp(activation_param_0);
203        sum = mix(sum, sum * afp(slope), lessThan(sum, afpvec4(0.f)));
204    }
205    if (activation_type == 3)
206    {
207        const afp const_min = afp(activation_param_0);
208        const afp const_max = afp(activation_param_1);
209        sum = clamp(sum, const_min, const_max);
210    }
211    if (activation_type == 4)
212    {
213        sum = afp(1.f) / (afp(1.f) + exp(-sum));
214    }
215
216#if NCNN_image_shader
217    image3d_st4(top_blob, ivec3(gx, gy, gz), sum);
218#else
219    const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
220
221    buffer_st4(top_blob_data, gi, sum);
222#endif
223}
224