Spector.js icon indicating copy to clipboard operation
Spector.js copied to clipboard

Support RenderingContext.prototype.call

Open dakom opened this issue 7 years ago • 16 comments

It seems that Spector, the browser extension at least, doesn't work with contexts created through wasm:

For example, try loading this page with the extension enabled, and the red button does not open the report: https://rustwasm.github.io/wasm-bindgen/exbuild/webgl/

I also couldn't see a way to do this through the spector object on window

dakom avatar Dec 20 '18 13:12 dakom

I ll look into it ASAP but are you also hooking on the context which could prevent the spector hook to work ?

I was thinking in the webgl.js file ?

Would be amazing if you have a bare webassembly/webgl example not evaled from webpack to see what we can do.

sebavan avatar Dec 20 '18 14:12 sebavan

That link is the officiam Rust/Wasm WebGL demo - not mine :)

I actually was able to get spector to generate something by playing with the various tickboxes. I know that's not very helpful, but it's something ;)

Is there a way to manually pass a Canvas element or a WebGlRenderingContext to Spector? Could try that in the console to see if it helps.

(my local demo is more convoluted, also using webpack - and typescript/react too so it's a bit more hectic overall...)

Happy to create a stripped-down version but tbh due to webassembly loading and the ecosystem overall, I think the typical use case is probably using webpack

dakom avatar Dec 20 '18 14:12 dakom

Ok so spector works fine, it is just that the samples does not render frames, just the first one shot render triangles :-)

You can then use the reload button to capture all the first 50 commands to understand every single calls :-)

image

It takes a bit of time as there are only 20 commands called so it display the result after a timeout of 10 seconds.

Closing this issue but would be interested to see on bigger scale samples.

sebavan avatar Dec 20 '18 14:12 sebavan

I put together an independent demo: https://github.com/dakom/temp-webgl-wasm-test

The wasm+webgl part renders a quad, but I can't get Spector to work with it. Thinking maybe it's something with the Typescript wrangling or webpack bundling?

Note that the production build does minify via Terser, but the same issue happens locally without that (you can clone the repo, npm install, and npm run dev to see and test that).

Decided to keep the Rust/WASM build pipeline out of the repo since adding that in complicates things and I don't think there's anything really to change there (i.e. the JS loads the .wasm and doesn't need to know about what it does)

The only difference on the code side I see at a glance, in terms of the JS/WASM divide, is that in this demo I create the canvas element on the JS side and pass it to wasm, whereas in the official webgl reference above the canvas element is created on the wasm side too.

Any tips to get Spector working here are greatly appreciated! At a glance I think this means the issue might benefit from being re-opened until this is resolved, but there's a good chance I just missed something too ;)

Thanks!

dakom avatar Dec 21 '18 08:12 dakom

Oh I forgot to mention - the other difference is I'm using Typescript, so all the webpack bundling is going through that.

The .tsx extension is because in ideally I'd like to handle the UI on the JS side with React, though I didn't add that here for the sake of simplicity

dakom avatar Dec 21 '18 08:12 dakom

So it looks like the issue is not wasm but the bindings.

It uses javascript call on the context making it impossible to hook per context. In spector, we hook at the context instance level not the context prototype itself to not pollute the default prototype.

Let me see if I could do smthg for it.

sebavan avatar Dec 21 '18 13:12 sebavan

@dakom the use of call in the webgl bindings e.g. :

const __widl_f_draw_arrays_WebGLRenderingContext_target = typeof WebGLRenderingContext === 'undefined' ? null : WebGLRenderingContext.prototype.drawArrays || function() {
    throw new Error(`wasm-bindgen: WebGLRenderingContext.drawArrays does not exist`);
};

export function __widl_f_draw_arrays_WebGLRenderingContext(arg0, arg1, arg2, arg3) {
    __widl_f_draw_arrays_WebGLRenderingContext_target.call(getObject(arg0), arg1, arg2, arg3);
}

prevents spector to get notified of the functions calls. Basically as the calls go straight to the prototype, not through the context (which is the overriden one) Spector is not able to detect them.

I ll see during my holliday if I can do smthg for it but I remember that going through the prototype directly was creating weird security issues in the plugin.

I ll rename the issue accordingly.

Thanks a lot for the repro.

sebavan avatar Dec 21 '18 14:12 sebavan

Thanks for the insight and heads up! I'll be away today/tomorrow but back around on Sunday to try things out if it helps :)

dakom avatar Dec 21 '18 14:12 dakom

I have began to look into the issue and modifying the prototype of the rendering is a huge architecture change. I am trying to find another approach if possible. It will nevertheless take a bit of time to develop :-) sorry for the delay.

sebavan avatar Jan 07 '19 10:01 sebavan

Whups, didn't realize it would lead to such a massive change! Sorry to be the bearer of bad news. Really appreciate your taking the time to work on this... but no pressure!

fwiw I'm still just getting the hang of basics in wasm/Rust (almost have a textured quad on the screen, hehe) - not really up to the proper shader stuff yet ;)

Spector is an amazing tool and I can't imagine debugging without it :) I'm sure when the larger community catches on to wasm/webgl opportunities it'll be wonderful to have this fix ready and waiting :D

dakom avatar Jan 13 '19 21:01 dakom

Yup totally agree and thanks for the nice feedback, just need to find a bit more time to sort it out but will probably require a local install not just from the extension from what I can see now.

sebavan avatar Jan 13 '19 21:01 sebavan

It looks like hooking the entire context prototype is creating some issues. I am currently investigating a way to bypass some of the restrictions it created.

sebavan avatar Feb 15 '19 17:02 sebavan

Hello! I am trying to debug a rust / WASM webgl build, and I inevitably ran into this issue. I just tested both the extension and npm build and neither seemed to work. What is the status of this refactor? If I fork this repo, is there a work around I could use to intercept the calls from WASM? Any bit of help or direction you could offer would be much appreciated!

Also, thank you again for all your work on this, it's been a pleasure to use in many other projects.

emistorrs avatar Apr 23 '21 13:04 emistorrs

Unfortunately no easy way to bypass those issues :-( you could try to change the way we do https://github.com/BabylonJS/Spector.js/blob/master/src/backend/spies/commandSpy.ts#L102

Basically the way we execute the original command from the context. Totally open for any PRs here.

sebavan avatar Apr 23 '21 21:04 sebavan

Thank you so much for the info! Fortunately we were able to resolve the problem, (a NaN snuck into our simulation) but in general we are very interested in using spector to help us with our project. So we'll take a look at this and see what we can do.

emistorrs avatar Apr 27 '21 15:04 emistorrs

Closing for now, feel free to reopen if you make a PR

sebavan avatar Jan 18 '22 02:01 sebavan