/* COPYRIGHT Esri - https://js.arcgis.com/5.1.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,c="indexedColormap"in a.parameters?o(u,a.parameters.indexedColormap):void 0,m="Reproject"===a.name,d=u.boundFramebuffer,p=u.getViewport();for(const o of i.bitmaps){const d=m?!(o.rasterTexture&&o.projected):!o.processed;if(!o.source||!d||o.suspended)continue;r.timeline.begin(this.name);const{painter:p}=r;p.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}}}),m||(o.processedTexture=e(o.processedTexture)),o.updateTexture(r);const[f,g]=o.getRasterTextureSize(m),l=f===t&&g===t,x=l?i.processorFbo:n(u,f,g);u.bindFramebuffer(x),u.setViewport(0,0,x.width,x.height),this._process(r,o,c);const h=s(r.context,f,g);if(x.copyToTexture(0,0,f,g,0,0,h),m)o.rasterTexture=h;else{const e=r.hasBranches?a.id:0;o.functionTextures[e]?.dispose(),o.functionTextures[e]=h}l||x.dispose(),r.timeline.end(this.name)}c?.dispose(),u.bindFramebuffer(d),u.setViewport(p.x,p.y,p.width,p.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{config:{srcImageSize:t.getRasterTextureSize(u)},textures:{texture:{texture:a,unit:0}}}}_getMultipleInputConfig(e,t){if(!t?.length)return{};if(2===t.length){const{config:r,textures:n}=this._getTwoInputConfig(t,e);return{twoRasterConfig:r,twoRasterTextures:n}}if(3===t.length){const{config:r,textures:n}=this._getThreeInputConfig(t,e);return{threeRasterConfig:r,threeRasterTextures:n}}return{}}_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{config:{image1Const:s>-1?e[s].parameters.value:0,imageSwap:o},textures:n?{image1:n}:void 0}}_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{config:{image1Const:n,image2Const:s,imageSwap:o},textures:i?{image1:i,image2:a??null}:void 0}}}export{i as BaseRasterProcessorTechnique};
