Title: [292637] trunk/Source/WebGPU
Revision
292637
Author
mmaxfi...@apple.com
Date
2022-04-08 15:00:22 -0700 (Fri, 08 Apr 2022)

Log Message

[WebGPU] Implement destroy() methods
https://bugs.webkit.org/show_bug.cgi?id=238723

Reviewed by Dean Jackson.

The destroy() methods cause objects to become invalid. This is important because
the test suite creates and destroys lots of objects, and we want to make sure we
free up resources before GC runs.

This patch also makes Device own a Ref<Adapter> instead of Ref<Instance> because
part of the implementation of Device::destroy() involves interacting with its
adapter.

* WebGPU/Adapter.h:
(WebGPU::Adapter::makeInvalid):
(WebGPU::Adapter::instance const):
* WebGPU/Adapter.mm:
(WebGPU::Adapter::requestDevice):
* WebGPU/Buffer.h:
* WebGPU/Device.h:
(WebGPU::Device::createInvalid):
(WebGPU::Device::instance const):
(WebGPU::Device::makeInvalid):
* WebGPU/Device.mm:
(WebGPU::Device::create):
(WebGPU::Device::Device):
(WebGPU::Device::loseTheDevice):
(WebGPU::Device::destroy):
(WebGPU::Device::generateAValidationError):
(WebGPU::Device::popErrorScope):
* WebGPU/QuerySet.h:
* WebGPU/QuerySet.mm:
(WebGPU::QuerySet::destroy):
* WebGPU/Queue.h:
(WebGPU::Queue::makeInvalid):
* WebGPU/Texture.h:
* WebGPU/Texture.mm:
(WebGPU::Texture::destroy):

Modified Paths

Diff

Modified: trunk/Source/WebGPU/ChangeLog (292636 => 292637)


--- trunk/Source/WebGPU/ChangeLog	2022-04-08 21:39:57 UTC (rev 292636)
+++ trunk/Source/WebGPU/ChangeLog	2022-04-08 22:00:22 UTC (rev 292637)
@@ -1,3 +1,44 @@
+2022-04-08  Myles C. Maxfield  <mmaxfi...@apple.com>
+
+        [WebGPU] Implement destroy() methods
+        https://bugs.webkit.org/show_bug.cgi?id=238723
+
+        Reviewed by Dean Jackson.
+
+        The destroy() methods cause objects to become invalid. This is important because
+        the test suite creates and destroys lots of objects, and we want to make sure we
+        free up resources before GC runs.
+
+        This patch also makes Device own a Ref<Adapter> instead of Ref<Instance> because
+        part of the implementation of Device::destroy() involves interacting with its
+        adapter.
+
+        * WebGPU/Adapter.h:
+        (WebGPU::Adapter::makeInvalid):
+        (WebGPU::Adapter::instance const):
+        * WebGPU/Adapter.mm:
+        (WebGPU::Adapter::requestDevice):
+        * WebGPU/Buffer.h:
+        * WebGPU/Device.h:
+        (WebGPU::Device::createInvalid):
+        (WebGPU::Device::instance const):
+        (WebGPU::Device::makeInvalid):
+        * WebGPU/Device.mm:
+        (WebGPU::Device::create):
+        (WebGPU::Device::Device):
+        (WebGPU::Device::loseTheDevice):
+        (WebGPU::Device::destroy):
+        (WebGPU::Device::generateAValidationError):
+        (WebGPU::Device::popErrorScope):
+        * WebGPU/QuerySet.h:
+        * WebGPU/QuerySet.mm:
+        (WebGPU::QuerySet::destroy):
+        * WebGPU/Queue.h:
+        (WebGPU::Queue::makeInvalid):
+        * WebGPU/Texture.h:
+        * WebGPU/Texture.mm:
+        (WebGPU::Texture::destroy):
+
 2022-04-08  Elliott Williams  <e...@apple.com>
 
         Unreviewed, reverting r292591.

Modified: trunk/Source/WebGPU/WebGPU/Adapter.h (292636 => 292637)


