2020-01-20 15:02:40 +08:00
|
|
|
|
// 该范例主要用于测试gl_FragCoord值
|
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
bool SaveToFile(const OSString &filename,VK_NAMESPACE::PipelineCreater *pc);
|
|
|
|
|
bool LoadFromFile(const OSString &filename,VK_NAMESPACE::PipelineCreater *pc);
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=128;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=128;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=4;
|
|
|
|
|
|
|
|
|
|
constexpr float vertex_data[VERTEX_COUNT][2]=
|
|
|
|
|
{
|
|
|
|
|
{0,0},
|
|
|
|
|
{SCREEN_WIDTH,0},
|
|
|
|
|
{0,SCREEN_HEIGHT},
|
|
|
|
|
{SCREEN_WIDTH,SCREEN_HEIGHT}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
Camera cam;
|
|
|
|
|
|
|
|
|
|
vulkan::Material * material =nullptr;
|
2020-01-20 15:39:39 +08:00
|
|
|
|
vulkan::MaterialInstance * material_instance =nullptr;
|
2020-01-20 15:02:40 +08:00
|
|
|
|
vulkan::Renderable * render_obj =nullptr;
|
|
|
|
|
vulkan::Buffer * ubo_mvp =nullptr;
|
|
|
|
|
|
|
|
|
|
vulkan::Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
vulkan::VertexBuffer * vertex_buffer =nullptr;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(vertex_buffer);
|
|
|
|
|
SAFE_CLEAR(pipeline);
|
|
|
|
|
SAFE_CLEAR(ubo_mvp);
|
|
|
|
|
SAFE_CLEAR(render_obj);
|
2020-01-20 15:39:39 +08:00
|
|
|
|
SAFE_CLEAR(material_instance);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
SAFE_CLEAR(material);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
|
|
|
|
material=shader_manage->CreateMaterial(OS_TEXT("res/shader/OnlyPosition.vert.spv"),
|
|
|
|
|
OS_TEXT("res/shader/FragCoord.frag.spv"));
|
|
|
|
|
if(!material)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
render_obj=material->CreateRenderable(VERTEX_COUNT);
|
2020-01-20 15:39:39 +08:00
|
|
|
|
material_instance=material->CreateInstance();
|
2020-01-20 15:13:08 +08:00
|
|
|
|
return(true);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
|
|
|
|
|
|
|
|
|
cam.width=extent.width;
|
|
|
|
|
cam.height=extent.height;
|
|
|
|
|
|
|
|
|
|
cam.Refresh();
|
2020-01-20 15:13:08 +08:00
|
|
|
|
|
|
|
|
|
ubo_mvp=device->CreateUBO(sizeof(WorldMatrix),&cam.matrix);
|
|
|
|
|
|
|
|
|
|
if(!ubo_mvp)
|
2020-01-23 21:00:35 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2020-01-20 15:39:39 +08:00
|
|
|
|
material_instance->BindUBO("world",ubo_mvp);
|
|
|
|
|
material_instance->BindUBO("fragment_world",ubo_mvp);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
|
2020-01-20 15:39:39 +08:00
|
|
|
|
material_instance->Update();
|
2020-01-20 15:02:40 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
2020-01-23 21:00:35 +08:00
|
|
|
|
|
2020-01-20 15:02:40 +08:00
|
|
|
|
void InitVBO()
|
|
|
|
|
{
|
|
|
|
|
vertex_buffer =device->CreateVBO(FMT_RG32F, VERTEX_COUNT,vertex_data);
|
|
|
|
|
|
|
|
|
|
render_obj->Set("Vertex", vertex_buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitPipeline()
|
|
|
|
|
{
|
2020-01-23 21:00:35 +08:00
|
|
|
|
AutoDelete<vulkan::PipelineCreater>
|
2020-01-20 15:02:40 +08:00
|
|
|
|
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target);
|
|
|
|
|
pipeline_creater->CloseCullFace();
|
|
|
|
|
pipeline_creater->Set(PRIM_TRIANGLE_STRIP);
|
|
|
|
|
|
|
|
|
|
pipeline=pipeline_creater->Create();
|
|
|
|
|
|
|
|
|
|
return pipeline;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
InitVBO();
|
|
|
|
|
|
|
|
|
|
if(!InitPipeline())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
BuildCommandBuffer(pipeline,material_instance,render_obj);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
|
|
|
|
cam.width=w;
|
|
|
|
|
cam.height=h;
|
|
|
|
|
|
|
|
|
|
cam.Refresh();
|
|
|
|
|
|
|
|
|
|
ubo_mvp->Write(&cam.matrix);
|
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
BuildCommandBuffer(pipeline,material_instance,render_obj);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|