Przeglądaj źródła

Merge pull request #6258 from Kangz/WebGPU

Updated WebGPU Engine and d.ts to match latest Chromium snapshot
sebavan 6 lat temu
rodzic
commit
665c11ea32
2 zmienionych plików z 28 dodań i 52 usunięć
  1. 14 29
      src/Engines/webgpuEngine.ts
  2. 14 23
      src/LibDeclarations/webgpu.d.ts

+ 14 - 29
src/Engines/webgpuEngine.ts

@@ -252,9 +252,9 @@ export class WebGPUEngine extends Engine {
     }
 
     private _initializeContextAndSwapChain(): void {
-        this._context = this._canvas.getContext('gpupresent') as GPUCanvasContext;
-        this._swapChain = this._device.createSwapChain({
-            context: this._context,
+        this._context = this._canvas.getContext('gpupresent') as unknown as GPUCanvasContext;
+        this._swapChain = this._context.configureSwapChain({
+            device: this._device,
             format: this._options.swapChainFormat,
         });
     }
@@ -436,24 +436,14 @@ export class WebGPUEngine extends Engine {
         }
 
         // Chunk
-        // After Migration to Canary
-        // const maxChunk = 1024 * 1024 * 16;
-        const maxChunk = 1024 * 1024;
+        const maxChunk = 1024 * 1024 * 15;
         let offset = 0;
         while ((chunkEnd - (chunkStart + offset)) > maxChunk) {
-            const tempView = new Uint8Array(src.buffer.slice(chunkStart + offset, chunkStart + offset + maxChunk));
-            buffer.setSubData(dstByteOffset + offset, tempView.buffer);
+            buffer.setSubData(dstByteOffset + offset, src, srcByteOffset + offset, maxChunk);
             offset += maxChunk;
 
-            // After Migration to Canary
-            // buffer.setSubData(dstByteOffset + offset, src, srcByteOffset + offset, maxChunk);
         }
-
-        const tempView = new Uint8Array(src.buffer.slice(chunkStart + offset, chunkEnd));
-        buffer.setSubData(dstByteOffset + offset, tempView.buffer);
-
-        // After Migration to Canary
-        // buffer.setSubData(dstByteOffset + offset, src, srcByteOffset + offset, byteLength - offset);
+        buffer.setSubData(dstByteOffset + offset, src, srcByteOffset + offset, byteLength - offset);
     }
 
     //------------------------------------------------------------------------------
@@ -688,19 +678,16 @@ export class WebGPUEngine extends Engine {
     }
 
     private _createPipelineStageDescriptor(vertexShader: Uint32Array, fragmentShader: Uint32Array): GPURenderPipelineStageDescriptor {
-        // After Migration to Canary
         return {
             vertexStage: {
                 module: this._device.createShaderModule({
-                    // code: vertexShader,
-                    code: vertexShader.buffer,
+                    code: vertexShader,
                 }),
                 entryPoint: "main",
             },
             fragmentStage: {
                 module: this._device.createShaderModule({
-                    // code: fragmentShader,
-                    code: fragmentShader.buffer,
+                    code: fragmentShader,
                 }),
                 entryPoint: "main"
             }
@@ -780,12 +767,12 @@ export class WebGPUEngine extends Engine {
             const location = matches[1];
             const name = matches[2];
 
-            const attributeIndex = attributesNames.indexOf(name);
-            if (attributeIndex === -1) {
+            const shaderLocation = attributesNames.indexOf(name);
+            if (shaderLocation === -1) {
                 continue;
             }
 
-            results[attributeIndex] = +location;
+            results[shaderLocation] = +location;
         }
 
         return results;
@@ -1081,7 +1068,8 @@ export class WebGPUEngine extends Engine {
                 format: "rgba8unorm",
                 mipLevelCount: noMipmap ? 1 : mipMaps + 1,
                 baseArrayLayer: 0,
-                baseMipLevel: 0
+                baseMipLevel: 0,
+                aspect: "all",
             });
             webglEngineTexture.dispose();
         };
@@ -1530,10 +1518,7 @@ export class WebGPUEngine extends Engine {
                 }
 
                 const positionAttributeDescriptor: GPUVertexAttributeDescriptor = {
-                    attributeIndex: location,
-
-                    // After Migration to Canary
-                    // shaderLocation: location,
+                    shaderLocation: location,
                     offset: 0, // not available in WebGL
                     format: this._getVertexInputDescriptorFormat(vertexBuffer.getKind(), vertexBuffer.type, vertexBuffer.normalized),
                 };

+ 14 - 23
src/LibDeclarations/webgpu.d.ts

@@ -153,7 +153,12 @@ type GPUTextureViewDimension =
   | "cube"
   | "cube-array"
   | "3d";
-  
+
+type GPUTextureAspect =
+ | "all"
+ | "stencil-only"
+ | "depth-only";
+
 type GPUVertexFormat =
   | "uchar"
   | "uchar2"
@@ -447,9 +452,7 @@ interface GPUSamplerDescriptor {
 }
 
 interface GPUShaderModuleDescriptor {
-  // After Migration to Canary
-  // code: Uint32Array;
-  code: ArrayBuffer | string;
+  code: Uint32Array;
   label?: string;
 }
 
@@ -461,7 +464,7 @@ interface GPUStencilStateFaceDescriptor {
 }
 
 interface GPUSwapChainDescriptor {
-  context?: GPUCanvasContext ;
+  device?: GPUDevice;
   format?: GPUTextureFormat;
   usage?: GPUTextureUsageFlags;
 }
@@ -484,7 +487,7 @@ interface GPUTextureDescriptor {
 }
 
 interface GPUTextureViewDescriptor {
-  aspect?: GPUTextureAspectFlags;
+  aspect?: GPUTextureAspect;
   baseArrayLayer?: number;
   baseMipLevel?: number;
   dimension?: GPUTextureViewDimension;
@@ -496,9 +499,7 @@ interface GPUTextureViewDescriptor {
 interface GPUVertexAttributeDescriptor {
   format?: GPUVertexFormat;
   offset?: number;
-  attributeIndex?: number;
-  // After Migration to Canary
-  // shaderLocation?: number;
+  shaderLocation?: number;
 }
 
 interface GPUVertexInputDescriptor {
@@ -527,13 +528,7 @@ interface GPUBuffer extends GPUDebugLabel {
 
   mapWriteAsync(): Promise<ArrayBuffer>;
   mapReadAsync(): Promise<ArrayBuffer>;
-  setSubData(offset: number, ab: ArrayBuffer): void;
-  
-  // After Migration to Canary
-  // PR #261
-  // If `byteLength` is 0, the ArrayBufferView is copied to the end.
-  // That is, `byteLength` "defaults" to `src.byteLength - srcByteOffset`.
-  // setSubData(dstByteOffset: number, src: ArrayBufferView, srcByteOffset = 0, byteLength = 0): void;
+  setSubData(dstByteOffset: number, src: ArrayBufferView, srcByteOffset = 0, byteLength = 0): void;
 }
 
 interface GPUCommandEncoder extends GPUDebugLabel {
@@ -563,6 +558,8 @@ interface GPUDebugLabel {
 
 // SwapChain / CanvasContext
 interface GPUCanvasContext {
+  configureSwapChain(descriptor: GPUSwapChainDescriptor): GPUSwapChain;
+  getSwapChainPreferredFormat(context: GPUCanvasContext): Promise<GPUTextureFormat> ;
 }
 
 interface GPUDevice {
@@ -584,12 +581,6 @@ interface GPUDevice {
   createTexture(descriptor: GPUTextureDescriptor): GPUTexture;
 
   getQueue(): GPUQueue;
-
-  // Calling createSwapChain a second time for the same GPUCanvasContext
-  // invalidates the previous one, and all of the textures it’s produced.
-  createSwapChain(descriptor: GPUSwapChainDescriptor): GPUSwapChain;
-
-  getSwapChainPreferredFormat(context: GPUCanvasContext): Promise<GPUTextureFormat> ;
 }
 
 interface GPUFence extends GPUDebugLabel {
@@ -712,4 +703,4 @@ interface GPU {
 
 interface Navigator {
     readonly gpu: GPU | undefined;
-}
+}