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

88.13% Statements 52/59
75.55% Branches 34/45
100% Functions 6/6
87.93% Lines 51/58

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              883x 2703x             2703x 2674x   2674x           2674x                                               2674x 2674x 2654x                 2654x           20x             2674x                   898x 5454x 5454x 10x     5454x 10x 14x 14x       7x 7x 7x           7x           898x 2727x   2727x         2727x 19x     2727x     2727x   2727x     2727x     898x 2727x   2727x 2727x 2727x 5x 5x       2727x   2727x 2727x 5x 5x       2727x   2727x 2727x 5x 5x       2727x                
import vtkShaderProgram from 'vtk.js/Sources/Rendering/OpenGL/ShaderProgram';
 
function implementReplaceShaderCoincidentOffset(
  publicAPI,
  model,
  initialValues = {}
) {
  publicAPI.replaceShaderCoincidentOffset = (shaders, ren, actor) => {
    const cp = publicAPI.getCoincidentParameters(ren, actor);
 
    // if we need an offset handle it here
    // The value of .000016 is suitable for depth buffers
    // of at least 16 bit depth. We do not query the depth
    // right now because we would need some mechanism to
    // cache the result taking into account FBO changes etc.
    if (cp && (cp.factor !== 0.0 || cp.offset !== 0.0)) {
      let FSSource = shaders.Fragment;
 
      FSSource = vtkShaderProgram.substitute(
        FSSource,
        '//VTK::Coincident::Dec',
        ['uniform float cfactor;', 'uniform float coffset;']
      ).result;
 
      Iif (model.context.getExtension('EXT_frag_depth')) {
        if (cp.factor !== 0.0) {
          FSSource = vtkShaderProgram.substitute(
            FSSource,
            '//VTK::UniformFlow::Impl',
            [
              'float cscale = length(vec2(dFdx(gl_FragCoord.z),dFdy(gl_FragCoord.z)));',
              '//VTK::UniformFlow::Impl',
            ],
            false
          ).result;
          FSSource = vtkShaderProgram.substitute(
            FSSource,
            '//VTK::Depth::Impl',
            'gl_FragDepthEXT = gl_FragCoord.z + cfactor*cscale + 0.000016*coffset;'
          ).result;
        } else {
          FSSource = vtkShaderProgram.substitute(
            FSSource,
            '//VTK::Depth::Impl',
            'gl_FragDepthEXT = gl_FragCoord.z + 0.000016*coffset;'
          ).result;
        }
      }
      if (model._openGLRenderWindow.getWebgl2()) {
        if (cp.factor !== 0.0) {
          FSSource = vtkShaderProgram.substitute(
            FSSource,
            '//VTK::UniformFlow::Impl',
            [
              'float cscale = length(vec2(dFdx(gl_FragCoord.z),dFdy(gl_FragCoord.z)));',
              '//VTK::UniformFlow::Impl',
            ],
            false
          ).result;
          FSSource = vtkShaderProgram.substitute(
            FSSource,
            '//VTK::Depth::Impl',
            'gl_FragDepth = gl_FragCoord.z + cfactor*cscale + 0.000016*coffset;'
          ).result;
        } else {
          FSSource = vtkShaderProgram.substitute(
            FSSource,
            '//VTK::Depth::Impl',
            'gl_FragDepth = gl_FragCoord.z + 0.000016*coffset;'
          ).result;
        }
      }
      shaders.Fragment = FSSource;
    }
  };
}
 
function implementBuildShadersWithReplacements(
  publicAPI,
  model,
  initialValues = {}
) {
  publicAPI.applyShaderReplacements = (shaders, viewSpec, pre) => {
    let shaderReplacements = null;
    if (viewSpec) {
      shaderReplacements = viewSpec.ShaderReplacements;
    }
 
    if (shaderReplacements) {
      for (let i = 0; i < shaderReplacements.length; i++) {
        const currReplacement = shaderReplacements[i];
        if (
          (pre && currReplacement.replaceFirst) ||
          (!pre && !currReplacement.replaceFirst)
        ) {
          const shaderType = currReplacement.shaderType;
          const ssrc = shaders[shaderType];
          const substituteRes = vtkShaderProgram.substitute(
            ssrc,
            currReplacement.originalValue,
            currReplacement.replacementValue,
            currReplacement.replaceAll
          );
          shaders[shaderType] = substituteRes.result;
        }
      }
    }
  };
 
  publicAPI.buildShaders = (shaders, ren, actor) => {
    publicAPI.getReplacedShaderTemplate(shaders, ren, actor);
 
    model.lastRenderPassShaderReplacement = model.currentRenderPass
      ? model.currentRenderPass.getShaderReplacement()
      : null;
 
    // apply any renderPassReplacements
    if (model.lastRenderPassShaderReplacement) {
      model.lastRenderPassShaderReplacement(shaders);
    }
 
    const openGLSpec = model.renderable.getViewSpecificProperties().OpenGL;
 
    // user specified pre replacements
    publicAPI.applyShaderReplacements(shaders, openGLSpec, true);
 
    publicAPI.replaceShaderValues(shaders, ren, actor);
 
    // user specified post replacements
    publicAPI.applyShaderReplacements(shaders, openGLSpec);
  };
 
  publicAPI.getReplacedShaderTemplate = (shaders, ren, actor) => {
    const openGLSpecProp = model.renderable.getViewSpecificProperties().OpenGL;
 
    publicAPI.getShaderTemplate(shaders, ren, actor);
    let vertexShaderCode = shaders.Vertex;
    if (openGLSpecProp) {
      const vertexSpecProp = openGLSpecProp.VertexShaderCode;
      Iif (vertexSpecProp !== undefined && vertexSpecProp !== '') {
        vertexShaderCode = vertexSpecProp;
      }
    }
    shaders.Vertex = vertexShaderCode;
 
    let fragmentShaderCode = shaders.Fragment;
    if (openGLSpecProp) {
      const fragmentSpecProp = openGLSpecProp.FragmentShaderCode;
      Iif (fragmentSpecProp !== undefined && fragmentSpecProp !== '') {
        fragmentShaderCode = fragmentSpecProp;
      }
    }
    shaders.Fragment = fragmentShaderCode;
 
    let geometryShaderCode = shaders.Geometry;
    if (openGLSpecProp) {
      const geometrySpecProp = openGLSpecProp.GeometryShaderCode;
      Iif (geometrySpecProp !== undefined) {
        geometryShaderCode = geometrySpecProp;
      }
    }
    shaders.Geometry = geometryShaderCode;
  };
}
 
export default {
  implementReplaceShaderCoincidentOffset,
  implementBuildShadersWithReplacements,
};