command buffer增加bind pipeline layout

This commit is contained in:
hyzboy 2019-04-19 00:46:49 +08:00
parent ca2a20aabd
commit 133019cf49
12 changed files with 187 additions and 82 deletions

View File

@ -15,8 +15,7 @@ SET(VULKAN_TEST_SOURCE_FILES main.cpp
VKDeviceCreater.cpp
VKDevice.cpp
VKBuffer.cpp
VKDescriptorSet.cpp
VKDescriptorSetLayout.cpp
VKDescriptorSets.cpp
VKPipelineLayout.cpp
VKRenderPass.cpp
VKShader.cpp
@ -34,8 +33,7 @@ SET(VULKAN_TEST_HEADER_FILES VK.h
VKDeviceAttribute.h
VKDevice.h
VKBuffer.h
VKDescriptorSetLayout.h
VKDescriptorSet.h
VKDescriptorSets.h
VKPipelineLayout.h
VKRenderPass.h
VKShader.h

View File

@ -2,7 +2,9 @@
#include"VKRenderPass.h"
#include"VKFramebuffer.h"
#include"VKPipeline.h"
#include"VKPipelineLayout.h"
#include"VKVertexInput.h"
#include"VKDescriptorSets.h"
VK_NAMESPACE_BEGIN
CommandBuffer::CommandBuffer(VkDevice dev,VkCommandPool cp,VkCommandBuffer cb)
@ -25,7 +27,7 @@ CommandBuffer::~CommandBuffer()
vkFreeCommandBuffers(device, pool, 1, cmd_bufs);
}
bool CommandBuffer::Bind(RenderPass *rp,Framebuffer *fb,Pipeline *p)
bool CommandBuffer::Begin(RenderPass *rp,Framebuffer *fb)
{
VkRenderPassBeginInfo rp_begin;
@ -38,12 +40,27 @@ bool CommandBuffer::Bind(RenderPass *rp,Framebuffer *fb,Pipeline *p)
rp_begin.pClearValues = clear_values;
vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, *p);
return(true);
}
bool CommandBuffer::Bind(VertexInput *vi)
bool CommandBuffer::Bind(Pipeline *p)
{
if(!p)return(false);
vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, *p);
return(true);
}
bool CommandBuffer::Bind(PipelineLayout *pl)
{
if(!pl)return(false);
vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, *pl, 0, pl->GetDescriptorSetCount(),pl->GetDescriptorSets(), 0, nullptr);
return(true);
}
bool CommandBuffer::Bind(VertexInput *vi,const VkDeviceSize offset)
{
if(!vi)
return(false);
@ -53,10 +70,24 @@ bool CommandBuffer::Bind(VertexInput *vi)
if(buf_list.GetCount()<=0)
return(false);
constexpr VkDeviceSize zero_offsets[1]={0};
VkDeviceSize zero_offsets[1]={offset};
vkCmdBindVertexBuffers(cmd_buf,0,buf_list.GetCount(),buf_list.GetData(),zero_offsets);
return(true);
}
void CommandBuffer::Draw(const uint32_t vertex_count)
{
vkCmdDraw(cmd_buf,vertex_count,1,0,0);
}
void CommandBuffer::Draw(const uint32_t vertex_count,const uint32_t instance_count,const uint32_t first_vertex,const uint32_t first_instance)
{
vkCmdDraw(cmd_buf,vertex_count,instance_count,first_vertex,first_instance);
}
void CommandBuffer::End()
{
vkCmdEndRenderPass(cmd_buf);
}
VK_NAMESPACE_END

View File

@ -6,6 +6,7 @@ VK_NAMESPACE_BEGIN
class RenderPass;
class Framebuffer;
class Pipeline;
class PipelineLayout;
class VertexInput;
class CommandBuffer
@ -23,9 +24,27 @@ public:
~CommandBuffer();
void SetRenderArea(const VkRect2D &ra){render_area=ra;}
void SetClearColor(float r,float g,float b,float a=1.0f)
{
clear_values[0].color.float32[0]=r;
clear_values[0].color.float32[1]=g;
clear_values[0].color.float32[2]=b;
clear_values[0].color.float32[3]=a;
}
bool Bind(RenderPass *rp,Framebuffer *fb,Pipeline *p);
bool Bind(VertexInput *vi);
void SetClearDepthStencil(float d=1.0f,float s=0)
{
clear_values[1].depthStencil.depth=d;
clear_values[1].depthStencil.stencil=s;
}
bool Begin(RenderPass *rp,Framebuffer *fb);
bool Bind(Pipeline *p);
bool Bind(PipelineLayout *pl);
bool Bind(VertexInput *vi,const VkDeviceSize offset=0);
void Draw(const uint32_t vertex_count);
void Draw(const uint32_t vertex_count,const uint32_t instance_count,const uint32_t first_vertex=0,const uint32_t first_instance=0);
void End();
};//class CommandBuffer
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE

View File

@ -1,4 +0,0 @@
#include"VKDescriptorSet.h"
VK_NAMESPACE_BEGIN
VK_NAMESPACE_END

View File

@ -1,8 +0,0 @@
#ifndef HGL_GRAPH_VULKAN_DESCRIPTOR_SET_INCLUDE
#define HGL_GRAPH_VULKAN_DESCRIPTOR_SET_INCLUDE
#include"VK.h"
VK_NAMESPACE_BEGIN
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SET_INCLUDE

View File

@ -1,48 +0,0 @@
#include"VKDescriptorSetLayout.h"
VK_NAMESPACE_BEGIN
DescriptorSetLayout::~DescriptorSetLayout()
{
const int count=desc_set_layout_list.GetCount();
if(count>0)
{
VkDescriptorSetLayout *dsl=desc_set_layout_list.GetData();
for(int i=0;i<count;i++)
{
vkDestroyDescriptorSetLayout(device,*dsl,nullptr);
++dsl;
}
}
}
void DescriptorSetLayoutCreater::Bind(const int binding,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags)
{
VkDescriptorSetLayoutBinding layout_binding = {};
layout_binding.binding = binding;
layout_binding.descriptorType = desc_type;
layout_binding.descriptorCount = 1;
layout_binding.stageFlags = stageFlags;
layout_binding.pImmutableSamplers = nullptr;
layout_binding_list.Add(layout_binding);
}
DescriptorSetLayout *DescriptorSetLayoutCreater::Create()
{
VkDescriptorSetLayoutCreateInfo descriptor_layout = {};
descriptor_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptor_layout.pNext = nullptr;
descriptor_layout.bindingCount = layout_binding_list.GetCount();
descriptor_layout.pBindings = layout_binding_list.GetData();
List<VkDescriptorSetLayout> dsl_list;
dsl_list.SetCount(layout_binding_list.GetCount());
if(vkCreateDescriptorSetLayout(device,&descriptor_layout, nullptr, dsl_list.GetData())!=VK_SUCCESS)
return(false);
return(new DescriptorSetLayout(device,dsl_list));
}
VK_NAMESPACE_END

View File

@ -0,0 +1,84 @@
#include"VKDescriptorSets.h"
#include"VKDevice.h"
VK_NAMESPACE_BEGIN
DescriptorSets::~DescriptorSets()
{
// 这里注释掉是因为从来不见那里的范便有FREE过但又有vkFreeDescriptorSets这个函数。如发现此注释请使用工具查是否有资源泄露
{
//const int count=desc_sets.GetCount();
//if(count>0)
// vkFreeDescriptorSets(device->GetDevice(),device->GetDescriptorPool(),count,desc_sets.GetData());
}
}
DescriptorSetLayout::~DescriptorSetLayout()
{
const int count=desc_set_layout_list.GetCount();
if(count>0)
{
VkDescriptorSetLayout *dsl=desc_set_layout_list.GetData();
for(int i=0;i<count;i++)
{
vkDestroyDescriptorSetLayout(device->GetDevice(),*dsl,nullptr);
++dsl;
}
}
}
DescriptorSets *DescriptorSetLayout::CreateSets()const
{
const int count=desc_set_layout_list.GetCount();
if(count<=0)
return(nullptr);
VkDescriptorSetAllocateInfo alloc_info[1];
alloc_info[0].sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
alloc_info[0].pNext = nullptr;
alloc_info[0].descriptorPool = device->GetDescriptorPool();
alloc_info[0].descriptorSetCount = count;
alloc_info[0].pSetLayouts = desc_set_layout_list.GetData();
List<VkDescriptorSet> desc_set;
desc_set.SetCount(count);
if(vkAllocateDescriptorSets(device->GetDevice(), alloc_info, desc_set.GetData())!=VK_SUCCESS)
return(nullptr);
return(new DescriptorSets(device,desc_set));
}
void DescriptorSetLayoutCreater::Bind(const int binding,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags)
{
VkDescriptorSetLayoutBinding layout_binding = {};
layout_binding.binding = binding;
layout_binding.descriptorType = desc_type;
layout_binding.descriptorCount = 1;
layout_binding.stageFlags = stageFlags;
layout_binding.pImmutableSamplers = nullptr;
layout_binding_list.Add(layout_binding);
}
DescriptorSetLayout *DescriptorSetLayoutCreater::Create()
{
VkDescriptorSetLayoutCreateInfo descriptor_layout = {};
descriptor_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptor_layout.pNext = nullptr;
descriptor_layout.bindingCount = layout_binding_list.GetCount();
descriptor_layout.pBindings = layout_binding_list.GetData();
List<VkDescriptorSetLayout> dsl_list;
dsl_list.SetCount(layout_binding_list.GetCount());
if(vkCreateDescriptorSetLayout(device->GetDevice(),&descriptor_layout, nullptr, dsl_list.GetData())!=VK_SUCCESS)
return(false);
return(new DescriptorSetLayout(device,dsl_list));
}
VK_NAMESPACE_END

