# Total instructions: 137

!!ARBvp1.0
OPTION ARB_position_invariant;

# Set texture coordinates
MOV	result.texcoord[0], vertex.attrib[8];

# Set clipmap visibility
SGE	result.texcoord[1].x, program.local[1].w, 1.5;
SGE	result.texcoord[1].y, program.local[2].w, 1.5;
SGE	result.texcoord[1].z, program.local[3].w, 1.5;
SGE	result.texcoord[1].w, program.local[4].w, 1.5;
SGE	result.texcoord[2].x, program.local[5].w, 1.5;
SGE	result.texcoord[2].y, program.local[6].w, 1.5;

# Set player position for clipmaps
MAD	result.texcoord[3].xy, program.local[1].xyxy, 0.5, -0.25;
MAD	result.texcoord[3].zw, program.local[2].xyxy, 0.25, -0.125;
MAD	result.texcoord[4].xy, program.local[3].xyxy, 0.125, -0.0625;
MAD	result.texcoord[4].zw, program.local[4].xyxy, 0.0625, -0.03125;
MAD	result.texcoord[5].xy, program.local[5].xyxy, 0.03125, -0.015625;
MAD	result.texcoord[5].zw, program.local[6].xyxy, 0.015625, -0.0078125;

END

#======================================================================
#
# (0.24951171875/512)*128 = 0.0623779296875
# lodParams1.y: 0.24951171875-0.0623779296875 = 
# 
#
# 0.24951171875 -> 0.1871337890625
# 0.124755859375 -> 0.09356689453125
# 0.0623779296875 -> 0.046783447265625
# 0.03118896484375 -> 0.0233917236328125
# 0.015594482421875 -> 0.01169586181640625
# 0.0077972412109375 -> 0.005847930908203125
# 0.00389862060546875 -> 0.0029239654541015625
#
# 0.0623779296875
# 0.03118896484375
# 0.015594482421875
# 0.0077972412109375
# 0.00389862060546875
# 0.001949310302734375
# 0.0009746551513671875
#
# 123 instructions

!!ARBfp1.0
OPTION ARB_precision_hint_nicest;

PARAM lodParams1 = { 0.248046875, 0.124023438, 0.062011719, 0.031005859 };
PARAM lodParams2 = { 0.01550293, 0.007751465 };
PARAM lodParams3 = { 0.1871337890625, 0.09356689453125, 0.046783447265625, 0.0233917236328125 };
PARAM lodParams4 = { 0.01169586181640625, 0.005847930908203125, 0.0029239654541015625 };

TEMP newTexCoord, R0, R1, finalColor, cmp, cmp2, dist, dist2;
TEMP lod1;
TEMP lod2;
TEMP val1, val2, blendFactor, color0, color1;

# set black color
MOV	finalColor, 0;

### Level 0 ###

# Calculate distance between tex coord and player pos
ADD	R1, fragment.texcoord[0].xyxy, fragment.texcoord[3];
ABS	R1, R1;
FRC	R1, R1;
MAX	dist, R1.x, R1.y;

# Set lower limit
MUL lod1.x, lodParams1.x, fragment.texcoord[1].x;

TXB	color0, fragment.texcoord[0], texture[1], 2D;
SGE	cmp, dist, lod1.x;
MAD finalColor, cmp, color0, finalColor;

### Blend 0->1 ###

# Set lower limit
MOV	lod1.y, lodParams3.x;

# Calculate blending
SUB val1, dist.y, lod1.y;
SUB val2, lod1.xxxx, lod1.yyyy;
RCP val2, val2.x;
MUL blendFactor, val1, val2;

# Calculate tex coord
MUL	newTexCoord, fragment.texcoord[0], 2;

TXB	color1, newTexCoord, texture[2], 2D;
LRP color1, blendFactor, color0, color1;
SLT	cmp, dist, lod1.x;
SGE	cmp2, dist, lod1.y;
MIN	cmp, cmp, cmp2;
MIN cmp, fragment.texcoord[1].x, cmp;
MAD finalColor, cmp, color1, finalColor;

### Level 1 ###

# set dist2
MAX	dist2, R1.z, R1.w;

# Set new upper limit
MOV lod1.x, lodParams3.x;
# Set new lower limit
MUL lod1.y, lodParams1.y, fragment.texcoord[1].y;

TXB	color0, newTexCoord, texture[2], 2D;
SLT	cmp, dist, lod1.x;
SGE	cmp2, dist2, lod1.y;
MIN	cmp, cmp, cmp2;
MIN cmp, fragment.texcoord[1].x, cmp;
MAD finalColor, cmp, color0, finalColor;

### Blend 1->2 ###

# Set lower limit
MOV lod1.z, lodParams3.y;

# Calculate blending
SUB val1, dist2.y, lod1.z;
SUB val2, lod1.yyyy, lod1.zzzz;
RCP val2, val2.x;
MUL blendFactor, val1, val2;

# Calculate tex coord
MUL	newTexCoord, newTexCoord, 2;

