All files / engine/Source/Renderer ShaderSource.js

98.39% Statements 184/187
88.75% Branches 71/80
100% Functions 24/24
98.31% Lines 175/178

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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525                  110005x   110005x   69512x 69512x 69512x 706223x   69512x               122467x 2214072x 17934x       122467x     104533x     104533x             104533x     122467x       122467x 17934x     104533x     104533x 104533x   103449x 378593x     103449x 231969x       116997x         116997x 116997x     116997x             5470x 5470x   5470x 104533x 104533x   104533x 5470x       5470x 104531x   104531x   104531x   116995x 116995x 116995x     116995x 99061x           5470x 5470x 104533x 2x         5470x   1x 1x 2x   1x             5470x 5470x 5470x 5470x       5469x 5469x 104530x     5469x               5472x 5472x 5472x 5472x   7539x       5472x       5472x       5x               5x           5x         5472x 5472x       1x       1x         5472x           5472x 5472x 18x             5472x   5472x 5472x 1x     5472x       2734x                         5472x 5472x 5472x 20670x 20670x 19863x           5472x         5472x 5464x       5472x 5472x 5470x       5471x     5471x 5471x                 1x     5471x 5471x     5471x 5463x   8x     5471x                                                           11829x 11829x     11829x         1x           11828x 11828x 11828x 11828x     1x 274x               1x 2818x 2818x                       1x   10455x 10455x 10455x 10455x 10455x   10455x                   1x 2738x                   1x 2734x             1x     1x 138x 138x       1x 90x 90x 90x 90x           1x 62x         62x               62x     1x       18x         18x                   18x       10x 10x 10x 49x 9x     1x       53x 53x 53x 78x 48x     5x       38x 38x 38x 38x 38x           1x   1x     15x 10x 9x   1x     5x     1x   1x 33x      
import Frozen from "../Core/Frozen.js";
import defined from "../Core/defined.js";
import DeveloperError from "../Core/DeveloperError.js";
import CzmBuiltins from "../Shaders/Builtin/CzmBuiltins.js";
import AutomaticUniforms from "./AutomaticUniforms.js";
import demodernizeShader from "./demodernizeShader.js";
 
function removeComments(source) {
  // remove inline comments
  source = source.replace(/\/\/.*/g, "");
  // remove multiline comment block
  return source.replace(/\/\*\*[\s\S]*?\*\//gm, function (match) {
    // preserve the number of lines in the comment block so the line numbers will be correct when debugging shaders
    const numberOfLines = match.match(/\n/gm).length;
    let replacement = "";
    for (let lineNumber = 0; lineNumber < numberOfLines; ++lineNumber) {
      replacement += "\n";
    }
    return replacement;
  });
}
 
function getDependencyNode(name, glslSource, nodes) {
  let dependencyNode;
 
  // check if already loaded
  for (let i = 0; i < nodes.length; ++i) {
    if (nodes[i].name === name) {
      dependencyNode = nodes[i];
    }
  }
 
  if (!defined(dependencyNode)) {
    // strip doc comments so we don't accidentally try to determine a dependency for something found
    // in a comment
    glslSource = removeComments(glslSource);
 
    // create new node
    dependencyNode = {
      name: name,
      glslSource: glslSource,
      dependsOn: [],
      requiredBy: [],
      evaluated: false,
    };
    nodes.push(dependencyNode);
  }
 
  return dependencyNode;
}
 
function generateDependencies(currentNode, dependencyNodes) {
  if (currentNode.evaluated) {
    return;
  }
 
  currentNode.evaluated = true;
 
  // identify all dependencies that are referenced from this glsl source code
  let czmMatches = currentNode.glslSource.match(/\bczm_[a-zA-Z0-9_]*/g);
  if (defined(czmMatches) && czmMatches !== null) {
    // remove duplicates
    czmMatches = czmMatches.filter(function (elem, pos) {
      return czmMatches.indexOf(elem) === pos;
    });
 
    czmMatches.forEach(function (element) {
      if (
        element !== currentNode.name &&
        ShaderSource._czmBuiltinsAndUniforms.hasOwnProperty(element)
      ) {
        const referencedNode = getDependencyNode(
          element,
          ShaderSource._czmBuiltinsAndUniforms[element],
          dependencyNodes,
        );
        currentNode.dependsOn.push(referencedNode);
        referencedNode.requiredBy.push(currentNode);
 
        // recursive call to find any dependencies of the new node
        generateDependencies(referencedNode, dependencyNodes);
      }
    });
  }
}
 
