layout (location = 0) out vec4 L_out;
uniform sampler2D inputTex;
uniform ivec2 oldDimensions;
uniform vec2 oldInvDimensions;
uniform float downscale = 2.0;
vec3 data = vec3(0.0);
float divider = 0.0;
void combine(in vec4 sample)
{
//If this is the first sample, just copy the min max values.
if (divider == 0)
{
data.r = 0.0;
data.g = sample.g;
data.b = sample.b;
}
//Store the value for averaging
data.r += sample.r;
divider += 1.0;
//Store the maximum value
data.g = max(sample.g, data.g);
//Store the maximum value
data.b = min(sample.b, data.b);
}
vec4 output_frag()
{
return vec4(data.r / divider, data.g, data.b, 1.0);
}
void main()
{
vec2 oldPixelOrigin = (downscale * gl_FragCoord.xy - vec2(0.5, 0.5)) * oldInvDimensions;
combine(textureOffset(inputTex, oldPixelOrigin, ivec2(0,0)));
combine(textureOffset(inputTex, oldPixelOrigin, ivec2(0,1)));
combine(textureOffset(inputTex, oldPixelOrigin, ivec2(1,0)));
combine(textureOffset(inputTex, oldPixelOrigin, ivec2(1,1)));
//Now determine if we need to add extra samples in case of
//non-power of two textures
bool extraXSamples = (2 * (int(gl_FragCoord.x) + 1) == oldDimensions.x - 1);
bool extraYSamples = (2 * (int(gl_FragCoord.y) + 1) == oldDimensions.y - 1);
if (extraXSamples)
{
combine(textureOffset(inputTex, oldPixelOrigin, ivec2(2,0)));
combine(textureOffset(inputTex, oldPixelOrigin, ivec2(2,1)));
}
if (extraYSamples)
{
combine(textureOffset(inputTex, oldPixelOrigin, ivec2(0,2)));
combine(textureOffset(inputTex, oldPixelOrigin, ivec2(1,2)));
}
if (extraXSamples && extraYSamples)
combine(textureOffset(inputTex, oldPixelOrigin, ivec2(2,2)));
L_out = output_frag();
}