#version 450
/* Copyright (c) 2020 Themaister
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#if SUBGROUP
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_vote : require
#extension GL_KHR_shader_subgroup_ballot : require
#extension GL_KHR_shader_subgroup_arithmetic : require
#endif
#include "small_types.h"

layout(local_size_x_id = 3, local_size_y_id = 4) in;

#include "noise.h"
#include "debug.h"
#include "data_structures_buffers.h"
#include "memory_interfacing.h"

layout(set = 0, binding = 3, std430) readonly buffer ColorBuffer
{
    mem_u8x4 elems[];
} color;

layout(set = 0, binding = 3, std430) readonly buffer ColorRawBuffer
{
    uint elems[];
} raw_color;

layout(set = 0, binding = 4, std430) readonly buffer DepthBuffer
{
    int elems[];
} depth;

layout(set = 0, binding = 5, std430) readonly buffer ShadeAlpha
{
    mem_u8 elems[];
} shade_alpha;

layout(set = 0, binding = 6, std430) readonly buffer Coverage
{
    mem_i8 elems[];
} coverage;

layout(std430, set = 0, binding = 7) readonly buffer TileInstanceOffset
{
    uint elems[];
} tile_instance_offsets;

layout(push_constant, std430) uniform Registers
{
    uint fb_addr_index;
    uint fb_depth_addr_index;
    uint fb_width;
    uint fb_height;
    uint group_mask;
} registers;

layout(constant_id = 5) const int MAX_PRIMITIVES = 256;
layout(constant_id = 6) const int MAX_WIDTH = 1024;

const int TILE_BINNING_STRIDE = MAX_PRIMITIVES / 32;
const int MAX_TILES_X = MAX_WIDTH / int(gl_WorkGroupSize.x);

// Overall architecture of the tiling is from RetroWarp.

void main()
{
    int x = int(gl_GlobalInvocationID.x);
    int y = int(gl_GlobalInvocationID.y);
    ivec2 tile = ivec2(gl_WorkGroupID.xy);

    int linear_tile = tile.x + tile.y * MAX_TILES_X;
    int linear_tile_base = linear_tile * TILE_BINNING_STRIDE;

    uint coarse_binned = tile_binning_coarse.elems[linear_tile] & registers.group_mask;
    if (coarse_binned == 0u)
        return;

    init_tile(gl_GlobalInvocationID.xy,
              registers.fb_width, registers.fb_height,
              registers.fb_addr_index, registers.fb_depth_addr_index);

    while (coarse_binned != 0u)
    {
        int mask_index = findLSB(coarse_binned);
        coarse_binned &= ~uint(1 << mask_index);

        uint tile_instance = tile_instance_offsets.elems[linear_tile_base + mask_index];
        uint binned = tile_binning.elems[linear_tile_base + mask_index];

        while (binned != 0u)
        {
            int i = findLSB(binned);
            binned &= ~uint(1 << i);
            uint primitive_index = uint(i + 32 * mask_index);

            uint index = tile_instance * (gl_WorkGroupSize.x * gl_WorkGroupSize.y) + gl_LocalInvocationIndex;
            int coverage = int(coverage.elems[index]);

            if (coverage >= 0)
            {
                if ((coverage & COVERAGE_FILL_BIT) != 0)
                {
                    fill_color(derived_setup.elems[primitive_index].fill_color);
                }
                else if ((coverage & COVERAGE_COPY_BIT) != 0)
                {
                    uint word = raw_color.elems[index];
                    copy_pipeline(word, primitive_index);
                }
                else
                {
                    ShadedData shaded;
                    shaded.combined = u8x4(color.elems[index]);
                    shaded.z_dith = depth.elems[index];
                    shaded.shade_alpha = u8(shade_alpha.elems[index]);
                    shaded.coverage_count = u8(coverage);
                    depth_blend(x, y, primitive_index, shaded);
                }
            }

            tile_instance++;
        }
    }

    finish_tile(gl_GlobalInvocationID.xy,
                registers.fb_width, registers.fb_height,
                registers.fb_addr_index, registers.fb_depth_addr_index);
}
