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

#version 450

layout (constant_id = 0) const int flip = 0;
layout (constant_id = 1) const int clip = 0;
layout (constant_id = 2) const float offset = 0;
layout (constant_id = 3) const float variances_0 = 0;
layout (constant_id = 4) const float variances_1 = 0;
layout (constant_id = 5) const float variances_2 = 0;
layout (constant_id = 6) const float variances_3 = 0;
layout (constant_id = 7) const int num_min_size = 0;
layout (constant_id = 8) const int num_max_size = 0;
layout (constant_id = 9) const int num_aspect_ratio = 0;
layout (constant_id = 10) const int num_prior = 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;

#if NCNN_fp16_packed
layout (binding = 0) writeonly buffer top_blob { vec4 top_blob_data[]; };
#else
layout (binding = 0) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
#endif
layout (binding = 1) readonly buffer min_sizes { sfp min_sizes_data[]; };
layout (binding = 2) readonly buffer max_sizes { sfp max_sizes_data[]; };
layout (binding = 3) readonly buffer aspect_ratios { sfp aspect_ratios_data[]; };

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

    float image_w;
    float image_h;
    float step_w;
    float step_h;
} p;

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

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

    // anchor and variance
    int v_offset = (gz * psc(w) + gy) * num_prior + gx;
    int var_offset = psc(w) * psc(h) * num_prior + v_offset;

    afp center_x = (afp(gy) + afp(offset)) * afp(p.step_w);
    afp center_y = (afp(gz) + afp(offset)) * afp(p.step_h);
    afpvec4 center = afpvec4(center_x, center_y, center_x, center_y);

    afpvec4 image_norm = afp(1.f) / afpvec4(p.image_w, p.image_h, p.image_w, p.image_h);

    afpvec4 box;

    afp box_w;
    afp box_h;

    afp min_size = buffer_ld1(min_sizes_data, gx);

    afpvec4 variances = afpvec4(afp(variances_0), afp(variances_1), afp(variances_2), afp(variances_3));

    // min size box
    box_w = box_h = min_size;

    box = (center + afpvec4(-box_w, -box_h, box_w, box_h) * afp(0.5f)) * image_norm;
    box = clip == 1 ? clamp(box, afp(0.f), afp(1.f)) : box;

#if NCNN_fp16_packed
    top_blob_data[v_offset] = vec4(box);
    top_blob_data[var_offset] = vec4(variances);
#else
    buffer_st4(top_blob_data, v_offset, box);
    buffer_st4(top_blob_data, var_offset, variances);
#endif

    v_offset += 1;
    var_offset += 1;

    if (num_max_size > 0)
    {
        afp max_size = buffer_ld1(max_sizes_data, gx);

        // max size box
        box_w = box_h = sqrt(min_size * max_size);

        box = (center + afpvec4(-box_w, -box_h, box_w, box_h) * afp(0.5f)) * image_norm;
        box = clip == 1 ? clamp(box, afp(0.f), afp(1.f)) : box;

#if NCNN_fp16_packed
        top_blob_data[v_offset] = vec4(box);
        top_blob_data[var_offset] = vec4(variances);
#else
        buffer_st4(top_blob_data, v_offset, box);
        buffer_st4(top_blob_data, var_offset, variances);
#endif

        v_offset += 1;
        var_offset += 1;
    }

    // all aspect_ratios
    for (int pi = 0; pi < num_aspect_ratio; pi++)
    {
        afp ar = buffer_ld1(aspect_ratios_data, pi);

        box_w = min_size * sqrt(ar);
        box_h = min_size / sqrt(ar);

        box = (center + afpvec4(-box_w, -box_h, box_w, box_h) * afp(0.5f)) * image_norm;
        box = clip == 1 ? clamp(box, afp(0.f), afp(1.f)) : box;

#if NCNN_fp16_packed
        top_blob_data[v_offset] = vec4(box);
        top_blob_data[var_offset] = vec4(variances);
#else
        buffer_st4(top_blob_data, v_offset, box);
        buffer_st4(top_blob_data, var_offset, variances);
#endif

        v_offset += 1;
        var_offset += 1;

        if (flip == 1)
        {
            box = (center + afpvec4(-box_h, -box_w, box_h, box_w) * afp(0.5f)) * image_norm;
            box = clip == 1 ? clamp(box, afp(0.f), afp(1.f)) : box;

#if NCNN_fp16_packed
            top_blob_data[v_offset] = vec4(box);
            top_blob_data[var_offset] = vec4(variances);
#else
            buffer_st4(top_blob_data, v_offset, box);
            buffer_st4(top_blob_data, var_offset, variances);
#endif

            v_offset += 1;
            var_offset += 1;
        }
    }
}
