@next2d/webgpu 3.8.1 → 3.9.0

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.
@@ -84,3 +84,107 @@ export const $clearFilterGradientAttachment = () => {
84
84
  }
85
85
  $filterGradientAttachment = null;
86
86
  };
87
+ /**
88
+ * @type {Map<string, IFilterGradientLUTEntry>}
89
+ * @private
90
+ */
91
+ const $filterLUTCache = new Map();
92
+ /**
93
+ * @type {number}
94
+ * @private
95
+ */
96
+ let $filterLUTCurrentFrame = 0;
97
+ /**
98
+ * @description フィルターLUTキャッシュのTTL(フレーム数)
99
+ * TTL for the filter LUT cache in frames
100
+ *
101
+ * @type {number}
102
+ * @const
103
+ */
104
+ const $FILTER_LUT_TTL = 60;
105
+ /**
106
+ * @description フィルターLUTのキャッシュキーを生成
107
+ * Build a cache key for a filter gradient LUT
108
+ *
109
+ * @param {Float32Array | number[]} ratios
110
+ * @param {Float32Array | number[]} colors
111
+ * @param {Float32Array | number[]} alphas
112
+ * @return {string}
113
+ * @private
114
+ */
115
+ const $buildFilterLUTKey = (ratios, colors, alphas) => {
116
+ return `${ratios.join(",")}_${colors.join(",")}_${alphas.join(",")}`;
117
+ };
118
+ /**
119
+ * @description キャッシュからフィルターLUTを取得。ヒットしなければ null。
120
+ * Retrieve a filter LUT from the cache. Returns null on miss.
121
+ *
122
+ * @param {Float32Array | number[]} ratios
123
+ * @param {Float32Array | number[]} colors
124
+ * @param {Float32Array | number[]} alphas
125
+ * @return {IFilterGradientLUTEntry | null}
126
+ * @method
127
+ * @protected
128
+ */
129
+ export const $getFilterLUTFromCache = (ratios, colors, alphas) => {
130
+ const entry = $filterLUTCache.get($buildFilterLUTKey(ratios, colors, alphas));
131
+ if (entry) {
132
+ entry.lastUsedFrame = $filterLUTCurrentFrame;
133
+ return entry;
134
+ }
135
+ return null;
136
+ };
137
+ /**
138
+ * @description フィルターLUTをキャッシュに格納
139
+ * Store a filter LUT into the cache
140
+ *
141
+ * @param {Float32Array | number[]} ratios
142
+ * @param {Float32Array | number[]} colors
143
+ * @param {Float32Array | number[]} alphas
144
+ * @param {GPUTexture} texture
145
+ * @param {GPUTextureView} view
146
+ * @return {IFilterGradientLUTEntry}
147
+ * @method
148
+ * @protected
149
+ */
150
+ export const $putFilterLUTToCache = (ratios, colors, alphas, texture, view) => {
151
+ const entry = {
152
+ texture,
153
+ view,
154
+ "lastUsedFrame": $filterLUTCurrentFrame
155
+ };
156
+ $filterLUTCache.set($buildFilterLUTKey(ratios, colors, alphas), entry);
157
+ return entry;
158
+ };
159
+ /**
160
+ * @description フレーム終了時にTTL超過エントリを解放
161
+ * Release entries that exceed the TTL at the end of each frame
162
+ *
163
+ * @return {void}
164
+ * @method
165
+ * @protected
166
+ */
167
+ export const $cleanupFilterLUTCache = () => {
168
+ $filterLUTCurrentFrame++;
169
+ for (const [key, entry] of $filterLUTCache) {
170
+ if ($filterLUTCurrentFrame - entry.lastUsedFrame > $FILTER_LUT_TTL) {
171
+ entry.texture.destroy();
172
+ $filterLUTCache.delete(key);
173
+ }
174
+ }
175
+ };
176
+ /**
177
+ * @description 全フィルターLUTキャッシュを破棄
178
+ * Destroy and clear the entire filter LUT cache
179
+ *
180
+ * @return {void}
181
+ * @method
182
+ * @protected
183
+ */
184
+ export const $clearFilterLUTCache = () => {
185
+ for (const entry of $filterLUTCache.values()) {
186
+ entry.texture.destroy();
187
+ }
188
+ $filterLUTCache.clear();
189
+ $filterLUTCurrentFrame = 0;
190
+ };
@@ -1,6 +1,7 @@
1
1
  import { $offset } from "../FilterOffset";
