   HLSL multiple lights
  Hello,I am new to HLSL (a beginner in 3D) and I am trying to create a shader that will handle x number of lights.  I have seen numerous examples of creating lights in a shader, but for the life of me I can't figure out how to render numerous lights.Can anyone show me a code snippet or even just a simple shader that handles this?Thanks!   
  With multiple lights, all you have to do is add together the contribution from each light.  You can do this directly in the shader by calculating the contribution for multiple lights and then adding, or you can do it in multiple passes using additive blending (SRCBLEND = ONE, DESTBLEND = ONE).  You can also do a combination of both.It's pretty simple to do it in the shader...for instance let's say your single light pixel shader looks something like this:float3 diffuse = CalcDiffuse(lightColor, lightDirection, normal) * diffuseAlbedo;float3 specular = CalcSpecular(lightColor, lightDirection, cameraPos, normal, specularExponent) * specularAlbedo;return float4(diffuse + specular, 1.0f);For multiple lights, you might do it like this:float3 diffuse = 0;float3 specular = 0;for (int i = 0; i <= NUM_LIGHTS; i++){    diffuse += CalcDiffuse(lightColors, lightDirections, normal) * diffuseAlbedo;    specular += CalcSpecular(lightColors, lightDirections, cameraPos, normal, specularExponent) * specularAlbedo;}return float4(diffuse + specular, 1.0f);;  Thanks for the reply MJP, that makes a lot of sense.  The only problem is that I'm having trouble applying it.  I have a shadow map shader and i'm having trouble getting multiple lights to show up.  Is it possible for you to show me what I'm doing wrong, or what I need to do to get multiple lights?  Thanks again for your help, it is very much appreciated!//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>// Variables.//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   float4x4 World  : WORLD;   float4x4 wViewProj   : WORLDVIEWPROJECTION;   float4   ViewPos: VIEWPOS;   float3   LightPos1;   float2   LightPos2;   float4   LightColour = float4(1, 0, 0, 1);    float  pStrength= 0.04;   float  uvScale  = 1;   float  TexScale = 0.0001;   float  FallOff  = 100;   float  SubFactor; float  SpecMP   = 5; #define NUM_SAMPLES 8   #define BIAS   0.98 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>// Textures and Samplers.//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> texture DiffMap : TEXTURE0;   texture NormMap : TEXTURE1;   texture SpecMap : TEXTURE2;   texture DeepMap; sampler2D DiffSample = sampler_state   { Texture = (DiffMap);MIPFILTER = LINEAR;MAGFILTER = LINEAR;MINFILTER = LINEAR;   }; sampler2D NormSample = sampler_state   { Texture = (NormMap);MIPFILTER = LINEAR;MAGFILTER = LINEAR;MINFILTER = LINEAR;   }; sampler2D SpecSample = sampler_state   { Texture = (SpecMap);MIPFILTER = LINEAR;MAGFILTER = LINEAR;MINFILTER = LINEAR;   }; samplerCUBE DeepSample = sampler_state   { Texture = (DeepMap);   };   //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>// Structures.//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> struct a2vp   { float4 Position: POSITION0; float3 Tangent : TANGENT; float3 Normal  : NORMAL; float3 BiNormal: BINORMAL; float2 UV : TEXCOORD0;   }; struct vp2fp   { float4 Position: POSITION0; float3 LightVec: TEXCOORD0; // float2 UV : TEXCOORD1; // float3 ViewVec : TEXCOORD2; // float3 SmPos   : TEXCOORD3; // float  Deep    : TEXCOORD4; //   };   struct fpin   { float3 LightVec: TEXCOORD0; // float2 UV : TEXCOORD1; // float3 ViewVec : TEXCOORD2; // float3 SmPos   : TEXCOORD3; // float  Deep    : TEXCOORD4; //   }; struct fp2a   { float4 Colour: COLOR0;   };   //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>// Functions.//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   void TBN(float2 UV, float4 Pos, float3 Tang, float3 Norm, float3 BiNorm, out float4 rPos, out float3 rLightVec, out float3 rViewVec, out float2 rUV)   { rPos  = mul(Pos, wViewProj); rUV   = UV * uvScale;  float3 PosWorld = mul(Pos, World);  float3x3 TBN    = float3x3(Tang, BiNorm, Norm); float3x3 WTTS   = mul(TBN, (float4x3)World);  rViewVec   = mul(WTTS, ViewPos - PosWorld); rLightVec  = mul(WTTS, (LightPos1 - PosWorld));   }   void Bumping(float3 ViewVec, float2 UV, out float3 rViewVec, out float2 rUV)   { rViewVec   = normalize(ViewVec); rUV   = (tex2D(NormSample, UV).a * pStrength - (pStrength * 0.5)) * rViewVec.xy + UV;   } float CalculateLyon(float3 VV, float3 LV, float3 NV)   { float3 HalfWay= normalize(VV + LV); float3 Difference  = HalfWay - NV; float  SS= saturate(dot(Difference, Difference) * 60); return pow(1 - SS, 3);   } void RigLighting(float3 LightVec, float3 ViewVec, float2 UV, out float4 rColour)   { float4 Col = tex2D(DiffSample, UV); float3 Norm= 2 * tex2D(NormSample, UV) - 1; float  Spec= tex2D(SpecSample, UV).r; float3 Light    = normalize(LightVec); //Blinds attenuation. Calc your own. float LenSq= dot(Light, Light); float Attn = min((FallOff * FallOff) / LenSq, 1);//End Blinds attenuation.float3newVect;newVect.x = LightVec.x + 3;newVect.y = LightVec.y;newVect.z = LightVec.z; float Diffuse   = saturate(dot(Light, Norm));// * Attn; Diffuse   += 0.15; float Specular  = CalculateLyon(ViewVec, Light, Norm);// float Specular  = (pow(saturate(dot(reflect(ViewVec, Norm), Light)), 16));  rColour    = ((Diffuse * Col) + (Specular * (Spec * SpecMP))) * LightColour;// * Attn;// rColour    = (Diffuse * Col) * LightColour * Attn; rColour.a  = Col.a;   } void SMapping(float3 sPos, float Deep, samplerCUBE sSampler, out float4 rsFactor)   { float3 Dir = normalize(LightPos1.xyz - sPos); float3 nDir= -Dir; // TexScale   = TexScale * Deep;// TexScale = 0.01;// TexScale   = clamp(TexScale, 0, 0.002);    float  Depth    = texCUBE(sSampler, float4(nDir, 1)).r;  float4 sFactor  = float4(1, 1, 1, 1);  for(int i = 0; i < NUM_SAMPLES; i++) {    float Depth0 = texCUBE(sSampler, float4(nDir, 1) + TexScale * float4(2 + i, -1 - i, 1 + i, 0)).r;    float Depth1 = texCUBE(sSampler, float4(nDir, 1) + TexScale * float4(1 + i, 2 + i, -1 - i, 0)).r;    float Depth2 = texCUBE(sSampler, float4(nDir, 1) + TexScale * float4(-1 - i, 1 + i, 2 + i, 0)).r;   if(Deep > Depth0) sFactor.rgb -= SubFactor;    if(Deep > Depth1) sFactor.rgb -= SubFactor;    if(Deep > Depth2) sFactor.rgb -= SubFactor; }  if(Deep > Depth) sFactor.rgb  -= SubFactor;  rsFactor   = saturate(sFactor);   } void VSMapping(float4 Pos, out float3 rsPos, out float rDeep)   { rsPos = mul(Pos, World).xyz; rDeep = length(rsPos - LightPos1.xyz) * BIAS;   } void Composite(float4 sFactor, float4 Colour, out float4 rColour)   { rColour    = sFactor * Colour;   }   //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>// Programs.//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   void vp(in a2vp IN, out vp2fp OUT)   { TBN(IN.UV, IN.Position, IN.Tangent, IN.Normal, IN.BiNormal, OUT.Position, OUT.LightVec, OUT.ViewVec, OUT.UV); VSMapping(IN.Position, OUT.SmPos, OUT.Deep); // OUT.UV = IN.UV;   } void fp(in fpin IN, out fp2a OUT)   { float3 VV_P; float2 UV_P; float4 SF_SM; float4 CR_RL; float4 ER_CC;  Bumping(IN.ViewVec, IN.UV, VV_P, UV_P); SMapping(IN.SmPos, IN.Deep, DeepSample, SF_SM); RigLighting(IN.LightVec, VV_P, UV_P, CR_RL); Composite(SF_SM, CR_RL, ER_CC);  OUT.Colour = ER_CC;   }//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>// Techniques.//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   technique object_shadows   { pass pass0 {    vertexshader = compile vs_3_0 vp();    pixelshader  = compile ps_3_0 fp(); }   };  Hello,It's been a little bit of time and after much testing, I have still been unable to add multiple lights to the example shader I posted.  Can anyone assist in getting this feature added to the shader?  Any direction or help would be wonderful.Thank you!   