//
// Project:
//
//   D3D RightMark
//
// Description:
//   Effect file for "Geometry Processing Speed" test.
//
// Effect Version: 
//
//   DirectX 9.0 (Summer 2003 SDK Update)
//
// Shader Profile:
//
//   Fixed-Function Pixel Shader
//
//   Vertex Shaders 1.1 ( Vertex Shader Assembler )
//

//
// Global Variables
//

vector vZero = { 0.0f, 0.0f, 0.0f, 0.0f };
vector vOne  = { 1.0f, 1.0f, 1.0f, 1.0f };

vector vLight0Pos;
vector vLight1Pos;
vector vLight2Pos;

vector vLight0Diffuse = { 1.0f, 0.0f, 0.0f, 1.0f };
vector vLight1Diffuse = { 0.0f, 1.0f, 0.0f, 1.0f };
vector vLight2Diffuse = { 0.0f, 0.0f, 1.0f, 1.0f };

vector vLight0Specular = { 1.0f, 1.0f, 1.0f, 1.0f };
vector vLight1Specular = { 1.0f, 1.0f, 1.0f, 1.0f };
vector vLight2Specular = { 1.0f, 1.0f, 1.0f, 1.0f };

vector vLight0Attenuation = { 0.0f, 0.1f, 0.0f, 0.0f };
vector vLight1Attenuation = { 0.0f, 0.1f, 0.0f, 0.0f };
vector vLight2Attenuation = { 0.0f, 0.1f, 0.0f, 0.0f };

vector vLight0Range = { 500.0f, 500.0f, 500.0f, 500.0f };
vector vLight1Range = { 500.0f, 500.0f, 500.0f, 500.0f };
vector vLight2Range = { 500.0f, 500.0f, 500.0f, 500.0f };

vector vMaterialDiffuse  = { 40.0f,  40.0f,  40.0f, 255.0f };
vector vMaterialSpecular = { 40.0f,  40.0f,  40.0f, 255.0f };
vector vMaterialAmbient  = {  0.0f,   0.0f,   0.0f, 255.0f };
vector vMaterialEmissive = {  0.0f,   0.0f,   0.0f, 255.0f };

vector vMaterialPower = { 16.0f,  16.0f,  16.0f, 16.0f };

vector vCameraPos;	// Camera Position

matrix mW;		// World Matrix
matrix mWVP;		// World * View * Projection Matrix

//
// Technique 1: Ambient Lighting
//

technique TAmbient
{   
	pass p0
	{
		//
		// Common States
		//

		CullMode		 = CCW;
		AlphaBlendEnable	 = False;
		ZEnable			 = True;

		ColorOp[ 0 ]             = SelectArg1;
	        ColorArg1[ 0 ] 		 = Diffuse;
		
		AlphaOp[ 0 ]             = SelectArg1;
	        AlphaArg1[ 0 ] 		 = Diffuse;
        
	        ColorOp[ 1 ]   		 = Disable;
        	AlphaOp[ 1 ]   		 = Disable;

		//
		// Shader Constants
		//

		VertexShaderConstant[0]  = <vZero>;	  // 0, 0, 0, 0
		VertexShaderConstant[1]  = <vOne>;	  // 1, 1, 1, 1

		VertexShaderConstant[12] = <mWVP>;	  // World * View * Projections Matrix

		//
		// Shaders
		//

		VertexShader = 

		   decl {
				stream 0;
				FLOAT    v0[3];
				FLOAT    v3[3];
			}
		   asm {	
				vs_1_1

				dcl_position v0
				dcl_normal   v3

				//
				// Position Setup
				//

				m4x4 oPos, v0, c[12]		

				//
				// Lighting Setup
				//

				mov oD0, c[1]
			};
	}
}

//
// Technique 2: 1 Diffuse Per-Vertex Point Light 
//

