1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2022 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
19
struct sfpvec8 { f16vec4 abcd; f16vec4 efgh; };
21
#if NCNN_fp16_arithmetic
22
#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
25
#extension GL_GOOGLE_include_directive: enable
26
#include "vulkan_activation.comp"
28
layout (constant_id = 0) const int kernel_w = 1;
29
layout (constant_id = 1) const int kernel_h = 1;
30
layout (constant_id = 2) const int dilation_w = 1;
31
layout (constant_id = 3) const int dilation_h = 1;
32
layout (constant_id = 4) const int stride_w = 1;
33
layout (constant_id = 5) const int stride_h = 1;
34
layout (constant_id = 6) const int bias_term = 0;
35
layout (constant_id = 7) const int activation_type = 0;
36
layout (constant_id = 8) const float activation_param_0 = 0;
37
layout (constant_id = 9) const float activation_param_1 = 0;
39
#define shape_constant_id_offset 10
40
layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
41
layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
43
layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
44
layout (constant_id = shape_constant_id_offset + 3) const int outh = 0;
45
layout (constant_id = shape_constant_id_offset + 4) const int outc = 0;
46
layout (constant_id = shape_constant_id_offset + 5) const int outcstep = 0;
49
layout (binding = 0) uniform unfp sampler3D col_blob;
50
layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
51
layout (binding = 2) uniform unfp sampler3D bias_blob;
53
layout (binding = 0) readonly buffer col_blob { sfpvec8 col_blob_data[]; };
54
layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };
55
layout (binding = 2) readonly buffer bias_blob { sfpvec8 bias_data[]; };
58
layout (push_constant) uniform parameter
71
int gx = int(gl_GlobalInvocationID.x);
72
int gy = int(gl_GlobalInvocationID.y);
73
int gz = int(gl_GlobalInvocationID.z);
75
if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
83
sum = image3d_ld8(bias_blob, ivec3(gz, 0, 0));
85
sum = buffer_ld8(bias_data, gz);
90
sum = afpvec8(afpvec4(0.f), afpvec4(0.f));
93
const int maxk = kernel_w * kernel_h;
95
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
96
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
98
const int sx_start = (gx < kernel_extent_w) ? 0 : (gx - kernel_extent_w) / stride_w + 1;
99
const int sx_end = min(gx / stride_w + 1, psc(w));
101
const int sy_start = (gy < kernel_extent_h) ? 0 : (gy - kernel_extent_h) / stride_h + 1;
102
const int sy_end = min(gy / stride_h + 1, psc(h));
104
for (int sy = sy_start; sy < sy_end; sy += 1)
106
for (int sx = sx_start; sx < sx_end; sx += 1)
108
int h_k = (gy - sy * stride_h);
109
int w_k = (gx - sx * stride_w);
111
if (h_k % dilation_h == 0 && w_k % dilation_w == 0)
117
sum += image3d_ld8(col_blob, ivec3(sy * psc(w) + sx, gz * maxk + h_k * kernel_w + w_k, 0));
119
const int gi = (gz * maxk + h_k * kernel_w + w_k) * psc(w) * psc(h) + sy * psc(w) + sx;
121
sum += buffer_ld8(col_blob_data, gi);
127
sum = activation_afpvec8(sum, activation_type, activation_param_0, activation_param_1);
130
image3d_st8(top_blob, ivec3(gx, gy, gz), sum);
132
const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
134
buffer_st8(top_blob_data, gi, sum);