layout (local_size_x = 32, local_size_y = 32) in;

layout (r32ui) uniform uimage2D colorimg3;

uniform vec2 viewSize;        // for Voxelization.glsl
uniform vec3 cameraPosition;  // for Voxelization.glsl
uniform float far;            // for Voxelization.glsl
#include "../../includes/Voxelization.glsl"
layout (r32ui) uniform uimage2D voxel_data_img;

#if (VOXEL_BUFFER_HEIGHT == 512)
const ivec3 workGroups = ivec3(512, 16, 1);
#elif (VOXEL_BUFFER_HEIGHT == 1024)
const ivec3 workGroups = ivec3(512, 32, 1);
#elif VOXEL_BUFFER_HEIGHT == 2048
// default: size of colortex0 = 16384,2048    32*512=16384 32*64=2048
const ivec3 workGroups = ivec3(512, 64, 1);
#elif (VOXEL_BUFFER_HEIGHT == 4096)
const ivec3 workGroups = ivec3(512, 128, 1);
#elif (VOXEL_BUFFER_HEIGHT == 6144)
const ivec3 workGroups = ivec3(512, 192, 1);
#elif (VOXEL_BUFFER_HEIGHT == 8192)
const ivec3 workGroups = ivec3(512, 256, 1);
#elif (VOXEL_BUFFER_HEIGHT == 12288)
const ivec3 workGroups = ivec3(512, 384, 1);
#else
const ivec3 workGroups = ivec3(512, 512, 1);
#endif

void main() {
    ivec2 coord = ivec2(gl_GlobalInvocationID.xy);
    
    //int maxLinearized = (int(viewSize.x) + int(viewSize.y) * int(viewSize.x)) * 2;
    int maxLinearized = (int(viewSize.y) * int(viewSize.x)) * 2;
    maxLinearized = int(sparse_voxel_buffer_size - 1) - maxLinearized;
    int coordLinearized = sparse_voxel_buffer_width * coord.y + coord.x;
    
    if (coordLinearized >= maxLinearized) return;
    
    uint val = imageLoad(voxel_data_img, coord).x;
    //imageStore(colorimg5, coord, uvec4(val));
    imageStore(colorimg3, coord + VOXELBAK0, uvec4(val));
}
