UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 6.84 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import"../../core/has.js";import{webglValidateShadersEnabled as t}from"./checkWebGLError.js";import{ShaderType as e,ResourceType as n}from"./enums.js";import{transpileShader as i}from"./ShaderTranspiler.js";const o=4294967295;class r{constructor(r,a,h,m,c=new Map,_=[]){this._context=r,this._locations=m,this._uniformBlockBindings=c,this._transformFeedbackVaryings=_,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,r||console.error("RenderingContext isn't initialized!"),0===a.length&&console.error("Shaders source should not be empty!"),a=i(a,e.VERTEX_SHADER),h=i(h,e.FRAGMENT_SHADER),this._vShader=s(this._context,e.VERTEX_SHADER,a),this._fShader=s(this._context,e.FRAGMENT_SHADER,h),f.enabled&&(this._linesOfCode=a.match(/\n/g).length+h.match(/\n/g).length+2,this._context.instanceCounter.increment(n.LinesOfCode,this._vShader,this._linesOfCode)),this._vShader&&this._fShader||console.error("Error loading shaders!"),this._context.instanceCounter.increment(n.Shader,this),t()&&(this.vertexShader=a,this.fragmentShader=h),this.usedMemory=a.length+h.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,e]of this._uniformBlockBindings){const n=l.getUniformBlockIndex(g,t);n<o&&l.uniformBlockBinding(g,n,e)}this._glName=g,this._context.instanceCounter.increment(n.Program,this)}get glName(){return this._glName}get hasGLName(){return null!=this._glName}get hasTransformFeedbackVaryings(){return!!this._transformFeedbackVaryings?.length}get compiled(){if(this._compiled)return!0;const t=this._context.gl.getExtension("KHR_parallel_shader_compile");return null==t||null==this.glName?(this._compiled=!0,!0):(this._compiled=!!this._context.gl.getProgramParameter(this.glName,t.COMPLETION_STATUS_KHR),this._compiled)}dispose(){if(--this._refCount>0)return;const t=this._context.gl,e=this._context.instanceCounter;this._nameToUniformLocation.forEach((t=>t&&e.decrement(n.Uniform,t))),this._nameToUniformLocation.clear(),this._vShader&&(this._linesOfCode>0&&(e.decrement(n.LinesOfCode,this._vShader,this._linesOfCode),this._linesOfCode=0),t.deleteShader(this._vShader),this._vShader=null,e.decrement(n.Shader,this)),this._fShader&&(t.deleteShader(this._fShader),this._fShader=null),this._glName&&(t.deleteProgram(this._glName),this._glName=null,e.decrement(n.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(n.Uniform,e),e}return null}hasUniform(t){return null!=this._getUniformLocation(t)}setUniform1i(t,e){const n=this._nameToUniform1.get(t);void 0!==n&&e===n||(this._context.gl.uniform1i(this._getUniformLocation(t),e),this._nameToUniform1.set(t,e))}setUniform1iv(t,e){m(this._nameToUniform1v,t,e)&&this._context.gl.uniform1iv(this._getUniformLocation(t),e)}setUniform2iv(t,e){m(this._nameToUniform2,t,e)&&this._context.gl.uniform2iv(this._getUniformLocation(t),e)}setUniform3iv(t,e){m(this._nameToUniform3,t,e)&&this._context.gl.uniform3iv(this._getUniformLocation(t),e)}setUniform4iv(t,e){m(this._nameToUniform4,t,e)&&this._context.gl.uniform4iv(this._getUniformLocation(t),e)}setUniform1f(t,e){const n=this._nameToUniform1.get(t);void 0!==n&&e===n||(this._context.gl.uniform1f(this._getUniformLocation(t),e),this._nameToUniform1.set(t,e))}setUniform1fv(t,e){m(this._nameToUniform1v,t,e)&&this._context.gl.uniform1fv(this._getUniformLocation(t),e)}setUniform2f(t,e,n){const i=this._nameToUniform2.get(t);void 0===i?(this._context.gl.uniform2f(this._getUniformLocation(t),e,n),this._nameToUniform2.set(t,[e,n])):e===i[0]&&n===i[1]||(this._context.gl.uniform2f(this._getUniformLocation(t),e,n),i[0]=e,i[1]=n)}setUniform2fv(t,e){m(this._nameToUniform2,t,e)&&this._context.gl.uniform2fv(this._getUniformLocation(t),e)}setUniform3f(t,e,n,i){const o=this._nameToUniform3.get(t);void 0===o?(this._context.gl.uniform3f(this._getUniformLocation(t),e,n,i),this._nameToUniform3.set(t,[e,n,i])):e===o[0]&&n===o[1]&&i===o[2]||(this._context.gl.uniform3f(this._getUniformLocation(t),e,n,i),o[0]=e,o[1]=n,o[2]=i)}setUniform3fv(t,e){const n=this._getUniformLocation(t);null!=n&&m(this._nameToUniform3,t,e)&&this._context.gl.uniform3fv(n,e)}setUniform4f(t,e,n,i,o){const r=this._nameToUniform4.get(t);void 0===r?(this._context.gl.uniform4f(this._getUniformLocation(t),e,n,i,o),this._nameToUniform4.set(t,[e,n,i,o])):void 0!==r&&e===r[0]&&n===r[1]&&i===r[2]&&o===r[3]||(this._context.gl.uniform4f(this._getUniformLocation(t),e,n,i,o),r[0]=e,r[1]=n,r[2]=i,r[3]=o)}setUniform4fv(t,e){const n=this._getUniformLocation(t);null!=n&&m(this._nameToUniform4,t,e)&&this._context.gl.uniform4fv(n,e)}setUniformMatrix3fv(t,e,n=!1){const i=this._getUniformLocation(t);null!=i&&m(this._nameToUniformMatrix3,t,e)&&this._context.gl.uniformMatrix3fv(i,n,e)}setUniformMatrix4fv(t,e,n=!1){const i=this._getUniformLocation(t);null!=i&&m(this._nameToUniformMatrix4,t,e)&&this._context.gl.uniformMatrix4fv(i,n,e)}stop(){}}function s(n,i,o){const r=n.gl,s=r.createShader(i);return r.shaderSource(s,o),r.compileShader(s),t()&&!r.getShaderParameter(s,r.COMPILE_STATUS)&&(console.error("Compile error in ".concat(i===e.VERTEX_SHADER?"vertex":"fragment"," shader")),console.error(r.getShaderInfoLog(s)),console.error(a(o))),s}function a(t){let e=2;return t.replaceAll("\n",(()=>"\n"+h(e++)+":"))}function h(t){return t>=1e3?t.toString():(" "+t).slice(-3)}function m(t,e,n){const i=t.get(e);if(!i)return t.set(e,Array.from(n)),!0;const o=n.length;if(i.length!==o)return t.set(e,Array.from(n)),!0;for(let r=0;r<o;++r){const t=n[r];if(i[r]!==t){for(i[r]=t;r<o;++r)i[r]=n[r];return!0}}return!1}const f={enabled:!1};export{r as Program,f as test};