ULRE/example/Vulkan/main.cpp

176 lines
4.3 KiB
C++
Raw Normal View History

2019-05-06 12:00:03 +08:00
// 0.triangle
// 该范例主要演示直接绘制一个渐变色的三角形
#include"VulkanAppFramework.h"
#include<hgl/math/Math.h>
2019-05-17 19:22:13 +08:00
#include<hgl/filesystem/FileSystem.h>
2019-04-15 22:19:09 +08:00
using namespace hgl;
using namespace hgl::graph;
2019-04-09 00:22:26 +08:00
bool SaveToFile(const OSString &filename,VK_NAMESPACE::PipelineCreater *pc);
bool LoadFromFile(const OSString &filename,VK_NAMESPACE::PipelineCreater *pc);
constexpr uint32_t SCREEN_WIDTH=128;
constexpr uint32_t SCREEN_HEIGHT=128;
constexpr uint32_t VERTEX_COUNT=3;
constexpr float vertex_data[VERTEX_COUNT][2]=
{
{SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.25},
{SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.75},
{SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.75}
};
constexpr float color_data[VERTEX_COUNT][3]=
{ {1,0,0},
{0,1,0},
{0,0,1}
};
2019-04-20 17:18:02 +08:00
2019-04-28 18:18:56 +08:00
class TestApp:public VulkanApplicationFramework
2019-04-09 00:22:26 +08:00
{
private:
2019-05-04 16:31:28 +08:00
2019-07-17 04:49:49 +08:00
WorldMatrix wm;
2019-04-28 18:18:56 +08:00
vulkan::Material * material =nullptr;
2019-05-23 19:23:49 +08:00
vulkan::DescriptorSets * descriptor_sets =nullptr;
vulkan::Renderable * render_obj =nullptr;
2019-04-28 18:18:56 +08:00
vulkan::Buffer * ubo_mvp =nullptr;
2019-04-28 18:18:56 +08:00
vulkan::Pipeline * pipeline =nullptr;
2019-04-09 00:22:26 +08:00
2019-04-28 18:18:56 +08:00
vulkan::VertexBuffer * vertex_buffer =nullptr;
vulkan::VertexBuffer * color_buffer =nullptr;
2019-04-10 01:37:37 +08:00
2019-04-28 18:18:56 +08:00
public:
2019-04-09 00:22:26 +08:00
2019-04-28 18:18:56 +08:00
~TestApp()
{
2019-04-28 18:18:56 +08:00
SAFE_CLEAR(color_buffer);
SAFE_CLEAR(vertex_buffer);
SAFE_CLEAR(pipeline);
SAFE_CLEAR(ubo_mvp);
SAFE_CLEAR(render_obj);
2019-05-23 19:23:49 +08:00
SAFE_CLEAR(descriptor_sets);
2019-04-28 18:18:56 +08:00
SAFE_CLEAR(material);
}
2019-04-28 18:18:56 +08:00
private:
2019-04-12 16:39:22 +08:00
2019-04-28 18:18:56 +08:00
bool InitMaterial()
2019-04-12 16:39:22 +08:00
{
material=shader_manage->CreateMaterial(OS_TEXT("res/shader/FlatColor.vert.spv"),
OS_TEXT("res/shader/FlatColor.frag.spv"));
2019-04-28 18:18:56 +08:00
if(!material)
return(false);
2019-05-24 19:28:27 +08:00
render_obj=material->CreateRenderable(VERTEX_COUNT);
2019-05-23 19:23:49 +08:00
descriptor_sets=material->CreateDescriptorSets();
2019-04-28 18:18:56 +08:00
return(true);
2019-04-12 16:39:22 +08:00
}
2019-04-28 18:18:56 +08:00
bool InitUBO()
{
const VkExtent2D extent=sc_render_target->GetExtent();
2019-07-17 04:49:49 +08:00
wm.vp_size.x=extent.width;
wm.vp_size.y=extent.height;
wm.ortho=ortho(extent.width,extent.height);
2019-04-28 18:18:56 +08:00
2019-07-17 04:49:49 +08:00
ubo_mvp=device->CreateUBO(sizeof(WorldMatrix),&wm);
2019-04-28 18:18:56 +08:00
if(!ubo_mvp)
return(false);
2019-04-28 18:45:26 +08:00
2019-07-06 16:46:19 +08:00
if(!descriptor_sets->BindUBO(material->GetUBO("world"),ubo_mvp))
return(false);
2019-05-23 19:23:49 +08:00
descriptor_sets->Update();
return(true);
}
2019-07-17 04:49:49 +08:00
2019-04-28 18:18:56 +08:00
void InitVBO()
{
2019-04-28 18:18:56 +08:00
vertex_buffer =device->CreateVBO(FMT_RG32F, VERTEX_COUNT,vertex_data);
color_buffer =device->CreateVBO(FMT_RGB32F, VERTEX_COUNT,color_data);
2019-04-18 21:02:42 +08:00
2019-04-28 18:18:56 +08:00
render_obj->Set("Vertex", vertex_buffer);
render_obj->Set("Color", color_buffer);
}
2019-04-18 21:02:42 +08:00
2019-04-28 18:18:56 +08:00
bool InitPipeline()
{
2019-05-15 21:28:16 +08:00
constexpr os_char PIPELINE_FILENAME[]=OS_TEXT("2DSolid.pipeline");
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target);
pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_TRIANGLES);
2019-05-15 21:28:16 +08:00
SaveToFile(PIPELINE_FILENAME,pipeline_creater);
}
{
2019-05-17 19:22:13 +08:00
void *data;
uint size=filesystem::LoadFileToMemory(PIPELINE_FILENAME,(void **)&data);
AutoDelete<vulkan::PipelineCreater> pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target,(uchar *)data,size);
pipeline=pipeline_creater->Create();
}
2019-05-15 21:28:16 +08:00
2019-04-28 18:18:56 +08:00
return pipeline;
}
2019-04-28 18:18:56 +08:00
public:
2019-04-20 16:12:22 +08:00
bool Init()
2019-04-28 18:18:56 +08:00
{
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
2019-04-28 18:18:56 +08:00
if(!InitMaterial())
return(false);
2019-04-20 02:28:57 +08:00
2019-04-28 18:18:56 +08:00
if(!InitUBO())
return(false);
2019-04-20 02:28:57 +08:00
2019-04-28 18:18:56 +08:00
InitVBO();
2019-04-20 16:12:22 +08:00
2019-04-28 18:18:56 +08:00
if(!InitPipeline())
return(false);
2019-04-20 22:54:35 +08:00
BuildCommandBuffer(pipeline,descriptor_sets,render_obj);
2019-04-28 18:18:56 +08:00
return(true);
}
2019-07-17 04:49:49 +08:00
void Resize(int w,int h)override
{
2019-07-17 04:49:49 +08:00
wm.vp_size.x=w;
wm.vp_size.y=h;
wm.ortho=ortho(w,h);
ubo_mvp->Write(&wm);
BuildCommandBuffer(pipeline,descriptor_sets,render_obj);
}
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
while(app.Run());
2019-04-09 00:22:26 +08:00
return 0;
}