Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A shader normally has values that vary per-vertex and values that vary per-fragment.

Can you have values that vary only per primitive, such as per-triangle or per-quad?

share|improve this question
Maybe you can use glVertexAttribDivisor: but I don't know, if it will work with ordinary draw commands (without instanced drawing). – zacharmarz May 22 '11 at 11:12
@zacharmarz that ought to go in an answer so it can be enlarged upon; it looks promising, I hadn't seen that before; it would require all bits I drew to be the same size, of course, but using it to give different transform for each unit and draw an army of the same unit, then it might be great! – Will May 23 '11 at 6:42
@Will If you're looking to draw an army with different transforms for each unit, instanced drawing is definitely what you want. I don't think there's a way to have per-triangle values without duplicating them to all three vertices. – Nathan Reed Nov 30 '13 at 20:25
Adding a geometry shader that takes a triangle as input and outputs the same triangle but with the extra attribute added (perhaps read from a texture) may be another way. But - if you're looking for this to try save storage or calculations, be aware that it's often faster to just accept the extra storage or calculations in exchange for a simpler setup. – 21st Century Moose Dec 3 '13 at 10:09
up vote 1 down vote accepted

No, you can't, there is no such concept in the graphics pipeline.

If you are using instancing, you can control the rate at which the instanced attributes advance for a given attribute index. This can be done independently for each attribute, and is probably the closest you're going to get to what you are asking for using the actual functionality of the pipeline. It may not be flexible enough for your needs, however, and it does require instanced rendering.

You can, of course, always build this abstraction into your CPU-side rendering layer by having a buffer object that understands what kind of primitive it stores (triangles, quads, lines, et cetera) and provides a way to manipulate per-primitive attribute buffers which, under the hood, it will replicate out into the real vertex buffer on a per-vertex basis. This won't give you the space-saving advantages, but it would present the simpler client-side interface that you are probably interested in.

share|improve this answer
And while it doesn't work portably right now in webGL, instancing has started to appear on Chrome as an extension. – Will Dec 17 '13 at 6:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.