@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.
package/src/Context.js CHANGED
@@ -7,7 +7,7 @@ import { TextureManager } from "./TextureManager";
7
7
  import { FrameBufferManager } from "./FrameBufferManager";
8
8
  import { PipelineManager } from "./Shader/PipelineManager";
9
9
  import { $rootNodes, $resetAtlas, $getActiveAtlasIndex, $setActiveAtlasIndex, $setAtlasCreator, $getAtlasAttachmentObject, $getAtlasAttachmentObjectByIndex } from "./AtlasManager";
10
- import { addDisplayObjectToInstanceArray, getInstancedShaderManager } from "./Blend/BlendInstancedManager";
10
+ import { addDisplayObjectToInstanceArray, getInstancedShaderManager, getComplexBlendQueue } from "./Blend/BlendInstancedManager";
11
11
  import { execute as maskBeginMaskService } from "./Mask/service/MaskBeginMaskService";
12
12
  import { execute as maskSetMaskBoundsService } from "./Mask/service/MaskSetMaskBoundsService";
13
13
  import { execute as maskEndMaskService } from "./Mask/service/MaskEndMaskService";
@@ -18,9 +18,10 @@ import { generateStrokeMesh } from "./Mesh/usecase/MeshStrokeGenerateUseCase";
18
18
  import { $gridDataMap, $fillBufferIndex, $terminateGrid } from "./Grid";
19
19
  import { $setGradientLUTDevice, $clearGradientAttachmentObjects, $cleanupLUTCache, $clearLUTCache } from "./Gradient/GradientLUTCache";
20
20
  import { $releaseFillTexture, $acquireRenderTexture, $releaseRenderTexture, $clearFillTexturePool, $getOrCreateView } from "./FillTexturePool";
21
- import { $setFilterGradientLUTDevice, $clearFilterGradientAttachment } from "./Filter/FilterGradientLUTCache";
21
+ import { $setFilterGradientLUTDevice, $clearFilterGradientAttachment, $cleanupFilterLUTCache, $clearFilterLUTCache } from "./Filter/FilterGradientLUTCache";
22
22
  // Context services
23
23
  import { execute as contextFillWithStencilService } from "./Context/service/ContextFillWithStencilService";
24
+ import { execute as frameBufferManagerResolveAttachmentService } from "./FrameBufferManager/service/FrameBufferManagerResolveAttachmentService";
24
25
  import { execute as contextFillWithStencilMainService } from "./Context/service/ContextFillWithStencilMainService";
25
26
  import { execute as contextFillSimpleService } from "./Context/service/ContextFillSimpleService";
26
27
  // Context usecases
@@ -80,6 +81,11 @@ const $ctUniform8 = new Float32Array(8);
80
81
  * Pre-allocated uniform for atlas node copy with Y-flip
81
82
  */
82
83
  const $atlasNodeCopyUniform = new Float32Array([1, -1, 0, 1]);
84
+ // copyTempToAtlasNode() 用 Static BindGroup キャッシュ
85
+ // uniformは定数、samplerはキャッシュ済みのため、source viewのみが可変。
86
+ // viewをキーにBindGroupを再利用する(present()と同型)。
87
+ let $atlasNodeCopyUniformBuffer = null;
88
+ const $atlasNodeCopyBindGroups = new WeakMap();
83
89
  /**
84
90
  * @description fill() 用 uniform プリアロケート (color + matrix = 16 floats = 64 bytes)
85
91
  * Pre-allocated uniform for fill() (color + matrix = 16 floats = 64 bytes)
@@ -317,6 +323,20 @@ export class Context {
317
323
  writable: true,
318
324
  value: null
319
325
  });
326
+ /**
327
+ * @description 現在openなrenderPassEncoderがインスタンスバッチ用パスかどうか。
328
+ * インスタンスパスは連続バッチ間で再利用されるが、fill/clip等の
329
+ * ステンシル前提のパスとは互換性がないため、他経路では必ず閉じる。
330
+ * Whether the currently open render pass belongs to instanced
331
+ * batching. Such passes are reused across consecutive batches
332
+ * but are incompatible with stencil-based fill/clip passes.
333
+ */
334
+ Object.defineProperty(this, "renderPassIsInstanced", {
335
+ enumerable: true,
336
+ configurable: true,
337
+ writable: true,
338
+ value: false
339
+ });
320
340
  /** @description メインキャンバステクスチャ(最終表示用、フレームごとに1回取得) / Main canvas texture (for final display, acquired once per frame) */
