Skip to content

Commit bb40d26

Browse files
PaulHaxsankhesh
authored andcommitted
feat(RenderWindow): cache cull-face mode and route raw calls through it
Extend the vtkOpenGLRenderWindow cull cache to mirror vtkOpenGLState in VTK C++: alongside the existing cullFaceEnabled flag, add cullFaceMode and a setCullFaceMode method that is no-op-on-match. Migrate the four raw gl.cullFace(X) call sites in PolyDataMapper, Glyph3DMapper, SurfaceLICMapper, and PolyDataMapper2D, as well as the raw gl.enable/gl.disable(CULL_FACE) call sites in SurfaceLICMapper's popState and post-LIC cleanup, and the raw restore in OrderIndependentTranslucentPass. After this commit the cache fully tracks cull state, so a stale cache cannot silently no-op a later cull write against a divergent real GL state. Pure infrastructure: no behavioral change.
1 parent 86d5a6a commit bb40d26

7 files changed

Lines changed: 31 additions & 10 deletions

File tree

Sources/Rendering/OpenGL/Glyph3DMapper/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ function vtkOpenGLGlyph3DMapper(publicAPI, model) {
7777
model._openGLRenderWindow.disableCullFace();
7878
} else if (frontfaceCulling) {
7979
model._openGLRenderWindow.enableCullFace();
80-
gl.cullFace(gl.FRONT);
80+
model._openGLRenderWindow.setCullFaceMode(gl.FRONT);
8181
} else {
8282
model._openGLRenderWindow.enableCullFace();
83-
gl.cullFace(gl.BACK);
83+
model._openGLRenderWindow.setCullFaceMode(gl.BACK);
8484
}
8585

8686
publicAPI.renderPieceStart(ren, actor);

Sources/Rendering/OpenGL/OrderIndependentTranslucentPass/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ function vtkOpenGLOrderIndependentTranslucentPass(publicAPI, model) {
333333
gl.depthFunc(gl.LEQUAL);
334334
if (cullFaceEnabled) {
335335
viewNode.enableCullFace();
336-
gl.cullFace(cullFaceMode);
336+
viewNode.setCullFaceMode(cullFaceMode);
337337
}
338338
model.translucentRGBATexture.deactivate();
339339
model.translucentRTexture.deactivate();

Sources/Rendering/OpenGL/PolyDataMapper/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,10 +1743,10 @@ function vtkOpenGLPolyDataMapper(publicAPI, model) {
17431743
model._openGLRenderWindow.disableCullFace();
17441744
} else if (frontfaceCulling) {
17451745
model._openGLRenderWindow.enableCullFace();
1746-
gl.cullFace(gl.FRONT);
1746+
model._openGLRenderWindow.setCullFaceMode(gl.FRONT);
17471747
} else {
17481748
model._openGLRenderWindow.enableCullFace();
1749-
gl.cullFace(gl.BACK);
1749+
model._openGLRenderWindow.setCullFaceMode(gl.BACK);
17501750
}
17511751

17521752
publicAPI.renderPieceStart(ren, actor);

Sources/Rendering/OpenGL/PolyDataMapper2D/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function vtkOpenGLPolyDataMapper2D(publicAPI, model) {
9696
// cull back face to avoid double drawing
9797
const gl = model.context;
9898
model._openGLRenderWindow.enableCullFace();
99-
gl.cullFace(gl.BACK);
99+
model._openGLRenderWindow.setCullFaceMode(gl.BACK);
100100

101101
publicAPI.renderPieceStart(ren, actor);
102102
publicAPI.renderPieceDraw(ren, actor);

Sources/Rendering/OpenGL/RenderWindow/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import vtkViewStream from '../../../IO/Core/ImageStream/ViewStream';
1616
*/
1717
export interface IOpenGLRenderWindowInitialValues {
1818
cullFaceEnabled?: boolean;
19+
cullFaceMode?: number | null;
1920
shaderCache?: null;
2021
initialized?: boolean;
2122
context?: WebGLRenderingContext | WebGL2RenderingContext;
@@ -376,6 +377,13 @@ export interface vtkOpenGLRenderWindow extends vtkViewNode {
376377
*/
377378
enableCullFace(): void;
378379

380+
/**
381+
* Set the cull face mode (gl.FRONT, gl.BACK, or gl.FRONT_AND_BACK).
382+
* Caches the value to avoid redundant gl.cullFace() calls.
383+
* @param mode A WebGL cull face mode constant.
384+
*/
385+
setCullFaceMode(mode: number): void;
386+
379387
/**
380388
*
381389
* @param {vtkViewStream} stream The vtkViewStream instance.

Sources/Rendering/OpenGL/RenderWindow/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const parentMethodsToProxy = [
2727
'deactivateTexture',
2828
'disableCullFace',
2929
'enableCullFace',
30+
'setCullFaceMode',
3031
'get3DContext',
3132
'getActiveFramebuffer',
3233
'getContext',
@@ -1143,6 +1144,13 @@ function vtkOpenGLRenderWindow(publicAPI, model) {
11431144
}
11441145
};
11451146

1147+
publicAPI.setCullFaceMode = (mode) => {
1148+
if (model.cullFaceMode !== mode) {
1149+
model.context.cullFace(mode);
1150+
model.cullFaceMode = mode;
1151+
}
1152+
};
1153+
11461154
publicAPI.setViewStream = (stream) => {
11471155
if (model.viewStream === stream) {
11481156
return false;
@@ -1302,6 +1310,7 @@ function vtkOpenGLRenderWindow(publicAPI, model) {
13021310

13031311
const DEFAULT_VALUES = {
13041312
cullFaceEnabled: false,
1313+
cullFaceMode: null,
13051314
shaderCache: null,
13061315
initialized: false,
13071316
context: null,

Sources/Rendering/OpenGL/SurfaceLIC/SurfaceLICMapper/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ function vtkOpenGLSurfaceLICMapper(publicAPI, model) {
134134
apply(gl.BLEND);
135135
apply(gl.DEPTH_TEST);
136136
apply(gl.SCISSOR_TEST);
137-
apply(gl.CULL_FACE);
137+
if (model.stateCache[gl.CULL_FACE]) {
138+
model._openGLRenderWindow.enableCullFace();
139+
} else {
140+
model._openGLRenderWindow.disableCullFace();
141+
}
138142
};
139143

140144
publicAPI.renderPiece = (ren, actor) => {
@@ -211,10 +215,10 @@ function vtkOpenGLSurfaceLICMapper(publicAPI, model) {
211215
model._openGLRenderWindow.disableCullFace();
212216
} else if (frontfaceCulling) {
213217
model._openGLRenderWindow.enableCullFace();
214-
gl.cullFace(gl.FRONT);
218+
model._openGLRenderWindow.setCullFaceMode(gl.FRONT);
215219
} else {
216220
model._openGLRenderWindow.enableCullFace();
217-
gl.cullFace(gl.BACK);
221+
model._openGLRenderWindow.setCullFaceMode(gl.BACK);
218222
}
219223

220224
const windowSize = model._openGLRenderWindow.getSize();
@@ -241,7 +245,7 @@ function vtkOpenGLSurfaceLICMapper(publicAPI, model) {
241245
publicAPI.pushState(model.context);
242246
model.VBOBuildTime.modified();
243247
model.openGLLicInterface.completedGeometry();
244-
model.context.disable(model.context.CULL_FACE);
248+
model._openGLRenderWindow.disableCullFace();
245249
model.openGLLicInterface.applyLIC();
246250
model.openGLLicInterface.combineColorsAndLIC();
247251
model.openGLLicInterface.copyToScreen(windowSize);

0 commit comments

Comments
 (0)