// Copyright 2022 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;
layout (constant_id = 10) const int outc = 0;

#define shape_constant_id_offset 11
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 cstep = 0;

layout (constant_id = shape_constant_id_offset + 3) const int outw = 0;
layout (constant_id = shape_constant_id_offset + 4) const int outh = 0;
layout (constant_id = shape_constant_id_offset + 5) const int outcstep = 0;

layout (binding = 0) readonly buffer col_blob { sfp col_blob_data[]; };
layout (binding = 1) writeonly buffer top_blob { sfp top_blob_data[]; };
layout (binding = 2) readonly buffer bias_blob { sfp bias_data[]; };

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

    int outw;
    int outh;
    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 >= outc)
        return;

    afp sum;

    if (bias_term == 1)
    {
        sum = buffer_ld1(bias_data, gz);
    }
    else
    {
        sum = afp(0.f);
    }

    const int maxk = kernel_w * kernel_h;

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

    const int sx_start = (gx < kernel_extent_w) ? 0 : (gx - kernel_extent_w) / stride_w + 1;
    const int sx_end = min(gx / stride_w + 1, psc(w));

    const int sy_start = (gy < kernel_extent_h) ? 0 : (gy - kernel_extent_h) / stride_h + 1;
    const int sy_end = min(gy / stride_h + 1, psc(h));

    for (int sy = sy_start; sy < sy_end; sy += 1)
    {
        for (int sx = sx_start; sx < sx_end; sx += 1)
        {
            int h_k = (gy - sy * stride_h);
            int w_k = (gx - sx * stride_w);

            if (h_k % dilation_h == 0 && w_k % dilation_w == 0)
            {
                h_k /= dilation_h;
                w_k /= dilation_w;

                const int gi = (gz * maxk + h_k * kernel_w + w_k) * psc(cstep) + sy * psc(w) + sx;

                sum += buffer_ld1(col_blob_data, gi);
            }
        }
    }

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

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

    buffer_st1(top_blob_data, gi, sum);
}
