Jump to content
  • Advertisement
Sign in to follow this  

Newbie HLSL Help

This topic is 5082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I just hacked together a my first shader in HLSL. It ran perfect on my Geforce 6800, then I ran on a lab computer w/ a Quadro FX1000 and it is brought to a halt. I tried elimanting the # of the light sources and there are noticable performance hits on every setting except 1 light. Im trying to do simple per-pixel diffuse lighting. Im only trying to apply this shader to my terrain, which is pretty small (50x50 vertices) Im totally new to shaders so Im sure Im doing something terribly wrong. I know the distance(...) call in there is pretty bad but even when I replace it with a constant it still runs slow (and doesnt render properly obviously) I just want to get per-pixel diffuse lighting for as many light sources as possible. Im sure there are better ways to do it. Can anyone please help? Thank you Ratman
// -------------------------------------------------------------
// variables that are provided by the application
// -------------------------------------------------------------
float4x4 matWorldViewProj;	
float4x4 matWorld;	

float4  vSpecIC;

float4  vDIC1;
float4  vLightPos1;
float   vLightRange1;

float4  vDIC2;
float4  vLightPos2;
float   vLightRange2;

float4  vDIC3;
float4  vSpecIC3;
float4  vLightPos3;
float   vLightRange3;

float4  vDIC4;
float4  vSpecIC4;
float4  vLightPos4;
float   vLightRange4;

float4  vDIC5;
float4  vLightPos5;
float   vLightRange5;

float4  vecEye;
texture ColorMap;

sampler ColorMapSampler = sampler_state
   Texture = <ColorMap>;
   MinFilter = Linear;
   MagFilter = Linear;
   MipFilter = Linear;      
// -------------------------------------------------------------
// Output channels
// -------------------------------------------------------------
struct VS_OUTPUT
    float4 Pos  : POSITION;     
    float2 TextUV: TEXCOORD0;   
    float4 Light: TEXCOORD1;
    float3 Norm : TEXCOORD2;  
    float4 Light2: TEXCOORD3;               
    float4 Light3: TEXCOORD4;
    float4 Light4: TEXCOORD5;
    float4 Light5: TEXCOORD6;

// -------------------------------------------------------------
// vertex shader function (input channels)
// -------------------------------------------------------------
VS_OUTPUT VS(float4 Pos : POSITION, float2 Tex: TEXCOORD0, float3 Normal : NORMAL, float3 Tangent : TANGENT)
    VS_OUTPUT Out = (VS_OUTPUT)0;              
    Out.TextUV = Tex;
     // compute the 3x3 tranform matrix 
    // to transform from world space to tangent space       
    // transform Position
    Out.Pos = mul(Pos, matWorldViewProj);	
    // transform Normal
    Out.Norm =  mul(Normal, matWorld);				
    float3 PosWorld = mul(Pos, matWorld);    
    float  range			= vLightRange1;
    float3 light		    = vLightPos1 - (PosWorld);	    
    //Light 1 Stuff
    float d = distance(vLightPos1.xyz, Pos.xyz);
    if(d > range) d = range;
    Out.Light = vLightPos1 - Pos;
    Out.Light.w = 1 - d/range;        
    if(range == 0) Out.Light.w = 0;       
    //Lgith 2 stuff    
    d     = distance(vLightPos2.xyz, Pos.xyz);
    range = vLightRange2;
    if(d > range) d = range;
    Out.Light2  = vLightPos2 - Pos;
    Out.Light2.w = 1 - d/range;
    if(range == 0) Out.Light2.w = 0;   
    //Light 3
    d     = distance(vLightPos3, Pos);
    range = vLightRange3;
    if(d > range) d = range;
    Out.Light3  = vLightPos3 - Pos;
    Out.Light3.w = 1 - d/range;
    if(range == 0) Out.Light3.w = 0;
     //Light 4
    d     = distance(vLightPos4, Pos);
    range = vLightRange4;
    if(d > range) d = range;
    Out.Light4  = vLightPos4 - Pos;
    Out.Light4.w = 1 - d/range;
    if(range == 0) Out.Light4.w = 0;        
    //Light 5
    d     = distance(vLightPos5, Pos);
    range = vLightRange5;
    if(d > range) d = range;
    Out.Light5  = vLightPos5 - Pos;
    Out.Light5.w = 1 - d/range;
    if(range == 0) Out.Light5.w = 0;         
    return Out;

// -------------------------------------------------------------
// Pixel Shader (input channels):output channel
// -------------------------------------------------------------
float4 PS(float2 TextUV: TEXCOORD0, float4 Light:TEXCOORD1, float3 Norm : TEXCOORD2, float4 Light2:TEXCOORD3, float4 Light3: TEXCOORD4, float4 Light4:TEXCOORD5,
           float4 Light5:TEXCOORD6) : COLOR
    float4 Acolor = tex2D(ColorMapSampler, TextUV);
    float3 Normal	= normalize(Norm);
    float3 LightDir = normalize(Light);        
    float  Diff = saturate(dot(Normal, LightDir));  
    float3 LightDir2 = normalize(Light2);        
    float  Diff2 = saturate(dot(Normal, LightDir2));         
    float3 LightDir3 = normalize(Light3);        
    float  Diff3 = saturate(dot(Normal, LightDir3));     
    float3 LightDir4 = normalize(Light4);        
    float  Diff4 = saturate(dot(Normal, LightDir4)); 
    float3 LightDir5 = normalize(Light5);        
    float  Diff5 = saturate(dot(Normal, LightDir5));        
    return  Acolor * ((.75) + (vDIC1 * Diff * Light.w) + (vDIC2 * Diff2 * Light2.w) + (vDIC3 * Diff3 * Light3.w) + (vDIC4 * Diff4 * Light4.w) + (vDIC5 * Diff5 * Light5.w)); 

// -------------------------------------------------------------
// -------------------------------------------------------------
technique TShader
    pass P0
		Sampler[0] = (ColorMapSampler);		
        // compile shaders
        VertexShader = compile vs_1_1 VS();
        PixelShader  = compile ps_2_0 PS();

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
dont quote me on this, but maybe doing multiple passes, each pass calculating only 2 lights?

i think it may be the sheer volume of pixel and vertex shader instructions that maybe causing the slow down.

also, 6800 has branch prediction for the if statements, AFAIK, no other cards have this, so remove all if statements that arnt absolutely necessary.

another thing, you have 1/range, what if range is zero? the hardware will have to deal with a division by zero exception wont it (you might not see it, but it mite be internal or something) ?


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!