2023-09-27 11:36:39 +08:00
|
|
|
|
// 画一个带纹理的矩形,2D模式专用
|
2023-09-25 20:32:12 +08:00
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/graph/VKTexture.h>
|
|
|
|
|
#include<hgl/graph/VKSampler.h>
|
|
|
|
|
#include<hgl/graph/VKInlinePipeline.h>
|
|
|
|
|
#include<hgl/graph/VKRenderablePrimitiveCreater.h>
|
2023-10-07 20:59:44 +08:00
|
|
|
|
#include<hgl/graph/mtl/Material2DCreateConfig.h>
|
2023-09-25 20:32:12 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
2023-09-25 21:49:35 +08:00
|
|
|
|
#include<hgl/filesystem/Filename.h>
|
2023-09-25 20:32:12 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
|
|
|
|
//Texture2D *CreateTexture2DFromFile(GPUDevice *device,const OSString &filename);
|
|
|
|
|
VK_NAMESPACE_END
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=256;
|
2023-09-25 20:32:12 +08:00
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=256;
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
float position_data[4]=
|
2023-09-25 20:32:12 +08:00
|
|
|
|
{
|
2023-09-25 21:49:35 +08:00
|
|
|
|
0, //left
|
|
|
|
|
0, //top
|
|
|
|
|
1, //right
|
|
|
|
|
1 //bottom
|
2023-09-25 20:32:12 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr float tex_coord_data[4]=
|
|
|
|
|
{
|
|
|
|
|
0,0,
|
|
|
|
|
1,1
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
constexpr const os_char *tex_filename[]=
|
|
|
|
|
{
|
|
|
|
|
OS_TEXT("001-online resume.Tex2D"),
|
|
|
|
|
OS_TEXT("002-salary.Tex2D"),
|
|
|
|
|
OS_TEXT("003-application.Tex2D"),
|
|
|
|
|
OS_TEXT("004-job interview.Tex2D")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr const size_t TexCount=sizeof(tex_filename)/sizeof(os_char *);
|
|
|
|
|
|
2023-09-25 20:32:12 +08:00
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
SceneNode render_root;
|
|
|
|
|
RenderList * render_list =nullptr;
|
|
|
|
|
|
2023-09-25 20:32:12 +08:00
|
|
|
|
Texture2DArray * texture =nullptr;
|
|
|
|
|
Sampler * sampler =nullptr;
|
2023-09-25 21:49:35 +08:00
|
|
|
|
Material * material =nullptr;
|
2023-09-25 20:32:12 +08:00
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
Pipeline * pipeline =nullptr;
|
2023-09-25 20:32:12 +08:00
|
|
|
|
DeviceBuffer * tex_id_ubo =nullptr;
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
MaterialInstance * mi;
|
|
|
|
|
Renderable * r;
|
|
|
|
|
}render_obj[TexCount]{};
|
|
|
|
|
|
2023-09-25 20:32:12 +08:00
|
|
|
|
private:
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
bool InitTexture()
|
|
|
|
|
{
|
|
|
|
|
texture=db->CreateTexture2DArray( 512,512, ///<纹理尺寸
|
|
|
|
|
TexCount, ///<纹理层数
|
|
|
|
|
PF_BC1_RGBAUN, ///<纹理格式
|
|
|
|
|
false); ///<是否自动产生mipmaps
|
|
|
|
|
|
|
|
|
|
if(!texture)return(false);
|
|
|
|
|
|
|
|
|
|
OSString filename;
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<TexCount;i++)
|
|
|
|
|
{
|
|
|
|
|
filename=filesystem::MergeFilename(OS_TEXT("res/image/icon/freepik"),tex_filename[i]);
|
|
|
|
|
|
|
|
|
|
if(!db->LoadTexture2DToArray(texture,i,filename))
|
|
|
|
|
return(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 20:32:12 +08:00
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2023-09-26 21:49:37 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"RectTexture2DArray",Prim::SolidRectangles);
|
2023-09-25 20:32:12 +08:00
|
|
|
|
|
|
|
|
|
cfg.coordinate_system=CoordinateSystem2D::ZeroToOne;
|
2023-09-25 21:49:35 +08:00
|
|
|
|
cfg.local_to_world=true;
|
2023-09-25 20:32:12 +08:00
|
|
|
|
|
2023-10-11 19:02:17 +08:00
|
|
|
|
material=db->LoadMaterial("Std2D/RectTexture2DArray",&cfg);
|
2023-09-25 20:32:12 +08:00
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
if(!material)
|
2023-09-25 20:32:12 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
pipeline=CreatePipeline(material,InlinePipeline::Solid2D,Prim::SolidRectangles); //等同上一行,为Framework重载,默认使用swapchain的render target
|
2023-09-25 20:32:12 +08:00
|
|
|
|
|
|
|
|
|
if(!pipeline)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
sampler=db->CreateSampler();
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
if(!material->BindImageSampler( DescriptorSetType::PerMaterial, ///<描述符合集
|
|
|
|
|
mtl::SamplerName::Color, ///<采样器名称
|
|
|
|
|
texture, ///<纹理
|
|
|
|
|
sampler)) ///<采样器
|
2023-09-25 20:32:12 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
for(uint32_t i=0;i<TexCount;i++)
|
|
|
|
|
{
|
|
|
|
|
render_obj[i].mi=db->CreateMaterialInstance(material);
|
2023-09-25 20:32:12 +08:00
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
if(!render_obj[i].mi)
|
|
|
|
|
return(false);
|
2023-09-25 20:32:12 +08:00
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
render_obj[i].mi->WriteMIData(i); //设置MaterialInstance的数据
|
|
|
|
|
}
|
2023-09-25 20:32:12 +08:00
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
return(true);
|
2023-09-25 20:32:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
bool InitVBOAndRenderList()
|
2023-09-25 20:32:12 +08:00
|
|
|
|
{
|
|
|
|
|
RenderablePrimitiveCreater rpc(db,1);
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
position_data[2]=1.0f/float(TexCount);
|
|
|
|
|
|
2023-09-25 20:32:12 +08:00
|
|
|
|
if(!rpc.SetVBO(VAN::Position,VF_V4F,position_data))return(false);
|
|
|
|
|
if(!rpc.SetVBO(VAN::TexCoord,VF_V4F,tex_coord_data))return(false);
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
Vector3f offset(1.0f/float(TexCount),0,0);
|
|
|
|
|
|
|
|
|
|
for(uint32_t i=0;i<TexCount;i++)
|
|
|
|
|
{
|
|
|
|
|
render_obj[i].r=rpc.Create(render_obj[i].mi,pipeline);
|
|
|
|
|
|
|
|
|
|
if(!render_obj[i].r)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
offset.x=position_data[2]*float(i);
|
|
|
|
|
|
|
|
|
|
render_root.CreateSubNode(translate(offset),render_obj[i].r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render_root.RefreshMatrix();
|
|
|
|
|
|
|
|
|
|
render_list->Expend(&render_root);
|
|
|
|
|
|
|
|
|
|
return(true);
|
2023-09-25 20:32:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(render_list);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 20:32:12 +08:00
|
|
|
|
bool Init()
|
|
|
|
|
{
|
2023-09-25 21:49:35 +08:00
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH*TexCount,SCREEN_HEIGHT))
|
2023-09-25 20:32:12 +08:00
|
|
|
|
return(false);
|
2023-09-25 21:49:35 +08:00
|
|
|
|
|
|
|
|
|
render_list=new RenderList(device);
|
|
|
|
|
|
|
|
|
|
if(!InitTexture())
|
2023-09-25 20:32:12 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
if(!InitMaterial())
|
2023-09-25 20:32:12 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
if(!InitVBOAndRenderList())
|
2023-09-25 20:32:12 +08:00
|
|
|
|
return(false);
|
2023-09-25 21:49:35 +08:00
|
|
|
|
|
|
|
|
|
BuildCommandBuffer(render_list);
|
2023-09-25 20:32:12 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
|
|
|
|
VulkanApplicationFramework::Resize(w,h);
|
|
|
|
|
|
2023-09-25 21:49:35 +08:00
|
|
|
|
BuildCommandBuffer(render_list);
|
2023-09-25 20:32:12 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
if(!CheckStrideBytesByFormat())
|
|
|
|
|
return 0xff;
|
|
|
|
|
#endif//
|
|
|
|
|
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|