2023-09-21 17:15:36 +08:00
|
|
|
|
// texture quad
|
|
|
|
|
// 画一个带纹理的四边形
|
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/graph/VKTexture.h>
|
|
|
|
|
#include<hgl/graph/VKSampler.h>
|
|
|
|
|
#include<hgl/graph/VKInlinePipeline.h>
|
|
|
|
|
#include<hgl/graph/VKRenderablePrimitiveCreater.h>
|
|
|
|
|
#include<hgl/graph/mtl/2d/Material2DCreateConfig.h>
|
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
|
|
|
|
Texture2D *CreateTexture2DFromFile(GPUDevice *device,const OSString &filename);
|
|
|
|
|
VK_NAMESPACE_END
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=256;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=256;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=4;
|
|
|
|
|
|
|
|
|
|
constexpr float position_data[VERTEX_COUNT][2]=
|
|
|
|
|
{
|
|
|
|
|
{-1, -1},
|
|
|
|
|
{ 1, -1},
|
2023-09-21 17:38:02 +08:00
|
|
|
|
{ 1, 1},
|
2023-09-21 17:15:36 +08:00
|
|
|
|
{-1, 1},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr float tex_coord_data[VERTEX_COUNT][2]=
|
|
|
|
|
{
|
|
|
|
|
{0,0},
|
|
|
|
|
{1,0},
|
2023-09-21 17:38:02 +08:00
|
|
|
|
{1,1},
|
|
|
|
|
{0,1}
|
2023-09-21 17:15:36 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
2023-09-21 20:46:08 +08:00
|
|
|
|
Texture2D * texture =nullptr;
|
|
|
|
|
Sampler * sampler =nullptr;
|
2023-09-26 21:16:00 +08:00
|
|
|
|
Material * material =nullptr;
|
2023-09-21 17:15:36 +08:00
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
|
|
|
|
Renderable * render_obj =nullptr;
|
|
|
|
|
Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2023-09-21 20:46:08 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"PureTexture2d");
|
2023-09-21 17:15:36 +08:00
|
|
|
|
|
|
|
|
|
cfg.coordinate_system=CoordinateSystem2D::NDC;
|
|
|
|
|
cfg.local_to_world=false;
|
|
|
|
|
|
2023-09-21 20:46:08 +08:00
|
|
|
|
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreatePureTexture2D(&cfg);
|
2023-09-21 17:15:36 +08:00
|
|
|
|
|
2023-09-26 21:16:00 +08:00
|
|
|
|
material=db->CreateMaterial(mci);
|
2023-09-21 17:15:36 +08:00
|
|
|
|
|
2023-09-26 21:16:00 +08:00
|
|
|
|
if(!material)
|
2023-09-21 17:15:36 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
|
2023-09-26 21:16:00 +08:00
|
|
|
|
pipeline=CreatePipeline(material,InlinePipeline::Solid2D,Prim::Fan); //等同上一行,为Framework重载,默认使用swapchain的render target
|
2023-09-21 17:15:36 +08:00
|
|
|
|
|
|
|
|
|
if(!pipeline)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2023-09-21 20:46:08 +08:00
|
|
|
|
texture=db->LoadTexture2D(OS_TEXT("res/image/lena.Tex2D"),true);
|
|
|
|
|
if(!texture)return(false);
|
2023-09-21 17:15:36 +08:00
|
|
|
|
|
2023-09-21 20:46:08 +08:00
|
|
|
|
sampler=db->CreateSampler();
|
2023-09-21 17:15:36 +08:00
|
|
|
|
|
2023-09-26 21:16:00 +08:00
|
|
|
|
if(!material->BindImageSampler( DescriptorSetType::PerMaterial, ///<描述符合集
|
|
|
|
|
mtl::SamplerName::Color, ///<采样器名称
|
|
|
|
|
texture, ///<纹理
|
|
|
|
|
sampler)) ///<采样器
|
2023-09-21 20:46:08 +08:00
|
|
|
|
return(false);
|
2023-09-21 17:15:36 +08:00
|
|
|
|
|
2023-09-26 21:16:00 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(material);
|
|
|
|
|
|
2023-09-21 17:15:36 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitVBO()
|
|
|
|
|
{
|
|
|
|
|
RenderablePrimitiveCreater rpc(db,VERTEX_COUNT);
|
|
|
|
|
|
|
|
|
|
if(!rpc.SetVBO(VAN::Position, VF_V2F, position_data))return(false);
|
2023-09-21 20:46:08 +08:00
|
|
|
|
if(!rpc.SetVBO(VAN::TexCoord, VF_V2F, tex_coord_data))return(false);
|
2023-09-21 17:15:36 +08:00
|
|
|
|
|
|
|
|
|
render_obj=rpc.Create(material_instance,pipeline);
|
|
|
|
|
return(render_obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitVBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
BuildCommandBuffer(render_obj);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
|
|
|
|
VulkanApplicationFramework::Resize(w,h);
|
|
|
|
|
|
|
|
|
|
BuildCommandBuffer(render_obj);
|
|
|
|
|
}
|
|
|
|
|
};//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;
|
|
|
|
|
}
|