Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webgl): Pass through canvas in WebGLDevice.attach() #1931

Merged
merged 5 commits into from
Jan 23, 2024

Conversation

felixpalmer
Copy link
Collaborator

@felixpalmer felixpalmer commented Jan 22, 2024

Background

When attaching to an existing WebGL context WebGLDevice.attach() doesn't pass through the canvas, leading to new WebGLDevice() to create another canvas, even if the WebGL context already has one (as is commonly the case). For example affects: visgl/deck.gl#8442

Change List

  • Pass through canvas in WebGLDevice.attach()
  • Handle resizing of external canvas

@ibgreen
Copy link
Collaborator

ibgreen commented Jan 22, 2024

Can you use gl.canvas? I mean why pass it in if you already have the context?

@felixpalmer
Copy link
Collaborator Author

@ibgreen I'm not sure where you mean exactly by "use", I've pushed a change that uses gl.canvas in the constructor instead.

In general I find it a bit strange that we accept both gl and canvas in DeviceProps, is there ever a case where we would have one and not the other?

Copy link
Collaborator

@ibgreen ibgreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure where you mean exactly by "use", I've pushed a change that uses gl.canvas in the constructor instead.

Yes... maybe my comment was a bit tardy, I was on a flight.

In general I find it a bit strange that we accept both gl and canvas in DeviceProps, is there ever a case where we would have one and not the other?

The idea is that you can

  • provide a canvas and the device creates a context for that canvas
  • provide a gl context and the Device wraps that context. In this case there is only one canvas that can be used, the canvas that this context was created for.

@@ -52,6 +53,8 @@ export class WebGLCanvasContext extends CanvasContext {
* See http://webgl2fundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
*/
resize(options?: {width?: number; height?: number; useDevicePixels?: boolean | number}): void {
if (!this.device.gl) return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be possible (i.e. a WebGLDevive should always have a gl context), unless this is some mid-initialization issue with function being called before Device is full constructed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, is it due to the initialization sequence. Creating a new WebGLCanvasContext invokes update() and thus resize(), which expects there to be a gl context present on the device. Later the creation of the gl context can depend on a canvasContext already being available, so there is a circular dependency.

Merging this for now, and will try to followup with a better solution

@@ -192,7 +192,8 @@ ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContex
}

// Create and instrument context
this.canvasContext = new WebGLCanvasContext(this, props);
const canvas = props.canvas || props.gl?.canvas;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might even change the order here, if there is a gl context it should not be possible to override the canvas. Or even throw an error

@felixpalmer felixpalmer merged commit dbc3908 into master Jan 23, 2024
2 checks passed
@felixpalmer felixpalmer deleted the felix/webgl-attach-canvas branch January 23, 2024 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants