2020-01-15 13:14:12 +00:00
|
|
|
// Copyright 2020 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.
|
|
|
|
|
2022-04-19 20:38:44 +00:00
|
|
|
#include <cstdlib>
|
2022-08-14 15:59:25 +00:00
|
|
|
#include <memory>
|
2022-04-20 00:58:34 +00:00
|
|
|
#include <utility>
|
2022-04-19 20:38:44 +00:00
|
|
|
|
2020-01-15 13:14:12 +00:00
|
|
|
#include "GLFW/glfw3.h"
|
2022-02-04 12:51:25 +00:00
|
|
|
#include "dawn/common/Platform.h"
|
2022-08-14 15:59:25 +00:00
|
|
|
#include "webgpu/webgpu_glfw.h"
|
2020-01-15 13:14:12 +00:00
|
|
|
|
2022-06-08 19:52:42 +00:00
|
|
|
#if DAWN_PLATFORM_IS(WINDOWS)
|
2022-05-01 14:40:55 +00:00
|
|
|
#define GLFW_EXPOSE_NATIVE_WIN32
|
2022-06-01 09:30:50 +00:00
|
|
|
#endif
|
|
|
|
#if defined(DAWN_USE_X11)
|
2022-05-01 14:40:55 +00:00
|
|
|
#define GLFW_EXPOSE_NATIVE_X11
|
2020-01-15 13:14:12 +00:00
|
|
|
#endif
|
2022-06-01 09:30:50 +00:00
|
|
|
#if defined(DAWN_USE_WAYLAND)
|
|
|
|
#define GLFW_EXPOSE_NATIVE_WAYLAND
|
|
|
|
#endif
|
2020-01-15 13:14:12 +00:00
|
|
|
#include "GLFW/glfw3native.h"
|
|
|
|
|
2022-08-14 15:59:25 +00:00
|
|
|
namespace wgpu::glfw {
|
2020-01-15 13:14:12 +00:00
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
wgpu::Surface CreateSurfaceForWindow(const wgpu::Instance& instance, GLFWwindow* window) {
|
|
|
|
std::unique_ptr<wgpu::ChainedStruct> chainedDescriptor =
|
|
|
|
SetupWindowAndGetSurfaceDescriptor(window);
|
2020-01-15 13:14:12 +00:00
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
wgpu::SurfaceDescriptor descriptor;
|
|
|
|
descriptor.nextInChain = chainedDescriptor.get();
|
|
|
|
wgpu::Surface surface = instance.CreateSurface(&descriptor);
|
2020-01-15 13:14:12 +00:00
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
return surface;
|
|
|
|
}
|
2020-01-15 13:14:12 +00:00
|
|
|
|
2022-06-01 09:30:50 +00:00
|
|
|
// SetupWindowAndGetSurfaceDescriptorCocoa defined in GLFWUtils_metal.mm
|
|
|
|
std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptorCocoa(GLFWwindow* window);
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptor(GLFWwindow* window) {
|
2022-06-08 19:52:42 +00:00
|
|
|
#if DAWN_PLATFORM_IS(WINDOWS)
|
2022-06-10 00:13:13 +00:00
|
|
|
std::unique_ptr<wgpu::SurfaceDescriptorFromWindowsHWND> desc =
|
|
|
|
std::make_unique<wgpu::SurfaceDescriptorFromWindowsHWND>();
|
|
|
|
desc->hwnd = glfwGetWin32Window(window);
|
|
|
|
desc->hinstance = GetModuleHandle(nullptr);
|
|
|
|
return std::move(desc);
|
|
|
|
#elif defined(DAWN_ENABLE_BACKEND_METAL)
|
|
|
|
return SetupWindowAndGetSurfaceDescriptorCocoa(window);
|
|
|
|
#elif defined(DAWN_USE_WAYLAND) || defined(DAWN_USE_X11)
|
|
|
|
#if defined(GLFW_PLATFORM_WAYLAND) && defined(DAWN_USE_WAYLAND)
|
|
|
|
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND) {
|
|
|
|
std::unique_ptr<wgpu::SurfaceDescriptorFromWaylandSurface> desc =
|
|
|
|
std::make_unique<wgpu::SurfaceDescriptorFromWaylandSurface>();
|
|
|
|
desc->display = glfwGetWaylandDisplay();
|
|
|
|
desc->surface = glfwGetWaylandWindow(window);
|
|
|
|
return std::move(desc);
|
|
|
|
} else // NOLINT(readability/braces)
|
2020-01-15 13:14:12 +00:00
|
|
|
#endif
|
2022-06-01 09:30:50 +00:00
|
|
|
#if defined(DAWN_USE_X11)
|
2022-06-10 00:13:13 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<wgpu::SurfaceDescriptorFromXlibWindow> desc =
|
|
|
|
std::make_unique<wgpu::SurfaceDescriptorFromXlibWindow>();
|
|
|
|
desc->display = glfwGetX11Display();
|
|
|
|
desc->window = glfwGetX11Window(window);
|
|
|
|
return std::move(desc);
|
2022-06-01 09:30:50 +00:00
|
|
|
}
|
2022-06-10 00:13:13 +00:00
|
|
|
#else
|
|
|
|
{ return nullptr; }
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
2022-06-01 09:30:50 +00:00
|
|
|
}
|
2020-01-15 13:14:12 +00:00
|
|
|
|
2022-08-14 15:59:25 +00:00
|
|
|
} // namespace wgpu::glfw
|