/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{getOrCreateMapValue as e}from"../../../../../core/MapUtils.js";import{DataType as t}from"../../../../webgl/enums.js";class s{constructor(e,t,s,r=0){this._owner=e,this._rctx=t,this._techniques=s,this._sorting=r,this._draws=new Array,this._previouslyBoundDraw=new Map}destroy(){this._draws.length=0,this._previouslyBoundDraw.clear()}submitDraw(e,t,s,i,n,a){const o=(s.indexed?s.vao.indexBuffer.indexType:null)??0;this._owner.registerDrawParameters(t);const h=new r(e,t,s,i,n,s.geometrySizeHint,o,a);this._draws.push(h)}acquire(t,s){const r=new Map;return this._draws.map(i=>e(r,i.material,()=>i.material.acquireTechnique(this._techniques,t,s,i.geometry.parameters)))}dispatch(e,t,s){const r=this._rctx;this._previouslyBoundDraw.clear();let n=null;const a=this._draws.length,o=t.highlight?.name;if(0===a)return;const h=1===this._sorting?-1:1,d=1===h?a:-1;for(let u=1===h?0:a-1;u!==d;u+=h){const a=this._draws[u];if(2===e.identifier){const e=a.highlightName;if(e){if(e!==o)continue}else if(!o||!t.overlay?.hasHighlight(o))continue}const h=s[u];h!==n&&(r.bindTechnique(h,t,e),n=h);const{geometryRanges:d,indexType:c,geometry:l,drawParameters:g}=a;r.bindVAO(l.vao),this._previouslyBoundDraw.get(h)!==g&&(h.program.bindDraw(t,e,g),this._previouslyBoundDraw.set(h,g));const m=d.length,{primitiveType:w}=l;for(let e=0;e<m;e+=2){const t=d[e],s=d[e+1];if(0!==c){const e=i.get(c);r.drawElements(w,s,c,t*e)}else r.drawArrays(w,t,s)}}}prepareSubmit(){this._draws.length=0}finishSubmit(){const e=0===this._sorting?1:-1;this._draws.sort((t,s)=>e*(t.depthSquaredHint-s.depthSquaredHint)||t.geometry.vao.usedMemory-s.geometry.vao.usedMemory)}get count(){return this._draws.length}}class r{constructor(e,t,s,r,i,n,a,o){this.material=e,this.drawParameters=t,this.geometry=s,this.geometryRanges=r,this.depthSquaredHint=i,this.geometrySizeHint=n,this.indexType=a,this.highlightName=o}}const i=new Map;i.set(t.UNSIGNED_BYTE,1),i.set(t.UNSIGNED_SHORT,2),i.set(t.UNSIGNED_INT,4);export{s as RenderPass};
