2019-04-10 21:54:39 +08:00
|
|
|
|
#include"Window.h"
|
2019-04-09 02:02:43 +08:00
|
|
|
|
#include"VKInstance.h"
|
2019-04-18 22:10:24 +08:00
|
|
|
|
#include"VKPhysicalDevice.h"
|
2019-04-18 16:06:44 +08:00
|
|
|
|
#include"VKDevice.h"
|
2019-04-18 22:10:24 +08:00
|
|
|
|
#include"VKBuffer.h"
|
2019-04-15 22:19:09 +08:00
|
|
|
|
#include"VKShader.h"
|
2019-04-22 00:33:48 +08:00
|
|
|
|
#include"VKImageView.h"
|
2019-04-18 21:02:42 +08:00
|
|
|
|
#include"VKVertexInput.h"
|
2019-04-19 12:11:16 +08:00
|
|
|
|
#include"VKDescriptorSets.h"
|
2019-04-18 22:10:24 +08:00
|
|
|
|
#include"VKRenderPass.h"
|
2019-04-18 21:02:42 +08:00
|
|
|
|
#include"VKPipelineLayout.h"
|
|
|
|
|
#include"VKPipeline.h"
|
2019-04-18 22:10:24 +08:00
|
|
|
|
#include"VKCommandBuffer.h"
|
2019-04-19 21:49:59 +08:00
|
|
|
|
#include"VKFormat.h"
|
2019-04-20 02:28:57 +08:00
|
|
|
|
#include"VKFramebuffer.h"
|
2019-04-23 02:46:47 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
2019-04-15 22:19:09 +08:00
|
|
|
|
|
2019-04-20 01:02:20 +08:00
|
|
|
|
#include<fstream>
|
2019-04-23 11:05:40 +08:00
|
|
|
|
#ifndef WIN32
|
2019-04-20 19:20:13 +08:00
|
|
|
|
#include<unistd.h>
|
|
|
|
|
#endif//
|
2019-04-10 01:13:31 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-04-23 02:46:47 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=1280;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=720;
|
|
|
|
|
|
2019-04-15 22:19:09 +08:00
|
|
|
|
VkShaderModule vs=nullptr;
|
|
|
|
|
VkShaderModule fs=nullptr;
|
|
|
|
|
|
2019-04-23 02:46:47 +08:00
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
Matrix4f mvp;
|
|
|
|
|
}ubo_vs;
|
|
|
|
|
|
2019-04-15 22:19:09 +08:00
|
|
|
|
char *LoadFile(const char *filename,uint32_t &file_length)
|
|
|
|
|
{
|
2019-04-20 01:02:20 +08:00
|
|
|
|
std::ifstream fs;
|
2019-04-15 22:19:09 +08:00
|
|
|
|
|
2019-04-20 01:02:20 +08:00
|
|
|
|
fs.open(filename,std::ios_base::binary);
|
2019-04-15 22:19:09 +08:00
|
|
|
|
|
2019-04-20 01:02:20 +08:00
|
|
|
|
if(!fs.is_open())
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
fs.seekg(0,std::ios_base::end);
|
|
|
|
|
file_length=fs.tellg();
|
2019-04-15 22:19:09 +08:00
|
|
|
|
char *data=new char[file_length];
|
|
|
|
|
|
2019-04-20 01:02:20 +08:00
|
|
|
|
fs.seekg(0,std::ios_base::beg);
|
|
|
|
|
fs.read(data,file_length);
|
2019-04-15 22:19:09 +08:00
|
|
|
|
|
2019-04-20 01:02:20 +08:00
|
|
|
|
fs.close();
|
2019-04-15 22:19:09 +08:00
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-18 16:18:05 +08:00
|
|
|
|
bool LoadShader(vulkan::Shader *sc,const char *filename,VkShaderStageFlagBits shader_flag)
|
2019-04-16 00:49:09 +08:00
|
|
|
|
{
|
|
|
|
|
uint32_t size;
|
|
|
|
|
char *data=LoadFile(filename,size);
|
|
|
|
|
|
|
|
|
|
if(!data)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!sc->Add(shader_flag,data,size))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
delete[] data;
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-18 21:02:42 +08:00
|
|
|
|
vulkan::Shader *LoadShader(VkDevice device)
|
2019-04-15 22:19:09 +08:00
|
|
|
|
{
|
2019-04-18 21:02:42 +08:00
|
|
|
|
vulkan::Shader *sc=new vulkan::Shader(device);
|
2019-04-16 00:49:09 +08:00
|
|
|
|
|
2019-04-18 21:02:42 +08:00
|
|
|
|
if(LoadShader(sc,"FlatColor.vert.spv",VK_SHADER_STAGE_VERTEX_BIT))
|
|
|
|
|
if(LoadShader(sc,"FlatColor.frag.spv",VK_SHADER_STAGE_FRAGMENT_BIT))
|
|
|
|
|
return sc;
|
2019-04-16 00:49:09 +08:00
|
|
|
|
|
2019-04-18 21:02:42 +08:00
|
|
|
|
delete sc;
|
|
|
|
|
return(nullptr);
|
2019-04-15 22:19:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 10:09:56 +08:00
|
|
|
|
vulkan::Buffer *CreateUBO(vulkan::Device *dev)
|
2019-04-23 02:46:47 +08:00
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
const VkExtent2D extent=dev->GetExtent();
|
|
|
|
|
|
2019-04-23 11:05:40 +08:00
|
|
|
|
ubo_vs.mvp=ortho(extent.width,extent.height);
|
2019-04-23 02:46:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vulkan::Buffer *ubo=dev->CreateUBO(sizeof(ubo_vs));
|
|
|
|
|
|
|
|
|
|
uint8_t *p=ubo->Map();
|
|
|
|
|
|
|
|
|
|
if(p)
|
|
|
|
|
{
|
|
|
|
|
memcpy(p,&ubo_vs,sizeof(ubo_vs));
|
|
|
|
|
ubo->Unmap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ubo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr float vertex_data[]=
|
|
|
|
|
{
|
2019-04-23 11:05:40 +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
|
2019-04-23 02:46:47 +08:00
|
|
|
|
};
|
2019-04-20 17:18:02 +08:00
|
|
|
|
constexpr float color_data[]={1,0,0, 0,1,0, 0,0,1 };
|
|
|
|
|
|
|
|
|
|
vulkan::VertexBuffer *vertex_buffer=nullptr;
|
|
|
|
|
vulkan::VertexBuffer *color_buffer=nullptr;
|
|
|
|
|
|
2019-04-26 21:43:22 +08:00
|
|
|
|
vulkan::VertexInput *CreateVertexBuffer(vulkan::Device *dev,const vulkan::VertexInputState *vis)
|
2019-04-25 16:02:13 +08:00
|
|
|
|
{
|
|
|
|
|
vertex_buffer =dev->CreateVBO(FMT_RG32F, 3,vertex_data);
|
|
|
|
|
color_buffer =dev->CreateVBO(FMT_RGB32F, 3,color_data);
|
2019-04-20 17:18:02 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
vulkan::VertexInput *vi=new vulkan::VertexInput(vis);
|
2019-04-20 17:18:02 +08:00
|
|
|
|
|
2019-04-26 03:03:21 +08:00
|
|
|
|
vi->Set("Vertex", vertex_buffer);
|
|
|
|
|
vi->Set("Color", color_buffer);
|
2019-04-20 17:18:02 +08:00
|
|
|
|
|
|
|
|
|
return vi;
|
|
|
|
|
}
|
2019-04-19 21:49:59 +08:00
|
|
|
|
|
2019-04-20 02:28:57 +08:00
|
|
|
|
void wait_seconds(int seconds) {
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
Sleep(seconds * 1000);
|
|
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
|
sleep(seconds);
|
|
|
|
|
#else
|
|
|
|
|
sleep(seconds);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
//class ExampleFramework
|
|
|
|
|
//{
|
|
|
|
|
// Window *win=nullptr;
|
|
|
|
|
// vulkan::Instance *inst=nullptr;
|
|
|
|
|
// vulkan::Device *device=nullptr;
|
|
|
|
|
// vulkan::Shader *shader=nullptr;
|
|
|
|
|
// vulkan::Buffer *ubo_mvp=nullptr;
|
|
|
|
|
// vulkan::VertexInput *vi=nullptr;
|
|
|
|
|
// vulkan::PipelineCreater
|
|
|
|
|
//};//
|
|
|
|
|
|
2019-04-09 00:22:26 +08:00
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
2019-04-16 14:49:55 +08:00
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
if(!vulkan::CheckStrideBytesByFormat())
|
|
|
|
|
return 0xff;
|
|
|
|
|
#endif//
|
|
|
|
|
|
2019-04-10 01:13:31 +08:00
|
|
|
|
Window *win=CreateRenderWindow(OS_TEXT("VulkanTest"));
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-04-23 02:46:47 +08:00
|
|
|
|
win->Create(SCREEN_WIDTH,SCREEN_HEIGHT);
|
2019-04-10 01:37:37 +08:00
|
|
|
|
|
2019-04-11 02:29:21 +08:00
|
|
|
|
vulkan::Instance *inst=vulkan::CreateInstance(U8_TEXT("VulkanTest"));
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-04-11 02:29:21 +08:00
|
|
|
|
if(!inst)
|
2019-04-10 01:13:31 +08:00
|
|
|
|
{
|
|
|
|
|
delete win;
|
2019-04-09 02:02:43 +08:00
|
|
|
|
return(-1);
|
2019-04-10 01:13:31 +08:00
|
|
|
|
}
|
2019-04-09 02:02:43 +08:00
|
|
|
|
|
2019-04-18 16:06:44 +08:00
|
|
|
|
vulkan::Device *device=inst->CreateRenderDevice(win);
|
2019-04-12 16:39:22 +08:00
|
|
|
|
|
2019-04-18 16:06:44 +08:00
|
|
|
|
if(!device)
|
2019-04-12 16:39:22 +08:00
|
|
|
|
{
|
|
|
|
|
delete inst;
|
|
|
|
|
delete win;
|
|
|
|
|
return(-2);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-13 21:44:26 +08:00
|
|
|
|
{
|
2019-04-18 16:06:44 +08:00
|
|
|
|
const vulkan::PhysicalDevice *render_device=device->GetPhysicalDevice();
|
2019-04-13 21:44:26 +08:00
|
|
|
|
|
|
|
|
|
std::cout<<"auto select physical device: "<<render_device->GetDeviceName()<<std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-18 21:02:42 +08:00
|
|
|
|
vulkan::Shader *shader=LoadShader(device->GetDevice());
|
|
|
|
|
|
|
|
|
|
if(!shader)
|
|
|
|
|
return -3;
|
|
|
|
|
|
2019-04-25 10:09:56 +08:00
|
|
|
|
vulkan::Buffer *ubo=CreateUBO(device);
|
2019-04-23 02:46:47 +08:00
|
|
|
|
|
2019-04-26 21:43:22 +08:00
|
|
|
|
vulkan::VertexInputStateInstance *vis_instance=shader->CreateVertexInputStateInstance();
|
2019-04-25 16:02:13 +08:00
|
|
|
|
|
2019-04-26 21:43:22 +08:00
|
|
|
|
vulkan::VertexInput *vi=CreateVertexBuffer(device,shader->GetVertexInputState());
|
2019-04-22 00:33:48 +08:00
|
|
|
|
|
2019-04-18 21:02:42 +08:00
|
|
|
|
vulkan::PipelineCreater pc(device);
|
2019-04-12 16:39:22 +08:00
|
|
|
|
|
2019-04-19 12:11:16 +08:00
|
|
|
|
vulkan::DescriptorSetLayoutCreater dslc(device);
|
2019-04-23 02:46:47 +08:00
|
|
|
|
|
2019-04-23 22:07:26 +08:00
|
|
|
|
const int mvp_binding=0; //shader->GetBinding("MVPMatrix"); 实现从材质shader文件中根据名称获取binding的功能
|
|
|
|
|
|
|
|
|
|
dslc.BindUBO(mvp_binding,VK_SHADER_STAGE_VERTEX_BIT);
|
2019-04-23 02:46:47 +08:00
|
|
|
|
|
2019-04-18 21:02:42 +08:00
|
|
|
|
vulkan::DescriptorSetLayout *dsl=dslc.Create();
|
2019-04-23 02:46:47 +08:00
|
|
|
|
|
2019-04-23 22:07:26 +08:00
|
|
|
|
dsl->UpdateUBO(mvp_binding,*ubo);
|
2019-04-23 02:46:47 +08:00
|
|
|
|
|
2019-04-23 00:37:45 +08:00
|
|
|
|
vulkan::PipelineLayout *pl=CreatePipelineLayout(*device,dsl);
|
2019-04-18 21:02:42 +08:00
|
|
|
|
|
2019-04-23 11:05:40 +08:00
|
|
|
|
pc.SetDepthTest(false);
|
|
|
|
|
pc.SetDepthWrite(false);
|
|
|
|
|
pc.CloseCullFace();
|
|
|
|
|
|
2019-04-18 21:02:42 +08:00
|
|
|
|
pc.Set(shader);
|
2019-04-26 21:43:22 +08:00
|
|
|
|
pc.Set(vis_instance);
|
2019-04-19 12:11:16 +08:00
|
|
|
|
pc.Set(PRIM_TRIANGLES);
|
2019-04-18 21:02:42 +08:00
|
|
|
|
pc.Set(*pl);
|
|
|
|
|
|
|
|
|
|
vulkan::Pipeline *pipeline=pc.Create();
|
|
|
|
|
|
2019-04-19 21:49:59 +08:00
|
|
|
|
if(!pipeline)
|
|
|
|
|
return(-4);
|
2019-04-20 01:02:20 +08:00
|
|
|
|
|
2019-04-20 16:12:22 +08:00
|
|
|
|
device->AcquireNextImage();
|
|
|
|
|
|
2019-04-23 00:02:59 +08:00
|
|
|
|
vulkan::CommandBuffer *cmd_buf=device->CreateCommandBuffer();
|
|
|
|
|
|
|
|
|
|
cmd_buf->Begin(device->GetRenderPass(),device->GetFramebuffer(0));
|
2019-04-20 02:28:57 +08:00
|
|
|
|
cmd_buf->Bind(pipeline);
|
|
|
|
|
cmd_buf->Bind(pl);
|
2019-04-20 17:18:02 +08:00
|
|
|
|
cmd_buf->Bind(vi);
|
2019-04-20 02:28:57 +08:00
|
|
|
|
cmd_buf->Draw(3);
|
|
|
|
|
cmd_buf->End();
|
|
|
|
|
|
2019-04-20 16:12:22 +08:00
|
|
|
|
device->QueueSubmit(cmd_buf);
|
|
|
|
|
device->Wait();
|
2019-04-20 02:28:57 +08:00
|
|
|
|
device->QueuePresent();
|
|
|
|
|
|
2019-04-20 19:27:10 +08:00
|
|
|
|
wait_seconds(3);
|
2019-04-20 16:12:22 +08:00
|
|
|
|
|
2019-04-20 22:54:35 +08:00
|
|
|
|
delete vertex_buffer;
|
|
|
|
|
delete color_buffer;
|
|
|
|
|
|
2019-04-19 21:49:59 +08:00
|
|
|
|
delete pipeline;
|
2019-04-18 21:02:42 +08:00
|
|
|
|
|
2019-04-20 16:12:22 +08:00
|
|
|
|
delete pl;
|
|
|
|
|
delete dsl;
|
|
|
|
|
|
2019-04-20 17:18:02 +08:00
|
|
|
|
delete vi;
|
2019-04-23 02:46:47 +08:00
|
|
|
|
delete ubo;
|
2019-04-09 02:02:43 +08:00
|
|
|
|
|
2019-04-20 16:12:22 +08:00
|
|
|
|
delete shader;
|
|
|
|
|
|
2019-04-09 02:02:43 +08:00
|
|
|
|
delete cmd_buf;
|
2019-04-18 16:06:44 +08:00
|
|
|
|
delete device;
|
2019-04-12 22:14:40 +08:00
|
|
|
|
delete inst;
|
2019-04-10 01:13:31 +08:00
|
|
|
|
delete win;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|