@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 7.11 kB
JavaScript
/*
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{webglValidateShadersEnabled as t,webglDebugEnabled as e}from"./checkWebGLError.js";import{ShaderType as o,ResourceType as i,Extension as n}from"./enums.js";import{transpileShader as r}from"./ShaderTranspiler.js";const s=4294967295;class a{constructor(e,n,a,m,f=new Map,c=[]){this._context=e,this._locations=m,this._uniformBlockBindings=f,this._transformFeedbackVaryings=c,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===n.length&&console.error("Shaders source should not be empty!"),n=r(n,o.VERTEX_SHADER),a=r(a,o.FRAGMENT_SHADER),this._vShader=h(this._context,o.VERTEX_SHADER,n),this._fShader=h(this._context,o.FRAGMENT_SHADER,a),_.enabled&&(this._linesOfCode=n.match(/\n/g).length+a.match(/\n/g).length+2,this._context.instanceCounter.increment(i.LinesOfCode,this._vShader,this._linesOfCode)),this._vShader&&this._fShader||console.error("Error loading shaders!"),this._context.instanceCounter.increment(i.Shader,this),t()&&(this.vertexShader=n,this.fragmentShader=a),this.usedMemory=n.length+a.length;const l=this._context.gl,g=l.createProgram();l.attachShader(g,this._vShader),l.attachShader(g,this._fShader),this._locations.forEach(((t,e)=>l.bindAttribLocation(g,t,e))),this._transformFeedbackVaryings?.length&&l.transformFeedbackVaryings(g,this._transformFeedbackVaryings,l.SEPARATE_ATTRIBS),l.linkProgram(g),t()&&!l.getProgramParameter(g,l.LINK_STATUS)&&console.error(`Could not link shader\nvalidated: ${l.getProgramParameter(g,l.VALIDATE_STATUS)}, gl error ${l.getError()}, vertex: ${l.getShaderParameter(this._vShader,l.COMPILE_STATUS)}, fragment: ${l.getShaderParameter(this._fShader,l.COMPILE_STATUS)}, info log: ${l.getProgramInfoLog(g)}, vertex source: ${this.vertexShader}, fragment source: ${this.fragmentShader}`);for(const[t,o]of this._uniformBlockBindings){const e=l.getUniformBlockIndex(g,t);e<s&&l.uniformBlockBinding(g,e,o)}this._glName=g,this._context.instanceCounter.increment(i.Program,this)}get glName(){return this._glName}get hasGLName(){return null!=this._glName}get hasTransformFeedbackVaryings(){return!!this._transformFeedbackVaryings?.length}get compiled(){return!!this._compiled||(this._context.capabilities.parallelShaderCompile&&null!=this.glName?(this._compiled=!!this._context.gl.getProgramParameter(this.glName,n.COMPLETION_STATUS_KHR),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(i.Uniform,t))),this._nameToUniformLocation.clear(),this._vShader&&(this._linesOfCode>0&&(e.decrement(i.LinesOfCode,this._vShader,this._linesOfCode),this._linesOfCode=0),t.deleteShader(this._vShader),this._vShader=null,e.decrement(i.Shader,this)),this._fShader&&(t.deleteShader(this._fShader),this._fShader=null),this._glName&&(t.deleteProgram(this._glName),this._glName=null,e.decrement(i.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(i.Uniform,e),e}return null}hasUniform(t){return null!=this._getUniformLocation(t)}setUniform1i(t,e){l(e);const o=this._nameToUniform1.get(t);void 0!==o&&e===o||(this._context.gl.uniform1i(this._getUniformLocation(t),e),this._nameToUniform1.set(t,e))}setUniform1iv(t,e){g(e),c(this._nameToUniform1v,t,e)&&this._context.gl.uniform1iv(this._getUniformLocation(t),e)}setUniform2iv(t,e){g(e),c(this._nameToUniform2,t,e)&&this._context.gl.uniform2iv(this._getUniformLocation(t),e)}setUniform3iv(t,e){g(e),c(this._nameToUniform3,t,e)&&this._context.gl.uniform3iv(this._getUniformLocation(t),e)}setUniform4iv(t,e){g(e),c(this._nameToUniform4,t,e)&&this._context.gl.uniform4iv(this._getUniformLocation(t),e)}setUniform1f(t,e){l(e);const o=this._nameToUniform1.get(t);void 0!==o&&e===o||(this._context.gl.uniform1f(this._getUniformLocation(t),e),this._nameToUniform1.set(t,e))}setUniform1fv(t,e){g(e),c(this._nameToUniform1v,t,e)&&this._context.gl.uniform1fv(this._getUniformLocation(t),e)}setUniform2f(t,e,o){l(e,o);const i=this._nameToUniform2.get(t);void 0===i?(this._context.gl.uniform2f(this._getUniformLocation(t),e,o),this._nameToUniform2.set(t,[e,o])):e===i[0]&&o===i[1]||(this._context.gl.uniform2f(this._getUniformLocation(t),e,o),i[0]=e,i[1]=o)}setUniform2fv(t,e){g(e),c(this._nameToUniform2,t,e)&&this._context.gl.uniform2fv(this._getUniformLocation(t),e)}setUniform3f(t,e,o,i){l(e,o,i);const n=this._nameToUniform3.get(t);void 0===n?(this._context.gl.uniform3f(this._getUniformLocation(t),e,o,i),this._nameToUniform3.set(t,[e,o,i])):e===n[0]&&o===n[1]&&i===n[2]||(this._context.gl.uniform3f(this._getUniformLocation(t),e,o,i),n[0]=e,n[1]=o,n[2]=i)}setUniform3fv(t,e){g(e);const o=this._getUniformLocation(t);null!=o&&c(this._nameToUniform3,t,e)&&this._context.gl.uniform3fv(o,e)}setUniform4f(t,e,o,i,n){l(e,o,i,n);const r=this._nameToUniform4.get(t);void 0===r?(this._context.gl.uniform4f(this._getUniformLocation(t),e,o,i,n),this._nameToUniform4.set(t,[e,o,i,n])):void 0!==r&&e===r[0]&&o===r[1]&&i===r[2]&&n===r[3]||(this._context.gl.uniform4f(this._getUniformLocation(t),e,o,i,n),r[0]=e,r[1]=o,r[2]=i,r[3]=n)}setUniform4fv(t,e){g(e);const o=this._getUniformLocation(t);null!=o&&c(this._nameToUniform4,t,e)&&this._context.gl.uniform4fv(o,e)}setUniformMatrix3fv(t,e,o=!1){g(e);const i=this._getUniformLocation(t);null!=i&&c(this._nameToUniformMatrix3,t,e)&&this._context.gl.uniformMatrix3fv(i,o,e)}setUniformMatrix4fv(t,e,o=!1){g(e);const i=this._getUniformLocation(t);null!=i&&c(this._nameToUniformMatrix4,t,e)&&this._context.gl.uniformMatrix4fv(i,o,e)}stop(){}}function h(e,i,n){const r=e.gl,s=r.createShader(i);return r.shaderSource(s,n),r.compileShader(s),t()&&!r.getShaderParameter(s,r.COMPILE_STATUS)&&(console.error("Compile error in ".concat(i===o.VERTEX_SHADER?"vertex":"fragment"," shader")),console.error(r.getShaderInfoLog(s)),console.error(m(n))),s}function m(t){let e=2;return t.replaceAll("\n",(()=>"\n"+f(e++)+":"))}function f(t){return t>=1e3?t.toString():(" "+t).slice(-3)}function c(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 _={enabled:!1},l=e()?(...t)=>g(t):()=>{},g=e()?t=>{const e=t.length;for(let o=0;o<e;++o){const e=t[o];Number.isNaN(e)&&console.error(`Got ${e} as uniform value from ${(new Error).stack}`)}}:()=>{};export{a as Program,_ as test};