Best way for passing color in fragment shader

#1

Hi! I have a mesh with many vertices and I want to assign the same color to them (the mesh is rendered with only one color).
So I think my options are two:

  • I can store for each vertex the same color , or
  • I can assign once with a uniform the color to be used in my shader before the render loop.

Which one and why do you think is better for performance?

Thanks for any suggestions :smiley:

#2

You have other options.

If by #1 you mean creating another vertex attrib array for colors with repeated color values (registered with glVertexAttribPointer*()), and by #2 you mean setting a constant vertex attribute value (registered with glVertexAttrib*()), then you could of course also pass this color in via some uniform state (e.g. via default scope uniform, UBO, SSBO, TBO, texture, etc.).

Option #2 may involve a slight reduction in the memory bandwidth needed to populate the vertex attributes for each vertex shader execution. But ultimately it’s going to depend on the internals of your GL driver and your GPU. Just try it it and see!