Original Post
[color="#0000FF"]
The title of this thread was a mistake. The below is the true error message.
I'm in the process of adding basic shadow maps to my shader and I get the below error.
Below is the function that causes the error. No matter what I put in the "if" statement, I'll get the above error. If I comment it out, every runs as normal.
//-----------------------------------------------------------------------------
// pixel shader to compair Z depth based on shadow map buffer to
// color or shade the pixel
//-----------------------------------------------------------------------------
float4 p_shaderShadowed( VS_OUTPUT IN ) : COLOR0
{
// Now, consult the ShadowMap to see if we're in shadow
float4 lightingPos = mul( IN.pos, lightWorldViewProjMatrix );
// Get the shadow map depth value for this pixel
float2 shadowTexC = 0.5 * lightingPos.xy / lightingPos.w + float2( 0.5, 0.5 );
shadowTexC.y = 1.0f - shadowTexC.y;
float shadowdepth = tex2D(shadowMapBufTextSampler, shadowTexC).r;
// Check our value against the depth value
// A fudge factor is added to account for floating-point error
float ourdepth = (1 - (lightingPos.z / lightingPos.w)) - 0.03;
// Check the shadowdepth against the depth of this pixel
if( shadowdepth > ourdepth )
{
//***** NO MATTER WHAT I PUT HERE, I'LL GET THE ERROR. NO ERROR IF THE BELOW IS COMMENTED OUT *****
//***** WHAT YOU CURRENTLY SEE HERE WILL RUN. THE BELOW SCREEN SHOT WAS TAKEN FROM THIS ******
//IN.color = ambient;
}
return tex2D( diffuseTextSampler, IN.uv0 ) * IN.color;
}
Below is all the code from the shader file. This problem seems so mysterious, I don't know what to think.
//-----------------------------------------------------------------------------
// #defines
//-----------------------------------------------------------------------------
// Make sure this matches in lightlst.h
#define MAX_LIGHTS 2 // 12 max for shader 2, more for shader 3
#define MAX_JOINTS 20
#define ELT_NONE 0
#define ELT_DIRECTIONAL 1
#define ELT_POINT_INFINITE 2
#define ELT_POINT_RADIUS 3
#define ELT_SPECULAR 4
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
// joint matrix
float4x4 jointMatrix[MAX_JOINTS];
// Lighting only matrix - rigid body transform
// This matrix has no scale in it
float4x4 worldMatrix;
// Pos only matrix
float4x4 worldViewProjMatrix;
// Projection matrix from the lights view point
float4x4 lightWorldViewProjMatrix;
// Light related globals
bool enableLights;
int lightType[MAX_LIGHTS];
float4 lightPos[MAX_LIGHTS];
float4 lightDif[MAX_LIGHTS];
float lightRadius[MAX_LIGHTS];
float4 ambient;
int lightCount;
// Textures - diffuse, normal, specular and displacement
// To Do: add normal, specular and displacement support
texture diffuseTexture;
// Texture - shadow map
texture shadowMapBufferTexture;
//-----------------------------------------------------------------------------
// Structor Definitions
//-----------------------------------------------------------------------------
struct VS_INPUT_RIGID_BODY
{
float4 pos : POSITION;
float3 normal : NORMAL;
float2 uv0 : TEXCOORD0;
};
struct VS_INPUT_JOINT
{
float4 pos : POSITION;
float weight : BLENDWEIGHT;
uint jIndex : BLENDINDICES;
float3 normal : NORMAL;
float2 uv0 : TEXCOORD0;
};
struct VS_INPUT_JOINT_SHADOW
{
float4 pos : POSITION;
float weight : BLENDWEIGHT;
uint jIndex : BLENDINDICES;
};
struct VS_OUTPUT
{
float4 pos : POSITION;
float2 uv0 : TEXCOORD0;
float4 color : COLOR0;
};
struct VS_OUTPUT_SHADOW
{
float4 pos : POSITION;
float depth : TEXCOORD0;
};
//-----------------------------------------------------------------------------
// Texture samplers
//-----------------------------------------------------------------------------
sampler diffuseTextSampler =
sampler_state
{
Texture = <diffuseTexture>;
};
sampler shadowMapBufTextSampler =
sampler_state
{
Texture = <shadowMapBufferTexture>;
};
//-----------------------------------------------------------------------------
// Point Radius light shader function
//-----------------------------------------------------------------------------
float4 CalcPointRadiusLight( int i, float4 vPos, float3 vNormal )
{
float4 color = float4( 0.f, 0.f, 0.f, 0.f );
// Light vector
float3 lVec = (float3)(lightPos - vPos);
// Get light distance
float dist = sqrt(length(lVec));
float3 dir = (float3)normalize( lVec );
float diffuse = max(0.0, dot(dir, vNormal));
if( diffuse > 0.0f )
{
if( dist < lightRadius )
{
color = float4( (float3)lightDif * diffuse, 1.0 );
}
else
{
// Use (lightRadius / dist) as a scaler against the diffuse
color = float4( (float3)(lightDif * diffuse) * (lightRadius / dist), 1.0 );
}
}
return color;
}
//-----------------------------------------------------------------------------
// Point Infinite light shader function
//-----------------------------------------------------------------------------
float4 CalcPointInfiniteLight( int i, float4 vPos, float3 vNormal )
{
float4 color = float4( 0.f, 0.f, 0.f, 0.f );
float3 dir = (float3)normalize( lightPos - vPos );
float diffuse = max(0.0, dot(dir, vNormal));
if( diffuse > 0.0f )
{
color = float4( (float3)lightDif * diffuse, 1.0 );
}
return color;
}
//-----------------------------------------------------------------------------
// Directional light shader function
//-----------------------------------------------------------------------------
float4 CalcDirLight( int i, float3 vNormal )
{
float4 color = float4( 0.f, 0.f, 0.f, 0.f );
float diffuse = max(0.0, dot(lightPos, vNormal));
if( diffuse > 0.0f )
{
color = float4( (float3)lightDif * diffuse, 1.0 );
}
return color;
}
//-----------------------------------------------------------------------------
// function to pick the type of light to use to set the color
//-----------------------------------------------------------------------------
float4 CalcLight( int i, float4 vPos, float3 vNormal )
{
float4 color = float4( 0.f, 0.f, 0.f, 0.0 );
if( lightType == ELT_POINT_INFINITE )
{
color = CalcPointInfiniteLight( i, vPos, vNormal );
}
else if( lightType == ELT_POINT_RADIUS )
{
color = CalcPointRadiusLight( i, vPos, vNormal );
}
else if( lightType == ELT_DIRECTIONAL )
{
color = CalcDirLight( i, vNormal );
}
return color;
}
//-----------------------------------------------------------------------------
// Vertex shader for ridig bodies
//-----------------------------------------------------------------------------
VS_OUTPUT v_shaderRigidBody( VS_INPUT_RIGID_BODY IN )
{
VS_OUTPUT OUT;
// Add the ambient
OUT.color = ambient;
if( enableLights )
{
float4 vPos = mul( IN.pos, worldMatrix );
float3 vNor = mul( IN.normal, (float3x3)worldMatrix );
for( int i = 0; i < lightCount; i++ )
{
OUT.color += CalcLight( i, vPos, vNor );
}
}
OUT.pos = mul( IN.pos, worldViewProjMatrix );
OUT.uv0 = IN.uv0;
OUT.color = min(1, OUT.color);
return OUT;
}
//-----------------------------------------------------------------------------
// Vertex shader for joint deformation
//-----------------------------------------------------------------------------
VS_OUTPUT v_shaderJoint( VS_INPUT_JOINT IN )
{
VS_OUTPUT OUT;
// Add the ambient
OUT.color = ambient;
// calculate the new position of the point and normal via deformation
float4 pos = mul( IN.pos, jointMatrix[IN.jIndex] );
float3 nor = mul( IN.normal, (float3x3)jointMatrix[IN.jIndex] );
if( enableLights )
{
float4 vPos = mul( pos, worldMatrix );
float3 vNor = mul( nor, (float3x3)worldMatrix );
for( int i = 0; i < lightCount; i++ )
{
OUT.color += CalcLight( i, vPos, vNor );
}
}
OUT.pos = mul( pos, worldViewProjMatrix );
OUT.uv0 = IN.uv0;
OUT.color = min(1, OUT.color);
return OUT;
}
//-----------------------------------------------------------------------------
// Vertex shader for rendering ridig bodies to the shadow map buffer
//-----------------------------------------------------------------------------
VS_OUTPUT_SHADOW v_shaderRigidBodyShadow( float4 pos : POSITION )
{
VS_OUTPUT_SHADOW OUT;
// Transform to the light's position
OUT.pos = mul( pos, lightWorldViewProjMatrix );
// Depth is Z/W. This is returned by the pixel shader.
// Subtracting from 1 gives us more precision in floating point.
OUT.depth = 1-(OUT.pos.z / OUT.pos.w);
return OUT;
}
//-----------------------------------------------------------------------------
// Vertex shader for rendering joint deformation to the shadow map buffer
//-----------------------------------------------------------------------------
VS_OUTPUT_SHADOW v_shaderJointShadow( VS_INPUT_JOINT_SHADOW IN )
{
VS_OUTPUT_SHADOW OUT;
// calculate the new position of the point via deformation
float4 pos = mul( IN.pos, jointMatrix[IN.jIndex] );
OUT.pos = mul( pos, lightWorldViewProjMatrix );
// Depth is Z/W. This is returned by the pixel shader.
// Subtracting from 1 gives us more precision in floating point.
OUT.depth = 1-(OUT.pos.z / OUT.pos.w);
return OUT;
}
//-----------------------------------------------------------------------------
// pixel shader function for rendering the lit pixel
//-----------------------------------------------------------------------------
float4 p_shader( VS_OUTPUT IN ) : COLOR0
{
return tex2D( diffuseTextSampler, IN.uv0 ) * IN.color;
}
//-----------------------------------------------------------------------------
// pixel shader to compair Z depth based on shadow map buffer to
// color or shade the pixel
//-----------------------------------------------------------------------------
float4 p_shaderShadowed( VS_OUTPUT IN ) : COLOR0
{
// Now, consult the ShadowMap to see if we're in shadow
float4 lightingPos = mul( IN.pos, lightWorldViewProjMatrix );
// Get the shadow map depth value for this pixel
float2 shadowTexC = 0.5 * lightingPos.xy / lightingPos.w + float2( 0.5, 0.5 );
shadowTexC.y = 1.0f - shadowTexC.y;
float shadowdepth = tex2D(shadowMapBufTextSampler, shadowTexC).r;
// Check our value against the depth value
// A fudge factor is added to account for floating-point error
float ourdepth = (1 - (lightingPos.z / lightingPos.w)) - 0.03;
// Check the shadowdepth against the depth of this pixel
if( shadowdepth > ourdepth )
{
IN.color = ambient;
}
return tex2D( diffuseTextSampler, IN.uv0 ) * IN.color;
}
//-----------------------------------------------------------------------------
// Pixel shader for rendering to the shadow map buffer
//-----------------------------------------------------------------------------
float4 p_shader_Shadow( VS_OUTPUT_SHADOW IN ) : COLOR0
{
// Output the scene depth
return float4( IN.depth, IN.depth, IN.depth, 1.0f );
}
//-----------------------------------------------------------------------------
// Effects
//-----------------------------------------------------------------------------
technique rigidBodyLightTransform
{
pass Pass0
{
VertexShader = compile vs_3_0 v_shaderRigidBody();
PixelShader = compile ps_3_0 p_shader();
}
}
technique rigidBodyLightTransformWithShadow
{
pass Pass0
{
VertexShader = compile vs_3_0 v_shaderRigidBody();
PixelShader = compile ps_3_0 p_shaderShadowed();
}
}
technique jointBodyLightTransform
{
pass Pass0
{
VertexShader = compile vs_3_0 v_shaderJoint();
PixelShader = compile ps_3_0 p_shader();
}
}
technique jointBodyLightTransformWithShadow
{
pass Pass0
{
VertexShader = compile vs_3_0 v_shaderJoint();
PixelShader = compile ps_3_0 p_shaderShadowed();
}
}
technique rigidBodyShadowTransform
{
pass Pass0
{
VertexShader = compile vs_3_0 v_shaderRigidBodyShadow();
PixelShader = compile ps_3_0 p_shader_Shadow();
}
}
technique jointBodyShadowTransform
{
pass Pass0
{
VertexShader = compile vs_3_0 v_shaderJointShadow();
PixelShader = compile ps_3_0 p_shader_Shadow();
}
}
This is what the scene currently looks like. The test light (doesn't light the scene at this time) is right above the models so the models should project a shadow on the ground right below them.
Any help would be greatly appreciated.

