@galacean/engine-shaderlab 2.0.0-alpha.24 → 2.0.0-alpha.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var engine = require('@galacean/engine');
6
6
 
7
+ function _instanceof(left, right) {
8
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
9
+ return !!right[Symbol.hasInstance](left);
10
+ } else return left instanceof right;
11
+ }
12
+
7
13
  function _array_like_to_array(arr, len) {
8
14
  if (len == null || len > arr.length) len = arr.length;
9
15
 
@@ -63,12 +69,6 @@ function _inherits(subClass, superClass) {
63
69
  if (superClass) _set_prototype_of(subClass, superClass);
64
70
  }
65
71
 
66
- function _instanceof(left, right) {
67
- if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
68
- return !!right[Symbol.hasInstance](left);
69
- } else return left instanceof right;
70
- }
71
-
72
72
  function _defineProperties(target, props) {
73
73
  for (var i = 0; i < props.length; i++) {
74
74
  var descriptor = props[i];
@@ -5479,933 +5479,452 @@ Lexer._lexemeTable = {
5479
5479
  "#undef": Keyword.MACRO_UNDEF
5480
5480
  };
5481
5481
 
5482
- function _type_of(obj) {
5483
- "@swc/helpers - typeof";
5484
-
5485
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
5486
- }
5487
-
5488
- var MacroParserToken = /*#__PURE__*/ function(MacroParserToken) {
5489
- MacroParserToken[MacroParserToken["id"] = 0] = "id";
5490
- MacroParserToken[MacroParserToken["line_remain"] = 1] = "line_remain";
5491
- MacroParserToken[MacroParserToken["chunk"] = 2] = "chunk";
5492
- MacroParserToken[MacroParserToken["int_constant"] = 3] = "int_constant";
5493
- MacroParserToken[MacroParserToken["string_const"] = 4] = "string_const";
5494
- /** \>> */ MacroParserToken[MacroParserToken["right_op"] = 5] = "right_op";
5495
- /** << */ MacroParserToken[MacroParserToken["left_op"] = 6] = "left_op";
5496
- MacroParserToken[MacroParserToken["left_paren"] = 7] = "left_paren";
5497
- MacroParserToken[MacroParserToken["right_paren"] = 8] = "right_paren";
5498
- /** \>= */ MacroParserToken[MacroParserToken["ge"] = 9] = "ge";
5499
- /** <= */ MacroParserToken[MacroParserToken["le"] = 10] = "le";
5500
- /** == */ MacroParserToken[MacroParserToken["eq"] = 11] = "eq";
5501
- /** != */ MacroParserToken[MacroParserToken["neq"] = 12] = "neq";
5502
- /** && */ MacroParserToken[MacroParserToken["and"] = 13] = "and";
5503
- /** || */ MacroParserToken[MacroParserToken["or"] = 14] = "or";
5504
- /** < */ MacroParserToken[MacroParserToken["left_angle"] = 15] = "left_angle";
5505
- /** \> */ MacroParserToken[MacroParserToken["right_angle"] = 16] = "right_angle";
5506
- /** \* */ MacroParserToken[MacroParserToken["star"] = 17] = "star";
5507
- /** + */ MacroParserToken[MacroParserToken["plus"] = 18] = "plus";
5508
- /** \- */ MacroParserToken[MacroParserToken["dash"] = 19] = "dash";
5509
- /** ! */ MacroParserToken[MacroParserToken["bang"] = 20] = "bang";
5510
- /** \/ */ MacroParserToken[MacroParserToken["slash"] = 21] = "slash";
5511
- /** % */ MacroParserToken[MacroParserToken["percent"] = 22] = "percent";
5512
- MacroParserToken[MacroParserToken["EOF"] = 100] = "EOF";
5513
- return MacroParserToken;
5514
- }({});
5515
- var MacroParserKeyword = /*#__PURE__*/ function(MacroParserKeyword) {
5516
- MacroParserKeyword[MacroParserKeyword["define"] = 101] = "define";
5517
- MacroParserKeyword[MacroParserKeyword["undef"] = 102] = "undef";
5518
- MacroParserKeyword[MacroParserKeyword["if"] = 103] = "if";
5519
- MacroParserKeyword[MacroParserKeyword["ifdef"] = 104] = "ifdef";
5520
- MacroParserKeyword[MacroParserKeyword["ifndef"] = 105] = "ifndef";
5521
- MacroParserKeyword[MacroParserKeyword["else"] = 106] = "else";
5522
- MacroParserKeyword[MacroParserKeyword["elif"] = 107] = "elif";
5523
- MacroParserKeyword[MacroParserKeyword["endif"] = 108] = "endif";
5524
- MacroParserKeyword[MacroParserKeyword["defined"] = 109] = "defined";
5525
- return MacroParserKeyword;
5526
- }({});
5527
-
5528
- var MacroDefine = /*#__PURE__*/ function() {
5529
- function MacroDefine(macro, body, location, args) {
5530
- this.macro = macro;
5531
- this.body = body;
5532
- this.location = location;
5533
- this.args = args;
5534
- if ((args == null ? void 0 : args.length) > 0) {
5535
- this._argsLexemes = this.args.map(function(item) {
5536
- return item.lexeme;
5537
- });
5538
- this._replaceRegex = new RegExp("\\b(" + this._argsLexemes.join("|") + ")\\b", "g");
5539
- }
5540
- }
5541
- var _proto = MacroDefine.prototype;
5542
- _proto.expandFunctionBody = function expandFunctionBody(args) {
5543
- var _this = this;
5544
- var _this_args;
5545
- if (args.length !== ((_this_args = this.args) == null ? void 0 : _this_args.length)) {
5546
- throw ShaderLabUtils.createGSError("mismatched function macro", GSErrorName.PreprocessorError, "", this.location);
5547
- }
5548
- if (args.length === 0) {
5549
- return this.body.lexeme;
5550
- }
5551
- return this.body.lexeme.replace(this._replaceRegex, function(m) {
5552
- return args[_this._argsLexemes.indexOf(m)];
5553
- });
5554
- };
5555
- _create_class(MacroDefine, [
5556
- {
5557
- key: "isFunction",
5558
- get: function get() {
5559
- return !!this.args;
5560
- }
5561
- }
5562
- ]);
5563
- return MacroDefine;
5564
- }();
5565
-
5566
- var BlockInfo = function BlockInfo(sourceFile, rangeInFile, sourceMap) {
5567
- this.sourceFile = sourceFile != null ? sourceFile : "__main__";
5568
- this.rangeInFile = rangeInFile;
5569
- this.sourceMap = sourceMap;
5482
+ /** Must stay in sync with ShaderPreprocessorDirective in @galacean/engine-core */ var ShaderPreprocessorDirective = {
5483
+ Text: 0,
5484
+ IfDef: 1,
5485
+ IfNdef: 2,
5486
+ IfCmp: 3,
5487
+ IfExpr: 4,
5488
+ Else: 5,
5489
+ Endif: 6,
5490
+ Define: 7,
5491
+ DefineVal: 8,
5492
+ DefineFunc: 9,
5493
+ Undef: 10
5570
5494
  };
5571
- var MapRange = /*#__PURE__*/ function() {
5572
- function MapRange(sourceBlock, rangeInBlock, generatedLoc) {
5573
- this.sourceLoc = {
5574
- block: sourceBlock,
5575
- rangeInBlock: rangeInBlock
5576
- };
5577
- this.generatedLoc = generatedLoc;
5578
- }
5579
- var _proto = MapRange.prototype;
5580
- _proto.getSourceIndex = function getSourceIndex(generatedIdx) {
5581
- var _block_rangeInFile;
5582
- var _this_sourceLoc = this.sourceLoc, block = _this_sourceLoc.block, rangeInBlock = _this_sourceLoc.rangeInBlock;
5583
- if (block.sourceMap) {
5584
- if (block.sourceFile !== "__main__") return block.sourceMap.map(generatedIdx - this.generatedLoc.start);
5585
- else if (rangeInBlock) {
5586
- var _block_rangeInFile1;
5587
- var _block_rangeInFile_start_index;
5588
- return {
5589
- sourceFile: block.sourceFile,
5590
- index: ((_block_rangeInFile_start_index = (_block_rangeInFile1 = block.rangeInFile) == null ? void 0 : _block_rangeInFile1.start.index) != null ? _block_rangeInFile_start_index : 0) + rangeInBlock.start.index
5591
- };
5592
- }
5593
- }
5594
- var _block_rangeInFile_start_index1;
5595
- return {
5596
- index: generatedIdx - this.generatedLoc.start + rangeInBlock.start.index + ((_block_rangeInFile_start_index1 = (_block_rangeInFile = block.rangeInFile) == null ? void 0 : _block_rangeInFile.start.index) != null ? _block_rangeInFile_start_index1 : 0),
5597
- sourceFile: this.sourceLoc.block.sourceFile
5598
- };
5599
- };
5600
- return MapRange;
5601
- }();
5602
- var PpSourceMap = /*#__PURE__*/ function() {
5603
- function PpSourceMap() {
5604
- this.mapRanges = [];
5605
- }
5606
- var _proto = PpSourceMap.prototype;
5607
- _proto.addMapRange = function addMapRange(mapRange) {
5608
- this.mapRanges.push(mapRange);
5609
- };
5610
- /**
5611
- * @returns index
5612
- */ _proto.map = function map(index) {
5613
- var curRange;
5614
- for(var _iterator = _create_for_of_iterator_helper_loose(this.mapRanges), _step; !(_step = _iterator()).done;){
5615
- var range = _step.value;
5616
- var generatedLoc = range.generatedLoc;
5617
- if (PpSourceMap.rangeContains(generatedLoc, index)) {
5618
- return range.getSourceIndex(index);
5619
- } else if (range.generatedLoc.start < index) {
5620
- curRange = range;
5621
- continue;
5622
- } else {
5495
+ /**
5496
+ * @internal
5497
+ */ var ShaderInstructionEncoder = /*#__PURE__*/ function() {
5498
+ function ShaderInstructionEncoder() {}
5499
+ ShaderInstructionEncoder.parse = function parse(glsl) {
5500
+ var instructions = [];
5501
+ var length = glsl.length;
5502
+ var pos = 0;
5503
+ var backfillStack = [];
5504
+ while(pos < length){
5505
+ var directiveStart = ShaderInstructionEncoder._findDirectiveStart(glsl, pos, length);
5506
+ if (directiveStart === -1) {
5507
+ ShaderInstructionEncoder._pushText(instructions, glsl, pos, length);
5623
5508
  break;
5624
5509
  }
5625
- }
5626
- if (!curRange) return {
5627
- sourceFile: "__main__",
5628
- index: index
5629
- };
5630
- return {
5631
- index: index - curRange.generatedLoc.end + curRange.sourceLoc.rangeInBlock.end.index,
5632
- sourceFile: curRange.sourceLoc.block.sourceFile
5633
- };
5634
- };
5635
- PpSourceMap.rangeContains = function rangeContains(range, index) {
5636
- return range.start <= index && range.end > index;
5637
- };
5638
- return PpSourceMap;
5639
- }();
5640
-
5641
- var MacroParserLexer = /*#__PURE__*/ function(BaseLexer1) {
5642
- _inherits(MacroParserLexer, BaseLexer1);
5643
- function MacroParserLexer(source, file, blockRange) {
5644
- if (file === void 0) file = "__main__";
5645
- var _this;
5646
- _this = BaseLexer1.call(this, source) || this, _this.macroLvl = 0, _this.sourceMap = new PpSourceMap();
5647
- _this.file = file;
5648
- _this.blockRange = blockRange;
5649
- return _this;
5650
- }
5651
- var _proto = MacroParserLexer.prototype;
5652
- _proto.scanWordsUntilTerminator = function scanWordsUntilTerminator(terminatorChar) {
5653
- var tokens = [];
5654
- while(true){
5655
- this.skipSpace(true);
5656
- if (BaseLexer.isAlpha(this.getCurCharCode())) {
5657
- tokens.push(this.scanWord());
5658
- } else if (this.getCurChar() === terminatorChar) {
5659
- this.advance(1);
5660
- return tokens;
5661
- } else {
5662
- this.advance(1);
5663
- }
5664
- }
5665
- };
5666
- _proto.scanWord = function scanWord() {
5667
- // Skip all non-alphabetic characters, primarily used for handling defined(MACRO) syntax
5668
- while(!BaseLexer.isAlpha(this.getCurCharCode()) && !this.isEnd()){
5669
- this.advance(1);
5670
- }
5671
- if (this.isEnd()) {
5672
- return EOF;
5673
- }
5674
- var start = this._currentIndex;
5675
- while(BaseLexer.isAlnum(this.getCurCharCode()) && !this.isEnd()){
5676
- this.advance(1);
5677
- }
5678
- var end = this._currentIndex;
5679
- var word = this._source.slice(start, end);
5680
- if (end === start) {
5681
- this.throwError(this.getShaderPosition(0), "no word found.");
5682
- }
5683
- var token = BaseToken.pool.get();
5684
- var _MacroParserLexer__lexemeTable_word;
5685
- var tokenType = (_MacroParserLexer__lexemeTable_word = MacroParserLexer._lexemeTable[word]) != null ? _MacroParserLexer__lexemeTable_word : MacroParserToken.id;
5686
- token.set(tokenType, word, this.getShaderPosition(word.length));
5687
- return token;
5688
- };
5689
- _proto.scanToken = function scanToken() {
5690
- this.skipCommentsAndSpace();
5691
- if (this.isEnd()) {
5692
- return;
5693
- }
5694
- var source = this._source;
5695
- var start = this._currentIndex;
5696
- var found = false;
5697
- for(var n = source.length; this._currentIndex < n;){
5698
- if (MacroParserLexer._isPpCharacters(source.charCodeAt(this._currentIndex))) {
5699
- this.advance(1);
5700
- found = true;
5701
- } else {
5702
- if (found) {
5703
- break;
5510
+ if (directiveStart > pos) {
5511
+ ShaderInstructionEncoder._pushText(instructions, glsl, pos, directiveStart);
5512
+ }
5513
+ var lineEnd = glsl.indexOf("\n", directiveStart);
5514
+ if (lineEnd === -1) lineEnd = length;
5515
+ var line = glsl.substring(directiveStart, lineEnd);
5516
+ pos = lineEnd < length ? lineEnd + 1 : length;
5517
+ var match = ShaderInstructionEncoder._DIRECTIVE_RE.exec(line);
5518
+ if (!match) {
5519
+ var last = instructions.length > 0 ? instructions[instructions.length - 1] : null;
5520
+ var text = lineEnd < length ? line + "\n" : line;
5521
+ if (last && last[0] === ShaderPreprocessorDirective.Text) {
5522
+ last[1] += text;
5523
+ } else {
5524
+ instructions.push([
5525
+ ShaderPreprocessorDirective.Text,
5526
+ text
5527
+ ]);
5704
5528
  }
5705
- this.advance(1);
5706
- this.skipCommentsAndSpace();
5707
- start = this._currentIndex;
5708
- }
5709
- }
5710
- var lexeme = source.slice(start, this._currentIndex);
5711
- var token = BaseToken.pool.get();
5712
- var _MacroParserLexer__lexemeTable_lexeme;
5713
- var type = (_MacroParserLexer__lexemeTable_lexeme = MacroParserLexer._lexemeTable[lexeme]) != null ? _MacroParserLexer__lexemeTable_lexeme : MacroParserToken.id;
5714
- token.set(type, lexeme, this.getShaderPosition(this._currentIndex - start));
5715
- if (type === MacroParserKeyword.if || type === MacroParserKeyword.ifdef || type === MacroParserKeyword.ifndef) {
5716
- this.macroLvl++;
5717
- } else if (type === MacroParserKeyword.endif) {
5718
- this.macroLvl--;
5719
- }
5720
- return token;
5721
- };
5722
- _proto.scanQuotedString = function scanQuotedString() {
5723
- this.skipSpace(true);
5724
- var source = this._source;
5725
- var sourceLength = source.length;
5726
- var start = this.getShaderPosition(0);
5727
- var index = this._currentIndex;
5728
- // Check for opening quote
5729
- if (source.charCodeAt(index) !== 34) {
5730
- // 34 = '"'
5731
- this.throwError(start, "Unexpected char, expected '\"'");
5732
- }
5733
- var contentStart = ++index; // Skip opening quote and record start
5734
- // Fast scan to closing quote
5735
- while(index < sourceLength && source.charCodeAt(index) !== 34){
5736
- index++;
5737
- }
5738
- if (index >= sourceLength) {
5739
- this.throwError(this.getShaderPosition(0), "Unexpected char, expected '\"'");
5740
- }
5741
- var lexeme = source.slice(contentStart, index);
5742
- this.advance(index + 1 - this._currentIndex); // Skip to after closing quote
5743
- var token = BaseToken.pool.get();
5744
- token.set(MacroParserToken.string_const, lexeme, start);
5745
- return token;
5746
- };
5747
- _proto.scanToChar = function scanToChar(char) {
5748
- var source = this._source;
5749
- while(source[this._currentIndex] !== char && !this.isEnd()){
5750
- this.advance(1);
5751
- }
5752
- };
5753
- _proto.scanMacroBranchBody = function scanMacroBranchBody() {
5754
- var shaderPosition = this.getShaderPosition(0);
5755
- var startLevel = this.macroLvl;
5756
- var nextDirective = this.scanToken();
5757
- while(true){
5758
- var type = nextDirective.type;
5759
- if (type === MacroParserKeyword.endif && startLevel - 1 === this.macroLvl) {
5760
- break;
5761
- } else if ((type === MacroParserKeyword.elif || type === MacroParserKeyword.else) && startLevel === this.macroLvl) {
5762
- break;
5763
- }
5764
- nextDirective = this.scanToken();
5765
- }
5766
- var lexeme = this._source.slice(shaderPosition.index, this._currentIndex - nextDirective.lexeme.length - 1);
5767
- var body = BaseToken.pool.get();
5768
- body.set(MacroParserToken.chunk, lexeme, shaderPosition);
5769
- return {
5770
- body: body,
5771
- nextDirective: nextDirective
5772
- };
5773
- };
5774
- _proto.scanPairedBlock = function scanPairedBlock(lc, rc) {
5775
- this.scanToChar(lc);
5776
- var level = 0;
5777
- var source = this._source;
5778
- do {
5779
- var curChar = source[this._currentIndex];
5780
- if (curChar === lc) {
5781
- level++;
5782
- } else if (curChar === rc) {
5783
- level--;
5784
- }
5785
- this.advance(1);
5786
- }while (level > 0);
5787
- };
5788
- /**
5789
- * @returns end ShaderPosition
5790
- */ _proto.scanRemainMacro = function scanRemainMacro() {
5791
- var startLvl = this.macroLvl;
5792
- var directive = this.scanToken();
5793
- while(!this.isEnd() && (directive.type !== MacroParserKeyword.endif || startLvl - 1 !== this.macroLvl)){
5794
- directive = this.scanToken();
5795
- }
5796
- return this.getShaderPosition(0);
5797
- };
5798
- _proto.peekNonSpace = function peekNonSpace() {
5799
- var current = this._currentIndex;
5800
- while(/\s/.test(this._source[current])){
5801
- current += 1;
5802
- }
5803
- return this._source[current];
5804
- };
5805
- _proto.scanInteger = function scanInteger() {
5806
- var start = this._currentIndex;
5807
- while(BaseLexer.isDigit(this.getCurCharCode())){
5808
- this.advance(1);
5809
- }
5810
- if (this._currentIndex === start) {
5811
- this.throwError(this.getShaderPosition(0), "no integer found");
5812
- }
5813
- var integer = this._source.slice(start, this._currentIndex);
5814
- var token = BaseToken.pool.get();
5815
- token.set(MacroParserToken.int_constant, integer, this.getShaderPosition(0));
5816
- return token;
5817
- };
5818
- _proto.scanMacroBody = function scanMacroBody() {
5819
- this.skipSpace(false);
5820
- var lexeme = "";
5821
- var source = this._source;
5822
- var sourceLength = source.length;
5823
- var start = this.getShaderPosition(0);
5824
- while(this._currentIndex < sourceLength){
5825
- var charCode = source.charCodeAt(this._currentIndex);
5826
- // Check for line break (terminates macro definition), break when encounter "\n"
5827
- if (charCode === 10) {
5828
- break;
5529
+ continue;
5829
5530
  }
5830
- // Check for comments (both single-line and multi-line)
5831
- if (charCode === 47) {
5832
- var nextIndex = this._currentIndex + 1;
5833
- if (nextIndex < sourceLength) {
5834
- var nextCharCode = source.charCodeAt(nextIndex);
5835
- // Single-line comment (terminates macro definition), break when encounter "//"
5836
- if (nextCharCode === 47) {
5531
+ var keyword = match[1];
5532
+ var rest = match[2].trim();
5533
+ switch(keyword){
5534
+ case "ifdef":
5535
+ {
5536
+ var idx = instructions.length;
5537
+ instructions.push([
5538
+ ShaderPreprocessorDirective.IfDef,
5539
+ rest,
5540
+ -1
5541
+ ]);
5542
+ backfillStack.push([
5543
+ idx
5544
+ ]);
5545
+ break;
5546
+ }
5547
+ case "ifndef":
5548
+ {
5549
+ var idx1 = instructions.length;
5550
+ instructions.push([
5551
+ ShaderPreprocessorDirective.IfNdef,
5552
+ rest,
5553
+ -1
5554
+ ]);
5555
+ backfillStack.push([
5556
+ idx1
5557
+ ]);
5837
5558
  break;
5838
5559
  }
5839
- // Multi-line comment (skip but don't terminate)
5840
- if (nextCharCode === 42) {
5841
- this.advance(2); // Skip "/*"
5842
- // Skip until end of multi-line comment
5843
- while(this._currentIndex + 1 < sourceLength){
5844
- var currentIndex = this._currentIndex;
5845
- if (source.charCodeAt(currentIndex) === 42 && source.charCodeAt(currentIndex + 1) === 47) {
5846
- this.advance(2); // Skip "*/
5847
- break;
5560
+ case "if":
5561
+ {
5562
+ var cond = ShaderInstructionEncoder._parseCondition(rest);
5563
+ var idx2 = instructions.length;
5564
+ ShaderInstructionEncoder._pushConditionInstruction(instructions, cond);
5565
+ backfillStack.push([
5566
+ idx2
5567
+ ]);
5568
+ break;
5569
+ }
5570
+ case "elif":
5571
+ {
5572
+ var stack = backfillStack[backfillStack.length - 1];
5573
+ var prevIdx = stack[stack.length - 1];
5574
+ var elseIdx = instructions.length;
5575
+ instructions.push([
5576
+ ShaderPreprocessorDirective.Else,
5577
+ -1
5578
+ ]);
5579
+ stack.push(elseIdx);
5580
+ ShaderInstructionEncoder._backfillJump(instructions[prevIdx], instructions.length);
5581
+ var cond1 = ShaderInstructionEncoder._parseCondition(rest);
5582
+ var idx3 = instructions.length;
5583
+ ShaderInstructionEncoder._pushConditionInstruction(instructions, cond1);
5584
+ stack.push(idx3);
5585
+ break;
5586
+ }
5587
+ case "else":
5588
+ {
5589
+ var stack1 = backfillStack[backfillStack.length - 1];
5590
+ var prevIdx1 = stack1[stack1.length - 1];
5591
+ var elseIdx1 = instructions.length;
5592
+ instructions.push([
5593
+ ShaderPreprocessorDirective.Else,
5594
+ -1
5595
+ ]);
5596
+ stack1.push(elseIdx1);
5597
+ ShaderInstructionEncoder._backfillJump(instructions[prevIdx1], instructions.length);
5598
+ break;
5599
+ }
5600
+ case "endif":
5601
+ {
5602
+ var endifIdx = instructions.length;
5603
+ instructions.push([
5604
+ ShaderPreprocessorDirective.Endif
5605
+ ]);
5606
+ var stack2 = backfillStack.pop();
5607
+ if (stack2) {
5608
+ var afterEndif = endifIdx + 1;
5609
+ for(var j = 0; j < stack2.length; j++){
5610
+ var inst = instructions[stack2[j]];
5611
+ if (inst[0] === ShaderPreprocessorDirective.Else) {
5612
+ inst[1] = afterEndif;
5613
+ } else {
5614
+ ShaderInstructionEncoder._backfillJump(inst, afterEndif, true);
5615
+ }
5848
5616
  }
5849
- this.advance(1);
5850
5617
  }
5851
- lexeme += " "; // Replace comment with space
5852
- continue;
5618
+ break;
5619
+ }
5620
+ case "define":
5621
+ {
5622
+ var funcMatch = ShaderInstructionEncoder._FUNC_MACRO_RE.exec(rest);
5623
+ if (funcMatch) {
5624
+ var params = funcMatch[2].split(",").map(function(p) {
5625
+ return p.trim();
5626
+ }).filter(function(p) {
5627
+ return p.length > 0;
5628
+ });
5629
+ instructions.push([
5630
+ ShaderPreprocessorDirective.DefineFunc,
5631
+ funcMatch[1],
5632
+ params,
5633
+ ShaderInstructionEncoder._stripLineComment(funcMatch[3].trim())
5634
+ ]);
5635
+ } else {
5636
+ var spaceIdx = rest.indexOf(" ");
5637
+ if (spaceIdx === -1) {
5638
+ instructions.push([
5639
+ ShaderPreprocessorDirective.Define,
5640
+ rest
5641
+ ]);
5642
+ } else {
5643
+ instructions.push([
5644
+ ShaderPreprocessorDirective.DefineVal,
5645
+ rest.substring(0, spaceIdx),
5646
+ ShaderInstructionEncoder._stripLineComment(rest.substring(spaceIdx + 1).trim())
5647
+ ]);
5648
+ }
5649
+ }
5650
+ break;
5651
+ }
5652
+ case "undef":
5653
+ {
5654
+ instructions.push([
5655
+ ShaderPreprocessorDirective.Undef,
5656
+ rest
5657
+ ]);
5658
+ break;
5853
5659
  }
5854
- }
5855
5660
  }
5856
- // Accumulate useful character
5857
- lexeme += source[this._currentIndex];
5858
- this.advance(1);
5859
5661
  }
5860
- if (lexeme === "") {
5861
- return null;
5862
- }
5863
- var valueToken = BaseToken.pool.get();
5864
- valueToken.set(MacroParserToken.line_remain, lexeme, ShaderLab.createRange(start, this.getShaderPosition(0)));
5865
- return valueToken;
5866
- };
5867
- MacroParserLexer._isPpCharacters = function _isPpCharacters(charCode) {
5868
- return charCode === 35 || // #
5869
- BaseLexer.isAlnum(charCode) // _, A-Z, a-z, 0-9
5870
- ;
5662
+ return instructions;
5871
5663
  };
5872
- return MacroParserLexer;
5873
- }(BaseLexer);
5874
- MacroParserLexer._lexemeTable = {
5875
- "#define": MacroParserKeyword.define,
5876
- "#undef": MacroParserKeyword.undef,
5877
- "#if": MacroParserKeyword.if,
5878
- "#ifdef": MacroParserKeyword.ifdef,
5879
- "#ifndef": MacroParserKeyword.ifndef,
5880
- "#else": MacroParserKeyword.else,
5881
- "#elif": MacroParserKeyword.elif,
5882
- "#endif": MacroParserKeyword.endif,
5883
- defined: MacroParserKeyword.defined
5884
- };
5885
-
5886
- var PpUtils = /*#__PURE__*/ function() {
5887
- function PpUtils() {}
5888
- PpUtils.expand = function expand(segments, source, sourceMap) {
5889
- var ret = [];
5890
- var startIdx = 0;
5891
- var generatedIdx = 0;
5892
- for(var _iterator = _create_for_of_iterator_helper_loose(segments), _step; !(_step = _iterator()).done;){
5893
- var seg = _step.value;
5894
- var originSlice = source.slice(startIdx, seg.rangeInBlock.start.index);
5895
- ret.push(originSlice, seg.replace);
5896
- var generatedIdxEnd = generatedIdx + originSlice.length + seg.replace.length;
5897
- var mapRange = new MapRange(seg.block, seg.rangeInBlock, {
5898
- start: generatedIdx + originSlice.length,
5899
- end: generatedIdxEnd
5900
- });
5901
- sourceMap == null ? void 0 : sourceMap.addMapRange(mapRange);
5902
- startIdx = seg.rangeInBlock.end.index;
5903
- generatedIdx = generatedIdxEnd;
5664
+ ShaderInstructionEncoder._pushConditionInstruction = function _pushConditionInstruction(instructions, cond) {
5665
+ if (cond.t === "def") {
5666
+ instructions.push([
5667
+ ShaderPreprocessorDirective.IfDef,
5668
+ cond.m,
5669
+ -1
5670
+ ]);
5671
+ } else if (cond.t === "ndef") {
5672
+ instructions.push([
5673
+ ShaderPreprocessorDirective.IfNdef,
5674
+ cond.m,
5675
+ -1
5676
+ ]);
5677
+ } else if (cond.t === "cmp") {
5678
+ instructions.push([
5679
+ ShaderPreprocessorDirective.IfCmp,
5680
+ cond.m,
5681
+ cond.op,
5682
+ cond.v,
5683
+ -1
5684
+ ]);
5685
+ } else {
5686
+ instructions.push([
5687
+ ShaderPreprocessorDirective.IfExpr,
5688
+ cond,
5689
+ -1
5690
+ ]);
5904
5691
  }
5905
- ret.push(source.slice(startIdx));
5906
- var result = ret.join("");
5907
- // Replace multiple consecutive newlines with a single newline to clean up the output
5908
- return result.replace(/\n\s*\n+/g, "\n");
5909
5692
  };
5910
- return PpUtils;
5911
- }();
5912
-
5913
- /** @internal */ var MacroParser = /*#__PURE__*/ function() {
5914
- function MacroParser() {}
5915
- MacroParser.parse = function parse(source, macros) {
5916
- MacroParser._reset();
5917
- for(var _iterator = _create_for_of_iterator_helper_loose(macros), _step; !(_step = _iterator()).done;){
5918
- var macro = _step.value;
5919
- MacroParser._addPredefinedMacro(macro.name, macro.value);
5920
- }
5921
- this.lexer = new MacroParserLexer(source);
5922
- return MacroParser._parseDirectives(this.lexer);
5923
- };
5924
- MacroParser._reset = function _reset() {
5925
- this._expandSegmentsStack.length = 0;
5926
- this._expandSegmentsStack.push([]);
5927
- this._definedMacros.clear();
5928
- this._addPredefinedMacro("GL_ES");
5929
- this._errors.length = 0;
5930
- };
5931
- MacroParser._addPredefinedMacro = function _addPredefinedMacro(macro, value) {
5932
- var token = BaseToken.pool.get();
5933
- token.set(MacroParserToken.id, macro);
5934
- var macroBody;
5935
- if (value != undefined) {
5936
- macroBody = BaseToken.pool.get();
5937
- macroBody.set(MacroParserToken.id, value);
5938
- }
5939
- this._definedMacros.set(macro, new MacroDefine(token, macroBody));
5940
- };
5941
- MacroParser._parseDirectives = function _parseDirectives(lexer) {
5942
- var directive;
5943
- while(directive = lexer.scanToken()){
5944
- switch(directive.type){
5945
- case MacroParserToken.id:
5946
- this._parseMacro(lexer, directive);
5947
- break;
5948
- case MacroParserKeyword.define:
5949
- this._parseDefine(lexer);
5950
- break;
5951
- case MacroParserKeyword.undef:
5952
- this._parseUndef(lexer);
5953
- break;
5954
- case MacroParserKeyword.if:
5955
- this._parseIfDirective(lexer, MacroParserKeyword.if);
5956
- break;
5957
- case MacroParserKeyword.ifndef:
5958
- this._parseIfDirective(lexer, MacroParserKeyword.ifndef);
5959
- break;
5960
- case MacroParserKeyword.ifdef:
5961
- this._parseIfDirective(lexer, MacroParserKeyword.ifdef);
5693
+ ShaderInstructionEncoder._findDirectiveStart = function _findDirectiveStart(source, from, length) {
5694
+ var i = from;
5695
+ while(i < length){
5696
+ var j = i;
5697
+ while(j < length){
5698
+ var c = source.charCodeAt(j);
5699
+ if (c === 32 /* space */ || c === 9 /* tab */ ) {
5700
+ j++;
5701
+ } else {
5962
5702
  break;
5703
+ }
5963
5704
  }
5705
+ if (j < length && source.charCodeAt(j) === 35 /* '#' */ ) return i;
5706
+ var nl = source.indexOf("\n", i);
5707
+ if (nl === -1) break;
5708
+ i = nl + 1;
5964
5709
  }
5965
- if (this._errors.length > 0) return null;
5966
- return PpUtils.expand(this._getExpandSegments(), lexer.source, lexer.sourceMap);
5967
- };
5968
- MacroParser._getExpandSegments = function _getExpandSegments() {
5969
- return this._expandSegmentsStack[this._expandSegmentsStack.length - 1];
5970
- };
5971
- MacroParser._reportError = function _reportError(loc, message, source, file) {
5972
- var error = ShaderLabUtils.createGSError(message, GSErrorName.PreprocessorError, source, loc, file);
5973
- this._errors.push(error);
5710
+ return -1;
5974
5711
  };
5975
- MacroParser._parseIfDirective = function _parseIfDirective(lexer, directiveType) {
5976
- var directiveLength = directiveType === MacroParserKeyword.if ? 3 : directiveType === MacroParserKeyword.ifdef ? 6 : 7; // #if = 3, #ifdef = 6, #ifndef = 7
5977
- var start = lexer.currentIndex - directiveLength;
5978
- var skipMacro = false;
5979
- var shouldInclude;
5980
- if (directiveType === MacroParserKeyword.if) {
5981
- shouldInclude = this._parseConstantExpression(lexer);
5712
+ ShaderInstructionEncoder._pushText = function _pushText(instructions, source, from, to) {
5713
+ if (from >= to) return;
5714
+ var last = instructions.length > 0 ? instructions[instructions.length - 1] : null;
5715
+ if (last && last[0] === ShaderPreprocessorDirective.Text) {
5716
+ last[1] += source.substring(from, to);
5982
5717
  } else {
5983
- var macroToken = lexer.scanWord();
5984
- var lexeme = macroToken.lexeme;
5985
- if (lexeme.startsWith("GL_")) {
5986
- skipMacro = true;
5987
- } else {
5988
- var defined = this._definedMacros.get(lexeme);
5989
- shouldInclude = directiveType === MacroParserKeyword.ifdef ? !!defined : !defined;
5990
- }
5991
- }
5992
- lexer.skipSpace(true);
5993
- var _lexer_scanMacroBranchBody = lexer.scanMacroBranchBody(), body = _lexer_scanMacroBranchBody.body, nextDirective = _lexer_scanMacroBranchBody.nextDirective;
5994
- if (skipMacro) return;
5995
- if (shouldInclude) {
5996
- var end = nextDirective.type === MacroParserKeyword.endif ? lexer.getShaderPosition(0) : lexer.scanRemainMacro();
5997
- var expanded = this._expandMacroChunk(body.lexeme, body.location, lexer);
5998
- this._addContentReplace(lexer.file, ShaderLab.createPosition(start), end, expanded.content, lexer.blockRange, expanded.sourceMap);
5999
- } else {
6000
- this._addEmptyReplace(lexer, start);
6001
- this._processConditionalDirective(nextDirective.type, lexer);
5718
+ instructions.push([
5719
+ ShaderPreprocessorDirective.Text,
5720
+ source.substring(from, to)
5721
+ ]);
6002
5722
  }
6003
5723
  };
6004
- MacroParser._processConditionalDirective = function _processConditionalDirective(directive, scanner) {
6005
- if (directive === MacroParserKeyword.endif) {
6006
- return;
6007
- }
6008
- var start = scanner.currentIndex;
6009
- if (directive === MacroParserKeyword.else) {
6010
- var body = scanner.scanMacroBranchBody().body;
6011
- var expanded = this._expandMacroChunk(body.lexeme, body.location, scanner);
6012
- this._addContentReplace(scanner.file, ShaderLab.createPosition(start), scanner.getShaderPosition(0), expanded.content, scanner.blockRange, expanded.sourceMap);
6013
- } else if (directive === MacroParserKeyword.elif) {
6014
- var constantExpr = this._parseConstantExpression(scanner);
6015
- var _scanner_scanMacroBranchBody = scanner.scanMacroBranchBody(), body1 = _scanner_scanMacroBranchBody.body, nextDirective = _scanner_scanMacroBranchBody.nextDirective;
6016
- if (constantExpr) {
6017
- var end = nextDirective.type === MacroParserKeyword.endif ? scanner.currentIndex : scanner.scanRemainMacro().index;
6018
- var expanded1 = this._expandMacroChunk(body1.lexeme, body1.location, scanner);
6019
- this._addContentReplace(scanner.file, ShaderLab.createPosition(start), ShaderLab.createPosition(end), expanded1.content, scanner.blockRange, expanded1.sourceMap);
6020
- } else {
6021
- this._addContentReplace(scanner.file, ShaderLab.createPosition(start), ShaderLab.createPosition(scanner.currentIndex), "", scanner.blockRange);
6022
- this._processConditionalDirective(nextDirective.type, scanner);
6023
- }
6024
- }
6025
- };
6026
- MacroParser._parseConstantExpression = function _parseConstantExpression(scanner) {
6027
- scanner.skipSpace(true);
6028
- return this._parseLogicalOrExpression(scanner);
6029
- };
6030
- MacroParser._parseLogicalOrExpression = function _parseLogicalOrExpression(scanner) {
6031
- var operand1 = this._parseLogicalAndExpression(scanner);
6032
- var operator = scanner.peek(2);
6033
- if (operator && operator === "||") {
6034
- scanner.advance(2);
6035
- scanner.skipSpace(false);
6036
- var operand2 = this._parseLogicalOrExpression(scanner);
6037
- return operand1 || operand2;
6038
- }
6039
- return operand1;
6040
- };
6041
- MacroParser._parseLogicalAndExpression = function _parseLogicalAndExpression(scanner) {
6042
- var operand1 = this._parseEqualityExpression(scanner);
6043
- var operator = scanner.peek(2);
6044
- if (operator && operator === "&&") {
6045
- scanner.advance(2);
6046
- scanner.skipSpace(false);
6047
- var operand2 = this._parseLogicalAndExpression(scanner);
6048
- return operand1 && operand2;
6049
- }
6050
- return operand1;
6051
- };
6052
- MacroParser._parseEqualityExpression = function _parseEqualityExpression(scanner) {
6053
- var operand1 = this._parseRelationalExpression(scanner);
6054
- var operator = scanner.peek(2);
6055
- if (operator && [
6056
- "==",
6057
- "!="
6058
- ].includes(operator)) {
6059
- scanner.advance(2);
6060
- scanner.skipSpace(false);
6061
- var operand2 = this._parseEqualityExpression(scanner);
6062
- switch(operator){
6063
- case "==":
6064
- return operand1 === operand2;
6065
- case "!=":
6066
- return operand1 !== operand2;
6067
- }
6068
- }
6069
- return operand1;
6070
- };
6071
- MacroParser._parseRelationalExpression = function _parseRelationalExpression(scanner) {
6072
- var operand1 = this._parseShiftExpression(scanner);
6073
- var operator = scanner.peek(2);
6074
- if (operator[1] !== "=") operator = operator[0];
6075
- if (operator && [
6076
- ">",
6077
- "<",
6078
- ">=",
6079
- "<="
6080
- ].includes(operator)) {
6081
- var opPos = scanner.getShaderPosition(0);
6082
- scanner.advance(operator.length);
6083
- scanner.skipSpace(false);
6084
- var operand2 = this._parseRelationalExpression(scanner);
6085
- if ((typeof operand1 === "undefined" ? "undefined" : _type_of(operand1)) !== (typeof operand2 === "undefined" ? "undefined" : _type_of(operand2)) && typeof operand1 !== "number") {
6086
- this._reportError(opPos, "invalid operator in relation expression.", scanner.source, scanner.file);
6087
- return;
6088
- }
6089
- switch(operator){
6090
- case ">":
6091
- return operand1 > operand2;
6092
- case "<":
6093
- return operand1 < operand2;
6094
- case ">=":
6095
- return operand1 >= operand2;
6096
- case "<=":
6097
- return operand1 <= operand2;
6098
- }
6099
- }
6100
- return operand1;
6101
- };
6102
- MacroParser._parseShiftExpression = function _parseShiftExpression(scanner) {
6103
- var operand1 = this._parseAdditiveExpression(scanner);
6104
- var operator = scanner.peek(2);
6105
- if (operator && [
6106
- ">>",
6107
- "<<"
6108
- ].includes(operator)) {
6109
- var opPos = scanner.getShaderPosition(0);
6110
- scanner.advance(2);
6111
- scanner.skipSpace(false);
6112
- var operand2 = this._parseShiftExpression(scanner);
6113
- if ((typeof operand1 === "undefined" ? "undefined" : _type_of(operand1)) !== (typeof operand2 === "undefined" ? "undefined" : _type_of(operand2)) && typeof operand1 !== "number") {
6114
- this._reportError(opPos, "invalid operator in shift expression.", scanner.source, scanner.file);
6115
- return;
6116
- }
6117
- switch(operator){
6118
- case ">>":
6119
- return operand1 >> operand2;
6120
- case "<<":
6121
- return operand1 << operand2;
6122
- }
6123
- }
6124
- return operand1;
6125
- };
6126
- MacroParser._parseAdditiveExpression = function _parseAdditiveExpression(scanner) {
6127
- var operand1 = this._parseMulticativeExpression(scanner);
6128
- if ([
6129
- ">",
6130
- "<"
6131
- ].includes(scanner.getCurChar())) {
6132
- var opPos = scanner.getShaderPosition(0);
6133
- scanner.advance(1);
6134
- var operator = scanner.getCurChar();
6135
- scanner.skipSpace(false);
6136
- var operand2 = this._parseAdditiveExpression(scanner);
6137
- if ((typeof operand1 === "undefined" ? "undefined" : _type_of(operand1)) !== (typeof operand2 === "undefined" ? "undefined" : _type_of(operand2)) && typeof operand1 !== "number") {
6138
- this._reportError(opPos, "invalid operator.", scanner.source, scanner.file);
6139
- return false;
6140
- }
6141
- switch(operator){
6142
- case "+":
6143
- return operand1 + operand2;
6144
- case "-":
6145
- return operand1 - operand2;
6146
- }
6147
- }
6148
- return operand1;
6149
- };
6150
- MacroParser._parseMulticativeExpression = function _parseMulticativeExpression(scanner) {
6151
- var operand1 = this._parseUnaryExpression(scanner);
6152
- scanner.skipSpace(false);
6153
- if ([
6154
- "*",
6155
- "/",
6156
- "%"
6157
- ].includes(scanner.getCurChar())) {
6158
- var opPos = scanner.getShaderPosition(0);
6159
- var operator = scanner.getCurChar();
6160
- scanner.skipSpace(false);
6161
- var operand2 = this._parseMulticativeExpression(scanner);
6162
- if ((typeof operand1 === "undefined" ? "undefined" : _type_of(operand1)) !== (typeof operand2 === "undefined" ? "undefined" : _type_of(operand2)) && typeof operand1 !== "number") {
6163
- this._reportError(opPos, "invalid operator.", scanner.source, scanner.file);
6164
- return;
6165
- }
6166
- switch(operator){
6167
- case "*":
6168
- return operand1 * operand2;
6169
- case "/":
6170
- return operand1 / operand2;
6171
- case "%":
6172
- return operand1 % operand2;
6173
- }
6174
- }
6175
- return operand1;
6176
- };
6177
- MacroParser._parseUnaryExpression = function _parseUnaryExpression(scanner) {
6178
- var operator = scanner.getCurChar();
6179
- if ([
6180
- "+",
6181
- "-",
6182
- "!"
6183
- ].includes(operator)) {
6184
- var opPos = scanner.getShaderPosition(0);
6185
- scanner.advance(1);
6186
- scanner.skipSpace(false);
6187
- var parenExpr = this._parseParenthesisExpression(scanner);
6188
- if (operator === "!" && typeof parenExpr !== "boolean" || operator !== "!" && typeof parenExpr !== "number") {
6189
- this._reportError(opPos, "invalid operator.", scanner.source, scanner.file);
6190
- }
6191
- switch(operator){
6192
- case "+":
6193
- return parenExpr;
6194
- case "-":
6195
- return -parenExpr;
6196
- case "!":
6197
- return !parenExpr;
6198
- }
6199
- }
6200
- return this._parseParenthesisExpression(scanner);
6201
- };
6202
- MacroParser._parseParenthesisExpression = function _parseParenthesisExpression(scanner) {
6203
- if (scanner.getCurChar() === "(") {
6204
- scanner.advance(1);
6205
- scanner.skipSpace(false);
6206
- var ret = this._parseConstantExpression(scanner);
6207
- scanner.scanToChar(")");
6208
- scanner.advance(1);
6209
- return ret;
6210
- }
6211
- return this._parseConstant(scanner);
6212
- };
6213
- MacroParser._parseConstant = function _parseConstant(scanner) {
6214
- if (BaseLexer.isAlpha(scanner.getCurCharCode())) {
6215
- var id = scanner.scanWord();
6216
- if (id.type === MacroParserKeyword.defined) {
6217
- var withParen = scanner.peekNonSpace() === "(";
6218
- var macro = scanner.scanWord();
6219
- if (withParen) {
6220
- scanner.scanToChar(")");
6221
- scanner.advance(1);
6222
- }
6223
- return !!this._definedMacros.get(macro.lexeme);
6224
- } else {
6225
- var macro1 = this._definedMacros.get(id.lexeme);
6226
- if (!macro1) {
6227
- return false;
6228
- }
6229
- if (!macro1.body) {
6230
- return true;
6231
- }
6232
- if (macro1.isFunction) {
6233
- this._reportError(id.location, "invalid function macro usage", scanner.source, scanner.file);
6234
- }
6235
- var value = Number(macro1.body.lexeme);
6236
- if (!Number.isInteger(value)) {
6237
- this._reportError(id.location, "invalid const macro: " + id.lexeme, scanner.source, scanner.file);
6238
- }
6239
- return value;
6240
- }
6241
- } else if (BaseLexer.isDigit(scanner.getCurCharCode())) {
6242
- var integer = scanner.scanInteger();
6243
- return Number(integer.lexeme);
6244
- } else {
6245
- this._reportError(scanner.getShaderPosition(0), "invalid token: " + scanner.getCurChar(), scanner.source, scanner.file);
5724
+ /**
5725
+ * Backfill jump offset of an IF/ELIF instruction.
5726
+ * When onlyPlaceholder is true, only backfill if the current value is still -1
5727
+ */ ShaderInstructionEncoder._backfillJump = function _backfillJump(inst, target, onlyPlaceholder) {
5728
+ if (onlyPlaceholder === void 0) onlyPlaceholder = false;
5729
+ var directive = inst[0];
5730
+ if (directive === ShaderPreprocessorDirective.IfDef || directive === ShaderPreprocessorDirective.IfNdef) {
5731
+ if (!onlyPlaceholder || inst[2] === -1) inst[2] = target;
5732
+ } else if (directive === ShaderPreprocessorDirective.IfCmp) {
5733
+ if (!onlyPlaceholder || inst[4] === -1) inst[4] = target;
5734
+ } else if (directive === ShaderPreprocessorDirective.IfExpr) {
5735
+ if (!onlyPlaceholder || inst[2] === -1) inst[2] = target;
5736
+ }
5737
+ };
5738
+ ShaderInstructionEncoder._stripLineComment = function _stripLineComment(s) {
5739
+ var idx = s.indexOf("//");
5740
+ return idx >= 0 ? s.substring(0, idx).trimEnd() : s;
5741
+ };
5742
+ ShaderInstructionEncoder._parseCondition = function _parseCondition(expr) {
5743
+ var ctx = {
5744
+ s: expr.trim(),
5745
+ i: 0
5746
+ };
5747
+ return ShaderInstructionEncoder._parseOr(ctx);
5748
+ };
5749
+ ShaderInstructionEncoder._skipWs = function _skipWs(ctx) {
5750
+ while(ctx.i < ctx.s.length && (ctx.s.charCodeAt(ctx.i) === 32 /* space */ || ctx.s.charCodeAt(ctx.i) === 9))ctx.i++;
5751
+ };
5752
+ ShaderInstructionEncoder._parseOr = function _parseOr(ctx) {
5753
+ var left = ShaderInstructionEncoder._parseAnd(ctx);
5754
+ ShaderInstructionEncoder._skipWs(ctx);
5755
+ while(ctx.i < ctx.s.length - 1 && ctx.s.charCodeAt(ctx.i) === 124 /* '|' */ && ctx.s.charCodeAt(ctx.i + 1) === 124 /* '|' */ ){
5756
+ ctx.i += 2;
5757
+ ShaderInstructionEncoder._skipWs(ctx);
5758
+ left = {
5759
+ t: "or",
5760
+ l: left,
5761
+ r: ShaderInstructionEncoder._parseAnd(ctx)
5762
+ };
5763
+ ShaderInstructionEncoder._skipWs(ctx);
5764
+ }
5765
+ return left;
5766
+ };
5767
+ ShaderInstructionEncoder._parseAnd = function _parseAnd(ctx) {
5768
+ var left = ShaderInstructionEncoder._parseUnary(ctx);
5769
+ ShaderInstructionEncoder._skipWs(ctx);
5770
+ while(ctx.i < ctx.s.length - 1 && ctx.s.charCodeAt(ctx.i) === 38 /* '&' */ && ctx.s.charCodeAt(ctx.i + 1) === 38 /* '&' */ ){
5771
+ ctx.i += 2;
5772
+ ShaderInstructionEncoder._skipWs(ctx);
5773
+ left = {
5774
+ t: "and",
5775
+ l: left,
5776
+ r: ShaderInstructionEncoder._parseUnary(ctx)
5777
+ };
5778
+ ShaderInstructionEncoder._skipWs(ctx);
5779
+ }
5780
+ return left;
5781
+ };
5782
+ ShaderInstructionEncoder._parseUnary = function _parseUnary(ctx) {
5783
+ ShaderInstructionEncoder._skipWs(ctx);
5784
+ if (ctx.s.charCodeAt(ctx.i) === 33 /* '!' */ ) {
5785
+ ctx.i++;
5786
+ ShaderInstructionEncoder._skipWs(ctx);
5787
+ return {
5788
+ t: "not",
5789
+ c: ShaderInstructionEncoder._parsePrimary(ctx)
5790
+ };
6246
5791
  }
6247
- };
6248
- MacroParser._parseMacroFunctionArgs = function _parseMacroFunctionArgs(source, startIndex, macroName) {
6249
- var length = source.length;
6250
- var i = startIndex + macroName.length;
6251
- // Find opening parenthesis
6252
- while(i < length && source.charCodeAt(i) !== 40)i++;
6253
- // Parse function arguments
6254
- var args = [];
6255
- var level = 1;
6256
- var argStart = i + 1;
6257
- var k = argStart;
6258
- while(k < length && level > 0){
6259
- var charCode = source.charCodeAt(k);
6260
- if (charCode === 40) {
6261
- level++;
6262
- } else if (charCode === 41) {
6263
- if (--level === 0) {
6264
- var arg = source.substring(argStart, k).trim();
6265
- if (arg.length > 0) args.push(arg);
6266
- break;
6267
- }
6268
- } else if (charCode === 44 && level === 1) {
6269
- var arg1 = source.substring(argStart, k).trim();
6270
- if (arg1.length > 0) args.push(arg1);
6271
- argStart = k + 1;
6272
- }
6273
- k++;
5792
+ return ShaderInstructionEncoder._parsePrimary(ctx);
5793
+ };
5794
+ ShaderInstructionEncoder._parsePrimary = function _parsePrimary(ctx) {
5795
+ ShaderInstructionEncoder._skipWs(ctx);
5796
+ var s = ctx.s;
5797
+ // Parenthesized expression
5798
+ if (s.charCodeAt(ctx.i) === 40 /* '(' */ ) {
5799
+ ctx.i++;
5800
+ ShaderInstructionEncoder._skipWs(ctx);
5801
+ var inner = ShaderInstructionEncoder._parseOr(ctx);
5802
+ ShaderInstructionEncoder._skipWs(ctx);
5803
+ if (s.charCodeAt(ctx.i) === 41 /* ')' */ ) ctx.i++;
5804
+ return inner;
5805
+ }
5806
+ // defined(MACRO) or defined MACRO
5807
+ if (s.substring(ctx.i, ctx.i + 7) === "defined") {
5808
+ ctx.i += 7;
5809
+ ShaderInstructionEncoder._skipWs(ctx);
5810
+ var hasParen = s.charCodeAt(ctx.i) === 40; /* '(' */
5811
+ if (hasParen) ctx.i++;
5812
+ ShaderInstructionEncoder._skipWs(ctx);
5813
+ var name = ShaderInstructionEncoder._scanIdentifier(ctx);
5814
+ ShaderInstructionEncoder._skipWs(ctx);
5815
+ if (hasParen && s.charCodeAt(ctx.i) === 41 /* ')' */ ) ctx.i++;
5816
+ return {
5817
+ t: "def",
5818
+ m: name
5819
+ };
6274
5820
  }
6275
- return {
6276
- args: args,
6277
- endIndex: k + 1
6278
- };
6279
- };
6280
- MacroParser._expandMacroBody = function _expandMacroBody(body) {
6281
- var visitedMacros = this._expandVisitedMacros;
6282
- var currentVersionId = ++this._expandVersionId;
6283
- var expandedBody = body;
6284
- var hasExpansion = true;
6285
- while(hasExpansion){
6286
- hasExpansion = false;
6287
- var length = expandedBody.length;
6288
- var i = 0;
6289
- while(i < length){
6290
- var charCode = expandedBody.charCodeAt(i);
6291
- if (!BaseLexer.isAlpha(charCode)) {
6292
- i++;
6293
- continue;
6294
- }
6295
- var start = i;
6296
- while(i < length && BaseLexer.isAlnum(expandedBody.charCodeAt(i))){
6297
- i++;
6298
- }
6299
- var macroName = expandedBody.substring(start, i);
6300
- var macro = this._definedMacros.get(macroName);
6301
- if (!macro || visitedMacros[macroName] === currentVersionId) {
6302
- continue;
6303
- }
6304
- // Prevent circular references
6305
- visitedMacros[macroName] = currentVersionId;
6306
- var replacement = void 0;
6307
- var endIndex = void 0;
6308
- if (!macro.isFunction) {
6309
- var _macro_body;
6310
- var _macro_body_lexeme;
6311
- replacement = (_macro_body_lexeme = (_macro_body = macro.body) == null ? void 0 : _macro_body.lexeme) != null ? _macro_body_lexeme : "";
6312
- endIndex = i;
6313
- } else {
6314
- var _this__parseMacroFunctionArgs = this._parseMacroFunctionArgs(expandedBody, start, macroName), args = _this__parseMacroFunctionArgs.args, newEndIndex = _this__parseMacroFunctionArgs.endIndex;
6315
- replacement = macro.expandFunctionBody(args);
6316
- endIndex = newEndIndex;
6317
- }
6318
- expandedBody = expandedBody.substring(0, start) + replacement + expandedBody.substring(endIndex);
6319
- hasExpansion = true;
6320
- break;
5821
+ // Numeric literal
5822
+ if (ctx.i < s.length && ShaderInstructionEncoder._isDigit(s.charCodeAt(ctx.i))) {
5823
+ var lhsNum = ShaderInstructionEncoder._scanNumber(ctx);
5824
+ ShaderInstructionEncoder._skipWs(ctx);
5825
+ var op = ShaderInstructionEncoder._scanOp(ctx);
5826
+ if (op) {
5827
+ ShaderInstructionEncoder._skipWs(ctx);
5828
+ return {
5829
+ t: "bool",
5830
+ v: ShaderInstructionEncoder._evalNumOp(lhsNum, op, ShaderInstructionEncoder._scanNumber(ctx))
5831
+ };
6321
5832
  }
5833
+ return {
5834
+ t: "bool",
5835
+ v: lhsNum !== 0
5836
+ };
6322
5837
  }
6323
- return expandedBody;
6324
- };
6325
- MacroParser._expandMacroChunk = function _expandMacroChunk(chunk, loc, scannerOrFile) {
6326
- this._expandSegmentsStack.push([]);
6327
- var scanner;
6328
- if (typeof scannerOrFile === "string") {
6329
- scanner = new MacroParserLexer(chunk, scannerOrFile);
6330
- } else {
6331
- scanner = new MacroParserLexer(chunk, scannerOrFile.file, loc);
5838
+ // Identifier — comparison or defined check
5839
+ var name1 = ShaderInstructionEncoder._scanIdentifier(ctx);
5840
+ if (!name1) return {
5841
+ t: "bool",
5842
+ v: false
5843
+ };
5844
+ ShaderInstructionEncoder._skipWs(ctx);
5845
+ var op1 = ShaderInstructionEncoder._scanOp(ctx);
5846
+ if (op1) {
5847
+ ShaderInstructionEncoder._skipWs(ctx);
5848
+ return {
5849
+ t: "cmp",
5850
+ m: name1,
5851
+ op: op1,
5852
+ v: ShaderInstructionEncoder._scanNumber(ctx)
5853
+ };
6332
5854
  }
6333
- var ret = this._parseDirectives(scanner);
6334
- this._expandSegmentsStack.pop();
6335
5855
  return {
6336
- content: ret,
6337
- sourceMap: scanner.sourceMap
6338
- };
6339
- };
6340
- MacroParser._addEmptyReplace = function _addEmptyReplace(lexer, start) {
6341
- this._addContentReplace(lexer.file, ShaderLab.createPosition(start), lexer.getShaderPosition(0), "", lexer.blockRange);
6342
- };
6343
- MacroParser._addContentReplace = function _addContentReplace(sourceFile, start, end, content, sourceRange, sourceMap) {
6344
- var block = new BlockInfo(sourceFile, sourceRange, sourceMap);
6345
- var range = ShaderLab.createRange(start, end);
6346
- this._getExpandSegments().push({
6347
- block: block,
6348
- rangeInBlock: range,
6349
- replace: content
6350
- });
6351
- };
6352
- MacroParser._parseDefine = function _parseDefine(lexer) {
6353
- var start = lexer.getShaderPosition(7);
6354
- var macroName = lexer.scanWord();
6355
- var lexeme = macroName.lexeme, location = macroName.location;
6356
- var end = location.end;
6357
- if (this._definedMacros.get(lexeme) && lexeme.startsWith("GL_")) {
6358
- this._reportError(location, "Redefined macro: " + lexeme, lexer.source, lexer.file);
6359
- }
6360
- var macroArgs;
6361
- if (lexer.getCurChar() === "(") {
6362
- macroArgs = lexer.scanWordsUntilTerminator(")");
6363
- end = lexer.getShaderPosition(0);
6364
- }
6365
- var macroBody = lexer.scanMacroBody();
6366
- var range = ShaderLab.createRange(start, end);
6367
- var macroDefine = new MacroDefine(macroName, macroBody, range, macroArgs);
6368
- this._definedMacros.set(lexeme, macroDefine);
6369
- this._addContentReplace(lexer.file, start, lexer.getShaderPosition(0), "", lexer.blockRange);
6370
- };
6371
- MacroParser._parseUndef = function _parseUndef(lexer) {
6372
- var start = lexer.getShaderPosition(6);
6373
- var macroName = lexer.scanWord();
6374
- this._definedMacros.delete(macroName.lexeme);
6375
- this._addContentReplace(lexer.file, start, lexer.getShaderPosition(0), "", lexer.blockRange);
6376
- };
6377
- MacroParser._parseMacro = function _parseMacro(lexer, token) {
6378
- var macro = this._definedMacros.get(token.lexeme);
6379
- if (macro) {
6380
- var location = token.location;
6381
- if (macro.isFunction) {
6382
- var _this__parseMacroFunctionArgs = this._parseMacroFunctionArgs(lexer.source, location.start.index, token.lexeme), args = _this__parseMacroFunctionArgs.args, endIndex = _this__parseMacroFunctionArgs.endIndex;
6383
- var macroBodyExpanded = macro.expandFunctionBody(args);
6384
- var expandedContent = this._expandMacroBody(macroBodyExpanded);
6385
- var remainingLength = endIndex - location.end.index;
6386
- lexer.advance(remainingLength);
6387
- this._addContentReplace(lexer.file, location.start, lexer.getShaderPosition(0), expandedContent, lexer.blockRange);
6388
- } else {
6389
- var _macro_body;
6390
- var _macro_body_lexeme;
6391
- var macroContent = (_macro_body_lexeme = (_macro_body = macro.body) == null ? void 0 : _macro_body.lexeme) != null ? _macro_body_lexeme : "";
6392
- var expandedContent1 = this._expandMacroBody(macroContent);
6393
- this._addContentReplace(lexer.file, location.start, location.end, expandedContent1, lexer.blockRange);
6394
- }
5856
+ t: "def",
5857
+ m: name1
5858
+ };
5859
+ };
5860
+ ShaderInstructionEncoder._isDigit = function _isDigit(charCode) {
5861
+ return charCode >= 48 /* '0' */ && charCode <= 57 /* '9' */ ;
5862
+ };
5863
+ ShaderInstructionEncoder._isAlnum = function _isAlnum(charCode) {
5864
+ return charCode >= 65 /* 'A' */ && charCode <= 90 || charCode >= 97 /* 'a' */ && charCode <= 122 || charCode >= 48 /* '0' */ && charCode <= 57 || charCode === 95 /* '_' */ ;
5865
+ };
5866
+ ShaderInstructionEncoder._scanIdentifier = function _scanIdentifier(ctx) {
5867
+ var start = ctx.i;
5868
+ while(ctx.i < ctx.s.length && ShaderInstructionEncoder._isAlnum(ctx.s.charCodeAt(ctx.i)))ctx.i++;
5869
+ return ctx.s.substring(start, ctx.i);
5870
+ };
5871
+ ShaderInstructionEncoder._scanNumber = function _scanNumber(ctx) {
5872
+ var start = ctx.i;
5873
+ if (ctx.s.charCodeAt(ctx.i) === 45 /* '-' */ ) ctx.i++;
5874
+ while(ctx.i < ctx.s.length && (ShaderInstructionEncoder._isDigit(ctx.s.charCodeAt(ctx.i)) || ctx.s.charCodeAt(ctx.i) === 46))ctx.i++;
5875
+ return Number(ctx.s.substring(start, ctx.i)) || 0;
5876
+ };
5877
+ ShaderInstructionEncoder._scanOp = function _scanOp(ctx) {
5878
+ var c = ctx.s.charCodeAt(ctx.i);
5879
+ var c2 = ctx.i + 1 < ctx.s.length ? ctx.s.charCodeAt(ctx.i + 1) : 0;
5880
+ if (c === 61 /* '=' */ && c2 === 61 /* '=' */ ) {
5881
+ ctx.i += 2;
5882
+ return "==";
5883
+ }
5884
+ if (c === 33 /* '!' */ && c2 === 61 /* '=' */ ) {
5885
+ ctx.i += 2;
5886
+ return "!=";
5887
+ }
5888
+ if (c === 62 /* '>' */ && c2 === 61 /* '=' */ ) {
5889
+ ctx.i += 2;
5890
+ return ">=";
5891
+ }
5892
+ if (c === 60 /* '<' */ && c2 === 61 /* '=' */ ) {
5893
+ ctx.i += 2;
5894
+ return "<=";
5895
+ }
5896
+ if (c === 62 /* '>' */ ) {
5897
+ ctx.i++;
5898
+ return ">";
5899
+ }
5900
+ if (c === 60 /* '<' */ ) {
5901
+ ctx.i++;
5902
+ return "<";
5903
+ }
5904
+ return "";
5905
+ };
5906
+ ShaderInstructionEncoder._evalNumOp = function _evalNumOp(lhs, op, rhs) {
5907
+ switch(op){
5908
+ case "==":
5909
+ return lhs === rhs;
5910
+ case "!=":
5911
+ return lhs !== rhs;
5912
+ case ">":
5913
+ return lhs > rhs;
5914
+ case "<":
5915
+ return lhs < rhs;
5916
+ case ">=":
5917
+ return lhs >= rhs;
5918
+ case "<=":
5919
+ return lhs <= rhs;
5920
+ default:
5921
+ return false;
6395
5922
  }
6396
5923
  };
6397
- MacroParser.convertSourceIndex = function convertSourceIndex(index) {
6398
- return this.lexer.sourceMap.map(index);
6399
- };
6400
- return MacroParser;
5924
+ return ShaderInstructionEncoder;
6401
5925
  }();
6402
- MacroParser._definedMacros = new Map();
6403
- MacroParser._expandSegmentsStack = [
6404
- []
6405
- ];
6406
- MacroParser._expandVisitedMacros = {};
6407
- MacroParser._expandVersionId = 1;
6408
- MacroParser._errors = [];
5926
+ ShaderInstructionEncoder._DIRECTIVE_RE = /^[ \t]*#[ \t]*(if|ifdef|ifndef|elif|else|endif|define|undef)\b(.*)/;
5927
+ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
6409
5928
 
6410
5929
  /**
6411
5930
  * The [LALR1](https://web.stanford.edu/class/archive/cs/cs143/cs143.1128/handouts/140%20LALR%20Parsing.pdf) Parser generator
@@ -8851,14 +8370,69 @@ var ShaderLab = /*#__PURE__*/ function() {
8851
8370
  engine.Logger.info("[Task - Total compilation] cost time: " + (performance.now() - totalStartTime) + "ms");
8852
8371
  ShaderLab._processingPassText = undefined;
8853
8372
  this._logErrors(codeGen.errors);
8373
+ if (ret) {
8374
+ // Always parse instructions for the compiled GLSL
8375
+ ret.vertexShaderInstructions = ShaderInstructionEncoder.parse(ret.vertex);
8376
+ ret.fragmentShaderInstructions = ShaderInstructionEncoder.parse(ret.fragment);
8377
+ }
8854
8378
  return ret;
8855
8379
  };
8856
- _proto._parseMacros = function _parseMacros(content, macros) {
8857
- var startTime = performance.now();
8858
- var parsedContent = MacroParser.parse(content, macros);
8859
- engine.Logger.info("[Task - parse macros] cost time: " + (performance.now() - startTime) + "ms");
8860
- this._logErrors(MacroParser._errors);
8861
- return parsedContent;
8380
+ _proto._precompile = function _precompile(sourceCode, platformTarget, basePath) {
8381
+ var _this = this;
8382
+ var shaderSource = this._parseShaderSource(sourceCode);
8383
+ var subShaders = shaderSource.subShaders.map(function(sub) {
8384
+ return {
8385
+ name: sub.name,
8386
+ tags: sub.tags,
8387
+ passes: sub.passes.map(function(pass) {
8388
+ if (pass.isUsePass) {
8389
+ return {
8390
+ name: pass.name,
8391
+ isUsePass: true,
8392
+ tags: pass.tags,
8393
+ renderStates: _this._serializeRenderStates(pass.renderStates)
8394
+ };
8395
+ }
8396
+ var programSource = _this._parseShaderPass(pass.contents, pass.vertexEntry, pass.fragmentEntry, platformTarget, basePath);
8397
+ if (!programSource) {
8398
+ throw new Error('Shader pass "' + shaderSource.name + "." + sub.name + "." + pass.name + '" precompile failed, please check the shader source code.');
8399
+ }
8400
+ return {
8401
+ name: pass.name,
8402
+ isUsePass: false,
8403
+ tags: pass.tags,
8404
+ renderStates: _this._serializeRenderStates(pass.renderStates),
8405
+ vertexShaderInstructions: programSource.vertexShaderInstructions,
8406
+ fragmentShaderInstructions: programSource.fragmentShaderInstructions
8407
+ };
8408
+ })
8409
+ };
8410
+ });
8411
+ return {
8412
+ name: shaderSource.name,
8413
+ platformTarget: platformTarget,
8414
+ subShaders: subShaders
8415
+ };
8416
+ };
8417
+ _proto._serializeRenderStates = function _serializeRenderStates(renderStates) {
8418
+ var constantMap = {};
8419
+ for(var key in renderStates.constantMap){
8420
+ var value = renderStates.constantMap[key];
8421
+ if (_instanceof(value, engine.Color)) {
8422
+ constantMap[key] = [
8423
+ value.r,
8424
+ value.g,
8425
+ value.b,
8426
+ value.a
8427
+ ];
8428
+ } else {
8429
+ constantMap[key] = value;
8430
+ }
8431
+ }
8432
+ return {
8433
+ constantMap: constantMap,
8434
+ variableMap: renderStates.variableMap
8435
+ };
8862
8436
  };
8863
8437
  /**
8864
8438
  * @internal
@@ -8887,7 +8461,7 @@ ShaderLab._shaderPositionPool = ShaderLabUtils.createObjectPool(ShaderPosition);
8887
8461
  ShaderLab._shaderRangePool = ShaderLabUtils.createObjectPool(ShaderRange);
8888
8462
 
8889
8463
  //@ts-ignore
8890
- var version = "2.0.0-alpha.24";
8464
+ var version = "2.0.0-alpha.26";
8891
8465
  var mode = "Release";
8892
8466
  mode = "Verbose";
8893
8467
  console.log("Galacean Engine ShaderLab Version: " + version + " | Mode: " + mode);