diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index 5cd969f5..41afad94 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -66,7 +66,7 @@ public: public: - const uint32_t GetSwapChainImageCount ()const {return attr->sc_image_views.GetCount();} + const uint32_t GetSwapChainImageCount ()const {return attr->sc_texture.GetCount();} //ImageView *GetColorImageView (int index) {return attr->sc_image_views[index];} //ImageView *GetDepthImageView () {return attr->depth.view;} @@ -109,8 +109,6 @@ public: //Buffer相关 public: //material相关 - Texture2D *CreateRefTexture2D(uint32_t width,uint32_t height,VkFormat format,VkImageAspectFlagBits flag,VkImage image,VkImageLayout image_layout,VkImageView image_view); - Texture2D *CreateTexture2D(const VkFormat video_format,uint32_t width,uint32_t height,const VkImageAspectFlags aspectMask,const uint usage,const VkImageLayout image_layout); Texture2D *CreateTexture2DColor(const VkFormat video_format,uint32_t width,uint32_t height) diff --git a/inc/hgl/graph/vulkan/VKDeviceAttribute.h b/inc/hgl/graph/vulkan/VKDeviceAttribute.h index a8a8e885..03589c49 100644 --- a/inc/hgl/graph/vulkan/VKDeviceAttribute.h +++ b/inc/hgl/graph/vulkan/VKDeviceAttribute.h @@ -1,7 +1,6 @@ #pragma once -#include -#include +#include VK_NAMESPACE_BEGIN @@ -36,9 +35,7 @@ struct DeviceAttribute VkCommandPool cmd_pool =nullptr; VkSwapchainKHR swap_chain =nullptr; - List sc_images; - ObjectList sc_image_views; - + ObjectList sc_texture; Texture2D * sc_depth =nullptr; VkDescriptorPool desc_pool =nullptr; diff --git a/inc/hgl/graph/vulkan/VKImageView.h b/inc/hgl/graph/vulkan/VKImageView.h index 1ffb1537..912674c4 100644 --- a/inc/hgl/graph/vulkan/VKImageView.h +++ b/inc/hgl/graph/vulkan/VKImageView.h @@ -16,7 +16,7 @@ protected: private: - friend ImageView *CreateRefImageView(VkDevice device,VkImageViewType type,VkFormat format,VkImageAspectFlags aspectMask,VkImageView img_view); + friend ImageView *CreateImageView(VkDevice device,VkImageViewType type,VkFormat format,VkImageAspectFlags aspectMask,VkImage img); ImageView(VkDevice dev,VkImageView iv,const VkImageViewType vt,const VkFormat fmt,const VkImageAspectFlags am) { @@ -29,7 +29,7 @@ private: public: - virtual ~ImageView()=default; + virtual ~ImageView(); operator VkImageView(){return image_view;} @@ -40,15 +40,7 @@ public: const VkImageAspectFlags GetAspectFlags ()const{return aspect_mask;} };//class ImageView -/** - * 创建一个引用ImageView,它的特点是不会自动删除VkImageView - */ -ImageView *CreateRefImageView(VkDevice device,VkImageViewType type,VkFormat format,VkImageAspectFlags aspectMask,VkImageView img_view); - -/** - * 根据VkImage创建一个VkImageView,在删除它时,它也会删除VkImageView - */ -ImageView *CreateImageView(VkDevice device,VkImageViewType type,VkFormat format,VkImageAspectFlags aspectMask,VkImage img=nullptr); +ImageView *CreateImageView(VkDevice device,VkImageViewType type,VkFormat format,VkImageAspectFlags aspectMask,VkImage img); #define CREATE_IMAGE_VIEW(short_name,larget_name) inline ImageView *CreateImageView##short_name(VkDevice device,VkFormat format,VkImageAspectFlags aspectMask,VkImage img=nullptr){return CreateImageView(device,VK_IMAGE_VIEW_TYPE_##larget_name,format,aspectMask,img);} CREATE_IMAGE_VIEW(1D,1D); diff --git a/inc/hgl/graph/vulkan/VKTexture.h b/inc/hgl/graph/vulkan/VKTexture.h index d11af3be..1a24293a 100644 --- a/inc/hgl/graph/vulkan/VKTexture.h +++ b/inc/hgl/graph/vulkan/VKTexture.h @@ -16,8 +16,6 @@ struct TextureData VkFormat format =VK_FORMAT_UNDEFINED; VkImageAspectFlags aspect =0; VkExtent3D extent; - - bool ref =false; //是否是引用 };//struct TextureData class Texture @@ -100,6 +98,7 @@ 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 diff --git a/src/RenderDevice/Vulkan/VKDevice.cpp b/src/RenderDevice/Vulkan/VKDevice.cpp index db0ce2e2..fd79919f 100644 --- a/src/RenderDevice/Vulkan/VKDevice.cpp +++ b/src/RenderDevice/Vulkan/VKDevice.cpp @@ -75,13 +75,13 @@ void Device::RecreateDevice() present_info.pSwapchains=&attr->swap_chain; - main_rp=CreateRenderPass(attr->sc_image_views[0]->GetFormat(),attr->sc_depth->GetFormat()); + main_rp=CreateRenderPass(attr->format,attr->sc_depth->GetFormat()); - swap_chain_count=attr->sc_image_views.GetCount(); + swap_chain_count=attr->sc_texture.GetCount(); for(uint i=0;isc_image_views[i],*attr->sc_depth)); + render_frame.Add(vulkan::CreateFramebuffer(this,main_rp,*attr->sc_texture[i],*attr->sc_depth)); fence_list.Add(this->CreateFence(true)); } diff --git a/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp b/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp index 2f5f9d7f..6191ee18 100644 --- a/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp @@ -43,8 +43,7 @@ bool DeviceAttribute::CheckMemoryType(uint32_t typeBits,VkMemoryPropertyFlags pr void DeviceAttribute::ClearSwapchain() { SAFE_CLEAR(sc_depth); - - sc_image_views.Clear(); + sc_texture.Clear(); if(swap_chain) { diff --git a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp index 03c38b97..1d8033d6 100644 --- a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp @@ -149,31 +149,35 @@ namespace return CreateImageView(device,VK_IMAGE_VIEW_TYPE_2D,format,VK_IMAGE_ASPECT_DEPTH_BIT,img); } - bool CreateSwapchainImageView(DeviceAttribute *rsa) + bool CreateSwapchainTexture(DeviceAttribute *rsa) { uint32_t count; if(vkGetSwapchainImagesKHR(rsa->device,rsa->swap_chain,&count,nullptr)!=VK_SUCCESS) return(false); - rsa->sc_images.SetCount(count); + VkImage *sc_images=new VkImage[count]; - if(vkGetSwapchainImagesKHR(rsa->device,rsa->swap_chain,&count,rsa->sc_images.GetData())!=VK_SUCCESS) + if(vkGetSwapchainImagesKHR(rsa->device,rsa->swap_chain,&count,sc_images)!=VK_SUCCESS) { - rsa->sc_images.Clear(); + delete sc_images; return(false); } - VkImage *ip=rsa->sc_images.GetData(); - ImageView *vp; + VkImage *ip=sc_images; + Texture2D *tex; + for(uint32_t i=0; idevice,rsa->format,*ip); + tex=VK_NAMESPACE::CreateTexture2D( rsa->device, + rsa->format, + rsa->swapchain_extent.width, + rsa->swapchain_extent.height, + VK_IMAGE_ASPECT_COLOR_BIT, + *ip, + VK_IMAGE_LAYOUT_UNDEFINED); - if(vp==nullptr) - return(false); - - rsa->sc_image_views.Add(vp); + rsa->sc_texture.Add(tex); ++ip; } @@ -183,66 +187,10 @@ namespace bool CreateDepthBuffer(DeviceAttribute *rsa) { - //VkImageCreateInfo image_info={}; - const VkFormat depth_format=VK_FORMAT_D16_UNORM; const VkFormatProperties props=rsa->physical_device->GetFormatProperties(depth_format); - //if(props.linearTilingFeatures&VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) - // image_info.tiling=VK_IMAGE_TILING_LINEAR; - //else - //if(props.optimalTilingFeatures&VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) - // image_info.tiling=VK_IMAGE_TILING_OPTIMAL; - //else - // return(false); - - //image_info.sType=VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; - //image_info.pNext=nullptr; - //image_info.imageType=VK_IMAGE_TYPE_2D; - //image_info.format=depth_format; - //image_info.extent.width=rsa->swapchain_extent.width; - //image_info.extent.height=rsa->swapchain_extent.height; - //image_info.extent.depth=1; - //image_info.mipLevels=1; - //image_info.arrayLayers=1; - //image_info.samples=VK_SAMPLE_COUNT_1_BIT; - //image_info.initialLayout=VK_IMAGE_LAYOUT_UNDEFINED; - //image_info.usage=VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; - //image_info.queueFamilyIndexCount=0; - //image_info.pQueueFamilyIndices=nullptr; - //image_info.sharingMode=VK_SHARING_MODE_EXCLUSIVE; - //image_info.flags=0; - - //rsa->depth.format=depth_format; - - //if(vkCreateImage(rsa->device,&image_info,nullptr,&rsa->depth.image)!=VK_SUCCESS) - // return(false); - - //VkMemoryRequirements mem_reqs; - //vkGetImageMemoryRequirements(rsa->device,rsa->depth.image,&mem_reqs); - - //VkMemoryAllocateInfo mem_alloc={}; - //mem_alloc.sType=VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - //mem_alloc.pNext=nullptr; - //mem_alloc.allocationSize=0; - //mem_alloc.memoryTypeIndex=0; - //mem_alloc.allocationSize=mem_reqs.size; - - //if(!rsa->CheckMemoryType(mem_reqs.memoryTypeBits,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,&mem_alloc.memoryTypeIndex)) - // return(false); - - //if(vkAllocateMemory(rsa->device,&mem_alloc,nullptr,&rsa->depth.mem)!=VK_SUCCESS) - // return(false); - - //if(vkBindImageMemory(rsa->device,rsa->depth.image,rsa->depth.mem,0)!=VK_SUCCESS) - // return(false); - - //rsa->depth.view=CreateDepthImageView(rsa->device,depth_format,rsa->depth.image); - - //if(rsa->depth.view==nullptr) - // return(false); - rsa->sc_depth=VK_NAMESPACE::CreateTexture2D(rsa->device,rsa->physical_device, depth_format, rsa->swapchain_extent.width, @@ -304,7 +252,7 @@ namespace if(!attr->swap_chain) return(false); - if(!CreateSwapchainImageView(attr)) + if(!CreateSwapchainTexture(attr)) return(false); if(!CreateDepthBuffer(attr)) diff --git a/src/RenderDevice/Vulkan/VKDeviceTexture.cpp b/src/RenderDevice/Vulkan/VKDeviceTexture.cpp index d5852a99..50bea1a1 100644 --- a/src/RenderDevice/Vulkan/VKDeviceTexture.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceTexture.cpp @@ -42,29 +42,6 @@ Texture2D *Device::CreateTexture2D(const VkFormat format,void *data,uint32_t wid return(tex); } -Texture2D *Device::CreateRefTexture2D(uint32_t width,uint32_t height,VkFormat format,VkImageAspectFlagBits aspectMask,VkImage image,VkImageLayout image_layout,VkImageView image_view) -{ - TextureData *tex_data=new TextureData; - - tex_data->ref =true; - - tex_data->memory =nullptr; - tex_data->image =image; - tex_data->image_layout =image_layout; - tex_data->image_view =CreateRefImageView(attr->device,VK_IMAGE_VIEW_TYPE_2D,format,aspectMask,image_view); - - tex_data->mip_levels =0; - tex_data->linear =false; - tex_data->format =format; - tex_data->aspect =aspectMask; - - tex_data->extent.width =width; - tex_data->extent.height =height; - tex_data->extent.depth =1; - - return(new Texture2D(width,height,attr->device,tex_data)); -} - bool Device::ChangeTexture2D(Texture2D *tex,void *data,uint32_t left,uint32_t top,uint32_t width,uint32_t height,uint32_t size) { if(!tex||!data diff --git a/src/RenderDevice/Vulkan/VKImageView.cpp b/src/RenderDevice/Vulkan/VKImageView.cpp index be2af9ec..fa309ce9 100644 --- a/src/RenderDevice/Vulkan/VKImageView.cpp +++ b/src/RenderDevice/Vulkan/VKImageView.cpp @@ -1,23 +1,9 @@ #include VK_NAMESPACE_BEGIN -class StandaloneImageView:public ImageView +ImageView::~ImageView() { - friend ImageView *CreateImageView(VkDevice device,VkImageViewType type,VkFormat format,VkImageAspectFlags aspectMask,VkImage img); - - using ImageView::ImageView; - -public: - - ~StandaloneImageView() - { - vkDestroyImageView(device,image_view,nullptr); - } -}; - -ImageView *CreateRefImageView(VkDevice device,VkImageViewType type,VkFormat format,VkImageAspectFlags aspectMask,VkImageView img_view) -{ - return(new ImageView(device,img_view,type,format,aspectMask)); + vkDestroyImageView(device,image_view,nullptr); } ImageView *CreateImageView(VkDevice device,VkImageViewType type,VkFormat format,VkImageAspectFlags aspectMask,VkImage img) @@ -45,6 +31,6 @@ ImageView *CreateImageView(VkDevice device,VkImageViewType type,VkFormat format, if(vkCreateImageView(device,&iv_createinfo,nullptr,&img_view)!=VK_SUCCESS) return(nullptr); - return(new StandaloneImageView(device,img_view,type,format,aspectMask)); + return(new ImageView(device,img_view,type,format,aspectMask)); } VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKTexture.cpp b/src/RenderDevice/Vulkan/VKTexture.cpp index 95d2aaa0..a5bd6748 100644 --- a/src/RenderDevice/Vulkan/VKTexture.cpp +++ b/src/RenderDevice/Vulkan/VKTexture.cpp @@ -9,13 +9,34 @@ Texture::~Texture() if(data->image_view) delete data->image_view; - if(data->ref)return; - - if(data->image) - vkDestroyImage(device,data->image,nullptr); - - if(data->memory) + if(data->memory) //没有memory的纹理都是从其它地方借来的,所以就不存在删除 + { delete data->memory; + + if(data->image) + vkDestroyImage(device,data->image,nullptr); + } +} + +Texture2D *CreateTexture2D(VkDevice device,VkFormat format,uint32_t width,uint32_t height,VkImageAspectFlagBits aspectMask,VkImage image,VkImageLayout image_layout) +{ + TextureData *tex_data=new TextureData; + + tex_data->memory =nullptr; + tex_data->image =image; + tex_data->image_layout =image_layout; + tex_data->image_view =CreateImageView(device,VK_IMAGE_VIEW_TYPE_2D,format,aspectMask,image); + + tex_data->mip_levels =0; + tex_data->linear =false; + tex_data->format =format; + tex_data->aspect =aspectMask; + + tex_data->extent.width =width; + tex_data->extent.height =height; + tex_data->extent.depth =1; + + return(new Texture2D(width,height,device,tex_data)); } 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) @@ -62,7 +83,6 @@ Texture2D *CreateTexture2D(VkDevice device,const PhysicalDevice *pd,const VkForm { TextureData *tex_data=new TextureData; - tex_data->ref = false; tex_data->mip_levels = 0; tex_data->memory = dm; tex_data->image_layout = image_layout;