HLSL Phong Bump Effect

Started by
3 comments, last by LudoDesign 12 years, 10 months ago
Hello,

I am looking to make a Phong Bump Effect in FX File. I work with Managed DirectX 9 and NVIDIA FX Composer 2.5 to help me with the HLSL.
I try to run the basic template given by NVIDIA on FX Composer. After simplifying it a little bit I reach to this version which, as the original one, fail to compile when I load the FX File in my program. :unsure:
I load the file with Microsoft.DirectX.Direct3D.Effect.FromFile() method.

Do you know what is wrong with this FX File ?

//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////

float4x4 WorldITXf;
float4x4 WvpXf;
float4x4 WorldXf;
float4x4 ViewIXf;

//// TWEAKABLE PARAMETERS ////////////////////

/// Point Lamp 0 ////////////
float3 Lamp0Pos = {-0.5f,2.0f,1.25f};
float3 Lamp0Color = {1.0f,1.0f,1.0f};

// Ambient Light
float3 AmbiColor = {0.07f,0.07f,0.07f};

float Ks = 0.4;

float SpecExpon = 55.0;


float Bump = 1.0;

//float Kr = 0.5;

//////// COLOR & TEXTURE /////////////////////

texture ColorTexture <
string ResourceName = "default_color.dds";
string ResourceType = "2D";
>;

sampler2D ColorSampler = sampler_state {
Texture = ColorTexture;
FILTER = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};

texture NormalTexture <
string ResourceName = "default_bump_normal.dds";
string ResourceType = "2D";
>;

sampler2D NormalSampler = sampler_state {
Texture = NormalTexture;
FILTER = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};


//////// CONNECTOR DATA STRUCTURES ///////////

/* data from application vertex buffer */
struct VS_INPUT {
float3 Position : POSITION;
float4 UV : TEXCOORD0;
float4 Normal : NORMAL;
float4 Tangent : TANGENT0;
float4 Binormal : BINORMAL0;
};

/* data passed from vertex shader to pixel shader */
struct VS_OUTPUT {
float4 HPosition : POSITION;
float2 UV : TEXCOORD0;
float3 LightVec : TEXCOORD1;
float3 WorldNormal : TEXCOORD2;
float3 WorldTangent : TEXCOORD3;
float3 WorldBinormal : TEXCOORD4;
float3 WorldView : TEXCOORD5;
};

///////// VERTEX SHADING /////////////////////

/*********** Generic Vertex Shader ******/

VS_OUTPUT std_VS(VS_INPUT IN) {
VS_OUTPUT OUT;
OUT.WorldNormal = mul(IN.Normal,WorldITXf).xyz;
OUT.WorldTangent = mul(IN.Tangent,WorldITXf).xyz;
OUT.WorldBinormal = mul(IN.Binormal,WorldITXf).xyz;
float4 Po = float4(IN.Position.xyz,1);
float3 Pw = mul(Po,WorldXf).xyz;
OUT.LightVec = (Lamp0Pos - Pw);
#ifdef FLIP_TEXTURE_Y
OUT.UV = float2(IN.UV.x,(1.0-IN.UV.y));
#else /* !FLIP_TEXTURE_Y */
OUT.UV = IN.UV.xy;
#endif /* !FLIP_TEXTURE_Y */
OUT.WorldView = normalize(ViewIXf[3].xyz - Pw);
OUT.HPosition = mul(Po,WvpXf);
return OUT;
}

///////// PIXEL SHADING //////////////////////

struct PS_OUTPUT {
float4 Color : COLOR;
};


PS_OUTPUT std_PS(VS_OUTPUT IN) : COLOR {
PS_OUTPUT OUT;
float3 diffContrib;
float3 specContrib;
float3 Ln = normalize(IN.LightVec);
float3 Vn = normalize(IN.WorldView);
float3 Nn = normalize(IN.WorldNormal);
float3 Tn = normalize(IN.WorldTangent);
float3 Bn = normalize(IN.WorldBinormal);
float3 bump = Bump * (tex2D(NormalSampler,IN.UV).rgb - float3(0.5,0.5,0.5));
Nn = Nn + bump.x*Tn + bump.y*Bn;
Nn = normalize(Nn);

float3 Hn = normalize(Vn + Ln);
float4 litV = lit(dot(Ln,Nn),dot(Hn,Nn),SpecExpon);
diffContrib = litV.y * Lamp0Color;
specContrib = litV.y * litV.z * Ks * Lamp0Color;
float3 diffuseColor = tex2D(ColorSampler,IN.UV).rgb;
float3 combineColor = specContrib+(diffuseColor*(diffContrib+AmbiColor));
OUT.Color = float4(combineColor.x, combineColor.y,combineColor.z,1.0f);
return OUT;
}

