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(AdapterBase* adapter, const DeviceDescriptor* descriptor)
: 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);
: 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<DeviceBase::Caches>();
mErrorScopeTracker = std::make_unique<ErrorScopeTracker>(this);
mFenceSignalTracker = std::make_unique<FenceSignalTracker>(this);
mDynamicUploader = std::make_unique<DynamicUploader>(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

View File

@ -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<DynamicUploader> mDynamicUploader;

View File

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

View File

@ -43,12 +43,16 @@
namespace dawn_native { namespace d3d12 {
Device::Device(Adapter* adapter, const DeviceDescriptor* descriptor)
: DeviceBase(adapter, descriptor) {
InitTogglesFromDriver();
// 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();
}
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() {

View File

@ -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<Device*> 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<BindGroupBase*> CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) override;
ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(

View File

@ -206,8 +206,9 @@ namespace dawn_native { namespace metal {
private:
ResultOrError<DeviceBase*> 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]) {

View File

@ -36,9 +36,13 @@ namespace dawn_native { namespace metal {
class Device : public DeviceBase {
public:
Device(AdapterBase* adapter, id<MTLDevice> mtlDevice, const DeviceDescriptor* descriptor);
static ResultOrError<Device*> Create(AdapterBase* adapter,
id<MTLDevice> 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> mtlDevice, const DeviceDescriptor* descriptor);
ResultOrError<BindGroupBase*> CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) override;
ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(

View File

@ -38,6 +38,15 @@
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,
id<MTLDevice> 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;

View File

@ -43,7 +43,7 @@ namespace dawn_native { namespace null {
}
ResultOrError<DeviceBase*> 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*> Device::Create(Adapter* adapter, const DeviceDescriptor* descriptor) {
Ref<Device> 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<BindGroupBase*> Device::CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) {
return new BindGroup(this, descriptor);

View File

@ -83,9 +83,11 @@ namespace dawn_native { namespace null {
class Device : public DeviceBase {
public:
Device(Adapter* adapter, const DeviceDescriptor* descriptor);
static ResultOrError<Device*> 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<BindGroupBase*> CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) override;
ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(

View File

@ -182,7 +182,7 @@ namespace dawn_native { namespace opengl {
ResultOrError<DeviceBase*> 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() {

View File

@ -33,18 +33,32 @@
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,
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);

View File

@ -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<Device*> 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<BindGroupBase*> CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) override;
ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(

View File

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

View File

@ -44,6 +44,13 @@
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)
: 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() {

View File

@ -43,7 +43,7 @@ namespace dawn_native { namespace vulkan {
class Device : public DeviceBase {
public:
Device(Adapter* adapter, const DeviceDescriptor* descriptor);
static ResultOrError<Device*> 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<BindGroupBase*> CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) override;
ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(