<jittershader name="mrt.simple.render.jxs">
	<param name="tex_ssao" type="int" default="0" />
	<param name="tex_normals" type="int" default="1" />
	<param name="tex_color" type="int" default="2" />
	<param name="invTexSize" type="vec2" state="INVERSE_VIEWPORT" />
	<param name="projmat" type="mat4" state="PROJECTION_MATRIX" />
	<param name="farDistance" type="float" state="FAR_CLIP" />
	<param name="farCorner" type="vec3" default="0. 0. 0." />		
	<language name="glsl" version="1.2">
		<bind param="tex_ssao" program="fp" />
		<bind param="tex_normals" program="fp" />
		<bind param="tex_color" program="fp" />
		<bind param="invTexSize" program="fp" />
		<bind param="projmat" program="fp" />		
		<bind param="farDistance" program="fp" />
		<bind param="farCorner" program="vp" />			
		<program name="vp" type="vertex">
<![CDATA[

varying vec2 uv;
varying vec2 uv_rect;
varying vec3 ray;
uniform vec3 farCorner;

void main()
{
	gl_Position = vec4(sign(gl_Vertex).xy,1,1);
	uv.x = 0.5 * (1. + gl_Position.x);
	uv.y = 0.5 * (1. + gl_Position.y);
	uv_rect = vec2(gl_TextureMatrix[0] * vec4(uv, 1,1));	
	ray = farCorner * vec3(gl_Position.xy, 1);
}

]]>		
		</program>
		<program name="fp" type="fragment">
<![CDATA[
#version 120
varying vec2 uv;
varying vec2 uv_rect;
varying vec3 ray;

uniform sampler2DRect tex_ssao; 
uniform sampler2DRect tex_normals;
uniform sampler2DRect tex_color;
uniform vec2 invTexSize;
uniform mat4 projmat;
uniform float farDistance;

float finalDepth(vec4 p)
{
    return (p.z / p.w) * 0.5 + 0.5;
}

#define NUM_BLUR_SAMPLES 8

void main()
{
	vec4 norm_depth = texture2DRect(tex_normals, uv_rect);
	vec3 norm = norm_depth.xyz;
	float depth = norm_depth.w;

	// Clip fragment if depth is too close, so the skybox can be rendered on the background
	//if(depth-0.0001<0.0)
		//discard;    

	vec2 o = vec2(0., invTexSize.y);
	vec4 sum = texture2DRect(tex_ssao, uv_rect) * (NUM_BLUR_SAMPLES + 1);
	float denom = NUM_BLUR_SAMPLES + 1;
	
	for (int i = 1; i <= NUM_BLUR_SAMPLES; ++i) {
		vec2 nuv = uv + o * i;
		vec2 nuv_rect = vec2(gl_TextureMatrix[0] * vec4(nuv, 1,1)).st;
		vec3 norm2 = texture2DRect(tex_normals, nuv_rect).xyz;
		float coef = (NUM_BLUR_SAMPLES + 1 - i) * int(dot(norm, norm2) > 0.9);
		sum += texture2DRect(tex_ssao, nuv_rect) * coef;
		denom += coef;
	}
	for (int i = 1; i <= 4; ++i) {
		vec2 nuv = uv + o * -i;
		vec2 nuv_rect = vec2(gl_TextureMatrix[0] * vec4(nuv, 1,1)).st;
		vec3 norm2 = texture2DRect(tex_normals, nuv_rect).xyz;
		float coef = (NUM_BLUR_SAMPLES + 1 - i) * int(dot(norm, norm2) > 0.9);
		sum += texture2DRect(tex_ssao, nuv_rect) * coef;
		denom += coef;
	}

	vec4 ssao = sum / denom;
	vec4 image = texture2DRect(tex_color, uv_rect);
	
	gl_FragColor = vec4((image*ssao).rgb ,0);

	// Calculate depth of fragment;
	// subtracting a proportion of far_clip to prevent small circle in center of frame at far plane
	vec3 viewPos = normalize(ray) * (farDistance-farDistance*0.1) * depth;
	vec4 projPos = projmat * vec4(viewPos, 1);
	gl_FragDepth = finalDepth(projPos);
}
]]>		
		</program>		
	</language>
</jittershader>
