// Copyright 2020 Tencent
// SPDX-License-Identifier: BSD-3-Clause

#version 450

layout (constant_id = 0) const int bugihfa = 0;

#define shape_constant_id_offset 1
layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
layout (constant_id = shape_constant_id_offset + 3) const int d = 0;
layout (constant_id = shape_constant_id_offset + 4) const int c = 0;
layout (constant_id = shape_constant_id_offset + 5) const int cstep = 0;

layout (constant_id = shape_constant_id_offset + 6) const int outdims = 0;
layout (constant_id = shape_constant_id_offset + 7) const int outw = 0;
layout (constant_id = shape_constant_id_offset + 8) const int outh = 0;
layout (constant_id = shape_constant_id_offset + 9) const int outd = 0;
layout (constant_id = shape_constant_id_offset + 10) const int outc = 0;
layout (constant_id = shape_constant_id_offset + 11) const int outcstep = 0;

#if NCNN_fp16_packed
layout (binding = 0) readonly buffer bottom_blob { sfpvec2 bottom_blob_data[]; };
#else
layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
#endif
layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };

layout (push_constant) uniform parameter
{
    int dims;
    int w;
    int h;
    int d;
    int c;
    int cstep;

    int outdims;
    int outw;
    int outh;
    int outd;
    int outc;
    int outcstep;

    int woffset;
    int hoffset;
    int doffset;
    int coffset;
} p;

void main()
{
    int gx = int(gl_GlobalInvocationID.x);
    int gy = int(gl_GlobalInvocationID.y);
    int gz = int(gl_GlobalInvocationID.z);

    if (gx >= psc(outw) || gy >= psc(outh) * psc(outd) || gz >= psc(outc))
        return;

    if (psc(dims) == 1)
    {
        ivec4 x4 = gx * 8 + p.woffset + ivec4(0, 1, 2, 3);
        ivec4 xx4 = x4 + 4;

#if NCNN_fp16_packed
        ivec4 v_offset = (x4 / 4) * 2 + (x4 % 4) / 2;
        ivec4 lane2 = x4 % 2;
        ivec4 vv_offset = (xx4 / 4) * 2 + (xx4 % 4) / 2;
        ivec4 lane4 = xx4 % 2;

        afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r);
        afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g);
        afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b);
        afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a);

        afpvec2 vvr = buffer_ld2(bottom_blob_data, vv_offset.r);
        afpvec2 vvg = buffer_ld2(bottom_blob_data, vv_offset.g);
        afpvec2 vvb = buffer_ld2(bottom_blob_data, vv_offset.b);
        afpvec2 vva = buffer_ld2(bottom_blob_data, vv_offset.a);

        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]);

        buffer_st8(top_blob_data, gx, v);
#else
        ivec4 v_offset = (x4 / 4) * 4 + x4 % 4;
        ivec4 vv_offset = (xx4 / 4) * 4 + xx4 % 4;

        buffer_cp1to8(top_blob_data, gx, bottom_blob_data, v_offset, vv_offset);
#endif
    }
    else if (psc(dims) == 2)
    {
        int x = gx + p.woffset;
        ivec4 y4 = gy * 8 + p.hoffset + ivec4(0, 1, 2, 3);
        ivec4 yy4 = y4 + 4;

        int gi = gy * psc(outw) + gx;

#if NCNN_fp16_packed
        ivec4 v_offset = ((y4 / 4) * psc(w) + x) * 2 + (y4 % 4) / 2;
        ivec4 lane2 = y4 % 2;
        ivec4 vv_offset = ((yy4 / 4) * psc(w) + x) * 2 + (yy4 % 4) / 2;
        ivec4 lane4 = yy4 % 2;

        afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r);
        afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g);
        afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b);
        afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a);

        afpvec2 vvr = buffer_ld2(bottom_blob_data, vv_offset.r);
        afpvec2 vvg = buffer_ld2(bottom_blob_data, vv_offset.g);
        afpvec2 vvb = buffer_ld2(bottom_blob_data, vv_offset.b);
        afpvec2 vva = buffer_ld2(bottom_blob_data, vv_offset.a);

        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]);

        buffer_st8(top_blob_data, gi, v);
