diff --git a/example/Basic/first_app.cpp b/example/Basic/first_app.cpp index 4125c83f..6acca385 100644 --- a/example/Basic/first_app.cpp +++ b/example/Basic/first_app.cpp @@ -7,5 +7,10 @@ int os_main(int,os_char **) { RenderFramework rf; - + if(!rf.Init(1280,720,OS_TEXT("FirstApp"))) + return 1; + + rf.Run(); + + return 0; } \ No newline at end of file diff --git a/inc/hgl/graph/RenderFramework.h b/inc/hgl/graph/RenderFramework.h index 6bfaa605..7445ab0e 100644 --- a/inc/hgl/graph/RenderFramework.h +++ b/inc/hgl/graph/RenderFramework.h @@ -101,6 +101,8 @@ public: virtual void MainLoop(); ///<主循环 + virtual void Run(); + public: //TileData TileData *CreateTileData(const VkFormat video_format,const uint width,const uint height,const uint count); ///<创建一个Tile数据集 diff --git a/inc/hgl/graph/VKRenderTarget.h b/inc/hgl/graph/VKRenderTarget.h index ceaea61f..623c7847 100644 --- a/inc/hgl/graph/VKRenderTarget.h +++ b/inc/hgl/graph/VKRenderTarget.h @@ -99,7 +99,7 @@ public: * 请求下一帧画面的索引 * @param present_complete_semaphore 推送完成信号 */ - int AcquireNextImage(); + uint32_t AcquireNextImage(); /** * 推送后台画面到前台 diff --git a/inc/hgl/graph/module/SwapchainModule.h b/inc/hgl/graph/module/SwapchainModule.h index 54264b87..f2f745eb 100644 --- a/inc/hgl/graph/module/SwapchainModule.h +++ b/inc/hgl/graph/module/SwapchainModule.h @@ -27,7 +27,7 @@ public: virtual void OnResize(const VkExtent2D &)override; ///<窗口大小改变 - virtual void OnExecute(const double,RenderCmdBuffer *); + //virtual void OnExecute(const double,RenderCmdBuffer *); public: diff --git a/src/SceneGraph/RenderFramework.cpp b/src/SceneGraph/RenderFramework.cpp index 9b3afd9a..d711da21 100644 --- a/src/SceneGraph/RenderFramework.cpp +++ b/src/SceneGraph/RenderFramework.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -59,8 +60,6 @@ void RenderFramework::EndFrame() { swapchain_module->EndFrame(); - device->WaitIdle(); - last_time=cur_time; ++frame_count; } @@ -83,13 +82,27 @@ void RenderFramework::MainLoop() rm->OnExecute(delta_time,nullptr); } + EndFrame(); + for(auto rm:module_list) { if(rm->IsEnable()) rm->OnPostFrame(); } +} - EndFrame(); +void RenderFramework::Run() +{ + if(!win)return; + if(!swapchain_module)return; + + while(win->Update()) + { + if(win->IsVisible()) + MainLoop(); + + device->WaitIdle(); + } } bool RenderFramework::Init(uint w,uint h,const OSString &app_name) @@ -126,13 +139,14 @@ bool RenderFramework::Init(uint w,uint h,const OSString &app_name) graph_module_manager=InitGraphModuleManager(device); - render_pass_manager=graph_module_manager->GetModule(true); - swapchain_module=graph_module_manager->GetModule(true); + render_pass_manager =graph_module_manager->GetModule(true); + texture_manager =graph_module_manager->GetModule(true); + swapchain_module =graph_module_manager->GetModule(true); } win->Join(this); - + return(true); } diff --git a/src/SceneGraph/Vulkan/VKDeviceCreater.cpp b/src/SceneGraph/Vulkan/VKDeviceCreater.cpp index 9bb5a089..5ece6757 100644 --- a/src/SceneGraph/Vulkan/VKDeviceCreater.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceCreater.cpp @@ -71,23 +71,16 @@ namespace // VK_EXT_SAMPLER_FILTER_MINMAX_EXTENSION_NAME, VK_KHR_SPIRV_1_4_EXTENSION_NAME, + + VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME, + VK_IMG_FORMAT_PVRTC_EXTENSION_NAME, + VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME, + VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME }; for(const char *ext_name:require_ext_list) if(physical_device->CheckExtensionSupport(ext_name)) ext_list->Add(ext_name); - - if(require.lineRasterization>=VulkanHardwareRequirement::SupportLevel::Want) - ext_list->Add(VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME); - - if(require.texture_compression.PVRTC>=VulkanHardwareRequirement::SupportLevel::Want) //前面检测过了,所以这里不用再次检测是否支持 - ext_list->Add(VK_IMG_FORMAT_PVRTC_EXTENSION_NAME); - - if(require.fullDrawIndexUint8>=VulkanHardwareRequirement::SupportLevel::Want) - ext_list->Add(VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME); - - if(require.blendOperationAdvanced>=VulkanHardwareRequirement::SupportLevel::Want) - ext_list->Add(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME); } void SetDeviceFeatures(VkPhysicalDeviceFeatures *features,const VkPhysicalDeviceFeatures &pdf,const VulkanHardwareRequirement &require) @@ -222,7 +215,7 @@ VkDevice VulkanDeviceCreater::CreateDevice(const uint32_t graphics_family) VkDeviceCreateInfo create_info; create_info.sType =VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; - create_info.pNext =nullptr; + //create_info.pNext =nullptr; create_info.flags =0; create_info.queueCreateInfoCount =1; create_info.pQueueCreateInfos =&queue_info; @@ -232,18 +225,38 @@ VkDevice VulkanDeviceCreater::CreateDevice(const uint32_t graphics_family) create_info.ppEnabledLayerNames =nullptr; create_info.pEnabledFeatures =&features; + void **pNext=const_cast(&create_info.pNext); + VkPhysicalDeviceIndexTypeUint8FeaturesEXT index_type_uint8_features; if(physical_device->SupportU8Index() &&require.fullDrawIndexUint8>=VulkanHardwareRequirement::SupportLevel::Want) { - create_info.pNext=&index_type_uint8_features; + *pNext=&index_type_uint8_features; index_type_uint8_features.sType =VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INDEX_TYPE_UINT8_FEATURES_EXT; - index_type_uint8_features.pNext =nullptr; + //index_type_uint8_features.pNext =nullptr; index_type_uint8_features.indexTypeUint8=VK_TRUE; + + pNext=const_cast(&index_type_uint8_features.pNext); } + VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT blend_op_advanced_features; + + if(physical_device->SupportBlendOpAdvanced() + &&require.blendOperationAdvanced>=VulkanHardwareRequirement::SupportLevel::Want) + { + *pNext=&blend_op_advanced_features; + + blend_op_advanced_features.sType=VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT; + //blend_op_advanced_features.pNext=nullptr; + blend_op_advanced_features.advancedBlendCoherentOperations=VK_TRUE; + + pNext=const_cast(&blend_op_advanced_features.pNext); + } + + *pNext=nullptr; + VkDevice device; if(vkCreateDevice(*physical_device,&create_info,nullptr,&device)==VK_SUCCESS) diff --git a/src/SceneGraph/Vulkan/VKPhysicalDevice.cpp b/src/SceneGraph/Vulkan/VKPhysicalDevice.cpp index 94ab27ee..ba5b0ebb 100644 --- a/src/SceneGraph/Vulkan/VKPhysicalDevice.cpp +++ b/src/SceneGraph/Vulkan/VKPhysicalDevice.cpp @@ -72,6 +72,9 @@ GPUPhysicalDevice::GPUPhysicalDevice(VkInstance inst,VkPhysicalDevice pd) VkPhysicalDeviceFeatures2 features2; VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT boaf; + hgl_zero(features2); + hgl_zero(boaf); + features2.sType=VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR; features2.pNext=&features11; @@ -103,6 +106,7 @@ GPUPhysicalDevice::GPUPhysicalDevice(VkInstance inst,VkPhysicalDevice pd) hgl_zero(properties11); hgl_zero(properties12); hgl_zero(properties13); + hgl_zero(blendOpAdvProperties); auto func=(PFN_vkGetPhysicalDeviceProperties2KHR)vkGetInstanceProcAddr(inst,"vkGetPhysicalDeviceProperties2KHR"); @@ -110,6 +114,8 @@ GPUPhysicalDevice::GPUPhysicalDevice(VkInstance inst,VkPhysicalDevice pd) { VkPhysicalDeviceProperties2 properties2; + hgl_zero(properties2); + properties2.sType=VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR; properties2.pNext=&properties11; diff --git a/src/SceneGraph/Vulkan/VKRenderTarget.cpp b/src/SceneGraph/Vulkan/VKRenderTarget.cpp index 931bdb46..35544c29 100644 --- a/src/SceneGraph/Vulkan/VKRenderTarget.cpp +++ b/src/SceneGraph/Vulkan/VKRenderTarget.cpp @@ -12,7 +12,7 @@ RenderTarget::RenderTarget(DeviceQueue *q,Semaphore *s) render_pass=nullptr; fbo=nullptr; - image_count=0; + color_count=0; color_textures=nullptr; depth_texture=nullptr; render_complete_semaphore=s; @@ -26,11 +26,10 @@ RenderTarget::RenderTarget(DeviceQueue *q,Semaphore *s,RenderPass *_rp,Framebuff depth_texture=dt; - image_count=cc; - if(image_count>0) + color_count=cc; + if(color_count>0) { - color_textures=new Texture2D *[image_count]; - hgl_cpy(color_textures,ctl,image_count); + color_textures=hgl_new_copy(ctl,color_count); extent.width=color_textures[0]->GetWidth(); extent.height=color_textures[0]->GetHeight(); @@ -53,7 +52,7 @@ RenderTarget::~RenderTarget() { SAFE_CLEAR(queue); SAFE_CLEAR(depth_texture); - SAFE_CLEAR_OBJECT_ARRAY_OBJECT(color_textures,image_count); + SAFE_CLEAR_OBJECT_ARRAY_OBJECT(color_textures,color_count); SAFE_CLEAR(render_complete_semaphore); SAFE_CLEAR(fbo); diff --git a/src/SceneGraph/Vulkan/VKSwapchainRenderTarget.cpp b/src/SceneGraph/Vulkan/VKSwapchainRenderTarget.cpp index 924326bc..5e533a79 100644 --- a/src/SceneGraph/Vulkan/VKSwapchainRenderTarget.cpp +++ b/src/SceneGraph/Vulkan/VKSwapchainRenderTarget.cpp @@ -30,12 +30,12 @@ RTSwapchain::~RTSwapchain() delete swapchain; } -int RTSwapchain::AcquireNextImage() +uint32_t RTSwapchain::AcquireNextImage() { if(vkAcquireNextImageKHR(device,swapchain->swap_chain,UINT64_MAX,*(this->present_complete_semaphore),VK_NULL_HANDLE,¤t_frame)==VK_SUCCESS) return current_frame; - return -1; + return UINT32_MAX; } bool RTSwapchain::PresentBackbuffer(VkSemaphore *wait_semaphores,const uint32_t count) @@ -48,12 +48,12 @@ bool RTSwapchain::PresentBackbuffer(VkSemaphore *wait_semaphores,const uint32_t 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; - } - } + if (result == VK_ERROR_OUT_OF_DATE_KHR) { + // Swap chain is no longer compatible with the surface and needs to be recreated + + return false; + } + } return(true); } diff --git a/src/SceneGraph/module/GraphModuleManager.cpp b/src/SceneGraph/module/GraphModuleManager.cpp index f9d23525..def50e9f 100644 --- a/src/SceneGraph/module/GraphModuleManager.cpp +++ b/src/SceneGraph/module/GraphModuleManager.cpp @@ -3,6 +3,9 @@ VK_NAMESPACE_BEGIN +void InitGraphModuleFactory(); +void ClearGraphModuleFactory(); + namespace { using GraphModuleManagerMap=Map; @@ -22,6 +25,9 @@ GraphModuleManager *InitGraphModuleManager(GPUDevice *dev) } else { + InitGraphModuleFactory(); + RegistryCommonGraphModule(); + graph_module_manager_map=new GraphModuleManagerMap; } @@ -48,6 +54,13 @@ bool ClearGraphModuleManager(GPUDevice *dev) graph_module_manager_map->DeleteByKey(dev); delete gmm; + + if(graph_module_manager_map->IsEmpty()) + { + ClearGraphModuleFactory(); + SAFE_CLEAR(graph_module_manager_map); + } + return(true); } diff --git a/src/SceneGraph/module/SwapchainModule.cpp b/src/SceneGraph/module/SwapchainModule.cpp index 378bd4c5..5370375d 100644 --- a/src/SceneGraph/module/SwapchainModule.cpp +++ b/src/SceneGraph/module/SwapchainModule.cpp @@ -5,6 +5,7 @@ #include #include #include +#include VK_NAMESPACE_BEGIN namespace @@ -144,9 +145,6 @@ bool SwapchainModule::CreateSwapchainFBO() bool SwapchainModule::CreateSwapchain() { - if(swapchain) - return(false); - auto *dev_attr=GetDeviceAttribute(); if(!dev_attr) @@ -207,7 +205,7 @@ void SwapchainModule::InitRenderCmdBuffer() AnsiString cmd_buf_name; - for(int32_t i=0;iimage_count;i++) + for(uint32_t i=0;iimage_count;i++) { cmd_buf_name=device->GetPhysicalDevice()->GetDeviceName()+AnsiString(":RenderCmdBuffer_")+AnsiString::numberOf(i); @@ -268,9 +266,9 @@ void SwapchainModule::OnResize(const VkExtent2D &extent) bool SwapchainModule::BeginFrame() { - int index=swapchain_rt->AcquireNextImage(); + uint32_t index=swapchain_rt->AcquireNextImage(); - if(index<0||index>=swapchain->image_count) + if(index>=swapchain->image_count) return(false); return(true); @@ -280,9 +278,12 @@ void SwapchainModule::EndFrame() { int index=swapchain_rt->GetCurrentFrameIndices(); + VkCommandBuffer cb=*(cmd_buf[index]); - - swapchain_rt->Submit(); + swapchain_rt->Submit(cb); + swapchain_rt->PresentBackbuffer(); + swapchain_rt->WaitQueue(); + swapchain_rt->WaitFence(); } VK_NAMESPACE_END