ULRE/example/Basic/rf_test.cpp

157 lines
4.3 KiB
C++
Raw Normal View History

2025-01-24 13:23:57 +08:00
// 该范例主要演示使用NDC坐标系直接绘制一个渐变色的三角形
#include"WorkObject.h"
#include<hgl/math/HalfFloat.h>
#include<hgl/graph/VKVertexInputConfig.h>
#include<hgl/graph/PrimitiveCreater.h>
#include<hgl/graph/mtl/Material2DCreateConfig.h>
#include<hgl/graph/VKMaterialInstance.h>
2025-01-24 13:23:57 +08:00
using namespace hgl;
using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=1280;
constexpr uint32_t SCREEN_HEIGHT=720;
constexpr uint32_t VERTEX_COUNT=3;
constexpr float position_data_float[VERTEX_COUNT*2]=
{
0.0, -0.5,
-0.5, 0.5,
0.5, 0.5
};
constexpr VkFormat PositionFormat=VF_V2F;
#define position_data position_data_float
constexpr float color_data[VERTEX_COUNT*4]=
{ 1,0,0,1,
0,1,0,1,
0,0,1,1
};
constexpr VkFormat ColorFormat=VF_V4F;
class TestApp:public WorkObject
{
private:
Color4f clear_color =Color4f(0.2f,0.2f,0.2f,1.0f);
2025-01-24 13:23:57 +08:00
MaterialInstance * material_instance =nullptr;
Renderable * render_obj =nullptr;
Pipeline * pipeline =nullptr;
private:
bool InitAutoMaterial()
{
mtl::Material2DCreateConfig cfg(GetDeviceAttribute(),"VertexColor2d",Prim::Triangles);
2025-01-24 13:23:57 +08:00
cfg.coordinate_system=CoordinateSystem2D::NDC;
cfg.local_to_world=false;
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreateVertexColor2D(&cfg);
material_instance=db->CreateMaterialInstance(mci);
2025-01-24 13:23:57 +08:00
return material_instance;
}
bool InitPipeline()
{
RenderPass *sc_render_pass=GetRenderFramework()->GetSwapchainModule()->GetRenderPass();
2025-01-24 13:23:57 +08:00
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
pipeline=sc_render_pass->CreatePipeline(material_instance,InlinePipeline::Solid2D,Prim::Triangles); //等同上一行为Framework重载默认使用swapchain的render target
2025-01-24 13:23:57 +08:00
return pipeline;
2025-01-24 13:23:57 +08:00
}
bool InitVBO()
{
PrimitiveCreater rpc(GetDevice(),material_instance->GetVIL());
2025-01-24 13:23:57 +08:00
rpc.Init("Triangle",VERTEX_COUNT);
if(!rpc.WriteVAB(VAN::Position, PositionFormat, position_data))return(false);
if(!rpc.WriteVAB(VAN::Color, ColorFormat, color_data ))return(false);
render_obj=db->CreateRenderable(&rpc,material_instance,pipeline);
return(render_obj);
}
public:
TestApp(RenderFramework *rf):WorkObject(rf)
{
2025-01-24 13:23:57 +08:00
if(!InitAutoMaterial())
return;
2025-01-24 13:23:57 +08:00
if(!InitPipeline())
return;
2025-01-24 13:23:57 +08:00
if(!InitVBO())
return;
2025-01-24 13:23:57 +08:00
}
2025-01-25 15:25:29 +08:00
void Tick(double)override
{}
void Render(RenderCmdBuffer *cb,Renderable *ri)
{
if(!ri)return;
cb->BeginRenderPass();
cb->BindPipeline(ri->GetPipeline());
cb->BindDescriptorSets(ri->GetMaterial());
cb->BindDataBuffer(ri->GetDataBuffer());
cb->Draw(ri->GetDataBuffer(),ri->GetRenderData());
cb->EndRenderPass();
}
2025-01-25 15:25:29 +08:00
void Render(double)
{
//WorkObject是工作对象不是渲染对象所以不应该直接自动指定RenderTarget更不能直接指定RenderCmdBuffer
//目前这里只是为了测试,所以这样写
RenderFramework *rf=GetRenderFramework();
SwapchainModule *sm=rf->GetSwapchainModule();
sm->BeginFrame(); //这里会有AcquireNextImage操作
//这个使用完全不合理录制CMD和推送swapchain是两回事需要分开操作。
//比如场景有的物件分静态和动态
//可能静态物件就全部一次性录制好,而动态物件则是每帧录制
RenderCmdBuffer *cb=sm->RecordCmdBuffer(); //这里会获取当前帧的RenderCmdBuffer、开启cmd录制、绑定FBO
if(cb)
{
cb->SetClearColor(0,clear_color);
Render(cb,render_obj);
cb->End(); //结束cmd录制
}
sm->EndFrame(); //这里会Submit和PresentBackbuffer
}
2025-01-24 13:23:57 +08:00
};//class TestApp:public VulkanApplicationFramework
int main(int,char **)
{
RenderFramework rf(OS_TEXT("RenderFramework Test"));
if(!rf.Init(SCREEN_WIDTH,SCREEN_HEIGHT))
2025-01-24 13:23:57 +08:00
return(-1);
2025-01-25 15:25:29 +08:00
WorkManager wm(&rf);
2025-01-24 13:23:57 +08:00
wm.Start(new TestApp(&rf));
2025-01-24 13:23:57 +08:00
}