Skip to content

feat(rendering): Introduce WebGLContextPool for parallel rendering in ContextPoolRenderingEngine - #2190

Merged
IbrahimCSAE merged 15 commits into
mainfrom
feat/renderingEngine-next
Jul 11, 2025
Merged

feat(rendering): Introduce WebGLContextPool for parallel rendering in ContextPoolRenderingEngine#2190
IbrahimCSAE merged 15 commits into
mainfrom
feat/renderingEngine-next

Conversation

@IbrahimCSAE

@IbrahimCSAE IbrahimCSAE commented Jul 10, 2025

Copy link
Copy Markdown
Member

Overview

This PR enhances the ContextPoolRenderingEngine (previously SequentialRenderingEngine) by introducing a WebGLContextPool class that enables parallel rendering across multiple WebGL contexts. This optimization significantly improves performance when rendering multiple viewports simultaneously while maintaining the core benefits of sequential rendering.

WebGL Context Pooling

Following the successful resolution of canvas size limitations in PR #2181, this PR takes the next step in performance optimization. While the sequential approach solved critical issues with viewport cropping and performance degradation on high-resolution displays, it rendered viewports one at a time. This new implementation introduces controlled parallelism to boost performance further.

Key Features

WebGLContextPool

The new WebGLContextPool class manages multiple WebGL contexts (default: 8) that can render viewports in parallel:

  • Configurable Context Count: Users can specify the number of WebGL contexts via webGlContextCount in the init configuration
  • Smart Distribution: Stack viewports are distributed across available contexts using round-robin assignment
  • Volume Viewport Limitation: Volume viewports continue to use a single context (index 0) due to WebGL resource sharing constraints
  • Safe Defaults: The default of 8 contexts provides excellent performance while staying well within browser limits (~16 contexts per tab)

Architecture

mermaid-diagram-2025-07-10-110004

Performance Benefits

Multiple stack viewports can now render simultaneously across different WebGL contexts, and significant performance improvement for sychronized operations, eg cine, synced scrolling.

Implementation Details

Configuration

Users can configure the number of WebGL contexts during initialization:

import { init } from '@cornerstonejs/core';

init({
  rendering: {
    webGlContextCount: 8,        // Number of WebGL contexts (default: 8)
  },
});

Context Assignment Strategy

  • Volume Viewports: Always assigned to context 0 (no parallelism due to resource sharing constraints between WebGL contexts)
  • Stack Viewports: Distributed across all available contexts using round-robin assignment

Important Notes

  1. Context Limit: While 8 is the safe default, users should not exceed this unless necessary. Browser limits are typically around 16 contexts per tab, but if your goal is to support as many devices as possible, it's best to stay well below this limit to avoid issues.

  2. Volume Viewport Limitation: Volume viewports cannot currently utilize parallel rendering due to WebGL resource sharing constraints between contexts. This is a current limitation of WebGL.

Example

https://marvelous-genie-f7fb6a.netlify.app/live-examples/webglcontextpooling

@sedghi sedghi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

add this point we need a doc in https://www.cornerstonejs.org/docs/concepts/cornerstone-core/renderingEngine
to talk about each RE

@IbrahimCSAE IbrahimCSAE changed the title wip (renderingEngine): rendering engine next feat(rendering): Introduce WebGLContextPool for parallel rendering in NextRenderingEngine Jul 10, 2025
@IbrahimCSAE
IbrahimCSAE requested a review from sedghi July 10, 2025 17:29
Comment thread packages/docs/docs/concepts/cornerstone-core/renderingEngine.md Outdated
Comment thread packages/docs/docs/concepts/cornerstone-core/renderingEngine.md Outdated
Comment thread packages/core/src/RenderingEngine/BaseRenderingEngine.ts Outdated
@IbrahimCSAE IbrahimCSAE changed the title feat(rendering): Introduce WebGLContextPool for parallel rendering in NextRenderingEngine feat(rendering): Introduce WebGLContextPool for parallel rendering in ContextPoolRenderingEngine Jul 10, 2025
@IbrahimCSAE

IbrahimCSAE commented Jul 10, 2025

Copy link
Copy Markdown
Member Author

Deploy preview: https://remarkable-torrone-d3cd0a.netlify.app/

@IbrahimCSAE
IbrahimCSAE requested a review from sedghi July 10, 2025 19:09

@sedghi sedghi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, add the extra tests we discussed, looks good

@IbrahimCSAE

Copy link
Copy Markdown
Member Author

I have tested all examples, and added multiple tests

@IbrahimCSAE
IbrahimCSAE merged commit dc8fb42 into main Jul 11, 2025
9 checks passed
@sedghi sedghi mentioned this pull request Aug 25, 2025
@sedghi sedghi mentioned this pull request Jun 4, 2026
3 tasks
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.

2 participants