2
2
  import { execute as filterApplyBlurFilterUseCase } from "../BlurFilter/FilterApplyBlurFilterUseCase";
3
3
  import { generateFilterGradientLUT } from "../../Gradient/GradientLUTGenerator";
4
+ import { $getFilterLUTFromCache, $putFilterLUTToCache } from "../FilterGradientLUTCache";
4
5
  import { DEG_TO_RAD } from "../FilterUtil";
5
6
  /**
6
7
  * @description プリアロケートされたFloat32Array
@@ -131,14 +132,20 @@ export const execute = (source_attachment, matrix, distance, angle, colors, alph
131
132
  // queue.writeTextureはcommandEncoder外で即座に実行されるため、
132
133
  // 同一フレーム内の複数GradientBevelFilter適用時に最後の書き込みで上書きされる。
133
134
  // 各呼び出しで専用テクスチャを作成してこのタイミング問題を回避する。
134
- const lutData = generateFilterGradientLUT(ratios, colors, alphas);
135
- const lutTexture = device.createTexture({
136
- "size": { "width": 256, "height": 1 },
137
- "format": "rgba8unorm",
138
- "usage": GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
139
- });
140
- device.queue.writeTexture({ "texture": lutTexture }, lutData.buffer, { "bytesPerRow": 256 * 4, "offset": lutData.byteOffset }, { "width": 256, "height": 1 });
141
- const lutView = lutTexture.createView();
135
+ // stopsキーのキャッシュを参照し、同一グラデーションのLUT再生成を回避する
136
+ // (キー毎に専用テクスチャのため、同一フレーム内の複数フィルター適用でも上書きされない)
137
+ let lutEntry = $getFilterLUTFromCache(ratios, colors, alphas);
138
+ if (!lutEntry) {
139
+ const lutData = generateFilterGradientLUT(ratios, colors, alphas);
140
+ const lutTexture = device.createTexture({
141
+ "size": { "width": 256, "height": 1 },
142
+ "format": "rgba8unorm",
143
+ "usage": GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
144
+ });
145
+ device.queue.writeTexture({ "texture": lutTexture }, lutData.buffer, { "bytesPerRow": 256 * 4, "offset": lutData.byteOffset }, { "width": 256, "height": 1 });
146
+ lutEntry = $putFilterLUTToCache(ratios, colors, alphas, lutTexture, lutTexture.createView());
147
+ }
148
+ const lutView = lutEntry.view;
142
149
  // ===== Step 5: UV変換パラメータ計算 =====
143
150
  // WebGL版と同じ: uv = v_coord * scale - offset
144
151
  // WebGPU: texCoord.y=0がトップ(Y-flip補正済み)
@@ -203,8 +210,6 @@ export const execute = (source_attachment, matrix, distance, angle, colors, alph
203
210
  passEncoder.setBindGroup(0, bindGroup);
204
211
  passEncoder.draw(6, 1, 0, 0);
205
212
  passEncoder.end();
206
- // クリーンアップ(lutTextureはsubmit後に遅延破棄)
207
- config.frameTextures.push(lutTexture);
208
213
  frameBufferManager.releaseTemporaryAttachment(blurAttachment);
209
214
  // WebGL版と同じオフセット更新
210
215
  $offset.x = baseOffsetX + baseTextureX;
@@ -1,6 +1,7 @@
1
1
  import { $offset } from "../FilterOffset";
2
2
  import { execute as filterApplyBlurFilterUseCase } from "../BlurFilter/FilterApplyBlurFilterUseCase";
3
3
  import { generateFilterGradientLUT } from "../../Gradient/GradientLUTGenerator";
4
+ import { $getFilterLUTFromCache, $putFilterLUTToCache } from "../FilterGradientLUTCache";
4
5
  import { DEG_TO_RAD } from "../FilterUtil";
5
6
  /**
6
7
  * @description プリアロケートされたFloat32Array (サイズ12)
@@ -81,14 +82,20 @@ export const execute = (source_attachment, matrix, distance, angle, colors, alph
81
82
  // 注意: 共有テクスチャは使用しない。
82
83
  // queue.writeTextureはcommandEncoder外で即座に実行されるため、
83
84
  // 同一フレーム内の複数GradientGlowFilter適用時に最後の書き込みで上書きされる。
84
- const lutData = generateFilterGradientLUT(ratios, colors, alphas);
85
- const lutTexture = device.createTexture({
86
- "size": { "width": 256, "height": 1 },
87
- "format": "rgba8unorm",
88
- "usage": GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
89
- });
90
- device.queue.writeTexture({ "texture": lutTexture }, lutData.buffer, { "bytesPerRow": 256 * 4, "offset": lutData.byteOffset }, { "width": 256, "height": 1 });
91
- const lutView = lutTexture.createView();
85
+ // stopsキーのキャッシュを参照し、同一グラデーションのLUT再生成を回避する
86
+ // (キー毎に専用テクスチャのため、同一フレーム内の複数フィルター適用でも上書きされない)
87
+ let lutEntry = $getFilterLUTFromCache(ratios, colors, alphas);
88
+ if (!lutEntry) {
89
+ const lutData = generateFilterGradientLUT(ratios, colors, alphas);
90
+ const lutTexture = device.createTexture({
91
+ "size": { "width": 256, "height": 1 },
92
+ "format": "rgba8unorm",
93
+ "usage": GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
94
+ });
95
+ device.queue.writeTexture({ "texture": lutTexture }, lutData.buffer, { "bytesPerRow": 256 * 4, "offset": lutData.byteOffset }, { "width": 256, "height": 1 });
96
+ lutEntry = $putFilterLUTToCache(ratios, colors, alphas, lutTexture, lutTexture.createView());
97
+ }
98
+ const lutView = lutEntry.view;
92
99
  // ===== UV変換パラメータ計算 =====
93
100
  // WebGPU: texCoord.y=0がトップ(Y-flip補正済み)
94
101
  const baseScaleX = width / baseWidth;
@@ -151,8 +158,6 @@ export const execute = (source_attachment, matrix, distance, angle, colors, alph
151
158
  passEncoder.setBindGroup(0, bindGroup);
152
159
  passEncoder.draw(6, 1, 0, 0);
153
160
  passEncoder.end();
154
- // クリーンアップ(lutTextureはsubmit後に遅延破棄)
155
- config.frameTextures.push(lutTexture);
156
161
  frameBufferManager.releaseTemporaryAttachment(blurAttachment);
157
162
  // WebGL版と同じオフセット更新
158
163
  $offset.x = baseOffsetX + baseTextureX;
@@ -0,0 +1,20 @@
1
+ import type { IAttachmentObject } from "../../interface/IAttachmentObject";
2
+ import type { FrameBufferManager } from "../../FrameBufferManager";
3
+ /**
4
+ * @description MSAAテクスチャに未リゾルブの描き込みがあれば、リゾルブ専用パス
5
+ * (描画なし・load/store + resolveTarget)で1回だけ解決する。
6
+ * 書き込みパスはresolveTargetを持たないため(リゾルブ遅延)、
7
+ * 解決はリゾルブ済みテクスチャの読み手の直前に集約される。
8
+ * Resolve the attachment's MSAA texture once via a resolve-only
9
+ * pass when it has unresolved drawing. Write passes carry no
10
+ * resolveTarget (deferred resolve), so resolution happens only
11
+ * right before the resolved texture is read.
12
+ *
13
+ * @param {GPUCommandEncoder} command_encoder
14
+ * @param {FrameBufferManager} frame_buffer_manager
15
+ * @param {IAttachmentObject} attachment_object
16
+ * @return {void}
17
+ * @method
18
+ * @protected
19
+ */
20
+ export declare const execute: (command_encoder: GPUCommandEncoder, frame_buffer_manager: FrameBufferManager, attachment_object: IAttachmentObject) => void;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @description MSAAテクスチャに未リゾルブの描き込みがあれば、リゾルブ専用パス
3
+ * (描画なし・load/store + resolveTarget)で1回だけ解決する。
4
+ * 書き込みパスはresolveTargetを持たないため(リゾルブ遅延)、
5
+ * 解決はリゾルブ済みテクスチャの読み手の直前に集約される。
6
+ * Resolve the attachment's MSAA texture once via a resolve-only
7
+ * pass when it has unresolved drawing. Write passes carry no
8
+ * resolveTarget (deferred resolve), so resolution happens only
9
+ * right before the resolved texture is read.
10
+ *
11
+ * @param {GPUCommandEncoder} command_encoder
12
+ * @param {FrameBufferManager} frame_buffer_manager
13
+ * @param {IAttachmentObject} attachment_object
14
+ * @return {void}
15
+ * @method
16
+ * @protected
17
+ */
18
+ export const execute = (command_encoder, frame_buffer_manager, attachment_object) => {
19
+ if (!attachment_object.msaaDirty
20
+ || !attachment_object.msaa
21
+ || !attachment_object.msaaTexture?.view
22
+ || !attachment_object.texture?.view) {
23
+ return;
24
+ }
25
+ const renderPassDescriptor = frame_buffer_manager.createRenderPassDescriptor(attachment_object.msaaTexture.view, 0, 0, 0, 0, "load", attachment_object.texture.view);
26
+ command_encoder.beginRenderPass(renderPassDescriptor).end();
27
+ attachment_object.msaaDirty = false;
28
+ };
@@ -3,6 +3,7 @@ import { execute as frameBufferManagerReleaseTemporaryAttachmentUseCase } from "
3
3
  import { execute as frameBufferManagerCreateRenderPassDescriptorService } from "./FrameBufferManager/service/FrameBufferManagerCreateRenderPassDescriptorService";
