UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 42.5 kB
import{math as t}from"../../../core/math/math.js";import{platform as e}from"../../../core/platform.js";import{Color as i}from"../../../core/math/color.js";import{DEVICETYPE_WEBGL2 as s,DEVICETYPE_WEBGL1 as r,UNIFORMTYPE_BOOL as n,UNIFORMTYPE_INT as a,UNIFORMTYPE_FLOAT as l,UNIFORMTYPE_VEC2 as o,UNIFORMTYPE_VEC3 as h,UNIFORMTYPE_VEC4 as c,UNIFORMTYPE_IVEC2 as u,UNIFORMTYPE_IVEC3 as f,UNIFORMTYPE_IVEC4 as _,UNIFORMTYPE_BVEC2 as d,UNIFORMTYPE_BVEC3 as E,UNIFORMTYPE_BVEC4 as m,UNIFORMTYPE_MAT2 as p,UNIFORMTYPE_MAT3 as T,UNIFORMTYPE_MAT4 as F,UNIFORMTYPE_TEXTURE2D as x,UNIFORMTYPE_TEXTURECUBE as A,UNIFORMTYPE_UINT as g,UNIFORMTYPE_UVEC2 as R,UNIFORMTYPE_UVEC3 as b,UNIFORMTYPE_UVEC4 as S,UNIFORMTYPE_TEXTURE2D_SHADOW as B,UNIFORMTYPE_TEXTURECUBE_SHADOW as L,UNIFORMTYPE_TEXTURE2D_ARRAY as C,UNIFORMTYPE_TEXTURE3D as U,UNIFORMTYPE_ITEXTURE2D as I,UNIFORMTYPE_UTEXTURE2D as P,UNIFORMTYPE_ITEXTURECUBE as v,UNIFORMTYPE_UTEXTURECUBE as O,UNIFORMTYPE_ITEXTURE3D as M,UNIFORMTYPE_UTEXTURE3D as N,UNIFORMTYPE_ITEXTURE2D_ARRAY as D,UNIFORMTYPE_UTEXTURE2D_ARRAY as y,UNIFORMTYPE_FLOATARRAY as k,UNIFORMTYPE_VEC2ARRAY as G,UNIFORMTYPE_VEC3ARRAY as W,UNIFORMTYPE_VEC4ARRAY as H,UNIFORMTYPE_INTARRAY as V,UNIFORMTYPE_UINTARRAY as w,UNIFORMTYPE_BOOLARRAY as X,UNIFORMTYPE_IVEC2ARRAY as z,UNIFORMTYPE_UVEC2ARRAY as K,UNIFORMTYPE_BVEC2ARRAY as j,UNIFORMTYPE_IVEC3ARRAY as Z,UNIFORMTYPE_UVEC3ARRAY as Y,UNIFORMTYPE_BVEC3ARRAY as q,UNIFORMTYPE_IVEC4ARRAY as Q,UNIFORMTYPE_UVEC4ARRAY as J,UNIFORMTYPE_BVEC4ARRAY as $,UNIFORMTYPE_MAT4ARRAY as tt,PIXELFORMAT_RGBA8 as et,PIXELFORMAT_RGBA16F as it,PIXELFORMAT_RGBA32F as st,PIXELFORMAT_RGB8 as rt,FUNC_ALWAYS as nt,STENCILOP_KEEP as at,ADDRESS_CLAMP_TO_EDGE as lt,semanticToLocation as ot,CLEARFLAG_COLOR as ht,CLEARFLAG_DEPTH as ct,CLEARFLAG_STENCIL as ut,CULLFACE_NONE as ft,PRIMITIVE_TRISTRIP as _t,FILTER_NEAREST_MIPMAP_NEAREST as dt,FILTER_NEAREST_MIPMAP_LINEAR as Et,FILTER_NEAREST as mt,FILTER_LINEAR_MIPMAP_NEAREST as pt,FILTER_LINEAR_MIPMAP_LINEAR as Tt,FILTER_LINEAR as Ft}from"../constants.js";import{GraphicsDevice as xt}from"../graphics-device.js";import{RenderTarget as At}from"../render-target.js";import{Texture as gt}from"../texture.js";import{WebglVertexBuffer as Rt}from"./webgl-vertex-buffer.js";import{WebglIndexBuffer as bt}from"./webgl-index-buffer.js";import{WebglShader as St}from"./webgl-shader.js";import{WebglTexture as Bt}from"./webgl-texture.js";import{WebglRenderTarget as Lt}from"./webgl-render-target.js";import{ShaderUtils as Ct}from"../shader-utils.js";import{Shader as Ut}from"../shader.js";import{BlendState as It}from"../blend-state.js";import{DepthState as Pt}from"../depth-state.js";import{StencilParameters as vt}from"../stencil-parameters.js";import{WebglGpuProfiler as Ot}from"./webgl-gpu-profiler.js";const Mt=[],Nt="\nattribute vec2 vertex_position;\nvarying vec2 vUv0;\nvoid main(void)\n{\n\tgl_Position = vec4(vertex_position, 0.5, 1.0);\n\tvUv0 = vertex_position.xy*0.5+0.5;\n}\n";function Dt(t,e,i){const s=t.renderTarget;t.setRenderTarget(e),t.updateBegin(),t.setCullMode(ft),t.setBlendState(It.NOBLEND),t.setDepthState(Pt.NODEPTH),t.setStencilState(null,null),t.setVertexBuffer(t.quadVertexBuffer,0),t.setShader(i),t.draw({type:_t,base:0,count:4,indexed:!1}),t.updateEnd(),t.setRenderTarget(s),t.updateBegin()}function yt(t,e){let i=!0;const s=t.createTexture();t.bindTexture(t.TEXTURE_2D,s),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,2,2,0,t.RGBA,e,null);const r=t.createFramebuffer();return t.bindFramebuffer(t.FRAMEBUFFER,r),t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,s,0),t.checkFramebufferStatus(t.FRAMEBUFFER)!==t.FRAMEBUFFER_COMPLETE&&(i=!1),t.bindTexture(t.TEXTURE_2D,null),t.deleteTexture(s),t.bindFramebuffer(t.FRAMEBUFFER,null),t.deleteFramebuffer(r),i}class kt extends xt{constructor(t,i={}){var rt;super(t,i),this.gl=void 0,this._defaultFramebuffer=null,this._defaultFramebufferChanged=!1,i=this.initOptions,this.updateClientRect(),this.initTextureUnits(),this.contextLost=!1,this._contextLostHandler=t=>{t.preventDefault(),this.loseContext(),this.fire("devicelost")},this._contextRestoredHandler=()=>{this.restoreContext(),this.fire("devicerestored")};const nt="undefined"!=typeof navigator&&navigator.userAgent;if(this.forceDisableMultisampling=nt&&nt.includes("AppleWebKit")&&(nt.includes("15.4")||nt.includes("15_4")),this.forceDisableMultisampling&&(i.antialias=!1),"firefox"===e.browserName){const t=("undefined"!=typeof navigator?navigator.userAgent:"").match(/Firefox\/(\d+(\.\d+)*)/),s=t?t[1]:null;if(s){const t=parseFloat(s);("windows"===e.name&&(t>=120||115===t)||"android"===e.name&&t>=132)&&(i.antialias=!1)}}let at=null;if(this.backBufferAntialias=null!=(rt=i.antialias)&&rt,i.antialias=!1,i.gl)at=i.gl;else{const e=void 0===i.preferWebGl2||i.preferWebGl2?["webgl2","webgl","experimental-webgl"]:["webgl","experimental-webgl"];for(let s=0;s<e.length&&(at=t.getContext(e[s],i),!at);s++);}if(!at)throw new Error("WebGL not supported");this.gl=at,this.isWebGL2="undefined"!=typeof WebGL2RenderingContext&&at instanceof WebGL2RenderingContext,this.isWebGL1=!this.isWebGL2,this._deviceType=this.isWebGL2?s:r,this.updateBackbufferFormat(null);const lt="chrome"===e.browserName,ot="safari"===e.browserName,ht=e.browser&&-1!==navigator.appVersion.indexOf("Mac");let ct,ut,ft,_t,dt;this._tempEnableSafariTextureUnitWorkaround=ot,this._tempMacChromeBlitFramebufferWorkaround=ht&&lt&&!i.alpha,t.addEventListener("webglcontextlost",this._contextLostHandler,!1),t.addEventListener("webglcontextrestored",this._contextRestoredHandler,!1),this.initializeExtensions(),this.initializeCapabilities(),this.initializeRenderState(),this.initializeContextCaches(),this.createBackbuffer(null),this.supportsImageBitmap=!ot&&"undefined"!=typeof ImageBitmap,this._samplerTypes=new Set([at.SAMPLER_2D,at.SAMPLER_CUBE,...this.isWebGL2?[at.UNSIGNED_INT_SAMPLER_2D,at.INT_SAMPLER_2D,at.SAMPLER_2D_SHADOW,at.SAMPLER_CUBE_SHADOW,at.SAMPLER_3D,at.INT_SAMPLER_3D,at.UNSIGNED_INT_SAMPLER_3D,at.SAMPLER_2D_ARRAY,at.INT_SAMPLER_2D_ARRAY,at.UNSIGNED_INT_SAMPLER_2D_ARRAY]:[]]),this.glAddress=[at.REPEAT,at.CLAMP_TO_EDGE,at.MIRRORED_REPEAT],this.glBlendEquation=[at.FUNC_ADD,at.FUNC_SUBTRACT,at.FUNC_REVERSE_SUBTRACT,this.isWebGL2?at.MIN:this.extBlendMinmax?this.extBlendMinmax.MIN_EXT:at.FUNC_ADD,this.isWebGL2?at.MAX:this.extBlendMinmax?this.extBlendMinmax.MAX_EXT:at.FUNC_ADD],this.glBlendFunctionColor=[at.ZERO,at.ONE,at.SRC_COLOR,at.ONE_MINUS_SRC_COLOR,at.DST_COLOR,at.ONE_MINUS_DST_COLOR,at.SRC_ALPHA,at.SRC_ALPHA_SATURATE,at.ONE_MINUS_SRC_ALPHA,at.DST_ALPHA,at.ONE_MINUS_DST_ALPHA,at.CONSTANT_COLOR,at.ONE_MINUS_CONSTANT_COLOR],this.glBlendFunctionAlpha=[at.ZERO,at.ONE,at.SRC_COLOR,at.ONE_MINUS_SRC_COLOR,at.DST_COLOR,at.ONE_MINUS_DST_COLOR,at.SRC_ALPHA,at.SRC_ALPHA_SATURATE,at.ONE_MINUS_SRC_ALPHA,at.DST_ALPHA,at.ONE_MINUS_DST_ALPHA,at.CONSTANT_ALPHA,at.ONE_MINUS_CONSTANT_ALPHA],this.glComparison=[at.NEVER,at.LESS,at.EQUAL,at.LEQUAL,at.GREATER,at.NOTEQUAL,at.GEQUAL,at.ALWAYS],this.glStencilOp=[at.KEEP,at.ZERO,at.REPLACE,at.INCR,at.INCR_WRAP,at.DECR,at.DECR_WRAP,at.INVERT],this.glClearFlag=[0,at.COLOR_BUFFER_BIT,at.DEPTH_BUFFER_BIT,at.COLOR_BUFFER_BIT|at.DEPTH_BUFFER_BIT,at.STENCIL_BUFFER_BIT,at.STENCIL_BUFFER_BIT|at.COLOR_BUFFER_BIT,at.STENCIL_BUFFER_BIT|at.DEPTH_BUFFER_BIT,at.STENCIL_BUFFER_BIT|at.COLOR_BUFFER_BIT|at.DEPTH_BUFFER_BIT],this.glCull=[0,at.BACK,at.FRONT,at.FRONT_AND_BACK],this.glFilter=[at.NEAREST,at.LINEAR,at.NEAREST_MIPMAP_NEAREST,at.NEAREST_MIPMAP_LINEAR,at.LINEAR_MIPMAP_NEAREST,at.LINEAR_MIPMAP_LINEAR],this.glPrimitive=[at.POINTS,at.LINES,at.LINE_LOOP,at.LINE_STRIP,at.TRIANGLES,at.TRIANGLE_STRIP,at.TRIANGLE_FAN],this.glType=[at.BYTE,at.UNSIGNED_BYTE,at.SHORT,at.UNSIGNED_SHORT,at.INT,at.UNSIGNED_INT,at.FLOAT,at.HALF_FLOAT],this.pcUniformType={},this.pcUniformType[at.BOOL]=n,this.pcUniformType[at.INT]=a,this.pcUniformType[at.FLOAT]=l,this.pcUniformType[at.FLOAT_VEC2]=o,this.pcUniformType[at.FLOAT_VEC3]=h,this.pcUniformType[at.FLOAT_VEC4]=c,this.pcUniformType[at.INT_VEC2]=u,this.pcUniformType[at.INT_VEC3]=f,this.pcUniformType[at.INT_VEC4]=_,this.pcUniformType[at.BOOL_VEC2]=d,this.pcUniformType[at.BOOL_VEC3]=E,this.pcUniformType[at.BOOL_VEC4]=m,this.pcUniformType[at.FLOAT_MAT2]=p,this.pcUniformType[at.FLOAT_MAT3]=T,this.pcUniformType[at.FLOAT_MAT4]=F,this.pcUniformType[at.SAMPLER_2D]=x,this.pcUniformType[at.SAMPLER_CUBE]=A,this.pcUniformType[at.UNSIGNED_INT]=g,this.pcUniformType[at.UNSIGNED_INT_VEC2]=R,this.pcUniformType[at.UNSIGNED_INT_VEC3]=b,this.pcUniformType[at.UNSIGNED_INT_VEC4]=S,this.isWebGL2&&(this.pcUniformType[at.SAMPLER_2D_SHADOW]=B,this.pcUniformType[at.SAMPLER_CUBE_SHADOW]=L,this.pcUniformType[at.SAMPLER_2D_ARRAY]=C,this.pcUniformType[at.SAMPLER_3D]=U,this.pcUniformType[at.INT_SAMPLER_2D]=I,this.pcUniformType[at.UNSIGNED_INT_SAMPLER_2D]=P,this.pcUniformType[at.INT_SAMPLER_CUBE]=v,this.pcUniformType[at.UNSIGNED_INT_SAMPLER_2D]=O,this.pcUniformType[at.INT_SAMPLER_3D]=M,this.pcUniformType[at.UNSIGNED_INT_SAMPLER_3D]=N,this.pcUniformType[at.INT_SAMPLER_2D_ARRAY]=D,this.pcUniformType[at.UNSIGNED_INT_SAMPLER_2D_ARRAY]=y),this.targetToSlot={},this.targetToSlot[at.TEXTURE_2D]=0,this.targetToSlot[at.TEXTURE_CUBE_MAP]=1,this.targetToSlot[at.TEXTURE_3D]=2,this.commitFunction=[],this.commitFunction[n]=function(t,e){t.value!==e&&(at.uniform1i(t.locationId,e),t.value=e)},this.commitFunction[a]=this.commitFunction[n],this.commitFunction[l]=function(t,e){t.value!==e&&(at.uniform1f(t.locationId,e),t.value=e)},this.commitFunction[o]=function(t,e){dt=t.value,ct=e[0],ut=e[1],dt[0]===ct&&dt[1]===ut||(at.uniform2fv(t.locationId,e),dt[0]=ct,dt[1]=ut)},this.commitFunction[h]=function(t,e){dt=t.value,ct=e[0],ut=e[1],ft=e[2],dt[0]===ct&&dt[1]===ut&&dt[2]===ft||(at.uniform3fv(t.locationId,e),dt[0]=ct,dt[1]=ut,dt[2]=ft)},this.commitFunction[c]=function(t,e){dt=t.value,ct=e[0],ut=e[1],ft=e[2],_t=e[3],dt[0]===ct&&dt[1]===ut&&dt[2]===ft&&dt[3]===_t||(at.uniform4fv(t.locationId,e),dt[0]=ct,dt[1]=ut,dt[2]=ft,dt[3]=_t)},this.commitFunction[u]=function(t,e){dt=t.value,ct=e[0],ut=e[1],dt[0]===ct&&dt[1]===ut||(at.uniform2iv(t.locationId,e),dt[0]=ct,dt[1]=ut)},this.commitFunction[d]=this.commitFunction[u],this.commitFunction[f]=function(t,e){dt=t.value,ct=e[0],ut=e[1],ft=e[2],dt[0]===ct&&dt[1]===ut&&dt[2]===ft||(at.uniform3iv(t.locationId,e),dt[0]=ct,dt[1]=ut,dt[2]=ft)},this.commitFunction[E]=this.commitFunction[f],this.commitFunction[_]=function(t,e){dt=t.value,ct=e[0],ut=e[1],ft=e[2],_t=e[3],dt[0]===ct&&dt[1]===ut&&dt[2]===ft&&dt[3]===_t||(at.uniform4iv(t.locationId,e),dt[0]=ct,dt[1]=ut,dt[2]=ft,dt[3]=_t)},this.commitFunction[m]=this.commitFunction[_],this.commitFunction[p]=function(t,e){at.uniformMatrix2fv(t.locationId,!1,e)},this.commitFunction[T]=function(t,e){at.uniformMatrix3fv(t.locationId,!1,e)},this.commitFunction[F]=function(t,e){at.uniformMatrix4fv(t.locationId,!1,e)},this.commitFunction[k]=function(t,e){at.uniform1fv(t.locationId,e)},this.commitFunction[G]=function(t,e){at.uniform2fv(t.locationId,e)},this.commitFunction[W]=function(t,e){at.uniform3fv(t.locationId,e)},this.commitFunction[H]=function(t,e){at.uniform4fv(t.locationId,e)},this.commitFunction[g]=function(t,e){t.value!==e&&(at.uniform1ui(t.locationId,e),t.value=e)},this.commitFunction[R]=function(t,e){dt=t.value,ct=e[0],ut=e[1],dt[0]===ct&&dt[1]===ut||(at.uniform2uiv(t.locationId,e),dt[0]=ct,dt[1]=ut)},this.commitFunction[b]=function(t,e){dt=t.value,ct=e[0],ut=e[1],ft=e[2],dt[0]===ct&&dt[1]===ut&&dt[2]===ft||(at.uniform3uiv(t.locationId,e),dt[0]=ct,dt[1]=ut,dt[2]=ft)},this.commitFunction[S]=function(t,e){dt=t.value,ct=e[0],ut=e[1],ft=e[2],_t=e[3],dt[0]===ct&&dt[1]===ut&&dt[2]===ft&&dt[3]===_t||(at.uniform4uiv(t.locationId,e),dt[0]=ct,dt[1]=ut,dt[2]=ft,dt[3]=_t)},this.commitFunction[V]=function(t,e){at.uniform1iv(t.locationId,e)},this.commitFunction[w]=function(t,e){at.uniform1uiv(t.locationId,e)},this.commitFunction[X]=this.commitFunction[V],this.commitFunction[z]=function(t,e){at.uniform2iv(t.locationId,e)},this.commitFunction[K]=function(t,e){at.uniform2uiv(t.locationId,e)},this.commitFunction[j]=this.commitFunction[z],this.commitFunction[Z]=function(t,e){at.uniform3iv(t.locationId,e)},this.commitFunction[Y]=function(t,e){at.uniform3uiv(t.locationId,e)},this.commitFunction[q]=this.commitFunction[Z],this.commitFunction[Q]=function(t,e){at.uniform4iv(t.locationId,e)},this.commitFunction[J]=function(t,e){at.uniform4uiv(t.locationId,e)},this.commitFunction[$]=this.commitFunction[Q],this.commitFunction[tt]=function(t,e){at.uniformMatrix4fv(t.locationId,!1,e)},this.supportsBoneTextures=this.extTextureFloat&&this.maxVertexTextures>0;let Et=this.vertexUniformsCount;Et-=16,Et-=8,Et-=1,Et-=16,this.boneLimit=Math.floor(Et/3),this.boneLimit=Math.min(this.boneLimit,128),this.constantTexSource=this.scope.resolve("source"),this.extTextureFloat?this.isWebGL2?this.textureFloatRenderable=!!this.extColorBufferFloat:this.textureFloatRenderable=yt(at,at.FLOAT):this.textureFloatRenderable=!1,this.extColorBufferHalfFloat?this.textureHalfFloatRenderable=!!this.extColorBufferHalfFloat:this.extTextureHalfFloat?this.isWebGL2?this.textureHalfFloatRenderable=!!this.extColorBufferFloat:this.textureHalfFloatRenderable=yt(at,this.extTextureHalfFloat.HALF_FLOAT_OES):this.textureHalfFloatRenderable=!1,this.supportsMorphTargetTexturesCore="highp"===this.maxPrecision&&this.maxVertexTextures>=2,this.supportsDepthShadow=this.isWebGL2,this._textureFloatHighPrecision=void 0,this._textureHalfFloatUpdatable=void 0,this.areaLightLutFormat=et,this.extTextureHalfFloat&&this.textureHalfFloatUpdatable&&this.extTextureHalfFloatLinear?this.areaLightLutFormat=it:this.extTextureFloat&&this.extTextureFloatLinear&&(this.areaLightLutFormat=st),this.postInit()}postInit(){super.postInit(),this.gpuProfiler=new Ot(this)}destroy(){super.destroy();const t=this.gl;this.isWebGL2&&this.feedback&&t.deleteTransformFeedback(this.feedback),this.clearVertexArrayObjectCache(),this.canvas.removeEventListener("webglcontextlost",this._contextLostHandler,!1),this.canvas.removeEventListener("webglcontextrestored",this._contextRestoredHandler,!1),this._contextLostHandler=null,this._contextRestoredHandler=null,this.gl=null,super.postDestroy()}createBackbuffer(t){this.supportsStencil=this.initOptions.stencil,this.backBuffer=new At({name:"WebglFramebuffer",graphicsDevice:this,depth:this.initOptions.depth,stencil:this.supportsStencil,samples:this.samples}),this.backBuffer.impl.suppliedColorFramebuffer=t}updateBackbufferFormat(t){const e=this.gl;e.bindFramebuffer(e.FRAMEBUFFER,t);const i=this.gl.getParameter(this.gl.ALPHA_BITS);this.backBufferFormat=i?et:rt}updateBackbuffer(){const t=this.canvas.width!==this.backBufferSize.x||this.canvas.height!==this.backBufferSize.y;(this._defaultFramebufferChanged||t)&&(this._defaultFramebufferChanged&&this.updateBackbufferFormat(this._defaultFramebuffer),this._defaultFramebufferChanged=!1,this.backBufferSize.set(this.canvas.width,this.canvas.height),this.backBuffer.destroy(),this.createBackbuffer(this._defaultFramebuffer))}createVertexBufferImpl(t,e){return new Rt}createIndexBufferImpl(t){return new bt(t)}createShaderImpl(t){return new St(t)}createTextureImpl(t){return new Bt}createRenderTargetImpl(t){return new Lt}getPrecision(){const t=this.gl;let e="highp";if(t.getShaderPrecisionFormat){const i=t.getShaderPrecisionFormat(t.VERTEX_SHADER,t.HIGH_FLOAT),s=t.getShaderPrecisionFormat(t.VERTEX_SHADER,t.MEDIUM_FLOAT),r=t.getShaderPrecisionFormat(t.FRAGMENT_SHADER,t.HIGH_FLOAT),n=t.getShaderPrecisionFormat(t.FRAGMENT_SHADER,t.MEDIUM_FLOAT);if(i&&s&&r&&n){const t=i.precision>0&&r.precision>0,a=s.precision>0&&n.precision>0;t||(e=a?"mediump":"lowp")}}return e}getExtension(){for(let t=0;t<arguments.length;t++)if(-1!==this.supportedExtensions.indexOf(arguments[t]))return this.gl.getExtension(arguments[t]);return null}get extDisjointTimerQuery(){return this._extDisjointTimerQuery||this.isWebGL2&&(this._extDisjointTimerQuery=this.getExtension("EXT_disjoint_timer_query_webgl2","EXT_disjoint_timer_query")),this._extDisjointTimerQuery}initializeExtensions(){var t;const e=this.gl;if(this.supportedExtensions=null!=(t=e.getSupportedExtensions())?t:[],this._extDisjointTimerQuery=null,this.isWebGL2)this.extBlendMinmax=!0,this.extDrawBuffers=!0,this.drawBuffers=e.drawBuffers.bind(e),this.extInstancing=!0,this.extStandardDerivatives=!0,this.extTextureFloat=!0,this.extTextureHalfFloat=!0,this.textureHalfFloatFilterable=!0,this.extTextureLod=!0,this.extUintElement=!0,this.extVertexArrayObject=!0,this.extColorBufferFloat=this.getExtension("EXT_color_buffer_float"),this.extDepthTexture=!0,this.textureRG11B10Renderable=!0;else{var i;if(this.extBlendMinmax=this.getExtension("EXT_blend_minmax"),this.extDrawBuffers=this.getExtension("WEBGL_draw_buffers"),this.extInstancing=this.getExtension("ANGLE_instanced_arrays"),this.drawBuffers=null==(i=this.extDrawBuffers)?void 0:i.drawBuffersWEBGL.bind(this.extDrawBuffers),this.extInstancing){const t=this.extInstancing;e.drawArraysInstanced=t.drawArraysInstancedANGLE.bind(t),e.drawElementsInstanced=t.drawElementsInstancedANGLE.bind(t),e.vertexAttribDivisor=t.vertexAttribDivisorANGLE.bind(t)}if(this.extStandardDerivatives=this.getExtension("OES_standard_derivatives"),this.extTextureFloat=this.getExtension("OES_texture_float"),this.extTextureLod=this.getExtension("EXT_shader_texture_lod"),this.extUintElement=this.getExtension("OES_element_index_uint"),this.extVertexArrayObject=this.getExtension("OES_vertex_array_object"),this.extVertexArrayObject){const t=this.extVertexArrayObject;e.createVertexArray=t.createVertexArrayOES.bind(t),e.deleteVertexArray=t.deleteVertexArrayOES.bind(t),e.isVertexArray=t.isVertexArrayOES.bind(t),e.bindVertexArray=t.bindVertexArrayOES.bind(t)}this.extColorBufferFloat=null,this.extDepthTexture=e.getExtension("WEBGL_depth_texture"),this.extTextureHalfFloat=this.getExtension("OES_texture_half_float"),this.extTextureHalfFloatLinear=this.getExtension("OES_texture_half_float_linear"),this.textureHalfFloatFilterable=!!this.extTextureHalfFloatLinear}this.extDebugRendererInfo=this.getExtension("WEBGL_debug_renderer_info"),this.extTextureFloatLinear=this.getExtension("OES_texture_float_linear"),this.textureFloatFilterable=!!this.extTextureFloatLinear,this.extFloatBlend=this.getExtension("EXT_float_blend"),this.extTextureFilterAnisotropic=this.getExtension("EXT_texture_filter_anisotropic","WEBKIT_EXT_texture_filter_anisotropic"),this.extCompressedTextureETC1=this.getExtension("WEBGL_compressed_texture_etc1"),this.extCompressedTextureETC=this.getExtension("WEBGL_compressed_texture_etc"),this.extCompressedTexturePVRTC=this.getExtension("WEBGL_compressed_texture_pvrtc","WEBKIT_WEBGL_compressed_texture_pvrtc"),this.extCompressedTextureS3TC=this.getExtension("WEBGL_compressed_texture_s3tc","WEBKIT_WEBGL_compressed_texture_s3tc"),this.extCompressedTextureATC=this.getExtension("WEBGL_compressed_texture_atc"),this.extCompressedTextureASTC=this.getExtension("WEBGL_compressed_texture_astc"),this.extParallelShaderCompile=this.getExtension("KHR_parallel_shader_compile"),this.extColorBufferHalfFloat=this.getExtension("EXT_color_buffer_half_float")}initializeCapabilities(){var t,i;const s=this.gl;let r;const n="undefined"!=typeof navigator?navigator.userAgent:"";this.maxPrecision=this.precision=this.getPrecision();const a=s.getContextAttributes();this.supportsMsaa=null!=(t=null==a?void 0:a.antialias)&&t,this.supportsStencil=null!=(i=null==a?void 0:a.stencil)&&i,this.supportsInstancing=!!this.extInstancing,this.maxTextureSize=s.getParameter(s.MAX_TEXTURE_SIZE),this.maxCubeMapSize=s.getParameter(s.MAX_CUBE_MAP_TEXTURE_SIZE),this.maxRenderBufferSize=s.getParameter(s.MAX_RENDERBUFFER_SIZE),this.maxTextures=s.getParameter(s.MAX_TEXTURE_IMAGE_UNITS),this.maxCombinedTextures=s.getParameter(s.MAX_COMBINED_TEXTURE_IMAGE_UNITS),this.maxVertexTextures=s.getParameter(s.MAX_VERTEX_TEXTURE_IMAGE_UNITS),this.vertexUniformsCount=s.getParameter(s.MAX_VERTEX_UNIFORM_VECTORS),this.fragmentUniformsCount=s.getParameter(s.MAX_FRAGMENT_UNIFORM_VECTORS),this.isWebGL2?(this.maxDrawBuffers=s.getParameter(s.MAX_DRAW_BUFFERS),this.maxColorAttachments=s.getParameter(s.MAX_COLOR_ATTACHMENTS),this.maxVolumeSize=s.getParameter(s.MAX_3D_TEXTURE_SIZE),this.supportsMrt=!0,this.supportsVolumeTextures=!0):(r=this.extDrawBuffers,this.supportsMrt=!!r,this.maxDrawBuffers=r?s.getParameter(r.MAX_DRAW_BUFFERS_WEBGL):1,this.maxColorAttachments=r?s.getParameter(r.MAX_COLOR_ATTACHMENTS_WEBGL):1,this.maxVolumeSize=1),r=this.extDebugRendererInfo,this.unmaskedRenderer=r?s.getParameter(r.UNMASKED_RENDERER_WEBGL):"",this.unmaskedVendor=r?s.getParameter(r.UNMASKED_VENDOR_WEBGL):"";this.supportsGpuParticles=!("ARM"===this.unmaskedVendor&&n.match(/SM-[a-zA-Z0-9]+/)||this.unmaskedRenderer.match(/\bMali-G52+/)),r=this.extTextureFilterAnisotropic,this.maxAnisotropy=r?s.getParameter(r.MAX_TEXTURE_MAX_ANISOTROPY_EXT):1;const l=this.isWebGL2&&!this.forceDisableMultisampling;this.maxSamples=l?s.getParameter(s.MAX_SAMPLES):1,this.maxSamples=Math.min(this.maxSamples,4),this.samples=l&&this.backBufferAntialias?this.maxSamples:1,this.supportsAreaLights=this.isWebGL2||!e.android,this.supportsTextureFetch=this.isWebGL2,this.maxTextures<=8&&(this.supportsAreaLights=!1)}initializeRenderState(){super.initializeRenderState();const t=this.gl;t.disable(t.BLEND),t.blendFunc(t.ONE,t.ZERO),t.blendEquation(t.FUNC_ADD),t.colorMask(!0,!0,!0,!0),t.blendColor(0,0,0,0),t.enable(t.CULL_FACE),this.cullFace=t.BACK,t.cullFace(t.BACK),t.enable(t.DEPTH_TEST),t.depthFunc(t.LEQUAL),t.depthMask(!0),this.stencil=!1,t.disable(t.STENCIL_TEST),this.stencilFuncFront=this.stencilFuncBack=nt,this.stencilRefFront=this.stencilRefBack=0,this.stencilMaskFront=this.stencilMaskBack=255,t.stencilFunc(t.ALWAYS,0,255),this.stencilFailFront=this.stencilFailBack=at,this.stencilZfailFront=this.stencilZfailBack=at,this.stencilZpassFront=this.stencilZpassBack=at,this.stencilWriteMaskFront=255,this.stencilWriteMaskBack=255,t.stencilOp(t.KEEP,t.KEEP,t.KEEP),t.stencilMask(255),this.alphaToCoverage=!1,this.raster=!0,this.isWebGL2&&(t.disable(t.SAMPLE_ALPHA_TO_COVERAGE),t.disable(t.RASTERIZER_DISCARD)),this.depthBiasEnabled=!1,t.disable(t.POLYGON_OFFSET_FILL),this.clearDepth=1,t.clearDepth(1),this.clearColor=new i(0,0,0,0),t.clearColor(0,0,0,0),this.clearStencil=0,t.clearStencil(0),this.isWebGL2?t.hint(t.FRAGMENT_SHADER_DERIVATIVE_HINT,t.NICEST):this.extStandardDerivatives&&t.hint(this.extStandardDerivatives.FRAGMENT_SHADER_DERIVATIVE_HINT_OES,t.NICEST),t.enable(t.SCISSOR_TEST),t.pixelStorei(t.UNPACK_COLORSPACE_CONVERSION_WEBGL,t.NONE),this.unpackFlipY=!1,t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,!1),this.unpackPremultiplyAlpha=!1,t.pixelStorei(t.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!1),t.pixelStorei(t.UNPACK_ALIGNMENT,1)}initTextureUnits(t=16){this.textureUnits=[];for(let e=0;e<t;e++)this.textureUnits.push([null,null,null])}initializeContextCaches(){super.initializeContextCaches(),this._vaoMap=new Map,this.boundVao=null,this.activeFramebuffer=null,this.feedback=null,this.transformFeedbackBuffer=null,this.textureUnit=0,this.initTextureUnits(this.maxCombinedTextures)}loseContext(){super.loseContext();for(const t of this.shaders)t.loseContext()}restoreContext(){this.initializeExtensions(),this.initializeCapabilities(),super.restoreContext();for(const t of this.shaders)t.restoreContext()}setViewport(t,e,i,s){this.vx===t&&this.vy===e&&this.vw===i&&this.vh===s||(this.gl.viewport(t,e,i,s),this.vx=t,this.vy=e,this.vw=i,this.vh=s)}setScissor(t,e,i,s){this.sx===t&&this.sy===e&&this.sw===i&&this.sh===s||(this.gl.scissor(t,e,i,s),this.sx=t,this.sy=e,this.sw=i,this.sh=s)}setFramebuffer(t){if(this.activeFramebuffer!==t){const e=this.gl;e.bindFramebuffer(e.FRAMEBUFFER,t),this.activeFramebuffer=t}}copyRenderTarget(t,e,i,s){const r=this.gl;if(t===this.backBuffer&&(t=null),!this.isWebGL2&&s)return!1;if(i)if(e){if(t){if(!t._colorBuffer||!e._colorBuffer)return!1;if(t._colorBuffer._format!==e._colorBuffer._format)return!1}}else if(!t._colorBuffer)return!1;if(s&&t&&!t._depth){if(!t._depthBuffer||!e._depthBuffer)return!1;if(t._depthBuffer._format!==e._depthBuffer._format)return!1}if(this.isWebGL2){var n,a;const l=this.renderTarget;this.renderTarget=e,this.updateBegin();const o=t?t.impl._glFrameBuffer:null==(n=this.backBuffer)?void 0:n.impl._glFrameBuffer,h=e?e.impl._glFrameBuffer:null==(a=this.backBuffer)?void 0:a.impl._glFrameBuffer;r.bindFramebuffer(r.READ_FRAMEBUFFER,o),r.bindFramebuffer(r.DRAW_FRAMEBUFFER,h);const c=t?t.width:e?e.width:this.width,u=t?t.height:e?e.height:this.height;r.blitFramebuffer(0,0,c,u,0,0,c,u,(i?r.COLOR_BUFFER_BIT:0)|(s?r.DEPTH_BUFFER_BIT:0),r.NEAREST),this.renderTarget=l,r.bindFramebuffer(r.FRAMEBUFFER,l?l.impl._glFrameBuffer:null)}else{const i=this.getCopyShader();this.constantTexSource.setValue(t._colorBuffer),Dt(this,e,i)}return!0}getCopyShader(){return this._copyShader||(this._copyShader=new Ut(this,Ct.createDefinition(this,{name:"outputTex2D",vertexCode:Nt,fragmentCode:"\nvarying vec2 vUv0;\nuniform sampler2D source;\nvoid main(void) {\n\tgl_FragColor = texture2D(source, vUv0);\n}\n"}))),this._copyShader}frameStart(){super.frameStart(),this.updateBackbuffer(),this.gpuProfiler.frameStart()}frameEnd(){super.frameEnd(),this.gpuProfiler.frameEnd(),this.gpuProfiler.request()}startRenderPass(t){var e;const i=null!=(e=t.renderTarget)?e:this.backBuffer;this.renderTarget=i,this.updateBegin();const{width:s,height:r}=i;this.setViewport(0,0,s,r),this.setScissor(0,0,s,r);const n=t.colorOps,a=t.depthStencilOps;if(null!=n&&n.clear||a.clearDepth||a.clearStencil){let t=0;const e={};null!=n&&n.clear&&(t|=ht,e.color=[n.clearValue.r,n.clearValue.g,n.clearValue.b,n.clearValue.a]),a.clearDepth&&(t|=ct,e.depth=a.clearDepthValue),a.clearStencil&&(t|=ut,e.stencil=a.clearStencilValue),e.flags=t,this.clear(e)}this.insideRenderPass=!0}endRenderPass(t){this.unbindVertexArray();const e=this.renderTarget,i=t.colorArrayOps.length;if(e){var s;if(this.isWebGL2){Mt.length=0;const s=this.gl;for(let e=0;e<i;e++){const i=t.colorArrayOps[e];i.store||i.resolve||Mt.push(s.COLOR_ATTACHMENT0+e)}e!==this.backBuffer&&(t.depthStencilOps.storeDepth||Mt.push(s.DEPTH_ATTACHMENT),t.depthStencilOps.storeStencil||Mt.push(s.STENCIL_ATTACHMENT)),Mt.length>0&&t.fullSizeClearRect&&s.invalidateFramebuffer(s.DRAW_FRAMEBUFFER,Mt)}null!=(s=t.colorOps)&&s.resolve&&this.isWebGL2&&t.samples>1&&e.autoResolve&&e.resolve(!0,!1);for(let s=0;s<i;s++){if(t.colorArrayOps[s].mipmaps){const t=e._colorBuffers[s];t&&t.impl._glTexture&&t.mipmaps&&(t.pot||this.isWebGL2)&&(this.activeTexture(this.maxCombinedTextures-1),this.bindTexture(t),this.gl.generateMipmap(t.impl._glTarget))}}}this.insideRenderPass=!1}set defaultFramebuffer(t){this._defaultFramebuffer!==t&&(this._defaultFramebuffer=t,this._defaultFramebufferChanged=!0)}get defaultFramebuffer(){return this._defaultFramebuffer}updateBegin(){var t;if(this.boundVao=null,this._tempEnableSafariTextureUnitWorkaround)for(let t=0;t<this.textureUnits.length;++t)for(let e=0;e<3;++e)this.textureUnits[t][e]=null;const e=null!=(t=this.renderTarget)?t:this.backBuffer,i=e.impl;i.initialized||this.initRenderTarget(e),this.setFramebuffer(i._glFrameBuffer)}updateEnd(){this.unbindVertexArray();const t=this.renderTarget;if(t&&t!==this.backBuffer){this.isWebGL2&&t._samples>1&&t.autoResolve&&t.resolve();const e=t._colorBuffer;e&&e.impl._glTexture&&e.mipmaps&&(e.pot||this.isWebGL2)&&(this.activeTexture(this.maxCombinedTextures-1),this.bindTexture(e),this.gl.generateMipmap(e.impl._glTarget))}}setUnpackFlipY(t){if(this.unpackFlipY!==t){this.unpackFlipY=t;const e=this.gl;e.pixelStorei(e.UNPACK_FLIP_Y_WEBGL,t)}}setUnpackPremultiplyAlpha(t){if(this.unpackPremultiplyAlpha!==t){this.unpackPremultiplyAlpha=t;const e=this.gl;e.pixelStorei(e.UNPACK_PREMULTIPLY_ALPHA_WEBGL,t)}}activeTexture(t){this.textureUnit!==t&&(this.gl.activeTexture(this.gl.TEXTURE0+t),this.textureUnit=t)}bindTexture(t){const e=t.impl,i=e._glTarget,s=e._glTexture,r=this.textureUnit,n=this.targetToSlot[i];this.textureUnits[r][n]!==s&&(this.gl.bindTexture(i,s),this.textureUnits[r][n]=s)}bindTextureOnUnit(t,e){const i=t.impl,s=i._glTarget,r=i._glTexture,n=this.targetToSlot[s];this.textureUnits[e][n]!==r&&(this.activeTexture(e),this.gl.bindTexture(s,r),this.textureUnits[e][n]=r)}setTextureParameters(e){const i=this.gl,s=e.impl.dirtyParameterFlags,r=e.impl._glTarget;if(1&s){let t=e._minFilter;(!e.pot&&!this.isWebGL2||!e._mipmaps||e._compressed&&1===e._levels.length)&&(t===dt||t===Et?t=mt:t!==pt&&t!==Tt||(t=Ft)),i.texParameteri(r,i.TEXTURE_MIN_FILTER,this.glFilter[t])}if(2&s&&i.texParameteri(r,i.TEXTURE_MAG_FILTER,this.glFilter[e._magFilter]),4&s&&(this.isWebGL2?i.texParameteri(r,i.TEXTURE_WRAP_S,this.glAddress[e._addressU]):i.texParameteri(r,i.TEXTURE_WRAP_S,this.glAddress[e.pot?e._addressU:lt])),8&s&&(this.isWebGL2?i.texParameteri(r,i.TEXTURE_WRAP_T,this.glAddress[e._addressV]):i.texParameteri(r,i.TEXTURE_WRAP_T,this.glAddress[e.pot?e._addressV:lt])),16&s&&this.isWebGL2&&i.texParameteri(r,i.TEXTURE_WRAP_R,this.glAddress[e._addressW]),32&s&&this.isWebGL2&&i.texParameteri(r,i.TEXTURE_COMPARE_MODE,e._compareOnRead?i.COMPARE_REF_TO_TEXTURE:i.NONE),64&s&&this.isWebGL2&&i.texParameteri(r,i.TEXTURE_COMPARE_FUNC,this.glComparison[e._compareFunc]),128&s){const s=this.extTextureFilterAnisotropic;s&&i.texParameterf(r,s.TEXTURE_MAX_ANISOTROPY_EXT,t.clamp(Math.round(e._anisotropy),1,this.maxAnisotropy))}}setTexture(t,e){const i=t.impl;i._glTexture||i.initialize(this,t),i.dirtyParameterFlags>0||t._needsUpload||t._needsMipmapsUpload?(this.activeTexture(e),this.bindTexture(t),i.dirtyParameterFlags&&(this.setTextureParameters(t),i.dirtyParameterFlags=0),(t._needsUpload||t._needsMipmapsUpload)&&(i.upload(this,t),t._needsUpload=!1,t._needsMipmapsUpload=!1)):this.bindTextureOnUnit(t,e)}createVertexArray(t){let e,i;const s=t.length>1;if(s){e="";for(let i=0;i<t.length;i++){const s=t[i];e+=s.id+s.format.renderingHash}i=this._vaoMap.get(e)}if(!i){const r=this.gl;i=r.createVertexArray(),r.bindVertexArray(i),r.bindBuffer(r.ELEMENT_ARRAY_BUFFER,null);for(let e=0;e<t.length;e++){const i=t[e];r.bindBuffer(r.ARRAY_BUFFER,i.impl.bufferId);const s=i.format.elements;for(let t=0;t<s.length;t++){const e=s[t],n=ot[e.name];e.asInt?r.vertexAttribIPointer(n,e.numComponents,this.glType[e.dataType],e.stride,e.offset):r.vertexAttribPointer(n,e.numComponents,this.glType[e.dataType],e.normalize,e.stride,e.offset),r.enableVertexAttribArray(n),i.format.instancing&&r.vertexAttribDivisor(n,1)}}r.bindVertexArray(null),r.bindBuffer(r.ARRAY_BUFFER,null),s&&this._vaoMap.set(e,i)}return i}unbindVertexArray(){this.boundVao&&(this.boundVao=null,this.gl.bindVertexArray(null))}setBuffers(){const t=this.gl;let e;if(1===this.vertexBuffers.length){const t=this.vertexBuffers[0];t.impl.vao||(t.impl.vao=this.createVertexArray(this.vertexBuffers)),e=t.impl.vao}else e=this.createVertexArray(this.vertexBuffers);this.boundVao!==e&&(this.boundVao=e,t.bindVertexArray(e)),this.clearVertexBuffer();const i=this.indexBuffer?this.indexBuffer.impl.bufferId:null;t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,i)}draw(t,e,i){const s=this.gl;if(this.activateShader(this),!this.shaderValid)return;let r,n,a,l,o,h,c,u;const f=this.shader;if(!f)return;const _=f.impl.samplers,d=f.impl.uniforms;i||this.setBuffers();let E=0;for(let t=0,e=_.length;t<e;t++){if(r=_[t],n=r.scopeId.value,!n)return;if(n instanceof gt)a=n,this.setTexture(a,E),r.slot!==E&&(s.uniform1i(r.locationId,E),r.slot=E),E++;else{r.array.length=0,l=n.length;for(let t=0;t<l;t++)a=n[t],this.setTexture(a,E),r.array[t]=E,E++;s.uniform1iv(r.locationId,r.array)}}for(let t=0,e=d.length;t<e;t++)o=d[t],h=o.scopeId,c=o.version,u=h.versionObject.version,c.globalId===u.globalId&&c.revision===u.revision||(c.globalId=u.globalId,c.revision=u.revision,null!==h.value&&this.commitFunction[o.dataType](o,h.value));this.isWebGL2&&this.transformFeedbackBuffer&&(s.bindBufferBase(s.TRANSFORM_FEEDBACK_BUFFER,0,this.transformFeedbackBuffer.impl.bufferId),s.beginTransformFeedback(s.POINTS));const m=this.glPrimitive[t.type],p=t.count;if(t.indexed){const i=this.indexBuffer,r=i.impl.glFormat,n=t.base*i.bytesPerIndex;e>0?s.drawElementsInstanced(m,p,r,n,e):s.drawElements(m,p,r,n)}else{const i=t.base;e>0?s.drawArraysInstanced(m,i,p,e):s.drawArrays(m,i,p)}this.isWebGL2&&this.transformFeedbackBuffer&&(s.endTransformFeedback(),s.bindBufferBase(s.TRANSFORM_FEEDBACK_BUFFER,0,null)),this._drawCallsPerFrame++}clear(t){var e;const i=this.defaultClearOptions,s=null!=(e=(t=t||i).flags)?e:i.flags;if(0!==s){const e=this.gl;if(s&ht){var r;const e=null!=(r=t.color)?r:i.color,s=e[0],n=e[1],a=e[2],l=e[3],o=this.clearColor;s===o.r&&n===o.g&&a===o.b&&l===o.a||(this.gl.clearColor(s,n,a,l),this.clearColor.set(s,n,a,l)),this.setBlendState(It.NOBLEND)}if(s&ct){var n;const e=null!=(n=t.depth)?n:i.depth;e!==this.clearDepth&&(this.gl.clearDepth(e),this.clearDepth=e),this.setDepthState(Pt.WRITEDEPTH)}if(s&ut){var a;const s=null!=(a=t.stencil)?a:i.stencil;s!==this.clearStencil&&(this.gl.clearStencil(s),this.clearStencil=s),e.stencilMask(255),this.stencilWriteMaskFront=255,this.stencilWriteMaskBack=255}e.clear(this.glClearFlag[s])}}submit(){this.gl.flush()}readPixels(t,e,i,s,r){const n=this.gl;n.readPixels(t,e,i,s,n.RGBA,n.UNSIGNED_BYTE,r)}async readPixelsAsync(t,e,i,s,r){var n,a,l;const o=this.gl;if(!this.isWebGL2)return void this.readPixels(t,e,i,s,r);const h=null==(n=this.renderTarget.colorBuffer)?void 0:n.impl,c=null!=(a=null==h?void 0:h._glFormat)?a:o.RGBA,u=null!=(l=null==h?void 0:h._glPixelType)?l:o.UNSIGNED_BYTE,f=o.createBuffer();o.bindBuffer(o.PIXEL_PACK_BUFFER,f),o.bufferData(o.PIXEL_PACK_BUFFER,r.byteLength,o.STREAM_READ),o.readPixels(t,e,i,s,c,u,0),o.bindBuffer(o.PIXEL_PACK_BUFFER,null),await((t,e)=>{const i=o.fenceSync(o.SYNC_GPU_COMMANDS_COMPLETE,0);return this.submit(),new Promise(((s,r)=>{!function n(){const a=o.clientWaitSync(i,t,0);a===o.WAIT_FAILED?(o.deleteSync(i),r(new Error("webgl clientWaitSync sync failed"))):a===o.TIMEOUT_EXPIRED?setTimeout(n,e):(o.deleteSync(i),s())}()}))})(0,20),o.bindBuffer(o.PIXEL_PACK_BUFFER,f),o.getBufferSubData(o.PIXEL_PACK_BUFFER,0,r),o.bindBuffer(o.PIXEL_PACK_BUFFER,null),o.deleteBuffer(f)}setAlphaToCoverage(t){this.isWebGL1||this.alphaToCoverage!==t&&(this.alphaToCoverage=t,t?this.gl.enable(this.gl.SAMPLE_ALPHA_TO_COVERAGE):this.gl.disable(this.gl.SAMPLE_ALPHA_TO_COVERAGE))}setTransformFeedbackBuffer(t){if(this.transformFeedbackBuffer!==t&&(this.transformFeedbackBuffer=t,this.isWebGL2)){const e=this.gl;t?(this.feedback||(this.feedback=e.createTransformFeedback()),e.bindTransformFeedback(e.TRANSFORM_FEEDBACK,this.feedback)):e.bindTransformFeedback(e.TRANSFORM_FEEDBACK,null)}}setRaster(t){this.raster!==t&&(this.raster=t,this.isWebGL2&&(t?this.gl.disable(this.gl.RASTERIZER_DISCARD):this.gl.enable(this.gl.RASTERIZER_DISCARD)))}setStencilTest(t){if(this.stencil!==t){const e=this.gl;t?e.enable(e.STENCIL_TEST):e.disable(e.STENCIL_TEST),this.stencil=t}}setStencilFunc(t,e,i){this.stencilFuncFront===t&&this.stencilRefFront===e&&this.stencilMaskFront===i&&this.stencilFuncBack===t&&this.stencilRefBack===e&&this.stencilMaskBack===i||(this.gl.stencilFunc(this.glComparison[t],e,i),this.stencilFuncFront=this.stencilFuncBack=t,this.stencilRefFront=this.stencilRefBack=e,this.stencilMaskFront=this.stencilMaskBack=i)}setStencilFuncFront(t,e,i){if(this.stencilFuncFront!==t||this.stencilRefFront!==e||this.stencilMaskFront!==i){const s=this.gl;s.stencilFuncSeparate(s.FRONT,this.glComparison[t],e,i),this.stencilFuncFront=t,this.stencilRefFront=e,this.stencilMaskFront=i}}setStencilFuncBack(t,e,i){if(this.stencilFuncBack!==t||this.stencilRefBack!==e||this.stencilMaskBack!==i){const s=this.gl;s.stencilFuncSeparate(s.BACK,this.glComparison[t],e,i),this.stencilFuncBack=t,this.stencilRefBack=e,this.stencilMaskBack=i}}setStencilOperation(t,e,i,s){this.stencilFailFront===t&&this.stencilZfailFront===e&&this.stencilZpassFront===i&&this.stencilFailBack===t&&this.stencilZfailBack===e&&this.stencilZpassBack===i||(this.gl.stencilOp(this.glStencilOp[t],this.glStencilOp[e],this.glStencilOp[i]),this.stencilFailFront=this.stencilFailBack=t,this.stencilZfailFront=this.stencilZfailBack=e,this.stencilZpassFront=this.stencilZpassBack=i),this.stencilWriteMaskFront===s&&this.stencilWriteMaskBack===s||(this.gl.stencilMask(s),this.stencilWriteMaskFront=s,this.stencilWriteMaskBack=s)}setStencilOperationFront(t,e,i,s){this.stencilFailFront===t&&this.stencilZfailFront===e&&this.stencilZpassFront===i||(this.gl.stencilOpSeparate(this.gl.FRONT,this.glStencilOp[t],this.glStencilOp[e],this.glStencilOp[i]),this.stencilFailFront=t,this.stencilZfailFront=e,this.stencilZpassFront=i),this.stencilWriteMaskFront!==s&&(this.gl.stencilMaskSeparate(this.gl.FRONT,s),this.stencilWriteMaskFront=s)}setStencilOperationBack(t,e,i,s){this.stencilFailBack===t&&this.stencilZfailBack===e&&this.stencilZpassBack===i||(this.gl.stencilOpSeparate(this.gl.BACK,this.glStencilOp[t],this.glStencilOp[e],this.glStencilOp[i]),this.stencilFailBack=t,this.stencilZfailBack=e,this.stencilZpassBack=i),this.stencilWriteMaskBack!==s&&(this.gl.stencilMaskSeparate(this.gl.BACK,s),this.stencilWriteMaskBack=s)}setBlendState(t){const e=this.blendState;if(!e.equals(t)){const i=this.gl,{blend:s,colorOp:r,alphaOp:n,colorSrcFactor:a,colorDstFactor:l,alphaSrcFactor:o,alphaDstFactor:h}=t;if(e.blend!==s&&(s?i.enable(i.BLEND):i.disable(i.BLEND)),e.colorOp!==r||e.alphaOp!==n){const t=this.glBlendEquation;i.blendEquationSeparate(t[r],t[n])}e.colorSrcFactor===a&&e.colorDstFactor===l&&e.alphaSrcFactor===o&&e.alphaDstFactor===h||i.blendFuncSeparate(this.glBlendFunctionColor[a],this.glBlendFunctionColor[l],this.glBlendFunctionAlpha[o],this.glBlendFunctionAlpha[h]),e.allWrite!==t.allWrite&&this.gl.colorMask(t.redWrite,t.greenWrite,t.blueWrite,t.alphaWrite),e.copy(t)}}setBlendColor(t,e,i,s){const r=this.blendColor;t===r.r&&e===r.g&&i===r.b&&s===r.a||(this.gl.blendColor(t,e,i,s),r.set(t,e,i,s))}setStencilState(t,e){t||e?(this.setStencilTest(!0),t===e?(this.setStencilFunc(t.func,t.ref,t.readMask),this.setStencilOperation(t.fail,t.zfail,t.zpass,t.writeMask)):(null!=t||(t=vt.DEFAULT),this.setStencilFuncFront(t.func,t.ref,t.readMask),this.setStencilOperationFront(t.fail,t.zfail,t.zpass,t.writeMask),null!=e||(e=vt.DEFAULT),this.setStencilFuncBack(e.func,e.ref,e.readMask),this.setStencilOperationBack(e.fail,e.zfail,e.zpass,e.writeMask))):this.setStencilTest(!1)}setDepthState(t){const e=this.depthState;if(!e.equals(t)){const i=this.gl,s=t.write;e.write!==s&&i.depthMask(s);let{func:r,test:n}=t;!n&&s&&(n=!0,r=nt),e.func!==r&&i.depthFunc(this.glComparison[r]),e.test!==n&&(n?i.enable(i.DEPTH_TEST):i.disable(i.DEPTH_TEST));const{depthBias:a,depthBiasSlope:l}=t;a||l?(this.depthBiasEnabled||(this.depthBiasEnabled=!0,this.gl.enable(this.gl.POLYGON_OFFSET_FILL)),i.polygonOffset(l,a)):this.depthBiasEnabled&&(this.depthBiasEnabled=!1,this.gl.disable(this.gl.POLYGON_OFFSET_FILL)),e.copy(t)}}setCullMode(t){if(this.cullMode!==t){if(t===ft)this.gl.disable(this.gl.CULL_FACE);else{this.cullMode===ft&&this.gl.enable(this.gl.CULL_FACE);const e=this.glCull[t];this.cullFace!==e&&(this.gl.cullFace(e),this.cullFace=e)}this.cullMode=t}}setShader(t,e=!1){t!==this.shader&&(this.shader=t,this.shaderAsyncCompile=e,this.shaderValid=void 0)}activateShader(t){const{shader:e}=this,{impl:i}=e;void 0===this.shaderValid&&(e.failed?this.shaderValid=!1:e.ready||(this.shaderAsyncCompile?i.isLinked(t)?i.finalize(this,e)||(e.failed=!0,this.shaderValid=!1):this.shaderValid=!1:i.finalize(this,e)||(e.failed=!0,this.shaderValid=!1))),void 0===this.shaderValid&&(this.gl.useProgram(i.glProgram),this.shaderValid=!0)}clearVertexArrayObjectCache(){const t=this.gl;this._vaoMap.forEach(((e,i,s)=>{t.deleteVertexArray(e)})),this._vaoMap.clear()}set fullscreen(t){if(t){this.gl.canvas.requestFullscreen()}else document.exitFullscreen()}get fullscreen(){return!!document.fullscreenElement}get textureFloatHighPrecision(){return void 0===this._textureFloatHighPrecision&&(this._textureFloatHighPrecision=function(t){if(!t.textureFloatRenderable)return!1;const e=new Ut(t,Ct.createDefinition(t,{name:"ptest1",vertexCode:Nt,fragmentCode:"\nvoid main(void) { \n\tgl_FragColor = vec4(2147483648.0);\n}\n"})),i=new Ut(t,Ct.createDefinition(t,{name:"ptest2",vertexCode:Nt,fragmentCode:"\nuniform sampler2D source;\nvec4 packFloat(float depth) {\n\tconst vec4 bit_shift = vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);\n\tconst vec4 bit_mask = vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);\n\tvec4 res = mod(depth * bit_shift * vec4(255), vec4(256) ) / vec4(255);\n\tres -= res.xxyz * bit_mask;\n\treturn res;\n}\nvoid main(void) {\n\tfloat c = texture2D(source, vec2(0.0)).r;\n\tfloat diff = abs(c - 2147483648.0) / 2147483648.0;\n\tgl_FragColor = packFloat(diff);\n}\n"})),s={format:st,width:1,height:1,mipmaps:!1,minFilter:mt,magFilter:mt,name:"testFHP"},r=new gt(t,s),n=new At({colorBuffer:r,depth:!1});Dt(t,n,e),s.format=et;const a=new gt(t,s),l=new At({colorBuffer:a,depth:!1});t.constantTexSource.setValue(r),Dt(t,l,i);const o=t.activeFramebuffer;t.setFramebuffer(l.impl._glFrameBuffer);const h=new Uint8Array(4);t.readPixels(0,0,1,1,h),t.setFramebuffer(o);const c=h[0]/255/16777216+h[1]/255/65536+h[2]/255/256+h[3]/255;return r.destroy(),n.destroy(),a.destroy(),l.destroy(),e.destroy(),i.destroy(),0===c}(this)),this._textureFloatHighPrecision}get textureHalfFloatUpdatable(){return void 0===this._textureHalfFloatUpdatable&&(this.isWebGL2?this._textureHalfFloatUpdatable=!0:this._textureHalfFloatUpdatable=function(t,e){let i=!0;const s=t.createTexture();t.bindTexture(t.TEXTURE_2D,s),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE);const r=new Uint16Array(16);return t.texImage2D(t.TEXTURE_2D,0,t.RGBA,2,2,0,t.RGBA,e,r),t.getError()!==t.NO_ERROR&&(i=!1,console.log("Above error related to HALF_FLOAT_OES can be ignored, it was triggered by testing half float texture support")),t.bindTexture(t.TEXTURE_2D,null),t.deleteTexture(s),i}(this.gl,this.extTextureHalfFloat.HALF_FLOAT_OES)),this._textureHalfFloatUpdatable}}export{kt as WebglGraphicsDevice};