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 <cwallez@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Corentin Wallez 2020-04-07 15:10:17 +00:00 committed by Commit Bot service account
parent 2d10e954ce
commit 09ee5eb499
16 changed files with 106 additions and 37 deletions

View File

@ -66,20 +66,13 @@ namespace dawn_native {
// DeviceBase // DeviceBase
DeviceBase::DeviceBase(AdapterBase* adapter, const DeviceDescriptor* descriptor) DeviceBase::DeviceBase(AdapterBase* adapter, const DeviceDescriptor* descriptor)
: mAdapter(adapter), : mAdapter(adapter) {
mRootErrorScope(AcquireRef(new ErrorScope())),
mCurrentErrorScope(mRootErrorScope.Get()) {
mCaches = std::make_unique<DeviceBase::Caches>();
mErrorScopeTracker = std::make_unique<ErrorScopeTracker>(this);
mFenceSignalTracker = std::make_unique<FenceSignalTracker>(this);
mDynamicUploader = std::make_unique<DynamicUploader>(this);
if (descriptor != nullptr) { if (descriptor != nullptr) {
ApplyToggleOverrides(descriptor); ApplyToggleOverrides(descriptor);
ApplyExtensions(descriptor); ApplyExtensions(descriptor);
} }
mFormatTable = BuildFormatTable(this);
mFormatTable = BuildFormatTable(this);
SetDefaultToggles(); SetDefaultToggles();
} }
@ -97,6 +90,18 @@ namespace dawn_native {
ASSERT(mCaches->shaderModules.empty()); ASSERT(mCaches->shaderModules.empty());
} }
MaybeError DeviceBase::Initialize() {
mRootErrorScope = AcquireRef(new ErrorScope());
mCurrentErrorScope = mRootErrorScope.Get();
mCaches = std::make_unique<DeviceBase::Caches>();
mErrorScopeTracker = std::make_unique<ErrorScopeTracker>(this);
mFenceSignalTracker = std::make_unique<FenceSignalTracker>(this);
mDynamicUploader = std::make_unique<DynamicUploader>(this);
return {};
}
void DeviceBase::BaseDestructor() { void DeviceBase::BaseDestructor() {
if (mLossStatus != LossStatus::Alive) { if (mLossStatus != LossStatus::Alive) {
// if device is already lost, we may still have fences and error scopes to clear since // if device is already lost, we may still have fences and error scopes to clear since

View File

@ -196,6 +196,8 @@ namespace dawn_native {
protected: protected:
void SetToggle(Toggle toggle, bool isEnabled); void SetToggle(Toggle toggle, bool isEnabled);
void ForceSetToggle(Toggle toggle, bool isEnabled); void ForceSetToggle(Toggle toggle, bool isEnabled);
MaybeError Initialize();
void BaseDestructor(); void BaseDestructor();
std::unique_ptr<DynamicUploader> mDynamicUploader; std::unique_ptr<DynamicUploader> mDynamicUploader;

View File

@ -95,9 +95,7 @@ namespace dawn_native { namespace d3d12 {
} }
ResultOrError<DeviceBase*> Adapter::CreateDeviceImpl(const DeviceDescriptor* descriptor) { ResultOrError<DeviceBase*> Adapter::CreateDeviceImpl(const DeviceDescriptor* descriptor) {
std::unique_ptr<Device> device = std::make_unique<Device>(this, descriptor); return Device::Create(this, descriptor);
DAWN_TRY(device->Initialize());
return device.release();
} }
}} // namespace dawn_native::d3d12 }} // namespace dawn_native::d3d12

View File

@ -43,12 +43,16 @@
namespace dawn_native { namespace d3d12 { namespace dawn_native { namespace d3d12 {
Device::Device(Adapter* adapter, const DeviceDescriptor* descriptor) // static
: DeviceBase(adapter, descriptor) { ResultOrError<Device*> Device::Create(Adapter* adapter, const DeviceDescriptor* descriptor) {
InitTogglesFromDriver(); Ref<Device> device = AcquireRef(new Device(adapter, descriptor));
DAWN_TRY(device->Initialize());
return device.Detach();
} }
MaybeError Device::Initialize() { MaybeError Device::Initialize() {
InitTogglesFromDriver();
mD3d12Device = ToBackend(GetAdapter())->GetDevice(); mD3d12Device = ToBackend(GetAdapter())->GetDevice();
ASSERT(mD3d12Device != nullptr); ASSERT(mD3d12Device != nullptr);
@ -110,7 +114,7 @@ namespace dawn_native { namespace d3d12 {
GetD3D12Device()->CreateCommandSignature(&programDesc, NULL, GetD3D12Device()->CreateCommandSignature(&programDesc, NULL,
IID_PPV_ARGS(&mDrawIndexedIndirectSignature)); IID_PPV_ARGS(&mDrawIndexedIndirectSignature));
return {}; return DeviceBase::Initialize();
} }
Device::~Device() { Device::~Device() {

View File

@ -45,7 +45,7 @@ namespace dawn_native { namespace d3d12 {
// Definition of backend types // Definition of backend types
class Device : public DeviceBase { class Device : public DeviceBase {
public: public:
Device(Adapter* adapter, const DeviceDescriptor* descriptor); static ResultOrError<Device*> Create(Adapter* adapter, const DeviceDescriptor* descriptor);
~Device(); ~Device();
MaybeError Initialize(); MaybeError Initialize();
@ -112,6 +112,8 @@ namespace dawn_native { namespace d3d12 {
void InitTogglesFromDriver(); void InitTogglesFromDriver();
private: private:
using DeviceBase::DeviceBase;
ResultOrError<BindGroupBase*> CreateBindGroupImpl( ResultOrError<BindGroupBase*> CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) override; const BindGroupDescriptor* descriptor) override;
ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl( ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(

View File

@ -206,8 +206,9 @@ namespace dawn_native { namespace metal {
private: private:
ResultOrError<DeviceBase*> CreateDeviceImpl(const DeviceDescriptor* descriptor) override { ResultOrError<DeviceBase*> CreateDeviceImpl(const DeviceDescriptor* descriptor) override {
return {new Device(this, mDevice, descriptor)}; return Device::Create(this, mDevice, descriptor);
} }
void InitializeSupportedExtensions() { void InitializeSupportedExtensions() {
#if defined(DAWN_PLATFORM_MACOS) #if defined(DAWN_PLATFORM_MACOS)
if ([mDevice supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v1]) { if ([mDevice supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v1]) {

View File

@ -36,9 +36,13 @@ namespace dawn_native { namespace metal {
class Device : public DeviceBase { class Device : public DeviceBase {
public: public:
Device(AdapterBase* adapter, id<MTLDevice> mtlDevice, const DeviceDescriptor* descriptor); static ResultOrError<Device*> Create(AdapterBase* adapter,
id<MTLDevice> mtlDevice,
const DeviceDescriptor* descriptor);
~Device(); ~Device();
MaybeError Initialize();
CommandBufferBase* CreateCommandBuffer(CommandEncoder* encoder, CommandBufferBase* CreateCommandBuffer(CommandEncoder* encoder,
const CommandBufferDescriptor* descriptor) override; const CommandBufferDescriptor* descriptor) override;
@ -68,6 +72,8 @@ namespace dawn_native { namespace metal {
uint64_t size) override; uint64_t size) override;
private: private:
Device(AdapterBase* adapter, id<MTLDevice> mtlDevice, const DeviceDescriptor* descriptor);
ResultOrError<BindGroupBase*> CreateBindGroupImpl( ResultOrError<BindGroupBase*> CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) override; const BindGroupDescriptor* descriptor) override;
ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl( ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(

View File

@ -38,6 +38,15 @@
namespace dawn_native { namespace metal { namespace dawn_native { namespace metal {
// static
ResultOrError<Device*> Device::Create(AdapterBase* adapter,
id<MTLDevice> mtlDevice,
const DeviceDescriptor* descriptor) {
Ref<Device> device = AcquireRef(new Device(adapter, mtlDevice, descriptor));
DAWN_TRY(device->Initialize());
return device.Detach();
}
Device::Device(AdapterBase* adapter, Device::Device(AdapterBase* adapter,
id<MTLDevice> mtlDevice, id<MTLDevice> mtlDevice,
const DeviceDescriptor* descriptor) const DeviceDescriptor* descriptor)
@ -46,15 +55,19 @@ namespace dawn_native { namespace metal {
mMapTracker(new MapRequestTracker(this)), mMapTracker(new MapRequestTracker(this)),
mCompletedSerial(0) { mCompletedSerial(0) {
[mMtlDevice retain]; [mMtlDevice retain];
mCommandQueue = [mMtlDevice newCommandQueue];
InitTogglesFromDriver();
} }
Device::~Device() { Device::~Device() {
BaseDestructor(); BaseDestructor();
} }
MaybeError Device::Initialize() {
InitTogglesFromDriver();
mCommandQueue = [mMtlDevice newCommandQueue];
return DeviceBase::Initialize();
}
void Device::InitTogglesFromDriver() { void Device::InitTogglesFromDriver() {
{ {
bool haveStoreAndMSAAResolve = false; bool haveStoreAndMSAAResolve = false;

View File

@ -43,7 +43,7 @@ namespace dawn_native { namespace null {
} }
ResultOrError<DeviceBase*> Adapter::CreateDeviceImpl(const DeviceDescriptor* descriptor) { ResultOrError<DeviceBase*> Adapter::CreateDeviceImpl(const DeviceDescriptor* descriptor) {
return {new Device(this, descriptor)}; return Device::Create(this, descriptor);
} }
class Backend : public BackendConnection { class Backend : public BackendConnection {
@ -78,8 +78,11 @@ namespace dawn_native { namespace null {
// Device // Device
Device::Device(Adapter* adapter, const DeviceDescriptor* descriptor) // static
: DeviceBase(adapter, descriptor) { ResultOrError<Device*> Device::Create(Adapter* adapter, const DeviceDescriptor* descriptor) {
Ref<Device> device = AcquireRef(new Device(adapter, descriptor));
DAWN_TRY(device->Initialize());
return device.Detach();
} }
Device::~Device() { Device::~Device() {
@ -89,6 +92,10 @@ namespace dawn_native { namespace null {
ASSERT(mMemoryUsage == 0); ASSERT(mMemoryUsage == 0);
} }
MaybeError Device::Initialize() {
return DeviceBase::Initialize();
}
ResultOrError<BindGroupBase*> Device::CreateBindGroupImpl( ResultOrError<BindGroupBase*> Device::CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) { const BindGroupDescriptor* descriptor) {
return new BindGroup(this, descriptor); return new BindGroup(this, descriptor);

View File

@ -83,9 +83,11 @@ namespace dawn_native { namespace null {
class Device : public DeviceBase { class Device : public DeviceBase {
public: public:
Device(Adapter* adapter, const DeviceDescriptor* descriptor); static ResultOrError<Device*> Create(Adapter* adapter, const DeviceDescriptor* descriptor);
~Device(); ~Device();
MaybeError Initialize();
CommandBufferBase* CreateCommandBuffer(CommandEncoder* encoder, CommandBufferBase* CreateCommandBuffer(CommandEncoder* encoder,
const CommandBufferDescriptor* descriptor) override; const CommandBufferDescriptor* descriptor) override;
@ -108,6 +110,8 @@ namespace dawn_native { namespace null {
void DecrementMemoryUsage(size_t bytes); void DecrementMemoryUsage(size_t bytes);
private: private:
using DeviceBase::DeviceBase;
ResultOrError<BindGroupBase*> CreateBindGroupImpl( ResultOrError<BindGroupBase*> CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) override; const BindGroupDescriptor* descriptor) override;
ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl( ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(

View File

@ -182,7 +182,7 @@ namespace dawn_native { namespace opengl {
ResultOrError<DeviceBase*> CreateDeviceImpl(const DeviceDescriptor* descriptor) override { ResultOrError<DeviceBase*> CreateDeviceImpl(const DeviceDescriptor* descriptor) override {
// There is no limit on the number of devices created from this adapter because they can // There is no limit on the number of devices created from this adapter because they can
// all share the same backing OpenGL context. // all share the same backing OpenGL context.
return {new Device(this, descriptor, mFunctions)}; return Device::Create(this, descriptor, mFunctions);
} }
void InitializeSupportedExtensions() { void InitializeSupportedExtensions() {

View File

@ -33,18 +33,32 @@
namespace dawn_native { namespace opengl { namespace dawn_native { namespace opengl {
// static
ResultOrError<Device*> Device::Create(AdapterBase* adapter,
const DeviceDescriptor* descriptor,
const OpenGLFunctions& functions) {
Ref<Device> device = AcquireRef(new Device(adapter, descriptor, functions));
DAWN_TRY(device->Initialize());
return device.Detach();
}
Device::Device(AdapterBase* adapter, Device::Device(AdapterBase* adapter,
const DeviceDescriptor* descriptor, const DeviceDescriptor* descriptor,
const OpenGLFunctions& functions) const OpenGLFunctions& functions)
: DeviceBase(adapter, descriptor), gl(functions) { : DeviceBase(adapter, descriptor), gl(functions) {
InitTogglesFromDriver();
mFormatTable = BuildGLFormatTable();
} }
Device::~Device() { Device::~Device() {
BaseDestructor(); BaseDestructor();
} }
MaybeError Device::Initialize() {
InitTogglesFromDriver();
mFormatTable = BuildGLFormatTable();
return DeviceBase::Initialize();
}
void Device::InitTogglesFromDriver() { void Device::InitTogglesFromDriver() {
bool supportsBaseVertex = gl.IsAtLeastGLES(3, 2) || gl.IsAtLeastGL(3, 2); bool supportsBaseVertex = gl.IsAtLeastGLES(3, 2) || gl.IsAtLeastGL(3, 2);

View File

@ -34,11 +34,13 @@ namespace dawn_native { namespace opengl {
class Device : public DeviceBase { class Device : public DeviceBase {
public: public:
Device(AdapterBase* adapter, static ResultOrError<Device*> Create(AdapterBase* adapter,
const DeviceDescriptor* descriptor, const DeviceDescriptor* descriptor,
const OpenGLFunctions& functions); const OpenGLFunctions& functions);
~Device(); ~Device();
MaybeError Initialize();
// Contains all the OpenGL entry points, glDoFoo is called via device->gl.DoFoo. // Contains all the OpenGL entry points, glDoFoo is called via device->gl.DoFoo.
const OpenGLFunctions gl; const OpenGLFunctions gl;
@ -63,6 +65,10 @@ namespace dawn_native { namespace opengl {
uint64_t size) override; uint64_t size) override;
private: private:
Device(AdapterBase* adapter,
const DeviceDescriptor* descriptor,
const OpenGLFunctions& functions);
ResultOrError<BindGroupBase*> CreateBindGroupImpl( ResultOrError<BindGroupBase*> CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) override; const BindGroupDescriptor* descriptor) override;
ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl( ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(

View File

@ -76,9 +76,7 @@ namespace dawn_native { namespace vulkan {
} }
ResultOrError<DeviceBase*> Adapter::CreateDeviceImpl(const DeviceDescriptor* descriptor) { ResultOrError<DeviceBase*> Adapter::CreateDeviceImpl(const DeviceDescriptor* descriptor) {
std::unique_ptr<Device> device = std::make_unique<Device>(this, descriptor); return Device::Create(this, descriptor);
DAWN_TRY(device->Initialize());
return device.release();
} }
}} // namespace dawn_native::vulkan }} // namespace dawn_native::vulkan

View File

@ -44,6 +44,13 @@
namespace dawn_native { namespace vulkan { namespace dawn_native { namespace vulkan {
// static
ResultOrError<Device*> Device::Create(Adapter* adapter, const DeviceDescriptor* descriptor) {
Ref<Device> device = AcquireRef(new Device(adapter, descriptor));
DAWN_TRY(device->Initialize());
return device.Detach();
}
Device::Device(Adapter* adapter, const DeviceDescriptor* descriptor) Device::Device(Adapter* adapter, const DeviceDescriptor* descriptor)
: DeviceBase(adapter, descriptor) { : DeviceBase(adapter, descriptor) {
InitTogglesFromDriver(); InitTogglesFromDriver();
@ -83,7 +90,7 @@ namespace dawn_native { namespace vulkan {
// the decision if it is not applicable. // the decision if it is not applicable.
ApplyDepth24PlusS8Toggle(); ApplyDepth24PlusS8Toggle();
return {}; return DeviceBase::Initialize();
} }
Device::~Device() { Device::~Device() {

View File

@ -43,7 +43,7 @@ namespace dawn_native { namespace vulkan {
class Device : public DeviceBase { class Device : public DeviceBase {
public: public:
Device(Adapter* adapter, const DeviceDescriptor* descriptor); static ResultOrError<Device*> Create(Adapter* adapter, const DeviceDescriptor* descriptor);
~Device(); ~Device();
MaybeError Initialize(); MaybeError Initialize();
@ -101,6 +101,8 @@ namespace dawn_native { namespace vulkan {
ResourceMemoryAllocator* GetResourceMemoryAllocatorForTesting() const; ResourceMemoryAllocator* GetResourceMemoryAllocatorForTesting() const;
private: private:
Device(Adapter* adapter, const DeviceDescriptor* descriptor);
ResultOrError<BindGroupBase*> CreateBindGroupImpl( ResultOrError<BindGroupBase*> CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) override; const BindGroupDescriptor* descriptor) override;
ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl( ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(