UNPKG

@arcgis/core

Version:

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

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