ULRE/example/Vulkan/Deferred.cpp

172 lines
4.3 KiB
C++
Raw Normal View History

// 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>
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;
};//
class TestApp:public CameraAppFramework
{
private:
SceneNode render_root;
RenderList render_list;
struct SubpassParam
{
2019-06-27 21:26:57 +08:00
vulkan::Material * material;
vulkan::DescriptorSets *desc_sets;
vulkan::Pipeline * pipeline;
};//
2019-06-28 20:46:32 +08:00
SubpassParam sp_gbuffer;
SubpassParam sp_composition;
vulkan::Renderable *ro_sphere;
private:
2019-06-27 21:26:57 +08:00
bool InitSubpass(SubpassParam *sp,const OSString &vs,const OSString &fs)
{
2019-06-27 21:26:57 +08:00
sp->material=shader_manage->CreateMaterial(vs,fs);
2019-06-27 21:26:57 +08:00
if(!sp->material)
return(false);
2019-06-27 21:26:57 +08:00
sp->desc_sets=sp->material->CreateDescriptorSets();
2019-06-27 21:26:57 +08:00
db->Add(sp->material);
db->Add(sp->desc_sets);
return(true);
}
2019-06-28 20:46:32 +08:00
bool InitGBufferPipeline(SubpassParam *sp)
{
2019-07-01 17:04:02 +08:00
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(true);
pipeline_creater->SetDepthWrite(true);
pipeline_creater->SetCullMode(VK_CULL_MODE_BACK_BIT);
pipeline_creater->Set(PRIM_TRIANGLES);
sp->pipeline=pipeline_creater->Create();
if(!sp->pipeline)
return(false);
2019-06-28 20:46:32 +08:00
db->Add(sp->pipeline);
2019-06-28 20:46:32 +08:00
delete pipeline_creater;
return(true);
}
2019-06-28 20:46:32 +08:00
bool InitCompositionPipeline(SubpassParam *sp)
{
2019-07-01 17:04:02 +08:00
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);
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)
return(false);
2019-06-28 20:46:32 +08:00
db->Add(sp->pipeline);
delete pipeline_creater;
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-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));
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))
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;
}