technique T1Diffuse
{   
	pass p0
	{
		//
		// Common States
		//

		CullMode		 = CCW;
		AlphaBlendEnable	 = False;
		ZWriteEnable		 = True;

		ColorOp[ 0 ]             = SelectArg1;
	        ColorArg1[ 0 ] 		 = Diffuse;

		AlphaOp[ 0 ]             = SelectArg1;
	        AlphaArg1[ 0 ] 		 = Diffuse;
        
	        ColorOp[ 1 ]   		 = Disable;
        	AlphaOp[ 1 ]   		 = Disable;

		//
		// Shader Constants
		//

		VertexShaderConstant[0]  = <vZero>;	         // 0, 0, 0, 0
		VertexShaderConstant[1]  = <vOne>;	         // 1, 1, 1, 1

		VertexShaderConstant[8]  = <mW>;	         // World Matrix
		VertexShaderConstant[16] = <mWVP>;	         // World * View * Projections Matrix

		VertexShaderConstant[40] = <vLight0Pos>;         // Light 0 Position
		VertexShaderConstant[41] = <vLight1Pos>;         // Light 1 Position
		VertexShaderConstant[42] = <vLight2Pos>;         // Light 2 Position

		VertexShaderConstant[30] = <vLight0Diffuse>;     // Light 0 Diffuse Color
		VertexShaderConstant[31] = <vLight1Diffuse>;     // Light 1 Diffuse Color
		VertexShaderConstant[32] = <vLight2Diffuse>;     // Light 2 Diffuse Color

		VertexShaderConstant[70] = <vLight0Attenuation>; // Light 0 Attenuation
		VertexShaderConstant[71] = <vLight1Attenuation>; // Light 1 Attenuation
		VertexShaderConstant[72] = <vLight2Attenuation>; // Light 2 Attenuation

		VertexShaderConstant[80] = <vLight0Range>;       // Light 0 Range
		VertexShaderConstant[81] = <vLight1Range>;       // Light 1 Range
		VertexShaderConstant[82] = <vLight2Range>;       // Light 2 Range

		VertexShaderConstant[90] = <vMaterialDiffuse>;   // Material Diffuse Color
		VertexShaderConstant[91] = <vMaterialSpecular>;  // Material Specular Color

		VertexShaderConstant[94] = <vMaterialPower>;     // Material Specular Power

		//
		// Shaders
		//

		VertexShader = 

		   decl {
				stream 0;
				FLOAT    v0[3];
				FLOAT    v3[3];
			}
		   asm {	
				vs_1_1

				dcl_position v0
				dcl_normal   v3

				//
				// Position Setup
				//

				m4x4 oPos, v0, c16

				//
				// Lighting Setup
				//
				
				m4x4 r10, v0, c8  	    // transform position to world space
				m3x3 r7.xyz, v3.xyz, c8     // transform normal to world space

				dp3 r7.w, r7, r7    	    // normalize normal
				rsq r7.w, r7.w
				mul r7, r7, r7.w

				mov r4, c0		    // set diffuse to 0,0,0,0

				mov r2, c0		    // setup diffuse,specular factors to 0,0
				mov r2.w, c94.w		    // setup specular power

				//
				//  Diffuse Lighting
				//
				
				// light 1

				add r1, c[40], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[70]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				dp3 r2.x, r7, r1	    // N*L

				sge r3.x, c[80].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x

				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[30]	    // calculate diffuse color
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				//
				// Finalize Lighting
				//

				mov  oD0, r4		    // final color
			};
	}
}

//
// Technique 3: 2 Diffuse Per-Vertex Point Light 
//

