#[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( set = 2, binding = 0 ) 
uniform sampler2D curves;


layout( push_constant, std430 ) 
uniform Parameters 
{
  float amount;

} parameters;


void main( ) 
{
  ivec2 size = imageSize( inputImage );
  ivec2 xy = ivec2( gl_GlobalInvocationID.xy );
  
  if ( any( greaterThanEqual( xy, size ) )  ) 
  {
    return;
  }

  vec2 uv = ( vec2( xy ) + 0.5 ) / vec2( size );    

  vec4 color = imageLoad( inputImage, ivec2( uv * size ) ); 

  vec4 processedColor = color;

  float r = texture( curves, vec2( color.r, 0 ) ).r;
  float g = texture( curves, vec2( color.g, 0 ) ).g;
  float b = texture( curves, vec2( color.b, 0 ) ).b;
  
  processedColor.r = r;
  processedColor.g = g;
  processedColor.b = b;


  vec4 blended = mix( color, processedColor, parameters.amount );


  imageStore( outputImage, xy, blended );
}