@antv/l7-renderer 2.20.12 → 2.20.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,181 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/device/DeviceCache.ts
20
+ var DeviceCache_exports = {};
21
+ __export(DeviceCache_exports, {
22
+ RenderCache: () => RenderCache
23
+ });
24
+ module.exports = __toCommonJS(DeviceCache_exports);
25
+ var import_g_device_api = require("@antv/g-device-api");
26
+ var import_HashMap = require("./utils/HashMap");
27
+ function blendStateHash(hash, a) {
28
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.blendMode);
29
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.blendSrcFactor);
30
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.blendDstFactor);
31
+ return hash;
32
+ }
33
+ function attachmentStateHash(hash, a) {
34
+ hash = blendStateHash(hash, a.rgbBlendState);
35
+ hash = blendStateHash(hash, a.alphaBlendState);
36
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.channelWriteMask);
37
+ return hash;
38
+ }
39
+ function colorHash(hash, a) {
40
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(
41
+ hash,
42
+ a.r << 24 | a.g << 16 | a.b << 8 | a.a
43
+ );
44
+ return hash;
45
+ }
46
+ function megaStateDescriptorHash(hash, a) {
47
+ var _a, _b, _c, _d, _e, _f, _g, _h;
48
+ for (let i = 0; i < a.attachmentsState.length; i++)
49
+ hash = attachmentStateHash(hash, a.attachmentsState[i]);
50
+ hash = colorHash(hash, a.blendConstant || import_g_device_api.TransparentBlack);
51
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.depthCompare);
52
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.depthWrite ? 1 : 0);
53
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, (_a = a.stencilFront) == null ? void 0 : _a.compare);
54
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, (_b = a.stencilFront) == null ? void 0 : _b.passOp);
55
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, (_c = a.stencilFront) == null ? void 0 : _c.failOp);
56
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, (_d = a.stencilFront) == null ? void 0 : _d.depthFailOp);
57
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, (_e = a.stencilBack) == null ? void 0 : _e.compare);
58
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, (_f = a.stencilBack) == null ? void 0 : _f.passOp);
59
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, (_g = a.stencilBack) == null ? void 0 : _g.failOp);
60
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, (_h = a.stencilBack) == null ? void 0 : _h.depthFailOp);
61
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.stencilWrite ? 1 : 0);
62
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.cullMode);
63
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.frontFace ? 1 : 0);
64
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.polygonOffset ? 1 : 0);
65
+ return hash;
66
+ }
67
+ function renderPipelineDescriptorHash(a) {
68
+ let hash = 0;
69
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.program.id);
70
+ if (a.inputLayout !== null)
71
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.inputLayout.id);
72
+ hash = megaStateDescriptorHash(hash, a.megaStateDescriptor);
73
+ for (let i = 0; i < a.colorAttachmentFormats.length; i++)
74
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.colorAttachmentFormats[i] || 0);
75
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, a.depthStencilAttachmentFormat || 0);
76
+ return (0, import_HashMap.hashCodeNumberFinish)(hash);
77
+ }
78
+ function bindingsDescriptorHash(a) {
79
+ let hash = 0;
80
+ if (a.samplerBindings) {
81
+ for (let i = 0; i < a.samplerBindings.length; i++) {
82
+ const binding = a.samplerBindings[i];
83
+ if (binding !== null && binding.texture !== null)
84
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, binding.texture.id);
85
+ }
86
+ }
87
+ if (a.uniformBufferBindings) {
88
+ for (let i = 0; i < a.uniformBufferBindings.length; i++) {
89
+ const binding = a.uniformBufferBindings[i];
90
+ if (binding !== null && binding.buffer !== null) {
91
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, binding.buffer.id);
92
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, binding.binding);
93
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, binding.offset);
94
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, binding.size);
95
+ }
96
+ }
97
+ }
98
+ if (a.storageBufferBindings) {
99
+ for (let i = 0; i < a.storageBufferBindings.length; i++) {
100
+ const binding = a.storageBufferBindings[i];
101
+ if (binding !== null && binding.buffer !== null) {
102
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, binding.buffer.id);
103
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, binding.binding);
104
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, binding.offset);
105
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, binding.size);
106
+ }
107
+ }
108
+ }
109
+ if (a.storageTextureBindings) {
110
+ for (let i = 0; i < a.storageTextureBindings.length; i++) {
111
+ const binding = a.storageTextureBindings[i];
112
+ if (binding !== null && binding.texture !== null) {
113
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, binding.texture.id);
114
+ hash = (0, import_HashMap.hashCodeNumberUpdate)(hash, binding.binding);
115
+ }
116
+ }
117
+ }
118
+ return (0, import_HashMap.hashCodeNumberFinish)(hash);
119
+ }
120
+ var RenderCache = class {
121
+ constructor(device) {
122
+ this.device = device;
123
+ this.bindingsCache = new import_HashMap.HashMap(
124
+ import_g_device_api.bindingsDescriptorEquals,
125
+ bindingsDescriptorHash
126
+ );
127
+ this.renderPipelinesCache = new import_HashMap.HashMap(import_g_device_api.renderPipelineDescriptorEquals, renderPipelineDescriptorHash);
128
+ this.inputLayoutsCache = new import_HashMap.HashMap(
129
+ import_g_device_api.inputLayoutDescriptorEquals,
130
+ import_HashMap.nullHashFunc
131
+ );
132
+ }
133
+ createBindings(descriptor) {
134
+ var _a;
135
+ let bindings = this.bindingsCache.get(descriptor);
136
+ if (bindings === null) {
137
+ const descriptorCopy = (0, import_g_device_api.bindingsDescriptorCopy)(descriptor);
138
+ descriptorCopy.uniformBufferBindings = (_a = descriptorCopy.uniformBufferBindings) == null ? void 0 : _a.filter(
139
+ ({ size }) => size && size > 0
140
+ );
141
+ bindings = this.device.createBindings(descriptorCopy);
142
+ this.bindingsCache.add(descriptorCopy, bindings);
143
+ }
144
+ return bindings;
145
+ }
146
+ createRenderPipeline(descriptor) {
147
+ let renderPipeline = this.renderPipelinesCache.get(descriptor);
148
+ if (renderPipeline === null) {
149
+ const descriptorCopy = (0, import_g_device_api.renderPipelineDescriptorCopy)(descriptor);
150
+ descriptorCopy.colorAttachmentFormats = descriptorCopy.colorAttachmentFormats.filter((f) => f);
151
+ renderPipeline = this.device.createRenderPipeline(descriptorCopy);
152
+ this.renderPipelinesCache.add(descriptorCopy, renderPipeline);
153
+ }
154
+ return renderPipeline;
155
+ }
156
+ createInputLayout(descriptor) {
157
+ descriptor.vertexBufferDescriptors = descriptor.vertexBufferDescriptors.filter((d) => !!d);
158
+ let inputLayout = this.inputLayoutsCache.get(descriptor);
159
+ if (inputLayout === null) {
160
+ const descriptorCopy = (0, import_g_device_api.inputLayoutDescriptorCopy)(descriptor);
161
+ inputLayout = this.device.createInputLayout(descriptorCopy);
162
+ this.inputLayoutsCache.add(descriptorCopy, inputLayout);
163
+ }
164
+ return inputLayout;
165
+ }
166
+ destroy() {
167
+ for (const bindings of this.bindingsCache.values())
168
+ bindings.destroy();
169
+ for (const renderPipeline of this.renderPipelinesCache.values())
170
+ renderPipeline.destroy();
171
+ for (const inputLayout of this.inputLayoutsCache.values())
172
+ inputLayout.destroy();
173
+ this.bindingsCache.clear();
174
+ this.renderPipelinesCache.clear();
175
+ this.inputLayoutsCache.clear();
176
+ }
177
+ };
178
+ // Annotate the CommonJS export names for ESM import in node:
179
+ 0 && (module.exports = {
180
+ RenderCache
181
+ });
@@ -31,10 +31,13 @@ var DeviceFramebuffer = class {
31
31
  this.createColorRenderTarget();
32
32
  this.createDepthRenderTarget();
33
33
  }
