Currently, uploading large 4096x4096 textures using texImage2d
is quite slow, locking the main thread while the texture is sent to the GPU and ultimately causing stuttering.
From what I've read, WebGL2 has the ability to use PBO's (Pixel Buffer Objects) to create a texture on the GPU in a more efficient manner. However, I am unable to find any examples online of how to do so.
I have found a good description of how to achieve this in OpenGL, but am unsure how to proceed using the WebGL API.
I would like to use either a Canvas
or an ImageBitmap
as the source for the texture data.
So far I am testing by drawing the texture to a canvas, then converting the image to an arrayBuffer
using canvas.toBlob()
followed by FileReader
and readAsArrayBuffer
. Then once I actually have a valid buffer, I attempt to create the PBO and upload it.
The relevant part of my code looks like this:
var buf = gl.createBuffer();
var view = new Uint8Array(ArrayBuffer);
gl.bindBuffer(gl.PIXEL_UNPACK_BUFFER, buf);
gl.bufferData(gl.PIXEL_UNPACK_BUFFER, view, gl.STATIC_DRAW);
gl.texImage2D(gl.TEXTURE_2D, 0, this.format, this.width, this.height, 0, this.format, this.type, 0);
But this returns the error:
GL_INVALID_OPERATION : glTexImage2D: pixel unpack buffer is not large enough
I really have no idea if i'm even approaching it correctly, so any help would be greatly appreciated.