technique T2Diffuse
{   
	pass p0
	{
		//
		// Common States
		//

		CullMode		 = CCW;
		AlphaBlendEnable	 = False;
		ZWriteEnable		 = True;

		ColorOp[ 0 ]             = SelectArg1;
	        ColorArg1[ 0 ] 		 = Diffuse;

		AlphaOp[ 0 ]             = SelectArg1;
	        AlphaArg1[ 0 ] 		 = Diffuse;
        
	        ColorOp[ 1 ]   		 = Disable;
        	AlphaOp[ 1 ]   		 = Disable;

		//
		// Shader Constants
		//

		VertexShaderConstant[0]  = <vZero>;	         // 0, 0, 0, 0
		VertexShaderConstant[1]  = <vOne>;	         // 1, 1, 1, 1

		VertexShaderConstant[8]  = <mW>;	         // World Matrix
		VertexShaderConstant[16] = <mWVP>;	         // World * View * Projections Matrix

		VertexShaderConstant[40] = <vLight0Pos>;         // Light 0 Position
		VertexShaderConstant[41] = <vLight1Pos>;         // Light 1 Position
		VertexShaderConstant[42] = <vLight2Pos>;         // Light 2 Position

		VertexShaderConstant[30] = <vLight0Diffuse>;     // Light 0 Diffuse Color
		VertexShaderConstant[31] = <vLight1Diffuse>;     // Light 1 Diffuse Color
		VertexShaderConstant[32] = <vLight2Diffuse>;     // Light 2 Diffuse Color

		VertexShaderConstant[70] = <vLight0Attenuation>; // Light 0 Attenuation
		VertexShaderConstant[71] = <vLight1Attenuation>; // Light 1 Attenuation
		VertexShaderConstant[72] = <vLight2Attenuation>; // Light 2 Attenuation

		VertexShaderConstant[80] = <vLight0Range>;       // Light 0 Range
		VertexShaderConstant[81] = <vLight1Range>;       // Light 1 Range
		VertexShaderConstant[82] = <vLight2Range>;       // Light 2 Range

		VertexShaderConstant[90] = <vMaterialDiffuse>;   // Material Diffuse Color
		VertexShaderConstant[91] = <vMaterialSpecular>;  // Material Specular Color

		VertexShaderConstant[94] = <vMaterialPower>;     // Material Specular Power
		//
		// Shaders
		//

		VertexShader = 

		   decl {
				stream 0;
				FLOAT    v0[3];
				FLOAT    v3[3];
			}
		   asm {	
				vs_1_1

				dcl_position v0
				dcl_normal   v3

				//
				// Position Setup
				//

				m4x4 oPos, v0, c16

				//
				// Lighting Setup
				//
				
				m4x4 r10, v0, c8  	    // transform position to world space
				m3x3 r7.xyz, v3.xyz, c8     // transform normal to world space

				dp3 r7.w, r7, r7    	    // normalize normal
				rsq r7.w, r7.w
				mul r7, r7, r7.w

				mov r4, c0		    // set diffuse to 0,0,0,0

				mov r2, c0		    // setup diffuse,specular factors to 0,0
				mov r2.w, c94.w		    // setup specular power

				//
				//  Diffuse Lighting
				//
				
				// light 1

				add r1, c[40], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[70]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				dp3 r2.x, r7, r1	    // N*L

				sge r3.x, c[80].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x

				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[30]	    // calculate diffuse color
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				// light 2

				add r1, c[41], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[71]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				dp3 r2.x, r7, r1	    // N*L

				sge r3.x, c[81].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x

				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[31]	    // calculate diffuse color
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				//
				// Finalize Lighting
				//

				mov  oD0, r4		    // final color
			};
	}
}

//
// 3 Diffuse Per-Vertex Point Light 
//

