Simplify BackendBinding following adapters.

It doesn't need to care about device creation anymore, except for the
GLFW window hints and creating a GL context to discover the adapter.

Also remove the non-adapter GetPCIInfo.

BUG=dawn:29

Change-Id: I9bc8232536a55d2f973463ae0f2e0548dfc35456
Reviewed-on: https://dawn-review.googlesource.com/c/4381
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
This commit is contained in:
Corentin Wallez
2019-02-12 15:48:15 +00:00
committed by Commit Bot service account
parent 5987c4e839
commit bb5696bcd3
14 changed files with 139 additions and 170 deletions

View File

@@ -14,55 +14,92 @@
#include "utils/BackendBinding.h"
#include "common/Assert.h"
#include "common/Compiler.h"
#include "GLFW/glfw3.h"
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
# include "dawn_native/OpenGLBackend.h"
#endif // defined(DAWN_ENABLE_BACKEND_OPENGL)
namespace utils {
#if defined(DAWN_ENABLE_BACKEND_D3D12)
BackendBinding* CreateD3D12Binding();
BackendBinding* CreateD3D12Binding(GLFWwindow* window, dawnDevice device);
#endif
#if defined(DAWN_ENABLE_BACKEND_METAL)
BackendBinding* CreateMetalBinding();
BackendBinding* CreateMetalBinding(GLFWwindow* window, dawnDevice device);
#endif
#if defined(DAWN_ENABLE_BACKEND_NULL)
BackendBinding* CreateNullBinding();
BackendBinding* CreateNullBinding(GLFWwindow* window, dawnDevice device);
#endif
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
BackendBinding* CreateOpenGLBinding();
BackendBinding* CreateOpenGLBinding(GLFWwindow* window, dawnDevice device);
#endif
#if defined(DAWN_ENABLE_BACKEND_VULKAN)
BackendBinding* CreateVulkanBinding();
BackendBinding* CreateVulkanBinding(GLFWwindow* window, dawnDevice device);
#endif
void BackendBinding::SetWindow(GLFWwindow* window) {
mWindow = window;
BackendBinding::BackendBinding(GLFWwindow* window, dawnDevice device)
: mWindow(window), mDevice(device) {
}
BackendBinding* CreateBinding(dawn_native::BackendType type) {
void SetupGLFWWindowHintsForBackend(dawn_native::BackendType type) {
if (type == dawn_native::BackendType::OpenGL) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
} else {
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
}
}
void DiscoverAdapter(dawn_native::Instance* instance,
GLFWwindow* window,
dawn_native::BackendType type) {
DAWN_UNUSED(type);
DAWN_UNUSED(window);
if (type == dawn_native::BackendType::OpenGL) {
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
glfwMakeContextCurrent(window);
dawn_native::opengl::AdapterDiscoveryOptions adapterOptions;
adapterOptions.getProc = reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress);
instance->DiscoverAdapters(&adapterOptions);
#endif // defined(DAWN_ENABLE_BACKEND_OPENGL)
} else {
instance->DiscoverDefaultAdapters();
}
}
BackendBinding* CreateBinding(dawn_native::BackendType type,
GLFWwindow* window,
dawnDevice device) {
switch (type) {
#if defined(DAWN_ENABLE_BACKEND_D3D12)
case dawn_native::BackendType::D3D12:
return CreateD3D12Binding();
return CreateD3D12Binding(window, device);
#endif
#if defined(DAWN_ENABLE_BACKEND_METAL)
case dawn_native::BackendType::Metal:
return CreateMetalBinding();
return CreateMetalBinding(window, device);
#endif
#if defined(DAWN_ENABLE_BACKEND_NULL)
case dawn_native::BackendType::Null:
return CreateNullBinding();
return CreateNullBinding(window, device);
#endif
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
case dawn_native::BackendType::OpenGL:
return CreateOpenGLBinding();
return CreateOpenGLBinding(window, device);
#endif
#if defined(DAWN_ENABLE_BACKEND_VULKAN)
case dawn_native::BackendType::Vulkan:
return CreateVulkanBinding();
return CreateVulkanBinding(window, device);
#endif
default:

View File

@@ -15,11 +15,10 @@
#ifndef UTILS_BACKENDBINDING_H_
#define UTILS_BACKENDBINDING_H_
#include <dawn_native/DawnNative.h>
#include "dawn/dawncpp.h"
#include "dawn_native/DawnNative.h"
struct GLFWwindow;
typedef struct dawnProcTable_s dawnProcTable;
typedef struct dawnDeviceImpl* dawnDevice;
namespace utils {
@@ -27,18 +26,23 @@ namespace utils {
public:
virtual ~BackendBinding() = default;
virtual void SetupGLFWWindowHints() = 0;
virtual dawnDevice CreateDevice() = 0;
virtual uint64_t GetSwapChainImplementation() = 0;
virtual dawnTextureFormat GetPreferredSwapChainTextureFormat() = 0;
void SetWindow(GLFWwindow* window);
protected:
BackendBinding(GLFWwindow* window, dawnDevice device);
GLFWwindow* mWindow = nullptr;
dawnDevice mDevice = nullptr;
};
BackendBinding* CreateBinding(dawn_native::BackendType type);
void SetupGLFWWindowHintsForBackend(dawn_native::BackendType type);
void DiscoverAdapter(dawn_native::Instance* instance,
GLFWwindow* window,
dawn_native::BackendType type);
BackendBinding* CreateBinding(dawn_native::BackendType type,
GLFWwindow* window,
dawnDevice device);
} // namespace utils

View File

@@ -27,30 +27,14 @@ namespace utils {
class D3D12Binding : public BackendBinding {
public:
void SetupGLFWWindowHints() override {
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
}
dawnDevice CreateDevice() override {
// Make an instance and find a D3D12 adapter
mInstance = std::make_unique<dawn_native::Instance>();
mInstance->DiscoverDefaultAdapters();
std::vector<dawn_native::Adapter> adapters = mInstance->GetAdapters();
for (dawn_native::Adapter adapter : adapters) {
if (adapter.GetBackendType() == dawn_native::BackendType::D3D12) {
mBackendDevice = adapter.CreateDevice();
return mBackendDevice;
}
}
UNREACHABLE();
D3D12Binding(GLFWwindow* window, dawnDevice device) : BackendBinding(window, device) {
}
uint64_t GetSwapChainImplementation() override {
if (mSwapchainImpl.userData == nullptr) {
HWND win32Window = glfwGetWin32Window(mWindow);
mSwapchainImpl =
dawn_native::d3d12::CreateNativeSwapChainImpl(mBackendDevice, win32Window);
dawn_native::d3d12::CreateNativeSwapChainImpl(mDevice, win32Window);
}
return reinterpret_cast<uint64_t>(&mSwapchainImpl);
}
@@ -61,13 +45,11 @@ namespace utils {
}
private:
std::unique_ptr<dawn_native::Instance> mInstance;
dawnDevice mBackendDevice = nullptr;
dawnSwapChainImplementation mSwapchainImpl = {};
};
BackendBinding* CreateD3D12Binding() {
return new D3D12Binding;
BackendBinding* CreateD3D12Binding(GLFWwindow* window, dawnDevice device) {
return new D3D12Binding(window, device);
}
} // namespace utils

View File

@@ -110,26 +110,7 @@ namespace utils {
class MetalBinding : public BackendBinding {
public:
void SetupGLFWWindowHints() override {
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
}
dawnDevice CreateDevice() override {
// Make an instance and find a Metal adapter
mInstance = std::make_unique<dawn_native::Instance>();
mInstance->DiscoverDefaultAdapters();
std::vector<dawn_native::Adapter> adapters = mInstance->GetAdapters();
for (dawn_native::Adapter adapter : adapters) {
if (adapter.GetBackendType() == dawn_native::BackendType::Metal) {
dawnDevice device = adapter.CreateDevice();
mMetalDevice = dawn_native::metal::GetMetalDevice(device);
return device;
}
}
UNREACHABLE();
return {};
MetalBinding(GLFWwindow* window, dawnDevice device) : BackendBinding(window, device) {
}
uint64_t GetSwapChainImplementation() override {
@@ -145,12 +126,10 @@ namespace utils {
}
private:
std::unique_ptr<dawn_native::Instance> mInstance;
id<MTLDevice> mMetalDevice = nil;
dawnSwapChainImplementation mSwapchainImpl = {};
};
BackendBinding* CreateMetalBinding() {
return new MetalBinding;
BackendBinding* CreateMetalBinding(GLFWwindow* window, dawnDevice device) {
return new MetalBinding(window, device);
}
}

View File

@@ -23,23 +23,9 @@ namespace utils {
class NullBinding : public BackendBinding {
public:
void SetupGLFWWindowHints() override {
NullBinding(GLFWwindow* window, dawnDevice device) : BackendBinding(window, device) {
}
dawnDevice CreateDevice() override {
// Make an instance and find the null adapter
mInstance = std::make_unique<dawn_native::Instance>();
mInstance->DiscoverDefaultAdapters();
std::vector<dawn_native::Adapter> adapters = mInstance->GetAdapters();
for (dawn_native::Adapter adapter : adapters) {
if (adapter.GetBackendType() == dawn_native::BackendType::Null) {
return adapter.CreateDevice();
}
}
UNREACHABLE();
return {};
}
uint64_t GetSwapChainImplementation() override {
if (mSwapchainImpl.userData == nullptr) {
mSwapchainImpl = dawn_native::null::CreateNativeSwapChainImpl();
@@ -51,12 +37,11 @@ namespace utils {
}
private:
std::unique_ptr<dawn_native::Instance> mInstance;
dawnSwapChainImplementation mSwapchainImpl = {};
};
BackendBinding* CreateNullBinding() {
return new NullBinding;
BackendBinding* CreateNullBinding(GLFWwindow* window, dawnDevice device) {
return new NullBinding(window, device);
}
} // namespace utils

View File

@@ -95,36 +95,9 @@ namespace utils {
class OpenGLBinding : public BackendBinding {
public:
void SetupGLFWWindowHints() override {
#if defined(DAWN_PLATFORM_APPLE)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#else
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
}
dawnDevice CreateDevice() override {
glfwMakeContextCurrent(mWindow);
OpenGLBinding(GLFWwindow* window, dawnDevice device) : BackendBinding(window, device) {
// Load the GL entry points in our copy of the glad static library
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress));
// Make an instance and "discover" an OpenGL adapter with glfw's getProc
mInstance = std::make_unique<dawn_native::Instance>();
dawn_native::opengl::AdapterDiscoveryOptions adapterOptions;
adapterOptions.getProc = reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress);
mInstance->DiscoverAdapters(&adapterOptions);
std::vector<dawn_native::Adapter> adapters = mInstance->GetAdapters();
ASSERT(adapters.size() == 1);
return adapters[0].CreateDevice();
}
uint64_t GetSwapChainImplementation() override {
@@ -139,12 +112,11 @@ namespace utils {
}
private:
std::unique_ptr<dawn_native::Instance> mInstance;
dawnSwapChainImplementation mSwapchainImpl = {};
};
BackendBinding* CreateOpenGLBinding() {
return new OpenGLBinding;
BackendBinding* CreateOpenGLBinding(GLFWwindow* window, dawnDevice device) {
return new OpenGLBinding(window, device);
}
} // namespace utils

View File

@@ -26,25 +26,9 @@ namespace utils {
class VulkanBinding : public BackendBinding {
public:
void SetupGLFWWindowHints() override {
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
VulkanBinding(GLFWwindow* window, dawnDevice device) : BackendBinding(window, device) {
}
dawnDevice CreateDevice() override {
// Make an instance and find a Vulkan adapter
mInstance = std::make_unique<dawn_native::Instance>();
mInstance->DiscoverDefaultAdapters();
std::vector<dawn_native::Adapter> adapters = mInstance->GetAdapters();
for (dawn_native::Adapter adapter : adapters) {
if (adapter.GetBackendType() == dawn_native::BackendType::Vulkan) {
mDevice = adapter.CreateDevice();
return mDevice;
}
}
UNREACHABLE();
return {};
}
uint64_t GetSwapChainImplementation() override {
if (mSwapchainImpl.userData == nullptr) {
VkSurfaceKHR surface = VK_NULL_HANDLE;
@@ -63,13 +47,11 @@ namespace utils {
}
private:
std::unique_ptr<dawn_native::Instance> mInstance;
dawnDevice mDevice = nullptr;
dawnSwapChainImplementation mSwapchainImpl = {};
};
BackendBinding* CreateVulkanBinding() {
return new VulkanBinding;
BackendBinding* CreateVulkanBinding(GLFWwindow* window, dawnDevice device) {
return new VulkanBinding(window, device);
}
} // namespace utils