Passing uniforms to fragment or vertex shader?

In Vulkan, we can pass uniforms from our code to the fragment shader or to the vertex shader. That’s our decision.
Question: Is it good practice to pass directly to fragment shader data that is only used there (instead of passing it to the vertex shader and let it pass the data to the fragment shader)? For example, the camera position or some lighting parameters.
But, if we do so, then aren’t we passing that data too many times (once per fragment)? We could pass it to the vertex shader (once per vertex).
Yeah, the vertex shader would have to pass that data to the fragment shader anyway, but maybe that is faster than sending the data directly to the fragments.
This may be a silly question, but I was worried about this because I have seen in many places how people send all the uniforms to the vertex shader and then pass them to the fragment shader, even uniforms that are only used in the fragment shader. I wanted to know how expensive/cheap each option is, and what should be done in each situation.

1 Like

You are really over-complicating something that at its core is very simple.

Does it change within a draw call? If not, then it should be a uniform; that’s why the word “uniform” was chosen.

Does it vary per-vertex in a draw call? Then it’s a vertex shader input. It may eventually get to the fragment shader via the VS and interpolation across a primitive’s surface, but it starts the pipeline as a VS input.

Where have you seen this? The only reason one might do this is if you want the same fragment shader text/compiled binary to be usable with data that comes from a uniform or a VS input.

1 Like

No, we’re not. The uniforms are only set once, irrespective of shader type. They’re then stored in GPU registers or buffers for subsequent executions of the shader, or until they’re changed.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.