1 | "use strict";
|
2 | var constants = require("@pixi/constants"), extensions = require("@pixi/extensions"), settings = require("@pixi/settings");
|
3 | let CONTEXT_UID_COUNTER = 0;
|
4 | class ContextSystem {
|
5 |
|
6 | constructor(renderer) {
|
7 | this.renderer = renderer, this.webGLVersion = 1, this.extensions = {}, this.supports = {
|
8 | uint32Indices: !1
|
9 | }, this.handleContextLost = this.handleContextLost.bind(this), this.handleContextRestored = this.handleContextRestored.bind(this);
|
10 | }
|
11 | |
12 |
|
13 |
|
14 |
|
15 | get isLost() {
|
16 | return !this.gl || this.gl.isContextLost();
|
17 | }
|
18 | |
19 |
|
20 |
|
21 |
|
22 | contextChange(gl) {
|
23 | this.gl = gl, this.renderer.gl = gl, this.renderer.CONTEXT_UID = CONTEXT_UID_COUNTER++;
|
24 | }
|
25 | init(options) {
|
26 | if (options.context)
|
27 | this.initFromContext(options.context);
|
28 | else {
|
29 | const alpha = this.renderer.background.alpha < 1, premultipliedAlpha = options.premultipliedAlpha;
|
30 | this.preserveDrawingBuffer = options.preserveDrawingBuffer, this.useContextAlpha = options.useContextAlpha, this.powerPreference = options.powerPreference, this.initFromOptions({
|
31 | alpha,
|
32 | premultipliedAlpha,
|
33 | antialias: options.antialias,
|
34 | stencil: !0,
|
35 | preserveDrawingBuffer: options.preserveDrawingBuffer,
|
36 | powerPreference: options.powerPreference
|
37 | });
|
38 | }
|
39 | }
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 | initFromContext(gl) {
|
46 | this.gl = gl, this.validateContext(gl), this.renderer.gl = gl, this.renderer.CONTEXT_UID = CONTEXT_UID_COUNTER++, this.renderer.runners.contextChange.emit(gl);
|
47 | const view = this.renderer.view;
|
48 | view.addEventListener !== void 0 && (view.addEventListener("webglcontextlost", this.handleContextLost, !1), view.addEventListener("webglcontextrestored", this.handleContextRestored, !1));
|
49 | }
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | initFromOptions(options) {
|
57 | const gl = this.createContext(this.renderer.view, options);
|
58 | this.initFromContext(gl);
|
59 | }
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | createContext(canvas, options) {
|
69 | let gl;
|
70 | if (settings.settings.PREFER_ENV >= constants.ENV.WEBGL2 && (gl = canvas.getContext("webgl2", options)), gl)
|
71 | this.webGLVersion = 2;
|
72 | else if (this.webGLVersion = 1, gl = canvas.getContext("webgl", options) || canvas.getContext("experimental-webgl", options), !gl)
|
73 | throw new Error("This browser does not support WebGL. Try using the canvas renderer");
|
74 | return this.gl = gl, this.getExtensions(), this.gl;
|
75 | }
|
76 |
|
77 | getExtensions() {
|
78 | const { gl } = this, common = {
|
79 | loseContext: gl.getExtension("WEBGL_lose_context"),
|
80 | anisotropicFiltering: gl.getExtension("EXT_texture_filter_anisotropic"),
|
81 | floatTextureLinear: gl.getExtension("OES_texture_float_linear"),
|
82 | s3tc: gl.getExtension("WEBGL_compressed_texture_s3tc"),
|
83 | s3tc_sRGB: gl.getExtension("WEBGL_compressed_texture_s3tc_srgb"),
|
84 |
|
85 | etc: gl.getExtension("WEBGL_compressed_texture_etc"),
|
86 | etc1: gl.getExtension("WEBGL_compressed_texture_etc1"),
|
87 | pvrtc: gl.getExtension("WEBGL_compressed_texture_pvrtc") || gl.getExtension("WEBKIT_WEBGL_compressed_texture_pvrtc"),
|
88 | atc: gl.getExtension("WEBGL_compressed_texture_atc"),
|
89 | astc: gl.getExtension("WEBGL_compressed_texture_astc"),
|
90 | bptc: gl.getExtension("EXT_texture_compression_bptc")
|
91 | };
|
92 | this.webGLVersion === 1 ? Object.assign(this.extensions, common, {
|
93 | drawBuffers: gl.getExtension("WEBGL_draw_buffers"),
|
94 | depthTexture: gl.getExtension("WEBGL_depth_texture"),
|
95 | vertexArrayObject: gl.getExtension("OES_vertex_array_object") || gl.getExtension("MOZ_OES_vertex_array_object") || gl.getExtension("WEBKIT_OES_vertex_array_object"),
|
96 | uint32ElementIndex: gl.getExtension("OES_element_index_uint"),
|
97 |
|
98 | floatTexture: gl.getExtension("OES_texture_float"),
|
99 | floatTextureLinear: gl.getExtension("OES_texture_float_linear"),
|
100 | textureHalfFloat: gl.getExtension("OES_texture_half_float"),
|
101 | textureHalfFloatLinear: gl.getExtension("OES_texture_half_float_linear")
|
102 | }) : this.webGLVersion === 2 && Object.assign(this.extensions, common, {
|
103 |
|
104 | colorBufferFloat: gl.getExtension("EXT_color_buffer_float")
|
105 | });
|
106 | }
|
107 | |
108 |
|
109 |
|
110 |
|
111 | handleContextLost(event) {
|
112 | event.preventDefault(), setTimeout(() => {
|
113 | this.gl.isContextLost() && this.extensions.loseContext && this.extensions.loseContext.restoreContext();
|
114 | }, 0);
|
115 | }
|
116 |
|
117 | handleContextRestored() {
|
118 | this.renderer.runners.contextChange.emit(this.gl);
|
119 | }
|
120 | destroy() {
|
121 | const view = this.renderer.view;
|
122 | this.renderer = null, view.removeEventListener !== void 0 && (view.removeEventListener("webglcontextlost", this.handleContextLost), view.removeEventListener("webglcontextrestored", this.handleContextRestored)), this.gl.useProgram(null), this.extensions.loseContext && this.extensions.loseContext.loseContext();
|
123 | }
|
124 |
|
125 | postrender() {
|
126 | this.renderer.objectRenderer.renderingToScreen && this.gl.flush();
|
127 | }
|
128 | |
129 |
|
130 |
|
131 |
|
132 | validateContext(gl) {
|
133 | const attributes = gl.getContextAttributes(), isWebGl2 = "WebGL2RenderingContext" in globalThis && gl instanceof globalThis.WebGL2RenderingContext;
|
134 | isWebGl2 && (this.webGLVersion = 2), attributes && !attributes.stencil && console.warn("Provided WebGL context does not have a stencil buffer, masks may not render correctly");
|
135 | const hasuint32 = isWebGl2 || !!gl.getExtension("OES_element_index_uint");
|
136 | this.supports.uint32Indices = hasuint32, hasuint32 || console.warn("Provided WebGL context does not support 32 index buffer, complex graphics may not render correctly");
|
137 | }
|
138 | }
|
139 | ContextSystem.defaultOptions = {
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 | context: null,
|
146 | |
147 |
|
148 |
|
149 |
|
150 |
|
151 | antialias: !1,
|
152 | |
153 |
|
154 |
|
155 |
|
156 |
|
157 | premultipliedAlpha: !0,
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 | preserveDrawingBuffer: !1,
|
164 | |
165 |
|
166 |
|
167 |
|
168 |
|
169 | powerPreference: "default"
|
170 | },
|
171 | ContextSystem.extension = {
|
172 | type: extensions.ExtensionType.RendererSystem,
|
173 | name: "context"
|
174 | };
|
175 | extensions.extensions.add(ContextSystem);
|
176 | exports.ContextSystem = ContextSystem;
|
177 |
|