ULRE/src/SceneGraph/Vulkan/VKDevice.cpp

153 lines
3.5 KiB
C++
Raw Normal View History

#include<hgl/graph/VKDevice.h>
#include<hgl/type/Pair.h>
#include<hgl/graph/VKSemaphore.h>
#include<hgl/graph/VKTexture.h>
#include<hgl/graph/VKImageView.h>
#include<hgl/graph/VKPipeline.h>
#include<hgl/graph/VKCommandBuffer.h>
//#include<hgl/graph/VKDescriptorSet.h>
#include<hgl/graph/VKRenderPass.h>
#include<hgl/graph/VKFramebuffer.h>
#include<hgl/graph/VKDescriptorSets.h>
#include<hgl/graph/VKDeviceRenderPassManage.h>
VK_NAMESPACE_BEGIN
2021-09-23 19:08:07 +08:00
void LogSurfaceFormat(const List<VkSurfaceFormatKHR> &surface_format_list)
{
const uint32_t format_count=surface_format_list.GetCount();
const VkSurfaceFormatKHR *sf=surface_format_list.GetData();
LOG_INFO(OS_TEXT("Current physics device support ")+OSString::valueOf(format_count)+OS_TEXT(" surface format"));
const VulkanFormat *vf;
const VulkanColorSpace *cs;
for(uint32_t i=0;i<format_count;i++)
{
vf=GetVulkanFormat(sf->format);
cs=GetVulkanColorSpace(sf->colorSpace);
LOG_INFO(" "+AnsiString::valueOf(i)+": "+AnsiString(vf->name)+", "+AnsiString(cs->name));
++sf;
}
}
2020-10-21 12:39:22 +08:00
GPUDevice::GPUDevice(GPUDeviceAttribute *da)
2019-04-19 20:10:59 +08:00
{
attr=da;
2021-03-25 20:00:19 +08:00
texture_queue=nullptr;
2019-06-14 10:32:43 +08:00
texture_cmd_buf=nullptr;
InitRenderPassManage();
swapchain=nullptr;
swapchainRT=nullptr;
Resize(attr->surface_caps.currentExtent);
2021-09-23 19:08:07 +08:00
LogSurfaceFormat(attr->surface_formats_list);
2019-04-19 20:10:59 +08:00
}
2020-10-21 12:39:22 +08:00
GPUDevice::~GPUDevice()
2019-04-18 22:24:39 +08:00
{
SAFE_CLEAR(swapchainRT);
SAFE_CLEAR(swapchain);
ClearRenderPassManage();
2021-03-25 20:00:19 +08:00
SAFE_CLEAR(texture_queue);
SAFE_CLEAR(texture_cmd_buf);
2019-04-20 16:12:22 +08:00
2019-04-18 22:24:39 +08:00
delete attr;
}
2020-10-21 12:39:22 +08:00
bool GPUDevice::Resize(const VkExtent2D &extent)
{
SAFE_CLEAR(swapchainRT);
SAFE_CLEAR(swapchain);
2019-06-14 10:32:43 +08:00
2021-03-25 20:00:19 +08:00
SAFE_CLEAR(texture_queue);
SAFE_CLEAR(texture_cmd_buf);
2019-07-17 04:49:16 +08:00
attr->Refresh();
2019-11-26 00:33:24 +08:00
if(!CreateSwapchain(extent))
return(false);
texture_cmd_buf=CreateTextureCommandBuffer();
2021-03-25 20:00:19 +08:00
texture_queue=new GPUQueue(this,attr->graphics_queue,1);
swapchainRT=new SwapchainRenderTarget(this,swapchain);
return(true);
}
VkCommandBuffer GPUDevice::CreateCommandBuffer()
{
2019-04-18 22:24:39 +08:00
if(!attr->cmd_pool)
return(VK_NULL_HANDLE);
2020-09-27 20:58:25 +08:00
CommandBufferAllocateInfo cmd;
2019-06-20 21:41:40 +08:00
cmd.commandPool =attr->cmd_pool;
cmd.level =VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmd.commandBufferCount =1;
VkCommandBuffer cmd_buf;
2019-04-18 22:24:39 +08:00
VkResult res=vkAllocateCommandBuffers(attr->device,&cmd,&cmd_buf);
if(res!=VK_SUCCESS)
return(VK_NULL_HANDLE);
return cmd_buf;
}
RenderCmdBuffer *GPUDevice::CreateRenderCommandBuffer()
{
VkCommandBuffer cb=CreateCommandBuffer();
if(cb==VK_NULL_HANDLE)return(nullptr);
return(new RenderCmdBuffer(attr,cb));
}
TextureCmdBuffer *GPUDevice::CreateTextureCommandBuffer()
{
VkCommandBuffer cb=CreateCommandBuffer();
if(cb==VK_NULL_HANDLE)return(nullptr);
return(new TextureCmdBuffer(attr,cb));
}
2019-06-26 11:26:38 +08:00
/**
*
* @param create_signaled
*/
2020-10-21 12:39:22 +08:00
GPUFence *GPUDevice::CreateFence(bool create_signaled)
2019-04-19 19:58:01 +08:00
{
2020-09-27 20:58:25 +08:00
FenceCreateInfo fenceInfo(create_signaled?VK_FENCE_CREATE_SIGNALED_BIT:0);
2019-04-19 19:58:01 +08:00
2019-04-20 16:12:22 +08:00
VkFence fence;
2019-04-19 19:58:01 +08:00
2019-04-20 16:12:22 +08:00
if(vkCreateFence(attr->device, &fenceInfo, nullptr, &fence)!=VK_SUCCESS)
2019-04-19 19:58:01 +08:00
return(nullptr);
2020-10-21 12:09:15 +08:00
return(new GPUFence(attr->device,fence));
2019-04-19 19:58:01 +08:00
}
2019-04-19 20:04:08 +08:00
2021-09-14 20:31:15 +08:00
GPUSemaphore *GPUDevice::CreateGPUSemaphore()
2019-04-19 20:04:08 +08:00
{
2020-09-27 20:58:25 +08:00
SemaphoreCreateInfo SemaphoreCreateInfo;
2019-04-19 20:04:08 +08:00
VkSemaphore sem;
2020-09-27 20:58:25 +08:00
2019-04-19 20:04:08 +08:00
if(vkCreateSemaphore(attr->device, &SemaphoreCreateInfo, nullptr, &sem)!=VK_SUCCESS)
return(nullptr);
2020-10-21 12:47:06 +08:00
return(new GPUSemaphore(attr->device,sem));
2019-04-19 20:04:08 +08:00
}
VK_NAMESPACE_END