yglukhov / nimx

GUI library
MIT License
1.09k stars 75 forks source link

Remove global graphic contexts. Fix windows for js backend. #482

Open quantimnot opened 3 years ago

quantimnot commented 3 years ago

I've been trying to get nimxapp and nimxedit to fully function for the js backend. While debugging the layout examples I learned the graphics context was globally shared and that is not supported by the webgl spec.

This pull request is a possible solution.

I consolidated relative global variables into context.GraphicsContext and fonts.FontContext. I then set the context to the window and ensured all views had their parent window initialized in their init method. But then I learned that the initialization state of a view's window property was being used elsewhere and my changes broke that. I didn't understand the code well enough to alter where View.window was being evaluated so I changed my approach. I instead created a View.gfx property that was initialized early on each view. The graphics context is then passed into each func/proc/... where it is needed.

State:

TODO:

I've only been testing the js and sdl backends. The others may not currently work.

Please give it a try and let me know:

quantimnot commented 2 years ago

Hey @yglukhov, I got side-tracked coming up with a CI for testing each target platform. I would like to contribute more to your project. Do you have any feedback about the core of this PR? I don't want to spend time on it if you're not interested. The core of this PR is to stop using global variables. I can refactor Rod too.

yglukhov commented 2 years ago

Hey @quantimnot, thanks for your interest in this. Your PR made me spend some time on researching the topic, and sadly this is a problem indeed. Unfortunately I don't see a good solution to this. Not only does it make the API more complex, but it introduces lots of additional questions e.g. resource management. Take a nimx.Image for instance. It implicitly and lazily manages a texture, created in the default (and single one) context. What if we want to draw an Image (or vertex buffer or any other resource) in the context it wasn't created in? Such ability would require to either (a) redesign image api from the ground up, so that the user is always aware of the Image's context, or (b) to add more logic for the image to have ability to belong to multiple contexts under the hood, which sounds pretty involved.

So my biggest concern is that a lot of effort has to be taken, likely a lot of API broken, for the possibility to support multiple webgl contexts, which feels like little gain to me. Maybe it's better for nimx to support only one webgl context, I think it would be a more reasonable limitation.