--- trunk/Source/WebGPU/WebGPU/Adapter.h	2022-04-08 21:39:57 UTC (rev 292636)
+++ trunk/Source/WebGPU/WebGPU/Adapter.h	2022-04-08 22:00:22 UTC (rev 292637)
@@ -61,12 +61,16 @@
     void requestDevice(const WGPUDeviceDescriptor&, CompletionHandler<void(WGPURequestDeviceStatus, RefPtr<Device>&&, String&&)>&& callback);
 
     bool isValid() const { return m_device; }
+    void makeInvalid() { m_device = nil; }
 
+    Instance& instance() const { return m_instance; }
+
+
 private:
     Adapter(id<MTLDevice>, Instance&);
     Adapter(Instance&);
 
-    const id<MTLDevice> m_device { nil };
+    id<MTLDevice> m_device { nil };
     const Ref<Instance> m_instance;
 };
 

Modified: trunk/Source/WebGPU/WebGPU/Adapter.mm (292636 => 292637)


--- trunk/Source/WebGPU/WebGPU/Adapter.mm	2022-04-08 21:39:57 UTC (rev 292636)
+++ trunk/Source/WebGPU/WebGPU/Adapter.mm	2022-04-08 22:00:22 UTC (rev 292637)
@@ -128,7 +128,7 @@
         return;
     }
 
-    callback(WGPURequestDeviceStatus_Success, Device::create(m_device, String::fromLatin1(descriptor.label), m_instance), { });
+    callback(WGPURequestDeviceStatus_Success, Device::create(m_device, String::fromLatin1(descriptor.label), *this), { });
 }
 
 } // namespace WebGPU

Modified: trunk/Source/WebGPU/WebGPU/Buffer.h (292636 => 292637)


--- trunk/Source/WebGPU/WebGPU/Buffer.h	2022-04-08 21:39:57 UTC (rev 292636)
+++ trunk/Source/WebGPU/WebGPU/Buffer.h	2022-04-08 22:00:22 UTC (rev 292637)
@@ -71,7 +71,6 @@
     bool isValid() const { return m_buffer; }
 
     // https://gpuweb.github.io/gpuweb/#buffer-state
