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