From 922fc2661b16af54546d66b0d3ad0e5cd8f9c903 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Tue, 26 Sep 2023 21:49:37 +0800 Subject: [PATCH] added primitive in Material2DCreateConfig --- example/Vulkan/first_triangle.cpp | 2 +- example/Vulkan/fourth_triangle.cpp | 2 +- example/Vulkan/second_triangle.cpp | 2 +- example/Vulkan/texture_quad.cpp | 2 +- example/Vulkan/texture_rect.cpp | 2 +- example/Vulkan/texture_rect_array.cpp | 2 +- example/Vulkan/third_triangle.cpp | 2 +- inc/hgl/graph/mtl/2d/Material2DCreateConfig.h | 15 +++++++++++++-- src/ShaderGen/2d/M_RectTexture2D.cpp | 17 ++--------------- src/ShaderGen/2d/Std2DMaterial.cpp | 17 ++++++++++++++--- 10 files changed, 36 insertions(+), 27 deletions(-) diff --git a/example/Vulkan/first_triangle.cpp b/example/Vulkan/first_triangle.cpp index 06c62ebb..5d724e3e 100644 --- a/example/Vulkan/first_triangle.cpp +++ b/example/Vulkan/first_triangle.cpp @@ -87,7 +87,7 @@ private: bool InitAutoMaterial() { - mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"VertexColor2d"); + mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"VertexColor2d",Prim::Triangles); cfg.coordinate_system=CoordinateSystem2D::NDC; cfg.local_to_world=false; diff --git a/example/Vulkan/fourth_triangle.cpp b/example/Vulkan/fourth_triangle.cpp index 170aa14b..cac1f0be 100644 --- a/example/Vulkan/fourth_triangle.cpp +++ b/example/Vulkan/fourth_triangle.cpp @@ -48,7 +48,7 @@ private: bool InitMaterial() { { - mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"PureColor2D"); + mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"PureColor2D",Prim::Triangles); cfg.coordinate_system=CoordinateSystem2D::NDC; cfg.local_to_world=true; diff --git a/example/Vulkan/second_triangle.cpp b/example/Vulkan/second_triangle.cpp index b269258e..a7c27f8f 100644 --- a/example/Vulkan/second_triangle.cpp +++ b/example/Vulkan/second_triangle.cpp @@ -45,7 +45,7 @@ private: bool InitMaterial() { - mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"VertexColor2D"); + mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"VertexColor2D",Prim::Triangles); #ifdef USE_ZERO2ONE_COORD cfg.coordinate_system=CoordinateSystem2D::ZeroToOne; diff --git a/example/Vulkan/texture_quad.cpp b/example/Vulkan/texture_quad.cpp index 05aa2105..13ce0489 100644 --- a/example/Vulkan/texture_quad.cpp +++ b/example/Vulkan/texture_quad.cpp @@ -52,7 +52,7 @@ private: bool InitMaterial() { - mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"PureTexture2d"); + mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"PureTexture2D",Prim::Fan); cfg.coordinate_system=CoordinateSystem2D::NDC; cfg.local_to_world=false; diff --git a/example/Vulkan/texture_rect.cpp b/example/Vulkan/texture_rect.cpp index c8f3d2aa..b7ad107b 100644 --- a/example/Vulkan/texture_rect.cpp +++ b/example/Vulkan/texture_rect.cpp @@ -48,7 +48,7 @@ private: bool InitMaterial() { - mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"RectTexture2D"); + mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"RectTexture2D",Prim::SolidRectangles); cfg.coordinate_system=CoordinateSystem2D::ZeroToOne; cfg.local_to_world=false; diff --git a/example/Vulkan/texture_rect_array.cpp b/example/Vulkan/texture_rect_array.cpp index dbf4fbf1..0c409f7b 100644 --- a/example/Vulkan/texture_rect_array.cpp +++ b/example/Vulkan/texture_rect_array.cpp @@ -90,7 +90,7 @@ private: bool InitMaterial() { - mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"RectTexture2DArray"); + mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"RectTexture2DArray",Prim::SolidRectangles); cfg.coordinate_system=CoordinateSystem2D::ZeroToOne; cfg.local_to_world=true; diff --git a/example/Vulkan/third_triangle.cpp b/example/Vulkan/third_triangle.cpp index 8c68eb7d..df7a8425 100644 --- a/example/Vulkan/third_triangle.cpp +++ b/example/Vulkan/third_triangle.cpp @@ -47,7 +47,7 @@ private: bool InitMaterial() { { - mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"VertexColor2D"); + mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"VertexColor2D",Prim::Triangles); cfg.coordinate_system=CoordinateSystem2D::NDC; cfg.local_to_world=true; diff --git a/inc/hgl/graph/mtl/2d/Material2DCreateConfig.h b/inc/hgl/graph/mtl/2d/Material2DCreateConfig.h index 468585fa..f7f48f0d 100644 --- a/inc/hgl/graph/mtl/2d/Material2DCreateConfig.h +++ b/inc/hgl/graph/mtl/2d/Material2DCreateConfig.h @@ -3,26 +3,37 @@ #include #include +#include STD_MTL_NAMESPACE_BEGIN struct Material2DCreateConfig:public MaterialCreateConfig { + Prim prim; ///<图元类型 + CoordinateSystem2D coordinate_system; ///<使用的坐标系 bool local_to_world; ///<包含LocalToWorld矩阵 - + VAT position_format; /// #include -#include"common/MFRectPrimitive.h" #include STD_MTL_NAMESPACE_BEGIN @@ -49,20 +48,8 @@ void main() bool CustomVertexShader(ShaderCreateInfoVertex *vsc) override { - { - RANGE_CHECK_RETURN_FALSE(cfg->coordinate_system) - - vsc->AddInput(VAT_VEC4,VAN::Position); - - vsc->AddFunction(func::GetPosition2DRect[size_t(cfg->coordinate_system)]); - - if(cfg->coordinate_system==CoordinateSystem2D::Ortho) - { - mci->AddUBO(VK_SHADER_STAGE_VERTEX_BIT, - DescriptorSetType::Global, - SBS_ViewportInfo); - } - } + if(!Std2DMaterial::CustomVertexShader(vsc)) + return(false); vsc->AddInput(VAT_VEC4,VAN::TexCoord); diff --git a/src/ShaderGen/2d/Std2DMaterial.cpp b/src/ShaderGen/2d/Std2DMaterial.cpp index 1831aa5f..dbbbad66 100644 --- a/src/ShaderGen/2d/Std2DMaterial.cpp +++ b/src/ShaderGen/2d/Std2DMaterial.cpp @@ -3,6 +3,7 @@ #include #include #include"common/MFGetPosition.h" +#include"common/MFRectPrimitive.h" STD_MTL_NAMESPACE_BEGIN Std2DMaterial::Std2DMaterial(const Material2DCreateConfig *c) @@ -16,7 +17,9 @@ bool Std2DMaterial::CustomVertexShader(ShaderCreateInfoVertex *vsc) { RANGE_CHECK_RETURN_FALSE(cfg->coordinate_system) - vsc->AddInput(VAT_VEC2,VAN::Position); + vsc->AddInput(cfg->position_format,VAN::Position); + + const bool is_rect=(cfg->prim==Prim::SolidRectangles||cfg->prim==Prim::WireRectangles); if(cfg->local_to_world) { @@ -24,10 +27,18 @@ bool Std2DMaterial::CustomVertexShader(ShaderCreateInfoVertex *vsc) vsc->AddAssign(); - vsc->AddFunction(func::GetPosition2DL2W[size_t(cfg->coordinate_system)]); + if(is_rect) + vsc->AddFunction(func::GetPosition2DRectL2W[size_t(cfg->coordinate_system)]); + else + vsc->AddFunction(func::GetPosition2DL2W[size_t(cfg->coordinate_system)]); } else - vsc->AddFunction(func::GetPosition2D[size_t(cfg->coordinate_system)]); + { + if(is_rect) + vsc->AddFunction(func::GetPosition2DRect[size_t(cfg->coordinate_system)]); + else + vsc->AddFunction(func::GetPosition2D[size_t(cfg->coordinate_system)]); + } if(cfg->coordinate_system==CoordinateSystem2D::Ortho) {