4
4
  import { execute as frameBufferManagerCreateStencilRenderPassDescriptorService } from "./FrameBufferManager/service/FrameBufferManagerCreateStencilRenderPassDescriptorService";
5
5
  import { TexturePool } from "./TexturePool";
6
+ import { $getOrCreateView } from "./FillTexturePool";
6
7
  /**
7
8
  * @description フレームバッファとアタッチメントの管理クラス
8
9
  * Manager class for frame buffers and attachments
@@ -171,8 +172,10 @@ export class FrameBufferManager {
171
172
  GPUTextureUsage.COPY_SRC |
172
173
  GPUTextureUsage.COPY_DST |
173
174
  GPUTextureUsage.STORAGE_BINDING;
175
+ // プールから再利用したGPUTextureのviewはWeakMapでキャッシュして再生成を避ける。
176
+ // view identityが安定することで、viewをキーにしたBindGroupキャッシュも有効になる。
174
177
  const gpuTexture = this.texturePool.acquire(width, height, "rgba8unorm", usage);
175
- const textureView = gpuTexture.createView();
178
+ const textureView = $getOrCreateView(gpuTexture);
176
179
  const texture = {
177
180
  "id": this.idCounter.textureId++,
178
181
  "resource": gpuTexture,
@@ -74,21 +74,24 @@ export const generateGradientLUT = (stops, _spread, interpolation) => {
74
74
  const $interpolateColor = (stops, t, interpolation) => {
75
75
  const stopsLength = stops.length / 5;
76
76
  // 最初と最後のストップを見つける
77
+ // endIdx の番兵は -1(0 は「最初のストップが該当」の正当な値のため、
78
+ // 0 を番兵にすると t が最初のストップ未満のときに最後のストップと
79
+ // 誤って補間され、範囲外へ外挿されてしまう)
77
80
  let startIdx = 0;
78
- let endIdx = 0;
81
+ let endIdx = -1;
79
82
  for (let i = 0; i < stopsLength; i++) {
80
83
  // offset は既に 0-1 範囲
81
84
  const offset = stops[i * 5];
82
85
  if (offset <= t) {
83
86
  startIdx = i;
84
87
  }
85
- if (offset >= t && endIdx === 0) {
88
+ if (offset >= t && endIdx === -1) {
86
89
  endIdx = i;
87
90
  break;
88
91
  }
89
92
  }
90
93
  // 最後のストップを超えている場合
91
- if (endIdx === 0) {
94
+ if (endIdx === -1) {
92
95
  endIdx = stopsLength - 1;
93
96
  }
94
97
  // 同じストップの場合(色は0-255範囲)
@@ -79,6 +79,16 @@ export declare class PipelineManager {
79
79
  * Preload all lazy initialization groups eagerly
80
80
  */
