增加使用索引画矩形的范例
This commit is contained in:
parent
1fbf0d34ed
commit
33420244fa
@ -4,4 +4,4 @@
|
||||
endmacro()
|
||||
|
||||
CreateProject(0.triangle main)
|
||||
CreateProject(1.cube cube)
|
||||
CreateProject(1.indices_rect indices_rect)
|
||||
|
196
example/Vulkan/indices_rect.cpp
Normal file
196
example/Vulkan/indices_rect.cpp
Normal file
@ -0,0 +1,196 @@
|
||||
// 1.indices_rect
|
||||
// 该示例是0.triangle的进化,演示使用索引数据画一个矩形
|
||||
|
||||
#include"VulkanAppFramework.h"
|
||||
#include<hgl/math/Math.h>
|
||||
|
||||
using namespace hgl;
|
||||
using namespace hgl::graph;
|
||||
|
||||
constexpr uint32_t SCREEN_WIDTH=128;
|
||||
constexpr uint32_t SCREEN_HEIGHT=128;
|
||||
|
||||
struct WorldConfig
|
||||
{
|
||||
Matrix4f mvp;
|
||||
}world;
|
||||
|
||||
constexpr uint32_t VERTEX_COUNT=4;
|
||||
|
||||
constexpr float vertex_data[VERTEX_COUNT][2]=
|
||||
{
|
||||
{SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.25},
|
||||
{SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.25},
|
||||
{SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.75},
|
||||
{SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.75}
|
||||
};
|
||||
|
||||
constexpr uint32_t INDEX_COUNT=6;
|
||||
|
||||
constexpr uint16 index_data[INDEX_COUNT]=
|
||||
{
|
||||
0,1,3,
|
||||
0,3,2
|
||||
};
|
||||
|
||||
class TestApp:public VulkanApplicationFramework
|
||||
{
|
||||
private:
|
||||
|
||||
uint swap_chain_count=0;
|
||||
|
||||
vulkan::Material * material =nullptr;
|
||||
vulkan::DescriptorSets * desciptor_sets =nullptr;
|
||||
vulkan::Renderable * render_obj =nullptr;
|
||||
vulkan::Buffer * ubo_mvp =nullptr;
|
||||
|
||||
vulkan::Pipeline * pipeline =nullptr;
|
||||
vulkan::CommandBuffer ** cmd_buf =nullptr;
|
||||
|
||||
vulkan::VertexBuffer * vertex_buffer =nullptr;
|
||||
vulkan::IndexBuffer * index_buffer =nullptr;
|
||||
|
||||
public:
|
||||
|
||||
~TestApp()
|
||||
{
|
||||
SAFE_CLEAR(index_buffer);
|
||||
SAFE_CLEAR(vertex_buffer);
|
||||
SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count);
|
||||
SAFE_CLEAR(pipeline);
|
||||
SAFE_CLEAR(ubo_mvp);
|
||||
SAFE_CLEAR(render_obj);
|
||||
SAFE_CLEAR(desciptor_sets);
|
||||
SAFE_CLEAR(material);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
bool InitMaterial()
|
||||
{
|
||||
material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition.vert.spv"),
|
||||
OS_TEXT("FlatColor.frag.spv"));
|
||||
if(!material)
|
||||
return(false);
|
||||
|
||||
render_obj=material->CreateRenderable();
|
||||
desciptor_sets=material->CreateDescriptorSets();
|
||||
return(true);
|
||||
}
|
||||
|
||||
bool InitUBO()
|
||||
{
|
||||
const VkExtent2D extent=device->GetExtent();
|
||||
|
||||
world.mvp=ortho(extent.width,extent.height);
|
||||
|
||||
ubo_mvp=device->CreateUBO(sizeof(WorldConfig),&world);
|
||||
|
||||
if(!ubo_mvp)
|
||||
return(false);
|
||||
|
||||
return desciptor_sets->UpdateUBO(material->GetUBOBinding("world"),*ubo_mvp);
|
||||
}
|
||||
|
||||
void InitVBO()
|
||||
{
|
||||
vertex_buffer =device->CreateVBO(FMT_RG32F,VERTEX_COUNT,vertex_data);
|
||||
index_buffer =device->CreateIBO16(INDEX_COUNT,index_data);
|
||||
|
||||
render_obj->Set("Vertex",vertex_buffer);
|
||||
render_obj->Set(index_buffer);
|
||||
}
|
||||
|
||||
bool InitPipeline()
|
||||
{
|
||||
vulkan::PipelineCreater *
|
||||
pipeline_creater=new vulkan::PipelineCreater(device,material,device->GetRenderPass());
|
||||
pipeline_creater->SetDepthTest(false);
|
||||
pipeline_creater->SetDepthWrite(false);
|
||||
pipeline_creater->CloseCullFace();
|
||||
pipeline_creater->Set(PRIM_TRIANGLES);
|
||||
|
||||
pipeline=pipeline_creater->Create();
|
||||
|
||||
delete pipeline_creater;
|
||||
pipeline_creater=nullptr;
|
||||
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
bool InitCommandBuffer()
|
||||
{
|
||||
cmd_buf=hgl_zero_new<vulkan::CommandBuffer *>(swap_chain_count);
|
||||
|
||||
for(uint i=0;i<swap_chain_count;i++)
|
||||
{
|
||||
cmd_buf[i]=device->CreateCommandBuffer();
|
||||
|
||||
if(!cmd_buf[i])
|
||||
return(false);
|
||||
|
||||
cmd_buf[i]->Begin();
|
||||
cmd_buf[i]->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(i));
|
||||
cmd_buf[i]->Bind(pipeline);
|
||||
cmd_buf[i]->Bind(desciptor_sets);
|
||||
cmd_buf[i]->Bind(render_obj);
|
||||
cmd_buf[i]->DrawIndexed(INDEX_COUNT);
|
||||
cmd_buf[i]->EndRenderPass();
|
||||
cmd_buf[i]->End();
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
bool Init()
|
||||
{
|
||||
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
||||
return(false);
|
||||
|
||||
swap_chain_count=device->GetSwapChainImageCount();
|
||||
|
||||
if(!InitMaterial())
|
||||
return(false);
|
||||
|
||||
if(!InitUBO())
|
||||
return(false);
|
||||
|
||||
InitVBO();
|
||||
|
||||
if(!InitPipeline())
|
||||
return(false);
|
||||
|
||||
if(!InitCommandBuffer())
|
||||
return(false);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
void Draw() override
|
||||
{
|
||||
const uint32_t frame_index=device->GetCurrentFrameIndices();
|
||||
|
||||
const vulkan::CommandBuffer *cb=cmd_buf[frame_index];
|
||||
|
||||
Submit(*cb);
|
||||
}
|
||||
};//class TestApp:public VulkanApplicationFramework
|
||||
|
||||
int main(int,char **)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
if(!vulkan::CheckStrideBytesByFormat())
|
||||
return 0xff;
|
||||
#endif//
|
||||
|
||||
TestApp app;
|
||||
|
||||
if(!app.Init())
|
||||
return(-1);
|
||||
|
||||
while(app.Run());
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,4 +1,7 @@
|
||||
#include"VulkanAppFramework.h"
|
||||
// 0.triangle
|
||||
// 该范例主要演示直接绘制一个渐变色的三角形
|
||||
|
||||
#include"VulkanAppFramework.h"
|
||||
#include<hgl/math/Math.h>
|
||||
|
||||
using namespace hgl;
|
||||
|
@ -79,8 +79,8 @@ public: //Buffer
|
||||
VertexBuffer * CreateVBO(VkFormat format,uint32_t count,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateVBO(format,count,nullptr,sharing_mode);}
|
||||
|
||||
IndexBuffer * CreateIBO(VkIndexType index_type,uint32_t count,const void *data,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE);
|
||||
IndexBuffer * CreateIBO16(uint32_t count,const void *data,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateIBO(VK_INDEX_TYPE_UINT16,count,data,sharing_mode);}
|
||||
IndexBuffer * CreateIBO32(uint32_t count,const void *data,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateIBO(VK_INDEX_TYPE_UINT32,count,data,sharing_mode);}
|
||||
IndexBuffer * CreateIBO16(uint32_t count,const uint16 *data,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateIBO(VK_INDEX_TYPE_UINT16,count,(void *)data,sharing_mode);}
|
||||
IndexBuffer * CreateIBO32(uint32_t count,const uint32 *data,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateIBO(VK_INDEX_TYPE_UINT32,count,(void *)data,sharing_mode);}
|
||||
|
||||
IndexBuffer * CreateIBO(VkIndexType index_type,uint32_t count,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateIBO(index_type,count,nullptr,sharing_mode);}
|
||||
IndexBuffer * CreateIBO16(uint32_t count,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateIBO(VK_INDEX_TYPE_UINT16,count,nullptr,sharing_mode);}
|
||||
|
17
res/shader/OnlyPosition.vert
Normal file
17
res/shader/OnlyPosition.vert
Normal file
@ -0,0 +1,17 @@
|
||||
#version 450 core
|
||||
|
||||
layout(location = 0) in vec2 Vertex;
|
||||
|
||||
layout(binding = 0) uniform WorldConfig
|
||||
{
|
||||
mat4 mvp;
|
||||
} world;
|
||||
|
||||
layout(location = 0) out vec4 FragmentColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragmentColor=vec4(1.0);
|
||||
|
||||
gl_Position=vec4(Vertex,0.0,1.0)*world.mvp;
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
glslangValidator -V -o FlatColor.vert.spv FlatColor.vert
|
||||
glslangValidator -V -o OnlyPosition.vert.spv OnlyPosition.vert
|
||||
glslangValidator -V -o FlatColor.frag.spv FlatColor.frag
|
||||
|
@ -87,8 +87,8 @@ IndexBuffer *Device::CreateIBO(VkIndexType index_type,uint32_t count,const void
|
||||
uint32_t stride;
|
||||
|
||||
if(index_type==VK_INDEX_TYPE_UINT16)stride=2;else
|
||||
if(index_type==VK_INDEX_TYPE_UINT32)stride=4;else
|
||||
return(nullptr);
|
||||
if(index_type==VK_INDEX_TYPE_UINT32)stride=4;else
|
||||
return(nullptr);
|
||||
|
||||
const VkDeviceSize size=stride*count;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user