/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{disposeMaybe as e}from"../../../../../../../../core/maybe.js";import{tileSize as t}from"../../../../definitions.js";import{Technique as r}from"../../Technique.js";import{createNewFBO as n,createProcessedTexture as s}from"./textureUtils.js";import{createColormapTexture as o}from"../../../../../../../webgl/rasterUtils.js";class i extends r{shutdown(e){super.shutdown(e),this._fbo?.dispose(),this._fbo=void 0}render(r,i){const{rasterFunction:a}=r;if(!a)return;const{context:u}=r,m="indexedColormap"in a.parameters?o(u,a.parameters.indexedColormap):void 0,p="Reproject"===a.name,d=u.getBoundFramebufferObject(),c=u.getViewport();for(const o of i.bitmaps){const d=p?!(o.rasterTexture&&o.projected):!o.processed;if(!o.source||!d||o.suspended)continue;r.timeline.begin(this.name);const{painter:c}=r;c.setPipelineState({depth:!1,stencil:{test:!1,write:!1},color:{write:[!0,!0,!0,!0],blendMode:"custom",blendParameters:{srcRGB:1,dstRGB:0,srcAlpha:1,dstAlpha:0}}}),p||(o.processedTexture=e(o.processedTexture)),o.updateTexture(r);const[l,g]=o.getRasterTextureSize(p),f=l===t&&g===t,h=f?i.processorFbo:n(u,l,g);u.bindFramebuffer(h),u.setViewport(0,0,h.width,h.height),this._process(r,o,m);const x=s(r.context,l,g);if(h.copyToTexture(0,0,l,g,0,0,x),p)o.rasterTexture=x;else{const e=r.hasBranches?a.id:0;o.functionTextures[e]?.dispose(),o.functionTextures[e]=x}f||h.dispose(),r.timeline.end(this.name)}m?.dispose(),u.bindFramebuffer(d),u.setViewport(c.x,c.y,c.width,c.height)}_getCommonConfig(e,t){const{rasterFunction:r,hasBranches:n}=e,{raster:s,rasters:o}=r.parameters,i=n?s?.id??o?.find(e=>"Constant"!==e.name)?.id??-1:0,a=t.functionTextures[i]??t.rasterTexture,u="Reproject"===r.name;return{texture:{texture:a,unit:0},srcImageSize:t.getRasterTextureSize(u)}}_getMultipleInputConfig(e,t){return t?.length?2===t.length?{twoRasterConfig:this._getTwoInputConfig(t,e)}:3===t.length?{threeRasterConfig:this._getThreeInputConfig(t,e)}:{}:{}}_getConstantCount(e){return e?.filter(e=>"Constant"===e.name).length??0}_getTextures(e,t){return e.filter(e=>"Constant"!==e.name).map(e=>null!=e.id&&"Identity"!==e.name?t.functionTextures[e.id]:t.rasterTexture)}_getTwoInputConfig(e,t){const r=this._getTextures(e,t),n=r[1]?{texture:r[1],unit:1}:void 0,s=e.findIndex(e=>"Constant"===e.name),o=0===s?new Float32Array([0,1,0,1,0,0,0,0,0]):new Float32Array([1,0,0,0,1,0,0,0,0]);return{image1:n,image1Const:s>-1?e[s].parameters.value:0,imageSwap:o}}_getThreeInputConfig(e,t){const r=this._getTextures(e,t);let n=0,s=0,o=new Float32Array([1,0,0,0,1,0,0,0,1]);const i=r[1]?{texture:r[1],unit:1}:void 0,a=r[2]?{texture:r[2],unit:2}:void 0,u=[];if(e.forEach((e,t)=>"Constant"===e.name&&u.push(t)),1===u.length)n=e[u[0]].parameters.value,o=0===u[0]?new Float32Array([0,1,0,0,0,1,1,0,0]):1===u[0]?new Float32Array([1,0,0,0,0,1,0,1,0]):new Float32Array([1,0,0,0,1,0,0,0,1]);else if(2===u.length){n=e[u[0]].parameters.value,s=e[u[1]].parameters.value;const t=e.findIndex(e=>"Constant"!==e.name);o=0===t?new Float32Array([1,0,0,0,1,0,0,0,1]):1===t?new Float32Array([0,1,0,1,0,0,0,0,1]):new Float32Array([0,0,1,1,0,0,0,1,0])}return{image1:i,image2:a,image1Const:n,image2Const:s,imageSwap:o}}}export{i as BaseRasterProcessorTechnique};
