Title: [291701] trunk/Source/WebGPU
Revision
291701
Author
[email protected]
Date
2022-03-22 14:11:31 -0700 (Tue, 22 Mar 2022)

Log Message

[WebGPU] Allocating a buffer of size 0 crashes
https://bugs.webkit.org/show_bug.cgi?id=238165

Reviewed by Kimmo Kinnunen.

Metal requires buffer sizes to be > 0, but WebGPU allows for = ""
This patch simply clamps all buffer sizes to 1. In the future, if this
causes performance problems, we could consider some more invasive change
where the MTLBuffer member of a WebGPU::Buffer is nil. But, for now,
just clamping the size to 1 is enough to get the test to stop crashing.

Test: api/validation/buffer/mapping.spec.ts. (We can't actually enable the test
until WebCore links with WebGPU.)

* WebGPU/BindGroup.mm:
(WebGPU::Device::createBindGroup):
* WebGPU/Buffer.mm:
(WebGPU::Device::safeCreateBuffer const):
(WebGPU::Device::createBuffer):
* WebGPU/ComputePipeline.mm:
* WebGPU/Device.h:
* WebGPU/Instance.mm:
(wgpuGetProcAddress):
* WebGPU/ShaderModule.mm:
(WebGPU::ShaderModule::createLibrary):

Modified Paths

Diff

Modified: trunk/Source/WebGPU/ChangeLog (291700 => 291701)


--- trunk/Source/WebGPU/ChangeLog	2022-03-22 21:09:51 UTC (rev 291700)
+++ trunk/Source/WebGPU/ChangeLog	2022-03-22 21:11:31 UTC (rev 291701)
@@ -1,5 +1,33 @@
 2022-03-22  Myles C. Maxfield  <[email protected]>
 
+        [WebGPU] Allocating a buffer of size 0 crashes
+        https://bugs.webkit.org/show_bug.cgi?id=238165
+
+        Reviewed by Kimmo Kinnunen.
+
+        Metal requires buffer sizes to be > 0, but WebGPU allows for = ""
+        This patch simply clamps all buffer sizes to 1. In the future, if this
+        causes performance problems, we could consider some more invasive change
+        where the MTLBuffer member of a WebGPU::Buffer is nil. But, for now,
+        just clamping the size to 1 is enough to get the test to stop crashing.
+
+        Test: api/validation/buffer/mapping.spec.ts. (We can't actually enable the test
+        until WebCore links with WebGPU.)
+
+        * WebGPU/BindGroup.mm:
+        (WebGPU::Device::createBindGroup):
+        * WebGPU/Buffer.mm:
+        (WebGPU::Device::safeCreateBuffer const):
+        (WebGPU::Device::createBuffer):
+        * WebGPU/ComputePipeline.mm:
+        * WebGPU/Device.h:
+        * WebGPU/Instance.mm:
+        (wgpuGetProcAddress):
+        * WebGPU/ShaderModule.mm:
+        (WebGPU::ShaderModule::createLibrary):
+
+2022-03-22  Myles C. Maxfield  <[email protected]>
+
         [WebGPU] Implement Queue::writeBuffer()
         https://bugs.webkit.org/show_bug.cgi?id=238194
 

Modified: trunk/Source/WebGPU/WebGPU/BindGroup.mm (291700 => 291701)


--- trunk/Source/WebGPU/WebGPU/BindGroup.mm	2022-03-22 21:09:51 UTC (rev 291700)
+++ trunk/Source/WebGPU/WebGPU/BindGroup.mm	2022-03-22 21:11:31 UTC (rev 291701)
@@ -57,11 +57,11 @@
 
     const BindGroupLayout& bindGroupLayout = WebGPU::fromAPI(descriptor.layout);
 
-    // FIXME: Don't allocate 3 new buffers for every bind group.
+    // FIXME(PERFORMANCE): Don't allocate 3 new buffers for every bind group.
     // In fact, don't even allocate a single new buffer for every bind group.
-    id<MTLBuffer> vertexArgumentBuffer = [m_device newBufferWithLength:bindGroupLayout.encodedLength() options:MTLResourceStorageModeShared];
-    id<MTLBuffer> fragmentArgumentBuffer = [m_device newBufferWithLength:bindGroupLayout.encodedLength() options:MTLResourceStorageModeShared];
-    id<MTLBuffer> computeArgumentBuffer = [m_device newBufferWithLength:bindGroupLayout.encodedLength() options:MTLResourceStorageModeShared];
+    id<MTLBuffer> vertexArgumentBuffer = safeCreateBuffer(bindGroupLayout.encodedLength(), MTLStorageModeShared);
+    id<MTLBuffer> fragmentArgumentBuffer = safeCreateBuffer(bindGroupLayout.encodedLength(), MTLStorageModeShared);
+    id<MTLBuffer> computeArgumentBuffer = safeCreateBuffer(bindGroupLayout.encodedLength(), MTLStorageModeShared);
     if (!vertexArgumentBuffer || !fragmentArgumentBuffer || !computeArgumentBuffer)
         return nullptr;
 

Modified: trunk/Source/WebGPU/WebGPU/Buffer.mm (291700 => 291701)


--- trunk/Source/WebGPU/WebGPU/Buffer.mm	2022-03-22 21:09:51 UTC (rev 291700)
+++ trunk/Source/WebGPU/WebGPU/Buffer.mm	2022-03-22 21:11:31 UTC (rev 291701)
@@ -97,6 +97,14 @@
     return MTLStorageModePrivate;
 }
 
