// Snowy Border Cg shader, based on Andrew Baldwin's 'Just Snow' shadertoy, licensed below:
// Copyright (c) 2013 Andrew Baldwin (twitter: baldand, www: http://thndl.com)
// License = Attribution-NonCommercial-ShareAlike (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US)

// "Just snow"
// Simple (but not cheap) snow made from multiple parallax layers with randomly positioned 
// flakes and directions. Also includes a DoF effect. Pan around with mouse.

#pragma parameter aspect_x "Aspect Ratio Numerator" 64.0 1.0 256. 1.0
#pragma parameter aspect_y "Aspect Ratio Denominator" 49.0 1.0 256. 1.0
#pragma parameter integer_scale "Force Integer Scaling" 1.0 0.0 1.0 1.0
#pragma parameter overscale "Integer Overscale" 0.0 0.0 1.0 1.0
#pragma parameter scanline_toggle "Scanline Toggle" 0.0 0.0 1.0 1.0
#pragma parameter interp_toggle "Sharpen Linear Scaling" 0.0 0.0 1.0 1.0
#pragma parameter THICKNESS "Scanline Thickness" 2.0 1.0 12.0 1.0
#pragma parameter DARKNESS "Scanline Darkness" 0.35 0.0 1.0 0.05
#pragma parameter R "Red" 0.20 0.0 1.0 0.01
#pragma parameter G "Green" 0.1 0.0 1.0 0.01
#pragma parameter B "Blue" 0.5 0.0 1.0 0.01
#pragma parameter OS_MASK_TOP "OS Mask Top" 0.0 0.0 1.0 0.005
#pragma parameter OS_MASK_BOTTOM "OS Mask Bottom" 0.0 0.0 1.0 0.005
#pragma parameter OS_MASK_LEFT "OS Mask Left" 0.0 0.0 1.0 0.005
#pragma parameter OS_MASK_RIGHT "OS Mask Right" 0.0 0.0 1.0 0.005
#ifdef PARAMETER_UNIFORM
uniform float aspect_x;
uniform float aspect_y;
uniform float integer_scale;
uniform float overscale;
uniform float scanline_toggle;
uniform float THICKNESS;
uniform float DARKNESS;
uniform float interp_toggle;
uniform float R;
uniform float G;
uniform float B;
uniform float OS_MASK_TOP;
uniform float OS_MASK_BOTTOM;
uniform float OS_MASK_LEFT;
uniform float OS_MASK_RIGHT;
#else
#define aspect_x 64.0
#define aspect_y 49.0
#define integer_scale 1.0
#define overscale 0.0
#define scanline_toggle 0.0
#define THICKNESS 2.0
#define DARKNESS 0.35
#define interp_toggle 0.0
#define R 0.20
#define G 0.1
#define B 0.5
#define OS_MASK_TOP 0.0
#define OS_MASK_BOTTOM 0.0
#define OS_MASK_LEFT 0.0
#define OS_MASK_RIGHT 0.0
#endif
// END PARAMETERS //

#define LAYERS 15.0
#define DEPTH 1.0
#define WIDTH 1.0
#define SPEED -.03

#include "../../compat_includes.inc"

uniform COMPAT_Texture2D(decal) : TEXUNIT0;
uniform float4x4 modelViewProj;

struct out_vertex{
   float4 position : COMPAT_POS;
   float2 texCoord : TEXCOORD;
   float2 tex_border : TEXCOORD1;
};

out_vertex main_vertex(COMPAT_IN_VERTEX)
{
#ifdef HLSL_4
	float4 position = VIN.position;
	float2 texCoord = VIN.texCoord;
	float2 t1 = VIN.t1;
#endif
	out_vertex OUT;

	OUT.position = mul(modelViewProj, position);

	float2 out_res = COMPAT_output_size;
	float2 corrected_size = COMPAT_video_size * float2(aspect_x / aspect_y, 1.0)
		 * float2(COMPAT_video_size.y / COMPAT_video_size.x, 1.0);
	float full_scale = (integer_scale > 0.5) ? floor(COMPAT_output_size.y /
		COMPAT_video_size.y) + overscale : COMPAT_output_size.y / COMPAT_video_size.y;
	float2 scale = (COMPAT_output_size / corrected_size) / full_scale;
	float2 middle = float2(0.49999, 0.49999) * COMPAT_video_size / COMPAT_texture_size;
	float2 diff = texCoord.xy - middle;
	OUT.texCoord = middle + diff * scale;

	float2 dist = t1 - middle;
	OUT.tex_border = texCoord;
	return OUT;
}

