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

64.86% Statements 24/37
52.94% Branches 9/17
75% Functions 3/4
64.86% Lines 24/37

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    1x                   16072x   16072x 550x   550x                 550x         550x         275x 275x     275x 275x     550x 550x 550x 550x       550x                 550x     16072x                             1x                       16072x     16072x 16072x                 16072x         1x          
import macro from 'vtk.js/Sources/macros';
 
const { vtkErrorMacro } = macro;
 
// export const SHADER_TYPES = ['Vertex', 'Fragment', 'Geometry', 'Unknown'];
 
// ----------------------------------------------------------------------------
// vtkShader methods
// ----------------------------------------------------------------------------
 
function vtkShader(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkShader');
 
  publicAPI.compile = () => {
    let stype = model.context.VERTEX_SHADER;
 
    Iif (
      !model.source ||
      !model.source.length ||
      model.shaderType === 'Unknown'
    ) {
      return false;
    }
 
    // Ensure we delete the previous shader if necessary.
    Iif (model.handle !== 0) {
      model.context.deleteShader(model.handle);
      model.handle = 0;
    }
 
    switch (model.shaderType) {
      // case vtkShader::Geometry:
      //   type = GL_GEOMETRY_SHADER;
      //   break;
      case 'Fragment':
        stype = model.context.FRAGMENT_SHADER;
        break;
      case 'Vertex':
      default:
        stype = model.context.VERTEX_SHADER;
        break;
    }
 
    model.handle = model.context.createShader(stype);
    model.context.shaderSource(model.handle, model.source);
    model.context.compileShader(model.handle);
    const isCompiled = model.context.getShaderParameter(
      model.handle,
      model.context.COMPILE_STATUS
    );
    Iif (!isCompiled) {
      const lastError = model.context.getShaderInfoLog(model.handle);
      vtkErrorMacro(`Error compiling shader '${model.source}': ${lastError}`);
      model.context.deleteShader(model.handle);
      model.handle = 0;
      return false;
    }
 
    // The shader compiled, store its handle and return success.
    return true;
  };
 
  publicAPI.cleanup = () => {
    if (model.shaderType === 'Unknown' || model.handle === 0) {
      return;
    }
 
    model.context.deleteShader(model.handle);
    model.handle = 0;
    model.dirty = true;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  shaderType: 'Unknown',
  source: '',
  error: '',
  handle: 0,
  dirty: false,
  context: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, [
    'shaderType',
    'source',
    'error',
    'handle',
    'context',
  ]);
 
  // Object methods
  vtkShader(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkShader');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };