ShaderSource.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import modernizeShader from "../Renderer/modernizeShader.js";
  5. import CzmBuiltins from "../Shaders/Builtin/CzmBuiltins.js";
  6. import AutomaticUniforms from "./AutomaticUniforms.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. // #version must be first
  192. // defaults to #version 100 if not specified
  193. if (defined(version)) {
  194. result = `#version ${version}\n`;
  195. }
  196. const extensionsLength = extensions.length;
  197. for (i = 0; i < extensionsLength; i++) {
  198. result += extensions[i];
  199. }
  200. if (isFragmentShader) {
  201. // If high precision isn't support replace occurrences of highp with mediump
  202. // The highp keyword is not always available on older mobile devices
  203. // 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
  204. result +=
  205. "\
  206. #ifdef GL_FRAGMENT_PRECISION_HIGH\n\
  207. precision highp float;\n\
  208. precision highp int;\n\
  209. #else\n\
  210. precision mediump float;\n\
  211. precision mediump int;\n\
  212. #define highp mediump\n\
  213. #endif\n\n";
  214. }
  215. // Prepend #defines for uber-shaders
  216. const defines = shaderSource.defines;
  217. if (defined(defines)) {
  218. for (i = 0, length = defines.length; i < length; ++i) {
  219. const define = defines[i];
  220. if (define.length !== 0) {
  221. result += `#define ${define}\n`;
  222. }
  223. }
  224. }
  225. // GLSLModernizer inserts its own layout qualifiers
  226. // at this position in the source
  227. if (context.webgl2) {
  228. result += "#define OUTPUT_DECLARATION\n\n";
  229. }
  230. // Define a constant for the OES_texture_float_linear extension since WebGL does not.
  231. if (context.textureFloatLinear) {
  232. result += "#define OES_texture_float_linear\n\n";
  233. }
  234. // Define a constant for the OES_texture_float extension since WebGL does not.
  235. if (context.floatingPointTexture) {
  236. result += "#define OES_texture_float\n\n";
  237. }
  238. // append built-ins
  239. if (shaderSource.includeBuiltIns) {
  240. result += getBuiltinsAndAutomaticUniforms(combinedSources);
  241. }
  242. // reset line number
  243. result += "\n#line 0\n";
  244. // append actual source
  245. result += combinedSources;
  246. // modernize the source
  247. if (context.webgl2) {
  248. result = modernizeShader(result, isFragmentShader, true);
  249. }
  250. return result;
  251. }
  252. /**
  253. * An object containing various inputs that will be combined to form a final GLSL shader string.
  254. *
  255. * @param {Object} [options] Object with the following properties:
  256. * @param {String[]} [options.sources] An array of strings to combine containing GLSL code for the shader.
  257. * @param {String[]} [options.defines] An array of strings containing GLSL identifiers to <code>#define</code>.
  258. * @param {String} [options.pickColorQualifier] The GLSL qualifier, <code>uniform</code> or <code>varying</code>, for the input <code>czm_pickColor</code>. When defined, a pick fragment shader is generated.
  259. * @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.
  260. *
  261. * @exception {DeveloperError} options.pickColorQualifier must be 'uniform' or 'varying'.
  262. *
  263. * @example
  264. * // 1. Prepend #defines to a shader
  265. * const source = new Cesium.ShaderSource({
  266. * defines : ['WHITE'],
  267. * sources : ['void main() { \n#ifdef WHITE\n gl_FragColor = vec4(1.0); \n#else\n gl_FragColor = vec4(0.0); \n#endif\n }']
  268. * });
  269. *
  270. * // 2. Modify a fragment shader for picking
  271. * const source2 = new Cesium.ShaderSource({
  272. * sources : ['void main() { gl_FragColor = vec4(1.0); }'],
  273. * pickColorQualifier : 'uniform'
  274. * });
  275. *
  276. * @private
  277. */
  278. function ShaderSource(options) {
  279. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  280. const pickColorQualifier = options.pickColorQualifier;
  281. //>>includeStart('debug', pragmas.debug);
  282. if (
  283. defined(pickColorQualifier) &&
  284. pickColorQualifier !== "uniform" &&
  285. pickColorQualifier !== "varying"
  286. ) {
  287. throw new DeveloperError(
  288. "options.pickColorQualifier must be 'uniform' or 'varying'."
  289. );
  290. }
  291. //>>includeEnd('debug');
  292. this.defines = defined(options.defines) ? options.defines.slice(0) : [];
  293. this.sources = defined(options.sources) ? options.sources.slice(0) : [];
  294. this.pickColorQualifier = pickColorQualifier;
  295. this.includeBuiltIns = defaultValue(options.includeBuiltIns, true);
  296. }
  297. ShaderSource.prototype.clone = function () {
  298. return new ShaderSource({
  299. sources: this.sources,
  300. defines: this.defines,
  301. pickColorQualifier: this.pickColorQualifier,
  302. includeBuiltIns: this.includeBuiltIns,
  303. });
  304. };
  305. ShaderSource.replaceMain = function (source, renamedMain) {
  306. renamedMain = `void ${renamedMain}()`;
  307. return source.replace(/void\s+main\s*\(\s*(?:void)?\s*\)/g, renamedMain);
  308. };
  309. /**
  310. * Create a single string containing the full, combined vertex shader with all dependencies and defines.
  311. *
  312. * @param {Context} context The current rendering context
  313. *
  314. * @returns {String} The combined shader string.
  315. */
  316. ShaderSource.prototype.createCombinedVertexShader = function (context) {
  317. return combineShader(this, false, context);
  318. };
  319. /**
  320. * Create a single string containing the full, combined fragment shader with all dependencies and defines.
  321. *
  322. * @param {Context} context The current rendering context
  323. *
  324. * @returns {String} The combined shader string.
  325. */
  326. ShaderSource.prototype.createCombinedFragmentShader = function (context) {
  327. return combineShader(this, true, context);
  328. };
  329. /**
  330. * For ShaderProgram testing
  331. * @private
  332. */
  333. ShaderSource._czmBuiltinsAndUniforms = {};
  334. // combine automatic uniforms and Cesium built-ins
  335. for (const builtinName in CzmBuiltins) {
  336. if (CzmBuiltins.hasOwnProperty(builtinName)) {
  337. ShaderSource._czmBuiltinsAndUniforms[builtinName] =
  338. CzmBuiltins[builtinName];
  339. }
  340. }
  341. for (const uniformName in AutomaticUniforms) {
  342. if (AutomaticUniforms.hasOwnProperty(uniformName)) {
  343. const uniform = AutomaticUniforms[uniformName];
  344. if (typeof uniform.getDeclaration === "function") {
  345. ShaderSource._czmBuiltinsAndUniforms[
  346. uniformName
  347. ] = uniform.getDeclaration(uniformName);
  348. }
  349. }
  350. }
  351. ShaderSource.createPickVertexShaderSource = function (vertexShaderSource) {
  352. const renamedVS = ShaderSource.replaceMain(
  353. vertexShaderSource,
  354. "czm_old_main"
  355. );
  356. const pickMain =
  357. "attribute vec4 pickColor; \n" +
  358. "varying vec4 czm_pickColor; \n" +
  359. "void main() \n" +
  360. "{ \n" +
  361. " czm_old_main(); \n" +
  362. " czm_pickColor = pickColor; \n" +
  363. "}";
  364. return `${renamedVS}\n${pickMain}`;
  365. };
  366. ShaderSource.createPickFragmentShaderSource = function (
  367. fragmentShaderSource,
  368. pickColorQualifier
  369. ) {
  370. const renamedFS = ShaderSource.replaceMain(
  371. fragmentShaderSource,
  372. "czm_old_main"
  373. );
  374. const pickMain =
  375. `${pickColorQualifier} vec4 czm_pickColor; \n` +
  376. `void main() \n` +
  377. `{ \n` +
  378. ` czm_old_main(); \n` +
  379. ` if (gl_FragColor.a == 0.0) { \n` +
  380. ` discard; \n` +
  381. ` } \n` +
  382. ` gl_FragColor = czm_pickColor; \n` +
  383. `}`;
  384. return `${renamedFS}\n${pickMain}`;
  385. };
  386. function containsString(shaderSource, string) {
  387. const sources = shaderSource.sources;
  388. const sourcesLength = sources.length;
  389. for (let i = 0; i < sourcesLength; ++i) {
  390. if (sources[i].indexOf(string) !== -1) {
  391. return true;
  392. }
  393. }
  394. return false;
  395. }
  396. function findFirstString(shaderSource, strings) {
  397. const stringsLength = strings.length;
  398. for (let i = 0; i < stringsLength; ++i) {
  399. const string = strings[i];
  400. if (containsString(shaderSource, string)) {
  401. return string;
  402. }
  403. }
  404. return undefined;
  405. }
  406. const normalVaryingNames = ["v_normalEC", "v_normal"];
  407. ShaderSource.findNormalVarying = function (shaderSource) {
  408. // Fix for ModelExperimental: the shader text always has the word v_normalEC
  409. // wrapped in an #ifdef so instead of looking for v_normalEC look for the define
  410. if (containsString(shaderSource, "#ifdef HAS_NORMALS")) {
  411. if (containsString(shaderSource, "#define HAS_NORMALS")) {
  412. return "v_normalEC";
  413. }
  414. return undefined;
  415. }
  416. return findFirstString(shaderSource, normalVaryingNames);
  417. };
  418. const positionVaryingNames = ["v_positionEC"];
  419. ShaderSource.findPositionVarying = function (shaderSource) {
  420. return findFirstString(shaderSource, positionVaryingNames);
  421. };
  422. export default ShaderSource;