#version 450

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

layout(binding = 0, std430)
writeonly buffer s_buffer_t {
  uint data[];
} s_buffer;

layout(binding = 1) uniform  sampler2DArray u_depth;
layout(binding = 2) uniform usampler2DArray u_stencil;

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.src_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 dst_index = dst_coord.x + u_info.dst_extent.x * (dst_coord.y + u_info.dst_extent.y * dst_coord.z);
    
    float depth   = texelFetch(u_depth,   ivec3(src_coord), 0).r;
    uint  stencil = texelFetch(u_stencil, ivec3(src_coord), 0).r;
    
    s_buffer.data[dst_index] = uint(mix(0.0f, float((1 << 24) - 1), depth)) | (stencil << 24);
  }
}