OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2016 Google Inc. | 3 * Copyright 2016 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
| 9 #include <Windows.h> |
| 10 #include "WindowContextFactory_win.h" |
| 11 |
9 #include "../VulkanWindowContext.h" | 12 #include "../VulkanWindowContext.h" |
10 #include "Window_win.h" | 13 #include "Window_win.h" |
11 | 14 |
12 #include "vk/GrVkInterface.h" | 15 #include "vk/GrVkInterface.h" |
13 #include "vk/GrVkUtil.h" | 16 #include "vk/GrVkUtil.h" |
14 | 17 |
15 namespace sk_app { | 18 namespace sk_app { |
| 19 namespace window_context_factory { |
16 | 20 |
17 // Platform dependant call | 21 WindowContext* NewVulkanForWin(HWND hwnd, const DisplayParams& params) { |
18 VkSurfaceKHR VulkanWindowContext::createVkSurface(VkInstance instance, void* pla
tformData) { | 22 auto createVkSurface = [hwnd] (VkInstance instance) -> VkSurfaceKHR { |
19 static PFN_vkCreateWin32SurfaceKHR createWin32SurfaceKHR = nullptr; | 23 static PFN_vkCreateWin32SurfaceKHR createWin32SurfaceKHR = nullptr; |
20 if (!createWin32SurfaceKHR) { | 24 if (!createWin32SurfaceKHR) { |
21 createWin32SurfaceKHR = (PFN_vkCreateWin32SurfaceKHR) vkGetInstanceProcA
ddr(instance, | 25 createWin32SurfaceKHR = (PFN_vkCreateWin32SurfaceKHR) |
22 "vkCreat
eWin32SurfaceKHR"); | 26 vkGetInstanceProcAddr(instance, "vkCreateWin32SurfaceKHR"); |
| 27 } |
| 28 HINSTANCE hinstance = GetModuleHandle(0); |
| 29 VkSurfaceKHR surface; |
| 30 |
| 31 VkWin32SurfaceCreateInfoKHR surfaceCreateInfo; |
| 32 memset(&surfaceCreateInfo, 0, sizeof(VkWin32SurfaceCreateInfoKHR)); |
| 33 surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KH
R; |
| 34 surfaceCreateInfo.pNext = nullptr; |
| 35 surfaceCreateInfo.flags = 0; |
| 36 surfaceCreateInfo.hinstance = hinstance; |
| 37 surfaceCreateInfo.hwnd = hwnd; |
| 38 |
| 39 VkResult res = createWin32SurfaceKHR(instance, &surfaceCreateInfo, nullp
tr, &surface); |
| 40 if (VK_SUCCESS != res) { |
| 41 return VK_NULL_HANDLE; |
| 42 } |
| 43 |
| 44 return surface; |
| 45 }; |
| 46 |
| 47 auto canPresent = [hwnd] (VkInstance instance, VkPhysicalDevice physDev, |
| 48 uint32_t queueFamilyIndex) { |
| 49 static PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR |
| 50 getPhysicalDeviceWin32PresentationSu
pportKHR = nullptr; |
| 51 if (!getPhysicalDeviceWin32PresentationSupportKHR) { |
| 52 getPhysicalDeviceWin32PresentationSupportKHR = |
| 53 (PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR) |
| 54 vkGetInstanceProcAddr(instance, |
| 55 "vkGetPhysicalDeviceWin32PresentationS
upportKHR"); |
| 56 } |
| 57 |
| 58 VkBool32 check = getPhysicalDeviceWin32PresentationSupportKHR(physDev, q
ueueFamilyIndex); |
| 59 return (VK_FALSE != check); |
| 60 }; |
| 61 |
| 62 WindowContext* ctx = new VulkanWindowContext(params, createVkSurface, canPre
sent); |
| 63 if (!ctx->isValid()) { |
| 64 delete ctx; |
| 65 return nullptr; |
23 } | 66 } |
24 | 67 return ctx; |
25 if (!platformData) { | |
26 return VK_NULL_HANDLE; | |
27 } | |
28 ContextPlatformData_win* winPlatformData = | |
29 reinterpret_cast<ContextPlatformData_w
in*>(platformData); | |
30 VkSurfaceKHR surface; | |
31 | |
32 VkWin32SurfaceCreateInfoKHR surfaceCreateInfo; | |
33 memset(&surfaceCreateInfo, 0, sizeof(VkWin32SurfaceCreateInfoKHR)); | |
34 surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; | |
35 surfaceCreateInfo.pNext = nullptr; | |
36 surfaceCreateInfo.flags = 0; | |
37 surfaceCreateInfo.hinstance = winPlatformData->fHInstance; | |
38 surfaceCreateInfo.hwnd = winPlatformData->fHWnd; | |
39 | |
40 VkResult res = createWin32SurfaceKHR(instance, &surfaceCreateInfo, nullptr,
&surface); | |
41 if (VK_SUCCESS != res) { | |
42 return VK_NULL_HANDLE; | |
43 } | |
44 | |
45 return surface; | |
46 } | 68 } |
47 | 69 |
48 // Platform dependant call | 70 } // namespace window_context_factory |
49 bool VulkanWindowContext::canPresent(VkInstance instance, VkPhysicalDevice physD
ev, | 71 } // namespace sk_app |
50 uint32_t queueFamilyIndex, void*) { | |
51 static PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR | |
52 getPhysicalDeviceWin32PresentationSu
pportKHR = nullptr; | |
53 if (!getPhysicalDeviceWin32PresentationSupportKHR) { | |
54 getPhysicalDeviceWin32PresentationSupportKHR = | |
55 (PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR) vkGetInstancePr
ocAddr(instance, | |
56 "vkGetPhysicalDeviceWin32Presen
tationSupportKHR"); | |
57 } | |
58 | |
59 VkBool32 check = getPhysicalDeviceWin32PresentationSupportKHR(physDev, queue
FamilyIndex); | |
60 return (VK_FALSE != check); | |
61 } | |
62 | |
63 } // namespace sk_app | |
OLD | NEW |