3

I am trying to pass an array of point lights from an object class to the vertex shader in OpenGL ES 2.0 on Android.

The point lights are stored as a float[] array and the shader would ideally read the floats as a vec4[] array.

The float[] can be passed to the vertex shader by calling glUniform4fv(...floatArray, 0) and is declared in the vertex shader as uniform vec4 u_PointLights[990] but this is extremely slow.

I'm trying to get the floats into GPU memory; first thought was a VBO but after binding the data and passing it to the shader, I can only read a single vec4 and not an array (i.e. cannot declare attribute vec4[] a_PointLights).

What is the best way to get second vec4[] into the shader?

4
  • 1
    What part of it is extremely slow, the transfer to the shader uniform or just the actual rendering? You might have some slowdown in your shader if theres large complicated for loops and if statements (branching is SLOW in shaders) Failing that a uniform buffer object might be more performant for transfering large arrays, but I don't think it should be too slow sending your array to the uniform. Commented Apr 14, 2016 at 13:55
  • I don't think uniform buffer objects are available on ES 2, sadly, though they arrive with ES 3. But second the query about the shaders themselves: how are you processing the data? Conditionality and random access are ideally to be avoided in shaders; attacking one like a conventional imperative program doesn't usually lead to good results. Commented Apr 14, 2016 at 14:16
  • Also, how often are you calling glUniform4fv? Once per frame? Commented Apr 14, 2016 at 14:51
  • Tommy - Yes, once per frame. WearyWanderer - the point lights are dynamic (sourced from a particle generator) so the vertex shader compares the distance from every point in the buffer against every vertex in each visible triangle. Commented Apr 14, 2016 at 15:52

1 Answer 1

1

In OpenGL ES 2.0 you can't do anything else really - either client-side uniforms or attributes, both of which are going to be somewhat limited.

In OpenGL ES 3.0 you could use a Uniform Buffer Object, but note that very large arrays of uniforms are always going to be relatively expensive, especially on mobile.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Isogen. If the native buffer isn't available in 2.0, how would the performance of passing values via texture map compare to using the float[] uniforms via glUniform4fv?
Not all OpenGL ES 2.0 GPUs support vertex shader texturing - it's not required in the specification - although it is mandatory in ES 3.0. Performance wise it should be OK if available, but you'll have to pack the data into an color format and reassemble the value (again, ES 3.0 would have native loat textures, but ES 2.0 doesn't).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.