| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 | struct sfpvec8 { f16vec4 abcd; f16vec4 efgh; }; |
| 20 | #endif |
| 21 | #if NCNN_fp16_arithmetic |
| 22 | #extension GL_EXT_shader_explicit_arithmetic_types_float16: require |
| 23 | #endif |
| 24 | |
| 25 | layout (constant_id = 0) const int maxk = 1; |
| 26 | |
| 27 | #define shape_constant_id_offset 1 |
| 28 | layout (constant_id = shape_constant_id_offset + 0) const int w = 0; |
| 29 | layout (constant_id = shape_constant_id_offset + 1) const int h = 0; |
| 30 | layout (constant_id = shape_constant_id_offset + 2) const int c = 0; |
| 31 | layout (constant_id = shape_constant_id_offset + 3) const int cstep = 0; |
| 32 | |
| 33 | layout (constant_id = shape_constant_id_offset + 4) const int outw = 0; |
| 34 | layout (constant_id = shape_constant_id_offset + 5) const int outh = 0; |
| 35 | |
| 36 | #if NCNN_image_shader |
| 37 | layout (binding = 0) uniform unfp sampler3D bottom_blob; |
| 38 | layout (binding = 1, imfmtc1) writeonly uniform unfp image3D col_blob; |
| 39 | layout (binding = 2) uniform unfp sampler3D weight_blob; |
| 40 | #else |
| 41 | layout (binding = 0) readonly buffer bottom_blob { sfpvec8 bottom_blob_data[]; }; |
| 42 | layout (binding = 1) writeonly buffer col_blob { sfp col_blob_data[]; }; |
| 43 | layout (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; }; |
| 44 | #endif |
| 45 | |
| 46 | layout (push_constant) uniform parameter |
| 47 | { |
| 48 | int w; |
| 49 | int h; |
| 50 | int c; |
| 51 | int cstep; |
| 52 | |
| 53 | int outw; |
| 54 | int outh; |
| 55 | } p; |
| 56 | |
| 57 | void main() |
| 58 | { |
| 59 | int gx = int(gl_GlobalInvocationID.x) * 4; |
| 60 | int gy = int(gl_GlobalInvocationID.y); |
| 61 | |
| 62 | if (gx >= psc(outw) || gy >= psc(outh)) |
| 63 | return; |
| 64 | |
| 65 | afp sum0 = afp(0.f); |
| 66 | afp sum1 = afp(0.f); |
| 67 | afp sum2 = afp(0.f); |
| 68 | afp sum3 = afp(0.f); |
| 69 | |
| 70 | #if NCNN_image_shader |
| 71 | ivec4 gx4 = gx + ivec4(0, 1, 2, 3); |
| 72 | |
| 73 | ivec4 sy4 = gx4 / psc(w); |
| 74 | ivec4 sx4 = gx4 % psc(w); |
| 75 | |
| 76 | for (int z = 0; z < psc(c); z++) |
| 77 | { |
| 78 | afpvec8 v0 = image3d_ld8(bottom_blob, ivec3(sx4.r, sy4.r, z)); |
| 79 | afpvec8 v1 = image3d_ld8(bottom_blob, ivec3(sx4.g, sy4.g, z)); |
| 80 | afpvec8 v2 = image3d_ld8(bottom_blob, ivec3(sx4.b, sy4.b, z)); |
| 81 | afpvec8 v3 = image3d_ld8(bottom_blob, ivec3(sx4.a, sy4.a, z)); |
| 82 | |
| 83 | afpvec8 k = image3d_ld8(weight_blob, ivec3(z, gy, 0)); |
| 84 | |
| 85 | // sum += dot(v, k); |
| 86 | sum0 += dot(v0[0], k[0]) + dot(v0[1], k[1]); |
| 87 | sum1 += dot(v1[0], k[0]) + dot(v1[1], k[1]); |
| 88 | sum2 += dot(v2[0], k[0]) + dot(v2[1], k[1]); |
| 89 | sum3 += dot(v3[0], k[0]) + dot(v3[1], k[1]); |
| 90 | } |
| 91 | #else |
| 92 | int v_offset = gx; |
| 93 | int w_offset = gy * psc(c); |
| 94 | |
| 95 | for (int z = 0; z < psc(c); z++) |
| 96 | { |
| 97 | afpvec8 v0 = buffer_ld8(bottom_blob_data, v_offset + 0); |
| 98 | afpvec8 v1 = buffer_ld8(bottom_blob_data, v_offset + 1); |
| 99 | afpvec8 v2 = buffer_ld8(bottom_blob_data, v_offset + 2); |
| 100 | afpvec8 v3 = buffer_ld8(bottom_blob_data, v_offset + 3); |
| 101 | |
| 102 | afpvec8 k = buffer_ld8(weight_data, w_offset); |
| 103 | |
| 104 | // sum += dot(v, k); |
| 105 | sum0 += dot(v0[0], k[0]) + dot(v0[1], k[1]); |
| 106 | sum1 += dot(v1[0], k[0]) + dot(v1[1], k[1]); |
| 107 | sum2 += dot(v2[0], k[0]) + dot(v2[1], k[1]); |
| 108 | sum3 += dot(v3[0], k[0]) + dot(v3[1], k[1]); |
| 109 | |
| 110 | v_offset += psc(cstep); |
| 111 | w_offset += 1; |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | #if NCNN_image_shader |
| 116 | image3d_st1(col_blob, ivec3(gx4.r, gy, 0), sum0); |
| 117 | image3d_st1(col_blob, ivec3(gx4.g, gy, 0), sum1); |
| 118 | image3d_st1(col_blob, ivec3(gx4.b, gy, 0), sum2); |
| 119 | image3d_st1(col_blob, ivec3(gx4.a, gy, 0), sum3); |
| 120 | #else |
| 121 | const int gi = gy * psc(outw) + gx; |
| 122 | |
| 123 | buffer_st1(col_blob_data, gi, sum0); |
| 124 | if (gx + 1 < psc(outw)) buffer_st1(col_blob_data, gi + 1, sum1); |
| 125 | if (gx + 2 < psc(outw)) buffer_st1(col_blob_data, gi + 2, sum2); |
| 126 | if (gx + 3 < psc(outw)) buffer_st1(col_blob_data, gi + 3, sum3); |
| 127 | #endif |
| 128 | } |