@next2d/webgpu 3.11.1 → 3.12.1

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.
Files changed (46) hide show
  1. package/package.json +3 -3
  2. package/src/Blend/BlendInstancedManager.d.ts +2 -1
  3. package/src/Blend/BlendInstancedManager.js +18 -15
  4. package/src/Blend/usecase/BlendApplyComplexBlendUseCase.d.ts +3 -1
  5. package/src/Blend/usecase/BlendApplyComplexBlendUseCase.js +24 -17
  6. package/src/BufferManager.d.ts +14 -0
  7. package/src/BufferManager.js +47 -0
  8. package/src/Context/usecase/ContextApplyFilterUseCase.js +12 -12
  9. package/src/Context/usecase/ContextContainerEndLayerUseCase.js +8 -8
  10. package/src/Context/usecase/ContextDrawArraysInstancedUseCase.js +4 -3
  11. package/src/Context/usecase/ContextDrawIndirectUseCase.d.ts +3 -1
  12. package/src/Context/usecase/ContextDrawIndirectUseCase.js +33 -15
  13. package/src/Context/usecase/ContextProcessComplexBlendQueueUseCase.js +18 -62
  14. package/src/Context.d.ts +20 -19
  15. package/src/Context.js +203 -105
  16. package/src/ExternalImageUploadBatch.d.ts +24 -0
  17. package/src/ExternalImageUploadBatch.js +133 -0
  18. package/src/Filter/BevelFilter/FilterApplyBevelFilterUseCase.js +5 -20
  19. package/src/Filter/BlurFilter/FilterApplyBlurFilterUseCase.js +38 -11
  20. package/src/Filter/ColorMatrixFilter/FilterApplyColorMatrixFilterUseCase.js +3 -10
  21. package/src/Filter/ConvolutionFilter/FilterApplyConvolutionFilterUseCase.js +3 -10
  22. package/src/Filter/DisplacementMapFilter/FilterApplyDisplacementMapFilterUseCase.js +5 -11
  23. package/src/Filter/DropShadowFilter/FilterApplyDropShadowFilterUseCase.js +8 -5
  24. package/src/Filter/GlowFilter/FilterApplyGlowFilterUseCase.js +8 -5
  25. package/src/Filter/GradientBevelFilter/FilterApplyGradientBevelFilterUseCase.js +5 -20
  26. package/src/Filter/GradientGlowFilter/FilterApplyGradientGlowFilterUseCase.js +3 -10
  27. package/src/Filter/service/FilterAllocateUniformBindingService.d.ts +11 -0
  28. package/src/Filter/service/FilterAllocateUniformBindingService.js +20 -0
  29. package/src/FrameBufferManager/usecase/FrameBufferManagerCreateAttachmentUseCase.js +2 -1
  30. package/src/InstanceBufferAllocator.d.ts +24 -0
  31. package/src/InstanceBufferAllocator.js +123 -0
  32. package/src/Shader/PipelineManager.d.ts +5 -0
  33. package/src/Shader/PipelineManager.js +55 -7
  34. package/src/Shader/ShaderSource.d.ts +9 -1
  35. package/src/Shader/ShaderSource.js +28 -6
  36. package/src/Shader/wgsl/fragment/FilterFragment.d.ts +4 -6
  37. package/src/Shader/wgsl/fragment/FilterFragment.js +10 -4
  38. package/src/TexturePool/service/TexturePoolCleanupService.d.ts +2 -2
  39. package/src/TexturePool/service/TexturePoolCleanupService.js +2 -1
  40. package/src/TexturePool/service/TexturePoolReleaseService.d.ts +2 -2
  41. package/src/TexturePool/service/TexturePoolReleaseService.js +8 -1
  42. package/src/TexturePool/usecase/TexturePoolAcquireUseCase.d.ts +2 -2
  43. package/src/TexturePool/usecase/TexturePoolAcquireUseCase.js +3 -1
  44. package/src/TexturePool.d.ts +1 -0
  45. package/src/TexturePool.js +11 -3
  46. package/src/interface/IFilterConfig.d.ts +1 -0
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Packs canvas snapshots before uploading them. Each flush submits copies to
3
+ * distinct, frame-owned destination textures before the renderer submits draws.
4
+ * A single 1024-square canvas and GPU staging texture are retained (4 MiB each,
5
+ * excluding browser/driver overhead). No render target is reordered or reused.
6
+ */
7
+ export class ExternalImageUploadBatch {
8
+ constructor(device) {
9
+ Object.defineProperty(this, "device", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: device
14
+ });
15
+ Object.defineProperty(this, "size", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: 1024
20
+ });
21
+ Object.defineProperty(this, "canvas", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: null
26
+ });
27
+ Object.defineProperty(this, "context", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: null
32
+ });
33
+ Object.defineProperty(this, "texture", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: null
38
+ });
39
+ Object.defineProperty(this, "copies", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: []
44
+ });
45
+ Object.defineProperty(this, "x", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: 0
50
+ });
51
+ Object.defineProperty(this, "y", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: 0
56
+ });
57
+ Object.defineProperty(this, "rowHeight", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: 0
62
+ });
63
+ Object.defineProperty(this, "usedWidth", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: 0
68
+ });
69
+ }
70
+ /** Copies the source synchronously, so the caller may immediately reuse it. */
71
+ append(source, destination, width, height) {
72
+ if (width <= 0 || height <= 0 || width > this.size || height > this.size
73
+ || width !== source.width || height !== source.height) {
74
+ return false;
75
+ }
76
+ if (!this.canvas) {
77
+ this.canvas = new OffscreenCanvas(this.size, this.size);
78
+ this.context = this.canvas.getContext("2d");
79
+ }
80
+ if (!this.context) {
81
+ return false;
82
+ }
83
+ if (this.x + width > this.size) {
84
+ this.x = 0;
85
+ this.y += this.rowHeight;
86
+ this.rowHeight = 0;
87
+ }
88
+ if (this.y + height > this.size) {
89
+ this.flush();
90
+ }
91
+ if (!this.texture) {
92
+ this.texture = this.device.createTexture({
93
+ "size": [this.size, this.size], "format": "rgba8unorm",
94
+ // COPY_SRC | COPY_DST | RENDER_ATTACHMENT (external image copy).
95
+ "usage": 0x13
96
+ });
97
+ }
98
+ // Integer, unscaled copy onto a cleared, disjoint region.
99
+ this.context.drawImage(source, this.x, this.y);
100
+ this.copies.push({ destination, "x": this.x, "y": this.y, width, height });
101
+ this.x += width;
102
+ this.usedWidth = Math.max(this.usedWidth, this.x);
103
+ this.rowHeight = Math.max(this.rowHeight, height);
104
+ return true;
105
+ }
106
+ flush() {
107
+ if (!this.copies.length || !this.canvas || !this.texture) {
108
+ return;
109
+ }
110
+ this.device.queue.copyExternalImageToTexture({ "source": this.canvas }, { "texture": this.texture, "premultipliedAlpha": true }, { "width": this.usedWidth, "height": this.y + this.rowHeight });
111
+ // Record copies only after the external upload. On WebKit, recording
112
+ // against a staging texture before its first upload can copy blank pixels.
113
+ const encoder = this.device.createCommandEncoder();
114
+ for (const copy of this.copies) {
115
+ encoder.copyTextureToTexture({ "texture": this.texture, "origin": [copy.x, copy.y] }, { "texture": copy.destination }, { "width": copy.width, "height": copy.height });
116
+ }
117
+ // Submit before reusing staging storage; these destinations remain owned
118
+ // by the frame until its later rendering submission.
119
+ this.device.queue.submit([encoder.finish()]);
120
+ this.copies.length = 0;
121
+ this.canvas.width = this.size;
122
+ this.x = this.y = this.rowHeight = this.usedWidth = 0;
123
+ }
124
+ /** Discard pending copies when resize discards the corresponding draw frame. */
125
+ dispose() {
126
+ this.copies.length = 0;
127
+ this.texture?.destroy();
128
+ this.texture = null;
129
+ this.canvas = null;
130
+ this.context = null;
131
+ this.x = this.y = this.rowHeight = this.usedWidth = 0;
132
+ }
133
+ }
@@ -1,3 +1,4 @@
1
+ import { execute as filterAllocateUniformBindingService } from "../service/FilterAllocateUniformBindingService";
1
2
  import { $offset } from "../FilterOffset";
