/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{tileSize as e}from"../../../definitions.js";import{Technique as t}from"../Technique.js";import{getInterpolationDefines as i}from"./processor/textureUtils.js";import{RasterRangeHighlightShader as n}from"../shaders/raster/RasterRangeHighlightShader.js";import{RasterXYBandHighlightShader as o}from"../shaders/raster/RasterXYBandHighlightShader.js";import{SizedPixelFormat as r,PixelType as s}from"../../../../../../webgl/enums.js";import{FramebufferObject as a}from"../../../../../../webgl/FramebufferObject.js";import g from"../../../../../../webgl/Texture.js";import{TextureDescriptor as h}from"../../../../../../webgl/TextureDescriptor.js";class d extends t{constructor(){super(...arguments),this.name="BrushRasterHighlight",this.type=2,this.shaders={range:new n,xyBand:new o}}shutdown(e){super.shutdown(e),this._fbo?.dispose(),this._fbo=void 0}_getBandIds(e,t){const{type:i}=e,n="xy-band"===i?e.bandIds:[e.bandId];return t?.length?n.map(e=>t.indexOf(e)):"xy-band"===i?[0,1]:[0]}_getSingleBandShaderOptions(e,t,n,o){const{config:r,projectionConfig:s,highlightConfig:a,projectionDefines:g}=this._getCommonConfig(t),h=i(t,o),d={bandSwap:n,ranges:e.ranges,color:e.color};return{shader:this.shaders.range,uniforms:{projectionConfig:s,config:r,rangeHighlightConfig:d,highlightConfig:a},defines:{...g,...h,applyPixelMask:!1,applyPixelHighlights:!1,hasExistingHighlights:!!a},optionalAttributes:null,useComputeBuffer:!1}}_getXYBandShaderOptions(e,t,n,o,r){const{config:s,projectionConfig:a,highlightConfig:g,projectionDefines:h}=this._getCommonConfig(t),d=i(t,o),u={bandSwap:n,xRange:e.xRange,yRange:e.yRange,color:e.color},p=r?{maskTexture:{texture:r,unit:4}}:void 0;return{shader:this.shaders.xyBand,uniforms:{projectionConfig:a,config:s,xyRangeHighlightConfig:u,xyMaskHighlightConfig:p,highlightConfig:g},defines:{...h,...d,applyPixelMask:!1,applyPixelHighlights:!1,hasExistingHighlights:!!g,useMask:!!p},optionalAttributes:null,useComputeBuffer:!1}}render(t,i){const{pixelHighlightOptions:n}=t;if(!n)return;const{context:o}=t;if(!this._fbo){const i=u(t.context,e,e);this._fbo=new a(o,i)}const{type:r}=n,s="xy-band"===r?p(t.context,n):void 0,g=o.getBoundFramebufferObject(),h=o.getViewport();for(const a of i.bitmaps){if(!a.source||a.highlighted||a.suspended||a.isRendereredSource)continue;const i=this._getBandIds(n,a.bandIds);if(i.some(e=>e<0||e>2))continue;t.timeline.begin(this.name);const{painter:g}=t;g.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}}}),a.updateTexture(t),a.updateProcessedTexture(!1);const h=new Float32Array(9);h[3*i[0]]=1,h[3*i[1]+1]="xy-band"===r?1:0;const d="single-band"===r?this._getSingleBandShaderOptions(n,a,h,o):this._getXYBandShaderOptions(n,a,h,o,s);o.bindFramebuffer(this._fbo),o.setViewport(0,0,e,e),g.submitDrawMesh(t.context,d,g.quadMesh);const p=u(t.context,e,e);this._fbo.copyToTexture(0,0,e,e,0,0,p),a.highlightTexture=p,t.timeline.end(this.name)}s?.dispose(),o.bindFramebuffer(g),o.setViewport(h.x,h.y,h.width,h.height)}_getCommonConfig(e){const{names:t,textures:i}=e.getTextures({forProcessing:!0,useProcessedTexture:e.processed}),n=i[t.indexOf("u_image")],o=e.getRasterTextureSize(),r={texture:{texture:n,unit:0},dvsMat3:new Float32Array([2,0,0,0,2,0,-1,-1,0]),coordScale:[1,1],srcImageSize:o,opacity:1},s=i[t.indexOf("u_transformGrid")],{transformGrid:a}=e,g=!(!s||!a),h=g?{transformTexture:{texture:s,unit:1},targetImageSize:[e.width,e.height],transformSpacing:a.spacing,transformGridSize:a.size}:void 0,{highlightTexture:d}=e;return{config:r,projectionConfig:h,highlightConfig:d?{highlightTexture:{texture:d,unit:2}}:void 0,projectionDefines:{applyProjection:g,lookupProjection:g&&1===a.spacing[0]}}}}function u(e,t,i){const n=new h(t,i);return n.internalFormat=r.RGBA8,n.samplingMode=9728,n.dataType=s.UNSIGNED_BYTE,n.isImmutable=!0,n.wrapMode=33071,new g(e,n)}function p(e,t){const{mask:i,maskSize:n}=t;if(!i?.length||!n?.length)return;const o=new h(n[0],n[1]);return o.internalFormat=r.R8,o.samplingMode=9728,o.dataType=s.UNSIGNED_BYTE,o.isImmutable=!0,o.wrapMode=33071,new g(e,o,i)}export{d as RasterHighlightTechnique};
