Skip to content

Commit 3b61b5e

Browse files
bruyeretfinetjul
authored andcommitted
refactor(VolumeMapper): clean volume mapper code
Factorize getNeedToRebuildShader, remove unused variables
1 parent bfa31f7 commit 3b61b5e

File tree

2 files changed

+74
-80
lines changed

2 files changed

+74
-80
lines changed

Diff for: Sources/Rendering/OpenGL/VolumeMapper/index.js

+73-74
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,8 @@ const { vtkWarningMacro, vtkErrorMacro } = macro;
3535
// helper methods
3636
// ----------------------------------------------------------------------------
3737

38-
function computeFnToString(property, pwfun, numberOfComponents) {
39-
if (pwfun) {
40-
const iComps = property.getIndependentComponents();
41-
return `${pwfun.getMTime()}-${iComps}-${numberOfComponents}`;
42-
}
43-
return '0';
38+
function computeFnToString(pwfun, useIComps, numberOfComponents) {
39+
return pwfun ? `${pwfun.getMTime()}-${useIComps}-${numberOfComponents}` : '0';
4440
}
4541

4642
function getColorCodeFromPreset(colorMixPreset) {
@@ -182,6 +178,17 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
182178
shaders.Geometry = '';
183179
};
184180

181+
publicAPI.useIndependentComponents = (actor) => {
182+
const iComps = actor.getProperty().getIndependentComponents();
183+
const image = model.currentInput;
184+
const numComp = image
185+
?.getPointData()
186+
?.getScalars()
187+
?.getNumberOfComponents();
188+
const colorMixPreset = actor.getProperty().getColorMixPreset();
189+
return (iComps && numComp >= 2) || !!colorMixPreset;
190+
};
191+
185192
publicAPI.replaceShaderValues = (shaders, ren, actor) => {
186193
let FSSource = shaders.Fragment;
187194

@@ -211,12 +218,12 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
211218
`#define vtkNumComponents ${numComp}`
212219
).result;
213220

214-
const iComps = actor.getProperty().getIndependentComponents();
215-
if (iComps) {
221+
const useIndependentComps = publicAPI.useIndependentComponents(actor);
222+
if (useIndependentComps) {
216223
FSSource = vtkShaderProgram.substitute(
217224
FSSource,
218225
'//VTK::IndependentComponentsOn',
219-
'#define vtkIndependentComponentsOn'
226+
'#define UseIndependentComponents'
220227
).result;
221228

222229
// Define any proportional components
@@ -282,11 +289,11 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
282289
FSSource = vtkShaderProgram.substitute(
283290
FSSource,
284291
'//VTK::LightComplexity',
285-
`#define vtkLightComplexity ${model.lastLightComplexity}`
292+
`#define vtkLightComplexity ${model.lightComplexity}`
286293
).result;
287294

288295
// set shadow blending flag
289-
if (model.lastLightComplexity > 0) {
296+
if (model.lightComplexity > 0) {
290297
if (model.renderable.getVolumetricScatteringBlending() > 0.0) {
291298
FSSource = vtkShaderProgram.substitute(
292299
FSSource,
@@ -314,11 +321,10 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
314321
}
315322

316323
// if using gradient opacity define that
317-
model.gopacity = actor.getProperty().getUseGradientOpacity(0);
318-
for (let nc = 1; iComps && !model.gopacity && nc < numComp; ++nc) {
319-
if (actor.getProperty().getUseGradientOpacity(nc)) {
320-
model.gopacity = true;
321-
}
324+
const numIComps = useIndependentComps ? numComp : 1;
325+
model.gopacity = false;
326+
for (let nc = 0; !model.gopacity && nc < numIComps; ++nc) {
327+
model.gopacity ||= actor.getProperty().getUseGradientOpacity(nc);
322328
}
323329
if (model.gopacity) {
324330
FSSource = vtkShaderProgram.substitute(
@@ -371,7 +377,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
371377
};
372378

373379
publicAPI.replaceShaderLight = (shaders, ren, actor) => {
374-
if (model.lastLightComplexity === 0) {
380+
if (model.lightComplexity === 0) {
375381
return;
376382
}
377383
let FSSource = shaders.Fragment;
@@ -401,7 +407,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
401407
false
402408
).result;
403409
// support any number of lights
404-
if (model.lastLightComplexity === 3) {
410+
if (model.lightComplexity === 3) {
405411
FSSource = vtkShaderProgram.substitute(
406412
FSSource,
407413
'//VTK::Light::Dec',
@@ -492,7 +498,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
492498
shaders.Fragment = FSSource;
493499
};
494500

495-
publicAPI.getNeedToRebuildShaders = (cellBO, ren, actor) => {
501+
const recomputeLightComplexity = (actor, lights) => {
496502
// do we need lighting?
497503
let lightComplexity = 0;
498504
if (
@@ -504,7 +510,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
504510
lightComplexity = 0;
505511
model.numberOfLights = 0;
506512

507-
ren.getLights().forEach((light) => {
513+
lights.forEach((light) => {
508514
const status = light.getSwitch();
509515
if (status > 0) {
510516
model.numberOfLights++;
@@ -526,26 +532,22 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
526532
}
527533
});
528534
}
529-
530-
let needRebuild = false;
531-
if (model.lastLightComplexity !== lightComplexity) {
532-
model.lastLightComplexity = lightComplexity;
533-
needRebuild = true;
535+
if (lightComplexity !== model.lightComplexity) {
536+
model.lightComplexity = lightComplexity;
537+
publicAPI.modified();
534538
}
539+
};
535540

536-
const numComp = model.scalarTexture.getComponents();
537-
const iComps = actor.getProperty().getIndependentComponents();
538-
let usesProportionalComponents = false;
539-
const proportionalComponents = [];
540-
if (iComps) {
541-
// Define any proportional components
542-
for (let nc = 0; nc < numComp; nc++) {
543-
proportionalComponents.push(actor.getProperty().getOpacityMode(nc));
544-
}
541+
publicAPI.getNeedToRebuildShaders = (cellBO, ren, actor) => {
542+
const actorProps = actor.getProperty();
545543

546-
if (proportionalComponents.length > 0) {
547-
usesProportionalComponents = true;
548-
}
544+
recomputeLightComplexity(actor, ren.getLights());
545+
546+
const numComp = model.scalarTexture.getComponents();
547+
const iComps = actorProps.getIndependentComponents();
548+
const opacityModes = [];
549+
for (let nc = 0; nc < numComp; nc++) {
550+
opacityModes.push(actorProps.getOpacityMode(nc));
549551
}
550552

551553
const ext = model.currentInput.getSpatialExtent();
@@ -561,46 +563,38 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
561563
const maxSamples =
562564
vec3.length(vsize) / publicAPI.getCurrentSampleDistance(ren);
563565

566+
const hasZBufferTexture = !!model.zBufferTexture;
567+
564568
const state = {
565-
colorMixPreset: actor.getProperty().getColorMixPreset(),
566-
interpolationType: actor.getProperty().getInterpolationType(),
567-
useLabelOutline: actor.getProperty().getUseLabelOutline(),
569+
colorMixPreset: actorProps.getColorMixPreset(),
570+
interpolationType: actorProps.getInterpolationType(),
571+
useLabelOutline: actorProps.getUseLabelOutline(),
568572
numComp,
569-
usesProportionalComponents,
570573
iComps,
571574
maxSamples,
572-
useGradientOpacity: actor.getProperty().getUseGradientOpacity(0),
575+
useGradientOpacity: actorProps.getUseGradientOpacity(0),
573576
blendMode: model.renderable.getBlendMode(),
574-
proportionalComponents,
577+
hasZBufferTexture,
578+
opacityModes,
575579
};
576580

577-
// We only need to rebuild the shader if one of these variables has changed,
581+
// We need to rebuild the shader if one of these variables has changed,
578582
// since they are used in the shader template replacement step.
579-
if (!model.previousState || !DeepEqual(model.previousState, state)) {
580-
model.previousState = state;
581-
582-
return true;
583-
}
584-
585-
// has something changed that would require us to recreate the shader?
583+
// We also need to rebuild if the shader source time is outdated.
586584
if (
587585
cellBO.getProgram()?.getHandle() === 0 ||
588-
needRebuild ||
589-
model.lastHaveSeenDepthRequest !== model.haveSeenDepthRequest ||
590-
!!model.lastZBufferTexture !== !!model.zBufferTexture ||
591586
cellBO.getShaderSourceTime().getMTime() < publicAPI.getMTime() ||
592-
cellBO.getShaderSourceTime().getMTime() < model.renderable.getMTime()
587+
cellBO.getShaderSourceTime().getMTime() < model.renderable.getMTime() ||
588+
!model.previousState ||
589+
!DeepEqual(model.previousState, state)
593590
) {
594-
model.lastZBufferTexture = model.zBufferTexture;
591+
model.previousState = state;
595592
return true;
596593
}
597-
598594
return false;
599595
};
600596

601597
publicAPI.updateShaders = (cellBO, ren, actor) => {
602-
model.lastBoundBO = cellBO;
603-
604598
// has something changed that would require us to recreate the shader?
605599
if (publicAPI.getNeedToRebuildShaders(cellBO, ren, actor)) {
606600
const shaders = { Vertex: null, Fragment: null, Geometry: null };
@@ -931,7 +925,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
931925
program.setUniformMatrix('PCVCMatrix', model.projectionToView);
932926

933927
// handle lighting values
934-
if (model.lastLightComplexity === 0) {
928+
if (model.lightComplexity === 0) {
935929
return;
936930
}
937931
let lightNum = 0;
@@ -966,7 +960,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
966960
program.setUniform3fv('lightDirectionVC', lightDir);
967961
program.setUniform3fv('lightHalfAngleVC', halfAngle);
968962

969-
if (model.lastLightComplexity === 3) {
963+
if (model.lightComplexity === 3) {
970964
lightNum = 0;
971965
const lightPositionVC = [];
972966
const lightAttenuation = [];
@@ -1051,8 +1045,8 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
10511045

10521046
// set the component mix when independent
10531047
const numComp = model.scalarTexture.getComponents();
1054-
const iComps = actor.getProperty().getIndependentComponents();
1055-
if (iComps && numComp >= 2) {
1048+
const useIndependentComps = publicAPI.useIndependentComponents(actor);
1049+
if (useIndependentComps) {
10561050
for (let i = 0; i < numComp; i++) {
10571051
program.setUniformf(
10581052
`mix${i}`,
@@ -1064,7 +1058,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
10641058
// three levels of shift scale combined into one
10651059
// for performance in the fragment shader
10661060
for (let i = 0; i < numComp; i++) {
1067-
const target = iComps ? i : 0;
1061+
const target = useIndependentComps ? i : 0;
10681062
const sscale = volInfo.scale[i];
10691063
const ofun = vprop.getScalarOpacity(target);
10701064
const oRange = ofun.getRange();
@@ -1082,7 +1076,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
10821076
}
10831077

10841078
if (model.gopacity) {
1085-
if (iComps) {
1079+
if (useIndependentComps) {
10861080
for (let nc = 0; nc < numComp; ++nc) {
10871081
const sscale = volInfo.scale[nc];
10881082
const useGO = vprop.getUseGradientOpacity(nc);
@@ -1138,7 +1132,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
11381132
program.setUniformf('outlineOpacity', labelOutlineOpacity);
11391133
}
11401134

1141-
if (model.lastLightComplexity > 0) {
1135+
if (model.lightComplexity > 0) {
11421136
program.setUniformf('vAmbient', vprop.getAmbient());
11431137
program.setUniformf('vDiffuse', vprop.getDiffuse());
11441138
program.setUniformf('vSpecular', vprop.getSpecular());
@@ -1312,9 +1306,6 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
13121306
model.scalarTexture.setMagnificationFilter(Filter.LINEAR);
13131307
}
13141308

1315-
// Bind the OpenGL, this is shared between the different primitive/cell types.
1316-
model.lastBoundBO = null;
1317-
13181309
// if we have a zbuffer texture then activate it
13191310
if (model.zBufferTexture !== null) {
13201311
model.zBufferTexture.activate();
@@ -1519,13 +1510,17 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
15191510
}
15201511

15211512
const numComp = scalars.getNumberOfComponents();
1522-
const iComps = vprop.getIndependentComponents();
1523-
const numIComps = iComps ? numComp : 1;
1513+
const useIndependentComps = publicAPI.useIndependentComponents(actor);
1514+
const numIComps = useIndependentComps ? numComp : 1;
15241515

15251516
const scalarOpacityFunc = vprop.getScalarOpacity();
15261517
const opTex =
15271518
model._openGLRenderWindow.getGraphicsResourceForObject(scalarOpacityFunc);
1528-
let toString = computeFnToString(vprop, scalarOpacityFunc, numIComps);
1519+
let toString = computeFnToString(
1520+
scalarOpacityFunc,
1521+
useIndependentComps,
1522+
numIComps
1523+
);
15291524
const reBuildOp =
15301525
!opTex.vtkObj ||
15311526
opTex.hash !== toString ||
@@ -1602,7 +1597,11 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
16021597

16031598
// rebuild color tfun?
16041599
const colorTransferFunc = vprop.getRGBTransferFunction();
1605-
toString = computeFnToString(vprop, colorTransferFunc, numIComps);
1600+
toString = computeFnToString(
1601+
colorTransferFunc,
1602+
useIndependentComps,
1603+
numIComps
1604+
);
16061605
const cTex =
16071606
model._openGLRenderWindow.getGraphicsResourceForObject(colorTransferFunc);
16081607
const reBuildC =
@@ -1841,7 +1840,7 @@ const DEFAULT_VALUES = {
18411840
targetXYF: 1.0,
18421841
zBufferTexture: null,
18431842
lastZBufferTexture: null,
1844-
lastLightComplexity: 0,
1843+
lightComplexity: 0,
18451844
fullViewportTime: 1.0,
18461845
idxToView: null,
18471846
idxNormalMatrix: null,

Diff for: Sources/Rendering/OpenGL/glsl/vtkVolumeFS.glsl

+1-6
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,12 @@ varying vec3 vertexVCVSOutput;
3030
// possibly define vtkTrilinearOn
3131
//VTK::TrilinearOn
3232

33-
// possibly define vtkIndependentComponents
33+
// possibly define UseIndependentComponents
3434
//VTK::IndependentComponentsOn
3535

3636
// possibly define vtkCustomComponentsColorMix
3737
//VTK::CustomComponentsColorMixOn
3838

39-
// possibly define if independent components are actually used
40-
#if (defined(vtkIndependentComponentsOn) && vtkNumComponents >= 2) || defined(vtkCustomComponentsColorMix)
41-
#define UseIndependentComponents
42-
#endif
43-
4439
// possibly define any "proportional" components
4540
//VTK::vtkProportionalComponents
4641

0 commit comments

Comments
 (0)