UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 7.23 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import has from"../../core/has.js";import{webglValidateShadersEnabled as t,webglDebugEnabled as e}from"./checkWebGLError.js";import{ResourceType as o}from"./enums.js";import{transpileShader as i}from"./ShaderTranspiler.js";const n=4294967295,r=!!has("esri-tests-disable-gpu-memory-measurements");class s{constructor(e,s,m,h,f=new Map,l=[]){this._context=e,this._refCount=1,this._compiled=!1,this._linesOfCode=0,this._nameToUniformLocation=new Map,this._nameToUniform1=new Map,this._nameToUniform1v=new Map,this._nameToUniform2=new Map,this._nameToUniform3=new Map,this._nameToUniform4=new Map,this._nameToUniformMatrix3=new Map,this._nameToUniformMatrix4=new Map,e||console.error("RenderingContext isn't initialized!"),0===s.length&&console.error("Shaders source should not be empty!"),s=i(s,35633),m=i(m,35632),this._vShader=a(this._context,35633,s),this._fShader=a(this._context,35632,m),c.enabled&&(this._linesOfCode=s.match(/\n/g).length+m.match(/\n/g).length+2,e.instanceCounter.increment(o.LinesOfCode,this._vShader,this._linesOfCode)),this._vShader&&this._fShader||console.error("Error loading shaders!"),e.instanceCounter.increment(o.Shader,this),t()&&(this.vertexShader=s,this.fragmentShader=m),this.usedMemory=r?0:s.length+m.length;const _=this._context.gl,g=_.createProgram();_.attachShader(g,this._vShader),_.attachShader(g,this._fShader),h.forEach((t,e)=>_.bindAttribLocation(g,t,e)),this.hasTransformFeedbackVaryings=!!l?.length,this.hasTransformFeedbackVaryings&&_.transformFeedbackVaryings(g,l,_.SEPARATE_ATTRIBS),_.linkProgram(g),t()&&!_.getProgramParameter(g,_.LINK_STATUS)&&console.error(`Could not link shader\nvalidated: ${_.getProgramParameter(g,_.VALIDATE_STATUS)}, gl error ${_.getError()}, vertex: ${_.getShaderParameter(this._vShader,_.COMPILE_STATUS)}, fragment: ${_.getShaderParameter(this._fShader,_.COMPILE_STATUS)}, info log: ${_.getProgramInfoLog(g)}, vertex source: ${this.vertexShader}, fragment source: ${this.fragmentShader}`);for(const[t,o]of f){const e=_.getUniformBlockIndex(g,t);e<n&&_.uniformBlockBinding(g,e,o)}this._glName=g,e.instanceCounter.increment(o.Program,this)}get glName(){return this._glName}get hasGLName(){return null!=this._glName}get compiled(){return!!this._compiled||(this._context.capabilities.parallelShaderCompile&&null!=this.glName?(this._compiled=!!this._context.gl.getProgramParameter(this.glName,37297),this._compiled):(this._compiled=!0,!0))}dispose(){if(--this._refCount>0)return;const t=this._context.gl,e=this._context.instanceCounter;this._nameToUniformLocation.forEach(t=>t&&e.decrement(o.Uniform,t)),this._nameToUniformLocation.clear(),this._vShader&&(this._linesOfCode>0&&(e.decrement(o.LinesOfCode,this._vShader,this._linesOfCode),this._linesOfCode=0),t.deleteShader(this._vShader),this._vShader=null,e.decrement(o.Shader,this)),this._fShader&&(t.deleteShader(this._fShader),this._fShader=null),this._glName&&(t.deleteProgram(this._glName),this._glName=null,e.decrement(o.Program,this))}ref(){++this._refCount}_getUniformLocation(t){const e=this._nameToUniformLocation.get(t);if(void 0!==e)return e;if(this.glName){const e=this._context.gl.getUniformLocation(this.glName,t);return this._nameToUniformLocation.set(t,e),e&&this._context.instanceCounter.increment(o.Uniform,e),e}return null}hasUniform(t){return null!=this._getUniformLocation(t)}setUniform1i(t,e,o){l(o,e);const i=this._nameToUniform1.get(t);void 0!==i&&e===i||(this._context.gl.uniform1i(this._getUniformLocation(t),e),this._nameToUniform1.set(t,e))}setUniform1iv(t,e,o){_(o,e),f(this._nameToUniform1v,t,e)&&this._context.gl.uniform1iv(this._getUniformLocation(t),e)}setUniform2iv(t,e,o){_(o,e),f(this._nameToUniform2,t,e)&&this._context.gl.uniform2iv(this._getUniformLocation(t),e)}setUniform3iv(t,e,o){_(o,e),f(this._nameToUniform3,t,e)&&this._context.gl.uniform3iv(this._getUniformLocation(t),e)}setUniform4iv(t,e,o){_(o,e),f(this._nameToUniform4,t,e)&&this._context.gl.uniform4iv(this._getUniformLocation(t),e)}setUniform1f(t,e,o){l(o,e);const i=this._nameToUniform1.get(t);void 0!==i&&e===i||(this._context.gl.uniform1f(this._getUniformLocation(t),e),this._nameToUniform1.set(t,e))}setUniform1fv(t,e,o){_(o,e),f(this._nameToUniform1v,t,e)&&this._context.gl.uniform1fv(this._getUniformLocation(t),e)}setUniform2f(t,e,o,i){l(i,e,o);const n=this._nameToUniform2.get(t);void 0===n?(this._context.gl.uniform2f(this._getUniformLocation(t),e,o),this._nameToUniform2.set(t,[e,o])):e===n[0]&&o===n[1]||(this._context.gl.uniform2f(this._getUniformLocation(t),e,o),n[0]=e,n[1]=o)}setUniform2fv(t,e,o){_(o,e),f(this._nameToUniform2,t,e)&&this._context.gl.uniform2fv(this._getUniformLocation(t),e)}setUniform3f(t,e,o,i,n){l(n,e,o,i);const r=this._nameToUniform3.get(t);void 0===r?(this._context.gl.uniform3f(this._getUniformLocation(t),e,o,i),this._nameToUniform3.set(t,[e,o,i])):e===r[0]&&o===r[1]&&i===r[2]||(this._context.gl.uniform3f(this._getUniformLocation(t),e,o,i),r[0]=e,r[1]=o,r[2]=i)}setUniform3fv(t,e,o){_(o,e);const i=this._getUniformLocation(t);null!=i&&f(this._nameToUniform3,t,e)&&this._context.gl.uniform3fv(i,e)}setUniform4f(t,e,o,i,n,r){l(r,e,o,i,n);const s=this._nameToUniform4.get(t);void 0===s?(this._context.gl.uniform4f(this._getUniformLocation(t),e,o,i,n),this._nameToUniform4.set(t,[e,o,i,n])):void 0!==s&&e===s[0]&&o===s[1]&&i===s[2]&&n===s[3]||(this._context.gl.uniform4f(this._getUniformLocation(t),e,o,i,n),s[0]=e,s[1]=o,s[2]=i,s[3]=n)}setUniform4fv(t,e,o){_(o,e);const i=this._getUniformLocation(t);null!=i&&f(this._nameToUniform4,t,e)&&this._context.gl.uniform4fv(i,e)}setUniformMatrix3fv(t,e,o=!1,i){_(i,e);const n=this._getUniformLocation(t);null!=n&&f(this._nameToUniformMatrix3,t,e)&&this._context.gl.uniformMatrix3fv(n,o,e)}setUniformMatrix4fv(t,e,o=!1,i){_(i,e);const n=this._getUniformLocation(t);if(null!=n){const i=this._nameToUniformMatrix4,r=i.get(t);let s=!1;if(r){const o=e.length;if(r.length!==o)i.set(t,Array.from(e)),s=!0;else for(let t=0;t<o;++t){const i=e[t];if(r[t]!==i){for(r[t]=i;t<o;++t)r[t]=e[t];s=!0;break}}}else i.set(t,Array.from(e)),s=!0;s&&this._context.gl.uniformMatrix4fv(n,o,e)}}setUniformMatrices4fv(t,e,o=!1,i){_(i,e);const n=this._getUniformLocation(t);null!=n&&f(this._nameToUniformMatrix4,t,e)&&this._context.gl.uniformMatrix4fv(n,o,e)}stop(){}}function a(e,o,i){const n=e.gl,r=n.createShader(o);return n.shaderSource(r,i),n.compileShader(r),t()&&!n.getShaderParameter(r,n.COMPILE_STATUS)&&(console.error("Compile error in ".concat(35633===o?"vertex":"fragment"," shader")),console.error(n.getShaderInfoLog(r)),console.error(m(i))),r}function m(t){let e=2;return t.replaceAll("\n",()=>"\n"+h(e++)+":")}function h(t){return t>=1e3?t.toString():(" "+t).slice(-3)}function f(t,e,o){const i=t.get(e);if(!i)return t.set(e,Array.from(o)),!0;const n=o.length;if(i.length!==n)return t.set(e,Array.from(o)),!0;for(let r=0;r<n;++r){const t=o[r];if(i[r]!==t){for(i[r]=t;r<n;++r)i[r]=o[r];return!0}}return!1}const c={enabled:!1},l=e()?(t,...e)=>_(t,e):()=>{},_=e()?(t,e)=>{if(t?.supportsNaN)return;const o=e.length;for(let i=0;i<o;++i){const t=e[i];Number.isNaN(t)&&console.error(`Got ${t} as uniform value from ${(new Error).stack}`)}}:()=>{};export{s as Program,c as test};