function sortDependencies(dependencyNodes) {
  const nodesWithoutIncomingEdges = [];
  const allNodes = [];
 
  while (dependencyNodes.length > 0) {
    const node = dependencyNodes.pop();
    allNodes.push(node);
 
    if (node.requiredBy.length === 0) {
      nodesWithoutIncomingEdges.push(node);
    }
  }
 
  while (nodesWithoutIncomingEdges.length > 0) {
    const currentNode = nodesWithoutIncomingEdges.shift();
 
    dependencyNodes.push(currentNode);
 
    for (let i = 0; i < currentNode.dependsOn.length; ++i) {
      // remove the edge from the graph
      const referencedNode = currentNode.dependsOn[i];
      const index = referencedNode.requiredBy.indexOf(currentNode);
      referencedNode.requiredBy.splice(index, 1);
 
      // if referenced node has no more incoming edges, add to list
      if (referencedNode.requiredBy.length === 0) {
        nodesWithoutIncomingEdges.push(referencedNode);
      }
    }
  }
 
  // if there are any nodes left with incoming edges, then there was a circular dependency somewhere in the graph
  const badNodes = [];
  for (let j = 0; j < allNodes.length; ++j) {
    if (allNodes[j].requiredBy.length !== 0) {
      badNodes.push(allNodes[j]);
    }
  }
 
  //>>includeStart('debug', pragmas.debug);
  if (badNodes.length !== 0) {
    let message =
      "A circular dependency was found in the following built-in functions/structs/constants: \n";
    for (let k = 0; k < badNodes.length; ++k) {
      message = `${message + badNodes[k].name}\n`;
    }
    throw new DeveloperError(message);
  }
  //>>includeEnd('debug');
}
 
function getBuiltinsAndAutomaticUniforms(shaderSource) {
  // generate a dependency graph for builtin functions
  const dependencyNodes = [];
  const root = getDependencyNode("main", shaderSource, dependencyNodes);
  generateDependencies(root, dependencyNodes);
  sortDependencies(dependencyNodes);
 
  // Concatenate the source code for the function dependencies.
  // Iterate in reverse so that dependent items are declared before they are used.
  let builtinsSource = "";
  for (let i = dependencyNodes.length - 1; i >= 0; --i) {
    builtinsSource = `${builtinsSource + dependencyNodes[i].glslSource}\n`;
  }
 
  return builtinsSource.replace(root.glslSource, "");
}
 
function combineShader(shaderSource, isFragmentShader, context) {
  let i;
  let length;
 
  // Combine shader sources, generally for pseudo-polymorphism, e.g., czm_getMaterial.
  let combinedSources = "";
  const sources = shaderSource.sources;
  Eif (defined(sources)) {
    for (i = 0, length = sources.length; i < length; ++i) {
      // #line needs to be on its own line.
      combinedSources += `\n#line 0\n${sources[i]}`;
    }
  }
 
  combinedSources = removeComments(combinedSources);
 
  // Extract existing shader version from sources
  let version;
  combinedSources = combinedSources.replace(
    /#version\s+(.*?)\n/gm,
    function (match, group1) {
      //>>includeStart('debug', pragmas.debug);
      Iif (defined(version) && version !== group1) {
        throw new DeveloperError(
          `inconsistent versions found: ${version} and ${group1}`,
        );
      }
      //>>includeEnd('debug');
 
      // Extract #version to put at the top
      version = group1;
 
      // Replace original #version directive with a new line so the line numbers
      // are not off by one.  There can be only one #version directive
      // and it must appear at the top of the source, only preceded by
      // whitespace and comments.
      return "\n";
    },
  );
 
  // Extract shader extensions from sources
  const extensions = [];
  combinedSources = combinedSources.replace(
    /#extension.*\n/gm,
    function (match) {
      // Extract extension to put at the top
      extensions.push(match);
 
      // Replace original #extension directive with a new line so the line numbers
      // are not off by one.
      return "\n";
    },
  );
 
  // Remove precision qualifier
  combinedSources = combinedSources.replace(
    /precision\s(lowp|mediump|highp)\s(float|int);/,
    "",
  );
 
  // Replace main() for picked if desired.
  const pickColorQualifier = shaderSource.pickColorQualifier;
  if (defined(pickColorQualifier)) {
    combinedSources = ShaderSource.createPickFragmentShaderSource(
      combinedSources,
      pickColorQualifier,
    );
  }
 
  // combine into single string
  let result = "";
 
  const extensionsLength = extensions.length;
  for (i = 0; i < extensionsLength; i++) {
    result += extensions[i];
  }
 
  if (isFragmentShader) {
    // If high precision isn't support replace occurrences of highp with mediump
    // The highp keyword is not always available on older mobile devices
    // See https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/WebGL_best_practices#In_WebGL_1_highp_float_support_is_optional_in_fragment_shaders
    result +=
      "\
#ifdef GL_FRAGMENT_PRECISION_HIGH\n\
    precision highp float;\n\
    precision highp int;\n\
#else\n\
    precision mediump float;\n\
    precision mediump int;\n\
    #define highp mediump\n\
#endif\n\n";
  }
 
  // Prepend #defines for uber-shaders
  const defines = shaderSource.defines;
  Eif (defined(defines)) {
    for (i = 0, length = defines.length; i < length; ++i) {
      const define = defines[i];
      if (define.length !== 0) {
        result += `#define ${define}\n`;
      }
    }
  }
 
  // Define a constant for the OES_texture_float_linear extension since WebGL does not.
  Iif (context.textureFloatLinear) {
    result += "#define OES_texture_float_linear\n\n";
  }
 
  // Define a constant for the OES_texture_float extension since WebGL does not.
  if (context.floatingPointTexture) {
    result += "#define OES_texture_float\n\n";
  }
 
  // append built-ins
  let builtinSources = "";
  if (shaderSource.includeBuiltIns) {
    builtinSources = getBuiltinsAndAutomaticUniforms(combinedSources);
  }
 
  // reset line number
  result += "\n#line 0\n";
 
  // append actual source
  const combinedShader = builtinSources + combinedSources;
  if (
    context.webgl2 &&
    isFragmentShader &&
    !/layout\s*\(location\s*=\s*0\)\s*out\s+vec4\s+out_FragColor;/g.test(
      combinedShader,
    ) &&
    !/czm_out_FragColor/g.test(combinedShader) &&
    /out_FragColor/g.test(combinedShader)
  ) {
    result += "layout(location = 0) out vec4 out_FragColor;\n\n";
  }
 
  result += builtinSources;
  result += combinedSources;
 
  // modernize the source
  if (!context.webgl2) {
    result = demodernizeShader(result, isFragmentShader);
  } else {
    result = `#version 300 es\n${result}`;
  }
 
  return result;
}
 
