UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 3.91 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ const e=5;function t(e,t,r){const i=t.split("\n");for(const n of i)if(n.trim().length){{let t="";null!=r&&(t+=`/*id:${r??"000"}*/ `),e.body+=t.padEnd(14)}e.body+=" ".repeat(e.indent)+n+"\n"}}class r{write(e){for(const t of e.rootOutputNodes())e.shouldPruneOutputNode(t)||(t.variableName=this._write(e,t.node));return e}_createVarName(e,t){let r="";return"boolean"!=typeof t&&"number"!=typeof t&&t.debugInfo.name&&(r=`${t.debugInfo.name}_`),`${r}v${e.varCount++}`}_write(e,t,r=!1){if("number"==typeof t)return t.toString();if("boolean"==typeof t)return t.toString();let i=e.getEmit(t);if(i)return i;switch(t.shaderType){case"scope-node":i=this._writeScopeNode(e,t);break;case"primitive-node":i=this._writePrimitiveNode(e,t,r);break;case"function-node":i=this._writeFunctionNode(e,t);break;case"property-access-node":i=this._writePropertyAccessNode(e,t);break;case"property-access-2d-node":i=this._writePropertyAccess2DNode(e,t);break;case"text-node":i=t.text;break;case"block-node":i=this._writeBlockNode(e,t);break;case"condition-node":i=this._writeConditionNode(e,t)}return e.setEmit(t,i),i}_writeScopeNode(e,r){const i=new r.child.constructor;i.setDebugName(r.debugInfo.name);const n=this._write(e,i,!0);t(e,`{ /*ScopeStart: ${r.uid} ${r.debugInfo.name}*/`),e.indent+=2;return t(e,`${n} = ${this._write(e,r.child)};`),e.indent-=2,t(e,`} /*ScopeEnd: ${r.uid} ${r.debugInfo.name}*/`),n}_writeConditionNode(e,r){const i=new r.ifTrue.constructor,n=this._write(e,i,!0);t(e,`if (${this._write(e,r.condition)}) {`),e.indent+=2;const o=e.createSubgraphContext(),s=this._write(o,r.ifTrue);if(e.body+=o.body,s&&t(e,`${n} = ${s};`),e.indent-=2,t(e,"}"),r.ifFalse){t(e,"else {"),e.indent+=2;const i=e.createSubgraphContext(),o=this._write(i,r.ifFalse);e.body+=i.body,o&&t(e,`${n} = ${o};`),e.indent-=2,t(e,"}")}return n}_writeBlockNode(e,r){const{captureList:i,generator:n,returnType:o}=r,s={};for(const t in i){if(!i[t])continue;const r=this._write(e,i[t]);s[t]=r}const d=new o,c=this._write(e,d,!0);if(s.out=c,r.subgraph){const t=e.createSubgraphContext(),i=this._write(t,r.subgraph.child),n=t.body;s.subgraph={varName:i,body:n}}const a=n(s);return t(e,"{\n"),e.indent+=2,t(e,a),e.indent-=2,t(e,"}\n"),c}_writePropertyAccessNode(e,t){const r=this._write(e,t.target);if("string"==typeof t.property&&t.property.includes("["))return`${r}${t.property}`;if("string"!=typeof t.property){return`${r}[${this._write(e,t.property)}]`}return`${r}.${t.property}`}_writePropertyAccess2DNode(e,t){return`${this._write(e,t.target)}[${this._write(e,t.x)}][${this._write(e,t.y)}]`}_writeFunctionNode(r,i){const n=i.returnType.type;if(i.isInfix){const[o,s]=i.children.map((e=>this._write(r,e))),d=this._createVarName(r,i);return t(r,`${n.padEnd(e)} ${d} = ${o} ${i.token} ${s};`,i.uid),d}const o=i.children.map((e=>this._write(r,e))).join(", "),s=this._createVarName(r,i);return t(r,`${n.padEnd(e)} ${s} = ${i.token}(${o});`,i.uid),s}_writePrimitiveNode(r,i,n=!1){const o=r.getInput(i);if(o)return o.isUsed=!0,o.variableName;const s=1===i.children.length&&i.children[0]?.type===i.type;if(!i.isMutable&&(i.isImplicit||s))return this._write(r,i.children[0]);const d=this._createVarName(r,i);if(n)return t(r,`${i.type.padEnd(e)} ${d};`,i.uid),d;const c=!i.debugInfo.name&&!i.isMutable;if(c&&"float"===i.type&&"number"==typeof i.children[0])return Number.isInteger(i.children[0])?i.children[0].toFixed(1):i.children[0].toString();if(c&&"int"===i.type&&"number"==typeof i.children[0]&&Number.isInteger(i.children[0]))return i.children[0].toString();const a=i.children.map((e=>this._write(r,e))).join(", ");return"array"===i.type?(t(r,`${i.type.padEnd(e)} ${d} = [${a}];`,i.uid),d):c?`${i.type}(${a})`:(t(r,`${i.type.padEnd(e)} ${d} = ${i.type}(${a});`,i.uid),d)}}export{r as GlslShaderWriter};