Skip to content

Commit

Permalink
feat(RenderWindow): Add sharing of GPU resources accross render windows
Browse files Browse the repository at this point in the history
To share resources accross render windows, they need to share a common context
To do so, the render windows can now have a parent that will contain the shared context
The child render windows will proxy some methods of their parent to do the rendering
The content of the parent canvas is then copied to the child canvas using a 2D context
  • Loading branch information
bruyeret authored and finetjul committed Apr 11, 2024
1 parent 82e8728 commit f853aa4
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 33 deletions.
17 changes: 17 additions & 0 deletions Sources/Rendering/Core/RenderWindow/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface IRenderWindowInitialValues {
interactor?: any,
neverRendered?: boolean,
numberOfLayers?: number
childRenderWindows?: vtkRenderWindow[],
}

interface IStatistics {
Expand Down Expand Up @@ -42,6 +43,12 @@ export interface vtkRenderWindow extends vtkObject {
*/
addRenderer(renderer: vtkRenderer): void;

/**
* Add a child render window
* @param {vtkRenderWindow} renderWindow The vtkRenderWindow instance.
*/
addRenderWindow(renderWindow: vtkRenderWindow): void;

/**
* Add renderer
* @param view
Expand Down Expand Up @@ -87,6 +94,16 @@ export interface vtkRenderWindow extends vtkObject {
*/
getRenderersByReference(): vtkRenderer[];

/**
*
*/
getChildRenderWindows(): vtkRenderWindow[];

/**
*
*/
getChildRenderWindowsByReference(): vtkRenderWindow[];

/**
*
*/
Expand Down
12 changes: 11 additions & 1 deletion Sources/Rendering/Core/RenderWindow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ function vtkRenderWindow(publicAPI, model) {
)
.filter((i) => !!i);
};

publicAPI.addRenderWindow = (child) => {
if (model.childRenderWindows.includes(child)) {
return false;
}
model.childRenderWindows.push(child);
publicAPI.modified();
return true;
};
}

// ----------------------------------------------------------------------------
Expand All @@ -156,6 +165,7 @@ const DEFAULT_VALUES = {
interactor: null,
neverRendered: true,
numberOfLayers: 1,
childRenderWindows: [],
};

// ----------------------------------------------------------------------------
Expand All @@ -173,7 +183,7 @@ export function extend(publicAPI, model, initialValues = {}) {
'defaultViewAPI',
]);
macro.get(publicAPI, model, ['neverRendered']);
macro.getArray(publicAPI, model, ['renderers']);
macro.getArray(publicAPI, model, ['renderers', 'childRenderWindows']);
macro.moveToProtected(publicAPI, model, ['views']);
macro.event(publicAPI, model, 'completion');

Expand Down
47 changes: 38 additions & 9 deletions Sources/Rendering/OpenGL/RenderWindow/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface IOpenGLRenderWindowInitialValues {
shaderCache?: null;
initialized?: boolean;
context?: WebGLRenderingContext | WebGL2RenderingContext;
context2D?: CanvasRenderingContext2D;
canvas?: HTMLCanvasElement;
cursorVisibility?: boolean;
cursor?: string;
Expand All @@ -40,13 +41,6 @@ export interface ICaptureOptions {
scale?: number
}

export interface I3DContextOptions {
preserveDrawingBuffer?: boolean;
depth?: boolean;
alpha?: boolean;
powerPreference?: string;
}

type vtkOpenGLRenderWindowBase = vtkObject & Omit<vtkAlgorithm,
| 'getInputData'
| 'setInputData'
Expand Down Expand Up @@ -228,9 +222,44 @@ export interface vtkOpenGLRenderWindow extends vtkOpenGLRenderWindowBase {

/**
*
* @param {I3DContextOptions} options
* @param {WebGLContextAttributes} options
*/
get3DContext(options: WebGLContextAttributes): Nullable<WebGLRenderingContext>;

/**
*
* @param {CanvasRenderingContext2DSettings} options
*/
get2DContext(options: CanvasRenderingContext2DSettings): Nullable<CanvasRenderingContext2D>;

/**
* Copy the content of the root parent, if there is one, to the canvas
*/
copyParentContent(): void;

/**
* Resize this render window using the size of its children
* The new size of the renderwindow is the size of the bounding box
* containing all the child render windows
*/
resizeFromChildRenderWindows(): void;

/**
* Returns the last ancestor of type vtkOpenGLRenderWindow if there is one
* If there is no parent vtkOpenGLRenderWindow, returns undefined
*/
getRootOpenGLRenderWindow(): vtkOpenGLRenderWindow | undefined;

/**
* The context 2D is created during initialization instead of the WebGL context
* when there is a parent render window
*/
getContext2D(): CanvasRenderingContext2D | undefined;

/**
*
*/
get3DContext(options: I3DContextOptions): Nullable<WebGLRenderingContext>;
setContext2D(context2D: CanvasRenderingContext2D | undefined): boolean;

/**
*
Expand Down

0 comments on commit f853aa4

Please sign in to comment.