Sign in to follow this  
jerrycao_1985

Perspective division in Vertex Shader? Is it allowed?

Recommended Posts

jerrycao_1985    145

Hi:

 

I've tried to append a very simple line of code onto my vertex shader which works well for everything.

The code is something like this:

 

output.pos /= output.pos.w;

 

Yes, I'm doing perspective division by myself. I knew that the hardware will do it for me right after vertex shader.

However, I want to see if anything goes wrong if it's done in vertex shader.

 

It turned out that everything related to this shader is wrong, not just about the vertex attribute, also the position.

 

So, what's wrong with that code?

 

Thanks in advance for your attention.

Edited by jerrycao_1985

Share this post


Link to post
Share on other sites
cgrant    1825

If out.pos is being outputted via your vertex shader, then it will again get divided by w. The .w coordinate is required for everything that is interpolated in a perspective correct manner. My question is why would you want to do the perspective division ? The hardware already does this....if the perspective correct values are required in the next pipeline stage, then I suggest you use a separate variable for that.

Share this post


Link to post
Share on other sites
kauna    2922

I guess that the problem arises at least with clipping : dividing with the w-component in the vertex shader, the hardware will have issues with vertices having zero or negative w-values.

 

Otherwise, the hardware will redo the division by w after vertex shader, but since the values are already divided by w, the w-component has value on 1.0 (unless w is zero) and it won't have any effect.

 

Cheers!

Share this post


Link to post
Share on other sites
Jason Z    6434

I would expect this to mess up your texture coordinates, if you are using perspective correct texture sampling.  Other than that, if you divide the entire position by its w-coordinate, it shouldn't affect the xy coordinates in the projection - they will be the same if you do the division or if the rasterizer does it.

 

Can you be more specific about what the scenario is where the positions are not correct?  What is the input, what is the VS code, and what is your rasterizer state configuration?  What comes out in your render window?

Share this post


Link to post
Share on other sites
jerrycao_1985    145

Thanks guys.

 

I think there are two issues if perspective division is done in vs.

 

 1. Since some points could be behind the eye, which means that the w component in clip space is negative. The output of Vertex shader is supposed to be in clip space. Of course you can do perspective division in VS and mathematically doing perspective division twice won't hurt anything. However, w will be 1 if perspective divide is done in VS. With w equaling to 1, the hardware is unable to reject any point behind the eye which will lead to wrong result of rasterization.

 2. Since w component of vertex output is used to interpolate attribute, you can't change it arbitrarily. Or there will be wrong attribute interpolation, in other words, perspective correction won't work for attribute.

Share this post


Link to post
Share on other sites

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