12345 |
- /*
- All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- See https://js.arcgis.com/4.25/esri/copyright.txt for details.
- */
- import{update as t}from"../../core/arrayUtils.js";import"../../core/has.js";import{isSome as o,isNone as i}from"../../core/maybe.js";import{webglValidateShadersEnabled as e}from"./checkWebGLError.js";import{ContextType as n}from"./context-util.js";import{ShaderType as r,ResourceType as s}from"./enums.js";import{transpileShader as m}from"./ShaderTranspiler.js";const a=4294967295;class h{constructor(t,o,i,a,h=new Map){this._context=t,this._locations=a,this._uniformBlockBindings=h,this._refCount=1,this._compiled=!1,this._nameToUniformLocation={},this._nameToUniform1={},this._nameToUniform1v=new Map,this._nameToUniform2=new Map,this._nameToUniform3=new Map,this._nameToUniform4=new Map,this._nameToUniformMatrix3=new Map,this._nameToUniformMatrix4=new Map,t||console.error("RenderingContext isn't initialized!"),0===o.length&&console.error("Shaders source should not be empty!"),this._context.type===n.WEBGL2&&(o=m(o,r.VERTEX_SHADER),i=m(i,r.FRAGMENT_SHADER)),this._vShader=f(this._context,r.VERTEX_SHADER,o),this._fShader=f(this._context,r.FRAGMENT_SHADER,i),this._vShader&&this._fShader||console.error("Error loading shaders!"),this._context.instanceCounter.increment(s.Shader,this),e()&&(this.vertexShader=o,this.fragmentShader=i)}get glName(){if(o(this._glName))return this._glName;if(i(this._vShader))return null;const t=this._context.gl,r=t.createProgram();if(t.attachShader(r,this._vShader),t.attachShader(r,this._fShader),this._locations.forEach(((o,i)=>t.bindAttribLocation(r,o,i))),t.linkProgram(r),e()&&!t.getProgramParameter(r,t.LINK_STATUS)&&console.error(`Could not link shader\nvalidated: ${t.getProgramParameter(r,t.VALIDATE_STATUS)}, gl error ${t.getError()}, vertex: ${t.getShaderParameter(this._vShader,t.COMPILE_STATUS)}, fragment: ${t.getShaderParameter(this._fShader,t.COMPILE_STATUS)}, info log: ${t.getProgramInfoLog(r)}, vertex source: ${this.vertexShader}, fragment source: ${this.fragmentShader}`),this._context.type===n.WEBGL2){const o=t;for(const[t,i]of this._uniformBlockBindings){const e=o.getUniformBlockIndex(r,t);e<a&&o.uniformBlockBinding(r,e,i)}}return this._glName=r,this._context.instanceCounter.increment(s.Program,this),r}get hasGLName(){return o(this._glName)}get isCompiled(){if(this._compiled)return!0;const t=this._context.gl.getExtension("KHR_parallel_shader_compile");return null==t?(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;this._vShader&&(t.deleteShader(this._vShader),this._vShader=null,this._context.instanceCounter.decrement(s.Shader,this)),this._fShader&&(t.deleteShader(this._fShader),this._fShader=null),this._glName&&(t.deleteProgram(this._glName),this._glName=null,this._context.instanceCounter.decrement(s.Program,this))}ref(){++this._refCount}_getUniformLocation(t){return void 0===this._nameToUniformLocation[t]&&(++l.numUniforms,this._nameToUniformLocation[t]=this._context.gl.getUniformLocation(this.glName,t)),this._nameToUniformLocation[t]}hasUniform(t){return null!==this._getUniformLocation(t)}setUniform1i(t,o){const i=this._nameToUniform1[t];void 0!==i&&o===i||(this._context.gl.uniform1i(this._getUniformLocation(t),o),this._nameToUniform1[t]=o)}setUniform1iv(t,o){g(this._nameToUniform1v,t,o)&&this._context.gl.uniform1iv(this._getUniformLocation(t),o)}setUniform2iv(t,o){g(this._nameToUniform2,t,o)&&this._context.gl.uniform2iv(this._getUniformLocation(t),o)}setUniform3iv(t,o){g(this._nameToUniform3,t,o)&&this._context.gl.uniform3iv(this._getUniformLocation(t),o)}setUniform4iv(t,o){g(this._nameToUniform4,t,o)&&this._context.gl.uniform4iv(this._getUniformLocation(t),o)}setUniform1f(t,o){const i=this._nameToUniform1[t];void 0!==i&&o===i||(this._context.gl.uniform1f(this._getUniformLocation(t),o),this._nameToUniform1[t]=o)}setUniform1fv(t,o){g(this._nameToUniform1v,t,o)&&this._context.gl.uniform1fv(this._getUniformLocation(t),o)}setUniform2f(t,o,i){const e=this._nameToUniform2.get(t);void 0===e?(this._context.gl.uniform2f(this._getUniformLocation(t),o,i),this._nameToUniform2.set(t,[o,i])):o===e[0]&&i===e[1]||(this._context.gl.uniform2f(this._getUniformLocation(t),o,i),e[0]=o,e[1]=i)}setUniform2fv(t,o){g(this._nameToUniform2,t,o)&&this._context.gl.uniform2fv(this._getUniformLocation(t),o)}setUniform3f(t,o,i,e){const n=this._nameToUniform3.get(t);void 0===n?(this._context.gl.uniform3f(this._getUniformLocation(t),o,i,e),this._nameToUniform3[t]=[o,i,e]):o===n[0]&&i===n[1]&&e===n[2]||(this._context.gl.uniform3f(this._getUniformLocation(t),o,i,e),n[0]=o,n[1]=i,n[2]=e)}setUniform3fv(t,o){g(this._nameToUniform3,t,o)&&this._context.gl.uniform3fv(this._getUniformLocation(t),o)}setUniform4f(t,o,i,e,n){const r=this._nameToUniform4.get(t);void 0===r?(this._context.gl.uniform4f(this._getUniformLocation(t),o,i,e,n),this._nameToUniform4.set(t,[o,i,e,n])):void 0!==r&&o===r[0]&&i===r[1]&&e===r[2]&&n===r[3]||(this._context.gl.uniform4f(this._getUniformLocation(t),o,i,e,n),r[0]=o,r[1]=i,r[2]=e,r[3]=n)}setUniform4fv(t,o){g(this._nameToUniform4,t,o)&&this._context.gl.uniform4fv(this._getUniformLocation(t),o)}setUniformMatrix3fv(t,o,i=!1){g(this._nameToUniformMatrix3,t,o)&&this._context.gl.uniformMatrix3fv(this._getUniformLocation(t),i,o)}setUniformMatrix4fv(t,o,i=!1){g(this._nameToUniformMatrix4,t,o)&&this._context.gl.uniformMatrix4fv(this._getUniformLocation(t),i,o)}stop(){}}function f(t,o,i){const n=t.gl,s=n.createShader(o);return n.shaderSource(s,i),n.compileShader(s),e()&&!n.getShaderParameter(s,n.COMPILE_STATUS)&&(console.error("Compile error in ".concat(o===r.VERTEX_SHADER?"vertex":"fragment"," shader")),console.error(n.getShaderInfoLog(s)),console.error(_(i))),l.enabled&&(l.compiledLOC+=i.match(/\n/g).length+1),s}function _(t){let o=2;return t.replace(/\n/g,(()=>"\n"+c(o++)+":"))}function c(t){return t>=1e3?t.toString():(" "+t).slice(-3)}function g(o,i,e){const n=o.get(i);return n?t(n,e):(o.set(i,Array.from(e)),!0)}const l={compiledLOC:0,numUniforms:0,enabled:!1};export{h as Program,l as test};
|