• 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, DawnDevice device);
29 #endif
30 #if defined(DAWN_ENABLE_BACKEND_METAL)
31     BackendBinding* CreateMetalBinding(GLFWwindow* window, DawnDevice device);
32 #endif
33 #if defined(DAWN_ENABLE_BACKEND_NULL)
34     BackendBinding* CreateNullBinding(GLFWwindow* window, DawnDevice device);
35 #endif
36 #if defined(DAWN_ENABLE_BACKEND_OPENGL)
37     BackendBinding* CreateOpenGLBinding(GLFWwindow* window, DawnDevice device);
38 #endif
39 #if defined(DAWN_ENABLE_BACKEND_VULKAN)
40     BackendBinding* CreateVulkanBinding(GLFWwindow* window, DawnDevice device);
41 #endif
42 
BackendBinding(GLFWwindow * window,DawnDevice device)43     BackendBinding::BackendBinding(GLFWwindow* window, DawnDevice device)
44         : mWindow(window), mDevice(device) {
45     }
46 
SetupGLFWWindowHintsForBackend(dawn_native::BackendType type)47     void SetupGLFWWindowHintsForBackend(dawn_native::BackendType type) {
48         if (type == dawn_native::BackendType::OpenGL) {
49             glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
50             glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
51             glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
52             glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
53         } else {
54             glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
55         }
56     }
57 
DiscoverAdapter(dawn_native::Instance * instance,GLFWwindow * window,dawn_native::BackendType type)58     void DiscoverAdapter(dawn_native::Instance* instance,
59                          GLFWwindow* window,
60                          dawn_native::BackendType type) {
61         DAWN_UNUSED(type);
62         DAWN_UNUSED(window);
63 
64         if (type == dawn_native::BackendType::OpenGL) {
65 #if defined(DAWN_ENABLE_BACKEND_OPENGL)
66             glfwMakeContextCurrent(window);
67             dawn_native::opengl::AdapterDiscoveryOptions adapterOptions;
68             adapterOptions.getProc = reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress);
69             instance->DiscoverAdapters(&adapterOptions);
70 #endif  // defined(DAWN_ENABLE_BACKEND_OPENGL)
71         } else {
72             instance->DiscoverDefaultAdapters();
73         }
74     }
75 
CreateBinding(dawn_native::BackendType type,GLFWwindow * window,DawnDevice device)76     BackendBinding* CreateBinding(dawn_native::BackendType type,
77                                   GLFWwindow* window,
78                                   DawnDevice device) {
79         switch (type) {
80 #if defined(DAWN_ENABLE_BACKEND_D3D12)
81             case dawn_native::BackendType::D3D12:
82                 return CreateD3D12Binding(window, device);
83 #endif
84 
85 #if defined(DAWN_ENABLE_BACKEND_METAL)
86             case dawn_native::BackendType::Metal:
87                 return CreateMetalBinding(window, device);
88 #endif
89 
90 #if defined(DAWN_ENABLE_BACKEND_NULL)
91             case dawn_native::BackendType::Null:
92                 return CreateNullBinding(window, device);
93 #endif
94 
95 #if defined(DAWN_ENABLE_BACKEND_OPENGL)
96             case dawn_native::BackendType::OpenGL:
97                 return CreateOpenGLBinding(window, device);
98 #endif
99 
100 #if defined(DAWN_ENABLE_BACKEND_VULKAN)
101             case dawn_native::BackendType::Vulkan:
102                 return CreateVulkanBinding(window, device);
103 #endif
104 
105             default:
106                 return nullptr;
107         }
108     }
109 
110 }  // namespace utils
111