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

#version 450

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

#define shape_constant_id_offset 2
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 { sfpvec4 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;
} 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;

    ivec4 x4;
    ivec4 y4;
    ivec4 z4;

    if (psc(dims) == 2)
    {
        // order_type
        // 0 = w h
        // 1 = h w

        gz = 0;
        z4 = ivec4(0);

        if (order_type == 0)
        {
            x4 = ivec4(gx);
            y4 = gy * 4 + ivec4(0, 1, 2, 3);
        }
        if (order_type == 1)
        {
            x4 = gy * 4 + ivec4(0, 1, 2, 3);
            y4 = ivec4(gx);
        }
    }
    else if (psc(dims) == 3)
    {
        // order_type
        // 0 = w h c
        // 1 = h w c
        // 2 = w c h
        // 3 = c w h
        // 4 = h c w
        // 5 = c h w

        if (order_type == 0)
        {
            x4 = ivec4(gx);
            y4 = ivec4(gy);
            z4 = gz * 4 + ivec4(0, 1, 2, 3);
        }
        if (order_type == 1)
        {
            x4 = ivec4(gy);
            y4 = ivec4(gx);
            z4 = gz * 4 + ivec4(0, 1, 2, 3);
        }
        if (order_type == 2)
        {
            x4 = ivec4(gx);
            y4 = gz * 4 + ivec4(0, 1, 2, 3);
            z4 = ivec4(gy);
        }
        if (order_type == 3)
        {
            x4 = ivec4(gy);
            y4 = gz * 4 + ivec4(0, 1, 2, 3);
            z4 = ivec4(gx);
        }
        if (order_type == 4)
        {
            x4 = gz * 4 + ivec4(0, 1, 2, 3);
            y4 = ivec4(gx);
            z4 = ivec4(gy);
        }
        if (order_type == 5)
        {
            x4 = gz * 4 + ivec4(0, 1, 2, 3);
            y4 = ivec4(gy);
            z4 = ivec4(gx);
        }
    }
    else // if (psc(dims) == 4)
    {
        // order_type
        // 0 = w h d c
        // 1 = h w d c
        // 2 = w d h c
        // 3 = d w h c
        // 4 = h d w c
        // 5 = d h w c
        // 6 = w h c d
        // 7 = h w c d
        // 8 = w c h d
        // 9 = c w h d
        //10 = h c w d
        //11 = c h w d
        //12 = w d c h
        //13 = d w c h
        //14 = w c d h
        //15 = c w d h
        //16 = d c w h
        //17 = c d w h
        //18 = h d c w
        //19 = d h c w
        //20 = h c d w
        //21 = c h d w
        //22 = d c h w
        //23 = c d h w

        int yd = gy / psc(outh);
        int yh = gy % psc(outh);

        if (order_type == 0)
        {
            x4 = ivec4(gx);
            y4 = ivec4(yd * psc(h) + yh);
            z4 = gz * 4 + ivec4(0, 1, 2, 3);
        }
        if (order_type == 1)
        {
            x4 = ivec4(yh);
            y4 = ivec4(yd * psc(h) + gx);
            z4 = gz * 4 + ivec4(0, 1, 2, 3);
        }
        if (order_type == 2)
        {
            x4 = ivec4(gx);
            y4 = ivec4(yh * psc(h) + yd);
            z4 = gz * 4 + ivec4(0, 1, 2, 3);
        }
        if (order_type == 3)
        {
            x4 = ivec4(yh);
            y4 = ivec4(gx * psc(h) + yd);
            z4 = gz * 4 + ivec4(0, 1, 2, 3);
        }
        if (order_type == 4)
        {
            x4 = ivec4(yd);
            y4 = ivec4(yh * psc(h) + gx);
            z4 = gz * 4 + ivec4(0, 1, 2, 3);
        }
        if (order_type == 5)
        {
            x4 = ivec4(yd);
            y4 = ivec4(gx * psc(h) + yh);
            z4 = gz * 4 + ivec4(0, 1, 2, 3);
        }
        if (order_type == 6)
        {
            x4 = ivec4(gx);
            y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yh;
            z4 = ivec4(yd);
        }
        if (order_type == 7)
        {
            x4 = ivec4(yh);
            y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + gx;
            z4 = ivec4(yd);
        }
        if (order_type == 8)
        {
            x4 = ivec4(gx);
            y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yd;
            z4 = ivec4(yh);
        }
        if (order_type == 9)
        {
            x4 = ivec4(yh);
            y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yd;
            z4 = ivec4(gx);
        }
        if (order_type == 10)
        {
            x4 = ivec4(yd);
            y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + gx;
            z4 = ivec4(yh);
        }
        if (order_type == 11)
        {
            x4 = ivec4(yd);
            y4 = (gz * 4 + ivec4(0, 1, 2, 3)) * psc(h) + yh;
            z4 = ivec4(gx);
        }
        if (order_type == 12)
        {
            x4 = ivec4(gx);
            y4 = yh * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
            z4 = ivec4(yd);
        }
        if (order_type == 13)
        {
            x4 = ivec4(yh);
            y4 = gx * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
            z4 = ivec4(yd);
        }
        if (order_type == 14)
        {
            x4 = ivec4(gx);
            y4 = yd * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
            z4 = ivec4(yh);
        }
        if (order_type == 15)
        {
            x4 = ivec4(yh);
            y4 = yd * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
            z4 = ivec4(gx);
        }
        if (order_type == 16)
        {
            x4 = ivec4(yd);
            y4 = gx * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
            z4 = ivec4(yh);
        }
        if (order_type == 17)
        {
            x4 = ivec4(yd);
            y4 = yh * psc(h) + gz * 4 + ivec4(0, 1, 2, 3);
            z4 = ivec4(gx);
        }
        if (order_type == 18)
        {
            x4 = gz * 4 + ivec4(0, 1, 2, 3);
            y4 = ivec4(yh * psc(h) + gx);
            z4 = ivec4(yd);
        }
        if (order_type == 19)
        {
            x4 = gz * 4 + ivec4(0, 1, 2, 3);
            y4 = ivec4(gx * psc(h) + yh);
            z4 = ivec4(yd);
        }
        if (order_type == 20)
        {
            x4 = gz * 4 + ivec4(0, 1, 2, 3);
            y4 = ivec4(yd * psc(h) + gx);
            z4 = ivec4(yh);
        }
        if (order_type == 21)
        {
            x4 = gz * 4 + ivec4(0, 1, 2, 3);
            y4 = ivec4(yd * psc(h) + yh);
            z4 = ivec4(gx);
        }
        if (order_type == 22)
        {
            x4 = gz * 4 + ivec4(0, 1, 2, 3);
            y4 = ivec4(gx * psc(h) + yd);
            z4 = ivec4(yh);
        }
        if (order_type == 23)
        {
            x4 = gz * 4 + ivec4(0, 1, 2, 3);
            y4 = ivec4(yh * psc(h) + yd);
            z4 = ivec4(gx);
        }
    }

    ivec4 i4 = z4 * psc(cstep) + y4 * psc(w) + x4;
    ivec4 v_offset;
