| 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 | layout (constant_id = 0) const int bugihfa = 0; |
| 26 | |
| 27 | #define shape_constant_id_offset 1 |
| 28 | layout (constant_id = shape_constant_id_offset + 0) const int dims = 0; |
| 29 | layout (constant_id = shape_constant_id_offset + 1) const int w = 0; |
| 30 | layout (constant_id = shape_constant_id_offset + 2) const int h = 0; |
| 31 | layout (constant_id = shape_constant_id_offset + 3) const int d = 0; |
| 32 | layout (constant_id = shape_constant_id_offset + 4) const int c = 0; |
| 33 | layout (constant_id = shape_constant_id_offset + 5) const int cstep = 0; |
| 34 | |
| 35 | layout (constant_id = shape_constant_id_offset + 6) const int outdims = 0; |
| 36 | layout (constant_id = shape_constant_id_offset + 7) const int outw = 0; |
| 37 | layout (constant_id = shape_constant_id_offset + 8) const int outh = 0; |
| 38 | layout (constant_id = shape_constant_id_offset + 9) const int outd = 0; |
| 39 | layout (constant_id = shape_constant_id_offset + 10) const int outc = 0; |
| 40 | layout (constant_id = shape_constant_id_offset + 11) const int outcstep = 0; |
| 41 | |
| 42 | #if NCNN_image_shader |
| 43 | layout (binding = 0) uniform unfp sampler3D bottom_blob; |
| 44 | layout (binding = 1, imfmtc1) writeonly uniform unfp image3D top_blob; |
| 45 | #else |
| 46 | #if NCNN_fp16_packed |
| 47 | layout (binding = 0) readonly buffer bottom_blob { sfpvec2 bottom_blob_data[]; }; |
| 48 | #else |
| 49 | layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; }; |
| 50 | #endif |
| 51 | layout (binding = 1) writeonly buffer top_blob { sfp top_blob_data[]; }; |
| 52 | #endif |
| 53 | |
| 54 | layout (push_constant) uniform parameter |
| 55 | { |
| 56 | int dims; |
| 57 | int w; |
| 58 | int h; |
| 59 | int d; |
| 60 | int c; |
| 61 | int cstep; |
| 62 | |
| 63 | int outdims; |
| 64 | int outw; |
| 65 | int outh; |
| 66 | int outd; |
| 67 | int outc; |
| 68 | int outcstep; |
| 69 | |
| 70 | int woffset; |
| 71 | int hoffset; |
| 72 | int doffset; |
| 73 | int coffset; |
| 74 | } p; |
| 75 | |
| 76 | void main() |
| 77 | { |
| 78 | int gx = int(gl_GlobalInvocationID.x); |
| 79 | int gy = int(gl_GlobalInvocationID.y); |
| 80 | int gz = int(gl_GlobalInvocationID.z); |
| 81 | |
| 82 | if (gx >= psc(outw) || gy >= psc(outh) * psc(outd) || gz >= psc(outc)) |
| 83 | return; |
| 84 | |
| 85 | if (psc(dims) == 1) |
| 86 | { |
| 87 | int x = gx + p.woffset; |
| 88 | |
| 89 | #if NCNN_image_shader |
| 90 | afpvec8 v = image3d_ld8(bottom_blob, ivec3(x / 8, 0, 0)); |
| 91 | |
| 92 | image3d_st1(top_blob, ivec3(gx, gy, gz), v[(x % 8) / 4][x % 4]); |
| 93 | #else |
| 94 | #if NCNN_fp16_packed |
| 95 | int v_offset = (x / 8) * 4 + (x % 8) / 2; |
| 96 | int lane2 = x % 2; |
| 97 | |
| 98 | afpvec2 v = buffer_ld2(bottom_blob_data, v_offset); |
| 99 | |
| 100 | buffer_st1(top_blob_data, gx, v[lane2]); |
| 101 | #else |
| 102 | int v_offset = (x / 8) * 8 + x % 8; |
| 103 | |
| 104 | buffer_cp1(top_blob_data, gx, bottom_blob_data, v_offset); |
| 105 | #endif |
| 106 | #endif |
| 107 | } |
| 108 | else if (psc(dims) == 2) |
| 109 | { |
| 110 | int x = gx + p.woffset; |
| 111 | int y = gy + p.hoffset; |
| 112 | |
| 113 | #if NCNN_image_shader |
| 114 | afpvec8 v = image3d_ld8(bottom_blob, ivec3(x, y / 8, 0)); |
| 115 | |
| 116 | image3d_st1(top_blob, ivec3(gx, gy, 0), v[(y % 8) / 4][y % 4]); |
| 117 | #else |
| 118 | int gi = gy * psc(outw) + gx; |
| 119 | |
| 120 | #if NCNN_fp16_packed |
| 121 | int v_offset = ((y / 8) * psc(w) + x) * 4 + (y % 8) / 2; |
| 122 | int lane2 = y % 2; |
| 123 | |
| 124 | afpvec2 v = buffer_ld2(bottom_blob_data, v_offset); |
| 125 | |
| 126 | buffer_st1(top_blob_data, gi, v[lane2]); |
| 127 | #else |
| 128 | int v_offset = ((y / 8) * psc(w) + x) * 8 + y % 8; |
| 129 | |
| 130 | buffer_cp1(top_blob_data, gi, bottom_blob_data, v_offset); |
| 131 | #endif |
| 132 | #endif |
| 133 | } |
| 134 | else if (psc(dims) == 3) |
| 135 | { |
| 136 | int x = gx + p.woffset; |
| 137 | int y = gy + p.hoffset; |
| 138 | int z = gz + p.coffset; |
| 139 | |
| 140 | #if NCNN_image_shader |
| 141 | afpvec8 v = image3d_ld8(bottom_blob, ivec3(x, y, z / 8)); |
| 142 | |
| 143 | image3d_st1(top_blob, ivec3(gx, gy, gz), v[(z % 8) / 4][z % 4]); |
| 144 | #else |
| 145 | int gi = gz * psc(outcstep) + gy * psc(outw) + gx; |
| 146 | |
| 147 | #if NCNN_fp16_packed |
| 148 | int v_offset = ((z / 8) * psc(cstep) + y * psc(w) + x) * 4 + (z % 8) / 2; |
| 149 | int lane2 = z % 2; |
| 150 | |
| 151 | afpvec2 v = buffer_ld2(bottom_blob_data, v_offset); |
| 152 | |
| 153 | buffer_st1(top_blob_data, gi, v[lane2]); |
| 154 | #else |
| 155 | int v_offset = ((z / 8) * psc(cstep) + y * psc(w) + x) * 8 + z % 8; |
| 156 | |
| 157 | buffer_cp1(top_blob_data, gi, bottom_blob_data, v_offset); |
| 158 | #endif |
| 159 | #endif |
| 160 | } |
| 161 | else // if (psc(dims) == 4) |
| 162 | { |
| 163 | int yd = gy / psc(outh); |
| 164 | int yh = gy % psc(outh); |
| 165 | |
| 166 | int x = gx + p.woffset; |
| 167 | int y = (yd + p.doffset) * psc(h) + (yh + p.hoffset); |
| 168 | int z = gz + p.coffset; |
| 169 | |
| 170 | #if NCNN_image_shader |
| 171 | afpvec8 v = image3d_ld8(bottom_blob, ivec3(x, y, z / 8)); |
| 172 | |
| 173 | image3d_st1(top_blob, ivec3(gx, gy, gz), v[(z % 8) / 4][z % 4]); |
| 174 | #else |
| 175 | int gi = gz * psc(outcstep) + gy * psc(outw) + gx; |
| 176 | |
| 177 | #if NCNN_fp16_packed |
| 178 | int v_offset = ((z / 8) * psc(cstep) + y * psc(w) + x) * 4 + (z % 8) / 2; |
| 179 | int lane2 = z % 2; |
| 180 | |
| 181 | afpvec2 v = buffer_ld2(bottom_blob_data, v_offset); |
| 182 | |
| 183 | buffer_st1(top_blob_data, gi, v[lane2]); |
| 184 | #else |
| 185 | int v_offset = ((z / 8) * psc(cstep) + y * psc(w) + x) * 8 + z % 8; |
| 186 | |
| 187 | buffer_cp1(top_blob_data, gi, bottom_blob_data, v_offset); |
| 188 | #endif |
| 189 | #endif |
| 190 | } |
| 191 | } |