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

#version 450

#define shape_constant_id_offset 0
layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
layout (constant_id = shape_constant_id_offset + 1) const int c = 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 outcstep = 0;

layout (binding = 0) readonly buffer bottom_blob { float bottom_blob_data[]; };
layout (binding = 1) writeonly buffer top_blob { float top_blob_data[]; };

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

    int outw;
    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 >= 1 || gz >= psc(c))
        return;

    const int end = min(8, (psc(w) - gx - 1) / psc(outw) + 1);

    const int v_offset = gz * psc(cstep);

    float sum = 0.f;

    for (int ii = 0; ii < end; ii++)
    {
        int i = gx + ii * psc(outw);

        float v = bottom_blob_data[v_offset + i];
        sum += v;
    }

    top_blob_data[gz * psc(outcstep) + gx] = sum;
}
