diff --git a/example/Vulkan/Deferred.cpp b/example/Vulkan/Deferred.cpp index 689c3854..edbe4871 100644 --- a/example/Vulkan/Deferred.cpp +++ b/example/Vulkan/Deferred.cpp @@ -96,7 +96,9 @@ private: *ro_sphere, *ro_torus, *ro_cylinder, - *ro_cone; + *ro_cone, + + *ro_plane; vulkan::Sampler * sampler=nullptr; @@ -153,8 +155,8 @@ private: bool InitGBuffer() { - gbuffer.extent.width =512; - gbuffer.extent.height =512; + gbuffer.extent.width =1024; + gbuffer.extent.height =1024; gbuffer.render_complete_semaphore =device->CreateSem(); @@ -263,7 +265,7 @@ private: pipeline_creater->SetDepthTest(false); pipeline_creater->SetDepthWrite(false); pipeline_creater->SetCullMode(VK_CULL_MODE_NONE); - pipeline_creater->Set(PRIM_TRIANGLES); + pipeline_creater->Set(PRIM_TRIANGLE_FAN); sp->pipeline_solid=pipeline_creater->Create(); @@ -277,14 +279,13 @@ private: bool InitMaterial() { if(!InitSubpass(&sp_gbuffer, OS_TEXT("res/shader/gbuffer_opaque.vert.spv"),OS_TEXT("res/shader/gbuffer_opaque.frag.spv")))return(false); - //if(!InitSubpass(&sp_composition,OS_TEXT("res/shader/ds_composition.vert.spv"),OS_TEXT("res/shader/ds_composition.frag.spv")))return(false); + if(!InitSubpass(&sp_composition,OS_TEXT("res/shader/gbuffer_composition.vert.spv"),OS_TEXT("res/shader/gbuffer_composition.frag.spv")))return(false); if(!InitGBufferPipeline(&sp_gbuffer))return(false); - //if(!InitCompositionPipeline(&sp_composition))return(false); + if(!InitCompositionPipeline(&sp_composition))return(false); texture.color =vulkan::LoadTGATexture(OS_TEXT("res/image/cardboardPlainStain.tga"),device); texture.normal =vulkan::LoadTGATexture(OS_TEXT("res/image/APOCWALL029_NRM.tga"),device); - //texture.normal =vulkan::LoadTGATexture(OS_TEXT("res/image/flat_normal.tga"),device); //texture.specular=vulkan::LoadTGATexture(OS_TEXT("res/image/APOCWALL029_SPEC.tga"),device); VkSamplerCreateInfo sampler_create_info; @@ -310,12 +311,17 @@ private: sampler=device->CreateSampler(&sampler_create_info); - sp_gbuffer.desc_sets->BindSampler(sp_gbuffer.material->GetSampler("TextureColor"),texture.color,sampler); - sp_gbuffer.desc_sets->BindSampler(sp_gbuffer.material->GetSampler("TextureNormal"),texture.normal,sampler); - InitCameraUBO(sp_gbuffer.desc_sets,sp_gbuffer.material->GetUBO("world")); + sp_gbuffer.desc_sets->BindSampler(sp_gbuffer.material->GetSampler("TextureColor" ),texture.color, sampler); + sp_gbuffer.desc_sets->BindSampler(sp_gbuffer.material->GetSampler("TextureNormal" ),texture.normal, sampler); sp_gbuffer.desc_sets->Update(); + + sp_composition.desc_sets->BindSampler(sp_composition.material->GetSampler("GB_Position" ),gbuffer.position, sampler); + sp_composition.desc_sets->BindSampler(sp_composition.material->GetSampler("GB_Normal" ),gbuffer.normal, sampler); + sp_composition.desc_sets->BindSampler(sp_composition.material->GetSampler("GB_Color" ),gbuffer.color, sampler); + sp_composition.desc_sets->Update(); + return(true); } @@ -383,6 +389,13 @@ private: ro_cone=CreateRenderableCone(db,mtl,&cci); } } + + bool InitCompositionRenderable() + { + ro_plane=CreateRenderableGBufferComposition(db,sp_composition.material); + + return ro_plane; + } bool InitScene(SubpassParam *sp) { @@ -438,6 +451,9 @@ public: if(!InitGBufferCommandBuffer()) return(false); + if(!InitCompositionRenderable()) + return(false); + return(true); } @@ -453,12 +469,11 @@ public: } void BuildCommandBuffer(uint32_t index) override - { - render_root.RefreshMatrix(); - render_list.Clear(); - render_root.ExpendToList(&render_list); - - VulkanApplicationFramework::BuildCommandBuffer(index,&render_list); + { + VulkanApplicationFramework::BuildCommandBuffer( index, + sp_composition.pipeline_solid, + sp_composition.desc_sets, + ro_plane); } };//class TestApp:public CameraAppFramework diff --git a/inc/hgl/graph/InlineGeometry.h b/inc/hgl/graph/InlineGeometry.h index 0a371d20..cbd7e918 100644 --- a/inc/hgl/graph/InlineGeometry.h +++ b/inc/hgl/graph/InlineGeometry.h @@ -21,6 +21,11 @@ namespace hgl vulkan::Renderable *CreateRenderableRectangle(SceneDB *db,vulkan::Material *mtl,const RectangleCreateInfo *rci); + /** + * 创建延迟渲染用全屏平面 + */ + vulkan::Renderable *CreateRenderableGBufferComposition(SceneDB *db,vulkan::Material *mtl); + /** * 圆角矩形创建信息(扇形/线圈) */ @@ -69,6 +74,13 @@ namespace hgl struct PlaneCreateInfo { Vector2f tile; + + public: + + PlaneCreateInfo() + { + tile.Set(1,1); + } };//struct PlaneCreateInfo /** diff --git a/res/shader/gbuffer_composition.frag b/res/shader/gbuffer_composition.frag index b86a2266..4c5ea8a2 100644 --- a/res/shader/gbuffer_composition.frag +++ b/res/shader/gbuffer_composition.frag @@ -1,9 +1,46 @@ #version 450 core +layout(binding = 0) uniform WorldMatrix +{ + mat4 ortho; + mat4 projection; + mat4 modelview; + mat4 mvp; + vec4 view_pos; +} world; + +layout(push_constant) uniform Consts { + mat4 local_to_world; +} pc; + +layout(binding = 0) uniform sampler2D GB_Position; +layout(binding = 1) uniform sampler2D GB_Normal; +layout(binding = 2) uniform sampler2D GB_Color; + layout(location = 0) in vec2 FragmentPosition; layout(location = 0) out vec4 FragColor; + + void main() { - FragColor=vec4(normalize(FragmentPosition),0.0,1.0); + vec3 pos =texture(GB_Position, FragmentPosition).xyz; + vec3 normal =texture(GB_Normal, FragmentPosition).xyz; + vec3 color =texture(GB_Color, FragmentPosition).xyz; + + vec3 light_pos=vec3(1,1,1); + vec3 light_halfVector=vec3(1,1,1); + float pf; + + float nDotVP=max(0.0,dot(normal,normalize(light_pos))); + float nDotHV=max(0.0,dot(normal,normalize(light_halfVector))); + + if(nDotVP==0.0) + { + pf=0.0; + } + else + { + pf=pow(nDotHV, + } } diff --git a/res/shader/gbuffer_composition.vert b/res/shader/gbuffer_composition.vert index 0323f6ab..2edde84d 100644 --- a/res/shader/gbuffer_composition.vert +++ b/res/shader/gbuffer_composition.vert @@ -8,5 +8,5 @@ void main() { gl_Position=vec4(Vertex,0.0,1.0); - FragmentPosition=Vertex; + FragmentPosition=(Vertex+1.0)/2.0; } diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index 8617d4ec..cb124c9c 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -153,6 +153,15 @@ namespace hgl return gc.Finish(); } + vulkan::Renderable *CreateRenderableGBufferComposition(SceneDB *db,vulkan::Material *mtl) + { + RectangleCreateInfo rci; + + rci.scope.Set(-1,-1,2,2); + + return CreateRenderableRectangle(db,mtl,&rci); + } + vulkan::Renderable *CreateRenderableRoundRectangle(SceneDB *db,vulkan::Material *mtl,const RoundRectangleCreateInfo *rci) { GeometryCreater2D gc(db,mtl);