Title: [291377] trunk/Source/WebGPU
Revision
291377
Author
[email protected]
Date
2022-03-16 17:05:30 -0700 (Wed, 16 Mar 2022)

Log Message

[WebGPU] Put nontrivial code in WebGPU's command line playground
https://bugs.webkit.org/show_bug.cgi?id=237876

Reviewed by Kimmo Kinnunen.

This is a simple mempcy() example. It uses asynchronous mapping and a queue submit.

* CommandLinePlayground/main.swift:
(device):

Modified Paths

Diff

Modified: trunk/Source/WebGPU/ChangeLog (291376 => 291377)


--- trunk/Source/WebGPU/ChangeLog	2022-03-17 00:02:29 UTC (rev 291376)
+++ trunk/Source/WebGPU/ChangeLog	2022-03-17 00:05:30 UTC (rev 291377)
@@ -1,5 +1,17 @@
 2022-03-16  Myles C. Maxfield  <[email protected]>
 
+        [WebGPU] Put nontrivial code in WebGPU's command line playground
+        https://bugs.webkit.org/show_bug.cgi?id=237876
+
+        Reviewed by Kimmo Kinnunen.
+
+        This is a simple mempcy() example. It uses asynchronous mapping and a queue submit.
+
+        * CommandLinePlayground/main.swift:
+        (device):
+
+2022-03-16  Myles C. Maxfield  <[email protected]>
+
         [WebGPU] Use the fromAPI() pattern
         https://bugs.webkit.org/show_bug.cgi?id=237942
 

Modified: trunk/Source/WebGPU/CommandLinePlayground/main.swift (291376 => 291377)


--- trunk/Source/WebGPU/CommandLinePlayground/main.swift	2022-03-17 00:02:29 UTC (rev 291376)
+++ trunk/Source/WebGPU/CommandLinePlayground/main.swift	2022-03-17 00:05:30 UTC (rev 291377)
@@ -34,9 +34,7 @@
 var adapter: WGPUAdapter!
 var requestAdapterOptions = WGPURequestAdapterOptions(nextInChain: nil, compatibleSurface: nil, powerPreference: WGPUPowerPreference_Undefined, forceFallbackAdapter: false)
 wgpuInstanceRequestAdapterWithBlock(instance, &requestAdapterOptions) { (status: WGPURequestAdapterStatus, localAdapter: Optional<WGPUAdapter>, message: Optional<UnsafePointer<Int8>>) in
-    guard let localAdapter = localAdapter else {
-        fatalError()
-    }
+    assert(localAdapter != nil)
     adapter = localAdapter
 }
 defer {
@@ -44,7 +42,62 @@
 }
 print("Adapter: \(String(describing: adapter))")
 
-//wgpuInstanceProcessEvents(instance)
+var device: WGPUDevice!
+var deviceDescriptor = WGPUDeviceDescriptor(nextInChain: nil, label: nil, requiredFeaturesCount: 0, requiredFeatures: nil, requiredLimits: nil)
+wgpuAdapterRequestDeviceWithBlock(adapter, &deviceDescriptor) { (status: WGPURequestDeviceStatus, localDevice: Optional<WGPUDevice>, message: Optional<UnsafePointer<Int8>>) in
+    assert(localDevice != nil)
+    device = localDevice
+}
+defer {
+    wgpuDeviceRelease(device)
+}
+print("Device: \(String(describing: device))")
 
+var uploadBufferDescriptor = WGPUBufferDescriptor(nextInChain: nil, label: nil, usage: WGPUBufferUsage_MapWrite.rawValue | WGPUBufferUsage_CopySrc.rawValue, size: UInt64(MemoryLayout<Int32>.size), mappedAtCreation: false)
+let uploadBuffer = wgpuDeviceCreateBuffer(device, &uploadBufferDescriptor)
+assert(uploadBuffer != nil)
+defer {
+    wgpuBufferRelease(uploadBuffer)
+}
 
+var downloadBufferDescriptor = WGPUBufferDescriptor(nextInChain: nil, label: nil, usage: WGPUBufferUsage_MapRead.rawValue | WGPUBufferUsage_CopyDst.rawValue, size: UInt64(MemoryLayout<Int32>.size), mappedAtCreation: false)
+let downloadBuffer = wgpuDeviceCreateBuffer(device, &downloadBufferDescriptor)
+assert(downloadBuffer != nil)
+defer {
+    wgpuBufferRelease(downloadBuffer)
+}
+
+wgpuBufferMapAsyncWithBlock(uploadBuffer, WGPUMapMode_Write.rawValue, 0, MemoryLayout<Int32>.size) { (status: WGPUBufferMapAsyncStatus) in
+    assert(status == WGPUBufferMapAsyncStatus_Success);
+    let writePointer = wgpuBufferGetMappedRange(uploadBuffer, 0, MemoryLayout<Int32>.size).bindMemory(to: Int32.self, capacity: 1)
+    writePointer[0] = 17
+    wgpuBufferUnmap(uploadBuffer)
+
+    var commandEncoderDescriptor = WGPUCommandEncoderDescriptor(nextInChain: nil, label: nil)
+    let commandEncoder = wgpuDeviceCreateCommandEncoder(device, &commandEncoderDescriptor)
+    defer {
+        wgpuCommandEncoderRelease(commandEncoder)
+    }
+    wgpuCommandEncoderCopyBufferToBuffer(commandEncoder, uploadBuffer, 0, downloadBuffer, 0, UInt64(MemoryLayout<Int32>.size))
+    var commandBufferDescriptor = WGPUCommandBufferDescriptor(nextInChain: nil, label: nil)
+    let commandBuffer = wgpuCommandEncoderFinish(commandEncoder, &commandBufferDescriptor)
+    defer {
+        wgpuCommandBufferRelease(commandBuffer)
+    }
+
+    let commands: [WGPUCommandBuffer?] = [commandBuffer]
+    wgpuQueueSubmit(wgpuDeviceGetQueue(device), UInt32(commands.count), commands)
+
+    wgpuQueueOnSubmittedWorkDoneWithBlock(wgpuDeviceGetQueue(device), 0) { (status: WGPUQueueWorkDoneStatus) in
+        assert(status == WGPUQueueWorkDoneStatus_Success)
+        wgpuBufferMapAsyncWithBlock(downloadBuffer, WGPUMapMode_Read.rawValue, 0, MemoryLayout<Int32>.size) { (status: WGPUBufferMapAsyncStatus) in
+            assert(status == WGPUBufferMapAsyncStatus_Success);
+            let readPointer = wgpuBufferGetMappedRange(downloadBuffer, 0, MemoryLayout<Int32>.size).bindMemory(to: Int32.self, capacity: 1)
+            print("Result: \(readPointer[0])")
+            wgpuBufferUnmap(downloadBuffer)
+            CFRunLoopStop(CFRunLoopGetMain())
+        }
+    }
+}
+
 CFRunLoopRun()
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to