2019-05-06 12:00:03 +08:00
|
|
|
|
// 0.triangle
|
|
|
|
|
// 该范例主要演示直接绘制一个渐变色的三角形
|
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
2019-04-23 02:46:47 +08:00
|
|
|
|
#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
|
|
|
|
|
2019-04-10 01:13:31 +08:00
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-05-16 20:50:09 +08:00
|
|
|
|
bool SaveToFile(const OSString &filename,VK_NAMESPACE::PipelineCreater *pc);
|
|
|
|
|
bool LoadFromFile(const OSString &filename,VK_NAMESPACE::PipelineCreater *pc);
|
2019-05-05 17:43:31 +08:00
|
|
|
|
|
2019-05-06 21:01:28 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=128;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=128;
|
2019-04-23 02:46:47 +08:00
|
|
|
|
|
2019-04-28 17:02:38 +08:00
|
|
|
|
constexpr uint32_t VERTEX_COUNT=3;
|
|
|
|
|
|
|
|
|
|
constexpr float vertex_data[VERTEX_COUNT][2]=
|
2019-04-23 02:46:47 +08:00
|
|
|
|
{
|
2019-04-28 17:02:38 +08:00
|
|
|
|
{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-23 02:46:47 +08:00
|
|
|
|
};
|
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
|
|
|
|
{
|
2019-05-05 14:22:58 +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;
|
2019-05-05 14:22:58 +08:00
|
|
|
|
vulkan::Renderable * render_obj =nullptr;
|
2019-04-28 18:18:56 +08:00
|
|
|
|
vulkan::Buffer * ubo_mvp =nullptr;
|
2019-04-16 14:49:55 +08:00
|
|
|
|
|
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-10 01:13:31 +08:00
|
|
|
|
{
|
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-10 01:13:31 +08:00
|
|
|
|
}
|
2019-04-09 02:02:43 +08:00
|
|
|
|
|
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
|
|
|
|
{
|
2019-07-10 21:21:17 +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()
|
2019-04-13 21:44:26 +08:00
|
|
|
|
{
|
2019-07-16 19:59:53 +08:00
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
2019-04-13 21:44:26 +08:00
|
|
|
|
|
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))
|
2019-05-21 12:02:57 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2019-05-23 19:23:49 +08:00
|
|
|
|
descriptor_sets->Update();
|
2019-05-21 12:02:57 +08:00
|
|
|
|
return(true);
|
2019-04-13 21:44:26 +08:00
|
|
|
|
}
|
2019-07-17 04:49:49 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
void InitVBO()
|
2019-05-05 14:22:58 +08:00
|
|
|
|
{
|
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");
|
|
|
|
|
|
2019-05-18 00:08:41 +08:00
|
|
|
|
{
|
2019-07-05 17:03:28 +08:00
|
|
|
|
AutoDelete<vulkan::PipelineCreater>
|
2019-07-16 20:22:29 +08:00
|
|
|
|
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target);
|
2019-05-18 00:08:41 +08:00
|
|
|
|
pipeline_creater->CloseCullFace();
|
|
|
|
|
pipeline_creater->Set(PRIM_TRIANGLES);
|
2019-05-15 21:28:16 +08:00
|
|
|
|
|
2019-05-18 00:08:41 +08:00
|
|
|
|
SaveToFile(PIPELINE_FILENAME,pipeline_creater);
|
|
|
|
|
}
|
2019-04-25 16:02:13 +08:00
|
|
|
|
|
2019-05-16 20:50:09 +08:00
|
|
|
|
{
|
2019-05-17 19:22:13 +08:00
|
|
|
|
void *data;
|
|
|
|
|
uint size=filesystem::LoadFileToMemory(PIPELINE_FILENAME,(void **)&data);
|
|
|
|
|
|
2019-07-16 20:22:29 +08:00
|
|
|
|
AutoDelete<vulkan::PipelineCreater> pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target,(uchar *)data,size);
|
2019-05-14 20:17:53 +08:00
|
|
|
|
|
2019-05-16 20:50:09 +08:00
|
|
|
|
pipeline=pipeline_creater->Create();
|
|
|
|
|
}
|
2019-05-15 21:28:16 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
return pipeline;
|
|
|
|
|
}
|
2019-04-23 02:46:47 +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
|
|
|
|
|
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
|
|
|
|
|
2019-06-11 23:14:13 +08:00
|
|
|
|
BuildCommandBuffer(pipeline,descriptor_sets,render_obj);
|
2019-04-28 18:18:56 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2019-06-13 23:12:11 +08:00
|
|
|
|
|
2019-07-17 04:49:49 +08:00
|
|
|
|
void Resize(int w,int h)override
|
2019-06-13 23:12:11 +08:00
|
|
|
|
{
|
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);
|
|
|
|
|
|
2019-06-14 17:13:30 +08:00
|
|
|
|
BuildCommandBuffer(pipeline,descriptor_sets,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;
|
|
|
|
|
}
|