From 09ee5eb4991aa93a6096b6b8e3351b865c52ff3a Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Tue, 7 Apr 2020 15:10:17 +0000 Subject: [PATCH] Add a DeviceBase::Initialize that must be called by backends. Bug: dawn:373 Change-Id: I5213496f4676bedc8e2a88912e89b6e0aacbac37 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/18800 Reviewed-by: Corentin Wallez Commit-Queue: Corentin Wallez --- src/dawn_native/Device.cpp | 23 ++++++++++++++--------- src/dawn_native/Device.h | 2 ++ src/dawn_native/d3d12/AdapterD3D12.cpp | 4 +--- src/dawn_native/d3d12/DeviceD3D12.cpp | 12 ++++++++---- src/dawn_native/d3d12/DeviceD3D12.h | 4 +++- src/dawn_native/metal/BackendMTL.mm | 3 ++- src/dawn_native/metal/DeviceMTL.h | 8 +++++++- src/dawn_native/metal/DeviceMTL.mm | 19 ++++++++++++++++--- src/dawn_native/null/DeviceNull.cpp | 13 ++++++++++--- src/dawn_native/null/DeviceNull.h | 6 +++++- src/dawn_native/opengl/BackendGL.cpp | 2 +- src/dawn_native/opengl/DeviceGL.cpp | 18 ++++++++++++++++-- src/dawn_native/opengl/DeviceGL.h | 12 +++++++++--- src/dawn_native/vulkan/AdapterVk.cpp | 4 +--- src/dawn_native/vulkan/DeviceVk.cpp | 9 ++++++++- src/dawn_native/vulkan/DeviceVk.h | 4 +++- 16 files changed, 106 insertions(+), 37 deletions(-) diff --git a/src/dawn_native/Device.cpp b/src/dawn_native/Device.cpp index 87ca07172e..d7a682ed09 100644 --- a/src/dawn_native/Device.cpp +++ b/src/dawn_native/Device.cpp @@ -66,20 +66,13 @@ namespace dawn_native { // DeviceBase DeviceBase::DeviceBase(AdapterBase* adapter, const DeviceDescriptor* descriptor) - : mAdapter(adapter), - mRootErrorScope(AcquireRef(new ErrorScope())), - mCurrentErrorScope(mRootErrorScope.Get()) { - mCaches = std::make_unique(); - mErrorScopeTracker = std::make_unique(this); - mFenceSignalTracker = std::make_unique(this); - mDynamicUploader = std::make_unique(this); - + : mAdapter(adapter) { if (descriptor != nullptr) { ApplyToggleOverrides(descriptor); ApplyExtensions(descriptor); } - mFormatTable = BuildFormatTable(this); + mFormatTable = BuildFormatTable(this); SetDefaultToggles(); } @@ -97,6 +90,18 @@ namespace dawn_native { ASSERT(mCaches->shaderModules.empty()); } + MaybeError DeviceBase::Initialize() { + mRootErrorScope = AcquireRef(new ErrorScope()); + mCurrentErrorScope = mRootErrorScope.Get(); + + mCaches = std::make_unique(); + mErrorScopeTracker = std::make_unique(this); + mFenceSignalTracker = std::make_unique(this); + mDynamicUploader = std::make_unique(this); + + return {}; + } + void DeviceBase::BaseDestructor() { if (mLossStatus != LossStatus::Alive) { // if device is already lost, we may still have fences and error scopes to clear since diff --git a/src/dawn_native/Device.h b/src/dawn_native/Device.h index ebe26edd74..ef6388ed64 100644 --- a/src/dawn_native/Device.h +++ b/src/dawn_native/Device.h @@ -196,6 +196,8 @@ namespace dawn_native { protected: void SetToggle(Toggle toggle, bool isEnabled); void ForceSetToggle(Toggle toggle, bool isEnabled); + + MaybeError Initialize(); void BaseDestructor(); std::unique_ptr mDynamicUploader; diff --git a/src/dawn_native/d3d12/AdapterD3D12.cpp b/src/dawn_native/d3d12/AdapterD3D12.cpp index 9f64134423..8e665d0cf1 100644 --- a/src/dawn_native/d3d12/AdapterD3D12.cpp +++ b/src/dawn_native/d3d12/AdapterD3D12.cpp @@ -95,9 +95,7 @@ namespace dawn_native { namespace d3d12 { } ResultOrError Adapter::CreateDeviceImpl(const DeviceDescriptor* descriptor) { - std::unique_ptr device = std::make_unique(this, descriptor); - DAWN_TRY(device->Initialize()); - return device.release(); + return Device::Create(this, descriptor); } }} // namespace dawn_native::d3d12 diff --git a/src/dawn_native/d3d12/DeviceD3D12.cpp b/src/dawn_native/d3d12/DeviceD3D12.cpp index 5bb02d8964..e7fc74c52d 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.cpp +++ b/src/dawn_native/d3d12/DeviceD3D12.cpp @@ -43,12 +43,16 @@ namespace dawn_native { namespace d3d12 { - Device::Device(Adapter* adapter, const DeviceDescriptor* descriptor) - : DeviceBase(adapter, descriptor) { - InitTogglesFromDriver(); + // static + ResultOrError Device::Create(Adapter* adapter, const DeviceDescriptor* descriptor) { + Ref device = AcquireRef(new Device(adapter, descriptor)); + DAWN_TRY(device->Initialize()); + return device.Detach(); } MaybeError Device::Initialize() { + InitTogglesFromDriver(); + mD3d12Device = ToBackend(GetAdapter())->GetDevice(); ASSERT(mD3d12Device != nullptr); @@ -110,7 +114,7 @@ namespace dawn_native { namespace d3d12 { GetD3D12Device()->CreateCommandSignature(&programDesc, NULL, IID_PPV_ARGS(&mDrawIndexedIndirectSignature)); - return {}; + return DeviceBase::Initialize(); } Device::~Device() { diff --git a/src/dawn_native/d3d12/DeviceD3D12.h b/src/dawn_native/d3d12/DeviceD3D12.h index 1f0c42f7cc..658d895b81 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.h +++ b/src/dawn_native/d3d12/DeviceD3D12.h @@ -45,7 +45,7 @@ namespace dawn_native { namespace d3d12 { // Definition of backend types class Device : public DeviceBase { public: - Device(Adapter* adapter, const DeviceDescriptor* descriptor); + static ResultOrError Create(Adapter* adapter, const DeviceDescriptor* descriptor); ~Device(); MaybeError Initialize(); @@ -112,6 +112,8 @@ namespace dawn_native { namespace d3d12 { void InitTogglesFromDriver(); private: + using DeviceBase::DeviceBase; + ResultOrError CreateBindGroupImpl( const BindGroupDescriptor* descriptor) override; ResultOrError CreateBindGroupLayoutImpl( diff --git a/src/dawn_native/metal/BackendMTL.mm b/src/dawn_native/metal/BackendMTL.mm index 2ec84109f0..dd7e25fdfd 100644 --- a/src/dawn_native/metal/BackendMTL.mm +++ b/src/dawn_native/metal/BackendMTL.mm @@ -206,8 +206,9 @@ namespace dawn_native { namespace metal { private: ResultOrError CreateDeviceImpl(const DeviceDescriptor* descriptor) override { - return {new Device(this, mDevice, descriptor)}; + return Device::Create(this, mDevice, descriptor); } + void InitializeSupportedExtensions() { #if defined(DAWN_PLATFORM_MACOS) if ([mDevice supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v1]) { diff --git a/src/dawn_native/metal/DeviceMTL.h b/src/dawn_native/metal/DeviceMTL.h index 6fa5b72c63..be4df7e10f 100644 --- a/src/dawn_native/metal/DeviceMTL.h +++ b/src/dawn_native/metal/DeviceMTL.h @@ -36,9 +36,13 @@ namespace dawn_native { namespace metal { class Device : public DeviceBase { public: - Device(AdapterBase* adapter, id mtlDevice, const DeviceDescriptor* descriptor); + static ResultOrError Create(AdapterBase* adapter, + id mtlDevice, + const DeviceDescriptor* descriptor); ~Device(); + MaybeError Initialize(); + CommandBufferBase* CreateCommandBuffer(CommandEncoder* encoder, const CommandBufferDescriptor* descriptor) override; @@ -68,6 +72,8 @@ namespace dawn_native { namespace metal { uint64_t size) override; private: + Device(AdapterBase* adapter, id mtlDevice, const DeviceDescriptor* descriptor); + ResultOrError CreateBindGroupImpl( const BindGroupDescriptor* descriptor) override; ResultOrError CreateBindGroupLayoutImpl( diff --git a/src/dawn_native/metal/DeviceMTL.mm b/src/dawn_native/metal/DeviceMTL.mm index faaa0406e9..51dec67c84 100644 --- a/src/dawn_native/metal/DeviceMTL.mm +++ b/src/dawn_native/metal/DeviceMTL.mm @@ -38,6 +38,15 @@ namespace dawn_native { namespace metal { + // static + ResultOrError Device::Create(AdapterBase* adapter, + id mtlDevice, + const DeviceDescriptor* descriptor) { + Ref device = AcquireRef(new Device(adapter, mtlDevice, descriptor)); + DAWN_TRY(device->Initialize()); + return device.Detach(); + } + Device::Device(AdapterBase* adapter, id mtlDevice, const DeviceDescriptor* descriptor) @@ -46,15 +55,19 @@ namespace dawn_native { namespace metal { mMapTracker(new MapRequestTracker(this)), mCompletedSerial(0) { [mMtlDevice retain]; - mCommandQueue = [mMtlDevice newCommandQueue]; - - InitTogglesFromDriver(); } Device::~Device() { BaseDestructor(); } + MaybeError Device::Initialize() { + InitTogglesFromDriver(); + mCommandQueue = [mMtlDevice newCommandQueue]; + + return DeviceBase::Initialize(); + } + void Device::InitTogglesFromDriver() { { bool haveStoreAndMSAAResolve = false; diff --git a/src/dawn_native/null/DeviceNull.cpp b/src/dawn_native/null/DeviceNull.cpp index b66be28e79..1e467d90b9 100644 --- a/src/dawn_native/null/DeviceNull.cpp +++ b/src/dawn_native/null/DeviceNull.cpp @@ -43,7 +43,7 @@ namespace dawn_native { namespace null { } ResultOrError Adapter::CreateDeviceImpl(const DeviceDescriptor* descriptor) { - return {new Device(this, descriptor)}; + return Device::Create(this, descriptor); } class Backend : public BackendConnection { @@ -78,8 +78,11 @@ namespace dawn_native { namespace null { // Device - Device::Device(Adapter* adapter, const DeviceDescriptor* descriptor) - : DeviceBase(adapter, descriptor) { + // static + ResultOrError Device::Create(Adapter* adapter, const DeviceDescriptor* descriptor) { + Ref device = AcquireRef(new Device(adapter, descriptor)); + DAWN_TRY(device->Initialize()); + return device.Detach(); } Device::~Device() { @@ -89,6 +92,10 @@ namespace dawn_native { namespace null { ASSERT(mMemoryUsage == 0); } + MaybeError Device::Initialize() { + return DeviceBase::Initialize(); + } + ResultOrError Device::CreateBindGroupImpl( const BindGroupDescriptor* descriptor) { return new BindGroup(this, descriptor); diff --git a/src/dawn_native/null/DeviceNull.h b/src/dawn_native/null/DeviceNull.h index 2a003e20b5..4e3963a6db 100644 --- a/src/dawn_native/null/DeviceNull.h +++ b/src/dawn_native/null/DeviceNull.h @@ -83,9 +83,11 @@ namespace dawn_native { namespace null { class Device : public DeviceBase { public: - Device(Adapter* adapter, const DeviceDescriptor* descriptor); + static ResultOrError Create(Adapter* adapter, const DeviceDescriptor* descriptor); ~Device(); + MaybeError Initialize(); + CommandBufferBase* CreateCommandBuffer(CommandEncoder* encoder, const CommandBufferDescriptor* descriptor) override; @@ -108,6 +110,8 @@ namespace dawn_native { namespace null { void DecrementMemoryUsage(size_t bytes); private: + using DeviceBase::DeviceBase; + ResultOrError CreateBindGroupImpl( const BindGroupDescriptor* descriptor) override; ResultOrError CreateBindGroupLayoutImpl( diff --git a/src/dawn_native/opengl/BackendGL.cpp b/src/dawn_native/opengl/BackendGL.cpp index 7116d8e773..5ce73d88b5 100644 --- a/src/dawn_native/opengl/BackendGL.cpp +++ b/src/dawn_native/opengl/BackendGL.cpp @@ -182,7 +182,7 @@ namespace dawn_native { namespace opengl { ResultOrError CreateDeviceImpl(const DeviceDescriptor* descriptor) override { // There is no limit on the number of devices created from this adapter because they can // all share the same backing OpenGL context. - return {new Device(this, descriptor, mFunctions)}; + return Device::Create(this, descriptor, mFunctions); } void InitializeSupportedExtensions() { diff --git a/src/dawn_native/opengl/DeviceGL.cpp b/src/dawn_native/opengl/DeviceGL.cpp index e0f8a7939f..eeb92fc538 100644 --- a/src/dawn_native/opengl/DeviceGL.cpp +++ b/src/dawn_native/opengl/DeviceGL.cpp @@ -33,18 +33,32 @@ namespace dawn_native { namespace opengl { + // static + ResultOrError Device::Create(AdapterBase* adapter, + const DeviceDescriptor* descriptor, + const OpenGLFunctions& functions) { + Ref device = AcquireRef(new Device(adapter, descriptor, functions)); + DAWN_TRY(device->Initialize()); + return device.Detach(); + } + Device::Device(AdapterBase* adapter, const DeviceDescriptor* descriptor, const OpenGLFunctions& functions) : DeviceBase(adapter, descriptor), gl(functions) { - InitTogglesFromDriver(); - mFormatTable = BuildGLFormatTable(); } Device::~Device() { BaseDestructor(); } + MaybeError Device::Initialize() { + InitTogglesFromDriver(); + mFormatTable = BuildGLFormatTable(); + + return DeviceBase::Initialize(); + } + void Device::InitTogglesFromDriver() { bool supportsBaseVertex = gl.IsAtLeastGLES(3, 2) || gl.IsAtLeastGL(3, 2); diff --git a/src/dawn_native/opengl/DeviceGL.h b/src/dawn_native/opengl/DeviceGL.h index d9ec9c024f..07d1473d21 100644 --- a/src/dawn_native/opengl/DeviceGL.h +++ b/src/dawn_native/opengl/DeviceGL.h @@ -34,11 +34,13 @@ namespace dawn_native { namespace opengl { class Device : public DeviceBase { public: - Device(AdapterBase* adapter, - const DeviceDescriptor* descriptor, - const OpenGLFunctions& functions); + static ResultOrError Create(AdapterBase* adapter, + const DeviceDescriptor* descriptor, + const OpenGLFunctions& functions); ~Device(); + MaybeError Initialize(); + // Contains all the OpenGL entry points, glDoFoo is called via device->gl.DoFoo. const OpenGLFunctions gl; @@ -63,6 +65,10 @@ namespace dawn_native { namespace opengl { uint64_t size) override; private: + Device(AdapterBase* adapter, + const DeviceDescriptor* descriptor, + const OpenGLFunctions& functions); + ResultOrError CreateBindGroupImpl( const BindGroupDescriptor* descriptor) override; ResultOrError CreateBindGroupLayoutImpl( diff --git a/src/dawn_native/vulkan/AdapterVk.cpp b/src/dawn_native/vulkan/AdapterVk.cpp index d138cef565..9cbe82d010 100644 --- a/src/dawn_native/vulkan/AdapterVk.cpp +++ b/src/dawn_native/vulkan/AdapterVk.cpp @@ -76,9 +76,7 @@ namespace dawn_native { namespace vulkan { } ResultOrError Adapter::CreateDeviceImpl(const DeviceDescriptor* descriptor) { - std::unique_ptr device = std::make_unique(this, descriptor); - DAWN_TRY(device->Initialize()); - return device.release(); + return Device::Create(this, descriptor); } }} // namespace dawn_native::vulkan diff --git a/src/dawn_native/vulkan/DeviceVk.cpp b/src/dawn_native/vulkan/DeviceVk.cpp index 6ee6b5153c..8df4bdac8a 100644 --- a/src/dawn_native/vulkan/DeviceVk.cpp +++ b/src/dawn_native/vulkan/DeviceVk.cpp @@ -44,6 +44,13 @@ namespace dawn_native { namespace vulkan { + // static + ResultOrError Device::Create(Adapter* adapter, const DeviceDescriptor* descriptor) { + Ref device = AcquireRef(new Device(adapter, descriptor)); + DAWN_TRY(device->Initialize()); + return device.Detach(); + } + Device::Device(Adapter* adapter, const DeviceDescriptor* descriptor) : DeviceBase(adapter, descriptor) { InitTogglesFromDriver(); @@ -83,7 +90,7 @@ namespace dawn_native { namespace vulkan { // the decision if it is not applicable. ApplyDepth24PlusS8Toggle(); - return {}; + return DeviceBase::Initialize(); } Device::~Device() { diff --git a/src/dawn_native/vulkan/DeviceVk.h b/src/dawn_native/vulkan/DeviceVk.h index a4445f26f7..b403995633 100644 --- a/src/dawn_native/vulkan/DeviceVk.h +++ b/src/dawn_native/vulkan/DeviceVk.h @@ -43,7 +43,7 @@ namespace dawn_native { namespace vulkan { class Device : public DeviceBase { public: - Device(Adapter* adapter, const DeviceDescriptor* descriptor); + static ResultOrError Create(Adapter* adapter, const DeviceDescriptor* descriptor); ~Device(); MaybeError Initialize(); @@ -101,6 +101,8 @@ namespace dawn_native { namespace vulkan { ResourceMemoryAllocator* GetResourceMemoryAllocatorForTesting() const; private: + Device(Adapter* adapter, const DeviceDescriptor* descriptor); + ResultOrError CreateBindGroupImpl( const BindGroupDescriptor* descriptor) override; ResultOrError CreateBindGroupLayoutImpl(