81
81
  preloadLazyGroups(): void;
82
+ /**
83
+ * @description 遅延初期化グループをバックグラウンドで順次warm upする。
84
+ * 1グループ=1マクロタスクに分割して起動処理・初回フレームをブロックしない。
85
+ * warm up前に必要になった場合は getPipeline() の遅延初期化が同期生成する
86
+ * (ensureLazyGroup は初期化済みグループをスキップするため二重生成しない)。
87
+ * Warm up lazy pipeline groups in the background, one group per
88
+ * macrotask, so startup and the first frame are not blocked.
89
+ * Pipelines needed earlier are created on demand by getPipeline().
90
+ */
91
+ warmupLazyGroupsAsync(): void;
82
92
  /**
83
93
  * @description 塗りつぶし描画用パイプラインを作成する(RGBA/BGRA/ステンシル対応)
84
94
  * Create fill render pipelines (RGBA, BGRA, and stencil variants)
@@ -275,6 +275,27 @@ export class PipelineManager {
275
275
  this.ensureLazyGroup(group);
276
276
  }
277
277
  }
278
+ /**
279
+ * @description 遅延初期化グループをバックグラウンドで順次warm upする。
280
+ * 1グループ=1マクロタスクに分割して起動処理・初回フレームをブロックしない。
281
+ * warm up前に必要になった場合は getPipeline() の遅延初期化が同期生成する
282
+ * (ensureLazyGroup は初期化済みグループをスキップするため二重生成しない)。
283
+ * Warm up lazy pipeline groups in the background, one group per
284
+ * macrotask, so startup and the first frame are not blocked.
285
+ * Pipelines needed earlier are created on demand by getPipeline().
286
+ */
287
+ warmupLazyGroupsAsync() {
288
+ const groups = ["texture_copy", "blur_filter", "filter", "complex_blend", "bitmap_sync"];
289
+ let idx = 0;
290
+ const next = () => {
291
+ if (idx >= groups.length) {
292
+ return;
293
+ }
294
+ this.ensureLazyGroup(groups[idx++]);
295
+ setTimeout(next, 0);
296
+ };
297
+ setTimeout(next, 0);
298
+ }
278
299
  /**
279
300
  * @description 塗りつぶし描画用パイプラインを作成する(RGBA/BGRA/ステンシル対応)
280
301
  * Create fill render pipelines (RGBA, BGRA, and stencil variants)
@@ -59,6 +59,11 @@ export interface IAttachmentObject {
59
59
  * MSAA texture (used when sampleCount > 1)
60
60
  */
61
61
  msaaTexture: ITextureObject | null;
62
+ /**
63
+ * @description MSAAテクスチャに未リゾルブの描き込みがあるか(リゾルブ遅延用)。
64
+ * Whether the MSAA texture has unresolved drawing (deferred resolve).
65
+ */
66
+ msaaDirty?: boolean;
62
67
  /**
63
68
  * @description MSAAステンシルテクスチャ(sampleCount > 1 の場合に使用)
64
69
  * MSAA stencil texture (used when sampleCount > 1)