// 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 group = 1;
layout (constant_id = 8) const int activation_type = 0;
layout (constant_id = 9) const float activation_param_0 = 0;
layout (constant_id = 10) const float activation_param_1 = 0;

#define shape_constant_id_offset 11
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 { sfp 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);
    int gy = int(gl_GlobalInvocationID.y);
    int gz = int(gl_GlobalInvocationID.z);

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

    afpvec8 sum;

    if (bias_term == 1)
    {
        sum = buffer_ld8(bias_data, gz);
    }
    else
    {
        sum = afpvec8(afpvec4(0.f), afpvec4(0.f));
    }

    const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
    const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;

    // group convolution
    const int channels_g = psc(c) / group;
    const int num_output_g = psc(outc) / group;

    // group id
    const int gg = gz / num_output_g;

    int w_offset_0 = gz * channels_g * kernel_w * kernel_h;
    int v_offset_0 = gg * channels_g * psc(cstep);

    for (int y = 0; y < kernel_h; y++)
    {
        int sys = (gy + y * dilation_h - (kernel_extent_h - 1));
        if (sys < 0 || sys % stride_h != 0)
            continue;

        int sy = sys / stride_h;
        if (sy >= psc(h))
            continue;

        for (int x = 0; x < kernel_w; x++)
        {
            int sxs = (gx + x * dilation_w - (kernel_extent_w - 1));
            if (sxs < 0 || sxs % stride_w != 0)
                continue;

            int sx = sxs / stride_w;
            if (sx >= psc(w))
                continue;

            int v_offset = v_offset_0 + sy * psc(w) + sx;
            int w_offset = w_offset_0 + y * kernel_w + x;

            for (int z = 0; z < channels_g; z++)
            {
                afp v = buffer_ld1(bottom_blob_data, v_offset);

                afpvec8 k = buffer_ld8(weight_data, w_offset);

                // sum += v * k;
                sum[0] += v * k[0];
                sum[1] += v * k[1];

                v_offset += psc(cstep);
                w_offset += kernel_w * kernel_h;
            }
        }
    }

    sum = activation_afpvec8(sum, activation_type, activation_param_0, activation_param_1);

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

    buffer_st8(top_blob_data, gi, sum);
}
