• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 || type == wgpu::BackendType::OpenGLES) {
54 #if defined(DAWN_ENABLE_BACKEND_OPENGL)
55             glfwMakeContextCurrent(window);
56             auto getProc = reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress);
57             if (type == wgpu::BackendType::OpenGL) {
58                 dawn_native::opengl::AdapterDiscoveryOptions adapterOptions;
59                 adapterOptions.getProc = getProc;
60                 instance->DiscoverAdapters(&adapterOptions);
61             } else {
62                 dawn_native::opengl::AdapterDiscoveryOptionsES adapterOptions;
63                 adapterOptions.getProc = getProc;
64                 instance->DiscoverAdapters(&adapterOptions);
65             }
66 #endif  // defined(DAWN_ENABLE_BACKEND_OPENGL)
67         } else {
68             instance->DiscoverDefaultAdapters();
69         }
70     }
71 
CreateBinding(wgpu::BackendType type,GLFWwindow * window,WGPUDevice device)72     BackendBinding* CreateBinding(wgpu::BackendType type, GLFWwindow* window, WGPUDevice device) {
73         switch (type) {
74 #if defined(DAWN_ENABLE_BACKEND_D3D12)
75             case wgpu::BackendType::D3D12:
76                 return CreateD3D12Binding(window, device);
77 #endif
78 
79 #if defined(DAWN_ENABLE_BACKEND_METAL)
80             case wgpu::BackendType::Metal:
81                 return CreateMetalBinding(window, device);
82 #endif
83 
84 #if defined(DAWN_ENABLE_BACKEND_NULL)
85             case wgpu::BackendType::Null:
86                 return CreateNullBinding(window, device);
87 #endif
88 
89 #if defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
90             case wgpu::BackendType::OpenGL:
91                 return CreateOpenGLBinding(window, device);
92 #endif
93 
94 #if defined(DAWN_ENABLE_BACKEND_OPENGLES)
95             case wgpu::BackendType::OpenGLES:
96                 return CreateOpenGLBinding(window, device);
97 #endif
98 
99 #if defined(DAWN_ENABLE_BACKEND_VULKAN)
100             case wgpu::BackendType::Vulkan:
101                 return CreateVulkanBinding(window, device);
102 #endif
103 
104             default:
105                 return nullptr;
106         }
107     }
108 
109 }  // namespace utils
110