+id<MTLBuffer> Device::safeCreateBuffer(NSUInteger length, MTLStorageMode storageMode, MTLCPUCacheMode cpuCacheMode, MTLHazardTrackingMode hazardTrackingMode) const
+{
+    MTLResourceOptions resourceOptions = (cpuCacheMode << MTLResourceCPUCacheModeShift) | (storageMode << MTLResourceStorageModeShift) | (hazardTrackingMode << MTLResourceHazardTrackingModeShift);
+    // FIXME(PERFORMANCE): Consider returning nil instead of clamping to 1.
+    // FIXME(PERFORMANCE): Suballocate multiple Buffers either from MTLHeaps or from larger MTLBuffers.
+    return [m_device newBufferWithLength:std::max(static_cast<NSUInteger>(1), length) options:resourceOptions];
+}
+
 RefPtr<Buffer> Device::createBuffer(const WGPUBufferDescriptor& descriptor)
 {
     if (descriptor.nextInChain)
@@ -108,12 +116,10 @@
     if (!validateCreateBuffer(*this, descriptor))
         return nullptr;
 
-    // FIXME: Use heap allocation to make this faster.
-    // 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.
+    // FIXME(PERFORMANCE): Consider write-combining CPU cache mode.
+    // FIXME(PERFORMANCE): Consider implementing hazard tracking ourself.
     MTLStorageMode storageMode = WebGPU::storageMode(hasUnifiedMemory(), descriptor.usage, descriptor.mappedAtCreation);
-    id<MTLBuffer> buffer = [m_device newBufferWithLength:static_cast<NSUInteger>(descriptor.size) options:storageMode];
+    auto buffer = safeCreateBuffer(static_cast<NSUInteger>(descriptor.size), storageMode);
     if (!buffer)
         return nullptr;
 

Modified: trunk/Source/WebGPU/WebGPU/ComputePipeline.mm (291700 => 291701)


--- trunk/Source/WebGPU/WebGPU/ComputePipeline.mm	2022-03-22 21:09:51 UTC (rev 291700)
+++ trunk/Source/WebGPU/WebGPU/ComputePipeline.mm	2022-03-22 21:11:31 UTC (rev 291701)
@@ -36,7 +36,7 @@
 
 struct LibraryCreationResult {
     id<MTLLibrary> library;
-    WGSL::Reflection::EntryPointInformation entryPointInformation; // FIXME: This is big. Don't copy this around.
+    WGSL::Reflection::EntryPointInformation entryPointInformation; // FIXME(PERFORMANCE): This is big. Don't copy this around.
 };
 
 static std::optional<LibraryCreationResult> createLibrary(id<MTLDevice> device, const ShaderModule& shaderModule, const PipelineLayout& pipelineLayout, const String& entryPoint, NSString *label)

Modified: trunk/Source/WebGPU/WebGPU/Device.h (291700 => 291701)


--- trunk/Source/WebGPU/WebGPU/Device.h	2022-03-22 21:09:51 UTC (rev 291700)
+++ trunk/Source/WebGPU/WebGPU/Device.h	2022-03-22 21:11:31 UTC (rev 291701)
@@ -102,6 +102,7 @@
     struct ErrorScope;
     ErrorScope* currentErrorScope(WGPUErrorFilter);
     bool validatePopErrorScope() const;
+    id<MTLBuffer> safeCreateBuffer(NSUInteger length, MTLStorageMode, MTLCPUCacheMode = MTLCPUCacheModeDefaultCache, MTLHazardTrackingMode = MTLHazardTrackingModeDefault) const;
 
     struct Error {
         WGPUErrorType type;

Modified: trunk/Source/WebGPU/WebGPU/Instance.mm (291700 => 291701)


--- trunk/Source/WebGPU/WebGPU/Instance.mm	2022-03-22 21:09:51 UTC (rev 291700)
+++ trunk/Source/WebGPU/WebGPU/Instance.mm	2022-03-22 21:11:31 UTC (rev 291701)
@@ -185,7 +185,7 @@
 
 WGPUProc wgpuGetProcAddress(WGPUDevice, const char* procName)
 {
-    // FIXME: Use gperf to make this faster.
+    // FIXME(PERFORMANCE): Use gperf to make this faster.
     // FIXME: Generate this at build time
     if (!strcmp(procName, "wgpuAdapterEnumerateFeatures"))
         return reinterpret_cast<WGPUProc>(&wgpuAdapterEnumerateFeatures);

Modified: trunk/Source/WebGPU/WebGPU/ShaderModule.mm (291700 => 291701)


--- trunk/Source/WebGPU/WebGPU/ShaderModule.mm	2022-03-22 21:09:51 UTC (rev 291700)
+++ trunk/Source/WebGPU/WebGPU/ShaderModule.mm	2022-03-22 21:11:31 UTC (rev 291701)
@@ -72,7 +72,7 @@
     auto options = [MTLCompileOptions new];
     options.fastMathEnabled = YES;
     NSError *error = nil;
-    // FIXME: Run the asynchronous version of this
+    // FIXME(PERFORMANCE): Run the asynchronous version of this
     id<MTLLibrary> library = [device newLibraryWithSource:msl options:options error:&error];
     if (error)
         WTFLogAlways("MSL compilation error: %@", error);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to