#version 450

layout(constant_id = 0) const int USE_AA = 0;

const float GAMMA_CONST = 0.4545;
const float SPAN_MAX = 8.0;
const float REDUCE_MIN = 1.0/128.0;
const float REDUCE_MUL = 1.0/32.0;

layout(location = 0) in vec2 inTextCoord;
layout(location = 0) out vec4 outFragColor;

layout(set = 0, binding = 0) uniform sampler2DMS inputTexture;

layout(set = 1, binding = 0) uniform ScreenSize{
    vec2 size;
} screenSize;

vec4 gamma(vec4 color){
    return color = vec4(pow(color.rgb,vec3(GAMMA_CONST)),color.a);
}


vec4 msaa(int sampleCount, sampler2DMS textureIn,vec2 TextCoord){
    ivec2 pixelCoords = ivec2(TextCoord * textureSize(textureIn));
    vec4 colorSum = vec4(0.0);

    for(int i = 0; i < sampleCount; ++i) {
        vec4 sampleColor = texelFetch(textureIn, pixelCoords, i);
        sampleColor.rgb = sampleColor.rgb / (sampleColor.rgb + vec3(1.0));
        colorSum += sampleColor;
    }
    return colorSum / float(sampleCount);
}

void main(){
    ivec2 pixelCoords = ivec2(inTextCoord * textureSize(inputTexture));

    if(USE_AA == 0){
        outFragColor = texelFetch(inputTexture,pixelCoords,0);
    }
    if(USE_AA == 1){
        outFragColor = texelFetch(inputTexture,pixelCoords,0);
    }
    if(USE_AA == 2){
        outFragColor = msaa(2,inputTexture,inTextCoord);
    }
    if(USE_AA == 3){
        outFragColor = msaa(4,inputTexture,inTextCoord);
    }
    if(USE_AA == 4){
        outFragColor = msaa(8,inputTexture,inTextCoord);
    }
    outFragColor = gamma(outFragColor);
}