2 lines
15 KiB
JavaScript
2 lines
15 KiB
JavaScript
import{z as B,M as l,hn as U,ho as y,hp as P,eA as D}from"./index-b5c8f851.js";import{u as F,E as z,a as A,T as g,b as C}from"./Texture-507d5ea8.js";import{A as a,F as S,V as m,C as x,X as u,G as L,U as H,t as O,n as b,M as d,B as T}from"./enums-bdecffa2.js";const f=B.getLogger("esri.views.webgl.BufferObject");let I=class p{static createIndex(t,e,i){return new p(t,a.ELEMENT_ARRAY_BUFFER,e,i)}static createVertex(t,e,i){return new p(t,a.ARRAY_BUFFER,e,i)}static createUniform(t,e,i){if(t.type!==l.WEBGL2)throw new Error("Uniform buffers are supported in WebGL2 only!");return new p(t,a.UNIFORM_BUFFER,e,i)}static createPixelPack(t,e=S.STREAM_READ,i){if(t.type!==l.WEBGL2)throw new Error("Pixel pack buffers are supported in WebGL2 only!");const r=new p(t,a.PIXEL_PACK_BUFFER,e);return i&&r.setSize(i),r}static createPixelUnpack(t,e=S.STREAM_DRAW,i){if(t.type!==l.WEBGL2)throw new Error("Pixel unpack buffers are supported in WebGL2 only!");return new p(t,a.PIXEL_UNPACK_BUFFER,e,i)}constructor(t,e,i,r){this._context=t,this.bufferType=e,this.usage=i,this._glName=null,this._size=-1,this._indexType=void 0,t.instanceCounter.increment(m.BufferObject,this),this._glName=this._context.gl.createBuffer(),F(this._context.gl),r&&this.setData(r)}get glName(){return this._glName}get size(){return this._size}get indexType(){return this._indexType}get byteLength(){return this.bufferType===a.ELEMENT_ARRAY_BUFFER?this._indexType===x.UNSIGNED_INT?4*this._size:2*this._size:this._size}get _isVAOAware(){return this.bufferType===a.ELEMENT_ARRAY_BUFFER||this.bufferType===a.ARRAY_BUFFER}dispose(){var t;(t=this._context)!=null&&t.gl?(this._glName&&(this._context.gl.deleteBuffer(this._glName),this._glName=null),this._context.instanceCounter.decrement(m.BufferObject,this),this._context=null):this._glName&&f.warn("Leaked WebGL buffer object")}setSize(t,e=null){if(t<=0&&f.error("Buffer size needs to be positive!"),this.bufferType===a.ELEMENT_ARRAY_BUFFER&&e!=null)switch(this._indexType=e,e){case x.UNSIGNED_SHORT:t*=2;break;case x.UNSIGNED_INT:t*=4}this._setBufferData(t)}setData(t){if(!t)return;let e=t.byteLength;this.bufferType===a.ELEMENT_ARRAY_BUFFER&&(U(t)&&(e/=2,this._indexType=x.UNSIGNED_SHORT),y(t)&&(e/=4,this._indexType=x.UNSIGNED_INT)),this._setBufferData(e,t)}_setBufferData(t,e=null){this._size=t;const i=this._context.getBoundVAO();this._isVAOAware&&this._context.bindVAO(null),this._context.bindBuffer(this);const r=this._context.gl;e!=null?r.bufferData(this.bufferType,e,this.usage):r.bufferData(this.bufferType,t,this.usage),F(r),this._isVAOAware&&this._context.bindVAO(i)}setSubData(t,e,i,r){if(!t)return;(e<0||e*t.BYTES_PER_ELEMENT>=this.byteLength)&&f.error("offset is out of range!"),i>=r&&f.error("end must be bigger than start!"),(e+(r-i))*t.BYTES_PER_ELEMENT>this.byteLength&&f.error("An attempt to write beyond the end of the buffer!");const n=this._context.getBoundVAO();this._isVAOAware&&this._context.bindVAO(null),this._context.bindBuffer(this);const h=this._context.gl;if(this._context.type===l.WEBGL2)h.bufferSubData(this.bufferType,e*t.BYTES_PER_ELEMENT,t,i,r-i);else{const o=i===0&&r===t.length?t:t.subarray(i,r);h.bufferSubData(this.bufferType,e*t.BYTES_PER_ELEMENT,o)}F(h),this._isVAOAware&&this._context.bindVAO(n)}getSubData(t,e=0,i,r){if(this._context.type!==l.WEBGL2)return void f.error("Get buffer subdata is supported in WebGL2 only!");if(i<0||r<0)return void f.error("Problem getting subdata: offset and length were less than zero!");const n=G(t)?t.BYTES_PER_ELEMENT:1;if(n*((i??0)+(r??0))>t.byteLength)return void f.error("Problem getting subdata: offset and length exceeded destination size!");e+n*(r??0)>this.byteLength&&f.warn("Potential problem getting subdata: requested data exceeds buffer size!");const h=this._context.gl;this._context.bindBuffer(this,a.COPY_READ_BUFFER),h.getBufferSubData(a.COPY_READ_BUFFER,e,t,i,r),this._context.unbindBuffer(a.COPY_READ_BUFFER)}async getSubDataAsync(t,e=0,i,r){this._context.type===l.WEBGL2?(await this._context.clientWaitAsync(),this.getSubData(t,e,i,r)):f.error("Get buffer subdata is supported in WebGL2 only!")}};function G(s){return P(s)}let K=class{constructor(t,e,i=e){this.internalFormat=t,this.width=e,this.height=i,this.multisampled=!1,this.samples=1}};function v(s){return s.width<=0||s.height<=0||s.internalFormat==null?0:s.width*s.height*z(s.internalFormat)}class X{constructor(t,e){this._context=t,this._descriptor=e,this.type=A.RenderBuffer,this._context.instanceCounter.increment(m.Renderbuffer,this);const i=this._context.gl;this.glName=i.createRenderbuffer(),this._context.bindRenderbuffer(this);const{width:r,height:n,internalFormat:h,multisampled:o}=e;if(o){if(this._context.type!==l.WEBGL2)throw new Error("Multisampled renderbuffers are not supported in WebGL1!");i.renderbufferStorageMultisample(i.RENDERBUFFER,this.samples,h,r,n)}else i.renderbufferStorage(i.RENDERBUFFER,h,r,n)}get descriptor(){return this._descriptor}get samples(){const t=this._descriptor.samples,e=this._context.parameters.maxSamples;return t?Math.min(t,e):e}get gpuMemoryUsage(){return v(this._descriptor)}resize(t,e){const i=this._descriptor;if(i.width===t&&i.height===e)return;i.width=t,i.height=e;const r=this._context.gl;this._context.bindRenderbuffer(this),i.multisampled?r.renderbufferStorageMultisample(r.RENDERBUFFER,this.samples,i.internalFormat,i.width,i.height):r.renderbufferStorage(r.RENDERBUFFER,i.internalFormat,i.width,i.height)}dispose(){this._context&&(this._context.gl.deleteRenderbuffer(this.glName),this._context.instanceCounter.decrement(m.Renderbuffer,this),this._context=null)}}const W=B.getLogger("esri.views.webgl.FramebufferObject");class E{constructor(t,e,i=null){this._context=t,this._glName=null,this._colorAttachments=new Map,this._depthStencilBuffer=null,this._depthStencilTexture=null,this._initialized=!1,t.instanceCounter.increment(m.FramebufferObject,this);const r=N(e)?e:new g(this._context,e);if(this._colorAttachments.set(u.COLOR_ATTACHMENT0,r),this._validateTextureDescriptor(r.descriptor),this._validateColorAttachmentPoint(u.COLOR_ATTACHMENT0),i!=null)if(V(i))this._context.capabilities.depthTexture||console.error("Setting the depth/stencil texture as an attachment requires WEBGL_depth_texture or WebGL2"),this._depthStencilTexture=N(i)?i:new g(this._context,i),this._validateTextureDescriptor(this._depthStencilTexture.descriptor);else{const n=Y(i)?i:new X(this._context,i);this._depthStencilBuffer=n,this._validateRenderBufferDescriptor(n.descriptor)}}dispose(){var e,i;if(this._colorAttachments.size===0&&!this._glName)return;const t=this._context.getBoundFramebufferObject();this._colorAttachments.forEach((r,n)=>{var h;return(h=this.detachColorTexture(n))==null?void 0:h.dispose()}),(e=this.detachDepthStencilBuffer())==null||e.dispose(),(i=this.detachDepthStencilTexture())==null||i.dispose(),this._glName&&(this._context.gl.deleteFramebuffer(this._glName),this._glName=null),this._context.bindFramebuffer(t),this._context.instanceCounter.decrement(m.FramebufferObject,this)}get glName(){return this._glName}get colorTexture(){return this._colorAttachments.get(u.COLOR_ATTACHMENT0)}get depthStencil(){return this._depthStencilTexture||this._depthStencilBuffer}get depthStencilTexture(){return this._depthStencilTexture}get width(){var e;const t=this._colorAttachments.get(u.COLOR_ATTACHMENT0);return((e=t==null?void 0:t.descriptor)==null?void 0:e.width)??0}get height(){var e;const t=this._colorAttachments.get(u.COLOR_ATTACHMENT0);return((e=t==null?void 0:t.descriptor)==null?void 0:e.height)??0}get gpuMemoryUsage(){var t;return[...this._colorAttachments].reduce((e,[i,r])=>e+r.gpuMemoryUsage,((t=this.depthStencil)==null?void 0:t.gpuMemoryUsage)??0)}getColorTexture(t){const e=this._colorAttachments.get(t);return e&&N(e)?e:null}get colorAttachments(){return[...this._colorAttachments.keys()]}attachColorTexture(t,e=u.COLOR_ATTACHMENT0){var r;if(!t)return;this._validateColorAttachmentPoint(e);const i=t.descriptor;this._validateTextureDescriptor(i),(r=this.detachColorTexture(e))==null||r.dispose(),this._initialized&&(this._context.bindFramebuffer(this),this._framebufferTexture2D(t.glName,e)),this._colorAttachments.set(e,t)}detachColorTexture(t=u.COLOR_ATTACHMENT0){const e=this._colorAttachments.get(t);if(e){if(this._initialized){const i=this._context.getBoundFramebufferObject();this._context.bindFramebuffer(this),this._framebufferTexture2D(null,t),this._context.bindFramebuffer(i)}return this._colorAttachments.delete(t),e}}setColorTextureTarget(t,e=u.COLOR_ATTACHMENT0){const i=this._colorAttachments.get(e);i&&this._framebufferTexture2D(i.glName,e,t)}attachDepthStencil(t){if(t)switch(t.type){case A.Texture:return this._attachDepthStencilTexture(t);case A.RenderBuffer:return this._attachDepthStencilBuffer(t)}}_attachDepthStencilTexture(t){var i;if(t==null)return;const e=t.descriptor;e.pixelFormat!==L.DEPTH_STENCIL&&e.pixelFormat!==L.DEPTH24_STENCIL8&&console.error("Depth/Stencil texture must have a pixel type of DEPTH_STENCIL!"),e.dataType!==H.UNSIGNED_INT_24_8&&console.error("Depth/Stencil texture must have data type of UNSIGNED_INT_24_8!"),this._context.capabilities.depthTexture||console.error("Extension WEBGL_depth_texture isn't supported therefore it is no possible to set the depth/stencil texture!"),this._validateTextureDescriptor(e),this._disposeDepthStencilAttachments(),this._initialized&&(this._context.bindFramebuffer(this),this._framebufferTexture2D(t.glName,O)),(i=this._depthStencilTexture)==null||i.dispose(),this._depthStencilTexture=t}detachDepthStencilTexture(){const t=this._depthStencilTexture;return t&&this._initialized&&(this._context.bindFramebuffer(this),this._framebufferTexture2D(null,O)),this._depthStencilTexture=null,t}_attachDepthStencilBuffer(t){if(t==null)return;const e=t.descriptor;if(this._validateRenderBufferDescriptor(e),this._disposeDepthStencilAttachments(),this._initialized){this._context.bindFramebuffer(this);const i=this._context.gl,r=this._getGLAttachmentPoint(e);i.framebufferRenderbuffer(b.FRAMEBUFFER,r,i.RENDERBUFFER,t.glName)}this._depthStencilBuffer=t}detachDepthStencilBuffer(){const t=this._depthStencilBuffer;if(t&&this._initialized){this._context.bindFramebuffer(this);const e=this._context.gl,i=this._getGLAttachmentPoint(t.descriptor);e.framebufferRenderbuffer(b.FRAMEBUFFER,i,e.RENDERBUFFER,null)}return this._depthStencilBuffer=null,t}copyToTexture(t,e,i,r,n,h,o){(t<0||e<0||n<0||h<0)&&console.error("Offsets cannot be negative!"),(i<=0||r<=0)&&console.error("Copy width and height must be greater than zero!");const c=o.descriptor;o.descriptor.target!==d.TEXTURE_2D&&console.error("Texture target must be TEXTURE_2D!"),((c==null?void 0:c.width)==null||(c==null?void 0:c.height)==null||t+i>this.width||e+r>this.height||n+i>c.width||h+r>c.height)&&console.error("Bad dimensions, the current input values will attempt to read or copy out of bounds!");const _=this._context,M=_.bindTexture(o,g.TEXTURE_UNIT_FOR_UPDATES);_.setActiveTexture(g.TEXTURE_UNIT_FOR_UPDATES),_.bindFramebuffer(this),_.gl.copyTexSubImage2D(d.TEXTURE_2D,0,n,h,t,e,i,r),_.bindTexture(M,g.TEXTURE_UNIT_FOR_UPDATES)}readPixels(t,e,i,r,n,h,o){(i<=0||r<=0)&&console.error("Copy width and height must be greater than zero!"),o||console.error("Target memory is not initialized!"),this._context.bindFramebuffer(this),this._context.gl.readPixels(t,e,i,r,n,h,o)}async readPixelsAsync(t,e,i,r,n,h,o){if(this._context.type!==l.WEBGL2)return C()&&console.warn("Attempting to read pixels using pixel buffer object without WebGL2"),void this.readPixels(t,e,i,r,n,h,o);const c=this._context.gl,_=I.createPixelPack(this._context,S.STREAM_READ,o.byteLength);this._context.bindBuffer(_),this._context.bindFramebuffer(this),c.readPixels(t,e,i,r,n,h,0),this._context.unbindBuffer(a.PIXEL_PACK_BUFFER),await _.getSubDataAsync(o),_.dispose()}resize(t,e){var r,n;if(this.width===t&&this.height===e)return;const i={width:t,height:e};R(i,this._context.parameters.maxTextureSize),this._colorAttachments.forEach(h=>h.resize(i.width,i.height)),(r=this._depthStencilTexture)==null||r.resize(i.width,i.height),this._initialized&&(R(i,this._context.parameters.maxRenderbufferSize),(n=this._depthStencilBuffer)==null||n.resize(i.width,i.height),this._context.getBoundFramebufferObject()===this&&this._context.bindFramebuffer(null),this._initialized=!1)}initializeAndBind(t=b.FRAMEBUFFER){const e=this._context.gl;if(this._initialized)return void e.bindFramebuffer(t,this.glName);this._glName&&e.deleteFramebuffer(this._glName);const i=e.createFramebuffer();if(e.bindFramebuffer(t,i),this._colorAttachments.forEach((r,n)=>this._framebufferTexture2D(r.glName,n,w(r),t)),this._depthStencilBuffer){const r=this._getGLAttachmentPoint(this._depthStencilBuffer.descriptor);e.framebufferRenderbuffer(t,r,e.RENDERBUFFER,this._depthStencilBuffer.glName)}else this._depthStencilTexture&&this._framebufferTexture2D(this._depthStencilTexture.glName,e.DEPTH_STENCIL_ATTACHMENT,w(this._depthStencilTexture),t);C()&&e.checkFramebufferStatus(t)!==e.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete!"),this._glName=i,this._initialized=!0}_framebufferTexture2D(t,e=u.COLOR_ATTACHMENT0,i=d.TEXTURE_2D,r=b.FRAMEBUFFER,n=0){this._context.gl.framebufferTexture2D(r,e,i,t,n)}_disposeDepthStencilAttachments(){const t=this._context.gl;if(this._depthStencilBuffer){if(this._initialized){this._context.bindFramebuffer(this);const e=this._getGLAttachmentPoint(this._depthStencilBuffer.descriptor);t.framebufferRenderbuffer(b.FRAMEBUFFER,e,t.RENDERBUFFER,null)}this._depthStencilBuffer=D(this._depthStencilBuffer)}this._depthStencilTexture&&(this._initialized&&(this._context.bindFramebuffer(this),this._framebufferTexture2D(null,t.DEPTH_STENCIL_ATTACHMENT)),this._depthStencilTexture=D(this._depthStencilTexture))}_validateTextureDescriptor(t){t.target!==d.TEXTURE_2D&&t.target!==d.TEXTURE_CUBE_MAP&&console.error("Texture type must be TEXTURE_2D or TEXTURE_CUBE_MAP!"),R(t,this._context.parameters.maxTextureSize),this._validateBufferDimensions(t)}_validateRenderBufferDescriptor(t){R(t,this._context.parameters.maxRenderbufferSize),this._validateBufferDimensions(t)}_validateBufferDimensions(t){t.width<=0&&(t.width=this.width),t.height<=0&&(t.height=this.height),this.width>0&&this.height>0&&(this.width===t.width&&this.height===t.height||console.error("Attachment size must match framebuffer size!"))}_getGLAttachmentPoint(t){switch(t.internalFormat){case T.DEPTH_COMPONENT16:case T.DEPTH_COMPONENT24:case T.DEPTH_COMPONENT32F:return this._context.gl.DEPTH_ATTACHMENT;case T.DEPTH24_STENCIL8:case T.DEPTH32F_STENCIL8:case T.DEPTH_STENCIL:return this._context.gl.DEPTH_STENCIL_ATTACHMENT;case T.STENCIL_INDEX8:return this._context.gl.STENCIL_ATTACHMENT}}_validateColorAttachmentPoint(t){if(E._MAX_COLOR_ATTACHMENTS===-1){const i=this._context.capabilities.drawBuffers;if(i){const r=this._context.gl;E._MAX_COLOR_ATTACHMENTS=r.getParameter(i.MAX_COLOR_ATTACHMENTS)}else E._MAX_COLOR_ATTACHMENTS=1}const e=t-u.COLOR_ATTACHMENT0;e+1>E._MAX_COLOR_ATTACHMENTS&&B.getLogger("esri.views.webgl.FrameBufferObject").error("esri.FrameBufferObject",`illegal attachment point for color attachment: ${e+1}. Implementation supports up to ${E._MAX_COLOR_ATTACHMENTS} color attachments`)}}function N(s){return s!=null&&"type"in s&&s.type===A.Texture}function Y(s){return s!=null&&"type"in s&&s.type===A.RenderBuffer}function V(s){return N(s)||s!=null&&"pixelFormat"in s}function R(s,t){const e=Math.max(s.width,s.height);if(e>t){W.warn(`Resizing FBO attachment size ${s.width}x${s.height} to device limit ${t}`);const i=t/e;return s.width=Math.round(s.width*i),s.height=Math.round(s.height*i),!1}return!0}function w(s){return s.descriptor.target===d.TEXTURE_CUBE_MAP?d.TEXTURE_CUBE_MAP_POSITIVE_X:d.TEXTURE_2D}E._MAX_COLOR_ATTACHMENTS=-1;export{E,K as a,I as h,X as i};
|