/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import"../../../../core/has.js";import t from"../../../../core/Logger.js";import{debugFlags as r}from"../../support/debugFlags.js";import{webglDebugEnabled as e}from"../../../webgl/checkWebGLError.js";class i{constructor(r,i,s){this._context=r,this.locations=s,this._textures=new Map,this.source=e()?i:null,i.attributeNames.forEach(r=>{s.has(r)||t.getLogger("esri.views.3d.webgl-engine.lib.Program").error(`Missing VertexAttributeLocation for ${r} used in shader`)}),this._glProgram=r.programCache.get(i.generate("vertex",!0),i.generate("fragment",!0),s),this._glProgram.stop=()=>{throw new Error("Wrapped _glProgram used directly")},this.bind=i.generateBind(this),this.bindPass=i.generateBindPass(this),this.bindDraw=i.generateBindDraw(this)}get glName(){return this._glProgram.glName}get hasTransformFeedbackVaryings(){return this._glProgram.hasTransformFeedbackVaryings}get compiled(){return this._glProgram.compiled}setUniform1b(t,r){this._glProgram.setUniform1i(t,r?1:0)}setUniform1i(t,r){this._glProgram.setUniform1i(t,r)}setUniform1f(t,r,e){this._glProgram.setUniform1f(t,r,e)}setUniform2fv(t,r,e){this._glProgram.setUniform2fv(t,r,e)}setUniform3fv(t,r,e){this._glProgram.setUniform3fv(t,r,e)}setUniform4fv(t,r,e){this._glProgram.setUniform4fv(t,r,e)}setUniformMatrix3fv(t,r,e){this._glProgram.setUniformMatrix3fv(t,r,!1,e)}setUniformMatrix4fv(t,r,e){this._glProgram.setUniformMatrix4fv(t,r,!1,e)}setUniformMatrices4fv(t,r,e){this._glProgram.setUniformMatrices4fv(t,r,!1,e)}setUniform1fv(t,r,e){this._glProgram.setUniform1fv(t,r,e)}setUniform1iv(t,r){this._glProgram.setUniform1iv(t,r)}setUniform2iv(t,r){this._glProgram.setUniform2iv(t,r)}setUniform3iv(t,r){this._glProgram.setUniform3iv(t,r)}setUniform4iv(t,r){this._glProgram.setUniform4iv(t,r)}assertCompatibleVertexAttributeLocations(t,e){if(r.ENABLE_OPTIMIZATIONS)return;let i=t.locations;if(e){const t=new Map(i);e.forEach((r,e)=>t.set(e,i.size+r)),i=t}i.size!==this.locations.size&&console.error(`VertexAttributeLocations are incompatible: ${i}, ${this.locations}`),this.locations.forEach((t,r)=>{i.get(r)!==t&&console.error(`VertexAttributeLocations are incompatible: Program has ${r} at position ${t}, VAO has it at position ${i.get(r)}.`)})}stop(){this._textures.clear()}bindTexture(t,r,i){if(!r?.glName){const i=`Texture sampler ${t} in ${this._context.debugBoundTechnique} has no given Texture in ${(new Error).stack}`;e()&&console.error(i),r=this._context.emptyTexture}const s=this._ensureTextureUnit(t,r);this._context.useProgram(this);const o=s.unit;this.setUniform1i(t,o),this._context.bindTexture(r,o,!1,i)}_ensureTextureUnit(t,r){let e=this._textures.get(t);return null==e?(e={texture:r,unit:this._textures.size},this._textures.set(t,e)):e.texture=r,e}}export{i as Program};
