UNPKG

8.21 kBJavaScriptView Raw
1import _regeneratorRuntime from "@babel/runtime/regenerator";
2import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4import _createClass from "@babel/runtime/helpers/createClass";
5
6var _dec, _class, _temp;
7
8/**
9 * render w/ regl
10 * @see https://github.com/regl-project/regl/blob/gh-pages/API.md
11 */
12import { gl } from '@antv/g-webgpu-core';
13import { injectable } from 'inversify';
14import regl from 'regl';
15import ReglAttribute from './ReglAttribute';
16import ReglBuffer from './ReglBuffer';
17import ReglComputeModel from './ReglComputeModel';
18import ReglElements from './ReglElements';
19import ReglFramebuffer from './ReglFramebuffer';
20import ReglModel from './ReglModel';
21import ReglTexture2D from './ReglTexture2D';
22/**
23 * regl renderer
24 */
25
26export var WebGLEngine = (_dec = injectable(), _dec(_class = (_temp = /*#__PURE__*/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 = /*#__PURE__*/function () {
39 var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(options) {
40 return _regeneratorRuntime.wrap(function _callee$(_context) {
41 while (1) {
42 switch (_context.prev = _context.next) {
43 case 0:
44 return _context.abrupt("return", new ReglModel(_this.gl, options));
45
46 case 1:
47 case "end":
48 return _context.stop();
49 }
50 }
51 }, _callee);
52 }));
53
54 return function (_x) {
55 return _ref.apply(this, arguments);
56 };
57 }();
58
59 this.createAttribute = function (options) {
60 return new ReglAttribute(_this.gl, options);
61 };
62
63 this.createBuffer = function (options) {
64 return new ReglBuffer(_this.gl, options);
65 };
66
67 this.createElements = function (options) {
68 return new ReglElements(_this.gl, options);
69 };
70
71 this.createTexture2D = function (options) {
72 return new ReglTexture2D(_this.gl, options);
73 };
74
75 this.createFramebuffer = function (options) {
76 return new ReglFramebuffer(_this.gl, options);
77 };
78
79 this.useFramebuffer = function (framebuffer, drawCommands) {
80 _this.gl({
81 framebuffer: framebuffer ? framebuffer.get() : null
82 })(drawCommands);
83 };
84
85 this.createComputeModel = /*#__PURE__*/function () {
86 var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(context) {
87 return _regeneratorRuntime.wrap(function _callee2$(_context2) {
88 while (1) {
89 switch (_context2.prev = _context2.next) {
90 case 0:
91 return _context2.abrupt("return", new ReglComputeModel(_this.gl, context));
92
93 case 1:
94 case "end":
95 return _context2.stop();
96 }
97 }
98 }, _callee2);
99 }));
100
101 return function (_x2) {
102 return _ref2.apply(this, arguments);
103 };
104 }();
105
106 this.clear = function (options) {
107 // @see https://github.com/regl-project/regl/blob/gh-pages/API.md#clear-the-draw-buffer
108 var color = options.color,
109 depth = options.depth,
110 stencil = options.stencil,
111 _options$framebuffer = options.framebuffer,
112 framebuffer = _options$framebuffer === void 0 ? null : _options$framebuffer;
113 var reglClearOptions = {
114 color: color,
115 depth: depth,
116 stencil: stencil
117 };
118 reglClearOptions.framebuffer = framebuffer === null ? framebuffer : framebuffer.get();
119
120 _this.gl.clear(reglClearOptions);
121 };
122
123 this.setScissor = function (scissor) {
124 if (_this.gl && _this.gl._gl) {
125 // https://developer.mozilla.org/zh-CN/docs/Web/API/WebGLRenderingContext/scissor
126 if (scissor.enable && scissor.box) {
127 // console.log(scissor.box);
128 _this.gl._gl.enable(gl.SCISSOR_TEST);
129
130 _this.gl._gl.scissor(scissor.box.x, scissor.box.y, scissor.box.width, scissor.box.height);
131 } else {
132 _this.gl._gl.disable(gl.SCISSOR_TEST);
133 }
134
135 _this.gl._refresh();
136 }
137 };
138
139 this.viewport = function (_ref3) {
140 var x = _ref3.x,
141 y = _ref3.y,
142 width = _ref3.width,
143 height = _ref3.height;
144
145 if (_this.gl && _this.gl._gl) {
146 // use WebGL context directly
147 // @see https://github.com/regl-project/regl/blob/gh-pages/API.md#unsafe-escape-hatch
148 _this.gl._gl.viewport(x, y, width, height);
149
150 _this.gl._refresh();
151 }
152 };
153
154 this.readPixels = function (options) {
155 var framebuffer = options.framebuffer,
156 x = options.x,
157 y = options.y,
158 width = options.width,
159 height = options.height;
160 var readPixelsOptions = {
161 x: x,
162 y: y,
163 width: width,
164 height: height
165 };
166
167 if (framebuffer) {
168 readPixelsOptions.framebuffer = framebuffer.get();
169 }
170
171 return _this.gl.read(readPixelsOptions);
172 };
173
174 this.getCanvas = function () {
175 return _this.$canvas;
176 };
177
178 this.getGLContext = function () {
179 return _this.gl._gl;
180 };
181
182 this.destroy = function () {
183 if (_this.gl) {
184 // @see https://github.com/regl-project/regl/blob/gh-pages/API.md#clean-up
185 _this.gl.destroy();
186
187 _this.inited = false;
188 }
189 };
190 }
191
192 _createClass(WebGLEngine, [{
193 key: "init",
194 value: function () {
195 var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(cfg) {
196 return _regeneratorRuntime.wrap(function _callee3$(_context3) {
197 while (1) {
198 switch (_context3.prev = _context3.next) {
199 case 0:
200 if (!this.inited) {
201 _context3.next = 2;
202 break;
203 }
204
205 return _context3.abrupt("return");
206
207 case 2:
208 this.$canvas = cfg.canvas; // tslint:disable-next-line:typedef
209
210 _context3.next = 5;
211 return new Promise(function (resolve, reject) {
212 regl({
213 canvas: cfg.canvas,
214 attributes: {
215 alpha: true,
216 // use TAA instead of MSAA
217 // @see https://www.khronos.org/registry/webgl/specs/1.0/#5.2.1
218 antialias: cfg.antialias,
219 premultipliedAlpha: true // preserveDrawingBuffer: false,
220
221 },
222 pixelRatio: 1,
223 // TODO: use extensions
224 extensions: ['OES_element_index_uint', 'OES_texture_float', 'OES_standard_derivatives', // wireframe
225 'angle_instanced_arrays' // VSM shadow map
226 ],
227 optionalExtensions: ['EXT_texture_filter_anisotropic', 'EXT_blend_minmax', 'WEBGL_depth_texture'],
228 profile: true,
229 onDone: function onDone(err, r) {
230 if (err || !r) {
231 reject(err);
232 } // @ts-ignore
233
234
235 resolve(r);
236 }
237 });
238 });
239
240 case 5:
241 this.gl = _context3.sent;
242 this.inited = true;
243
244 case 7:
245 case "end":
246 return _context3.stop();
247 }
248 }
249 }, _callee3, this);
250 }));
251
252 function init(_x3) {
253 return _init.apply(this, arguments);
254 }
255
256 return init;
257 }()
258 }, {
259 key: "isFloatSupported",
260 value: function isFloatSupported() {
261 // @see https://github.com/antvis/GWebGPUEngine/issues/26
262 // @ts-ignore
263 return this.gl.limits.readFloat;
264 }
265 }, {
266 key: "beginFrame",
267 value: function beginFrame() {//
268 }
269 }, {
270 key: "endFrame",
271 value: function endFrame() {//
272 }
273 }]);
274
275 return WebGLEngine;
276}(), _temp)) || _class);
277//# sourceMappingURL=index.js.map
\No newline at end of file