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 bias_term = 0;
28
layout (constant_id = 1) const int activation_type = 0;
29
layout (constant_id = 2) const float activation_param_0 = 0;
30
layout (constant_id = 3) const float activation_param_1 = 0;
32
#define shape_constant_id_offset 4
33
layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
34
layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
35
layout (constant_id = shape_constant_id_offset + 2) const int c = 0;
36
layout (constant_id = shape_constant_id_offset + 3) const int cstep = 0;
38
layout (constant_id = shape_constant_id_offset + 4) const int outw = 0;
39
layout (constant_id = shape_constant_id_offset + 5) const int outh = 0;
40
layout (constant_id = shape_constant_id_offset + 6) const int outc = 0;
41
layout (constant_id = shape_constant_id_offset + 7) const int outcstep = 0;
44
layout (binding = 0) uniform unfp sampler3D bottom_blob;
45
layout (binding = 1, imfmtc1) writeonly uniform unfp image3D top_blob;
46
layout (binding = 2) uniform unfp sampler3D weight_blob;
47
layout (binding = 3) uniform unfp sampler3D bias_blob;
50
layout (binding = 0) readonly buffer bottom_blob { vec4 bottom_blob_data[]; };
51
layout (binding = 1) writeonly buffer top_blob { vec4 top_blob_data[]; };
53
layout (binding = 0) readonly buffer bottom_blob { sfpvec4 bottom_blob_data[]; };
54
layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
56
layout (binding = 2) readonly buffer weight_blob { sfp weight_data[]; };
57
layout (binding = 3) readonly buffer bias_blob { sfp bias_data[]; };
60
layout (push_constant) uniform parameter
76
int gx = int(gl_GlobalInvocationID.x) * 4;
77
int gy = int(gl_GlobalInvocationID.y);
79
if (gx >= psc(outw) * psc(outh) || gy >= psc(outc))
82
int gx = int(gl_GlobalInvocationID.x);
83
int gy = int(gl_GlobalInvocationID.y);
85
if (gx * 4 >= psc(outcstep) || gy >= psc(outc))
94
sum = afpvec4(image3d_ld1(bias_blob, ivec3(gy, 0, 0)));
96
sum = afpvec4(buffer_ld1(bias_data, gy));
105
ivec4 gx4 = gx + ivec4(0, 1, 2, 3);
107
ivec4 sy4 = gx4 / psc(w);
108
ivec4 sx4 = gx4 % psc(w);
110
for (int z = 0; z < psc(c); z++)
112
afp k = image3d_ld1(weight_blob, ivec3(0, z, gy));
114
sum.r += k * image3d_ld1(bottom_blob, ivec3(sx4.r, sy4.r, z));
115
sum.g += k * image3d_ld1(bottom_blob, ivec3(sx4.g, sy4.g, z));
116
sum.b += k * image3d_ld1(bottom_blob, ivec3(sx4.b, sy4.b, z));
117
sum.a += k * image3d_ld1(bottom_blob, ivec3(sx4.a, sy4.a, z));
120
int w_offset = gy * psc(c);
123
for (int z = 0; z < psc(c); z++)
126
sum += afp(weight_data[w_offset]) * afpvec4(bottom_blob_data[v_offset]);
128
sum += buffer_ld1(weight_data, w_offset) * buffer_ld4(bottom_blob_data, v_offset);
132
v_offset += psc(cstep) / 4;
136
sum = activation_afpvec4(sum, activation_type, activation_param_0, activation_param_1);
139
image3d_st1(top_blob, ivec3(sx4.r, sy4.r, gy), sum.r);
140
image3d_st1(top_blob, ivec3(sx4.g, sy4.g, gy), sum.g);
141
image3d_st1(top_blob, ivec3(sx4.b, sy4.b, gy), sum.b);
142
image3d_st1(top_blob, ivec3(sx4.a, sy4.a, gy), sum.a);
144
const int gi = gy * psc(outcstep) / 4 + gx;
147
top_blob_data[gi] = sum;
149
buffer_st4(top_blob_data, gi, sum);