shaderCodeInliner.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. interface IInlineFunctionDescr {
  2. name: string;
  3. type: string;
  4. parameters: string[];
  5. body: string;
  6. callIndex: number;
  7. }
  8. /**
  9. * Class used to inline functions in shader code
  10. */
  11. export class ShaderCodeInliner {
  12. private static readonly _RegexpFindFunctionNameAndType = /((\s+?)(\w+)\s+(\w+)\s*?)$/;
  13. private _sourceCode: string;
  14. private _functionDescr: IInlineFunctionDescr[];
  15. private _numMaxIterations: number;
  16. /** Gets or sets the token used to mark the functions to inline */
  17. public inlineToken: string;
  18. /** Gets or sets the debug mode */
  19. public debug: boolean = false;
  20. /** Gets the code after the inlining process */
  21. public get code(): string {
  22. return this._sourceCode;
  23. }
  24. /**
  25. * Initializes the inliner
  26. * @param sourceCode shader code source to inline
  27. * @param numMaxIterations maximum number of iterations (used to detect recursive calls)
  28. */
  29. constructor(sourceCode: string, numMaxIterations = 20) {
  30. this._sourceCode = sourceCode;
  31. this._numMaxIterations = numMaxIterations;
  32. this._functionDescr = [];
  33. this.inlineToken = "#define inline";
  34. }
  35. /**
  36. * Start the processing of the shader code
  37. */
  38. public processCode() {
  39. if (this.debug) {
  40. console.log(`Start inlining process (code size=${this._sourceCode.length})...`);
  41. }
  42. this._collectFunctions();
  43. this._processInlining(this._numMaxIterations);
  44. if (this.debug) {
  45. console.log("End of inlining process.");
  46. }
  47. }
  48. private _collectFunctions() {
  49. let startIndex = 0;
  50. while (startIndex < this._sourceCode.length) {
  51. // locate the function to inline and extract its name
  52. const inlineTokenIndex = this._sourceCode.indexOf(this.inlineToken, startIndex);
  53. if (inlineTokenIndex < 0) {
  54. break;
  55. }
  56. const funcParamsStartIndex = this._sourceCode.indexOf("(", inlineTokenIndex + this.inlineToken.length);
  57. if (funcParamsStartIndex < 0) {
  58. if (this.debug) {
  59. console.warn(`Could not find the opening parenthesis after the token. startIndex=${startIndex}`);
  60. }
  61. startIndex = inlineTokenIndex + this.inlineToken.length;
  62. continue;
  63. }
  64. const funcNameMatch = ShaderCodeInliner._RegexpFindFunctionNameAndType.exec(this._sourceCode.substring(inlineTokenIndex + this.inlineToken.length, funcParamsStartIndex));
  65. if (!funcNameMatch) {
  66. if (this.debug) {
  67. console.warn(`Could not extract the name/type of the function from: ${this._sourceCode.substring(inlineTokenIndex + this.inlineToken.length, funcParamsStartIndex)}`);
  68. }
  69. startIndex = inlineTokenIndex + this.inlineToken.length;
  70. continue;
  71. }
  72. const [funcType, funcName] = [funcNameMatch[3], funcNameMatch[4]];
  73. // extract the parameters of the function as a whole string (without the leading / trailing parenthesis)
  74. const funcParamsEndIndex = this._extractBetweenMarkers('(', ')', this._sourceCode, funcParamsStartIndex);
  75. if (funcParamsEndIndex < 0) {
  76. if (this.debug) {
  77. console.warn(`Could not extract the parameters the function '${funcName}' (type=${funcType}). funcParamsStartIndex=${funcParamsStartIndex}`);
  78. }
  79. startIndex = inlineTokenIndex + this.inlineToken.length;
  80. continue;
  81. }
  82. const funcParams = this._sourceCode.substring(funcParamsStartIndex + 1, funcParamsEndIndex);
  83. // extract the body of the function (with the curly brackets)
  84. const funcBodyStartIndex = this._skipWhitespaces(this._sourceCode, funcParamsEndIndex + 1);
  85. if (funcBodyStartIndex === this._sourceCode.length) {
  86. if (this.debug) {
  87. console.warn(`Could not extract the body of the function '${funcName}' (type=${funcType}). funcParamsEndIndex=${funcParamsEndIndex}`);
  88. }
  89. startIndex = inlineTokenIndex + this.inlineToken.length;
  90. continue;
  91. }
  92. const funcBodyEndIndex = this._extractBetweenMarkers('{', '}', this._sourceCode, funcBodyStartIndex);
  93. if (funcBodyEndIndex < 0) {
  94. if (this.debug) {
  95. console.warn(`Could not extract the body of the function '${funcName}' (type=${funcType}). funcBodyStartIndex=${funcBodyStartIndex}`);
  96. }
  97. startIndex = inlineTokenIndex + this.inlineToken.length;
  98. continue;
  99. }
  100. const funcBody = this._sourceCode.substring(funcBodyStartIndex, funcBodyEndIndex + 1);
  101. // process the parameters: extract each names
  102. const params = this._removeComments(funcParams).split(",");
  103. const paramNames = [];
  104. for (let p = 0; p < params.length; ++p) {
  105. const param = params[p].trim();
  106. const idx = param.lastIndexOf(" ");
  107. if (idx >= 0) {
  108. paramNames.push(param.substring(idx + 1));
  109. }
  110. }
  111. if (funcType !== 'void') {
  112. // for functions that return a value, we will replace "return" by "tempvarname = ", tempvarname being a unique generated name
  113. paramNames.push('return');
  114. }
  115. // collect the function
  116. this._functionDescr.push({
  117. "name": funcName,
  118. "type": funcType,
  119. "parameters": paramNames,
  120. "body": funcBody,
  121. "callIndex": 0,
  122. });
  123. startIndex = funcBodyEndIndex + 1;
  124. // remove the function from the source code
  125. const partBefore = inlineTokenIndex > 0 ? this._sourceCode.substring(0, inlineTokenIndex) : "";
  126. const partAfter = funcBodyEndIndex + 1 < this._sourceCode.length - 1 ? this._sourceCode.substring(funcBodyEndIndex + 1) : "";
  127. this._sourceCode = partBefore + partAfter;
  128. startIndex -= funcBodyEndIndex + 1 - inlineTokenIndex;
  129. }
  130. if (this.debug) {
  131. console.log(`Collect functions: ${this._functionDescr.length} functions found. functionDescr=`, this._functionDescr);
  132. }
  133. }
  134. private _processInlining(numMaxIterations: number = 20): boolean {
  135. while (numMaxIterations-- >= 0) {
  136. if (!this._replaceFunctionCallsByCode()) {
  137. break;
  138. }
  139. }
  140. if (this.debug) {
  141. console.log(`numMaxIterations is ${numMaxIterations} after inlining process`);
  142. }
  143. return numMaxIterations >= 0;
  144. }
  145. private _extractBetweenMarkers(markerOpen: string, markerClose: string, block: string, startIndex: number): number {
  146. let currPos = startIndex,
  147. openMarkers = 0,
  148. waitForChar = '';
  149. while (currPos < block.length) {
  150. let currChar = block.charAt(currPos);
  151. if (!waitForChar) {
  152. switch (currChar) {
  153. case markerOpen:
  154. openMarkers++;
  155. break;
  156. case markerClose:
  157. openMarkers--;
  158. break;
  159. case '"':
  160. case "'":
  161. case "`":
  162. waitForChar = currChar;
  163. break;
  164. case '/':
  165. if (currPos + 1 < block.length) {
  166. const nextChar = block.charAt(currPos + 1);
  167. if (nextChar === '/') {
  168. waitForChar = '\n';
  169. } else if (nextChar === '*') {
  170. waitForChar = '*/';
  171. }
  172. }
  173. break;
  174. }
  175. } else {
  176. if (currChar === waitForChar) {
  177. if (waitForChar === '"' || waitForChar === "'") {
  178. block.charAt(currPos - 1) !== '\\' && (waitForChar = '');
  179. } else {
  180. waitForChar = '';
  181. }
  182. } else if (waitForChar === '*/' && currChar === '*' && currPos + 1 < block.length) {
  183. block.charAt(currPos + 1) === '/' && (waitForChar = '');
  184. if (waitForChar === '') {
  185. currPos++;
  186. }
  187. }
  188. }
  189. currPos++ ;
  190. if (openMarkers === 0) {
  191. break;
  192. }
  193. }
  194. return openMarkers === 0 ? currPos - 1 : -1;
  195. }
  196. private _skipWhitespaces(s: string, index: number): number {
  197. while (index < s.length) {
  198. const c = s[index];
  199. if (c !== ' ' && c !== '\n' && c !== '\r' && c !== '\t' && c !== '\u000a' && c !== '\u00a0') {
  200. break;
  201. }
  202. index++;
  203. }
  204. return index;
  205. }
  206. private _isIdentifierChar(c: string): boolean {
  207. const v = c.charCodeAt(0);
  208. return (v >= 48 && v <= 57) || // 0-9
  209. (v >= 65 && v <= 90) || // A-Z
  210. (v >= 97 && v <= 122) || // a-z
  211. (v == 95); // _
  212. }
  213. private _removeComments(block: string): string {
  214. let currPos = 0,
  215. waitForChar = '',
  216. inComments = false,
  217. s = [];
  218. while (currPos < block.length) {
  219. let currChar = block.charAt(currPos);
  220. if (!waitForChar) {
  221. switch (currChar) {
  222. case '"':
  223. case "'":
  224. case "`":
  225. waitForChar = currChar;
  226. break;
  227. case '/':
  228. if (currPos + 1 < block.length) {
  229. const nextChar = block.charAt(currPos + 1);
  230. if (nextChar === '/') {
  231. waitForChar = '\n';
  232. inComments = true;
  233. } else if (nextChar === '*') {
  234. waitForChar = '*/';
  235. inComments = true;
  236. }
  237. }
  238. break;
  239. }
  240. if (!inComments) {
  241. s.push(currChar);
  242. }
  243. } else {
  244. if (currChar === waitForChar) {
  245. if (waitForChar === '"' || waitForChar === "'") {
  246. block.charAt(currPos - 1) !== '\\' && (waitForChar = '');
  247. s.push(currChar);
  248. } else {
  249. waitForChar = '';
  250. inComments = false;
  251. }
  252. } else if (waitForChar === '*/' && currChar === '*' && currPos + 1 < block.length) {
  253. block.charAt(currPos + 1) === '/' && (waitForChar = '');
  254. if (waitForChar === '') {
  255. inComments = false;
  256. currPos++;
  257. }
  258. } else {
  259. if (!inComments) {
  260. s.push(currChar);
  261. }
  262. }
  263. }
  264. currPos++ ;
  265. }
  266. return s.join('');
  267. }
  268. private _replaceFunctionCallsByCode(): boolean {
  269. let doAgain = false;
  270. for (const func of this._functionDescr) {
  271. const { name, type, parameters, body } = func;
  272. let startIndex = 0;
  273. while (startIndex < this._sourceCode.length) {
  274. // Look for the function name in the source code
  275. const functionCallIndex = this._sourceCode.indexOf(name, startIndex);
  276. if (functionCallIndex < 0) {
  277. break;
  278. }
  279. // Make sure "name" is not part of a bigger string
  280. if (functionCallIndex === 0 || this._isIdentifierChar(this._sourceCode.charAt(functionCallIndex - 1))) {
  281. startIndex = functionCallIndex + name.length;
  282. continue;
  283. }
  284. // Find the opening parenthesis
  285. const callParamsStartIndex = this._skipWhitespaces(this._sourceCode, functionCallIndex + name.length);
  286. if (callParamsStartIndex === this._sourceCode.length || this._sourceCode.charAt(callParamsStartIndex) !== '(') {
  287. startIndex = functionCallIndex + name.length;
  288. continue;
  289. }
  290. // extract the parameters of the function call as a whole string (without the leading / trailing parenthesis)
  291. const callParamsEndIndex = this._extractBetweenMarkers('(', ')', this._sourceCode, callParamsStartIndex);
  292. if (callParamsEndIndex < 0) {
  293. if (this.debug) {
  294. console.warn(`Could not extract the parameters of the function call. Function '${name}' (type=${type}). callParamsStartIndex=${callParamsStartIndex}`);
  295. }
  296. startIndex = functionCallIndex + name.length;
  297. continue;
  298. }
  299. const callParams = this._sourceCode.substring(callParamsStartIndex + 1, callParamsEndIndex);
  300. // process the parameter call: extract each names
  301. // this function split the parameter list used in the function call at ',' boundaries by taking care of potential parenthesis like in:
  302. // myfunc(a, vec2(1., 0.), 4.)
  303. const splitParameterCall = (s: string) => {
  304. const parameters = [];
  305. let curIdx = 0, startParamIdx = 0;
  306. while (curIdx < s.length) {
  307. if (s.charAt(curIdx) === '(') {
  308. const idx2 = this._extractBetweenMarkers('(', ')', s, curIdx);
  309. if (idx2 < 0) {
  310. return null;
  311. }
  312. curIdx = idx2;
  313. } else if (s.charAt(curIdx) === ',') {
  314. parameters.push(s.substring(startParamIdx, curIdx));
  315. startParamIdx = curIdx + 1;
  316. }
  317. curIdx++;
  318. }
  319. if (startParamIdx < curIdx) {
  320. parameters.push(s.substring(startParamIdx, curIdx));
  321. }
  322. return parameters;
  323. };
  324. const params = splitParameterCall(this._removeComments(callParams));
  325. if (params === null) {
  326. if (this.debug) {
  327. console.warn(`Invalid function call: can't extract the parameters of the function call. Function '${name}' (type=${type}). callParamsStartIndex=${callParamsStartIndex}, callParams=` + callParams);
  328. }
  329. startIndex = functionCallIndex + name.length;
  330. continue;
  331. }
  332. const paramNames = [];
  333. for (let p = 0; p < params.length; ++p) {
  334. const param = params[p].trim();
  335. paramNames.push(param);
  336. }
  337. const retParamName = type !== 'void' ? name + '_' + (func.callIndex++) : null;
  338. if (retParamName) {
  339. paramNames.push(retParamName + ' =');
  340. }
  341. if (paramNames.length !== parameters.length) {
  342. if (this.debug) {
  343. console.warn(`Invalid function call: not the same number of parameters for the call than the number expected by the function. Function '${name}' (type=${type}). function parameters=${parameters}, call parameters=${paramNames}`);
  344. }
  345. startIndex = functionCallIndex + name.length;
  346. continue;
  347. }
  348. startIndex = callParamsEndIndex + 1;
  349. // replace the function call by the body function
  350. const funcBody = this._replaceNames(body, parameters, paramNames);
  351. let partBefore = functionCallIndex > 0 ? this._sourceCode.substring(0, functionCallIndex) : "";
  352. let partAfter = callParamsEndIndex + 1 < this._sourceCode.length - 1 ? this._sourceCode.substring(callParamsEndIndex + 1) : "";
  353. if (retParamName) {
  354. // case where the function returns a value. We generate:
  355. // FUNCTYPE retParamName;
  356. // {function body}
  357. // and replace the function call by retParamName
  358. const injectDeclarationIndex = this._findBackward(this._sourceCode, functionCallIndex - 1, '\n');
  359. partBefore = this._sourceCode.substring(0, injectDeclarationIndex + 1);
  360. let partBetween = this._sourceCode.substring(injectDeclarationIndex + 1, functionCallIndex);
  361. this._sourceCode = partBefore + type + " " + retParamName + ";\n" + funcBody + "\n" + partBetween + retParamName + partAfter;
  362. if (this.debug) {
  363. console.log(`Replace function call by code. Function '${name}' (type=${type}). injectDeclarationIndex=${injectDeclarationIndex}, call parameters=${paramNames}`);
  364. }
  365. } else {
  366. // simple case where the return value of the function is "void"
  367. this._sourceCode = partBefore + funcBody + partAfter;
  368. startIndex += funcBody.length - (callParamsEndIndex + 1 - functionCallIndex);
  369. if (this.debug) {
  370. console.log(`Replace function call by code. Function '${name}' (type=${type}). functionCallIndex=${functionCallIndex}, call parameters=${paramNames}`);
  371. }
  372. }
  373. doAgain = true;
  374. }
  375. }
  376. return doAgain;
  377. }
  378. private _findBackward(s: string, index: number, c: string): number {
  379. while (index >= 0 && s.charAt(index) !== c) {
  380. index--;
  381. }
  382. return index;
  383. }
  384. private _escapeRegExp(s: string): string {
  385. return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  386. }
  387. private _replaceNames(code: string, sources: string[], destinations: string[]): string {
  388. for (let i = 0; i < sources.length; ++i) {
  389. const source = new RegExp(this._escapeRegExp(sources[i]), 'g'),
  390. sourceLen = sources[i].length,
  391. destination = destinations[i];
  392. code = code.replace(source, (match, ...args) => {
  393. const offset: number = args[0];
  394. // Make sure "source" is not part of a bigger identifier (for eg, if source=view and we matched it with viewDirection)
  395. if (this._isIdentifierChar(code.charAt(offset - 1)) || this._isIdentifierChar(code.charAt(offset + sourceLen))) {
  396. return sources[i];
  397. }
  398. return destination;
  399. });
  400. }
  401. return code;
  402. }
  403. }