2022-02-24 20:21:14 +08:00
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
|
|
|
|
#include<iostream>
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
2022-02-24 21:06:20 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=256;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=256;
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=1;
|
|
|
|
|
|
|
|
|
|
constexpr int16_t position_data[VERTEX_COUNT][4]=
|
|
|
|
|
{
|
|
|
|
|
SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.25, //left,top
|
|
|
|
|
SCREEN_WIDTH*0.75 , SCREEN_HEIGHT*0.75 //right,bottom
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct RoundedBoxConfig
|
|
|
|
|
{
|
|
|
|
|
Color4f Color;
|
|
|
|
|
Color4f InColor;
|
|
|
|
|
Color4f OutColor;
|
2022-06-30 18:34:03 +08:00
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
float bottom_right;
|
|
|
|
|
float top_right;
|
|
|
|
|
float bottom_left;
|
|
|
|
|
float top_left;
|
|
|
|
|
}radius;
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
|
|
|
|
float LineWidth;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static RoundedBoxConfig rb_config;
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
Camera cam;
|
|
|
|
|
|
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2022-06-24 21:17:28 +08:00
|
|
|
|
Renderable * render_obj =nullptr;
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
2023-02-13 11:48:53 +08:00
|
|
|
|
DeviceBuffer * ubo_camera_info =nullptr;
|
|
|
|
|
DeviceBuffer * ubo_rb_config =nullptr;
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
|
|
|
|
Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
|
|
|
|
//2D渲染Position坐标全部是使用整数,这里强制要求Position输入流使用RGBA16I格式
|
2022-10-11 19:16:06 +08:00
|
|
|
|
VILConfig vil_config;
|
2022-02-24 20:21:14 +08:00
|
|
|
|
VAConfig va_cfg;
|
|
|
|
|
|
|
|
|
|
va_cfg.format=VF_V4I16;
|
|
|
|
|
va_cfg.instance=false;
|
|
|
|
|
|
2022-10-11 19:16:06 +08:00
|
|
|
|
vil_config.Add("Position",va_cfg);
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
2022-10-11 19:16:06 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/RoundedBox"),&vil_config);
|
2022-02-24 20:21:14 +08:00
|
|
|
|
if(!material_instance)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
pipeline=CreatePipeline(material_instance,OS_TEXT("res/pipeline/alpha2d"),Prim::SolidRectangles); //等同上一行,为Framework重载,默认使用swapchain的render target
|
|
|
|
|
|
|
|
|
|
if(!pipeline)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
DeviceBuffer *CreateUBO(const AnsiString &name,const VkDeviceSize size,void *data)
|
2022-02-24 20:21:14 +08:00
|
|
|
|
{
|
2022-10-14 17:52:35 +08:00
|
|
|
|
DeviceBuffer *ubo=db->CreateUBO(size,data);
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
|
|
|
|
if(!ubo)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
return ubo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
|
|
|
|
|
|
|
|
|
cam.width=extent.width;
|
|
|
|
|
cam.height=extent.height;
|
|
|
|
|
|
|
|
|
|
cam.RefreshCameraInfo();
|
|
|
|
|
|
|
|
|
|
{
|
2023-02-22 21:53:51 +08:00
|
|
|
|
MaterialParameters *mp_global=material_instance->GetMP(DescriptorSetType::Global);
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
|
|
|
|
if(!mp_global)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
ubo_camera_info =db->CreateUBO(sizeof(CameraInfo), &cam.info);
|
|
|
|
|
|
|
|
|
|
if(!mp_global->BindUBO("g_camera",ubo_camera_info))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
mp_global->Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2023-02-22 21:53:51 +08:00
|
|
|
|
MaterialParameters *mp_value=material_instance->GetMP(DescriptorSetType::Value);
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
|
|
|
|
if(!mp_value)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
rb_config.Color.Set(1,1,1,1);
|
|
|
|
|
rb_config.OutColor.Set(0,0,0,0);
|
2022-03-28 16:10:24 +08:00
|
|
|
|
rb_config.InColor=GetColor4f(COLOR::MozillaOrange,1.0);
|
2022-06-30 18:34:03 +08:00
|
|
|
|
rb_config.radius.bottom_left=8;
|
|
|
|
|
rb_config.radius.bottom_right=8;
|
|
|
|
|
rb_config.radius.top_left=8;
|
|
|
|
|
rb_config.radius.top_right=8;
|
|
|
|
|
rb_config.LineWidth=1.5;
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
|
|
|
|
ubo_rb_config =db->CreateUBO(sizeof(RoundedBoxConfig), &rb_config);
|
|
|
|
|
|
|
|
|
|
if(!mp_value->BindUBO("rb_config",ubo_rb_config))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
mp_value->Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitVBO()
|
|
|
|
|
{
|
2022-06-24 18:00:22 +08:00
|
|
|
|
Primitive *primitive=db->CreatePrimitive(VERTEX_COUNT);
|
|
|
|
|
if(!primitive)return(false);
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
2022-06-24 18:00:22 +08:00
|
|
|
|
if(!primitive->Set(VAN::Position, db->CreateVBO(VF_V4I16,VERTEX_COUNT,position_data)))return(false);
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
render_obj=db->CreateRenderable(primitive,material_instance,pipeline);
|
2022-02-24 20:21:14 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitVBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
BuildCommandBuffer(render_obj);
|
2022-02-24 20:21:14 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
|
|
|
|
cam.width=w;
|
|
|
|
|
cam.height=h;
|
|
|
|
|
|
|
|
|
|
cam.RefreshCameraInfo();
|
|
|
|
|
|
|
|
|
|
ubo_camera_info->Write(&cam.info);
|
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
BuildCommandBuffer(render_obj);
|
2022-02-24 20:21:14 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|