#if NCNN_fp16_packed
    ivec4 lane2;
#endif

    if (psc(dims) == 2)
    {
        y4 = i4 / psc(w);
        x4 = i4 % psc(w);

#if NCNN_fp16_packed
        v_offset = ((y4 / 4) * psc(w) + x4) * 2 + (y4 % 4) / 2;
        lane2 = y4 % 2;
#else
        v_offset = ((y4 / 4) * psc(w) + x4) * 4 + y4 % 4;
#endif
    }
    else if (psc(dims) == 3)
    {
        int size = psc(w) * psc(h);

        z4 = i4 / size;
        y4 = i4 % size / psc(w);
        x4 = i4 % size % psc(w);

#if NCNN_fp16_packed
        v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 2 + (z4 % 4) / 2;
        lane2 = z4 % 2;
#else
        v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 4 + z4 % 4;
#endif
    }
    else // if (psc(dims) == 4)
    {
        int size = psc(w) * psc(h) * psc(d);
        int dsize = psc(w) * psc(h);

        z4 = i4 / size;
        ivec4 yd4 = i4 % size / dsize;
        ivec4 yh4 = i4 % size % dsize / psc(w);
        x4 = i4 % size % dsize % psc(w);

        y4 = yd4 * psc(h) + yh4;

#if NCNN_fp16_packed
        v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 2 + (z4 % 4) / 2;
        lane2 = z4 % 2;
#else
        v_offset = ((z4 / 4) * psc(cstep) + y4 * psc(w) + x4) * 4 + z4 % 4;
#endif
    }

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

#if NCNN_fp16_packed
    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);

    afpvec4 v = afpvec4(vr[lane2.r], vg[lane2.g], vb[lane2.b], va[lane2.a]);

    buffer_st4(top_blob_data, gi, v);
#else
    buffer_cp1to4(top_blob_data, gi, bottom_blob_data, v_offset);
#endif
}
