HLSL Position semantic error

Started by
6 comments, last by Atli Thor 16 years, 9 months ago
Hi. Im trying to use the POSITION variable that the VertexShader is passing to my PixelShader to calculate stuff. The problem is that every time I try to use it, I get this error:

(27): error X4502: invalid input semantic 'POSITION'
(104): ID3DXEffectCompiler::CompileEffect: There was an error compiling expression



I've tried renaming the struct members and using POSITION0, but without any luck. This is my HLSL code. (Or attleast the parts I belive are relevant)
[source=c]
// VS input / output structs
struct VS_INPUT
{
  float4 position : POSITION;
  float3 normal : NORMAL;
};

struct VS_OUTPUT
{
  float4 position : POSITION;
  float3 normal : TEXCOORD0;
};

// Vertex shader
VS_OUTPUT VS_Directional(VS_INPUT input)
{
  VS_OUTPUT output = (VS_OUTPUT)0;
	
  // Transform into clip space
  output.position = mul(input.position, worldViewProjection);
	
  // Rotate normal into world space
  output.normal = mul(input.normal, worldViewProjection);
	
  return output;
}

// Pixel shader
float4 PS_Point(VS_OUTPUT input) : COLOR
{
  float4 output;

  // This will cause the error
  float4 tst = input.position;
  
  //... Stuff here that doesn't seem to matter
  
  return output;
}



Any help would be much appreciated. Thanks.
Advertisement

The only thing that I can see in your code is that your INPUT position is probably wrong:

Usually, input vertex position is a float3, not a float4, because Input elements should match those on the vertex buffer.

Notice that, when using a float3 position, you have to manually set a float4 position in your shader, so you can set the w=1, and be able to transform that position by the projection matrix.

hope it helps
This is the error I get using the June SDK:

error X4502: invalid ps_2_0 input semantic 'POSITION'

The line number corresponds to the VS output struct, basically what this means is that you can't directly read the position in the pixel shader (since that input was consumed by the rasterizer). You could use VPOS if that's what you're looking for, or just pass the position in another parameter:

struct VS_OUTPUT
{
float4 position : POSITION;
float3 normal : TEXCOORD0;
float4 worldpos : TEXCOORD1;
};

...

output.worldpos = input.position; // or transform it if you like

...

float4 tst = input.worldpos;

Like the previous poster said, POSITION isn't technically a valid pixel shader input semantic. And according to the docs VPOS/VFACE are only available in PS 3.0. So for now, you're stuck with having to pass the position in a texture coordinate. I think it's silly too, but that's just the way things are.
Quote:Original post by vicviper

The only thing that I can see in your code is that your INPUT position is probably wrong:

Usually, input vertex position is a float3, not a float4, because Input elements should match those on the vertex buffer.

Notice that, when using a float3 position, you have to manually set a float4 position in your shader, so you can set the w=1, and be able to transform that position by the projection matrix.

hope it helps

Not true. All shader inputs get expanded if they're too short, filling in y=0, z=0, and w=1. I always say my position is a float4 to get the automagic 1.0.
That did it!

I added a TEXCOORD1 to my VertexShader output and copied the output position in the VertexShader, like was mentioned above.

It's weird though, that it isn't possible to acctually use the data that is being passed between the shaders. What is the point of the POSITION parameter that the vertex shader apparently has to provide, if you can't even use it?

Well, anyways.
Thanks everybody.
The point of the position output is for the rasterizer to know where to draw your triangle.
Quote:Original post by Namethatnobodyelsetook
The point of the position output is for the rasterizer to know where to draw your triangle.


Ahh ok. So it is not really used by the PixelShader, but rather used to position the pixel the PixelShader is working on at any given moment.

This topic is closed to new replies.

Advertisement