/**
 * An object containing various inputs that will be combined to form a final GLSL shader string.
 *
 * @param {object} [options] Object with the following properties:
 * @param {string[]} [options.sources] An array of strings to combine containing GLSL code for the shader.
 * @param {string[]} [options.defines] An array of strings containing GLSL identifiers to <code>#define</code>.
 * @param {string} [options.pickColorQualifier] The GLSL qualifier, <code>uniform</code> or <code>in</code>, for the input <code>czm_pickColor</code>.  When defined, a pick fragment shader is generated.
 * @param {boolean} [options.includeBuiltIns=true] If true, referenced built-in functions will be included with the combined shader.  Set to false if this shader will become a source in another shader, to avoid duplicating functions.
 *
 * @exception {DeveloperError} options.pickColorQualifier must be 'uniform' or 'in'.
 *
 * @example
 * // 1. Prepend #defines to a shader
 * const source = new Cesium.ShaderSource({
 *   defines : ['WHITE'],
 *   sources : ['void main() { \n#ifdef WHITE\n out_FragColor = vec4(1.0); \n#else\n out_FragColor = vec4(0.0); \n#endif\n }']
 * });
 *
 * // 2. Modify a fragment shader for picking
 * const source2 = new Cesium.ShaderSource({
 *   sources : ['void main() { out_FragColor = vec4(1.0); }'],
 *   pickColorQualifier : 'uniform'
 * });
 *
 * @private
 */
function ShaderSource(options) {
  options = options ?? Frozen.EMPTY_OBJECT;
  const pickColorQualifier = options.pickColorQualifier;
 
  //>>includeStart('debug', pragmas.debug);
  if (
    defined(pickColorQualifier) &&
    pickColorQualifier !== "uniform" &&
    pickColorQualifier !== "in"
  ) {
    throw new DeveloperError(
      "options.pickColorQualifier must be 'uniform' or 'in'.",
    );
  }
  //>>includeEnd('debug');
 
  this.defines = defined(options.defines) ? options.defines.slice(0) : [];
  this.sources = defined(options.sources) ? options.sources.slice(0) : [];
  this.pickColorQualifier = pickColorQualifier;
  this.includeBuiltIns = options.includeBuiltIns ?? true;
}
 
ShaderSource.prototype.clone = function () {
  return new ShaderSource({
    sources: this.sources,
    defines: this.defines,
    pickColorQualifier: this.pickColorQualifier,
    includeBuiltIns: this.includeBuiltIns,
  });
};
 
ShaderSource.replaceMain = function (source, renamedMain) {
  renamedMain = `void ${renamedMain}()`;
  return source.replace(/void\s+main\s*\(\s*(?:void)?\s*\)/g, renamedMain);
};
 
/**
 * Since {@link ShaderSource#createCombinedVertexShader} and
 * {@link ShaderSource#createCombinedFragmentShader} are both expensive to
 * compute, create a simpler string key for lookups in the {@link ShaderCache}.
 *
 * @returns {string} A key for identifying this shader
 *
 * @private
 */
