Sign in to follow this  

vertex projection in shaders

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

question for the shader developers out there.

is it general practice to upload the MVP matrix as a constant and perform one matrix-vector transformation to get the output
or
does one upload the world_matrix per model and the VP matrix and perform two transformations?

the way I see it is that if you pre calculate the matrix then that is good becuase there is less work for the GPU per vertex (one matrix mult) but the down side is the added CPU of the matrix matrix multiplication and the fact that there is potentially less flexibility to mess with the vertex position in the vertex shader.


If you could share with me your thoughts on this that would be super appreciated as I am trying to decide on how best to build this into my engines pipeline :)

Share this post


Link to post
Share on other sites
It very much depends on your application. If you are rendering one object at a time, it would be best to do the matrix-matrix multiplication on the CPU, as it is insiginificant in terms of computing time. However, on a modern GPU a few more DP3s will not make much difference, especially if there are not too many vertices. The times when you would want to do two (or more) matrix multiplications are when you are using, for example, instancing, or something a bit more complicated than simply rendering a model.

So to answer your question, I would multiply the matrices on the CPU, and do one matrix multiply per vertex on the GPU if you have an application which needs to draw one model at a time.

Hope that was of some help!

Share this post


Link to post
Share on other sites
[quote name='bwhiting' timestamp='1311337101' post='4838869']
question for the shader developers out there.

is it general practice to upload the MVP matrix as a constant and perform one matrix-vector transformation to get the output
or
does one upload the world_matrix per model and the VP matrix and perform two transformations?

the way I see it is that if you pre calculate the matrix then that is good becuase there is less work for the GPU per vertex (one matrix mult) but the down side is the added CPU of the matrix matrix multiplication and the fact that there is potentially less flexibility to mess with the vertex position in the vertex shader.


If you could share with me your thoughts on this that would be super appreciated as I am trying to decide on how best to build this into my engines pipeline :)
[/quote]


Both are fine. 1 matrix multiply is about 4 dot products in the shader so it isn't really big overhead to do several matrix vector multiplies.

However:

- sometimes you may need the position of vertex in view space (for example storing scaled view space z in deferred shading) so you'll need view and projection matrices separately in the shader. Luckily you'll need to update those matrices only once per frame

- With geometry instancing your vertex shader may receive world matrix from other stream so you cannot really calculate the MVP matrix before the vertex shader. Again, you'll need view and projection matrices. To make life easier you can also provide viewprojection matrix.

I do most of my drawing with instancing so I have lots of matrix calculations in the shader. Seems faster than drawing objects one by one anyway.

Cheers!

Share this post


Link to post
Share on other sites
cheers for the fast feedback! v informative

I am writing this for the flash platform and only have very low level access to a very limited feature set, so no proper instancing to be abused :( and no deferred rendering


I guess that means I am rendering one object at a time leading to a fair few draw calls, but as I am on the flash platform matrix x matrix multiplication is about 10-100 times slower than what you guys are probably used to, that said it shouldn't really ever be more than a few hundred objects being drawn at any time.

Good food for thought!

Feel free to add anything else to this as am sure there are other angles I haven't considered yet!

Share this post


Link to post
Share on other sites

This topic is 2334 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.

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