2023-09-27 11:36:39 +08:00
|
|
|
|
// 该范例主要演示使用NDC坐标系直接绘制一个渐变色的三角形
|
2019-05-06 12:00:03 +08:00
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
2019-04-23 02:46:47 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
2023-02-21 18:35:08 +08:00
|
|
|
|
#include<hgl/math/HalfFloat.h>
|
2019-05-17 19:22:13 +08:00
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
2021-05-24 20:44:20 +08:00
|
|
|
|
#include<hgl/graph/SceneInfo.h>
|
2023-03-19 19:41:21 +08:00
|
|
|
|
#include<hgl/graph/VKVertexInputConfig.h>
|
2023-02-23 13:49:18 +08:00
|
|
|
|
#include<hgl/graph/VKRenderablePrimitiveCreater.h>
|
2023-10-07 20:59:44 +08:00
|
|
|
|
#include<hgl/graph/mtl/Material2DCreateConfig.h>
|
2019-04-15 22:19:09 +08:00
|
|
|
|
|
2019-04-10 01:13:31 +08:00
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2020-06-09 19:40:08 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=1280;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=720;
|
2019-04-23 02:46:47 +08:00
|
|
|
|
|
2019-04-28 17:02:38 +08:00
|
|
|
|
constexpr uint32_t VERTEX_COUNT=3;
|
|
|
|
|
|
2023-02-21 18:35:08 +08:00
|
|
|
|
constexpr float position_data_float[VERTEX_COUNT*2]=
|
2019-04-23 02:46:47 +08:00
|
|
|
|
{
|
2023-02-21 18:35:08 +08:00
|
|
|
|
0.0, -0.5,
|
|
|
|
|
-0.5, 0.5,
|
|
|
|
|
0.5, 0.5
|
2019-04-28 17:02:38 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-02-21 18:35:08 +08:00
|
|
|
|
#define USE_HALF_FLOAT_POSITION
|
|
|
|
|
|
|
|
|
|
#ifdef USE_HALF_FLOAT_POSITION
|
|
|
|
|
constexpr VkFormat PositionFormat=VF_V2HF;
|
|
|
|
|
|
|
|
|
|
half_float position_data_hf[VERTEX_COUNT*2];
|
|
|
|
|
|
|
|
|
|
#define position_data position_data_hf
|
|
|
|
|
#else
|
|
|
|
|
constexpr VkFormat PositionFormat=VF_V2F;
|
|
|
|
|
|
|
|
|
|
#define position_data position_data_float
|
|
|
|
|
#endif//USE_HALF_FLOAT_POSITION
|
|
|
|
|
|
|
|
|
|
#define USE_UNORM8_COLOR
|
|
|
|
|
|
|
|
|
|
#ifdef USE_UNORM8_COLOR
|
|
|
|
|
constexpr uint8 color_data[VERTEX_COUNT*4]=
|
|
|
|
|
{ 255,0,0,255,
|
|
|
|
|
0,255,0,255,
|
|
|
|
|
0,0,255,255
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr VkFormat ColorFormat=VF_V4UN8;
|
|
|
|
|
#else
|
|
|
|
|
constexpr float color_data[VERTEX_COUNT*4]=
|
|
|
|
|
{ 1,0,0,1,
|
|
|
|
|
0,1,0,1,
|
|
|
|
|
0,0,1,1
|
2019-04-23 02:46:47 +08:00
|
|
|
|
};
|
2019-04-20 17:18:02 +08:00
|
|
|
|
|
2023-02-21 18:35:08 +08:00
|
|
|
|
constexpr VkFormat ColorFormat=VF_V4F;
|
|
|
|
|
#endif//USE_UNORM8_COLOR
|
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
2019-04-09 00:22:26 +08:00
|
|
|
|
{
|
2019-05-05 14:22:58 +08:00
|
|
|
|
private:
|
2019-05-04 16:31:28 +08:00
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
#if defined(USE_HALF_FLOAT_POSITION)||defined(USE_UNORM8_COLOR)
|
|
|
|
|
VILConfig vil_config;
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-06-16 20:29:25 +08:00
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2022-06-24 21:17:28 +08:00
|
|
|
|
Renderable * render_obj =nullptr;
|
2019-04-16 14:49:55 +08:00
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Pipeline * pipeline =nullptr;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
private:
|
2019-04-12 16:39:22 +08:00
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
void InitVIL()
|
2019-04-12 16:39:22 +08:00
|
|
|
|
{
|
2023-02-21 18:35:08 +08:00
|
|
|
|
#ifdef USE_HALF_FLOAT_POSITION
|
|
|
|
|
vil_config.Add(VAN::Position,PositionFormat);
|
|
|
|
|
#endif//USE_HALF_FLOAT_POSITION
|
|
|
|
|
|
|
|
|
|
#ifdef USE_UNORM8_COLOR
|
|
|
|
|
vil_config.Add(VAN::Color,ColorFormat);
|
|
|
|
|
#endif//USE_HALF_FLOAT_POSITION
|
2023-03-21 14:17:33 +08:00
|
|
|
|
}
|
2023-02-21 18:35:08 +08:00
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
bool InitAutoMaterial()
|
|
|
|
|
{
|
2023-09-26 21:49:37 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"VertexColor2d",Prim::Triangles);
|
2023-05-08 09:50:45 +08:00
|
|
|
|
|
2023-05-16 15:21:32 +08:00
|
|
|
|
cfg.coordinate_system=CoordinateSystem2D::NDC;
|
2023-05-08 09:50:45 +08:00
|
|
|
|
cfg.local_to_world=false;
|
|
|
|
|
|
2023-05-16 15:21:32 +08:00
|
|
|
|
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreateVertexColor2D(&cfg);
|
2023-03-21 14:17:33 +08:00
|
|
|
|
|
2023-03-21 21:01:16 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(mci,&vil_config);
|
2023-03-21 14:17:33 +08:00
|
|
|
|
|
|
|
|
|
return material_instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitPipeline()
|
|
|
|
|
{
|
2021-09-22 17:22:17 +08:00
|
|
|
|
// 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
|
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
return pipeline;
|
2019-04-12 16:39:22 +08:00
|
|
|
|
}
|
2023-02-23 14:43:57 +08:00
|
|
|
|
|
2020-06-10 17:11:24 +08:00
|
|
|
|
bool InitVBO()
|
2019-05-05 14:22:58 +08:00
|
|
|
|
{
|
2023-10-12 10:39:43 +08:00
|
|
|
|
RenderablePrimitiveCreater rpc(db,"Triangle",VERTEX_COUNT);
|
2020-09-20 21:17:16 +08:00
|
|
|
|
|
2023-02-21 18:35:08 +08:00
|
|
|
|
#ifdef USE_HALF_FLOAT_POSITION
|
|
|
|
|
Float32toFloat16(position_data_hf,position_data_float,VERTEX_COUNT*2);
|
|
|
|
|
#endif//USE_HALF_FLOAT_POSITION
|
|
|
|
|
|
2024-04-24 01:44:01 +08:00
|
|
|
|
if(!rpc.SetVAB(VAN::Position, PositionFormat, position_data))return(false);
|
|
|
|
|
if(!rpc.SetVAB(VAN::Color, ColorFormat, color_data ))return(false);
|
2020-09-21 19:31:34 +08:00
|
|
|
|
|
2023-02-23 13:49:18 +08:00
|
|
|
|
render_obj=rpc.Create(material_instance,pipeline);
|
2022-10-28 17:57:09 +08:00
|
|
|
|
return(render_obj);
|
2019-04-28 18:18:56 +08:00
|
|
|
|
}
|
2019-04-18 21:02:42 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
public:
|
2019-04-20 16:12:22 +08:00
|
|
|
|
|
2019-05-05 14:22:58 +08:00
|
|
|
|
bool Init()
|
2019-04-28 18:18:56 +08:00
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
2019-04-23 00:02:59 +08:00
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
InitVIL();
|
|
|
|
|
|
|
|
|
|
if(!InitAutoMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitPipeline())
|
2019-04-28 18:18:56 +08:00
|
|
|
|
return(false);
|
2019-04-20 02:28:57 +08:00
|
|
|
|
|
2020-06-10 17:11:24 +08:00
|
|
|
|
if(!InitVBO())
|
|
|
|
|
return(false);
|
2019-04-20 16:12:22 +08:00
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
if(!BuildCommandBuffer(render_obj))
|
2020-10-24 19:14:31 +08:00
|
|
|
|
return(false);
|
2019-04-28 18:18:56 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2019-06-13 23:12:11 +08:00
|
|
|
|
|
2024-03-06 23:24:57 +08:00
|
|
|
|
void Resize(uint w,uint h)override
|
2019-06-13 23:12:11 +08:00
|
|
|
|
{
|
2022-03-09 20:55:09 +08:00
|
|
|
|
VulkanApplicationFramework::Resize(w,h);
|
2019-07-17 04:49:49 +08:00
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
BuildCommandBuffer(render_obj);
|
2019-06-13 23:12:11 +08:00
|
|
|
|
}
|
2019-04-28 18:18:56 +08:00
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
2019-04-20 16:12:22 +08:00
|
|
|
|
|
2019-05-04 16:52:44 +08:00
|
|
|
|
while(app.Run());
|
2019-05-05 14:22:58 +08:00
|
|
|
|
|
2019-04-09 00:22:26 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|