Perspective division in Vertex Shader? Is it allowed?

Started by
3 comments, last by jerrycao_1985 10 years, 3 months ago

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.

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.

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!

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?

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.

This topic is closed to new replies.

Advertisement