Skip to content

Commit 87d1180

Browse files
bruyeretfinetjul
authored andcommitted
refactor(shareGPU): Refactor sharing of GPU ressources
Made the code cleaner, by using a single map and meaningful names
1 parent 5f0ce1d commit 87d1180

File tree

5 files changed

+42
-45
lines changed

5 files changed

+42
-45
lines changed

Sources/Rendering/OpenGL/ImageMapper/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ function vtkOpenGLImageMapper(publicAPI, model) {
929929
model._openGLRenderWindow.getGraphicsResourceForObject(colorTransferFunc);
930930

931931
const reBuildC =
932-
!cTex?.vtkObj?.getHandle() ||
932+
!cTex?.oglObject?.getHandle() ||
933933
cTex?.hash !== cfunToString ||
934934
model.colorTextureString !== cfunToString;
935935
if (reBuildC) {
@@ -1002,7 +1002,7 @@ function vtkOpenGLImageMapper(publicAPI, model) {
10021002
);
10031003
}
10041004
} else {
1005-
model.colorTexture = cTex.vtkObj;
1005+
model.colorTexture = cTex.oglObject;
10061006
model.colorTextureString = cTex.hash;
10071007
}
10081008

@@ -1015,7 +1015,7 @@ function vtkOpenGLImageMapper(publicAPI, model) {
10151015
model._openGLRenderWindow.getGraphicsResourceForObject(pwFunc);
10161016
// rebuild opacity tfun?
10171017
const reBuildPwf =
1018-
!pwfTex?.vtkObj?.getHandle() ||
1018+
!pwfTex?.oglObject?.getHandle() ||
10191019
pwfTex?.hash !== pwfunToString ||
10201020
model.pwfTextureString !== pwfunToString;
10211021
if (reBuildPwf) {
@@ -1092,7 +1092,7 @@ function vtkOpenGLImageMapper(publicAPI, model) {
10921092
);
10931093
}
10941094
} else {
1095-
model.pwfTexture = pwfTex.vtkObj;
1095+
model.pwfTexture = pwfTex.oglObject;
10961096
model.pwfTextureString = pwfTex.hash;
10971097
}
10981098

@@ -1274,7 +1274,7 @@ function vtkOpenGLImageMapper(publicAPI, model) {
12741274

12751275
const tex =
12761276
model._openGLRenderWindow.getGraphicsResourceForObject(scalars);
1277-
if (!tex?.vtkObj?.getHandle()) {
1277+
if (!tex?.oglObject?.getHandle()) {
12781278
if (model._scalars !== scalars) {
12791279
model._openGLRenderWindow.releaseGraphicsResourcesForObject(
12801280
model._scalars
@@ -1296,7 +1296,7 @@ function vtkOpenGLImageMapper(publicAPI, model) {
12961296
model.VBOBuildString
12971297
);
12981298
} else {
1299-
model.openGLTexture = tex.vtkObj;
1299+
model.openGLTexture = tex.oglObject;
13001300
model.VBOBuildString = tex.hash;
13011301
}
13021302
model.openGLTexture.activate();
@@ -1362,7 +1362,7 @@ function vtkOpenGLImageMapper(publicAPI, model) {
13621362
const toString = `${labelOutlineThicknessArray.join('-')}`;
13631363

13641364
const reBuildL =
1365-
!lTex?.vtkObj?.getHandle() ||
1365+
!lTex?.oglObject?.getHandle() ||
13661366
lTex?.hash !== toString ||
13671367
model.labelOutlineThicknessTextureString !== toString;
13681368

@@ -1408,7 +1408,7 @@ function vtkOpenGLImageMapper(publicAPI, model) {
14081408
);
14091409
}
14101410
} else {
1411-
model.labelOutlineThicknessTexture = lTex.vtkObj;
1411+
model.labelOutlineThicknessTexture = lTex.oglObject;
14121412
model.labelOutlineThicknessTextureString = lTex.hash;
14131413
}
14141414
};

Sources/Rendering/OpenGL/ImageResliceMapper/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
219219
let toString = `${image.getMTime()}A${scalars.getMTime()}`;
220220

221221
const tex = model._openGLRenderWindow.getGraphicsResourceForObject(scalars);
222-
const reBuildTex = !tex?.vtkObj?.getHandle() || tex?.hash !== toString;
222+
const reBuildTex = !tex?.oglObject?.getHandle() || tex?.hash !== toString;
223223
if (reBuildTex) {
224224
if (!model.openGLTexture) {
225225
model.openGLTexture = vtkOpenGLTexture.newInstance();
@@ -247,7 +247,7 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
247247
);
248248
}
249249
} else {
250-
model.openGLTexture = tex.vtkObj;
250+
model.openGLTexture = tex.oglObject;
251251
}
252252

253253
const ppty = actor.getProperty();
@@ -260,7 +260,7 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
260260
const cTex =
261261
model._openGLRenderWindow.getGraphicsResourceForObject(colorTransferFunc);
262262
const reBuildC =
263-
!cTex?.vtkObj?.getHandle() ||
263+
!cTex?.oglObject?.getHandle() ||
264264
cTex?.hash !== toString ||
265265
model.colorTextureString !== toString;
266266
if (reBuildC) {
@@ -324,7 +324,7 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
324324
);
325325
}
326326
} else {
327-
model.colorTexture = cTex.vtkObj;
327+
model.colorTexture = cTex.oglObject;
328328
model.colorTextureString = cTex.hash;
329329
}
330330

