diff --git a/CMSceneGraph b/CMSceneGraph index 71b56d6d..5ec0f4af 160000 --- a/CMSceneGraph +++ b/CMSceneGraph @@ -1 +1 @@ -Subproject commit 71b56d6d7b1cff70856882351feb0040dc04461f +Subproject commit 5ec0f4af20c873d4b7aeb18c16ac6deedf44dbe2 diff --git a/example/Vulkan/third_triangle.cpp b/example/Vulkan/third_triangle.cpp index 79f05a9e..a3a9429f 100644 --- a/example/Vulkan/third_triangle.cpp +++ b/example/Vulkan/third_triangle.cpp @@ -45,9 +45,16 @@ private: bool InitMaterial() { - AutoDelete mci=mtl::CreateVertexColor2D(mtl::CoordinateSystem2D::ZeroToOne); + { + mtl::Material2DConfig cfg; - material_instance=db->CreateMaterialInstance(mci); + cfg.coordinate_system=mtl::CoordinateSystem2D::ZeroToOne; + cfg.local_to_world=true; + + AutoDelete mci=mtl::CreateVertexColor2D(&cfg); + + material_instance=db->CreateMaterialInstance(mci); + } if(!material_instance) return(false); @@ -70,7 +77,8 @@ private: if(!render_obj) return(false); - render_root.CreateSubNode(render_obj); + render_root.CreateSubNode(translate(-0.25,0),render_obj); + render_root.CreateSubNode(translate( 0.25,0),render_obj); render_root.RefreshMatrix(); diff --git a/inc/hgl/graph/RenderNode2D.h b/inc/hgl/graph/RenderNode2D.h index b6fd5af7..ca057a74 100644 --- a/inc/hgl/graph/RenderNode2D.h +++ b/inc/hgl/graph/RenderNode2D.h @@ -16,7 +16,7 @@ namespace hgl struct RenderNode2D { - Matrix3x4f local_to_world; + Matrix4f local_to_world; Renderable *ri; }; @@ -81,7 +81,7 @@ namespace hgl MaterialRenderList2D(GPUDevice *d,Material *m); ~MaterialRenderList2D(); - void Add(Renderable *ri,const Matrix3x4f &mat); + void Add(Renderable *ri,const Matrix4f &mat); void ClearData() { diff --git a/inc/hgl/graph/mtl/2d/VertexColor2D.h b/inc/hgl/graph/mtl/2d/VertexColor2D.h index 8f9506dc..23333846 100644 --- a/inc/hgl/graph/mtl/2d/VertexColor2D.h +++ b/inc/hgl/graph/mtl/2d/VertexColor2D.h @@ -1,4 +1,4 @@ -#ifndef HGL_GRAPH_MTL_2D_VERTEX2D_INCLUDE +#ifndef HGL_GRAPH_MTL_2D_VERTEX2D_INCLUDE #define HGL_GRAPH_MTL_2D_VERTEX2D_INCLUDE #include @@ -11,7 +11,14 @@ namespace hgl namespace mtl { - MaterialCreateInfo *CreateVertexColor2D(const CoordinateSystem2D &); + struct Material2DConfig + { + CoordinateSystem2D coordinate_system; ///<使用的坐标系 + + bool local_to_world=false; ///<包含LocalToWorld矩阵 + }; + + MaterialCreateInfo *CreateVertexColor2D(const Material2DConfig *); }//namespace mtl }//namespace graph }//namespace hgl diff --git a/inc/hgl/graph/mtl/StdMaterial.h b/inc/hgl/graph/mtl/StdMaterial.h index bd59d9f1..045135f7 100644 --- a/inc/hgl/graph/mtl/StdMaterial.h +++ b/inc/hgl/graph/mtl/StdMaterial.h @@ -5,6 +5,7 @@ #define STD_MTL_NAMESPACE_USING using namespace hgl::graph::mtl; +#include #include STD_MTL_NAMESPACE_BEGIN @@ -13,7 +14,9 @@ enum class CoordinateSystem2D { NDC, ZeroToOne, //左上角为0,0;右下角为1,1 - Ortho //左上角为0,0;右下角为(width-1),(height-1) + Ortho, //左上角为0,0;右下角为(width-1),(height-1) + + ENUM_CLASS_RANGE(NDC,Ortho) }; constexpr const ShaderBufferSource SBS_ViewportInfo= @@ -53,4 +56,17 @@ vec3 world_up; float znear,zfar;)" }; + +namespace func +{ + constexpr const char GetLocalToWorld[]=R"( +mat4 GetLocalToWorld() +{ + return mat4(LocalToWorld_0, + LocalToWorld_1, + LocalToWorld_2, + LocalToWorld_3); +} +)"; +}//namespace func STD_MTL_NAMESPACE_END diff --git a/inc/hgl/shadergen/ShaderCreateInfo.h b/inc/hgl/shadergen/ShaderCreateInfo.h index 91a134fa..5b7cdcfa 100644 --- a/inc/hgl/shadergen/ShaderCreateInfo.h +++ b/inc/hgl/shadergen/ShaderCreateInfo.h @@ -3,6 +3,7 @@ #include #include +#include namespace hgl{namespace graph { @@ -21,10 +22,10 @@ protected: protected: - AnsiString main_codes; - AnsiString output_struct; + AnsiStringList function_list; + AnsiString final_shader; SPVData *spv_data; @@ -59,10 +60,7 @@ public: int AddOutput(const graph::VAT &type,const AnsiString &name); int AddOutput(const AnsiString &type,const AnsiString &name); - void SetShaderCodes(const AnsiString &str) - { - main_codes=str; - } + void AddFunction(const AnsiString &str){function_list.Add(str);} const AnsiString &GetOutputStruct()const{return output_struct;} const AnsiString &GetShaderSource()const{return final_shader;} diff --git a/inc/hgl/shadergen/ShaderCreateInfoVertex.h b/inc/hgl/shadergen/ShaderCreateInfoVertex.h index 3bf66e67..6cf63a55 100644 --- a/inc/hgl/shadergen/ShaderCreateInfoVertex.h +++ b/inc/hgl/shadergen/ShaderCreateInfoVertex.h @@ -28,17 +28,17 @@ namespace hgl AddInput(weight,VAN::BoneWeight,VK_VERTEX_INPUT_RATE_VERTEX,VertexInputGroup::Bone); } - void AddLocalToWorld(const uint count) + void AddLocalToWorld() { - VAT l2w{VertexAttribType::BaseType::Float,4}; + VAT vat{VertexAttribType::BaseType::Float,4}; char name[]= "LocalToWorld_?"; - for(uint i=0;iSubNode) - Expend(sub); + ExpendNode(sub); return(true); } diff --git a/src/SceneGraph/RenderNode2D.cpp b/src/SceneGraph/RenderNode2D.cpp index 142ac5b6..d7c6df83 100644 --- a/src/SceneGraph/RenderNode2D.cpp +++ b/src/SceneGraph/RenderNode2D.cpp @@ -65,8 +65,8 @@ namespace hgl { uint count; - VBO *l2w_vbo[3]; - VkBuffer l2w_buffer[3]; + VBO *l2w_vbo[4]; + VkBuffer l2w_buffer[4]; public: @@ -85,6 +85,7 @@ namespace hgl SAFE_CLEAR(l2w_vbo[0]) SAFE_CLEAR(l2w_vbo[1]) SAFE_CLEAR(l2w_vbo[2]) + SAFE_CLEAR(l2w_vbo[3]) count=0; } @@ -93,7 +94,7 @@ namespace hgl Clear(); count=power_to_2(c); - for(uint i=0;i<3;i++) + for(uint i=0;i<4;i++) { l2w_vbo[i]=dev->CreateVBO(VF_V4F,count); l2w_buffer[i]=l2w_vbo[i]->GetBuffer(); @@ -105,7 +106,7 @@ namespace hgl RenderNode2D *rn; glm::vec4 *tp; - for(uint col=0;col<3;col++) + for(uint col=0;col<4;col++) { tp=(glm::vec4 *)(l2w_vbo[col]->Map()); @@ -145,7 +146,7 @@ namespace hgl SAFE_CLEAR(extra_buffer) } - void MaterialRenderList2D::Add(Renderable *ri,const Matrix3x4f &mat) + void MaterialRenderList2D::Add(Renderable *ri,const Matrix4f &mat) { RenderNode2D rn; @@ -278,12 +279,12 @@ namespace hgl if(l2w_binding_count>0) //有变换矩阵信息 { - if(l2w_binding_count!=3) //2D的l2w使用mat3x4f,应该只有3个 + if(l2w_binding_count!=4) return(false); - hgl_cpy(buffer_list+count,extra_buffer->l2w_buffer,3); + hgl_cpy(buffer_list+count,extra_buffer->l2w_buffer,4); - for(uint i=0;i<3;i++) + for(uint i=0;i<4;i++) buffer_offset[count+i]=first*16; //mat3x4f每列都是rgba32f,自然是16字节 count+=l2w_binding_count; diff --git a/src/ShaderGen/2d/VertexColor2D.cpp b/src/ShaderGen/2d/VertexColor2D.cpp index 4278e7f5..fde6da3f 100644 --- a/src/ShaderGen/2d/VertexColor2D.cpp +++ b/src/ShaderGen/2d/VertexColor2D.cpp @@ -5,53 +5,73 @@ #include STD_MTL_NAMESPACE_BEGIN -MaterialCreateInfo *CreateVertexColor2D(const CoordinateSystem2D &cs) +MaterialCreateInfo *CreateVertexColor2D(const Material2DConfig *cfg) { - AnsiString mtl_name; + if(!cfg)return(nullptr); - if(cs==CoordinateSystem2D::NDC )mtl_name="VertexColor2DNDC";else - if(cs==CoordinateSystem2D::ZeroToOne)mtl_name="VertexColor2DZeroToOne";else - if(cs==CoordinateSystem2D::Ortho )mtl_name="VertexColor2DOrtho";else - return(nullptr); + RANGE_CHECK_RETURN_NULLPTR(cfg->coordinate_system) + + AnsiString mtl_name; MaterialCreateInfo *mci=new MaterialCreateInfo( mtl_name, ///<名称 1, ///<最终一个RT输出 false); ///<无深度输出 - AnsiString sfGetPosition; + AnsiString sfComputePosition; - if(cs==CoordinateSystem2D::Ortho) + if(cfg->coordinate_system==CoordinateSystem2D::Ortho) { + mtl_name="VertexColor2DOrtho"; + mci->AddUBO(VK_SHADER_STAGE_VERTEX_BIT, DescriptorSetType::Global, SBS_ViewportInfo); - sfGetPosition="vec4 GetPosition(){return viewport.ortho_matrix*vec4(Position,0,1);}"; + sfComputePosition="vec4 ComputePosition(vec4 pos){return viewport.ortho_matrix*pos;}"; } else - if(cs==CoordinateSystem2D::ZeroToOne) - sfGetPosition="vec4 GetPosition(){return vec4(Position*2-1,0,1);}"; + if(cfg->coordinate_system==CoordinateSystem2D::ZeroToOne) + { + mtl_name="VertexColor2DZeroToOne"; + + sfComputePosition="vec4 ComputePosition(vec4 pos){return vec4(pos.xy*2-1,pos.z,pos.w);}"; + } else - sfGetPosition="vec4 GetPosition(){return vec4(Position,0,1);}"; + { + mtl_name="VertexColor2DNDC"; + + sfComputePosition="vec4 ComputePosition(vec4 pos){return pos;}"; + } //vertex部分 { ShaderCreateInfoVertex *vsc=mci->GetVS(); + vsc->AddOutput(VAT_VEC4,"Color"); + vsc->AddInput(VAT_VEC2,VAN::Position); vsc->AddInput(VAT_VEC4,VAN::Color); - vsc->AddOutput(VAT_VEC4,"Color"); + vsc->AddFunction(sfComputePosition); - vsc->SetShaderCodes( - sfGetPosition+ -R"( -void main() -{ - Output.Color=Color; + AnsiString main_codes="void main()\n{\n\tOutput.Color=Color;\n"; - gl_Position=GetPosition(); -})"); + if(cfg->local_to_world) + { + vsc->AddLocalToWorld(); + + vsc->AddFunction(mtl::func::GetLocalToWorld); + + main_codes+="\tvec4 pos=GetLocalToWorld()*vec4(Position,0,1);\n"; + } + else + { + main_codes+="\tvec4 pos=vec4(Position,0,1);\n"; + } + + main_codes+="\tgl_Position=ComputePosition(pos);\n}"; + + vsc->AddFunction(main_codes); } //fragment部分 @@ -60,7 +80,7 @@ void main() fsc->AddOutput(VAT_VEC4,"Color"); - fsc->SetShaderCodes(R"( + fsc->AddFunction(R"( void main() { Color=Input.Color; diff --git a/src/ShaderGen/ShaderCreateInfo.cpp b/src/ShaderGen/ShaderCreateInfo.cpp index e094e178..18cd2afc 100644 --- a/src/ShaderGen/ShaderCreateInfo.cpp +++ b/src/ShaderGen/ShaderCreateInfo.cpp @@ -121,7 +121,7 @@ bool ShaderCreateInfo::ProcOutput() final_shader+="layout(location=0) out "; final_shader+=output_struct; - final_shader+="Output;\n\n"; + final_shader+="Output;\n"; return(true); } @@ -270,7 +270,11 @@ bool ShaderCreateInfo::CreateShader(ShaderCreateInfo *last_sc) ProcOutput(); - final_shader+=main_codes; + for(uint i=0;iGetShaderStageIO().output; @@ -25,6 +25,8 @@ void ShaderCreateInfoFragment::UseDefaultMain() } main_codes+="}"; + + AddFunction(main_codes); } bool ShaderCreateInfoFragment::ProcOutput()