| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 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 | #define shape_constant_id_offset 0 |
| 26 | layout (constant_id = shape_constant_id_offset + 0) const int dims = 0; |
| 27 | layout (constant_id = shape_constant_id_offset + 1) const int w = 0; |
| 28 | layout (constant_id = shape_constant_id_offset + 2) const int h = 0; |
| 29 | layout (constant_id = shape_constant_id_offset + 3) const int c = 0; |
| 30 | layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0; |
| 31 | |
| 32 | layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0; |
| 33 | layout (constant_id = shape_constant_id_offset + 6) const int outw = 0; |
| 34 | layout (constant_id = shape_constant_id_offset + 7) const int outh = 0; |
| 35 | layout (constant_id = shape_constant_id_offset + 8) const int outc = 0; |
| 36 | layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0; |
| 37 | |
| 38 | #if NCNN_image_shader |
| 39 | layout (binding = 0) uniform unfp sampler3D bottom_blob_3d; |
| 40 | layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob; |
| 41 | #else |
| 42 | #if NCNN_fp16_packed |
| 43 | layout (binding = 0) readonly buffer bottom_blob { sfpvec2 bottom_blob_data[]; }; |
| 44 | #else |
| 45 | layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; }; |
| 46 | #endif |
| 47 | layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; }; |
| 48 | #endif |
| 49 | |
| 50 | layout (push_constant) uniform parameter |
| 51 | { |
| 52 | int dims; |
| 53 | int w; |
| 54 | int h; |
| 55 | int c; |
| 56 | int cstep; |
| 57 | |
| 58 | int outdims; |
| 59 | int outw; |
| 60 | int outh; |
| 61 | int outc; |
| 62 | int outcstep; |
| 63 | } p; |
| 64 | |
| 65 | void main() |
| 66 | { |
| 67 | int gx = int(gl_GlobalInvocationID.x); |
| 68 | int gy = int(gl_GlobalInvocationID.y); |
| 69 | int gz = int(gl_GlobalInvocationID.z); |
| 70 | |
| 71 | if (gx >= psc(outw) || gy >= 1 || gz >= 1) |
| 72 | return; |
| 73 | |
| 74 | ivec4 i4 = gx * 8 + ivec4(0, 1, 2, 3); |
| 75 | ivec4 ii4 = i4 + 4; |
| 76 | |
| 77 | #if NCNN_image_shader |
| 78 | afpvec8 v; |
| 79 | |
| 80 | if (psc(dims) == 2) |
| 81 | { |
| 82 | ivec4 y4 = i4 / psc(w); |
| 83 | ivec4 x4 = i4 % psc(w); |
| 84 | ivec4 yy4 = ii4 / psc(w); |
| 85 | ivec4 xx4 = ii4 % psc(w); |
| 86 | |
| 87 | afpvec4 v0 = image3d_ld4(bottom_blob_3d, ivec3(x4.r, y4.r / 4, 0)); |
| 88 | afpvec4 v1 = image3d_ld4(bottom_blob_3d, ivec3(x4.g, y4.g / 4, 0)); |
| 89 | afpvec4 v2 = image3d_ld4(bottom_blob_3d, ivec3(x4.b, y4.b / 4, 0)); |
| 90 | afpvec4 v3 = image3d_ld4(bottom_blob_3d, ivec3(x4.a, y4.a / 4, 0)); |
| 91 | afpvec4 v4 = image3d_ld4(bottom_blob_3d, ivec3(xx4.r, yy4.r / 4, 0)); |
| 92 | afpvec4 v5 = image3d_ld4(bottom_blob_3d, ivec3(xx4.g, yy4.g / 4, 0)); |
| 93 | afpvec4 v6 = image3d_ld4(bottom_blob_3d, ivec3(xx4.b, yy4.b / 4, 0)); |
| 94 | afpvec4 v7 = image3d_ld4(bottom_blob_3d, ivec3(xx4.a, yy4.a / 4, 0)); |
| 95 | |
| 96 | v[0].r = v0[y4.r % 4]; |
| 97 | v[0].g = v1[y4.g % 4]; |
| 98 | v[0].b = v2[y4.b % 4]; |
| 99 | v[0].a = v3[y4.a % 4]; |
| 100 | v[1].r = v4[yy4.r % 4]; |
| 101 | v[1].g = v5[yy4.g % 4]; |
| 102 | v[1].b = v6[yy4.b % 4]; |
| 103 | v[1].a = v7[yy4.a % 4]; |
| 104 | } |
| 105 | else // if (psc(dims) == 3) |
| 106 | { |
| 107 | int size = psc(w) * psc(h); |
| 108 | |
| 109 | ivec4 z4 = i4 / size; |
| 110 | ivec4 y4 = i4 % size / psc(w); |
| 111 | ivec4 x4 = i4 % size % psc(w); |
| 112 | ivec4 zz4 = ii4 / size; |
| 113 | ivec4 yy4 = ii4 % size / psc(w); |
| 114 | ivec4 xx4 = ii4 % size % psc(w); |
| 115 | |
| 116 | afpvec4 v0 = image3d_ld4(bottom_blob_3d, ivec3(x4.r, y4.r, z4.r / 4)); |
| 117 | afpvec4 v1 = image3d_ld4(bottom_blob_3d, ivec3(x4.g, y4.g, z4.g / 4)); |
| 118 | afpvec4 v2 = image3d_ld4(bottom_blob_3d, ivec3(x4.b, y4.b, z4.b / 4)); |
| 119 | afpvec4 v3 = image3d_ld4(bottom_blob_3d, ivec3(x4.a, y4.a, z4.a / 4)); |
| 120 | afpvec4 v4 = image3d_ld4(bottom_blob_3d, ivec3(xx4.r, yy4.r, zz4.r / 4)); |
| 121 | afpvec4 v5 = image3d_ld4(bottom_blob_3d, ivec3(xx4.g, yy4.g, zz4.g / 4)); |
| 122 | afpvec4 v6 = image3d_ld4(bottom_blob_3d, ivec3(xx4.b, yy4.b, zz4.b / 4)); |
| 123 | afpvec4 v7 = image3d_ld4(bottom_blob_3d, ivec3(xx4.a, yy4.a, zz4.a / 4)); |
| 124 | |
| 125 | v[0].r = v0[z4.r % 4]; |
| 126 | v[0].g = v1[z4.g % 4]; |
| 127 | v[0].b = v2[z4.b % 4]; |
| 128 | v[0].a = v3[z4.a % 4]; |
| 129 | v[1].r = v4[zz4.r % 4]; |
| 130 | v[1].g = v5[zz4.g % 4]; |
| 131 | v[1].b = v6[zz4.b % 4]; |
| 132 | v[1].a = v7[zz4.a % 4]; |
| 133 | } |
| 134 | |
| 135 | image3d_st8(top_blob, ivec3(gx, 0, 0), v); |
| 136 | #else |
| 137 | #if NCNN_fp16_packed |
| 138 | ivec4 v_offset; |
| 139 | ivec4 lane4; |
| 140 | ivec4 vv_offset; |
| 141 | ivec4 lane8; |
| 142 | |
| 143 | if (psc(dims) == 2) |
| 144 | { |
| 145 | ivec4 y4 = i4 / psc(w); |
| 146 | ivec4 x4 = i4 % psc(w); |
| 147 | ivec4 yy4 = ii4 / psc(w); |
| 148 | ivec4 xx4 = ii4 % psc(w); |
| 149 | |
| 150 | v_offset = ((y4 / 4) * psc(w) + x4) * 2 + (y4 % 4) / 2; |
| 151 | lane4 = y4 % 2; |
| 152 | vv_offset = ((yy4 / 4) * psc(w) + xx4) * 2 + (yy4 % 4) / 2; |
| 153 | lane8 = yy4 % 2; |
| 154 | } |
| 155 | else // if (psc(dims) == 3) |
| 156 | { |
| 157 | int size = psc(w) * psc(h); |
| 158 | |
| 159 | ivec4 z4 = i4 / size; |
| 160 | ivec4 y4 = i4 % size / psc(w); |
| 161 | ivec4 x4 = i4 % size % psc(w); |
| 162 | ivec4 zz4 = ii4 / size; |
| 163 | ivec4 yy4 = ii4 % size / psc(w); |
| 164 | ivec4 xx4 = ii4 % size % psc(w); |
| 165 | |
| 166 | v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 2 + (z4 % 4) / 2; |
| 167 | lane4 = z4 % 2; |
| 168 | vv_offset = ((zz4 / 4) * psc(cstep) + yy4 * psc(w) + xx4) * 2 + (zz4 % 4) / 2; |
| 169 | lane8 = zz4 % 2; |
| 170 | } |
| 171 | |
| 172 | afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r); |
| 173 | afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g); |
| 174 | afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b); |
| 175 | afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a); |
| 176 | |
| 177 | afpvec2 vvr = buffer_ld2(bottom_blob_data, vv_offset.r); |
| 178 | afpvec2 vvg = buffer_ld2(bottom_blob_data, vv_offset.g); |
| 179 | afpvec2 vvb = buffer_ld2(bottom_blob_data, vv_offset.b); |
| 180 | afpvec2 vva = buffer_ld2(bottom_blob_data, vv_offset.a); |
| 181 | |
| 182 | afpvec8 v = afpvec8(vr[lane4.r], vg[lane4.g], vb[lane4.b], va[lane4.a], vvr[lane8.r], vvg[lane8.g], vvb[lane8.b], vva[lane8.a]); |
| 183 | |
| 184 | buffer_st8(top_blob_data, gx, v); |
| 185 | #else |
| 186 | ivec4 v_offset; |
| 187 | ivec4 vv_offset; |
| 188 | |
| 189 | if (psc(dims) == 2) |
| 190 | { |
| 191 | ivec4 y4 = i4 / psc(w); |
| 192 | ivec4 x4 = i4 % psc(w); |
| 193 | ivec4 yy4 = ii4 / psc(w); |
| 194 | ivec4 xx4 = ii4 % psc(w); |
| 195 | |
| 196 | v_offset = ((y4 / 4) * psc(w) + x4) * 4 + y4 % 4; |
| 197 | vv_offset = ((yy4 / 4) * psc(w) + xx4) * 4 + yy4 % 4; |
| 198 | } |
| 199 | else // if (psc(dims) == 3) |
| 200 | { |
| 201 | int size = psc(w) * psc(h); |
| 202 | |
| 203 | ivec4 z4 = i4 / size; |
| 204 | ivec4 y4 = i4 % size / psc(w); |
| 205 | ivec4 x4 = i4 % size % psc(w); |
| 206 | ivec4 zz4 = ii4 / size; |
| 207 | ivec4 yy4 = ii4 % size / psc(w); |
| 208 | ivec4 xx4 = ii4 % size % psc(w); |
| 209 | |
| 210 | v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 4 + z4 % 4; |
| 211 | vv_offset = ((zz4 / 4) * psc(cstep) + yy4 * psc(w) + xx4) * 4 + zz4 % 4; |
| 212 | } |
| 213 | |
| 214 | buffer_cp1to8(top_blob_data, gx, bottom_blob_data, v_offset, vv_offset); |
| 215 | #endif |
| 216 | #endif |
| 217 | } |