diff --git a/examples/SampleUtils.cpp b/examples/SampleUtils.cpp index 0b3c2d6ca1..d2ccd95679 100644 --- a/examples/SampleUtils.cpp +++ b/examples/SampleUtils.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "GLFW/glfw3.h" #include @@ -84,9 +85,8 @@ dawn::Device CreateCppDawnDevice() { binding->SetWindow(window); - dawnDevice backendDevice; - dawnProcTable backendProcs; - binding->GetProcAndDevice(&backendProcs, &backendDevice); + dawnDevice backendDevice = binding->CreateDevice(); + dawnProcTable backendProcs = dawn_native::GetProcs(); dawnDevice cDevice = nullptr; dawnProcTable procs; diff --git a/generator/templates/dawn_native/ProcTable.cpp b/generator/templates/dawn_native/ProcTable.cpp index 9ad84fc42f..f967ccb4f1 100644 --- a/generator/templates/dawn_native/ProcTable.cpp +++ b/generator/templates/dawn_native/ProcTable.cpp @@ -15,6 +15,7 @@ #include "common/Assert.h" #include "dawn_native/dawn_platform.h" +#include "dawn_native/DawnNative.h" #include "dawn_native/ErrorData.h" #include "dawn_native/ValidationUtils_autogen.h" @@ -156,7 +157,7 @@ namespace dawn_native { {% endfor %} } - dawnProcTable GetProcs() { + dawnProcTable GetProcsAutogen() { dawnProcTable table; {% for type in by_category["object"] %} {% for method in native_methods(type) %} diff --git a/src/dawn_native/CMakeLists.txt b/src/dawn_native/CMakeLists.txt index 4828d179bd..92db945d90 100644 --- a/src/dawn_native/CMakeLists.txt +++ b/src/dawn_native/CMakeLists.txt @@ -335,6 +335,7 @@ list(APPEND DAWN_NATIVE_SOURCES ${DAWN_NATIVE_DIR}/ComputePipeline.h ${DAWN_NATIVE_DIR}/CommandBufferStateTracker.cpp ${DAWN_NATIVE_DIR}/CommandBufferStateTracker.h + ${DAWN_NATIVE_DIR}/DawnNative.cpp ${DAWN_NATIVE_DIR}/DepthStencilState.cpp ${DAWN_NATIVE_DIR}/DepthStencilState.h ${DAWN_NATIVE_DIR}/Device.cpp @@ -371,6 +372,7 @@ list(APPEND DAWN_NATIVE_SOURCES ${DAWN_NATIVE_DIR}/Texture.h ${DAWN_NATIVE_DIR}/ToBackend.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 diff --git a/src/dawn_native/DawnNative.cpp b/src/dawn_native/DawnNative.cpp new file mode 100644 index 0000000000..b3efe843e7 --- /dev/null +++ b/src/dawn_native/DawnNative.cpp @@ -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 diff --git a/src/dawn_native/d3d12/DeviceD3D12.cpp b/src/dawn_native/d3d12/DeviceD3D12.cpp index 0d4b833e78..1d434d5663 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.cpp +++ b/src/dawn_native/d3d12/DeviceD3D12.cpp @@ -39,16 +39,10 @@ #include "dawn_native/d3d12/SwapChainD3D12.h" #include "dawn_native/d3d12/TextureD3D12.h" -namespace dawn_native { - dawnProcTable GetProcs(); -} // namespace dawn_native - namespace dawn_native { namespace d3d12 { - void Init(dawnProcTable* procs, dawnDevice* device) { - *device = nullptr; - *procs = GetProcs(); - *device = reinterpret_cast(new Device()); + dawnDevice CreateDevice() { + return reinterpret_cast(new Device()); } dawnSwapChainImplementation CreateNativeSwapChainImpl(dawnDevice device, HWND window) { diff --git a/src/dawn_native/metal/DeviceMTL.mm b/src/dawn_native/metal/DeviceMTL.mm index 2f8b5167d4..c8bf183158 100644 --- a/src/dawn_native/metal/DeviceMTL.mm +++ b/src/dawn_native/metal/DeviceMTL.mm @@ -35,17 +35,10 @@ #include -namespace dawn_native { - dawnProcTable GetProcs(); -} // namespace dawn_native - namespace dawn_native { namespace metal { - void Init(id metalDevice, dawnProcTable* procs, dawnDevice* device) { - *device = nullptr; - - *procs = GetProcs(); - *device = reinterpret_cast(new Device(metalDevice)); + dawnDevice CreateDevice(id metalDevice) { + return reinterpret_cast(new Device(metalDevice)); } // Device diff --git a/src/dawn_native/null/NullBackend.cpp b/src/dawn_native/null/NullBackend.cpp index 244ecb091f..b4c6badc15 100644 --- a/src/dawn_native/null/NullBackend.cpp +++ b/src/dawn_native/null/NullBackend.cpp @@ -19,15 +19,10 @@ #include -namespace dawn_native { - dawnProcTable GetProcs(); -} // namespace dawn_native - namespace dawn_native { namespace null { - void Init(dawnProcTable* procs, dawnDevice* device) { - *procs = GetProcs(); - *device = reinterpret_cast(new Device); + dawnDevice CreateDevice() { + return reinterpret_cast(new Device); } // Device diff --git a/src/dawn_native/opengl/DeviceGL.cpp b/src/dawn_native/opengl/DeviceGL.cpp index 67d51e699b..55fb1b1f71 100644 --- a/src/dawn_native/opengl/DeviceGL.cpp +++ b/src/dawn_native/opengl/DeviceGL.cpp @@ -32,23 +32,16 @@ #include "dawn_native/opengl/SwapChainGL.h" #include "dawn_native/opengl/TextureGL.h" -namespace dawn_native { - dawnProcTable GetProcs(); -} // namespace dawn_native - namespace dawn_native { namespace opengl { - void Init(void* (*getProc)(const char*), dawnProcTable* procs, dawnDevice* device) { - *device = nullptr; - + dawnDevice CreateDevice(void* (*getProc)(const char*)) { gladLoadGLLoader(reinterpret_cast(getProc)); - *procs = GetProcs(); - *device = reinterpret_cast(new Device); - glEnable(GL_DEPTH_TEST); glEnable(GL_SCISSOR_TEST); glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX); + + return reinterpret_cast(new Device); } // Device diff --git a/src/dawn_native/vulkan/DeviceVk.cpp b/src/dawn_native/vulkan/DeviceVk.cpp index 7365ed6a75..0fce56dda0 100644 --- a/src/dawn_native/vulkan/DeviceVk.cpp +++ b/src/dawn_native/vulkan/DeviceVk.cpp @@ -51,17 +51,10 @@ const char kVulkanLibName[] = "vulkan-1.dll"; # error "Unimplemented Vulkan backend platform" #endif -namespace dawn_native { - dawnProcTable GetProcs(); -} // namespace dawn_native - namespace dawn_native { namespace vulkan { - void Init(dawnProcTable* procs, - dawnDevice* device, - const std::vector& requiredInstanceExtensions) { - *procs = GetProcs(); - *device = reinterpret_cast(new Device(requiredInstanceExtensions)); + dawnDevice CreateDevice(const std::vector& requiredInstanceExtensions) { + return reinterpret_cast(new Device(requiredInstanceExtensions)); } VkInstance GetInstance(dawnDevice device) { diff --git a/src/include/dawn_native/D3D12Backend.h b/src/include/dawn_native/D3D12Backend.h index a3def2a5b3..ddc3f12fb3 100644 --- a/src/include/dawn_native/D3D12Backend.h +++ b/src/include/dawn_native/D3D12Backend.h @@ -22,7 +22,7 @@ #include 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, HWND window); diff --git a/src/include/dawn_native/DawnNative.h b/src/include/dawn_native/DawnNative.h new file mode 100644 index 0000000000..6a9fef9088 --- /dev/null +++ b/src/include/dawn_native/DawnNative.h @@ -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 +#include + +namespace dawn_native { + + // Backend-agnostic API for dawn_native + DAWN_NATIVE_EXPORT dawnProcTable GetProcs(); + +} // namespace dawn_native + +#endif // DAWNNATIVE_DAWNNATIVE_H_ diff --git a/src/include/dawn_native/MetalBackend.h b/src/include/dawn_native/MetalBackend.h index a2b6044dca..758868d1f3 100644 --- a/src/include/dawn_native/MetalBackend.h +++ b/src/include/dawn_native/MetalBackend.h @@ -23,9 +23,7 @@ #import namespace dawn_native { namespace metal { - DAWN_NATIVE_EXPORT void Init(id metalDevice, - dawnProcTable* procs, - dawnDevice* device); + DAWN_NATIVE_EXPORT dawnDevice CreateDevice(id metalDevice); DAWN_NATIVE_EXPORT void SetNextDrawable(dawnDevice device, id drawable); DAWN_NATIVE_EXPORT void Present(dawnDevice device); }} // namespace dawn_native::metal diff --git a/src/include/dawn_native/NullBackend.h b/src/include/dawn_native/NullBackend.h index 9a1b42adae..be7c3e715e 100644 --- a/src/include/dawn_native/NullBackend.h +++ b/src/include/dawn_native/NullBackend.h @@ -19,7 +19,7 @@ #include namespace dawn_native { namespace null { - DAWN_NATIVE_EXPORT void Init(dawnProcTable* procs, dawnDevice* device); + DAWN_NATIVE_EXPORT dawnDevice CreateDevice(); }} // namespace dawn_native::null #endif // DAWNNATIVE_NULLBACKEND_H_ diff --git a/src/include/dawn_native/OpenGLBackend.h b/src/include/dawn_native/OpenGLBackend.h index aa275cda5f..e31f7d38d5 100644 --- a/src/include/dawn_native/OpenGLBackend.h +++ b/src/include/dawn_native/OpenGLBackend.h @@ -19,9 +19,7 @@ #include namespace dawn_native { namespace opengl { - DAWN_NATIVE_EXPORT void Init(void* (*getProc)(const char*), - dawnProcTable* procs, - dawnDevice* device); + DAWN_NATIVE_EXPORT dawnDevice CreateDevice(void* (*getProc)(const char*)); }} // namespace dawn_native::opengl #endif // DAWNNATIVE_OPENGLBACKEND_H_ diff --git a/src/include/dawn_native/VulkanBackend.h b/src/include/dawn_native/VulkanBackend.h index c0aef2c1e7..e52e0cec04 100644 --- a/src/include/dawn_native/VulkanBackend.h +++ b/src/include/dawn_native/VulkanBackend.h @@ -24,9 +24,8 @@ #include namespace dawn_native { namespace vulkan { - DAWN_NATIVE_EXPORT void Init(dawnProcTable* procs, - dawnDevice* device, - const std::vector& requiredInstanceExtensions); + DAWN_NATIVE_EXPORT dawnDevice + CreateDevice(const std::vector& requiredInstanceExtensions); DAWN_NATIVE_EXPORT VkInstance GetInstance(dawnDevice device); diff --git a/src/tests/DawnTest.cpp b/src/tests/DawnTest.cpp index 168a88e14b..a523977d5a 100644 --- a/src/tests/DawnTest.cpp +++ b/src/tests/DawnTest.cpp @@ -17,6 +17,7 @@ #include "common/Assert.h" #include "common/Constants.h" #include "common/Math.h" +#include "dawn_native/DawnNative.h" #include "dawn_wire/Wire.h" #include "utils/BackendBinding.h" #include "utils/DawnHelpers.h" @@ -136,9 +137,8 @@ void DawnTest::SetUp() { mBinding->SetWindow(testWindow); - dawnDevice backendDevice; - dawnProcTable backendProcs; - mBinding->GetProcAndDevice(&backendProcs, &backendDevice); + dawnDevice backendDevice = mBinding->CreateDevice(); + dawnProcTable backendProcs = dawn_native::GetProcs(); // Choose whether to use the backend procs and devices directly, or set up the wire. dawnDevice cDevice = nullptr; diff --git a/src/tests/unittests/validation/ValidationTest.cpp b/src/tests/unittests/validation/ValidationTest.cpp index f3f7db2752..5eeb832770 100644 --- a/src/tests/unittests/validation/ValidationTest.cpp +++ b/src/tests/unittests/validation/ValidationTest.cpp @@ -15,6 +15,8 @@ #include "tests/unittests/validation/ValidationTest.h" #include "dawn/dawn.h" +#include "dawn_native/DawnNative.h" +#include "dawn_native/NullBackend.h" namespace dawn_native { namespace null { @@ -23,9 +25,8 @@ namespace dawn_native { } ValidationTest::ValidationTest() { - dawnProcTable procs; - dawnDevice cDevice; - dawn_native::null::Init(&procs, &cDevice); + dawnProcTable procs = dawn_native::GetProcs(); + dawnDevice cDevice = dawn_native::null::CreateDevice(); dawnSetProcs(&procs); device = dawn::Device::Acquire(cDevice); diff --git a/src/utils/BackendBinding.h b/src/utils/BackendBinding.h index c4f8fbffeb..622f8d7360 100644 --- a/src/utils/BackendBinding.h +++ b/src/utils/BackendBinding.h @@ -36,7 +36,7 @@ namespace utils { virtual ~BackendBinding() = default; virtual void SetupGLFWWindowHints() = 0; - virtual void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) = 0; + virtual dawnDevice CreateDevice() = 0; virtual uint64_t GetSwapChainImplementation() = 0; virtual dawnTextureFormat GetPreferredSwapChainTextureFormat() = 0; diff --git a/src/utils/D3D12Binding.cpp b/src/utils/D3D12Binding.cpp index 187446b71b..ab5126747e 100644 --- a/src/utils/D3D12Binding.cpp +++ b/src/utils/D3D12Binding.cpp @@ -29,9 +29,8 @@ namespace utils { glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); } - void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) override { - dawn_native::d3d12::Init(procs, device); - mBackendDevice = *device; + dawnDevice CreateDevice() override { + return dawn_native::d3d12::CreateDevice(); } uint64_t GetSwapChainImplementation() override { diff --git a/src/utils/MetalBinding.mm b/src/utils/MetalBinding.mm index 055c1a1121..8eaba1712b 100644 --- a/src/utils/MetalBinding.mm +++ b/src/utils/MetalBinding.mm @@ -105,11 +105,9 @@ namespace utils { void SetupGLFWWindowHints() override { glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); } - void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) override { + dawnDevice CreateDevice() override { mMetalDevice = MTLCreateSystemDefaultDevice(); - - dawn_native::metal::Init(mMetalDevice, procs, device); - mBackendDevice = *device; + return dawn_native::metal::CreateDevice(mMetalDevice); } uint64_t GetSwapChainImplementation() override { @@ -126,7 +124,6 @@ namespace utils { private: id mMetalDevice = nil; - dawnDevice mBackendDevice = nullptr; dawnSwapChainImplementation mSwapchainImpl = {}; }; diff --git a/src/utils/NullBinding.cpp b/src/utils/NullBinding.cpp index 0327f42550..558531e33c 100644 --- a/src/utils/NullBinding.cpp +++ b/src/utils/NullBinding.cpp @@ -22,8 +22,8 @@ namespace utils { public: void SetupGLFWWindowHints() override { } - void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) override { - dawn_native::null::Init(procs, device); + dawnDevice CreateDevice() override { + return dawn_native::null::CreateDevice(); } uint64_t GetSwapChainImplementation() override { return 0; diff --git a/src/utils/OpenGLBinding.cpp b/src/utils/OpenGLBinding.cpp index 037b7c2032..a0fbc002a6 100644 --- a/src/utils/OpenGLBinding.cpp +++ b/src/utils/OpenGLBinding.cpp @@ -108,15 +108,13 @@ namespace utils { glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif } - void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) override { + dawnDevice CreateDevice() override { glfwMakeContextCurrent(mWindow); // Load the GL entry points in our copy of the glad static library gladLoadGLLoader(reinterpret_cast(glfwGetProcAddress)); - dawn_native::opengl::Init(reinterpret_cast(glfwGetProcAddress), - procs, device); - - mBackendDevice = *device; + return dawn_native::opengl::CreateDevice( + reinterpret_cast(glfwGetProcAddress)); } uint64_t GetSwapChainImplementation() override { @@ -131,7 +129,6 @@ namespace utils { } private: - dawnDevice mBackendDevice = nullptr; dawnSwapChainImplementation mSwapchainImpl = {}; }; diff --git a/src/utils/VulkanBinding.cpp b/src/utils/VulkanBinding.cpp index 48bbce2507..2c3a794f40 100644 --- a/src/utils/VulkanBinding.cpp +++ b/src/utils/VulkanBinding.cpp @@ -53,15 +53,14 @@ namespace utils { void SetupGLFWWindowHints() override { glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); } - void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) override { + dawnDevice CreateDevice() override { uint32_t extensionCount = 0; const char** glfwInstanceExtensions = glfwGetRequiredInstanceExtensions(&extensionCount); std::vector requiredExtensions(glfwInstanceExtensions, glfwInstanceExtensions + extensionCount); - dawn_native::vulkan::Init(procs, device, requiredExtensions); - mDevice = *device; + return dawn_native::vulkan::CreateDevice(requiredExtensions); } uint64_t GetSwapChainImplementation() override { if (mSwapchainImpl.userData == nullptr) {