"lit" function in HLSL node

All other topics about Ventuz here.

Moderator: Support

Post Reply
TVM Cracklings
Posts: 33
Joined: 30 Sep 2015, 10:49

"lit" function in HLSL node

Post by TVM Cracklings » 30 Sep 2015, 11:18

Hi there.

I was trying to play around with custom lighting models, and immediately encountered the following problem, which should be a really basic one, but I'm really not sure why it works as it is.

code:

Code: Select all

#define pi 3.14159265358979324

float4x4 mMVP : WORLDVIEWPROJECTION;
float4x4 mMV : WORLDVIEW;
float4x4 mIMV : WORLDVIEWINVERSE;

struct VDL{
	float3 D;
	float3 S;
	float3 A;
	float3 vDir;};

struct VPL{
	float3 D;
	float3 S;
	float3 A;
	float3 vPos;
	float3 vAtt;
	float Range;};

struct VSL{
	int Type;
	float3 D;
	float3 S;
	float3 A;
	float3 vPos;
	float3 vDir;
	float3 Att;
	float4 FalloffO2I;};

struct VL{
	int Type;
	float3 D;
	float3 S;
	float3 A;
	float3 vPos;
	float3 vDir;
	float3 vAtt;
	float4 FalloffO2I;};


VDL DL[8]<	string SasBindAddress = "Ventuz.DirectionalLight[*]";>;
VPL PL[8]<	string SasBindAddress = "Ventuz.PointLight[*]"		;>;
VSL SL[8]<	string SasBindAddress = "Ventuz.SpotLight[*]"		;>;
VL  L[8] <	string SasBindAddress = "Ventuz.Light[*]"			;>;

int DLCount<	string SasBindAddress = "Ventuz.NumDirectionalLights"	;>;
int PLCount<	string SasBindAddress = "Ventuz.NumPointLights"			;>;
int SLCount<	string SasBindAddress = "Ventuz.NumSpotLights"			;>;
int LCount<		string SasBindAddress = "Ventuz.NumLights"				;>;

struct a2v{
	float4 vPos : POSITION0;
	float4 vNormal : NORMAL0;
};

struct v2f{
	float4 vPos : POSITION0;
	float4 vPosVS : TEXCOORD0;
	float4 vNormalVS : NORMAL0;
};

float sp<string SasUiLabel="Specular Power";float SasUiMin=0;> = 50;
float3 c<string SasUiControl="ColorPicker";>;

v2f vert(a2v Input){
	float4x4 mITMV = transpose(mIMV);
	v2f Output;
	Output.vPos = mul(Input.vPos, mMVP);
	Output.vNormalVS = mul(Input.vNormal, mITMV);
	Output.vPosVS = mul(Input.vPos, mMV);
	return Output;
}

float4 frag(v2f Input) : COLOR{
	//	Directional Light:
	float3 n = normalize(Input.vNormalVS.xyz);
	float3 l = -normalize(DL[0].vDir);
	float3 v = normalize(Input.vPos.xyz);
	float3 h = (l+v)/2;
	float4 LCoef = lit(dot(n,l),dot(n,h),sp);
	
//	return LCoef*float4(c,1);
	return float4(c,1);
}

technique tech0{
	pass pass0{
		vertexshader = compile vs_3_0 vert();
		pixelshader = compile ps_3_0 frag();
		
		ZEnable = TRUE;
	}
}
So as you can see, nothing crazy yet, I was just trying to get the traditional Phong lighting working.

Problem: In the last two lines in my pixelshader (the frag function), I have one return commented out. The fx shader works fine as it is, but if I swap both lines, I get compile errors:
  • (58,16)Legal indices are in [1,15]
  • (58,16)invalid ps_3_0 input semantic 'POSITION0'
  • (90,17)There was an error compiling expression
line 57-58:

Code: Select all

struct v2f{
    float4 vPos : POSITION0;
line 90:

Code: Select all

pixelshader = compile ps_3_0 frag();
I would appreciate it a lot if anyone could point out what I did wrong there.

Thanks in advance

-C

User avatar
lerou
Posts: 331
Joined: 06 Sep 2013, 07:14
Location: Hamburg, Germany

Re: "lit" function in HLSL node

Post by lerou » 01 Oct 2015, 11:13

Hi,

the pixel shader does not have POSITION0 as input semantic. You output it from the vertex shader but can't access it directly from the ps.

As a quick workaround try this struct:

Code: Select all

struct v2f{
	float4 vPos : POSITION0;
	float4 vPos2 : TEXCOORD1;
	float4 vPosVS : TEXCOORD0;
	float4 vNormalVS : NORMAL0;
};
In the vs:

Code: Select all

Output.vPos2 = Output.vPos;
In the ps:

Code: Select all

float3 v = normalize(Input.vPos2.xyz);
this basically stores the position vector in a texcoord register making it accessable.

Best,
rou

TVM Cracklings
Posts: 33
Joined: 30 Sep 2015, 10:49

Re: "lit" function in HLSL node

Post by TVM Cracklings » 01 Oct 2015, 14:40

Hi rou,

thank you for your reply.

Problem identified.
It shouldn't have bin the coord in projection space at all, it was meant to be vPosVS, which was passed per TEXCOORD1.
I knew I was doing something silly x)


But to make it more clear:

If I understand correctly, POSITION0 is the equivalence of gl_Position in GLSL?
Which would then explain why a pixel shader can't have access to POSITION0 (it was eaten by the rasterizer :o ).

Thanks!

BR
-C

Post Reply