2025-01-26 23:22:25 +08:00
|
|
|
|
#pragma once
|
2019-06-19 21:12:39 +08:00
|
|
|
|
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VK.h>
|
|
|
|
|
#include<hgl/graph/VKRenderPass.h>
|
|
|
|
|
#include<hgl/graph/VKFramebuffer.h>
|
|
|
|
|
#include<hgl/graph/VKSwapchain.h>
|
2020-10-21 12:39:22 +08:00
|
|
|
|
#include<hgl/graph/VKQueue.h>
|
2020-10-27 18:11:39 +08:00
|
|
|
|
#include<hgl/graph/VKPipeline.h>
|
2025-01-26 23:22:25 +08:00
|
|
|
|
#include<hgl/graph/VKCommandBuffer.h>
|
2025-01-28 20:03:44 +08:00
|
|
|
|
//#include<iostream>
|
2025-01-25 15:25:29 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
VK_NAMESPACE_BEGIN
|
2025-02-01 16:32:08 +08:00
|
|
|
|
|
|
|
|
|
class RenderFramework;
|
|
|
|
|
|
2025-01-25 15:25:29 +08:00
|
|
|
|
/**
|
|
|
|
|
* RenderTarget 存在几种情况:
|
|
|
|
|
*
|
|
|
|
|
* 1.正常单帧渲染目标,即只有一帧的数据,每次渲染都是当前帧
|
|
|
|
|
*
|
|
|
|
|
* 2.多帧渲染目标,即有多帧数据,每次渲染都是指定帧,典型的是Swapchain
|
|
|
|
|
*
|
|
|
|
|
* 所以RenderTarget的其实是一个多态类,根据不同的情况,有不同的实现
|
|
|
|
|
*/
|
|
|
|
|
|
2025-02-01 16:32:08 +08:00
|
|
|
|
template<typename T> class DeviceBufferObject
|
|
|
|
|
{
|
|
|
|
|
T data;
|
|
|
|
|
DeviceBuffer *dev_buffer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-25 15:25:29 +08:00
|
|
|
|
class IRenderTarget
|
|
|
|
|
{
|
2025-02-01 16:32:08 +08:00
|
|
|
|
RenderFramework *render_framework;
|
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
VkExtent2D extent;
|
2025-02-01 16:32:08 +08:00
|
|
|
|
graph::ViewportInfo vp_info;
|
|
|
|
|
graph::DeviceBuffer *ubo_vp_info;
|
2025-01-26 23:22:25 +08:00
|
|
|
|
|
2025-01-25 15:25:29 +08:00
|
|
|
|
public:
|
|
|
|
|
|
2025-02-01 16:32:08 +08:00
|
|
|
|
RenderFramework * GetRenderFramework ()const{return render_framework;}
|
|
|
|
|
GPUDevice * GetDevice ()const{return render_framework->GetDevice();}
|
|
|
|
|
VkDevice GetVkDevice ()const{return render_framework->GetDevice()->GetDevice();}
|
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
const VkExtent2D &GetExtent ()const{return extent;}
|
|
|
|
|
|
|
|
|
|
virtual uint32_t GetColorCount ()=0;
|
|
|
|
|
virtual bool hasDepth ()=0;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2025-02-01 16:32:08 +08:00
|
|
|
|
void OnResize(const VkExtent2D &ext);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
IRenderTarget(RenderFramework *,const VkExtent2D &);
|
|
|
|
|
virtual ~IRenderTarget();
|
2025-01-26 23:22:25 +08:00
|
|
|
|
|
|
|
|
|
virtual Framebuffer * GetFramebuffer ()=0;
|
|
|
|
|
virtual RenderPass * GetRenderPass ()=0;
|
2025-01-25 15:25:29 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
virtual Texture2D * GetColorTexture (const int index=0)=0;
|
|
|
|
|
virtual Texture2D * GetDepthTexture ()=0;
|
|
|
|
|
|
|
|
|
|
public: // Command Buffer
|
|
|
|
|
|
|
|
|
|
virtual DeviceQueue * GetQueue ()=0;
|
|
|
|
|
virtual Semaphore * GetRenderCompleteSemaphore()=0;
|
|
|
|
|
|
|
|
|
|
virtual RenderCmdBuffer * GetRenderCmdBuffer ()=0;
|
|
|
|
|
|
|
|
|
|
virtual bool Submit (Semaphore *wait_sem)=0;
|
|
|
|
|
|
|
|
|
|
virtual bool Submit (){return Submit(nullptr);}
|
|
|
|
|
|
|
|
|
|
virtual bool WaitQueue ()=0;
|
|
|
|
|
virtual bool WaitFence ()=0;
|
2025-01-28 20:03:44 +08:00
|
|
|
|
|
|
|
|
|
virtual RenderCmdBuffer * BeginRender ()=0;
|
|
|
|
|
virtual void EndRender ()=0;
|
2025-01-25 15:25:29 +08:00
|
|
|
|
};//class IRenderTarget
|
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
struct RenderTargetData
|
|
|
|
|
{
|
|
|
|
|
Framebuffer * fbo;
|
|
|
|
|
DeviceQueue * queue;
|
|
|
|
|
Semaphore * render_complete_semaphore;
|
|
|
|
|
|
|
|
|
|
RenderCmdBuffer * cmd_buf;
|
|
|
|
|
|
|
|
|
|
uint32_t color_count; ///<颜色成分数量
|
|
|
|
|
Texture2D ** color_textures; ///<颜色成分纹理列表
|
|
|
|
|
Texture2D * depth_texture; ///<深度成分纹理
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
Texture2D *GetColorTexture(const uint32_t index)
|
|
|
|
|
{
|
|
|
|
|
if(index>=color_count)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
return color_textures[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void Clear();
|
|
|
|
|
};//struct RenderTargetData
|
|
|
|
|
|
2019-07-16 19:59:53 +08:00
|
|
|
|
/**
|
2025-01-26 23:22:25 +08:00
|
|
|
|
* 单帧渲染目标
|
2019-07-16 19:59:53 +08:00
|
|
|
|
*/
|
2025-01-26 23:22:25 +08:00
|
|
|
|
class RenderTarget:public IRenderTarget
|
2019-07-16 19:59:53 +08:00
|
|
|
|
{
|
2025-01-26 23:22:25 +08:00
|
|
|
|
RenderTargetData *data;
|
|
|
|
|
|
2019-07-16 19:59:53 +08:00
|
|
|
|
protected:
|
2019-06-19 21:12:39 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
friend class SwapchainModule;
|
|
|
|
|
friend class RenderTargetManager;
|
2021-12-15 19:57:35 +08:00
|
|
|
|
|
2025-02-01 16:32:08 +08:00
|
|
|
|
RenderTarget(RenderFramework *rf,RenderTargetData *rtd):IRenderTarget(rf,rtd->fbo->GetExtent())
|
2025-01-26 23:22:25 +08:00
|
|
|
|
{
|
|
|
|
|
data=rtd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2025-02-01 16:32:08 +08:00
|
|
|
|
virtual ~RenderTarget() override
|
2025-01-26 23:22:25 +08:00
|
|
|
|
{
|
|
|
|
|
if(data)
|
|
|
|
|
{
|
|
|
|
|
data->Clear();
|
|
|
|
|
delete data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Framebuffer * GetFramebuffer ()override{return data->fbo;}
|
|
|
|
|
RenderPass * GetRenderPass ()override{return data->fbo->GetRenderPass();}
|
|
|
|
|
|
|
|
|
|
uint32_t GetColorCount ()override{return data->color_count;}
|
2020-10-17 22:12:16 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
bool hasDepth ()override{return data->depth_texture;}
|
2020-10-15 17:30:03 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
Texture2D * GetColorTexture (const int index=0) override{return data->GetColorTexture(index);}
|
|
|
|
|
Texture2D * GetDepthTexture () override{return data->depth_texture;}
|
|
|
|
|
|
|
|
|
|
public: // Command Buffer
|
|
|
|
|
|
|
|
|
|
DeviceQueue * GetQueue ()override{return data->queue;}
|
|
|
|
|
Semaphore * GetRenderCompleteSemaphore()override{return data->render_complete_semaphore;}
|
|
|
|
|
|
|
|
|
|
RenderCmdBuffer * GetRenderCmdBuffer ()override{return data->cmd_buf;}
|
|
|
|
|
|
|
|
|
|
virtual bool Submit (Semaphore *wait_sem)override
|
|
|
|
|
{
|
|
|
|
|
if(!data)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2025-01-26 23:47:04 +08:00
|
|
|
|
return data->queue->Submit(data->cmd_buf,wait_sem,data->render_complete_semaphore);
|
2025-01-26 23:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WaitQueue ()override{return data->queue->WaitQueue();}
|
|
|
|
|
bool WaitFence ()override{return data->queue->WaitFence();}
|
2025-01-28 20:03:44 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
virtual RenderCmdBuffer *BeginRender()override
|
|
|
|
|
{
|
|
|
|
|
if(!data->cmd_buf)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
data->cmd_buf->Begin();
|
|
|
|
|
data->cmd_buf->BindFramebuffer(data->fbo);
|
|
|
|
|
return data->cmd_buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void EndRender() override
|
|
|
|
|
{
|
|
|
|
|
if(!data->cmd_buf)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
data->cmd_buf->End();
|
|
|
|
|
}
|
2025-01-26 23:22:25 +08:00
|
|
|
|
};//class RenderTarget
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 多帧渲染目标
|
|
|
|
|
*/
|
|
|
|
|
class MFRenderTarget:public IRenderTarget
|
|
|
|
|
{
|
2020-10-17 14:20:49 +08:00
|
|
|
|
protected:
|
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
uint32_t frame_number;
|
|
|
|
|
uint32_t current_frame;
|
2025-01-25 15:25:29 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
RenderTarget **rt_list;
|
2020-10-17 14:20:49 +08:00
|
|
|
|
|
2019-07-16 19:59:53 +08:00
|
|
|
|
protected:
|
2019-06-19 21:12:39 +08:00
|
|
|
|
|
2025-01-19 18:13:06 +08:00
|
|
|
|
friend class RenderTargetManager;
|
2019-06-19 21:12:39 +08:00
|
|
|
|
|
2025-02-01 16:32:08 +08:00
|
|
|
|
MFRenderTarget(RenderFramework *rf,const uint32_t fn,RenderTarget **rtl):IRenderTarget(rf,rtl[0]->GetFramebuffer()->GetExtent())
|
2025-01-26 23:22:25 +08:00
|
|
|
|
{
|
|
|
|
|
frame_number=fn;
|
|
|
|
|
current_frame=0;
|
|
|
|
|
|
|
|
|
|
rt_list=rtl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2025-02-01 16:32:08 +08:00
|
|
|
|
virtual ~MFRenderTarget() override
|
2025-01-26 23:22:25 +08:00
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR_OBJECT_ARRAY_OBJECT(rt_list,frame_number);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void NextFrame()
|
|
|
|
|
{
|
|
|
|
|
++current_frame;
|
|
|
|
|
|
|
|
|
|
if(current_frame>=frame_number)
|
|
|
|
|
current_frame=0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t GetCurrentFrameIndices ()const{return current_frame;}
|
|
|
|
|
uint32_t GetFrameCount ()const{return frame_number;}
|
|
|
|
|
RenderTarget * GetCurrentFrameRenderTarget (){return rt_list[current_frame];}
|
2019-06-19 21:12:39 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
Framebuffer * GetFramebuffer ()override{return rt_list[current_frame]->GetFramebuffer();}
|
|
|
|
|
RenderPass * GetRenderPass ()override{return rt_list[current_frame]->GetRenderPass();}
|
|
|
|
|
|
|
|
|
|
uint32_t GetColorCount ()override{return rt_list[current_frame]->GetColorCount();}
|
|
|
|
|
|
|
|
|
|
Texture2D * GetColorTexture (const int index=0) override{return rt_list[current_frame]->GetColorTexture(index);}
|
|
|
|
|
Texture2D * GetDepthTexture () override{return rt_list[current_frame]->GetDepthTexture();}
|
|
|
|
|
|
2019-07-16 19:59:53 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
bool hasDepth ()override{return rt_list[current_frame]->hasDepth();}
|
2020-10-17 22:12:16 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
public: // Command Buffer
|
2020-10-17 22:12:16 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
DeviceQueue * GetQueue ()override{return rt_list[current_frame]->GetQueue();}
|
|
|
|
|
Semaphore * GetRenderCompleteSemaphore()override{return rt_list[current_frame]->GetRenderCompleteSemaphore();}
|
|
|
|
|
RenderCmdBuffer * GetRenderCmdBuffer ()override{return rt_list[current_frame]->GetRenderCmdBuffer();}
|
|
|
|
|
|
|
|
|
|
Framebuffer * GetFramebuffer (const uint32_t index){return rt_list[index]->GetFramebuffer();}
|
|
|
|
|
RenderCmdBuffer * GetRenderCmdBuffer (const uint32_t index){return rt_list[index]->GetRenderCmdBuffer();}
|
2020-10-26 21:51:30 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
virtual bool Submit ()override{return rt_list[current_frame]->Submit(nullptr);}
|
|
|
|
|
|
|
|
|
|
virtual bool Submit (Semaphore *wait_sem) override
|
|
|
|
|
{
|
|
|
|
|
return rt_list[current_frame]->Submit(wait_sem);
|
|
|
|
|
}
|
2021-12-15 19:57:35 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
bool WaitQueue ()override{return rt_list[current_frame]->WaitQueue();}
|
|
|
|
|
bool WaitFence ()override{return rt_list[current_frame]->WaitFence();}
|
2025-01-28 20:03:44 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
virtual RenderCmdBuffer *BeginRender()override
|
|
|
|
|
{
|
|
|
|
|
//std::cout<<"Begin Render frame="<<current_frame<<std::endl;
|
|
|
|
|
return rt_list[current_frame]->BeginRender();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void EndRender() override
|
|
|
|
|
{
|
|
|
|
|
//std::cout<<"End Render frame="<<current_frame<<std::endl;
|
|
|
|
|
rt_list[current_frame]->EndRender();
|
|
|
|
|
}
|
2025-01-26 23:22:25 +08:00
|
|
|
|
};//class MFRenderTarget
|
2019-07-16 19:59:53 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 交换链专用渲染目标
|
|
|
|
|
*/
|
2025-01-26 23:22:25 +08:00
|
|
|
|
class SwapchainRenderTarget:public MFRenderTarget
|
2019-07-16 19:59:53 +08:00
|
|
|
|
{
|
|
|
|
|
Swapchain *swapchain;
|
2020-09-27 20:58:25 +08:00
|
|
|
|
PresentInfo present_info;
|
2019-06-19 21:12:39 +08:00
|
|
|
|
|
2022-10-14 19:40:16 +08:00
|
|
|
|
Semaphore *present_complete_semaphore=nullptr;
|
2020-10-18 13:55:12 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
private:
|
2019-07-16 19:59:53 +08:00
|
|
|
|
|
2025-02-01 16:32:08 +08:00
|
|
|
|
SwapchainRenderTarget(RenderFramework *rf,Swapchain *sc,Semaphore *pcs,RenderTarget **rtl);
|
2025-01-26 12:12:13 +08:00
|
|
|
|
|
2025-01-26 23:22:25 +08:00
|
|
|
|
friend class SwapchainModule;
|
|
|
|
|
friend class RenderTargetManager;
|
2020-10-26 21:51:30 +08:00
|
|
|
|
|
|
|
|
|
public:
|
2020-10-17 14:20:49 +08:00
|
|
|
|
|
2025-02-01 16:32:08 +08:00
|
|
|
|
~SwapchainRenderTarget() override;
|
2019-07-16 19:59:53 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2025-01-28 20:03:44 +08:00
|
|
|
|
IRenderTarget *AcquireNextImage(); ///<获取下一帧的索引
|
2020-10-17 14:20:49 +08:00
|
|
|
|
|
2025-01-28 20:03:44 +08:00
|
|
|
|
bool Submit()override; ///<提交当前帧的渲染,交推送到前台
|
2025-01-26 23:22:25 +08:00
|
|
|
|
};//class SwapchainRenderTarget:public RenderTarget
|
2019-06-19 21:12:39 +08:00
|
|
|
|
VK_NAMESPACE_END
|