#[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 
{
  float outputX;
  float outputY;
  float direction;
  float inputEdge;

  float blurMode;
  float p2;
  float p3;
  float p4;


} parameters; 

float getLuma( vec4 rgb ) 
{
	return dot( rgb.rgb, vec3( 0.3, 0.5, 0.2 ) );
}

void main( ) 
{
  ivec2 size = ivec2( int( parameters.outputX ), int( parameters.outputY ) );
  ivec2 xy   = ivec2( gl_GlobalInvocationID.xy );
  
  if ( any( greaterThanEqual( xy, size ) )  ) 
  {
    return;
  }

  int X_DIRECTION = 0;
  int Y_DIRECTION = 1;

  ivec2 uv0 = xy;
  ivec2 uv1 = xy;

  int inputEdge = int( parameters.inputEdge ) - 1;

  if ( X_DIRECTION == int( parameters.direction ) )
  {
    uv0.y = min( inputEdge, xy.y * 2 );
    uv1.y = min( inputEdge, xy.y * 2 + 1 );
  }
  else
  {
    uv0.x = min( inputEdge, xy.x * 2 );
    uv1.x = min( inputEdge, xy.x * 2 + 1 );
  }
  
  vec4 colorA = imageLoad( inputImage, uv0 );
  vec4 colorB = imageLoad( inputImage, uv1 );

  int blurMode = int( parameters.blurMode );
  vec4 maxColor = vec4( 0.0 );
  
  int MAX     = 0;
  int AVERAGE = 1;

  if ( blurMode == MAX )
  {
    maxColor = max( colorA, colorB );
  }
  else if ( blurMode == AVERAGE )
  {
    maxColor = ( colorA + colorB ) / 2.0;
  }
  

  imageStore( outputImage, xy, maxColor );
}