TXB	color1, newTexCoord, texture[3], 2D;
LRP color1, blendFactor, color0, color1;
SLT	cmp, dist2, lod1.y;
SGE	cmp2, dist2, lod1.z;
MIN	cmp, cmp, cmp2;
MIN cmp, fragment.texcoord[1].y, cmp;
MAD finalColor, cmp, color1, finalColor;


### Level 2 ###

# Calculate distance between tex coord and player pos
ADD	R1, fragment.texcoord[0].xyxy, fragment.texcoord[4];
ABS	R1, R1;
FRC	R1, R1;
MAX	dist, R1.x, R1.y;

# Set upper limit
MOV lod1.y, lodParams3.y;
# Set lower limit
MUL lod1.z, lodParams1.z, fragment.texcoord[1].z;

TXB	color0, newTexCoord, texture[3], 2D;
SLT	cmp, dist2, lod1.y;
SGE	cmp2, dist, lod1.z;
MIN	cmp, cmp, cmp2;
MIN cmp, fragment.texcoord[1].y, cmp;
MAD finalColor, cmp, color0, finalColor;

### Blend 2->3 ###

# Set lower limit
MOV lod1.w, lodParams3.z;

# Calculate blending
SUB val1, dist.y, lod1.w; # lower
SUB val2, lod1.zzzz, lod1.wwww; # upper
RCP val2, val2.x;
MUL blendFactor, val1, val2;

# Calculate tex coord
MUL	newTexCoord, fragment.texcoord[0], 8;

TXB	color1, newTexCoord, texture[4], 2D;
LRP color1, blendFactor, color0, color1;
SLT	cmp, dist, lod1.z;
SGE	cmp2, dist, lod1.w;
MIN	cmp, cmp, cmp2;
MIN cmp, fragment.texcoord[1].z, cmp;
MAD finalColor, cmp, color1, finalColor;

### Level 3 ###

# distance
MAX	dist2, R1.z, R1.w;

# Set new upper limit
MOV lod1.z, lodParams3.z;
# Set new lower limit
MUL lod1.w, lodParams1.w, fragment.texcoord[1].w;

TXB	color0, newTexCoord, texture[4], 2D;
SLT	cmp, dist, lod1.z;
SGE	cmp2, dist2, lod1.w;
MIN	cmp, cmp, cmp2;
MIN cmp, fragment.texcoord[1].z, cmp;
MAD finalColor, cmp, color0, finalColor;

### Blend 3->4 ###

# Set lower limit
MOV lod2.x, lodParams3.w;

# Calculate blending
SUB val1, dist2.y, lod2.x;
SUB val2, lod1.wwww, lod2.xxxx;
RCP val2, val2.x;
MUL blendFactor, val1, val2;

# Calculate tex coord
MUL	newTexCoord, newTexCoord, 2;

TXB	color1, newTexCoord, texture[5], 2D;
LRP color1, blendFactor, color0, color1;
SLT	cmp, dist2, lod1.w;
SGE	cmp2, dist2, lod2.x;
MIN	cmp, cmp, cmp2;
MIN cmp, fragment.texcoord[1].w, cmp;
MAD finalColor, cmp, color1, finalColor;

### Level 4 ###

# Calculate distance between tex coord and player pos
ADD	R1, fragment.texcoord[0].xyxy, fragment.texcoord[5];
ABS	R1, R1;
FRC	R1, R1;
MAX	dist, R1.x, R1.y;

# Set new upper limit
MOV lod1.w, lodParams3.w;
# Set new lower limit
MUL lod2.x, lodParams2.x, fragment.texcoord[2].x;

TXB	color0, newTexCoord, texture[5], 2D;
SLT	cmp, dist2, lod1.w;
SGE	cmp2, dist, lod2.x;
MIN	cmp, cmp, cmp2;
MIN cmp, fragment.texcoord[1].w, cmp;
MAD finalColor, cmp, color0, finalColor;

### Blend 4->5 ###

# Set lower limit
MOV lod2.y, lodParams4.x;

# Calculate blending
SUB val1, dist.y, lod2.y; # lower
SUB val2, lod2.xxxx, lod2.yyyy; # upper-lower
RCP val2, val2.x;
MUL blendFactor, val1, val2;

# Calculate tex coord
MUL	newTexCoord, fragment.texcoord[0], 32;

TXB	color1, newTexCoord, texture[6], 2D;
LRP color1, blendFactor, color0, color1;
SLT	cmp, dist, lod2.x;
SGE	cmp2, dist, lod2.y;
MIN	cmp, cmp, cmp2;
MIN cmp, fragment.texcoord[2].x, cmp;
MAD finalColor, cmp, color1, finalColor;

### Level 5 ###

# Set new upper limit
MOV lod2.x, lodParams4.x;

TXB	color0, newTexCoord, texture[6], 2D;
SLT	cmp, dist, lod2.x;
MIN cmp, fragment.texcoord[2].x, cmp;
MAD finalColor, cmp, color0, finalColor;

MOV	result.color.xyz, finalColor;

END