/**
 * transform color from linear to srgb space
 */
#include "version.sh"

uniform sampler2D texUnit;

in vec2 UV_f;
uniform vec4 viewport;
uniform float radius;
uniform float weight;

layout(location = 0) out vec4 fragColor;

float roundedBoxSDF(vec2 centerPosition, vec2 size, float radius) {
    return length(max(abs(centerPosition)-size+radius,0.0))-radius;
}

void main()
{
    vec4 color = texture(texUnit, UV_f);
    
#ifdef DISL_GAMMA_CORRECTION
    vec3 _col = color.rgb;
    color.rgb = mix(_col * 12.92, pow(_col, vec3(0.41666)) * 1.055 - vec3(0.055), step(vec3(0.003130), color.rgb));
#endif
    
#ifdef DISL_VIEW_ROUNDED_CORNER
    vec2 half_size = viewport.zw * 0.5f;
    vec2 pos = gl_FragCoord.xy-viewport.xy-half_size;
    float distance = roundedBoxSDF(pos, half_size, radius);
    
    float edgeSoftness  = 1.0f;
    float smoothedAlpha = 1.0f-smoothstep(0.0f, edgeSoftness * 2.0f,distance);
    fragColor = vec4(color.xyz, smoothedAlpha * weight);
    
#else
    fragColor = color;
#endif
}
