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

78.04% Statements 64/82
51.61% Branches 16/31
63.63% Functions 7/11
78.04% Lines 64/82

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              1x                       209x   209x             2737x 2737x               2737x   2737x   2737x 2737x 2737x                                       2737x                         2737x                             2737x 2737x         2737x           2737x 2737x 2737x 2756x         2756x 2756x   2737x                   2737x           2737x       209x         2737x           2737x           2737x     209x 4193x         4193x         4193x       4193x     209x   2737x 2737x     2737x   298x 298x 298x 298x 298x     298x 298x 298x     2439x     209x                                 209x               209x 4193x 3157x       1036x 840x   1036x 1036x 1036x               1x                   209x     209x     209x 209x 209x     209x         1x          
import Md5 from 'spark-md5';
 
import macro from 'vtk.js/Sources/macros';
import vtkShaderProgram from 'vtk.js/Sources/Rendering/OpenGL/ShaderProgram';
 
// ----------------------------------------------------------------------------
 
const SET_GET_FIELDS = [
  'lastShaderProgramBound',
  'context',
  '_openGLRenderWindow',
];
 
// ----------------------------------------------------------------------------
// vtkShaderCache methods
// ----------------------------------------------------------------------------
 
function vtkShaderCache(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkShaderCache');
 
  publicAPI.replaceShaderValues = (VSSource, FSSource, GSSource) => {
    // first handle renaming any Fragment shader inputs
    // if we have a geometry shader. By default fragment shaders
    // assume their inputs come from a Vertex Shader. When we
    // have a Geometry shader we rename the frament shader inputs
    // to come from the geometry shader
 
    let nFSSource = FSSource;
    Iif (GSSource.length > 0) {
      nFSSource = vtkShaderProgram.substitute(
        nFSSource,
        'VSOut',
        'GSOut'
      ).result;
    }
 
    const gl2 = model._openGLRenderWindow.getWebgl2();
 
    let fragDepthString = '\n';
 
    let version = '#version 100\n';
    if (gl2) {
      version =
        '#version 300 es\n' +
        '#define attribute in\n' +
        '#define textureCube texture\n' +
        '#define texture2D texture\n' +
        '#define textureCubeLod textureLod\n' +
        '#define texture2DLod textureLod\n';
    } else E{
      model.context.getExtension('OES_standard_derivatives');
      if (model.context.getExtension('EXT_frag_depth')) {
        fragDepthString = '#extension GL_EXT_frag_depth : enable\n';
      }
      if (model.context.getExtension('EXT_shader_texture_lod')) {
        fragDepthString +=
          '#extension GL_EXT_shader_texture_lod : enable\n' +
          '#define textureCubeLod textureCubeLodEXT\n' +
          '#define texture2DLod texture2DLodEXT';
      }
    }
 
    nFSSource = vtkShaderProgram.substitute(nFSSource, '//VTK::System::Dec', [
      `${version}\n`,
      gl2 ? '' : '#extension GL_OES_standard_derivatives : enable\n',
      fragDepthString,
      '#ifdef GL_FRAGMENT_PRECISION_HIGH',
      'precision highp float;',
      'precision highp int;',
      '#else',
      'precision mediump float;',
      'precision mediump int;',
      '#endif',
    ]).result;
 
    let nVSSource = vtkShaderProgram.substitute(
      VSSource,
      '//VTK::System::Dec',
      [
        `${version}\n`,
        '#ifdef GL_FRAGMENT_PRECISION_HIGH',
        'precision highp float;',
        'precision highp int;',
        '#else',
        'precision mediump float;',
        'precision mediump int;',
        '#endif',
      ]
    ).result;
 
    if (gl2) {
      nVSSource = vtkShaderProgram.substitute(
        nVSSource,
        'varying',
        'out'
      ).result;
      nFSSource = vtkShaderProgram.substitute(
        nFSSource,
        'varying',
        'in'
      ).result;
 
      let shaderOutputs = '';
      let outputCount = 0;
      while (nFSSource.includes(`gl_FragData[${outputCount}]`)) {
        nFSSource = vtkShaderProgram.substitute(
          nFSSource,
          `gl_FragData\\[${outputCount}\\]`,
          `fragOutput${outputCount}`
        ).result;
        shaderOutputs += `layout(location = ${outputCount}) out vec4 fragOutput${outputCount};\n`;
        outputCount++;
      }
      nFSSource = vtkShaderProgram.substitute(
        nFSSource,
        '//VTK::Output::Dec',
        shaderOutputs
      ).result;
    }
 
    // nFSSource = ShaderProgram.substitute(nFSSource, 'gl_FragData\\[0\\]',
    //   'gl_FragColor').result;
 
    const nGSSource = vtkShaderProgram.substitute(
      GSSource,
      '//VTK::System::Dec',
      version
    ).result;
 
    return { VSSource: nVSSource, FSSource: nFSSource, GSSource: nGSSource };
  };
 
  // return NULL if there is an issue
  publicAPI.readyShaderProgramArray = (
    vertexCode,
    fragmentCode,
    geometryCode
  ) => {
    const data = publicAPI.replaceShaderValues(
      vertexCode,
      fragmentCode,
      geometryCode
    );
 
    const shaderProgram = publicAPI.getShaderProgram(
      data.VSSource,
      data.FSSource,
      data.GSSource
    );
 
    return publicAPI.readyShaderProgram(shaderProgram);
  };
 
  publicAPI.readyShaderProgram = (program) => {
    Iif (!program) {
      return null;
    }
 
    // compile if needed
    Iif (!program.getCompiled() && !program.compileShader()) {
      return null;
    }
 
    // bind if needed
    Iif (!publicAPI.bindShaderProgram(program)) {
      return null;
    }
 
    return program;
  };
 
  publicAPI.getShaderProgram = (vertexCode, fragmentCode, geometryCode) => {
    // compute the MD5 and the check the map
    const hashInput = `${vertexCode}${fragmentCode}${geometryCode}`;
    const result = Md5.hash(hashInput);
 
    // does it already exist?
    if (!(result in model.shaderPrograms)) {
      // create one
      const sps = vtkShaderProgram.newInstance();
      sps.setContext(model.context);
      sps.getVertexShader().setSource(vertexCode);
      sps.getFragmentShader().setSource(fragmentCode);
      Iif (geometryCode) {
        sps.getGeometryShader().setSource(geometryCode);
      }
      sps.setMd5Hash(result);
      model.shaderPrograms[result] = sps;
      return sps;
    }
 
    return model.shaderPrograms[result];
  };
 
  publicAPI.releaseGraphicsResources = (win) => {
    // NOTE:
    // In the current implementation as of October 26th, if a shader
    // program is created by ShaderCache then it should make sure
    // that it releases the graphics resources used by these programs.
    // It is not wisely for callers to do that since then they would
    // have to loop over all the programs were in use and invoke
    // release graphics resources individually.
 
    publicAPI.releaseCurrentShaderProgram();
 
    Object.keys(model.shaderPrograms)
      .map((key) => model.shaderPrograms[key])
      .forEach((sp) => sp.cleanup());
    model.shaderPrograms = {};
  };
 
  publicAPI.releaseCurrentShaderProgram = () => {
    // release prior shader
    if (model.lastShaderProgramBound) {
      model.lastShaderProgramBound.cleanup();
      model.lastShaderProgramBound = null;
    }
  };
 
  publicAPI.bindShaderProgram = (program) => {
    if (model.lastShaderProgramBound === program) {
      return 1;
    }
 
    // release prior program
    if (model.lastShaderProgramBound) {
      model.lastShaderProgramBound.release();
    }
    program.bind();
    model.lastShaderProgramBound = program;
    return 1;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  lastShaderProgramBound: null,
  shaderPrograms: null,
  context: null,
  // _openGLRenderWindow: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Internal objects
  model.shaderPrograms = {};
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, SET_GET_FIELDS);
  macro.moveToProtected(publicAPI, model, ['openGLRenderWindow']);
 
  // Object methods
  vtkShaderCache(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkShaderCache');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };