RenderTarget初步制作并测试成功

This commit is contained in:
hyzboy 2019-07-16 19:59:53 +08:00
parent ec106446b2
commit 32db6678cc
22 changed files with 491 additions and 252 deletions

View File

@ -88,7 +88,7 @@ private:
bool InitPipeline()
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent());
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target->GetRenderPass(),sc_render_target->GetExtent());
pipeline_creater->SetDepthTest(true);
pipeline_creater->SetDepthWrite(true);
pipeline_creater->SetCullMode(VK_CULL_MODE_NONE);

View File

@ -251,7 +251,7 @@ private:
bool InitCompositionPipeline(SubpassParam *sp)
{
AutoDelete<vulkan::PipelineCreater> pipeline_creater=new vulkan::PipelineCreater(device,sp->material,swapchain->GetMainRenderPass(),swapchain->GetExtent());
AutoDelete<vulkan::PipelineCreater> pipeline_creater=new vulkan::PipelineCreater(device,sp->material,sc_render_target->GetRenderPass(),sc_render_target->GetExtent());
pipeline_creater->SetDepthTest(false);
pipeline_creater->SetDepthWrite(false);
pipeline_creater->SetCullMode(VK_CULL_MODE_NONE);
@ -408,7 +408,7 @@ private:
gbuffer_cmd->EndRenderPass();
gbuffer_cmd->End();
device->SubmitDraw(*gbuffer_cmd);
device->Submit(*gbuffer_cmd);
return(true);
}

View File

@ -96,7 +96,7 @@ private:
bool InitUBO()
{
const VkExtent2D extent=swapchain->GetExtent();
const VkExtent2D extent=sc_render_target->GetExtent();
world.mvp=ortho(extent.width,extent.height);
@ -115,7 +115,7 @@ private:
bool InitPipeline()
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent());
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target->GetRenderPass(),sc_render_target->GetExtent());
pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_TRIANGLE_FAN);

View File

@ -58,7 +58,7 @@ private:
bool InitPipeline(MDP *mdp,const VkPrimitiveTopology primitive)
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,mdp->material,swapchain->GetMainRenderPass(),swapchain->GetExtent());
pipeline_creater=new vulkan::PipelineCreater(device,mdp->material,sc_render_target->GetRenderPass(),sc_render_target->GetExtent());
pipeline_creater->CloseCullFace();
pipeline_creater->Set(primitive);

View File

@ -11,8 +11,8 @@
using namespace hgl;
using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=1280;
constexpr uint32_t SCREEN_HEIGHT=720;
constexpr uint32_t SCREEN_WIDTH=128;
constexpr uint32_t SCREEN_HEIGHT=128;
class TestApp:public CameraAppFramework
{
@ -137,7 +137,7 @@ private:
bool InitPipeline()
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent());
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target->GetRenderPass(),sc_render_target->GetExtent());
pipeline_creater->Set(PRIM_LINES);
pipeline_line=pipeline_creater->Create();

View File

@ -163,7 +163,7 @@ private:
bool InitPipeline()
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent());
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target->GetRenderPass(),sc_render_target->GetExtent());
pipeline_creater->SetDepthTest(false);
pipeline_creater->SetDepthWrite(false);
pipeline_creater->SetPolygonMode(VK_POLYGON_MODE_LINE);

View File

@ -75,7 +75,7 @@ private:
bool InitPipeline()
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent());
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target->GetRenderPass(),sc_render_target->GetExtent());
pipeline_creater->SetDepthTest(true);
pipeline_creater->SetDepthWrite(true);
pipeline_creater->CloseCullFace();

View File

@ -15,6 +15,7 @@
#include<hgl/graph/vulkan/VKFormat.h>
#include<hgl/graph/vulkan/VKFramebuffer.h>
#include<hgl/graph/vulkan/VKMaterial.h>
#include<hgl/graph/vulkan/VKRenderTarget.h>
#include<hgl/graph/SceneDB.h>
#include<hgl/graph/RenderList.h>
@ -50,19 +51,19 @@ protected:
protected:
vulkan::Device * device =nullptr;
vulkan::Swapchain * swapchain =nullptr;
vulkan::Device * device =nullptr;
vulkan::SwapchainRenderTarget * sc_render_target =nullptr;
vulkan::Semaphore * present_complete_semaphore =nullptr,
* render_complete_semaphore =nullptr;
vulkan::Semaphore * present_complete_semaphore =nullptr,
* render_complete_semaphore =nullptr;
vulkan::ShaderModuleManage *shader_manage =nullptr;
vulkan::ShaderModuleManage * shader_manage =nullptr;
private:
uint32_t swap_chain_count=0;
uint32_t swap_chain_count=0;
vulkan::CommandBuffer ** cmd_buf =nullptr;
vulkan::CommandBuffer ** cmd_buf =nullptr;
protected:
@ -113,7 +114,7 @@ public:
if(!device)
return(false);
swapchain=device->GetSwapchain();
sc_render_target=device->GetSwapchainRT();
present_complete_semaphore =device->CreateSem();
render_complete_semaphore =device->CreateSem();
@ -147,10 +148,9 @@ public:
if(cmd_buf)
SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count);
swap_chain_count=swapchain->GetImageCount();
swap_chain_count=sc_render_target->GetImageCount();
{
const VkExtent2D extent=swapchain->GetExtent();
const VkExtent2D extent=sc_render_target->GetExtent();
cmd_buf=hgl_zero_new<vulkan::CommandBuffer *>(swap_chain_count);
@ -169,7 +169,7 @@ public:
vulkan::CommandBuffer *cb=cmd_buf[index];
cb->Begin();
cb->BeginRenderPass(swapchain->GetMainRenderPass(),swapchain->GetFramebuffer(index));
cb->BeginRenderPass(sc_render_target->GetRenderPass(),sc_render_target->GetFramebuffer(index));
cb->Bind(p);
cb->Bind(ds);
cb->Bind(r);
@ -191,7 +191,7 @@ public:
void BuildCurrentCommandBuffer(vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r)
{
BuildCommandBuffer(swapchain->GetCurrentFrameIndices(),p,ds,r);
BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),p,ds,r);
}
void BuildCommandBuffer(uint32_t index,RenderList *rl)
@ -201,7 +201,7 @@ public:
vulkan::CommandBuffer *cb=cmd_buf[index];
cb->Begin();
cb->BeginRenderPass(swapchain->GetMainRenderPass(),swapchain->GetFramebuffer(index));
cb->BeginRenderPass(sc_render_target->GetRenderPass(),sc_render_target->GetFramebuffer(index));
rl->Render(cb);
cb->EndRenderPass();
cb->End();
@ -215,16 +215,16 @@ public:
void BuildCurrentCommandBuffer(RenderList *rl)
{
BuildCommandBuffer(swapchain->GetCurrentFrameIndices(),rl);
BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),rl);
}
public:
int AcquireNextImage()
{
if(swapchain->Wait())
if(sc_render_target->Wait())
{
int cur=swapchain->AcquireNextImage(present_complete_semaphore);
int cur=sc_render_target->AcquireNextImage(present_complete_semaphore);
return cur;
}
@ -236,8 +236,8 @@ public:
{
VkCommandBuffer cb=*cmd_buf[index];
swapchain->SubmitDraw(cb,present_complete_semaphore,render_complete_semaphore);
swapchain->PresentBackbuffer(render_complete_semaphore);
sc_render_target->Submit(cb,present_complete_semaphore,render_complete_semaphore);
sc_render_target->PresentBackbuffer(render_complete_semaphore);
}
virtual void Draw()

View File

@ -76,7 +76,7 @@ private:
bool InitUBO()
{
const VkExtent2D extent=swapchain->GetExtent();
const VkExtent2D extent=sc_render_target->GetExtent();
world.mvp=ortho(extent.width,extent.height);
@ -104,7 +104,7 @@ private:
bool InitPipeline()
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent());
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target->GetRenderPass(),sc_render_target->GetExtent());
pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_TRIANGLES);

View File

@ -77,7 +77,7 @@ private:
bool InitUBO()
{
const VkExtent2D extent=swapchain->GetExtent();
const VkExtent2D extent=sc_render_target->GetExtent();
world.mvp=ortho(extent.width,extent.height);
@ -108,7 +108,7 @@ private:
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent());
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target->GetRenderPass(),sc_render_target->GetExtent());
pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_TRIANGLES);
@ -119,7 +119,7 @@ private:
void *data;
uint size=filesystem::LoadFileToMemory(PIPELINE_FILENAME,(void **)&data);
AutoDelete<vulkan::PipelineCreater> pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent(),(uchar *)data,size);
AutoDelete<vulkan::PipelineCreater> pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target->GetRenderPass(),sc_render_target->GetExtent(),(uchar *)data,size);
pipeline=pipeline_creater->Create();
}

View File

@ -126,7 +126,7 @@ private:
bool InitUBO()
{
const VkExtent2D extent=swapchain->GetExtent();
const VkExtent2D extent=sc_render_target->GetExtent();
world.mvp=ortho(extent.width,extent.height);
@ -149,7 +149,7 @@ private:
bool InitPipeline()
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent());
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target->GetRenderPass(),sc_render_target->GetExtent());
pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_TRIANGLES);

View File

@ -20,6 +20,9 @@ class Device;
struct DeviceAttribute;
class ImageView;
class Framebuffer;
class Swapchain;
class RenderTarget;
class SwapchainRenderTarget;
class Texture;
class Texture1D;

View File

@ -8,6 +8,7 @@
#include<hgl/graph/vulkan/VK.h>
#include<hgl/graph/vulkan/VKDeviceAttribute.h>
#include<hgl/graph/vulkan/VKSwapchain.h>
#include<hgl/graph/vulkan/VKRenderTarget.h>
#include<hgl/graph/VertexBufferCreater.h>
VK_NAMESPACE_BEGIN
@ -21,6 +22,7 @@ class Device
CommandBuffer *texture_cmd_buf;
Swapchain *swapchain;
SwapchainRenderTarget *swapchainRT;
private:
@ -47,6 +49,8 @@ public:
Swapchain * GetSwapchain () {return swapchain;}
SwapchainRenderTarget * GetSwapchainRT () {return swapchainRT;}
public:
bool Resize (const VkExtent2D &);
@ -180,6 +184,8 @@ public: //Command Buffer 相关
public:
bool SubmitTexture (const VkCommandBuffer *cmd_bufs,const uint32_t count=1); ///<提交纹理处理到队列
RenderTarget *CreateRenderTarget(Framebuffer *);
};//class Device
VK_NAMESPACE_END
#endif//HGL_GRAPH_RENDER_SURFACE_INCLUDE

View File

@ -3,27 +3,48 @@
#include<hgl/graph/vulkan/VK.h>
VK_NAMESPACE_BEGIN
class Framebuffer
{
VkDevice device;
VkFramebuffer frame_buffer;
VkFramebufferCreateInfo *fb_info;
VkExtent2D extent;
uint32_t color_count;
bool has_depth;
private:
friend Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth);
Framebuffer(VkDevice dev,VkFramebuffer fb)
Framebuffer(VkDevice dev,VkFramebuffer fb,VkFramebufferCreateInfo *fb_create_info,bool depth)
{
device=dev;
frame_buffer=fb;
fb_info=fb_create_info;
extent.width=fb_info->width;
extent.height=fb_info->height;
has_depth=depth;
if(has_depth)
color_count=fb_info->attachmentCount-1;
else
color_count=fb_info->attachmentCount;
}
public:
~Framebuffer();
operator VkFramebuffer(){return frame_buffer;}
VkFramebuffer GetFramebuffer (){return frame_buffer;}
VkRenderPass GetRenderPass (){return fb_info->renderPass;}
const VkExtent2D & GetExtent ()const{return extent;}
const uint32_t GetAttachmentCount ()const{return fb_info->attachmentCount;} ///<获取渲染目标成分数量
const uint32_t GetColorCount ()const{return color_count;} ///<取得颜色成分数量
const bool HasDepth ()const{return has_depth;} ///<是否包含深度成分
};//class Framebuffer
Framebuffer *CreateFramebuffer(Device *,RenderPass *,List<ImageView *> &color,ImageView *depth);

View File

@ -4,42 +4,93 @@
#include<hgl/graph/vulkan/VK.h>
#include<hgl/graph/vulkan/VKRenderPass.h>
#include<hgl/graph/vulkan/VKFramebuffer.h>
#include<hgl/graph/vulkan/VKSwapchain.h>
VK_NAMESPACE_BEGIN
class RenderTarget
class SubmitQueue
{
protected:
Device *device;
VkQueue queue;
RenderPass *rp;
Framebuffer *fb;
uint32_t current_fence;
ObjectList<Fence> fence_list;
VkExtent2D extent;
List<ImageView *> colors;
ImageView *depth;
private:
friend class Device;
RenderTarget(Device *dev,RenderPass *_rp,Framebuffer *_fb)
{
device=dev;
rp=_rp;
fb=_fb;
}
VkSubmitInfo submit_info;
public:
virtual ~RenderTarget()
{
if(fb)
delete fb;
}
SubmitQueue(Device *dev,VkQueue q,const uint32_t fence_count=1);
virtual ~SubmitQueue();
const VkExtent2D & GetExtent ()const{return extent;} ///<取得画面尺寸
bool Wait(const bool wait_wall=true,const uint64 time_out=HGL_NANO_SEC_PER_SEC);
bool Submit(VkCommandBuffer &cmd_buf,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem);
};//class SumbitQueue
/**
*
*/
class RenderTarget:public SubmitQueue
{
protected:
Framebuffer *fb;
protected:
friend class Device;
RenderTarget(Device *dev,Framebuffer *_fb,const uint32_t fence_count=1);
public:
virtual ~RenderTarget()=default;
const uint GetColorCount ()const{colors.GetCount();} ///<取得颜色成份数量
const bool IsExistDepth ()const{return depth;} ///<是否存在深度成份
};//class RenderTarget
/**
*
*/
class SwapchainRenderTarget:public RenderTarget
{
Swapchain *swapchain;
VkSwapchainKHR vk_swapchain;
VkPresentInfoKHR present_info;
RenderPass *main_rp=nullptr;
uint32_t swap_chain_count;
VkExtent2D extent;
uint32_t current_frame;
ObjectList<Framebuffer> render_frame;
public:
SwapchainRenderTarget(Device *dev,Swapchain *sc);
~SwapchainRenderTarget();
const uint32_t GetImageCount()const{return swap_chain_count;}
const VkExtent2D & GetExtent()const{return extent;}
RenderPass * GetRenderPass(){return main_rp;}
Framebuffer * GetFramebuffer(const uint32_t index){return render_frame[index];}
const uint32_t GetCurrentFrameIndices()const{return current_frame;}
public:
/**
*
* @param present_complete_semaphore
*/
int AcquireNextImage(vulkan::Semaphore *present_complete_semaphore);
/**
*
* @param render_complete_semaphore
*/
bool PresentBackbuffer(vulkan::Semaphore *render_complete_semaphore);
};//class SwapchainRenderTarget:public RenderTarget
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_RENDER_TARGET_INCLUDE

View File

@ -16,28 +16,33 @@ protected:
protected:
VkPipelineStageFlags pipe_stage_flags;
// VkPipelineStageFlags pipe_stage_flags;
uint32_t current_frame;
ObjectList<Framebuffer> render_frame;
//uint32_t current_frame;
//ObjectList<Framebuffer> render_frame;
uint32_t current_fence;
ObjectList<Fence> fence_list;
//uint32_t current_fence;
//ObjectList<Fence> fence_list;
RenderPass *main_rp =nullptr;
//RenderPass *main_rp =nullptr;
VkSubmitInfo submit_info;
VkPresentInfoKHR present_info;
//VkSubmitInfo submit_info;
//VkPresentInfoKHR present_info;
public:
VkSwapchainKHR GetSwapchain () {return sc_attr->swap_chain;}
const VkExtent2D & GetExtent ()const {return sc_attr->extent;}
const uint32_t GetImageCount ()const {return sc_attr->sc_color.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];}
Texture2D ** GetColorTextures () {return sc_attr->sc_color.GetData();}
Texture2D * GetColorTexture (int index) {return sc_attr->sc_color[index];}
Texture2D * GetDepthTexture () {return sc_attr->sc_depth;}
// 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:
@ -45,36 +50,36 @@ public:
virtual ~Swapchain();
bool Wait (bool wait_all=VK_TRUE,uint64_t time_out=HGL_NANO_SEC_PER_SEC*0.1); ///<等待队列完成
//bool Wait (bool wait_all=VK_TRUE,uint64_t time_out=HGL_NANO_SEC_PER_SEC*0.1); ///<等待队列完成
/**
*
* @param complete_semaphore
* @return
* @return <0
*/
int AcquireNextImage (vulkan::Semaphore *complete_semaphore); ///<请求获得下一帧的索引
///**
// * 请求获得下一帧的索引,并将确认信息发送到指定信号
// * @param complete_semaphore 完成后请发送至此信号
// * @return 下一帧的索引
// * @return <0 错误
// */
//int AcquireNextImage (vulkan::Semaphore *complete_semaphore); ///<请求获得下一帧的索引
//
///**
// * 提交一个绘制指令
// * @param cmd_list 绘制指令
// * @param wait_sem 指令开始前要等待的确认的信号
// * @param complete_semaphore 绘制完成后发送的信号
// */
//bool SubmitDraw (VkCommandBuffer &cmd_list,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_semaphore); ///<提交绘制指令
/**
*
* @param cmd_list
* @param wait_sem
* @param complete_semaphore
*/
bool SubmitDraw (VkCommandBuffer &cmd_list,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_semaphore); ///<提交绘制指令
///**
// * 提交一批绘制指令
// * @param cmd_list 绘制指令
// * @param wait_sems 指令开始前要等待的确认的信号
// * @param complete_semaphores 绘制完成后发送的信号
// */
//bool SubmitDraw (List<VkCommandBuffer> &cmd_list,List<VkSemaphore> &wait_sems,List<VkSemaphore> &complete_semaphores); ///<提交绘制指令
/**
*
* @param cmd_list
* @param wait_sems
* @param complete_semaphores
*/
bool SubmitDraw (List<VkCommandBuffer> &cmd_list,List<VkSemaphore> &wait_sems,List<VkSemaphore> &complete_semaphores); ///<提交绘制指令
/**
* @param render_complete_semaphore
*/
bool PresentBackbuffer (vulkan::Semaphore *render_complete_semaphore); ///<等待绘制队列完成,并将后台缓冲区呈现到前台
///**
// * @param render_complete_semaphore 渲染完成信号
// */
//bool PresentBackbuffer (vulkan::Semaphore *render_complete_semaphore); ///<等待绘制队列完成,并将后台缓冲区呈现到前台
};//class Swapchain
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE

View File

@ -17,7 +17,7 @@
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKPrimivate.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKRenderable.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKRenderPass.h
# ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKRenderTarget.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKRenderTarget.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSemaphore.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKShaderResource.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKShaderModule.h
@ -46,7 +46,7 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
VKDescriptorSetLayoutCreater.cpp
VKDescriptorSetLayoutCreater.h
VKRenderPass.cpp
# VKRenderTarget.cpp
VKRenderTarget.cpp
VKShaderParse.h
VKShaderModule.cpp
VKShaderModuleManage.cpp

View File

@ -64,7 +64,7 @@ bool CommandBuffer::BeginRenderPass(RenderPass *rp,Framebuffer *fb)
rp_begin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
rp_begin.pNext = nullptr;
rp_begin.renderPass = *rp;
rp_begin.framebuffer = *fb;
rp_begin.framebuffer = fb->GetFramebuffer();
rp_begin.renderArea = render_area;
rp_begin.clearValueCount = cv_count;
rp_begin.pClearValues = clear_values;

View File

@ -24,11 +24,13 @@ Device::Device(DeviceAttribute *da)
texture_cmd_buf=nullptr;
swapchain=nullptr;
swapchainRT=nullptr;
Resize(attr->surface_caps.currentExtent);
}
Device::~Device()
{
SAFE_CLEAR(swapchainRT);
SAFE_CLEAR(swapchain);
delete texture_cmd_buf;
@ -39,8 +41,8 @@ Device::~Device()
bool Device::Resize(const VkExtent2D &extent)
{
if(swapchain)
delete swapchain;
SAFE_CLEAR(swapchainRT);
SAFE_CLEAR(swapchain);
swapchain=CreateSwapchain(this,extent);
@ -48,6 +50,8 @@ bool Device::Resize(const VkExtent2D &extent)
texture_cmd_buf=CreateCommandBuffer(swapchain->GetExtent(),0);
swapchainRT=new SwapchainRenderTarget(this,swapchain);
return(true);
}
@ -110,4 +114,9 @@ ShaderModuleManage *Device::CreateShaderModuleManage()
{
return(new ShaderModuleManage(this));
}
RenderTarget *Device::CreateRenderTarget(Framebuffer *fb)
{
return(new RenderTarget(this,fb));
}
VK_NAMESPACE_END

View File

@ -16,7 +16,7 @@ Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,ImageView **color_list
if(depth)++att_count;
SharedArray<VkImageView> attachments=new VkImageView[att_count];
VkImageView *attachments=new VkImageView[att_count];
if(color_count)
{
@ -39,30 +39,33 @@ Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,ImageView **color_list
if(depth)
{
if(rp->GetDepthFormat()!=depth->GetFormat())
{
delete[] attachments;
return(nullptr);
}
attachments[color_count]=*depth;
}
const VkExtent3D extent=depth->GetExtent();
VkFramebufferCreateInfo fb_info;
fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
fb_info.pNext = nullptr;
fb_info.flags = 0;
fb_info.renderPass = *rp;
fb_info.attachmentCount = att_count;
fb_info.pAttachments = attachments;
fb_info.width = extent.width;
fb_info.height = extent.height;
fb_info.layers = 1;
VkFramebufferCreateInfo *fb_info=new VkFramebufferCreateInfo;
fb_info->sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
fb_info->pNext = nullptr;
fb_info->flags = 0;
fb_info->renderPass = *rp;
fb_info->attachmentCount = att_count;
fb_info->pAttachments = attachments;
fb_info->width = extent.width;
fb_info->height = extent.height;
fb_info->layers = 1;
VkFramebuffer fb;
if(vkCreateFramebuffer(dev->GetDevice(), &fb_info, nullptr, &fb)!=VK_SUCCESS)
if(vkCreateFramebuffer(dev->GetDevice(),fb_info,nullptr,&fb)!=VK_SUCCESS)
return(nullptr);
return(new Framebuffer(dev->GetDevice(),fb));
return(new Framebuffer(dev->GetDevice(),fb,fb_info,depth));
}
Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,List<ImageView *> &color,ImageView *depth)

View File

