diff --git a/example/Vulkan/CMakeLists.txt b/example/Vulkan/CMakeLists.txt index 551419de..c71b91fc 100644 --- a/example/Vulkan/CMakeLists.txt +++ b/example/Vulkan/CMakeLists.txt @@ -7,10 +7,8 @@ ENDIF() add_executable(VulkanTest main.cpp ${RENDER_WINDOW_SOURCE} - VKInstance.cpp - VKSurface.cpp - VKPhysicalDevice.cpp - VKDevice.cpp - VKCommandBuffer.cpp) + VKInstance.cpp + VKCommandBuffer.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 new file mode 100644 index 00000000..1e30e085 --- /dev/null +++ b/example/Vulkan/RenderSurface.cpp @@ -0,0 +1,188 @@ +#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) + return(nullptr); + + VkCommandBufferAllocateInfo cmd={}; + cmd.sType=VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; + cmd.pNext=nullptr; + cmd.commandPool=cmd_pool; + cmd.level=VK_COMMAND_BUFFER_LEVEL_PRIMARY; + cmd.commandBufferCount=1; + + VkCommandBuffer cmd_buf; + + VkResult res=vkAllocateCommandBuffers(device,&cmd,&cmd_buf); + + if(res!=VK_SUCCESS) + return(nullptr); + + return(new CommandBuffer(device,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(nullptr); + + CreateCommandPool(); + return(true); +} +VK_NAMESPACE_END diff --git a/example/Vulkan/RenderSurface.h b/example/Vulkan/RenderSurface.h new file mode 100644 index 00000000..5f1616af --- /dev/null +++ b/example/Vulkan/RenderSurface.h @@ -0,0 +1,52 @@ +#ifndef HGL_GRAPH_RENDER_SURFACE_INCLUDE +#define HGL_GRAPH_RENDER_SURFACE_INCLUDE + +#include +#include"VK.h" +#include"Window.h" +#include"VKCommandBuffer.h" + +VK_NAMESPACE_BEGIN +class RenderSurface +{ + Window *win; + VkInstance instance; + VkPhysicalDevice physical_device; + VkSurfaceKHR surface; + + VkPhysicalDeviceFeatures features; + VkPhysicalDeviceProperties properties; + VkPhysicalDeviceMemoryProperties memory_properties; + + List family_properties; + List supports_present; + + 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(); + +public: + + RenderSurface(Window *,VkInstance,VkPhysicalDevice); + virtual ~RenderSurface(); + + VkPhysicalDevice GetPhysicalDevice() { return physical_device; } + VkSurfaceKHR GetSurface() { return surface; } + +public: + + CommandBuffer *CreateCommandBuffer(); +};//class RenderSurface +VK_NAMESPACE_END +#endif//HGL_GRAPH_RENDER_SURFACE_INCLUDE diff --git a/example/Vulkan/VKDevice.cpp b/example/Vulkan/VKDevice.cpp deleted file mode 100644 index 5d7025d9..00000000 --- a/example/Vulkan/VKDevice.cpp +++ /dev/null @@ -1,53 +0,0 @@ -锘#include"VKDevice.h" -#include"VKCommandBuffer.h" - -VK_NAMESPACE_BEGIN -Device::Device(VkDevice dev,int family_index) -{ - device=dev; - - cmd_pool=nullptr; - - if(!device) - return; - - { - 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); - } -} - -Device::~Device() -{ - vkDestroyCommandPool(device,cmd_pool,nullptr); - vkDestroyDevice(device,nullptr); -} - -CommandBuffer *Device::CreateCommandBuffer() -{ - if(!cmd_pool) - return(nullptr); - - VkCommandBufferAllocateInfo cmd = {}; - cmd.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; - cmd.pNext = nullptr; - cmd.commandPool = cmd_pool; - cmd.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - cmd.commandBufferCount = 1; - - VkCommandBuffer cmd_buf; - - VkResult res = vkAllocateCommandBuffers(device, &cmd, &cmd_buf); - - if(res!=VK_SUCCESS) - return(nullptr); - - return(new CommandBuffer(device,cmd_pool,cmd_buf)); -} -VK_NAMESPACE_END diff --git a/example/Vulkan/VKDevice.h b/example/Vulkan/VKDevice.h deleted file mode 100644 index 0693008a..00000000 --- a/example/Vulkan/VKDevice.h +++ /dev/null @@ -1,27 +0,0 @@ -锘#ifndef HGL_GRAPH_VULKAN_DEVICE_INCLUDE -#define HGL_GRAPH_VULKAN_DEVICE_INCLUDE - -#include"VK.h" - -VK_NAMESPACE_BEGIN - - class CommandBuffer; - - /** - * Vulkan璁惧瀵硅薄灏佽 - */ - class Device - { - VkDevice device; - VkCommandPool cmd_pool; ///<鍛戒护姹狅紝鐢ㄤ簬鍒涘缓鍛戒护缂撳啿鍖恒傜敱浜庝笉鐭ラ亾鍒涘缓澶氫釜鏄惁鏈夊ソ澶勶紝鎵浠ユ殏鏃惰璁′负涓涓澶囧彧鏈変竴涓 - - public: - - Device(VkDevice,int); - virtual ~Device(); - - CommandBuffer *CreateCommandBuffer(); - };//class Device -VK_NAMESPACE_END - -#endif//HGL_GRAPH_VULKAN_DEVICE_INCLUDE diff --git a/example/Vulkan/VKInstance.cpp b/example/Vulkan/VKInstance.cpp index 8acb2d66..df26208c 100644 --- a/example/Vulkan/VKInstance.cpp +++ b/example/Vulkan/VKInstance.cpp @@ -59,22 +59,21 @@ bool Instance::Init() if(res!=VK_SUCCESS) return(false); - VkPhysicalDevice *pd_list=new VkPhysicalDevice[gpu_count]; physical_devices.SetCount(gpu_count); - vkEnumeratePhysicalDevices(inst, &gpu_count, pd_list); - - for(uint32_t i=0;iCreateVulkanSurface(inst); + VkPhysicalDevice pd; + + if(!physical_devices.Get(pd_index,pd)) + return(false); + + return(new RenderSurface(win,inst,pd)); } VK_NAMESPACE_END diff --git a/example/Vulkan/VKInstance.h b/example/Vulkan/VKInstance.h index e100ec7e..f406e714 100644 --- a/example/Vulkan/VKInstance.h +++ b/example/Vulkan/VKInstance.h @@ -4,9 +4,7 @@ #include #include #include"Window.h" -#include"VK.h" -#include"VKSurface.h" -#include"VKPhysicalDevice.h" +#include"RenderSurface.h" VK_NAMESPACE_BEGIN class Instance @@ -20,7 +18,7 @@ VK_NAMESPACE_BEGIN VkInstance inst; - ObjectList physical_devices; + List physical_devices; private: @@ -33,9 +31,9 @@ VK_NAMESPACE_BEGIN virtual bool Init(); - const ObjectList & GetDeviceList()const{return physical_devices;} + const List & GetDeviceList()const{return physical_devices;} - virtual Surface *CreateSurface(); + RenderSurface *CreateRenderSurface(int pd_index=0); };//class Instance VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_INSTANCE_INCLUDE diff --git a/example/Vulkan/VKPhysicalDevice.cpp b/example/Vulkan/VKPhysicalDevice.cpp deleted file mode 100644 index 654f1f16..00000000 --- a/example/Vulkan/VKPhysicalDevice.cpp +++ /dev/null @@ -1,83 +0,0 @@ -锘#include"VKPhysicalDevice.h" -#include"VKDevice.h" - -VK_NAMESPACE_BEGIN - -PhysicalDevice::PhysicalDevice(VkPhysicalDevice pd) -{ - physical_device=pd; - - if(!pd)return; - - vkGetPhysicalDeviceFeatures(physical_device,&features); - vkGetPhysicalDeviceProperties(physical_device,&properties); - vkGetPhysicalDeviceMemoryProperties(physical_device,&memory_properties); - - { - uint32_t family_count; - vkGetPhysicalDeviceQueueFamilyProperties(physical_device,&family_count,nullptr); - family_properties.SetCount(family_count); - vkGetPhysicalDeviceQueueFamilyProperties(physical_device,&family_count,family_properties.GetData()); - } -} - -PhysicalDevice::~PhysicalDevice() -{ -} - -int PhysicalDevice::QueueFamilyProperties(VkQueueFlags flag) const -{ - const int count=family_properties.GetCount(); - - if(count<=0) - return(-1); - - VkQueueFamilyProperties *fp=family_properties.GetData(); - for(int i=0;iqueueFlags&flag) - return i; - - ++fp; - } - - return -1; -} - -Device *PhysicalDevice::CreateGraphicsDevice() const -{ - const int index=QueueFamilyProperties(VK_QUEUE_GRAPHICS_BIT); - - if(index==-1) - return(nullptr); - - float queue_priorities[1] = {0.0}; - - VkDeviceQueueCreateInfo queue_info; - queue_info.queueFamilyIndex=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; - VkResult res = vkCreateDevice(physical_device, &create_info, nullptr, &device); - if(res != VK_SUCCESS) - return(nullptr); - - return(new Device(device,index)); -} - -VK_NAMESPACE_END diff --git a/example/Vulkan/VKPhysicalDevice.h b/example/Vulkan/VKPhysicalDevice.h deleted file mode 100644 index ad6e1b1d..00000000 --- a/example/Vulkan/VKPhysicalDevice.h +++ /dev/null @@ -1,34 +0,0 @@ -锘#ifndef HGL_GRAPH_VULKAN_PHYSICAL_DEVICE_INCLUDE -#define HGL_GRAPH_VULKAN_PHYSICAL_DEVICE_INCLUDE - -#include"VK.h" -#include - -VK_NAMESPACE_BEGIN - - class Device; - - /** - * Vulkan鐗╃悊璁惧瀵硅薄灏佽
- * 娉細杩欎釜璁惧鍙兘鏄浘褰㈣澶囷紝涔熷彲鑳芥槸璁$畻璁惧绛夛紝鎴戜滑鏆傛椂鍙敮鎸佸浘褰㈣澶 - */ - class PhysicalDevice - { - VkPhysicalDevice physical_device; - - VkPhysicalDeviceFeatures features; - VkPhysicalDeviceProperties properties; - VkPhysicalDeviceMemoryProperties memory_properties; - - List family_properties; - int QueueFamilyProperties(VkQueueFlags) const; - - public: - - PhysicalDevice(VkPhysicalDevice pd); - ~PhysicalDevice(); - - Device *CreateGraphicsDevice()const; ///<鍒涘缓涓涓浘褰㈣澶 - };//class PhysicalDevice -VK_NAMESPACE_END -#endif//HGL_GRAPH_VULKAN_PHYSICAL_DEVICE_INCLUDE diff --git a/example/Vulkan/VKSurface.cpp b/example/Vulkan/VKSurface.cpp deleted file mode 100644 index 04aae8d7..00000000 --- a/example/Vulkan/VKSurface.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include"VKSurface.h" - -VK_NAMESPACE_BEGIN -Surface::~Surface() -{ - vkDestroySurfaceKHR(instance, surface, nullptr); -} -VK_NAMESPACE_END diff --git a/example/Vulkan/VKSurface.h b/example/Vulkan/VKSurface.h deleted file mode 100644 index 0474578f..00000000 --- a/example/Vulkan/VKSurface.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef HGL_GRAPH_VULKAN_SURFACE_INCLUDE -#define HGL_GRAPH_VULKAN_SURFACE_INCLUDE - -#include"VK.h" - -VK_NAMESPACE_BEGIN - -class Surface -{ - VkInstance instance; - VkSurfaceKHR surface; - -public: - - Surface(VkInstance inst,VkSurfaceKHR surf) - { - instance = inst; - surface = surf; - } - - virtual ~Surface(); -};//class Surface - -VK_NAMESPACE_END -#endif//HGL_GRAPH_VULKAN_SURFACE_INCLUDE diff --git a/example/Vulkan/WinWindow.cpp b/example/Vulkan/WinWindow.cpp index 0b0a48e6..5f0c669d 100644 --- a/example/Vulkan/WinWindow.cpp +++ b/example/Vulkan/WinWindow.cpp @@ -173,7 +173,7 @@ namespace hgl UpdateWindow(win_hwnd); } - vulkan::Surface* CreateVulkanSurface(VkInstance vk_inst)const override + VkSurfaceKHR CreateSurface(VkInstance vk_inst)const override { VkWin32SurfaceCreateInfoKHR createInfo = {}; createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; @@ -188,7 +188,7 @@ namespace hgl if (res != VK_SUCCESS) return(nullptr); - return(new vulkan::Surface(vk_inst,surface)); + return(surface); } };//class WinWindow :public Window diff --git a/example/Vulkan/Window.h b/example/Vulkan/Window.h index e9ef1984..fd368ee9 100644 --- a/example/Vulkan/Window.h +++ b/example/Vulkan/Window.h @@ -2,7 +2,7 @@ #define HGL_GRAPH_WINDOW_INCLUDE #include -#include"VKSurface.h" +#include"VK.h" namespace hgl { namespace graph @@ -38,7 +38,7 @@ namespace hgl virtual void Show()=0; virtual void Hide()=0; - virtual vulkan::Surface *CreateVulkanSurface(VkInstance)const = 0; + virtual VkSurfaceKHR CreateSurface(VkInstance)const = 0; };//class Window Window *CreateRenderWindow(const OSString &win_name); diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index a0551f6d..0fd54c56 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -1,9 +1,5 @@ -锘#include"VK.h" +锘#include"Window.h" #include"VKInstance.h" -#include"VKSurface.h" -#include"VKDevice.h" -#include"VKCommandBuffer.h" -#include"Window.h" using namespace hgl; using namespace hgl::graph; @@ -22,19 +18,13 @@ int main(int,char **) return(-1); } - vulkan::Surface *surface = inst.CreateSurface(); + vulkan::RenderSurface *render=inst.CreateRenderSurface(); + vulkan::CommandBuffer *cmd_buf=render->CreateCommandBuffer(); - const ObjectList &device_list=inst.GetDeviceList(); - vulkan::PhysicalDevice *pd=device_list[0]; - vulkan::Device *dev=pd->CreateGraphicsDevice(); - - vulkan::CommandBuffer *cmd_buf=dev->CreateCommandBuffer(); - - delete surface; delete cmd_buf; - delete dev; + delete render; delete win; return 0;