From c4c63a7add6b5a6b0fe5e48295a3ec46d5eb6783 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 29 May 2019 21:48:56 +0800 Subject: [PATCH] =?UTF-8?q?SceneNode=E7=9F=A9=E9=98=B5=E5=8F=98=E6=8D=A2?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E5=8F=8A=E4=BD=BF=E7=94=A8PushConstants?= =?UTF-8?q?=E4=BC=A0=E9=80=92LocalToWorld=E7=BB=98=E5=88=B6=E6=88=90?= =?UTF-8?q?=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/Geometry3D.cpp | 71 ++++++++---------- inc/hgl/graph/Camera.h | 4 +- inc/hgl/graph/InlineGeometry.h | 6 ++ inc/hgl/graph/RenderList.h | 40 +--------- inc/hgl/graph/SceneOrient.h | 5 ++ inc/hgl/graph/vulkan/VK.h | 5 ++ inc/hgl/graph/vulkan/VKCommandBuffer.h | 18 +---- inc/hgl/math/Matrix.h | 10 +++ res/shader/FlatColor.frag | 2 +- res/shader/FlatColor.frag.spv | Bin 536 -> 384 bytes res/shader/FlatColor3D.vert | 15 ++-- res/shader/OnlyPosition3D.vert | 22 ------ res/shader/PositionColor3D.vert | 26 +++++++ res/shader/shader_compile.sh | 5 +- .../Vulkan/VKDescriptorSetLayoutCreater.cpp | 10 ++- src/SceneGraph/Camera.cpp | 10 ++- src/SceneGraph/InlineGeometry.cpp | 30 +++++++- src/SceneGraph/RenderList.cpp | 43 ++++------- src/SceneGraph/SceneOrient.cpp | 3 + 19 files changed, 164 insertions(+), 161 deletions(-) delete mode 100644 res/shader/OnlyPosition3D.vert create mode 100644 res/shader/PositionColor3D.vert diff --git a/example/Vulkan/Geometry3D.cpp b/example/Vulkan/Geometry3D.cpp index 719c039c..b55f0293 100644 --- a/example/Vulkan/Geometry3D.cpp +++ b/example/Vulkan/Geometry3D.cpp @@ -31,13 +31,11 @@ private: vulkan::Material * material =nullptr; vulkan::DescriptorSets * descriptor_sets =nullptr; - vulkan::Renderable *ro_plane_grid =nullptr, - *ro_cube =nullptr; + vulkan::Renderable *ro_plane_grid[3]; - vulkan::Buffer * ubo_mvp =nullptr; + vulkan::Buffer * ubo_world_matrix =nullptr; vulkan::Pipeline * pipeline_line =nullptr; - vulkan::Pipeline * pipeline_triangles =nullptr; vulkan::CommandBuffer ** cmd_buf =nullptr; public: @@ -69,7 +67,7 @@ private: bool InitMaterial() { - material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition3D.vert.spv"), + material=shader_manage->CreateMaterial(OS_TEXT("PositionColor3D.vert.spv"), OS_TEXT("FlatColor.frag.spv")); if(!material) return(false); @@ -94,34 +92,40 @@ private: pgci.step.u=20; pgci.step.v=20; - ro_plane_grid=CreatePlaneGrid(db,material,&pgci); - } + pgci.side_step.u=10; + pgci.side_step.v=10; - { - struct CubeCreateInfo cci; + pgci.color.Set(0.75,0,0,1); + pgci.side_color.Set(1,0,0,1); - cci.tile.x=0; - cci.tile.y=1; + ro_plane_grid[0]=CreatePlaneGrid(db,material,&pgci); - ro_cube=CreateCube(db,material,&cci); + pgci.color.Set(0,0.75,0,1); + pgci.side_color.Set(0,1,0,1); + + ro_plane_grid[1]=CreatePlaneGrid(db,material,&pgci); + + pgci.color.Set(0,0,0.75,1); + pgci.side_color.Set(0,0,1,1); + ro_plane_grid[2]=CreatePlaneGrid(db,material,&pgci); } } - //bool InitUBO() - //{ - // const VkExtent2D extent=device->GetExtent(); + bool InitUBO() + { + const VkExtent2D extent=device->GetExtent(); - // ubo_mvp=db->CreateUBO(sizeof(WorldConfig),&world); + ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&camera.matrix); - // if(!ubo_mvp) - // return(false); + if(!ubo_world_matrix) + return(false); - // if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp)) - // return(false); + if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_world_matrix)) + return(false); - // descriptor_sets->Update(); - // return(true); - //} + descriptor_sets->Update(); + return(true); + } bool InitPipeline() { @@ -140,14 +144,6 @@ private: db->Add(pipeline_line); - pipeline_creater->Set(PRIM_TRIANGLES); - - pipeline_triangles=pipeline_creater->Create(); - if(!pipeline_triangles) - return(false); - - db->Add(pipeline_triangles); - delete pipeline_creater; } @@ -156,12 +152,11 @@ private: bool InitScene() { - render_root.Add(db->CreateRenderableInstance(pipeline_line,descriptor_sets,ro_plane_grid)); - - render_root.Add(db->CreateRenderableInstance(pipeline_triangles,descriptor_sets,ro_cube),scale(50,50,50)); - //render_root.Add(db->CreateRenderableInstance(pipeline,descriptor_sets,ro_circle)); + render_root.Add(db->CreateRenderableInstance(pipeline_line,descriptor_sets,ro_plane_grid[0])); + render_root.Add(db->CreateRenderableInstance(pipeline_line,descriptor_sets,ro_plane_grid[1]),rotate(90,0,1,0)); + render_root.Add(db->CreateRenderableInstance(pipeline_line,descriptor_sets,ro_plane_grid[2]),rotate(90,1,0,0)); - render_root.RefreshMatrix(&(camera.mvp)); + render_root.RefreshMatrix(); render_root.ExpendToList(&render_list); return(true); @@ -206,8 +201,8 @@ public: CreateRenderObject(); -// if(!InitUBO()) -// return(false); + if(!InitUBO()) + return(false); if(!InitPipeline()) return(false); diff --git a/inc/hgl/graph/Camera.h b/inc/hgl/graph/Camera.h index aaa1dae7..87bc5cd2 100644 --- a/inc/hgl/graph/Camera.h +++ b/inc/hgl/graph/Camera.h @@ -32,9 +32,7 @@ namespace hgl public: - Matrix4f projection; - Matrix4f modelview; - Matrix4f mvp; + WorldMatrix matrix; Frustum frustum; diff --git a/inc/hgl/graph/InlineGeometry.h b/inc/hgl/graph/InlineGeometry.h index 1386bf0e..54fa623f 100644 --- a/inc/hgl/graph/InlineGeometry.h +++ b/inc/hgl/graph/InlineGeometry.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace hgl { namespace graph @@ -50,6 +51,11 @@ namespace hgl { Vector3f coord[4]; vec2 step; + + vec2 side_step; //到边界的步数 + + Color4f color; //一般线条颜色 + Color4f side_color; //边界线条颜色 };//struct PlaneGridCreateInfo vulkan::Renderable *CreatePlaneGrid(SceneDB *db,vulkan::Material *mtl,const PlaneGridCreateInfo *pgci); diff --git a/inc/hgl/graph/RenderList.h b/inc/hgl/graph/RenderList.h index cbac3aeb..eeea1571 100644 --- a/inc/hgl/graph/RenderList.h +++ b/inc/hgl/graph/RenderList.h @@ -11,51 +11,28 @@ namespace hgl { class RenderableInstance; - struct UBOMatrixData - { - Matrix4f projection; - Matrix4f modelview; - Matrix4f mvp; - Matrix3f normal; - };// - - struct UBOSkyLight - { - Color4f sun_color; - Vector4f sun_direction; - };// - class RenderList { vulkan::CommandBuffer *cmd_buf; - private: - - Camera camera; - - Frustum frustum; - - private: - - UBOMatrixData ubo_matrix; - UBOSkyLight ubo_skylight; - private: List scene_node_list; + vulkan::PushConstant * last_pc; vulkan::Pipeline * last_pipeline; vulkan::DescriptorSets *last_desc_sets; vulkan::Renderable * last_renderable; - void Render(RenderableInstance *); - void Render(List &); + void Render(SceneNode *,RenderableInstance *); + void Render(SceneNode *,List &); public: RenderList() { cmd_buf=nullptr; + last_pc=nullptr; last_pipeline=nullptr; last_desc_sets=nullptr; last_renderable=nullptr; @@ -66,15 +43,6 @@ namespace hgl void Add (SceneNode *node) {if(node)scene_node_list.Add(node);} void Clear () {scene_node_list.ClearData();} - void SetCamera(const Camera &); - void SetMVP(const Matrix4f &proj,const Matrix4f &mv); - - void SetSkyLightColor(const Color4f &c,const Vector4f &d) - { - ubo_skylight.sun_color=c; - ubo_skylight.sun_direction=d; - } - bool Render(vulkan::CommandBuffer *); };//class RenderList }//namespace graph diff --git a/inc/hgl/graph/SceneOrient.h b/inc/hgl/graph/SceneOrient.h index 1056f256..f961df45 100644 --- a/inc/hgl/graph/SceneOrient.h +++ b/inc/hgl/graph/SceneOrient.h @@ -3,6 +3,7 @@ //#include #include +#include //#include namespace hgl { @@ -23,11 +24,15 @@ namespace hgl Matrix4f InverseLocalMatrix; ///<反向当前矩阵 Matrix4f InverseLocalToWorldMatrix; ///<反向当前到世界矩阵 + vulkan::PushConstant pc; + public: SceneOrient(); virtual ~SceneOrient()=default; + vulkan::PushConstant *GetPushConstant(){return &pc;} + Matrix4f & SetLocalMatrix (const Matrix4f &); ///<设定当前节点矩阵 Matrix4f & SetLocalToWorldMatrix (const Matrix4f &); ///<设定当前节点到世界矩阵 diff --git a/inc/hgl/graph/vulkan/VK.h b/inc/hgl/graph/vulkan/VK.h index 2c8888fa..c0c1e07e 100644 --- a/inc/hgl/graph/vulkan/VK.h +++ b/inc/hgl/graph/vulkan/VK.h @@ -63,6 +63,11 @@ enum class ShaderType Compute =VK_SHADER_STAGE_COMPUTE_BIT };// +struct PushConstant +{ + Matrix4f local_to_world; +}; + #ifdef _DEBUG bool CheckStrideBytesByFormat(); ///<检验所有数据类型长度数组是否符合规则 #endif//_DEBUG diff --git a/inc/hgl/graph/vulkan/VKCommandBuffer.h b/inc/hgl/graph/vulkan/VKCommandBuffer.h index 7071e9ef..db108b2d 100644 --- a/inc/hgl/graph/vulkan/VKCommandBuffer.h +++ b/inc/hgl/graph/vulkan/VKCommandBuffer.h @@ -7,21 +7,6 @@ VK_NAMESPACE_BEGIN //push constant 一般只有128/256字节,仅能存在矩阵。 //所以我们将每个对象的独立变换矩阵存在push constant中 - -struct PushConstant256 -{ - Matrix4f projection; - Matrix4f modelview; - Matrix4f mvp; - Matrix3f normal; -};// - -struct PushConstant128 -{ - Matrix4f projection; - Matrix4f modelview; -};// - class CommandBuffer { VkDevice device; @@ -112,8 +97,7 @@ public: vkCmdPushConstants(cmd_buf,pipeline_layout,(VkShaderStageFlagBits)shader_type,offset,size,pValues); } - void PushConstants(const PushConstant256 *pc){vkCmdPushConstants(cmd_buf,pipeline_layout,VK_SHADER_STAGE_VERTEX_BIT,0,sizeof(PushConstant256),pc);} - void PushConstants(const PushConstant128 *pc){vkCmdPushConstants(cmd_buf,pipeline_layout,VK_SHADER_STAGE_VERTEX_BIT,0,sizeof(PushConstant128),pc);} + void PushConstants(const PushConstant *pc){vkCmdPushConstants(cmd_buf,pipeline_layout,VK_SHADER_STAGE_VERTEX_BIT,0,sizeof(PushConstant),pc);} bool Bind(Renderable *); diff --git a/inc/hgl/math/Matrix.h b/inc/hgl/math/Matrix.h index 306b4deb..4983bd04 100644 --- a/inc/hgl/math/Matrix.h +++ b/inc/hgl/math/Matrix.h @@ -11,6 +11,16 @@ namespace hgl using Matrix3f=float3x3; using Matrix4f=float4x4; + struct WorldMatrix + { + Matrix4f two_dim; //2D矩阵 + + Matrix4f projection; + Matrix4f modelview; + Matrix4f mvp; + Matrix3f normal; + };// + inline Matrix4f identity() { return Matrix4f::identity; diff --git a/res/shader/FlatColor.frag b/res/shader/FlatColor.frag index 6a19256d..1e87450b 100644 --- a/res/shader/FlatColor.frag +++ b/res/shader/FlatColor.frag @@ -5,5 +5,5 @@ layout(location = 0) out vec4 FragColor; void main() { - FragColor=vec4(FragmentColor.rgb,1); + FragColor=FragmentColor; } diff --git a/res/shader/FlatColor.frag.spv b/res/shader/FlatColor.frag.spv index 69908edc099e8e7853d5536df7ee878bea304dc3..79908ffe99160f8ab663bbc7daa0c60b057d5667 100644 GIT binary patch delta 55 zcmbQi(!k8i%%sfDz`)4B&cMOIyOGzJadHBqiVP0}0|Pe@+c7XRZ~|!%|1VIL1rP%O Drwau` delta 209 zcmXwzF$%&!6hvpUiBWVlF^a9Aon;y;^#;)sC3mp#8Xi#ajhGJ)=Fk3FW{9sm#q3Z@ zL8YeJyi*(YJLOtPnm~jTyY&jB;rhIVW1o<7=!p4v3HFR9Kn4#Lzi@^2jc4>~v_0RB e-umx3d#4oL)PVE8_d7+ly3lRL;>QnPoZ$step.u+1)+(pgci->step.v+1))*2); vertex->Begin(); - for(int row=0;row<=pgci->step.u;row++) + for(uint row=0;row<=pgci->step.u;row++) { float pos=float(row)/float(pgci->step.u); @@ -175,7 +175,7 @@ namespace hgl to(pgci->coord[3],pgci->coord[2],pos)); } - for(int col=0;col<=pgci->step.v;col++) + for(uint col=0;col<=pgci->step.v;col++) { float pos=float(col)/float(pgci->step.v); @@ -188,6 +188,32 @@ namespace hgl render_obj->Set(vertex_binding,db->CreateVBO(vertex)); render_obj->SetBoundingBox(vertex->GetAABB()); + const int color_binding=vsm->GetStageInputBinding("Color"); + if(color_binding!=-1) + { + VB4f *color=new VB4f(((pgci->step.u+1)+(pgci->step.v+1))*2); + + color->Begin(); + for(uint row=0;row<=pgci->step.u;row++) + { + if((row%pgci->side_step.u)==0) + color->Fill(pgci->side_color,2); + else + color->Fill(pgci->color,2); + } + + for(uint col=0;col<=pgci->step.v;col++) + { + if((col%pgci->side_step.v)==0) + color->Fill(pgci->side_color,2); + else + color->Fill(pgci->color,2); + } + color->End(); + + render_obj->Set(color_binding,db->CreateVBO(color)); + } + delete vertex; db->Add(render_obj); return render_obj; diff --git a/src/SceneGraph/RenderList.cpp b/src/SceneGraph/RenderList.cpp index 55124dd2..17a33cc5 100644 --- a/src/SceneGraph/RenderList.cpp +++ b/src/SceneGraph/RenderList.cpp @@ -26,42 +26,29 @@ namespace hgl // return (((Frustum *)fc)->BoxIn(node->GetWorldBoundingBox())!=Frustum::OUTSIDE); //} - void RenderList::SetCamera(const Camera &cam) - { - camera=cam; - - ubo_matrix.projection=camera.projection; - ubo_matrix.modelview=camera.modelview; - ubo_matrix.mvp =ubo_matrix.projection*ubo_matrix.modelview; - ubo_matrix.normal =ubo_matrix.modelview.Float3x3Part(); //法线矩阵为3x3 - - frustum=camera.frustum; - } - - void RenderList::SetMVP(const Matrix4f &proj,const Matrix4f &mv) - { - ubo_matrix.projection =proj; - ubo_matrix.modelview =mv; - ubo_matrix.mvp =proj*mv; - ubo_matrix.normal =ubo_matrix.modelview.Float3x3Part(); //法线矩阵为3x3 - } - - void RenderList::Render(RenderableInstance *ri) + void RenderList::Render(SceneNode *node,RenderableInstance *ri) { if(last_pipeline!=ri->GetPipeline()) { - cmd_buf->Bind(ri->GetPipeline()); - last_pipeline=ri->GetPipeline(); + cmd_buf->Bind(last_pipeline); + last_desc_sets=nullptr; } if(last_desc_sets!=ri->GetDescriptorSets()) { - cmd_buf->Bind(ri->GetDescriptorSets()); - last_desc_sets=ri->GetDescriptorSets(); + + cmd_buf->Bind(last_desc_sets); + } + + if(last_pc!=node->GetPushConstant()) + { + last_pc=node->GetPushConstant(); + + cmd_buf->PushConstants(last_pc); } //更新fin_mvp @@ -87,14 +74,14 @@ namespace hgl } } - void RenderList::Render(List &ri_list) + void RenderList::Render(SceneNode *node,List &ri_list) { const int count=ri_list.GetCount(); RenderableInstance **ri=ri_list.GetData(); for(int i=0;irenderable_instances); + Render(*node,(*node)->renderable_instances); ++node; } diff --git a/src/SceneGraph/SceneOrient.cpp b/src/SceneGraph/SceneOrient.cpp index 7692b3cf..ea33da18 100644 --- a/src/SceneGraph/SceneOrient.cpp +++ b/src/SceneGraph/SceneOrient.cpp @@ -7,6 +7,7 @@ namespace hgl SceneOrient::SceneOrient() { + pc.local_to_world= LocalMatrix= LocalToWorldMatrix= InverseLocalMatrix= @@ -28,6 +29,8 @@ namespace hgl InverseLocalToWorldMatrix=inverse(LocalToWorldMatrix); + pc.local_to_world=LocalToWorldMatrix; + return LocalToWorldMatrix; }