Hi,
I'm currently implementing WebGL2/GLES3 support in my 3D framework (via emscripten), and I'd like to implement dynamic uniform updates similar to how I do it in Metal and D3D12: have one big uniform buffer where I'm recording the uniform updates for the entire frame, and then before each draw call, only set an offset into this uniform buffer for the next draw call.
In pseudo code it would look like this:
// first record all uniform updates into a linear buffer, and store offsets
for dc in draw_calls {
dc.uniform_offset = copy_to_linear_buffer(dc.uniform_data, dc.uniform_size);
}
// flush uniform data for entire frame in one go to GL
glBufferSubData(GL_UNIFORM_BUFFER, 0, max_uniform_offset, linear_buffer);
// draw loop, only bind offset into big uniform buffer
for dc in draw_calls {
glBindBufferRange(GL_UNIFORM_BUFFER, bind_point, uniform_buffer, dc.uniform_offset, dc.uniform_size);
glDrawElements(...);
}
Does that make sense, and would it be fast in the current WebGL2 implementations? It basically depends on glBindBufferRange()
being a very fast operation (it should just 'record' the argument values, and not an actual data copy, the big copy would happen
in glBufferSubData()).
In real code I would use alternate between 2 big uniform buffers from frame to frame, that's what I'm doing for dynamic vertex- and index-data, and it looks like this is the fastest way in WebGL.
Thanks!
-Floh.