新的RenderSurface创建结构,更为合理化。在初始化不成功前不会创建RenderSurface类,以节省内存开销
This commit is contained in:
parent
ef52f5e7b8
commit
4d4e491985
@ -9,6 +9,8 @@ add_executable(VulkanTest main.cpp
|
||||
${RENDER_WINDOW_SOURCE}
|
||||
VKInstance.cpp
|
||||
VKCommandBuffer.cpp
|
||||
RenderSurfaceAttribute.cpp
|
||||
RenderSurfaceCreater.cpp
|
||||
RenderSurface.cpp)
|
||||
|
||||
target_link_libraries(VulkanTest PRIVATE ${ULRE} ${VULKAN_LIB} ${RENDER_WINDOW_LIBRARY})
|
||||
|
@ -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; i<family_count; i++)
|
||||
{
|
||||
vkGetPhysicalDeviceSurfaceSupportKHR(physical_device,i,surface,sp);
|
||||
++sp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device,surface,&surface_caps);
|
||||
|
||||
VkExtent2D swapchain_extent;
|
||||
if(surface_caps.currentExtent.width==0xFFFFFFFF)
|
||||
{
|
||||
swapchain_extent.width=win->GetWidth();
|
||||
swapchain_extent.height=win->GetHeight();
|
||||
|
||||
if(swapchain_extent.width<surface_caps.minImageExtent.width)swapchain_extent.width=surface_caps.minImageExtent.width;else
|
||||
if(swapchain_extent.width>surface_caps.maxImageExtent.width)swapchain_extent.width=surface_caps.maxImageExtent.width;
|
||||
|
||||
if(swapchain_extent.height<surface_caps.minImageExtent.height)swapchain_extent.height=surface_caps.minImageExtent.height;else
|
||||
if(swapchain_extent.height>surface_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;i<count;i++)
|
||||
{
|
||||
if((*sp)
|
||||
&&(fp->queueFlags&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
|
||||
|
@ -4,49 +4,36 @@
|
||||
#include<hgl/type/List.h>
|
||||
#include"VK.h"
|
||||
#include"Window.h"
|
||||
#include"RenderSurfaceAttribute.h"
|
||||
#include"VKCommandBuffer.h"
|
||||
|
||||
VK_NAMESPACE_BEGIN
|
||||
|
||||
using RefRenderSurfaceAttribute=SharedPtr<RenderSurfaceAttribute>;
|
||||
|
||||
class RenderSurface
|
||||
{
|
||||
Window *win;
|
||||
VkInstance instance;
|
||||
VkPhysicalDevice physical_device;
|
||||
VkSurfaceKHR surface;
|
||||
RefRenderSurfaceAttribute rsa;
|
||||
|
||||
VkPhysicalDeviceFeatures features;
|
||||
VkPhysicalDeviceProperties properties;
|
||||
VkPhysicalDeviceMemoryProperties memory_properties;
|
||||
private:
|
||||
|
||||
List<VkQueueFamilyProperties> family_properties;
|
||||
List<VkBool32> supports_present;
|
||||
friend RenderSurface *CreateRenderSuface(VkInstance,VkPhysicalDevice,Window *);
|
||||
|
||||
List<VkSurfaceFormatKHR> surface_formts;
|
||||
VkSurfaceCapabilitiesKHR surface_caps;
|
||||
List<VkPresentModeKHR> 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
|
||||
|
74
example/Vulkan/RenderSurfaceAttribute.cpp
Normal file
74
example/Vulkan/RenderSurfaceAttribute.cpp
Normal file
@ -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; i<family_count; i++)
|
||||
{
|
||||
vkGetPhysicalDeviceSurfaceSupportKHR(physical_device,i,surface,sp);
|
||||
|
||||
if(family_index==-1)
|
||||
{
|
||||
if(*sp&&(fp->queueFlags&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
|
34
example/Vulkan/RenderSurfaceAttribute.h
Normal file
34
example/Vulkan/RenderSurfaceAttribute.h
Normal file
@ -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<VkQueueFamilyProperties> family_properties;
|
||||
List<VkBool32> supports_present;
|
||||
|
||||
VkPhysicalDeviceFeatures features;
|
||||
VkPhysicalDeviceProperties properties;
|
||||
VkPhysicalDeviceMemoryProperties memory_properties;
|
||||
|
||||
List<VkSurfaceFormatKHR> surface_formts;
|
||||
List<VkPresentModeKHR> present_modes;
|
||||
|
||||
VkDevice device =nullptr;
|
||||
VkCommandPool cmd_pool =nullptr;
|
||||
|
||||
public:
|
||||
|
||||
RenderSurfaceAttribute(VkInstance inst,VkPhysicalDevice pd,VkSurfaceKHR s);
|
||||
~RenderSurfaceAttribute();
|
||||
};//class RenderSurfaceAttribute
|
||||
VK_NAMESPACE_END
|
109
example/Vulkan/RenderSurfaceCreater.cpp
Normal file
109
example/Vulkan/RenderSurfaceCreater.cpp
Normal file
@ -0,0 +1,109 @@
|
||||
#include"RenderSurface.h"
|
||||
#include"VKInstance.h"
|
||||
#include<hgl/type/Smart.h>
|
||||
|
||||
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.width<surface_caps.minImageExtent.width)swapchain_extent.width=surface_caps.minImageExtent.width;else
|
||||
if(swapchain_extent.width>surface_caps.maxImageExtent.width)swapchain_extent.width=surface_caps.maxImageExtent.width;
|
||||
|
||||
if(swapchain_extent.height<surface_caps.minImageExtent.height)swapchain_extent.height=surface_caps.minImageExtent.height;else
|
||||
if(swapchain_extent.height>surface_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
|
@ -1,9 +1,12 @@
|
||||
#ifndef HGL_GRAPH_VULKAN_INCLUDE
|
||||
#define HGL_GRAPH_VULKAN_INCLUDE
|
||||
|
||||
#include<hgl/type/List.h>
|
||||
#include<vulkan/vulkan.h>
|
||||
|
||||
#define VK_NAMESPACE_BEGIN namespace hgl{namespace graph{namespace vulkan{
|
||||
#define VK_NAMESPACE_END }}}
|
||||
|
||||
using CharPointerList=hgl::List<const char *>;
|
||||
|
||||
#endif//HGL_GRAPH_VULKAN_INCLUDE
|
||||
|
@ -1,12 +1,15 @@
|
||||
#include"VKInstance.h"
|
||||
#include"VKSurfaceExtensionName.h"
|
||||
#include<hgl/type/DataType.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -4,36 +4,40 @@
|
||||
#include<hgl/type/BaseString.h>
|
||||
#include<hgl/type/List.h>
|
||||
#include"Window.h"
|
||||
#include"VK.h"
|
||||
#include"RenderSurface.h"
|
||||
|
||||
VK_NAMESPACE_BEGIN
|
||||
class Instance
|
||||
{
|
||||
Window *win;
|
||||
|
||||
List<const char *> ext_list;
|
||||
|
||||
VkApplicationInfo app_info;
|
||||
VkInstanceCreateInfo inst_info;
|
||||
|
||||
VkInstance inst;
|
||||
|
||||
CharPointerList ext_list;
|
||||
|
||||
List<VkPhysicalDevice> 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<VkPhysicalDevice> & GetDeviceList()const{return physical_devices;}
|
||||
const CharPointerList & GetExtList ()const {return ext_list;}
|
||||
const List<VkPhysicalDevice> &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
|
||||
|
26
example/Vulkan/VKSurfaceExtensionName.h
Normal file
26
example/Vulkan/VKSurfaceExtensionName.h
Normal file
@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include"VK.h"
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include<vulkan/vulkan_android.h>
|
||||
#define HGL_VK_SURFACE_EXTENSION_NAME VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
|
||||
#elif defined(_WIN32)
|
||||
#include<Windows.h>
|
||||
#include<vulkan/vulkan_win32.h>
|
||||
#define HGL_VK_SURFACE_EXTENSION_NAME VK_KHR_WIN32_SURFACE_EXTENSION_NAME
|
||||
#elif defined(VK_USE_PLATFORM_IOS_MVK)
|
||||
#include<vulkan/vulkan_ios.h>
|
||||
#define HGL_VK_SURFACE_EXTENSION_NAME VK_MVK_IOS_SURFACE_EXTENSION_NAME
|
||||
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
|
||||
#include<vulkan/vulkan_macos.h>
|
||||
#define HGL_VK_SURFACE_EXTENSION_NAME VK_MVK_MACOS_SURFACE_EXTENSION_NAME
|
||||
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
|
||||
#include<vulkan/vulkan_wayland.h>
|
||||
#define HGL_VK_SURFACE_EXTENSION_NAME VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
|
||||
#else
|
||||
#include<xcb/xcb.h>
|
||||
#include<xcb/xcb_atom.h>
|
||||
#include<vulkan/vulkan_xcb.h>
|
||||
#define HGL_VK_SURFACE_EXTENSION_NAME VK_KHR_XCB_SURFACE_EXTENSION_NAME
|
||||
#endif
|
@ -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;
|
||||
|
@ -2,7 +2,6 @@
|
||||
#define HGL_GRAPH_WINDOW_INCLUDE
|
||||
|
||||
#include<hgl/type/BaseString.h>
|
||||
#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);
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include"Window.h"
|
||||
#include<xcb/xcb.h>
|
||||
#include<xcb/xcb_atom.h>
|
||||
#include"VK.h"
|
||||
#include<vulkan/vk_sdk_platform.h>
|
||||
#include<vulkan/vulkan.h>
|
||||
#include<vulkan/vulkan_xcb.h>
|
||||
@ -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,13 +107,30 @@ namespace hgl
|
||||
void Show()override{}
|
||||
void Hide()override{}
|
||||
|
||||
VkSurfaceKHR CreateSurface(VkInstance vk_inst)const override
|
||||
public:
|
||||
|
||||
xcb_connection_t *GetConnection(){return connection;}
|
||||
xcb_window_t GetWindow(){return window;}
|
||||
|
||||
};//class XCBWindow:public Window
|
||||
|
||||
Window *CreateRenderWindow(const UTF8String &win_name)
|
||||
{
|
||||
return(new XCBWindow(win_name));
|
||||
}
|
||||
}//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 = connection;
|
||||
createInfo.window = window;
|
||||
createInfo.connection = xcb_win->GetConnection();
|
||||
createInfo.window = xcb_win->GetWindow();
|
||||
|
||||
VkSurfaceKHR surface;
|
||||
|
||||
@ -127,12 +140,5 @@ namespace hgl
|
||||
return(nullptr);
|
||||
|
||||
return(surface);
|
||||
}
|
||||
};//class XCBWindow:public Window
|
||||
|
||||
Window *CreateRenderWindow(const UTF8String &win_name)
|
||||
{
|
||||
return(new XCBWindow(win_name));
|
||||
}
|
||||
}//namespace graph
|
||||
}//namespace hgl
|
||||
}
|
||||
VK_NAMESPACE_END
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user