0

I have a shared array buffer, I'm currently putting the shared array buffer into a Unit8ClampedArray, then I copy it's contents into a non-shared buffer which is slow and inefficient is there a faster way of doing this? I'm using ctx.putimagedata to render to the canvas, all that is important is that the canvas renders the sharedarraybuffer.

the thing being renderered is at a resolution of 175x87

edit: I don't care what context the canvas is in as the SharedArrayBuffer the only thing being rendered. I'm only looking to support desktop chrome. the reason why I'm using SAB is web workers won't make a copy of it rather they'll all share that memory. I'm doing this to leave the main thread open to do other operations other than the workers sending information in bulk and throwing out a ton of large arrays.

MathMan05
  • 3
  • 4
  • Nope no other way. I'll amend the answer in the linked Q/A to reuse the same AB as that's the best we can do memory wise. – Kaiido Aug 29 '21 at 02:10
  • @Kaiido at the end of the SharedArrayBuffer there is a part that says this: `const canvas = document.querySelector('canvas'); const gl = canvas.getContext('webgl'); const buffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buffer); gl.bufferData(gl.ARRAY_BUFFER, sab, gl.STATIC_DRAW);` I tried to use it and nothing happened, do you know what it does? – MathMan05 Aug 29 '21 at 02:19
  • Where did you find this exactly? – Kaiido Aug 29 '21 at 02:21
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer – MathMan05 Aug 29 '21 at 02:22
  • This is for webgl contexts, putImageData is for 2D context. In 2D context there is no way to use a SharedArrayBuffer directly. If you're ok to use a webgl context then you could create a WEBGLBuffer backed by the SharedArrayBuffer, but this is not my area. – Kaiido Aug 29 '21 at 02:26
  • I don't care how it gets rendered really, although I'm lost in trying to figure out how WebGL works. – MathMan05 Aug 29 '21 at 02:27
  • How come you don't care? Is this the only thing you want to render on your canvas? – Kaiido Aug 29 '21 at 02:31
  • yeah, it's the only thing that's being rendered. – MathMan05 Aug 29 '21 at 02:54
  • 1
    In that case webgl might be an option yes, and I'll reopen your question, but please [edit] it to make it clear you are ok using any rendering context. And also please explain more about your case. Why use a SAB at all? Sounds like an ImageBitmap would be even better here. Can the context populating this SAB produce one? Which browsers are you targeting? Maybe even an OffscreenCanvas would be a viable solution for you. – Kaiido Aug 29 '21 at 02:59
  • 1
    Please give some background as to why you use a SharedArrayBuffer. What does it mean "I do take mouse inputs". **Clearly** explain your case. And add information about the browsers you need to support (e.g all iOS and Webkit based browsers are out by the mere fact of using SAB. – Kaiido Aug 29 '21 at 03:20
  • If you only want to support Chrome then you want an OffscreenCanvas, don't even bother passing anything back to the main thread. – Kaiido Aug 29 '21 at 04:19

0 Answers0