// 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 kernel_w = 1;
layout (constant_id = 1) const int kernel_h = 1;
layout (constant_id = 2) const int dilation_w = 1;
layout (constant_id = 3) const int dilation_h = 1;
layout (constant_id = 4) const int stride_w = 1;
layout (constant_id = 5) const int stride_h = 1;
layout (constant_id = 6) const int bias_term = 0;
layout (constant_id = 7) const int activation_type = 0;
layout (constant_id = 8) const float activation_param_0 = 0;
layout (constant_id = 9) const float activation_param_1 = 0;

#define shape_constant_id_offset 10
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 (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; };
layout (binding = 3) readonly buffer bias_blob { sfpvec8 bias_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;
} p;

void main()
{
    int gx = int(gl_GlobalInvocationID.x) * 2;
    int gy = int(gl_GlobalInvocationID.y) * 2;
    int gz = int(gl_GlobalInvocationID.z) * 2;

    if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
        return;

    const ivec2 gx2 = gx + ivec2(0, 1);
    const ivec2 gy2 = gy + ivec2(0, 1);
    const ivec2 gz2 = gz + ivec2(0, 1);

    afpvec8 sum0;
    afpvec8 sum1;
    afpvec8 sum2;
    afpvec8 sum3;
    afpvec8 sum4;
    afpvec8 sum5;
    afpvec8 sum6;
    afpvec8 sum7;

    if (bias_term == 1)
    {
        sum0 = buffer_ld8(bias_data, gz2.x);
        sum4 = buffer_ld8(bias_data, gz2.y);
        sum1 = sum0;
        sum2 = sum0;
        sum3 = sum0;
        sum5 = sum4;
        sum6 = sum4;
        sum7 = sum4;
    }
    else
    {
        sum0 = afpvec8(afpvec4(0.f), afpvec4(0.f));
        sum1 = afpvec8(afpvec4(0.f), afpvec4(0.f));
        sum2 = afpvec8(afpvec4(0.f), afpvec4(0.f));
        sum3 = afpvec8(afpvec4(0.f), afpvec4(0.f));
        sum4 = afpvec8(afpvec4(0.f), afpvec4(0.f));
        sum5 = afpvec8(afpvec4(0.f), afpvec4(0.f));
        sum6 = afpvec8(afpvec4(0.f), afpvec4(0.f));
        sum7 = afpvec8(afpvec4(0.f), afpvec4(0.f));
    }

    ivec2 w_offset = gz2 * psc(c) * kernel_w * kernel_h;

    for (int z = 0; z < psc(c); z++)
    {
        ivec4 v_offset;
        v_offset.rg = z * psc(cstep) + gy2.x * stride_h * psc(w) + gx2 * stride_w;
        v_offset.ba = z * psc(cstep) + gy2.y * stride_h * psc(w) + gx2 * stride_w;

        for (int y = 0; y < kernel_h; y++)
        {
            for (int x = 0; x < kernel_w; x++)
            {
                afpvec8 v0 = buffer_ld8(bottom_blob_data, v_offset.r + x * dilation_w);
                afpvec8 v1 = buffer_ld8(bottom_blob_data, v_offset.g + x * dilation_w);
                afpvec8 v2 = buffer_ld8(bottom_blob_data, v_offset.b + x * dilation_w);
                afpvec8 v3 = buffer_ld8(bottom_blob_data, v_offset.a + x * dilation_w);

                afpvec8 k0 = buffer_ld8(weight_data, (w_offset.x + x) * 8 + 0);
                afpvec8 k1 = buffer_ld8(weight_data, (w_offset.x + x) * 8 + 1);
                afpvec8 k2 = buffer_ld8(weight_data, (w_offset.x + x) * 8 + 2);
                afpvec8 k3 = buffer_ld8(weight_data, (w_offset.x + x) * 8 + 3);
                afpvec8 k4 = buffer_ld8(weight_data, (w_offset.x + x) * 8 + 4);
                afpvec8 k5 = buffer_ld8(weight_data, (w_offset.x + x) * 8 + 5);
                afpvec8 k6 = buffer_ld8(weight_data, (w_offset.x + x) * 8 + 6);
                afpvec8 k7 = buffer_ld8(weight_data, (w_offset.x + x) * 8 + 7);

                afpvec8 k8 = buffer_ld8(weight_data, (w_offset.y + x) * 8 + 0);
                afpvec8 k9 = buffer_ld8(weight_data, (w_offset.y + x) * 8 + 1);
                afpvec8 ka = buffer_ld8(weight_data, (w_offset.y + x) * 8 + 2);
                afpvec8 kb = buffer_ld8(weight_data, (w_offset.y + x) * 8 + 3);
                afpvec8 kc = buffer_ld8(weight_data, (w_offset.y + x) * 8 + 4);
                afpvec8 kd = buffer_ld8(weight_data, (w_offset.y + x) * 8 + 5);
                afpvec8 ke = buffer_ld8(weight_data, (w_offset.y + x) * 8 + 6);
                afpvec8 kf = buffer_ld8(weight_data, (w_offset.y + x) * 8 + 7);

                // sum += v * k
                sum0[0].r += dot(v0[0], k0[0]) + dot(v0[1], k0[1]);
                sum0[0].g += dot(v0[0], k1[0]) + dot(v0[1], k1[1]);
                sum0[0].b += dot(v0[0], k2[0]) + dot(v0[1], k2[1]);
                sum0[0].a += dot(v0[0], k3[0]) + dot(v0[1], k3[1]);
                sum0[1].r += dot(v0[0], k4[0]) + dot(v0[1], k4[1]);
                sum0[1].g += dot(v0[0], k5[0]) + dot(v0[1], k5[1]);
                sum0[1].b += dot(v0[0], k6[0]) + dot(v0[1], k6[1]);
                sum0[1].a += dot(v0[0], k7[0]) + dot(v0[1], k7[1]);
                sum1[0].r += dot(v1[0], k0[0]) + dot(v1[1], k0[1]);
                sum1[0].g += dot(v1[0], k1[0]) + dot(v1[1], k1[1]);
                sum1[0].b += dot(v1[0], k2[0]) + dot(v1[1], k2[1]);
                sum1[0].a += dot(v1[0], k3[0]) + dot(v1[1], k3[1]);
                sum1[1].r += dot(v1[0], k4[0]) + dot(v1[1], k4[1]);
                sum1[1].g += dot(v1[0], k5[0]) + dot(v1[1], k5[1]);
                sum1[1].b += dot(v1[0], k6[0]) + dot(v1[1], k6[1]);
                sum1[1].a += dot(v1[0], k7[0]) + dot(v1[1], k7[1]);
                sum2[0].r += dot(v2[0], k0[0]) + dot(v2[1], k0[1]);
                sum2[0].g += dot(v2[0], k1[0]) + dot(v2[1], k1[1]);
                sum2[0].b += dot(v2[0], k2[0]) + dot(v2[1], k2[1]);
                sum2[0].a += dot(v2[0], k3[0]) + dot(v2[1], k3[1]);
                sum2[1].r += dot(v2[0], k4[0]) + dot(v2[1], k4[1]);
                sum2[1].g += dot(v2[0], k5[0]) + dot(v2[1], k5[1]);
                sum2[1].b += dot(v2[0], k6[0]) + dot(v2[1], k6[1]);
                sum2[1].a += dot(v2[0], k7[0]) + dot(v2[1], k7[1]);
                sum3[0].r += dot(v3[0], k0[0]) + dot(v3[1], k0[1]);
                sum3[0].g += dot(v3[0], k1[0]) + dot(v3[1], k1[1]);
                sum3[0].b += dot(v3[0], k2[0]) + dot(v3[1], k2[1]);
                sum3[0].a += dot(v3[0], k3[0]) + dot(v3[1], k3[1]);
                sum3[1].r += dot(v3[0], k4[0]) + dot(v3[1], k4[1]);
                sum3[1].g += dot(v3[0], k5[0]) + dot(v3[1], k5[1]);
                sum3[1].b += dot(v3[0], k6[0]) + dot(v3[1], k6[1]);
                sum3[1].a += dot(v3[0], k7[0]) + dot(v3[1], k7[1]);

                sum4[0].r += dot(v0[0], k8[0]) + dot(v0[1], k8[1]);
                sum4[0].g += dot(v0[0], k9[0]) + dot(v0[1], k9[1]);
                sum4[0].b += dot(v0[0], ka[0]) + dot(v0[1], ka[1]);
                sum4[0].a += dot(v0[0], kb[0]) + dot(v0[1], kb[1]);
                sum4[1].r += dot(v0[0], kc[0]) + dot(v0[1], kc[1]);
                sum4[1].g += dot(v0[0], kd[0]) + dot(v0[1], kd[1]);
                sum4[1].b += dot(v0[0], ke[0]) + dot(v0[1], ke[1]);
                sum4[1].a += dot(v0[0], kf[0]) + dot(v0[1], kf[1]);
                sum5[0].r += dot(v1[0], k8[0]) + dot(v1[1], k8[1]);
                sum5[0].g += dot(v1[0], k9[0]) + dot(v1[1], k9[1]);
                sum5[0].b += dot(v1[0], ka[0]) + dot(v1[1], ka[1]);
                sum5[0].a += dot(v1[0], kb[0]) + dot(v1[1], kb[1]);
                sum5[1].r += dot(v1[0], kc[0]) + dot(v1[1], kc[1]);
                sum5[1].g += dot(v1[0], kd[0]) + dot(v1[1], kd[1]);
                sum5[1].b += dot(v1[0], ke[0]) + dot(v1[1], ke[1]);
                sum5[1].a += dot(v1[0], kf[0]) + dot(v1[1], kf[1]);
                sum6[0].r += dot(v2[0], k8[0]) + dot(v2[1], k8[1]);
                sum6[0].g += dot(v2[0], k9[0]) + dot(v2[1], k9[1]);
                sum6[0].b += dot(v2[0], ka[0]) + dot(v2[1], ka[1]);
                sum6[0].a += dot(v2[0], kb[0]) + dot(v2[1], kb[1]);
                sum6[1].r += dot(v2[0], kc[0]) + dot(v2[1], kc[1]);
                sum6[1].g += dot(v2[0], kd[0]) + dot(v2[1], kd[1]);
                sum6[1].b += dot(v2[0], ke[0]) + dot(v2[1], ke[1]);
                sum6[1].a += dot(v2[0], kf[0]) + dot(v2[1], kf[1]);
                sum7[0].r += dot(v3[0], k8[0]) + dot(v3[1], k8[1]);
                sum7[0].g += dot(v3[0], k9[0]) + dot(v3[1], k9[1]);
                sum7[0].b += dot(v3[0], ka[0]) + dot(v3[1], ka[1]);
                sum7[0].a += dot(v3[0], kb[0]) + dot(v3[1], kb[1]);
                sum7[1].r += dot(v3[0], kc[0]) + dot(v3[1], kc[1]);
                sum7[1].g += dot(v3[0], kd[0]) + dot(v3[1], kd[1]);
                sum7[1].b += dot(v3[0], ke[0]) + dot(v3[1], ke[1]);
                sum7[1].a += dot(v3[0], kf[0]) + dot(v3[1], kf[1]);
            }

            v_offset += dilation_h * psc(w);
            w_offset += kernel_w;
        }
    }

    sum0 = activation_afpvec8(sum0, activation_type, activation_param_0, activation_param_1);
    sum1 = activation_afpvec8(sum1, activation_type, activation_param_0, activation_param_1);
    sum2 = activation_afpvec8(sum2, activation_type, activation_param_0, activation_param_1);
    sum3 = activation_afpvec8(sum3, activation_type, activation_param_0, activation_param_1);
    sum4 = activation_afpvec8(sum4, activation_type, activation_param_0, activation_param_1);
    sum5 = activation_afpvec8(sum5, activation_type, activation_param_0, activation_param_1);
    sum6 = activation_afpvec8(sum6, activation_type, activation_param_0, activation_param_1);
    sum7 = activation_afpvec8(sum7, activation_type, activation_param_0, activation_param_1);

    const ivec2 gi = gz2 * psc(outcstep) + gy * psc(outw) + gx;

    buffer_st8(top_blob_data, gi.x, sum0);
    if (gx + 1 < psc(outw)) buffer_st8(top_blob_data, gi.x + 1, sum1);
    if (gy + 1 < psc(outh)) buffer_st8(top_blob_data, gi.x + psc(outw), sum2);
    if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st8(top_blob_data, gi.x + psc(outw) + 1, sum3);
    if (gz + 1 < psc(outc))
    {
        buffer_st8(top_blob_data, gi.y, sum4);
        if (gx + 1 < psc(outw)) buffer_st8(top_blob_data, gi.y + 1, sum5);
        if (gy + 1 < psc(outh)) buffer_st8(top_blob_data, gi.y + psc(outw), sum6);
        if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st8(top_blob_data, gi.y + psc(outw) + 1, sum7);
    }
}
