Fusion2 WebGPU project with "Enable Native C/C++ Multithreading

Hello,
I’ve built a Unity WebGPU project with “Enable Native C/C++ Multithreading” set to True in unity 6000.0f .

Everything works fine in the browser, but when I integrate Fusion2 with WebSocket,

the browser notifies me with the following message: “TypeError: WebSocket.send: Argument 1 can’t be a SharedArrayBuffer or an ArrayBufferView backed by a SharedArrayBuffer.” How can I solve this problem?

Note: In Unity, “Enable Native C/C++ Multithreading” works with graphics jobs but not with C#. How can I make WebSocket behave similarly to jobs?

Does this also occur in a regular WebGL app? The way you describe it makes it seem as if the issue is related to having native multithreading enabled, but it might not be. Perhaps best to (also) check with Photon for support.

Thank you for your interest.

Yes, it’s the same issue. Both WebGL and WebGPU operate well without multithreading enabled, but they encounter problems when multithreading is activated. I receive the following error:

“TypeError: WebSocket.send: Argument 1 can’t be a SharedArrayBuffer or an ArrayBufferView backed by a SharedArrayBuffer.”

I’ve reached out to Photon about this on Discord, but I haven’t received a response yet.

Hi!

What kind of bindings do you use to use WebSockets? Do you use an existing library or your own plugin?

When multithreading is enabled the access to the global WebAssembly memory uses a SharedArrayBuffer to allow access accross main thread and workers. WebSocket.send does not accept these buffers so you need to create a local copy into a normal TypedArray.

Something like this should work:

// Given a buffer with the data to send, offset and length
var data;
if (buffer instanceof SharedArrayBuffer) {
  // Create copy of buffer region
  var array = new UInt8Array(length);
  array.set(new Uint8Array(buffer.slice(offset, offset + length)));
  data = array.buffer;
} else {
  // Just use slice of buffer
  data = buffer.slice(offset, offset + length);
}
// Send over websocket
webSocket.send(data);
1 Like