@next2d/webgpu 3.12.0 → 3.13.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/webgpu",
3
- "version": "3.12.0",
3
+ "version": "3.13.0",
4
4
  "description": "Next2D WebGPU Package",
5
5
  "author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
6
6
  "license": "MIT",
@@ -24,7 +24,7 @@
24
24
  "url": "git+https://github.com/Next2D/Player.git"
25
25
  },
26
26
  "dependencies": {
27
- "@next2d/texture-packer": "3.12.0",
28
- "@next2d/render-queue": "3.12.0"
27
+ "@next2d/texture-packer": "3.13.0",
28
+ "@next2d/render-queue": "3.13.0"
29
29
  }
30
30
  }
@@ -1,3 +1,4 @@
1
+ import { resetAtlasScratch } from "./AtlasScratch";
1
2
  /**
2
3
  * @description テクスチャアトラス境界の初期最大値
3
4
  * Initial maximum value for texture atlas bounds
@@ -171,6 +172,7 @@ export const $getCurrentAtlasIndex = () => {
171
172
  * @return {void}
172
173
  */
173
174
  export const $resetAtlas = () => {
175
+ resetAtlasScratch();
174
176
  $rootNodes.length = 0;
175
177
  $setActiveAtlasIndex(0);
176
178
  for (let idx = 0; idx < $atlasAttachmentObjects.length; idx++) {
@@ -0,0 +1,14 @@
1
+ import type { IAttachmentObject } from "./interface/IAttachmentObject";
2
+ interface IAtlasScratch {
3
+ color: GPUTexture;
4
+ stencil: GPUTexture;
5
+ owner: GPUTexture | null;
6
+ pipeline: GPURenderPipeline;
7
+ }
8
+ /** Resolved cache pages stay resident; multisample working storage is shared. */
9
+ export declare function atlasScratch(device: GPUDevice, width: number, height: number): IAtlasScratch;
10
+ export declare function needsAtlasRestore(device: GPUDevice, attachment: IAttachmentObject): boolean;
11
+ export declare function restoreAtlas(device: GPUDevice, encoder: GPUCommandEncoder, attachment: IAttachmentObject): void;
12
+ /** AtlasManager destroys the shared resources along with the page attachments. */
13
+ export declare function resetAtlasScratch(): void;
14
+ export {};
@@ -0,0 +1,60 @@
1
+ import { $samples } from "./WebGPUUtil";
2
+ let pools = new WeakMap();
3
+ /** Resolved cache pages stay resident; multisample working storage is shared. */
4
+ export function atlasScratch(device, width, height) {
5
+ let pool = pools.get(device);
6
+ if (!pool) {
7
+ pool = new Map();
8
+ pools.set(device, pool);
9
+ }
10
+ const key = `${width}:${height}`;
11
+ const cached = pool.get(key);
12
+ if (cached) {
13
+ return cached;
14
+ }
15
+ const shader = device.createShaderModule({ "code": `
16
+ @group(0) @binding(0) var image: texture_2d<f32>;
17
+ @vertex fn vertex(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f {
18
+ let points = array<vec2f, 3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3));
19
+ return vec4f(points[i],0,1);
20
+ }
21
+ @fragment fn fragment(@builtin(position) p: vec4f) -> @location(0) vec4f {
22
+ return textureLoad(image,vec2i(p.xy),0);
23
+ }` });
24
+ const value = {
25
+ "color": device.createTexture({ "size": { width, height }, "format": "rgba8unorm",
26
+ "sampleCount": $samples, "usage": GPUTextureUsage.RENDER_ATTACHMENT }),
27
+ "stencil": device.createTexture({ "size": { width, height }, "format": "stencil8",
28
+ "sampleCount": $samples, "usage": GPUTextureUsage.RENDER_ATTACHMENT }),
29
+ "owner": null,
30
+ "pipeline": device.createRenderPipeline({ "layout": "auto",
31
+ "vertex": { "module": shader, "entryPoint": "vertex" },
32
+ "fragment": { "module": shader, "entryPoint": "fragment", "targets": [{ "format": "rgba8unorm" }] },
33
+ "multisample": { "count": $samples } })
34
+ };
35
+ pool.set(key, value);
36
+ return value;
37
+ }
38
+ export function needsAtlasRestore(device, attachment) {
39
+ const scratch = pools.get(device)?.get(`${attachment.width}:${attachment.height}`);
40
+ return !!scratch && attachment.msaaTexture?.resource === scratch.color
41
+ && scratch.owner !== attachment.texture?.resource;
42
+ }
43
+ export function restoreAtlas(device, encoder, attachment) {
44
+ const scratch = atlasScratch(device, attachment.width, attachment.height);
45
+ if (!attachment.texture || !attachment.msaaTexture) {
46
+ return;
47
+ }
48
+ const pass = encoder.beginRenderPass({ "colorAttachments": [{
49
+ "view": attachment.msaaTexture.view, "loadOp": "clear", "storeOp": "store",
50
+ "clearValue": { "r": 0, "g": 0, "b": 0, "a": 0 }
51
+ }] });
52
+ pass.setPipeline(scratch.pipeline);
53
+ pass.setBindGroup(0, device.createBindGroup({ "layout": scratch.pipeline.getBindGroupLayout(0),
54
+ "entries": [{ "binding": 0, "resource": attachment.texture.view }] }));
55
+ pass.draw(3);
56
+ pass.end();
57
+ scratch.owner = attachment.texture.resource;
58
+ }
59
+ /** AtlasManager destroys the shared resources along with the page attachments. */
60
+ export function resetAtlasScratch() { pools = new WeakMap(); }
package/src/Context.d.ts CHANGED
@@ -488,6 +488,14 @@ export declare class Context {
488
488
  * @return {void}
489
489
  */
490
490
  useGrid(grid_data: Float32Array | null): void;
491
+ /**
492
+ * @description 共有MSAAスクラッチが別ページの内容を保持している場合、対象アトラスの解決済みテクスチャから復元
493
+ * Restore the shared MSAA scratch from the target atlas when it holds another page
494
+ *
495
+ * @param {IAttachmentObject} attachment - 描画対象のアトラス / Target atlas attachment
496
+ * @return {void}
497
+ */
498
+ private prepareAtlasScratch;
491
499
  /**
492
500
  * @description 指定のノード範囲で描画を開始(アトラステクスチャへの描画)
493
501
  * Begin rendering for the specified node region (drawing to atlas texture)
package/src/Context.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { needsAtlasRestore, restoreAtlas } from "./AtlasScratch";
1
2
  import { TexturePacker } from "@next2d/texture-packer";
2
3
  import { $cacheStore } from "@next2d/cache";
3
4
  import { WebGPUUtil, $setContext } from "./WebGPUUtil";
@@ -1055,6 +1056,10 @@ export class Context {
1055
1056
  // 現在のレンダーターゲットを取得(メインまたはオフスクリーン)
1056
1057
  const textureView = this.getCurrentTextureView();
1057
1058
  const attachment = $getAtlasAttachmentObject();
1059
+ // アトラスへ描画する場合は共有MSAAスクラッチを対象ページの内容に揃える
1060
+ if (this.currentRenderTarget && attachment) {
1061
+ this.prepareAtlasScratch(attachment);
1062
+ }
1058
1063
  // MSAAテクスチャを使用するかどうか
1059
1064
  const useMsaa = attachment?.msaa && attachment?.msaaTexture?.view;
1060
1065
  const colorView = useMsaa ? attachment.msaaTexture.view : textureView;
@@ -1580,6 +1585,25 @@ export class Context {
1580
1585
  useGrid(grid_data) {
1581
1586
  $gridDataMap.set($fillBufferIndex, grid_data);
1582
1587
  }
1588
+ /**
1589
+ * @description 共有MSAAスクラッチが別ページの内容を保持している場合、対象アトラスの解決済みテクスチャから復元
1590
+ * Restore the shared MSAA scratch from the target atlas when it holds another page
1591
+ *
1592
+ * @param {IAttachmentObject} attachment - 描画対象のアトラス / Target atlas attachment
1593
+ * @return {void}
1594
+ */
1595
+ prepareAtlasScratch(attachment) {
1596
+ if (!needsAtlasRestore(this.device, attachment)) {
1597
+ return;
1598
+ }
1599
+ if (this.renderPassEncoder) {
1600
+ this.renderPassEncoder.end();
1601
+ this.renderPassEncoder = null;
1602
+ }
1603
+ this.nodeRenderPassAtlasIndex = -1;
1604
+ this.ensureCommandEncoder();
1605
+ restoreAtlas(this.device, this.commandEncoder, attachment);
1606
+ }
1583
1607
  /**
1584
1608
  * @description 指定のノード範囲で描画を開始(アトラステクスチャへの描画)
1585
1609
  * Begin rendering for the specified node region (drawing to atlas texture)
@@ -1598,6 +1622,7 @@ export class Context {
1598
1622
  // アトラステクスチャの該当箇所をレンダーターゲットに設定
1599
1623
  const attachment = $getAtlasAttachmentObjectByIndex(node.index) || $getAtlasAttachmentObject();
1600
1624
  if (attachment && attachment.texture) {
1625
+ this.prepareAtlasScratch(attachment);
1601
1626
  // 同一アトラスへの連続描画ならレンダーパスを再利用
1602
1627
  if (this.renderPassEncoder && this.nodeRenderPassAtlasIndex === node.index) {
1603
1628
  // レンダーパスを再利用 — シザーレクトのみ更新
@@ -1937,6 +1962,7 @@ export class Context {
1937
1962
  * @return {void}
1938
1963
  */
1939
1964
  drawPixelsToMsaa(attachment, node, pixels, width, height) {
1965
+ this.prepareAtlasScratch(attachment);
1940
1966
  // 一時テクスチャをプールから取得
1941
1967
  const tempTexture = $acquireRenderTexture(this.device, width, height);
1942
1968
  // ピクセルデータを一時テクスチャに書き込む
@@ -2066,6 +2092,7 @@ export class Context {
2066
2092
  }
2067
2093
  /** Draw to the MSAA atlas through a frame-owned temporary image texture. */
2068
2094
  drawElementToMsaa(attachment, node, element, width, height, flip_y) {
2095
+ this.prepareAtlasScratch(attachment);
2069
2096
  // 一時テクスチャをプールから取得
2070
2097
  const tempTexture = $acquireRenderTexture(this.device, width, height);
2071
2098
  const pipeline = this.pipelineManager.getPipeline("bitmap_render_msaa");
@@ -2361,6 +2388,7 @@ export class Context {
2361
2388
  if (!atlas || !atlas.texture || !temp_attachment.texture) {
2362
2389
  return;
2363
2390
  }
2391
+ this.prepareAtlasScratch(atlas);
2364
2392
  // アトラスはrgba8unormフォーマット(FrameBufferManagerCreateAttachmentUseCase参照)
2365
2393
  // atlas_*テクスチャはcopyExternalImageToTextureとの互換性のためrgba8unormで作成される
2366
2394
  const useMsaa = atlas.msaa && atlas.msaaTexture?.view;
@@ -2758,19 +2786,32 @@ export class Context {
2758
2786
  * @return {Node} 作成されたノード / Created node
2759
2787
  */
2760
2788
  createNode(width, height) {
2761
- // WebGPU node creation implementation using texture-packer
2762
- const index = $getActiveAtlasIndex();
2763
- if (!$rootNodes[index]) {
2764
- const maxSize = WebGPUUtil.getRenderMaxSize();
2765
- $rootNodes[index] = new TexturePacker(index, maxSize, maxSize);
2789
+ const maxSize = WebGPUUtil.getRenderMaxSize();
2790
+ if (!Number.isFinite(width) || !Number.isFinite(height)
2791
+ || width > maxSize || height > maxSize) {
2792
+ throw new RangeError("Invalid texture atlas allocation size");
2793
+ }
2794
+ // サイズ0の要素でも描画処理を止めないよう最小1pxで確保
2795
+ width = Math.max(1, width);
2796
+ height = Math.max(1, height);
2797
+ const count = $rootNodes.length;
2798
+ const start = count ? $getActiveAtlasIndex() % count : 0;
2799
+ // Search all existing pages before growing; freed earlier pages remain reusable.
2800
+ for (let offset = 0; offset < count; offset++) {
2801
+ const index = (start + offset) % count;
2802
+ const root = $rootNodes[index];
2803
+ const reused = root?.insert(width, height);
2804
+ if (!reused) {
2805
+ continue;
2806
+ }
2807
+ $setActiveAtlasIndex(index);
2808
+ this._nodeRoots.set(reused, root);
2809
+ return reused;
2766
2810
  }
2767
- const rootNode = $rootNodes[index];
2811
+ const rootNode = new TexturePacker(count, maxSize, maxSize);
2812
+ $rootNodes[count] = rootNode;
2813
+ $setActiveAtlasIndex(count);
2768
2814
  const node = rootNode.insert(width, height);
2769
- if (!node) {
2770
- // アトラスがいっぱいの場合、新しいアトラスインデックスに切り替えて再試行
2771
- $setActiveAtlasIndex(index + 1);
2772
- return this.createNode(width, height);
2773
- }
2774
2815
  this._nodeRoots.set(node, rootNode);
2775
2816
  return node;
2776
2817
  }
@@ -1,4 +1,5 @@
1
1
  import { $samples } from "../../WebGPUUtil";
2
+ import { atlasScratch } from "../../AtlasScratch";
2
3
  /**
3
4
  * @description アタッチメントオブジェクトを作成
4
5
  * Create attachment object
@@ -49,7 +50,7 @@ export const execute = (device, format, attachments, name, width, height, msaa,
49
50
  // MSAAテクスチャを作成(sampleCount > 1の場合)
50
51
  let msaaTexture = null;
51
52
  if (useMsaa) {
52
- const msaaGpuTexture = device.createTexture({
53
+ const msaaGpuTexture = isAtlas ? atlasScratch(device, width, height).color : device.createTexture({
53
54
  "size": { width, height },
54
55
  "format": textureFormat,
55
56
  "sampleCount": sampleCount,
@@ -90,7 +91,7 @@ export const execute = (device, format, attachments, name, width, height, msaa,
90
91
  };
91
92
  // MSAAステンシルテクスチャを作成(sampleCount > 1の場合)
92
93
  if (useMsaa) {
93
- const msaaStencilTexture = device.createTexture({
94
+ const msaaStencilTexture = isAtlas ? atlasScratch(device, width, height).stencil : device.createTexture({
94
95
  "size": { width, height },
95
96
  "format": "stencil8",
96
97
  "sampleCount": sampleCount,
@@ -155,6 +155,11 @@ export class FrameBufferManager {
155
155
  if (attachment.stencil) {
156
156
  attachment.stencil.resource.destroy();
157
157
  }
158
+ // Atlas scratch is shared and released by AtlasManager as a group.
159
+ if (name !== "atlas" && !name.startsWith("atlas_")) {
160
+ attachment.msaaTexture?.resource.destroy();
161
+ attachment.msaaStencil?.resource.destroy();
162
+ }
158
163
  this.attachments.delete(name);
159
164
  }
160
165
  }