glow icon indicating copy to clipboard operation
glow copied to clipboard

Expose loading textures from images in WebGL

Open anlumo opened this issue 6 years ago • 5 comments

In WebGL, most images are loaded via an HTMLImageElement, since then the browser does all of the image loading and format decoding. To load one of these into WebGL, web_sys exposes the function tex_image_2d_with_u32_and_u32_and_html_image_element, but glew does not provide any way to call it.

anlumo avatar Jun 16 '19 16:06 anlumo

Same with buffer_data_with_array_buffer_view. In WebGL, you can avoid copying an array buffer to JavaScript memory by creating an array buffer view into the WASM memory and then passing that object to the mentioned function.

anlumo avatar Jun 16 '19 17:06 anlumo

For tex_image_2d_with_u32_and_u32_and_html_image_element I think we can expose it on the web backend directly for now.

For buffer_data_with_array_buffer_view, I think buffer_data_with_u8_array (used in https://github.com/grovesNL/glow/blob/master/src/web.rs#L598) already expands into an array buffer, but if this is doing a copy internally then I agree – we should try to use the wasm memory directly instead.

grovesNL avatar Jun 16 '19 21:06 grovesNL

Take a look at the official web-sys example for WebGL on how Vertex Array Buffers can be created without copying the data. If you pass a slice, I think it'll be copied over to JavaScript.

anlumo avatar Jun 16 '19 21:06 anlumo

Sounds good – I'd prefer to use whatever method has the least number of copies.

Based on the example you linked, it looks like moving through a temporary typed array and using subarray to attach it to wasm memory is probably the best option right now, so we should switch to using that internally.

grovesNL avatar Jun 16 '19 21:06 grovesNL

Yeah, if glow could abstract the whole array buffer view-hack away from the application code, that would be the best solution.

anlumo avatar Jun 16 '19 21:06 anlumo