RESOLVED: Constant buffers were set for VS and not for PS
I am currently having issues with HLSL shaders. I am trying to implement point lights but I am getting unusual data when debugging the shader in Visual Studio.
I have read on several sites that say that the data stored in CBuffers should be aligned to 4 bytes, thus I added padding where it was needed (is this correct?).
When I set the data in c++, some of it comes through alright but other data is not. For example the color comes through alright but the range is being displayed as NaN or just zeros in the VS graphics debugger.
Is there something wrong with the code below? (All structs are the same as those in c++ structs)
Please ask if there is anything else you would like to see code wise. Thanks!
All the code is stored in a file called buffers.hlsl which is included by the separate shaders i.e. poingLight.vs/.ps include it and make a call to calcPointLight passing in there normal calculations etc...
Lighting Shader:
//Structs
struct BaseLight
{
float4 color;
float intensity;
float3 padding3; //TEST
};
struct DirectionalLight
{
BaseLight base;
float3 direction;
};
struct Attenuation
{
float constantAtten;
float linearAtten;
float exponentAtten;
float padding;
};
struct PointLight
{
BaseLight base;
float3 atten;
float3 pos;
float range;
};
struct SpotLight
{
PointLight pointLight;
float3 direction;
float cutoff;
float padding;
};
cbuffer MatrixBuffer : register(b0)
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};
cbuffer LightingBuffer : register(b1)
{
float4 ambient;
float specularPower;
float3 padding3;
PointLight pointLight;
//DirectionalLight light;
};
cbuffer CameraBuffer : register(b2)
{
float4 cameraPos;
};
struct VertexInputType
{
float4 position : POSITION;
float4 color : COLOR;
float2 texCoords : TEXCOORD0;
float3 normal : NORMAL;
float3 tangent : TANGENT;
float3 biTangent : BINORMAL;
};
struct PixelInputType
{
float4 position : SV_POSITION;
float4 color : COLOR;
float3 normal : NORMAL;
float2 texCoords : TEXCOORD0;
float3 worldPos : TEXCOORD1;
float3 tangent : TANGENT;
float3 biTangent : BINORMAL;
};
//Methods
float4 calcLight(BaseLight base, float3 direction, float3 normal, float3 worldPos0, float4 specularMap)
{
float diffuseFactor = (dot(normal, -direction));
float4 diffuseColor = float4(0, 0, 0, 0);
float4 specularColor = float4(0, 0, 0, 0);
if (diffuseFactor > 0.0f)
{
diffuseColor = base.color * base.intensity * diffuseFactor;
diffuseColor = saturate(diffuseColor);
float3 directionToEye = normalize(cameraPos - worldPos0);
float3 reflectDirection = normalize(reflect(direction, normal));
float specularFactor = dot(directionToEye, reflectDirection);
specularFactor = pow(specularFactor, specularPower);
if (specularFactor > 0)
{
#if 0
specularColor = float4(1, 0, 0, 1) * specularFactor; //Debug Specular
#else
specularColor = base.color * 1.0f * specularFactor; //Normal Specular
#endif
}
if (specularMap.a > 0.0f)
specularColor = specularColor * specularMap;
}
return ambient + diffuseColor + specularColor;
}
float4 calcDirectionalLight(DirectionalLight directionalLight, float3 normal, float3 worldPos0, float4 specularMap)
{
return calcLight(directionalLight.base, -directionalLight.direction, normal, worldPos0, specularMap);
}
float4 calcPointLight(PointLight pointLight, float3 normal, float3 worldPos0, float4 specularMap, float4 diffuseTex)
{
float3 lightDirection = worldPos0 - pointLight.pos;
float distanceToPoint = length(lightDirection);
// if (distanceToPoint > pointLight.range)
// return float4(0, 0, 0, 0);
lightDirection = normalize(lightDirection);
float4 color = calcLight(pointLight.base, lightDirection, normal, worldPos0, specularMap);
float attenuation = pointLight.atten[0] +
pointLight.atten[1] * distanceToPoint +
pointLight.atten[2] * distanceToPoint * distanceToPoint +
0.0001;
return color / attenuation;
}
The main problem that I have found is caused by the specular calculations in calcLight. The specular seems to be having some kind of odd effect for example in my current build it leaves like a "tail" / "trail" on the point light which just plain looks wrong.
When I inspect it via the VS debugger the cameraPos is showing as float4(0,0,0, Nan) which is clearly incorrect.