Lugdunum  0.1.0
Surface.cpp
Go to the documentation of this file.
2 
4 
5 namespace lug {
6 namespace Graphics {
7 namespace Vulkan {
8 namespace API {
9 namespace Builder {
10 
11 Surface::Surface(const API::Instance& instance) : _instance{instance} {}
12 
13 bool Surface::build(API::Surface& surface, VkResult* returnResult) {
14  // Create the surface
15  VkSurfaceKHR vkSurface{VK_NULL_HANDLE};
16 
17 #if defined(LUG_SYSTEM_WINDOWS) // Win32 surface
18  const VkWin32SurfaceCreateInfoKHR createInfo{
19  /* createInfo.sType */ VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR,
20  /* createInfo.pNext */ nullptr,
21  /* createInfo.flags */ 0,
22  /* createInfo.hinstance */ _hinstance,
23  /* createInfo.hwnd */ _hwnd
24  };
25 
26  VkResult result = vkCreateWin32SurfaceKHR(static_cast<VkInstance>(_instance), &createInfo, nullptr, &vkSurface);
27 #elif defined(LUG_SYSTEM_LINUX) // Linux surface
28  const VkXlibSurfaceCreateInfoKHR createInfo{
29  /* createInfo.sType */ VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
30  /* createInfo.pNext */ nullptr,
31  /* createInfo.flags */ 0,
32  /* createInfo.dpy */ _dpy,
33  /* createInfo.window */ _window
34  };
35 
36  VkResult result = vkCreateXlibSurfaceKHR(static_cast<VkInstance>(_instance), &createInfo, nullptr, &vkSurface);
37 #elif defined(LUG_SYSTEM_ANDROID) // Android Surface
38  const VkAndroidSurfaceCreateInfoKHR createInfo{
39  /* createInfo.sType */ VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR,
40  /* createInfo.pNext */ nullptr,
41  /* createInfo.flags */ 0,
42  /* createInfo.window */ _window
43  };
44 
45  VkResult result = vkCreateAndroidSurfaceKHR(static_cast<VkInstance>(_instance), &createInfo, nullptr, &vkSurface);
46 #endif
47 
48  if (returnResult) {
49  *returnResult = result;
50  }
51 
52  if (result != VK_SUCCESS) {
53  return false;
54  }
55 
56  surface = API::Surface(vkSurface, &_instance);
57 
58  return true;
59 }
60 
61 std::unique_ptr<API::Surface> Surface::build(VkResult* returnResult) {
62  std::unique_ptr<API::Surface> surface = std::make_unique<API::Surface>();
63  return build(*surface, returnResult) ? std::move(surface) : nullptr;
64 }
65 
66 } // Builder
67 } // API
68 } // Vulkan
69 } // Graphics
70 } // lug
Surface(const API::Instance &instance)
Definition: Surface.cpp:11
bool build(API::Surface &instance, VkResult *returnResult=nullptr)
Definition: Surface.cpp:13