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

#version 450

layout (constant_id = 0) const int type = 1;
layout (constant_id = 1) const float value = 0;
layout (constant_id = 2) const int per_channel_pad = 0;

#define shape_constant_id_offset 3
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 { sfpvec4 bottom_blob_data[]; };
layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
layout (binding = 2) readonly buffer per_channel_pad_blob { sfpvec4 per_channel_pad_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 left;
    int top;
    int front;
} 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;

    afpvec4 v;

    if (psc(dims) == 1)
    {
        int x = gx - p.left / 4;

        if (type == 0)
        {
            // nvidia driver crash when using load and store pair  :(
            // copy is the workaround  --- nihui
            if (x >= 0 && x < psc(w) * 2)
            {
                buffer_cp4(top_blob_data, gx, bottom_blob_data, x);
            }
            else
            {
                // nvidia driver is unhappy if we do not touch the v variable here  :<
                v = afpvec4(value);
                buffer_st4(top_blob_data, gx, v);
                // buffer_st4(top_blob_data, gx, afpvec4(value));
            }
        }
        if (type == 1)
        {
            if (x < 0)
            {
                v = afpvec4(buffer_ld4(bottom_blob_data, 0).r);
            }
            else if (x >= psc(w) * 2)
            {
                v = afpvec4(buffer_ld4(bottom_blob_data, psc(w) * 2 - 1).a);
            }
            else
            {
                v = buffer_ld4(bottom_blob_data, (x / 2) * 2 + x % 2);
            }

            buffer_st4(top_blob_data, gx, v);
        }
        if (type == 2)
        {
            if (x < 0)
            {
                ivec2 x01 = -x + ivec2(1, 0);
                afpvec4 v0 = buffer_ld4(bottom_blob_data, (x01.x / 2) * 2 + x01.x % 2);
                afpvec4 v1 = buffer_ld4(bottom_blob_data, (x01.y / 2) * 2 + x01.y % 2);
                v = afpvec4(v1.r, v0.a, v0.b, v0.g);
            }
            else if (x >= psc(w) * 2)
            {
                ivec2 x01 = psc(w) * 2 - x + psc(w) * 2 - 1 - ivec2(1, 0);
                afpvec4 v0 = buffer_ld4(bottom_blob_data, (x01.x / 2) * 2 + x01.x % 2);
                afpvec4 v1 = buffer_ld4(bottom_blob_data, (x01.y / 2) * 2 + x01.y % 2);
                v = afpvec4(v1.b, v1.g, v1.r, v0.a);
            }
            else
            {
                v = buffer_ld4(bottom_blob_data, (x / 2) * 2 + x % 2);
            }

            buffer_st4(top_blob_data, gx, v);
        }
    }
    else if (psc(dims) == 2)
    {
        int x = gx - p.left;
        int y = gy - p.top / 4;

        if (type == 0)
        {
            if (x >= 0 && x < psc(w) && y >= 0 && y < psc(h) * 2)
            {
                v = buffer_ld4(bottom_blob_data, (y / 2) * psc(w) * 2 + x * 2 + y % 2);
            }
            else
            {
                v = afpvec4(value);
            }

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

            buffer_st4(top_blob_data, gi, v);
        }
        if (type == 1)
        {
            x = clamp(x, 0, psc(w) * 2 - 1);

            if (y < 0)
            {
                v = afpvec4(buffer_ld4(bottom_blob_data, x * 2).r);
            }
            else if (y >= psc(h) * 2)
            {
                v = afpvec4(buffer_ld4(bottom_blob_data, (psc(h) * 2 - 1) * psc(w) * 2 + x * 2 + 1).a);
            }
            else
            {
                v = buffer_ld4(bottom_blob_data, (y / 2) * psc(w) * 2 + x * 2 + y % 2);
            }

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

            buffer_st4(top_blob_data, gi, v);
        }
        if (type == 2)
        {
            x = abs(x);
            // NOTE psc(X) get zeros on nvidia
            // TODO only enable this workaround for some nvidia driver
            x = (p.w * 2 - 1) - abs(x - (p.w * 2 - 1));
//             x = (psc(w) * 2 - 1) - abs(x - (psc(w) * 2 - 1));

            if (y < 0)
            {
                ivec2 y01 = -y + ivec2(1, 0);
                afpvec4 v0 = buffer_ld4(bottom_blob_data, (y01.x / 2) * psc(w) * 2 + x * 2 + y01.x % 2);
                afpvec4 v1 = buffer_ld4(bottom_blob_data, (y01.y / 2) * psc(w) * 2 + x * 2 + y01.y % 2);
                v = afpvec4(v1.r, v0.a, v0.b, v0.g);
            }
            else if (y >= psc(h) * 2)
            {
                ivec2 y01 = psc(h) * 2 - y + psc(h) * 2 - 1 - ivec2(1, 0);
                afpvec4 v0 = buffer_ld4(bottom_blob_data, (y01.x / 2) * psc(w) * 2 + x * 2 + y01.x % 2);
                afpvec4 v1 = buffer_ld4(bottom_blob_data, (y01.y / 2) * psc(w) * 2 + x * 2 + y01.y % 2);
                v = afpvec4(v1.b, v1.g, v1.r, v0.a);
            }
            else
            {
                v = buffer_ld4(bottom_blob_data, (y / 2) * psc(w) + x * 2 + y % 2);
            }

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

            buffer_st4(top_blob_data, gi, v);
        }
    }
    else // if (psc(dims) == 3)
    {
        int x = gx - p.left;
        int y = gy - p.top;
        int z = gz - p.front / 4;

        if (type == 0)
        {
            if (x >= 0 && x < psc(w) && y >= 0 && y < psc(h) && z >= 0 && z < psc(c) * 2)
            {
                v = buffer_ld4(bottom_blob_data, ((z / 2) * psc(cstep) + y * psc(w)) * 2 + x * 2 + z % 2);
            }
            else
            {
                v = per_channel_pad == 1 ? buffer_ld4(per_channel_pad_blob_data, gz) : afpvec4(value);
            }

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

            buffer_st4(top_blob_data, gi, v);
        }
        if (type == 1)
        {
            x = clamp(x, 0, psc(w) * 2 - 1);
            y = clamp(y, 0, psc(h) * 2 - 1);

            if (z < 0)
            {
                v = afpvec4(buffer_ld4(bottom_blob_data, y * psc(w) * 2 + x * 2).r);
            }
            else if (z >= psc(c) * 2)
            {
                v = afpvec4(buffer_ld4(bottom_blob_data, ((psc(c) * 2 - 1) * psc(cstep) + y * psc(w)) * 2 + x * 2 + 1).a);
            }
            else
            {
                v = buffer_ld4(bottom_blob_data, ((z / 2) * psc(cstep) + y * psc(w)) * 2 + x * 2 + y % 2);
            }

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

            buffer_st4(top_blob_data, gi, v);
        }
        if (type == 2)
        {
            x = abs(x);
            y = abs(y);
            // NOTE psc(X) get zeros on nvidia
            // TODO only enable this workaround for some nvidia driver
            x = (p.w * 2 - 1) - abs(x - (p.w * 2 - 1));
            y = (p.h * 2 - 1) - abs(y - (p.h * 2 - 1));
//             x = (psc(w) * 2 - 1) - abs(x - (psc(w) * 2 - 1));
//             y = (psc(h) * 2 - 1) - abs(y - (psc(h) * 2 - 1));

            if (z < 0)
            {
                ivec2 z01 = -y + ivec2(1, 0);
                afpvec4 v0 = buffer_ld4(bottom_blob_data, ((z01.x / 2) * psc(cstep) + y * psc(w)) * 2 + x * 2 + z01.x % 2);
                afpvec4 v1 = buffer_ld4(bottom_blob_data, ((z01.y / 2) * psc(cstep) + y * psc(w)) * 2 + x * 2 + z01.y % 2);
                v = afpvec4(v1.r, v0.a, v0.b, v0.g);
            }
            else if (z >= psc(c) * 2)
            {
                ivec2 z01 = psc(c) * 2 - z + psc(c) * 2 - 1 - ivec2(1, 0);
                afpvec4 v0 = buffer_ld4(bottom_blob_data, ((z01.x / 2) * psc(cstep) + y * psc(w)) * 2 + x * 2 + z01.x % 2);
                afpvec4 v1 = buffer_ld4(bottom_blob_data, ((z01.y / 2) * psc(cstep) + y * psc(w)) * 2 + x * 2 + z01.y % 2);
                v = afpvec4(v1.b, v1.g, v1.r, v0.a);
            }
            else
            {
                v = buffer_ld4(bottom_blob_data, ((z / 2) * psc(cstep) + y * psc(w)) * 2 + x * 2 + y % 2);
            }

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

            buffer_st4(top_blob_data, gi, v);
        }
    }
}
