#[compute]
#version 450

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

layout(rgba16f, set = 0, binding = 0) 
uniform image2D inputImage;

layout(rgba16f, set = 1, binding = 0) 
uniform image2D outputImage;

layout( push_constant, std430) 
uniform Parameters
{
  vec2 rasterSize;
  float pixelSize;
  float reserved;

} parameters;

void main() 
{
  ivec2 xy   = ivec2( gl_GlobalInvocationID.xy );
  ivec2 size = ivec2( parameters.rasterSize );

  if ( xy.x >= size.x || xy.y >= size.y ) 
  {
    return;
  }

  float pixelSizeF = parameters.pixelSize;
  int pixelSize = int( pixelSizeF );
  float halfPixelSizeF = pixelSizeF / 2.0;

  vec2 rasterSize = parameters.rasterSize;

  float x = float( xy.x % pixelSize );
  float y = float( xy.y % pixelSize );

  x = xy.x + floor( halfPixelSizeF ) - x;
  y = xy.y + floor( halfPixelSizeF ) - y;

  x = min( x, rasterSize.x - max( 1.0, halfPixelSizeF ) );
  y = min( y, rasterSize.y - max( 1.0, halfPixelSizeF ) );

  vec4 color = imageLoad( inputImage, ivec2( x, y ) );
  imageStore( outputImage, xy, color );
}
