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

#version 450

layout (constant_id = 0) const int across_spatial = 0;
layout (constant_id = 1) const int across_channel = 0;
layout (constant_id = 2) const int channel_shared = 0;
layout (constant_id = 3) const int scale_term = 0;
layout (constant_id = 4) const float channel_shared_scale = 1.f;

#define shape_constant_id_offset 5
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 (binding = 0) buffer bottom_top_blob { sfpvec8 bottom_top_blob_data[]; };
layout (binding = 1) readonly buffer coeffs_blob { sfpvec8 coeffs_blob_data[]; };
layout (binding = 2) readonly buffer scale_blob { sfpvec8 scale_blob_data[]; };

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

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

    if (gx >= psc(w) || gy >= psc(h) || gz >= psc(c))
        return;

    const int gi = gz * psc(cstep) + gy * psc(w) + gx;

    afpvec8 v = buffer_ld8(bottom_top_blob_data, gi);

    afpvec8 a;

    if (across_spatial == 1 && across_channel == 1)
    {
        a = buffer_ld8(coeffs_blob_data, 0);
    }

    if (across_spatial == 1 && across_channel == 0)
    {
        a = buffer_ld8(coeffs_blob_data, gz);
    }

    if (across_spatial == 0 && across_channel == 1)
    {
        a = buffer_ld8(coeffs_blob_data, gy * psc(w) + gx);
    }

#if (NCNN_fp16_packed || NCNN_fp16_storage)
    // NOTE coeffs may produce (X, undef, X, undef) on nvidia fp16p/fp16s
    // TODO only enable this workaround for some nvidia driver
    if (across_channel == 1)
    {
        a = afpvec8(afpvec4(a[0].r), afpvec4(a[0].r));
    }
#endif

    v[0] = v[0] * a[0];
    v[1] = v[1] * a[1];

    if (scale_term == 1)
    {
        if (channel_shared == 1)
        {
            v[0] = v[0] * afp(channel_shared_scale);
            v[1] = v[1] * afp(channel_shared_scale);
        }
        else
        {
            afpvec8 scale = buffer_ld8(scale_blob_data, gz);

            v[0] = v[0] * scale[0];
            v[1] = v[1] * scale[1];
        }
    }

    buffer_st8(bottom_top_blob_data, gi, v);
}
