Skip to content

Commit b04e59d

Browse files
sedghifinetjul
authored andcommitted
chore(refactor): rename setUseHalfFloat
1 parent c65bbf6 commit b04e59d

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ export interface vtkOpenGLTexture extends vtkViewNode {
175175
*/
176176
getOpenGLWrapMode(vtktype: Wrap): any;
177177

178+
/**
179+
* Updates the data array to match the required data type for OpenGL.
180+
*
181+
* This function takes the input data and converts it to the appropriate
182+
* format required by the OpenGL texture, based on the specified data type.
183+
*
184+
* @param {string} dataType - The original data type of the input data.
185+
* @param {Array} data - The input data array that needs to be updated.
186+
* @param {boolean} [depth=false] - Indicates whether the data is a 3D array.
187+
* @returns {Array} The updated data array that matches the OpenGL data type.
188+
*/
189+
updateArrayDataTypeForGL(
190+
dataType: VtkDataTypes,
191+
data: any,
192+
depth?: boolean
193+
): void;
194+
178195
/**
179196
* Creates a 2D texture from raw data.
180197
* @param width The width of the texture.
@@ -323,6 +340,15 @@ export interface vtkOpenGLTexture extends vtkViewNode {
323340
* @returns {number} The maximum texture size.
324341
*/
325342
getMaximumTextureSize(ctx: any): number;
343+
344+
/**
345+
* Public API to disable half float usage.
346+
* Half float is automatically enabled when creating the texture,
347+
* but users may want to disable it in certain cases
348+
* (e.g., streaming data where the full range is not yet available).
349+
* @param useHalfFloat - whether to use half float
350+
*/
351+
enableUseHalfFloat(useHalfFloat: boolean): void;
326352
}
327353

