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 69908edc..79908ffe 100644 Binary files a/res/shader/FlatColor.frag.spv and b/res/shader/FlatColor.frag.spv differ diff --git a/res/shader/FlatColor3D.vert b/res/shader/FlatColor3D.vert index b4869f28..c91498ac 100644 --- a/res/shader/FlatColor3D.vert +++ b/res/shader/FlatColor3D.vert @@ -2,19 +2,20 @@ layout(location = 0) in vec3 Vertex; layout(location = 1) in vec3 Color; +layout(location = 2) in vec3 Normal; -layout(binding = 0) uniform SunLightConfig +layout(binding = 0) uniform WorldMatrix { - vec4 color; - vec4 direction; -} sun; - -layout(push_constant) uniform Consts { + mat4 two_dim; mat4 projection; mat4 modelview; mat4 mvp; mat3 normal; -} matrix; +} world; + +layout(push_constant) uniform Consts { + mat4 local_to_world; +} pc; layout(location = 0) out vec4 FragmentColor; diff --git a/res/shader/OnlyPosition3D.vert b/res/shader/OnlyPosition3D.vert deleted file mode 100644 index 6866b5b3..00000000 --- a/res/shader/OnlyPosition3D.vert +++ /dev/null @@ -1,22 +0,0 @@ -#version 450 core - -layout(location = 0) in vec3 Vertex; -layout(location = 1) in vec3 Normal; - -layout(push_constant) uniform MatrixConstants { - mat4 projection; - mat4 modelview; - mat4 mvp; - mat3 normal; -}matrix; - -layout(location = 0) out vec4 FragmentColor; -layout(location = 1) out vec3 FragmentNormal; - -void main() -{ - FragmentColor=vec4(Color,1.0); - FragmentNormal=Normal; - - gl_Position=vec4(Vertex,1.0)*matrix.mvp; -} diff --git a/res/shader/PositionColor3D.vert b/res/shader/PositionColor3D.vert new file mode 100644 index 00000000..dd539dd3 --- /dev/null +++ b/res/shader/PositionColor3D.vert @@ -0,0 +1,26 @@ +#version 450 core + +layout(location = 0) in vec3 Vertex; +layout(location = 1) in vec4 Color; + +layout(binding = 0) uniform WorldMatrix +{ + mat4 two_dim; + mat4 projection; + mat4 modelview; + mat4 mvp; + mat3 normal; +} world; + +layout(push_constant) uniform Consts { + mat4 local_to_world; +} pc; + +layout(location = 0) out vec4 FragmentColor; + +void main() +{ + FragmentColor=Color; + + gl_Position=vec4(Vertex,1.0)*pc.local_to_world*world.mvp; +} diff --git a/res/shader/shader_compile.sh b/res/shader/shader_compile.sh index 7bece093..753b5738 100755 --- a/res/shader/shader_compile.sh +++ b/res/shader/shader_compile.sh @@ -1,7 +1,10 @@ glslangValidator -V -o FlatColor.vert.spv FlatColor.vert +glslangValidator -V -o FlatColor3D.vert.spv FlatColor3D.vert glslangValidator -V -o OnlyPosition.vert.spv OnlyPosition.vert -glslangValidator -V -o OnlyPosition3D.vert.spv OnlyPosition3D.vert glslangValidator -V -o FlatColor.frag.spv FlatColor.frag glslangValidator -V -o FlatTexture.vert.spv FlatTexture.vert glslangValidator -V -o FlatTexture.frag.spv FlatTexture.frag + + +glslangValidator -V -o PositionColor3D.vert.spv PositionColor3D.vert \ No newline at end of file diff --git a/src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.cpp b/src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.cpp index 4cdac091..5d0cc97c 100644 --- a/src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.cpp +++ b/src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.cpp @@ -69,12 +69,18 @@ bool DescriptorSetLayoutCreater::CreatePipelineLayout() if(vkCreateDescriptorSetLayout(*device,&descriptor_layout,nullptr,&dsl)!=VK_SUCCESS) return(false); + + VkPushConstantRange push_constant_rage; + + push_constant_rage.stageFlags=VK_SHADER_STAGE_VERTEX_BIT; + push_constant_rage.size=sizeof(PushConstant); + push_constant_rage.offset=0; VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {}; pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pPipelineLayoutCreateInfo.pNext = nullptr; - pPipelineLayoutCreateInfo.pushConstantRangeCount = 0; - pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr; + pPipelineLayoutCreateInfo.pushConstantRangeCount = 1; + pPipelineLayoutCreateInfo.pPushConstantRanges = &push_constant_rage; pPipelineLayoutCreateInfo.setLayoutCount = 1; pPipelineLayoutCreateInfo.pSetLayouts = &dsl; diff --git a/src/SceneGraph/Camera.cpp b/src/SceneGraph/Camera.cpp index e838103e..14c4357a 100644 --- a/src/SceneGraph/Camera.cpp +++ b/src/SceneGraph/Camera.cpp @@ -26,13 +26,15 @@ namespace hgl void Camera::Refresh() { if(type==CameraType::Perspective) - projection=perspective(fov,width/height,znear,zfar); + matrix.projection=perspective(fov,width/height,znear,zfar); else - projection=ortho(width,height,znear,zfar); + matrix.projection=ortho(width,height,znear,zfar); //这个算的不对 - modelview=hgl::graph::LookAt(eye,center,up_vector); + matrix.modelview=hgl::graph::LookAt(eye,center,up_vector); - mvp=projection*modelview; + matrix.mvp=matrix.projection*matrix.modelview; + + matrix.two_dim=ortho(width,height,znear,zfar); frustum.SetVerticalFovAndAspectRatio(DegToRad(fov),width/height); frustum.SetViewPlaneDistances(znear,zfar); diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index dbf675d8..69427118 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -167,7 +167,7 @@ namespace hgl VB3f *vertex=new VB3f(((pgci->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; }