#version 450

/*
   Interlacing
   
   Copyright (C) 2020 - 2025 guest(r)

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   
*/  

layout(push_constant) uniform Push
{
	vec4 OriginalSize;
	vec4 OutputSize;
	uint FrameCount;
	float GAMMA_INPUT;
	float downsample_levelx;
	float downsample_levely;
	float gamma_out;
} params;


layout(std140, set = 0, binding = 0) uniform UBO
{
	vec4 SourceSize;
	mat4 MVP;
} global;

#pragma parameter downsample_levelx "          Downsampling-X (High-res content, pre-scalers)" 0.0 0.0 3.0 0.05
#define downsample_levelx         params.downsample_levelx     // downsample level

#pragma parameter downsample_levely "          Downsampling-Y (High-res content, pre-scalers)" 0.0 0.0 3.0 0.05
#define downsample_levely         params.downsample_levely     // downsample level

#pragma stage vertex
layout(location = 0) in vec4 Position;
layout(location = 1) in vec2 TexCoord;
layout(location = 0) out vec2 vTexCoord;

void main()
{
   gl_Position = global.MVP * Position;
   vTexCoord = TexCoord * 1.00001;
}

#pragma stage fragment
layout(location = 0) in vec2 vTexCoord;
layout(location = 0) out vec4 FragColor;
layout(set = 0, binding = 2) uniform sampler2D NtscPass;
layout(set = 0, binding = 3) uniform sampler2D InterlacePass;

#define COMPAT_TEXTURE(c,d) texture(c,d)


vec3 fetch_pixel(vec2 coord)
{
	vec2 dx = vec2(global.SourceSize.z, 0.0) * downsample_levelx;
	vec2 dy = vec2(0.0, global.SourceSize.w) * downsample_levely;	
	vec2 d1 = dx + dy;
	vec2 d2 = dx - dy;
	
	float sum = 15.0;
	vec3 result = 3.0*COMPAT_TEXTURE(NtscPass, coord     ).rgb + 
	              2.0*COMPAT_TEXTURE(NtscPass, coord + dx).rgb +
	              2.0*COMPAT_TEXTURE(NtscPass, coord - dx).rgb +
	              2.0*COMPAT_TEXTURE(NtscPass, coord + dy).rgb +
	              2.0*COMPAT_TEXTURE(NtscPass, coord - dy).rgb +
	              COMPAT_TEXTURE(NtscPass, coord + d1).rgb +
	              COMPAT_TEXTURE(NtscPass, coord - d1).rgb +
	              COMPAT_TEXTURE(NtscPass, coord + d2).rgb +
	              COMPAT_TEXTURE(NtscPass, coord - d2).rgb;  
				  
	return result/sum;
}


void main()
{
	vec3 c = COMPAT_TEXTURE(NtscPass, vTexCoord).rgb;
	
	if ((downsample_levelx + downsample_levely) > 0.025)
	{
		c = fetch_pixel(vTexCoord);
	}

	float intera = COMPAT_TEXTURE(InterlacePass, vec2(0.75, 0.75)).a;
	float gamma_in = 1.0;

	c = pow(c, vec3(gamma_in));
	
	if (vTexCoord.x > 0.5) gamma_in = intera; else gamma_in = 1.0/gamma_in;
	
	FragColor = vec4(c, gamma_in);
}