0

WebGL 1 doesn't seem to have support for the flat modifier.

As a result, how do I avoid duplicating a ton of data when sending data to the vertex shader?

For example, in my use-case I have many lights to render (which are just a bunch of triangles clipped by a circular radius). So let's say each light point has a radius, tint, and center. If I were to do separate draw calls, this is trivial, as I can pass things like tint and radius as uniforms.

However, let's say I wanted to render all of my lights in one draw call. My intuition is that I would have to load the vertex buffer like so:

Triangle Points Attribute: X1, Y1, X2, Y2, X3, Y3, ...
Radius Attribute: 100, 100, 100, 100, 100, 100, ...
Center Attribute: X1, Y1, X1, Y1, X1, Y1, ...
Tint Attribute: 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, ...

This is because varyings are naturally interpolated, so unless I duplicate them, the attribute values will be interpolated from triangle to triangle, when in reality I desire they just stay static and unchanging, like a uniform. But they can't be a uniform because different light points have different radiuses, centers, tints, etc. As you can see, in the above, the only non-duplicated data are the triangle points. Everything else is just repeated over and over.

I suppose an alternative is to just have a separate draw call for each light point, but I'm not sure which is "better" practice. If I have separate draw calls, then I avoid duplicating data, but then I have to iterate through static every light in the game and redraw them all separately.

Ideally there would be a way to just pass attributes as non-varying so that I could avoid having to duplicate all this data, but I'm not sure how to do that. Is that possible?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136

1 Answers1

0

If I inderstand you correctly you're building a vertex buffer with a triangulated circle for each of your lights in it? If so this is not a matter of flat vs interpolated varyings (data passed from one shader stage to the other) fed by attributes but just a matter of "how do I not set my light properties on each vertex of the light primitive" in which case you can either use instanced drawing (available through extension in WebGL1) or you fetch the data via a texture lookup in the vertex shader. If you're not memory bound duplication is probably the fastest of the bunch though.

Drawing each light individually would be quite inefficient.

LJᛃ
  • 7,655
  • 2
  • 24
  • 35