From 4d4e4919854b58e0b8541f66e333efabbae0c476 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 11 Apr 2019 02:29:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=9A=84RenderSurface=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E7=BB=93=E6=9E=84=EF=BC=8C=E6=9B=B4=E4=B8=BA=E5=90=88?= =?UTF-8?q?=E7=90=86=E5=8C=96=E3=80=82=E5=9C=A8=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E4=B8=8D=E6=88=90=E5=8A=9F=E5=89=8D=E4=B8=8D=E4=BC=9A=E5=88=9B?= =?UTF-8?q?=E5=BB=BARenderSurface=E7=B1=BB=EF=BC=8C=E4=BB=A5=E8=8A=82?= =?UTF-8?q?=E7=9C=81=E5=86=85=E5=AD=98=E5=BC=80=E9=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/CMakeLists.txt | 4 +- example/Vulkan/RenderSurface.cpp | 170 +--------------------- example/Vulkan/RenderSurface.h | 43 ++---- example/Vulkan/RenderSurfaceAttribute.cpp | 74 ++++++++++ example/Vulkan/RenderSurfaceAttribute.h | 34 +++++ example/Vulkan/RenderSurfaceCreater.cpp | 109 ++++++++++++++ example/Vulkan/VK.h | 3 + example/Vulkan/VKInstance.cpp | 76 +++++----- example/Vulkan/VKInstance.h | 28 ++-- example/Vulkan/VKSurfaceExtensionName.h | 26 ++++ example/Vulkan/WinWindow.cpp | 15 +- example/Vulkan/Window.h | 5 - example/Vulkan/XCBWindow.cpp | 46 +++--- example/Vulkan/main.cpp | 8 +- 14 files changed, 355 insertions(+), 286 deletions(-) create mode 100644 example/Vulkan/RenderSurfaceAttribute.cpp create mode 100644 example/Vulkan/RenderSurfaceAttribute.h create mode 100644 example/Vulkan/RenderSurfaceCreater.cpp create mode 100644 example/Vulkan/VKSurfaceExtensionName.h diff --git a/example/Vulkan/CMakeLists.txt b/example/Vulkan/CMakeLists.txt index c71b91fc..c6d5a46f 100644 --- a/example/Vulkan/CMakeLists.txt +++ b/example/Vulkan/CMakeLists.txt @@ -7,8 +7,10 @@ ENDIF() add_executable(VulkanTest main.cpp ${RENDER_WINDOW_SOURCE} - VKInstance.cpp + VKInstance.cpp VKCommandBuffer.cpp + RenderSurfaceAttribute.cpp + RenderSurfaceCreater.cpp RenderSurface.cpp) target_link_libraries(VulkanTest PRIVATE ${ULRE} ${VULKAN_LIB} ${RENDER_WINDOW_LIBRARY}) diff --git a/example/Vulkan/RenderSurface.cpp b/example/Vulkan/RenderSurface.cpp index 102e704b..2a589a6c 100644 --- a/example/Vulkan/RenderSurface.cpp +++ b/example/Vulkan/RenderSurface.cpp @@ -1,189 +1,27 @@ #include"RenderSurface.h" VK_NAMESPACE_BEGIN -RenderSurface::RenderSurface(Window *w,VkInstance inst,VkPhysicalDevice pd) -{ - win=w; - instance=inst; - physical_device=pd; - family_index=-1; - device=nullptr; - cmd_pool=nullptr; - - vkGetPhysicalDeviceFeatures(physical_device,&features); - vkGetPhysicalDeviceProperties(physical_device,&properties); - vkGetPhysicalDeviceMemoryProperties(physical_device,&memory_properties); - - surface=win->CreateSurface(inst); - - { - uint32_t family_count; - vkGetPhysicalDeviceQueueFamilyProperties(physical_device,&family_count,nullptr); - family_properties.SetCount(family_count); - vkGetPhysicalDeviceQueueFamilyProperties(physical_device,&family_count,family_properties.GetData()); - - { - supports_present.SetCount(family_count); - VkBool32 *sp=supports_present.GetData(); - for(uint32_t i=0; iGetWidth(); - swapchain_extent.height=win->GetHeight(); - - if(swapchain_extent.widthsurface_caps.maxImageExtent.width)swapchain_extent.width=surface_caps.maxImageExtent.width; - - if(swapchain_extent.heightsurface_caps.maxImageExtent.height)swapchain_extent.height=surface_caps.maxImageExtent.height; - } - else - { - swapchain_extent=surface_caps.currentExtent; - } - } - - CreateDevice(); -} - -RenderSurface::~RenderSurface() -{ - if(device) - { - if(cmd_pool) - vkDestroyCommandPool(device,cmd_pool,nullptr); - - vkDestroyDevice(device,nullptr); - } - - if(surface) - vkDestroySurfaceKHR(instance,surface,nullptr); -} - -int RenderSurface::QueueFamilyProperties(VkQueueFlags flag) const -{ - const int count=family_properties.GetCount(); - - if(count<=0) - return(-1); - - VkBool32*sp=supports_present.GetData(); - VkQueueFamilyProperties*fp=family_properties.GetData(); - for(int i=0;iqueueFlags&flag)) - return i; - - ++sp; - ++fp; - } - - return -1; -} - -bool RenderSurface::CreateCommandPool() -{ - VkCommandPoolCreateInfo cmd_pool_info={}; - - cmd_pool_info.sType=VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; - cmd_pool_info.pNext=NULL; - cmd_pool_info.queueFamilyIndex=family_index; - cmd_pool_info.flags=0; - - VkResult res=vkCreateCommandPool(device,&cmd_pool_info,nullptr,&cmd_pool); - - return(res==VK_SUCCESS); -} CommandBuffer *RenderSurface::CreateCommandBuffer() { - if(!cmd_pool) + if(!rsa->cmd_pool) return(nullptr); VkCommandBufferAllocateInfo cmd={}; cmd.sType=VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; cmd.pNext=nullptr; - cmd.commandPool=cmd_pool; + cmd.commandPool=rsa->cmd_pool; cmd.level=VK_COMMAND_BUFFER_LEVEL_PRIMARY; cmd.commandBufferCount=1; VkCommandBuffer cmd_buf; - VkResult res=vkAllocateCommandBuffers(device,&cmd,&cmd_buf); + VkResult res=vkAllocateCommandBuffers(rsa->device,&cmd,&cmd_buf); if(res!=VK_SUCCESS) return(nullptr); - return(new CommandBuffer(device,cmd_pool,cmd_buf)); + return(new CommandBuffer(rsa->device,rsa->cmd_pool,cmd_buf)); } -bool RenderSurface::CreateDevice() -{ - family_index=QueueFamilyProperties(VK_QUEUE_GRAPHICS_BIT); - - if(family_index==-1) - return(false); - - float queue_priorities[1]={0.0}; - - VkDeviceQueueCreateInfo queue_info; - queue_info.queueFamilyIndex=family_index; - queue_info.sType=VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; - queue_info.pNext=nullptr; - queue_info.queueCount=1; - queue_info.pQueuePriorities=queue_priorities; - - VkDeviceCreateInfo create_info={}; - const char *ext_list[1]={VK_KHR_SWAPCHAIN_EXTENSION_NAME}; - create_info.sType=VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; - create_info.pNext=nullptr; - create_info.queueCreateInfoCount=1; - create_info.pQueueCreateInfos=&queue_info; - create_info.enabledExtensionCount=1; - create_info.ppEnabledExtensionNames=ext_list; - create_info.enabledLayerCount=0; - create_info.ppEnabledLayerNames=nullptr; - create_info.pEnabledFeatures=nullptr; - - VkResult res=vkCreateDevice(physical_device,&create_info,nullptr,&device); - - if(res!=VK_SUCCESS) - return(false); - - CreateCommandPool(); - return(true); -} VK_NAMESPACE_END diff --git a/example/Vulkan/RenderSurface.h b/example/Vulkan/RenderSurface.h index 5f1616af..2f7df16a 100644 --- a/example/Vulkan/RenderSurface.h +++ b/example/Vulkan/RenderSurface.h @@ -4,49 +4,36 @@ #include #include"VK.h" #include"Window.h" +#include"RenderSurfaceAttribute.h" #include"VKCommandBuffer.h" VK_NAMESPACE_BEGIN + +using RefRenderSurfaceAttribute=SharedPtr; + class RenderSurface { - Window *win; - VkInstance instance; - VkPhysicalDevice physical_device; - VkSurfaceKHR surface; + RefRenderSurfaceAttribute rsa; - VkPhysicalDeviceFeatures features; - VkPhysicalDeviceProperties properties; - VkPhysicalDeviceMemoryProperties memory_properties; +private: - List family_properties; - List supports_present; + friend RenderSurface *CreateRenderSuface(VkInstance,VkPhysicalDevice,Window *); - List surface_formts; - VkSurfaceCapabilitiesKHR surface_caps; - List present_modes; - - uint32_t family_index; - VkDevice device; - VkCommandPool cmd_pool; ///<أڴڲ֪ǷкôʱΪһ豸ֻһ - -protected: - - int QueueFamilyProperties(VkQueueFlags) const; - - bool CreateDevice(); - bool CreateCommandPool(); + RenderSurface(RefRenderSurfaceAttribute &ref_rsa) + { + rsa=ref_rsa; + } public: - RenderSurface(Window *,VkInstance,VkPhysicalDevice); - virtual ~RenderSurface(); + virtual ~RenderSurface()=default; - VkPhysicalDevice GetPhysicalDevice() { return physical_device; } - VkSurfaceKHR GetSurface() { return surface; } + VkPhysicalDevice GetPhysicalDevice () { return rsa->physical_device; } + VkSurfaceKHR GetSurface () { return rsa->surface; } public: - CommandBuffer *CreateCommandBuffer(); + CommandBuffer * CreateCommandBuffer (); };//class RenderSurface VK_NAMESPACE_END #endif//HGL_GRAPH_RENDER_SURFACE_INCLUDE diff --git a/example/Vulkan/RenderSurfaceAttribute.cpp b/example/Vulkan/RenderSurfaceAttribute.cpp new file mode 100644 index 00000000..7a289dc1 --- /dev/null +++ b/example/Vulkan/RenderSurfaceAttribute.cpp @@ -0,0 +1,74 @@ +#include"RenderSurfaceAttribute.h" + +VK_NAMESPACE_BEGIN +RenderSurfaceAttribute::RenderSurfaceAttribute(VkInstance inst,VkPhysicalDevice pd,VkSurfaceKHR s) +{ + instance=inst; + physical_device=pd; + surface=s; + + vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device,surface,&surface_caps); + + vkGetPhysicalDeviceFeatures(physical_device,&features); + vkGetPhysicalDeviceProperties(physical_device,&properties); + vkGetPhysicalDeviceMemoryProperties(physical_device,&memory_properties); + + { + uint32_t format_count; + if(vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device,surface,&format_count,nullptr)==VK_SUCCESS) + { + surface_formts.SetCount(format_count); + + if(vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device,surface,&format_count,surface_formts.GetData())!=VK_SUCCESS) + surface_formts.Clear(); + } + } + + { + uint32_t mode_count; + if(vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device,surface,&mode_count,nullptr)==VK_SUCCESS) + { + present_modes.SetCount(mode_count); + if(vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device,surface,&mode_count,present_modes.GetData())!=VK_SUCCESS) + present_modes.Clear(); + } + } + + { + uint32_t family_count; + vkGetPhysicalDeviceQueueFamilyProperties(physical_device,&family_count,nullptr); + family_properties.SetCount(family_count); + vkGetPhysicalDeviceQueueFamilyProperties(physical_device,&family_count,family_properties.GetData()); + + VkQueueFamilyProperties *fp=family_properties.GetData(); + + supports_present.SetCount(family_count); + VkBool32 *sp=supports_present.GetData(); + for(uint32_t i=0; iqueueFlags&VK_QUEUE_GRAPHICS_BIT)) + family_index=i; + } + + ++fp; + ++sp; + } + } +} + +RenderSurfaceAttribute::~RenderSurfaceAttribute() +{ + if(cmd_pool) + vkDestroyCommandPool(device,cmd_pool,nullptr); + + if(device) + vkDestroyDevice(device,nullptr); + + if(surface) + vkDestroySurfaceKHR(instance,surface,nullptr); +} +VK_NAMESPACE_END diff --git a/example/Vulkan/RenderSurfaceAttribute.h b/example/Vulkan/RenderSurfaceAttribute.h new file mode 100644 index 00000000..ec16d69e --- /dev/null +++ b/example/Vulkan/RenderSurfaceAttribute.h @@ -0,0 +1,34 @@ +#pragma once + +#include"VK.h" + +VK_NAMESPACE_BEGIN +struct RenderSurfaceAttribute +{ + VkInstance instance =nullptr; + VkPhysicalDevice physical_device =nullptr; + VkSurfaceKHR surface =nullptr; + VkSurfaceCapabilitiesKHR surface_caps; + VkExtent2D swapchain_extent; + + int family_index =-1; + + List family_properties; + List supports_present; + + VkPhysicalDeviceFeatures features; + VkPhysicalDeviceProperties properties; + VkPhysicalDeviceMemoryProperties memory_properties; + + List surface_formts; + List present_modes; + + VkDevice device =nullptr; + VkCommandPool cmd_pool =nullptr; + +public: + + RenderSurfaceAttribute(VkInstance inst,VkPhysicalDevice pd,VkSurfaceKHR s); + ~RenderSurfaceAttribute(); +};//class RenderSurfaceAttribute +VK_NAMESPACE_END diff --git a/example/Vulkan/RenderSurfaceCreater.cpp b/example/Vulkan/RenderSurfaceCreater.cpp new file mode 100644 index 00000000..ef8f331d --- /dev/null +++ b/example/Vulkan/RenderSurfaceCreater.cpp @@ -0,0 +1,109 @@ +#include"RenderSurface.h" +#include"VKInstance.h" +#include + +VK_NAMESPACE_BEGIN + +VkSurfaceKHR CreateSurface(VkInstance,Window *); + +namespace +{ + VkExtent2D GetSwapchainExtent(VkSurfaceCapabilitiesKHR &surface_caps,int width,int height) + { + if(surface_caps.currentExtent.width==0xFFFFFFFF) + { + VkExtent2D swapchain_extent; + + swapchain_extent.width=width; + swapchain_extent.height=height; + + if(swapchain_extent.widthsurface_caps.maxImageExtent.width)swapchain_extent.width=surface_caps.maxImageExtent.width; + + if(swapchain_extent.heightsurface_caps.maxImageExtent.height)swapchain_extent.height=surface_caps.maxImageExtent.height; + + return swapchain_extent; + } + else + { + return surface_caps.currentExtent; + } + } + + VkDevice CreateDevice(VkInstance instance,VkPhysicalDevice physical_device,int family_index) + { + float queue_priorities[1]={0.0}; + + VkDeviceQueueCreateInfo queue_info; + queue_info.queueFamilyIndex=family_index; + queue_info.sType=VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; + queue_info.pNext=nullptr; + queue_info.queueCount=1; + queue_info.pQueuePriorities=queue_priorities; + + VkDeviceCreateInfo create_info={}; + const char *ext_list[1]={VK_KHR_SWAPCHAIN_EXTENSION_NAME}; + create_info.sType=VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; + create_info.pNext=nullptr; + create_info.queueCreateInfoCount=1; + create_info.pQueueCreateInfos=&queue_info; + create_info.enabledExtensionCount=1; + create_info.ppEnabledExtensionNames=ext_list; + create_info.enabledLayerCount=0; + create_info.ppEnabledLayerNames=nullptr; + create_info.pEnabledFeatures=nullptr; + + VkDevice device; + + if(vkCreateDevice(physical_device,&create_info,nullptr,&device)==VK_SUCCESS) + return device; + + return nullptr; + } + + VkCommandPool CreateCommandPool(VkDevice device,int family_index) + { + VkCommandPoolCreateInfo cmd_pool_info={}; + + cmd_pool_info.sType=VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; + cmd_pool_info.pNext=nullptr; + cmd_pool_info.queueFamilyIndex=family_index; + cmd_pool_info.flags=0; + + VkCommandPool cmd_pool; + + if(vkCreateCommandPool(device,&cmd_pool_info,nullptr,&cmd_pool)==VK_SUCCESS) + return cmd_pool; + + return(nullptr); + } +}//namespace + +RenderSurface *CreateRenderSuface(VkInstance inst,VkPhysicalDevice physical_device,Window *win) +{ + VkSurfaceKHR surface=CreateSurface(inst,win); + + if(!surface) + return(nullptr); + + RefRenderSurfaceAttribute rsa=new RenderSurfaceAttribute(inst,physical_device,surface); + + rsa->swapchain_extent=GetSwapchainExtent(rsa->surface_caps,win->GetWidth(),win->GetHeight()); + + if(rsa->family_index==-1) + return(nullptr); + + rsa->device=CreateDevice(inst,physical_device,rsa->family_index); + + if(!rsa->device) + return(nullptr); + + rsa->cmd_pool=CreateCommandPool(rsa->device,rsa->family_index); + + if(!rsa->cmd_pool) + return(nullptr); + + return(new RenderSurface(rsa)); +} +VK_NAMESPACE_END diff --git a/example/Vulkan/VK.h b/example/Vulkan/VK.h index 4eab5f90..f977f02f 100644 --- a/example/Vulkan/VK.h +++ b/example/Vulkan/VK.h @@ -1,9 +1,12 @@ #ifndef HGL_GRAPH_VULKAN_INCLUDE #define HGL_GRAPH_VULKAN_INCLUDE +#include #include #define VK_NAMESPACE_BEGIN namespace hgl{namespace graph{namespace vulkan{ #define VK_NAMESPACE_END }}} +using CharPointerList=hgl::List; + #endif//HGL_GRAPH_VULKAN_INCLUDE diff --git a/example/Vulkan/VKInstance.cpp b/example/Vulkan/VKInstance.cpp index 7bccf1ec..3ccfb315 100644 --- a/example/Vulkan/VKInstance.cpp +++ b/example/Vulkan/VKInstance.cpp @@ -1,12 +1,15 @@ #include"VKInstance.h" +#include"VKSurfaceExtensionName.h" #include VK_NAMESPACE_BEGIN +RenderSurface *CreateRenderSuface(VkInstance,VkPhysicalDevice,Window *); -Instance::Instance(const UTF8String &an,Window *w) +Instance *CreateInstance(const UTF8String &app_name) { - win=w; - app_name=an; + VkApplicationInfo app_info; + VkInstanceCreateInfo inst_info; + CharPointerList ext_list; app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; app_info.pNext = nullptr; @@ -17,7 +20,7 @@ Instance::Instance(const UTF8String &an,Window *w) app_info.apiVersion = VK_API_VERSION_1_0; ext_list.Add(VK_KHR_SURFACE_EXTENSION_NAME); - ext_list.Add(win->GetVulkanSurfaceExtname()); + ext_list.Add(HGL_VK_SURFACE_EXTENSION_NAME); //此宏在CMAKE中定义 inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; inst_info.pNext = nullptr; @@ -28,52 +31,45 @@ Instance::Instance(const UTF8String &an,Window *w) inst_info.enabledLayerCount = 0; inst_info.ppEnabledLayerNames = nullptr; - inst=nullptr; + VkInstance inst; + + if(vkCreateInstance(&inst_info,nullptr,&inst)==VK_SUCCESS) + return(new Instance(inst,ext_list)); + + return(nullptr); +} + +Instance::Instance(VkInstance i,CharPointerList &el) +{ + inst=i; + ext_list=el; + + uint32_t gpu_count = 1; + + if(vkEnumeratePhysicalDevices(inst, &gpu_count, nullptr)==VK_SUCCESS) + { + physical_devices.SetCount(gpu_count); + vkEnumeratePhysicalDevices(inst, &gpu_count,physical_devices.GetData()); + } } Instance::~Instance() { physical_devices.Clear(); - if(inst) - vkDestroyInstance(inst,nullptr); + vkDestroyInstance(inst,nullptr); } -bool Instance::Init() +RenderSurface *Instance::CreateSurface(Window *win,int pd_index) { - if(inst) - return(false); - - VkResult res=vkCreateInstance(&inst_info,nullptr,&inst); - - if(res) - { - inst=nullptr; - return(false); - } - - { - uint32_t gpu_count = 1; - res = vkEnumeratePhysicalDevices(inst, &gpu_count, nullptr); - - if(res!=VK_SUCCESS) - return(false); - - physical_devices.SetCount(gpu_count); - vkEnumeratePhysicalDevices(inst, &gpu_count,physical_devices.GetData()); - } - - return(true); -} - -RenderSurface *Instance::CreateRenderSurface(int pd_index) -{ - VkPhysicalDevice pd; - - if(!physical_devices.Get(pd_index,pd)) + if(!win) return(nullptr); - return(new RenderSurface(win,inst,pd)); -} + VkPhysicalDevice pd=GetDevice(pd_index); + if(!pd) + return(nullptr); + + return CreateRenderSuface(inst,pd,win); +} VK_NAMESPACE_END diff --git a/example/Vulkan/VKInstance.h b/example/Vulkan/VKInstance.h index f406e714..973d6bae 100644 --- a/example/Vulkan/VKInstance.h +++ b/example/Vulkan/VKInstance.h @@ -4,36 +4,40 @@ #include #include #include"Window.h" +#include"VK.h" #include"RenderSurface.h" VK_NAMESPACE_BEGIN class Instance { - Window *win; - - List ext_list; - - VkApplicationInfo app_info; - VkInstanceCreateInfo inst_info; - VkInstance inst; + CharPointerList ext_list; + List physical_devices; private: - UTF8String app_name; + friend Instance *CreateInstance(const UTF8String &app_name); + + Instance(VkInstance,CharPointerList &); public: - Instance(const UTF8String &,Window *); virtual ~Instance(); - virtual bool Init(); + VkInstance GetVkInstance () {return inst;} - const List & GetDeviceList()const{return physical_devices;} + const CharPointerList & GetExtList ()const {return ext_list;} + const List &GetDeviceList ()const {return physical_devices;} + VkPhysicalDevice GetDevice (int index) + { + return GetObject(physical_devices,index); + } - RenderSurface *CreateRenderSurface(int pd_index=0); + RenderSurface * CreateSurface (Window *,int pd_index=0); };//class Instance + + Instance *CreateInstance(const UTF8String &); ///<创建一个Vulkan实例 VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_INSTANCE_INCLUDE diff --git a/example/Vulkan/VKSurfaceExtensionName.h b/example/Vulkan/VKSurfaceExtensionName.h new file mode 100644 index 00000000..35c5024a --- /dev/null +++ b/example/Vulkan/VKSurfaceExtensionName.h @@ -0,0 +1,26 @@ +#pragma once + +#include"VK.h" + +#ifdef __ANDROID__ + #include + #define HGL_VK_SURFACE_EXTENSION_NAME VK_KHR_ANDROID_SURFACE_EXTENSION_NAME +#elif defined(_WIN32) + #include + #include + #define HGL_VK_SURFACE_EXTENSION_NAME VK_KHR_WIN32_SURFACE_EXTENSION_NAME +#elif defined(VK_USE_PLATFORM_IOS_MVK) + #include + #define HGL_VK_SURFACE_EXTENSION_NAME VK_MVK_IOS_SURFACE_EXTENSION_NAME +#elif defined(VK_USE_PLATFORM_MACOS_MVK) + #include + #define HGL_VK_SURFACE_EXTENSION_NAME VK_MVK_MACOS_SURFACE_EXTENSION_NAME +#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) + #include + #define HGL_VK_SURFACE_EXTENSION_NAME VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME +#else + #include + #include + #include + #define HGL_VK_SURFACE_EXTENSION_NAME VK_KHR_XCB_SURFACE_EXTENSION_NAME +#endif diff --git a/example/Vulkan/WinWindow.cpp b/example/Vulkan/WinWindow.cpp index 5f0c669d..ce395ab8 100644 --- a/example/Vulkan/WinWindow.cpp +++ b/example/Vulkan/WinWindow.cpp @@ -12,11 +12,11 @@ namespace hgl { constexpr wchar_t WIN_CLASS_NAME[] = L"CMGameEngine/ULRE Window Class"; - LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) + LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { Window *win=(Window *)GetWindowLongPtrW(hWnd, GWLP_USERDATA); - switch (uMsg) + switch (uMsg) { case WM_CLOSE: PostQuitMessage(0); @@ -125,11 +125,6 @@ namespace hgl Close(); } - const char* GetVulkanSurfaceExtname()const - { - return VK_KHR_WIN32_SURFACE_EXTENSION_NAME; - } - bool Create(uint w, uint h) override { width = w; @@ -143,7 +138,7 @@ namespace hgl return Create(); } - bool Create(uint, uint, uint)override + bool Create(uint, uint, uint)override { return(false); } @@ -158,7 +153,7 @@ namespace hgl win_hwnd = nullptr; } - void Show()override + void Show()override { ShowWindow(win_hwnd, SW_SHOW); SetForegroundWindow(win_hwnd); @@ -167,7 +162,7 @@ namespace hgl UpdateWindow(win_hwnd); } - void Hide()override + void Hide()override { ShowWindow(win_hwnd, SW_HIDE); UpdateWindow(win_hwnd); diff --git a/example/Vulkan/Window.h b/example/Vulkan/Window.h index fd368ee9..fe1610bd 100644 --- a/example/Vulkan/Window.h +++ b/example/Vulkan/Window.h @@ -2,7 +2,6 @@ #define HGL_GRAPH_WINDOW_INCLUDE #include -#include"VK.h" namespace hgl { namespace graph @@ -29,16 +28,12 @@ namespace hgl } virtual ~Window()=default; - virtual const char *GetVulkanSurfaceExtname()const=0; - virtual bool Create(uint,uint)=0; virtual bool Create(uint,uint,uint)=0; virtual void Close()=0; virtual void Show()=0; virtual void Hide()=0; - - virtual VkSurfaceKHR CreateSurface(VkInstance)const = 0; };//class Window Window *CreateRenderWindow(const OSString &win_name); diff --git a/example/Vulkan/XCBWindow.cpp b/example/Vulkan/XCBWindow.cpp index bb218514..c7f9a426 100644 --- a/example/Vulkan/XCBWindow.cpp +++ b/example/Vulkan/XCBWindow.cpp @@ -1,6 +1,7 @@ #include"Window.h" #include #include +#include"VK.h" #include #include #include @@ -48,11 +49,6 @@ namespace hgl { } - const char *GetVulkanSurfaceExtname()const - { - return VK_KHR_XCB_SURFACE_EXTENSION_NAME; - } - bool Create(uint w,uint h) override { if(w<=0||h<=0)return(false); @@ -111,23 +107,11 @@ namespace hgl void Show()override{} void Hide()override{} - VkSurfaceKHR CreateSurface(VkInstance vk_inst)const override - { - VkXcbSurfaceCreateInfoKHR createInfo = {}; - createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; - createInfo.pNext = nullptr; - createInfo.connection = connection; - createInfo.window = window; + public: - VkSurfaceKHR surface; + xcb_connection_t *GetConnection(){return connection;} + xcb_window_t GetWindow(){return window;} - VkResult res = vkCreateXcbSurfaceKHR(vk_inst, &createInfo, nullptr, &surface); - - if (res != VK_SUCCESS) - return(nullptr); - - return(surface); - } };//class XCBWindow:public Window Window *CreateRenderWindow(const UTF8String &win_name) @@ -136,3 +120,25 @@ namespace hgl } }//namespace graph }//namespace hgl + +VK_NAMESPACE_BEGIN +VkSurfaceKHR CreateSurface(VkInstance vk_inst,Window *win) +{ + XCBWindow *xcb_win=(XCBWindow *)win; + + VkXcbSurfaceCreateInfoKHR createInfo = {}; + createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; + createInfo.pNext = nullptr; + createInfo.connection = xcb_win->GetConnection(); + createInfo.window = xcb_win->GetWindow(); + + VkSurfaceKHR surface; + + VkResult res = vkCreateXcbSurfaceKHR(vk_inst, &createInfo, nullptr, &surface); + + if (res != VK_SUCCESS) + return(nullptr); + + return(surface); +} +VK_NAMESPACE_END diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index 0fd54c56..022dff48 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -1,5 +1,6 @@ #include"Window.h" #include"VKInstance.h" +#include"RenderSurface.h" using namespace hgl; using namespace hgl::graph; @@ -10,19 +11,18 @@ int main(int,char **) win->Create(1280,720); - vulkan::Instance inst(U8_TEXT("VulkanTest"),win); + vulkan::Instance *inst=vulkan::CreateInstance(U8_TEXT("VulkanTest")); - if(!inst.Init()) + if(!inst) { delete win; return(-1); } - vulkan::RenderSurface *render=inst.CreateRenderSurface(); + vulkan::RenderSurface *render=inst->CreateSurface(win); vulkan::CommandBuffer *cmd_buf=render->CreateCommandBuffer(); - delete cmd_buf; delete render; delete win;