added M_RectTexture2D.cpp

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-09-21 21:36:55 +08:00
parent eb51df8dd0
commit 907cb3c852
9 changed files with 191 additions and 50 deletions

View File

@ -37,14 +37,6 @@ constexpr float tex_coord_data[VERTEX_COUNT][2]=
{0,1} {0,1}
}; };
constexpr float color_data[VERTEX_COUNT][4]=
{
{1,0,0,1},
{1,1,0,1},
{0,1,0,1},
{0,1,1,1}
};
class TestApp:public VulkanApplicationFramework class TestApp:public VulkanApplicationFramework
{ {
private: private:

View File

@ -1,10 +1,12 @@
// 2.texture_rect // texture rect
// 该示例是1.indices_rect的进化演示在矩形上贴上贴图 // 画一个带纹理的矩形2D模式专用
#include"VulkanAppFramework.h" #include"VulkanAppFramework.h"
#include<hgl/graph/VKTexture.h> #include<hgl/graph/VKTexture.h>
#include<hgl/graph/VKSampler.h> #include<hgl/graph/VKSampler.h>
#include<hgl/graph/VKInlinePipeline.h> #include<hgl/graph/VKInlinePipeline.h>
#include<hgl/graph/VKRenderablePrimitiveCreater.h>
#include<hgl/graph/mtl/2d/Material2DCreateConfig.h>
#include<hgl/math/Math.h> #include<hgl/math/Math.h>
using namespace hgl; using namespace hgl;
@ -17,30 +19,16 @@ VK_NAMESPACE_END
constexpr uint32_t SCREEN_WIDTH=256; constexpr uint32_t SCREEN_WIDTH=256;
constexpr uint32_t SCREEN_HEIGHT=256; constexpr uint32_t SCREEN_HEIGHT=256;
constexpr uint32_t VERTEX_COUNT=4; constexpr float position_data[4]=
constexpr float position_data[VERTEX_COUNT][2]=
{ {
{0, 0}, -1,-1,
{SCREEN_WIDTH, 0}, 2,2
{0, SCREEN_HEIGHT},
{SCREEN_WIDTH, SCREEN_HEIGHT}
}; };
constexpr float tex_coord_data[VERTEX_COUNT][2]= constexpr float tex_coord_data[4]=
{ {
{0,0}, 0,0,
{1,0}, 1,1
{0,1},
{1,1}
};
constexpr uint32_t INDEX_COUNT=6;
constexpr uint16 index_data[INDEX_COUNT]=
{
0,1,3,
0,3,2
}; };
class TestApp:public VulkanApplicationFramework class TestApp:public VulkanApplicationFramework
@ -50,21 +38,28 @@ private:
Texture2D * texture =nullptr; Texture2D * texture =nullptr;
Sampler * sampler =nullptr; Sampler * sampler =nullptr;
MaterialInstance * material_instance =nullptr; MaterialInstance * material_instance =nullptr;
Renderable * renderable =nullptr; Renderable * render_obj =nullptr;
Pipeline * pipeline =nullptr; Pipeline * pipeline =nullptr;
private: private:
bool InitMaterial() bool InitMaterial()
{ {
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/Texture2D")); mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"RectTexture2D");
if(!material_instance)return(false);
BindCameraUBO(material_instance); cfg.coordinate_system=CoordinateSystem2D::NDC;
cfg.local_to_world=false;
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreateRectTexture2D(&cfg);
material_instance=db->CreateMaterialInstance(mci);
if(!material_instance)
return(false);
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d")); // pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D,Prim::Triangles); //等同上一行为Framework重载默认使用swapchain的render target pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D,Prim::SolidRectangles); //等同上一行为Framework重载默认使用swapchain的render target
if(!pipeline) if(!pipeline)
return(false); return(false);
@ -73,23 +68,24 @@ private:
sampler=db->CreateSampler(); sampler=db->CreateSampler();
if(!material_instance->BindImageSampler(DescriptorSetType::Value,"tex",texture,sampler))return(false); if(!material_instance->BindImageSampler(DescriptorSetType::PerMaterial, ///<描述符合集
mtl::SamplerName::Color, ///<采样器名称
texture, ///<纹理
sampler)) ///<采样器
return(false);
return(true); return(true);
} }
bool InitVBO() bool InitVBO()
{ {
auto primitive=db->CreatePrimitive(VERTEX_COUNT); RenderablePrimitiveCreater rpc(db,1);
if(!primitive)return(false);
if(!primitive->Set(VAN::Position,db->CreateVBO(VF_V2F,VERTEX_COUNT,position_data)))return(false); if(!rpc.SetVBO(VAN::Position,VF_V4F,position_data))return(false);
if(!primitive->Set(VAN::TexCoord,db->CreateVBO(VF_V2F,VERTEX_COUNT,tex_coord_data)))return(false); if(!rpc.SetVBO(VAN::TexCoord,VF_V4F,tex_coord_data))return(false);
if(!primitive->Set(db->CreateIBO16(INDEX_COUNT,index_data)))return(false);
renderable=db->CreateRenderable(primitive,material_instance,pipeline); render_obj=rpc.Create(material_instance,pipeline);
return(render_obj);
return(true);
} }
public: public:
@ -105,7 +101,7 @@ public:
if(!InitVBO()) if(!InitVBO())
return(false); return(false);
BuildCommandBuffer(renderable); BuildCommandBuffer(render_obj);
return(true); return(true);
} }
@ -114,7 +110,7 @@ public:
{ {
VulkanApplicationFramework::Resize(w,h); VulkanApplicationFramework::Resize(w,h);
BuildCommandBuffer(renderable); BuildCommandBuffer(render_obj);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public VulkanApplicationFramework

View File

@ -32,5 +32,6 @@ namespace SamplerName
MaterialCreateInfo *CreateVertexColor2D(const Material2DCreateConfig *); MaterialCreateInfo *CreateVertexColor2D(const Material2DCreateConfig *);
MaterialCreateInfo *CreatePureColor2D(const Material2DCreateConfig *); MaterialCreateInfo *CreatePureColor2D(const Material2DCreateConfig *);
MaterialCreateInfo *CreatePureTexture2D(const Material2DCreateConfig *); MaterialCreateInfo *CreatePureTexture2D(const Material2DCreateConfig *);
MaterialCreateInfo *CreateRectTexture2D(const Material2DCreateConfig *);
STD_MTL_NAMESPACE_END STD_MTL_NAMESPACE_END
#endif//HGL_GRAPH_MTL_2D_CREATE_CONFIG_INCLUDE #endif//HGL_GRAPH_MTL_2D_CREATE_CONFIG_INCLUDE

View File

@ -0,0 +1,105 @@
#include"Std2DMaterial.h"
#include<hgl/shadergen/MaterialCreateInfo.h>
#include<hgl/graph/mtl/2d/Material2DCreateConfig.h>
#include"common/MFRectPrimitive.h"
#include<hgl/graph/mtl/UBOCommon.h>
STD_MTL_NAMESPACE_BEGIN
namespace
{
constexpr const char vs_main[]=R"(
void main()
{
Output.TexCoord=TexCoord;
gl_Position=RectVertexPosition(GetPosition2D());
})";
//一个shader中输出的所有数据会被定义在一个名为Output的结构中。所以编写时要用Output.XXXX来使用。
//而同时这个结构在下一个Shader中以Input名称出现使用时以Input.XXX的形式使用。
constexpr const char gs_main[]=R"(
void main()
{
vec2 vlt=gl_in[0].gl_Position.xy;
vec2 vrb=gl_in[0].gl_Position.zw;
vec2 tlt=Input.TexCoord[0].xy;
vec2 trb=Input.TexCoord[0].zw;
gl_Position=vec4(vlt, vec2(0,1));Output.TexCoord=tlt; EmitVertex();
gl_Position=vec4(vlt.x, vrb.y, vec2(0,1));Output.TexCoord=vec2(tlt.x,trb.y); EmitVertex();
gl_Position=vec4(vrb.x, vlt.y, vec2(0,1));Output.TexCoord=vec2(trb.x,tlt.y); EmitVertex();
gl_Position=vec4(vrb, vec2(0,1));Output.TexCoord=trb; EmitVertex();
EndPrimitive();
})";
constexpr const char fs_main[]=R"(
void main()
{
Color=texture(TextureColor,Input.TexCoord);
})";
class MaterialPureTexture2D:public Std2DMaterial
{
public:
using Std2DMaterial::Std2DMaterial;
~MaterialPureTexture2D()=default;
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);
}
}
vsc->AddInput(VAT_VEC4,VAN::TexCoord);
vsc->AddOutput(VAT_VEC4,"TexCoord");
vsc->AddFunction(func::RectVertexPosition);
vsc->SetMain(vs_main);
return(true);
}
bool CustomGeometryShader(ShaderCreateInfoGeometry *gsc) override
{
gsc->SetGeom(Prim::Points,Prim::TriangleStrip,4);
gsc->AddOutput(VAT_VEC2,"TexCoord");
gsc->SetMain(fs_main);
return(true);
}
bool CustomFragmentShader(ShaderCreateInfoFragment *fsc) override
{
mci->AddSampler(VK_SHADER_STAGE_FRAGMENT_BIT,DescriptorSetType::PerMaterial,SamplerType::Sampler2D,mtl::SamplerName::Color);
fsc->AddOutput(VAT_VEC4,"Color"); //Fragment shader的输出等于最终的RT了所以这个名称其实随便起。
fsc->SetMain(fs_main);
return(true);
}
};//class MaterialPureTexture2D:public Std2DMaterial
}//namespace
MaterialCreateInfo *CreatePureTexture2D(const mtl::Material2DCreateConfig *cfg)
{
MaterialPureTexture2D mvc2d(cfg);
return mvc2d.Create();
}
STD_MTL_NAMESPACE_END

View File

@ -49,9 +49,10 @@ SET(STD_MTL_2D_HEADER_PATH ${STD_MTL_HEADER_PATH}/2d)
SET(STD_MTL_2D_SOURCE_FILES ${STD_MTL_2D_HEADER_PATH}/Material2DCreateConfig.h SET(STD_MTL_2D_SOURCE_FILES ${STD_MTL_2D_HEADER_PATH}/Material2DCreateConfig.h
2d/Std2DMaterial.h 2d/Std2DMaterial.h
2d/Std2DMaterial.cpp 2d/Std2DMaterial.cpp
2d/VertexColor2D.cpp 2d/M_VertexColor2D.cpp
2d/PureColor2D.cpp 2d/M_PureColor2D.cpp
2d/PureTexture2D.cpp) 2d/M_PureTexture2D.cpp
2d/M_RectTexture2D.cpp)
SET(STD_MTL_SOURCE ${STD_MTL_HEADER_PATH}/MaterialConfig.h SET(STD_MTL_SOURCE ${STD_MTL_HEADER_PATH}/MaterialConfig.h
${STD_MTL_HEADER_PATH}/StdMaterial.h ${STD_MTL_HEADER_PATH}/StdMaterial.h

View File

@ -0,0 +1,46 @@
#pragma once
#include<hgl/graph/mtl/StdMaterial.h>
STD_MTL_NAMESPACE_BEGIN
namespace func
{
constexpr const char *GetPosition2DRect[size_t(CoordinateSystem2D::RANGE_SIZE)]=
{
R"(
vec4 GetPosition2D()
{
return Position;
}
)",
R"(
vec4 GetPosition2D()
{
return vec4(Position.xy*2-1,Position.zw);
}
)",
R"(
vec4 GetPosition2D()
{
vec4 tmp=viewport.ortho_matrix*vec4(Position.xy,0,1);
return vec4(tmp.xy,Position.zw);
}
)"
};
constexpr const char RectVertexPosition[]=R"(
vec4 RectVertexPosition(vec4 pos)
{
vec4 lt=vec4(pos.xy,vec2(0,1));
vec4 rb=vec4(pos.zw,vec2(0,1));
vec4 lt_fin=g_camera.ortho*lt;
vec4 rb_fin=g_camera.ortho*rb;
return vec4(lt_fin.xy,rb_fin.xy);
}
)";
}//namespace func
STD_MTL_NAMESPACE_END