#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #include #include #include using namespace hgl; using namespace hgl::io; using namespace hgl::graph; class VulkanApplicationFramework:WindowEvent { protected: Window * win =nullptr; VulkanInstance * inst =nullptr; protected: GPUDevice * device =nullptr; RenderPass * device_render_pass =nullptr; SwapchainRenderTarget * sc_render_target =nullptr; protected: int32_t swap_chain_count =0; RenderCmdBuffer ** cmd_buf =nullptr; Color4f clear_color; protected: RenderResource * db =nullptr; public: virtual ~VulkanApplicationFramework() { win->Unjoin(this); SAFE_CLEAR(db); SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count); SAFE_CLEAR(device); SAFE_CLEAR(win); SAFE_CLEAR(inst); } virtual bool Init(int w,int h) { clear_color.Zero(); #ifdef _DEBUG if(!CheckStrideBytesByFormat()) return(false); #endif// InitNativeWindowSystem(); InitVulkanInstanceProperties(); win=CreateRenderWindow(OS_TEXT("VulkanTest")); if(!win) return(false); if(!win->Create(w,h)) return(false); { CreateInstanceLayerInfo cili; hgl_zero(cili); cili.lunarg.standard_validation = true; cili.khronos.validation = true; cili.RenderDoc.Capture = true; inst=CreateInstance("VulkanTest",nullptr,&cili); if(!inst) return(false); } device=CreateRenderDevice(inst,win); if(!device) return(false); device_render_pass=device->GetRenderPass(); db=new RenderResource(device); InitCommandBuffer(); win->Join(this); return(true); } virtual void Resize(int,int)=0; void SetClearColor(COLOR cc) { clear_color.Use(cc,1.0); } void OnResize(uint w,uint h) override { if(w>0&&h>0) device->Resize(w,h); InitCommandBuffer(); Resize(w,h); } void InitCommandBuffer() { if(cmd_buf) SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count); sc_render_target=device->GetSwapchainRT(); swap_chain_count=sc_render_target->GetImageCount(); { const VkExtent2D extent=sc_render_target->GetExtent(); cmd_buf=hgl_zero_new(swap_chain_count); for(int32_t i=0;iCreateRenderCommandBuffer(); } } bool BuildCommandBuffer(RenderCmdBuffer *cb,RenderPass *rp,Framebuffer *fb,RenderableInstance *ri) { if(!ri)return(false); const IndexBuffer *ib=ri->GetIndexBuffer(); cb->Begin(); cb->BindFramebuffer(rp,fb); cb->SetClearColor(0,clear_color.r,clear_color.g,clear_color.b); cb->BeginRenderPass(); cb->BindPipeline(ri->GetPipeline()); cb->BindDescriptorSets(ri); cb->BindVBO(ri); if (ib) cb->DrawIndexed(ib->GetCount()); else cb->Draw(ri->GetDrawCount()); cb->EndRenderPass(); cb->End(); return(true); } void BuildCommandBuffer(RenderCmdBuffer *cb,RenderTarget *rt,RenderableInstance *ri) { if(!cb||!rt||!ri) return; BuildCommandBuffer(cb,rt->GetRenderPass(),rt->GetFramebuffer(),ri); } bool BuildCommandBuffer(uint32_t index,RenderableInstance *ri) { if(!ri)return(false); return BuildCommandBuffer(cmd_buf[index],sc_render_target->GetRenderPass(),sc_render_target->GetFramebuffer(index),ri); } bool BuildCommandBuffer(RenderableInstance *ri) { if(!ri)return(false); for(int32_t i=0;iGetCurrentFrameIndices(),ri); } void BuildCommandBuffer(uint32_t index,RenderList *rl) { if(!rl)return; RenderCmdBuffer *cb=cmd_buf[index]; cb->Begin(); cb->BindFramebuffer(sc_render_target->GetRenderPass(),sc_render_target->GetFramebuffer(index)); cb->SetClearColor(0,clear_color.r,clear_color.g,clear_color.b); cb->BeginRenderPass(); rl->Render(cb); cb->EndRenderPass(); cb->End(); } void BuildCommandBuffer(RenderList *rl) { for(int32_t i=0;iGetCurrentFrameIndices(),rl); } template Pipeline *CreatePipeline(ARGS...args){return device_render_pass->CreatePipeline(args...);} public: int AcquireNextImage() { return sc_render_target->AcquireNextImage(); } virtual void SubmitDraw(int index) { VkCommandBuffer cb=*cmd_buf[index]; sc_render_target->Submit(cb); sc_render_target->PresentBackbuffer(); sc_render_target->WaitQueue(); sc_render_target->WaitFence(); } virtual void Draw() { int index=AcquireNextImage(); if(index<0||index>=swap_chain_count)return; SubmitDraw(index); } bool Run() { if(!win->Update())return(false); if(win->IsVisible()) Draw(); return(true); } };//class VulkanApplicationFramework class CameraKeyboardControl:public KeyboardStateEvent { FirstPersonCameraControl *camera; float move_speed; public: CameraKeyboardControl(FirstPersonCameraControl *wc) { camera=wc; move_speed=1.0f; } bool OnPressed(const KeyboardButton &kb)override { if(!KeyboardStateEvent::OnPressed(kb)) return(false); if(kb==KeyboardButton::Minus )move_speed*=0.9f;else if(kb==KeyboardButton::Equals )move_speed*=1.1f; return(true); } void Update() { if(HasPressed(KeyboardButton::W ))camera->Forward (move_speed);else if(HasPressed(KeyboardButton::S ))camera->Backward (move_speed);else if(HasPressed(KeyboardButton::A ))camera->Left (move_speed);else if(HasPressed(KeyboardButton::D ))camera->Right (move_speed);else //if(HasPressed(KeyboardButton::R ))camera->Up (move_speed);else //if(HasPressed(KeyboardButton::F ))camera->Down (move_speed);else //if(HasPressed(KeyboardButton::Left ))camera->HoriRotate( move_speed);else //if(HasPressed(KeyboardButton::Right ))camera->HoriRotate(-move_speed);else //if(HasPressed(KeyboardButton::Up ))camera->VertRotate( move_speed);else //if(HasPressed(KeyboardButton::Down ))camera->VertRotate(-move_speed);else return; } }; class CameraMouseControl:public MouseEvent { FirstPersonCameraControl *camera; double cur_time; double last_time; Vector2f mouse_last_pos; protected: bool OnPressed(int x,int y,MouseButton) override { mouse_last_pos.x=x; mouse_last_pos.y=y; last_time=cur_time; return(true); } bool OnMove(int x,int y) override { bool left=HasPressed(MouseButton::Left); bool right=HasPressed(MouseButton::Right); if(left||right) { Vector2f pos(x,y); Vector2f gap=pos-mouse_last_pos; camera->Rotate(gap/180.0f,(cur_time-last_time)*5); last_time=cur_time; mouse_last_pos=Vector2f(x,y); } return(true); } public: CameraMouseControl(FirstPersonCameraControl *wc) { camera=wc; cur_time=0; } void Update() { cur_time=GetDoubleTime(); } }; class CameraAppFramework:public VulkanApplicationFramework { private: GPUBuffer *ubo_camera_info=nullptr; protected: Camera *camera=nullptr; FirstPersonCameraControl *camera_control=nullptr; CameraKeyboardControl * ckc=nullptr; CameraMouseControl * cmc=nullptr; public: virtual ~CameraAppFramework() { SAFE_CLEAR(ckc); SAFE_CLEAR(cmc); SAFE_CLEAR(camera); } virtual bool Init(int w,int h) { if(!VulkanApplicationFramework::Init(w,h)) return(false); InitCamera(w,h); return(true); } virtual void InitCamera(int w,int h) { camera=new Camera; camera->width=w; camera->height=h; camera->vp_width=w; camera->vp_height=h; camera->pos=Vector3f(10,10,10); camera_control=new FirstPersonCameraControl(camera); camera_control->Refresh(); //更新矩阵计算 ubo_camera_info=db->CreateUBO(sizeof(CameraInfo),&camera->info); ckc=new CameraKeyboardControl(camera_control); cmc=new CameraMouseControl(camera_control); win->Join(ckc); win->Join(cmc); } void Resize(int w,int h)override { camera->width=w; camera->height=h; camera_control->Refresh(); ubo_camera_info->Write(&camera->info); } const CameraInfo &GetCameraInfo() { return camera->info; } GPUBuffer *GetCameraInfoBuffer() { return ubo_camera_info; } bool BindCameraUBO(MaterialInstance *mi) { MaterialParameters *mp_global=mi->GetMP(DescriptorSetsType::Global); if(!mp_global) return(false); if(!mp_global->BindUBO("g_camera",ubo_camera_info))return(false); mp_global->Update(); return(true); } virtual void BuildCommandBuffer(uint32_t index)=0; virtual void Draw()override { camera_control->Refresh(); //更新相机矩阵 ubo_camera_info->Write(&camera->info); //写入缓冲区 const uint32_t index=AcquireNextImage(); BuildCommandBuffer(index); SubmitDraw(index); ckc->Update(); cmc->Update(); } };//class CameraAppFramework