@ -1,9 +1,150 @@
#include<hgl/graph/vulkan/VKRenderTarget.h>
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/graph/vulkan/VKSwapchain.h>
VK_NAMESPACE_BEGIN
RenderTarget::~RenderTarget()
namespace
{
if(fb)
delete fb;
const VkPipelineStageFlags pipe_stage_flags=VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
}//namespace
SubmitQueue::SubmitQueue(Device *dev,VkQueue q,const uint32_t fence_count)
{
device=dev;
queue=q;
for(uint32_t i=0;i<fence_count;i++)
fence_list.Add(device->CreateFence(true));
current_fence=0;
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;
}
SubmitQueue::~SubmitQueue()
{
fence_list.Clear();
}
bool SubmitQueue::Wait(const 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 SubmitQueue::Submit(VkCommandBuffer &cmd_buf,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem)
{
VkSemaphore ws=*wait_sem;
VkSemaphore cs=*complete_sem;
submit_info.waitSemaphoreCount =1;
submit_info.pWaitSemaphores =&ws;
submit_info.commandBufferCount =1;
submit_info.pCommandBuffers =&cmd_buf;
submit_info.signalSemaphoreCount=1;
submit_info.pSignalSemaphores =&cs;
VkFence fence=*fence_list[current_fence];
VkResult result=vkQueueSubmit(queue,1,&submit_info,fence);
if(++current_fence==fence_list.GetCount())
current_fence=0;
//不在这里立即等待fence完成是因为有可能queue submit需要久一点工作时间我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence而且是另一帧的fence。这样有利于异步处理
return(result==VK_SUCCESS);
}
RenderTarget::RenderTarget(Device *dev,Framebuffer *_fb,const uint32_t fence_count):SubmitQueue(dev,dev->GetGraphicsQueue(),fence_count)
{
fb=_fb;
}
SwapchainRenderTarget::SwapchainRenderTarget(Device *dev,Swapchain *sc):RenderTarget(dev,nullptr,sc->GetImageCount())
{
swapchain=sc;
vk_swapchain=swapchain->GetSwapchain();
present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
present_info.pNext = nullptr;
present_info.waitSemaphoreCount = 0;
present_info.pWaitSemaphores = nullptr;
present_info.swapchainCount = 1;
present_info.pResults = nullptr;
present_info.pSwapchains = &vk_swapchain;
Texture2D **sc_color=swapchain->GetColorTextures();
Texture2D *sc_depth=swapchain->GetDepthTexture();
main_rp=device->CreateRenderPass((*sc_color)->GetFormat(),sc_depth->GetFormat());
swap_chain_count=swapchain->GetImageCount();
extent=swapchain->GetExtent();
for(uint i=0;i<swap_chain_count;i++)
{
render_frame.Add(vulkan::CreateFramebuffer(device,main_rp,(*sc_color)->GetImageView(),sc_depth->GetImageView()));
++sc_color;
}
current_frame=0;
}
SwapchainRenderTarget::~SwapchainRenderTarget()
{
render_frame.Clear();
delete main_rp;
}
int SwapchainRenderTarget::AcquireNextImage(vulkan::Semaphore *present_complete_semaphore)
{
VkSemaphore sem=*present_complete_semaphore;
if(vkAcquireNextImageKHR(device->GetDevice(),vk_swapchain,UINT64_MAX,sem,VK_NULL_HANDLE,&current_frame)==VK_SUCCESS)
return current_frame;
return -1;
}
bool SwapchainRenderTarget::PresentBackbuffer(vulkan::Semaphore *render_complete_semaphore)
{
VkSemaphore sem=*render_complete_semaphore;
present_info.waitSemaphoreCount=1;
present_info.pWaitSemaphores=&sem;
present_info.pImageIndices=&current_frame;
VkResult result=vkQueuePresentKHR(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(queue);
if(result!=VK_SUCCESS)
return(false);
return(true);
}
VK_NAMESPACE_END

View File

@ -8,140 +8,140 @@ Swapchain::Swapchain(Device *dev,SwapchainAttribute *sa)
device=dev;
sc_attr=sa;
pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
//pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
//
//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;
//
//present_info.pSwapchains=&(sc_attr->swap_chain);
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;
//main_rp=device->CreateRenderPass(sc_attr->sc_color[0]->GetFormat(),sc_attr->sc_depth->GetFormat());
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;
present_info.pSwapchains=&(sc_attr->swap_chain);
main_rp=device->CreateRenderPass(sc_attr->sc_color[0]->GetFormat(),sc_attr->sc_depth->GetFormat());
for(uint i=0;i<sc_attr->swap_chain_count;i++)
{
render_frame.Add(vulkan::CreateFramebuffer(device,main_rp,sc_attr->sc_color[i]->GetImageView(),sc_attr->sc_depth->GetImageView()));
fence_list.Add(device->CreateFence(true));
}
current_frame=0;
current_fence=0;
//for(uint i=0;i<sc_attr->swap_chain_count;i++)
//{
// render_frame.Add(vulkan::CreateFramebuffer(device,main_rp,sc_attr->sc_color[i]->GetImageView(),sc_attr->sc_depth->GetImageView()));
// fence_list.Add(device->CreateFence(true));
//}
//
//current_frame=0;
//current_fence=0;
}
Swapchain::~Swapchain()
{
fence_list.Clear();
render_frame.Clear();
//fence_list.Clear();
//render_frame.Clear();
delete main_rp;
//delete main_rp;
SAFE_CLEAR(sc_attr);
}
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);
}
int Swapchain::AcquireNextImage(vulkan::Semaphore *present_complete_semaphore)
{
VkSemaphore sem=*present_complete_semaphore;
if(vkAcquireNextImageKHR(device->GetDevice(),sc_attr->swap_chain,UINT64_MAX,sem,VK_NULL_HANDLE,&current_frame)==VK_SUCCESS)
return current_frame;
return -1;
}
bool Swapchain::SubmitDraw(VkCommandBuffer &cmd_list,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem)
{
VkSemaphore ws=*wait_sem;
VkSemaphore cs=*complete_sem;
submit_info.waitSemaphoreCount =1;
submit_info.pWaitSemaphores =&ws;
submit_info.commandBufferCount =1;
submit_info.pCommandBuffers =&cmd_list;
submit_info.signalSemaphoreCount=1;
submit_info.pSignalSemaphores =&cs;
VkFence fence=*fence_list[current_fence];
VkResult result=vkQueueSubmit(sc_attr->graphics_queue,1,&submit_info,fence);
if(++current_fence==sc_attr->swap_chain_count)
current_fence=0;
//不在这里立即等待fence完成是因为有可能queue submit需要久一点工作时间我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence而且是另一帧的fence。这样有利于异步处理
return(result==VK_SUCCESS);
}
bool Swapchain::SubmitDraw(List<VkCommandBuffer> &cmd_lists,List<VkSemaphore> &wait_sems,List<VkSemaphore> &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(sc_attr->graphics_queue,1,&submit_info,fence);
if(++current_fence==sc_attr->swap_chain_count)
current_fence=0;
//不在这里立即等待fence完成是因为有可能queue submit需要久一点工作时间我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence而且是另一帧的fence。这样有利于异步处理
return(result==VK_SUCCESS);
}
bool Swapchain::PresentBackbuffer(vulkan::Semaphore *render_complete_semaphore)
{
VkSemaphore sem=*render_complete_semaphore;
present_info.pWaitSemaphores=&sem;
present_info.pImageIndices=&current_frame;
VkResult result=vkQueuePresentKHR(sc_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(sc_attr->graphics_queue);
if(result!=VK_SUCCESS)
return(false);
return(true);
}
//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);
//}
//
//int Swapchain::AcquireNextImage(vulkan::Semaphore *present_complete_semaphore)
//{
// VkSemaphore sem=*present_complete_semaphore;
//
// if(vkAcquireNextImageKHR(device->GetDevice(),sc_attr->swap_chain,UINT64_MAX,sem,VK_NULL_HANDLE,&current_frame)==VK_SUCCESS)
// return current_frame;
//
// return -1;
//}
//
//bool Swapchain::SubmitDraw(VkCommandBuffer &cmd_list,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem)
//{
// VkSemaphore ws=*wait_sem;
// VkSemaphore cs=*complete_sem;
//
// submit_info.waitSemaphoreCount =1;
// submit_info.pWaitSemaphores =&ws;
// submit_info.commandBufferCount =1;
// submit_info.pCommandBuffers =&cmd_list;
// submit_info.signalSemaphoreCount=1;
// submit_info.pSignalSemaphores =&cs;
//
// VkFence fence=*fence_list[current_fence];
//
// VkResult result=vkQueueSubmit(sc_attr->graphics_queue,1,&submit_info,fence);
//
// if(++current_fence==sc_attr->swap_chain_count)
// current_fence=0;
//
// //不在这里立即等待fence完成是因为有可能queue submit需要久一点工作时间我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence而且是另一帧的fence。这样有利于异步处理
//
// return(result==VK_SUCCESS);
//}
//
//bool Swapchain::SubmitDraw(List<VkCommandBuffer> &cmd_lists,List<VkSemaphore> &wait_sems,List<VkSemaphore> &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(sc_attr->graphics_queue,1,&submit_info,fence);
//
// if(++current_fence==sc_attr->swap_chain_count)
// current_fence=0;
//
// //不在这里立即等待fence完成是因为有可能queue submit需要久一点工作时间我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence而且是另一帧的fence。这样有利于异步处理
//
// return(result==VK_SUCCESS);
//}
//
//bool Swapchain::PresentBackbuffer(vulkan::Semaphore *render_complete_semaphore)
//{
// VkSemaphore sem=*render_complete_semaphore;
//
// present_info.pWaitSemaphores=&sem;
// present_info.pImageIndices=&current_frame;
//
// VkResult result=vkQueuePresentKHR(sc_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(sc_attr->graphics_queue);
//
// if(result!=VK_SUCCESS)
// return(false);
//
// return(true);
//}
VK_NAMESPACE_END