Original Post
My current task is to get some basic shaders written for our engine. This is the first time I've been working on shaders; I have a general understanding of how they work, setup, etc... but I've been hitting some specific problems. Basic setup: DX9 engine, using HLSL (but not Effects), compiling primarily for vs_2_0 and ps_2_0, though may use that and 3.0 going ahead. My current problem is that of per-pixel lighting and normal interpolation. (May be changed in the future for normal maps, but need to get the basics done first.) Here is the code:
float4x4 g_Model;
float4x4 g_ModelViewProj;
struct VS_INPUT
{
float4 localPos : POSITION;
float3 localNorm : NORMAL;
float2 uv : TEXCOORD0;
float4 diffuse : COLOR0;
};
struct VS_OUTPUT
{
float4 projPos : POSITION;
float4 diffuse : COLOR0;
float2 uv : TEXCOORD0;
float3 worldNorm : TEXCOORD1;
};
VS_OUTPUT vshader(VS_INPUT I)
{
VS_OUTPUT O;
O.projPos = mul(I.localPos, g_ModelViewProj);
O.worldNorm = mul(I.localNorm, (float3x3) g_Model);
O.diffuse = I.diffuse;
O.uv = I.uv;
return O;
}
float4 pshader(VS_OUTPUT I): COLOR
{
// just a sample light direction (towards light) for testing
float3 light = normalize(float3(1.0, -0.2, 0.5));
return I.diffuse * saturate(dot(light, normalize(I.worldNorm)));
}
This is dead-simple per-pixel lighting. But it doesn't work, unless I change VS_OUTPUT to this:
struct VS_OUTPUT
{
float4 projPos : POSITION;
float4 diffuse : COLOR0;
float2 uv : TEXCOORD1;
float3 worldNorm : TEXCOORD0;
};
The only difference is that I swapped the texcoord indices for uv and worldNorm. Then my per-pixel lighting and normals look fine. Likewise, I had some basic code working to sample a texture with the uv. That stopped working when I output the uv's to TEXCOORD1 instead of TEXCOORD0. So offhand, this doesn't seem like a shader issue, but more of a render/texture/sampler state issue. But I have no idea at this point what state is preventing TEXCOORD1 (or higher?) from being useful. Please, any hints, suggestions? Thanks...