1 | import _regeneratorRuntime from "@babel/runtime/regenerator";
|
2 | import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
3 | import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
4 | import _createClass from "@babel/runtime/helpers/createClass";
|
5 |
|
6 | var _dec, _class, _temp;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | import { gl } from '@antv/g-webgpu-core';
|
13 | import { injectable } from 'inversify';
|
14 | import regl from 'regl';
|
15 | import ReglAttribute from './ReglAttribute';
|
16 | import ReglBuffer from './ReglBuffer';
|
17 | import ReglComputeModel from './ReglComputeModel';
|
18 | import ReglElements from './ReglElements';
|
19 | import ReglFramebuffer from './ReglFramebuffer';
|
20 | import ReglModel from './ReglModel';
|
21 | import ReglTexture2D from './ReglTexture2D';
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export var WebGLEngine = (_dec = injectable(), _dec(_class = (_temp = function () {
|
27 | function WebGLEngine() {
|
28 | var _this = this;
|
29 |
|
30 | _classCallCheck(this, WebGLEngine);
|
31 |
|
32 | this.supportWebGPU = false;
|
33 | this.useWGSL = false;
|
34 | this.$canvas = void 0;
|
35 | this.gl = void 0;
|
36 | this.inited = void 0;
|
37 |
|
38 | this.createModel = function () {
|
39 | var _ref = _asyncToGenerator( _regeneratorRuntime.mark(function _callee2(options) {
|
40 | return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
41 | while (1) {
|
42 | switch (_context2.prev = _context2.next) {
|
43 | case 0:
|
44 | if (!options.uniforms) {
|
45 | _context2.next = 3;
|
46 | break;
|
47 | }
|
48 |
|
49 | _context2.next = 3;
|
50 | return Promise.all(Object.keys(options.uniforms).map( function () {
|
51 | var _ref2 = _asyncToGenerator( _regeneratorRuntime.mark(function _callee(name) {
|
52 | var texture;
|
53 | return _regeneratorRuntime.wrap(function _callee$(_context) {
|
54 | while (1) {
|
55 | switch (_context.prev = _context.next) {
|
56 | case 0:
|
57 | if (!(options.uniforms[name] && options.uniforms[name].load !== undefined)) {
|
58 | _context.next = 5;
|
59 | break;
|
60 | }
|
61 |
|
62 | _context.next = 3;
|
63 | return options.uniforms[name].load();
|
64 |
|
65 | case 3:
|
66 | texture = _context.sent;
|
67 |
|
68 | options.uniforms[name] = texture;
|
69 |
|
70 | case 5:
|
71 | case "end":
|
72 | return _context.stop();
|
73 | }
|
74 | }
|
75 | }, _callee);
|
76 | }));
|
77 |
|
78 | return function (_x2) {
|
79 | return _ref2.apply(this, arguments);
|
80 | };
|
81 | }()));
|
82 |
|
83 | case 3:
|
84 | return _context2.abrupt("return", new ReglModel(_this.gl, options));
|
85 |
|
86 | case 4:
|
87 | case "end":
|
88 | return _context2.stop();
|
89 | }
|
90 | }
|
91 | }, _callee2);
|
92 | }));
|
93 |
|
94 | return function (_x) {
|
95 | return _ref.apply(this, arguments);
|
96 | };
|
97 | }();
|
98 |
|
99 | this.createAttribute = function (options) {
|
100 | return new ReglAttribute(_this.gl, options);
|
101 | };
|
102 |
|
103 | this.createBuffer = function (options) {
|
104 | return new ReglBuffer(_this.gl, options);
|
105 | };
|
106 |
|
107 | this.createElements = function (options) {
|
108 | return new ReglElements(_this.gl, options);
|
109 | };
|
110 |
|
111 | this.createTexture2D = function (options) {
|
112 | return new ReglTexture2D(_this.gl, options);
|
113 | };
|
114 |
|
115 | this.createFramebuffer = function (options) {
|
116 | return new ReglFramebuffer(_this.gl, options);
|
117 | };
|
118 |
|
119 | this.useFramebuffer = function (framebuffer, drawCommands) {
|
120 | _this.gl({
|
121 | framebuffer: framebuffer ? framebuffer.get() : null
|
122 | })(drawCommands);
|
123 | };
|
124 |
|
125 | this.createComputeModel = function () {
|
126 | var _ref3 = _asyncToGenerator( _regeneratorRuntime.mark(function _callee3(context) {
|
127 | return _regeneratorRuntime.wrap(function _callee3$(_context3) {
|
128 | while (1) {
|
129 | switch (_context3.prev = _context3.next) {
|
130 | case 0:
|
131 | return _context3.abrupt("return", new ReglComputeModel(_this.gl, context));
|
132 |
|
133 | case 1:
|
134 | case "end":
|
135 | return _context3.stop();
|
136 | }
|
137 | }
|
138 | }, _callee3);
|
139 | }));
|
140 |
|
141 | return function (_x3) {
|
142 | return _ref3.apply(this, arguments);
|
143 | };
|
144 | }();
|
145 |
|
146 | this.clear = function (options) {
|
147 |
|
148 | var color = options.color,
|
149 | depth = options.depth,
|
150 | stencil = options.stencil,
|
151 | _options$framebuffer = options.framebuffer,
|
152 | framebuffer = _options$framebuffer === void 0 ? null : _options$framebuffer;
|
153 | var reglClearOptions = {
|
154 | color: color,
|
155 | depth: depth,
|
156 | stencil: stencil
|
157 | };
|
158 | reglClearOptions.framebuffer = framebuffer === null ? framebuffer : framebuffer.get();
|
159 |
|
160 | _this.gl.clear(reglClearOptions);
|
161 | };
|
162 |
|
163 | this.setScissor = function (scissor) {
|
164 | if (_this.gl && _this.gl._gl) {
|
165 |
|
166 | if (scissor.enable && scissor.box) {
|
167 |
|
168 | _this.gl._gl.enable(gl.SCISSOR_TEST);
|
169 |
|
170 | _this.gl._gl.scissor(scissor.box.x, scissor.box.y, scissor.box.width, scissor.box.height);
|
171 | } else {
|
172 | _this.gl._gl.disable(gl.SCISSOR_TEST);
|
173 | }
|
174 |
|
175 | _this.gl._refresh();
|
176 | }
|
177 | };
|
178 |
|
179 | this.viewport = function (_ref4) {
|
180 | var x = _ref4.x,
|
181 | y = _ref4.y,
|
182 | width = _ref4.width,
|
183 | height = _ref4.height;
|
184 |
|
185 | if (_this.gl && _this.gl._gl) {
|
186 |
|
187 |
|
188 | _this.gl._gl.viewport(x, y, width, height);
|
189 |
|
190 | _this.gl._refresh();
|
191 | }
|
192 | };
|
193 |
|
194 | this.readPixels = function (options) {
|
195 | var framebuffer = options.framebuffer,
|
196 | x = options.x,
|
197 | y = options.y,
|
198 | width = options.width,
|
199 | height = options.height;
|
200 | var readPixelsOptions = {
|
201 | x: x,
|
202 | y: y,
|
203 | width: width,
|
204 | height: height
|
205 | };
|
206 |
|
207 | if (framebuffer) {
|
208 | readPixelsOptions.framebuffer = framebuffer.get();
|
209 | }
|
210 |
|
211 | return _this.gl.read(readPixelsOptions);
|
212 | };
|
213 |
|
214 | this.getCanvas = function () {
|
215 | return _this.$canvas;
|
216 | };
|
217 |
|
218 | this.getGLContext = function () {
|
219 | return _this.gl._gl;
|
220 | };
|
221 |
|
222 | this.destroy = function () {
|
223 | if (_this.gl) {
|
224 |
|
225 | _this.gl.destroy();
|
226 |
|
227 | _this.inited = false;
|
228 | }
|
229 | };
|
230 | }
|
231 |
|
232 | _createClass(WebGLEngine, [{
|
233 | key: "init",
|
234 | value: function () {
|
235 | var _init = _asyncToGenerator( _regeneratorRuntime.mark(function _callee4(cfg) {
|
236 | return _regeneratorRuntime.wrap(function _callee4$(_context4) {
|
237 | while (1) {
|
238 | switch (_context4.prev = _context4.next) {
|
239 | case 0:
|
240 | if (!this.inited) {
|
241 | _context4.next = 2;
|
242 | break;
|
243 | }
|
244 |
|
245 | return _context4.abrupt("return");
|
246 |
|
247 | case 2:
|
248 | this.$canvas = cfg.canvas;
|
249 |
|
250 | _context4.next = 5;
|
251 | return new Promise(function (resolve, reject) {
|
252 | regl({
|
253 | canvas: cfg.canvas,
|
254 | attributes: {
|
255 | alpha: true,
|
256 |
|
257 |
|
258 | antialias: cfg.antialias,
|
259 | premultipliedAlpha: true
|
260 |
|
261 | },
|
262 | pixelRatio: 1,
|
263 |
|
264 | extensions: ['OES_element_index_uint', 'OES_texture_float', 'OES_standard_derivatives',
|
265 | 'angle_instanced_arrays'
|
266 | ],
|
267 | optionalExtensions: ['EXT_texture_filter_anisotropic', 'EXT_blend_minmax', 'WEBGL_depth_texture'],
|
268 | profile: true,
|
269 | onDone: function onDone(err, r) {
|
270 | if (err || !r) {
|
271 | reject(err);
|
272 | }
|
273 |
|
274 |
|
275 | resolve(r);
|
276 | }
|
277 | });
|
278 | });
|
279 |
|
280 | case 5:
|
281 | this.gl = _context4.sent;
|
282 | this.inited = true;
|
283 |
|
284 | case 7:
|
285 | case "end":
|
286 | return _context4.stop();
|
287 | }
|
288 | }
|
289 | }, _callee4, this);
|
290 | }));
|
291 |
|
292 | function init(_x4) {
|
293 | return _init.apply(this, arguments);
|
294 | }
|
295 |
|
296 | return init;
|
297 | }()
|
298 | }, {
|
299 | key: "isFloatSupported",
|
300 | value: function isFloatSupported() {
|
301 |
|
302 |
|
303 | return this.gl.limits.readFloat;
|
304 | }
|
305 | }, {
|
306 | key: "beginFrame",
|
307 | value: function beginFrame() {
|
308 | }
|
309 | }, {
|
310 | key: "endFrame",
|
311 | value: function endFrame() {
|
312 | }
|
313 | }]);
|
314 |
|
315 | return WebGLEngine;
|
316 | }(), _temp)) || _class);
|
317 |
|
\ | No newline at end of file |