#[compute]
#version 450

/*

The original streaks code was taken from Karl Bittner's streak implementation for his lens-flare-code:
https://github.com/kb173/godot-procedural-lens-flare


Licens of the code:

MIT License

Copyright (c) 2025 Karl Bittner

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

// Adapted from https://chrisoat.com/papers/Oat-ScenePostprocessing.pdf


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

layout( set = 0, binding = 0 ) 
uniform sampler2D inputImage;

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


layout( push_constant, std430 ) 
uniform Parameters 
{
  vec2 direction;
  vec2 attenuationAmount;

  vec2 samplesIteration;
  vec2 lumaOffsetScale;

  vec2 streaksBlur;
  vec2 rotationDesaturation;


} parameters;

float mapClamped( float value, float inMin, float inMax, float outMin, float outMax )
{
  float n = ( value - inMin ) / ( inMax - inMin );

  n = clamp( n, 0.0, 1.0 );

  return mix( outMin, outMax, n );
}

vec2 rotate( vec2 uv, float angle )
{
  float s = sin( angle );
  float c = cos( angle );

    
  float x = uv.x;
  float y = uv.y; 
 
  uv.x = c * x - s * y;
  uv.y = s * x + c * y;

  return uv;
}


void main() 
{
  ivec2 xy   = ivec2( gl_GlobalInvocationID.xy );
  ivec2 size = imageSize( outputImage );

  if ( any( greaterThanEqual( xy, size ) )  ) 
  {
    return;
  }

  vec4 color = vec4( 0.0, 0.0, 0.0, 1.0 ); 

  int samples = int( parameters.samplesIteration.x );

  float b = pow( samples, parameters.samplesIteration.y );

  int numStreaks = int( parameters.streaksBlur.x );

  float blurScale = parameters.streaksBlur.y;

  float streamAmount = 1.0 / float( numStreaks );
  float angleOffset = streamAmount * 6.28318530;

  float rotationOffset = parameters.rotationDesaturation.x;
  float desaturation = parameters.rotationDesaturation.y;

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

  for ( int j = 0; j < numStreaks; j++ )
  {
    vec2 direction = rotate( parameters.direction, float( j ) * angleOffset + rotationOffset ) ;
    direction /= vec2( size );

    for ( int i = 0; i < samples; i++ ) 
    {
      float weight = pow( parameters.attenuationAmount.x, b * ( i + 1 ) );

      vec2 offset = vec2( direction * b * i );

      vec3 sampledColor = texture( inputImage, uv + offset ).rgb;

      float luma = length( sampledColor );

      float lumaAmount = max( luma - parameters.lumaOffsetScale.x, 0.0 ) * parameters.lumaOffsetScale.y;


      sampledColor = mix( sampledColor * lumaAmount, vec3( lumaAmount ), desaturation );

      color.rgb += clamp( weight, 0.0, 1.0) * sampledColor;
    }
    
  }

  vec4 originalColor = texture( inputImage, uv );

  color.rgb = originalColor.rgb + color.rgb * parameters.attenuationAmount.y;

  // color = mix( originalColor, color, parameters.attenuationAmount.y );

  imageStore( outputImage, xy, color);
}
