UNPKG

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