Closed almarklein closed 1 week ago
BTW: a context being able to present to the screen means that it can make the result appear on screen based only on the window id.
Re opengl: I think its possible to create an OpenGL context that way, So if someone was to create an OpenGLContext that follows our rules, people can use that, and we don't have to care.
Reading this the next morning. It's rather long, so let's try to summarize it:
Rendercanvas looks independent on wgpu, but right now you can only really render to it with wgpu. I propose to enable other contexts to present to it as well, by formalizing (and tweaking) the API between the canvas and the context. Contexts must support the render-methods that we already have, so the changes are quite minimal. The effect is that other rendering tools can use rendercanvas
as well.
Intro
Our
canvas.get_context()
always returns aWgpuCanvasContext
. I think we can release this a bit, opening options for other tools that want to render to a canvas, without adding much complexity on our end. I did some thinking and experimenting today. In this issue I try to stick to the high-level ideas; I have a PR underway with implementation.Rendering vs presenting
Let's get some terms straight. The context helps the user-code to render to a canvas. The context presents the result to the canvas. From the p.o.v. of RenderCanvas, we don't care what API the context provides the user with. We only care that the context presents the result in a way that the canvas supports.
Some thoughts / observations / statements
rendercanvas
should be mostly agnostic about wgpu.get_context()
in WebGPU is becauseget_context()
already exists for webgl, 2D api, etc. Strictly speaking I don't mind using a completely different API here, but in the end I think this mechanic/abstraction actually works well for our case.rendercanvas
cannot provide the context itself, but must load it from another library.Proposal
To get a canvas:
canvas.get_contex(context_type)
to allow selecting a context:canvas.get_contex("wgpu")
orcanvas.get_contex("something_else")
context_type
can be "some.module.name", and if that module containscontext_from_rendercanvas_hook()
, we call it to get the context.canvas.get_contex("bitmap")
that mimics this. This offers a really simple way for people to e.g. make small games using a numpy array. Simply fun! Also allows creating working examples that don't require tons of lines for wgpu ;)The contract between the context and the canvas:
present_capabilities
, a dict with info on what methods of presentation the canvas supports.present()
, that returns a little dict that specifies what present method was used, and possibly the data corresponding to the present method.Future: