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

#version 450

layout (constant_id = 0) const int group = 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 c = 0;
layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;

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

layout (binding = 0) readonly buffer bottom_blob { sfpvec8 bottom_blob_data[]; };
layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };

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

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

    int group;
} 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) || gz >= psc(outc))
        return;

    const int channels_g = psc(c) * 8 / psc(group);

    ivec4 gz4 = ivec4(gz * 8) + ivec4(0, 1, 2, 3);
    ivec4 gzz4 = gz4 + 4;

    ivec4 z4 = (gz4 % psc(group)) * channels_g + (gz4 / psc(group));
    ivec4 zz4 = (gzz4 % psc(group)) * channels_g + (gzz4 / psc(group));

    ivec4 v_offset = (z4 / 8) * psc(cstep) + gy * psc(w) + gx;
    ivec4 vv_offset = (zz4 / 8) * psc(cstep) + gy * psc(w) + gx;

    // v = v4[lane]

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

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

    ivec4 sz4 = (z4 % 8) / 4;
    ivec4 szz4 = (zz4 % 8) / 4;

    ivec4 lane4 = z4 % 4;
    ivec4 lane8 = zz4 % 4;

    afpvec8 v;
#if NCNN_fp16_arithmetic
    if (bugihfa == 1)
    {
        if (lane4.r == 0) v[0].r = vr[sz4.r].r;
        if (lane4.r == 1) v[0].r = vr[sz4.r].g;
        if (lane4.r == 2) v[0].r = vr[sz4.r].b;
        if (lane4.r == 3) v[0].r = vr[sz4.r].a;
        if (lane4.g == 0) v[0].g = vr[sz4.g].r;
        if (lane4.g == 1) v[0].g = vr[sz4.g].g;
        if (lane4.g == 2) v[0].g = vr[sz4.g].b;
        if (lane4.g == 3) v[0].g = vr[sz4.g].a;
        if (lane4.b == 0) v[0].b = vr[sz4.b].r;
        if (lane4.b == 1) v[0].b = vr[sz4.b].g;
        if (lane4.b == 2) v[0].b = vr[sz4.b].b;
        if (lane4.b == 3) v[0].b = vr[sz4.b].a;
        if (lane4.a == 0) v[0].a = vr[sz4.a].r;
        if (lane4.a == 1) v[0].a = vr[sz4.a].g;
        if (lane4.a == 2) v[0].a = vr[sz4.a].b;
        if (lane4.a == 3) v[0].a = vr[sz4.a].a;
        if (lane8.r == 0) v[1].r = vvr[szz4.r].r;
        if (lane8.r == 1) v[1].r = vvr[szz4.r].g;
        if (lane8.r == 2) v[1].r = vvr[szz4.r].b;
        if (lane8.r == 3) v[1].r = vvr[szz4.r].a;
        if (lane8.g == 0) v[1].g = vvr[szz4.g].r;
        if (lane8.g == 1) v[1].g = vvr[szz4.g].g;
        if (lane8.g == 2) v[1].g = vvr[szz4.g].b;
        if (lane8.g == 3) v[1].g = vvr[szz4.g].a;
        if (lane8.b == 0) v[1].b = vvr[szz4.b].r;
        if (lane8.b == 1) v[1].b = vvr[szz4.b].g;
        if (lane8.b == 2) v[1].b = vvr[szz4.b].b;
        if (lane8.b == 3) v[1].b = vvr[szz4.b].a;
        if (lane8.a == 0) v[1].a = vvr[szz4.a].r;
        if (lane8.a == 1) v[1].a = vvr[szz4.a].g;
        if (lane8.a == 2) v[1].a = vvr[szz4.a].b;
        if (lane8.a == 3) v[1].a = vvr[szz4.a].a;
    }
    else
#endif
    {
        v = afpvec8(vr[sz4.r][lane4.r], vg[sz4.g][lane4.g], vb[sz4.b][lane4.b], va[sz4.a][lane4.a], vvr[szz4.r][lane8.r], vvg[szz4.g][lane8.g], vvb[szz4.b][lane8.b], vva[szz4.a][lane8.a]);
    }

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

    buffer_st8(top_blob_data, gi, v);
}