-    // Each GPUBuffer has a current buffer state on the Content timeline which is one of the following:
     enum class State : uint8_t {
         Mapped,
         MappedAtCreation,

Modified: trunk/Source/WebGPU/WebGPU/Device.h (292636 => 292637)


--- trunk/Source/WebGPU/WebGPU/Device.h	2022-04-08 21:39:57 UTC (rev 292636)
+++ trunk/Source/WebGPU/WebGPU/Device.h	2022-04-08 22:00:22 UTC (rev 292637)
@@ -25,6 +25,7 @@
 
 #pragma once
 
+#import "Adapter.h"
 #import "Queue.h"
 #import <wtf/CompletionHandler.h>
 #import <wtf/FastMalloc.h>
@@ -60,10 +61,10 @@
 class Device : public WGPUDeviceImpl, public ThreadSafeRefCounted<Device> {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    static RefPtr<Device> create(id<MTLDevice>, String&& deviceLabel, Instance&);
-    static Ref<Device> createInvalid(Instance& instance)
+    static RefPtr<Device> create(id<MTLDevice>, String&& deviceLabel, Adapter&);
+    static Ref<Device> createInvalid(Adapter& adapter)
     {
-        return adoptRef(*new Device(instance));
+        return adoptRef(*new Device(adapter));
     }
 
     ~Device();
@@ -100,12 +101,12 @@
 
     void generateAValidationError(String&& message);
 
-    Instance& instance() const { return m_instance; }
+    Instance& instance() const { return m_adapter->instance(); }
     bool hasUnifiedMemory() const { return m_device.hasUnifiedMemory; }
 
 private:
-    Device(id<MTLDevice>, id<MTLCommandQueue> defaultQueue, Instance&);
-    Device(Instance&);
+    Device(id<MTLDevice>, id<MTLCommandQueue> defaultQueue, Adapter&);
+    Device(Adapter&);
 
     struct ErrorScope;
     ErrorScope* currentErrorScope(WGPUErrorFilter);
@@ -113,6 +114,10 @@
     id<MTLBuffer> safeCreateBuffer(NSUInteger length, MTLStorageMode, MTLCPUCacheMode = MTLCPUCacheModeDefaultCache, MTLHazardTrackingMode = MTLHazardTrackingModeDefault) const;
     bool validateCreateTexture(const WGPUTextureDescriptor&, const Vector<WGPUTextureFormat>& viewFormats);
 
+    void makeInvalid() { m_device = nil; }
+
+    void loseTheDevice();
+
     struct Error {
         WGPUErrorType type;
         String message;
@@ -122,9 +127,9 @@
         const WGPUErrorFilter filter;
     };
 
-    const id<MTLDevice> m_device { nil };
+    id<MTLDevice> m_device { nil };
     const Ref<Queue> m_defaultQueue;
-    const Ref<Instance> m_instance;
+    const Ref<Adapter> m_adapter;
 
     Function<void(WGPUErrorType, String&&)> m_uncapturedErrorCallback;
     Vector<ErrorScope> m_errorScopeStack;

Modified: trunk/Source/WebGPU/WebGPU/Device.mm (292636 => 292637)


--- trunk/Source/WebGPU/WebGPU/Device.mm	2022-04-08 21:39:57 UTC (rev 292636)
+++ trunk/Source/WebGPU/WebGPU/Device.mm	2022-04-08 22:00:22 UTC (rev 292637)
@@ -46,7 +46,7 @@
 
 namespace WebGPU {
 
-RefPtr<Device> Device::create(id<MTLDevice> device, String&& deviceLabel, Instance& instance)
+RefPtr<Device> Device::create(id<MTLDevice> device, String&& deviceLabel, Adapter& adapter)
 {
     id<MTLCommandQueue> commandQueue = [device newCommandQueue];
     if (!commandQueue)
@@ -58,27 +58,45 @@
     if (!deviceLabel.isEmpty())
         commandQueue.label = [NSString stringWithFormat:@"Default queue for device %s", deviceLabel.utf8().data()];
 
-    return adoptRef(*new Device(device, commandQueue, instance));
+    return adoptRef(*new Device(device, commandQueue, adapter));
 }
 
-Device::Device(id<MTLDevice> device, id<MTLCommandQueue> defaultQueue, Instance& instance)
+Device::Device(id<MTLDevice> device, id<MTLCommandQueue> defaultQueue, Adapter& adapter)
     : m_device(device)
     , m_defaultQueue(Queue::create(defaultQueue, *this))
-    , m_instance(instance)
+    , m_adapter(adapter)
 {
 }
 
-Device::Device(Instance& instance)
+Device::Device(Adapter& adapter)
     : m_defaultQueue(Queue::createInvalid(*this))
-    , m_instance(instance)
+    , m_adapter(adapter)
 {
 }
 
 Device::~Device() = default;
 
+void Device::loseTheDevice()
+{
+    // https://gpuweb.github.io/gpuweb/#lose-the-device
+
+    m_adapter->makeInvalid();
+
+    makeInvalid();
+
+    // FIXME: "Resolve device.lost with a new GPUDeviceLostInfo with reason set to reason and message set to an implementation-defined value."
+
+    // FIXME: The spec doesn't actually say to do this, but it's pretty important because
+    // the total number of command queues alive at a time is limited to a pretty low limit.
+    // We should make sure either that this is unobservable or that the spec says to do this.
+    m_defaultQueue->makeInvalid();
+}
+
 void Device::destroy()
 {
-    // FIXME: Implement this.
+    // https://gpuweb.github.io/gpuweb/#dom-gpudevice-destroy
+
+    loseTheDevice();
 }
 
 size_t Device::enumerateFeatures(WGPUFeatureName*)
@@ -132,7 +150,7 @@
     }
 
     if (m_uncapturedErrorCallback) {
-        m_instance->scheduleWork([protectedThis = Ref { *this }, message = WTFMove(message)]() mutable {
+        instance().scheduleWork([protectedThis = Ref { *this }, message = WTFMove(message)]() mutable {
             protectedThis->m_uncapturedErrorCallback(WGPUErrorType_Validation, WTFMove(message));
         });
     }
@@ -159,7 +177,7 @@
 
     auto scope = m_errorScopeStack.takeLast();
 
-    m_instance->scheduleWork([scope = WTFMove(scope), callback = WTFMove(callback)]() mutable {
+    instance().scheduleWork([scope = WTFMove(scope), callback = WTFMove(callback)]() mutable {
         if (scope.error)
             callback(scope.error->type, WTFMove(scope.error->message));
         else

Modified: trunk/Source/WebGPU/WebGPU/QuerySet.h (292636 => 292637)


--- trunk/Source/WebGPU/WebGPU/QuerySet.h	2022-04-08 21:39:57 UTC (rev 292636)
+++ trunk/Source/WebGPU/WebGPU/QuerySet.h	2022-04-08 22:00:22 UTC (rev 292637)
@@ -64,7 +64,7 @@
     QuerySet(id<MTLCounterSampleBuffer>, Device&);
     QuerySet(Device&);
 
-    const id<MTLCounterSampleBuffer> m_counterSampleBuffer { nil };
+    id<MTLCounterSampleBuffer> m_counterSampleBuffer { nil };
 
     const Ref<Device> m_device;
 };

Modified: trunk/Source/WebGPU/WebGPU/QuerySet.mm (292636 => 292637)


--- trunk/Source/WebGPU/WebGPU/QuerySet.mm	2022-04-08 21:39:57 UTC (rev 292636)
+++ trunk/Source/WebGPU/WebGPU/QuerySet.mm	2022-04-08 22:00:22 UTC (rev 292637)
@@ -52,6 +52,9 @@
 
 void QuerySet::destroy()
 {
+    // https://gpuweb.github.io/gpuweb/#dom-gpuqueryset-destroy
+
+    m_counterSampleBuffer = nil;
 }
 
 void QuerySet::setLabel(String&&)

Modified: trunk/Source/WebGPU/WebGPU/Queue.h (292636 => 292637)


--- trunk/Source/WebGPU/WebGPU/Queue.h	2022-04-08 21:39:57 UTC (rev 292636)
+++ trunk/Source/WebGPU/WebGPU/Queue.h	2022-04-08 22:00:22 UTC (rev 292637)
@@ -65,6 +65,7 @@
     void setLabel(String&&);
 
     bool isValid() const { return m_commandQueue; }
+    void makeInvalid() { m_commandQueue = nil; }
 
     id<MTLCommandQueue> commandQueue() const { return m_commandQueue; }
 
@@ -84,7 +85,7 @@
     // This can be called on a background thread.
     void scheduleWork(Instance::WorkItem&&);
 
-    const id<MTLCommandQueue> m_commandQueue { nil };
+    id<MTLCommandQueue> m_commandQueue { nil };
     id<MTLCommandBuffer> m_commandBuffer { nil };
     id<MTLBlitCommandEncoder> m_blitCommandEncoder { nil };
     Device& m_device; // The only kind of queues that exist right now are default queues, which are owned by Devices.

Modified: trunk/Source/WebGPU/WebGPU/Texture.h (292636 => 292637)


--- trunk/Source/WebGPU/WebGPU/Texture.h	2022-04-08 21:39:57 UTC (rev 292636)
+++ trunk/Source/WebGPU/WebGPU/Texture.h	2022-04-08 22:00:22 UTC (rev 292637)
@@ -93,7 +93,7 @@
     uint32_t arrayLayerCount() const;
     bool validateCreateView(const WGPUTextureViewDescriptor&) const;
 
-    const id<MTLTexture> m_texture { nil };
+    id<MTLTexture> m_texture { nil };
 
     const WGPUTextureDescriptor m_descriptor { };
     const Vector<WGPUTextureFormat> m_viewFormats;

Modified: trunk/Source/WebGPU/WebGPU/Texture.mm (292636 => 292637)


--- trunk/Source/WebGPU/WebGPU/Texture.mm	2022-04-08 21:39:57 UTC (rev 292636)
+++ trunk/Source/WebGPU/WebGPU/Texture.mm	2022-04-08 22:00:22 UTC (rev 292637)
@@ -2348,6 +2348,9 @@
 
 void Texture::destroy()
 {
+    // https://gpuweb.github.io/gpuweb/#dom-gputexture-destroy
+
+    m_texture = nil;
 }
 
 void Texture::setLabel(String&& label)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to