UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) • 10.6 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import"../../core/has.js";import e from"./reservedWordsGLSL3.js";import{shaderTranspiler as t}from"./testUtils.js";const r=["precision","highp","mediump","lowp","attribute","const","uniform","varying","break","continue","do","for","while","if","else","in","out","inout","float","int","void","bool","true","false","discard","return","mat2","mat3","mat4","vec2","vec3","vec4","ivec2","ivec3","ivec4","uvec2","uvec3","uvec4","bvec2","bvec3","bvec4","sampler1D","sampler2D","sampler3D","usampler1D","usampler2D","usampler3D","samplerCube","sampler1DShadow","sampler2DShadow","struct","asm","class","union","enum","typedef","template","this","packed","goto","switch","default","inline","noinline","volatile","public","static","extern","external","interface","long","short","double","half","fixed","unsigned","input","output","hvec2","hvec3","hvec4","dvec2","dvec3","dvec4","fvec2","fvec3","fvec4","sampler2DRect","sampler3DRect","sampler2DRectShadow","sizeof","cast","namespace","using"],a=["<<=",">>=","++","--","<<",">>","<=",">=","==","!=","&&","||","+=","-=","*=","/=","%=","&=","^^","^=","|=","(",")","[","]",".","!","~","*","/","%","+","-","<",">","&","^","|","?",":","=",",",";","{","}"],o=["abs","acos","all","any","asin","atan","ceil","clamp","cos","cross","dFdx","dFdy","degrees","distance","dot","equal","exp","exp2","faceforward","floor","fract","gl_BackColor","gl_BackLightModelProduct","gl_BackLightProduct","gl_BackMaterial","gl_BackSecondaryColor","gl_ClipPlane","gl_ClipVertex","gl_Color","gl_DepthRange","gl_DepthRangeParameters","gl_EyePlaneQ","gl_EyePlaneR","gl_EyePlaneS","gl_EyePlaneT","gl_Fog","gl_FogCoord","gl_FogFragCoord","gl_FogParameters","gl_FragColor","gl_FragCoord","gl_FragData","gl_FragDepth","gl_FragDepthEXT","gl_FrontColor","gl_FrontFacing","gl_FrontLightModelProduct","gl_FrontLightProduct","gl_FrontMaterial","gl_FrontSecondaryColor","gl_LightModel","gl_LightModelParameters","gl_LightModelProducts","gl_LightProducts","gl_LightSource","gl_LightSourceParameters","gl_MaterialParameters","gl_MaxClipPlanes","gl_MaxCombinedTextureImageUnits","gl_MaxDrawBuffers","gl_MaxFragmentUniformComponents","gl_MaxLights","gl_MaxTextureCoords","gl_MaxTextureImageUnits","gl_MaxTextureUnits","gl_MaxVaryingFloats","gl_MaxVertexAttribs","gl_MaxVertexTextureImageUnits","gl_MaxVertexUniformComponents","gl_ModelViewMatrix","gl_ModelViewMatrixInverse","gl_ModelViewMatrixInverseTranspose","gl_ModelViewMatrixTranspose","gl_ModelViewProjectionMatrix","gl_ModelViewProjectionMatrixInverse","gl_ModelViewProjectionMatrixInverseTranspose","gl_ModelViewProjectionMatrixTranspose","gl_MultiTexCoord0","gl_MultiTexCoord1","gl_MultiTexCoord2","gl_MultiTexCoord3","gl_MultiTexCoord4","gl_MultiTexCoord5","gl_MultiTexCoord6","gl_MultiTexCoord7","gl_Normal","gl_NormalMatrix","gl_NormalScale","gl_ObjectPlaneQ","gl_ObjectPlaneR","gl_ObjectPlaneS","gl_ObjectPlaneT","gl_Point","gl_PointCoord","gl_PointParameters","gl_PointSize","gl_Position","gl_ProjectionMatrix","gl_ProjectionMatrixInverse","gl_ProjectionMatrixInverseTranspose","gl_ProjectionMatrixTranspose","gl_SecondaryColor","gl_TexCoord","gl_TextureEnvColor","gl_TextureMatrix","gl_TextureMatrixInverse","gl_TextureMatrixInverseTranspose","gl_TextureMatrixTranspose","gl_Vertex","greaterThan","greaterThanEqual","inversesqrt","length","lessThan","lessThanEqual","log","log2","matrixCompMult","max","min","mix","mod","normalize","not","notEqual","pow","radians","reflect","refract","sign","sin","smoothstep","sqrt","step","tan","texture2D","texture2DLod","texture2DProj","texture2DProjLod","textureCube","textureCubeLod","texture2DLodEXT","texture2DProjLodEXT","textureCubeLodEXT","texture2DGradEXT","texture2DProjGradEXT","textureCubeGradEXT","textureSize","texelFetch"];var n=999,i=9999,l=0,s=1,c=2,u=3,d=4,g=5,p=6,f=7,_=8,h=9,x=10,m=11,y=["block-comment","line-comment","preprocessor","operator","integer","float","ident","builtin","keyword","whitespace","eof","integer"];function w(){var e,t,w,M=0,v=0,b=n,T=[],P=[],C=1,k=0,j=0,D=!1,E=!1,F="";return function(e){return P=[],null!==e?S(e.replace?e.replace(/\r\n/g,"\n"):e):V()};function L(e){e.length&&P.push({type:y[b],data:e,position:j,line:C,column:k})}function S(t){var r;for(M=0,w=(F+=t).length;e=F[M],M<w;){switch(r=M,b){case l:M=R();break;case s:M=O();break;case c:M=G();break;case u:M=U();break;case d:M=z();break;case m:M=B();break;case g:M=A();break;case i:M=N();break;case h:M=I();break;case n:M=X()}if(r!==M)if("\n"===F[r])k=0,++C;else++k}return v+=M,F=F.slice(M),P}function V(e){return T.length&&L(T.join("")),b=x,L("(eof)"),P}function X(){return T=T.length?[]:T,"/"===t&&"*"===e?(j=v+M-1,b=l,t=e,M+1):"/"===t&&"/"===e?(j=v+M-1,b=s,t=e,M+1):"#"===e?(b=c,j=v+M,M):/\s/.test(e)?(b=h,j=v+M,M):(D=/\d/.test(e),E=/[^\w_]/.test(e),j=v+M,b=D?d:E?u:i,M)}function I(){return/[^\s]/g.test(e)?(L(T.join("")),b=n,M):(T.push(e),t=e,M+1)}function G(){return"\r"!==e&&"\n"!==e||"\\"===t?(T.push(e),t=e,M+1):(L(T.join("")),b=n,M)}function O(){return G()}function R(){return"/"===e&&"*"===t?(T.push(e),L(T.join("")),b=n,M+1):(T.push(e),t=e,M+1)}function U(){if("."===t&&/\d/.test(e))return b=g,M;if("/"===t&&"*"===e)return b=l,M;if("/"===t&&"/"===e)return b=s,M;if("."===e&&T.length){for(;q(T););return b=g,M}if(";"===e||")"===e||"("===e){if(T.length)for(;q(T););return L(e),b=n,M+1}var r=2===T.length&&"="!==e;if(/[\w_\d\s]/.test(e)||r){for(;q(T););return b=n,M}return T.push(e),t=e,M+1}function q(e){for(var t,r,o=0;;){if(t=a.indexOf(e.slice(0,e.length+o).join("")),r=a[t],-1===t){if(o--+e.length>0)continue;r=e.slice(0,1).join("")}return L(r),j+=r.length,(T=T.slice(r.length)).length}}function B(){return/[^a-fA-F0-9]/.test(e)?(L(T.join("")),b=n,M):(T.push(e),t=e,M+1)}function z(){return"."===e||/[eE]/.test(e)?(T.push(e),b=g,t=e,M+1):"x"===e&&1===T.length&&"0"===T[0]?(b=m,T.push(e),t=e,M+1):/[^\d]/.test(e)?(L(T.join("")),b=n,M):(T.push(e),t=e,M+1)}function A(){return"f"===e&&(T.push(e),t=e,M+=1),/[eE]/.test(e)||"-"===e&&/[eE]/.test(t)?(T.push(e),t=e,M+1):/[^\d]/.test(e)?(L(T.join("")),b=n,M):(T.push(e),t=e,M+1)}function N(){if(/[^\d\w_]/.test(e)){var a=T.join("");return b=r.indexOf(a)>-1?_:o.indexOf(a)>-1?f:p,L(T.join("")),b=n,M}return T.push(e),t=e,M+1}}function M(e){var t=w(),r=[];return r=(r=r.concat(t(e))).concat(t(null))}function v(e){return M(e)}function b(e){return e.map(e=>"eof"!==e.type?e.data:"").join("")}const T=new Set(["GL_OES_standard_derivatives","GL_EXT_frag_depth","GL_EXT_draw_buffers","GL_EXT_shader_texture_lod"]);function P(e,t="100",r="300 es"){const a=/^\s*#version\s+([0-9]+(\s+[a-zA-Z]+)?)\s*/;for(const o of e)if("preprocessor"===o.type){const e=a.exec(o.data);if(e){const a=e[1].replaceAll(/\s{2,}/g," ");if(a===r)return a;if(a===t)return o.data="#version "+r,t;throw new Error("unknown glsl version: "+a)}}return e.splice(0,0,{type:"preprocessor",data:"#version "+r},{type:"whitespace",data:"\n"}),null}function C(e,t){for(let r=t-1;r>=0;r--){const t=e[r];if("whitespace"!==t.type&&"block-comment"!==t.type){if("keyword"!==t.type)break;if("attribute"===t.data||"in"===t.data)return!0}}return!1}function k(e,t,r,a){a=a||r;for(const o of e)if("ident"===o.type&&o.data===r){a in t?t[a]++:t[a]=0;return k(e,t,a+"_"+t[a],a)}return r}function j(e,t,r="afterVersion"){function a(e,t){for(let r=t;r<e.length;r++){const t=e[r];if("operator"===t.type&&";"===t.data)return r}return null}function o(e){let t=-1,o=0,n=-1;for(let i=0;i<e.length;i++){const l=e[i];if("preprocessor"===l.type&&(/#(if|ifdef|ifndef)\s+.+/.test(l.data)?++o:/#endif\s*.*/.test(l.data)&&--o),"afterVersion"!==r&&"afterPrecision"!==r||"preprocessor"===l.type&&l.data.startsWith("#version")&&(n=Math.max(n,i)),"afterPrecision"===r&&"keyword"===l.type&&"precision"===l.data){const t=a(e,i);if(null===t)throw new Error("precision statement not followed by any semicolons!");n=Math.max(n,t)}t<n&&0===o&&(t=i)}return t+1}const n={data:"\n",type:"whitespace"},i=t=>t<e.length&&/[^\r\n]$/.test(e[t].data);let l=o(e);i(l-1)&&e.splice(l++,0,n);for(const s of t)e.splice(l++,0,s);i(l-1)&&i(l)&&e.splice(l,0,n)}function D(e,t,r,a="lowp"){j(e,[{type:"keyword",data:"out"},{type:"whitespace",data:" "},{type:"keyword",data:a},{type:"whitespace",data:" "},{type:"keyword",data:r},{type:"whitespace",data:" "},{type:"ident",data:t},{type:"operator",data:";"}],"afterPrecision")}function E(e,t,r,a,o="lowp"){j(e,[{type:"keyword",data:"layout"},{type:"operator",data:"("},{type:"keyword",data:"location"},{type:"whitespace",data:" "},{type:"operator",data:"="},{type:"whitespace",data:" "},{type:"integer",data:a.toString()},{type:"operator",data:")"},{type:"whitespace",data:" "},{type:"keyword",data:"out"},{type:"whitespace",data:" "},{type:"keyword",data:o},{type:"whitespace",data:" "},{type:"keyword",data:r},{type:"whitespace",data:" "},{type:"ident",data:t},{type:"operator",data:";"}],"afterPrecision")}function F(e,t){let r,a,o=-1;for(let n=t;n<e.length;n++){const t=e[n];if("operator"===t.type&&("["===t.data&&(r=n),"]"===t.data)){a=n;break}"integer"===t.type&&(o=parseInt(t.data,10))}return r&&a&&e.splice(r,a-r+1),o}function L(t,r){if(t.startsWith("#version 300"))return t;const a=V(t);if(null!=a)return a;const o=v(t);if("300 es"===P(o,"100","300 es"))return t;let n=null,i=null;const l={},s={};for(let c=0;c<o.length;++c){const t=o[c];switch(t.type){case"keyword":35633===r&&"attribute"===t.data?t.data="in":"varying"===t.data&&(t.data=35633===r?"out":"in");break;case"builtin":if(/^texture(2D|Cube)(Proj)?(Lod|Grad)?(EXT)?$/.test(t.data.trim())&&(t.data=t.data.replaceAll(/(2D|Cube|EXT)/g,"")),35632===r&&"gl_FragColor"===t.data&&(n||(n=k(o,l,"fragColor"),D(o,n,"vec4")),t.data=n),35632===r&&"gl_FragData"===t.data){const e=F(o,c+1),r=k(o,l,"fragData");E(o,r,"vec4",e,"mediump"),t.data=r}else 35632===r&&"gl_FragDepthEXT"===t.data&&(i||(i=k(o,l,"gl_FragDepth")),t.data=i);break;case"ident":if(e.includes(t.data)){if(35633===r&&C(o,c))throw new Error("attribute in vertex shader uses a name that is a reserved word in glsl 300 es");t.data in s||(s[t.data]=k(o,l,t.data)),t.data=s[t.data]}}}for(let e=o.length-1;e>=0;--e){const t=o[e];if("preprocessor"===t.type){const r=t.data.match(/#extension\s+(.*):/);if(r?.[1]&&T.has(r[1].trim())){const t=o[e+1];o.splice(e,t&&"whitespace"===t.type?2:1)}const a=t.data.match(/#ifdef\s+(.*)/);a?.[1]&&T.has(a[1].trim())&&(t.data="#if 1");const n=t.data.match(/#ifndef\s+(.*)/);n?.[1]&&T.has(n[1].trim())&&(t.data="#if 0")}}return X(t,b(o))}const S=new Map;function V(e){return t.enableCache?S.get(e):null}function X(e,r){return t.enableCache&&S.set(e,r),r}export{L as transpileShader};