Packing uniforms into matrix

Started by
4 comments, last by corysama 5 years, 11 months ago

Hi, I'm trying to optimize a webgl 1.0 app, and right know we have around 8-9 uniforms that are updated every frame (mostly floats and vec2's). We have a high cpu overhead due to drawcall count and I was wondering if it would be worth to pack all of those uniforms in a 3x3 matrix instead of setting all of them one by one.

Advertisement

Rather than 3x3 it might help to lay them out as float4 (vec4) and upload them at that granularity. Also make sure each float4 is 16-byte aligned, just in case.

Btw, did you mean updated every draw-call rather than every frame? Uploading 100 bytes (?) once a frame costs nothing even on the poorest mobile HW.

Thanks for the 16-byte aligned tip! And regarding the draw-call, we have a lot of objects that use the same shader, but we can't batch them because they need different uniform values, so those values change per frame and per object.

You could batch the per-draw-call values into elements of an array inside of the constant buffer and index by the index of the individual objects (where to get that from? per-draw uniform update :D) The whole constant buffer with values for N objects would be uploaded at once. Max size is platform dependent. I'm not sure how well could this perform and if in-shader indexing of arrays in CBs is available and/or performant.

Don't stop at packing some of them into a matrix.  Pack all of them into an array of vec4s and alias them with "#define shininess array[3].y"

This topic is closed to new replies.

Advertisement