technique T3Diffuse
{   
	pass p0
	{
		//
		// Common States
		//

		CullMode		 = CCW;
		AlphaBlendEnable	 = False;
		ZWriteEnable		 = True;

		ColorOp[ 0 ]             = SelectArg1;
	        ColorArg1[ 0 ] 		 = Diffuse;

		AlphaOp[ 0 ]             = SelectArg1;
	        AlphaArg1[ 0 ] 		 = Diffuse;
        
	        ColorOp[ 1 ]   		 = Disable;
        	AlphaOp[ 1 ]   		 = Disable;

		//
		// Shader Constants
		//

		VertexShaderConstant[0]  = <vZero>;	         // 0, 0, 0, 0
		VertexShaderConstant[1]  = <vOne>;	         // 1, 1, 1, 1

		VertexShaderConstant[8]  = <mW>;	         // World Matrix
		VertexShaderConstant[16] = <mWVP>;	         // World * View * Projections Matrix

		VertexShaderConstant[40] = <vLight0Pos>;         // Light 0 Position
		VertexShaderConstant[41] = <vLight1Pos>;         // Light 1 Position
		VertexShaderConstant[42] = <vLight2Pos>;         // Light 2 Position

		VertexShaderConstant[30] = <vLight0Diffuse>;     // Light 0 Diffuse Color
		VertexShaderConstant[31] = <vLight1Diffuse>;     // Light 1 Diffuse Color
		VertexShaderConstant[32] = <vLight2Diffuse>;     // Light 2 Diffuse Color

		VertexShaderConstant[70] = <vLight0Attenuation>; // Light 0 Attenuation
		VertexShaderConstant[71] = <vLight1Attenuation>; // Light 1 Attenuation
		VertexShaderConstant[72] = <vLight2Attenuation>; // Light 2 Attenuation

		VertexShaderConstant[80] = <vLight0Range>;       // Light 0 Range
		VertexShaderConstant[81] = <vLight1Range>;       // Light 1 Range
		VertexShaderConstant[82] = <vLight2Range>;       // Light 2 Range

		VertexShaderConstant[90] = <vMaterialDiffuse>;   // Material Diffuse Color
		VertexShaderConstant[91] = <vMaterialSpecular>;  // Material Specular Color

		VertexShaderConstant[94] = <vMaterialPower>;     // Material Specular Power

		//
		// Shaders
		//

		VertexShader = 

		   decl {
				stream 0;
				FLOAT    v0[3];
				FLOAT    v3[3];
			}
		   asm {	
				vs_1_1

				dcl_position v0
				dcl_normal   v3

				//
				// Position Setup
				//

				m4x4 oPos, v0, c16

				//
				// Lighting Setup
				//
				
				m4x4 r10, v0, c8  	    // transform position to world space
				m3x3 r7.xyz, v3.xyz, c8     // transform normal to world space

				dp3 r7.w, r7, r7    	    // normalize normal
				rsq r7.w, r7.w
				mul r7, r7, r7.w

				mov r4, c0		    // set diffuse to 0,0,0,0

				mov r2, c0		    // setup diffuse,specular factors to 0,0
				mov r2.w, c94.w		    // setup specular power

				//
				//  Diffuse Lighting
				//
				
				// light 1

				add r1, c[40], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[70]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				dp3 r2.x, r7, r1	    // N*L

				sge r3.x, c[80].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x

				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[30]	    // calculate diffuse color
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				// light 2

				add r1, c[41], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[71]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				dp3 r2.x, r7, r1	    // N*L

				sge r3.x, c[81].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x

				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[31]	    // calculate diffuse color
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				// light 3

				add r1, c[42], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[72]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				dp3 r2.x, r7, r1	    // N*L

				sge r3.x, c[82].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x

				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[32]	    // calculate diffuse color
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				//
				// Finalize Lighting
				//

				mov  oD0, r4		    // final color
			};
	}
}