ShaderSource.prototype.getCacheKey = function () {
  // Sort defines to make the key comparison deterministic
  const sortedDefines = this.defines.slice().sort();
  const definesKey = sortedDefines.join(",");
  const pickKey = this.pickColorQualifier;
  const builtinsKey = this.includeBuiltIns;
  const sourcesKey = this.sources.join("\n");
 
  return `${definesKey}:${pickKey}:${builtinsKey}:${sourcesKey}`;
};
 
/**
 * Create a single string containing the full, combined vertex shader with all dependencies and defines.
 *
 * @param {Context} context The current rendering context
 *
 * @returns {string} The combined shader string.
 */
ShaderSource.prototype.createCombinedVertexShader = function (context) {
  return combineShader(this, false, context);
};
 
/**
 * Create a single string containing the full, combined fragment shader with all dependencies and defines.
 *
 * @param {Context} context The current rendering context
 *
 * @returns {string} The combined shader string.
 */
ShaderSource.prototype.createCombinedFragmentShader = function (context) {
  return combineShader(this, true, context);
};
 
/**
 * For ShaderProgram testing
 * @private
 */
ShaderSource._czmBuiltinsAndUniforms = {};
 
// combine automatic uniforms and Cesium built-ins
for (const builtinName in CzmBuiltins) {
  Eif (CzmBuiltins.hasOwnProperty(builtinName)) {
    ShaderSource._czmBuiltinsAndUniforms[builtinName] =
      CzmBuiltins[builtinName];
  }
}
for (const uniformName in AutomaticUniforms) {
  Eif (AutomaticUniforms.hasOwnProperty(uniformName)) {
    const uniform = AutomaticUniforms[uniformName];
    Eif (typeof uniform.getDeclaration === "function") {
      ShaderSource._czmBuiltinsAndUniforms[uniformName] =
        uniform.getDeclaration(uniformName);
    }
  }
}
 
ShaderSource.createPickVertexShaderSource = function (vertexShaderSource) {
  const renamedVS = ShaderSource.replaceMain(
    vertexShaderSource,
    "czm_old_main",
  );
  const pickMain =
    "in vec4 pickColor; \n" +
    "out vec4 czm_pickColor; \n" +
    "void main() \n" +
    "{ \n" +
    "    czm_old_main(); \n" +
    "    czm_pickColor = pickColor; \n" +
    "}";
 
  return `${renamedVS}\n${pickMain}`;
};
 
ShaderSource.createPickFragmentShaderSource = function (
  fragmentShaderSource,
  pickColorQualifier,
) {
  const renamedFS = ShaderSource.replaceMain(
    fragmentShaderSource,
    "czm_old_main",
  );
  const pickMain =
    `${pickColorQualifier} vec4 czm_pickColor; \n` +
    `void main() \n` +
    `{ \n` +
    `    czm_old_main(); \n` +
    `    if (out_FragColor.a == 0.0) { \n` +
    `       discard; \n` +
    `    } \n` +
    `    out_FragColor = czm_pickColor; \n` +
    `}`;
 
  return `${renamedFS}\n${pickMain}`;
};
 
function containsDefine(shaderSource, define) {
  const defines = shaderSource.defines;
  const definesLength = defines.length;
  for (let i = 0; i < definesLength; ++i) {
    if (defines[i] === define) {
      return true;
    }
  }
  return false;
}
 
function containsString(shaderSource, string) {
  const sources = shaderSource.sources;
  const sourcesLength = sources.length;
  for (let i = 0; i < sourcesLength; ++i) {
    if (sources[i].indexOf(string) !== -1) {
      return true;
    }
  }
  return false;
}
 
function findFirstString(shaderSource, strings) {
  const stringsLength = strings.length;
  for (let i = 0; i < stringsLength; ++i) {
    const string = strings[i];
    Eif (containsString(shaderSource, string)) {
      return string;
    }
  }
  return undefined;
}
 
const normalVaryingNames = ["v_normalEC", "v_normal"];
 
ShaderSource.findNormalVarying = function (shaderSource) {
  // Fix for Model: the shader text always has the word v_normalEC
  // wrapped in an #ifdef so instead of looking for v_normalEC look for the define
  if (containsString(shaderSource, "#ifdef HAS_NORMALS")) {
    if (containsDefine(shaderSource, "HAS_NORMALS")) {
      return "v_normalEC";
    }
    return undefined;
  }
 
  return findFirstString(shaderSource, normalVaryingNames);
};
 
const positionVaryingNames = ["v_positionEC"];
 
ShaderSource.findPositionVarying = function (shaderSource) {
  return findFirstString(shaderSource, positionVaryingNames);
};
export default ShaderSource;