Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A triangle with 3 varyings of same value.. does GPU interpolate / waste performance?

I have a simple question of which I was unable to find solid facts about GPUs behaviour in case of 3 vertexes having the same varying output from vertex shader. Does the GPU notice that case or does it try to interpolate when its not even needed ?

This might be interesting as there are quite some cases where you want a constantish varying available in fragment shader per triangle. Please don't just guess, try to bring up references or atleast reasons why you think its the one way or another.

like image 562
Manuel Arwed Schmidt Avatar asked Apr 10 '14 22:04

Manuel Arwed Schmidt


1 Answers

The GPU does the interpolation, no matter if it's needed or not.

The reason is quite simple: checking if the varying variable has already been changed is very expensive.


Shaders are small programs, that are executed concurrently on different GPU cores. So if you would like to avoid that two different cores are computing the same value, you would have to "reserve" the output variable. So you need an additional data structure (like a flag or mutex) that every core can read. In your case this would mean, that three different cores have to read the same flag, and the first of them has to reserve it if it's not already reserved.

This has to happen atomically, meaning that the reserving core has to be the only one who is setting the flag at a time. To do this all other cores would e.g. have to be stopped for a tick. As you don't know the which cores are computing the vertex shader you would have to stop ALL other cores (on a GTX Titan this would be 2687 others).

Additionally, when the variable is set and a new frame is rendered, all the flags would have to be reset, so the race for the flag can begin again.

To conclude: you would need additional hardware in your GPU, that is expensive and slows down the rendering pipeline.


It is the programmers job to avoid that multiple shaders are producing the same output. So if you are doing your job right this does not happen or you know, that avoiding it (on the CPU) would cost more than ignoring it.

An example would be the stiching for different levels of detail (like on a height map), where most methods are creating some fragments twice. This is a very small impact on the rendering performance but would require a lot of CPU time to avoid.

like image 163
Corbie Avatar answered Oct 02 '22 13:10

Corbie