#define fragcoord (tex.xy * (texture_size.xy/video_size.xy))

float4 conv_background(float4 back, float2 coord, float frame_count)
{
   return float4(back.rgb, back.a);
}

float4 scanlines(float4 frame, float2 coord, float2 texture_size, float2
	video_size, float2 output_size)
{
	float lines = fract(coord.y * texture_size.y);
	float scale_factor = floor((output_size.y / video_size.y) + 0.4999);
	return (scanline_toggle > 0.5 && (lines < (1.0 / scale_factor * THICKNESS)))
		? frame * vec4(1.0 - DARKNESS) : frame;
}

float2 interp_coord(float2 coord, float2 texture_size)
{
	float2 p = coord.xy;

	p = p * texture_size.xy + float2(0.5, 0.5);

	float2 i = floor(p);
	float2 f = p - i;

	// Smoothstep - amazingly, smoothstep() is slower than calculating directly the expression!
	f = f * f * f * f * (f * (f * (-20.0 * f + float2(70.0, 70.0)) - float2(84.0, 84.0)) + float2(35.0, 35.0));

	p = i + f;

	p = (p - float2(0.5, 0.5)) * 1.0 / texture_size;
	return p;
}

float4 snow(float2 texture_size, float frame_count, float2 uv)
{
	const float3x3 p = float3x3(13.323122,21.1212,21.8112,23.5112,28.7312,14.7212,21.71123,11.9312,61.3934);
	float3 acc = float3(R, G, B);
	float dof = 5.*sin(frame_count*.1);
	for (int i=0;i<LAYERS;i++) {
		float fi = float(i);
		float2 q = uv*(1.+fi*DEPTH);
		q += float2(q.y*(WIDTH*fmod(abs(fi*7.238917),1.)-WIDTH*.5),SPEED*frame_count/(1.+fi*DEPTH*.03));
		float3 n = float3(floor(q),31.189+fi);
		float3 m = floor(n)*.00001 + frac(n);
		float3 mp = (31415.9+m)/frac(mul(p,m));
		float3 r = frac(mp);
		float2 s = abs(fmod(abs(q),1.)-.5+.9*r.xy-.45);
		s += .01*abs(2.*frac(10.*q.yx)-1.); 
		float d = .6*max(s.x-s.y,s.x+s.y)+max(s.x,s.y)-.01;
		float edge = .05+.05*min(.5*abs(fi-5.-dof),1.);
		float acc_mod = smoothstep(edge,-edge,d)*(r.x/(1.+.02*fi*DEPTH));
		acc += float3(acc_mod, acc_mod, acc_mod);
	}
	return float4(float3(acc),1.0);
}

float4 border(float2 texture_size, float2 video_size, float2 output_size,
	float frame_count, float2 tex, COMPAT_Texture2D(decal), float2 tex_border)
{
	float4 effect = snow(texture_size, frame_count, tex_border * (texture_size / video_size)
	 * float2(output_size.x / output_size.y, 1.0));
	
	float2 coord = (interp_toggle < 0.5) ? tex : interp_coord(tex, texture_size);
	float4 frame = COMPAT_SamplePoint(decal, coord);
	frame = scanlines(frame, tex, texture_size, video_size, output_size);
	if (fragcoord.x < 1.0 - OS_MASK_RIGHT && fragcoord.x > 0.0 + OS_MASK_LEFT &&
		fragcoord.y < 1.0 - OS_MASK_BOTTOM && fragcoord.y > 0.0 + OS_MASK_TOP)
		return frame;
	
	else return effect;
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return border(COMPAT_texture_size, COMPAT_video_size, COMPAT_output_size,
		COMPAT_frame_count, VOUT.texCoord, decal, VOUT.tex_border);
}
COMPAT_END
