- Revision
- 292671
- Author
- [email protected]
- Date
- 2022-04-08 19:14:23 -0700 (Fri, 08 Apr 2022)
Log Message
[WebGPU] Implement the concept of device lost
https://bugs.webkit.org/show_bug.cgi?id=238725
Reviewed by Dean Jackson.
This patch builds on https://bugs.webkit.org/show_bug.cgi?id=238723 to finish
the implementation of the concept of device lost. The device can only be lost
on macOS, where you can register a block callback to be run when it happens.
Also, the registration function returns a token that you can pass to the
unregistration function, which happens in Device's destructor.
* WebGPU/Buffer.mm:
(WebGPU::validateDescriptor):
* WebGPU/Device.h:
(WebGPU::Device::isLost const):
* WebGPU/Device.mm:
(WebGPU::Device::Device):
(WebGPU::Device::~Device):
(WebGPU::Device::loseTheDevice):
(WebGPU::Device::destroy):
(WebGPU::Device::validatePopErrorScope const):
(WebGPU::Device::setDeviceLostCallback):
Modified Paths
Diff
Modified: trunk/Source/WebGPU/ChangeLog (292670 => 292671)
--- trunk/Source/WebGPU/ChangeLog 2022-04-09 01:45:07 UTC (rev 292670)
+++ trunk/Source/WebGPU/ChangeLog 2022-04-09 02:14:23 UTC (rev 292671)
@@ -1,5 +1,30 @@
2022-04-08 Myles C. Maxfield <[email protected]>
+ [WebGPU] Implement the concept of device lost
+ https://bugs.webkit.org/show_bug.cgi?id=238725
+
+ Reviewed by Dean Jackson.
+
+ This patch builds on https://bugs.webkit.org/show_bug.cgi?id=238723 to finish
+ the implementation of the concept of device lost. The device can only be lost
+ on macOS, where you can register a block callback to be run when it happens.
+ Also, the registration function returns a token that you can pass to the
+ unregistration function, which happens in Device's destructor.
+
+ * WebGPU/Buffer.mm:
+ (WebGPU::validateDescriptor):
+ * WebGPU/Device.h:
+ (WebGPU::Device::isLost const):
+ * WebGPU/Device.mm:
+ (WebGPU::Device::Device):
+ (WebGPU::Device::~Device):
+ (WebGPU::Device::loseTheDevice):
+ (WebGPU::Device::destroy):
+ (WebGPU::Device::validatePopErrorScope const):
+ (WebGPU::Device::setDeviceLostCallback):
+
+2022-04-08 Myles C. Maxfield <[email protected]>
+
[WebGPU] Represent failure by invalidity rather than nullptr
https://bugs.webkit.org/show_bug.cgi?id=238724
Modified: trunk/Source/WebGPU/WebGPU/Buffer.mm (292670 => 292671)
--- trunk/Source/WebGPU/WebGPU/Buffer.mm 2022-04-09 01:45:07 UTC (rev 292670)
+++ trunk/Source/WebGPU/WebGPU/Buffer.mm 2022-04-09 02:14:23 UTC (rev 292671)
@@ -38,7 +38,8 @@
// https://gpuweb.github.io/gpuweb/#abstract-opdef-validating-gpubufferdescriptor
- // FIXME: "If device is lost return false."
+ if (device.isLost())
+ return false;
// FIXME: "If any of the bits of descriptor’s usage aren’t present in this device’s [[allowed buffer usages]] return false."
Modified: trunk/Source/WebGPU/WebGPU/Device.h (292670 => 292671)
--- trunk/Source/WebGPU/WebGPU/Device.h 2022-04-09 01:45:07 UTC (rev 292670)
+++ trunk/Source/WebGPU/WebGPU/Device.h 2022-04-09 02:14:23 UTC (rev 292671)
@@ -33,6 +33,7 @@
#import <wtf/Ref.h>
#import <wtf/ThreadSafeRefCounted.h>
#import <wtf/Vector.h>
+#import <wtf/WeakPtr.h>
#import <wtf/text/WTFString.h>
struct WGPUDeviceImpl {
@@ -57,7 +58,7 @@
class Texture;
// https://gpuweb.github.io/gpuweb/#gpudevice
-class Device : public WGPUDeviceImpl, public ThreadSafeRefCounted<Device> {
+class Device : public WGPUDeviceImpl, public ThreadSafeRefCounted<Device>, public CanMakeWeakPtr<Device> {
WTF_MAKE_FAST_ALLOCATED;
public:
static Ref<Device> create(id<MTLDevice>, String&& deviceLabel, Adapter&);
@@ -95,6 +96,7 @@
void setLabel(String&&);
bool isValid() const { return m_device; }
+ bool isLost() const { return m_isLost; }
id<MTLDevice> device() const { return m_device; }
@@ -115,7 +117,7 @@
void makeInvalid() { m_device = nil; }
- void loseTheDevice();
+ void loseTheDevice(WGPUDeviceLostReason);
struct Error {
WGPUErrorType type;
@@ -128,10 +130,15 @@
id<MTLDevice> m_device { nil };
const Ref<Queue> m_defaultQueue;
- const Ref<Adapter> m_adapter;
Function<void(WGPUErrorType, String&&)> m_uncapturedErrorCallback;
Vector<ErrorScope> m_errorScopeStack;
+
+ Function<void(WGPUDeviceLostReason, String&&)> m_deviceLostCallback;
+ bool m_isLost { false };
+ id<NSObject> m_deviceObserver { nil };
+
+ const Ref<Adapter> m_adapter;
};
} // namespace WebGPU
Modified: trunk/Source/WebGPU/WebGPU/Device.mm (292670 => 292671)
--- trunk/Source/WebGPU/WebGPU/Device.mm 2022-04-09 01:45:07 UTC (rev 292670)
+++ trunk/Source/WebGPU/WebGPU/Device.mm 2022-04-09 02:14:23 UTC (rev 292671)
@@ -43,6 +43,7 @@
#import "SwapChain.h"
#import "Texture.h"
#import <wtf/StdLibExtras.h>
+#import <wtf/WeakPtr.h>
namespace WebGPU {
@@ -66,6 +67,29 @@
, m_defaultQueue(Queue::create(defaultQueue, *this))
, m_adapter(adapter)
{
+#if PLATFORM(MAC)
+ auto devices = MTLCopyAllDevicesWithObserver(&m_deviceObserver, [weakThis = WeakPtr { *this }](id<MTLDevice> device, MTLDeviceNotificationName) {
+ RefPtr<Device> strongThis = weakThis.get();
+ if (!strongThis)
+ return;
+ strongThis->instance().scheduleWork([strongThis = WTFMove(strongThis), device = device]() {
+ if (![strongThis->m_device isEqual:device])
+ return;
+ strongThis->loseTheDevice(WGPUDeviceLostReason_Undefined);
+ });
+ });
+
+#if ASSERT_ENABLED
+ bool found = false;
+ for (id<MTLDevice> observedDevice in devices) {
+ if ([observedDevice isEqual:device]) {
+ found = true;
+ break;
+ }
+ }
+ ASSERT(found);
+#endif
+#endif
}
Device::Device(Adapter& adapter)
@@ -74,9 +98,14 @@
{
}
-Device::~Device() = default;
+Device::~Device()
+{
+#if PLATFORM(MAC)
+ MTLRemoveDeviceObserver(m_deviceObserver);
+#endif
+}
-void Device::loseTheDevice()
+void Device::loseTheDevice(WGPUDeviceLostReason reason)
{
// https://gpuweb.github.io/gpuweb/#lose-the-device
@@ -84,12 +113,15 @@
makeInvalid();
- // FIXME: "Resolve device.lost with a new GPUDeviceLostInfo with reason set to reason and message set to an implementation-defined value."
+ if (m_deviceLostCallback)
+ m_deviceLostCallback(reason, "Device lost."_s);
// 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();
+
+ m_isLost = true;
}
void Device::destroy()
@@ -96,7 +128,7 @@
{
// https://gpuweb.github.io/gpuweb/#dom-gpudevice-destroy
- loseTheDevice();
+ loseTheDevice(WGPUDeviceLostReason_Destroyed);
}
size_t Device::enumerateFeatures(WGPUFeatureName*)
@@ -158,7 +190,8 @@
bool Device::validatePopErrorScope() const
{
- // FIXME: "this must not be lost."
+ if (m_isLost)
+ return false;
if (m_errorScopeStack.isEmpty())
return false;
@@ -199,8 +232,7 @@
void Device::setDeviceLostCallback(Function<void(WGPUDeviceLostReason, String&&)>&& callback)
{
- // FIXME: Implement this.
- UNUSED_PARAM(callback);
+ m_deviceLostCallback = WTFMove(callback);
}
void Device::setUncapturedErrorCallback(Function<void(WGPUErrorType, String&&)>&& callback)