Skip to content

Shared WebGL context render window#3453

Open
PaulHax wants to merge 2 commits intoKitware:masterfrom
PaulHax:shared-render-window
Open

Shared WebGL context render window#3453
PaulHax wants to merge 2 commits intoKitware:masterfrom
PaulHax:shared-render-window

Conversation

@PaulHax
Copy link
Collaborator

@PaulHax PaulHax commented Mar 26, 2026

Are folks intersted in putting this in the vtk.js repo? Its an uncommon use case. We could get away with simply doing something to keep vtkOpenGLRenderWindow from resizing the canvas and put SharedRenderWindow in another repo/package.

Context

This PR adds an OpenGL shared-context rendering path for vtk.js so it can render into a WebGL2RenderingContext owned by another library, instead of always creating and managing its own canvas/context.

The immediate use case is interleaving vtk.js content with host renderers such as MapLibre custom layers, including depth-tested rendering into the host scene. vtk.js also needs an explicit way to opt out of canvas DOM management when the canvas is externally owned.

Results

Before this change:

  • vtkOpenGLRenderWindow assumed ownership of the canvas and could resize/restyle it.
  • vtk.js did not provide a supported API for rendering into a host-owned external WebGL context.

After this change:

  • vtk.js can render into a host-owned WebGL2 context through vtkSharedRenderWindow.createFromContext(canvas, gl).
  • Canvas management opt-outable via manageCanvas on vtkOpenGLRenderWindow.
  • A new MapLibre shared-context example demonstrates vtk.js geometry rendered into the map's WebGL context, including terrain-backed depth interaction.
  • New tests cover the shared render-window path, host framebuffer rendering, host state recovery after vtk rendering, and unsupported-context rejection.

Changes

  • Added manageCanvas to vtkOpenGLRenderWindow (default true) so externally owned canvases can opt out of vtk.js canvas sizing/styling.

  • Added vtkSharedRenderWindow for rendering into an externally provided WebGL2RenderingContext.

  • Added vtkSharedRenderer for the shared-context rendering path.

  • Added vtkSharedRenderWindow.createFromContext(canvas, gl, options) for creating a shared render window from a host-owned canvas/context.

  • Added shared-rendering options for automatic clearing control (autoClear, autoClearColor, autoClearDepth).

  • Scoped the vtkSharedRenderer override to the shared render-window instance rather than changing the global OpenGL view-node factory. This avoids leaking shared-context behavior into unrelated normal OpenGL render windows.

  • Added a MapLibre example showing shared-context rendering on terrain using vtk.js cones, spheres, and a connecting line.

  • Documentation and TypeScript definitions were updated to match those changes

PR and Code Checklist

  • semantic-release commit messages
  • Run npm run reformat to have correctly formatted code

Testing

npm run example -- SharedContext

  • This change adds or fixes unit tests
  • Tested environment:
    • OS: Linux
    • Browser: Chrome
image

@PaulHax PaulHax force-pushed the shared-render-window branch from 5f88757 to 93f4a3a Compare March 26, 2026 03:25
@PaulHax PaulHax force-pushed the shared-render-window branch from 93f4a3a to 5e23b91 Compare March 26, 2026 03:29
Copy link
Member

@finetjul finetjul left a comment

Choose a reason for hiding this comment

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

@sankhesh I do not have any opinion on the approach, I leave it to you.

publicAPI.registerOverride = (className, fn) => {
model.overrides[className] = fn;
};

Copy link
Member

Choose a reason for hiding this comment

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

method definition should be moved in vtkOpenGLViewNodeFactory()

@daker
Copy link
Collaborator

daker commented Mar 26, 2026

@sedghi do you have an opnion on this ?

@finetjul finetjul requested a review from sankhesh March 26, 2026 15:39
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.

3 participants