UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 6.03 kB
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,h,c=new Map){this._context=t,this._locations=h,this._uniformBlockBindings=c,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);const _=this._context.gl,g=_.createProgram();if(_.attachShader(g,this._vShader),_.attachShader(g,this._fShader),this._locations.forEach(((t,o)=>_.bindAttribLocation(g,t,o))),_.linkProgram(g),e()&&!_.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}`),this._context.type===n.WEBGL2){const t=_;for(const[o,i]of this._uniformBlockBindings){const e=t.getUniformBlockIndex(g,o);e<a&&t.uniformBlockBinding(g,e,i)}}this._glName=g,this._context.instanceCounter.increment(s.Program,this)}get glName(){return this._glName}get hasGLName(){return o(this._glName)}get compiled(){if(this._compiled)return!0;const t=this._context.gl.getExtension("KHR_parallel_shader_compile");return null==t||i(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;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]&&o(this.glName)&&(++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(c(i))),l.enabled&&(l.compiledLOC+=i.match(/\n/g).length+1),s}function c(t){let o=2;return t.replace(/\n/g,(()=>"\n"+_(o++)+":"))}function _(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};