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

#version 450

#define LOCAL_MEMORY_UNROLL_INCH 8

layout (constant_id = 0) const int maxk = 1;

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

layout (constant_id = shape_constant_id_offset + 4) const int outcstep = 0;
layout (constant_id = shape_constant_id_offset + 5) const int outc = 0;

layout (binding = 0) readonly buffer bottom_blob { sfpvec4 bottom_blob_data[]; };
layout (binding = 1) writeonly buffer col_blob { sfp col_blob_data[]; };
layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; };

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

    int outcstep;
    int outc;
} p;

#if NCNN_shader_local_memory
shared lfpvec4 tmp_v[8][LOCAL_MEMORY_UNROLL_INCH][4];
shared lfpvec4 tmp_k[8][LOCAL_MEMORY_UNROLL_INCH];
#endif

void main()
{
    int gx = int(gl_GlobalInvocationID.x) * 4;
    int gy = int(gl_GlobalInvocationID.y);

#if !NCNN_shader_local_memory
    if (gx >= psc(outcstep) || gy >= psc(outc))
        return;
#endif

    afp sum0 = afp(0.f);
    afp sum1 = afp(0.f);
    afp sum2 = afp(0.f);
    afp sum3 = afp(0.f);

    int v_offset = gx;
    int w_offset = gy * psc(c);

#if NCNN_shader_local_memory
    const int lx = int(gl_LocalInvocationID.x);
    const int ly = int(gl_LocalInvocationID.y);

    int z = 0;
    for (; z + (LOCAL_MEMORY_UNROLL_INCH - 1) < psc(c); z += LOCAL_MEMORY_UNROLL_INCH)
    {
        if (ly < 4)
        {
            for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++)
            {
                tmp_v[lx][z4][ly] = sfp2lfpvec4(bottom_blob_data[v_offset + z4 * psc(cstep) + ly]);
            }
        }

        if (lx == 0)
        {
            for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++)
            {
                tmp_k[ly][z4] = sfp2lfpvec4(weight_data[w_offset + z4]);
            }
        }

        barrier();

        for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++)
        {
            afpvec4 v0 = lfp2afpvec4(tmp_v[lx][z4][0]);
            afpvec4 v1 = lfp2afpvec4(tmp_v[lx][z4][1]);
            afpvec4 v2 = lfp2afpvec4(tmp_v[lx][z4][2]);
            afpvec4 v3 = lfp2afpvec4(tmp_v[lx][z4][3]);

            afpvec4 k = lfp2afpvec4(tmp_k[ly][z4]);

            sum0 += dot(v0, k);
            sum1 += dot(v1, k);
            sum2 += dot(v2, k);
            sum3 += dot(v3, k);
        }

        v_offset += LOCAL_MEMORY_UNROLL_INCH * psc(cstep);
        w_offset += LOCAL_MEMORY_UNROLL_INCH;

        barrier();
    }

    if (z < psc(c))
    {
        const int remain = psc(c) - z;

        if (ly < 4)
        {
            for (int z4 = 0; z4 < remain; z4++)
            {
                tmp_v[lx][z4][ly] = sfp2lfpvec4(bottom_blob_data[v_offset + z4 * psc(cstep) + ly]);
            }
        }

        if (lx == 0)
        {
            for (int z4 = 0; z4 < remain; z4++)
            {
                tmp_k[ly][z4] = sfp2lfpvec4(weight_data[w_offset + z4]);
            }
        }

        barrier();

        for (int z4 = 0; z4 < remain; z4++)
        {
            afpvec4 v0 = lfp2afpvec4(tmp_v[lx][z4][0]);
            afpvec4 v1 = lfp2afpvec4(tmp_v[lx][z4][1]);
            afpvec4 v2 = lfp2afpvec4(tmp_v[lx][z4][2]);
            afpvec4 v3 = lfp2afpvec4(tmp_v[lx][z4][3]);

            afpvec4 k = lfp2afpvec4(tmp_k[ly][z4]);

            sum0 += dot(v0, k);
            sum1 += dot(v1, k);
            sum2 += dot(v2, k);
            sum3 += dot(v3, k);
        }
    }
#else
    for (int z = 0; z < psc(c); z++)
    {
        afpvec4 v0 = buffer_ld4(bottom_blob_data, v_offset + 0);
        afpvec4 v1 = buffer_ld4(bottom_blob_data, v_offset + 1);
        afpvec4 v2 = buffer_ld4(bottom_blob_data, v_offset + 2);
        afpvec4 v3 = buffer_ld4(bottom_blob_data, v_offset + 3);

        afpvec4 k = buffer_ld4(weight_data, w_offset);

        sum0 += dot(v0, k);
        sum1 += dot(v1, k);
        sum2 += dot(v2, k);
        sum3 += dot(v3, k);

        v_offset += psc(cstep);
        w_offset += 1;
    }
#endif

#if NCNN_shader_local_memory
    if (gx >= psc(outcstep) || gy >= psc(outc))
        return;
#endif

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

    buffer_st1(col_blob_data, gi, sum0);
    if (gx + 1 < psc(outcstep)) buffer_st1(col_blob_data, gi + 1, sum1);
    if (gx + 2 < psc(outcstep)) buffer_st1(col_blob_data, gi + 2, sum2);
    if (gx + 3 < psc(outcstep)) buffer_st1(col_blob_data, gi + 3, sum3);
}