@@ -337,7 +337,7 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
337337
model._openGLRenderWindow.getGraphicsResourceForObject(pwFunc);
338338
// rebuild opacity tfun?
339339
const reBuildPwf =
340-
!pwfTex?.vtkObj?.getHandle() ||
340+
!pwfTex?.oglObject?.getHandle() ||
341341
pwfTex?.hash !== toString ||
342342
model.pwfTextureString !== toString;
343343
if (reBuildPwf) {
@@ -404,7 +404,7 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
404404
);
405405
}
406406
} else {
407-
model.pwfTexture = pwfTex.vtkObj;
407+
model.pwfTexture = pwfTex.oglObject;
408408
model.pwfTextureString = pwfTex.hash;
409409
}
410410

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,9 @@ export interface vtkOpenGLRenderWindow extends vtkOpenGLRenderWindowBase {
454454
* the cached resource.
455455
* @return {Object} Dictionary with the graphics resource and string hash
456456
*/
457-
getGraphicsResourceForObject(
458-
vtkObj: vtkCellArray | vtkDataArray | vtkPoints
459-
): { gObj: vtkOpenGLTexture | vtkBufferObject; hash: string };
457+
getGraphicsResourceForObject<T extends vtkCellArray | vtkDataArray | vtkPoints>(
458+
vtkObj: T
459+
): { coreObject: T, oglObject: vtkOpenGLTexture | vtkBufferObject; hash: string };
460460

461461
/**
462462
* Get approximate graphics memory usage, in bytes, for the context. This is a simple computation

Sources/Rendering/OpenGL/RenderWindow/index.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,49 +1204,47 @@ function vtkOpenGLRenderWindow(publicAPI, model) {
12041204
return modified;
12051205
};
12061206

1207-
publicAPI.getGraphicsResourceForObject = (vtkObj) => {
1208-
if (!vtkObj) {
1207+
publicAPI.getGraphicsResourceForObject = (coreObject) => {
1208+
if (!coreObject) {
12091209
return null;
12101210
}
1211-
const vtko = model._graphicsResources.get(vtkObj);
1212-
const vtkh = model._graphicsResourceHash.get(vtkObj);
1213-
return { vtkObj: vtko, hash: vtkh };
1211+
return model._graphicsResources.get(coreObject);
12141212
};
1215-
publicAPI.setGraphicsResourceForObject = (vtkObj, gObj, hash) => {
1216-
if (!vtkObj) {
1213+
publicAPI.setGraphicsResourceForObject = (coreObject, oglObject, hash) => {
1214+
if (!coreObject) {
12171215
return;
12181216
}
1219-
model._graphicsResources.set(vtkObj, gObj);
1220-
model._graphicsResourceHash.set(vtkObj, hash);
1217+
model._graphicsResources.set(coreObject, {
1218+
coreObject,
1219+
oglObject,
1220+
hash,
1221+
});
12211222
};
12221223
publicAPI.getGraphicsMemoryInfo = () => {
12231224
let memUsed = 0;
1224-
model._graphicsResources.forEach((gObj, vtkObj) => {
1225-
memUsed += gObj.getAllocatedGPUMemoryInBytes();
1225+
model._graphicsResources.forEach(({ oglObject }) => {
1226+
memUsed += oglObject.getAllocatedGPUMemoryInBytes();
12261227
});
12271228
return memUsed;
12281229
};
12291230
publicAPI.releaseGraphicsResourcesForObject = (vtkObj) => {
12301231
if (!vtkObj) {
12311232
return false;
12321233
}
1233-
model._graphicsResources.get(vtkObj)?.releaseGraphicsResources(publicAPI);
1234-
return (
1235-
model._graphicsResources.delete(vtkObj) &&
1236-
model._graphicsResourceHash.delete(vtkObj)
1237-
);
1234+
const { oglObject } = model._graphicsResources.get(vtkObj);
1235+
oglObject?.releaseGraphicsResources(publicAPI);
1236+
return model._graphicsResources.delete(vtkObj);
12381237
};
12391238
publicAPI.releaseGraphicsResources = () => {
12401239
// Clear the shader cache
12411240
if (model.shaderCache !== null) {
12421241
model.shaderCache.releaseGraphicsResources(publicAPI);
12431242
}
12441243
// Free cached graphics resources at the context level
1245-
model._graphicsResources.forEach((gObj, vtkObj) => {
1246-
gObj.releaseGraphicsResources(publicAPI);
1244+
model._graphicsResources.forEach(({ oglObject }) => {
1245+
oglObject.releaseGraphicsResources(publicAPI);
12471246
});
12481247
model._graphicsResources.clear();
1249-
model._graphicsResourceHash.clear();
12501248
if (model.textureUnitManager !== null) {
12511249
model.textureUnitManager.freeAll();
12521250
}
@@ -1326,7 +1324,6 @@ export function extend(publicAPI, model, initialValues = {}) {
13261324

13271325
model._textureResourceIds = new Map();
13281326
model._graphicsResources = new Map();
1329-
model._graphicsResourceHash = new Map();
13301327
model._glInformation = null;
13311328

13321329
model.myFactory = vtkOpenGLViewNodeFactory.newInstance();

Sources/Rendering/OpenGL/VolumeMapper/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
15341534
numIComps
15351535
);
15361536
const reBuildOp =
1537-
!opTex.vtkObj ||
1537+
!opTex?.oglObject ||
15381538
opTex.hash !== toString ||
15391539
model.opacityTextureString !== toString;
15401540
if (reBuildOp) {
@@ -1603,7 +1603,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
16031603
);
16041604
}
16051605
} else {
1606-
model.opacityTexture = opTex.vtkObj;
1606+
model.opacityTexture = opTex.oglObject;
16071607
model.opacityTextureString = opTex.hash;
16081608
}
16091609

@@ -1617,7 +1617,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
16171617
const cTex =
16181618
model._openGLRenderWindow.getGraphicsResourceForObject(colorTransferFunc);
16191619
const reBuildC =
1620-
!cTex?.vtkObj?.getHandle() ||
1620+
!cTex?.oglObject?.getHandle() ||
16211621
cTex?.hash !== toString ||
16221622
model.colorTextureString !== toString;
16231623
if (reBuildC) {
@@ -1657,7 +1657,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
16571657
);
16581658
}
16591659
} else {
1660-
model.colorTexture = cTex.vtkObj;
1660+
model.colorTexture = cTex.oglObject;
16611661
model.colorTextureString = cTex.hash;
16621662
}
16631663

@@ -1666,7 +1666,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
16661666
const tex = model._openGLRenderWindow.getGraphicsResourceForObject(scalars);
16671667
// rebuild the scalarTexture if the data has changed
16681668
toString = `${image.getMTime()}A${scalars.getMTime()}`;
1669-
const reBuildTex = !tex?.vtkObj?.getHandle() || tex?.hash !== toString;
1669+
const reBuildTex = !tex?.oglObject?.getHandle() || tex?.hash !== toString;
16701670
if (reBuildTex) {
16711671
// Build the textures
16721672
const dims = image.getDimensions();
@@ -1691,7 +1691,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
16911691
);
16921692
}
16931693
} else {
1694-
model.scalarTexture = tex.vtkObj;
1694+
model.scalarTexture = tex.oglObject;
16951695
}
16961696

16971697
if (!model.tris.getCABO().getElementCount()) {
@@ -1772,7 +1772,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
17721772
const toString = `${labelOutlineThicknessArray.join('-')}`;
17731773

17741774
const reBuildL =
1775-
!lTex?.vtkObj?.getHandle() ||
1775+
!lTex?.oglObject?.getHandle() ||
17761776
lTex?.hash !== toString ||
17771777
model.labelOutlineThicknessTextureString !== toString;
17781778

@@ -1820,7 +1820,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
18201820
);
18211821
}
18221822
} else {
1823-
model.labelOutlineThicknessTexture = lTex.vtkObj;
1823+
model.labelOutlineThicknessTexture = lTex.oglObject;
18241824
model.labelOutlineThicknessTextureString = lTex.hash;
18251825
}
18261826
};

0 commit comments

Comments
 (0)