/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import"../../../../core/has.js";import{estimateNumberMemory as t}from"../../../../core/memoryEstimations.js";import{List as e}from"./cpuMapped/FreeList.js";import{isHittest as n}from"./shaderGraph/techniques/featureTechniqueUtils.js";import{PrimitiveType as i,DataType as s}from"../../../webgl/enums.js";function a(t,e){return t<<16|255&e}function r(t){return 255&t}class d{constructor(t,e,n,i,s){this.instance=t,this.materialKey=e,this.target=n,this.start=i,this.count=s}get textureKey(){return r(this.materialKey)}get indexEnd(){return this.start+this.count}extend(t){this.count+=t}render(t){this.instance.techniqueRef.render(t,this)}getStencilReference(){return this.target.stencilRef}getAttributePrecisionPackFactors(){const t=this.instance.instanceId;return this.target.getMesh(t).getAttributePrecisionPackFactors()}draw(t,e){n(t)?this.drawCompute(t.context,e):this.drawGeometry(t.context,e)}drawCompute(t,e){const n=this.instance.instanceId,a=this.target.getMesh(n).getComputeVAO(t,e),r=this.start*Uint32Array.BYTES_PER_ELEMENT/3;t.bindVAO(a,e.locations),t.drawElements(i.POINTS,this.count/3,s.UNSIGNED_INT,r),t.bindVAO(null)}drawGeometry(t,e){const n=this.instance.instanceId,a=this.target.getMesh(n).getGeometryVAO(t,e),r=this.start*Uint32Array.BYTES_PER_ELEMENT;t.bindVAO(a,e.locations),t.drawElements(i.TRIANGLES,this.count,s.UNSIGNED_INT,r),t.bindVAO(null)}}class h{constructor(){this._length=0,this._minOrderedLength=0,this._materialKeys=new Set}static fromDisplayEntities(t,e,n,i){const s=new h;for(const r of t.values())for(const t of r.records){const r=n.getInstance(t.instanceId),d=a(r.instanceId,t.textureKey);s.addRecord(r,d,t.indexStart,t.indexCount,t.vertexStart,t.vertexCount,e,i)}return s}get length(){return this._length}get minOrderedLength(){return this._minOrderedLength}get minUnorderedLength(){return this._materialKeys.size}get usedMemory(){return this._length?5*this._length*t:0}render(t,e){const{drawPhase:n}=t;for(const i of this.infos()){const s=i.instance.techniqueRef;s.drawPhase&n&&(null==e||s.type===e)&&i.render(t)}}addRecord(t,n,i,s,a,r,h,o){let l=i,c=s;if(c||(l=a,c=r),!c)return;if(null==this._head){const i=new d(t,n,h,l,c);return this._head=new e(i),this._tail=this._head,this._length++,void this._minOrderedLength++}if(1===o)return this._insert(t,n,h,l,c,this._tail,null);let u=null,_=this._head;const g=t.instanceId,m=t.techniqueRef.symbologyPlane;if(2===o&&(2===m||3===m))return this._insert(t,n,h,l,c,this._tail,null);for(;_;){const e=_.data.instance,i=e.instanceId,s=e.techniqueRef.symbologyPlane,a=u?.data.instance.instanceId;if(m<s||g===a&&g!==i)return this._insert(t,n,h,l,c,u,_);u=_,_=_.next}this._insert(t,n,h,l,c,u,null)}*infos(){if(null!=this._head)for(const t of this._head.values())yield t}_insert(t,n,i,s,a,r,h){if(null==r&&null==h){const r=new d(t,n,i,s,a);return this._head=new e(r),this._tail=this._head,this._length++,void this._minOrderedLength++}return n!==this._tail.data.materialKey&&this._minOrderedLength++,this._materialKeys.add(n),null==r&&null!=h?this._insertAtHead(t,n,i,s,a,h):null!=r&&null==h?this._insertAtEnd(t,n,i,s,a,r):null!=r&&null!=h?this._insertAtMiddle(t,n,i,s,a,r,h):void 0}_insertAtHead(t,n,i,s,a,r){const h=s+a;if(n===r.data.materialKey&&i===r.data.target&&h===r.data.start)r.data.start=s,r.data.count+=a;else{const h=new d(t,n,i,s,a);this._head=new e(h),this._head.next=r,this._length++}}_insertAtEnd(t,n,i,s,a,r){if(r.data.materialKey===n&&r.data.indexEnd===s)r.data.count+=a;else{const h=new d(t,n,i,s,a);this._tail=new e(h),r.next=this._tail,this._length++}}_insertAtMiddle(t,n,i,s,a,r,h){const o=s+a;if(r.data.materialKey===n&&r.data.target===i&&r.data.indexEnd===s)r.data.count+=a,r.data.materialKey===h.data.materialKey&&r.data.target===h.data.target&&r.data.indexEnd===h.data.start&&(r.data.count+=h.data.count,r.next=h.next,this._length--);else if(n===h.data.materialKey&&i===h.data.target&&o===h.data.start)h.data.start=s,h.data.count+=a;else{const o=new d(t,n,i,s,a),l=new e(o);r.next=l,l.next=h,this._length++}}}export{h as DisplayList,d as DisplayListInfo};
