diff --git a/example/Vulkan/CMakeLists.txt b/example/Vulkan/CMakeLists.txt index ff06bada..08fa592d 100644 --- a/example/Vulkan/CMakeLists.txt +++ b/example/Vulkan/CMakeLists.txt @@ -1,7 +1,7 @@ macro(CreateProject name) add_executable(${name} ${ARGN} VulkanAppFramework.h) target_link_libraries(${name} ${ULRE}) - + IF(WIN32) set_target_properties(${name} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${ULRE_RUNTIME_PATH}) ENDIF() @@ -12,7 +12,7 @@ endmacro() CreateProject(00.triangle first_triangle.cpp) CreateProject(01.indices_rect indices_rect.cpp) CreateProject(02.1.texture_rect texture_rect.cpp TGATexture.cpp) -CreateProject(02.2.texture_linear texture_linear.cpp TGATexture.cpp) +#CreateProject(02.2.texture_linear texture_linear.cpp TGATexture.cpp) CreateProject(03.HQFilterTexture HQFilterTexture.cpp TGATexture.cpp) CreateProject(04.Geometry2D Geometry2D.cpp) CreateProject(05.Geometry3D Geometry3D.cpp) @@ -29,6 +29,6 @@ CreateProject(10.PBRBasic PBRBasic.cpp) CreateProject(11.Deferred Deferred.cpp TGATexture.cpp) -CreateProject(12.DeferredModel DeferredModel.cpp TGATexture.cpp) +#CreateProject(12.DeferredModel DeferredModel.cpp TGATexture.cpp) CreateProject(13.TextureFormat TextureFormat.cpp) diff --git a/example/Vulkan/TGATexture.cpp b/example/Vulkan/TGATexture.cpp index 68428f6a..307422fe 100644 --- a/example/Vulkan/TGATexture.cpp +++ b/example/Vulkan/TGATexture.cpp @@ -260,7 +260,7 @@ Texture2D *LoadTGATexture(const OSString &filename,Device *device,bool use_optim } else { - device->CreateTexture2DLinear(format,buf,header.width, header.height); + //device->CreateTexture2DLinear(format,buf,header.width, header.height); } if(tex) diff --git a/example/Vulkan/texture_rect.cpp b/example/Vulkan/texture_rect.cpp index 8f090f65..53898b73 100644 --- a/example/Vulkan/texture_rect.cpp +++ b/example/Vulkan/texture_rect.cpp @@ -10,7 +10,7 @@ using namespace hgl; using namespace hgl::graph; VK_NAMESPACE_BEGIN -Texture2D *LoadTGATexture(const OSString &filename,Device *device); +Texture2D *LoadTGATexture(const OSString &filename,Device *device,bool use_optimal=true); VK_NAMESPACE_END constexpr uint32_t SCREEN_WIDTH=128; diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index 49b68f6e..f6629db6 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -22,6 +22,11 @@ class Device Swapchain *swapchain; SwapchainRenderTarget *swapchainRT; + bool CreateSwapchainColorTexture(); + bool CreateSwapchainDepthTexture(); + + Swapchain *CreateSwapchain(const VkExtent2D &acquire_extent); + private: friend Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,VkSurfaceKHR surface,const VkExtent2D &extent); diff --git a/inc/hgl/graph/vulkan/VKTexture.h b/inc/hgl/graph/vulkan/VKTexture.h index ca14ddfb..075bde9c 100644 --- a/inc/hgl/graph/vulkan/VKTexture.h +++ b/inc/hgl/graph/vulkan/VKTexture.h @@ -93,8 +93,5 @@ public: // uint32_t width,height,count; //};//class TextureCubemapArray:public Texture -Texture2D *CreateTexture2D(VkDevice device,VkFormat format,uint32_t width,uint32_t height,VkImageAspectFlagBits aspectMask,VkImage image,VkImageLayout image_layout); -Texture2D *CreateTexture2D(VkDevice device,const PhysicalDevice *pd,const VkFormat format,uint32_t width,uint32_t height,const VkImageAspectFlags aspectMask,const uint usage,const VkImageLayout image_layout,const VkImageTiling tiling); - VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_TEXTURE_INCLUDE diff --git a/src/RenderDevice/Vulkan/CMakeLists.txt b/src/RenderDevice/Vulkan/CMakeLists.txt index 461a5715..14959f64 100644 --- a/src/RenderDevice/Vulkan/CMakeLists.txt +++ b/src/RenderDevice/Vulkan/CMakeLists.txt @@ -14,6 +14,7 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp VKDeviceBuffer.cpp VKDeviceImage.cpp VKDeviceTexture.cpp + VKDeviceSwapchain.cpp VKDeviceRenderPass.cpp VKBuffer.cpp VKDescriptorSets.cpp diff --git a/src/RenderDevice/Vulkan/VKDevice.cpp b/src/RenderDevice/Vulkan/VKDevice.cpp index b7c69049..d05371dc 100644 --- a/src/RenderDevice/Vulkan/VKDevice.cpp +++ b/src/RenderDevice/Vulkan/VKDevice.cpp @@ -11,8 +11,6 @@ #include VK_NAMESPACE_BEGIN -Swapchain *CreateSwapchain(const DeviceAttribute *attr,const VkExtent2D &acquire_extent); - Device::Device(DeviceAttribute *da) { attr=da; @@ -45,7 +43,7 @@ bool Device::Resize(const VkExtent2D &extent) SAFE_CLEAR(texture_cmd_buf); attr->Refresh(); - swapchain=CreateSwapchain(attr,extent); + swapchain=CreateSwapchain(extent); texture_cmd_buf=CreateCommandBuffer(extent,0); textureSQ=new SubmitQueue(this,attr->graphics_queue,1); diff --git a/src/RenderDevice/Vulkan/VKDeviceSwapchain.cpp b/src/RenderDevice/Vulkan/VKDeviceSwapchain.cpp new file mode 100644 index 00000000..9b48a3a6 --- /dev/null +++ b/src/RenderDevice/Vulkan/VKDeviceSwapchain.cpp @@ -0,0 +1,141 @@ +#include +#include +#include + +VK_NAMESPACE_BEGIN +namespace +{ + VkExtent2D SwapchainExtentClamp(const VkSurfaceCapabilitiesKHR &surface_caps,const VkExtent2D &acquire_extent) + { + VkExtent2D swapchain_extent; + + swapchain_extent.width =hgl_clamp(acquire_extent.width, surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width ); + swapchain_extent.height =hgl_clamp(acquire_extent.height, surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height ); + + return swapchain_extent; + } + + VkSwapchainKHR CreateSwapChain(const DeviceAttribute *dev_attr,const VkExtent2D &extent) + { + VkSwapchainCreateInfoKHR swapchain_ci; + + swapchain_ci.sType =VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; + swapchain_ci.pNext =nullptr; + swapchain_ci.flags =0; + swapchain_ci.surface =dev_attr->surface; + swapchain_ci.minImageCount =3;//rsa->surface_caps.minImageCount; + swapchain_ci.imageFormat =dev_attr->format; + swapchain_ci.imageColorSpace =VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; + swapchain_ci.imageExtent =extent; + swapchain_ci.imageArrayLayers =1; + swapchain_ci.imageUsage =VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; + swapchain_ci.queueFamilyIndexCount =0; + swapchain_ci.pQueueFamilyIndices =nullptr; + swapchain_ci.preTransform =dev_attr->preTransform; + swapchain_ci.compositeAlpha =dev_attr->compositeAlpha; + swapchain_ci.presentMode =VK_PRESENT_MODE_FIFO_KHR; + swapchain_ci.clipped =VK_TRUE; + swapchain_ci.oldSwapchain =VK_NULL_HANDLE; + + if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_SRC_BIT) + swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_SRC_BIT; + + if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_DST_BIT) + swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_DST_BIT; + + uint32_t queueFamilyIndices[2]={dev_attr->graphics_family, dev_attr->present_family}; + if(dev_attr->graphics_family!=dev_attr->present_family) + { + // If the graphics and present queues are from different queue families, + // we either have to explicitly transfer ownership of images between + // the queues, or we have to create the swapchain with imageSharingMode + // as VK_SHARING_MODE_CONCURRENT + swapchain_ci.imageSharingMode=VK_SHARING_MODE_CONCURRENT; + swapchain_ci.queueFamilyIndexCount=2; + swapchain_ci.pQueueFamilyIndices=queueFamilyIndices; + } + else + { + swapchain_ci.imageSharingMode=VK_SHARING_MODE_EXCLUSIVE; + } + + VkSwapchainKHR swap_chain; + + if(vkCreateSwapchainKHR(dev_attr->device,&swapchain_ci,nullptr,&swap_chain)==VK_SUCCESS) + return(swap_chain); + + return(VK_NULL_HANDLE); + } +}//namespace + +bool Device::CreateSwapchainColorTexture() +{ + if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->swap_chain_count),nullptr)!=VK_SUCCESS) + return(false); + + AutoDeleteArray sc_images=new VkImage[swapchain->swap_chain_count]; + + if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->swap_chain_count),sc_images)!=VK_SUCCESS) + { + delete sc_images; + return(false); + } + + VkImage *ip=sc_images; + Texture2D *tex; + + for(uint32_t i=0; iswap_chain_count; i++) + { + tex=CreateTexture2D(attr->format, + swapchain->extent.width, + swapchain->extent.height, + VK_IMAGE_ASPECT_COLOR_BIT, + *ip, + VK_IMAGE_LAYOUT_UNDEFINED); + + swapchain->sc_color.Add(tex); + + ++ip; + } + + return(true); +} + +bool Device::CreateSwapchainDepthTexture() +{ + const VkFormat depth_format=attr->physical_device->GetDepthFormat(); + + const VkFormatProperties props=attr->physical_device->GetFormatProperties(depth_format); + + swapchain->sc_depth=CreateTexture2D(depth_format, + swapchain->extent.width, + swapchain->extent.height, + VK_IMAGE_ASPECT_DEPTH_BIT, + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, + VK_IMAGE_LAYOUT_UNDEFINED, + (props.optimalTilingFeatures&VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)?VK_IMAGE_TILING_OPTIMAL:VK_IMAGE_TILING_LINEAR); + + return swapchain->sc_depth; +} + +Swapchain *Device::CreateSwapchain(const VkExtent2D &acquire_extent) +{ + AutoDelete sc=new Swapchain; + + sc->device =attr->device; + sc->extent =SwapchainExtentClamp(attr->surface_caps,acquire_extent); + sc->graphics_queue =attr->graphics_queue; + sc->swap_chain =CreateSwapChain(attr,sc->extent); + + if(!sc->swap_chain) + return(nullptr); + + if(!CreateSwapchainColorTexture()) + return(nullptr); + + if(!CreateSwapchainDepthTexture()) + return(nullptr); + + return sc.Finish(); +} +VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKSwapchain.cpp b/src/RenderDevice/Vulkan/VKSwapchain.cpp index 8d4bb3a2..66a6c816 100644 --- a/src/RenderDevice/Vulkan/VKSwapchain.cpp +++ b/src/RenderDevice/Vulkan/VKSwapchain.cpp @@ -1,6 +1,4 @@ #include -#include -#include VK_NAMESPACE_BEGIN Swapchain::~Swapchain() @@ -16,142 +14,4 @@ Swapchain::~Swapchain() swap_chain_count=0; } - -namespace -{ - VkExtent2D SwapchainExtentClamp(const VkSurfaceCapabilitiesKHR &surface_caps,const VkExtent2D &acquire_extent) - { - VkExtent2D swapchain_extent; - - swapchain_extent.width =hgl_clamp(acquire_extent.width, surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width ); - swapchain_extent.height =hgl_clamp(acquire_extent.height, surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height ); - - return swapchain_extent; - } - - VkSwapchainKHR CreateSwapChain(const DeviceAttribute *dev_attr,const VkExtent2D &extent) - { - VkSwapchainCreateInfoKHR swapchain_ci; - - swapchain_ci.sType =VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; - swapchain_ci.pNext =nullptr; - swapchain_ci.flags =0; - swapchain_ci.surface =dev_attr->surface; - swapchain_ci.minImageCount =3;//rsa->surface_caps.minImageCount; - swapchain_ci.imageFormat =dev_attr->format; - swapchain_ci.imageColorSpace =VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; - swapchain_ci.imageExtent =extent; - swapchain_ci.imageArrayLayers =1; - swapchain_ci.imageUsage =VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - swapchain_ci.queueFamilyIndexCount =0; - swapchain_ci.pQueueFamilyIndices =nullptr; - swapchain_ci.preTransform =dev_attr->preTransform; - swapchain_ci.compositeAlpha =dev_attr->compositeAlpha; - swapchain_ci.presentMode =VK_PRESENT_MODE_FIFO_KHR; - swapchain_ci.clipped =VK_TRUE; - swapchain_ci.oldSwapchain =VK_NULL_HANDLE; - - if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_SRC_BIT) - swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_SRC_BIT; - - if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_DST_BIT) - swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_DST_BIT; - - uint32_t queueFamilyIndices[2]={dev_attr->graphics_family, dev_attr->present_family}; - if(dev_attr->graphics_family!=dev_attr->present_family) - { - // If the graphics and present queues are from different queue families, - // we either have to explicitly transfer ownership of images between - // the queues, or we have to create the swapchain with imageSharingMode - // as VK_SHARING_MODE_CONCURRENT - swapchain_ci.imageSharingMode=VK_SHARING_MODE_CONCURRENT; - swapchain_ci.queueFamilyIndexCount=2; - swapchain_ci.pQueueFamilyIndices=queueFamilyIndices; - } - else - { - swapchain_ci.imageSharingMode=VK_SHARING_MODE_EXCLUSIVE; - } - - VkSwapchainKHR swap_chain; - - if(vkCreateSwapchainKHR(dev_attr->device,&swapchain_ci,nullptr,&swap_chain)==VK_SUCCESS) - return(swap_chain); - - return(VK_NULL_HANDLE); - } - - bool CreateSwapchainColorTexture(Swapchain *sa,const DeviceAttribute *dev_attr) - { - if(vkGetSwapchainImagesKHR(dev_attr->device,sa->swap_chain,&(sa->swap_chain_count),nullptr)!=VK_SUCCESS) - return(false); - - AutoDeleteArray sc_images=new VkImage[sa->swap_chain_count]; - - if(vkGetSwapchainImagesKHR(dev_attr->device,sa->swap_chain,&(sa->swap_chain_count),sc_images)!=VK_SUCCESS) - { - delete sc_images; - return(false); - } - - VkImage *ip=sc_images; - Texture2D *tex; - - for(uint32_t i=0; iswap_chain_count; i++) - { - tex=VK_NAMESPACE::CreateTexture2D( dev_attr->device, - dev_attr->format, - sa->extent.width, - sa->extent.height, - VK_IMAGE_ASPECT_COLOR_BIT, - *ip, - VK_IMAGE_LAYOUT_UNDEFINED); - - sa->sc_color.Add(tex); - - ++ip; - } - - return(true); - } - - bool CreateSwapchainDepthTexture(Swapchain *sa,const DeviceAttribute *dev_attr) - { - const VkFormat depth_format=dev_attr->physical_device->GetDepthFormat(); - - const VkFormatProperties props=dev_attr->physical_device->GetFormatProperties(depth_format); - - sa->sc_depth=VK_NAMESPACE::CreateTexture2D( dev_attr->device,dev_attr->physical_device, - depth_format, - sa->extent.width, - sa->extent.height, - VK_IMAGE_ASPECT_DEPTH_BIT, - VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, - VK_IMAGE_LAYOUT_UNDEFINED, - (props.optimalTilingFeatures&VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)?VK_IMAGE_TILING_OPTIMAL:VK_IMAGE_TILING_LINEAR); - - return sa->sc_depth; - } -}//namespace - -Swapchain *CreateSwapchain(const DeviceAttribute *attr,const VkExtent2D &acquire_extent) -{ - AutoDelete sc=new Swapchain; - - sc->device =attr->device; - sc->extent =SwapchainExtentClamp(attr->surface_caps,acquire_extent); - sc->graphics_queue =attr->graphics_queue; - sc->swap_chain =CreateSwapChain(attr,sc->extent); - - if(!sc->swap_chain) - return(nullptr); - - if(!CreateSwapchainColorTexture(sc,attr)) - return(nullptr); - - if(!CreateSwapchainDepthTexture(sc,attr)) - return(nullptr); - - return sc.Finish(); -} VK_NAMESPACE_END