@galacean/engine-shader-compiler 2.0.0-alpha.54 → 2.0.0-alpha.56

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.
package/dist/main.js CHANGED
@@ -155,8 +155,69 @@ function _create_class(Constructor, protoProps, staticProps) {
155
155
  return Constructor;
156
156
  }
157
157
 
158
+ /**
159
+ * Words that ShaderLab accepts as identifiers but GLSL ES 1.00 or 3.00 reserves. GLES100 output also runs as
160
+ * GLSL ES 3.00 on WebGL2, so both versions apply to every target.
161
+ */ var reservedWords = new Set(("asm attribute active atomic_uint case cast centroid class coherent common default double dvec2 dvec3 dvec4 " + "enum extern external filter fixed flat fvec2 fvec3 fvec4 goto half hvec2 hvec3 hvec4 iimage1D iimage1DArray " + "iimage2D iimage2DArray iimage3D iimageBuffer iimageCube image1D image1DArray image2D image2DArray image3D " + "imageBuffer imageCube inline input interface isampler1D isampler1DArray isampler2D isampler2DArray " + "isampler2DMS isampler2DMSArray isampler2DRect isampler3D isamplerBuffer isamplerCube layout long mat2x2 " + "mat2x3 mat2x4 mat3x2 mat3x3 mat3x4 mat4x2 mat4x3 mat4x4 namespace noinline noperspective output packed " + "partition patch public readonly resource restrict sample sampler1D sampler1DArray sampler1DArrayShadow " + "sampler1DShadow sampler2DArray sampler2DArrayShadow sampler2DMS sampler2DMSArray sampler2DRect " + "sampler2DRectShadow sampler2DShadow sampler3D sampler3DRect samplerBuffer samplerCubeShadow short sizeof " + "smooth static subroutine superp switch template this typedef uimage1D uimage1DArray uimage2D uimage2DArray " + "uimage3D uimageBuffer uimageCube uint union unsigned usampler1D usampler1DArray usampler2D usampler2DArray " + "usampler2DMS usampler2DMSArray usampler2DRect usampler3D usamplerBuffer usamplerCube using uvec2 uvec3 uvec4 " + "varying volatile writeonly").split(" "));
162
+ /**
163
+ * Tests whether GLSL ES rejects an identifier that ShaderLab accepts.
164
+ * @param name - Source identifier
165
+ * @returns Whether the identifier uses a reserved word or namespace
166
+ * @internal
167
+ */ function isUnsafeGLSLIdentifier(name) {
168
+ return reservedWords.has(name) || name.startsWith("gl_") || name.includes("__");
169
+ }
170
+ /**
171
+ * Allocates generated identifiers that avoid source identifiers and each other.
172
+ * @internal
173
+ */ var NameAllocator = /*#__PURE__*/ function() {
174
+ function NameAllocator() {
175
+ this._used = new Set();
176
+ this._names = new Map();
177
+ }
178
+ var _proto = NameAllocator.prototype;
179
+ /**
180
+ * Reserves a source identifier so that generated names never shadow it.
181
+ * @param name - Source identifier
182
+ */ _proto.reserve = function reserve(name) {
183
+ this._used.add(name);
184
+ };
185
+ /**
186
+ * Keeps a source identifier when GLSL accepts it, otherwise assigns a legal name to its identity.
187
+ * @param key - Source declaration or namespace identity
188
+ * @param name - Source identifier
189
+ * @returns Identifier used by the declaration and its references
190
+ */ _proto.sourceName = function sourceName(key, name) {
191
+ if (!isUnsafeGLSLIdentifier(name)) return name;
192
+ return this.allocate(key, name.startsWith("gl_") ? "user_" + name : "" + name + "_");
193
+ };
194
+ /**
195
+ * Returns the same identifier for every request with the same key.
196
+ * @param key - Identity of the generated declaration, independent of its spelling
197
+ * @param preferred - Readable base name. Repeated underscores collapse to one, because WebGL rejects `__`
198
+ * @returns An identifier that differs from every reserved and previously allocated name
199
+ */ _proto.allocate = function allocate(key, preferred) {
200
+ var name = this._names.get(key);
201
+ if (name === undefined) {
202
+ var base = preferred.replace(/_{2,}/g, "_");
203
+ var separator = base.endsWith("_") ? "" : "_";
204
+ name = base;
205
+ for(var suffix = 1; this._used.has(name); suffix++)name = "" + base + separator + suffix;
206
+ this._used.add(name);
207
+ this._names.set(key, name);
208
+ }
209
+ return name;
210
+ };
211
+ /** Releases all reservations and allocations between compilation requests. */ _proto.reset = function reset() {
212
+ this._used.clear();
213
+ this._names.clear();
214
+ };
215
+ return NameAllocator;
216
+ }();
217
+
158
218
  /** @internal */ var VisitorContext = /*#__PURE__*/ function() {
159
219
  function VisitorContext() {
220
+ this.names = new NameAllocator();
160
221
  this._lookupSymbol = new internal.SymbolInfo("", null);
161
222
  this._structRoles = new Map();
162
223
  this._vertexUnresolvedVariableRoles = new Map();
@@ -173,16 +234,12 @@ function _create_class(Constructor, protoProps, staticProps) {
173
234
  this._referencedGlobalMacroASTs = [];
174
235
  this._structVariableRoles = new Map();
175
236
  this.codeCache = new Map();
176
- this.fragmentReturnModes = new Map();
177
- this.terminalInterfaceReturns = new Set();
178
237
  this.reset();
179
238
  }
180
239
  var _proto = VisitorContext.prototype;
181
240
  _proto.reset = function reset(resetAll) {
182
241
  if (resetAll === void 0) resetAll = true;
183
242
  this.codeCache.clear();
184
- this.fragmentReturnModes.clear();
185
- this.terminalInterfaceReturns.clear();
186
243
  if (resetAll) {
187
244
  this.attributeStructs.length = 0;
188
245
  this.attributeList.length = 0;
@@ -286,30 +343,6 @@ function _create_class(Constructor, protoProps, staticProps) {
286
343
  }
287
344
  return resolvedRole;
288
345
  };
289
- /**
290
- * Marks a value-return belonging to a fragment entry for backend lowering.
291
- * @param statement - Exact return statement identity.
292
- * @param mode - Fragment output contract of the containing entry declaration.
293
- */ _proto.registerFragmentReturn = function registerFragmentReturn(statement, mode) {
294
- this.fragmentReturnModes.set(statement, mode);
295
- };
296
- /**
297
- * Finds the output contract for a fragment-entry return statement.
298
- * @param statement - Return statement identity.
299
- * @returns Output mode, or `undefined` for ordinary helper/vertex returns.
300
- */ _proto.getFragmentReturnMode = function getFragmentReturnMode(statement) {
301
- return this.fragmentReturnModes.get(statement);
302
- };
303
- /** Marks a syntactically final interface return whose control-flow exit can be omitted. */ _proto.registerTerminalInterfaceReturn = function registerTerminalInterfaceReturn(statement) {
304
- this.terminalInterfaceReturns.add(statement);
305
- };
306
- /**
307
- * Tests whether an interface return is the final syntactic statement in its function.
308
- * @param statement - Return statement identity.
309
- * @returns Whether backend lowering may omit a trailing `return;`.
310
- */ _proto.isTerminalInterfaceReturn = function isTerminalInterfaceReturn(statement) {
311
- return this.terminalInterfaceReturns.has(statement);
312
- };
313
346
  _proto.referenceAttribute = function referenceAttribute(ident) {
314
347
  this._referenceProp(ident.lexeme, this.attributeList, this._referencedAttributeList);
315
348
  };
@@ -386,7 +419,7 @@ var TempArray = /*#__PURE__*/ function() {
386
419
  var child = _step.value;
387
420
  if (_instanceof(child, internal.BaseToken)) {
388
421
  // Legacy opaque `#define` lexemes carry the directive verbatim; expression-style defines use the AST path
389
- ret.array.push(child.lexeme);
422
+ ret.array.push(this.tokenLexeme(child));
390
423
  } else {
391
424
  ret.array.push(child.codeGen(this));
392
425
  }
@@ -396,6 +429,16 @@ var TempArray = /*#__PURE__*/ function() {
396
429
  pool.return(ret);
397
430
  return result;
398
431
  };
432
+ /**
433
+ * Spells a source token in the generated code.
434
+ * @param token - Source token
435
+ * @returns Generated text for the token
436
+ */ _proto.tokenLexeme = function tokenLexeme(token) {
437
+ return token.lexeme;
438
+ };
439
+ _proto.interfaceMemberName = function interfaceMemberName(name) {
440
+ return name;
441
+ };
399
442
  _proto.visitPostfixExpression = function visitPostfixExpression(node) {
400
443
  var children = node.children;
401
444
  var derivationLength = children.length;
@@ -416,13 +459,13 @@ var TempArray = /*#__PURE__*/ function() {
416
459
  var unresolvedName = internal.ParserUtils.extractDirectIdentLexeme(postExpr);
417
460
  role = symbols.length ? context.getStructVarRole(symbols) : unresolvedName ? context.getUnresolvedStructVarRole(unresolvedName) : undefined;
418
461
  }
462
+ if (!directRoot) role = context.getStructRole(node.resolvedMemberOwners);
419
463
  if (role) {
420
464
  if (role === internal.ShaderStructRole.Attribute) context.referenceAttribute(prop);
421
465
  else if (role === internal.ShaderStructRole.Varying) context.referenceVarying(prop);
422
466
  else context.referenceMRTProp(prop);
423
- return prop.lexeme;
424
467
  }
425
- return postExpr.codeGen(this) + "." + prop.lexeme;
468
+ return postExpr.codeGen(this) + "." + (role ? this.interfaceMemberName(prop.lexeme) : prop.lexeme);
426
469
  } else {
427
470
  return postExpr.codeGen(this) + "." + prop.codeGen(this);
428
471
  }
@@ -457,35 +500,6 @@ var TempArray = /*#__PURE__*/ function() {
457
500
  var symbol = _step.value;
458
501
  this.referenceFunction(symbol, node.location.start.index, node._branch);
459
502
  }
460
- var paramList = call.children[2];
461
- if (_instanceof(paramList, internal.ASTNode.FunctionCallParameterList)) {
462
- var astNodes = paramList.paramNodes;
463
- var finalSymbol = this.context._referencedGlobals[call.fnSymbol.ident].find(function(candidate) {
464
- return _instanceof(candidate, internal.FnSymbol) && candidate.equal(call.fnSymbol);
465
- });
466
- var paramInfoList = (finalSymbol != null ? finalSymbol : call.fnSymbol).astNode.protoType.parameterList;
467
- var context = this.context;
468
- var params = astNodes.filter(function(_, i) {
469
- var _paramInfoList_i;
470
- var parameter = paramInfoList == null ? void 0 : (_paramInfoList_i = paramInfoList[i]) == null ? void 0 : _paramInfoList_i.astNode;
471
- // Drop struct-IO parameters (attribute/varying/mrt) — they're flattened
472
- // into top-level declarations and no longer exist as function-call args.
473
- return !_instanceof(parameter, internal.ASTNode.ParameterDeclaration) || !parameter.symbol || !context.getStructVarRole([
474
- parameter.symbol
475
- ]);
476
- });
477
- var paramsCode = "";
478
- for(var i = 0, length = params.length; i < length; i++){
479
- var astNode = params[i];
480
- var code = astNode.codeGen(this);
481
- if (_instanceof(astNode, internal.ASTNode.MacroCallArgBlock) || i === 0) {
482
- paramsCode += code;
483
- } else {
484
- paramsCode += ", " + code;
485
- }
486
- }
487
- return call.fnSymbol.ident + "(" + paramsCode + ")";
488
- }
489
503
  }
490
504
  return this.defaultCodeGen(node.children);
491
505
  };
@@ -494,25 +508,7 @@ var TempArray = /*#__PURE__*/ function() {
494
508
  var paramList = children[2];
495
509
  if (_instanceof(paramList, internal.ASTNode.FunctionCallParameterList)) {
496
510
  var astNodes = paramList.paramNodes;
497
- var context = this.context;
498
- // Drop bare IO-struct args only when the macro aliases a user fn (whose
499
- // formal was flattened). All other shapes preserve args verbatim.
500
- var params;
501
- if (node.isFunctionLikeMacro || !node.aliasesNonBuiltinIdent) {
502
- params = astNodes;
503
- } else {
504
- params = astNodes.filter(function(arg) {
505
- if (_instanceof(arg, internal.ASTNode.AssignmentExpression)) {
506
- var variableParam = internal.ParserUtils.unwrapBareIdentifier(arg, {
507
- allowParens: false
508
- });
509
- if (variableParam && context.getStructVarRole(variableParam.resolvedValueSymbols())) {
510
- return false;
511
- }
512
- }
513
- return true;
514
- });
515
- }
511
+ var params = astNodes;
516
512
  var paramsCode = "";
517
513
  for(var i = 0, length = params.length; i < length; i++){
518
514
  var argNode = params[i];
@@ -538,8 +534,7 @@ var TempArray = /*#__PURE__*/ function() {
538
534
  };
539
535
  /**
540
536
  * Code-generate a `#define` macro parsed as expression AST. Produces a `#define`
541
- * directive whose value is the AST-rewritten string — so varying flattening
542
- * happens naturally via `visitPostfixExpression` inside the AST walk.
537
+ * directive whose value retains ordinary expression and member-access semantics.
543
538
  */ _proto.visitMacroDefine = function visitMacroDefine(node) {
544
539
  // For function-like macros, preserve the original `(params)` lexeme verbatim —
545
540
  // the parameter list is user-authored text that shouldn't be canonicalized.
@@ -561,79 +556,27 @@ var TempArray = /*#__PURE__*/ function() {
561
556
  var fullType = children[0];
562
557
  if (_instanceof(fullType, internal.ASTNode.FullySpecifiedType) && fullType.typeSpecifier.isCustom) {
563
558
  var context = this.context;
564
- // Global interface variables are flattened and therefore are not emitted as uniforms.
559
+ // Global interface variables are mutable shader values, not material bindings.
565
560
  if (context.getStructVarRole([
566
561
  node.declarator.symbol
567
562
  ])) {
568
- return "";
563
+ return this.defaultCodeGen(children);
569
564
  }
570
565
  context.referenceGlobal(fullType.type, internal.ESymbolType.STRUCT);
571
566
  }
572
567
  return "uniform " + this.defaultCodeGen(children);
573
568
  };
574
569
  _proto.visitDeclaration = function visitDeclaration(node) {
575
- var context = this.context;
576
- var children = node.children;
577
- var child = children[0];
578
- if (_instanceof(child, internal.ASTNode.InitDeclaratorList)) {
579
- var first = child.children[0];
580
- var _child_declarator;
581
- var declarator = (_child_declarator = child.declarator) != null ? _child_declarator : _instanceof(first, internal.ASTNode.SingleDeclaration) ? first.declarator : undefined;
582
- var role = declarator && context.getStructVarRole([
583
- declarator.symbol
584
- ]);
585
- if (role === internal.ShaderStructRole.Varying || role === internal.ShaderStructRole.Mrt) return "";
586
- }
587
- return this.defaultCodeGen(children);
570
+ return this.defaultCodeGen(node.children);
588
571
  };
589
572
  _proto.visitFunctionParameterList = function visitFunctionParameterList(node) {
590
- var context = this.context;
591
- var params = node.parameterInfoList.filter(function(item) {
592
- var parameter = item.astNode;
593
- return !_instanceof(parameter, internal.ASTNode.ParameterDeclaration) || !parameter.symbol || !context.getStructVarRole([
594
- parameter.symbol
595
- ]);
596
- });
597
- var out = "";
598
- for(var i = 0, length = params.length; i < length; i++){
599
- var item = params[i];
600
- var astNode = item.astNode;
601
- var code = astNode.codeGen(this);
602
- if (_instanceof(astNode, internal.ASTNode.MacroParamBlock) || i === 0) {
603
- out += code;
604
- } else {
605
- out += ", " + code;
606
- }
607
- }
608
- return out;
573
+ return this.defaultCodeGen(node.children);
609
574
  };
610
575
  _proto.visitFunctionHeader = function visitFunctionHeader(node) {
611
- var returnType = node.returnType.typeSpecifier;
612
- if (this.context.getStructRole(returnType.structDeclarations) === internal.ShaderStructRole.Varying) {
613
- return "void " + node.ident.lexeme + "(";
614
- }
615
576
  return this.defaultCodeGen(node.children);
616
577
  };
617
578
  _proto.visitJumpStatement = function visitJumpStatement(node) {
618
- var children = node.children;
619
- var cmd = children[0];
620
- if (cmd.type === internal.Keyword.RETURN) {
621
- var expr = children[1];
622
- if (_instanceof(expr, internal.ASTNode.Expression)) {
623
- var returnVar = internal.ParserUtils.unwrapNodeByType(expr, internal.NoneTerminal.variable_identifier);
624
- if (returnVar && this.context.getStructVarRole(returnVar.resolvedValueSymbols()) === internal.ShaderStructRole.Varying) {
625
- return this.context.isTerminalInterfaceReturn(node) ? "" : "return;";
626
- }
627
- var returnFnCall = internal.ParserUtils.unwrapNodeByType(expr, internal.NoneTerminal.function_call);
628
- var calledFunction = returnFnCall == null ? void 0 : returnFnCall.children[0];
629
- var returnDeclarations = _instanceof(calledFunction == null ? void 0 : calledFunction.fnSymbol, internal.FnSymbol) ? calledFunction.fnSymbol.astNode.protoType.returnType.typeSpecifier.structDeclarations : [];
630
- if (this.context.getStructRole(returnDeclarations) === internal.ShaderStructRole.Varying) {
631
- var call = "" + expr.codeGen(this) + ";";
632
- return this.context.isTerminalInterfaceReturn(node) ? call : "{ " + call + " return; }";
633
- }
634
- }
635
- }
636
- return this.defaultCodeGen(children);
579
+ return this.defaultCodeGen(node.children);
637
580
  };
638
581
  _proto.visitFunctionIdentifier = function visitFunctionIdentifier(node) {
639
582
  return this.defaultCodeGen(node.children);
@@ -642,63 +585,10 @@ var TempArray = /*#__PURE__*/ function() {
642
585
  return this.defaultCodeGen(node.children);
643
586
  };
644
587
  _proto.visitStructSpecifier = function visitStructSpecifier(node) {
645
- var _this = this;
646
- var context = this.context;
647
- var varyingStructs = context.varyingStructs, attributeStructs = context.attributeStructs, mrtStructs = context.mrtStructs;
648
- var isVaryingStruct = varyingStructs.indexOf(node) !== -1;
649
- var isAttributeStruct = attributeStructs.indexOf(node) !== -1;
650
- var isMRTStruct = mrtStructs.indexOf(node) !== -1;
651
- if (isVaryingStruct || isAttributeStruct || isMRTStruct) {
652
- var _result;
653
- var result = [];
654
- (_result = result).push.apply(_result, [].concat(node.macroExpressions.map(function(item) {
655
- return {
656
- text: _instanceof(item, internal.BaseToken) ? item.lexeme : item.codeGen(_this),
657
- index: item.location.start.index
658
- };
659
- })));
660
- for(var _iterator = _create_for_of_iterator_helper_loose(node.propList), _step; !(_step = _iterator()).done;){
661
- var prop = _step.value;
662
- var _context__referencedVaryingList_name, _context__referencedAttributeList_name, _context__referencedMRTList_name;
663
- var name = prop.ident.lexeme;
664
- if (isVaryingStruct && ((_context__referencedVaryingList_name = context._referencedVaryingList[name]) == null ? void 0 : _context__referencedVaryingList_name.indexOf(prop)) >= 0) {
665
- result.push({
666
- text: "" + this.getVaryingProp(prop) + "\n",
667
- index: prop.ident.location.start.index
668
- });
669
- } else if (isAttributeStruct && ((_context__referencedAttributeList_name = context._referencedAttributeList[name]) == null ? void 0 : _context__referencedAttributeList_name.indexOf(prop)) >= 0) {
670
- result.push({
671
- text: "" + this.getAttributeProp(prop) + "\n",
672
- index: prop.ident.location.start.index
673
- });
674
- } else if (isMRTStruct && ((_context__referencedMRTList_name = context._referencedMRTList[name]) == null ? void 0 : _context__referencedMRTList_name.indexOf(prop)) >= 0) {
675
- result.push({
676
- text: "" + this.getMRTProp(prop) + "\n",
677
- index: prop.ident.location.start.index
678
- });
679
- }
680
- }
681
- var text = result.sort(function(a, b) {
682
- return a.index - b.index;
683
- }).map(function(item) {
684
- return item.text;
685
- }).join("");
686
- return text;
687
- } else {
688
- return this.defaultCodeGen(node.children);
689
- }
588
+ return this.defaultCodeGen(node.children);
690
589
  };
691
590
  _proto.visitFunctionDefinition = function visitFunctionDefinition(fnNode) {
692
- var fnName = fnNode.protoType.ident.lexeme;
693
- var context = this.context;
694
- var terminal = internal.ParserUtils.lastStatement(fnNode.statements);
695
- if (_instanceof(terminal, internal.ASTNode.JumpStatement)) context.registerTerminalInterfaceReturn(terminal);
696
- if (fnName == context.stageEntry) {
697
- var statements = fnNode.statements.codeGen(this);
698
- return "void main() " + statements;
699
- } else {
700
- return this.defaultCodeGen(fnNode.children);
701
- }
591
+ return this.defaultCodeGen(fnNode.children);
702
592
  };
703
593
  return CodeGenVisitor;
704
594
  }();
@@ -1067,7 +957,7 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1067
957
  _inherits(GLESVisitor, CodeGenVisitor);
1068
958
  function GLESVisitor() {
1069
959
  var _this;
1070
- _this = CodeGenVisitor.apply(this, arguments) || this, _this._globalCodeArray = [], _this._forwardFunctionDeclarations = new Map(), _this._forwardStructIndices = new Map(), _this._structCodeSegments = new Map(), _this._forwardVariableIndices = new Map(), _this._variableCodeSegments = new Map(), _this._declarationOwners = new Map(), _this._deferredPrototypes = new Map(), _this._declarationReferences = new Map(), _this._outerMacroDeclarations = [];
960
+ _this = CodeGenVisitor.apply(this, arguments) || this, _this.supportsArrayStructReturn = true, /** Names of pass-scope declarations, which stage-interface globals must not redeclare. */ _this._globalNames = new Set(), /** Interface type and variable tokens whose source names GLSL ES rejects. */ _this._escapedDeclarations = new Map(), _this._fragmentVaryingProps = new Set(), _this._globalInputs = new Map(), _this._inputHelperBodies = new Map(), _this._inputHelperGroups = new Map(), _this._globalCodeArray = [], _this._forwardFunctionDeclarations = new Map(), _this._forwardStructIndices = new Map(), _this._structCodeSegments = new Map(), _this._forwardVariableIndices = new Map(), _this._variableCodeSegments = new Map(), _this._declarationOwners = new Map(), _this._deferredPrototypes = new Map(), _this._declarationReferences = new Map(), _this._outerMacroDeclarations = [];
1071
961
  return _this;
1072
962
  }
1073
963
  var _proto = GLESVisitor.prototype;
@@ -1083,6 +973,8 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1083
973
  this._variableCodeSegments.clear();
1084
974
  this._deferredPrototypes.clear();
1085
975
  this._declarationReferences.clear();
976
+ this._inputHelperBodies.clear();
977
+ this._inputHelperGroups.clear();
1086
978
  this._currentDeclaration = undefined;
1087
979
  };
1088
980
  /**
@@ -1090,6 +982,10 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1090
982
  * @internal
1091
983
  */ _proto.dispose = function dispose() {
1092
984
  this.context.reset();
985
+ this.context.names.reset();
986
+ this._globalNames.clear();
987
+ this._escapedDeclarations.clear();
988
+ this._vertexOutputNames = undefined;
1093
989
  this.reset();
1094
990
  };
1095
991
  /**
@@ -1097,8 +993,15 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1097
993
  * @param data - Parser-owned shader facts.
1098
994
  * @param out - Destination code segments.
1099
995
  */ _proto.getOtherGlobal = function getOtherGlobal(data, out) {
1100
- for(var _iterator = _create_for_of_iterator_helper_loose(data.globalPrecisions), _step; !(_step = _iterator()).done;){
1101
- var precision = _step.value;
996
+ for(var _iterator = _create_for_of_iterator_helper_loose(this._inputHelperGroups.values()), _step; !(_step = _iterator()).done;){
997
+ var _step_value = _step.value, name = _step_value.name, fallback = _step_value.fallback;
998
+ out.push({
999
+ text: ShaderInstructionEncoder.declaration("void " + name + "() {}", fallback) + ShaderInstructionEncoder.reference(0, fallback.id),
1000
+ index: -1
1001
+ });
1002
+ }
1003
+ for(var _iterator1 = _create_for_of_iterator_helper_loose(data.globalPrecisions), _step1; !(_step1 = _iterator1()).done;){
1004
+ var precision = _step1.value;
1102
1005
  out.push({
1103
1006
  text: precision.codeGen(this),
1104
1007
  index: precision.location.start.index
@@ -1111,14 +1014,39 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1111
1014
  * @param coreInfo - Entry and stage-interface facts derived from the same IR.
1112
1015
  * @returns Generated vertex and fragment source.
1113
1016
  */ _proto.generate = function generate(ir, coreInfo) {
1114
- var _this = this;
1017
+ var _this, _loop = function(_i, _iter) {
1018
+ var _iter__i = _iter[_i], stage = _iter__i[0], variables = _iter__i[1];
1019
+ var inputRole = stage === internal.EShaderStage.VERTEX ? internal.ShaderStructRole.Attribute : internal.ShaderStructRole.Varying;
1020
+ _this._globalInputs.set(stage, Array.from(variables).filter(function(param) {
1021
+ var variable = param[0], role = param[1];
1022
+ return role === inputRole && _instanceof(variable.astNode, internal.ASTNode.VariableDeclaration);
1023
+ }).map(function(param) {
1024
+ var variable = param[0];
1025
+ return variable;
1026
+ }));
1027
+ };
1028
+ var _this1 = this;
1115
1029
  var _context_attributeStructs, _context_attributeList, _context_varyingStructs, _context_varyingList, _context_mrtStructs, _context_mrtList;
1116
1030
  this.context.reset();
1031
+ this.context.names.reset();
1032
+ this._globalNames.clear();
1033
+ this._escapedDeclarations.clear();
1034
+ this._vertexOutputNames = undefined;
1117
1035
  this.reset();
1118
1036
  var node = ir.program;
1119
1037
  var shaderData = node.shaderData;
1120
1038
  var context = this.context;
1121
1039
  context._passSymbolTable = shaderData.symbolTable;
1040
+ var hasUnsafeIdentifier = false;
1041
+ var _ir_source_match;
1042
+ for(var _iterator = _create_for_of_iterator_helper_loose((_ir_source_match = ir.source.match(/[A-Za-z_][A-Za-z0-9_]*/g)) != null ? _ir_source_match : []), _step; !(_step = _iterator()).done;){
1043
+ var name = _step.value;
1044
+ context.names.reserve(name);
1045
+ hasUnsafeIdentifier || (hasUnsafeIdentifier = isUnsafeGLSLIdentifier(name));
1046
+ }
1047
+ shaderData.symbolTable.forEach(function(symbol) {
1048
+ return _this1._globalNames.add(symbol.ident);
1049
+ });
1122
1050
  var outerGlobalMacroDeclarations = coreInfo.outerGlobalMacroDeclarations;
1123
1051
  var io = coreInfo.io;
1124
1052
  (_context_attributeStructs = context.attributeStructs).push.apply(_context_attributeStructs, [].concat(io.attributeStructs));
@@ -1139,23 +1067,50 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1139
1067
  io.fragmentStructVariableRoles.forEach(function(role, variable) {
1140
1068
  context.registerStructVar(variable, role, internal.EShaderStage.FRAGMENT);
1141
1069
  });
1070
+ if (hasUnsafeIdentifier) this._collectEscapedDeclarations(node);
1071
+ this._globalInputs.clear();
1072
+ for(var _i = 0, _iter = [
1073
+ [
1074
+ internal.EShaderStage.VERTEX,
1075
+ io.vertexStructVariableRoles
1076
+ ],
1077
+ [
1078
+ internal.EShaderStage.FRAGMENT,
1079
+ io.fragmentStructVariableRoles
1080
+ ]
1081
+ ]; _i < _iter.length; _i++)_this = this, _loop(_i, _iter);
1142
1082
  this._sourceIR = ir;
1143
1083
  this._outerMacroDeclarations = outerGlobalMacroDeclarations;
1144
1084
  try {
1145
1085
  coreInfo.deferredDeclarationOwnership.forEach(function(owner, symbol) {
1146
- return _this._declarationOwners.set(symbol.astNode, owner);
1086
+ return _this1._declarationOwners.set(symbol.astNode, owner);
1147
1087
  });
1088
+ var nextInputOwner = 0;
1089
+ for(var _iterator1 = _create_for_of_iterator_helper_loose(this._declarationOwners.values()), _step1; !(_step1 = _iterator1()).done;){
1090
+ var owner = _step1.value;
1091
+ nextInputOwner = Math.max(nextInputOwner, owner.id, owner.group);
1092
+ }
1093
+ for(var _iterator2 = _create_for_of_iterator_helper_loose(this._globalInputs.values()), _step2; !(_step2 = _iterator2()).done;){
1094
+ var variables = _step2.value;
1095
+ for(var _iterator3 = _create_for_of_iterator_helper_loose(variables), _step3; !(_step3 = _iterator3()).done;){
1096
+ var variable = _step3.value;
1097
+ if (this._declarationOwners.has(variable.astNode)) continue;
1098
+ var id = ++nextInputOwner;
1099
+ this._declarationOwners.set(variable.astNode, {
1100
+ id: id,
1101
+ group: id,
1102
+ sourceScope: variable.sourceScope
1103
+ });
1104
+ }
1105
+ }
1148
1106
  if (this._declarationOwners.size) {
1149
- var nextId = 0;
1150
- var nextGroup = 0;
1151
- this._declarationOwners.forEach(function(owner) {
1152
- nextId = Math.max(nextId, owner.id);
1153
- nextGroup = Math.max(nextGroup, owner.group);
1154
- });
1107
+ // `nextInputOwner` already exceeds every owner id and group.
1108
+ var nextId = nextInputOwner;
1109
+ var nextGroup = nextInputOwner;
1155
1110
  // Reachability follows the selected declaration, including dependencies without an override.
1156
1111
  shaderData.symbolTable.forEach(function(symbol) {
1157
- if (symbol.astNode && !_this._declarationOwners.has(symbol.astNode)) {
1158
- _this._declarationOwners.set(symbol.astNode, {
1112
+ if (symbol.astNode && !_this1._declarationOwners.has(symbol.astNode)) {
1113
+ _this1._declarationOwners.set(symbol.astNode, {
1159
1114
  id: ++nextId,
1160
1115
  group: ++nextGroup,
1161
1116
  sourceScope: symbol.sourceScope
@@ -1165,54 +1120,71 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1165
1120
  context.onReferenceGlobal = function(symbols) {
1166
1121
  var _this_currentDeclarationOwner;
1167
1122
  var _this_currentDeclarationOwner_id;
1168
- var from = (_this_currentDeclarationOwner_id = (_this_currentDeclarationOwner = _this.currentDeclarationOwner) == null ? void 0 : _this_currentDeclarationOwner.id) != null ? _this_currentDeclarationOwner_id : 0;
1123
+ var from = (_this_currentDeclarationOwner_id = (_this_currentDeclarationOwner = _this1.currentDeclarationOwner) == null ? void 0 : _this_currentDeclarationOwner.id) != null ? _this_currentDeclarationOwner_id : 0;
1169
1124
  for(var _iterator = _create_for_of_iterator_helper_loose(symbols), _step; !(_step = _iterator()).done;){
1170
1125
  var symbol = _step.value;
1171
- _this._referenceDeclaration(from, symbol.astNode);
1126
+ _this1._referenceDeclaration(from, symbol.astNode);
1172
1127
  }
1173
1128
  };
1174
1129
  var propDeclarations = new Map();
1175
- for(var _iterator = _create_for_of_iterator_helper_loose([].concat(io.attributeStructs, io.varyingStructs, io.mrtStructs)), _step; !(_step = _iterator()).done;){
1176
- var struct = _step.value;
1177
- for(var _iterator1 = _create_for_of_iterator_helper_loose(struct.propList), _step1; !(_step1 = _iterator1()).done;){
1178
- var prop = _step1.value;
1130
+ for(var _iterator4 = _create_for_of_iterator_helper_loose([].concat(io.attributeStructs, io.varyingStructs, io.mrtStructs)), _step4; !(_step4 = _iterator4()).done;){
1131
+ var struct = _step4.value;
1132
+ for(var _iterator5 = _create_for_of_iterator_helper_loose(struct.propList), _step5; !(_step5 = _iterator5()).done;){
1133
+ var prop = _step5.value;
1179
1134
  propDeclarations.set(prop, struct);
1180
1135
  }
1181
1136
  }
1182
1137
  context.onReferenceStructProps = function(props) {
1183
1138
  var _this_currentDeclarationOwner;
1184
1139
  var _this_currentDeclarationOwner_id;
1185
- var from = (_this_currentDeclarationOwner_id = (_this_currentDeclarationOwner = _this.currentDeclarationOwner) == null ? void 0 : _this_currentDeclarationOwner.id) != null ? _this_currentDeclarationOwner_id : 0;
1140
+ var from = (_this_currentDeclarationOwner_id = (_this_currentDeclarationOwner = _this1.currentDeclarationOwner) == null ? void 0 : _this_currentDeclarationOwner.id) != null ? _this_currentDeclarationOwner_id : 0;
1186
1141
  for(var _iterator = _create_for_of_iterator_helper_loose(props), _step; !(_step = _iterator()).done;){
1187
1142
  var prop = _step.value;
1188
- _this._referenceDeclaration(from, propDeclarations.get(prop));
1143
+ _this1._referenceDeclaration(from, propDeclarations.get(prop));
1189
1144
  }
1190
1145
  };
1191
1146
  }
1192
1147
  if (coreInfo.unsupportedDeferredDeclarations.length) {
1193
1148
  throw internal.mapExpandedShaderError(new internal.GSError(internal.GSErrorName.CompilationError, "Deferred inheritance requires exact signatures and cannot resolve a known partial-coverage conflict.", coreInfo.unsupportedDeferredDeclarations[0].astNode.location, ir.source, undefined, "UnsupportedDeferredDeclaration"), ir.source, ir.sourceMap);
1194
1149
  }
1150
+ this._fragmentVaryingProps.clear();
1151
+ var fragment = this._fragmentMain(coreInfo.fragmentEntry, shaderData, outerGlobalMacroDeclarations);
1152
+ var vertex = this._vertexMain(coreInfo.vertexEntry, shaderData, outerGlobalMacroDeclarations);
1195
1153
  return {
1196
- vertex: this._vertexMain(coreInfo.vertexEntry, shaderData, outerGlobalMacroDeclarations),
1197
- fragment: this._fragmentMain(coreInfo.fragmentEntry, shaderData, outerGlobalMacroDeclarations)
1154
+ vertex: vertex,
1155
+ fragment: fragment,
1156
+ vertexOutputNames: this._vertexOutputNames
1198
1157
  };
1199
1158
  } finally{
1200
1159
  this._sourceIR = undefined;
1201
1160
  this._outerMacroDeclarations = [];
1161
+ this._globalInputs.clear();
1202
1162
  this._declarationOwners.clear();
1203
1163
  context.onReferenceGlobal = undefined;
1204
1164
  context.onReferenceStructProps = undefined;
1165
+ this._fragmentVaryingProps.clear();
1166
+ this._globalNames.clear();
1167
+ this._escapedDeclarations.clear();
1168
+ this._vertexOutputNames = undefined;
1169
+ context.reset();
1170
+ context.names.reset();
1205
1171
  }
1206
1172
  };
1207
1173
  _proto._vertexMain = function _vertexMain(entryInfo, data, outerGlobalMacroDeclarations) {
1208
1174
  var context = this.context;
1209
1175
  context.stage = internal.EShaderStage.VERTEX;
1210
1176
  context.stageEntry = entryInfo.name;
1177
+ for(var _iterator = _create_for_of_iterator_helper_loose(this._fragmentVaryingProps), _step; !(_step = _iterator()).done;){
1178
+ var prop = _step.value;
1179
+ context.referenceVarying(prop.ident);
1180
+ }
1211
1181
  // Attribute/varying structs were collected in ShaderCoreInfo
1212
1182
  // Pre-walk global `#define` values so referenced struct properties emit `attribute`/`varying` declarations.
1213
1183
  this._preRegisterGlobalMacroRefs(outerGlobalMacroDeclarations);
1214
1184
  var globalCodeArray = this._globalCodeArray;
1215
1185
  context.referenceGlobal(entryInfo.name, internal.ESymbolType.FN);
1186
+ this._referenceInterfaceDependencies([].concat(context.attributeStructs, context.varyingStructs));
1187
+ this._initializeGlobalInputs();
1216
1188
  this._getGlobalSymbol(globalCodeArray);
1217
1189
  this._getCustomStruct(context.attributeStructs, globalCodeArray);
1218
1190
  this._getCustomStruct(context.varyingStructs, globalCodeArray);
@@ -1229,28 +1201,17 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1229
1201
  return globalCode;
1230
1202
  };
1231
1203
  _proto._fragmentMain = function _fragmentMain(entryInfo, data, outerGlobalMacroStatements) {
1232
- var _this = this;
1233
1204
  var context = this.context;
1234
1205
  context.stage = internal.EShaderStage.FRAGMENT;
1235
1206
  context.stageEntry = entryInfo.name;
1236
1207
  this.prepareFragment(entryInfo, outerGlobalMacroStatements);
1237
- // Every value-return must preserve early-exit control flow after entry return values are
1238
- // lowered into fragment outputs.
1239
- entryInfo.functions.forEach(function(fnSymbol) {
1240
- var returnType = fnSymbol.astNode.protoType.returnType;
1241
- var mode = returnType.type === internal.Keyword.VEC4 ? "color" : returnType.typeSpecifier.structDeclarations.some(function(struct) {
1242
- return context.hasStructRole(struct, internal.ShaderStructRole.Mrt);
1243
- }) ? "mrt" : undefined;
1244
- if (mode) {
1245
- var statements = fnSymbol.astNode.statements;
1246
- _this._registerFragmentReturns(statements, mode, internal.ParserUtils.lastStatement(statements));
1247
- }
1248
- });
1249
1208
  // Struct-variable identities are already populated from ShaderCoreInfo; just pre-walk macro
1250
1209
  // refs so struct codegen sees the references.
1251
1210
  this._preRegisterGlobalMacroRefs(outerGlobalMacroStatements);
1252
1211
  var globalCodeArray = this._globalCodeArray;
1253
1212
  context.referenceGlobal(entryInfo.name, internal.ESymbolType.FN);
1213
+ this._referenceInterfaceDependencies([].concat(context.varyingStructs, context.mrtStructs));
1214
+ this._initializeGlobalInputs();
1254
1215
  this._getGlobalSymbol(globalCodeArray);
1255
1216
  this._getCustomStruct(context.varyingStructs, globalCodeArray);
1256
1217
  this._getCustomStruct(context.mrtStructs, globalCodeArray);
@@ -1262,10 +1223,347 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1262
1223
  }).map(function(item) {
1263
1224
  return item.text;
1264
1225
  }).join("\n");
1265
- context.reset();
1226
+ for(var _iterator = _create_for_of_iterator_helper_loose(Object.values(context._referencedVaryingList)), _step; !(_step = _iterator()).done;){
1227
+ var props = _step.value;
1228
+ for(var _iterator1 = _create_for_of_iterator_helper_loose(props), _step1; !(_step1 = _iterator1()).done;){
1229
+ var prop = _step1.value;
1230
+ this._fragmentVaryingProps.add(prop);
1231
+ }
1232
+ }
1233
+ context.reset(false);
1266
1234
  this.reset();
1267
1235
  return globalCode;
1268
1236
  };
1237
+ _proto._referenceInterfaceDependencies = function _referenceInterfaceDependencies(structs) {
1238
+ for(var _iterator = _create_for_of_iterator_helper_loose(structs), _step; !(_step = _iterator()).done;){
1239
+ var struct = _step.value;
1240
+ this._currentDeclaration = struct;
1241
+ for(var _iterator1 = _create_for_of_iterator_helper_loose(struct.propList), _step1; !(_step1 = _iterator1()).done;){
1242
+ var prop = _step1.value;
1243
+ var _prop_typeInfo_arraySpecifier;
1244
+ for(var _iterator2 = _create_for_of_iterator_helper_loose(prop.typeInfo.structDeclarations), _step2; !(_step2 = _iterator2()).done;){
1245
+ var dependency = _step2.value;
1246
+ if (dependency.ident) this.context.referenceGlobal(dependency.ident.lexeme, internal.ESymbolType.STRUCT);
1247
+ }
1248
+ (_prop_typeInfo_arraySpecifier = prop.typeInfo.arraySpecifier) == null ? void 0 : _prop_typeInfo_arraySpecifier.codeGen(this);
1249
+ }
1250
+ }
1251
+ this._currentDeclaration = undefined;
1252
+ };
1253
+ /**
1254
+ * Resolves an interface member's global name, shared by both stages for varyings.
1255
+ * @param prop - Interface member
1256
+ * @param role - Stage-interface role of the member's structure
1257
+ * @returns Generated global identifier
1258
+ */ _proto.interfaceName = function interfaceName(prop, role) {
1259
+ var context = this.context;
1260
+ var name = prop.ident.lexeme;
1261
+ var isOutput = role === internal.ShaderStructRole.Mrt;
1262
+ var sameName = function(item) {
1263
+ return item.ident.lexeme === name;
1264
+ };
1265
+ var conflicts = this._globalNames.has(name) || // Vertex attributes and fragment outputs share a stage scope with varyings.
1266
+ !isOutput && (context.attributeList.some(sameName) || context.mrtList.some(sameName));
1267
+ var key = (isOutput ? "output" : "varying") + ":" + name;
1268
+ var generated = isUnsafeGLSLIdentifier(name) ? context.names.sourceName(key, name) : conflicts ? context.names.allocate(key, name) : name;
1269
+ if (!isOutput && context.stage === internal.EShaderStage.VERTEX && generated !== name) {
1270
+ var _this__vertexOutputNames;
1271
+ ((_this__vertexOutputNames = this._vertexOutputNames) != null ? _this__vertexOutputNames : this._vertexOutputNames = Object.create(null))[name] = generated;
1272
+ }
1273
+ return generated;
1274
+ };
1275
+ _proto.interfaceMemberName = function interfaceMemberName(name) {
1276
+ return this.context.names.sourceName("member:" + name, name);
1277
+ };
1278
+ _proto._interfaceTypeName = function _interfaceTypeName(struct) {
1279
+ var name = struct.ident.lexeme;
1280
+ return this.context.names.sourceName("type:" + name, name);
1281
+ };
1282
+ /**
1283
+ * Escapes interface-typed variables whose names GLSL ES reserves.
1284
+ * @param name - Source name
1285
+ * @param structs - Struct declarations of the variable type
1286
+ * @returns The escaped name, or the source name when no escape is necessary
1287
+ */ _proto._interfaceVariableName = function _interfaceVariableName(name, structs) {
1288
+ return (structs == null ? void 0 : structs.length) && this.context.getStructRole(structs) ? this.context.names.sourceName("variable:" + name, name) : name;
1289
+ };
1290
+ _proto._collectEscapedDeclarations = function _collectEscapedDeclarations(node) {
1291
+ if (_instanceof(node, internal.ASTNode.TypeSpecifier) && this.context.getStructRole(node.structDeclarations)) {
1292
+ var _node_structDeclarations__ident;
1293
+ var token = node.children[0].children[0];
1294
+ if (_instanceof(token, internal.BaseToken) && token.lexeme === ((_node_structDeclarations__ident = node.structDeclarations[0].ident) == null ? void 0 : _node_structDeclarations__ident.lexeme)) {
1295
+ var name = this._interfaceTypeName(node.structDeclarations[0]);
1296
+ if (name !== token.lexeme) this._escapedDeclarations.set(token, name);
1297
+ }
1298
+ }
1299
+ var identifier;
1300
+ var structs;
1301
+ if (_instanceof(node, internal.ASTNode.ParameterDeclarator)) {
1302
+ var _node_typeInfo;
1303
+ identifier = node.ident;
1304
+ structs = (_node_typeInfo = node.typeInfo) == null ? void 0 : _node_typeInfo.structDeclarations;
1305
+ } else {
1306
+ var _declarator_typeInfo;
1307
+ // Local, global and multi-variable declarations share the parser's `declarator` record.
1308
+ var declarator = node.declarator;
1309
+ identifier = declarator == null ? void 0 : declarator.identifier;
1310
+ structs = declarator == null ? void 0 : (_declarator_typeInfo = declarator.typeInfo) == null ? void 0 : _declarator_typeInfo.structDeclarations;
1311
+ }
1312
+ if (identifier) {
1313
+ var name1 = this._interfaceVariableName(identifier.lexeme, structs);
1314
+ if (name1 !== identifier.lexeme) this._escapedDeclarations.set(identifier, name1);
1315
+ }
1316
+ for(var _iterator = _create_for_of_iterator_helper_loose(node.children), _step; !(_step = _iterator()).done;){
1317
+ var child = _step.value;
1318
+ if (_instanceof(child, internal.TreeNode)) this._collectEscapedDeclarations(child);
1319
+ }
1320
+ };
1321
+ _proto.tokenLexeme = function tokenLexeme(token) {
1322
+ var _this__escapedDeclarations_get;
1323
+ return (_this__escapedDeclarations_get = this._escapedDeclarations.get(token)) != null ? _this__escapedDeclarations_get : token.lexeme;
1324
+ };
1325
+ _proto._escapedReference = function _escapedReference(node) {
1326
+ var token = node.children[0];
1327
+ if (!this._escapedDeclarations.size || !_instanceof(token, internal.BaseToken) || !isUnsafeGLSLIdentifier(token.lexeme)) {
1328
+ return undefined;
1329
+ }
1330
+ var symbols = node.resolvedValueSymbols();
1331
+ if (!symbols.length) {
1332
+ // Macro values can lack symbols. The stage's unambiguous name-to-role map binds them, as for member access.
1333
+ return this.context.getUnresolvedStructVarRole(token.lexeme) ? this.context.names.sourceName("variable:" + token.lexeme, token.lexeme) : undefined;
1334
+ }
1335
+ var name;
1336
+ for(var _iterator = _create_for_of_iterator_helper_loose(symbols), _step; !(_step = _iterator()).done;){
1337
+ var symbol = _step.value;
1338
+ var _symbol_dataType;
1339
+ var candidate = _instanceof(symbol, internal.VarSymbol) ? this._interfaceVariableName(token.lexeme, (_symbol_dataType = symbol.dataType) == null ? void 0 : _symbol_dataType.structDeclarations) : _instanceof(symbol, internal.StructSymbol) && this.context.getStructRole([
1340
+ symbol.astNode
1341
+ ]) ? this._interfaceTypeName(symbol.astNode) : token.lexeme;
1342
+ // Branch candidates must agree, otherwise the reference keeps its source spelling.
1343
+ if (name !== undefined && name !== candidate) return undefined;
1344
+ name = candidate;
1345
+ }
1346
+ return name;
1347
+ };
1348
+ _proto._transferName = function _transferName(struct, read) {
1349
+ return this.context.names.allocate((read ? "read" : "write") + ":" + struct.ident.lexeme, (read ? "read" : "write") + "_" + struct.ident.lexeme);
1350
+ };
1351
+ _proto.visitStructSpecifier = function visitStructSpecifier(node) {
1352
+ var _this = this;
1353
+ var context = this.context;
1354
+ var role = context.getStructRole([
1355
+ node
1356
+ ]);
1357
+ if (!role) return CodeGenVisitor.prototype.visitStructSpecifier.call(this, node);
1358
+ var typeName = this._interfaceTypeName(node);
1359
+ var memberCode = function(emit) {
1360
+ var segments = node.macroExpressions.map(function(item) {
1361
+ return {
1362
+ text: _instanceof(item, internal.BaseToken) ? item.lexeme : item.codeGen(_this),
1363
+ index: item.location.start.index
1364
+ };
1365
+ });
1366
+ for(var _iterator = _create_for_of_iterator_helper_loose(node.propList), _step; !(_step = _iterator()).done;){
1367
+ var prop = _step.value;
1368
+ if (!retain(prop)) continue;
1369
+ segments.push({
1370
+ text: emit(prop),
1371
+ index: prop.ident.location.start.index
1372
+ });
1373
+ }
1374
+ return segments.sort(function(a, b) {
1375
+ return a.index - b.index;
1376
+ }).map(function(item) {
1377
+ return item.text;
1378
+ }).join("\n");
1379
+ };
1380
+ var references = role === internal.ShaderStructRole.Attribute ? context._referencedAttributeList : role === internal.ShaderStructRole.Varying ? context._referencedVaryingList : context._referencedMRTList;
1381
+ var used = function(prop) {
1382
+ var _references_prop_ident_lexeme;
1383
+ return !!((_references_prop_ident_lexeme = references[prop.ident.lexeme]) == null ? void 0 : _references_prop_ident_lexeme.includes(prop));
1384
+ };
1385
+ // Interface members have no array wire form, and GLSL ES 1.00 functions cannot return structures that contain
1386
+ // arrays. GLES100 therefore omits array members from interface structures.
1387
+ var retain = function(prop) {
1388
+ return _this.supportsArrayStructReturn || !_this._containsArray(prop);
1389
+ };
1390
+ var declaration = function(prop) {
1391
+ var _prop_typeInfo_arraySpecifier;
1392
+ var _prop_typeInfo_arraySpecifier_codeGen;
1393
+ return prop.typeInfo.typeLexeme + " " + _this.interfaceMemberName(prop.ident.lexeme) + ((_prop_typeInfo_arraySpecifier_codeGen = (_prop_typeInfo_arraySpecifier = prop.typeInfo.arraySpecifier) == null ? void 0 : _prop_typeInfo_arraySpecifier.codeGen(_this)) != null ? _prop_typeInfo_arraySpecifier_codeGen : "") + ";";
1394
+ };
1395
+ var structCode;
1396
+ var hasRetainedField = node.propList.some(retain);
1397
+ if (hasRetainedField && !node.macroExpressions.length) {
1398
+ structCode = "struct " + typeName + " {\n" + memberCode(declaration) + "\n};";
1399
+ } else {
1400
+ var hasFields = context.names.allocate(node, "" + typeName + "_has_fields");
1401
+ var padding = context.names.allocate("empty-interface-member", "interface_padding");
1402
+ // A macro configuration can remove every source field; GLSL still requires a nonempty struct.
1403
+ structCode = "#undef " + hasFields + "\nstruct " + typeName + " {\n" + memberCode(function(prop) {
1404
+ return declaration(prop) + "\n#define " + hasFields + "\n";
1405
+ }) + "\n#ifndef " + hasFields + "\nfloat " + padding + ";\n#endif\n};\n#undef " + hasFields + "\n";
1406
+ }
1407
+ var declarations = memberCode(function(prop) {
1408
+ var _this_getMRTProp;
1409
+ return !used(prop) ? "" : role === internal.ShaderStructRole.Attribute ? _this.getAttributeProp(prop) : role === internal.ShaderStructRole.Varying ? _this.getVaryingProp(prop) : (_this_getMRTProp = _this.getMRTProp(prop)) != null ? _this_getMRTProp : "";
1410
+ });
1411
+ var read = role === internal.ShaderStructRole.Attribute || role === internal.ShaderStructRole.Varying && context.stage === internal.EShaderStage.FRAGMENT;
1412
+ var value = context.names.allocate("interface-value", "interface_value");
1413
+ var assignments = memberCode(function(prop) {
1414
+ if (!used(prop)) return "";
1415
+ var wire = role === internal.ShaderStructRole.Attribute ? prop.ident.lexeme : role === internal.ShaderStructRole.Mrt ? _this.mrtOutput(prop) : _this.interfaceName(prop, role);
1416
+ var member = value + "." + _this.interfaceMemberName(prop.ident.lexeme);
1417
+ return read ? member + " = " + wire + ";" : wire + " = " + member + ";";
1418
+ });
1419
+ var transfer = read ? typeName + " " + this._transferName(node, true) + "() { " + typeName + " " + value + ";\n" + assignments + "\nreturn " + value + "; }" : "void " + this._transferName(node, false) + "(" + typeName + " " + value + ") {\n" + assignments + "\n}";
1420
+ return structCode + "\n" + declarations + "\n" + transfer;
1421
+ };
1422
+ _proto._containsArray = function _containsArray(prop, visited) {
1423
+ var _this = this;
1424
+ if (visited === void 0) visited = new Set();
1425
+ if (prop.typeInfo.arraySpecifier) return true;
1426
+ for(var _iterator = _create_for_of_iterator_helper_loose(prop.typeInfo.structDeclarations), _step; !(_step = _iterator()).done;){
1427
+ var struct = _step.value;
1428
+ if (visited.has(struct)) continue;
1429
+ visited.add(struct);
1430
+ if (struct.propList.some(function(member) {
1431
+ return _this._containsArray(member, visited);
1432
+ })) return true;
1433
+ }
1434
+ return false;
1435
+ };
1436
+ _proto.defaultCodeGen = function defaultCodeGen(children) {
1437
+ var _this = this;
1438
+ var parent = _instanceof(children[0], internal.TreeNode) ? children[0].parent : undefined;
1439
+ if (_instanceof(parent, internal.ASTNode.EqualityExpression) && children.length === 3) {
1440
+ var observe = function(node) {
1441
+ if (_instanceof(node, internal.ASTNode.VariableIdentifier)) {
1442
+ for(var _iterator = _create_for_of_iterator_helper_loose(node.resolvedValueSymbols()), _step; !(_step = _iterator()).done;){
1443
+ var symbol = _step.value;
1444
+ var _symbol_dataType;
1445
+ var _symbol_dataType_structDeclarations;
1446
+ if (_instanceof(symbol, internal.VarSymbol)) for(var _iterator1 = _create_for_of_iterator_helper_loose((_symbol_dataType_structDeclarations = (_symbol_dataType = symbol.dataType) == null ? void 0 : _symbol_dataType.structDeclarations) != null ? _symbol_dataType_structDeclarations : []), _step1; !(_step1 = _iterator1()).done;){
1447
+ var struct = _step1.value;
1448
+ _this._observeWholeStruct(struct);
1449
+ }
1450
+ }
1451
+ }
1452
+ if (_instanceof(node, internal.ASTNode.FunctionCallGeneric)) {
1453
+ var _node_fnSymbol;
1454
+ var declaration = (_node_fnSymbol = node.fnSymbol) == null ? void 0 : _node_fnSymbol.astNode;
1455
+ if (_instanceof(declaration, internal.ASTNode.StructSpecifier)) _this._observeWholeStruct(declaration);
1456
+ else if (_instanceof(declaration, internal.ASTNode.FunctionDefinition)) {
1457
+ for(var _iterator2 = _create_for_of_iterator_helper_loose(declaration.protoType.returnType.typeSpecifier.structDeclarations), _step2; !(_step2 = _iterator2()).done;){
1458
+ var struct1 = _step2.value;
1459
+ _this._observeWholeStruct(struct1);
1460
+ }
1461
+ }
1462
+ }
1463
+ for(var _iterator3 = _create_for_of_iterator_helper_loose(node.children), _step3; !(_step3 = _iterator3()).done;){
1464
+ var child = _step3.value;
1465
+ if (_instanceof(child, internal.TreeNode)) observe(child);
1466
+ }
1467
+ };
1468
+ for(var _iterator = _create_for_of_iterator_helper_loose(children), _step; !(_step = _iterator()).done;){
1469
+ var child = _step.value;
1470
+ if (_instanceof(child, internal.TreeNode) && typeof child.type === "string") observe(child);
1471
+ }
1472
+ }
1473
+ return CodeGenVisitor.prototype.defaultCodeGen.call(this, children);
1474
+ };
1475
+ _proto._observeWholeStruct = function _observeWholeStruct(struct) {
1476
+ var role = this.context.getStructRole([
1477
+ struct
1478
+ ]);
1479
+ for(var _iterator = _create_for_of_iterator_helper_loose(struct.propList), _step; !(_step = _iterator()).done;){
1480
+ var prop = _step.value;
1481
+ if (role === internal.ShaderStructRole.Attribute) this.context.referenceAttribute(prop.ident);
1482
+ else if (role === internal.ShaderStructRole.Varying) this.context.referenceVarying(prop.ident);
1483
+ else if (role === internal.ShaderStructRole.Mrt) this.context.referenceMRTProp(prop.ident);
1484
+ }
1485
+ };
1486
+ _proto.visitFunctionHeader = function visitFunctionHeader(node) {
1487
+ if (node.ident.lexeme === "main" && this.context.stageEntry === "main") {
1488
+ return node.returnType.codeGen(this) + " " + this.context.names.allocate("entry-main", "entry_main") + "(";
1489
+ }
1490
+ return CodeGenVisitor.prototype.visitFunctionHeader.call(this, node);
1491
+ };
1492
+ _proto.visitFunctionIdentifier = function visitFunctionIdentifier(node) {
1493
+ var call = node.parent;
1494
+ if (node.lexeme === "main" && this.context.stageEntry === "main" && _instanceof(call, internal.ASTNode.FunctionCallGeneric) && _instanceof(call.fnSymbol, internal.FnSymbol)) {
1495
+ return this.context.names.allocate("entry-main", "entry_main");
1496
+ }
1497
+ return CodeGenVisitor.prototype.visitFunctionIdentifier.call(this, node);
1498
+ };
1499
+ _proto.visitFunctionDefinition = function visitFunctionDefinition(fnNode) {
1500
+ var definition = CodeGenVisitor.prototype.visitFunctionDefinition.call(this, fnNode);
1501
+ if (fnNode.protoType.ident.lexeme !== this.context.stageEntry) return definition;
1502
+ var args = [];
1503
+ var parameters = [];
1504
+ var _fnNode_protoType_parameterList;
1505
+ for(var _iterator = _create_for_of_iterator_helper_loose((_fnNode_protoType_parameterList = fnNode.protoType.parameterList) != null ? _fnNode_protoType_parameterList : []), _step; !(_step = _iterator()).done;){
1506
+ var parameter = _step.value;
1507
+ if (!parameter.ident || !parameter.typeInfo) continue;
1508
+ var struct = parameter.typeInfo.structDeclarations[0];
1509
+ if (struct) {
1510
+ var name = this.context.names.allocate(parameter, "input_" + parameter.ident.lexeme);
1511
+ parameters.push(this._interfaceTypeName(struct) + " " + name + " = " + this._transferName(struct, true) + "();");
1512
+ args.push(name);
1513
+ }
1514
+ }
1515
+ var name1 = fnNode.protoType.ident.lexeme;
1516
+ var entryName = name1 === "main" ? this.context.names.allocate("entry-main", "entry_main") : name1;
1517
+ var call = entryName + "(" + args.join(", ") + ")";
1518
+ var type = fnNode.protoType.returnType;
1519
+ var struct1 = type.typeSpecifier.structDeclarations[0];
1520
+ var statement;
1521
+ if (struct1) {
1522
+ statement = this._transferName(struct1, false) + "(" + call + ");";
1523
+ } else if (this.context.stage === internal.EShaderStage.FRAGMENT && type.type === internal.Keyword.VEC4) {
1524
+ statement = this.fragmentColorOutput() + " = " + call + ";";
1525
+ } else statement = "" + call + ";";
1526
+ var inputs = this._initializeGlobalInputs();
1527
+ return definition + "\nvoid main() {\n" + inputs.join("\n") + "\n" + parameters.join("\n") + "\n" + statement + " }";
1528
+ };
1529
+ _proto._initializeGlobalInputs = function _initializeGlobalInputs() {
1530
+ var calls = new Set();
1531
+ var _this__globalInputs_get;
1532
+ for(var _iterator = _create_for_of_iterator_helper_loose((_this__globalInputs_get = this._globalInputs.get(this.context.stage)) != null ? _this__globalInputs_get : []), _step; !(_step = _iterator()).done;){
1533
+ var variable = _step.value;
1534
+ this.context.referenceGlobal(variable.ident, internal.ESymbolType.VAR);
1535
+ var struct = variable.dataType.structDeclarations[0];
1536
+ var owner = this._declarationOwners.get(variable.astNode);
1537
+ this._referenceDeclaration(owner.id, struct);
1538
+ var group = this._inputHelperGroups.get(owner.group);
1539
+ if (!group) {
1540
+ var nextId = 0;
1541
+ for(var _iterator1 = _create_for_of_iterator_helper_loose(this._declarationOwners.values()), _step1; !(_step1 = _iterator1()).done;){
1542
+ var declaration = _step1.value;
1543
+ nextId = Math.max(nextId, declaration.id);
1544
+ }
1545
+ for(var _iterator2 = _create_for_of_iterator_helper_loose(this._inputHelperGroups.values()), _step2; !(_step2 = _iterator2()).done;){
1546
+ var helper = _step2.value;
1547
+ nextId = Math.max(nextId, helper.fallback.id);
1548
+ }
1549
+ // A missing conditional input selects the no-op; an active declaration selects its own initializer.
1550
+ var fallback = {
1551
+ id: nextId + 1,
1552
+ group: owner.group,
1553
+ sourceScope: -1
1554
+ };
1555
+ var name = this.context.names.allocate("input-initializer:" + owner.group, "initialize_" + variable.ident);
1556
+ this._inputHelperGroups.set(owner.group, group = {
1557
+ name: name,
1558
+ fallback: fallback
1559
+ });
1560
+ }
1561
+ var target = this._interfaceVariableName(variable.ident, variable.dataType.structDeclarations);
1562
+ this._inputHelperBodies.set(variable.astNode, "void " + group.name + "() { " + target + " = " + this._transferName(struct, true) + "(); }");
1563
+ calls.add("" + group.name + "();");
1564
+ }
1565
+ return Array.from(calls);
1566
+ };
1269
1567
  _proto.prepareFragment = function prepareFragment(entryInfo, outerGlobalMacroStatements) {
1270
1568
  };
1271
1569
  /**
@@ -1295,17 +1593,6 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1295
1593
  }
1296
1594
  }
1297
1595
  };
1298
- _proto._registerFragmentReturns = function _registerFragmentReturns(node, mode, terminal) {
1299
- if (_instanceof(node, internal.ASTNode.JumpStatement) && node.children.length === 3) {
1300
- this.context.registerFragmentReturn(node, mode);
1301
- if (node === terminal) this.context.registerTerminalInterfaceReturn(node);
1302
- return;
1303
- }
1304
- for(var _iterator = _create_for_of_iterator_helper_loose(node.children), _step; !(_step = _iterator()).done;){
1305
- var child = _step.value;
1306
- if (_instanceof(child, internal.TreeNode)) this._registerFragmentReturns(child, mode, terminal);
1307
- }
1308
- };
1309
1596
  _proto._getGlobalSymbol = function _getGlobalSymbol(out) {
1310
1597
  var context = this.context;
1311
1598
  // Set iteration includes dependencies discovered while generating earlier declarations.
@@ -1426,11 +1713,6 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1426
1713
  for(var _iterator2 = _create_for_of_iterator_helper_loose((_prototype_parameterList = prototype.parameterList) != null ? _prototype_parameterList : []), _step2; !(_step2 = _iterator2()).done;){
1427
1714
  var parameter = _step2.value;
1428
1715
  var _parameter_typeInfo;
1429
- if (_instanceof(parameter.astNode, internal.ASTNode.ParameterDeclaration) && parameter.astNode.symbol && this.context.getStructVarRole([
1430
- parameter.astNode.symbol
1431
- ])) {
1432
- continue;
1433
- }
1434
1716
  var _parameter_typeInfo_structDeclarations;
1435
1717
  this._prepareForwardTypes((_parameter_typeInfo_structDeclarations = (_parameter_typeInfo = parameter.typeInfo) == null ? void 0 : _parameter_typeInfo.structDeclarations) != null ? _parameter_typeInfo_structDeclarations : [], anchor, typeAnchor);
1436
1718
  }
@@ -1438,12 +1720,9 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1438
1720
  }
1439
1721
  };
1440
1722
  _proto._getForwardSignature = function _getForwardSignature(prototype) {
1441
- var _this, _loop = function() {
1723
+ var _loop = function() {
1442
1724
  var parameter = _step.value;
1443
1725
  var declaration = parameter.astNode;
1444
- if (_instanceof(declaration, internal.ASTNode.ParameterDeclaration) && declaration.symbol && _this.context.getStructVarRole([
1445
- declaration.symbol
1446
- ])) return "continue";
1447
1726
  var tokens = [];
1448
1727
  var visit = function(node) {
1449
1728
  if (_instanceof(node, internal.BaseToken)) {
@@ -1458,7 +1737,7 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1458
1737
  };
1459
1738
  var parameters = [];
1460
1739
  var _prototype_parameterList;
1461
- for(var _iterator = _create_for_of_iterator_helper_loose((_prototype_parameterList = prototype.parameterList) != null ? _prototype_parameterList : []), _step; !(_step = _iterator()).done;)_this = this, _loop();
1740
+ for(var _iterator = _create_for_of_iterator_helper_loose((_prototype_parameterList = prototype.parameterList) != null ? _prototype_parameterList : []), _step; !(_step = _iterator()).done;)_loop();
1462
1741
  return prototype.returnType.codeGen(this) + "(" + parameters.join(",") + ")";
1463
1742
  };
1464
1743
  _proto._canBranchesOverlap = function _canBranchesOverlap(left, right) {
@@ -1511,7 +1790,8 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1511
1790
  };
1512
1791
  var code = CodeGenVisitor.prototype.visitVariableIdentifier.call(this, node);
1513
1792
  for(var _iterator = _create_for_of_iterator_helper_loose(node.resolvedSymbols()), _step; !(_step = _iterator()).done;)_this = this, _loop();
1514
- return code;
1793
+ var _this__escapedReference;
1794
+ return (_this__escapedReference = this._escapedReference(node)) != null ? _this__escapedReference : code;
1515
1795
  };
1516
1796
  _proto._getForwardDeclarations = function _getForwardDeclarations(out) {
1517
1797
  var _this, _loop = function() {
@@ -1687,6 +1967,7 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1687
1967
  _proto._getCustomStruct = function _getCustomStruct(structNodes, out) {
1688
1968
  for(var _iterator = _create_for_of_iterator_helper_loose(structNodes), _step; !(_step = _iterator()).done;){
1689
1969
  var node = _step.value;
1970
+ if (this._structCodeSegments.has(node)) continue;
1690
1971
  var text = node.codeGen(this);
1691
1972
  if (!node.isInMacroBranch) {
1692
1973
  out.push({
@@ -1811,7 +2092,8 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1811
2092
  };
1812
2093
  _proto._declaration = function _declaration(text, node, activate) {
1813
2094
  if (activate === void 0) activate = true;
1814
- return ShaderInstructionEncoder.declaration(text, this._declarationOwners.get(node), activate);
2095
+ var initializer = text && this._inputHelperBodies.get(node);
2096
+ return ShaderInstructionEncoder.declaration(initializer ? text + "\n" + initializer : text, this._declarationOwners.get(node), activate);
1815
2097
  };
1816
2098
  _create_class(GLESVisitor, [
1817
2099
  {
@@ -1827,57 +2109,29 @@ ShaderInstructionEncoder._FUNC_MACRO_RE = /^(\w+)\(([^)]*)\)\s*(.*)/;
1827
2109
  var GLES100Visitor = /*#__PURE__*/ function(GLESVisitor) {
1828
2110
  _inherits(GLES100Visitor, GLESVisitor);
1829
2111
  function GLES100Visitor() {
1830
- return GLESVisitor.apply(this, arguments) || this;
2112
+ var _this;
2113
+ _this = GLESVisitor.apply(this, arguments) || this, _this.supportsArrayStructReturn = false;
2114
+ return _this;
1831
2115
  }
1832
2116
  var _proto = GLES100Visitor.prototype;
1833
2117
  _proto.getAttributeProp = function getAttributeProp(prop) {
1834
2118
  return "attribute " + prop.typeInfo.typeLexeme + " " + prop.ident.lexeme + ";";
1835
2119
  };
1836
2120
  _proto.getVaryingProp = function getVaryingProp(prop) {
1837
- return "varying " + prop.typeInfo.typeLexeme + " " + prop.ident.lexeme + ";";
2121
+ return "varying " + prop.typeInfo.typeLexeme + " " + this.interfaceName(prop, internal.ShaderStructRole.Varying) + ";";
1838
2122
  };
1839
2123
  _proto.getMRTProp = function getMRTProp() {
1840
2124
  return "";
1841
2125
  };
1842
- _proto.visitPostfixExpression = function visitPostfixExpression(node) {
1843
- var children = node.children;
1844
- var postExpr = children[0];
1845
- var context = this.context;
1846
- var directRoot = _instanceof(postExpr, internal.ASTNode.PostfixExpression) ? internal.ParserUtils.unwrapBareIdentifier(postExpr, {
1847
- allowParens: true
1848
- }) : undefined;
1849
- var _directRoot_resolvedValueSymbols;
1850
- var directSymbols = (_directRoot_resolvedValueSymbols = directRoot == null ? void 0 : directRoot.resolvedValueSymbols()) != null ? _directRoot_resolvedValueSymbols : [];
1851
- var unresolvedName = _instanceof(postExpr, internal.ASTNode.PostfixExpression) ? internal.ParserUtils.extractDirectIdentLexeme(postExpr) : null;
1852
- var directRole = directRoot ? directSymbols.length ? context.getStructVarRole(directSymbols) : unresolvedName ? context.getUnresolvedStructVarRole(unresolvedName) : undefined : undefined;
1853
- if (directRole === internal.ShaderStructRole.Mrt) {
1854
- var propReferenced = children[2];
1855
- var prop = context.mrtList.find(function(item) {
1856
- return item.ident.lexeme === propReferenced.lexeme;
1857
- });
1858
- // The parser already validated struct fields (UndeclaredStructMember); a miss here is an
1859
- // already-errored shader, so emit nothing rather than re-report.
1860
- if (!prop) return "";
1861
- return "gl_FragData[" + prop.mrtIndex + "]";
1862
- }
1863
- return GLESVisitor.prototype.visitPostfixExpression.call(this, node);
2126
+ _proto.fragmentColorOutput = function fragmentColorOutput() {
2127
+ return "gl_FragColor";
1864
2128
  };
1865
- _proto.visitJumpStatement = function visitJumpStatement(node) {
1866
- var mode = this.context.getFragmentReturnMode(node);
1867
- var terminal = this.context.isTerminalInterfaceReturn(node);
1868
- if (mode === "mrt") return terminal ? "" : "return;";
1869
- if (mode === "color") {
1870
- var expression = node.children[1];
1871
- var assignment = "gl_FragColor = " + expression.codeGen(this) + ";";
1872
- return terminal ? assignment : "{ " + assignment + " return; }";
1873
- }
1874
- return GLESVisitor.prototype.visitJumpStatement.call(this, node);
2129
+ _proto.mrtOutput = function mrtOutput(prop) {
2130
+ return "gl_FragData[" + prop.mrtIndex + "]";
1875
2131
  };
1876
2132
  return GLES100Visitor;
1877
2133
  }(GLESVisitor);
1878
2134
 
1879
- var V3_GL_FragColor = "GS_glFragColor";
1880
- var V3_GL_FragData = "GS_glFragData";
1881
2135
  var GLES300Visitor = /*#__PURE__*/ function(GLESVisitor) {
1882
2136
  _inherits(GLES300Visitor, GLESVisitor);
1883
2137
  function GLES300Visitor() {
@@ -1909,15 +2163,15 @@ var GLES300Visitor = /*#__PURE__*/ function(GLESVisitor) {
1909
2163
  };
1910
2164
  _proto.getVaryingProp = function getVaryingProp(prop) {
1911
2165
  var qualifier = this.context.stage === internal.EShaderStage.FRAGMENT ? "in" : "out";
1912
- return qualifier + " " + prop.typeInfo.typeLexeme + " " + prop.ident.lexeme + ";";
2166
+ return qualifier + " " + prop.typeInfo.typeLexeme + " " + this.interfaceName(prop, internal.ShaderStructRole.Varying) + ";";
1913
2167
  };
1914
2168
  _proto.getMRTProp = function getMRTProp(prop) {
1915
- return "layout(location = " + prop.mrtIndex + ") out vec4 " + prop.ident.lexeme + ";";
2169
+ return "layout(location = " + prop.mrtIndex + ") out vec4 " + this.interfaceName(prop, internal.ShaderStructRole.Mrt) + ";";
1916
2170
  };
1917
2171
  _proto.visitFunctionIdentifier = function visitFunctionIdentifier(node) {
1918
2172
  var children = node.children;
1919
2173
  var typeSpecifier = children[0];
1920
- if (typeSpecifier.children.length !== 1 || typeSpecifier.structDependencies.length) {
2174
+ if (!node.isBuiltin || typeSpecifier.children.length !== 1 || typeSpecifier.structDependencies.length) {
1921
2175
  // Struct constructors still need the shared type-dependency visitor.
1922
2176
  return GLESVisitor.prototype.visitFunctionIdentifier.call(this, node);
1923
2177
  }
@@ -1951,14 +2205,14 @@ var GLES300Visitor = /*#__PURE__*/ function(GLESVisitor) {
1951
2205
  var context = this.context;
1952
2206
  if (context.stage === internal.EShaderStage.FRAGMENT && node.builtinSemantic === internal.ShaderBuiltinSemantic.FragmentOutput0) {
1953
2207
  this._registerFragColorVariable();
1954
- return V3_GL_FragColor;
2208
+ return this._fragmentColorName();
1955
2209
  }
1956
2210
  if (context.stage === internal.EShaderStage.FRAGMENT && node.builtinSemantic === internal.ShaderBuiltinSemantic.FragmentDepth) {
1957
2211
  return "gl_FragDepth";
1958
2212
  }
1959
2213
  if (context.stage === internal.EShaderStage.FRAGMENT && node.builtinSemantic === internal.ShaderBuiltinSemantic.FragmentOutputArray) {
1960
2214
  this._registerFragDataArray();
1961
- return V3_GL_FragData;
2215
+ return this._fragmentDataName();
1962
2216
  }
1963
2217
  return GLESVisitor.prototype.visitVariableIdentifier.call(this, node);
1964
2218
  };
@@ -1969,20 +2223,22 @@ var GLES300Visitor = /*#__PURE__*/ function(GLESVisitor) {
1969
2223
  }
1970
2224
  return GLESVisitor.prototype.visitPostfixExpression.call(this, node);
1971
2225
  };
1972
- _proto.visitJumpStatement = function visitJumpStatement(node) {
1973
- var mode = this.context.getFragmentReturnMode(node);
1974
- var terminal = this.context.isTerminalInterfaceReturn(node);
1975
- if (mode === "mrt") return terminal ? "" : "return;";
1976
- if (mode === "color") {
1977
- this._registerFragColorVariable();
1978
- var expression = node.children[1];
1979
- var assignment = V3_GL_FragColor + " = " + expression.codeGen(this) + ";";
1980
- return terminal ? assignment : "{ " + assignment + " return; }";
1981
- }
1982
- return GLESVisitor.prototype.visitJumpStatement.call(this, node);
2226
+ _proto.fragmentColorOutput = function fragmentColorOutput() {
2227
+ this._registerFragColorVariable();
2228
+ return this._fragmentColorName();
2229
+ };
2230
+ _proto.mrtOutput = function mrtOutput(prop) {
2231
+ return this.interfaceName(prop, internal.ShaderStructRole.Mrt);
2232
+ };
2233
+ _proto._fragmentColorName = function _fragmentColorName() {
2234
+ return this.context.names.allocate("fragment-output", "output_color");
2235
+ };
2236
+ _proto._fragmentDataName = function _fragmentDataName() {
2237
+ return this.context.names.allocate("fragment-output-array", "output_colors");
1983
2238
  };
1984
2239
  _proto._registerFragColorVariable = function _registerFragColorVariable() {
1985
- this._registerOutput(V3_GL_FragColor, "out vec4 " + V3_GL_FragColor + ";");
2240
+ var name = this._fragmentColorName();
2241
+ this._registerOutput(name, "out vec4 " + name + ";");
1986
2242
  };
1987
2243
  _proto._registerOutput = function _registerOutput(name, text) {
1988
2244
  var output = this._outputDeclarations.get(name);
@@ -2035,10 +2291,11 @@ var GLES300Visitor = /*#__PURE__*/ function(GLESVisitor) {
2035
2291
  }
2036
2292
  };
2037
2293
  _proto._registerFragDataArray = function _registerFragDataArray() {
2038
- this._registerOutput(V3_GL_FragData, "layout(location = 0) out vec4 " + V3_GL_FragData + "[gl_MaxDrawBuffers];");
2294
+ var name = this._fragmentDataName();
2295
+ this._registerOutput(name, "layout(location = 0) out vec4 " + name + "[gl_MaxDrawBuffers];");
2039
2296
  };
2040
2297
  _proto._registerFragDataVariable = function _registerFragDataVariable(index) {
2041
- var name = "" + V3_GL_FragData + index;
2298
+ var name = this.context.names.allocate("fragment-output:" + index, "output_color_" + index);
2042
2299
  this._registerOutput(name, "layout(location = " + index + ") out vec4 " + name + ";");
2043
2300
  return name;
2044
2301
  };
@@ -2227,7 +2484,7 @@ function generateShaderProgram(ir, coreInfo, backend, preprocessorExpressions) {
2227
2484
 
2228
2485
  /**
2229
2486
  * Version of the shader compiler package, replaced with the package version during builds.
2230
- */ var version = "2.0.0-alpha.54";
2487
+ */ var version = "2.0.0-alpha.56";
2231
2488
  engineCore.Logger.info("Galacean Engine Shader Compiler Version: " + version);
2232
2489
 
2233
2490
  Object.defineProperty(exports, 'GSError', {