Translating from PSA to HLSL

Started by
6 comments, last by bababooey 14 years, 1 month ago
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]
Advertisement
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;
}
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.0dcl_position0 v0dcl_normal0 v1dcl_texcoord0 v2dcl_position1 v3dcl_normal1 v4dcl_texcoord1 v5def c14, -1.0f,0.0f,0.0f,0.5f//{tween,1-tween,0,1} in c12mul r0,v0,c12.yyyy			//multiply position0 (v0) by 1-tweenmad r1,v3,c12.xxxx,r0		//add it to position1 (v3) multiplied by tweenm4x4 r0,r1,c8				//multiply tweened position (r1) by world matrixm4x4 r1,r0,c4m4x4 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]
Easy solution:
 return float4(diffuse, 1.0)

Real solution:
Most people use 4 component colors...
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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?
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?
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
As I posted above:
vs.2.0dcl_position0 v0dcl_normal0 v1dcl_texcoord0 v2dcl_position1 v3dcl_normal1 v4dcl_texcoord1 v5def c14, -1.0f,0.0f,0.0f,0.5f//{tween,1-tween,0,1} in c12mul r0,v0,c12.yyyy			//multiply position0 (v0) by 1-tweenmad r1,v3,c12.xxxx,r0		//add it to position1 (v3) multiplied by tweenm4x4 r0,r1,c8				//multiply tweened position (r1) by world matrixm4x4 r1,r0,c4m4x4 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?
*Bump*

This topic is closed to new replies.

Advertisement