mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-21 18:59:21 +00:00
dawn_native: Add Instance and Adapters
New objects are introduced to control what happens before device creation in dawn_native: - Instance: a connection from the application to dawn_native that is used for dependency injection and to discover adapters. - Adapters: represents the possibility of device creation for a specific (GPU, backend) pair. - BackendConnection: an internal object that standardizes the interface between the frontend and backends. The BackendConnection interface is implemented for the Null backend and stubbed out in other backends. This allows this change to port the ValidationTests to use the new Instance and Adapters concept and deal with other backends later. BUG=dawn:29 Change-Id: I19719a9342b4af091accc0c02fb6b9697eadde7b Reviewed-on: https://dawn-review.googlesource.com/c/3500 Reviewed-by: Stephen White <senorblanco@chromium.org> Reviewed-by: Kai Ninomiya <kainino@chromium.org> Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
committed by
Commit Bot service account
parent
e9212dfe30
commit
ac67fec1c9
119
src/dawn_native/Instance.cpp
Normal file
119
src/dawn_native/Instance.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
// Copyright 2018 The Dawn Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "dawn_native/Instance.h"
|
||||
|
||||
#include "common/Assert.h"
|
||||
#include "dawn_native/ErrorData.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace dawn_native {
|
||||
|
||||
// Forward definitions of each backend's "Connect" function that creates new BackendConnection.
|
||||
// Conditionally compiled declarations are used to avoid using static constructors instead.
|
||||
#if defined(DAWN_ENABLE_BACKEND_D3D12)
|
||||
namespace d3d12 {
|
||||
BackendConnection* Connect(InstanceBase* instance);
|
||||
}
|
||||
#endif // defined(DAWN_ENABLE_BACKEND_D3D12)
|
||||
#if defined(DAWN_ENABLE_BACKEND_METAL)
|
||||
namespace metal {
|
||||
BackendConnection* Connect(InstanceBase* instance);
|
||||
}
|
||||
#endif // defined(DAWN_ENABLE_BACKEND_METAL)
|
||||
#if defined(DAWN_ENABLE_BACKEND_NULL)
|
||||
namespace null {
|
||||
BackendConnection* Connect(InstanceBase* instance);
|
||||
}
|
||||
#endif // defined(DAWN_ENABLE_BACKEND_NULL)
|
||||
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
|
||||
namespace opengl {
|
||||
BackendConnection* Connect(InstanceBase* instance);
|
||||
}
|
||||
#endif // defined(DAWN_ENABLE_BACKEND_OPENGL)
|
||||
#if defined(DAWN_ENABLE_BACKEND_VULKAN)
|
||||
namespace vulkan {
|
||||
BackendConnection* Connect(InstanceBase* instance);
|
||||
}
|
||||
#endif // defined(DAWN_ENABLE_BACKEND_VULKAN)
|
||||
|
||||
// InstanceBase
|
||||
|
||||
void InstanceBase::DiscoverDefaultAdapters() {
|
||||
EnsureBackendConnections();
|
||||
|
||||
// Query and merge all default adapters for all backends
|
||||
for (std::unique_ptr<BackendConnection>& backend : mBackends) {
|
||||
std::vector<std::unique_ptr<AdapterBase>> backendAdapters =
|
||||
backend->DiscoverDefaultAdapters();
|
||||
|
||||
for (std::unique_ptr<AdapterBase>& adapter : backendAdapters) {
|
||||
ASSERT(adapter->GetBackendType() == backend->GetType());
|
||||
ASSERT(adapter->GetInstance() == this);
|
||||
mAdapters.push_back(std::move(adapter));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<std::unique_ptr<AdapterBase>>& InstanceBase::GetAdapters() const {
|
||||
return mAdapters;
|
||||
}
|
||||
|
||||
void InstanceBase::EnsureBackendConnections() {
|
||||
if (mBackendsConnected) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto Register = [this](BackendConnection* connection, BackendType expectedType) {
|
||||
if (connection != nullptr) {
|
||||
ASSERT(connection->GetType() == expectedType);
|
||||
ASSERT(connection->GetInstance() == this);
|
||||
mBackends.push_back(std::unique_ptr<BackendConnection>(connection));
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(DAWN_ENABLE_BACKEND_D3D12)
|
||||
Register(d3d12::Connect(this), BackendType::D3D12);
|
||||
#endif // defined(DAWN_ENABLE_BACKEND_D3D12)
|
||||
#if defined(DAWN_ENABLE_BACKEND_METAL)
|
||||
Register(metal::Connect(this), BackendType::Metal);
|
||||
#endif // defined(DAWN_ENABLE_BACKEND_METAL)
|
||||
#if defined(DAWN_ENABLE_BACKEND_NULL)
|
||||
Register(null::Connect(this), BackendType::Null);
|
||||
#endif // defined(DAWN_ENABLE_BACKEND_NULL)
|
||||
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
|
||||
Register(opengl::Connect(this), BackendType::OpenGL);
|
||||
#endif // defined(DAWN_ENABLE_BACKEND_OPENGL)
|
||||
#if defined(DAWN_ENABLE_BACKEND_VULKAN)
|
||||
Register(vulkan::Connect(this), BackendType::Vulkan);
|
||||
#endif // defined(DAWN_ENABLE_BACKEND_VULKAN)
|
||||
|
||||
mBackendsConnected = true;
|
||||
}
|
||||
|
||||
bool InstanceBase::ConsumedError(MaybeError maybeError) {
|
||||
if (maybeError.IsError()) {
|
||||
ErrorData* error = maybeError.AcquireError();
|
||||
|
||||
ASSERT(error != nullptr);
|
||||
std::cout << error->GetMessage() << std::endl;
|
||||
delete error;
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace dawn_native
|
||||
Reference in New Issue
Block a user