4
\$\begingroup\$

While coding my WebGL app I've encountered an interesting phenomena:

On my first PC (with GPU Radeon HD 5850), BrowserLeaks (link) tells me that in my browser - Google Chrome Version 36.0.1985.143m the Max Vertex Uniform Vectors value equals 1024 - which is true, when I try to create inside the shader an attribute array bigger than 1024, the browser throws an error: too many uniforms, which f.e. in my case let's me draw about 85 simple cubes in a single draw call.

Meanwhile on my other PC (with GPU Intel X3100) with Opera-Next Version 12.15 installed, where BrowserLeaks shows a value 4096 next to the Max Vertex Uniform Vectors field, I can init an array of size 250 000 and even bigger, I can draw 20 000 cubes in a single draw call and everything works fine (except a very low framerate).

So now my question is: Why those numbers varies so much, why in the second case the upper limit value does not seem to be valid? How would I find the true upper limit value (and read it inside my WebGL app at the runtime)?

EDIT: Ok, I've found out how to get this parameter inside a WebGL app: gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS), but the question now is how would I adapt it to shaders...

\$\endgroup\$
1
\$\begingroup\$

The answer is relatively similar to this one except you already figured out how to query the limits, so I try to exand it a little bit.

There is no clear perfect solution how to work with those limits, but a good strategy seem to be to only use uniforms you absolutely need to get things to display at all ( which should be really low ) and then "use up" the remaining slots for optional enhancements you can enable / disable or number of lights you can increase / decrease.

edit: you may struggle with the fact that all your for loops must be constant, you can't plainly write for( int i = 0; i < numLights; i++ ) { ... }. It's a bit unintuitive but if you want that you have to write.

#define MAX_LIGHTS 4

uniform int numLights;

void main ( void ) {
     for( int i = 0; i < MAX_LIGHTS; i++ ) {
         if ( i == numLights ) break;
         /* your lighting calculations */
     }
}

Remember that you can also easily runtime compile your shaders in webgl in order for them to work very well in different configurations, it's common for webgl engines to split their shader code into small parts they can parameterize / exchange. The above example has the disadvantage to always "use up" all the active uniforms for the maximum amount of lights, even if the actual amount is lower. If you'd want to do something else with it you'd have to recompile it.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ > runtime compile your shaders - bling bling Could you tell me something more about this method? Right now I have wroten a simple shader generator that is based on an idea "if this material needs to handle 2 lights, then put apropriate String to shader variable and then compile it" but it is extremely debug-hostile. \$\endgroup\$ – Winged Feb 4 '15 at 10:02
-1
\$\begingroup\$

Old hardware had more limits how many uniforms can be allocated. DX10 hardware removed this limit with constant buffers.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This does not answers a question at all. \$\endgroup\$ – Kromster Aug 29 '14 at 12:48
  • \$\begingroup\$ It answers why there's different limit no? \$\endgroup\$ – Jeff Miles Aug 29 '14 at 12:52
  • \$\begingroup\$ I forgot to add that my second PC is much older than the first one, I'll add their specifications as soon as I'll arrive home. - INFO ADDED \$\endgroup\$ – Winged Aug 29 '14 at 12:54
  • \$\begingroup\$ Maybe because Chrome has other max limit then Opera then? Did you try same browser on the other PC? \$\endgroup\$ – Jeff Miles Aug 29 '14 at 13:00
  • \$\begingroup\$ @Jeff On my first PC I have checked Chrome, Opera and Opera-Next and those browsers has a limit of 1024 MVUV's, and on all of them the limit is valid and the declaration of an array bigger than 1024 results in an error. On the other PC, on Chrome, Opera-Next and dwb, it shows a limit of 4096, and it is valid too on all browsers except for the Opera-Next, where the limit seems to be at least much much bigger. \$\endgroup\$ – Winged Aug 30 '14 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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