Sign in to follow this  

Translating from PSA to HLSL

This topic is 2843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm still trying to convert my pixel shader from PSA2.0 to HLSL to fix a problem with it cutting off diffuse lighting from directions with negative numbers as representations that I posted weeks ago. Here is my original shader, which works fine except for cutting off all light when it comes from the left/bottom/front:
ps.2.0
dcl t0 //tex coord
dcl t1 //face normal
dcl_2d s0 //texture
def c3, 0.0f,0.5f,2.0f,-1.0f //handy values for math
//c2.xyz: diffuse licht direction, c2.w=1
//c4: ambient farbe
//c5: diffuse farbe
//c6: difference
dp3_sat r0,t1,c2 //dp face normal (t1) with light direction (c2). Saturates to [0,1]
//dp3 r0,t1,c2//dp face normal (t1) with light direction (c2).
mul r0,r0,c5//multiply by diffuse in [0,1]
mul r0,r0,c6//scale diffuse addend to [0,diffuse-ambient]
add r0,r0,c4//add ambient
//mov r0.w,c2.w
texld r1,t0,s0			//get stage 0 texture from coordinates t0
mul r0,r0,r1//mul texture and light/norm dp
mov oC0,r0//output





Here is the HLSL which MJP posted on that thread:
float3 LightDir;
float3 AmbientColor;
float3 DiffuseColor;
float3 DiffuseAmbientDiff;
sampler2D DiffuseSampler:register(s0);

float4 PSMain(in float2 texCoord:TEXCOORD0,in float3 normal:TEXCOORD1):COLOR0{
    normal=normalize(normal);
    float NdotL=saturate(dot(normal,LightDir)); 
    float3 diffuse = NdotL * DiffuseColor * DiffuseAmbientDiff;
    //Here is what I believe we'll eventually have to incorporate to include the texture coordinates from the sampler:
    //float4 diffuse=tex2D(DiffuseSampler,texCoord)*NdotL*DiffuseColor*DiffuseAmbientDiff+AmbientColor;
    return diffuse;
}





I compile the new HLSL shader with:
D3DXCompileShaderFromFile(graphPSFile[dw],NULL,NULL,"PSMain","vs_3_0",D3DXSHADER_DEBUG,&buf,&fehlerMsgs,NULL)
and it gives the error:
hlslPS.txt(12,5): error X3017: cannot implicitly convert from 'float3' to 'float4'
Help appreciated. Thx. [Edited by - bababooey on February 27, 2010 3:07:47 PM]

Share this post


Link to post
Share on other sites
You're trying to pass in a float3 where a float4 is expected.

Eg you declare the function to return a float4, then you try to return a float3..


float4 PSMain(in float2 texCoord:TEXCOORD0,in float3 normal:TEXCOORD1):COLOR0{
float3 diffuse = NdotL * DiffuseColor * DiffuseAmbientDiff;
return diffuse;
}

Share this post


Link to post
Share on other sites
Yes, I've also tried changing the return type to:

float3 PSMain(...)

and also converting all of the variables (except direction) to float4:


float3 LightDir;
float4 AmbientColor;
float4 DiffuseColor;
float4 DiffuseAmbientDiff;
sampler2D DiffuseSampler:register(s0);
float4 PSMain(in float2 texCoord:TEXCOORD0,in float3 normal:TEXCOORD1):COLOR0{
normal=normalize(normal);
float NdotL=saturate(dot(normal,LightDir));
float4 diffuse = NdotL * DiffuseColor * DiffuseAmbientDiff;
//Bla bla bla
//float4 diffuse=tex2D(DiffuseSampler,texCoord)*NdotL*DiffuseColor*DiffuseAmbientDiff+AmbientColor;
return diffuse;
}




Either way, it says:

error X4541: vertex shader must minimally write all four components of POSITION



Here is the part of my vertex shader that writes POSITION:

vs.2.0
dcl_position0 v0
dcl_normal0 v1
dcl_texcoord0 v2
dcl_position1 v3
dcl_normal1 v4
dcl_texcoord1 v5
def c14, -1.0f,0.0f,0.0f,0.5f
//{tween,1-tween,0,1} in c12

mul r0,v0,c12.yyyy //multiply position0 (v0) by 1-tween
mad r1,v3,c12.xxxx,r0 //add it to position1 (v3) multiplied by tween
m4x4 r0,r1,c8 //multiply tweened position (r1) by world matrix
m4x4 r1,r0,c4
m4x4 oPos,r1,c0 //multiply position (r1) by projection matrix

//more stuff here for normal and texture coords





Isn't that writing all 4 components of position?

[Edited by - bababooey on February 28, 2010 6:11:31 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Promit
Easy solution:
 return float4(diffuse, 1.0)


Still the same error:

error X4541: vertex shader must minimally write all four components of POSITION


Quote:
Original post by Promit
Real solution:
Most people use 4 component colors...

Right, so how do I change the above to do that? I thought that my (asm) vertex shader above was dealing with a 4-component position, starting with:

dcl_position0 v0

and after applying matrix math shoving it into oPos. How does this not get all 4 components through to POSITION of the pixel shader?

Share this post


Link to post
Share on other sites
Quote:
Original post by bababooey
Still the same error:

error X4541: vertex shader must minimally write all four components of POSITION



so what does your vs code look like?

Share this post


Link to post
Share on other sites
As I posted above:

vs.2.0
dcl_position0 v0
dcl_normal0 v1
dcl_texcoord0 v2
dcl_position1 v3
dcl_normal1 v4
dcl_texcoord1 v5
def c14, -1.0f,0.0f,0.0f,0.5f
//{tween,1-tween,0,1} in c12

mul r0,v0,c12.yyyy //multiply position0 (v0) by 1-tween
mad r1,v3,c12.xxxx,r0 //add it to position1 (v3) multiplied by tween
m4x4 r0,r1,c8 //multiply tweened position (r1) by world matrix
m4x4 r1,r0,c4
m4x4 oPos,r1,c0 //multiply position (r1) by projection matrix

//more stuff here for normal and texture coords



Shouldn't that write all 4 components of POSITION? Perhaps that is best converted to HLSL as well?

Share this post


Link to post
Share on other sites

This topic is 2843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this