All files / packages/core/src/RenderingEngine/vtkClasses vtkStreamingOpenGLTexture.js

77.92% Statements 60/77
35.71% Branches 10/28
100% Functions 7/7
78.37% Lines 58/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273                              82x   82x   82x                 59x 59x   59x                                 82x 41x   41x 35x   6x 6x 6x         6x 6x 6x                           6x 60x 60x         6x   6x       6x 6x                         82x           60x   60x 60x   60x 60x   60x                       60x 60x         60x   60x         60x       60x 60x   60x   60x 60x     60x 60x   60x           60x                               60x                                 60x 60x     60x           60x                               82x 100x                         82x 90x                   1x         82x   82x     82x         1x                
import macro from '@kitware/vtk.js/macros';
import vtkOpenGLTexture from '@kitware/vtk.js/Rendering/OpenGL/Texture';
import HalfFloat from '@kitware/vtk.js/Common/Core/HalfFloat';
import { getConfiguration } from '../../init';
 
/**
 * vtkStreamingOpenGLTexture - A derived class of the core vtkOpenGLTexture.
 * This class has methods to update the texture memory on the GPU slice by slice
 * in an efficient yet GPU-architecture friendly manner.
 *
 *
 * @param {*} publicAPI The public API to extend
 * @param {*} model The private model to extend.
 */
