diff --git a/example/Vulkan/AssimpLoaderMesh.cpp b/example/Vulkan/AssimpLoaderMesh.cpp index 25c5b291..b9f70844 100644 --- a/example/Vulkan/AssimpLoaderMesh.cpp +++ b/example/Vulkan/AssimpLoaderMesh.cpp @@ -1,4 +1,5 @@ #include"AssimpLoaderMesh.h" +#include #include #include #include @@ -48,25 +49,6 @@ namespace inline const vec pos_to_vec(const aiVector3D &v3d){return vec(v3d.x,v3d.y,v3d.z,1.0);} - void VecFlipY(List &target,const aiVector3D *source,const uint count) - { - target.SetCount(count); - - if(count<=0)return; - - Vector3f *tp=target.GetData(); - - for(uint i=0;ix=source->x; - tp->y=source->y; - tp->z=source->z; - - ++source; - ++tp; - } - } - void VecCopy(List &target,const aiVector3D *source,const uint count) { target.SetCount(count); @@ -159,8 +141,6 @@ class AssimpLoaderMesh ModelData *model_data; - Matrix4f OpenGLCoord2VulkanCoordMatrix; - private: void GetBoundingBox(const aiNode * node, @@ -199,7 +179,7 @@ private: void AssimpLoaderMesh::GetBoundingBox(const aiNode *node,aiVector3D *min_pos,aiVector3D *max_pos) { - Matrix4f root_matrix=OpenGLCoord2VulkanCoordMatrix; + Matrix4f root_matrix=hgl::graph::MATRIX_FROM_OPENGL_COORDINATE; min_pos->x = min_pos->y = min_pos->z = 1e10f; max_pos->x = max_pos->y = max_pos->z = -1e10f; @@ -211,8 +191,6 @@ public: AssimpLoaderMesh(const OSString &fn,const aiScene *s):filename(fn),scene(s) { - OpenGLCoord2VulkanCoordMatrix=scale(1,-1,1)*rotate(hgl_ang2rad(90),Vector3f(1,0,0)); - model_data=new ModelData; aiVector3D scene_min,scene_max; @@ -236,11 +214,11 @@ private: md->name=mesh->mName.C_Str(); if(mesh->HasPositions()) - VecFlipY(md->position,mesh->mVertices,mesh->mNumVertices); + VecCopy(md->position,mesh->mVertices,mesh->mNumVertices); if(mesh->HasNormals()) { - VecFlipY(md->normal,mesh->mNormals,mesh->mNumVertices); + VecCopy(md->normal,mesh->mNormals,mesh->mNumVertices); if(mesh->HasTangentsAndBitangents()) { @@ -325,7 +303,7 @@ public: if(!Load(model_data->root_node,scene->mRootNode)) return(false); - model_data->root_node->local_matrix=OpenGLCoord2VulkanCoordMatrix*model_data->root_node->local_matrix; + model_data->root_node->local_matrix=hgl::graph::MATRIX_FROM_OPENGL_COORDINATE*model_data->root_node->local_matrix; return(true); } };//class AssimpLoaderMesh diff --git a/inc/hgl/graph/Camera.h b/inc/hgl/graph/Camera.h index fe02479c..34f6e4cd 100644 --- a/inc/hgl/graph/Camera.h +++ b/inc/hgl/graph/Camera.h @@ -6,19 +6,6 @@ namespace hgl { namespace graph { - /* - * OpenGL Coordinate System Vulkan Coordinate System My Coordinate System - * - * /Z - * Y| /Z / Z| /Y - * | / / | / - * | / *------------ | / - * | / | X | / - * |/ | |/ - * *------------ | *------------ - * X | Y X - */ - /** * 摄像机类型 */ diff --git a/inc/hgl/graph/Coordinate.h b/inc/hgl/graph/Coordinate.h new file mode 100644 index 00000000..a53947f7 --- /dev/null +++ b/inc/hgl/graph/Coordinate.h @@ -0,0 +1,27 @@ +#ifndef HGL_GRAPH_COORDINATE_INCLUDE +#define HGL_GRAPH_COORDINATE_INCLUDE + +#include + +namespace hgl +{ + namespace graph + { + /* + * OpenGL Coordinate System Vulkan Coordinate System My Coordinate System + * + * /Z + * Y| /Z / Z| /Y + * | / / | / + * | / *------------ | / + * | / | X | / + * |/ | |/ + * *------------ | *------------ + * X | Y X + */ + + extern Matrix4f MATRIX_FROM_OPENGL_COORDINATE; //OpenGLϵݵҷϵݱ任þ + + }//namespace graph +}//namespace hgl +#endif//HGL_GRAPH_COORDINATE_INCLUDE diff --git a/inc/hgl/graph/vulkan/VKCommandBuffer.h b/inc/hgl/graph/vulkan/VKCommandBuffer.h index d0ce2b64..4cb0bae4 100644 --- a/inc/hgl/graph/vulkan/VKCommandBuffer.h +++ b/inc/hgl/graph/vulkan/VKCommandBuffer.h @@ -31,9 +31,9 @@ public: void SetRenderArea(const VkRect2D &ra){render_area=ra;} void SetViewport(const VkViewport &vp){viewport=vp;} - void SetClearColor(int index,float r,float g,float b,float a=1.0f) + void SetClearColor(uint32_t index,float r,float g,float b,float a=1.0f) { - if(index<0||index>cv_count)return; + if(index>=cv_count)return; VkClearValue *cv=clear_values+index; @@ -43,9 +43,9 @@ public: cv->color.float32[3]=a; } - void SetClearDepthStencil(int index,float d=1.0f,float s=0) + void SetClearDepthStencil(uint32_t index,float d=1.0f,float s=0) { - if(index<0||index>cv_count)return; + if(index>=cv_count)return; VkClearValue *cv=clear_values+index; diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index d08b8af4..63e42dc2 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -7,9 +7,7 @@ #include #include #include -#include -#include -#include +#include #include VK_NAMESPACE_BEGIN @@ -17,32 +15,12 @@ class Device { DeviceAttribute *attr; - uint swap_chain_count; - - Semaphore *present_complete_semaphore=nullptr, - *render_complete_semaphore=nullptr; - - VkPipelineStageFlags pipe_stage_flags; - VkSubmitInfo submit_info; - Fence *texture_fence; VkSubmitInfo texture_submit_info; CommandBuffer *texture_cmd_buf; - RenderPass *main_rp; - - uint32_t current_frame; - ObjectList render_frame; - - uint32_t current_fence; - ObjectList fence_list; - - VkPresentInfoKHR present_info; - -private: - - void RecreateDevice(); + Swapchain *swapchain; private: @@ -54,26 +32,23 @@ public: virtual ~Device(); - operator VkDevice () {return attr->device;} + operator VkDevice () {return attr->device;} - VkSurfaceKHR GetSurface () {return attr->surface;} - VkDevice GetDevice () {return attr->device;} - const PhysicalDevice *GetPhysicalDevice ()const {return attr->physical_device;} - const VkExtent2D & GetExtent ()const {return attr->swapchain_extent;} + VkSurfaceKHR GetSurface () {return attr->surface;} + VkDevice GetDevice () {return attr->device;} + const PhysicalDevice * GetPhysicalDevice ()const {return attr->physical_device;} - VkDescriptorPool GetDescriptorPool () {return attr->desc_pool;} - VkPipelineCache GetPipelineCache () {return attr->pipeline_cache;} + VkDescriptorPool GetDescriptorPool () {return attr->desc_pool;} + VkPipelineCache GetPipelineCache () {return attr->pipeline_cache;} + + const VkFormat GetSurfaceFormat ()const {return attr->format;} + VkQueue GetGraphicsQueue () {return attr->graphics_queue;} + + Swapchain * GetSwapchain () {return swapchain;} public: - const uint32_t GetSwapChainImageCount ()const {return attr->sc_texture.GetCount();} - - RenderPass * GetMainRenderPass () {return main_rp;} - Framebuffer * GetFramebuffer (int index) {return render_frame[index];} - const uint32_t GetCurrentFrameIndices () {return current_frame;} - Framebuffer * GetCurrentFramebuffer () {return render_frame[current_frame];} - - bool Resize (uint,uint); + bool Resize (const VkExtent2D &); public: //内存相关 @@ -164,7 +139,7 @@ public: //material相关 public: //Command Buffer 相关 - CommandBuffer * CreateCommandBuffer(const VkExtent2D *extent,const uint32_t atta_count); + CommandBuffer * CreateCommandBuffer(const VkExtent2D &extent,const uint32_t atta_count); bool CreateAttachment( List &ref_list, List &desc_list, @@ -194,27 +169,6 @@ public: //Command Buffer 相关 Fence * CreateFence(bool); Semaphore * CreateSem(); - -public: //提交相关 - - bool Wait (bool wait_all=VK_TRUE,uint64_t time_out=HGL_NANO_SEC_PER_SEC*0.1); ///<等待队列完成 - - /** - * 请求获得下一帧的索引,并将确认信息发送到指定信号 - */ - bool AcquireNextImage (VkSemaphore); ///<请求获得下一帧的索引 - - /** - * 提交一批绘制指令 - * @param cmd_list 绘制指令 - * @param wait_sems 指令开始前要等待的确认的信号 - * @param complete_semaphores 绘制完成后发送的信号 - */ - bool SubmitDraw (List &cmd_list,List &wait_sems,List &complete_semaphores); ///<提交绘制指令 - - bool SubmitTexture (const VkCommandBuffer *cmd_bufs,const uint32_t count=1); ///<提交纹理处理到队列 - - bool PresentBackbuffer (); ///<等待绘制队列完成,并将后台缓冲区呈现到前台 };//class Device VK_NAMESPACE_END #endif//HGL_GRAPH_RENDER_SURFACE_INCLUDE diff --git a/inc/hgl/graph/vulkan/VKDeviceAttribute.h b/inc/hgl/graph/vulkan/VKDeviceAttribute.h index 9d882151..e323c113 100644 --- a/inc/hgl/graph/vulkan/VKDeviceAttribute.h +++ b/inc/hgl/graph/vulkan/VKDeviceAttribute.h @@ -13,7 +13,6 @@ struct DeviceAttribute VkSurfaceKHR surface =VK_NULL_HANDLE; VkSurfaceCapabilitiesKHR surface_caps; - VkExtent2D swapchain_extent; uint32_t graphics_family =ERROR_FAMILY_INDEX; uint32_t present_family =ERROR_FAMILY_INDEX; @@ -33,10 +32,6 @@ struct DeviceAttribute VkDevice device =VK_NULL_HANDLE; VkCommandPool cmd_pool =VK_NULL_HANDLE; - VkSwapchainKHR swap_chain =VK_NULL_HANDLE; - - ObjectList sc_texture; - Texture2D * sc_depth =nullptr; VkDescriptorPool desc_pool =VK_NULL_HANDLE; @@ -49,7 +44,6 @@ public: bool CheckMemoryType(uint32_t typeBits,VkMemoryPropertyFlags properties,uint32_t *typeIndex) const; - void ClearSwapchain(); void Refresh(); };//class DeviceAttribute VK_NAMESPACE_END diff --git a/inc/hgl/graph/vulkan/VKSwapchain.h b/inc/hgl/graph/vulkan/VKSwapchain.h new file mode 100644 index 00000000..05053e06 --- /dev/null +++ b/inc/hgl/graph/vulkan/VKSwapchain.h @@ -0,0 +1,82 @@ +#ifndef HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE +#define HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE + +#include +#include +#include +#include +VK_NAMESPACE_BEGIN +class Swapchain +{ +protected: + + Device *device; + + VkExtent2D extent; + + VkQueue graphics_queue =VK_NULL_HANDLE; + VkSwapchainKHR swap_chain =VK_NULL_HANDLE; + + uint32_t swap_chain_count; + + ObjectList sc_texture; + Texture2D * sc_depth =nullptr; + +protected: + + VkPipelineStageFlags pipe_stage_flags; + + uint32_t current_frame; + ObjectList render_frame; + + uint32_t current_fence; + ObjectList fence_list; + + RenderPass *main_rp =nullptr; + + Semaphore *present_complete_semaphore =nullptr, + *render_complete_semaphore =nullptr; + + VkSubmitInfo submit_info; + VkPresentInfoKHR present_info; + +public: + + const VkExtent2D & GetExtent ()const {return extent;} + const uint32_t GetImageCount ()const {return sc_texture.GetCount();} + + RenderPass * GetMainRenderPass () {return main_rp;} + Framebuffer * GetFramebuffer (int index) {return render_frame[index];} + const uint32_t GetCurrentFrameIndices () {return current_frame;} + Framebuffer * GetCurrentFramebuffer () {return render_frame[current_frame];} + +public: + + Swapchain(Device *); + virtual ~Swapchain(); + + void Clear (); + + void Recreate (); + + bool Wait (bool wait_all=VK_TRUE,uint64_t time_out=HGL_NANO_SEC_PER_SEC*0.1); ///<ȴ + + /** + * һ֡ȷϢ͵ָź + */ + bool AcquireNextImage (VkSemaphore); ///<һ֡ + + /** + * ύһָ + * @param cmd_list ָ + * @param wait_sems ָʼǰҪȴȷϵź + * @param complete_semaphores ɺ͵ź + */ + bool SubmitDraw (List &cmd_list,List &wait_sems,List &complete_semaphores); ///<ύָ + + bool SubmitTexture (const VkCommandBuffer *cmd_bufs,const uint32_t count=1); ///<ύ + + bool PresentBackbuffer (); ///<ȴƶɣֵ̨ǰ̨ +};//class Swapchain +VK_NAMESPACE_END +#endif//HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE diff --git a/src/RenderDevice/Vulkan/CMakeLists.txt b/src/RenderDevice/Vulkan/CMakeLists.txt index e4b938c2..fd073932 100644 --- a/src/RenderDevice/Vulkan/CMakeLists.txt +++ b/src/RenderDevice/Vulkan/CMakeLists.txt @@ -22,6 +22,7 @@ ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKShaderModule.h ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKShaderModuleManage.h ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSurfaceExtensionName.h + ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSwapchain.h ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKVertexAttributeBinding.h ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKTexture.h ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSampler.h @@ -56,7 +57,7 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp VKRenderable.cpp VKTexture.cpp VKSampler.cpp - #VKRenderTarget.cpp + VKSwapchain.cpp ) SET(RENDER_DEVICE_VULKAN_POD_SOURCE pod/VKPipelineCreateInfo.POD.cpp) diff --git a/src/RenderDevice/Vulkan/VKDevice.cpp b/src/RenderDevice/Vulkan/VKDevice.cpp index a2b88d8f..e3dbfd6f 100644 --- a/src/RenderDevice/Vulkan/VKDevice.cpp +++ b/src/RenderDevice/Vulkan/VKDevice.cpp @@ -10,97 +10,45 @@ #include VK_NAMESPACE_BEGIN -bool ResizeRenderDevice(DeviceAttribute *attr,uint width,uint height); +Swapchain *CreateSwapchain(DeviceAttribute *attr,const VkExtent2D &); Device::Device(DeviceAttribute *da) { attr=da; - current_frame=0; - texture_fence=this->CreateFence(false); hgl_zero(texture_submit_info); texture_submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - main_rp=nullptr; texture_cmd_buf=nullptr; - - pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - - present_complete_semaphore =this->CreateSem(); - render_complete_semaphore =this->CreateSem(); - - submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - submit_info.pNext = nullptr; - submit_info.waitSemaphoreCount = 1; - submit_info.pWaitSemaphores = *present_complete_semaphore; - submit_info.pWaitDstStageMask = &pipe_stage_flags; - submit_info.signalSemaphoreCount = 1; - submit_info.pSignalSemaphores = *render_complete_semaphore; - - present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; - present_info.pNext = nullptr; - present_info.waitSemaphoreCount = 1; - present_info.pWaitSemaphores = *render_complete_semaphore; - present_info.swapchainCount = 1; - present_info.pResults = nullptr; - RecreateDevice(); + Resize(attr->surface_caps.currentExtent); } Device::~Device() { - fence_list.Clear(); - render_frame.Clear(); - - delete present_complete_semaphore; - delete render_complete_semaphore; - - delete main_rp; - delete texture_cmd_buf; delete texture_fence; delete attr; } -void Device::RecreateDevice() +bool Device::Resize(const VkExtent2D &extent) { - fence_list.Clear(); - render_frame.Clear(); + if(swapchain) + delete swapchain; - if(main_rp)delete main_rp; + swapchain=CreateSwapchain(attr,extent); + if(texture_cmd_buf)delete texture_cmd_buf; - present_info.pSwapchains=&attr->swap_chain; + texture_cmd_buf=CreateCommandBuffer(swapchain->GetExtent(),0); - main_rp=CreateRenderPass(attr->format,attr->sc_depth->GetFormat()); - - swap_chain_count=attr->sc_texture.GetCount(); - - for(uint i=0;isc_texture[i]->GetImageView(),attr->sc_depth->GetImageView())); - fence_list.Add(this->CreateFence(true)); - } - - texture_cmd_buf=CreateCommandBuffer(nullptr,0); - - current_frame=0; - current_fence=0; -} - -bool Device::Resize(uint width,uint height) -{ - if(!ResizeRenderDevice(attr,width,height)) - return(false); - - RecreateDevice(); return(true); } -CommandBuffer *Device::CreateCommandBuffer(const VkExtent2D *extent,const uint32_t atta_count) +CommandBuffer *Device::CreateCommandBuffer(const VkExtent2D &extent,const uint32_t atta_count) { if(!attr->cmd_pool) return(nullptr); @@ -119,7 +67,7 @@ CommandBuffer *Device::CreateCommandBuffer(const VkExtent2D *extent,const uint32 if(res!=VK_SUCCESS) return(nullptr); - return(new CommandBuffer(attr->device,extent?*extent:attr->swapchain_extent,atta_count,attr->cmd_pool,cmd_buf)); + return(new CommandBuffer(attr->device,extent,atta_count,attr->cmd_pool,cmd_buf)); } /** @@ -159,68 +107,4 @@ ShaderModuleManage *Device::CreateShaderModuleManage() { return(new ShaderModuleManage(this)); } - -bool Device::Wait(bool wait_all,uint64_t time_out) -{ - VkFence fence=*fence_list[current_fence]; - - VkResult result; - - result=vkWaitForFences(attr->device,1,&fence,wait_all,time_out); - result=vkResetFences(attr->device,1,&fence); - - return(true); -} - -bool Device::AcquireNextImage(VkSemaphore present_complete_semaphore) -{ - return(vkAcquireNextImageKHR(attr->device,attr->swap_chain,UINT64_MAX,present_complete_semaphore,VK_NULL_HANDLE,¤t_frame)==VK_SUCCESS); -} - -bool Device::SubmitDraw(List &cmd_lists,List &wait_sems,List &complete_sems) -{ - if(cmd_lists.GetCount()<=0) - return(false); - - submit_info.waitSemaphoreCount =wait_sems.GetCount(); - submit_info.pWaitSemaphores =wait_sems.GetData(); - submit_info.commandBufferCount =cmd_lists.GetCount(); - submit_info.pCommandBuffers =cmd_lists.GetData(); - submit_info.signalSemaphoreCount=complete_sems.GetCount(); - submit_info.pSignalSemaphores =complete_sems.GetData(); - - VkFence fence=*fence_list[current_fence]; - - VkResult result=vkQueueSubmit(attr->graphics_queue,1,&submit_info,fence); - - if(++current_fence==swap_chain_count) - current_fence=0; - - //不在这里立即等待fence完成,是因为有可能queue submit需要久一点工作时间,我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence,而且是另一帧的fence。这样有利于异步处理 - - return(result==VK_SUCCESS); -} - -bool Device::PresentBackbuffer() -{ - present_info.pImageIndices=¤t_frame; - - VkResult result=vkQueuePresentKHR(attr->graphics_queue,&present_info); - - if (!((result == VK_SUCCESS) || (result == VK_SUBOPTIMAL_KHR))) - { - if (result == VK_ERROR_OUT_OF_DATE_KHR) { - // Swap chain is no longer compatible with the surface and needs to be recreated - - return false; - } - } - - result=vkQueueWaitIdle(attr->graphics_queue); - - if(result!=VK_SUCCESS) - return(false); - - return(true); -} VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp b/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp index 2c31ebe1..1a69446a 100644 --- a/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp @@ -5,7 +5,6 @@ #include VK_NAMESPACE_BEGIN - DeviceAttribute::DeviceAttribute(VkInstance inst,const PhysicalDevice *pd,VkSurfaceKHR s) { instance=inst; @@ -23,8 +22,6 @@ DeviceAttribute::~DeviceAttribute() if(desc_pool) vkDestroyDescriptorPool(device,desc_pool,nullptr); - ClearSwapchain(); - if(cmd_pool) vkDestroyCommandPool(device,cmd_pool,nullptr); @@ -40,18 +37,6 @@ bool DeviceAttribute::CheckMemoryType(uint32_t typeBits,VkMemoryPropertyFlags pr return physical_device->CheckMemoryType(typeBits,properties,typeIndex); } -void DeviceAttribute::ClearSwapchain() -{ - SAFE_CLEAR(sc_depth); - sc_texture.Clear(); - - if(swap_chain) - { - vkDestroySwapchainKHR(device,swap_chain,nullptr); - swap_chain=VK_NULL_HANDLE; - } -} - void DeviceAttribute::Refresh() { VkPhysicalDevice pdevice = *physical_device; @@ -70,7 +55,7 @@ void DeviceAttribute::Refresh() } { - constexpr VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = { VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, + constexpr VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4]={VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR, VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR, VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR }; diff --git a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp index 87031e50..6bfbefa7 100644 --- a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp @@ -20,14 +20,14 @@ namespace return cur; } - VkExtent2D GetSwapchainExtent(VkSurfaceCapabilitiesKHR &surface_caps,uint32_t width,uint32_t height) + VkExtent2D GetSwapchainExtent(VkSurfaceCapabilitiesKHR &surface_caps,const VkExtent2D &acquire_extent) { if(surface_caps.currentExtent.width==UINT32_MAX) { VkExtent2D swapchain_extent; - swapchain_extent.width=Clamp(width,surface_caps.minImageExtent.width,surface_caps.maxImageExtent.width); - swapchain_extent.height=Clamp(height,surface_caps.minImageExtent.height,surface_caps.maxImageExtent.height); + swapchain_extent.width =Clamp(acquire_extent.width, surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width ); + swapchain_extent.height =Clamp(acquire_extent.height, surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height ); return swapchain_extent; } @@ -549,17 +549,11 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device return(new Device(attr)); } -bool ResizeRenderDevice(DeviceAttribute *attr,uint width,uint height) +Swapchain *CreateSwapchain(DeviceAttribute *attr,const VkExtent2D &acquire_extent) { - if(attr->swapchain_extent.width==width - &&attr->swapchain_extent.height==height) - return(true); - - attr->ClearSwapchain(); - attr->Refresh(); - attr->swapchain_extent=GetSwapchainExtent(attr->surface_caps,width,height); + VkExtent2D extent=GetSwapchainExtent(attr->surface_caps,acquire_extent); return CreateSwapchinAndDepthBuffer(attr); } diff --git a/src/RenderDevice/Vulkan/VKSwapchain.cpp b/src/RenderDevice/Vulkan/VKSwapchain.cpp new file mode 100644 index 00000000..57d4503d --- /dev/null +++ b/src/RenderDevice/Vulkan/VKSwapchain.cpp @@ -0,0 +1,147 @@ +#include +#include +#include + +VK_NAMESPACE_BEGIN +Swapchain::Swapchain(Device *dev) +{ + device=dev; + + pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + + current_frame=0; + main_rp=nullptr; + + present_complete_semaphore =device->CreateSem(); + render_complete_semaphore =device->CreateSem(); + + submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + submit_info.pNext = nullptr; + submit_info.waitSemaphoreCount = 1; + submit_info.pWaitSemaphores = *present_complete_semaphore; + submit_info.pWaitDstStageMask = &pipe_stage_flags; + submit_info.signalSemaphoreCount = 1; + submit_info.pSignalSemaphores = *render_complete_semaphore; + + present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; + present_info.pNext = nullptr; + present_info.waitSemaphoreCount = 1; + present_info.pWaitSemaphores = *render_complete_semaphore; + present_info.swapchainCount = 1; + present_info.pResults = nullptr; + + graphics_queue=device->GetGraphicsQueue(); +} + +Swapchain::~Swapchain() +{ + fence_list.Clear(); + render_frame.Clear(); + + delete present_complete_semaphore; + delete render_complete_semaphore; + + delete main_rp; + + Clear(); +} + +void Swapchain::Clear() +{ + SAFE_CLEAR(sc_depth); + sc_texture.Clear(); + + if(swap_chain) + { + vkDestroySwapchainKHR(device->GetDevice(),swap_chain,nullptr); + swap_chain=VK_NULL_HANDLE; + } +} + +void Swapchain::Recreate() +{ + fence_list.Clear(); + render_frame.Clear(); + + if(main_rp)delete main_rp; + + present_info.pSwapchains=&swap_chain; + + main_rp=device->CreateRenderPass(device->GetSurfaceFormat(),sc_depth->GetFormat()); + + swap_chain_count=sc_texture.GetCount(); + + for(uint i=0;iGetImageView(),sc_depth->GetImageView())); + fence_list.Add(device->CreateFence(true)); + } + + current_frame=0; + current_fence=0; +} + +bool Swapchain::Wait(bool wait_all,uint64_t time_out) +{ + VkFence fence=*fence_list[current_fence]; + + VkResult result; + + result=vkWaitForFences(device->GetDevice(),1,&fence,wait_all,time_out); + result=vkResetFences(device->GetDevice(),1,&fence); + + return(true); +} + +bool Swapchain::AcquireNextImage(VkSemaphore present_complete_semaphore) +{ + return(vkAcquireNextImageKHR(device->GetDevice(),swap_chain,UINT64_MAX,present_complete_semaphore,VK_NULL_HANDLE,¤t_frame)==VK_SUCCESS); +} + +bool Swapchain::SubmitDraw(List &cmd_lists,List &wait_sems,List &complete_sems) +{ + if(cmd_lists.GetCount()<=0) + return(false); + + submit_info.waitSemaphoreCount =wait_sems.GetCount(); + submit_info.pWaitSemaphores =wait_sems.GetData(); + submit_info.commandBufferCount =cmd_lists.GetCount(); + submit_info.pCommandBuffers =cmd_lists.GetData(); + submit_info.signalSemaphoreCount=complete_sems.GetCount(); + submit_info.pSignalSemaphores =complete_sems.GetData(); + + VkFence fence=*fence_list[current_fence]; + + VkResult result=vkQueueSubmit(graphics_queue,1,&submit_info,fence); + + if(++current_fence==swap_chain_count) + current_fence=0; + + //ȴfenceɣΪпqueue submitҪһ㹤ʱ䣬ʱȥɱġAcquireNextImageʱȥȴfenceһ֡fence첽 + + return(result==VK_SUCCESS); +} + +bool Swapchain::PresentBackbuffer() +{ + present_info.pImageIndices=¤t_frame; + + VkResult result=vkQueuePresentKHR(graphics_queue,&present_info); + + if (!((result == VK_SUCCESS) || (result == VK_SUBOPTIMAL_KHR))) + { + if (result == VK_ERROR_OUT_OF_DATE_KHR) { + // Swap chain is no longer compatible with the surface and needs to be recreated + + return false; + } + } + + result=vkQueueWaitIdle(graphics_queue); + + if(result!=VK_SUCCESS) + return(false); + + return(true); +} +VK_NAMESPACE_END diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 6693bb41..a5d48073 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -1,4 +1,5 @@ -SET(SCENE_GRAPH_HEADER ${ROOT_INCLUDE_PATH}/hgl/graph/AABox.h +SET(SCENE_GRAPH_HEADER ${ROOT_INCLUDE_PATH}/hgl/graph/Coordinate.h + ${ROOT_INCLUDE_PATH}/hgl/graph/AABox.h ${ROOT_INCLUDE_PATH}/hgl/graph/Camera.h ${ROOT_INCLUDE_PATH}/hgl/graph/Light.h ${ROOT_INCLUDE_PATH}/hgl/graph/SceneDB.h @@ -15,11 +16,12 @@ #${ROOT_INCLUDE_PATH}/hgl/graph/Spline.h ) -SET(SCENE_GRAPH_SOURCE AABox.cpp +SET(SCENE_GRAPH_SOURCE Coordinate.cpp + AABox.cpp Camera.cpp RenderList.cpp SceneDB.cpp - SceneNode.cpp + SceneNode.cpp SceneOrient.cpp InlineGeometry.cpp #InlinePipeline.cpp diff --git a/src/SceneGraph/Coordinate.cpp b/src/SceneGraph/Coordinate.cpp new file mode 100644 index 00000000..2841bff9 --- /dev/null +++ b/src/SceneGraph/Coordinate.cpp @@ -0,0 +1,11 @@ +#include + +namespace hgl +{ + namespace graph + { + Matrix4f MATRIX_FROM_OPENGL_COORDINATE=scale(1,-1,1)*rotate(hgl_ang2rad(90),Vector3f(1,0,0)); + + + }//namespace graph +}//namespace hgl