ShaderSource.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import CzmBuiltins from "../Shaders/Builtin/CzmBuiltins.js";
  5. import AutomaticUniforms from "./AutomaticUniforms.js";
  6. import demodernizeShader from "./demodernizeShader.js";
  7. function removeComments(source) {
  8. // remove inline comments
  9. source = source.replace(/\/\/.*/g, "");
  10. // remove multiline comment block
  11. return source.replace(/\/\*\*[\s\S]*?\*\//gm, function (match) {
  12. // preserve the number of lines in the comment block so the line numbers will be correct when debugging shaders
  13. const numberOfLines = match.match(/\n/gm).length;
  14. let replacement = "";
  15. for (let lineNumber = 0; lineNumber < numberOfLines; ++lineNumber) {
  16. replacement += "\n";
  17. }
  18. return replacement;
  19. });
  20. }
  21. function getDependencyNode(name, glslSource, nodes) {
  22. let dependencyNode;
  23. // check if already loaded
  24. for (let i = 0; i < nodes.length; ++i) {
  25. if (nodes[i].name === name) {
  26. dependencyNode = nodes[i];
  27. }
  28. }
  29. if (!defined(dependencyNode)) {
  30. // strip doc comments so we don't accidentally try to determine a dependency for something found
  31. // in a comment
  32. glslSource = removeComments(glslSource);
  33. // create new node
  34. dependencyNode = {
  35. name: name,
  36. glslSource: glslSource,
  37. dependsOn: [],
  38. requiredBy: [],
  39. evaluated: false,
  40. };
  41. nodes.push(dependencyNode);
  42. }
  43. return dependencyNode;
  44. }
  45. function generateDependencies(currentNode, dependencyNodes) {
  46. if (currentNode.evaluated) {
  47. return;
  48. }
  49. currentNode.evaluated = true;
  50. // identify all dependencies that are referenced from this glsl source code
  51. let czmMatches = currentNode.glslSource.match(/\bczm_[a-zA-Z0-9_]*/g);
  52. if (defined(czmMatches) && czmMatches !== null) {
  53. // remove duplicates
  54. czmMatches = czmMatches.filter(function (elem, pos) {
  55. return czmMatches.indexOf(elem) === pos;
  56. });
  57. czmMatches.forEach(function (element) {
  58. if (
  59. element !== currentNode.name &&
  60. ShaderSource._czmBuiltinsAndUniforms.hasOwnProperty(element)
  61. ) {
  62. const referencedNode = getDependencyNode(
  63. element,
  64. ShaderSource._czmBuiltinsAndUniforms[element],
  65. dependencyNodes
  66. );
  67. currentNode.dependsOn.push(referencedNode);
  68. referencedNode.requiredBy.push(currentNode);
  69. // recursive call to find any dependencies of the new node
  70. generateDependencies(referencedNode, dependencyNodes);
  71. }
  72. });
  73. }
  74. }
  75. function sortDependencies(dependencyNodes) {
  76. const nodesWithoutIncomingEdges = [];
  77. const allNodes = [];
  78. while (dependencyNodes.length > 0) {
  79. const node = dependencyNodes.pop();
  80. allNodes.push(node);
  81. if (node.requiredBy.length === 0) {
  82. nodesWithoutIncomingEdges.push(node);
  83. }
  84. }
  85. while (nodesWithoutIncomingEdges.length > 0) {
  86. const currentNode = nodesWithoutIncomingEdges.shift();
  87. dependencyNodes.push(currentNode);
  88. for (let i = 0; i < currentNode.dependsOn.length; ++i) {
  89. // remove the edge from the graph
  90. const referencedNode = currentNode.dependsOn[i];
  91. const index = referencedNode.requiredBy.indexOf(currentNode);
  92. referencedNode.requiredBy.splice(index, 1);
  93. // if referenced node has no more incoming edges, add to list
  94. if (referencedNode.requiredBy.length === 0) {
  95. nodesWithoutIncomingEdges.push(referencedNode);
  96. }
  97. }
  98. }
  99. // if there are any nodes left with incoming edges, then there was a circular dependency somewhere in the graph
  100. const badNodes = [];
  101. for (let j = 0; j < allNodes.length; ++j) {
  102. if (allNodes[j].requiredBy.length !== 0) {
  103. badNodes.push(allNodes[j]);
  104. }
  105. }
  106. //>>includeStart('debug', pragmas.debug);
  107. if (badNodes.length !== 0) {
  108. let message =
  109. "A circular dependency was found in the following built-in functions/structs/constants: \n";
  110. for (let k = 0; k < badNodes.length; ++k) {
  111. message = `${message + badNodes[k].name}\n`;
  112. }
  113. throw new DeveloperError(message);
  114. }
  115. //>>includeEnd('debug');
  116. }
  117. function getBuiltinsAndAutomaticUniforms(shaderSource) {
  118. // generate a dependency graph for builtin functions
  119. const dependencyNodes = [];
  120. const root = getDependencyNode("main", shaderSource, dependencyNodes);
  121. generateDependencies(root, dependencyNodes);
  122. sortDependencies(dependencyNodes);
  123. // Concatenate the source code for the function dependencies.
  124. // Iterate in reverse so that dependent items are declared before they are used.
  125. let builtinsSource = "";
  126. for (let i = dependencyNodes.length - 1; i >= 0; --i) {
  127. builtinsSource = `${builtinsSource + dependencyNodes[i].glslSource}\n`;
  128. }
  129. return builtinsSource.replace(root.glslSource, "");
  130. }
  131. function combineShader(shaderSource, isFragmentShader, context) {
  132. let i;
  133. let length;
  134. // Combine shader sources, generally for pseudo-polymorphism, e.g., czm_getMaterial.
  135. let combinedSources = "";
  136. const sources = shaderSource.sources;
  137. if (defined(sources)) {
  138. for (i = 0, length = sources.length; i < length; ++i) {
  139. // #line needs to be on its own line.
  140. combinedSources += `\n#line 0\n${sources[i]}`;
  141. }
  142. }
  143. combinedSources = removeComments(combinedSources);
  144. // Extract existing shader version from sources
  145. let version;
  146. combinedSources = combinedSources.replace(/#version\s+(.*?)\n/gm, function (
  147. match,
  148. group1
  149. ) {
  150. //>>includeStart('debug', pragmas.debug);
  151. if (defined(version) && version !== group1) {
  152. throw new DeveloperError(
  153. `inconsistent versions found: ${version} and ${group1}`
  154. );
  155. }
  156. //>>includeEnd('debug');
  157. // Extract #version to put at the top
  158. version = group1;
  159. // Replace original #version directive with a new line so the line numbers
  160. // are not off by one. There can be only one #version directive
  161. // and it must appear at the top of the source, only preceded by
  162. // whitespace and comments.
  163. return "\n";
  164. });
  165. // Extract shader extensions from sources
  166. const extensions = [];
  167. combinedSources = combinedSources.replace(/#extension.*\n/gm, function (
  168. match
  169. ) {
  170. // Extract extension to put at the top
  171. extensions.push(match);
  172. // Replace original #extension directive with a new line so the line numbers
  173. // are not off by one.
  174. return "\n";
  175. });
  176. // Remove precision qualifier
  177. combinedSources = combinedSources.replace(
  178. /precision\s(lowp|mediump|highp)\s(float|int);/,
  179. ""
  180. );
  181. // Replace main() for picked if desired.
  182. const pickColorQualifier = shaderSource.pickColorQualifier;
  183. if (defined(pickColorQualifier)) {
  184. combinedSources = ShaderSource.createPickFragmentShaderSource(
  185. combinedSources,
  186. pickColorQualifier
  187. );
  188. }
  189. // combine into single string
  190. let result = "";
  191. const extensionsLength = extensions.length;
  192. for (i = 0; i < extensionsLength; i++) {
  193. result += extensions[i];
  194. }
  195. if (isFragmentShader) {
  196. // If high precision isn't support replace occurrences of highp with mediump
  197. // The highp keyword is not always available on older mobile devices
  198. // 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
  199. result +=
  200. "\
  201. #ifdef GL_FRAGMENT_PRECISION_HIGH\n\
  202. precision highp float;\n\
  203. precision highp int;\n\
  204. #else\n\
  205. precision mediump float;\n\
  206. precision mediump int;\n\
  207. #define highp mediump\n\
  208. #endif\n\n";
  209. }
  210. // Prepend #defines for uber-shaders
  211. const defines = shaderSource.defines;
  212. if (defined(defines)) {
  213. for (i = 0, length = defines.length; i < length; ++i) {
  214. const define = defines[i];
  215. if (define.length !== 0) {
  216. result += `#define ${define}\n`;
  217. }
  218. }
  219. }
  220. // Define a constant for the OES_texture_float_linear extension since WebGL does not.
  221. if (context.textureFloatLinear) {
  222. result += "#define OES_texture_float_linear\n\n";
  223. }
  224. // Define a constant for the OES_texture_float extension since WebGL does not.
  225. if (context.floatingPointTexture) {
  226. result += "#define OES_texture_float\n\n";
  227. }
  228. // append built-ins
  229. let builtinSources = "";
  230. if (shaderSource.includeBuiltIns) {
  231. builtinSources = getBuiltinsAndAutomaticUniforms(combinedSources);
  232. }
  233. // reset line number
  234. result += "\n#line 0\n";
  235. // append actual source
  236. const combinedShader = builtinSources + combinedSources;
  237. if (
  238. context.webgl2 &&
  239. isFragmentShader &&
  240. !/layout\s*\(location\s*=\s*0\)\s*out\s+vec4\s+out_FragColor;/g.test(
  241. combinedShader
  242. ) &&
  243. !/czm_out_FragColor/g.test(combinedShader) &&
  244. /out_FragColor/g.test(combinedShader)
  245. ) {
  246. result += "layout(location = 0) out vec4 out_FragColor;\n\n";
  247. }
  248. result += builtinSources;
  249. result += combinedSources;
  250. // modernize the source
  251. if (!context.webgl2) {
  252. result = demodernizeShader(result, isFragmentShader);
  253. } else {
  254. result = `#version 300 es\n${result}`;
  255. }
  256. return result;
  257. }
  258. /**
  259. * An object containing various inputs that will be combined to form a final GLSL shader string.
  260. *
  261. * @param {object} [options] Object with the following properties:
  262. * @param {string[]} [options.sources] An array of strings to combine containing GLSL code for the shader.
  263. * @param {string[]} [options.defines] An array of strings containing GLSL identifiers to <code>#define</code>.
  264. * @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.
  265. * @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.
  266. *
  267. * @exception {DeveloperError} options.pickColorQualifier must be 'uniform' or 'in'.
  268. *
  269. * @example
  270. * // 1. Prepend #defines to a shader
  271. * const source = new Cesium.ShaderSource({
  272. * defines : ['WHITE'],
  273. * sources : ['void main() { \n#ifdef WHITE\n out_FragColor = vec4(1.0); \n#else\n out_FragColor = vec4(0.0); \n#endif\n }']
  274. * });
  275. *
  276. * // 2. Modify a fragment shader for picking
  277. * const source2 = new Cesium.ShaderSource({
  278. * sources : ['void main() { out_FragColor = vec4(1.0); }'],
  279. * pickColorQualifier : 'uniform'
  280. * });
  281. *
  282. * @private
  283. */
  284. function ShaderSource(options) {
  285. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  286. const pickColorQualifier = options.pickColorQualifier;
  287. //>>includeStart('debug', pragmas.debug);
  288. if (
  289. defined(pickColorQualifier) &&
  290. pickColorQualifier !== "uniform" &&
  291. pickColorQualifier !== "in"
  292. ) {
  293. throw new DeveloperError(
  294. "options.pickColorQualifier must be 'uniform' or 'in'."
  295. );
  296. }
  297. //>>includeEnd('debug');
  298. this.defines = defined(options.defines) ? options.defines.slice(0) : [];
  299. this.sources = defined(options.sources) ? options.sources.slice(0) : [];
  300. this.pickColorQualifier = pickColorQualifier;
  301. this.includeBuiltIns = defaultValue(options.includeBuiltIns, true);
  302. }
  303. ShaderSource.prototype.clone = function () {
  304. return new ShaderSource({
  305. sources: this.sources,
  306. defines: this.defines,
  307. pickColorQualifier: this.pickColorQualifier,
  308. includeBuiltIns: this.includeBuiltIns,
  309. });
  310. };
  311. ShaderSource.replaceMain = function (source, renamedMain) {
  312. renamedMain = `void ${renamedMain}()`;
  313. return source.replace(/void\s+main\s*\(\s*(?:void)?\s*\)/g, renamedMain);
  314. };
  315. /**
  316. * Since {@link ShaderSource#createCombinedVertexShader} and
  317. * {@link ShaderSource#createCombinedFragmentShader} are both expensive to
  318. * compute, create a simpler string key for lookups in the {@link ShaderCache}.
  319. *
  320. * @returns {string} A key for identifying this shader
  321. *
  322. * @private
  323. */
  324. ShaderSource.prototype.getCacheKey = function () {
  325. // Sort defines to make the key comparison deterministic
  326. const sortedDefines = this.defines.slice().sort();
  327. const definesKey = sortedDefines.join(",");
  328. const pickKey = this.pickColorQualifier;
  329. const builtinsKey = this.includeBuiltIns;
  330. const sourcesKey = this.sources.join("\n");
  331. return `${definesKey}:${pickKey}:${builtinsKey}:${sourcesKey}`;
  332. };
  333. /**
  334. * Create a single string containing the full, combined vertex shader with all dependencies and defines.
  335. *
  336. * @param {Context} context The current rendering context
  337. *
  338. * @returns {string} The combined shader string.
  339. */
  340. ShaderSource.prototype.createCombinedVertexShader = function (context) {
  341. return combineShader(this, false, context);
  342. };
  343. /**
  344. * Create a single string containing the full, combined fragment shader with all dependencies and defines.
  345. *
  346. * @param {Context} context The current rendering context
  347. *
  348. * @returns {string} The combined shader string.
  349. */
  350. ShaderSource.prototype.createCombinedFragmentShader = function (context) {
  351. return combineShader(this, true, context);
  352. };
  353. /**
  354. * For ShaderProgram testing
  355. * @private
  356. */
  357. ShaderSource._czmBuiltinsAndUniforms = {};
  358. // combine automatic uniforms and Cesium built-ins
  359. for (const builtinName in CzmBuiltins) {
  360. if (CzmBuiltins.hasOwnProperty(builtinName)) {
  361. ShaderSource._czmBuiltinsAndUniforms[builtinName] =
  362. CzmBuiltins[builtinName];
  363. }
  364. }
  365. for (const uniformName in AutomaticUniforms) {
  366. if (AutomaticUniforms.hasOwnProperty(uniformName)) {
  367. const uniform = AutomaticUniforms[uniformName];
  368. if (typeof uniform.getDeclaration === "function") {
  369. ShaderSource._czmBuiltinsAndUniforms[
  370. uniformName
  371. ] = uniform.getDeclaration(uniformName);
  372. }
  373. }
  374. }
  375. ShaderSource.createPickVertexShaderSource = function (vertexShaderSource) {
  376. const renamedVS = ShaderSource.replaceMain(
  377. vertexShaderSource,
  378. "czm_old_main"
  379. );
  380. const pickMain =
  381. "in vec4 pickColor; \n" +
  382. "out vec4 czm_pickColor; \n" +
  383. "void main() \n" +
  384. "{ \n" +
  385. " czm_old_main(); \n" +
  386. " czm_pickColor = pickColor; \n" +
  387. "}";
  388. return `${renamedVS}\n${pickMain}`;
  389. };
  390. ShaderSource.createPickFragmentShaderSource = function (
  391. fragmentShaderSource,
  392. pickColorQualifier
  393. ) {
  394. const renamedFS = ShaderSource.replaceMain(
  395. fragmentShaderSource,
  396. "czm_old_main"
  397. );
  398. const pickMain =
  399. `${pickColorQualifier} vec4 czm_pickColor; \n` +
  400. `void main() \n` +
  401. `{ \n` +
  402. ` czm_old_main(); \n` +
  403. ` if (out_FragColor.a == 0.0) { \n` +
  404. ` discard; \n` +
  405. ` } \n` +
  406. ` out_FragColor = czm_pickColor; \n` +
  407. `}`;
  408. return `${renamedFS}\n${pickMain}`;
  409. };
  410. function containsDefine(shaderSource, define) {
  411. const defines = shaderSource.defines;
  412. const definesLength = defines.length;
  413. for (let i = 0; i < definesLength; ++i) {
  414. if (defines[i] === define) {
  415. return true;
  416. }
  417. }
  418. return false;
  419. }
  420. function containsString(shaderSource, string) {
  421. const sources = shaderSource.sources;
  422. const sourcesLength = sources.length;
  423. for (let i = 0; i < sourcesLength; ++i) {
  424. if (sources[i].indexOf(string) !== -1) {
  425. return true;
  426. }
  427. }
  428. return false;
  429. }
  430. function findFirstString(shaderSource, strings) {
  431. const stringsLength = strings.length;
  432. for (let i = 0; i < stringsLength; ++i) {
  433. const string = strings[i];
  434. if (containsString(shaderSource, string)) {
  435. return string;
  436. }
  437. }
  438. return undefined;
  439. }
  440. const normalVaryingNames = ["v_normalEC", "v_normal"];
  441. ShaderSource.findNormalVarying = function (shaderSource) {
  442. // Fix for Model: the shader text always has the word v_normalEC
  443. // wrapped in an #ifdef so instead of looking for v_normalEC look for the define
  444. if (containsString(shaderSource, "#ifdef HAS_NORMALS")) {
  445. if (containsDefine(shaderSource, "HAS_NORMALS")) {
  446. return "v_normalEC";
  447. }
  448. return undefined;
  449. }
  450. return findFirstString(shaderSource, normalVaryingNames);
  451. };
  452. const positionVaryingNames = ["v_positionEC"];
  453. ShaderSource.findPositionVarying = function (shaderSource) {
  454. return findFirstString(shaderSource, positionVaryingNames);
  455. };
  456. export default ShaderSource;