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

96.29% Statements 104/108
91.8% Branches 56/61
92.3% Functions 12/13
96.29% Lines 104/108

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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315              1x                                 5106x   5106x 5111x 5111x 5111x 5111x     5106x 1384x 1384x 1384x     5106x   3969x   3969x 3969x 3969x 3969x 3969x 529x   3969x 3969x 6x 6x             3963x 3963x 3963x   3963x     3969x 3969x 529x   3969x         5106x 11266x 1587x   9679x 9679x 36x   9643x           970x   8673x     5106x 8214x       19x 19x             19x   8195x     5106x           4070x           2667x   1403x     5106x   4070x 2667x 2667x     2667x                 2667x 2101x   2101x     2667x   1403x         4070x   4070x 4070x 4070x 4070x   4070x     5106x 4070x 7x     7x 7x 7x     7x   4070x       12x     4058x 529x           5106x 2667x     2667x       2667x               2667x         5x                   5x                                 2667x     5106x 529x     529x 5x   524x     5106x               1x                           5106x     5106x   5106x 5106x   5106x 5106x   5106x                   5106x 5106x 5106x     5106x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkCellArrayBufferObject from 'vtk.js/Sources/Rendering/OpenGL/CellArrayBufferObject';
import vtkShaderProgram from 'vtk.js/Sources/Rendering/OpenGL/ShaderProgram';
import vtkVertexArrayObject from 'vtk.js/Sources/Rendering/OpenGL/VertexArrayObject';
 
import { Representation } from 'vtk.js/Sources/Rendering/Core/Property/Constants';
 
export const primTypes = {
  Start: 0,
  Points: 0,
  Lines: 1,
  Tris: 2,
  TriStrips: 3,
  TrisEdges: 4,
  TriStripsEdges: 5,
  End: 6,
};
 
// ----------------------------------------------------------------------------
// vtkOpenGLHelper methods
// ----------------------------------------------------------------------------
 
