2019-05-08 00:25:40 +08:00
|
|
|
|
#pragma once
|
2019-04-30 16:42:59 +08:00
|
|
|
|
#include<hgl/platform/Window.h>
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKInstance.h>
|
|
|
|
|
#include<hgl/graph/VKPhysicalDevice.h>
|
2023-05-11 21:17:06 +08:00
|
|
|
|
#include<hgl/graph/VKDeviceCreater.h>
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKSemaphore.h>
|
|
|
|
|
#include<hgl/graph/VKBuffer.h>
|
|
|
|
|
#include<hgl/graph/VKShaderModule.h>
|
|
|
|
|
#include<hgl/graph/VKImageView.h>
|
2022-06-24 17:51:05 +08:00
|
|
|
|
#include<hgl/graph/VKPrimitive.h>
|
2023-02-13 11:50:55 +08:00
|
|
|
|
#include<hgl/graph/VKDescriptorSet.h>
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKRenderPass.h>
|
|
|
|
|
#include<hgl/graph/VKPipeline.h>
|
|
|
|
|
#include<hgl/graph/VKCommandBuffer.h>
|
|
|
|
|
#include<hgl/graph/VKFormat.h>
|
|
|
|
|
#include<hgl/graph/VKFramebuffer.h>
|
|
|
|
|
#include<hgl/graph/VKMaterial.h>
|
2021-06-16 11:43:19 +08:00
|
|
|
|
#include<hgl/graph/VKMaterialParameters.h>
|
2021-06-16 20:29:25 +08:00
|
|
|
|
#include<hgl/graph/VKMaterialInstance.h>
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKRenderTarget.h>
|
2020-10-21 12:39:22 +08:00
|
|
|
|
#include<hgl/graph/VKRenderResource.h>
|
2023-10-12 05:55:39 +08:00
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
#include<hgl/graph/VKDeviceAttribute.h>
|
|
|
|
|
#endif//_DEBUG
|
2023-05-07 01:07:26 +08:00
|
|
|
|
#include<hgl/graph/RenderList.h>
|
2023-05-16 15:21:32 +08:00
|
|
|
|
#include<hgl/graph/mtl/UBOCommon.h>
|
2023-02-19 19:28:47 +08:00
|
|
|
|
#include<hgl/color/Color.h>
|
2022-01-24 19:36:54 +08:00
|
|
|
|
#include<hgl/Time.h>
|
2023-07-28 20:17:46 +08:00
|
|
|
|
#include<hgl/log/LogInfo.h>
|
2022-01-24 19:36:54 +08:00
|
|
|
|
|
2022-01-24 22:31:46 +08:00
|
|
|
|
//#include<hgl/graph/LookAtCameraControl.h>
|
|
|
|
|
#include<hgl/graph/FirstPersonCameraControl.h>
|
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
#include<hgl/io/event/KeyboardEvent.h>
|
|
|
|
|
#include<hgl/io/event/MouseEvent.h>
|
2019-04-30 16:42:59 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
2022-01-24 19:36:54 +08:00
|
|
|
|
using namespace hgl::io;
|
2019-04-30 16:42:59 +08:00
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
2023-03-21 18:05:48 +08:00
|
|
|
|
namespace hgl{namespace graph
|
2023-03-21 14:17:33 +08:00
|
|
|
|
{
|
2023-03-21 18:05:48 +08:00
|
|
|
|
bool InitShaderCompiler();
|
|
|
|
|
void CloseShaderCompiler();
|
|
|
|
|
}}//namespace hgl::graph
|
2023-03-21 14:17:33 +08:00
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
class VulkanApplicationFramework:WindowEvent
|
2019-04-30 16:42:59 +08:00
|
|
|
|
{
|
2022-01-24 19:36:54 +08:00
|
|
|
|
protected:
|
2019-04-30 16:42:59 +08:00
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Window * win =nullptr;
|
|
|
|
|
VulkanInstance * inst =nullptr;
|
2019-04-30 16:42:59 +08:00
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
2020-10-21 12:52:32 +08:00
|
|
|
|
GPUDevice * device =nullptr;
|
2021-09-22 17:15:17 +08:00
|
|
|
|
RenderPass * device_render_pass =nullptr;
|
2023-08-17 11:08:21 +08:00
|
|
|
|
RTSwapchain * sc_render_target =nullptr;
|
2020-01-23 21:00:35 +08:00
|
|
|
|
|
2019-07-17 04:49:16 +08:00
|
|
|
|
protected:
|
2019-06-11 23:14:13 +08:00
|
|
|
|
|
2020-10-21 12:52:32 +08:00
|
|
|
|
int32_t swap_chain_count =0;
|
2019-06-11 23:14:13 +08:00
|
|
|
|
|
2020-11-09 15:37:00 +08:00
|
|
|
|
RenderCmdBuffer ** cmd_buf =nullptr;
|
2019-06-14 17:13:30 +08:00
|
|
|
|
|
2020-10-21 12:52:32 +08:00
|
|
|
|
Color4f clear_color;
|
2020-10-18 19:12:00 +08:00
|
|
|
|
|
2019-06-11 23:14:13 +08:00
|
|
|
|
protected:
|
|
|
|
|
|
2020-10-21 12:52:32 +08:00
|
|
|
|
RenderResource * db =nullptr;
|
2022-03-09 20:55:09 +08:00
|
|
|
|
|
|
|
|
|
protected:
|
2023-03-02 20:19:25 +08:00
|
|
|
|
|
|
|
|
|
ViewportInfo vp_info;
|
2023-03-22 02:40:26 +08:00
|
|
|
|
DeviceBuffer * ubo_vp_info =nullptr;
|
2019-06-11 23:14:13 +08:00
|
|
|
|
|
2019-04-30 16:42:59 +08:00
|
|
|
|
public:
|
|
|
|
|
|
2024-07-30 00:47:28 +08:00
|
|
|
|
virtual void BindRenderResource(RenderResource *rr)
|
|
|
|
|
{
|
|
|
|
|
if(!rr)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
rr->static_descriptor.AddUBO(mtl::SBS_ViewportInfo.name,ubo_vp_info);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-30 16:42:59 +08:00
|
|
|
|
virtual ~VulkanApplicationFramework()
|
2019-06-11 23:14:13 +08:00
|
|
|
|
{
|
2023-03-21 18:05:48 +08:00
|
|
|
|
CloseShaderCompiler();
|
2023-03-21 14:17:33 +08:00
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
win->Unjoin(this);
|
|
|
|
|
|
2019-06-11 23:14:13 +08:00
|
|
|
|
SAFE_CLEAR(db);
|
2024-07-01 23:29:59 +08:00
|
|
|
|
SAFE_CLEAR_OBJECT_ARRAY_OBJECT(cmd_buf,swap_chain_count);
|
2019-06-11 23:14:13 +08:00
|
|
|
|
|
2019-08-23 11:18:09 +08:00
|
|
|
|
SAFE_CLEAR(device);
|
2020-01-23 21:00:35 +08:00
|
|
|
|
SAFE_CLEAR(win);
|
2019-04-30 16:42:59 +08:00
|
|
|
|
SAFE_CLEAR(inst);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-10 04:24:10 +08:00
|
|
|
|
virtual bool Init(uint w,uint h)
|
2019-04-30 16:42:59 +08:00
|
|
|
|
{
|
2023-06-01 14:46:21 +08:00
|
|
|
|
logger::InitLogger(OS_TEXT("VulkanTest"));
|
|
|
|
|
|
2023-03-21 18:05:48 +08:00
|
|
|
|
if(!InitShaderCompiler())
|
2023-03-21 14:17:33 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-09-28 18:57:15 +08:00
|
|
|
|
SetClearColor(COLOR::MozillaCharcoal);
|
2020-10-18 19:12:00 +08:00
|
|
|
|
|
2019-05-06 11:41:05 +08:00
|
|
|
|
#ifdef _DEBUG
|
2020-10-21 12:47:06 +08:00
|
|
|
|
if(!CheckStrideBytesByFormat())
|
2019-05-06 11:41:05 +08:00
|
|
|
|
return(false);
|
|
|
|
|
#endif//
|
|
|
|
|
|
2019-04-30 19:01:44 +08:00
|
|
|
|
InitNativeWindowSystem();
|
|
|
|
|
|
2020-10-28 17:28:07 +08:00
|
|
|
|
InitVulkanInstanceProperties();
|
2019-11-14 17:57:46 +08:00
|
|
|
|
|
2019-04-30 16:42:59 +08:00
|
|
|
|
win=CreateRenderWindow(OS_TEXT("VulkanTest"));
|
|
|
|
|
if(!win)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!win->Create(w,h))
|
|
|
|
|
return(false);
|
|
|
|
|
|
2019-11-14 17:57:46 +08:00
|
|
|
|
{
|
2020-10-21 12:52:32 +08:00
|
|
|
|
CreateInstanceLayerInfo cili;
|
2019-04-30 16:42:59 +08:00
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
hgl_zero(cili);
|
2019-11-14 17:57:46 +08:00
|
|
|
|
|
|
|
|
|
cili.lunarg.standard_validation = true;
|
|
|
|
|
cili.khronos.validation = true;
|
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
inst=CreateInstance("VulkanTest",nullptr,&cili);
|
2019-11-14 17:57:46 +08:00
|
|
|
|
|
|
|
|
|
if(!inst)
|
|
|
|
|
return(false);
|
|
|
|
|
}
|
2019-04-30 16:42:59 +08:00
|
|
|
|
|
2023-11-01 11:00:55 +08:00
|
|
|
|
{
|
|
|
|
|
VulkanHardwareRequirement vh_req;
|
2019-04-30 16:42:59 +08:00
|
|
|
|
|
2024-04-18 01:09:29 +08:00
|
|
|
|
vh_req.wideLines=VulkanHardwareRequirement::SupportLevel::Want;
|
2023-11-01 11:00:55 +08:00
|
|
|
|
|
|
|
|
|
device=CreateRenderDevice(inst,win,&vh_req);
|
|
|
|
|
|
|
|
|
|
if(!device)
|
|
|
|
|
return(false);
|
|
|
|
|
}
|
2019-04-30 16:42:59 +08:00
|
|
|
|
|
2021-09-22 17:15:17 +08:00
|
|
|
|
device_render_pass=device->GetRenderPass();
|
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
db=new RenderResource(device);
|
2019-06-13 20:36:12 +08:00
|
|
|
|
|
|
|
|
|
InitCommandBuffer();
|
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
win->Join(this);
|
2019-06-14 19:56:21 +08:00
|
|
|
|
|
2023-03-22 02:40:26 +08:00
|
|
|
|
{
|
|
|
|
|
vp_info.Set(w,h);
|
|
|
|
|
|
2023-10-13 10:42:21 +08:00
|
|
|
|
ubo_vp_info=db->CreateUBO("Viewport",sizeof(ViewportInfo),&vp_info);
|
2023-03-22 02:40:26 +08:00
|
|
|
|
}
|
2022-03-09 20:55:09 +08:00
|
|
|
|
|
2024-07-30 00:47:28 +08:00
|
|
|
|
BindRenderResource(db);
|
|
|
|
|
|
2019-06-13 20:36:12 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
2019-06-11 23:14:13 +08:00
|
|
|
|
|
2023-11-10 04:24:10 +08:00
|
|
|
|
virtual void Resize(uint w,uint h)
|
2022-03-09 20:55:09 +08:00
|
|
|
|
{
|
2023-03-02 20:19:25 +08:00
|
|
|
|
vp_info.Set(w,h);
|
2023-03-22 02:40:26 +08:00
|
|
|
|
ubo_vp_info->Write(&vp_info);
|
2022-03-09 20:55:09 +08:00
|
|
|
|
}
|
2020-01-23 21:00:35 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
ViewportInfo *GetViewportInfo()
|
2023-10-05 00:48:38 +08:00
|
|
|
|
{
|
2024-08-08 01:43:20 +08:00
|
|
|
|
return &vp_info;
|
2023-10-05 00:48:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DeviceBuffer *GetViewportInfoBuffer()
|
|
|
|
|
{
|
|
|
|
|
return ubo_vp_info;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 17:38:21 +08:00
|
|
|
|
void SetClearColor(const Color4f &cc)
|
2020-10-26 22:16:38 +08:00
|
|
|
|
{
|
2022-03-11 17:38:21 +08:00
|
|
|
|
clear_color=cc;
|
2020-10-26 22:16:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 17:58:53 +08:00
|
|
|
|
void SetClearColor(const enum class COLOR &ce)
|
|
|
|
|
{
|
|
|
|
|
clear_color=GetColor4f(ce,1.0f);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
void OnResize(uint w,uint h) override
|
2019-07-17 04:49:16 +08:00
|
|
|
|
{
|
2019-08-23 11:18:09 +08:00
|
|
|
|
if(w>0&&h>0)
|
|
|
|
|
device->Resize(w,h);
|
|
|
|
|
|
2019-07-17 04:49:16 +08:00
|
|
|
|
InitCommandBuffer();
|
|
|
|
|
Resize(w,h);
|
|
|
|
|
}
|
2019-06-13 23:12:11 +08:00
|
|
|
|
|
2019-06-13 20:36:12 +08:00
|
|
|
|
void InitCommandBuffer()
|
|
|
|
|
{
|
|
|
|
|
if(cmd_buf)
|
2024-07-01 23:29:59 +08:00
|
|
|
|
SAFE_CLEAR_OBJECT_ARRAY_OBJECT(cmd_buf,swap_chain_count);
|
2020-01-23 21:00:35 +08:00
|
|
|
|
|
2019-07-17 04:49:16 +08:00
|
|
|
|
sc_render_target=device->GetSwapchainRT();
|
2019-06-11 23:14:13 +08:00
|
|
|
|
|
2019-07-16 19:59:53 +08:00
|
|
|
|
swap_chain_count=sc_render_target->GetImageCount();
|
2019-06-13 20:36:12 +08:00
|
|
|
|
{
|
2019-07-16 19:59:53 +08:00
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
2019-07-10 21:00:36 +08:00
|
|
|
|
|
2020-11-09 15:37:00 +08:00
|
|
|
|
cmd_buf=hgl_zero_new<RenderCmdBuffer *>(swap_chain_count);
|
2019-06-11 23:14:13 +08:00
|
|
|
|
|
2020-07-16 12:03:33 +08:00
|
|
|
|
for(int32_t i=0;i<swap_chain_count;i++)
|
2023-10-13 01:48:07 +08:00
|
|
|
|
cmd_buf[i]=device->CreateRenderCommandBuffer(device->GetPhysicalDevice()->GetDeviceName()+AnsiString(":RenderCmdBuffer_")+AnsiString::numberOf(i));
|
2019-06-13 20:36:12 +08:00
|
|
|
|
}
|
2019-04-30 16:42:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 21:06:38 +08:00
|
|
|
|
bool BuildCommandBuffer(RenderCmdBuffer *cb,RenderPass *rp,Framebuffer *fb,Renderable *ri)
|
2020-10-26 22:16:38 +08:00
|
|
|
|
{
|
|
|
|
|
if(!ri)return(false);
|
2020-10-17 22:13:08 +08:00
|
|
|
|
|
|
|
|
|
cb->Begin();
|
2020-10-26 22:16:38 +08:00
|
|
|
|
cb->BindFramebuffer(rp,fb);
|
2022-03-11 17:43:07 +08:00
|
|
|
|
cb->SetClearColor(0,clear_color);
|
2020-10-30 18:59:58 +08:00
|
|
|
|
cb->BeginRenderPass();
|
2020-10-17 22:13:08 +08:00
|
|
|
|
cb->BindPipeline(ri->GetPipeline());
|
2023-09-05 20:19:53 +08:00
|
|
|
|
cb->BindDescriptorSets(ri->GetMaterial());
|
2024-05-31 22:04:02 +08:00
|
|
|
|
cb->BindDataBuffer(ri->GetDataBuffer());
|
2024-05-29 00:55:12 +08:00
|
|
|
|
cb->Draw(ri->GetDataBuffer(),ri->GetRenderData());
|
2020-10-17 22:13:08 +08:00
|
|
|
|
cb->EndRenderPass();
|
|
|
|
|
cb->End();
|
2020-10-26 22:16:38 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
2020-10-17 22:13:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 21:06:38 +08:00
|
|
|
|
void BuildCommandBuffer(RenderCmdBuffer *cb,RenderTarget *rt,Renderable *ri)
|
2020-10-18 19:12:00 +08:00
|
|
|
|
{
|
2020-10-27 22:43:24 +08:00
|
|
|
|
if(!cb||!rt||!ri)
|
2020-10-26 22:16:38 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2020-10-27 22:43:24 +08:00
|
|
|
|
BuildCommandBuffer(cb,rt->GetRenderPass(),rt->GetFramebuffer(),ri);
|
2020-10-18 19:12:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 21:06:38 +08:00
|
|
|
|
bool BuildCommandBuffer(uint32_t index,Renderable *ri)
|
2020-10-24 19:11:54 +08:00
|
|
|
|
{
|
|
|
|
|
if(!ri)return(false);
|
2019-06-11 23:14:13 +08:00
|
|
|
|
|
2020-10-26 22:16:38 +08:00
|
|
|
|
return BuildCommandBuffer(cmd_buf[index],sc_render_target->GetRenderPass(),sc_render_target->GetFramebuffer(index),ri);
|
2019-06-18 00:48:05 +08:00
|
|
|
|
}
|
2020-01-23 21:00:35 +08:00
|
|
|
|
|
2022-06-24 21:06:38 +08:00
|
|
|
|
bool BuildCommandBuffer(Renderable *ri)
|
2019-06-18 00:48:05 +08:00
|
|
|
|
{
|
2020-10-24 19:11:54 +08:00
|
|
|
|
if(!ri)return(false);
|
|
|
|
|
|
2020-07-16 12:03:33 +08:00
|
|
|
|
for(int32_t i=0;i<swap_chain_count;i++)
|
2020-09-21 19:05:25 +08:00
|
|
|
|
BuildCommandBuffer(i,ri);
|
2020-10-24 19:11:54 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
2019-06-11 23:14:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 21:06:38 +08:00
|
|
|
|
bool BuildCurrentCommandBuffer(Renderable *ri)
|
2019-06-11 23:14:13 +08:00
|
|
|
|
{
|
2020-10-24 19:11:54 +08:00
|
|
|
|
if(!ri)return(false);
|
|
|
|
|
|
|
|
|
|
return BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),ri);
|
2019-06-18 00:48:05 +08:00
|
|
|
|
}
|
2019-06-11 23:14:13 +08:00
|
|
|
|
|
2023-05-07 01:07:26 +08:00
|
|
|
|
void BuildCommandBuffer(uint32_t index,RenderList *rl)
|
2023-05-06 19:30:08 +08:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
cb->BeginRenderPass();
|
|
|
|
|
rl->Render(cb);
|
|
|
|
|
cb->EndRenderPass();
|
|
|
|
|
cb->End();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-07 01:07:26 +08:00
|
|
|
|
void BuildCommandBuffer(RenderList *rl)
|
2023-05-06 19:30:08 +08:00
|
|
|
|
{
|
|
|
|
|
for(int32_t i=0;i<swap_chain_count;i++)
|
|
|
|
|
BuildCommandBuffer(i,rl);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-07 01:07:26 +08:00
|
|
|
|
void BuildCurrentCommandBuffer(RenderList *rl)
|
2023-05-06 19:30:08 +08:00
|
|
|
|
{
|
|
|
|
|
BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),rl);
|
|
|
|
|
}
|
2019-06-11 23:14:13 +08:00
|
|
|
|
|
2020-10-27 18:11:39 +08:00
|
|
|
|
template<typename ...ARGS>
|
2023-10-12 05:55:39 +08:00
|
|
|
|
Pipeline *CreatePipeline(ARGS...args)
|
|
|
|
|
{
|
|
|
|
|
Pipeline *p=device_render_pass->CreatePipeline(args...);
|
|
|
|
|
|
|
|
|
|
if(!p)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2023-10-13 02:01:57 +08:00
|
|
|
|
DebugUtils *du=device->GetDebugUtils();
|
2023-10-12 05:55:39 +08:00
|
|
|
|
|
2023-10-13 02:01:57 +08:00
|
|
|
|
if(du)
|
|
|
|
|
du->SetPipeline(*p,"Pipeline:"+p->GetName());
|
2023-10-12 05:55:39 +08:00
|
|
|
|
#endif//_DEBUG
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
2020-10-27 18:11:39 +08:00
|
|
|
|
|
2019-06-11 23:14:13 +08:00
|
|
|
|
public:
|
|
|
|
|
|
2019-06-18 00:48:05 +08:00
|
|
|
|
int AcquireNextImage()
|
2019-06-11 23:14:13 +08:00
|
|
|
|
{
|
2020-10-18 13:55:12 +08:00
|
|
|
|
return sc_render_target->AcquireNextImage();
|
2019-06-18 00:48:05 +08:00
|
|
|
|
}
|
2019-06-11 23:14:13 +08:00
|
|
|
|
|
2019-07-16 21:32:29 +08:00
|
|
|
|
virtual void SubmitDraw(int index)
|
2019-06-18 00:48:05 +08:00
|
|
|
|
{
|
2019-06-15 22:02:20 +08:00
|
|
|
|
VkCommandBuffer cb=*cmd_buf[index];
|
2020-01-23 21:00:35 +08:00
|
|
|
|
|
2020-10-18 13:55:12 +08:00
|
|
|
|
sc_render_target->Submit(cb);
|
|
|
|
|
sc_render_target->PresentBackbuffer();
|
|
|
|
|
sc_render_target->WaitQueue();
|
|
|
|
|
sc_render_target->WaitFence();
|
2019-06-11 23:14:13 +08:00
|
|
|
|
}
|
2019-05-04 16:52:44 +08:00
|
|
|
|
|
2019-06-18 00:48:05 +08:00
|
|
|
|
virtual void Draw()
|
|
|
|
|
{
|
|
|
|
|
int index=AcquireNextImage();
|
|
|
|
|
|
2019-07-01 17:04:02 +08:00
|
|
|
|
if(index<0||index>=swap_chain_count)return;
|
2019-06-25 23:40:09 +08:00
|
|
|
|
|
2019-06-18 00:48:05 +08:00
|
|
|
|
SubmitDraw(index);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-30 19:01:44 +08:00
|
|
|
|
bool Run()
|
2019-04-30 16:42:59 +08:00
|
|
|
|
{
|
2019-05-04 16:52:44 +08:00
|
|
|
|
if(!win->Update())return(false);
|
|
|
|
|
|
|
|
|
|
if(win->IsVisible())
|
|
|
|
|
Draw();
|
|
|
|
|
|
2022-03-11 18:27:04 +08:00
|
|
|
|
device->WaitIdle();
|
|
|
|
|
|
2019-05-04 16:52:44 +08:00
|
|
|
|
return(true);
|
2019-04-30 16:42:59 +08:00
|
|
|
|
}
|
|
|
|
|
};//class VulkanApplicationFramework
|
2019-06-14 17:13:30 +08:00
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
class CameraKeyboardControl:public KeyboardStateEvent
|
|
|
|
|
{
|
2022-01-24 22:31:46 +08:00
|
|
|
|
FirstPersonCameraControl *camera;
|
2022-01-24 19:36:54 +08:00
|
|
|
|
float move_speed;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2022-01-24 22:31:46 +08:00
|
|
|
|
CameraKeyboardControl(FirstPersonCameraControl *wc)
|
2022-01-24 19:36:54 +08:00
|
|
|
|
{
|
|
|
|
|
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
|
2022-01-24 22:31:46 +08:00
|
|
|
|
//if(HasPressed(KeyboardButton::R ))camera->Up (move_speed);else
|
|
|
|
|
//if(HasPressed(KeyboardButton::F ))camera->Down (move_speed);else
|
2022-01-24 19:36:54 +08:00
|
|
|
|
|
2022-01-24 22:31:46 +08:00
|
|
|
|
//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
|
2022-01-24 19:36:54 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class CameraMouseControl:public MouseEvent
|
|
|
|
|
{
|
2022-01-24 22:31:46 +08:00
|
|
|
|
FirstPersonCameraControl *camera;
|
2022-01-24 19:36:54 +08:00
|
|
|
|
double cur_time;
|
2022-01-24 22:31:46 +08:00
|
|
|
|
double last_time;
|
2022-01-24 19:36:54 +08:00
|
|
|
|
|
2022-02-10 18:56:00 +08:00
|
|
|
|
Vector2f mouse_pos;
|
2022-01-24 19:36:54 +08:00
|
|
|
|
Vector2f mouse_last_pos;
|
|
|
|
|
|
2023-10-11 19:38:12 +08:00
|
|
|
|
protected:
|
2022-01-24 19:36:54 +08:00
|
|
|
|
|
|
|
|
|
bool OnPressed(int x,int y,MouseButton) override
|
|
|
|
|
{
|
|
|
|
|
mouse_last_pos.x=x;
|
|
|
|
|
mouse_last_pos.y=y;
|
|
|
|
|
|
2022-01-24 22:31:46 +08:00
|
|
|
|
last_time=cur_time;
|
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
2022-03-11 19:13:20 +08:00
|
|
|
|
|
|
|
|
|
bool OnWheel(int,int y) override
|
|
|
|
|
{
|
|
|
|
|
if(y==0)return(false);
|
|
|
|
|
|
|
|
|
|
camera->Forward(float(y)/10.0f);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2022-01-24 19:36:54 +08:00
|
|
|
|
|
|
|
|
|
bool OnMove(int x,int y) override
|
|
|
|
|
{
|
2022-02-10 18:56:00 +08:00
|
|
|
|
mouse_pos.x=x;
|
|
|
|
|
mouse_pos.y=y;
|
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
bool left=HasPressed(MouseButton::Left);
|
|
|
|
|
bool right=HasPressed(MouseButton::Right);
|
2022-03-11 19:13:20 +08:00
|
|
|
|
|
|
|
|
|
Vector2f pos(x,y);
|
|
|
|
|
Vector2f gap=pos-mouse_last_pos;
|
|
|
|
|
|
|
|
|
|
if(left)
|
2022-01-24 19:36:54 +08:00
|
|
|
|
{
|
2022-03-11 19:13:20 +08:00
|
|
|
|
gap/=-5.0f;
|
2022-01-24 19:36:54 +08:00
|
|
|
|
|
2022-03-09 18:40:31 +08:00
|
|
|
|
camera->Rotate(gap);
|
2022-03-11 19:13:20 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
if(right)
|
|
|
|
|
{
|
|
|
|
|
gap/=10.0f;
|
2022-01-24 22:31:46 +08:00
|
|
|
|
|
2022-03-11 19:13:20 +08:00
|
|
|
|
camera->Move(Vector3f(gap.x,0,gap.y));
|
2022-01-24 19:36:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 19:13:20 +08:00
|
|
|
|
last_time=cur_time;
|
|
|
|
|
mouse_last_pos=Vector2f(x,y);
|
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2022-01-24 22:31:46 +08:00
|
|
|
|
CameraMouseControl(FirstPersonCameraControl *wc)
|
2022-01-24 19:36:54 +08:00
|
|
|
|
{
|
|
|
|
|
camera=wc;
|
|
|
|
|
cur_time=0;
|
2024-03-16 00:41:20 +08:00
|
|
|
|
last_time=0;
|
2022-01-24 19:36:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 18:56:00 +08:00
|
|
|
|
const Vector2f &GetMouseCoord()const{return mouse_pos;}
|
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
cur_time=GetDoubleTime();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-15 18:07:20 +08:00
|
|
|
|
class CameraAppFramework:public VulkanApplicationFramework
|
2019-06-14 17:13:30 +08:00
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
|
2023-03-02 20:33:44 +08:00
|
|
|
|
Camera * camera =nullptr;
|
|
|
|
|
|
|
|
|
|
DeviceBuffer * ubo_camera_info =nullptr;
|
|
|
|
|
|
2022-01-24 22:31:46 +08:00
|
|
|
|
FirstPersonCameraControl *camera_control=nullptr;
|
2019-06-14 17:13:30 +08:00
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
CameraKeyboardControl * ckc=nullptr;
|
|
|
|
|
CameraMouseControl * cmc=nullptr;
|
2019-06-14 19:56:21 +08:00
|
|
|
|
|
2022-02-10 18:56:00 +08:00
|
|
|
|
const Vector2f &GetMouseCoord()const{return cmc->GetMouseCoord();}
|
|
|
|
|
|
2019-06-14 17:13:30 +08:00
|
|
|
|
public:
|
|
|
|
|
|
2024-07-30 00:47:28 +08:00
|
|
|
|
virtual void BindRenderResource(RenderResource *rr) override
|
|
|
|
|
{
|
|
|
|
|
if(!rr)return;
|
|
|
|
|
|
|
|
|
|
VulkanApplicationFramework::BindRenderResource(rr);
|
|
|
|
|
|
|
|
|
|
rr->global_descriptor.AddUBO(mtl::SBS_CameraInfo.name,ubo_camera_info);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 17:20:59 +08:00
|
|
|
|
virtual ~CameraAppFramework()
|
|
|
|
|
{
|
2022-01-24 19:36:54 +08:00
|
|
|
|
SAFE_CLEAR(ckc);
|
|
|
|
|
SAFE_CLEAR(cmc);
|
2023-03-02 20:33:44 +08:00
|
|
|
|
SAFE_CLEAR(camera);
|
2020-11-17 17:20:59 +08:00
|
|
|
|
}
|
2019-06-14 17:13:30 +08:00
|
|
|
|
|
2023-11-10 04:24:10 +08:00
|
|
|
|
virtual bool Init(uint w,uint h) override
|
2019-06-14 17:13:30 +08:00
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(w,h))
|
|
|
|
|
return(false);
|
2020-01-23 21:00:35 +08:00
|
|
|
|
|
2019-06-16 02:58:12 +08:00
|
|
|
|
InitCamera(w,h);
|
2024-07-30 00:47:28 +08:00
|
|
|
|
|
|
|
|
|
BindRenderResource(db);
|
2019-06-14 17:13:30 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 02:58:12 +08:00
|
|
|
|
virtual void InitCamera(int w,int h)
|
2019-06-14 17:13:30 +08:00
|
|
|
|
{
|
2023-09-28 17:41:45 +08:00
|
|
|
|
camera=new Camera;
|
|
|
|
|
|
|
|
|
|
camera->pos=Vector3f(10,10,10);
|
|
|
|
|
|
2023-03-02 20:19:25 +08:00
|
|
|
|
camera_control=new FirstPersonCameraControl(&vp_info,camera);
|
2022-01-24 22:31:46 +08:00
|
|
|
|
|
|
|
|
|
camera_control->Refresh(); //更新矩阵计算
|
2022-01-24 19:36:54 +08:00
|
|
|
|
|
2022-01-24 22:31:46 +08:00
|
|
|
|
ckc=new CameraKeyboardControl(camera_control);
|
|
|
|
|
cmc=new CameraMouseControl(camera_control);
|
2022-01-24 19:36:54 +08:00
|
|
|
|
|
|
|
|
|
win->Join(ckc);
|
|
|
|
|
win->Join(cmc);
|
2023-03-02 20:33:44 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
RefreshCameraInfo(camera_control->GetCameraInfo(),&vp_info,camera);
|
2023-03-02 20:33:44 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
ubo_camera_info=db->CreateUBO("CameraInfo",sizeof(CameraInfo),camera_control->GetCameraInfo());
|
2019-06-14 17:13:30 +08:00
|
|
|
|
}
|
2020-01-23 21:00:35 +08:00
|
|
|
|
|
2023-11-10 04:24:10 +08:00
|
|
|
|
void Resize(uint w,uint h)override
|
2019-06-16 02:58:12 +08:00
|
|
|
|
{
|
2023-03-02 20:19:25 +08:00
|
|
|
|
vp_info.Set(w,h);
|
2021-09-15 19:17:56 +08:00
|
|
|
|
|
2022-01-24 22:31:46 +08:00
|
|
|
|
camera_control->Refresh();
|
2021-09-15 19:17:56 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
ubo_camera_info->Write(camera_control->GetCameraInfo());
|
2023-03-02 20:33:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
CameraInfo *GetCameraInfo()
|
2023-03-02 20:33:44 +08:00
|
|
|
|
{
|
|
|
|
|
return camera_control->GetCameraInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DeviceBuffer *GetCameraInfoBuffer()
|
|
|
|
|
{
|
|
|
|
|
return ubo_camera_info;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 00:48:05 +08:00
|
|
|
|
virtual void BuildCommandBuffer(uint32_t index)=0;
|
|
|
|
|
|
2019-06-14 20:01:46 +08:00
|
|
|
|
virtual void Draw()override
|
2019-06-14 17:13:30 +08:00
|
|
|
|
{
|
2023-03-02 20:33:44 +08:00
|
|
|
|
camera_control->Refresh(); //更新相机矩阵
|
2023-03-02 20:19:25 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
ubo_camera_info->Write(camera_control->GetCameraInfo()); //写入缓冲区
|
2019-06-14 17:13:30 +08:00
|
|
|
|
|
2019-07-11 02:42:11 +08:00
|
|
|
|
const uint32_t index=AcquireNextImage();
|
|
|
|
|
|
2019-06-18 00:48:05 +08:00
|
|
|
|
BuildCommandBuffer(index);
|
|
|
|
|
|
|
|
|
|
SubmitDraw(index);
|
2019-06-14 17:13:30 +08:00
|
|
|
|
|
2022-01-24 19:36:54 +08:00
|
|
|
|
ckc->Update();
|
|
|
|
|
cmc->Update();
|
2019-06-15 13:01:39 +08:00
|
|
|
|
}
|
2023-10-11 18:49:29 +08:00
|
|
|
|
};//class CameraAppFramework
|
|
|
|
|
|
|
|
|
|
class SceneAppFramework:public CameraAppFramework
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
SceneNode render_root;
|
|
|
|
|
RenderList * render_list =nullptr;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
SceneAppFramework()=default;
|
|
|
|
|
|
|
|
|
|
virtual ~SceneAppFramework()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(render_list);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-10 04:24:10 +08:00
|
|
|
|
virtual bool Init(uint width,uint height) override
|
2023-10-11 18:49:29 +08:00
|
|
|
|
{
|
|
|
|
|
if(!CameraAppFramework::Init(width,height))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
render_list=new RenderList(device);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void BuildCommandBuffer(uint32 index) override
|
|
|
|
|
{
|
|
|
|
|
VulkanApplicationFramework::BuildCommandBuffer(index,render_list);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-10 04:24:10 +08:00
|
|
|
|
virtual void Resize(uint w,uint h) override
|
2023-10-11 18:49:29 +08:00
|
|
|
|
{
|
|
|
|
|
CameraAppFramework::Resize(w,h);
|
|
|
|
|
|
|
|
|
|
VulkanApplicationFramework::BuildCommandBuffer(render_list);
|
|
|
|
|
}
|
|
|
|
|
};//class SceneAppFramework:public CameraAppFramework
|
|
|
|
|
|
|
|
|
|
template<typename T> int RunApp(uint w,uint h)
|
|
|
|
|
{
|
|
|
|
|
T app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init(w,h))
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|