GLSLDecoder.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. import { Program, FunctionDeclaration, For, AccessorElements, Ternary, Varying, DynamicElement, StaticElement, FunctionParameter, Unary, Conditional, VariableDeclaration, Operator, Number, String, FunctionCall, Return, Accessor, Uniform, Discard } from './AST.js';
  2. const unaryOperators = [
  3. '+', '-', '~', '!', '++', '--'
  4. ];
  5. const precedenceOperators = [
  6. '*', '/', '%',
  7. '-', '+',
  8. '<<', '>>',
  9. '<', '>', '<=', '>=',
  10. '==', '!=',
  11. '&',
  12. '^',
  13. '|',
  14. '&&',
  15. '^^',
  16. '||',
  17. '?',
  18. '=',
  19. '+=', '-=', '*=', '/=', '%=', '^=', '&=', '|=', '<<=', '>>=',
  20. ','
  21. ].reverse();
  22. const associativityRightToLeft = [
  23. '=',
  24. '+=', '-=', '*=', '/=', '%=', '^=', '&=', '|=', '<<=', '>>=',
  25. ',',
  26. '?',
  27. ':'
  28. ];
  29. const glslToTSL = {
  30. inversesqrt: 'inverseSqrt'
  31. };
  32. const samplers = [ 'sampler1D', 'sampler2D', 'sampler2DArray', 'sampler2DShadow', 'sampler2DArrayShadow', 'isampler2D', 'isampler2DArray', 'usampler2D', 'usampler2DArray' ];
  33. const samplersCube = [ 'samplerCube', 'samplerCubeShadow', 'usamplerCube', 'isamplerCube' ];
  34. const samplers3D = [ 'sampler3D', 'isampler3D', 'usampler3D' ];
  35. const spaceRegExp = /^((\t| )\n*)+/;
  36. const lineRegExp = /^\n+/;
  37. const commentRegExp = /^\/\*[\s\S]*?\*\//;
  38. const inlineCommentRegExp = /^\/\/.*?(\n|$)/;
  39. const numberRegExp = /^((0x\w+)|(\.?\d+\.?\d*((e-?\d+)|\w)?))/;
  40. const stringDoubleRegExp = /^(\"((?:[^"\\]|\\.)*)\")/;
  41. const stringSingleRegExp = /^(\'((?:[^'\\]|\\.)*)\')/;
  42. const literalRegExp = /^[A-Za-z](\w|\.)*/;
  43. const operatorsRegExp = new RegExp( '^(\\' + [
  44. '<<=', '>>=', '++', '--', '<<', '>>', '+=', '-=', '*=', '/=', '%=', '&=', '^^', '^=', '|=',
  45. '<=', '>=', '==', '!=', '&&', '||',
  46. '(', ')', '[', ']', '{', '}',
  47. '.', ',', ';', '!', '=', '~', '*', '/', '%', '+', '-', '<', '>', '&', '^', '|', '?', ':', '#'
  48. ].join( '$' ).split( '' ).join( '\\' ).replace( /\\\$/g, '|' ) + ')' );
  49. function getFunctionName( str ) {
  50. return glslToTSL[ str ] || str;
  51. }
  52. function getGroupDelta( str ) {
  53. if ( str === '(' || str === '[' || str === '{' ) return 1;
  54. if ( str === ')' || str === ']' || str === '}' ) return - 1;
  55. return 0;
  56. }
  57. class Token {
  58. constructor( tokenizer, type, str, pos ) {
  59. this.tokenizer = tokenizer;
  60. this.type = type;
  61. this.str = str;
  62. this.pos = pos;
  63. this.tag = null;
  64. }
  65. get endPos() {
  66. return this.pos + this.str.length;
  67. }
  68. get isNumber() {
  69. return this.type === Token.NUMBER;
  70. }
  71. get isString() {
  72. return this.type === Token.STRING;
  73. }
  74. get isLiteral() {
  75. return this.type === Token.LITERAL;
  76. }
  77. get isOperator() {
  78. return this.type === Token.OPERATOR;
  79. }
  80. }
  81. Token.LINE = 'line';
  82. Token.COMMENT = 'comment';
  83. Token.NUMBER = 'number';
  84. Token.STRING = 'string';
  85. Token.LITERAL = 'literal';
  86. Token.OPERATOR = 'operator';
  87. const TokenParserList = [
  88. { type: Token.LINE, regexp: lineRegExp, isTag: true },
  89. { type: Token.COMMENT, regexp: commentRegExp, isTag: true },
  90. { type: Token.COMMENT, regexp: inlineCommentRegExp, isTag: true },
  91. { type: Token.NUMBER, regexp: numberRegExp },
  92. { type: Token.STRING, regexp: stringDoubleRegExp, group: 2 },
  93. { type: Token.STRING, regexp: stringSingleRegExp, group: 2 },
  94. { type: Token.LITERAL, regexp: literalRegExp },
  95. { type: Token.OPERATOR, regexp: operatorsRegExp }
  96. ];
  97. class Tokenizer {
  98. constructor( source ) {
  99. this.source = source;
  100. this.position = 0;
  101. this.tokens = [];
  102. }
  103. tokenize() {
  104. let token = this.readToken();
  105. while ( token ) {
  106. this.tokens.push( token );
  107. token = this.readToken();
  108. }
  109. return this;
  110. }
  111. skip( ...params ) {
  112. let remainingCode = this.source.substr( this.position );
  113. let i = params.length;
  114. while ( i -- ) {
  115. const skip = params[ i ].exec( remainingCode );
  116. const skipLength = skip ? skip[ 0 ].length : 0;
  117. if ( skipLength > 0 ) {
  118. this.position += skipLength;
  119. remainingCode = this.source.substr( this.position );
  120. // re-skip, new remainingCode is generated
  121. // maybe exist previous regexp non detected
  122. i = params.length;
  123. }
  124. }
  125. return remainingCode;
  126. }
  127. readToken() {
  128. const remainingCode = this.skip( spaceRegExp );
  129. for ( var i = 0; i < TokenParserList.length; i ++ ) {
  130. const parser = TokenParserList[ i ];
  131. const result = parser.regexp.exec( remainingCode );
  132. if ( result ) {
  133. const token = new Token( this, parser.type, result[ parser.group || 0 ], this.position );
  134. this.position += result[ 0 ].length;
  135. if ( parser.isTag ) {
  136. const nextToken = this.readToken();
  137. if ( nextToken ) {
  138. nextToken.tag = token;
  139. }
  140. return nextToken;
  141. }
  142. return token;
  143. }
  144. }
  145. }
  146. }
  147. const isType = ( str ) => /void|bool|float|u?int|mat[234]|mat[234]x[234]|(u|i|b)?vec[234]/.test( str );
  148. class GLSLDecoder {
  149. constructor() {
  150. this.index = 0;
  151. this.tokenizer = null;
  152. this.keywords = [];
  153. this._currentFunction = null;
  154. this.addPolyfill( 'gl_FragCoord', 'vec3 gl_FragCoord = vec3( screenCoordinate.x, screenCoordinate.y.oneMinus(), screenCoordinate.z );' );
  155. }
  156. addPolyfill( name, polyfill ) {
  157. this.keywords.push( { name, polyfill } );
  158. return this;
  159. }
  160. get tokens() {
  161. return this.tokenizer.tokens;
  162. }
  163. readToken() {
  164. return this.tokens[ this.index ++ ];
  165. }
  166. getToken( offset = 0 ) {
  167. return this.tokens[ this.index + offset ];
  168. }
  169. getTokensUntil( str, tokens, offset = 0 ) {
  170. const output = [];
  171. let groupIndex = 0;
  172. for ( let i = offset; i < tokens.length; i ++ ) {
  173. const token = tokens[ i ];
  174. groupIndex += getGroupDelta( token.str );
  175. output.push( token );
  176. if ( groupIndex === 0 && token.str === str ) {
  177. break;
  178. }
  179. }
  180. return output;
  181. }
  182. readTokensUntil( str ) {
  183. const tokens = this.getTokensUntil( str, this.tokens, this.index );
  184. this.index += tokens.length;
  185. return tokens;
  186. }
  187. parseExpressionFromTokens( tokens ) {
  188. if ( tokens.length === 0 ) return null;
  189. const firstToken = tokens[ 0 ];
  190. const lastToken = tokens[ tokens.length - 1 ];
  191. // precedence operators
  192. let groupIndex = 0;
  193. for ( const operator of precedenceOperators ) {
  194. const parseToken = ( i, inverse = false ) => {
  195. const token = tokens[ i ];
  196. groupIndex += getGroupDelta( token.str );
  197. if ( ! token.isOperator || i === 0 || i === tokens.length - 1 ) return;
  198. if ( groupIndex === 0 && token.str === operator ) {
  199. if ( operator === '?' ) {
  200. const conditionTokens = tokens.slice( 0, i );
  201. const leftTokens = this.getTokensUntil( ':', tokens, i + 1 ).slice( 0, - 1 );
  202. const rightTokens = tokens.slice( i + leftTokens.length + 2 );
  203. const condition = this.parseExpressionFromTokens( conditionTokens );
  204. const left = this.parseExpressionFromTokens( leftTokens );
  205. const right = this.parseExpressionFromTokens( rightTokens );
  206. return new Ternary( condition, left, right );
  207. } else {
  208. const left = this.parseExpressionFromTokens( tokens.slice( 0, i ) );
  209. const right = this.parseExpressionFromTokens( tokens.slice( i + 1, tokens.length ) );
  210. return this._evalOperator( new Operator( operator, left, right ) );
  211. }
  212. }
  213. if ( inverse ) {
  214. if ( groupIndex > 0 ) {
  215. return this.parseExpressionFromTokens( tokens.slice( i ) );
  216. }
  217. } else {
  218. if ( groupIndex < 0 ) {
  219. return this.parseExpressionFromTokens( tokens.slice( 0, i ) );
  220. }
  221. }
  222. };
  223. if ( associativityRightToLeft.includes( operator ) ) {
  224. for ( let i = 0; i < tokens.length; i ++ ) {
  225. const result = parseToken( i );
  226. if ( result ) return result;
  227. }
  228. } else {
  229. for ( let i = tokens.length - 1; i >= 0; i -- ) {
  230. const result = parseToken( i, true );
  231. if ( result ) return result;
  232. }
  233. }
  234. }
  235. // unary operators (before)
  236. if ( firstToken.isOperator ) {
  237. for ( const operator of unaryOperators ) {
  238. if ( firstToken.str === operator ) {
  239. const right = this.parseExpressionFromTokens( tokens.slice( 1 ) );
  240. return new Unary( operator, right );
  241. }
  242. }
  243. }
  244. // unary operators (after)
  245. if ( lastToken.isOperator ) {
  246. for ( const operator of unaryOperators ) {
  247. if ( lastToken.str === operator ) {
  248. const left = this.parseExpressionFromTokens( tokens.slice( 0, tokens.length - 1 ) );
  249. return new Unary( operator, left, true );
  250. }
  251. }
  252. }
  253. // groups
  254. if ( firstToken.str === '(' ) {
  255. const leftTokens = this.getTokensUntil( ')', tokens );
  256. const left = this.parseExpressionFromTokens( leftTokens.slice( 1, leftTokens.length - 1 ) );
  257. const operator = tokens[ leftTokens.length ];
  258. if ( operator ) {
  259. const rightTokens = tokens.slice( leftTokens.length + 1 );
  260. const right = this.parseExpressionFromTokens( rightTokens );
  261. return this._evalOperator( new Operator( operator.str, left, right ) );
  262. }
  263. return left;
  264. }
  265. // primitives and accessors
  266. if ( firstToken.isNumber ) {
  267. let type;
  268. const isHex = /^(0x)/.test( firstToken.str );
  269. if ( isHex ) type = 'int';
  270. else if ( /u$|U$/.test( firstToken.str ) ) type = 'uint';
  271. else if ( /f|e|\./.test( firstToken.str ) ) type = 'float';
  272. else type = 'int';
  273. let str = firstToken.str.replace( /u|U|i$/, '' );
  274. if ( isHex === false ) {
  275. str = str.replace( /f$/, '' );
  276. }
  277. return new Number( str, type );
  278. } else if ( firstToken.isString ) {
  279. return new String( firstToken.str );
  280. } else if ( firstToken.isLiteral ) {
  281. if ( firstToken.str === 'return' ) {
  282. return new Return( this.parseExpressionFromTokens( tokens.slice( 1 ) ) );
  283. } else if ( firstToken.str === 'discard' ) {
  284. return new Discard();
  285. }
  286. const secondToken = tokens[ 1 ];
  287. if ( secondToken ) {
  288. if ( secondToken.str === '(' ) {
  289. // function call
  290. const internalTokens = this.getTokensUntil( ')', tokens, 1 ).slice( 1, - 1 );
  291. const paramsTokens = this.parseFunctionParametersFromTokens( internalTokens );
  292. const functionCall = new FunctionCall( getFunctionName( firstToken.str ), paramsTokens );
  293. const accessTokens = tokens.slice( 3 + internalTokens.length );
  294. if ( accessTokens.length > 0 ) {
  295. const elements = this.parseAccessorElementsFromTokens( accessTokens );
  296. return new AccessorElements( functionCall, elements );
  297. }
  298. return functionCall;
  299. } else if ( secondToken.str === '[' ) {
  300. // array accessor
  301. const elements = this.parseAccessorElementsFromTokens( tokens.slice( 1 ) );
  302. return new AccessorElements( new Accessor( firstToken.str ), elements );
  303. }
  304. }
  305. return new Accessor( firstToken.str );
  306. }
  307. }
  308. parseAccessorElementsFromTokens( tokens ) {
  309. const elements = [];
  310. let currentTokens = tokens;
  311. while ( currentTokens.length > 0 ) {
  312. const token = currentTokens[ 0 ];
  313. if ( token.str === '[' ) {
  314. const accessorTokens = this.getTokensUntil( ']', currentTokens );
  315. const element = this.parseExpressionFromTokens( accessorTokens.slice( 1, accessorTokens.length - 1 ) );
  316. currentTokens = currentTokens.slice( accessorTokens.length );
  317. elements.push( new DynamicElement( element ) );
  318. } else if ( token.str === '.' ) {
  319. const accessorTokens = currentTokens.slice( 1, 2 );
  320. const element = this.parseExpressionFromTokens( accessorTokens );
  321. currentTokens = currentTokens.slice( 2 );
  322. elements.push( new StaticElement( element ) );
  323. } else {
  324. console.error( 'Unknown accessor expression', token );
  325. break;
  326. }
  327. }
  328. return elements;
  329. }
  330. parseFunctionParametersFromTokens( tokens ) {
  331. if ( tokens.length === 0 ) return [];
  332. const expression = this.parseExpressionFromTokens( tokens );
  333. const params = [];
  334. let current = expression;
  335. while ( current.type === ',' ) {
  336. params.push( current.left );
  337. current = current.right;
  338. }
  339. params.push( current );
  340. return params;
  341. }
  342. parseExpression() {
  343. const tokens = this.readTokensUntil( ';' );
  344. const exp = this.parseExpressionFromTokens( tokens.slice( 0, tokens.length - 1 ) );
  345. return exp;
  346. }
  347. parseFunctionParams( tokens ) {
  348. const params = [];
  349. for ( let i = 0; i < tokens.length; i ++ ) {
  350. const immutable = tokens[ i ].str === 'const';
  351. if ( immutable ) i ++;
  352. let qualifier = tokens[ i ].str;
  353. if ( /^(in|out|inout)$/.test( qualifier ) ) {
  354. i ++;
  355. } else {
  356. qualifier = null;
  357. }
  358. const type = tokens[ i ++ ].str;
  359. const name = tokens[ i ++ ].str;
  360. params.push( new FunctionParameter( type, name, qualifier, immutable ) );
  361. if ( tokens[ i ] && tokens[ i ].str !== ',' ) throw new Error( 'Expected ","' );
  362. }
  363. return params;
  364. }
  365. parseFunction() {
  366. const type = this.readToken().str;
  367. const name = this.readToken().str;
  368. const paramsTokens = this.readTokensUntil( ')' );
  369. const params = this.parseFunctionParams( paramsTokens.slice( 1, paramsTokens.length - 1 ) );
  370. const func = new FunctionDeclaration( type, name, params );
  371. this._currentFunction = func;
  372. this.parseBlock( func );
  373. this._currentFunction = null;
  374. return func;
  375. }
  376. parseVariablesFromToken( tokens, type ) {
  377. let index = 0;
  378. const immutable = tokens[ 0 ].str === 'const';
  379. if ( immutable ) index ++;
  380. type = type || tokens[ index ++ ].str;
  381. const name = tokens[ index ++ ].str;
  382. const token = tokens[ index ];
  383. let init = null;
  384. let next = null;
  385. if ( token ) {
  386. const initTokens = this.getTokensUntil( ',', tokens, index );
  387. if ( initTokens[ 0 ].str === '=' ) {
  388. const expressionTokens = initTokens.slice( 1 );
  389. if ( expressionTokens[ expressionTokens.length - 1 ].str === ',' ) expressionTokens.pop();
  390. init = this.parseExpressionFromTokens( expressionTokens );
  391. }
  392. const nextTokens = tokens.slice( initTokens.length + ( index - 1 ) );
  393. if ( nextTokens[ 0 ] && nextTokens[ 0 ].str === ',' ) {
  394. next = this.parseVariablesFromToken( nextTokens.slice( 1 ), type );
  395. }
  396. }
  397. const variable = new VariableDeclaration( type, name, init, next, immutable );
  398. return variable;
  399. }
  400. parseVariables() {
  401. const tokens = this.readTokensUntil( ';' );
  402. return this.parseVariablesFromToken( tokens.slice( 0, tokens.length - 1 ) );
  403. }
  404. parseUniform() {
  405. const tokens = this.readTokensUntil( ';' );
  406. let type = tokens[ 1 ].str;
  407. const name = tokens[ 2 ].str;
  408. // GLSL to TSL types
  409. if ( samplers.includes( type ) ) type = 'texture';
  410. else if ( samplersCube.includes( type ) ) type = 'cubeTexture';
  411. else if ( samplers3D.includes( type ) ) type = 'texture3D';
  412. return new Uniform( type, name );
  413. }
  414. parseVarying() {
  415. const tokens = this.readTokensUntil( ';' );
  416. const type = tokens[ 1 ].str;
  417. const name = tokens[ 2 ].str;
  418. return new Varying( type, name );
  419. }
  420. parseReturn() {
  421. this.readToken(); // skip 'return'
  422. const expression = this.parseExpression();
  423. return new Return( expression );
  424. }
  425. parseFor() {
  426. this.readToken(); // skip 'for'
  427. const forTokens = this.readTokensUntil( ')' ).slice( 1, - 1 );
  428. const initializationTokens = this.getTokensUntil( ';', forTokens, 0 ).slice( 0, - 1 );
  429. const conditionTokens = this.getTokensUntil( ';', forTokens, initializationTokens.length + 1 ).slice( 0, - 1 );
  430. const afterthoughtTokens = forTokens.slice( initializationTokens.length + conditionTokens.length + 2 );
  431. let initialization;
  432. if ( initializationTokens[ 0 ] && isType( initializationTokens[ 0 ].str ) ) {
  433. initialization = this.parseVariablesFromToken( initializationTokens );
  434. } else {
  435. initialization = this.parseExpressionFromTokens( initializationTokens );
  436. }
  437. const condition = this.parseExpressionFromTokens( conditionTokens );
  438. const afterthought = this.parseExpressionFromTokens( afterthoughtTokens );
  439. const statement = new For( initialization, condition, afterthought );
  440. if ( this.getToken().str === '{' ) {
  441. this.parseBlock( statement );
  442. } else {
  443. statement.body.push( this.parseExpression() );
  444. }
  445. return statement;
  446. }
  447. parseIf() {
  448. const parseIfExpression = () => {
  449. this.readToken(); // skip 'if'
  450. const condTokens = this.readTokensUntil( ')' );
  451. return this.parseExpressionFromTokens( condTokens.slice( 1, condTokens.length - 1 ) );
  452. };
  453. const parseIfBlock = ( cond ) => {
  454. if ( this.getToken().str === '{' ) {
  455. this.parseBlock( cond );
  456. } else {
  457. cond.body.push( this.parseExpression() );
  458. }
  459. };
  460. //
  461. const conditional = new Conditional( parseIfExpression() );
  462. parseIfBlock( conditional );
  463. //
  464. let current = conditional;
  465. while ( this.getToken() && this.getToken().str === 'else' ) {
  466. this.readToken(); // skip 'else'
  467. const previous = current;
  468. if ( this.getToken().str === 'if' ) {
  469. current = new Conditional( parseIfExpression() );
  470. } else {
  471. current = new Conditional();
  472. }
  473. previous.elseConditional = current;
  474. parseIfBlock( current );
  475. }
  476. return conditional;
  477. }
  478. parseBlock( scope ) {
  479. const firstToken = this.getToken();
  480. if ( firstToken.str === '{' ) {
  481. this.readToken(); // skip '{'
  482. }
  483. let groupIndex = 0;
  484. while ( this.index < this.tokens.length ) {
  485. const token = this.getToken();
  486. let statement = null;
  487. groupIndex += getGroupDelta( token.str );
  488. if ( groupIndex < 0 ) {
  489. this.readToken(); // skip '}'
  490. break;
  491. }
  492. //
  493. if ( token.isLiteral ) {
  494. if ( token.str === 'const' ) {
  495. statement = this.parseVariables();
  496. } else if ( token.str === 'uniform' ) {
  497. statement = this.parseUniform();
  498. } else if ( token.str === 'varying' ) {
  499. statement = this.parseVarying();
  500. } else if ( isType( token.str ) ) {
  501. if ( this.getToken( 2 ).str === '(' ) {
  502. statement = this.parseFunction();
  503. } else {
  504. statement = this.parseVariables();
  505. }
  506. } else if ( token.str === 'return' ) {
  507. statement = this.parseReturn();
  508. } else if ( token.str === 'if' ) {
  509. statement = this.parseIf();
  510. } else if ( token.str === 'for' ) {
  511. statement = this.parseFor();
  512. } else {
  513. statement = this.parseExpression();
  514. }
  515. }
  516. if ( statement ) {
  517. scope.body.push( statement );
  518. } else {
  519. this.index ++;
  520. }
  521. }
  522. }
  523. _evalOperator( operator ) {
  524. if ( operator.type.includes( '=' ) ) {
  525. const parameter = this._getFunctionParameter( operator.left.property );
  526. if ( parameter !== undefined ) {
  527. // Parameters are immutable in WGSL
  528. parameter.immutable = false;
  529. }
  530. }
  531. return operator;
  532. }
  533. _getFunctionParameter( name ) {
  534. if ( this._currentFunction ) {
  535. for ( const param of this._currentFunction.params ) {
  536. if ( param.name === name ) {
  537. return param;
  538. }
  539. }
  540. }
  541. }
  542. parse( source ) {
  543. let polyfill = '';
  544. for ( const keyword of this.keywords ) {
  545. if ( new RegExp( `(^|\\b)${ keyword.name }($|\\b)`, 'gm' ).test( source ) ) {
  546. polyfill += keyword.polyfill + '\n';
  547. }
  548. }
  549. if ( polyfill ) {
  550. polyfill = '// Polyfills\n\n' + polyfill + '\n';
  551. }
  552. this.index = 0;
  553. this.tokenizer = new Tokenizer( polyfill + source ).tokenize();
  554. const program = new Program();
  555. this.parseBlock( program );
  556. return program;
  557. }
  558. }
  559. export default GLSLDecoder;
粤ICP备19079148号