2
3
  import { DEG_TO_RAD, intToPremultipliedRGBA } from "../FilterUtil";
3
4
  import { execute as filterApplyBlurFilterUseCase } from "../BlurFilter/FilterApplyBlurFilterUseCase";
@@ -91,16 +92,8 @@ export const execute = (source_attachment, matrix, distance, angle, highlight_co
91
92
  $uniform8[5] = 0;
92
93
  $uniform8[6] = 0;
93
94
  $uniform8[7] = 0;
94
- const eraseUniformBuffer = config.bufferManager
95
- ? config.bufferManager.acquireAndWriteUniformBuffer($uniform8)
96
- : device.createBuffer({
97
- "size": $uniform8.byteLength,
98
- "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
99
- });
100
- if (!config.bufferManager) {
101
- device.queue.writeBuffer(eraseUniformBuffer, 0, $uniform8);
102
- }
103
- $entries3[0].resource.buffer = eraseUniformBuffer;
95
+ const eraseUniformBinding = filterAllocateUniformBindingService(device, $uniform8, config.bufferManager);
96
+ $entries3[0].resource = eraseUniformBinding;
104
97
  $entries3[1].resource = eraseSampler;
105
98
  $entries3[2].resource = source_attachment.texture.view;
106
99
  const eraseBindGroup = device.createBindGroup({
@@ -187,17 +180,9 @@ export const execute = (source_attachment, matrix, distance, angle, highlight_co
187
180
  $uniform20[17] = blurScaleY;
188
181
  $uniform20[18] = blurOffsetUVX;
189
182
  $uniform20[19] = blurOffsetUVY;
190
- const uniformBuffer = config.bufferManager
191
- ? config.bufferManager.acquireAndWriteUniformBuffer($uniform20)
192
- : device.createBuffer({
193
- "size": $uniform20.byteLength,
194
- "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
195
- });
196
- if (!config.bufferManager) {
197
- device.queue.writeBuffer(uniformBuffer, 0, $uniform20);
198
- }
183
+ const uniformBinding = filterAllocateUniformBindingService(device, $uniform20, config.bufferManager);
199
184
  // バインドグループを作成(元テクスチャとブラーテクスチャを直接バインド)
200
- $entries4[0].resource.buffer = uniformBuffer;
185
+ $entries4[0].resource = uniformBinding;
201
186
  $entries4[1].resource = sampler;
202
187
  $entries4[2].resource = blurAttachment.texture.view;
203
188
  $entries4[3].resource = source_attachment.texture.view;
@@ -12,6 +12,36 @@ const $entries3 = [
12
12
  { "binding": 1, "resource": null },
13
13
  { "binding": 2, "resource": null }
14
14
  ];
15
+ // Only the latest buffer/layout/sampler combination is kept for each live view.
16
+ // Static offsets remain distinct; cap churn when the frame's allocation order changes.
17
+ const $blurBindingCache = new WeakMap();
18
+ const $getBlurBindGroup = (device, layout, sampler, view, binding, cacheable) => {
19
+ let cache;
20
+ const offset = binding.offset ?? 0;
21
+ if (cacheable) {
22
+ cache = $blurBindingCache.get(view);
23
+ if (!cache || cache.device !== device || cache.layout !== layout
24
+ || cache.sampler !== sampler || cache.buffer !== binding.buffer || cache.size !== binding.size) {
25
+ cache = { device, layout, sampler, "buffer": binding.buffer, "size": binding.size, "groups": new Map() };
26
+ $blurBindingCache.set(view, cache);
27
+ }
28
+ const existing = cache.groups.get(offset);
29
+ if (existing) {
30
+ return existing;
31
+ }
32
+ }
33
+ $entries3[0].resource = binding;
34
+ $entries3[1].resource = sampler;
35
+ $entries3[2].resource = view;
36
+ const group = device.createBindGroup({ layout, "entries": $entries3 });
37
+ if (cache) {
38
+ if (cache.groups.size >= 64) {
39
+ cache.groups.clear();
40
+ }
41
+ cache.groups.set(offset, group);
42
+ }
43
+ return group;
44
+ };
15
45
  /**
16
46
  * @description copy/upscale 用の定数uniform(scale=1,1 offset=0,0)の永続バッファ
17
47
  * Persistent uniform buffer for the constant copy/upscale uniform
@@ -204,22 +234,19 @@ const applyDirectionalBlur = (device, command_encoder, frame_buffer_manager, pip
204
234
  $uniform4[1] = offsetY;
205
235
  $uniform4[2] = fraction;
206
236
  $uniform4[3] = samples;
207
- const uniformBuffer = buffer_manager
208
- ? buffer_manager.acquireAndWriteUniformBuffer($uniform4)
209
- : device.createBuffer({
237
+ let uniformBinding;
238
+ if (buffer_manager) {
239
+ uniformBinding = buffer_manager.allocateUniformBinding($uniform4);
240
+ }
241
+ else {
242
+ const uniformBuffer = device.createBuffer({
210
243
  "size": $uniform4.byteLength,
211
244
  "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
212
245
  });
213
- if (!buffer_manager) {
214
246
  device.queue.writeBuffer(uniformBuffer, 0, $uniform4);
247
+ uniformBinding = { "buffer": uniformBuffer, "offset": 0, "size": $uniform4.byteLength };
215
248
  }
216
- $entries3[0].resource.buffer = uniformBuffer;
217
- $entries3[1].resource = sampler;
218
- $entries3[2].resource = source.texture.view;
219
- const bindGroup = device.createBindGroup({
220
- "layout": bindGroupLayout,
221
- "entries": $entries3
222
- });
249
+ const bindGroup = $getBlurBindGroup(device, bindGroupLayout, sampler, source.texture.view, uniformBinding, !!buffer_manager);
223
250
  const renderPassDescriptor = frame_buffer_manager.createRenderPassDescriptor(dest.texture.view, 0, 0, 0, 0, "clear");
224
251
  const passEncoder = command_encoder.beginRenderPass(renderPassDescriptor);
225
252
  passEncoder.setPipeline(pipeline);
@@ -1,3 +1,4 @@
1
+ import { execute as filterAllocateUniformBindingService } from "../service/FilterAllocateUniformBindingService";
1
2
  /**
2
3
  * @description プリアロケートされたFloat32Array
3
4
  */
@@ -61,17 +62,9 @@ export const execute = (source_attachment, matrix, config) => {
61
62
  $uniform20[17] = matrix[9] / 255;
62
63
  $uniform20[18] = matrix[14] / 255;
63
64
  $uniform20[19] = matrix[19] / 255;
64
- const uniformBuffer = config.bufferManager
65
- ? config.bufferManager.acquireAndWriteUniformBuffer($uniform20)
66
- : device.createBuffer({
67
- "size": $uniform20.byteLength,
68
- "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
69
- });
70
- if (!config.bufferManager) {
71
- device.queue.writeBuffer(uniformBuffer, 0, $uniform20);
72
- }
65
+ const uniformBinding = filterAllocateUniformBindingService(device, $uniform20, config.bufferManager);
73
66
  // バインドグループを作成
74
- $entries3[0].resource.buffer = uniformBuffer;
67
+ $entries3[0].resource = uniformBinding;
75
68
  $entries3[1].resource = sampler;
76
69
  $entries3[2].resource = source_attachment.texture.view;
77
70
  const bindGroup = device.createBindGroup({
@@ -1,3 +1,4 @@
1
+ import { execute as filterAllocateUniformBindingService } from "../service/FilterAllocateUniformBindingService";
1
2
  import { ShaderSource } from "../../Shader/ShaderSource";
2
3
  import { intToStraightRGBA } from "../FilterUtil";
3
4
  /**
@@ -122,17 +123,9 @@ export const execute = (source_attachment, matrix_x, matrix_y, matrix, divisor,
122
123
  for (let i = 0; i < paddedMatrix.length; i++) {
123
124
  uniformData[8 + i] = paddedMatrix[i];
124
125
  }
125
- const uniformBuffer = config.bufferManager
126
- ? config.bufferManager.acquireAndWriteUniformBuffer(uniformData)
127
- : device.createBuffer({
128
- "size": uniformData.byteLength,
129
- "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
130
- });
131
- if (!config.bufferManager) {
132
- device.queue.writeBuffer(uniformBuffer, 0, uniformData);
133
- }
126
+ const uniformBinding = filterAllocateUniformBindingService(device, uniformData, config.bufferManager);
134
127
  // バインドグループを作成
135
- $entries3[0].resource.buffer = uniformBuffer;
128
+ $entries3[0].resource = uniformBinding;
136
129
  $entries3[1].resource = sampler;
137
130
  $entries3[2].resource = source_attachment.texture.view;
138
131
  const bindGroup = device.createBindGroup({
@@ -1,9 +1,12 @@
1
+ import { execute as filterAllocateUniformBindingService } from "../service/FilterAllocateUniformBindingService";
1
2
  import { ShaderSource } from "../../Shader/ShaderSource";
2
3
  import { intToPremultipliedRGBA } from "../FilterUtil";
3
4
  /**
4
5
  * @description プリアロケートされたFloat32Array (サイズ12: 最大48バイト)
5
6
  */
6
7
  const $uniform12 = new Float32Array(12);
8
+ // Reuse the 32-byte prefix for modes without substituteColor.
9
+ const $uniform8 = $uniform12.subarray(0, 8);
7
10
  /**
8
11
  * @description プリアロケートされたBindGroupEntry配列 (バインディング4つ)
9
12
  */
@@ -131,7 +134,6 @@ export const execute = (source_attachment, _matrix, bitmap_buffer, bitmap_width,
131
134
  const sampler = textureManager.createSampler("displacement_sampler", true);
132
135
  // ユニフォームバッファを作成
133
136
  const needsSubstituteColor = mode === 1;
134
- const uniformSize = needsSubstituteColor ? 48 : 32;
135
137
  // uvToStScale
136
138
  $uniform12[0] = baseWidth / bitmap_width;
137
139
  $uniform12[1] = baseHeight / bitmap_height;
@@ -152,17 +154,9 @@ export const execute = (source_attachment, _matrix, bitmap_buffer, bitmap_width,
152
154
  $uniform12[10] = b;
153
155
  $uniform12[11] = a;
154
156
  }
155
- const uniformBuffer = config.bufferManager
156
- ? config.bufferManager.acquireAndWriteUniformBuffer($uniform12, uniformSize)
157
- : device.createBuffer({
158
- "size": uniformSize,
159
- "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
160
- });
161
- if (!config.bufferManager) {
162
- device.queue.writeBuffer(uniformBuffer, 0, $uniform12, 0, uniformSize / 4);
163
- }
157
+ const uniformBinding = filterAllocateUniformBindingService(device, needsSubstituteColor ? $uniform12 : $uniform8, config.bufferManager);
164
158
  // バインドグループを作成
165
- $entries4[0].resource.buffer = uniformBuffer;
159
+ $entries4[0].resource = uniformBinding;
166
160
  $entries4[1].resource = sampler;
167
161
  $entries4[2].resource = source_attachment.texture.view;
168
162
  $entries4[3].resource = mapTexture.createView();
@@ -136,17 +136,20 @@ export const execute = (source_attachment, matrix, distance, angle, color, alpha
136
136
  $uniform16[13] = isInner ? 1.0 : 0.0;
137
137
  $uniform16[14] = isKnockout ? 1.0 : 0.0;
138
138
  $uniform16[15] = isHideObject ? 1.0 : 0.0;
139
- const uniformBuffer = config.bufferManager
140
- ? config.bufferManager.acquireAndWriteUniformBuffer($uniform16)
141
- : device.createBuffer({
139
+ let uniformBinding;
140
+ if (config.bufferManager) {
141
+ uniformBinding = config.bufferManager.allocateUniformBinding($uniform16);
142
+ }
143
+ else {
144
+ const uniformBuffer = device.createBuffer({
142
145
  "size": $uniform16.byteLength,
143
146
  "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
144
147
  });
145
- if (!config.bufferManager) {
146
148
  device.queue.writeBuffer(uniformBuffer, 0, $uniform16);
149
+ uniformBinding = { "buffer": uniformBuffer, "offset": 0, "size": $uniform16.byteLength };
147
150
  }
148
151
  // バインドグループを作成(オリジナルテクスチャを直接使用)
149
- $entries4[0].resource.buffer = uniformBuffer;
152
+ $entries4[0].resource = uniformBinding;
150
153
  $entries4[1].resource = sampler;
151
154
  $entries4[2].resource = blurAttachment.texture.view;
152
155
  $entries4[3].resource = source_attachment.texture.view;
@@ -103,17 +103,20 @@ export const execute = (source_attachment, matrix, color, alpha, blur_x, blur_y,
103
103
  $uniform16[13] = inner ? 1.0 : 0.0;
104
104
  $uniform16[14] = knockout ? 1.0 : 0.0;
105
105
  $uniform16[15] = 0.0;
106
- const uniformBuffer = config.bufferManager
107
- ? config.bufferManager.acquireAndWriteUniformBuffer($uniform16)
108
- : device.createBuffer({
106
+ let uniformBinding;
107
+ if (config.bufferManager) {
108
+ uniformBinding = config.bufferManager.allocateUniformBinding($uniform16);
109
+ }
110
+ else {
111
+ const uniformBuffer = device.createBuffer({
109
112
  "size": $uniform16.byteLength,
110
113
  "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
111
114
  });
112
- if (!config.bufferManager) {
113
115
  device.queue.writeBuffer(uniformBuffer, 0, $uniform16);
116
+ uniformBinding = { "buffer": uniformBuffer, "offset": 0, "size": $uniform16.byteLength };
114
117
  }
115
118
  // バインドグループを作成(元テクスチャとブラーテクスチャを直接バインド)
116
- $entries4[0].resource.buffer = uniformBuffer;
119
+ $entries4[0].resource = uniformBinding;
117
120
  $entries4[1].resource = sampler;
118
121
  $entries4[2].resource = blurAttachment.texture.view;
119
122
  $entries4[3].resource = source_attachment.texture.view;
@@ -1,3 +1,4 @@
1
+ import { execute as filterAllocateUniformBindingService } from "../service/FilterAllocateUniformBindingService";
1
2
  import { $offset } from "../FilterOffset";
2
3
  import { execute as filterApplyBlurFilterUseCase } from "../BlurFilter/FilterApplyBlurFilterUseCase";
3
4
  import { generateFilterGradientLUT } from "../../Gradient/GradientLUTGenerator";
@@ -82,16 +83,8 @@ export const execute = (source_attachment, matrix, distance, angle, colors, alph
82
83
  $uniform4[1] = 2 * y / baseHeight;
83
84
  $uniform4[2] = 0.0;
84
85
  $uniform4[3] = 0.0;
85
- const bevelBaseUniformBuffer = config.bufferManager
86
- ? config.bufferManager.acquireAndWriteUniformBuffer($uniform4)
87
- : device.createBuffer({
88
- "size": $uniform4.byteLength,
89
- "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
90
- });
91
- if (!config.bufferManager) {
92
- device.queue.writeBuffer(bevelBaseUniformBuffer, 0, $uniform4);
93
- }
94
- $entries3[0].resource.buffer = bevelBaseUniformBuffer;
86
+ const bevelBaseUniformBinding = filterAllocateUniformBindingService(device, $uniform4, config.bufferManager);
87
+ $entries3[0].resource = bevelBaseUniformBinding;
95
88
  $entries3[1].resource = bevelBaseSampler;
96
89
  $entries3[2].resource = source_attachment.texture.view;
97
90
  const bevelBaseBindGroup = device.createBindGroup({
@@ -184,17 +177,9 @@ export const execute = (source_attachment, matrix, distance, angle, colors, alph
184
177
  $uniform12[9] = blurScaleY;
185
178
  $uniform12[10] = blurOffsetUVX;
186
179
  $uniform12[11] = blurOffsetUVY;
187
- const uniformBuffer = config.bufferManager
188
- ? config.bufferManager.acquireAndWriteUniformBuffer($uniform12)
189
- : device.createBuffer({
190
- "size": $uniform12.byteLength,
191
- "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
192
- });
193
- if (!config.bufferManager) {
194
- device.queue.writeBuffer(uniformBuffer, 0, $uniform12);
195
- }
180
+ const uniformBinding = filterAllocateUniformBindingService(device, $uniform12, config.bufferManager);
196
181
  // バインドグループを作成(オリジナルテクスチャを直接使用)
197
- $entries5[0].resource.buffer = uniformBuffer;
182
+ $entries5[0].resource = uniformBinding;
198
183
  $entries5[1].resource = sampler;
199
184
  $entries5[2].resource = blurAttachment.texture.view;
200
185
  $entries5[3].resource = source_attachment.texture.view;
@@ -1,3 +1,4 @@
1
+ import { execute as filterAllocateUniformBindingService } from "../service/FilterAllocateUniformBindingService";
1
2
  import { $offset } from "../FilterOffset";
2
3
  import { execute as filterApplyBlurFilterUseCase } from "../BlurFilter/FilterApplyBlurFilterUseCase";
3
4
  import { generateFilterGradientLUT } from "../../Gradient/GradientLUTGenerator";
@@ -132,17 +133,9 @@ export const execute = (source_attachment, matrix, distance, angle, colors, alph
132
133
  $uniform12[9] = blurScaleY;
133
134
  $uniform12[10] = blurOffsetUVX;
134
135
  $uniform12[11] = blurOffsetUVY;
135
- const uniformBuffer = config.bufferManager
136
- ? config.bufferManager.acquireAndWriteUniformBuffer($uniform12)
137
- : device.createBuffer({
138
- "size": $uniform12.byteLength,
139
- "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
140
- });
141
- if (!config.bufferManager) {
142
- device.queue.writeBuffer(uniformBuffer, 0, $uniform12);
143
- }
136
+ const uniformBinding = filterAllocateUniformBindingService(device, $uniform12, config.bufferManager);
144
137
  // バインドグループを作成(オリジナルテクスチャを直接使用)
145
- $entries5[0].resource.buffer = uniformBuffer;
138
+ $entries5[0].resource = uniformBinding;
146
139
  $entries5[1].resource = sampler;
147
140
  $entries5[2].resource = blurAttachment.texture.view;
148
141
  $entries5[3].resource = source_attachment.texture.view;
@@ -0,0 +1,11 @@
1
+ import type { IFilterConfig } from "../../interface/IFilterConfig";
2
+ /**
3
+ * @description フレーム共有領域へUniformをコピーし、静的offset/size付きbindingを返す。
4
+ * Stage uniforms in the frame arena, or upload independently for standalone callers.
5
+ * The caller must flush the arena before submit and retain buffers until submit.
6
+ * @param {GPUDevice} device
7
+ * @param {Float32Array} data
8
+ * @param {IFilterConfig["bufferManager"]} [buffer_manager]
9
+ * @return {GPUBufferBinding}
10
+ */
11
+ export declare const execute: (device: GPUDevice, data: Float32Array, buffer_manager?: IFilterConfig["bufferManager"]) => GPUBufferBinding;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @description フレーム共有領域へUniformをコピーし、静的offset/size付きbindingを返す。
3
+ * Stage uniforms in the frame arena, or upload independently for standalone callers.
4
+ * The caller must flush the arena before submit and retain buffers until submit.
5
+ * @param {GPUDevice} device
6
+ * @param {Float32Array} data
7
+ * @param {IFilterConfig["bufferManager"]} [buffer_manager]
8
+ * @return {GPUBufferBinding}
9
+ */
10
+ export const execute = (device, data, buffer_manager) => {
11
+ if (buffer_manager) {
12
+ return buffer_manager.allocateUniformBinding(data);
13
+ }
14
+ const buffer = device.createBuffer({
15
+ "size": data.byteLength,
16
+ "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
17
+ });
18
+ device.queue.writeBuffer(buffer, 0, data);
19
+ return { buffer, "offset": 0, "size": data.byteLength };
20
+ };
@@ -53,7 +53,8 @@ export const execute = (device, format, attachments, name, width, height, msaa,
53
53
  "size": { width, height },
54
54
  "format": textureFormat,
55
55
  "sampleCount": sampleCount,
56
- "usage": GPUTextureUsage.RENDER_ATTACHMENT
56
+ "usage": GPUTextureUsage.RENDER_ATTACHMENT |
57
+ (name === "main" ? GPUTextureUsage.TEXTURE_BINDING : 0)
57
58
  });
58
59
  const msaaTextureView = msaaGpuTexture.createView();
59
60
  msaaTexture = {
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @description フレーム内のインスタンスを別々の領域へ格納し、submit前に一括転送する。
3
+ * Stage instance batches in disjoint ranges and upload before submission.
4
+ */
5
+ export declare class InstanceBufferAllocator {
6
+ private readonly device;
7
+ private readonly maxSize;
8
+ private capacity;
9
+ private buffer;
10
+ private staging;
11
+ private offset;
12
+ private dirtyStart;
13
+ private readonly retiredBuffers;
14
+ constructor(device: GPUDevice, capacity?: number);
15
+ getBuffer(): GPUBuffer;
16
+ allocate(data: Float32Array): number;
17
+ flush(): void;
18
+ /**
19
+ * @description コマンド送信後にのみ呼び出す。確保済み領域は次フレームで再利用する。
20
+ * Call only after submission; reuse the current buffer next frame.
21
+ */
22
+ resetFrame(): void;
23
+ dispose(): void;
24
+ }
@@ -0,0 +1,123 @@
1
+ /**
2
+ * @description フレーム内のインスタンスを別々の領域へ格納し、submit前に一括転送する。
3
+ * Stage instance batches in disjoint ranges and upload before submission.
4
+ */
5
+ export class InstanceBufferAllocator {
6
+ constructor(device, capacity = 65536) {
7
+ Object.defineProperty(this, "device", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ Object.defineProperty(this, "maxSize", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "capacity", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ Object.defineProperty(this, "buffer", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: null
30
+ });
31
+ Object.defineProperty(this, "staging", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: void 0
36
+ });
37
+ Object.defineProperty(this, "offset", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: 0
42
+ });
43
+ Object.defineProperty(this, "dirtyStart", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: 0
48
+ });
49
+ Object.defineProperty(this, "retiredBuffers", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: []
54
+ });
55
+ this.device = device;
56
+ this.maxSize = device.limits?.maxBufferSize ?? 268435456;
57
+ this.capacity = Math.min(this.maxSize, capacity);
58
+ this.staging = new Float32Array(this.capacity / 4);
59
+ }
60
+ getBuffer() {
61
+ if (!this.buffer) {
62
+ this.buffer = this.device.createBuffer({
63
+ "size": this.capacity,
64
+ "usage": GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
65
+ });
66
+ }
67
+ return this.buffer;
68
+ }
69
+ allocate(data) {
70
+ if (data.byteLength > this.maxSize) {
71
+ throw new RangeError("Instance batch exceeds maxBufferSize");
72
+ }
73
+ if (this.offset + data.byteLength > this.capacity) {
74
+ // 記録済みのdrawは旧GPUBufferを参照する。旧領域への転送を完了し、
75
+ // submit後までそのバッファを生存させる。
76
+ this.flush();
77
+ if (this.buffer) {
78
+ this.retiredBuffers.push(this.buffer);
79
+ this.buffer = null;
80
+ }
81
+ this.capacity = Math.min(this.maxSize, Math.max(this.capacity * 2, data.byteLength));
82
+ this.staging = new Float32Array(this.capacity / 4);
83
+ this.offset = 0;
84
+ this.dirtyStart = 0;
85
+ }
86
+ const offset = this.offset;
87
+ // 大きなバッチは既に転送が集約されているため、CPUでの二重コピーを省く。
88
+ // 前後の小バッチは別の範囲としてflushし、直接転送した領域を上書きしない。
89
+ if (data.byteLength >= 16384) {
90
+ this.flush();
91
+ this.device.queue.writeBuffer(this.getBuffer(), offset, data.buffer, data.byteOffset, data.byteLength);
92
+ this.dirtyStart = offset + data.byteLength;
93
+ }
94
+ else {
95
+ this.staging.set(data, offset / 4);
96
+ }
97
+ this.offset += data.byteLength;
98
+ return offset;
99
+ }
100
+ flush() {
101
+ if (this.offset > this.dirtyStart) {
102
+ this.device.queue.writeBuffer(this.getBuffer(), this.dirtyStart, this.staging.buffer, this.dirtyStart, this.offset - this.dirtyStart);
103
+ this.dirtyStart = this.offset;
104
+ }
105
+ }
106
+ /**
107
+ * @description コマンド送信後にのみ呼び出す。確保済み領域は次フレームで再利用する。
108
+ * Call only after submission; reuse the current buffer next frame.
109
+ */
110
+ resetFrame() {
111
+ for (const buffer of this.retiredBuffers) {
112
+ buffer.destroy();
113
+ }
114
+ this.retiredBuffers.length = 0;
115
+ this.offset = 0;
116
+ this.dirtyStart = 0;
117
+ }
118
+ dispose() {
119
+ this.resetFrame();
120
+ this.buffer?.destroy();
121
+ this.buffer = null;
122
+ }
123
+ }