#version 450

layout(
  local_size_x = 64,
  local_size_y = 1,
  local_size_z = 1) in;

layout(binding = 0, r32f) writeonly uniform  imageBuffer u_depth;
layout(binding = 1, r8ui) writeonly uniform uimageBuffer u_stencil;

layout(binding = 2)
readonly buffer s_buffer_t {
  uint data[];
} s_buffer;

layout(push_constant)
uniform u_info_t {
  uvec2 src_offset;
  uvec2 src_extent;
  uvec2 dst_offset;
  uvec2 dst_extent;
} u_info;

void main() {
  if (all(lessThan(gl_GlobalInvocationID.xy, u_info.dst_extent))) {
    uvec3 src_coord = uvec3(
      gl_GlobalInvocationID.xy + u_info.src_offset,
      gl_GlobalInvocationID.z);

    uvec3 dst_coord = uvec3(
      gl_GlobalInvocationID.xy + u_info.dst_offset,
      gl_GlobalInvocationID.z);
    
    uint src_index = src_coord.x + u_info.src_extent.x * (src_coord.y + u_info.src_extent.y * src_coord.z);
    uint dst_index = dst_coord.x + u_info.dst_extent.x * (dst_coord.y + u_info.dst_extent.y * dst_coord.z);

    uint src_data = s_buffer.data[src_index];
    imageStore(u_depth,   int(dst_index),  vec4(float(src_data & 0xFFFFFF) / float(0xFFFFFF)));
    imageStore(u_stencil, int(dst_index), uvec4(src_data >> 24));
  }
}