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-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 19:58:01 +08:00
|
|
|
|
#include"VKFence.h"
|
2019-04-19 20:04:08 +08:00
|
|
|
|
#include"VKSemaphore.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-15 22:19:09 +08:00
|
|
|
|
|
2019-04-20 01:02:20 +08:00
|
|
|
|
#include<fstream>
|
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-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-20 17:18:02 +08:00
|
|
|
|
constexpr float vertex_data[]={0.0f,0.5f, -0.5f,-0.5f, 0.5f,-0.5f };
|
|
|
|
|
constexpr float color_data[]={1,0,0, 0,1,0, 0,0,1 };
|
|
|
|
|
|
|
|
|
|
vulkan::VertexBuffer *vertex_buffer=nullptr;
|
|
|
|
|
vulkan::VertexBuffer *color_buffer=nullptr;
|
|
|
|
|
|
|
|
|
|
vulkan::VertexInput *CreateVertexBuffer(vulkan::Device *dev)
|
|
|
|
|
{
|
|
|
|
|
vertex_buffer=dev->CreateVBO(FMT_RG32F,6*sizeof(float));
|
|
|
|
|
color_buffer=dev->CreateVBO(FMT_RGB32F,9*sizeof(float));
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
float *p=(float *)vertex_buffer->Map();
|
|
|
|
|
|
|
|
|
|
memcpy(p,vertex_data,6*sizeof(float));
|
|
|
|
|
|
|
|
|
|
vertex_buffer->Unmap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
float *p=(float *)color_buffer->Map();
|
|
|
|
|
|
|
|
|
|
memcpy(p,color_data,9*sizeof(float));
|
|
|
|
|
|
|
|
|
|
color_buffer->Unmap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vulkan::VertexInput *vi=new vulkan::VertexInput();
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t position_shader_location=0; //对应shader中的layout(locaiton=0,暂时这样写
|
|
|
|
|
constexpr uint32_t color_shader_location=1;
|
|
|
|
|
|
|
|
|
|
vi->Add(position_shader_location, vertex_buffer);
|
|
|
|
|
vi->Add(color_shader_location, color_buffer);
|
|
|
|
|
|
|
|
|
|
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-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-20 02:28:57 +08:00
|
|
|
|
//vulkan::Buffer *ubo=device->CreateUBO(1024);
|
2019-04-11 22:40:13 +08:00
|
|
|
|
|
2019-04-20 02:28:57 +08:00
|
|
|
|
//uint8_t *p=ubo->Map();
|
2019-04-11 22:40:13 +08:00
|
|
|
|
|
2019-04-20 02:28:57 +08:00
|
|
|
|
//if(p)
|
|
|
|
|
//{
|
|
|
|
|
// memset(p,0,1024);
|
|
|
|
|
// ubo->Unmap();
|
|
|
|
|
//}
|
2019-04-11 22:40:13 +08:00
|
|
|
|
|
2019-04-18 21:02:42 +08:00
|
|
|
|
vulkan::Shader *shader=LoadShader(device->GetDevice());
|
|
|
|
|
|
|
|
|
|
if(!shader)
|
|
|
|
|
return -3;
|
|
|
|
|
|
2019-04-19 20:04:08 +08:00
|
|
|
|
vulkan::Semaphore *sem=device->CreateSem();
|
2019-04-20 17:18:02 +08:00
|
|
|
|
|
|
|
|
|
vulkan::VertexInput *vi=CreateVertexBuffer(device);
|
2019-04-19 21:49:59 +08:00
|
|
|
|
|
2019-04-18 21:02:42 +08:00
|
|
|
|
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-19 12:11:16 +08:00
|
|
|
|
vulkan::DescriptorSetLayoutCreater dslc(device);
|
2019-04-18 21:02:42 +08:00
|
|
|
|
vulkan::DescriptorSetLayout *dsl=dslc.Create();
|
|
|
|
|
vulkan::PipelineLayout *pl=CreatePipelineLayout(device->GetDevice(),dsl);
|
|
|
|
|
|
|
|
|
|
pc.Set(shader);
|
2019-04-20 17:18:02 +08:00
|
|
|
|
pc.Set(vi);
|
2019-04-19 12:11:16 +08:00
|
|
|
|
pc.Set(PRIM_TRIANGLES);
|
2019-04-18 21:02:42 +08:00
|
|
|
|
pc.Set(*pl);
|
|
|
|
|
pc.Set(*rp);
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
const int image_count=device->GetSwapChainImageCount();
|
|
|
|
|
vulkan::Framebuffer **fb=new vulkan::Framebuffer *[image_count];
|
2019-04-20 02:28:57 +08:00
|
|
|
|
|
2019-04-20 16:12:22 +08:00
|
|
|
|
for(int i=0;i<image_count;i++)
|
|
|
|
|
fb[i]=vulkan::CreateFramebuffer(device,rp,device->GetColorImageView(i));
|
2019-04-20 02:28:57 +08:00
|
|
|
|
|
2019-04-20 16:12:22 +08:00
|
|
|
|
cmd_buf->Begin(rp,fb[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 16:12:22 +08:00
|
|
|
|
wait_seconds(1);
|
|
|
|
|
|
|
|
|
|
for(int i=0;i<image_count;i++)
|
|
|
|
|
delete fb[i];
|
|
|
|
|
delete[] fb;
|
2019-04-20 02:28:57 +08:00
|
|
|
|
|
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-19 20:04:08 +08:00
|
|
|
|
delete sem;
|
2019-04-12 16:39:22 +08:00
|
|
|
|
delete rp;
|
|
|
|
|
|
2019-04-20 17:18:02 +08:00
|
|
|
|
delete vi;
|
2019-04-20 02:28:57 +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;
|
|
|
|
|
}
|