i have o problem with my shader code (HLSL). I use "DirectX for Managed Code" and Shader Model 3.0. I try to write a custom depth value into the depth buffer by using the DEPTH semantic in the pixel shader output struct:
struct PSOutput
{
float4 col : COLOR0;
float dept : DEPTH;
};
and i use this struct as return value in my pixel shader:
PSOutput PSFunction(VertexShaderOutput input)
{
PSOutput output;
...
output.col = float4(...);
output.dept = ...;
return output;
}
DirectX throws an exeption when i try to compile this shader, but gives no detailed information why. But when i remove the depth variable from the output struct it works! I also tried to write DEPTH0 as semantic, but no success. I hope anyone can help my with that.
EDIT:
If i write the following, it fails:
PSOutput PSFunction(VertexShaderOutput input)
{
PSOutput output;
float resDepth = input.Position[2] / input.Position[3];
if(...)
{
resDepth = ...;
}
output.col = float4(...);
output.dept = resDepth;
return output;
}
but if i write this code, it compiles:
PSOutput PSFunction(VertexShaderOutput input)
{
PSOutput output;
float resDepth = input.Position[2] / input.Position[3];
if(...)
{
resDepth = ...;
}
output.col = float4(...);
output.dept = 0.5;
return output;
}
any ideas?
Here's the full code:
float4x4 World;
float4x4 View;
float4x4 Projection;
float4 CamPos;
float4 LightDir;
float4 ObjColor;
static const float PI = 3.14159265f;
static const int MAX_FU = 32;
float fuPercent[MAX_FU];
float4 fuColor[MAX_FU];
int buildDir;
static int fuCount = 2;
float4 boxMin;
float4 boxMax;
struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Normal : NORMAL;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float3 Normal : NORMAL;
float3 ExactPos : TEXCOORD1;
};
struct PSOutput
{
float4 col : COLOR0;
//float dept : DEPTH;
};
VertexShaderOutput VSFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.Normal = mul(input.Normal, World);
output.ExactPos = input.Position;
return output;
}
PSOutput PSFunction(VertexShaderOutput input)
{
PSOutput output;
float4 resColor = ObjColor;
float resDepth = input.Position[2] / input.Position[3];
float prpos = 0;
if (buildDir == 0)
{
prpos = (input.ExactPos[1] - boxMin[1]) / (boxMax[1] - boxMin[1]);
}
else if (buildDir == 1)
{
prpos = 1.0 - ((input.ExactPos[1] - boxMin[1]) / (boxMax[1] - boxMin[1]));
}
else if (buildDir == 2)
{
prpos = (input.ExactPos[2] - boxMin[2]) / (boxMax[2] - boxMin[2]);
}
else if (buildDir == 3)
{
prpos = 1.0 - ((input.ExactPos[2] - boxMin[2]) / (boxMax[1] - boxMin[2]));
}
else if (buildDir == 4)
{
prpos = (input.ExactPos[0] - boxMin[0]) / (boxMax[0] - boxMin[0]);
}
else if (buildDir == 5)
{
prpos = 1.0 - ((input.ExactPos[0] - boxMin[0]) / (boxMax[0] - boxMin[0]));
}
float currPerc = 1.1;
for (int i = 0; i < fuCount; i++)
{
if (prpos - 0.0001 <= fuPercent[i])
{
if (fuPercent[i] < currPerc)
{
currPerc = fuPercent[i];
resColor = fuColor[i];
}
}
else
{
resDepth = 1.0;
resColor[3] = 0.0;
}
}
float3 nor = input.Normal;
float3 pos = input.ExactPos;
float glo = 0.5;
float id = (acos(dot(LightDir,nor) / pow(dot(LightDir,LightDir) * dot(nor, nor), 0.5)) / PI );
id = pow(id,2);
float3 look = reflect(normalize(pos - CamPos), nor);
float gl = (acos(dot(LightDir,look) / pow(dot(LightDir,LightDir) * dot(look, look), 0.5)) / PI );
gl = max(gl * 10.0 - 9.0, 0.0);
gl = pow(gl,2) * glo;
output.col = float4(resColor[0] * id + gl, resColor[1] * id + gl, resColor[2] * id + gl, resColor[3]);
//output.dept = resDepth;
return output;
}
technique MyTechnique
{
pass Pass1
{
VertexShader = compile vs_3_0 VSFunction();
PixelShader = compile ps_3_0 PSFunction();
}
}