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

#version 450

#extension GL_GOOGLE_include_directive: enable
#include "vulkan_activation.comp"

layout (constant_id = 0) const int bias_term = 0;
layout (constant_id = 1) const int activation_type = 0;
layout (constant_id = 2) const float activation_param_0 = 0;
layout (constant_id = 3) const float activation_param_1 = 0;

#define shape_constant_id_offset 4
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 outw = 0;
layout (constant_id = shape_constant_id_offset + 5) const int outh = 0;
layout (constant_id = shape_constant_id_offset + 6) const int outc = 0;
layout (constant_id = shape_constant_id_offset + 7) const int outcstep = 0;

layout (binding = 0) readonly buffer bottom_blob { sfpvec8 bottom_blob_data[]; };
layout (binding = 1) writeonly buffer top_blob { sfp top_blob_data[]; };
layout (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; };
layout (binding = 3) readonly buffer bias_blob { sfp bias_data[]; };

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

    int outw;
    int outh;
    int outc;
    int outcstep;
} p;

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

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

    afp sum0;
    afp sum1;
    afp sum2;
    afp sum3;

    if (bias_term == 1)
    {
        afp b = buffer_ld1(bias_data, gy);
        sum0 = b;
        sum1 = b;
        sum2 = b;
        sum3 = b;
    }
    else
    {
        sum0 = afp(0.f);
        sum1 = afp(0.f);
        sum2 = afp(0.f);
        sum3 = afp(0.f);
    }

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

    for (int z = 0; z < psc(c); z++)
    {
        afpvec8 v0 = buffer_ld8(bottom_blob_data, v_offset + 0);
        afpvec8 v1 = buffer_ld8(bottom_blob_data, v_offset + 1);
        afpvec8 v2 = buffer_ld8(bottom_blob_data, v_offset + 2);
        afpvec8 v3 = buffer_ld8(bottom_blob_data, v_offset + 3);

        afpvec8 k = buffer_ld8(weight_data, w_offset);

        // sum += dot(v, k);
        sum0 += dot(v0[0], k[0]) + dot(v0[1], k[1]);
        sum1 += dot(v1[0], k[0]) + dot(v1[1], k[1]);
        sum2 += dot(v2[0], k[0]) + dot(v2[1], k[1]);
        sum3 += dot(v3[0], k[0]) + dot(v3[1], k[1]);

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

    sum0 = activation_afp(sum0, activation_type, activation_param_0, activation_param_1);
    sum1 = activation_afp(sum1, activation_type, activation_param_0, activation_param_1);
    sum2 = activation_afp(sum2, activation_type, activation_param_0, activation_param_1);
    sum3 = activation_afp(sum3, activation_type, activation_param_0, activation_param_1);

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

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