View File

@ -1,16 +1,31 @@
#ifndef HGL_GRAPH_VULKAN_DESCRIPTOR_SET_LAYOUT_INCLUDE
#define HGL_GRAPH_VULKAN_DESCRIPTOR_SET_LAYOUT_INCLUDE
#ifndef HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE
#define HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE
#include"VK.h"
VK_NAMESPACE_BEGIN
class Device;
class DescriptorSets
{
Device *device;
List<VkDescriptorSet> desc_sets;
public:
DescriptorSets(Device *dev,List<VkDescriptorSet> &ds):device(dev),desc_sets(ds){}
~DescriptorSets();
const uint32_t GetCount()const{return desc_sets.GetCount();}
const VkDescriptorSet * GetData()const{return desc_sets.GetData();}
};//class DescriptorSets
class DescriptorSetLayout
{
VkDevice device;
Device *device;
List<VkDescriptorSetLayout> desc_set_layout_list;
public:
DescriptorSetLayout(VkDevice dev,const List<VkDescriptorSetLayout> &dsl_list)
DescriptorSetLayout(Device *dev,const List<VkDescriptorSetLayout> &dsl_list)
{
device=dev;
desc_set_layout_list=dsl_list;
@ -20,6 +35,8 @@ public:
const uint32_t GetCount()const{return desc_set_layout_list.GetCount();}
const VkDescriptorSetLayout * GetData ()const{return desc_set_layout_list.GetData();}
DescriptorSets *CreateSets()const;
};//class DescriptorSetLayout
/**
@ -27,14 +44,14 @@ public:
*/
class DescriptorSetLayoutCreater
{
VkDevice device;
Device *device;
VkDescriptorSet desc_set;
List<VkDescriptorSetLayoutBinding> layout_binding_list;
public:
DescriptorSetLayoutCreater(VkDevice dev):device(dev){}
DescriptorSetLayoutCreater(Device *dev):device(dev){}
~DescriptorSetLayoutCreater()=default;
void Bind(const int binding,VkDescriptorType,VkShaderStageFlagBits);
@ -62,4 +79,4 @@ public:
DescriptorSetLayout *Create();
};//class DescriptorSet
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SET_LAYOUT_INCLUDE
#endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE

View File

@ -35,6 +35,8 @@ public:
const PhysicalDevice *GetPhysicalDevice ()const {return attr->physical_device;}
const VkExtent2D & GetExtent ()const {return attr->swapchain_extent;}
VkDescriptorPool GetDescriptorPool () {return attr->desc_pool;}
public:
Buffer * CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize size,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE);

View File

@ -50,6 +50,7 @@ private:
public:
PipelineCreater(Device *dev);
~PipelineCreater()=default;

View File

@ -1,5 +1,5 @@
#include"VKPipelineLayout.h"
#include"VKDescriptorSetLayout.h"
#include"VKDescriptorSets.h"
VK_NAMESPACE_BEGIN
PipelineLayout::~PipelineLayout()
@ -14,6 +14,11 @@ PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl
if(dsl->GetCount()<=0)return(nullptr);
DescriptorSets *desc_sets=dsl->CreateSets();
if(!desc_sets)
return(nullptr);
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pPipelineLayoutCreateInfo.pNext = nullptr;
@ -25,8 +30,11 @@ PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl
VkPipelineLayout pipeline_layout;
if(vkCreatePipelineLayout(dev, &pPipelineLayoutCreateInfo, nullptr, &pipeline_layout)!=VK_SUCCESS)
{
delete desc_sets;
return(nullptr);
}
return(new PipelineLayout(dev,pipeline_layout));
return(new PipelineLayout(dev,pipeline_layout,desc_sets));
}
VK_NAMESPACE_END

View File

@ -2,24 +2,29 @@
#define HGL_GRAPH_VULKAN_PIPELINE_LAYOUT_INCLUDE
#include"VK.h"
#include"VKDescriptorSets.h"
VK_NAMESPACE_BEGIN
class DescriptorSetLayout;
class PipelineLayout
{
VkDevice device;
VkPipelineLayout layout;
const DescriptorSets *desc_sets;
private:
friend PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl);
PipelineLayout(VkDevice dev,VkPipelineLayout pl){device=dev;layout=pl;}
PipelineLayout(VkDevice dev,VkPipelineLayout pl,const DescriptorSets *ds){device=dev;layout=pl;desc_sets=ds;}
public:
~PipelineLayout();
operator VkPipelineLayout (){return layout;}
const uint32_t GetDescriptorSetCount ()const{return desc_sets->GetCount();}
const VkDescriptorSet * GetDescriptorSets ()const{return desc_sets->GetData();}
};//class PipelineLayout
PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl);