2019-06-26 20:44:53 +08:00
|
|
|
// 9.延迟渲染
|
|
|
|
// 简单的延迟渲染测试,仅一个太阳光
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
|
|
|
#include<hgl/graph/InlineGeometry.h>
|
|
|
|
#include<hgl/graph/SceneDB.h>
|
|
|
|
#include<hgl/graph/RenderableInstance.h>
|
|
|
|
#include<hgl/graph/RenderList.h>
|
2019-07-03 19:45:39 +08:00
|
|
|
#include<hgl/graph/vulkan/VKTexture.h>
|
|
|
|
#include<hgl/graph/vulkan/VKImageView.h>
|
|
|
|
#include<hgl/graph/vulkan/VKFramebuffer.h>
|
2019-06-26 20:44:53 +08:00
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=128;
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=128;
|
|
|
|
|
|
|
|
struct AtomsphereData
|
|
|
|
{
|
|
|
|
alignas(16) Vector3f position;
|
|
|
|
float intensity;
|
|
|
|
float scattering_direction;
|
|
|
|
};//
|
|
|
|
|
2019-07-03 19:45:39 +08:00
|
|
|
using Texture2DPointer=vulkan::Texture2D *;
|
|
|
|
|
2019-06-26 20:44:53 +08:00
|
|
|
class TestApp:public CameraAppFramework
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
SceneNode render_root;
|
|
|
|
RenderList render_list;
|
|
|
|
|
2019-07-03 19:45:39 +08:00
|
|
|
struct DeferredGBuffer
|
|
|
|
{
|
|
|
|
uint32_t width,height;
|
|
|
|
vulkan::Framebuffer *framebuffer;
|
|
|
|
vulkan::RenderPass *renderpass;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
2019-07-04 19:49:18 +08:00
|
|
|
Texture2DPointer position,normal,color,depth;
|
2019-07-03 19:45:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Texture2DPointer texture_list[4];
|
|
|
|
};
|
|
|
|
|
2019-07-05 17:00:49 +08:00
|
|
|
List<VkFormat> gbuffer_format_list;
|
2019-07-03 19:45:39 +08:00
|
|
|
List<vulkan::ImageView *> image_view_list;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
List<VkAttachmentDescription> desc_list;
|
|
|
|
List<VkAttachmentReference> ref_list;
|
|
|
|
}attachment;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
List<VkSubpassDescription> desc;
|
|
|
|
List<VkSubpassDependency> dependency;
|
|
|
|
}subpass;
|
|
|
|
}gbuffer;//
|
|
|
|
|
2019-06-26 20:44:53 +08:00
|
|
|
struct SubpassParam
|
|
|
|
{
|
2019-06-27 21:26:57 +08:00
|
|
|
vulkan::Material * material;
|
2019-06-26 20:44:53 +08:00
|
|
|
vulkan::DescriptorSets *desc_sets;
|
|
|
|
vulkan::Pipeline * pipeline;
|
|
|
|
};//
|
|
|
|
|
2019-06-28 20:46:32 +08:00
|
|
|
SubpassParam sp_gbuffer;
|
|
|
|
SubpassParam sp_composition;
|
2019-06-26 20:44:53 +08:00
|
|
|
|
|
|
|
vulkan::Renderable *ro_sphere;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2019-07-03 19:45:39 +08:00
|
|
|
bool InitGBuffer()
|
|
|
|
{
|
|
|
|
gbuffer.width=power_to_2(SCREEN_WIDTH);
|
|
|
|
gbuffer.height=power_to_2(SCREEN_HEIGHT);
|
|
|
|
|
|
|
|
gbuffer.position=device->CreateTexture2DColor(FMT_RGB32F, gbuffer.width,gbuffer.height);
|
2019-07-04 19:49:18 +08:00
|
|
|
gbuffer.color =device->CreateTexture2DColor(FMT_RGB32F, gbuffer.width,gbuffer.height);
|
2019-07-03 19:45:39 +08:00
|
|
|
gbuffer.normal =device->CreateTexture2DColor(FMT_RGB32F, gbuffer.width,gbuffer.height);
|
|
|
|
gbuffer.depth =device->CreateTexture2DDepth(FMT_D32F, gbuffer.width,gbuffer.height);
|
|
|
|
|
|
|
|
for(uint i=0;i<3;i++)
|
|
|
|
{
|
2019-07-05 17:00:49 +08:00
|
|
|
gbuffer.gbuffer_format_list.Add(gbuffer.texture_list[i]->GetFormat());
|
2019-07-03 19:45:39 +08:00
|
|
|
gbuffer.image_view_list.Add(gbuffer.texture_list[i]->GetImageView());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!device->CreateAttachment( gbuffer.attachment.ref_list,
|
|
|
|
gbuffer.attachment.desc_list,
|
2019-07-05 17:00:49 +08:00
|
|
|
gbuffer.gbuffer_format_list,
|
2019-07-03 19:45:39 +08:00
|
|
|
gbuffer.depth->GetFormat()))
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
VkSubpassDescription desc;
|
|
|
|
|
|
|
|
device->CreateSubpassDescription(desc,gbuffer.attachment.ref_list);
|
|
|
|
|
|
|
|
gbuffer.subpass.desc.Add(desc);
|
|
|
|
|
2019-07-05 17:35:43 +08:00
|
|
|
device->CreateSubpassDependency(gbuffer.subpass.dependency,2); //为啥要2个还不清楚
|
2019-07-03 19:45:39 +08:00
|
|
|
|
|
|
|
gbuffer.renderpass=device->CreateRenderPass(gbuffer.attachment.desc_list,
|
|
|
|
gbuffer.subpass.desc,
|
|
|
|
gbuffer.subpass.dependency,
|
2019-07-05 17:00:49 +08:00
|
|
|
gbuffer.gbuffer_format_list,
|
2019-07-03 19:45:39 +08:00
|
|
|
gbuffer.depth->GetFormat());
|
|
|
|
|
|
|
|
if(!gbuffer.renderpass)
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
gbuffer.framebuffer=vulkan::CreateFramebuffer(device,gbuffer.renderpass,gbuffer.image_view_list);
|
|
|
|
|
|
|
|
if(!gbuffer.framebuffer)
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
2019-06-27 21:26:57 +08:00
|
|
|
bool InitSubpass(SubpassParam *sp,const OSString &vs,const OSString &fs)
|
2019-06-26 20:44:53 +08:00
|
|
|
{
|
2019-06-27 21:26:57 +08:00
|
|
|
sp->material=shader_manage->CreateMaterial(vs,fs);
|
2019-06-26 20:44:53 +08:00
|
|
|
|
2019-06-27 21:26:57 +08:00
|
|
|
if(!sp->material)
|
2019-07-05 17:00:49 +08:00
|
|
|
return(false);
|
2019-06-26 20:44:53 +08:00
|
|
|
|
2019-06-27 21:26:57 +08:00
|
|
|
sp->desc_sets=sp->material->CreateDescriptorSets();
|
2019-06-26 20:44:53 +08:00
|
|
|
|
2019-06-27 21:26:57 +08:00
|
|
|
db->Add(sp->material);
|
|
|
|
db->Add(sp->desc_sets);
|
|
|
|
return(true);
|
2019-06-26 20:44:53 +08:00
|
|
|
}
|
|
|
|
|
2019-06-28 20:46:32 +08:00
|
|
|
bool InitGBufferPipeline(SubpassParam *sp)
|
2019-06-26 20:44:53 +08:00
|
|
|
{
|
2019-07-05 17:03:28 +08:00
|
|
|
AutoDelete<vulkan::PipelineCreater> pipeline_creater=new vulkan::PipelineCreater(device,sp->material,gbuffer.renderpass,device->GetExtent());
|
2019-06-28 20:46:32 +08:00
|
|
|
pipeline_creater->SetDepthTest(true);
|
|
|
|
pipeline_creater->SetDepthWrite(true);
|
|
|
|
pipeline_creater->SetCullMode(VK_CULL_MODE_BACK_BIT);
|
|
|
|
pipeline_creater->Set(PRIM_TRIANGLES);
|
2019-07-05 17:00:49 +08:00
|
|
|
|
2019-06-28 20:46:32 +08:00
|
|
|
sp->pipeline=pipeline_creater->Create();
|
|
|
|
|
|
|
|
if(!sp->pipeline)
|
2019-06-26 20:44:53 +08:00
|
|
|
return(false);
|
|
|
|
|
2019-06-28 20:46:32 +08:00
|
|
|
db->Add(sp->pipeline);
|
2019-06-26 20:44:53 +08:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
2019-06-28 20:46:32 +08:00
|
|
|
bool InitCompositionPipeline(SubpassParam *sp)
|
2019-06-26 20:44:53 +08:00
|
|
|
{
|
2019-07-05 17:03:28 +08:00
|
|
|
AutoDelete<vulkan::PipelineCreater> pipeline_creater=new vulkan::PipelineCreater(device,sp->material,device->GetMainRenderPass(),device->GetExtent());
|
2019-06-28 20:46:32 +08:00
|
|
|
pipeline_creater->SetDepthTest(false);
|
|
|
|
pipeline_creater->SetDepthWrite(false);
|
2019-06-26 20:44:53 +08:00
|
|
|
pipeline_creater->SetCullMode(VK_CULL_MODE_NONE);
|
|
|
|
pipeline_creater->Set(PRIM_TRIANGLES);
|
2019-06-28 20:46:32 +08:00
|
|
|
sp->pipeline=pipeline_creater->Create();
|
2019-07-01 17:04:02 +08:00
|
|
|
|
2019-06-28 20:46:32 +08:00
|
|
|
if(!sp->pipeline)
|
2019-06-26 20:44:53 +08:00
|
|
|
return(false);
|
|
|
|
|
2019-06-28 20:46:32 +08:00
|
|
|
db->Add(sp->pipeline);
|
2019-06-26 20:44:53 +08:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
2019-06-27 21:26:57 +08:00
|
|
|
bool InitMaterial()
|
|
|
|
{
|
2019-07-01 17:04:02 +08:00
|
|
|
if(!InitSubpass(&sp_gbuffer, OS_TEXT("gbuffer_opaque.vert.spv"),OS_TEXT("gbuffer_opaque.frag.spv")))return(false);
|
|
|
|
if(!InitSubpass(&sp_composition, OS_TEXT("ds_composition.vert.spv"),OS_TEXT("ds_composition.frag.spv")))return(false);
|
2019-06-28 20:46:32 +08:00
|
|
|
|
2019-07-01 17:04:02 +08:00
|
|
|
if(!InitGBufferPipeline(&sp_gbuffer))return(false);
|
|
|
|
if(!InitCompositionPipeline(&sp_composition))return(false);
|
|
|
|
|
|
|
|
return(true);
|
2019-06-28 20:46:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CreateRenderObject(vulkan::Material *mtl)
|
|
|
|
{
|
|
|
|
ro_sphere=CreateRenderableSphere(db,mtl,128);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InitUBO(SubpassParam *sp)
|
|
|
|
{
|
|
|
|
if(!InitCameraUBO(sp->desc_sets,sp->material->GetUBO("world")))
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
sp->desc_sets->Update();
|
|
|
|
return(true);
|
2019-06-27 21:26:57 +08:00
|
|
|
}
|
|
|
|
|
2019-06-28 20:46:32 +08:00
|
|
|
bool InitScene(SubpassParam *sp)
|
2019-06-26 20:44:53 +08:00
|
|
|
{
|
2019-06-28 20:46:32 +08:00
|
|
|
if(!InitUBO(sp))
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
CreateRenderObject(sp->material);
|
|
|
|
|
|
|
|
render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_sphere),scale(1000));
|
2019-06-26 20:44:53 +08:00
|
|
|
|
|
|
|
render_root.RefreshMatrix();
|
|
|
|
render_root.ExpendToList(&render_list);
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
{
|
|
|
|
if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
return(false);
|
|
|
|
|
2019-06-28 20:46:32 +08:00
|
|
|
if(!InitScene(&sp_gbuffer))
|
2019-06-26 20:44:53 +08:00
|
|
|
return(false);
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BuildCommandBuffer(uint32_t index) override
|
|
|
|
{
|
|
|
|
render_root.RefreshMatrix();
|
|
|
|
render_list.Clear();
|
|
|
|
render_root.ExpendToList(&render_list);
|
|
|
|
|
|
|
|
VulkanApplicationFramework::BuildCommandBuffer(index,&render_list);
|
|
|
|
}
|
|
|
|
};//class TestApp:public CameraAppFramework
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
{
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|