function vtkOpenGLHelper(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkOpenGLHelper');
 
  publicAPI.setOpenGLRenderWindow = (win) => {
    model.context = win.getContext();
    model.program.setContext(model.context);
    model.VAO.setOpenGLRenderWindow(win);
    model.CABO.setOpenGLRenderWindow(win);
  };
 
  publicAPI.releaseGraphicsResources = (oglwin) => {
    model.VAO.releaseGraphicsResources();
    model.CABO.releaseGraphicsResources();
    model.CABO.setElementCount(0);
  };
 
  publicAPI.drawArrays = (ren, actor, rep, oglMapper) => {
    // Are there any entries
    if (model.CABO.getElementCount()) {
      // are we drawing edges
      const mode = publicAPI.getOpenGLMode(rep);
      const wideLines = publicAPI.haveWideLines(ren, actor);
      const gl = model.context;
      const depthMask = gl.getParameter(gl.DEPTH_WRITEMASK);
      if (model.pointPicking) {
        gl.depthMask(false);
      }
      const drawingLines = mode === gl.LINES;
      if (drawingLines && wideLines) {
        publicAPI.updateShaders(ren, actor, oglMapper);
        gl.drawArraysInstanced(
          mode,
          0,
          model.CABO.getElementCount(),
          2 * Math.ceil(actor.getProperty().getLineWidth())
        );
      } else {
        gl.lineWidth(actor.getProperty().getLineWidth());
        publicAPI.updateShaders(ren, actor, oglMapper);
        gl.drawArrays(mode, 0, model.CABO.getElementCount());
        // reset the line width
        gl.lineWidth(1);
      }
      const stride =
        (mode === gl.POINTS ? 1 : 0) || (mode === gl.LINES ? 2 : 3);
      if (model.pointPicking) {
        gl.depthMask(depthMask);
      }
      return model.CABO.getElementCount() / stride;
    }
    return 0;
  };
 
  publicAPI.getOpenGLMode = (rep) => {
    if (model.pointPicking) {
      return model.context.POINTS;
    }
    const type = model.primitiveType;
    if (rep === Representation.POINTS || type === primTypes.Points) {
      return model.context.POINTS;
    }
    if (
      rep === Representation.WIREFRAME ||
      type === primTypes.Lines ||
      type === primTypes.TrisEdges ||
      type === primTypes.TriStripsEdges
    ) {
      return model.context.LINES;
    }
    return model.context.TRIANGLES;
  };
 
  publicAPI.haveWideLines = (ren, actor) => {
    if (actor.getProperty().getLineWidth() > 1.0) {
      // we have wide lines, but the OpenGL implementation may
      // actually support them, check the range to see if we
      // really need have to implement our own wide lines
      if (model.CABO.getOpenGLRenderWindow()) {
        Iif (
          model.CABO.getOpenGLRenderWindow().getHardwareMaximumLineWidth() >=
          actor.getProperty().getLineWidth()
        ) {
          return false;
        }
      }
      return true;
    }
    return false;
  };
 
  publicAPI.getNeedToRebuildShaders = (ren, actor, oglMapper) => {
    // has something changed that would require us to recreate the shader?
    // candidates are
    // property modified (representation interpolation and lighting)
    // input modified
    // mapper modified (lighting complexity)
    if (
      oglMapper.getNeedToRebuildShaders(publicAPI, ren, actor) ||
      publicAPI.getProgram() === 0 ||
      publicAPI.getShaderSourceTime().getMTime() < oglMapper.getMTime() ||
      publicAPI.getShaderSourceTime().getMTime() < actor.getMTime()
    ) {
      return true;
    }
    return false;
  };
 
  publicAPI.updateShaders = (ren, actor, oglMapper) => {
    // has something changed that would require us to recreate the shader?
    if (publicAPI.getNeedToRebuildShaders(ren, actor, oglMapper)) {
      const shaders = { Vertex: null, Fragment: null, Geometry: null };
      oglMapper.buildShaders(shaders, ren, actor);
 
      // compile and bind the program if needed
      const newShader = model.CABO.getOpenGLRenderWindow()
        .getShaderCache()
        .readyShaderProgramArray(
          shaders.Vertex,
          shaders.Fragment,
          shaders.Geometry
        );
 
      // if the shader changed reinitialize the VAO
      if (newShader !== publicAPI.getProgram()) {
        publicAPI.setProgram(newShader);
        // reset the VAO as the shader has changed
        publicAPI.getVAO().releaseGraphicsResources();
      }
 
      publicAPI.getShaderSourceTime().modified();
    } else {
      model.CABO.getOpenGLRenderWindow()
        .getShaderCache()
        .readyShaderProgram(publicAPI.getProgram());
    }
 
    publicAPI.getVAO().bind();
 
    oglMapper.setMapperShaderParameters(publicAPI, ren, actor);
    oglMapper.setPropertyShaderParameters(publicAPI, ren, actor);
    oglMapper.setCameraShaderParameters(publicAPI, ren, actor);
    oglMapper.setLightingShaderParameters(publicAPI, ren, actor);
 
    oglMapper.invokeShaderCallbacks(publicAPI, ren, actor);
  };
 
  publicAPI.setMapperShaderParameters = (ren, actor, size) => {
    if (publicAPI.haveWideLines(ren, actor)) {
      publicAPI
        .getProgram()
        .setUniform2f('viewportSize', size.usize, size.vsize);
      const lineWidth = parseFloat(actor.getProperty().getLineWidth());
      const halfLineWidth = lineWidth / 2.0;
      publicAPI
        .getProgram()
        .setUniformf('lineWidthStepSize', lineWidth / Math.ceil(lineWidth));
      publicAPI.getProgram().setUniformf('halfLineWidth', halfLineWidth);
    }
    if (
      model.primitiveType === primTypes.Points ||
      actor.getProperty().getRepresentation() === Representation.POINTS
    ) {
      publicAPI
        .getProgram()
        .setUniformf('pointSize', actor.getProperty().getPointSize());
    } else if (model.pointPicking) {
      publicAPI
        .getProgram()
        .setUniformf('pointSize', publicAPI.getPointPickingPrimitiveSize());
    }
  };
 
  publicAPI.replaceShaderPositionVC = (shaders, ren, actor) => {
    let VSSource = shaders.Vertex;
 
    // Always set point size in case we need picking
    VSSource = vtkShaderProgram.substitute(VSSource, '//VTK::PositionVC::Dec', [
      '//VTK::PositionVC::Dec',
      'uniform float pointSize;',
    ]).result;
    VSSource = vtkShaderProgram.substitute(
      VSSource,
      '//VTK::PositionVC::Impl',
      ['//VTK::PositionVC::Impl', '  gl_PointSize = pointSize;'],
      false
    ).result;
 
    // for lines, make sure we add the width code
    if (
      publicAPI.getOpenGLMode(actor.getProperty().getRepresentation()) ===
        model.context.LINES &&
      publicAPI.haveWideLines(ren, actor)
    ) {
      VSSource = vtkShaderProgram.substitute(
        VSSource,
        '//VTK::PositionVC::Dec',
        [
          '//VTK::PositionVC::Dec',
          'uniform vec2 viewportSize;',
          'uniform float lineWidthStepSize;',
          'uniform float halfLineWidth;',
        ]
      ).result;
      VSSource = vtkShaderProgram.substitute(
        VSSource,
        '//VTK::PositionVC::Impl',
        [
          '//VTK::PositionVC::Impl',
          ' if (halfLineWidth > 0.0)',
          '   {',
          '   float offset = float(gl_InstanceID / 2) * lineWidthStepSize - halfLineWidth;',
          '   vec4 tmpPos = gl_Position;',
          '   vec3 tmpPos2 = tmpPos.xyz / tmpPos.w;',
          '   tmpPos2.x = tmpPos2.x + 2.0 * mod(float(gl_InstanceID), 2.0) * offset / viewportSize[0];',
          '   tmpPos2.y = tmpPos2.y + 2.0 * mod(float(gl_InstanceID + 1), 2.0) * offset / viewportSize[1];',
          '   gl_Position = vec4(tmpPos2.xyz * tmpPos.w, tmpPos.w);',
          '   }',
        ]
      ).result;
    }
    shaders.Vertex = VSSource;
  };
 
  publicAPI.getPointPickingPrimitiveSize = () => {
    Iif (model.primitiveType === primTypes.Points) {
      return 2;
    }
    if (model.primitiveType === primTypes.Lines) {
      return 4;
    }
    return 6;
  };
 
  publicAPI.getAllocatedGPUMemoryInBytes = () =>
    publicAPI.getCABO().getAllocatedGPUMemoryInBytes();
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  context: null,
  program: null,
  shaderSourceTime: null,
  VAO: null,
  attributeUpdateTime: null,
  CABO: null,
  primitiveType: 0,
  pointPicking: false,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
 
  model.shaderSourceTime = {};
  macro.obj(model.shaderSourceTime);
 
  model.attributeUpdateTime = {};
  macro.obj(model.attributeUpdateTime);
 
  macro.setGet(publicAPI, model, [
    'program',
    'shaderSourceTime',
    'VAO',
    'attributeUpdateTime',
    'CABO',
    'primitiveType',
    'pointPicking',
  ]);
 
  model.program = vtkShaderProgram.newInstance();
  model.VAO = vtkVertexArrayObject.newInstance();
  model.CABO = vtkCellArrayBufferObject.newInstance();
 
  // Object methods
  vtkOpenGLHelper(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, primTypes };