1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
18
#extension GL_EXT_shader_16bit_storage: require
20
#if NCNN_fp16_arithmetic
21
#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
24
#extension GL_GOOGLE_include_directive: enable
25
#include "vulkan_activation.comp"
27
layout (constant_id = 0) const int kernel_w = 1;
28
layout (constant_id = 1) const int kernel_h = 1;
29
layout (constant_id = 2) const int dilation_w = 1;
30
layout (constant_id = 3) const int dilation_h = 1;
31
layout (constant_id = 4) const int stride_w = 1;
32
layout (constant_id = 5) const int stride_h = 1;
33
layout (constant_id = 6) const int bias_term = 0;
34
layout (constant_id = 7) const int activation_type = 0;
35
layout (constant_id = 8) const float activation_param_0 = 0;
36
layout (constant_id = 9) const float activation_param_1 = 0;
38
#define shape_constant_id_offset 10
39
layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
40
layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
41
layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
42
layout (constant_id = shape_constant_id_offset + 3) const int c = 0;
43
layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;
45
layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0;
46
layout (constant_id = shape_constant_id_offset + 6) const int outw = 0;
47
layout (constant_id = shape_constant_id_offset + 7) const int outh = 0;
48
layout (constant_id = shape_constant_id_offset + 8) const int outc = 0;
49
layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0;
52
layout (binding = 0) uniform unfp sampler3D bottom_blob;
53
layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
54
layout (binding = 2) uniform unfp sampler3D weight_blob;
55
layout (binding = 3) uniform unfp sampler3D bias_blob;
57
layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
58
layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
59
layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; };
60
layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; };
63
layout (push_constant) uniform parameter
80
int gx = int(gl_GlobalInvocationID.x) * 2;
81
int gy = int(gl_GlobalInvocationID.y) * 2;
82
int gz = int(gl_GlobalInvocationID.z) * 2;
84
if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
87
const ivec2 gx2 = gx + ivec2(0, 1);
88
const ivec2 gy2 = gy + ivec2(0, 1);
89
const ivec2 gz2 = gz + ivec2(0, 1);
103
sum0 = image3d_ld4(bias_blob, ivec3(gz2.x, 0, 0));
104
sum4 = image3d_ld4(bias_blob, ivec3(gz2.y, 0, 0));
106
sum0 = buffer_ld4(bias_data, gz2.x);
107
sum4 = buffer_ld4(bias_data, gz2.y);
129
for (int z = 0; z < psc(c); z++)
131
ivec2 sy = gy2 * stride_h;
134
for (int y = 0; y < kernel_h; y++)
136
ivec2 sx = gx2 * stride_w;
138
for (int x = 0; x < kernel_w; x++)
140
afp v0 = image3d_ld1(bottom_blob, ivec3(sx.x, sy.x, z));
141
afp v1 = image3d_ld1(bottom_blob, ivec3(sx.y, sy.x, z));
142
afp v2 = image3d_ld1(bottom_blob, ivec3(sx.x, sy.y, z));
143
afp v3 = image3d_ld1(bottom_blob, ivec3(sx.y, sy.y, z));
145
afpvec4 k0 = image3d_ld4(weight_blob, ivec3(wx, z, gz2.x));
146
afpvec4 k1 = image3d_ld4(weight_blob, ivec3(wx, z, gz2.y));
165
ivec2 w_offset = gz2 * psc(c) * kernel_w * kernel_h;
167
for (int z = 0; z < psc(c); z++)
170
v_offset.rg = z * psc(cstep) + gy2.x * stride_h * psc(w) + gx2 * stride_w;
171
v_offset.ba = z * psc(cstep) + gy2.y * stride_h * psc(w) + gx2 * stride_w;
173
for (int y = 0; y < kernel_h; y++)
175
for (int x = 0; x < kernel_w; x++)
177
afp v0 = buffer_ld1(bottom_blob_data, v_offset.r + x * dilation_w);
178
afp v1 = buffer_ld1(bottom_blob_data, v_offset.g + x * dilation_w);
179
afp v2 = buffer_ld1(bottom_blob_data, v_offset.b + x * dilation_w);
180
afp v3 = buffer_ld1(bottom_blob_data, v_offset.a + x * dilation_w);
182
afpvec4 k0 = buffer_ld4(weight_data, w_offset.x + x);
183
afpvec4 k1 = buffer_ld4(weight_data, w_offset.y + x);
195
v_offset += dilation_h * psc(w);
196
w_offset += kernel_w;
201
sum0 = activation_afpvec4(sum0, activation_type, activation_param_0, activation_param_1);
202
sum1 = activation_afpvec4(sum1, activation_type, activation_param_0, activation_param_1);
203
sum2 = activation_afpvec4(sum2, activation_type, activation_param_0, activation_param_1);
204
sum3 = activation_afpvec4(sum3, activation_type, activation_param_0, activation_param_1);
205
sum4 = activation_afpvec4(sum4, activation_type, activation_param_0, activation_param_1);
206
sum5 = activation_afpvec4(sum5, activation_type, activation_param_0, activation_param_1);
207
sum6 = activation_afpvec4(sum6, activation_type, activation_param_0, activation_param_1);
208
sum7 = activation_afpvec4(sum7, activation_type, activation_param_0, activation_param_1);
211
image3d_st4(top_blob, ivec3(gx2.x, gy2.x, gz2.x), sum0);
212
image3d_st4(top_blob, ivec3(gx2.y, gy2.x, gz2.x), sum1);
213
image3d_st4(top_blob, ivec3(gx2.x, gy2.y, gz2.x), sum2);
214
image3d_st4(top_blob, ivec3(gx2.y, gy2.y, gz2.x), sum3);
215
image3d_st4(top_blob, ivec3(gx2.x, gy2.x, gz2.y), sum4);
216
image3d_st4(top_blob, ivec3(gx2.y, gy2.x, gz2.y), sum5);
217
image3d_st4(top_blob, ivec3(gx2.x, gy2.y, gz2.y), sum6);
218
image3d_st4(top_blob, ivec3(gx2.y, gy2.y, gz2.y), sum7);
220
const ivec2 gi = gz2 * psc(outcstep) + gy * psc(outw) + gx;
222
buffer_st4(top_blob_data, gi.x, sum0);
223
if (gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi.x + 1, sum1);
224
if (gy + 1 < psc(outh)) buffer_st4(top_blob_data, gi.x + psc(outw), sum2);
225
if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi.x + psc(outw) + 1, sum3);
226
if (gz + 1 < psc(outc))
228
buffer_st4(top_blob_data, gi.y, sum4);
229
if (gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi.y + 1, sum5);
230
if (gy + 1 < psc(outh)) buffer_st4(top_blob_data, gi.y + psc(outw), sum6);
231
if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi.y + psc(outw) + 1, sum7);