1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2023 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 dilation_w = 1;
30
layout (constant_id = 2) const int stride_w = 1;
31
layout (constant_id = 3) const int bias_term = 0;
32
layout (constant_id = 4) const int activation_type = 0;
33
layout (constant_id = 5) const float activation_param_0 = 0;
34
layout (constant_id = 6) const float activation_param_1 = 0;
36
#define shape_constant_id_offset 7
37
layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
38
layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
40
layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
41
layout (constant_id = shape_constant_id_offset + 3) const int outh = 0;
44
layout (binding = 0) uniform unfp sampler3D bottom_blob;
45
layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
46
layout (binding = 2) uniform unfp sampler3D weight_blob;
47
layout (binding = 3) uniform unfp sampler3D bias_blob;
49
layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
50
layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };
51
layout (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; };
52
layout (binding = 3) readonly buffer bias_blob { sfpvec8 bias_data[]; };
55
layout (push_constant) uniform parameter
66
int gx = int(gl_GlobalInvocationID.x) * 2;
67
int gy = int(gl_GlobalInvocationID.y) * 2;
69
if (gx >= psc(outw) || gy >= psc(outh))
72
const ivec2 gx2 = gx + ivec2(0, 1);
73
const ivec2 gy2 = gy + ivec2(0, 1);
75
afpvec8 sum0 = afpvec8(afpvec4(0.0f), afpvec4(0.0f));
76
afpvec8 sum1 = afpvec8(afpvec4(0.0f), afpvec4(0.0f));
77
afpvec8 sum2 = afpvec8(afpvec4(0.0f), afpvec4(0.0f));
78
afpvec8 sum3 = afpvec8(afpvec4(0.0f), afpvec4(0.0f));
83
sum0 = image3d_ld8(bias_blob, ivec3(gy2.x, 0, 0));
84
sum2 = image3d_ld8(bias_blob, ivec3(gy2.y, 0, 0));
86
sum0 = buffer_ld8(bias_data, gy2.x);
87
sum2 = buffer_ld8(bias_data, gy2.y);
95
ivec2 v_offset = gx2 * stride_w;
97
for (int y = 0; y < psc(h); y++)
101
for (int x = 0; x < kernel_w; x++)
103
afp v0 = image3d_ld1(bottom_blob, ivec3(v_offset.x + x * dilation_w, y, 0));
104
afp v1 = image3d_ld1(bottom_blob, ivec3(v_offset.y + x * dilation_w, y, 0));
106
afpvec8 k0 = image3d_ld8(weight_blob, ivec3(wx, y, gy2.x));
107
afpvec8 k1 = image3d_ld8(weight_blob, ivec3(wx, y, gy2.y));
109
sum0[0] += v0 * k0[0];
110
sum0[1] += v0 * k0[1];
111
sum1[0] += v1 * k0[0];
112
sum1[1] += v1 * k0[1];
113
sum2[0] += v0 * k1[0];
114
sum2[1] += v0 * k1[1];
115
sum3[0] += v1 * k1[0];
116
sum3[1] += v1 * k1[1];
124
ivec2 v_offset = gx2 * stride_w;
125
ivec2 w_offset = gy2 * psc(h) * kernel_w;
127
for (int y = 0; y < psc(h); y++)
129
for (int x = 0; x < kernel_w; x++)
131
afp v0 = buffer_ld1(bottom_blob_data, v_offset.x + x * dilation_w);
132
afp v1 = buffer_ld1(bottom_blob_data, v_offset.y + x * dilation_w);
134
afpvec8 k0 = buffer_ld8(weight_data, w_offset.x + x);
135
afpvec8 k1 = buffer_ld8(weight_data, w_offset.y + x);
137
sum0[0] += v0 * k0[0];
138
sum0[1] += v0 * k0[1];
139
sum1[0] += v1 * k0[0];
140
sum1[1] += v1 * k0[1];
141
sum2[0] += v0 * k1[0];
142
sum2[1] += v0 * k1[1];
143
sum3[0] += v1 * k1[0];
144
sum3[1] += v1 * k1[1];
147
w_offset += kernel_w;
152
sum0 = activation_afpvec8(sum0, activation_type, activation_param_0, activation_param_1);
153
sum1 = activation_afpvec8(sum1, activation_type, activation_param_0, activation_param_1);
154
sum2 = activation_afpvec8(sum2, activation_type, activation_param_0, activation_param_1);
155
sum3 = activation_afpvec8(sum3, activation_type, activation_param_0, activation_param_1);
159
image3d_st8(top_blob, ivec3(gx2.x, gy2.x, 0), sum0);
160
image3d_st8(top_blob, ivec3(gx2.y, gy2.x, 0), sum1);
161
image3d_st8(top_blob, ivec3(gx2.x, gy2.y, 0), sum2);
162
image3d_st8(top_blob, ivec3(gx2.y, gy2.y, 0), sum3);
166
const int gi = gy * psc(outw) + gx;
168
buffer_st8(top_blob_data, gi, sum0);
169
if (gx + 1 < psc(outw)) buffer_st8(top_blob_data, gi + 1, sum1);
170
if (gy + 1 < psc(outh)) buffer_st8(top_blob_data, gi + psc(outw), sum2);
171
if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st8(top_blob_data, gi + psc(outw) + 1, sum3);