Title: [291686] trunk/Source/WebGPU
Revision
291686
Author
mmaxfi...@apple.com
Date
2022-03-22 11:58:56 -0700 (Tue, 22 Mar 2022)

Log Message

[WebGPU] Implement GPUBufferDescriptor.mappedAtCreation
https://bugs.webkit.org/show_bug.cgi?id=238190

Reviewed by Kimmo Kinnunen.

On Macs, we can use MTLStorageModeManaged to implement mappedAtCreation.
On non-Macs, all resources are shared, so they're already mapped.
This patch turned out to be surprisingly easy to write, because Metal has
already implemented all the heavy lifting.

Test: api/validation/buffer/mapping.spec.ts

* WebGPU/Buffer.mm:
(WebGPU::storageMode):
(WebGPU::Device::createBuffer):
(WebGPU::Buffer::getMappedRange):
(WebGPU::Buffer::unmap):

Modified Paths

Diff

Modified: trunk/Source/WebGPU/ChangeLog (291685 => 291686)


--- trunk/Source/WebGPU/ChangeLog	2022-03-22 18:54:16 UTC (rev 291685)
+++ trunk/Source/WebGPU/ChangeLog	2022-03-22 18:58:56 UTC (rev 291686)
@@ -1,5 +1,25 @@
 2022-03-22  Myles C. Maxfield  <mmaxfi...@apple.com>
 
+        [WebGPU] Implement GPUBufferDescriptor.mappedAtCreation
+        https://bugs.webkit.org/show_bug.cgi?id=238190
+
+        Reviewed by Kimmo Kinnunen.
+
+        On Macs, we can use MTLStorageModeManaged to implement mappedAtCreation.
+        On non-Macs, all resources are shared, so they're already mapped.
+        This patch turned out to be surprisingly easy to write, because Metal has
+        already implemented all the heavy lifting.
+
+        Test: api/validation/buffer/mapping.spec.ts
+
+        * WebGPU/Buffer.mm:
+        (WebGPU::storageMode):
+        (WebGPU::Device::createBuffer):
+        (WebGPU::Buffer::getMappedRange):
+        (WebGPU::Buffer::unmap):
+
+2022-03-22  Myles C. Maxfield  <mmaxfi...@apple.com>
+
         [WebGPU] Lifetime of CommandEncoder's internal MTLBlitCommandEncoder is not managed correctly
         https://bugs.webkit.org/show_bug.cgi?id=238167
 

Modified: trunk/Source/WebGPU/WebGPU/Buffer.mm (291685 => 291686)


--- trunk/Source/WebGPU/WebGPU/Buffer.mm	2022-03-22 18:54:16 UTC (rev 291685)
+++ trunk/Source/WebGPU/WebGPU/Buffer.mm	2022-03-22 18:58:56 UTC (rev 291686)
@@ -82,14 +82,18 @@
     return true;
 }
 
-static MTLStorageMode storageMode(bool deviceHasUnifiedMemory, WGPUBufferUsageFlags usage)
+static MTLStorageMode storageMode(bool deviceHasUnifiedMemory, WGPUBufferUsageFlags usage, bool mappedAtCreation)
 {
     if (deviceHasUnifiedMemory)
         return MTLStorageModeShared;
-    // On discrete memory GPUs, mappable memory is shared, whereas non-mappable memory is private.
-    // There is no situation where we'll use the managed storage mode.
     if ((usage & WGPUBufferUsage_MapRead) || (usage & WGPUBufferUsage_MapWrite))
         return MTLStorageModeShared;
+#if PLATFORM(MAC) || PLATFORM(MACCATALYST)
+    if (mappedAtCreation)
+        return MTLStorageModeManaged;
+#else
+    UNUSED_PARAM(mappedAtCreation);
+#endif
     return MTLStorageModePrivate;
 }
 
@@ -108,7 +112,7 @@
     // If/when we do that, we have to make sure that "new" buffers get cleared to 0.
     // FIXME: Consider write-combining CPU cache mode.
     // FIXME: Consider implementing hazard tracking ourself.
-    MTLStorageMode storageMode = WebGPU::storageMode(hasUnifiedMemory(), descriptor.usage);
+    MTLStorageMode storageMode = WebGPU::storageMode(hasUnifiedMemory(), descriptor.usage, descriptor.mappedAtCreation);
     id<MTLBuffer> buffer = [m_device newBufferWithLength:static_cast<NSUInteger>(descriptor.size) options:storageMode];
     if (!buffer)
         return nullptr;
@@ -225,6 +229,7 @@
 
     // "Append m to this.[[mapped_ranges]]."
     m_mappedRanges.add({ offset, offset + rangeSize });
+    m_mappedRanges.compact();
 
     // "Return m."
     return static_cast<char*>(m_buffer.contents) + offset;
@@ -381,6 +386,13 @@
         // "Set this.[[mapped_ranges]] to null."
     }
 
+#if PLATFORM(MAC) || PLATFORM(MACCATALYST)
+    if (m_state == State::MappedAtCreation && m_buffer.storageMode == MTLStorageModeManaged) {
+        for (const auto& mappedRange : m_mappedRanges)
+            [m_buffer didModifyRange:NSMakeRange(static_cast<NSUInteger>(mappedRange.begin()), static_cast<NSUInteger>(mappedRange.end() - mappedRange.begin()))];
+    }
+#endif
+
     // "Set this.[[state]] to unmapped."
     m_state = State::Unmapped;
 }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to