• Advertisement
Sign in to follow this  

Perspective division in Vertex Shader? Is it allowed?

This topic is 1510 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

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
Advertisement

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

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

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

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
Sign in to follow this  

  • Advertisement