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

#version 450

layout (constant_id = 0) const int ndim = 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 { 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 i4;
    if (ndim == 1) i4 = gx * 4 + ivec4(0, 1, 2, 3);
    if (ndim == 2) i4 = (gy * 4) * psc(outw) + gx + ivec4(0, 1, 2, 3) * psc(outw);
    if (ndim == 3) i4 = (gz * 4) * psc(outh) * psc(outw) + gy * psc(outw) + gx + ivec4(0, 1, 2, 3) * psc(outh) * psc(outw);
    if (ndim == 4) i4 = (gz * 4) * psc(outd) * psc(outh) * psc(outw) + gy * psc(outw) + gx + ivec4(0, 1, 2, 3) * psc(outd) * psc(outh) * psc(outw);

    ivec4 x4;
    ivec4 y4;
    ivec4 z4;

    ivec4 v_offset;
#if NCNN_fp16_packed
    ivec4 lane2;
#endif

    if (psc(dims) == 1)
    {
        z4 = ivec4(0);
        y4 = ivec4(0);
        x4 = i4;

#if NCNN_fp16_packed
        v_offset = i4 / 2;
        lane2 = i4 % 2;
#else
        v_offset = i4;
#endif
    }
    else if (psc(dims) == 2)
    {
        z4 = ivec4(0);
        y4 = i4 / psc(w);
        x4 = i4 % psc(w);

#if NCNN_fp16_packed

        v_offset = ((y4 / 8) * psc(w) + x4) * 4 + (y4 % 8) / 2;
        lane2 = y4 % 2;
#else
        v_offset = ((y4 / 8) * psc(w) + x4) * 8 + y4 % 8;
#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 / 8) * psc(cstep) + y4 * psc(w) + x4) * 4 + (z4 % 8) / 2;
        lane2 = z4 % 2;
#else
        v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 8 + z4 % 8;
#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 / 8) * psc(cstep) + y4 * psc(w) + x4) * 4 + (z4 % 8) / 2;
        lane2 = z4 % 2;
#else
        v_offset = ((z4 / 8) * psc(cstep) + y4 * psc(w) + x4) * 8 + z4 % 8;
#endif
    }

    int gi;
    if (ndim == 1) gi = gx;
    if (ndim == 2) gi = gy * psc(outw) + gx;
    if (ndim == 3 || ndim == 4) 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
}
