added WorkObject.h
This commit is contained in:
parent
0a2862a051
commit
487d3c4978
@ -10,6 +10,7 @@
|
||||
set_property(TARGET ${name} PROPERTY FOLDER "ULRE/Example/Basic")
|
||||
endmacro()
|
||||
|
||||
CreateProject(00_RenderFrameworkTest rf_test.cpp)
|
||||
CreateProject(01_draw_triangle_in_NDC draw_triangle_in_NDC.cpp)
|
||||
CreateProject(02_draw_triangle_use_UBO draw_triangle_use_UBO.cpp)
|
||||
CreateProject(03_auto_instance auto_instance.cpp)
|
||||
|
165
example/Basic/rf_test.cpp
Normal file
165
example/Basic/rf_test.cpp
Normal file
@ -0,0 +1,165 @@
|
||||
// 该范例主要演示使用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>
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
#define USE_HALF_FLOAT_POSITION
|
||||
|
||||
#ifdef USE_HALF_FLOAT_POSITION
|
||||
constexpr VkFormat PositionFormat=VF_V2HF;
|
||||
|
||||
half_float position_data_hf[VERTEX_COUNT*2];
|
||||
|
||||
#define position_data position_data_hf
|
||||
#else
|
||||
constexpr VkFormat PositionFormat=VF_V2F;
|
||||
|
||||
#define position_data position_data_float
|
||||
#endif//USE_HALF_FLOAT_POSITION
|
||||
|
||||
#define USE_UNORM8_COLOR
|
||||
|
||||
#ifdef USE_UNORM8_COLOR
|
||||
constexpr uint8 color_data[VERTEX_COUNT*4]=
|
||||
{ 255,0,0,255,
|
||||
0,255,0,255,
|
||||
0,0,255,255
|
||||
};
|
||||
|
||||
constexpr VkFormat ColorFormat=VF_V4UN8;
|
||||
#else
|
||||
constexpr float color_data[VERTEX_COUNT*4]=
|
||||
{ 1,0,0,1,
|
||||
0,1,0,1,
|
||||
0,0,1,1
|
||||
};
|
||||
|
||||
constexpr VkFormat ColorFormat=VF_V4F;
|
||||
#endif//USE_UNORM8_COLOR
|
||||
|
||||
class TestApp:public WorkObject
|
||||
{
|
||||
private:
|
||||
|
||||
#if defined(USE_HALF_FLOAT_POSITION)||defined(USE_UNORM8_COLOR)
|
||||
VILConfig vil_config;
|
||||
#endif
|
||||
|
||||
MaterialInstance * material_instance =nullptr;
|
||||
Renderable * render_obj =nullptr;
|
||||
|
||||
Pipeline * pipeline =nullptr;
|
||||
|
||||
private:
|
||||
|
||||
void InitVIL()
|
||||
{
|
||||
#ifdef USE_HALF_FLOAT_POSITION
|
||||
vil_config.Add(VAN::Position,PositionFormat);
|
||||
#endif//USE_HALF_FLOAT_POSITION
|
||||
|
||||
#ifdef USE_UNORM8_COLOR
|
||||
vil_config.Add(VAN::Color,ColorFormat);
|
||||
#endif//USE_HALF_FLOAT_POSITION
|
||||
}
|
||||
|
||||
bool InitAutoMaterial()
|
||||
{
|
||||
mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"VertexColor2d",Prim::Triangles);
|
||||
|
||||
cfg.coordinate_system=CoordinateSystem2D::NDC;
|
||||
cfg.local_to_world=false;
|
||||
|
||||
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreateVertexColor2D(&cfg);
|
||||
|
||||
material_instance=db->CreateMaterialInstance(mci,&vil_config);
|
||||
|
||||
return material_instance;
|
||||
}
|
||||
|
||||
bool InitPipeline()
|
||||
{
|
||||
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
|
||||
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D,Prim::Triangles); //等同上一行,为Framework重载,默认使用swapchain的render target
|
||||
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
bool InitVBO()
|
||||
{
|
||||
PrimitiveCreater rpc(device,material_instance->GetVIL());
|
||||
|
||||
rpc.Init("Triangle",VERTEX_COUNT);
|
||||
|
||||
#ifdef USE_HALF_FLOAT_POSITION
|
||||
Float32toFloat16(position_data_hf,position_data_float,VERTEX_COUNT*2);
|
||||
#endif//USE_HALF_FLOAT_POSITION
|
||||
|
||||
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:
|
||||
|
||||
bool Init(uint w,uint h)
|
||||
{
|
||||
if(!VulkanApplicationFramework::Init(w,h))
|
||||
return(false);
|
||||
|
||||
InitVIL();
|
||||
|
||||
if(!InitAutoMaterial())
|
||||
return(false);
|
||||
|
||||
if(!InitPipeline())
|
||||
return(false);
|
||||
|
||||
if(!InitVBO())
|
||||
return(false);
|
||||
|
||||
if(!BuildCommandBuffer(render_obj))
|
||||
return(false);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
void Resize(uint w,uint h)override
|
||||
{
|
||||
VulkanApplicationFramework::Resize(w,h);
|
||||
|
||||
BuildCommandBuffer(render_obj);
|
||||
}
|
||||
};//class TestApp:public VulkanApplicationFramework
|
||||
|
||||
int main(int,char **)
|
||||
{
|
||||
RenderFramework rf(OS_TEXT("RenderFramework Test"));
|
||||
|
||||
if(rf.Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
||||
return(-1);
|
||||
|
||||
WorkManager wm;
|
||||
|
||||
wm.Start(new TestApp());
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/common)
|
||||
|
||||
SET(VULKAN_APP_FRAMEWORK ${CMAKE_CURRENT_SOURCE_DIR}/common/VulkanAppFramework.h)
|
||||
SET(VULKAN_APP_FRAMEWORK ${CMAKE_CURRENT_SOURCE_DIR}/common/VulkanAppFramework.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/WorkObject.h)
|
||||
|
||||
add_subdirectory(Basic)
|
||||
add_subdirectory(Texture)
|
||||
|
121
example/common/WorkObject.h
Normal file
121
example/common/WorkObject.h
Normal file
@ -0,0 +1,121 @@
|
||||
#pragma once
|
||||
#include<hgl/graph/RenderFramework.h>
|
||||
#include<hgl/Time.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
class WorkObject
|
||||
{
|
||||
RenderFramework *render_framework;
|
||||
|
||||
bool destroy_flag=false;
|
||||
|
||||
bool tickable=true;
|
||||
bool renderable=true;
|
||||
|
||||
public:
|
||||
|
||||
const bool IsDestroy()const{return destroy_flag;}
|
||||
const bool IsTickable()const{return tickable;}
|
||||
const bool IsRenderable()const{return renderable;}
|
||||
|
||||
void MarkDestory(){destroy_flag=true;}
|
||||
void SetTickable(bool t){tickable=t;}
|
||||
void SetRenderable(bool r){renderable=r;}
|
||||
|
||||
public:
|
||||
|
||||
virtual ~WorkObject()=default;
|
||||
|
||||
virtual void Start(RenderFramework *rf)
|
||||
{
|
||||
render_framework=rf;
|
||||
}
|
||||
|
||||
virtual void Tick(double delta_time)=0;
|
||||
virtual void Render(double delta_time)=0;
|
||||
};//class WorkObject
|
||||
|
||||
class WorkManager
|
||||
{
|
||||
graph::RenderTarget *render_target;
|
||||
|
||||
uint fps=60;
|
||||
double frame_time=1.0f/double(fps);
|
||||
|
||||
double last_update_time=0;
|
||||
double last_render_time=0;
|
||||
|
||||
WorkObject *cur_work_object=nullptr;
|
||||
|
||||
public:
|
||||
|
||||
WorkManager()=default;
|
||||
|
||||
bool Init(const OSString &app_name,uint w,uint h)
|
||||
{
|
||||
render_framework=new graph::RenderFramework(app_name);
|
||||
|
||||
if(!render_framework->Init(w,h))
|
||||
return(false);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
void SetFPS(uint f)
|
||||
{
|
||||
fps=f;
|
||||
frame_time=1.0f/double(fps);
|
||||
}
|
||||
|
||||
void Update(WorkObject *wo)
|
||||
{
|
||||
double cur_time=GetDoubleTime();
|
||||
double delta_time;
|
||||
|
||||
if(wo->IsTickable())
|
||||
{
|
||||
delta_time=cur_time-last_update_time;
|
||||
|
||||
if(delta_time>=frame_time)
|
||||
{
|
||||
last_update_time=cur_time;
|
||||
wo->Tick(delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
if(wo->IsRenderable())
|
||||
{
|
||||
delta_time=cur_time-last_render_time;
|
||||
|
||||
if(delta_time>=frame_time)
|
||||
{
|
||||
last_render_time=cur_time;
|
||||
wo->Render(delta_time);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Run()
|
||||
{
|
||||
if(!cur_work_object)
|
||||
return;
|
||||
|
||||
while(!cur_work_object->IsDestroy())
|
||||
{
|
||||
Update(cur_work_object);
|
||||
}
|
||||
}
|
||||
|
||||
void Start(WorkObject *wo)
|
||||
{
|
||||
if(!wo)return;
|
||||
|
||||
last_update_time=last_render_time=0;
|
||||
|
||||
cur_work_object=wo;
|
||||
|
||||
Run();
|
||||
}
|
||||
};//class WorkManager
|
||||
}//namespcae hgl
|
@ -52,7 +52,7 @@ public:
|
||||
|
||||
public:
|
||||
|
||||
GraphModuleManager * GetModuleManager(){return module_manager;}
|
||||
GraphModuleManager * GetModuleManager (){return module_manager;}
|
||||
|
||||
RenderPassManager * GetRenderPassManager (){return render_pass_manager;}
|
||||
TextureManager * GetTextureManager (){return texture_manager;}
|
||||
|
Loading…
x
Reference in New Issue
Block a user