UNPKG

4.72 kBJavaScriptView Raw
1import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2import _createClass from "@babel/runtime/helpers/createClass";
3import { gl } from '@antv/g-webgpu-core';
4import * as WebGPUConstants from '@webgpu/types/dist/constants';
5import { filterMap, formatMap, wrapModeMap } from './constants';
6/**
7 * adaptor for regl.Buffer
8 * @see https://github.com/regl-project/regl/blob/gh-pages/API.md#buffers
9 */
10
11var WebGPUTexture2D = /*#__PURE__*/function () {
12 function WebGPUTexture2D(engine, options) {
13 _classCallCheck(this, WebGPUTexture2D);
14
15 this.engine = engine;
16 this.options = options;
17 this.texture = void 0;
18 this.sampler = void 0;
19 this.width = void 0;
20 this.height = void 0;
21 this.createTexture();
22 }
23
24 _createClass(WebGPUTexture2D, [{
25 key: "get",
26 value: function get() {
27 return {
28 texture: this.texture,
29 sampler: this.sampler
30 };
31 }
32 }, {
33 key: "update",
34 value: function update() {// TODO
35 }
36 }, {
37 key: "resize",
38 value: function resize(_ref) {
39 var width = _ref.width,
40 height = _ref.height;
41
42 // TODO: it seems that Texture doesn't support `resize`
43 if (width !== this.width || height !== this.height) {
44 this.destroy();
45 this.createTexture();
46 }
47
48 this.width = width;
49 this.height = height;
50 }
51 }, {
52 key: "destroy",
53 value: function destroy() {
54 if (this.texture) {
55 this.texture.destroy();
56 }
57 }
58 }, {
59 key: "createTexture",
60 value: function createTexture() {
61 var _this$options = this.options,
62 data = _this$options.data,
63 _this$options$type = _this$options.type,
64 type = _this$options$type === void 0 ? gl.UNSIGNED_BYTE : _this$options$type,
65 width = _this$options.width,
66 height = _this$options.height,
67 _this$options$flipY = _this$options.flipY,
68 flipY = _this$options$flipY === void 0 ? false : _this$options$flipY,
69 _this$options$format = _this$options.format,
70 format = _this$options$format === void 0 ? gl.RGBA : _this$options$format,
71 _this$options$mipmap = _this$options.mipmap,
72 mipmap = _this$options$mipmap === void 0 ? false : _this$options$mipmap,
73 _this$options$wrapS = _this$options.wrapS,
74 wrapS = _this$options$wrapS === void 0 ? gl.CLAMP_TO_EDGE : _this$options$wrapS,
75 _this$options$wrapT = _this$options.wrapT,
76 wrapT = _this$options$wrapT === void 0 ? gl.CLAMP_TO_EDGE : _this$options$wrapT,
77 _this$options$aniso = _this$options.aniso,
78 aniso = _this$options$aniso === void 0 ? 0 : _this$options$aniso,
79 _this$options$alignme = _this$options.alignment,
80 alignment = _this$options$alignme === void 0 ? 1 : _this$options$alignme,
81 _this$options$premult = _this$options.premultiplyAlpha,
82 premultiplyAlpha = _this$options$premult === void 0 ? false : _this$options$premult,
83 _this$options$mag = _this$options.mag,
84 mag = _this$options$mag === void 0 ? gl.NEAREST : _this$options$mag,
85 _this$options$min = _this$options.min,
86 min = _this$options$min === void 0 ? gl.NEAREST : _this$options$min,
87 _this$options$colorSp = _this$options.colorSpace,
88 colorSpace = _this$options$colorSp === void 0 ? gl.BROWSER_DEFAULT_WEBGL : _this$options$colorSp,
89 usage = _this$options.usage;
90 this.width = width;
91 this.height = height;
92 this.texture = this.engine.device.createTexture({
93 size: [width, height, 1],
94 // TODO: arrayLayerCount is deprecated: use size.depth
95 // arrayLayerCount: 1,
96 mipLevelCount: 1,
97 // TODO: https://gpuweb.github.io/gpuweb/#dom-gputextureviewdescriptor-miplevelcount
98 sampleCount: 1,
99 dimension: WebGPUConstants.TextureDimension.E2d,
100 format: formatMap[format],
101 // could throw texture binding usage mismatch
102 usage: usage || WebGPUConstants.TextureUsage.Sampled | WebGPUConstants.TextureUsage.CopyDst
103 });
104
105 if (!usage || usage & WebGPUConstants.TextureUsage.Sampled) {
106 this.sampler = this.engine.device.createSampler({
107 addressModeU: wrapModeMap[wrapS],
108 addressModeV: wrapModeMap[wrapT],
109 addressModeW: wrapModeMap[wrapS],
110 // TODO: same as addressModeU
111 magFilter: filterMap[mag],
112 minFilter: filterMap[min],
113 maxAnisotropy: aniso // @see https://gpuweb.github.io/gpuweb/#dom-gpusamplerdescriptor-maxanisotropy
114
115 });
116 }
117 }
118 }]);
119
120 return WebGPUTexture2D;
121}();
122
123export { WebGPUTexture2D as default };
124//# sourceMappingURL=WebGPUTexture2D.js.map
\No newline at end of file