开发者

When should a uniform be used in shader programming?

开发者 https://www.devze.com 2022-12-26 07:46 出处:网络
In a vertex shader, I calculate a vector using only uniforms. Therefore, the outcome of this calculation is the same for all instantiations of the vertex shader. Should I just do this calculation on t

In a vertex shader, I calculate a vector using only uniforms. Therefore, the outcome of this calculation is the same for all instantiations of the vertex shader. Should I just do this calculation on the CPU and upload it as a uniform? What if I have ten such calculations? If 开发者_开发知识库I upload a lot of uniforms in this way, does CPU-GPU communication ever get so slow that recomputing such values in the vertex shader is actually faster?


I actually depends on the uniforms/vertex count ratio. In the case vertices are more than the uniform variables, it's probably better to compute uniforms on CPU, which is the most frequent scenario.

In the case uniforms values are many and their computation is complex (i.e. inverse matrix), while vertices count is low, it may be better to off load the CPU and perform computation on GPU.

The ratio threshold value is difficoult to determine, since many factor influence the shader execution. Normally its better to compute most of uniforms on CPU, to offload the shader execution.

A note: once you set the uniform variables, the shader program mantains their values untill its relinked again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号