| 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 | |
| 24 | #extension GL_GOOGLE_include_directive: enable |
| 25 | #include "vulkan_activation.comp" |
| 26 | |
| 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; |
| 31 | |
| 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; |
| 37 | |
| 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; |
| 42 | |
| 43 | #if NCNN_image_shader |
| 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; |
| 48 | #else |
| 49 | #if NCNN_fp16_packed |
| 50 | layout (binding = 0) readonly buffer bottom_blob { vec4 bottom_blob_data[]; }; |
| 51 | layout (binding = 1) writeonly buffer top_blob { vec4 top_blob_data[]; }; |
| 52 | #else |
| 53 | layout (binding = 0) readonly buffer bottom_blob { sfpvec4 bottom_blob_data[]; }; |
| 54 | layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; }; |
| 55 | #endif |
| 56 | layout (binding = 2) readonly buffer weight_blob { sfp weight_data[]; }; |
| 57 | layout (binding = 3) readonly buffer bias_blob { sfp bias_data[]; }; |
| 58 | #endif |
| 59 | |
| 60 | layout (push_constant) uniform parameter |
| 61 | { |
| 62 | int w; |
| 63 | int h; |
| 64 | int c; |
| 65 | int cstep; |
| 66 | |
| 67 | int outw; |
| 68 | int outh; |
| 69 | int outc; |
| 70 | int outcstep; |
| 71 | } p; |
| 72 | |
| 73 | void main() |
| 74 | { |
| 75 | #if NCNN_image_shader |
| 76 | int gx = int(gl_GlobalInvocationID.x) * 4; |
| 77 | int gy = int(gl_GlobalInvocationID.y); |
| 78 | |
| 79 | if (gx >= psc(outw) * psc(outh) || gy >= psc(outc)) |
| 80 | return; |
| 81 | #else |
| 82 | int gx = int(gl_GlobalInvocationID.x); |
| 83 | int gy = int(gl_GlobalInvocationID.y); |
| 84 | |
| 85 | if (gx * 4 >= psc(outcstep) || gy >= psc(outc)) |
| 86 | return; |
| 87 | #endif |
| 88 | |
| 89 | afpvec4 sum; |
| 90 | |
| 91 | if (bias_term == 1) |
| 92 | { |
| 93 | #if NCNN_image_shader |
| 94 | sum = afpvec4(image3d_ld1(bias_blob, ivec3(gy, 0, 0))); |
| 95 | #else |
| 96 | sum = afpvec4(buffer_ld1(bias_data, gy)); |
| 97 | #endif |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | sum = afpvec4(0.f); |
| 102 | } |
| 103 | |
| 104 | #if NCNN_image_shader |
| 105 | ivec4 gx4 = gx + ivec4(0, 1, 2, 3); |
| 106 | |
| 107 | ivec4 sy4 = gx4 / psc(w); |
| 108 | ivec4 sx4 = gx4 % psc(w); |
| 109 | |
| 110 | for (int z = 0; z < psc(c); z++) |
| 111 | { |
| 112 | afp k = image3d_ld1(weight_blob, ivec3(0, z, gy)); |
| 113 | |
| 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)); |
| 118 | } |
| 119 | #else |
| 120 | int w_offset = gy * psc(c); |
| 121 | int v_offset = gx; |
| 122 | |
| 123 | for (int z = 0; z < psc(c); z++) |
| 124 | { |
| 125 | #if NCNN_fp16_packed |
| 126 | sum += afp(weight_data[w_offset]) * afpvec4(bottom_blob_data[v_offset]); |
| 127 | #else |
| 128 | sum += buffer_ld1(weight_data, w_offset) * buffer_ld4(bottom_blob_data, v_offset); |
| 129 | #endif |
| 130 | |
| 131 | w_offset += 1; |
| 132 | v_offset += psc(cstep) / 4; |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | sum = activation_afpvec4(sum, activation_type, activation_param_0, activation_param_1); |
| 137 | |
| 138 | #if NCNN_image_shader |
| 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); |
| 143 | #else |
| 144 | const int gi = gy * psc(outcstep) / 4 + gx; |
| 145 | |
| 146 | #if NCNN_fp16_packed |
| 147 | top_blob_data[gi] = sum; |
| 148 | #else |
| 149 | buffer_st4(top_blob_data, gi, sum); |
| 150 | #endif |
| 151 | #endif |
| 152 | } |