Skip to content

Commit

Permalink
fix: avoid norm16 for textures with linear filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
slak44 committed Dec 19, 2024
1 parent 89758ed commit 806ad25
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
13 changes: 13 additions & 0 deletions Sources/Rendering/OpenGL/Texture/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,19 @@ export interface vtkOpenGLTexture extends vtkViewNode {
* @param useHalfFloat - whether to use half float
*/
enableUseHalfFloat(useHalfFloat: boolean): void;

/**
* Even when the EXT_texture_norm16 extension is present, linear filtering
* might not be supported for normalized fixed point textures.
*
* There seems to be no way of checking for support, aside from actually
* trying to render a texture and seeing if the pixels are black or not.
*
* @param useLinearNorm16 When this is false (the default), the texture will
* avoid using EXT_texture_norm16 entirely if filtering is set to linear.
* Set this to true to use it anyway.
*/
enableLinearNorm16Ext(useLinearNorm16: boolean): void;
}

/**
Expand Down
42 changes: 27 additions & 15 deletions Sources/Rendering/OpenGL/Texture/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ function vtkOpenGLTexture(publicAPI, model) {
}
};

const getNorm16Ext = () => {
if (
!model.enableLinearNorm16Ext &&
(model.minificationFilter === Filter.LINEAR ||
model.magnificationFilter === Filter.LINEAR)
) {
return undefined;
}
return model.oglNorm16Ext;
};

//----------------------------------------------------------------------------
publicAPI.destroyTexture = () => {
// deactivate it first
Expand Down Expand Up @@ -391,7 +402,7 @@ function vtkOpenGLTexture(publicAPI, model) {
result = model._openGLRenderWindow.getDefaultTextureInternalFormat(
vtktype,
numComps,
model.oglNorm16Ext,
getNorm16Ext(),
publicAPI.useHalfFloat()
);
if (result) {
Expand Down Expand Up @@ -478,9 +489,9 @@ function vtkOpenGLTexture(publicAPI, model) {
return model.context.UNSIGNED_BYTE;
// prefer norm16 since that is accurate compared to
// half float which is not
case model.oglNorm16Ext && !useHalfFloat && VtkDataTypes.SHORT:
case getNorm16Ext() && !useHalfFloat && VtkDataTypes.SHORT:
return model.context.SHORT;
case model.oglNorm16Ext && !useHalfFloat && VtkDataTypes.UNSIGNED_SHORT:
case getNorm16Ext() && !useHalfFloat && VtkDataTypes.UNSIGNED_SHORT:
return model.context.UNSIGNED_SHORT;
// use half float type
case useHalfFloat && VtkDataTypes.SHORT:
Expand Down Expand Up @@ -820,7 +831,7 @@ function vtkOpenGLTexture(publicAPI, model) {
if (
webGLInfo.RENDERER.value.match(/WebKit/gi) &&
navigator.platform.match(/Mac/gi) &&
model.oglNorm16Ext &&
getNorm16Ext() &&
(dataType === VtkDataTypes.UNSIGNED_SHORT ||
dataType === VtkDataTypes.SHORT)
) {
Expand Down Expand Up @@ -925,7 +936,7 @@ function vtkOpenGLTexture(publicAPI, model) {
numComps *
model._openGLRenderWindow.getDefaultTextureByteSize(
dataType,
model.oglNorm16Ext,
getNorm16Ext(),
publicAPI.useHalfFloat()
);
publicAPI.deactivate();
Expand Down Expand Up @@ -1049,7 +1060,7 @@ function vtkOpenGLTexture(publicAPI, model) {
numComps *
model._openGLRenderWindow.getDefaultTextureByteSize(
dataType,
model.oglNorm16Ext,
getNorm16Ext(),
publicAPI.useHalfFloat()
);
// generateMipmap must not be called here because we manually upload all levels
Expand Down Expand Up @@ -1138,7 +1149,7 @@ function vtkOpenGLTexture(publicAPI, model) {
model.components *
model._openGLRenderWindow.getDefaultTextureByteSize(
dataType,
model.oglNorm16Ext,
getNorm16Ext(),
publicAPI.useHalfFloat()
);

Expand Down Expand Up @@ -1248,7 +1259,7 @@ function vtkOpenGLTexture(publicAPI, model) {
model.components *
model._openGLRenderWindow.getDefaultTextureByteSize(
VtkDataTypes.UNSIGNED_CHAR,
model.oglNorm16Ext,
getNorm16Ext(),
publicAPI.useHalfFloat()
);

Expand Down Expand Up @@ -1401,11 +1412,7 @@ function vtkOpenGLTexture(publicAPI, model) {
}

// Handle SHORT data type with EXT_texture_norm16 extension
if (
model.oglNorm16Ext &&
!useHalfFloat &&
dataType === VtkDataTypes.SHORT
) {
if (getNorm16Ext() && !useHalfFloat && dataType === VtkDataTypes.SHORT) {
for (let c = 0; c < numComps; ++c) {
model.volumeInfo.scale[c] = 32767.0; // Scale to [-1, 1] range
}
Expand All @@ -1414,7 +1421,7 @@ function vtkOpenGLTexture(publicAPI, model) {

// Handle UNSIGNED_SHORT data type with EXT_texture_norm16 extension
if (
model.oglNorm16Ext &&
getNorm16Ext() &&
!useHalfFloat &&
dataType === VtkDataTypes.UNSIGNED_SHORT
) {
Expand Down Expand Up @@ -1569,7 +1576,7 @@ function vtkOpenGLTexture(publicAPI, model) {
model.components *
model._openGLRenderWindow.getDefaultTextureByteSize(
dataTypeToUse,
model.oglNorm16Ext,
getNorm16Ext(),
publicAPI.useHalfFloat()
);

Expand Down Expand Up @@ -1861,6 +1868,10 @@ function vtkOpenGLTexture(publicAPI, model) {
publicAPI.enableUseHalfFloat = (use) => {
model.enableUseHalfFloat = use;
};

publicAPI.enableLinearNorm16Ext = (enabled) => {
model.enableLinearNorm16Ext = enabled;
};
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -1894,6 +1905,7 @@ const DEFAULT_VALUES = {
maxLevel: 1000,
generateMipmap: false,
oglNorm16Ext: null,
enableLinearNorm16Ext: false,
allocatedGPUMemoryInBytes: 0,
// by default it is enabled
enableUseHalfFloat: true,
Expand Down

0 comments on commit 806ad25

Please sign in to comment.