@next2d/webgpu 3.0.2 → 3.0.4

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 (2) hide show
  1. package/package.json +3 -3
  2. package/src/Context.js +29 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/webgpu",
3
- "version": "3.0.2",
3
+ "version": "3.0.4",
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.0.2",
28
- "@next2d/render-queue": "3.0.2"
27
+ "@next2d/texture-packer": "3.0.4",
28
+ "@next2d/render-queue": "3.0.4"
29
29
  }
30
30
  }
package/src/Context.js CHANGED
@@ -2227,10 +2227,11 @@ export class Context {
2227
2227
  * @description ImageBitmapを生成
2228
2228
  */
2229
2229
  async createImageBitmap(width, height) {
2230
- // アトラステクスチャから現在の描画内容を取得
2231
- const attachment = $getAtlasAttachmentObject();
2232
- if (!attachment) {
2233
- throw new Error("[WebGPU] Atlas attachment not found");
2230
+ // メインアタッチメントから合成済み描画結果を取得
2231
+ // (drawArraysInstanced()がアトラスからメインアタッチメントへ合成済み)
2232
+ const mainAttachment = this.$mainAttachmentObject;
2233
+ if (!mainAttachment || !mainAttachment.texture) {
2234
+ throw new Error("[WebGPU] Main attachment not found");
2234
2235
  }
2235
2236
  // 描画を完了
2236
2237
  if (this.renderPassEncoder) {
@@ -2246,14 +2247,11 @@ export class Context {
2246
2247
  "size": bufferSize,
2247
2248
  "usage": GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
2248
2249
  });
2249
- // コマンドエンコーダーを作成
2250
- const commandEncoder = this.device.createCommandEncoder();
2251
- // アトラステクスチャからピクセルバッファにコピー
2252
- if (!attachment.texture) {
2253
- throw new Error("Attachment texture is null");
2254
- }
2255
- commandEncoder.copyTextureToBuffer({
2256
- "texture": attachment.texture.resource,
2250
+ // フレームのcommandEncoderにcopyコマンドを追加
2251
+ // 描画コマンドの後にコピーが実行されるため、正しい順序が保証される
2252
+ this.ensureCommandEncoder();
2253
+ this.commandEncoder.copyTextureToBuffer({
2254
+ "texture": mainAttachment.texture.resource,
2257
2255
  "mipLevel": 0,
2258
2256
  "origin": { "x": 0, "y": 0, "z": 0 }
2259
2257
  }, {
@@ -2265,18 +2263,32 @@ export class Context {
2265
2263
  "height": height,
2266
2264
  "depthOrArrayLayers": 1
2267
2265
  });
2268
- // コマンドを送信
2269
- this.device.queue.submit([commandEncoder.finish()]);
2266
+ // endFrame()で描画コマンドとcopyコマンドを一括submit
2267
+ // swap chain textureの参照もクリアされる
2268
+ this.endFrame();
2270
2269
  // バッファをマップして読み込み
2271
2270
  await pixelBuffer.mapAsync(GPUMapMode.READ);
2272
2271
  const mappedRange = pixelBuffer.getMappedRange();
2273
2272
  const pixels = new Uint8Array(mappedRange);
2274
- // ピクセルデータをコピー(アライメントを考慮)
2273
+ // メインアタッチメントはbgra8unormフォーマットのため、
2274
+ // ImageData(RGBA)用にB⇔Rチャンネルをスワップしつつコピー
2275
+ const isBgra = this.preferredFormat === "bgra8unorm";
2275
2276
  const resultPixels = new Uint8Array(width * height * 4);
2277
+ const rowBytes = width * 4;
2276
2278
  for (let y = 0; y < height; y++) {
2277
2279
  const srcOffset = y * bytesPerRow;
2278
- const dstOffset = y * width * 4;
2279
- resultPixels.set(pixels.subarray(srcOffset, srcOffset + width * 4), dstOffset);
2280
+ const dstOffset = y * rowBytes;
2281
+ if (isBgra) {
2282
+ for (let x = 0; x < rowBytes; x += 4) {
2283
+ resultPixels[dstOffset + x] = pixels[srcOffset + x + 2]; // R ← B
2284
+ resultPixels[dstOffset + x + 1] = pixels[srcOffset + x + 1]; // G
2285
+ resultPixels[dstOffset + x + 2] = pixels[srcOffset + x]; // B ← R
2286
+ resultPixels[dstOffset + x + 3] = pixels[srcOffset + x + 3]; // A
2287
+ }
2288
+ }
2289
+ else {
2290
+ resultPixels.set(pixels.subarray(srcOffset, srcOffset + rowBytes), dstOffset);
2291
+ }
2280
2292
  }
2281
2293
  pixelBuffer.unmap();
2282
2294
  pixelBuffer.destroy();