1 // Copyright 2017 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "utils/BackendBinding.h"
16 
17 #include "common/Compiler.h"
18 
19 #include "GLFW/glfw3.h"
20 
21 #if defined(DAWN_ENABLE_BACKEND_OPENGL)
22 #    include "dawn_native/OpenGLBackend.h"
23 #endif  // defined(DAWN_ENABLE_BACKEND_OPENGL)
24 
25 namespace utils {
26 
27 #if defined(DAWN_ENABLE_BACKEND_D3D12)
28     BackendBinding* CreateD3D12Binding(GLFWwindow* window, WGPUDevice device);
29 #endif
30 #if defined(DAWN_ENABLE_BACKEND_METAL)
31     BackendBinding* CreateMetalBinding(GLFWwindow* window, WGPUDevice device);
32 #endif
33 #if defined(DAWN_ENABLE_BACKEND_NULL)
34     BackendBinding* CreateNullBinding(GLFWwindow* window, WGPUDevice device);
35 #endif
36 #if defined(DAWN_ENABLE_BACKEND_OPENGL)
37     BackendBinding* CreateOpenGLBinding(GLFWwindow* window, WGPUDevice device);
38 #endif
39 #if defined(DAWN_ENABLE_BACKEND_VULKAN)
40     BackendBinding* CreateVulkanBinding(GLFWwindow* window, WGPUDevice device);
41 #endif
42 
BackendBinding(GLFWwindow * window,WGPUDevice device)43     BackendBinding::BackendBinding(GLFWwindow* window, WGPUDevice device)
44         : mWindow(window), mDevice(device) {
45     }
46 
DiscoverAdapter(dawn_native::Instance * instance,GLFWwindow * window,wgpu::BackendType type)47     void DiscoverAdapter(dawn_native::Instance* instance,
48                          GLFWwindow* window,
49                          wgpu::BackendType type) {
50         DAWN_UNUSED(type);
51         DAWN_UNUSED(window);
52 
53         if (type == wgpu::BackendType::OpenGL) {
54 #if defined(DAWN_ENABLE_BACKEND_OPENGL)
55             glfwMakeContextCurrent(window);
56             dawn_native::opengl::AdapterDiscoveryOptions adapterOptions;
57             adapterOptions.getProc = reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress);
58             instance->DiscoverAdapters(&adapterOptions);
59 #endif  // defined(DAWN_ENABLE_BACKEND_OPENGL)
60         } else {
61             instance->DiscoverDefaultAdapters();
62         }
63     }
64 
CreateBinding(wgpu::BackendType type,GLFWwindow * window,WGPUDevice device)65     BackendBinding* CreateBinding(wgpu::BackendType type, GLFWwindow* window, WGPUDevice device) {
66         switch (type) {
67 #if defined(DAWN_ENABLE_BACKEND_D3D12)
68             case wgpu::BackendType::D3D12:
69                 return CreateD3D12Binding(window, device);
70 #endif
71 
72 #if defined(DAWN_ENABLE_BACKEND_METAL)
73             case wgpu::BackendType::Metal:
74                 return CreateMetalBinding(window, device);
75 #endif
76 
77 #if defined(DAWN_ENABLE_BACKEND_NULL)
78             case wgpu::BackendType::Null:
79                 return CreateNullBinding(window, device);
80 #endif
81 
82 #if defined(DAWN_ENABLE_BACKEND_OPENGL)
83             case wgpu::BackendType::OpenGL:
84                 return CreateOpenGLBinding(window, device);
85 #endif
86 
87 #if defined(DAWN_ENABLE_BACKEND_VULKAN)
88             case wgpu::BackendType::Vulkan:
89                 return CreateVulkanBinding(window, device);
90 #endif
91 
92             default:
93                 return nullptr;
94         }
95     }
96 
97 }  // namespace utils
98