dawn_native: Factor getting procs into a single function.

Now the backends only expose the creation of devices. Getting the procs
is moved to DawnNative that will over time more backend-agnostic APIs.
This commit is contained in:
Corentin Wallez 2018-08-02 22:27:57 +02:00 committed by Corentin Wallez
parent f9f44ae289
commit dcb71a131c
23 changed files with 98 additions and 84 deletions

View File

@ -22,6 +22,7 @@
#include <dawn/dawn.h> #include <dawn/dawn.h>
#include <dawn/dawncpp.h> #include <dawn/dawncpp.h>
#include <dawn/dawn_wsi.h> #include <dawn/dawn_wsi.h>
#include <dawn_native/DawnNative.h>
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include <cstring> #include <cstring>
@ -84,9 +85,8 @@ dawn::Device CreateCppDawnDevice() {
binding->SetWindow(window); binding->SetWindow(window);
dawnDevice backendDevice; dawnDevice backendDevice = binding->CreateDevice();
dawnProcTable backendProcs; dawnProcTable backendProcs = dawn_native::GetProcs();
binding->GetProcAndDevice(&backendProcs, &backendDevice);
dawnDevice cDevice = nullptr; dawnDevice cDevice = nullptr;
dawnProcTable procs; dawnProcTable procs;

View File

@ -15,6 +15,7 @@
#include "common/Assert.h" #include "common/Assert.h"
#include "dawn_native/dawn_platform.h" #include "dawn_native/dawn_platform.h"
#include "dawn_native/DawnNative.h"
#include "dawn_native/ErrorData.h" #include "dawn_native/ErrorData.h"
#include "dawn_native/ValidationUtils_autogen.h" #include "dawn_native/ValidationUtils_autogen.h"
@ -156,7 +157,7 @@ namespace dawn_native {
{% endfor %} {% endfor %}
} }
dawnProcTable GetProcs() { dawnProcTable GetProcsAutogen() {
dawnProcTable table; dawnProcTable table;
{% for type in by_category["object"] %} {% for type in by_category["object"] %}
{% for method in native_methods(type) %} {% for method in native_methods(type) %}

View File

@ -335,6 +335,7 @@ list(APPEND DAWN_NATIVE_SOURCES
${DAWN_NATIVE_DIR}/ComputePipeline.h ${DAWN_NATIVE_DIR}/ComputePipeline.h
${DAWN_NATIVE_DIR}/CommandBufferStateTracker.cpp ${DAWN_NATIVE_DIR}/CommandBufferStateTracker.cpp
${DAWN_NATIVE_DIR}/CommandBufferStateTracker.h ${DAWN_NATIVE_DIR}/CommandBufferStateTracker.h
${DAWN_NATIVE_DIR}/DawnNative.cpp
${DAWN_NATIVE_DIR}/DepthStencilState.cpp ${DAWN_NATIVE_DIR}/DepthStencilState.cpp
${DAWN_NATIVE_DIR}/DepthStencilState.h ${DAWN_NATIVE_DIR}/DepthStencilState.h
${DAWN_NATIVE_DIR}/Device.cpp ${DAWN_NATIVE_DIR}/Device.cpp
@ -371,6 +372,7 @@ list(APPEND DAWN_NATIVE_SOURCES
${DAWN_NATIVE_DIR}/Texture.h ${DAWN_NATIVE_DIR}/Texture.h
${DAWN_NATIVE_DIR}/ToBackend.h ${DAWN_NATIVE_DIR}/ToBackend.h
${DAWN_NATIVE_INCLUDE_DIR}/dawn_native_export.h ${DAWN_NATIVE_INCLUDE_DIR}/dawn_native_export.h
${DAWN_NATIVE_INCLUDE_DIR}/DawnNative.h
) )
# We want to produce both a static and a shared libdawn_native library. The static one is for # We want to produce both a static and a shared libdawn_native library. The static one is for

View File

@ -0,0 +1,27 @@
// 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/DawnNative.h"
// Contains the entry-points into dawn_native
namespace dawn_native {
dawnProcTable GetProcsAutogen();
dawnProcTable GetProcs() {
return GetProcsAutogen();
}
} // namespace dawn_native

View File

@ -39,16 +39,10 @@
#include "dawn_native/d3d12/SwapChainD3D12.h" #include "dawn_native/d3d12/SwapChainD3D12.h"
#include "dawn_native/d3d12/TextureD3D12.h" #include "dawn_native/d3d12/TextureD3D12.h"
namespace dawn_native {
dawnProcTable GetProcs();
} // namespace dawn_native
namespace dawn_native { namespace d3d12 { namespace dawn_native { namespace d3d12 {
void Init(dawnProcTable* procs, dawnDevice* device) { dawnDevice CreateDevice() {
*device = nullptr; return reinterpret_cast<dawnDevice>(new Device());
*procs = GetProcs();
*device = reinterpret_cast<dawnDevice>(new Device());
} }
dawnSwapChainImplementation CreateNativeSwapChainImpl(dawnDevice device, HWND window) { dawnSwapChainImplementation CreateNativeSwapChainImpl(dawnDevice device, HWND window) {

View File

@ -35,17 +35,10 @@
#include <unistd.h> #include <unistd.h>
namespace dawn_native {
dawnProcTable GetProcs();
} // namespace dawn_native
namespace dawn_native { namespace metal { namespace dawn_native { namespace metal {
void Init(id<MTLDevice> metalDevice, dawnProcTable* procs, dawnDevice* device) { dawnDevice CreateDevice(id<MTLDevice> metalDevice) {
*device = nullptr; return reinterpret_cast<dawnDevice>(new Device(metalDevice));
*procs = GetProcs();
*device = reinterpret_cast<dawnDevice>(new Device(metalDevice));
} }
// Device // Device

View File

@ -19,15 +19,10 @@
#include <spirv-cross/spirv_cross.hpp> #include <spirv-cross/spirv_cross.hpp>
namespace dawn_native {
dawnProcTable GetProcs();
} // namespace dawn_native
namespace dawn_native { namespace null { namespace dawn_native { namespace null {
void Init(dawnProcTable* procs, dawnDevice* device) { dawnDevice CreateDevice() {
*procs = GetProcs(); return reinterpret_cast<dawnDevice>(new Device);
*device = reinterpret_cast<dawnDevice>(new Device);
} }
// Device // Device

View File

@ -32,23 +32,16 @@
#include "dawn_native/opengl/SwapChainGL.h" #include "dawn_native/opengl/SwapChainGL.h"
#include "dawn_native/opengl/TextureGL.h" #include "dawn_native/opengl/TextureGL.h"
namespace dawn_native {
dawnProcTable GetProcs();
} // namespace dawn_native
namespace dawn_native { namespace opengl { namespace dawn_native { namespace opengl {
void Init(void* (*getProc)(const char*), dawnProcTable* procs, dawnDevice* device) { dawnDevice CreateDevice(void* (*getProc)(const char*)) {
*device = nullptr;
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(getProc)); gladLoadGLLoader(reinterpret_cast<GLADloadproc>(getProc));
*procs = GetProcs();
*device = reinterpret_cast<dawnDevice>(new Device);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST); glEnable(GL_SCISSOR_TEST);
glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX); glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
return reinterpret_cast<dawnDevice>(new Device);
} }
// Device // Device

View File

@ -51,17 +51,10 @@ const char kVulkanLibName[] = "vulkan-1.dll";
# error "Unimplemented Vulkan backend platform" # error "Unimplemented Vulkan backend platform"
#endif #endif
namespace dawn_native {
dawnProcTable GetProcs();
} // namespace dawn_native
namespace dawn_native { namespace vulkan { namespace dawn_native { namespace vulkan {
void Init(dawnProcTable* procs, dawnDevice CreateDevice(const std::vector<const char*>& requiredInstanceExtensions) {
dawnDevice* device, return reinterpret_cast<dawnDevice>(new Device(requiredInstanceExtensions));
const std::vector<const char*>& requiredInstanceExtensions) {
*procs = GetProcs();
*device = reinterpret_cast<dawnDevice>(new Device(requiredInstanceExtensions));
} }
VkInstance GetInstance(dawnDevice device) { VkInstance GetInstance(dawnDevice device) {

View File

@ -22,7 +22,7 @@
#include <windows.h> #include <windows.h>
namespace dawn_native { namespace d3d12 { namespace dawn_native { namespace d3d12 {
DAWN_NATIVE_EXPORT void Init(dawnProcTable* procs, dawnDevice* device); DAWN_NATIVE_EXPORT dawnDevice CreateDevice();
DAWN_NATIVE_EXPORT dawnSwapChainImplementation CreateNativeSwapChainImpl(dawnDevice device, DAWN_NATIVE_EXPORT dawnSwapChainImplementation CreateNativeSwapChainImpl(dawnDevice device,
HWND window); HWND window);

View File

@ -0,0 +1,28 @@
// 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.
#ifndef DAWNNATIVE_DAWNNATIVE_H_
#define DAWNNATIVE_DAWNNATIVE_H_
#include <dawn/dawn.h>
#include <dawn_native/dawn_native_export.h>
namespace dawn_native {
// Backend-agnostic API for dawn_native
DAWN_NATIVE_EXPORT dawnProcTable GetProcs();
} // namespace dawn_native
#endif // DAWNNATIVE_DAWNNATIVE_H_

View File

@ -23,9 +23,7 @@
#import <QuartzCore/CAMetalLayer.h> #import <QuartzCore/CAMetalLayer.h>
namespace dawn_native { namespace metal { namespace dawn_native { namespace metal {
DAWN_NATIVE_EXPORT void Init(id<MTLDevice> metalDevice, DAWN_NATIVE_EXPORT dawnDevice CreateDevice(id<MTLDevice> metalDevice);
dawnProcTable* procs,
dawnDevice* device);
DAWN_NATIVE_EXPORT void SetNextDrawable(dawnDevice device, id<CAMetalDrawable> drawable); DAWN_NATIVE_EXPORT void SetNextDrawable(dawnDevice device, id<CAMetalDrawable> drawable);
DAWN_NATIVE_EXPORT void Present(dawnDevice device); DAWN_NATIVE_EXPORT void Present(dawnDevice device);
}} // namespace dawn_native::metal }} // namespace dawn_native::metal

View File

@ -19,7 +19,7 @@
#include <dawn_native/dawn_native_export.h> #include <dawn_native/dawn_native_export.h>
namespace dawn_native { namespace null { namespace dawn_native { namespace null {
DAWN_NATIVE_EXPORT void Init(dawnProcTable* procs, dawnDevice* device); DAWN_NATIVE_EXPORT dawnDevice CreateDevice();
}} // namespace dawn_native::null }} // namespace dawn_native::null
#endif // DAWNNATIVE_NULLBACKEND_H_ #endif // DAWNNATIVE_NULLBACKEND_H_

View File

@ -19,9 +19,7 @@
#include <dawn_native/dawn_native_export.h> #include <dawn_native/dawn_native_export.h>
namespace dawn_native { namespace opengl { namespace dawn_native { namespace opengl {
DAWN_NATIVE_EXPORT void Init(void* (*getProc)(const char*), DAWN_NATIVE_EXPORT dawnDevice CreateDevice(void* (*getProc)(const char*));
dawnProcTable* procs,
dawnDevice* device);
}} // namespace dawn_native::opengl }} // namespace dawn_native::opengl
#endif // DAWNNATIVE_OPENGLBACKEND_H_ #endif // DAWNNATIVE_OPENGLBACKEND_H_

View File

@ -24,9 +24,8 @@
#include <vector> #include <vector>
namespace dawn_native { namespace vulkan { namespace dawn_native { namespace vulkan {
DAWN_NATIVE_EXPORT void Init(dawnProcTable* procs, DAWN_NATIVE_EXPORT dawnDevice
dawnDevice* device, CreateDevice(const std::vector<const char*>& requiredInstanceExtensions);
const std::vector<const char*>& requiredInstanceExtensions);
DAWN_NATIVE_EXPORT VkInstance GetInstance(dawnDevice device); DAWN_NATIVE_EXPORT VkInstance GetInstance(dawnDevice device);

View File

@ -17,6 +17,7 @@
#include "common/Assert.h" #include "common/Assert.h"
#include "common/Constants.h" #include "common/Constants.h"
#include "common/Math.h" #include "common/Math.h"
#include "dawn_native/DawnNative.h"
#include "dawn_wire/Wire.h" #include "dawn_wire/Wire.h"
#include "utils/BackendBinding.h" #include "utils/BackendBinding.h"
#include "utils/DawnHelpers.h" #include "utils/DawnHelpers.h"
@ -136,9 +137,8 @@ void DawnTest::SetUp() {
mBinding->SetWindow(testWindow); mBinding->SetWindow(testWindow);
dawnDevice backendDevice; dawnDevice backendDevice = mBinding->CreateDevice();
dawnProcTable backendProcs; dawnProcTable backendProcs = dawn_native::GetProcs();
mBinding->GetProcAndDevice(&backendProcs, &backendDevice);
// Choose whether to use the backend procs and devices directly, or set up the wire. // Choose whether to use the backend procs and devices directly, or set up the wire.
dawnDevice cDevice = nullptr; dawnDevice cDevice = nullptr;

View File

@ -15,6 +15,8 @@
#include "tests/unittests/validation/ValidationTest.h" #include "tests/unittests/validation/ValidationTest.h"
#include "dawn/dawn.h" #include "dawn/dawn.h"
#include "dawn_native/DawnNative.h"
#include "dawn_native/NullBackend.h"
namespace dawn_native { namespace dawn_native {
namespace null { namespace null {
@ -23,9 +25,8 @@ namespace dawn_native {
} }
ValidationTest::ValidationTest() { ValidationTest::ValidationTest() {
dawnProcTable procs; dawnProcTable procs = dawn_native::GetProcs();
dawnDevice cDevice; dawnDevice cDevice = dawn_native::null::CreateDevice();
dawn_native::null::Init(&procs, &cDevice);
dawnSetProcs(&procs); dawnSetProcs(&procs);
device = dawn::Device::Acquire(cDevice); device = dawn::Device::Acquire(cDevice);

View File

@ -36,7 +36,7 @@ namespace utils {
virtual ~BackendBinding() = default; virtual ~BackendBinding() = default;
virtual void SetupGLFWWindowHints() = 0; virtual void SetupGLFWWindowHints() = 0;
virtual void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) = 0; virtual dawnDevice CreateDevice() = 0;
virtual uint64_t GetSwapChainImplementation() = 0; virtual uint64_t GetSwapChainImplementation() = 0;
virtual dawnTextureFormat GetPreferredSwapChainTextureFormat() = 0; virtual dawnTextureFormat GetPreferredSwapChainTextureFormat() = 0;

View File

@ -29,9 +29,8 @@ namespace utils {
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
} }
void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) override { dawnDevice CreateDevice() override {
dawn_native::d3d12::Init(procs, device); return dawn_native::d3d12::CreateDevice();
mBackendDevice = *device;
} }
uint64_t GetSwapChainImplementation() override { uint64_t GetSwapChainImplementation() override {

View File

@ -105,11 +105,9 @@ namespace utils {
void SetupGLFWWindowHints() override { void SetupGLFWWindowHints() override {
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
} }
void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) override { dawnDevice CreateDevice() override {
mMetalDevice = MTLCreateSystemDefaultDevice(); mMetalDevice = MTLCreateSystemDefaultDevice();
return dawn_native::metal::CreateDevice(mMetalDevice);
dawn_native::metal::Init(mMetalDevice, procs, device);
mBackendDevice = *device;
} }
uint64_t GetSwapChainImplementation() override { uint64_t GetSwapChainImplementation() override {
@ -126,7 +124,6 @@ namespace utils {
private: private:
id<MTLDevice> mMetalDevice = nil; id<MTLDevice> mMetalDevice = nil;
dawnDevice mBackendDevice = nullptr;
dawnSwapChainImplementation mSwapchainImpl = {}; dawnSwapChainImplementation mSwapchainImpl = {};
}; };

View File

@ -22,8 +22,8 @@ namespace utils {
public: public:
void SetupGLFWWindowHints() override { void SetupGLFWWindowHints() override {
} }
void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) override { dawnDevice CreateDevice() override {
dawn_native::null::Init(procs, device); return dawn_native::null::CreateDevice();
} }
uint64_t GetSwapChainImplementation() override { uint64_t GetSwapChainImplementation() override {
return 0; return 0;

View File

@ -108,15 +108,13 @@ namespace utils {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif #endif
} }
void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) override { dawnDevice CreateDevice() override {
glfwMakeContextCurrent(mWindow); glfwMakeContextCurrent(mWindow);
// Load the GL entry points in our copy of the glad static library // Load the GL entry points in our copy of the glad static library
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress)); gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress));
dawn_native::opengl::Init(reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress), return dawn_native::opengl::CreateDevice(
procs, device); reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress));
mBackendDevice = *device;
} }
uint64_t GetSwapChainImplementation() override { uint64_t GetSwapChainImplementation() override {
@ -131,7 +129,6 @@ namespace utils {
} }
private: private:
dawnDevice mBackendDevice = nullptr;
dawnSwapChainImplementation mSwapchainImpl = {}; dawnSwapChainImplementation mSwapchainImpl = {};
}; };

View File

@ -53,15 +53,14 @@ namespace utils {
void SetupGLFWWindowHints() override { void SetupGLFWWindowHints() override {
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
} }
void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) override { dawnDevice CreateDevice() override {
uint32_t extensionCount = 0; uint32_t extensionCount = 0;
const char** glfwInstanceExtensions = const char** glfwInstanceExtensions =
glfwGetRequiredInstanceExtensions(&extensionCount); glfwGetRequiredInstanceExtensions(&extensionCount);
std::vector<const char*> requiredExtensions(glfwInstanceExtensions, std::vector<const char*> requiredExtensions(glfwInstanceExtensions,
glfwInstanceExtensions + extensionCount); glfwInstanceExtensions + extensionCount);
dawn_native::vulkan::Init(procs, device, requiredExtensions); return dawn_native::vulkan::CreateDevice(requiredExtensions);
mDevice = *device;
} }
uint64_t GetSwapChainImplementation() override { uint64_t GetSwapChainImplementation() override {
if (mSwapchainImpl.userData == nullptr) { if (mSwapchainImpl.userData == nullptr) {