Is it possible to provide data from an ArrayBuffer to c++/wasm without copying?

32 views
Skip to first unread message

キャロウ マーク

unread,
Jan 9, 2020, 1:09:48 PM1/9/20
to emscripten-discuss
I have tried and failed to find some documentation about using emscripten::val to transfer data in an ArrayBuffer to a c++ function compiled to .wasm.

I found an example from which I took the lines shown below

       static texture createFromMemory(const emscripten::val& data)
       {
           std::vector<uint8_t> bytes{};
           bytes.resize(data["byteLength"].as<size_t>());
           emscripten::val memory = emscripten::val::module_property("HEAP8")["buffer"];
           emscripten::val memoryView = data["constructor"].new_(memory, reinterpret_cast<uintptr_t>(bytes.data()), data["length"].as<uint32_t>());
           memoryView.call<void>("set", data);
           
       }

but it looks to me as if this is copying that data from the ArrayBuffer to the std::vector.

Is is possible to get a pointer to the data for use by the c++ side without copying, either with embind or with the idl binder? The textures I’m working with here can be *very* large.

Regards

    -Mark
signature.asc

Alon Zakai

unread,
Jan 9, 2020, 1:30:50 PM1/9/20
to emscripte...@googlegroups.com
There isn't a way to let compiled code access a new ArrayBuffer. The compiled code has hardcoded access to the wasm Memory it was instantiated with - all the pointers it can understand are indexes into that Memory. It can't refer to anything else, I'm afraid.

In the future using different address spaces or techniques with reference types may open up some possibilities here.

(The other direction, accessing data in the wasm Memory from the outside without copying, is possible, doing a .subarray() on a typed array view on that memory. That's how the WebGL bindings etc. work.)


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/B97FFC45-FAED-4413-9558-4A6A0BF7287B%40callow.im.
Reply all
Reply to author
Forward
0 new messages