/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{RenderingDevice as e}from"../webgl/RenderingDevice.js";import{simplePipelineState as t}from"../webgl/shaderGraph/utils.js";import{VTLTechniquesRepo as i}from"../webgl/shaderGraph/techniques/vectorTiles/VTLTechniques.js";const r=1e-6;class n{constructor(t,r){this.spriteMosaic=t,this.glyphMosaic=r,this._vtlTechniques=new i,this._context=null,this._vtlTechniques.startup(),this._renderingDevice=new e}dispose(){this._vtlTechniques&&this._vtlTechniques.shutdown(this._context),this.spriteMosaic.dispose(),this.glyphMosaic.dispose(),this._renderingDevice.dispose(),this._context=null}drawSymbols(e,t,i){const n=i.layers;e.renderPass="translucent";const a=this._vtlTechniques.getTechnique(3);s[0]=t;for(let o=0;o<n.length;o++){const t=n[o];if(3!==t.type)continue;const i=t.getLayoutProperty("visibility");if(i&&1===i.getValue())continue;const l=e.displayLevel;void 0!==t.minzoom&&t.minzoom>l+r||void 0!==t.maxzoom&&t.maxzoom<=l-r||(e.styleLayerUID=t.uid,e.styleLayer=t,a.render(e,{tiles:s}))}s[0]=null}drawBackground(e,t,i){if(0===i.backgroundBucketIds.length)return;const{displayLevel:n,requiredLevel:a}=e;t.key.level=a,this._renderingDevice.setPipelineState({color:{write:[!0,!0,!0,!0],blendMode:"composite"},stencil:!1,depth:{write:!1,test:519}}),e.renderPass="background";const o=this._vtlTechniques.getTechnique(0);s[0]=t,i.backgroundBucketIds.forEach(t=>{const a=i.getLayerById(t);if(0!==a.type)return;const l=a.getLayoutProperty("visibility");l&&1===l.getValue()||void 0!==a.minzoom&&a.minzoom>n+r||void 0!==a.maxzoom&&a.maxzoom<=n-r||(e.styleLayerUID=a.uid,e.styleLayer=a,o.render(e,{tiles:s}))}),s[0]=null}drawTile(e,i,r,n){const s=r.layers,a=n?e=>n!==e.type||!i.layerData.has(e.uid):e=>!i.layerData.has(e.uid),o=s.filter(e=>{if(a(e))return!1;const t=e.getLayoutProperty("visibility");return 1!==t?.getValue()}),{context:l}=e,c=l.getPipelineState(),{depthWrite:d}=c;i.disposeLayerData();let h=0,u=1;void 0!==d?.zNear&&void 0!==d.zFar&&(h=d.zNear,u=d.zFar),this._renderingDevice.setPipelineState({color:{write:[!0,!0,!0,!0],blendMode:"none"},stencil:!1,depth:{write:{zNear:h,zFar:u},test:515}}),e.renderPass="opaque";for(let t=o.length-1;t>=0;--t)this._renderStyleLayer(o[t],e,i);this._renderingDevice.setPipelineState({color:{write:[!0,!0,!0,!0],blendMode:"composite"},stencil:!1,depth:{write:!1,test:515}}),e.renderPass="translucent",o.forEach(t=>this._renderStyleLayer(t,e,i)),this._renderingDevice.setPipelineState(t),l.setPipelineState(c),this._context||(this._context=e.context)}setShader(e){return this._renderingDevice.setShader(e)}setPipelineState(e){return this._renderingDevice.setPipelineState(e)}getPipelineState(){return this._renderingDevice.getPipelineState()}submitDraw(e,t,i){return this._renderingDevice.submitDraw(e,t,i)}submitDrawMesh(e,t,i,r){this._renderingDevice.submitDrawMesh(e,t,i,r)}submitDrawMeshUntyped(e,t,i,r){this._renderingDevice.submitDrawMeshUntyped(e,t,i,r)}updatePipelineState(e){return this._renderingDevice.updatePipelineState(e)}setStencilRef(e,t){return this._renderingDevice.setStencilRef(e,t)}_renderStyleLayer(e,t,i){const{renderPass:n}=t;let a;switch(e.type){case 0:if("background"!==n)return;a=this._vtlTechniques.getTechnique(0);break;case 1:if("opaque"!==n&&"translucent"!==n)return;a=this._vtlTechniques.getTechnique(1);break;case 2:if("translucent"!==n)return;a=this._vtlTechniques.getTechnique(2);break;case 4:if("translucent"!==n)return;a=this._vtlTechniques.getTechnique(4);break;case 3:if("translucent"!==n)return;a=this._vtlTechniques.getTechnique(3)}const{displayLevel:o}=t,{minzoom:l,maxzoom:c}=e;if(void 0!==l&&l>o+r||void 0!==c&&c<=o-r||!a)return;const d=this._renderingDevice.getPipelineState();3===e.type&&this._renderingDevice.setPipelineState({color:{write:[!0,!0,!0,!0],blendMode:"composite"},stencil:!1,depth:!1}),t.styleLayerUID=e.uid,t.styleLayer=e,s[0]=i,a.render(t,{tiles:s}),s[0]=null,3===e.type&&this._renderingDevice.setPipelineState(d)}}const s=[null];export{n as default};
