!!ARBfp1.0
OPTION ARB_precision_hint_fastest;

#/*ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*\
#	File:			Program for XREngine final framebuffer paste
#					
#	Author:			Magnus Högdahl
#					
#	Copyright:		Starbreeze AB 2004
#					
#	History:
#
# 		Cycles/64 pixel vector: ALU 16, vertex 0, texture 16, sequencer 12, interpolator 8
# 		3 GPRs, 63 threads, Performance (if enough threads): ~16 cycles per vector
#		577400 ns @ 1280x720
#\*____________________________________________________________________________________________*/


#-----------------------------------
OUTPUT oCol = result.color;

ATTRIB tc0 = fragment.texcoord[0];
ATTRIB tc1 = fragment.texcoord[1];


PARAM Exposure = program.env[0];
PARAM ContrastSaturation = program.env[1];
#PARAM H1 = program.env[1];

PARAM Const1 = { 2.7182818284590, 1, 2, 0.5 };
#PARAM H0f = { 3.75, 2.5, 0.75, 0.5 };
#PARAM H1f = { 0.5, 0.4, -0, 0 };

TEMP t0;
TEMP t1;
TEMP t2;
TEMP r0;
TEMP r1;
TEMP r2;

#-----------------------------------

TEX t0, tc0, texture[0], 2D;
TEX t1, tc1, texture[1], 2D;

MUL t1,t1,t1;
MUL t0,t0,t0;


ADD r0, t0, t1;
MUL_SAT r1.rgb, t0, t1;
SUB r0.rgb, r0, r1;

@if xenon

@else

MAX r2.a, r0.r, r0.g;
MAX r2.a, r0.b, r2.a;
RCP r1.a, r2.a;
MUL r1.rgb, r0, r1.a;
SUB r1.rgb, r1, 0.5;
TEX t2, r1, texture[2], CUBE;
MUL r0, t2, r2.a;
@endif

# (1 - exp(-light*exposure))
MUL r1.rgb, -r0, Exposure;
POW r1.r, Const1.x, r1.r;
POW r1.g, Const1.x, r1.g;
POW r1.b, Const1.x, r1.b;
SUB r1.rgb, Const1.y, r1;
MOV r0.rgb, r1;

MAX r0, r0, 0.00000001;
RSQ r1.x, r0.x;
RSQ r1.y, r0.y;
RSQ r1.z, r0.z;
MUL r0.rgb, r0, r1;


# --- Contrast ----
#DP3 r1.a, r0, r0;	# r1.a = len^2
#RSQ r1.r, r1.a;		# r1.r = 1/len
#MUL r1.g, r1.r, r1.a;	# r1.g = len

#MOV oCol, r1.g;

#POW r1.g, r1.g, Const1.w;
#MUL r1.g, r1.g, r1.g;

#MOV r1.b, r1.g;
#MUL r1.g, r1.g, Const1.z;
#SUB r1.g, r1.g, 1;
#MAX r1.g, r1.g, -r1.g;
#POW r1.g, r1.g, ContrastSaturation.x;
#SUB r1.b, r1.b, 0.5;
#MOV r2.g, -r1.g;
#CMP r1.g, r1.b, r2.g, r1.g;
#ADD r1.g, r1.g, 1;
#MUL r1.g, r1.g, 0.5;

#MUL r1.g, r1.g, r1.g;
#POW r1.g, r1.g, Const1.w;

#MUL r1.g, r1.g, r1.r;
#MUL r0.rgb, r0, r1.g;

# ---


MOV oCol, r0;


END