328354
/**

Sources/Rendering/OpenGL/Texture/index.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ function vtkOpenGLTexture(publicAPI, model) {
392392
vtktype,
393393
numComps,
394394
model.oglNorm16Ext,
395-
model.useHalfFloat
395+
publicAPI.useHalfFloat()
396396
);
397397
if (result) {
398398
return result;
@@ -408,6 +408,8 @@ function vtkOpenGLTexture(publicAPI, model) {
408408
return result;
409409
};
410410

411+
publicAPI.useHalfFloat = () => model.enableHalfFloat && model.canUseHalfFloat;
412+
411413
//----------------------------------------------------------------------------
412414
publicAPI.setInternalFormat = (iFormat) => {
413415
model._forceInternalFormat = true;
@@ -465,6 +467,7 @@ function vtkOpenGLTexture(publicAPI, model) {
465467

466468
//----------------------------------------------------------------------------
467469
publicAPI.getDefaultDataType = (vtkScalarType) => {
470+
const useHalfFloat = publicAPI.useHalfFloat();
468471
// DON'T DEAL with VTK_CHAR as this is platform dependent.
469472
if (model._openGLRenderWindow.getWebgl2()) {
470473
switch (vtkScalarType) {
@@ -474,16 +477,14 @@ function vtkOpenGLTexture(publicAPI, model) {
474477
return model.context.UNSIGNED_BYTE;
475478
// prefer norm16 since that is accurate compared to
476479
// half float which is not
477-
case model.oglNorm16Ext && !model.useHalfFloat && VtkDataTypes.SHORT:
480+
case model.oglNorm16Ext && !useHalfFloat && VtkDataTypes.SHORT:
478481
return model.context.SHORT;
479-
case model.oglNorm16Ext &&
480-
!model.useHalfFloat &&
481-
VtkDataTypes.UNSIGNED_SHORT:
482+
case model.oglNorm16Ext && !useHalfFloat && VtkDataTypes.UNSIGNED_SHORT:
482483
return model.context.UNSIGNED_SHORT;
483484
// use half float type
484-
case model.useHalfFloat && VtkDataTypes.SHORT:
485+
case useHalfFloat && VtkDataTypes.SHORT:
485486
return model.context.HALF_FLOAT;
486-
case model.useHalfFloat && VtkDataTypes.UNSIGNED_SHORT:
487+
case useHalfFloat && VtkDataTypes.UNSIGNED_SHORT:
487488
return model.context.HALF_FLOAT;
488489
// case VtkDataTypes.INT:
489490
// return model.context.INT;
@@ -924,7 +925,7 @@ function vtkOpenGLTexture(publicAPI, model) {
924925
model._openGLRenderWindow.getDefaultTextureByteSize(
925926
dataType,
926927
model.oglNorm16Ext,
927-
model.useHalfFloat
928+
publicAPI.useHalfFloat()
928929
);
929930
publicAPI.deactivate();
930931
return true;
@@ -1048,7 +1049,7 @@ function vtkOpenGLTexture(publicAPI, model) {
10481049
model._openGLRenderWindow.getDefaultTextureByteSize(
10491050
dataType,
10501051
model.oglNorm16Ext,
1051-
model.useHalfFloat
1052+
publicAPI.useHalfFloat()
10521053
);
10531054
// generateMipmap must not be called here because we manually upload all levels
10541055
// if it is called, all levels will be overwritten
@@ -1137,7 +1138,7 @@ function vtkOpenGLTexture(publicAPI, model) {
11371138
model._openGLRenderWindow.getDefaultTextureByteSize(
11381139
dataType,
11391140
model.oglNorm16Ext,
1140-
model.useHalfFloat
1141+
publicAPI.useHalfFloat()
11411142
);
11421143

11431144
publicAPI.deactivate();
@@ -1247,7 +1248,7 @@ function vtkOpenGLTexture(publicAPI, model) {
12471248
model._openGLRenderWindow.getDefaultTextureByteSize(
12481249
VtkDataTypes.UNSIGNED_CHAR,
12491250
model.oglNorm16Ext,
1250-
model.useHalfFloat
1251+
publicAPI.useHalfFloat()
12511252
);
12521253

12531254
publicAPI.deactivate();
@@ -1280,7 +1281,7 @@ function vtkOpenGLTexture(publicAPI, model) {
12801281
return true;
12811282
}
12821283

1283-
function setUseHalfFloat(dataType, offset, scale, preferSizeOverAccuracy) {
1284+
function setCanUseHalfFloat(dataType, offset, scale, preferSizeOverAccuracy) {
12841285
publicAPI.getOpenGLDataType(dataType);
12851286

12861287
let useHalfFloat = false;
@@ -1297,7 +1298,7 @@ function vtkOpenGLTexture(publicAPI, model) {
12971298
const isHalfFloat =
12981299
useHalfFloat &&
12991300
(hasExactHalfFloat(offset, scale) || preferSizeOverAccuracy);
1300-
model.useHalfFloat = isHalfFloat;
1301+
model.canUseHalfFloat = isHalfFloat;
13011302
}
13021303

13031304
function processDataArray(dataArray, preferSizeOverAccuracy) {
@@ -1319,7 +1320,7 @@ function vtkOpenGLTexture(publicAPI, model) {
13191320

13201321
// preferSizeOverAccuracy will override norm16 due to bug with norm16 implementation
13211322
// https://bugs.chromium.org/p/chromium/issues/detail?id=1408247
1322-
setUseHalfFloat(
1323+
setCanUseHalfFloat(
13231324
dataType,
13241325
scaleOffsets.offset,
13251326
scaleOffsets.scale,
@@ -1328,7 +1329,7 @@ function vtkOpenGLTexture(publicAPI, model) {
13281329

13291330
// since our default is to use half float, in case that we can't use it
13301331
// we need to use another type
1331-
if (!model.useHalfFloat) {
1332+
if (!publicAPI.useHalfFloat()) {
13321333
publicAPI.getOpenGLDataType(dataType, true);
13331334
}
13341335

@@ -1375,6 +1376,7 @@ function vtkOpenGLTexture(publicAPI, model) {
13751376

13761377
publicAPI.updateVolumeInfoForGL = (dataType, numComps) => {
13771378
let isScalingApplied = false;
1379+
const useHalfFloat = publicAPI.useHalfFloat();
13781380

13791381
// Initialize volume info if it doesn't exist
13801382
if (!model.volumeInfo?.scale || !model.volumeInfo?.offset) {
@@ -1393,7 +1395,7 @@ function vtkOpenGLTexture(publicAPI, model) {
13931395
// Handle SHORT data type with EXT_texture_norm16 extension
13941396
if (
13951397
model.oglNorm16Ext &&
1396-
!model.useHalfFloat &&
1398+
!useHalfFloat &&
13971399
dataType === VtkDataTypes.SHORT
13981400
) {
13991401
for (let c = 0; c < numComps; ++c) {
@@ -1405,7 +1407,7 @@ function vtkOpenGLTexture(publicAPI, model) {
14051407
// Handle UNSIGNED_SHORT data type with EXT_texture_norm16 extension
14061408
if (
14071409
model.oglNorm16Ext &&
1408-
!model.useHalfFloat &&
1410+
!useHalfFloat &&
14091411
dataType === VtkDataTypes.UNSIGNED_SHORT
14101412
) {
14111413
for (let c = 0; c < numComps; ++c) {
@@ -1425,7 +1427,7 @@ function vtkOpenGLTexture(publicAPI, model) {
14251427
// No scaling needed for FLOAT or HalfFloat (SHORT/UNSIGNED_SHORT)
14261428
if (
14271429
dataType === VtkDataTypes.FLOAT ||
1428-
(model.useHalfFloat &&
1430+
(useHalfFloat &&
14291431
(dataType === VtkDataTypes.SHORT ||
14301432
dataType === VtkDataTypes.UNSIGNED_SHORT))
14311433
) {
@@ -1560,7 +1562,7 @@ function vtkOpenGLTexture(publicAPI, model) {
15601562
model._openGLRenderWindow.getDefaultTextureByteSize(
15611563
dataTypeToUse,
15621564
model.oglNorm16Ext,
1563-
model.useHalfFloat
1565+
publicAPI.useHalfFloat()
15641566
);
15651567

15661568
publicAPI.deactivate();
@@ -1848,8 +1850,8 @@ function vtkOpenGLTexture(publicAPI, model) {
18481850
};
18491851

18501852
// set use half float
1851-
publicAPI.setUseHalfFloat = (use) => {
1852-
model.useHalfFloat = use;
1853+
publicAPI.enableUseHalfFloat = (use) => {
1854+
model.enableUseHalfFloat = use;
18531855
};
18541856
}
18551857

@@ -1883,13 +1885,12 @@ const DEFAULT_VALUES = {
18831885
baseLevel: 0,
18841886
maxLevel: 1000,
18851887
generateMipmap: false,
1886-
// use half float by default, but it will get set
1887-
// to false if the context does not support it or
1888-
// the voxel intensity range is out of the accurate
1889-
// range of half float
1890-
useHalfFloat: true,
18911888
oglNorm16Ext: null,
18921889
allocatedGPUMemoryInBytes: 0,
1890+
// by default it is enabled
1891+
enableUseHalfFloat: true,
1892+
// but by default we don't know if we can use half float base on the data range
1893+
canUseHalfFloat: false,
18931894
};
18941895

18951896
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)