function vtkStreamingOpenGLTexture(publicAPI, model) {
  model.classHierarchy.push('vtkStreamingOpenGLTexture');
 
  const superCreate3DFilterableFromRaw = publicAPI.create3DFilterableFromRaw;
 
  publicAPI.create3DFilterableFromRaw = (
    width,
    height,
    depth,
    numComps,
    dataType,
    data,
    preferSizeOverAccuracy
  ) => {
    model.inputDataType = dataType;
    model.inputNumComps = numComps;
 
    superCreate3DFilterableFromRaw(
      width,
      height,
      depth,
      numComps,
      dataType,
      data,
      preferSizeOverAccuracy
    );
  };
 
  /**
   * This function updates the GPU texture memory to match the current
   * representation of data held in RAM.
   *
   * @param {Float32Array|Uint8Array|Int16Array|Uint16Array} data The data array which has been updated.
   */
  publicAPI.update3DFromRaw = (data) => {
    const { updatedFrames } = model;
 
    if (!updatedFrames.length) {
      return;
    }
    model._openGLRenderWindow.activateTexture(publicAPI);
    publicAPI.createTexture();
    publicAPI.bind();
 
    let bytesPerVoxel;
    let TypedArrayConstructor;
 
    if (data instanceof Uint8Array) {
      bytesPerVoxel = 1;
      TypedArrayConstructor = Uint8Array;
    } else Eif (data instanceof Int16Array) {
      bytesPerVoxel = 2;
      TypedArrayConstructor = Int16Array;
    } else if (data instanceof Uint16Array) {
      bytesPerVoxel = 2;
      TypedArrayConstructor = Uint16Array;
    } else if (data instanceof Float32Array) {
      bytesPerVoxel = 4;
      TypedArrayConstructor = Float32Array;
    } else {
      throw new Error(`No support for given TypedArray.`);
    }
 
    for (let i = 0; i < updatedFrames.length; i++) {
      Eif (updatedFrames[i]) {
        model.fillSubImage3D(data, i, bytesPerVoxel, TypedArrayConstructor);
      }
    }
 
    // Reset updatedFrames
    model.updatedFrames = [];
 
    Iif (model.generateMipmap) {
      model.context.generateMipmap(model.target);
    }
 
    publicAPI.deactivate();
    return true;
  };
 
  /**
   * This function updates the GPU texture memory to match the current
   * representation of data held in RAM.
   *
   * @param {Float32Array|Uint8Array} data The data array which has been updated.
   * @param {number} frameIndex The frame to load in.
   * @param {number} BytesPerVoxel The number of bytes per voxel in the data, so we don't have to constantly
   * check the array type.
   * @param {object} TypedArrayConstructor The constructor for the array type. Again so we don't have to constantly check.
   */
  model.fillSubImage3D = (
    data,
    frameIndex,
    bytesPerVoxel,
    TypedArrayConstructor
  ) => {
    const buffer = data.buffer;
 
    const frameLength = model.width * model.height;
    const frameLengthInBytes = frameLength * model.components * bytesPerVoxel;
 
    const zOffset = frameIndex * frameLengthInBytes;
    const rowLength = model.width * model.components;
 
    const gl = model.context;
 
    /**
     * It appears that the implementation of texSubImage3D uses 2D textures to do the texture copy if
     * MAX_TEXTURE_SIZE is greater than MAX_TEXTURE_SIZE_3D. As such if you make a single block too big
     * the transfer messes up cleanly and you render a black box or some data if you are lucky.
     *
     * This block-size based on 2D texture size seems like the safest approach that should work on most systems.
     *
     * There are certainly further optimizations that could be done here, we can do bigger chunks with other systems
     * But we need to find the _exact_ criteria. And then its not even guaranteed it'll be much faster.
     */
    const MAX_TEXTURE_SIZE = gl.getParameter(gl.MAX_TEXTURE_SIZE);
    let blockHeight = Math.floor(
      (bytesPerVoxel * MAX_TEXTURE_SIZE) / model.width
    );
 
    // Cap to actual frame height:
    blockHeight = Math.min(blockHeight, model.height);
    const { useNorm16Texture, preferSizeOverAccuracy } =
      getConfiguration().rendering;
    // TODO: there is currently a bug in chrome and safari which requires
    // blockheight = 1 for norm16 textures:
    // https://bugs.chromium.org/p/chromium/issues/detail?id=1408247
    // https://bugs.webkit.org/show_bug.cgi?id=252039
    Iif (useNorm16Texture && !preferSizeOverAccuracy) {
      blockHeight = 1;
    }
 
    const multiRowBlockLength = rowLength * blockHeight;
    const multiRowBlockLengthInBytes = multiRowBlockLength * bytesPerVoxel;
 
    const normalBlocks = Math.floor(model.height / blockHeight);
 
    const lastBlockHeight = model.height % blockHeight;
    const multiRowLastBlockLength = rowLength * lastBlockHeight;
 
    // Perform most blocks.
    for (let block = 0; block < normalBlocks; block++) {
      const yOffset = block * blockHeight;
 
      let dataView = new TypedArrayConstructor(
        buffer,
        zOffset + block * multiRowBlockLengthInBytes,
        multiRowBlockLength
      );
 
      Iif (
        model.useHalfFloat &&
        (TypedArrayConstructor === Uint16Array ||
          TypedArrayConstructor === Int16Array)
      ) {
        // in the case we want to use halfFloat rendering (preferSizeOverAccuracy = true),
        // we need to convert uint16 and int16 into fp16 format.
        // This is the step where precision is lost for streaming volume viewport.
        for (let idx = 0; idx < dataView.length; idx++) {
          dataView[idx] = HalfFloat.toHalf(dataView[idx]);
        }
        if (TypedArrayConstructor === Int16Array) {
          dataView = new Uint16Array(dataView);
        }
      }
 
      gl.texSubImage3D(
        model.target, // target
        0, // mipMap level (always zero)
        0, // xOffset
        yOffset, // yOffset
        frameIndex,
        model.width,
        blockHeight, //model.height,
        1, // numFramesInBlock,
        model.format,
        model.openGLDataType,
        dataView
      );
    }
 
    // perform last block if present
 
    Eif (lastBlockHeight !== 0) {
      const yOffset = normalBlocks * blockHeight;
 
      // Dataview of last block
      const dataView = new TypedArrayConstructor(
        buffer,
        zOffset + normalBlocks * multiRowBlockLengthInBytes,
        multiRowLastBlockLength
      );
 
      gl.texSubImage3D(
        model.target, // target
        0, // mipMap level (always zero)
        0, // xOffset
        yOffset, // yOffset
        frameIndex,
        model.width,
        lastBlockHeight, //model.height,
        1, // numFramesInBlock,
        model.format,
        model.openGLDataType,
        dataView
      );
    }
  };
 
  publicAPI.getTextureParameters = () => {
    return {
      width: model.width,
      height: model.height,
      depth: model.depth,
      numComps: model.inputNumComps,
      dataType: model.inputDataType,
    };
  };
 
  /**
   * Called when a frame is loaded so that on next render we know which data to load in.
   * @param {number} frameIndex The frame to load in.
   */
  publicAPI.setUpdatedFrame = (frameIndex) => {
    model.updatedFrames[frameIndex] = true;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  updatedFrames: [],
};
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  vtkOpenGLTexture.extend(publicAPI, model, initialValues);
 
  // Object methods
  vtkStreamingOpenGLTexture(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkStreamingOpenGLTexture'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };