简体   繁体   中英

Forcing WebGL texture reload from buffer

I am using (a slightly modified version of) mmap in electron to share memory between two different processes. Then I use this memory, in the form of a buffer to upload a WebGL texture and I use it to draw stuff:

buffer = mmap(196608,mmap.PROT_READ,mmap.MAP_SHARED,"shared_mmap",0)

f = function(){
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 256, 256, 0, gl.RGB, gl.UNSIGNED_BYTE, buffer);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null)
}

This works perfectly, the first time. Although I can see the data being changed by the other process (if I evaluate buffer[100] in the console) the texture remains the same in the WebGL scene.

However, if I copy the buffer to a TypedArray, it works perfectly:

arr = new Uint8Array(buffer)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 256, 256, 0, gl.RGB, gl.UNSIGNED_BYTE, arr);

The problem is that doing it this way I am repeatedly copying all the data every frame, which kind of defeats the purpose of using a mmap.

Is there a way of updating the WebGL texture with buffer data without copying its contents to a new array every time?

You are not copying the data, with new Uint8Array(buffer) , you are creating a new reference to the same data.

Here you can read more about DataViews.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM