All files / Sources/Rendering/OpenGL/BufferObject index.js

76.81% Statements 53/69
47.61% Branches 10/21
78.57% Functions 11/14
78.46% Lines 51/65

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      1x                   1x               5185x       11177x                     11177x         5185x 5185x 5185x 5185x     5545x   5185x 5107x     11090x 5185x   5185x 1406x 1406x 1108x 1108x   1406x     5185x   1406x 1406x       1406x 1406x         1406x 1406x 1406x     5185x 5553x     5553x 5553x     5185x               5185x 6517x               5185x 5188x 59x   5129x 5129x 5129x 5129x 5129x       5185x             1x                   5185x     5185x   5185x       5185x   5185x         1x          
import macro from 'vtk.js/Sources/macros';
import Constants from 'vtk.js/Sources/Rendering/OpenGL/BufferObject/Constants';
 
const { ObjectType } = Constants;
 
// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------
 
// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------
 
export const STATIC = {};
 
// ----------------------------------------------------------------------------
// vtkOpenGLBufferObject methods
// ----------------------------------------------------------------------------
 
function vtkOpenGLBufferObject(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkOpenGLBufferObject');
 
  // Class-specific private functions
  function convertType(type) {
    switch (type) {
      case ObjectType.ELEMENT_ARRAY_BUFFER:
        return model.context.ELEMENT_ARRAY_BUFFER;
      case ObjectType.TEXTURE_BUFFER:
        if ('TEXTURE_BUFFER' in model.context) {
          return model.context.TEXTURE_BUFFER;
        }
      /* eslint-disable no-fallthrough */
      // Intentional fallthrough in case there is no TEXTURE_BUFFER in WebGL
      case ObjectType.ARRAY_BUFFER:
      default:
        return model.context.ARRAY_BUFFER;
      /* eslint-enable no-fallthrough */
    }
  }
 
  let internalType = null;
  let internalHandle = null;
  let dirty = true;
  let error = '';
 
  // Public API methods
  publicAPI.getType = () => internalType;
 
  publicAPI.setType = (value) => {
    internalType = value;
  };
 
  publicAPI.getHandle = () => internalHandle;
  publicAPI.isReady = () => dirty === false;
 
  publicAPI.generateBuffer = (type) => {
    const objectTypeGL = convertType(type);
    if (internalHandle === null) {
      internalHandle = model.context.createBuffer();
      internalType = type;
    }
    return convertType(internalType) === objectTypeGL;
  };
 
  publicAPI.upload = (data, type) => {
    // buffer, size, type
    const alreadyGenerated = publicAPI.generateBuffer(type);
    Iif (!alreadyGenerated) {
      error = 'Trying to upload array buffer to incompatible buffer.';
      return false;
    }
    model.context.bindBuffer(convertType(internalType), internalHandle);
    model.context.bufferData(
      convertType(internalType),
      data,
      model.context.STATIC_DRAW
    );
    model.allocatedGPUMemoryInBytes = data.length * data.BYTES_PER_ELEMENT;
    dirty = false;
    return true;
  };
 
  publicAPI.bind = () => {
    Iif (!internalHandle) {
      return false;
    }
    model.context.bindBuffer(convertType(internalType), internalHandle);
    return true;
  };
 
  publicAPI.release = () => {
    if (!internalHandle) {
      return false;
    }
    model.context.bindBuffer(convertType(internalType), null);
    return true;
  };
 
  publicAPI.releaseGraphicsResources = () => {
    Iif (internalHandle !== null) {
      model.context.bindBuffer(convertType(internalType), null);
      model.context.deleteBuffer(internalHandle);
      internalHandle = null;
      model.allocatedGPUMemoryInBytes = 0;
    }
  };
 
  publicAPI.setOpenGLRenderWindow = (rw) => {
    if (model._openGLRenderWindow === rw) {
      return;
    }
    publicAPI.releaseGraphicsResources();
    model._openGLRenderWindow = rw;
    model.context = null;
    if (rw) {
      model.context = model._openGLRenderWindow.getContext();
    }
  };
 
  publicAPI.getError = () => error;
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  objectType: ObjectType.ARRAY_BUFFER,
  // _openGLRenderWindow: null,
  context: null,
  allocatedGPUMemoryInBytes: 0,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  macro.obj(publicAPI, model);
 
  macro.get(publicAPI, model, [
    '_openGLRenderWindow',
    'allocatedGPUMemoryInBytes',
  ]);
  macro.moveToProtected(publicAPI, model, ['openGLRenderWindow']);
 
  vtkOpenGLBufferObject(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...STATIC, ...Constants };