/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{nextPowerOfTwo as t}from"../../../../../../../core/mathUtils.js";import{create as e}from"../../../../../../../core/libs/gl-matrix-2/factories/mat3f32.js";import{fromValues as r}from"../../../../../../../core/libs/gl-matrix-2/factories/vec4f32.js";import{getDefinesKey as s}from"../../../../vectorTiles/shaders/vtlMaterialUtils.js";import{vtlTextureBindingUnitSprites as o,vtlHighResCutoff as i}from"../../../definitions.js";import{Mesh as a}from"../../../meshing/Mesh.js";import{simplePipelineState as n}from"../../utils.js";import{Technique as l}from"../Technique.js";import{VTLShaderBackground as c}from"../shaders/vector-tiles/VTLShaderBackground.js";import{PrimitiveType as h}from"../../../../../../webgl/enums.js";class m extends l{constructor(){super(...arguments),this.type=35,this._color=r(1,0,0,1),this._patternMatrix=e(),this._mesh=null,this.shaders={background:new c}}render(r,a){if(0===a.tiles.length)return;this._loadResources(r);const{context:l,displayLevel:c,painter:h,spriteMosaic:m,state:u,styleLayer:d}=r,p=d,f=p.backgroundMaterial,g=p.getPaintValue("background-color",c),y=p.getPaintValue("background-opacity",c),M=p.getPaintValue("background-pattern",c),_=void 0!==M,b=1|window.devicePixelRatio;let x,v;const w=f.key+s({hasPattern:_}),j=b>i?2:1,k={layerInfo:{depth:p.z,coordRange:a.tiles[0].rangeX},tileInfo:{dvsMat:u.displayViewMat3},solidColor:null,pattern:null,patternMat:{mat:e()}};if(_){const t=m.getMosaicItemPosition(M,!0);if(null!=t){const{tl:e,br:r,page:s}=t;x=r[0]-e[0],v=r[1]-e[1];const i=m.getPageSize(s);null!=i&&(m.bind(l,9729,s,o),k.pattern={tlbr:[e[0],e[1],r[0],r[1]],mosaicSize:i,texture:{texture:m.getTexture(s),unit:o},opacity:y})}}else{const t=g[3]*y;this._color[0]=t*g[0],this._color[1]=t*g[1],this._color[2]=t*g[2],this._color[3]=t,k.solidColor={color:this._color}}h.setPipelineState(n);for(const e of a.tiles){if(k.tileInfo.dvsMat=e.transforms.displayViewScreenMat3,k.layerInfo.coordRange=e.rangeX,_){const r=Math.max(2**(Math.round(c)-e.key.level),1),s=j*e.width*r,o=s/t(x),i=s/t(v);this._patternMatrix[0]=o,this._patternMatrix[4]=i,k.patternMat={mat:this._patternMatrix}}const r={shader:this.shaders.background,uniforms:k,defines:{renderMode:_?1:0},optionalAttributes:f.getUsedAttributes(),useComputeBuffer:!1,precisionFactors:f.getPrecisionFactors(),materialKey:w};h.submitDrawMeshUntyped(l,r,this._mesh,{stencilRef:e.stencilRef})}}shutdown(){this._mesh&&(this._mesh.destroy(),this._mesh=null)}_loadResources(t){if(this._mesh)return;const{context:e,styleLayer:r}=t,s=r,o=new Int16Array([0,0,1,0,0,1,1,1]),i=s.backgroundMaterial.geometryLayout;if(!i)return;const n={vertex:{geometryVB:{data:o,layout:i}},groups:[{primitive:h.TRIANGLE_STRIP}],parts:[{group:0,start:0,count:4}]};this._mesh=new a(e,n)}}export{m as VTLTechniqueBackground};
