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-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-13 13:33:27 +08:00
|
|
|
|
void SaveToJSON(const OSString &filename,const VkGraphicsPipelineCreateInfo *info);
|
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-27 01:36:58 +08:00
|
|
|
|
struct WorldConfig
|
2019-04-23 02:46:47 +08:00
|
|
|
|
{
|
2019-05-05 14:22:58 +08:00
|
|
|
|
Matrix4f mvp;
|
2019-04-27 01:36:58 +08:00
|
|
|
|
}world;
|
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
|
|
|
|
|
|
|
|
|
uint swap_chain_count=0;
|
2019-04-28 18:18:56 +08:00
|
|
|
|
|
|
|
|
|
vulkan::Material * material =nullptr;
|
2019-05-05 21:30:55 +08:00
|
|
|
|
vulkan::DescriptorSets * desciptor_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-05-04 16:31:28 +08:00
|
|
|
|
vulkan::CommandBuffer ** cmd_buf =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);
|
2019-05-04 16:31:28 +08:00
|
|
|
|
SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count);
|
2019-04-28 18:18:56 +08:00
|
|
|
|
SAFE_CLEAR(pipeline);
|
|
|
|
|
SAFE_CLEAR(ubo_mvp);
|
|
|
|
|
SAFE_CLEAR(render_obj);
|
2019-05-05 21:30:55 +08:00
|
|
|
|
SAFE_CLEAR(desciptor_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-04-30 16:42:59 +08:00
|
|
|
|
material=shader_manage->CreateMaterial(OS_TEXT("FlatColor.vert.spv"),
|
|
|
|
|
OS_TEXT("FlatColor.frag.spv"));
|
2019-04-28 18:18:56 +08:00
|
|
|
|
if(!material)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
render_obj=material->CreateRenderable();
|
2019-05-05 21:30:55 +08:00
|
|
|
|
desciptor_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-04-28 18:18:56 +08:00
|
|
|
|
const VkExtent2D extent=device->GetExtent();
|
2019-04-13 21:44:26 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
world.mvp=ortho(extent.width,extent.height);
|
|
|
|
|
|
|
|
|
|
ubo_mvp=device->CreateUBO(sizeof(WorldConfig),&world);
|
|
|
|
|
|
|
|
|
|
if(!ubo_mvp)
|
|
|
|
|
return(false);
|
2019-04-28 18:45:26 +08:00
|
|
|
|
|
2019-05-06 22:33:21 +08:00
|
|
|
|
return desciptor_sets->UpdateUBO(material->GetUBO("world"),*ubo_mvp); //material中这里可以改成不区分类型,返回的值包含类型和ID,这样descriptor_sets->Update也不再需要类型
|
2019-04-13 21:44:26 +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-05 00:23:14 +08:00
|
|
|
|
vulkan::PipelineCreater *
|
2019-05-05 11:54:49 +08:00
|
|
|
|
pipeline_creater=new vulkan::PipelineCreater(device,material,device->GetRenderPass());
|
2019-04-28 18:18:56 +08:00
|
|
|
|
pipeline_creater->SetDepthTest(false);
|
|
|
|
|
pipeline_creater->SetDepthWrite(false);
|
|
|
|
|
pipeline_creater->CloseCullFace();
|
|
|
|
|
pipeline_creater->Set(PRIM_TRIANGLES);
|
2019-04-23 02:46:47 +08:00
|
|
|
|
|
2019-05-13 13:33:27 +08:00
|
|
|
|
SaveToJSON(OS_TEXT("pipeline.json"),pipeline_creater->GetInfo());
|
2019-05-05 17:43:31 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
pipeline=pipeline_creater->Create();
|
2019-04-25 16:02:13 +08:00
|
|
|
|
|
2019-05-05 00:23:14 +08:00
|
|
|
|
delete pipeline_creater;
|
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
|
|
|
|
bool InitCommandBuffer()
|
|
|
|
|
{
|
2019-05-04 16:31:28 +08:00
|
|
|
|
cmd_buf=hgl_zero_new<vulkan::CommandBuffer *>(swap_chain_count);
|
2019-04-18 21:02:42 +08:00
|
|
|
|
|
2019-05-04 16:31:28 +08:00
|
|
|
|
for(uint i=0;i<swap_chain_count;i++)
|
|
|
|
|
{
|
|
|
|
|
cmd_buf[i]=device->CreateCommandBuffer();
|
|
|
|
|
|
|
|
|
|
if(!cmd_buf[i])
|
|
|
|
|
return(false);
|
2019-04-18 21:02:42 +08:00
|
|
|
|
|
2019-05-04 16:31:28 +08:00
|
|
|
|
cmd_buf[i]->Begin();
|
|
|
|
|
cmd_buf[i]->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(i));
|
|
|
|
|
cmd_buf[i]->Bind(pipeline);
|
2019-05-05 21:30:55 +08:00
|
|
|
|
cmd_buf[i]->Bind(desciptor_sets);
|
2019-05-04 16:31:28 +08:00
|
|
|
|
cmd_buf[i]->Bind(render_obj);
|
|
|
|
|
cmd_buf[i]->Draw(VERTEX_COUNT);
|
|
|
|
|
cmd_buf[i]->EndRenderPass();
|
|
|
|
|
cmd_buf[i]->End();
|
|
|
|
|
}
|
2019-04-20 01:02:20 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-05-04 16:31:28 +08:00
|
|
|
|
swap_chain_count=device->GetSwapChainImageCount();
|
|
|
|
|
|
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-04-28 18:18:56 +08:00
|
|
|
|
if(!InitCommandBuffer())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2019-04-18 21:02:42 +08:00
|
|
|
|
|
2019-05-04 16:52:44 +08:00
|
|
|
|
void Draw() override
|
2019-04-28 18:18:56 +08:00
|
|
|
|
{
|
2019-05-05 11:54:49 +08:00
|
|
|
|
const uint32_t frame_index=device->GetCurrentFrameIndices();
|
|
|
|
|
|
|
|
|
|
const vulkan::CommandBuffer *cb=cmd_buf[frame_index];
|
|
|
|
|
|
|
|
|
|
Submit(*cb);
|
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;
|
|
|
|
|
}
|