technique T1DiffuseSpecular
{   
	pass p0
	{
		//
		// Common States
		//

		CullMode		 = CCW;
		AlphaBlendEnable	 = False;
		ZWriteEnable		 = True;

		ColorOp[ 0 ]             = SelectArg1;
	        ColorArg1[ 0 ] 		 = Diffuse;

		AlphaOp[ 0 ]             = SelectArg1;
	        AlphaArg1[ 0 ] 		 = Diffuse;
        
	        ColorOp[ 1 ]   		 = Disable;
        	AlphaOp[ 1 ]   		 = Disable;

		//
		// Shader Constants
		//

		VertexShaderConstant[0]  = <vZero>;	         // 0, 0, 0, 0
		VertexShaderConstant[1]  = <vOne>;	         // 1, 1, 1, 1

		VertexShaderConstant[2]   = <vCameraPos>;        // Camera Position

		VertexShaderConstant[8]  = <mW>;	         // World Matrix
		VertexShaderConstant[16] = <mWVP>;	         // World * View * Projections Matrix

		VertexShaderConstant[40] = <vLight0Pos>;         // Light 0 Position
		VertexShaderConstant[41] = <vLight1Pos>;         // Light 1 Position
		VertexShaderConstant[42] = <vLight2Pos>;         // Light 2 Position

		VertexShaderConstant[30] = <vLight0Diffuse>;     // Light 0 Diffuse Color
		VertexShaderConstant[31] = <vLight1Diffuse>;     // Light 1 Diffuse Color
		VertexShaderConstant[32] = <vLight2Diffuse>;     // Light 2 Diffuse Color

		VertexShaderConstant[60] = <vLight0Specular>;    // Light 0 Specular Color
		VertexShaderConstant[61] = <vLight1Specular>;    // Light 1 Specular Color
		VertexShaderConstant[62] = <vLight2Specular>;    // Light 2 Specular Color

		VertexShaderConstant[70] = <vLight0Attenuation>; // Light 0 Attenuation
		VertexShaderConstant[71] = <vLight1Attenuation>; // Light 1 Attenuation
		VertexShaderConstant[72] = <vLight2Attenuation>; // Light 2 Attenuation

		VertexShaderConstant[80] = <vLight0Range>;       // Light 0 Range
		VertexShaderConstant[81] = <vLight1Range>;       // Light 1 Range
		VertexShaderConstant[82] = <vLight2Range>;       // Light 2 Range

		VertexShaderConstant[90] = <vMaterialDiffuse>;   // Material Diffuse Color
		VertexShaderConstant[91] = <vMaterialSpecular>;  // Material Specular Color

		VertexShaderConstant[94] = <vMaterialPower>;     // Material Specular Power

		//
		// Shaders
		//

		VertexShader = 

		   decl {
				stream 0;
				FLOAT    v0[3];
				FLOAT    v3[3];
			}
		   asm {	
				vs_1_1

				dcl_position v0
				dcl_normal   v3

				//
				// Position Setup
				//

				m4x4 oPos, v0, c16

				//
				// Lighting Setup
				//
				
				m4x4 r10, v0, c8  	    // transform position to world space
				m3x3 r7.xyz, v3.xyz, c8     // transform normal to world space

				dp3 r7.w, r7, r7    	    // normalize normal
				rsq r7.w, r7.w
				mul r7, r7, r7.w

				add r6, -r10, c2     	    // get a vector toward the camera position

				dp3 r6.w, r6, r6    	    // normalize eye-vector
				rsq r6.w, r6.w
				mul r6, r6, r6.w

				mov r4, c0		    // set diffuse to 0,0,0,0

				mov r2, c0		    // setup diffuse,specular factors
				mov r2.w, c94.w		    // setup specular power

				//
				//  Diffuse Lighting
				//
				
				// light 1

				add r1, c[40], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[70]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				add r0, r6, r1		    // calculate half-vector (light vector + eye vector)

				dp3 r0.w, r0, r0    	    // normalize half-vector
				rsq r0.w, r0.w
				mul r0, r0, r0.w

				dp3 r2.x, r7, r1	    // N*L
				dp3 r2.yz, r7, r0 	    // N*H

				sge r3.x, c[80].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x
				mul r2.y, r2.y, r3.x

				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[30]	    // calculate diffuse color
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				mul r0, r5.z, c[60]	    // calculate specular color
				mad r4, r0, c91, r4         // add (specular color * material specular)

				//
				// Finalize Lighting
				//

				mov  oD0, r4		    // final color
			};
	}
}