34
- createColorRenderTarget() {
34
+ createColorRenderTarget(resize = false) {
35
35
  const { width, height, color } = this.options;
36
36
  if (color) {
37
37
  if ((0, import_DeviceTexture2D.isTexture2D)(color)) {
38
+ if (resize) {
39
+ color.resize({ width, height });
40
+ }
38
41
  this.colorTexture = color.get();
39
42
  this.colorRenderTarget = this.device.createRenderTargetFromTexture(
40
43
  this.colorTexture
@@ -56,10 +59,13 @@ var DeviceFramebuffer = class {
56
59
  }
57
60
  }
58
61
  }
59
- createDepthRenderTarget() {
62
+ createDepthRenderTarget(resize = false) {
60
63
  const { width, height, depth } = this.options;
61
64
  if (depth) {
62
65
  if ((0, import_DeviceTexture2D.isTexture2D)(depth)) {
66
+ if (resize) {
67
+ depth.resize({ width, height });
68
+ }
63
69
  this.depthTexture = depth.get();
64
70
  this.depthRenderTarget = this.device.createRenderTargetFromTexture(
65
71
  this.depthTexture
@@ -92,10 +98,12 @@ var DeviceFramebuffer = class {
92
98
  resize({ width, height }) {
93
99
  if (this.width !== width || this.height !== height) {
94
100
  this.destroy();
101
+ this.colorTexture.destroyed = true;
102
+ this.depthTexture.destroyed = true;
95
103
  this.options.width = width;
96
104
  this.options.height = height;
97
- this.createColorRenderTarget();
98
- this.createDepthRenderTarget();
105
+ this.createColorRenderTarget(true);
106
+ this.createDepthRenderTarget(true);
99
107
  }
100
108
  }
101
109
  };
@@ -47,14 +47,24 @@ var DeviceModel = class {
47
47
  this.destroyed = false;
48
48
  this.uniforms = {};
49
49
  this.vertexBuffers = [];
50
- const { vs, fs, attributes, uniforms, count, elements } = options;
50
+ const {
51
+ vs,
52
+ fs,
53
+ attributes,
54
+ uniforms,
55
+ count,
56
+ elements,
57
+ diagnosticDerivativeUniformityEnabled
58
+ } = options;
51
59
  this.options = options;
60
+ const diagnosticDerivativeUniformityHeader = diagnosticDerivativeUniformityEnabled ? "" : this.service["viewportOrigin"] === import_g_device_api.ViewportOrigin.UPPER_LEFT ? "diagnostic(off,derivative_uniformity);" : "";
52
61
  const program = device.createProgram({
53
62
  vertex: {
54
63
  glsl: vs
55
64
  },
56
65
  fragment: {
57
- glsl: fs
66
+ glsl: fs,
67
+ postprocess: (fs2) => diagnosticDerivativeUniformityHeader + fs2
58
68
  }
59
69
  });
60
70
  this.program = program;
@@ -96,7 +106,7 @@ var DeviceModel = class {
96
106
  if (elements) {
97
107
  this.indexBuffer = elements.get();
98
108
  }
99
- const inputLayout = device.createInputLayout({
109
+ const inputLayout = service.renderCache.createInputLayout({
100
110
  vertexBufferDescriptors,
101
111
  indexBufferFormat: elements ? import_g_device_api.Format.U32_R : null,
102
112
  program
@@ -114,7 +124,7 @@ var DeviceModel = class {
114
124
  const blendEnabled = !!(blendParams && blendParams.enable);
115
125
  const stencilParams = this.getStencilDrawParams({ stencil });
116
126
  const stencilEnabled = !!(stencilParams && stencilParams.enable);
117
- return this.device.createRenderPipeline({
127
+ return this.service.renderCache.createRenderPipeline({
118
128
  inputLayout: this.inputLayout,
119
129
  program: this.program,
120
130
  topology: import_constants.primitiveMap[primitive],
@@ -155,11 +165,17 @@ var DeviceModel = class {
155
165
  stencilWrite: stencilEnabled,
156
166
  stencilFront: {
157
167
  compare: stencilEnabled ? stencilParams.func.cmp : import_g_device_api.CompareFunction.ALWAYS,
158
- passOp: stencilParams.opFront.zpass
168
+ passOp: stencilParams.opFront.zpass,
169
+ failOp: stencilParams.opFront.fail,
170
+ depthFailOp: stencilParams.opFront.zfail,
171
+ mask: stencilParams.opFront.mask
159
172
  },
160
173
  stencilBack: {
161
174
  compare: stencilEnabled ? stencilParams.func.cmp : import_g_device_api.CompareFunction.ALWAYS,
162
- passOp: stencilParams.opBack.zpass
175
+ passOp: stencilParams.opBack.zpass,
176
+ failOp: stencilParams.opBack.fail,
177
+ depthFailOp: stencilParams.opBack.zfail,
178
+ mask: stencilParams.opBack.mask
163
179
  }
164
180
  }
165
181
  });
@@ -202,14 +218,18 @@ var DeviceModel = class {
202
218
  ...this.uniforms,
203
219
  ...this.extractUniforms(uniforms)
204
220
  };
205
- const { renderPass, currentFramebuffer, width, height } = this.service;
221
+ const { renderPass, currentFramebuffer, width, height, renderCache } = this.service;
206
222
  this.pipeline = this.createPipeline(mergedOptions, pick);
223
+ const device = this.service["device"];
224
+ const tmpHeight = device["swapChainHeight"];
225
+ device["swapChainHeight"] = (currentFramebuffer == null ? void 0 : currentFramebuffer["height"]) || height;
207
226
  renderPass.setViewport(
208
227
  0,
209
228
  0,
210
229
  (currentFramebuffer == null ? void 0 : currentFramebuffer["width"]) || width,
211
230
  (currentFramebuffer == null ? void 0 : currentFramebuffer["height"]) || height
212
231
  );
232
+ device["swapChainHeight"] = tmpHeight;
213
233
  renderPass.setPipeline(this.pipeline);
214
234
  renderPass.setStencilReference(1);
215
235
  renderPass.setVertexInput(
@@ -223,7 +243,7 @@ var DeviceModel = class {
223
243
  } : null
224
244
  );
225
245
  if (uniformBuffers) {
226
- this.bindings = this.device.createBindings({
246
+ this.bindings = renderCache.createBindings({
227
247
  pipeline: this.pipeline,
228
248
  uniformBufferBindings: uniformBuffers.map((uniformBuffer, i) => {
229
249
  const buffer = uniformBuffer;
@@ -338,12 +358,14 @@ var DeviceModel = class {
338
358
  opFront: {
339
359
  fail: import_constants.stencilOpMap[opFront.fail],
340
360
  zfail: import_constants.stencilOpMap[opFront.zfail],
341
- zpass: import_constants.stencilOpMap[opFront.zpass]
361
+ zpass: import_constants.stencilOpMap[opFront.zpass],
362
+ mask: func.mask
342
363
  },
343
364
  opBack: {
344
365
  fail: import_constants.stencilOpMap[opBack.fail],
345
366
  zfail: import_constants.stencilOpMap[opBack.zfail],
346
- zpass: import_constants.stencilOpMap[opBack.zpass]
367
+ zpass: import_constants.stencilOpMap[opBack.zpass],
368
+ mask: func.mask
347
369
  }
348
370
  };
349
371
  }
@@ -31,7 +31,31 @@ function isTexture2D(t) {
31
31
  }
32
32
  var DeviceTexture2D = class {
33
33
  constructor(device, options) {
34
+ this.device = device;
35
+ this.options = options;
34
36
  this.isDestroy = false;
37
+ const {
38
+ wrapS = import_l7_core.gl.CLAMP_TO_EDGE,
39
+ wrapT = import_l7_core.gl.CLAMP_TO_EDGE,
40
+ aniso,
41
+ mipmap = false,
42
+ // premultiplyAlpha = false,
43
+ mag = import_l7_core.gl.NEAREST,
44
+ min = import_l7_core.gl.NEAREST
45
+ } = options;
46
+ this.createTexture(options);
47
+ this.sampler = device.createSampler({
48
+ addressModeU: import_constants.wrapModeMap[wrapS],
49
+ addressModeV: import_constants.wrapModeMap[wrapT],
50
+ minFilter: min === import_l7_core.gl.NEAREST ? import_g_device_api.FilterMode.POINT : import_g_device_api.FilterMode.BILINEAR,
51
+ magFilter: mag === import_l7_core.gl.NEAREST ? import_g_device_api.FilterMode.POINT : import_g_device_api.FilterMode.BILINEAR,
52
+ mipmapFilter: import_g_device_api.MipmapFilterMode.NO_MIP,
53
+ // lodMinClamp: 0,
54
+ // lodMaxClamp: 0,
55
+ maxAnisotropy: aniso
56
+ });
57
+ }
58
+ createTexture(options) {
35
59
  const {
36
60
  data,
37
61
  type = import_l7_core.gl.UNSIGNED_BYTE,
@@ -39,25 +63,22 @@ var DeviceTexture2D = class {
39
63
  height,
40
64
  flipY = false,
41
65
  format = import_l7_core.gl.RGBA,
42
- wrapS = import_l7_core.gl.CLAMP_TO_EDGE,
43
- wrapT = import_l7_core.gl.CLAMP_TO_EDGE,
44
- aniso = 0,
66
+ aniso,
45
67
  alignment = 1,
46
68
  usage = import_l7_core.TextureUsage.SAMPLED,
47
- mipmap = false,
48
69
  // premultiplyAlpha = false,
49
- mag = import_l7_core.gl.NEAREST,
50
- min = import_l7_core.gl.NEAREST
70
+ unorm = false,
51
71
  // colorSpace = gl.BROWSER_DEFAULT_WEBGL,
52
72
  // x = 0,
53
73
  // y = 0,
54
74
  // copy = false,
75
+ label
55
76
  } = options;
56
77
  this.width = width;
57
78
  this.height = height;
58
79
  let pixelFormat = import_g_device_api.Format.U8_RGBA_RT;
59
80
  if (type === import_l7_core.gl.UNSIGNED_BYTE && format === import_l7_core.gl.RGBA) {
60
- pixelFormat = import_g_device_api.Format.U8_RGBA_RT;
81
+ pixelFormat = unorm ? import_g_device_api.Format.U8_RGBA_NORM : import_g_device_api.Format.U8_RGBA_RT;
61
82
  } else if (type === import_l7_core.gl.UNSIGNED_BYTE && format === import_l7_core.gl.LUMINANCE) {
62
83
  pixelFormat = import_g_device_api.Format.U8_LUMINANCE;
63
84
  } else if (type === import_l7_core.gl.FLOAT && format === import_l7_core.gl.RGB) {
@@ -69,7 +90,7 @@ var DeviceTexture2D = class {
69
90
  } else {
70
91
  throw new Error(`create texture error, type: ${type}, format: ${format}`);
71
92
  }
72
- this.texture = device.createTexture({
93
+ this.texture = this.device.createTexture({
73
94
  format: pixelFormat,
74
95
  width,
75
96
  height,
@@ -78,21 +99,15 @@ var DeviceTexture2D = class {
78
99
  unpackFlipY: flipY,
79
100
  packAlignment: alignment
80
101
  },
81
- mipLevelCount: usage === import_l7_core.TextureUsage.RENDER_TARGET ? 1 : mipmap ? 1 : 0
102
+ // mipLevelCount: usage === TextureUsage.RENDER_TARGET ? 1 : mipmap ? 1 : 0,
103
+ mipLevelCount: 1
82
104
  });
105
+ if (label) {
106
+ this.device.setResourceName(this.texture, label);
107
+ }
83
108
  if (data) {
84
109
  this.texture.setImageData([data]);
85
110
  }
86
- this.sampler = device.createSampler({
87
- addressModeU: import_constants.wrapModeMap[wrapS],
88
- addressModeV: import_constants.wrapModeMap[wrapT],
89
- minFilter: min === import_l7_core.gl.NEAREST ? import_g_device_api.FilterMode.POINT : import_g_device_api.FilterMode.BILINEAR,
90
- magFilter: mag === import_l7_core.gl.NEAREST ? import_g_device_api.FilterMode.POINT : import_g_device_api.FilterMode.BILINEAR,
91
- mipmapFilter: import_g_device_api.MipmapFilterMode.NO_MIP,
92
- // lodMinClamp: 0,
93
- // lodMaxClamp: 0,
94
- maxAnisotropy: aniso
95
- });
96
111
  }
97
112
  get() {
98
113
  return this.texture;
@@ -104,15 +119,20 @@ var DeviceTexture2D = class {
104
119
  bind() {
105
120
  }
106
121
  resize({ width, height }) {
107
- this.width = width;
108
- this.height = height;
122
+ if (this.width !== width || this.height !== height) {
123
+ this.destroy();
124
+ }
125
+ this.options.width = width;
126
+ this.options.height = height;
127
+ this.createTexture(this.options);
128
+ this.isDestroy = false;
109
129
  }
110
130
  getSize() {
111
131
  return [this.width, this.height];
112
132
  }
113
133
  destroy() {
114
134
  var _a;
115
- if (!this.isDestroy) {
135
+ if (!this.isDestroy && !this.texture.destroyed) {
116
136
  (_a = this.texture) == null ? void 0 : _a.destroy();
117
137
  }
118
138
  this.isDestroy = true;
@@ -42,15 +42,18 @@ __export(device_exports, {
42
42
  });
43
43
  module.exports = __toCommonJS(device_exports);
44
44
  var import_g_device_api = require("@antv/g-device-api");
45
+ var import_l7_utils = require("@antv/l7-utils");
45
46
  var import_inversify = require("inversify");
46
47
  var import_reflect_metadata = require("reflect-metadata");
47
48
  var import_DeviceAttribute = __toESM(require("./DeviceAttribute"));
48
49
  var import_DeviceBuffer = __toESM(require("./DeviceBuffer"));
50
+ var import_DeviceCache = require("./DeviceCache");
49
51
  var import_DeviceElements = __toESM(require("./DeviceElements"));
50
52
  var import_DeviceFramebuffer = __toESM(require("./DeviceFramebuffer"));
51
53
  var import_DeviceModel = __toESM(require("./DeviceModel"));
52
54
  var import_DeviceTexture2D = __toESM(require("./DeviceTexture2D"));
53
55
  var import_webgl = require("./utils/webgl");
56
+ var { isUndefined } = import_l7_utils.lodashUtil;
54
57
  var DeviceRendererService = class {
55
58
  constructor() {
56
59
  this.uniformBuffers = [];
@@ -70,10 +73,38 @@ var DeviceRendererService = class {
70
73
  this.endFrame();
71
74
  this.currentFramebuffer = null;
72
75
  };
76
+ this.useFramebufferAsync = async (framebuffer, drawCommands) => {
77
+ this.currentFramebuffer = framebuffer;
78
+ this.preRenderPass = this.renderPass;
79
+ this.beginFrame();
80
+ await drawCommands();
81
+ this.endFrame();
82
+ this.currentFramebuffer = null;
83
+ this.renderPass = this.preRenderPass;
84
+ };
73
85
  this.clear = (options) => {
74
86
  const { color, depth, stencil, framebuffer = null } = options;
75
87
  if (framebuffer) {
76
88
  framebuffer.clearOptions = { color, depth, stencil };
89
+ } else {
90
+ const platformString = this.queryVerdorInfo();
91
+ if (platformString === "WebGL1") {
92
+ const gl = this.getGLContext();
93
+ if (!isUndefined(stencil)) {
94
+ gl.clearStencil(stencil);
95
+ gl.clear(gl.STENCIL_BUFFER_BIT);
96
+ } else if (!isUndefined(depth)) {
97
+ gl.clearDepth(depth);
98
+ gl.clear(gl.DEPTH_BUFFER_BIT);
99
+ }
100
+ } else if (platformString === "WebGL2") {
101
+ const gl = this.getGLContext();
102
+ if (!isUndefined(stencil)) {
103
+ gl.clearBufferiv(gl.STENCIL, 0, [stencil]);
104
+ } else if (!isUndefined(depth)) {
105
+ gl.clearBufferfv(gl.DEPTH, 0, [depth]);
106
+ }
107
+ }
77
108
  }
78
109
  };
79
110
  this.viewport = ({
@@ -82,6 +113,8 @@ var DeviceRendererService = class {
82
113
  width,
83
114
  height
84
115
  }) => {
116
+ this.swapChain.configureSwapChain(width, height);
117
+ this.createMainColorDepthRT(width, height);
85
118
  this.width = width;
86
119
  this.height = height;
87
120
  };
@@ -89,14 +122,46 @@ var DeviceRendererService = class {
89
122
  const { framebuffer, x, y, width, height } = options;
90
123
  const readback = this.device.createReadback();
91
124
  const texture = framebuffer["colorTexture"];
92
- return readback.readTextureSync(
125
+ const result = readback.readTextureSync(
93
126
  texture,
94
127
  x,
95
- y,
128
+ /**
129
+ * Origin is at lower-left corner. Width / height is already multiplied by dpr.
130
+ * WebGPU needs flipY
131
+ */
132
+ this.viewportOrigin === import_g_device_api.ViewportOrigin.LOWER_LEFT ? y : this.height - y,
96
133
  width,
97
134
  height,
98
135
  new Uint8Array(width * height * 4)
99
136
  );
137
+ readback.destroy();
138
+ return result;
139
+ };
140
+ this.readPixelsAsync = async (options) => {
141
+ const { framebuffer, x, y, width, height } = options;
142
+ const readback = this.device.createReadback();
143
+ const texture = framebuffer["colorTexture"];
144
+ const result = await readback.readTexture(
145
+ texture,
146
+ x,
147
+ /**
148
+ * Origin is at lower-left corner. Width / height is already multiplied by dpr.
149
+ * WebGPU needs flipY
150
+ */
151
+ this.viewportOrigin === import_g_device_api.ViewportOrigin.LOWER_LEFT ? y : this.height - y,
152
+ width,
153
+ height,
154
+ new Uint8Array(width * height * 4)
155
+ );
156
+ if (this.viewportOrigin !== import_g_device_api.ViewportOrigin.LOWER_LEFT) {
157
+ for (let j = 0; j < result.length; j += 4) {
158
+ const t = result[j];
159
+ result[j] = result[j + 2];
160
+ result[j + 2] = t;
161
+ }
162
+ }
163
+ readback.destroy();
164
+ return result;
100
165
  };
101
166
  this.getViewportSize = () => {
102
167
  return {
@@ -121,6 +186,7 @@ var DeviceRendererService = class {
121
186
  buffer.destroy();
122
187
  });
123
188
  this.device.destroy();
189
+ this.renderCache.destroy();
124
190
  };
125
191
  }
126
192
  async init(canvas, cfg) {
@@ -145,34 +211,44 @@ var DeviceRendererService = class {
145
211
  swapChain.configureSwapChain(canvas.width, canvas.height);
146
212
  this.device = swapChain.getDevice();
147
213
  this.swapChain = swapChain;
214
+ this.renderCache = new import_DeviceCache.RenderCache(this.device);
148
215
  this.currentFramebuffer = null;
216
+ this.viewportOrigin = this.device.queryVendorInfo().viewportOrigin;
149
217
  const gl = this.device["gl"];
150
218
  this.extensionObject = {
151
219
  // @ts-ignore
152
220
  OES_texture_float: !(0, import_webgl.isWebGL2)(gl) && this.device["OES_texture_float"]
153
221
  };
222
+ this.createMainColorDepthRT(canvas.width, canvas.height);
223
+ }
224
+ createMainColorDepthRT(width, height) {
225
+ if (this.mainColorRT) {
226
+ this.mainColorRT.destroy();
227
+ }
228
+ if (this.mainDepthRT) {
229
+ this.mainDepthRT.destroy();
230
+ }
154
231
  this.mainColorRT = this.device.createRenderTargetFromTexture(
155
232
  this.device.createTexture({
156
233
  format: import_g_device_api.Format.U8_RGBA_RT,
157
- width: canvas.width,
158
- height: canvas.height,
234
+ width,
235
+ height,
159
236
  usage: import_g_device_api.TextureUsage.RENDER_TARGET
160
237
  })
161
238
  );
162
239
  this.mainDepthRT = this.device.createRenderTargetFromTexture(
163
240
  this.device.createTexture({
164
241
  format: import_g_device_api.Format.D24_S8,
165
- width: canvas.width,
166
- height: canvas.height,
242
+ width,
243
+ height,
167
244
  usage: import_g_device_api.TextureUsage.RENDER_TARGET
168
245
  })
169
246
  );
170
247
  }
171
248
  beginFrame() {
172
249
  const { currentFramebuffer, swapChain, mainColorRT, mainDepthRT } = this;
173
- const onscreenTexture = swapChain.getOnscreenTexture();
174
250
  const colorAttachment = currentFramebuffer ? currentFramebuffer["colorRenderTarget"] : mainColorRT;
175
- const colorResolveTo = currentFramebuffer ? null : onscreenTexture;
251
+ const colorResolveTo = currentFramebuffer ? null : swapChain.getOnscreenTexture();
176
252
  const depthStencilAttachment = currentFramebuffer ? currentFramebuffer["depthRenderTarget"] : mainDepthRT;
177
253
  const { color = [0, 0, 0, 0], depth = 1, stencil = 0 } = (
178
254
  // @ts-ignore
@@ -190,6 +266,8 @@ var DeviceRendererService = class {
190
266
  colorAttachment: [colorAttachment],
191
267
  colorResolveTo: [colorResolveTo],
192
268
  colorClearColor: [colorClearColor],
269
+ // colorStore: [!!currentFramebuffer],
270
+ colorStore: [true],
193
271
  depthStencilAttachment,
194
272
  depthClearValue,
195
273
  stencilClearValue