| 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 upscale_factor = 0; |
| 26 | layout (constant_id = 1) const int mode = 0; |
| 27 | |
| 28 | #define shape_constant_id_offset 2 |
| 29 | layout (constant_id = shape_constant_id_offset + 0) const int dims = 0; |
| 30 | layout (constant_id = shape_constant_id_offset + 1) const int w = 0; |
| 31 | layout (constant_id = shape_constant_id_offset + 2) const int h = 0; |
| 32 | layout (constant_id = shape_constant_id_offset + 3) const int c = 0; |
| 33 | layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0; |
| 34 | |
| 35 | layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0; |
| 36 | layout (constant_id = shape_constant_id_offset + 6) const int outw = 0; |
| 37 | layout (constant_id = shape_constant_id_offset + 7) const int outh = 0; |
| 38 | layout (constant_id = shape_constant_id_offset + 8) const int outc = 0; |
| 39 | layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0; |
| 40 | |
| 41 | #if NCNN_image_shader |
| 42 | layout (binding = 0) uniform unfp sampler3D bottom_blob; |
| 43 | layout (binding = 1, imfmtc4) 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 { sfpvec8 top_blob_data[]; }; |
| 51 | #endif |
| 52 | |
| 53 | layout (push_constant) uniform parameter |
| 54 | { |
| 55 | int dims; |
| 56 | int w; |
| 57 | int h; |
| 58 | int c; |
| 59 | int cstep; |
| 60 | |
| 61 | int outdims; |
| 62 | int outw; |
| 63 | int outh; |
| 64 | int outc; |
| 65 | int outcstep; |
| 66 | } p; |
| 67 | |
| 68 | void main() |
| 69 | { |
| 70 | int gx = int(gl_GlobalInvocationID.x); |
| 71 | int gy = int(gl_GlobalInvocationID.y); |
| 72 | int gz = int(gl_GlobalInvocationID.z); |
| 73 | |
| 74 | if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc)) |
| 75 | return; |
| 76 | |
| 77 | ivec4 gz4 = gz * 8 + ivec4(0, 1, 2, 3); |
| 78 | ivec4 gzz4 = gz4 + 4; |
| 79 | |
| 80 | int x = gx / upscale_factor; |
| 81 | int y = gy / upscale_factor; |
| 82 | ivec4 z4; |
| 83 | ivec4 zz4; |
| 84 | if (mode == 0) |
| 85 | { |
| 86 | z4 = gz4 * upscale_factor * upscale_factor + (gy % upscale_factor) * upscale_factor + gx % upscale_factor; |
| 87 | zz4 = gzz4 * upscale_factor * upscale_factor + (gy % upscale_factor) * upscale_factor + gx % upscale_factor; |
| 88 | } |
| 89 | else // if (mode == 1) |
| 90 | { |
| 91 | z4 = ((gy % upscale_factor) * upscale_factor + gx % upscale_factor) * psc(outc) * 8 + gz4; |
| 92 | zz4 = ((gy % upscale_factor) * upscale_factor + gx % upscale_factor) * psc(outc) * 8 + gzz4; |
| 93 | } |
| 94 | |
| 95 | #if NCNN_image_shader |
| 96 | afpvec8 v0 = image3d_ld8(bottom_blob, ivec3(x, y, z4.r / 8)); |
| 97 | afpvec8 v1 = image3d_ld8(bottom_blob, ivec3(x, y, z4.g / 8)); |
| 98 | afpvec8 v2 = image3d_ld8(bottom_blob, ivec3(x, y, z4.b / 8)); |
| 99 | afpvec8 v3 = image3d_ld8(bottom_blob, ivec3(x, y, z4.a / 8)); |
| 100 | afpvec8 v4 = image3d_ld8(bottom_blob, ivec3(x, y, zz4.r / 8)); |
| 101 | afpvec8 v5 = image3d_ld8(bottom_blob, ivec3(x, y, zz4.g / 8)); |
| 102 | afpvec8 v6 = image3d_ld8(bottom_blob, ivec3(x, y, zz4.b / 8)); |
| 103 | afpvec8 v7 = image3d_ld8(bottom_blob, ivec3(x, y, zz4.a / 8)); |
| 104 | |
| 105 | afpvec8 v; |
| 106 | v[0].r = v0[(z4.r % 8) / 4][z4.r % 4]; |
| 107 | v[0].g = v1[(z4.g % 8) / 4][z4.g % 4]; |
| 108 | v[0].b = v2[(z4.b % 8) / 4][z4.b % 4]; |
| 109 | v[0].a = v3[(z4.a % 8) / 4][z4.a % 4]; |
| 110 | v[1].r = v4[(zz4.r % 8) / 4][zz4.r % 4]; |
| 111 | v[1].g = v5[(zz4.g % 8) / 4][zz4.g % 4]; |
| 112 | v[1].b = v6[(zz4.b % 8) / 4][zz4.b % 4]; |
| 113 | v[1].a = v7[(zz4.a % 8) / 4][zz4.a % 4]; |
| 114 | |
| 115 | image3d_st8(top_blob, ivec3(gx, gy, gz), v); |
| 116 | #else |
| 117 | int gi = gz * psc(outcstep) + gy * psc(outw) + gx; |
| 118 | |
| 119 | #if NCNN_fp16_packed |
| 120 | ivec4 v_offset = ((z4 / 8) * psc(cstep) + y * psc(w) + x) * 4 + (z4 % 8) / 2; |
| 121 | ivec4 lane2 = z4 % 2; |
| 122 | ivec4 vv_offset = ((zz4 / 8) * psc(cstep) + y * psc(w) + x) * 4 + (zz4 % 8) / 2; |
| 123 | ivec4 lane4 = zz4 % 2; |
| 124 | |
| 125 | afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r); |
| 126 | afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g); |
| 127 | afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b); |
| 128 | afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a); |
| 129 | |
| 130 | afpvec2 vvr = buffer_ld2(bottom_blob_data, vv_offset.r); |
| 131 | afpvec2 vvg = buffer_ld2(bottom_blob_data, vv_offset.g); |
| 132 | afpvec2 vvb = buffer_ld2(bottom_blob_data, vv_offset.b); |
| 133 | afpvec2 vva = buffer_ld2(bottom_blob_data, vv_offset.a); |
| 134 | |
| 135 | afpvec8 v = afpvec8(vr[lane2.r], vg[lane2.g], vb[lane2.b], va[lane2.a], vvr[lane4.r], vvg[lane4.g], vvb[lane4.b], vva[lane4.a]); |
| 136 | |
| 137 | buffer_st8(top_blob_data, gi, v); |
| 138 | #else |
| 139 | ivec4 v_offset = ((z4 / 8) * psc(cstep) + y * psc(w) + x) * 8 + z4 % 8; |
| 140 | ivec4 vv_offset = ((zz4 / 8) * psc(cstep) + y * psc(w) + x) * 8 + zz4 % 8; |
| 141 | |
| 142 | buffer_cp1to8(top_blob_data, gi, bottom_blob_data, v_offset, vv_offset); |
| 143 | #endif |
| 144 | #endif |
| 145 | } |