technique T2DiffuseSpecular
{   
	pass p0
	{
		//
		// Common States
		//

		CullMode		 = CCW;
		AlphaBlendEnable	 = False;
		ZWriteEnable		 = True;

		ColorOp[ 0 ]             = SelectArg1;
	        ColorArg1[ 0 ] 		 = Diffuse;

		AlphaOp[ 0 ]             = SelectArg1;
	        AlphaArg1[ 0 ] 		 = Diffuse;
        
	        ColorOp[ 1 ]   		 = Disable;
        	AlphaOp[ 1 ]   		 = Disable;

		//
		// Shader Constants
		//

		VertexShaderConstant[0]  = <vZero>;	         // 0, 0, 0, 0
		VertexShaderConstant[1]  = <vOne>;	         // 1, 1, 1, 1

		VertexShaderConstant[2]   = <vCameraPos>;        // Camera Position

		VertexShaderConstant[8]  = <mW>;	         // World Matrix
		VertexShaderConstant[16] = <mWVP>;	         // World * View * Projections Matrix

		VertexShaderConstant[40] = <vLight0Pos>;         // Light 0 Position
		VertexShaderConstant[41] = <vLight1Pos>;         // Light 1 Position
		VertexShaderConstant[42] = <vLight2Pos>;         // Light 2 Position

		VertexShaderConstant[30] = <vLight0Diffuse>;     // Light 0 Diffuse Color
		VertexShaderConstant[31] = <vLight1Diffuse>;     // Light 1 Diffuse Color
		VertexShaderConstant[32] = <vLight2Diffuse>;     // Light 2 Diffuse Color

		VertexShaderConstant[60] = <vLight0Specular>;    // Light 0 Specular Color
		VertexShaderConstant[61] = <vLight1Specular>;    // Light 1 Specular Color
		VertexShaderConstant[62] = <vLight2Specular>;    // Light 2 Specular Color

		VertexShaderConstant[70] = <vLight0Attenuation>; // Light 0 Attenuation
		VertexShaderConstant[71] = <vLight1Attenuation>; // Light 1 Attenuation
		VertexShaderConstant[72] = <vLight2Attenuation>; // Light 2 Attenuation

		VertexShaderConstant[80] = <vLight0Range>;       // Light 0 Range
		VertexShaderConstant[81] = <vLight1Range>;       // Light 1 Range
		VertexShaderConstant[82] = <vLight2Range>;       // Light 2 Range

		VertexShaderConstant[90] = <vMaterialDiffuse>;   // Material Diffuse Color
		VertexShaderConstant[91] = <vMaterialSpecular>;  // Material Specular Color

		VertexShaderConstant[94] = <vMaterialPower>;     // Material Specular Power

		//
		// Shaders
		//

		VertexShader = 

		   decl {
				stream 0;
				FLOAT    v0[3];
				FLOAT    v3[3];
			}
		   asm {	
				vs_1_1

				dcl_position v0
				dcl_normal   v3

				//
				// Position Setup
				//

				m4x4 oPos, v0, c16

				//
				// Lighting Setup
				//
				
				m4x4 r10, v0, c8  	    // transform position to world space
				m3x3 r7.xyz, v3.xyz, c8     // transform normal to world space

				dp3 r7.w, r7, r7    	    // normalize normal
				rsq r7.w, r7.w
				mul r7, r7, r7.w

				add r6, -r10, c2     	    // get a vector toward the camera position

				dp3 r6.w, r6, r6    	    // normalize eye-vector
				rsq r6.w, r6.w
				mul r6, r6, r6.w

				mov r4, c0		    // set diffuse to 0,0,0,0

				mov r2, c0		    // setup diffuse,specular factors
				mov r2.w, c94.w		    // setup specular power

				//
				//  Diffuse Lighting
				//
				
				// light 1

				add r1, c[40], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[70]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				add r0, r6, r1		    // calculate half-vector (light vector + eye vector)

				dp3 r0.w, r0, r0    	    // normalize half-vector
				rsq r0.w, r0.w
				mul r0, r0, r0.w

				dp3 r2.x, r7, r1	    // N*L
				dp3 r2.yz, r7, r0 	    // N*H

				sge r3.x, c[80].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x
				mul r2.y, r2.y, r3.x

				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[30]	    // calculate diffuse color
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				mul r0, r5.z, c[60]	    // calculate specular color
				mad r4, r0, c91, r4         // add (specular color * material specular)

				// light 2

				add r1, c[41], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[71]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				add r0, r6, r1		    // calculate half-vector (light vector + eye vector)

				dp3 r0.w, r0, r0    	    // normalize half-vector
				rsq r0.w, r0.w
				mul r0, r0, r0.w

				dp3 r2.x, r7, r1	    // N*L
				dp3 r2.yz, r7, r0 	    // N*H

				sge r3.x, c[81].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x
				mul r2.y, r2.y, r3.x

				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[31]	    // calculate diffuse color
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				mul r0, r5.z, c[61]	    // calculate specular color
				mad r4, r0, c91, r4         // add (specular color * material specular)

				//
				// Finalize Lighting
				//

				mov  oD0, r4		    // final color
			};
	}
}