#else
        ivec4 v_offset = ((y4 / 4) * psc(w) + x) * 4 + y4 % 4;
        ivec4 vv_offset = ((yy4 / 4) * psc(w) + x) * 4 + yy4 % 4;

        buffer_cp1to8(top_blob_data, gi, bottom_blob_data, v_offset, vv_offset);
#endif
    }
    else if (psc(dims) == 3)
    {
        int x = gx + p.woffset;
        int y = gy + p.hoffset;
        ivec4 z4 = gz * 8 + p.coffset + ivec4(0, 1, 2, 3);
        ivec4 zz4 = z4 + 4;

        int gi = gz * psc(outcstep) + gy * psc(outw) + gx;

#if NCNN_fp16_packed
        ivec4 v_offset = ((z4 / 4) * psc(cstep) + y * psc(w) + x) * 2 + (z4 % 4) / 2;
        ivec4 lane2 = z4 % 2;
        ivec4 vv_offset = ((zz4 / 4) * psc(cstep) + y * psc(w) + x) * 2 + (zz4 % 4) / 2;
        ivec4 lane4 = zz4 % 2;

        afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r);
        afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g);
        afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b);
        afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a);

        afpvec2 vvr = buffer_ld2(bottom_blob_data, vv_offset.r);
        afpvec2 vvg = buffer_ld2(bottom_blob_data, vv_offset.g);
        afpvec2 vvb = buffer_ld2(bottom_blob_data, vv_offset.b);
        afpvec2 vva = buffer_ld2(bottom_blob_data, vv_offset.a);

        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]);

        buffer_st8(top_blob_data, gi, v);
#else
        ivec4 v_offset = ((z4 / 4) * psc(cstep) + y * psc(w) + x) * 4 + z4 % 4;
        ivec4 vv_offset = ((zz4 / 4) * psc(cstep) + y * psc(w) + x) * 4 + zz4 % 4;

        buffer_cp1to8(top_blob_data, gi, bottom_blob_data, v_offset, vv_offset);
#endif
    }
    else // if (psc(dims) == 4)
    {
        int yd = gy / psc(outh);
        int yh = gy % psc(outh);

        int x = gx + p.woffset;
        int y = (yd + p.doffset) * psc(h) + (yh + p.hoffset);
        ivec4 z4 = gz * 8 + p.coffset + ivec4(0, 1, 2, 3);
        ivec4 zz4 = z4 + 4;

        int gi = gz * psc(outcstep) + gy * psc(outw) + gx;

#if NCNN_fp16_packed
        ivec4 v_offset = ((z4 / 4) * psc(cstep) + y * psc(w) + x) * 2 + (z4 % 4) / 2;
        ivec4 lane2 = z4 % 2;
        ivec4 vv_offset = ((zz4 / 4) * psc(cstep) + y * psc(w) + x) * 2 + (zz4 % 4) / 2;
        ivec4 lane4 = zz4 % 2;

        afpvec2 vr = buffer_ld2(bottom_blob_data, v_offset.r);
        afpvec2 vg = buffer_ld2(bottom_blob_data, v_offset.g);
        afpvec2 vb = buffer_ld2(bottom_blob_data, v_offset.b);
        afpvec2 va = buffer_ld2(bottom_blob_data, v_offset.a);

        afpvec2 vvr = buffer_ld2(bottom_blob_data, vv_offset.r);
        afpvec2 vvg = buffer_ld2(bottom_blob_data, vv_offset.g);
        afpvec2 vvb = buffer_ld2(bottom_blob_data, vv_offset.b);
        afpvec2 vva = buffer_ld2(bottom_blob_data, vv_offset.a);

        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]);

        buffer_st8(top_blob_data, gi, v);
#else
        ivec4 v_offset = ((z4 / 4) * psc(cstep) + y * psc(w) + x) * 4 + z4 % 4;
        ivec4 vv_offset = ((zz4 / 4) * psc(cstep) + y * psc(w) + x) * 4 + zz4 % 4;

        buffer_cp1to8(top_blob_data, gi, bottom_blob_data, v_offset, vv_offset);
#endif
    }
}