///// TECHNIQUES /////////////////////////////

technique Main {
pass p0
{
VertexShader = compile vs_2_0 std_VS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
AlphaBlendEnable = false;
CullMode = None;
PixelShader = compile ps_2_0 std_PS();
}
}


Must be some sort of syntax or general form of the file. :blink: I try several changes in the PixelShader version 2_0, 2_a, 2_b... but nothing works on this side.
FX Composer debugger point out error on the return OUT; of the PixelShader but I do not see the problem.

Many thanks for your help !
Advertisement
Well, out is a special word in HLSL so I personaly wouldn't use it as a variable name. BUT - that's not the problem as HLSL is case sensitive and so OUT in capital letters is fine.
Also, you don't need structure as a pixel shader output value, just a float4 would be fine and easier to work with. Your structure PS_OUTPUT contains just a single float4 anyway. But also this shouldn't be the problem.

Can you please tell us the error message you're getting?
I used fxc to compile the effect:

fxc /Tfx_2_0 /Od file.fx
Where file.fx contains the code you posted.

The shader code compiles fine except for the fact that you use redundant COLOR semantic on your pixel shader function. The PS_OUTPUT structure already contains a field that has the aforementioned semantic.

The compiler issues a warning X3576 for this.

Niko Suni

[font="Arial"][color="#000000"]
Hello Nik02 and Tom KQT !
Many thanks for your replies. :lol:

I post to you the screenshot of FX Composer which stop during the debugging.
[/font]http://postimage.org/image/1005tszwk/

I do not know so much more about that problem. Apparently it is not a problem in my program that crash for effect loading because other effects works just fine.
Like the following shader that create fog on a part of the area : (the two shaders are not in interaction my scene is simple and I only select one effect at a time)

float4x4 matWorldViewProj;
float4x4 matWorld;
float4x4 matWorldIT;
float4 AmbientColor;
float4 PlayerPos;
float AmbientIntensity = 0.6;
float StartFog = 112;
float4 ColorFog = {0.96,0.96,0.96,1};
float3 LightDirection = {1,1,1};

struct VS_INPUT
{
float4 Position : POSITION;
float3 Normal : NORMAL;
};

struct VS_OUTPUT
{
float4 Position: POSITION;
float3 Light : TEXCOORD0;
float3 Normal : TEXCOORD1;
float Fog: FOG;
};

VS_OUTPUT vs_main(VS_INPUT In)
{
VS_OUTPUT Out;
Out.Position = mul(In.Position, matWorldViewProj);
Out.Light = normalize(LightDirection);
Out.Normal = normalize(mul(matWorld,In.Normal));
float4 PlayerPosWorld = mul(PlayerPos, matWorld);
float DistFog = distance(In.Position.xy, PlayerPosWorld.xy);
Out.Fog = saturate(exp((StartFog-DistFog)*0.33));
return Out;
}

struct PS_OUTPUT
{
float4 Color : COLOR;
};

PS_OUTPUT ps_main(VS_OUTPUT In) : COLOR
{
PS_OUTPUT Out;
float4 ColorBase = saturate(AmbientIntensity * AmbientColor+dot(In.Light,In.Normal));
Out.Color = lerp(ColorFog, ColorBase, In.Fog);
return Out;
}

technique Simplest
{
pass P0
{
VertexShader = compile vs_3_0 vs_main();
PixelShader = compile ps_3_0 ps_main();
}
}
Hello !

I found the solution of this odd problem.
The samplers are not properly declared. In DX9 the samplers have to be declared with the following syntax:

texture ColorTexture;
texture NormalTexture;

sampler ColorSampler = sampler_state {
Texture = (ColorTexture);
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
MaxAnisotropy = 8;
};

sampler NormalSampler = sampler_state {
Texture = (NormalTexture);
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
MaxAnisotropy = 8;
};


Works perfectly ! :D
Thanks !

This topic is closed to new replies.

Advertisement