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 16:06:44 +08:00
|
|
|
|
#include"VKDevice.h"
|
2019-04-15 22:19:09 +08:00
|
|
|
|
#include"VKShader.h"
|
2019-04-18 21:02:42 +08:00
|
|
|
|
#include"VKVertexInput.h"
|
|
|
|
|
#include"VKDescriptorSetLayout.h"
|
|
|
|
|
#include"VKPipelineLayout.h"
|
|
|
|
|
#include"VKPipeline.h"
|
2019-04-15 22:19:09 +08:00
|
|
|
|
|
|
|
|
|
#include<io.h>
|
|
|
|
|
#include<fcntl.h>
|
|
|
|
|
#include<stdlib.h>
|
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-15 22:19:09 +08:00
|
|
|
|
VkShaderModule vs=nullptr;
|
|
|
|
|
VkShaderModule fs=nullptr;
|
|
|
|
|
|
|
|
|
|
char *LoadFile(const char *filename,uint32_t &file_length)
|
|
|
|
|
{
|
2019-04-16 00:49:09 +08:00
|
|
|
|
int fp=_open(filename,O_RDONLY|O_BINARY);
|
2019-04-15 22:19:09 +08:00
|
|
|
|
|
|
|
|
|
if(fp==-1)return(nullptr);
|
|
|
|
|
|
|
|
|
|
file_length=_filelength(fp);
|
|
|
|
|
char *data=new char[file_length];
|
|
|
|
|
|
2019-04-16 00:49:09 +08:00
|
|
|
|
const int result=_read(fp,data,file_length);
|
|
|
|
|
|
|
|
|
|
if(result!=file_length)
|
2019-04-15 22:19:09 +08:00
|
|
|
|
{
|
|
|
|
|
delete[] data;
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_close(fp);
|
|
|
|
|
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-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-10 10:26:25 +08:00
|
|
|
|
win->Create(1280,720);
|
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 16:06:44 +08:00
|
|
|
|
vulkan::CommandBuffer *cmd_buf=device->CreateCommandBuffer();
|
2019-04-10 14:00:06 +08:00
|
|
|
|
|
2019-04-18 16:06:44 +08:00
|
|
|
|
vulkan::Buffer *ubo=device->CreateUBO(1024);
|
2019-04-11 22:40:13 +08:00
|
|
|
|
|
|
|
|
|
uint8_t *p=ubo->Map();
|
|
|
|
|
|
|
|
|
|
if(p)
|
|
|
|
|
{
|
|
|
|
|
memset(p,0,1024);
|
|
|
|
|
ubo->Unmap();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-18 21:02:42 +08:00
|
|
|
|
vulkan::Shader *shader=LoadShader(device->GetDevice());
|
|
|
|
|
|
|
|
|
|
if(!shader)
|
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
|
|
vulkan::VertexInput vi;
|
|
|
|
|
vulkan::PipelineCreater pc(device);
|
2019-04-18 16:06:44 +08:00
|
|
|
|
vulkan::RenderPass *rp=device->CreateRenderPass();
|
2019-04-12 16:39:22 +08:00
|
|
|
|
|
2019-04-18 21:02:42 +08:00
|
|
|
|
vulkan::DescriptorSetLayoutCreater dslc(device->GetDevice());
|
|
|
|
|
vulkan::DescriptorSetLayout *dsl=dslc.Create();
|
|
|
|
|
vulkan::PipelineLayout *pl=CreatePipelineLayout(device->GetDevice(),dsl);
|
|
|
|
|
|
|
|
|
|
pc.Set(shader);
|
|
|
|
|
pc.Set(&vi);
|
|
|
|
|
pc.Set(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
|
|
|
|
|
pc.Set(*pl);
|
|
|
|
|
pc.Set(*rp);
|
|
|
|
|
|
|
|
|
|
vulkan::Pipeline *pipeline=pc.Create();
|
|
|
|
|
|
|
|
|
|
if(pipeline)
|
|
|
|
|
{
|
|
|
|
|
delete pipeline;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 16:39:22 +08:00
|
|
|
|
delete rp;
|
|
|
|
|
|
2019-04-11 22:40:13 +08:00
|
|
|
|
delete ubo;
|
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;
|
|
|
|
|
}
|