321
341
  Object.defineProperty(this, "mainTexture", {
322
342
  enumerable: true,
@@ -573,8 +593,9 @@ export class Context {
573
593
  this.textureManager = new TextureManager(device);
574
594
  this.frameBufferManager = new FrameBufferManager(device, preferred_format);
575
595
  this.pipelineManager = new PipelineManager(device, preferred_format);
576
- // 遅延パイプライン群を即座に先行作成(初回アクセス時のレイテンシ解消)
577
- this.pipelineManager.preloadLazyGroups();
596
+ // 遅延パイプライン群はバックグラウンドで順次warm up(起動ブロック解消)。
597
+ // warm up前に必要になったパイプラインは getPipeline() が同期生成する。
598
+ this.pipelineManager.warmupLazyGroupsAsync();
578
599
  // グラデーションLUT共有アタッチメントにGPUDeviceを設定
579
600
  $setGradientLUTDevice(device);
580
601
  $setFilterGradientLUTDevice(device);
@@ -651,8 +672,11 @@ export class Context {
651
672
  $bgColorAttachment.view = clearUseMsaa
652
673
  ? this.$mainAttachmentObject.msaaTexture.view
653
674
  : this.$mainAttachmentObject.texture.view;
654
- $bgColorAttachment.resolveTarget = clearUseMsaa
655
- ? this.$mainAttachmentObject.texture.view : undefined;
675
+ // リゾルブ遅延: クリアも書き込みパスとして扱い、読み手直前にまとめて解決する
676
+ $bgColorAttachment.resolveTarget = undefined;
677
+ if (clearUseMsaa) {
678
+ this.$mainAttachmentObject.msaaDirty = true;
679
+ }
656
680
  $bgClearValue.r = this.$clearColorR;
657
681
  $bgClearValue.g = this.$clearColorG;
658
682
  $bgClearValue.b = this.$clearColorB;
@@ -670,6 +694,7 @@ export class Context {
670
694
  }
671
695
  // 背景クリア用のレンダーパスを開始して即座に終了
672
696
  this.renderPassEncoder = this.commandEncoder.beginRenderPass($bgDescriptor);
697
+ this.renderPassIsInstanced = false;
673
698
  this.renderPassEncoder.end();
674
699
  this.renderPassEncoder = null;
675
700
  }
@@ -744,6 +769,7 @@ export class Context {
744
769
  $clearGradientAttachmentObjects();
745
770
  $clearLUTCache();
746
771
  $clearFilterGradientAttachment();
772
+ $clearFilterLUTCache();
747
773
  // アトラスのパッキングデータをリセット(WebGL版と同じ)
748
774
  $resetAtlas();
749
775
  // FrameBufferManagerのアトラステクスチャを再作成(古いコンテンツをクリア)
@@ -976,11 +1002,17 @@ export class Context {
976
1002
  // コマンドエンコーダーを確保
977
1003
  this.ensureCommandEncoder();
978
1004
  // 既存のレンダーパスがある場合はearlyリターン(ノード領域クリアのみ確認)
1005
+ // ただしインスタンスバッチ用パスはステンシル構成が異なるため引き継がず閉じる
979
1006
  if (this.renderPassEncoder) {
980
- if (this.currentRenderTarget) {
981
- this.ensureNodeAreaCleared();
1007
+ if (!this.renderPassIsInstanced) {
1008
+ if (this.currentRenderTarget) {
1009
+ this.ensureNodeAreaCleared();
1010
+ }
1011
+ return;
982
1012
  }
983
- return;
1013
+ this.renderPassEncoder.end();
1014
+ this.renderPassEncoder = null;
1015
+ this.renderPassIsInstanced = false;
984
1016
  }
985
1017
  // レンダーパスがない場合のみ新規作成
986
1018
  {
@@ -1002,6 +1034,7 @@ export class Context {
1002
1034
  const renderPassDescriptor = this.frameBufferManager.createStencilRenderPassDescriptor(colorView, stencilView, "load", "clear", // ステンシルをクリア
1003
1035
  resolveTarget);
1004
1036
  this.renderPassEncoder = this.commandEncoder.beginRenderPass(renderPassDescriptor);
1037
+ this.renderPassIsInstanced = false;
1005
1038
  }
1006
1039
  else if (!this.currentRenderTarget && ($isMaskTestEnabled() || $isMaskDrawing()) && this.$mainAttachmentObject?.stencil?.view) {
1007
1040
  // マスク描画時またはマスクテスト有効時のメインアタッチメントへの描画(ステンシル付き)
@@ -1012,10 +1045,15 @@ export class Context {
1012
1045
  const mainStencilView = mainUseMsaa && this.$mainAttachmentObject.msaaStencil?.view
1013
1046
  ? this.$mainAttachmentObject.msaaStencil.view
1014
1047
  : this.$mainAttachmentObject.stencil.view;
1015
- const mainResolveTarget = mainUseMsaa ? this.$mainAttachmentObject.texture.view : null;
1048
+ // リゾルブ遅延: 書き込みパスでは解決しない
1049
+ const mainResolveTarget = null;
1050
+ if (mainUseMsaa) {
1051
+ this.$mainAttachmentObject.msaaDirty = true;
1052
+ }
1016
1053
  const renderPassDescriptor = this.frameBufferManager.createStencilRenderPassDescriptor(mainColorView, mainStencilView, "load", "load", // ステンシルは既存の値を保持(マスク情報)
1017
1054
  mainResolveTarget);
1018
1055
  this.renderPassEncoder = this.commandEncoder.beginRenderPass(renderPassDescriptor);
1056
+ this.renderPassIsInstanced = false;
1019
1057
  // マスクテスト時はステンシル参照値を設定
1020
1058
  if ($isMaskTestEnabled()) {
1021
1059
  this.renderPassEncoder.setStencilReference($getMaskStencilReference());
@@ -1026,7 +1064,11 @@ export class Context {
1026
1064
  // 2パスステンシルフィルを使用するため、常にステンシル付きレンダーパスを作成
1027
1065
  const mainUseMsaa = this.$mainAttachmentObject.msaa && this.$mainAttachmentObject.msaaTexture?.view;
1028
1066
  const mainColorView = mainUseMsaa ? this.$mainAttachmentObject.msaaTexture.view : this.$mainAttachmentObject.texture.view;
1029
- const mainResolveTarget = mainUseMsaa ? this.$mainAttachmentObject.texture.view : null;
1067
+ // リゾルブ遅延: 書き込みパスでは解決しない
1068
+ const mainResolveTarget = null;
1069
+ if (mainUseMsaa) {
1070
+ this.$mainAttachmentObject.msaaDirty = true;
1071
+ }
1030
1072
  if (this.$mainAttachmentObject.stencil?.view) {
1031
1073
  // ステンシル付きレンダーパス(2パスステンシルフィル用)
1032
1074
  const mainStencilView = mainUseMsaa && this.$mainAttachmentObject.msaaStencil?.view
@@ -1035,16 +1077,19 @@ export class Context {
1035
1077
  const renderPassDescriptor = this.frameBufferManager.createStencilRenderPassDescriptor(mainColorView, mainStencilView, "load", "clear", // ステンシルをクリア(各描画でステンシルを0からスタート)
1036
1078
  mainResolveTarget);
1037
1079
  this.renderPassEncoder = this.commandEncoder.beginRenderPass(renderPassDescriptor);
1080
+ this.renderPassIsInstanced = false;
1038
1081
  }
1039
1082
  else {
1040
1083
  // ステンシルなし(フォールバック)
1041
1084
  const renderPassDescriptor = this.frameBufferManager.createRenderPassDescriptor(mainColorView, 0, 0, 0, 0, "load", mainResolveTarget);
1042
1085
  this.renderPassEncoder = this.commandEncoder.beginRenderPass(renderPassDescriptor);
1086
+ this.renderPassIsInstanced = false;
1043
1087
  }
1044
1088
  }
1045
1089
  else {
1046
1090
  const renderPassDescriptor = this.frameBufferManager.createRenderPassDescriptor(colorView, 0, 0, 0, 0, "load", resolveTarget);
1047
1091
  this.renderPassEncoder = this.commandEncoder.beginRenderPass(renderPassDescriptor);
1092
+ this.renderPassIsInstanced = false;
1048
1093
  }
1049
1094
  }
1050
1095
  // ノードレンダリング中の場合、最初の描画前にノード領域をクリア
@@ -1415,6 +1460,12 @@ export class Context {
1415
1460
  if (pathVertices.length === 0) {
1416
1461
  return;
1417
1462
  }
1463
+ // インスタンスバッチ用パスはステンシル構成が異なるため引き継がず閉じる
1464
+ if (this.renderPassEncoder && this.renderPassIsInstanced) {
1465
+ this.renderPassEncoder.end();
1466
+ this.renderPassEncoder = null;
1467
+ this.renderPassIsInstanced = false;
1468
+ }
1418
1469
  // レンダーパスがない場合は作成
1419
1470
  if (!this.renderPassEncoder) {
1420
1471
  this.ensureCommandEncoder();
@@ -1430,6 +1481,7 @@ export class Context {
1430
1481
  // resolveTargetなし: clip()はwriteMask=0でcolorを変更しない
1431
1482
  const renderPassDescriptor = this.frameBufferManager.createStencilRenderPassDescriptor(clipColorView, clipStencilView, "load", "load");
1432
1483
  this.renderPassEncoder = this.commandEncoder.beginRenderPass(renderPassDescriptor);
1484
+ this.renderPassIsInstanced = false;
1433
1485
  }
1434
1486
  else {
1435
1487
  return;
@@ -1527,10 +1579,12 @@ export class Context {
1527
1579
  : attachment.stencil.view;
1528
1580
  const renderPassDescriptor = this.frameBufferManager.createStencilRenderPassDescriptor(colorView, stencilView, "load", "load", resolveTarget);
1529
1581
  this.renderPassEncoder = this.commandEncoder.beginRenderPass(renderPassDescriptor);
1582
+ this.renderPassIsInstanced = false;
1530
1583
  }
1531
1584
  else {
1532
1585
  const renderPassDescriptor = this.frameBufferManager.createRenderPassDescriptor(colorView, 0, 0, 0, 0, "load", resolveTarget);
1533
1586
  this.renderPassEncoder = this.commandEncoder.beginRenderPass(renderPassDescriptor);
1587
+ this.renderPassIsInstanced = false;
1534
1588
  }
1535
1589
  this.nodeRenderPassAtlasIndex = node.index;
1536
1590
  }
@@ -1669,13 +1723,45 @@ export class Context {
1669
1723
  *
1670
1724
  * @return {void}
1671
1725
  */
1726
+ /**
1727
+ * @description メインアタッチメントのMSAAテクスチャに未リゾルブの描き込みがあれば、
1728
+ * リゾルブ専用パス(描画なし・load/store+resolveTarget)で1回だけ解決する。
1729
+ * 書き込みパスはresolveTargetを持たないため、リゾルブは
1730
+ * 「リゾルブ済みテクスチャを読む直前」のここに集約される。
1731
+ * Resolve the main attachment's MSAA texture once via a
1732
+ * resolve-only pass when it has unresolved drawing. Write
1733
+ * passes carry no resolveTarget, so resolution happens only
1734
+ * right before the resolved texture is read.
1735
+ *
1736
+ * @return {void}
1737
+ */
1738
+ resolveMainAttachment() {
1739
+ const mainAttachment = this.$mainAttachmentObject;
1740
+ if (!mainAttachment
1741
+ || !mainAttachment.msaaDirty
1742
+ || !mainAttachment.msaa
1743
+ || !mainAttachment.msaaTexture?.view
1744
+ || !mainAttachment.texture?.view) {
1745
+ return;
1746
+ }
1747
+ // 開いているパスがあれば終了(以降のリゾルブ専用パスと並行できないため)
1748
+ if (this.renderPassEncoder) {
1749
+ this.renderPassEncoder.end();
1750
+ this.renderPassEncoder = null;
1751
+ this.renderPassIsInstanced = false;
1752
+ }
1753
+ this.ensureCommandEncoder();
1754
+ frameBufferManagerResolveAttachmentService(this.commandEncoder, this.frameBufferManager, mainAttachment);
1755
+ }
1672
1756
  drawArraysInstanced() {
1673
1757
  // フレームが開始されていない場合は開始
1674
1758
  if (!this.frameStarted) {
1675
1759
  this.beginFrame();
1676
1760
  }
1677
1761
  // 既存のレンダーパスを終了(アトラスパス統合をリセット)
1678
- if (this.renderPassEncoder) {
1762
+ // ただし自前のインスタンスバッチ用パスは、状態が一致すればusecase側で再利用する
1763
+ // (パス終了ごとの全画面MSAAリゾルブとロード/ストア往復を削減)
1764
+ if (this.renderPassEncoder && !this.renderPassIsInstanced) {
1679
1765
  this.renderPassEncoder.end();
1680
1766
  this.renderPassEncoder = null;
1681
1767
  }
@@ -1689,13 +1775,21 @@ export class Context {
1689
1775
  }
1690
1776
  if (this.useOptimizedInstancing) {
1691
1777
  // 最適化版: Storage Buffer + Indirect Drawing
1692
- this.renderPassEncoder = contextDrawIndirectUseCase(this.device, this.commandEncoder, this.renderPassEncoder, this.$mainAttachmentObject, this.bufferManager, this.frameBufferManager, this.textureManager, this.pipelineManager, true, // useIndirect
1693
- true // useStorageBuffer
1694
- );
1778
+ const previousPass = this.renderPassEncoder;
1779
+ this.renderPassEncoder = contextDrawIndirectUseCase(this.device, this.commandEncoder, this.renderPassEncoder, this.$mainAttachmentObject, this.bufferManager, this.frameBufferManager, this.textureManager, this.pipelineManager,
1780
+ // インスタンス数はCPU側で確定しているため間接描画は不要。
1781
+ // indirectバッファ生成+writeBufferのバッチ毎オーバーヘッドを避けて直接draw()する。
1782
+ false, // useIndirect
1783
+ true, // useStorageBuffer
1784
+ this.renderPassIsInstanced);
1785
+ // count===0で未変更返却の場合はフラグ維持、新規/再利用パスならinstanced扱い
1786
+ this.renderPassIsInstanced = this.renderPassEncoder !== null
1787
+ && (this.renderPassEncoder !== previousPass || this.renderPassIsInstanced);
1695
1788
  }
1696
1789
  else {
1697
1790
  // 従来版: 毎フレームVertex Buffer新規生成
1698
1791
  this.renderPassEncoder = contextDrawArraysInstancedUseCase(this.device, this.commandEncoder, this.renderPassEncoder, this.$mainAttachmentObject, this.bufferManager, this.frameBufferManager, this.textureManager, this.pipelineManager);
1792
+ this.renderPassIsInstanced = false;
1699
1793
  }
1700
1794
  // 複雑なブレンドモードの処理
1701
1795
  this.processComplexBlendQueue();
@@ -1707,6 +1801,18 @@ export class Context {
1707
1801
  * @return {void}
1708
1802
  */
1709
1803
  processComplexBlendQueue() {
1804
+ // キューが空なら何もしない(開いたままのインスタンスパスも維持できる)
1805
+ if (!getComplexBlendQueue().length) {
1806
+ return;
1807
+ }
1808
+ // 複雑ブレンドはリゾルブ済みのメインテクスチャを読むため、
1809
+ // 開いているレンダーパスを終了し、未リゾルブ分を解決する
1810
+ if (this.renderPassEncoder) {
1811
+ this.renderPassEncoder.end();
1812
+ this.renderPassEncoder = null;
1813
+ this.renderPassIsInstanced = false;
1814
+ }
1815
+ this.resolveMainAttachment();
1710
1816
  // コマンドエンコーダーを確保
1711
1817
  this.ensureCommandEncoder();
1712
1818
  // $mainAttachmentObjectを渡す(レンダーパスベースのコピーに必要)
@@ -1828,6 +1934,8 @@ export class Context {
1828
1934
  // フレームエンコーダーを使用してMSAAテクスチャに描画
1829
1935
  this.ensureCommandEncoder();
1830
1936
  $msaaColorAttachment.view = attachment.msaaTexture.view;
1937
+ // アトラスへの書き込みは従来通りパス終了時に解決する
1938
+ // (アトラスの解決済みテクスチャはインスタンス描画が毎バッチ参照するため遅延できない)
1831
1939
  $msaaColorAttachment.resolveTarget = attachment.texture.view;
1832
1940
  const stencilView = attachment.msaaStencil?.view;
1833
1941
  if (stencilView) {
@@ -1943,6 +2051,8 @@ export class Context {
1943
2051
  // フレームエンコーダーを使用してMSAAテクスチャに描画
1944
2052
  this.ensureCommandEncoder();
1945
2053
  $msaaColorAttachment.view = attachment.msaaTexture.view;
2054
+ // アトラスへの書き込みは従来通りパス終了時に解決する
2055
+ // (アトラスの解決済みテクスチャはインスタンス描画が毎バッチ参照するため遅延できない)
1946
2056
  $msaaColorAttachment.resolveTarget = attachment.texture.view;
1947
2057
  const stencilView = attachment.msaaStencil?.view;
1948
2058
  if (stencilView) {
@@ -2174,7 +2284,7 @@ export class Context {
2174
2284
  */
2175
2285
  containerEndAtlasNode() {
2176
2286
  this.drawArraysInstanced();
2177
- // 既存のレンダーパスを終了(temp FBOのMSAAリゾルブが実行される)
2287
+ // 既存のレンダーパスを終了
2178
2288
  if (this.renderPassEncoder) {
2179
2289
  this.renderPassEncoder.end();
2180
2290
  this.renderPassEncoder = null;
@@ -2184,6 +2294,8 @@ export class Context {
2184
2294
  const node = this._pendingAtlasNodes.pop();
2185
2295
  // mainを復元
2186
2296
  this.$mainAttachmentObject = this.$containerLayerStack.pop();
2297
+ // リゾルブ遅延: temp FBO(メインのMSAA設定を継承)の未リゾルブ分を解決してからコピー
2298
+ frameBufferManagerResolveAttachmentService(this.commandEncoder, this.frameBufferManager, tempAttachment);
2187
2299
  // temp FBO → アトラスノードへコピー
2188
2300
  this.copyTempToAtlasNode(tempAttachment, node);
2189
2301
  // temp FBOをリリース
@@ -2217,18 +2329,29 @@ export class Context {
2217
2329
  // アトラスのShape描画はFillVertexのyFlipSign=1.0によりY反転で格納されるため、
2218
2330
  // cacheAsBitmapのコピーも同じ規則に合わせてY反転する
2219
2331
  // UV.y = texCoord.y * scaleY + offsetY = texCoord.y * (-1) + 1 = 1 - texCoord.y
2220
- const uniformBuffer = this.bufferManager.acquireAndWriteUniformBuffer($atlasNodeCopyUniform);
2221
- // サンプラーとソーステクスチャ(MSAAリゾルブ済みのテクスチャ)
2222
- const sampler = this.textureManager.createSampler("container_atlas_copy", false);
2332
+ // uniformは定数のため永続バッファに一度だけ書き込み、BindGroupはsource view毎に再利用
2333
+ if (!$atlasNodeCopyUniformBuffer) {
2334
+ $atlasNodeCopyUniformBuffer = this.device.createBuffer({
2335
+ "size": $atlasNodeCopyUniform.byteLength,
2336
+ "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
2337
+ });
2338
+ this.device.queue.writeBuffer($atlasNodeCopyUniformBuffer, 0, $atlasNodeCopyUniform.buffer, $atlasNodeCopyUniform.byteOffset, $atlasNodeCopyUniform.byteLength);
2339
+ }
2223
2340
  const srcView = temp_attachment.texture.view;
2224
- const bindGroup = this.device.createBindGroup({
2225
- "layout": bindGroupLayout,
2226
- "entries": [
2227
- { "binding": 0, "resource": { "buffer": uniformBuffer } },
2228
- { "binding": 1, "resource": sampler },
2229
- { "binding": 2, "resource": srcView }
2230
- ]
2231
- });
2341
+ let bindGroup = $atlasNodeCopyBindGroups.get(srcView);
2342
+ if (!bindGroup) {
2343
+ // サンプラーとソーステクスチャ(MSAAリゾルブ済みのテクスチャ)
2344
+ const sampler = this.textureManager.createSampler("container_atlas_copy", false);
2345
+ bindGroup = this.device.createBindGroup({
2346
+ "layout": bindGroupLayout,
2347
+ "entries": [
2348
+ { "binding": 0, "resource": { "buffer": $atlasNodeCopyUniformBuffer } },
2349
+ { "binding": 1, "resource": sampler },
2350
+ { "binding": 2, "resource": srcView }
2351
+ ]
2352
+ });
2353
+ $atlasNodeCopyBindGroups.set(srcView, bindGroup);
2354
+ }
2232
2355
  // アトラスのノード領域にレンダーパスを作成
2233
2356
  const colorView = useMsaa ? atlas.msaaTexture.view : atlas.texture.view;
2234
2357
  const resolveTarget = useMsaa ? atlas.texture.view : null;
@@ -2360,8 +2483,11 @@ export class Context {
2360
2483
  "entries": $entries3
2361
2484
  });
2362
2485
  const colorView = useMsaa ? mainAttachment.msaaTexture.view : mainAttachment.texture.view;
2363
- const resolveTarget = useMsaa ? mainAttachment.texture.view : null;
2364
- const renderPassDescriptor = this.frameBufferManager.createRenderPassDescriptor(colorView, 0, 0, 0, 0, "load", resolveTarget);
2486
+ // リゾルブ遅延: 書き込みパスでは解決しない
2487
+ if (useMsaa) {
2488
+ mainAttachment.msaaDirty = true;
2489
+ }
2490
+ const renderPassDescriptor = this.frameBufferManager.createRenderPassDescriptor(colorView, 0, 0, 0, 0, "load", null);
2365
2491
  const vpX = Math.max(0, drawX);
2366
2492
  const vpY = Math.max(0, drawY);
2367
2493
  const vpW = Math.max(1, drawAttachment.width);
@@ -2513,6 +2639,7 @@ export class Context {
2513
2639
  this.pooledRenderTextures.length = 0;
2514
2640
  // Gradient LUTキャッシュのTTL超過エントリを解放
2515
2641
  $cleanupLUTCache();
2642
+ $cleanupFilterLUTCache();
2516
2643
  // Dynamic Uniform BindGroupをリセット(バッファオフセットがリセットされるため)
2517
2644
  this.fillDynamicBindGroup = null;
2518
2645
  this.fillDynamicBindGroupBuffer = null;
@@ -2631,6 +2758,8 @@ export class Context {
2631
2758
  this.endFrame();
2632
2759
  return;
2633
2760
  }
2761
+ // present はリゾルブ済みテクスチャをサンプリングするため、未リゾルブ分を解決
2762
+ this.resolveMainAttachment();
2634
2763
  // Static BindGroup キャッシュ: mainAttachment.texture.viewが同じ間は再利用
2635
2764
  const currentView = this.$mainAttachmentObject.texture.view;
2636
2765
  if (!$presentBindGroup || $presentBindGroupView !== currentView) {
@@ -2681,7 +2810,10 @@ export class Context {
2681
2810
  if (this.renderPassEncoder) {
2682
2811
  this.renderPassEncoder.end();
2683
2812
  this.renderPassEncoder = null;
2813
+ this.renderPassIsInstanced = false;
2684
2814
  }
2815
+ // リゾルブ済みテクスチャを読むため、未リゾルブ分を解決
2816
+ this.resolveMainAttachment();
2685
2817
  // GPUバッファにピクセルデータを読み込み
2686
2818
  const bytesPerPixel = 4;
2687
2819
  const bytesPerRow = Math.ceil(width * bytesPerPixel / 256) * 256; // 256バイトアライメント
@@ -2808,6 +2940,7 @@ export class Context {
2808
2940
  stencilLoadOp // 最初のマスク: クリア、ネスト: 保持
2809
2941
  );
2810
2942
  this.renderPassEncoder = this.commandEncoder.beginRenderPass(renderPassDescriptor);
2943
+ this.renderPassIsInstanced = false;
2811
2944
  // ビューポートサイズを更新
2812
2945
  this.viewportWidth = this.$mainAttachmentObject.width;
2813
2946
  this.viewportHeight = this.$mainAttachmentObject.height;
@@ -12,6 +12,53 @@ const $entries3 = [
12
12
  { "binding": 1, "resource": null },
13
13
  { "binding": 2, "resource": null }
14
14
  ];
15
+ /**
16
+ * @description copy/upscale 用の定数uniform(scale=1,1 offset=0,0)の永続バッファ
17
+ * Persistent uniform buffer for the constant copy/upscale uniform
18
+ */
19
+ let $identityUvUniformBuffer = null;
20
+ /**
21
+ * @description copy/upscale 用の source view キー BindGroup キャッシュ。
22
+ * uniform・sampler・layout が全て安定のため view のみが可変。
23
+ * BindGroup cache keyed by source view for copy/upscale passes.
24
+ */
25
+ const $identityCopyBindGroups = new WeakMap();
26
+ /**
27
+ * @description 定数uniformのコピー用BindGroupを取得(なければ生成してキャッシュ)
28
+ * Get (or lazily create) the cached copy bind group for a source view.
29
+ *
30
+ * @param {GPUDevice} device
31
+ * @param {GPUBindGroupLayout} bind_group_layout
32
+ * @param {GPUSampler} sampler
33
+ * @param {GPUTextureView} source_view
34
+ * @return {GPUBindGroup}
35
+ */
36
+ const $getIdentityCopyBindGroup = (device, bind_group_layout, sampler, source_view) => {
37
+ if (!$identityUvUniformBuffer) {
38
+ $uniform4[0] = 1;
39
+ $uniform4[1] = 1;
40
+ $uniform4[2] = 0;
41
+ $uniform4[3] = 0;
42
+ $identityUvUniformBuffer = device.createBuffer({
43
+ "size": $uniform4.byteLength,
44
+ "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
45
+ });
46
+ device.queue.writeBuffer($identityUvUniformBuffer, 0, $uniform4);
47
+ }
48
+ let bindGroup = $identityCopyBindGroups.get(source_view);
49
+ if (!bindGroup) {
50
+ bindGroup = device.createBindGroup({
51
+ "layout": bind_group_layout,
52
+ "entries": [
53
+ { "binding": 0, "resource": { "buffer": $identityUvUniformBuffer } },
54
+ { "binding": 1, "resource": sampler },
55
+ { "binding": 2, "resource": source_view }
56
+ ]
57
+ });
58
+ $identityCopyBindGroups.set(source_view, bindGroup);
59
+ }
60
+ return bindGroup;
61
+ };
15
62
  /**
16
63
  * @description ブラーフィルターを適用
17
64
  * Apply blur filter
@@ -44,7 +91,7 @@ export const execute = (source_attachment, matrix, blur_x, blur_y, quality, devi
44
91
  // サンプラーを作成(線形補間)
45
92
  const sampler = textureManager.createSampler("blur_sampler", true);
46
93
  // ソーステクスチャをattachment0にコピー(スケーリング付き)
47
- copyTextureToAttachment(device, commandEncoder, frameBufferManager, pipelineManager, source_attachment, attachment0, sampler, bufferScaleX, bufferScaleY, offsetX * bufferScaleX, offsetY * bufferScaleY, config.bufferManager);
94
+ copyTextureToAttachment(device, commandEncoder, frameBufferManager, pipelineManager, source_attachment, attachment0, sampler, bufferScaleX, bufferScaleY, offsetX * bufferScaleX, offsetY * bufferScaleY);
48
95
  // バッファスケールを考慮したブラー値
49
96
  const bufferBlurX = baseBlurX * bufferScaleX;
50
97
  const bufferBlurY = baseBlurY * bufferScaleY;
@@ -70,7 +117,7 @@ export const execute = (source_attachment, matrix, blur_x, blur_y, quality, devi
70
117
  // バッファスケールが1でない場合は元のサイズにアップスケール
71
118
  if (bufferScaleX !== 1 || bufferScaleY !== 1) {
72
119
  const finalAttachment = frameBufferManager.createTemporaryAttachment(width, height);
73
- upscaleTexture(device, commandEncoder, frameBufferManager, pipelineManager, resultAttachment, finalAttachment, sampler, config.bufferManager);
120
+ upscaleTexture(device, commandEncoder, frameBufferManager, pipelineManager, resultAttachment, finalAttachment, sampler);
74
121
  // ピンポンバッファを解放
75
122
  frameBufferManager.releaseTemporaryAttachment(attachment0);
76
123
  frameBufferManager.releaseTemporaryAttachment(attachment1);
@@ -98,10 +145,9 @@ export const execute = (source_attachment, matrix, blur_x, blur_y, quality, devi
98
145
  * @param {number} buffer_scale_y - Y方向のバッファスケール
99
146
  * @param {number} pixel_offset_x - デスト内でのX方向オフセット(ピクセル単位、スケーリング済み)
100
147
  * @param {number} pixel_offset_y - デスト内でのY方向オフセット(ピクセル単位、スケーリング済み)
101
- * @param {IFilterConfig["bufferManager"]} [buffer_manager] - バッファマネージャー
102
148
  * @return {void}
103
149
  */
104
- const copyTextureToAttachment = (device, command_encoder, frame_buffer_manager, pipeline_manager, source, dest, sampler, buffer_scale_x, buffer_scale_y, pixel_offset_x, pixel_offset_y, buffer_manager) => {
150
+ const copyTextureToAttachment = (device, command_encoder, frame_buffer_manager, pipeline_manager, source, dest, sampler, buffer_scale_x, buffer_scale_y, pixel_offset_x, pixel_offset_y) => {
105
151
  // texture_copy_rgba8を使用し、ビューポートでオフセットを制御
106
152
  const pipeline = pipeline_manager.getPipeline("texture_copy_rgba8");
107
153
  const bindGroupLayout = pipeline_manager.getBindGroupLayout("texture_copy");
@@ -113,32 +159,9 @@ const copyTextureToAttachment = (device, command_encoder, frame_buffer_manager,
113
159
  const scaledSourceWidth = source.width * buffer_scale_x;
114
160
  const scaledSourceHeight = source.height * buffer_scale_y;
115
161
  // シェーダー: uv = texCoord * scale + offset
116
- // ソース全体をサンプリングするので scale = 1, offset = 0
117
- const scaleX = 1;
118
- const scaleY = 1;
119
- const offsetX = 0;
120
- const offsetY = 0;
121
- // ユニフォームバッファ: scale(2) + offset(2)
122
- $uniform4[0] = scaleX;
123
- $uniform4[1] = scaleY;
124
- $uniform4[2] = offsetX;
125
- $uniform4[3] = offsetY;
126
- const uniformBuffer = buffer_manager
127
- ? buffer_manager.acquireAndWriteUniformBuffer($uniform4)
128
- : device.createBuffer({
129
- "size": $uniform4.byteLength,
130
- "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
131
- });
132
- if (!buffer_manager) {
133
- device.queue.writeBuffer(uniformBuffer, 0, $uniform4);
134
- }
135
- $entries3[0].resource.buffer = uniformBuffer;
136
- $entries3[1].resource = sampler;
137
- $entries3[2].resource = source.texture.view;
138
- const bindGroup = device.createBindGroup({
139
- "layout": bindGroupLayout,
140
- "entries": $entries3
141
- });
162
+ // ソース全体をサンプリングするので scale = 1, offset = 0 (定数)
163
+ // uniformは永続バッファ、BindGroupはsource viewキーで再利用
164
+ const bindGroup = $getIdentityCopyBindGroup(device, bindGroupLayout, sampler, source.texture.view);
142
165
  const renderPassDescriptor = frame_buffer_manager.createRenderPassDescriptor(dest.texture.view, 0, 0, 0, 0, "clear");
143
166
  const passEncoder = command_encoder.beginRenderPass(renderPassDescriptor);
144
167
  passEncoder.setPipeline(pipeline);
@@ -148,7 +171,6 @@ const copyTextureToAttachment = (device, command_encoder, frame_buffer_manager,
148
171
  passEncoder.setScissorRect(Math.floor(pixel_offset_x), Math.floor(pixel_offset_y), Math.ceil(scaledSourceWidth), Math.ceil(scaledSourceHeight));
149
172
  passEncoder.draw(6, 1, 0, 0);
150
173
  passEncoder.end();
151
- // Note: uniformBuffer is not destroyed here - it will be garbage collected after GPU submission
152
174
  };
153
175
  /**
154
176
  * @description 方向ブラーを適用
@@ -217,10 +239,9 @@ const applyDirectionalBlur = (device, command_encoder, frame_buffer_manager, pip
217
239
  * @param {IAttachmentObject} source - ソーステクスチャ
218
240
  * @param {IAttachmentObject} dest - デストテクスチャ
219
241
  * @param {GPUSampler} sampler - サンプラー
220
- * @param {IFilterConfig["bufferManager"]} [buffer_manager] - バッファマネージャー
221
242
  * @return {void}
222
243
  */
223
- const upscaleTexture = (device, command_encoder, frame_buffer_manager, pipeline_manager, source, dest, sampler, buffer_manager) => {
244
+ const upscaleTexture = (device, command_encoder, frame_buffer_manager, pipeline_manager, source, dest, sampler) => {
224
245
  // temp_アタッチメントはrgba8unormフォーマットなので、texture_copy_rgba8パイプラインを使用
225
246
  const pipeline = pipeline_manager.getPipeline("texture_copy_rgba8");
226
247
  const bindGroupLayout = pipeline_manager.getBindGroupLayout("texture_copy");
@@ -231,26 +252,8 @@ const upscaleTexture = (device, command_encoder, frame_buffer_manager, pipeline_
231
252
  // アップスケールではソース全体をデスト全体にマッピング
232
253
  // シェーダー: uv = (texCoord - offset) * scale
233
254
  // scale = 1, offset = 0 で uv = texCoord となり、ソース全体がデスト全体にマッピングされる
234
- $uniform4[0] = 1;
235
- $uniform4[1] = 1;
236
- $uniform4[2] = 0;
237
- $uniform4[3] = 0;
238
- const uniformBuffer = buffer_manager
239
- ? buffer_manager.acquireAndWriteUniformBuffer($uniform4)
240
- : device.createBuffer({
241
- "size": $uniform4.byteLength,
242
- "usage": GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
243
- });
244
- if (!buffer_manager) {
245
- device.queue.writeBuffer(uniformBuffer, 0, $uniform4);
246
- }
247
- $entries3[0].resource.buffer = uniformBuffer;
248
- $entries3[1].resource = sampler;
249
- $entries3[2].resource = source.texture.view;
250
- const bindGroup = device.createBindGroup({
251
- "layout": bindGroupLayout,
252
- "entries": $entries3
253
- });
255
+ // uniformは永続バッファ、BindGroupはsource viewキーで再利用
256
+ const bindGroup = $getIdentityCopyBindGroup(device, bindGroupLayout, sampler, source.texture.view);
254
257
  const renderPassDescriptor = frame_buffer_manager.createRenderPassDescriptor(dest.texture.view, 0, 0, 0, 0, "clear");
255
258
  const passEncoder = command_encoder.beginRenderPass(renderPassDescriptor);
256
259
  passEncoder.setPipeline(pipeline);
@@ -27,3 +27,57 @@ export declare const $getFilterGradientAttachmentObject: () => IAttachmentObject
27
27
  * @protected
28
28
  */
29
29
  export declare const $clearFilterGradientAttachment: () => void;
30
+ /**
31
+ * @description フィルターLUTキャッシュエントリ
32
+ * Filter LUT cache entry
33
+ */
34
+ interface IFilterGradientLUTEntry {
35
+ texture: GPUTexture;
36
+ view: GPUTextureView;
37
+ lastUsedFrame: number;
38
+ }
39
+ /**
40
+ * @description キャッシュからフィルターLUTを取得。ヒットしなければ null。
41
+ * Retrieve a filter LUT from the cache. Returns null on miss.
42
+ *
43
+ * @param {Float32Array | number[]} ratios
44
+ * @param {Float32Array | number[]} colors
45
+ * @param {Float32Array | number[]} alphas
46
+ * @return {IFilterGradientLUTEntry | null}
47
+ * @method
48
+ * @protected
49
+ */
50
+ export declare const $getFilterLUTFromCache: (ratios: Float32Array | number[], colors: Float32Array | number[], alphas: Float32Array | number[]) => IFilterGradientLUTEntry | null;
51
+ /**
52
+ * @description フィルターLUTをキャッシュに格納
53
+ * Store a filter LUT into the cache
54
+ *
55
+ * @param {Float32Array | number[]} ratios
56
+ * @param {Float32Array | number[]} colors
57
+ * @param {Float32Array | number[]} alphas
58
+ * @param {GPUTexture} texture
59
+ * @param {GPUTextureView} view
60
+ * @return {IFilterGradientLUTEntry}
61
+ * @method
62
+ * @protected
63
+ */
64
+ export declare const $putFilterLUTToCache: (ratios: Float32Array | number[], colors: Float32Array | number[], alphas: Float32Array | number[], texture: GPUTexture, view: GPUTextureView) => IFilterGradientLUTEntry;
65
+ /**
66
+ * @description フレーム終了時にTTL超過エントリを解放
67
+ * Release entries that exceed the TTL at the end of each frame
68
+ *
69
+ * @return {void}
70
+ * @method
71
+ * @protected
72
+ */
73
+ export declare const $cleanupFilterLUTCache: () => void;
74
+ /**
75
+ * @description 全フィルターLUTキャッシュを破棄
76
+ * Destroy and clear the entire filter LUT cache
77
+ *
78
+ * @return {void}
79
+ * @method
80
+ * @protected
81
+ */
82
+ export declare const $clearFilterLUTCache: () => void;
83
+ export {};