12345 |
- /*
- All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- See https://js.arcgis.com/4.25/esri/copyright.txt for details.
- */
- import"../../core/has.js";import{isSome as t}from"../../core/maybe.js";import{ShaderType as e}from"./enums.js";import a from"./reservedWordsGLSL3.js";import{shaderTranspiler as r}from"./testUtils.js";import{o as n,l as o,b as i}from"../../chunks/builtins.js";var s=999,c=9999,p=0,u=1,d=2,f=3,l=4,h=5,y=6,w=7,g=8,m=9,b=10,k=11,E=["block-comment","line-comment","preprocessor","operator","integer","float","ident","builtin","keyword","whitespace","eof","integer"];function _(){var t,e,a,r=0,_=0,v=s,j=[],x=[],D=1,R=0,T=0,A=!1,G=!1,S="";return function(t){return x=[],null!==t?F(t.replace?t.replace(/\r\n/g,"\n"):t):L()};function X(t){t.length&&x.push({type:E[v],data:t,position:T,line:D,column:R})}function F(e){var n;for(r=0,a=(S+=e).length;t=S[r],r<a;){switch(n=r,v){case p:r=V();break;case u:r=P();break;case d:r=M();break;case f:r=O();break;case l:r=z();break;case k:r=$();break;case h:r=I();break;case c:r=U();break;case m:r=H();break;case s:r=C()}if(n!==r)if("\n"===S[n])R=0,++D;else++R}return _+=r,S=S.slice(r),x}function L(t){return j.length&&X(j.join("")),v=b,X("(eof)"),x}function C(){return j=j.length?[]:j,"/"===e&&"*"===t?(T=_+r-1,v=p,e=t,r+1):"/"===e&&"/"===t?(T=_+r-1,v=u,e=t,r+1):"#"===t?(v=d,T=_+r,r):/\s/.test(t)?(v=m,T=_+r,r):(A=/\d/.test(t),G=/[^\w_]/.test(t),T=_+r,v=A?l:G?f:c,r)}function H(){return/[^\s]/g.test(t)?(X(j.join("")),v=s,r):(j.push(t),e=t,r+1)}function M(){return"\r"!==t&&"\n"!==t||"\\"===e?(j.push(t),e=t,r+1):(X(j.join("")),v=s,r)}function P(){return M()}function V(){return"/"===t&&"*"===e?(j.push(t),X(j.join("")),v=s,r+1):(j.push(t),e=t,r+1)}function O(){if("."===e&&/\d/.test(t))return v=h,r;if("/"===e&&"*"===t)return v=p,r;if("/"===e&&"/"===t)return v=u,r;if("."===t&&j.length){for(;N(j););return v=h,r}if(";"===t||")"===t||"("===t){if(j.length)for(;N(j););return X(t),v=s,r+1}var a=2===j.length&&"="!==t;if(/[\w_\d\s]/.test(t)||a){for(;N(j););return v=s,r}return j.push(t),e=t,r+1}function N(t){for(var e,a,r=0;;){if(e=n.indexOf(t.slice(0,t.length+r).join("")),a=n[e],-1===e){if(r--+t.length>0)continue;a=t.slice(0,1).join("")}return X(a),T+=a.length,(j=j.slice(a.length)).length}}function $(){return/[^a-fA-F0-9]/.test(t)?(X(j.join("")),v=s,r):(j.push(t),e=t,r+1)}function z(){return"."===t||/[eE]/.test(t)?(j.push(t),v=h,e=t,r+1):"x"===t&&1===j.length&&"0"===j[0]?(v=k,j.push(t),e=t,r+1):/[^\d]/.test(t)?(X(j.join("")),v=s,r):(j.push(t),e=t,r+1)}function I(){return"f"===t&&(j.push(t),e=t,r+=1),/[eE]/.test(t)||"-"===t&&/[eE]/.test(e)?(j.push(t),e=t,r+1):/[^\d]/.test(t)?(X(j.join("")),v=s,r):(j.push(t),e=t,r+1)}function U(){if(/[^\d\w_]/.test(t)){var a=j.join("");return v=o.indexOf(a)>-1?g:i.indexOf(a)>-1?w:y,X(j.join("")),v=s,r}return j.push(t),e=t,r+1}}function v(t){var e=_(),a=[];return a=(a=a.concat(e(t))).concat(e(null))}function j(t){return v(t)}function x(t){return t.map((t=>"eof"!==t.type?t.data:"")).join("")}const D=["GL_OES_standard_derivatives","GL_EXT_frag_depth","GL_EXT_draw_buffers","GL_EXT_shader_texture_lod"];function R(t,e="100",a="300 es"){const r=/^\s*\#version\s+([0-9]+(\s+[a-zA-Z]+)?)\s*/;for(const n of t)if("preprocessor"===n.type){const t=r.exec(n.data);if(t){const r=t[1].replace(/\s\s+/g," ");if(r===a)return r;if(r===e)return n.data="#version "+a,e;throw new Error("unknown glsl version: "+r)}}return t.splice(0,0,{type:"preprocessor",data:"#version "+a},{type:"whitespace",data:"\n"}),null}function T(t,e){for(let a=e-1;a>=0;a--){const e=t[a];if("whitespace"!==e.type&&"block-comment"!==e.type){if("keyword"!==e.type)break;if("attribute"===e.data||"in"===e.data)return!0}}return!1}function A(t,e,a,r){r=r||a;for(const n of t)if("ident"===n.type&&n.data===a){r in e?e[r]++:e[r]=0;return A(t,e,r+"_"+e[r],r)}return a}function G(t,e,a="afterVersion"){function r(t,e){for(let a=e;a<t.length;a++){const e=t[a];if("operator"===e.type&&";"===e.data)return a}return null}function n(t){let e=-1,n=0,o=-1;for(let i=0;i<t.length;i++){const s=t[i];if("preprocessor"===s.type&&(s.data.match(/\#(if|ifdef|ifndef)\s+.+/)?++n:s.data.match(/\#endif\s*.*/)&&--n),"afterVersion"!==a&&"afterPrecision"!==a||"preprocessor"===s.type&&/^#version/.test(s.data)&&(o=Math.max(o,i)),"afterPrecision"===a&&"keyword"===s.type&&"precision"===s.data){const e=r(t,i);if(null===e)throw new Error("precision statement not followed by any semicolons!");o=Math.max(o,e)}e<o&&0===n&&(e=i)}return e+1}const o={data:"\n",type:"whitespace"},i=e=>e<t.length&&/[^\r\n]$/.test(t[e].data);let s=n(t);i(s-1)&&t.splice(s++,0,o);for(const c of e)t.splice(s++,0,c);i(s-1)&&i(s)&&t.splice(s,0,o)}function S(t,e,a,r="lowp"){G(t,[{type:"keyword",data:"out"},{type:"whitespace",data:" "},{type:"keyword",data:r},{type:"whitespace",data:" "},{type:"keyword",data:a},{type:"whitespace",data:" "},{type:"ident",data:e},{type:"operator",data:";"}],"afterPrecision")}function X(t,e,a,r,n="lowp"){G(t,[{type:"keyword",data:"layout"},{type:"operator",data:"("},{type:"keyword",data:"location"},{type:"whitespace",data:" "},{type:"operator",data:"="},{type:"whitespace",data:" "},{type:"integer",data:r.toString()},{type:"operator",data:")"},{type:"whitespace",data:" "},{type:"keyword",data:"out"},{type:"whitespace",data:" "},{type:"keyword",data:n},{type:"whitespace",data:" "},{type:"keyword",data:a},{type:"whitespace",data:" "},{type:"ident",data:e},{type:"operator",data:";"}],"afterPrecision")}function F(t,e){let a,r,n=-1;for(let o=e;o<t.length;o++){const e=t[o];if("operator"===e.type&&("["===e.data&&(a=o),"]"===e.data)){r=o;break}"integer"===e.type&&(n=parseInt(e.data,10))}return a&&r&&t.splice(a,r-a+1),n}function L(r,n){const o=H(r);if(t(o))return o;const i=j(r);if("300 es"===R(i,"100","300 es"))return r;let s=null,c=null;const p={},u={};for(let t=0;t<i.length;++t){const r=i[t];switch(r.type){case"keyword":n===e.VERTEX_SHADER&&"attribute"===r.data?r.data="in":"varying"===r.data&&(r.data=n===e.VERTEX_SHADER?"out":"in");break;case"builtin":if(/^texture(2D|Cube)(Proj)?(Lod|Grad)?(EXT)?$/.test(r.data.trim())&&(r.data=r.data.replace(/(2D|Cube|EXT)/g,"")),n===e.FRAGMENT_SHADER&&"gl_FragColor"===r.data&&(s||(s=A(i,p,"fragColor"),S(i,s,"vec4")),r.data=s),n===e.FRAGMENT_SHADER&&"gl_FragData"===r.data){const e=F(i,t+1),a=A(i,p,"fragData");X(i,a,"vec4",e,"mediump"),r.data=a}else n===e.FRAGMENT_SHADER&&"gl_FragDepthEXT"===r.data&&(c||(c=A(i,p,"gl_FragDepth")),r.data=c);break;case"ident":if(a.includes(r.data)){if(n===e.VERTEX_SHADER&&T(i,t))throw new Error("attribute in vertex shader uses a name that is a reserved word in glsl 300 es");r.data in u||(u[r.data]=A(i,p,r.data)),r.data=u[r.data]}}}for(let t=i.length-1;t>=0;--t){const e=i[t];if("preprocessor"===e.type){const a=e.data.match(/\#extension\s+(.*)\:/);if(a&&a[1]&&D.includes(a[1].trim())){const e=i[t+1];i.splice(t,e&&"whitespace"===e.type?2:1)}const r=e.data.match(/\#ifdef\s+(.*)/);r&&r[1]&&D.includes(r[1].trim())&&(e.data="#if 1");const n=e.data.match(/\#ifndef\s+(.*)/);n&&n[1]&&D.includes(n[1].trim())&&(e.data="#if 0")}}return M(r,x(i))}const C=new Map;function H(t){return r.enableCache?C.get(t):null}function M(t,e){return r.enableCache&&C.set(t,e),e}export{L as transpileShader};
|