technique T3DiffuseSpecular
{   
	pass p0
	{
		//
		// Common States
		//

		CullMode		 = CCW;
		AlphaBlendEnable	 = False;
		ZWriteEnable		 = True;

		ColorOp[ 0 ]             = SelectArg1;
	        ColorArg1[ 0 ] 		 = Diffuse;

		AlphaOp[ 0 ]             = SelectArg1;
	        AlphaArg1[ 0 ] 		 = Diffuse;
        
	        ColorOp[ 1 ]   		 = Disable;
        	AlphaOp[ 1 ]   		 = Disable;

		//
		// Shader Constants
		//

		VertexShaderConstant[0]  = <vZero>;	         // 0, 0, 0, 0
		VertexShaderConstant[1]  = <vOne>;	         // 1, 1, 1, 1

		VertexShaderConstant[2]   = <vCameraPos>;        // Camera Position

		VertexShaderConstant[8]  = <mW>;	         // World Matrix
		VertexShaderConstant[16] = <mWVP>;	         // World * View * Projections Matrix

		VertexShaderConstant[40] = <vLight0Pos>;         // Light 0 Position
		VertexShaderConstant[41] = <vLight1Pos>;         // Light 1 Position
		VertexShaderConstant[42] = <vLight2Pos>;         // Light 2 Position

		VertexShaderConstant[30] = <vLight0Diffuse>;     // Light 0 Diffuse Color
		VertexShaderConstant[31] = <vLight1Diffuse>;     // Light 1 Diffuse Color
		VertexShaderConstant[32] = <vLight2Diffuse>;     // Light 2 Diffuse Color

		VertexShaderConstant[60] = <vLight0Specular>;    // Light 0 Specular Color
		VertexShaderConstant[61] = <vLight1Specular>;    // Light 1 Specular Color
		VertexShaderConstant[62] = <vLight2Specular>;    // Light 2 Specular Color

		VertexShaderConstant[70] = <vLight0Attenuation>; // Light 0 Attenuation
		VertexShaderConstant[71] = <vLight1Attenuation>; // Light 1 Attenuation
		VertexShaderConstant[72] = <vLight2Attenuation>; // Light 2 Attenuation

		VertexShaderConstant[80] = <vLight0Range>;       // Light 0 Range
		VertexShaderConstant[81] = <vLight1Range>;       // Light 1 Range
		VertexShaderConstant[82] = <vLight2Range>;       // Light 2 Range

		VertexShaderConstant[90] = <vMaterialDiffuse>;   // Material Diffuse Color
		VertexShaderConstant[91] = <vMaterialSpecular>;  // Material Specular Color

		VertexShaderConstant[94] = <vMaterialPower>;     // Material Specular Power

		//
		// Shaders
		//

		VertexShader = 

		   decl {
				stream 0;
				FLOAT    v0[3];
				FLOAT    v3[3];
			}
		   asm {	
				vs_1_1

				dcl_position v0
				dcl_normal   v3

				//
				// Position Setup
				//

				m4x4 oPos, v0, c16

				//
				// Lighting Setup
				//
				
				m4x4 r10, v0, c8  	    // transform position to world space
				m3x3 r7.xyz, v3.xyz, c8     // transform normal to world space

				dp3 r7.w, r7, r7    	    // normalize normal
				rsq r7.w, r7.w
				mul r7, r7, r7.w

				add r6, -r10, c2     	    // get a vector toward the camera position

				dp3 r6.w, r6, r6    	    // normalize eye-vector
				rsq r6.w, r6.w
				mul r6, r6, r6.w

				mov r4, c0		    // set diffuse to 0,0,0,0

				mov r2, c0		    // setup diffuse,specular factors
				mov r2.w, c94.w		    // setup specular power

				//
				//  Diffuse Lighting
				//
				
				// light 1

				add r1, c[40], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[70]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				add r0, r6, r1		    // calculate half-vector (light vector + eye vector)

				dp3 r0.w, r0, r0    	    // normalize half-vector
				rsq r0.w, r0.w
				mul r0, r0, r0.w

				dp3 r2.x, r7, r1	    // N*L
				dp3 r2.yz, r7, r0 	    // N*H

				sge r3.x, c[80].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x
				mul r2.y, r2.y, r3.x

				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[30]	    // calculate diffuse color
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				mul r0, r5.z, c[60]	    // calculate specular color
				mad r4, r0, c91, r4         // add (specular color * material specular)

				// light 2

				add r1, c[41], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[71]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				add r0, r6, r1		    // calculate half-vector (light vector + eye vector)

				dp3 r0.w, r0, r0    	    // normalize half-vector
				rsq r0.w, r0.w
				mul r0, r0, r0.w

				dp3 r2.x, r7, r1	    // N*L
				dp3 r2.yz, r7, r0 	    // N*H

				sge r3.x, c[81].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x
				mul r2.y, r2.y, r3.x

				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[31]	    // calculate diffuse color
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				mul r0, r5.z, c[61]	    // calculate specular color
				mad r4, r0, c91, r4         // add (specular color * material specular)

				// light 3

				add r1, c[42], -r10	    // vertex to light direction
				dp3 r0.w, r1, r1
				rsq r1.w, r0.w

				dst r9, r0.wwww, r1.wwww    // (1, d, d*d, 1/d)
				dp3 r0.w, r9, c[72]	    // (a0 + a1*d + a2*d2)
				rcp r8.w, r0.w		    // 1 / (a0 + a1*d + a2*d)	

				mul r1, r1, r1.w	    // normalize the vertex to the light vector

				add r0, r6, r1		    // calculate half-vector (light vector + eye vector)

				dp3 r0.w, r0, r0    	    // normalize half-vector
				rsq r0.w, r0.w
				mul r0, r0, r0.w

				dp3 r2.x, r7, r1	    // N*L
				dp3 r2.yz, r7, r0 	    // N*H

				sge r3.x, c[82].y, r9.y	    // (range > d) ? 1:0
				mul r2.x, r2.x, r3.x
				mul r2.y, r2.y, r3.x
				
				lit r5, r2		    // calculate the diffuse & specular factors
				mul r5, r5, r8.w	    // scale by attenuation

				mul r0, r5.y, c[32]	    // calculate diffuse color (light diffuse * diffuse factor )
				mad r4, r0, c90, r4         // add (diffuse color * material diffuse)

				mul r0, r5.z, c[62]	    // calculate specular color (light specular * specular factor )
				mad r4, r0, c91, r4         // add (specular color * material specular)

				//
				// Finalize Lighting
				//

				mov  oD0, r4		    // final color
			};
	}
}

// End of file
