1.将DescriptorSetLayoutCreater类独立到src目录做为私有代码隐藏

2.将shader数据更改放在DescriptorSets类
This commit is contained in:
hyzboy 2019-05-05 21:30:55 +08:00
parent 14fe3c7629
commit 9425a2a91e
18 changed files with 300 additions and 278 deletions

View File

@ -4,7 +4,7 @@
using namespace hgl;
using namespace hgl::graph;
void SaveToTOML(const OSString &filename,const VkGraphicsPipelineCreateInfo *info);
//void SaveToTOML(const OSString &filename,const VkGraphicsPipelineCreateInfo *info);
constexpr uint32_t SCREEN_WIDTH=1280;
constexpr uint32_t SCREEN_HEIGHT=720;
@ -36,6 +36,7 @@ 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;
@ -55,6 +56,7 @@ public:
SAFE_CLEAR(pipeline);
SAFE_CLEAR(ubo_mvp);
SAFE_CLEAR(render_obj);
SAFE_CLEAR(desciptor_sets);
SAFE_CLEAR(material);
}
@ -68,6 +70,7 @@ private:
return(false);
render_obj=material->CreateRenderable();
desciptor_sets=material->CreateDescriptorSets();
return(true);
}
@ -82,7 +85,7 @@ private:
if(!ubo_mvp)
return(false);
return material->UpdateUBO("world",*ubo_mvp);
return desciptor_sets->UpdateUBO(material->GetUBOBinding("world"),*ubo_mvp);
}
void InitVBO()
@ -103,7 +106,7 @@ private:
pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_TRIANGLES);
SaveToTOML(OS_TEXT("pipeline.toml"),pipeline_creater->GetInfo());
// SaveToTOML(OS_TEXT("pipeline.toml"),pipeline_creater->GetInfo());
pipeline=pipeline_creater->Create();
@ -125,7 +128,7 @@ private:
cmd_buf[i]->Begin();
cmd_buf[i]->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(i));
cmd_buf[i]->Bind(pipeline);
cmd_buf[i]->Bind(material);
cmd_buf[i]->Bind(desciptor_sets);
cmd_buf[i]->Bind(render_obj);
cmd_buf[i]->Draw(VERTEX_COUNT);
cmd_buf[i]->EndRenderPass();

View File

@ -6,7 +6,7 @@ VK_NAMESPACE_BEGIN
class RenderPass;
class Framebuffer;
class Pipeline;
class Material;
class DescriptorSets;
class Renderable;
class CommandBuffer
@ -47,7 +47,7 @@ public:
bool Begin();
bool BeginRenderPass(RenderPass *rp,Framebuffer *fb);
bool Bind(Pipeline *p);
bool Bind(Material *);
bool Bind(DescriptorSets *);
bool Bind(Renderable *);
void EndRenderPass();
bool End();

View File

@ -6,11 +6,10 @@
VK_NAMESPACE_BEGIN
class Device;
class DescriptorSetLayout
class DescriptorSets
{
Device *device;
int count;
VkDescriptorSetLayout *desc_set_layout_list;
VkDescriptorSet *desc_set_list;
Map<uint32_t,int> index_by_binding;
@ -20,11 +19,10 @@ private:
friend class DescriptorSetLayoutCreater;
DescriptorSetLayout(Device *dev,const int c,VkDescriptorSetLayout *dsl_list,VkPipelineLayout pl,VkDescriptorSet *desc_set,Map<uint32_t,int> &bi)
DescriptorSets(Device *dev,const int c,VkPipelineLayout pl,VkDescriptorSet *desc_set,Map<uint32_t,int> &bi)
{
device=dev;
count=c;
desc_set_layout_list=dsl_list;
desc_set_list=desc_set;
index_by_binding=bi;
pipeline_layout=pl;
@ -32,56 +30,21 @@ private:
public:
~DescriptorSetLayout();
~DescriptorSets();
const uint32_t GetCount ()const{return count;}
const VkDescriptorSetLayout * GetLayouts ()const{return desc_set_layout_list;}
const VkDescriptorSet * GetDescriptorSets ()const{return desc_set_list;}
VkDescriptorSet GetDescriptorSet (const uint32_t binding);
const VkPipelineLayout GetPipelineLayout ()const{return pipeline_layout;}
};//class DescriptorSetLayout
/**
*
*/
class DescriptorSetLayoutCreater
{
Device *device;
bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info);
//bool UpdateUBO(const UTF8String &name,const VkDescriptorBufferInfo *buf_info)
//{
// if(name.IsEmpty()||!buf_info)
// return(false);
List<VkDescriptorSetLayoutBinding> layout_binding_list;
Map<uint32_t,int> index_by_binding;
public:
DescriptorSetLayoutCreater(Device *dev):device(dev){}
~DescriptorSetLayoutCreater()=default;
void Bind(const uint32_t binding,VkDescriptorType,VkShaderStageFlagBits);
void Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType type,VkShaderStageFlagBits stage);
#define DESC_SET_BIND_FUNC(name,vkname) void Bind##name(const uint32_t binding,VkShaderStageFlagBits stage_flag){Bind(binding,VK_DESCRIPTOR_TYPE_##vkname,stage_flag);} \
void Bind##name(const uint32_t *binding,const uint32_t count,VkShaderStageFlagBits stage_flag){Bind(binding,count,VK_DESCRIPTOR_TYPE_##vkname,stage_flag);}
DESC_SET_BIND_FUNC(Sampler, SAMPLER);
DESC_SET_BIND_FUNC(UBO, UNIFORM_BUFFER);
DESC_SET_BIND_FUNC(SSBO, STORAGE_BUFFER);
DESC_SET_BIND_FUNC(CombinedImageSampler, COMBINED_IMAGE_SAMPLER);
DESC_SET_BIND_FUNC(SampledImage, SAMPLED_IMAGE);
DESC_SET_BIND_FUNC(StorageImage, STORAGE_IMAGE);
DESC_SET_BIND_FUNC(UniformTexelBuffer, UNIFORM_TEXEL_BUFFER);
DESC_SET_BIND_FUNC(StorageTexelBuffer, STORAGE_TEXEL_BUFFER);
DESC_SET_BIND_FUNC(UBODynamic, UNIFORM_BUFFER_DYNAMIC);
DESC_SET_BIND_FUNC(SBODynamic, STORAGE_BUFFER_DYNAMIC);
DESC_SET_BIND_FUNC(InputAttachment, INPUT_ATTACHMENT);
#undef DESC_SET_BIND_FUNC
DescriptorSetLayout *Create();
};//class DescriptorSet
// return UpdateUBO(GetUBOBinding(name),buf_info);
//}
};//class DescriptorSets
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE

View File

@ -8,8 +8,8 @@ VK_NAMESPACE_BEGIN
class Device;
class ShaderModule;
class VertexShaderModule;
class DescriptorSets;
class DescriptorSetLayoutCreater;
class DescriptorSetLayout;
class VertexAttributeBinding;
class VertexBuffer;
class Renderable;
@ -23,17 +23,18 @@ using ShaderModuleMap=hgl::Map<VkShaderStageFlagBits,const ShaderModule *>;
*/
class Material
{
VkDevice device;
Device *device;
ShaderModuleMap *shader_maps;
VertexShaderModule *vertex_sm;
List<VkPipelineShaderStageCreateInfo> *shader_stage_list;
DescriptorSetLayoutCreater *dsl_creater;
DescriptorSetLayout *desc_set_layout;
VertexAttributeBinding *vab;
public:
Material(Device *dev,ShaderModuleMap *smm,VertexShaderModule *vsm,List<VkPipelineShaderStageCreateInfo> *,DescriptorSetLayoutCreater *dslc,DescriptorSetLayout *dsl,VertexAttributeBinding *v);
Material(Device *dev,ShaderModuleMap *smm,List<VkPipelineShaderStageCreateInfo> *,DescriptorSetLayoutCreater *dslc);
~Material();
const int GetUBOBinding(const UTF8String &)const;
@ -42,18 +43,8 @@ public:
const uint32_t GetStageCount ()const{return shader_stage_list->GetCount();}
const VkPipelineShaderStageCreateInfo * GetStages ()const{return shader_stage_list->GetData();}
const VkPipelineLayout GetPipelineLayout ()const;
const uint32_t GetDescriptorSetCount ()const;
const VkDescriptorSet * GetDescriptorSets ()const;
bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info);
bool UpdateUBO(const UTF8String &name,const VkDescriptorBufferInfo *buf_info)
{
if(name.IsEmpty()||!buf_info)
return(false);
return UpdateUBO(GetUBOBinding(name),buf_info);
}
const VkPipelineLayout GetPipelineLayout ()const;
DescriptorSets * CreateDescriptorSets()const;
void Write(VkPipelineVertexInputStateCreateInfo &vis)const;

View File

@ -17,8 +17,8 @@ class ShaderParse;
*/
class ShaderModule
{
VkDevice device;
int shader_id;
int ref_count;
private:
@ -34,7 +34,7 @@ protected:
public:
ShaderModule(int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *);
ShaderModule(VkDevice dev,int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *);
virtual ~ShaderModule();
const int GetID()const{return shader_id;}
@ -78,16 +78,9 @@ private:
Set<VertexAttributeBinding *> vab_sets;
private:
void ParseVertexInput(const ShaderParse *);
public:
VertexShaderModule(int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *parse):ShaderModule(id,pssci,parse)
{
ParseVertexInput(parse);
}
VertexShaderModule(VkDevice dev,int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *parse);
virtual ~VertexShaderModule();
const int GetLocation (const UTF8String &)const;

View File

@ -543,7 +543,7 @@ namespace hgl
}
template<typename F,typename T,typename DataPair>
void _Map<F,T,DataPair>::EnumValue(void (*enum_func)(T))
void _Map<F,T,DataPair>::EnumAllValue(void (*enum_func)(T))
{
const int count=data_list.GetCount();

View File

@ -104,9 +104,8 @@ namespace hgl
void Enum(void (*enum_func)(const F &,T)); ///<枚举所有数据项
void EnumKey(void (*enum_func)(const F &)); ///<枚举所有索引
void EnumValue(void (*enum_func)(T)); ///<枚举所有数值
void EnumValue(bool (*enum_func)(T)); ///<枚举所有数值
void EnumAllValue(void (*enum_func)(T)); ///<枚举所有数值
void EnumValue(bool (*enum_func)(T)); ///<枚举所有数值(返回true/false表示是否继续)
};//class _Map
template<typename F,typename T> class Map:public _Map<F,T,Pair<F,T> >

View File

@ -186,8 +186,9 @@ namespace hgl
return KeyConvert[key];
}
void WMProcDestroy(WinWindow *,uint32,uint32)
void WMProcDestroy(WinWindow *win,uint32,uint32)
{
win->OnClose();
PostQuitMessage(0);
}

View File

@ -122,12 +122,21 @@ namespace hgl
void WinWindow::Close()
{
ReleaseDC(win_hwnd, win_dc);
DestroyWindow(win_hwnd);
if(win_hwnd)
{
if(win_dc)
{
ReleaseDC(win_hwnd,win_dc);
win_dc = nullptr;
}
DestroyWindow(win_hwnd);
win_hwnd = nullptr;
}
UnregisterClassW(WIN_CLASS_NAME,hInstance);
win_dc = nullptr;
win_hwnd = nullptr;
is_close = true;
}

View File

@ -19,8 +19,6 @@ namespace hgl
bool Create();
void OnClose() override{Close();}
public:
using Window::Window;

View File

@ -34,6 +34,8 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
VKDeviceBuffer.cpp
VKBuffer.cpp
VKDescriptorSets.cpp
VKDescriptorSetLayoutCreater.cpp
VKDescriptorSetLayoutCreater.h
VKRenderPass.cpp
VKShaderModule.cpp
VKShaderModuleManage.cpp
@ -46,11 +48,11 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
VKRenderable.cpp)
#SET(RENDER_DEVICE_VULKAN_TOML_SOURCE toml/VKPipelineCreateInfo.TOML.cpp)
SET(RENDER_DEVICE_VULKAN_JSON_SOURCE json/VKPipelineCreateInfo.JSON.cpp)
#SET(RENDER_DEVICE_VULKAN_JSON_SOURCE json/VKPipelineCreateInfo.JSON.cpp)
SOURCE_GROUP("Header Files" FILES ${RENDER_DEVICE_VULKAN_HEADER})
SOURCE_GROUP("Source Files" FILES ${RENDER_DEVICE_VULKAN_SOURCE})
SOURCE_GROUP("JSON Source Files" FILES ${RENDER_DEVICE_VULKAN_JSON_SOURCE})
#SOURCE_GROUP("JSON Source Files" FILES ${RENDER_DEVICE_VULKAN_JSON_SOURCE})
add_library(ULRE.RenderDevice.Vulkan STATIC ${RENDER_DEVICE_VULKAN_HEADER}
${RENDER_DEVICE_VULKAN_SOURCE}

View File

@ -80,15 +80,15 @@ bool CommandBuffer::Bind(Pipeline *p)
return(true);
}
bool CommandBuffer::Bind(Material *mat)
bool CommandBuffer::Bind(DescriptorSets *dsl)
{
if(!mat)
if(!dsl)
return(false);
const uint32_t count=mat->GetDescriptorSetCount();
const uint32_t count=dsl->GetCount();
if(count>0)
vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,mat->GetPipelineLayout(),0,count,mat->GetDescriptorSets(),0,nullptr);
vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,dsl->GetPipelineLayout(),0,count,dsl->GetDescriptorSets(),0,nullptr);
return(true);
}

View File

@ -0,0 +1,131 @@
#include"VKDescriptorSetLayoutCreater.h"
#include<hgl/graph/vulkan/VKDescriptorSets.h>
#include<hgl/graph/vulkan/VKDevice.h>
VK_NAMESPACE_BEGIN
namespace
{
void DestroyDescriptorSetLayout(VkDevice device,const int count,VkDescriptorSetLayout *dsl_list)
{
if(count<=0)
return;
for(int i=0;i<count;i++)
{
vkDestroyDescriptorSetLayout(device,*dsl_list,nullptr);
++dsl_list;
}
}
}//namespace
DescriptorSetLayoutCreater::~DescriptorSetLayoutCreater()
{
if(pipeline_layout)
vkDestroyPipelineLayout(*device,pipeline_layout,nullptr);
if(dsl_list)
{
DestroyDescriptorSetLayout(*device,layout_binding_list.GetCount(),dsl_list);
delete[] dsl_list;
}
}
void DescriptorSetLayoutCreater::Bind(const uint32_t 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;
const int index=layout_binding_list.Add(layout_binding);
index_by_binding.Add(binding,index);
}
void DescriptorSetLayoutCreater::Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags)
{
const uint old_count=layout_binding_list.GetCount();
layout_binding_list.SetCount(old_count+count);
VkDescriptorSetLayoutBinding *p=layout_binding_list.GetData()+old_count;
for(uint i=old_count;i<old_count+count;i++)
{
p->binding = *binding;
p->descriptorType = desc_type;
p->descriptorCount = 1;
p->stageFlags = stageFlags;
p->pImmutableSamplers = nullptr;
index_by_binding.Add(*binding,i);
++binding;
++p;
}
}
bool DescriptorSetLayoutCreater::CreatePipelineLayout()
{
const int count=layout_binding_list.GetCount();
if(count<=0)
return(false);
VkDescriptorSetLayoutCreateInfo descriptor_layout = {};
descriptor_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptor_layout.pNext = nullptr;
descriptor_layout.bindingCount = count;
descriptor_layout.pBindings = layout_binding_list.GetData();
dsl_list=new VkDescriptorSetLayout[count];
if(vkCreateDescriptorSetLayout(*device,&descriptor_layout,nullptr,dsl_list)==VK_SUCCESS)
{
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pPipelineLayoutCreateInfo.pNext = nullptr;
pPipelineLayoutCreateInfo.pushConstantRangeCount = 0;
pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
pPipelineLayoutCreateInfo.setLayoutCount = count;
pPipelineLayoutCreateInfo.pSetLayouts = dsl_list;
if(vkCreatePipelineLayout(*device,&pPipelineLayoutCreateInfo,nullptr,&pipeline_layout)==VK_SUCCESS)
return(true);
}
delete[] dsl_list;
dsl_list=nullptr;
return(false);
}
DescriptorSets *DescriptorSetLayoutCreater::Create()
{
if(!pipeline_layout||!dsl_list)
return(nullptr);
const int count=layout_binding_list.GetCount();
if(count<=0)
return(nullptr);
VkDescriptorSetAllocateInfo alloc_info;
alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
alloc_info.pNext = nullptr;
alloc_info.descriptorPool = device->GetDescriptorPool();
alloc_info.descriptorSetCount = count;
alloc_info.pSetLayouts = dsl_list;
VkDescriptorSet *desc_set=new VkDescriptorSet[count];
if(vkAllocateDescriptorSets(*device,&alloc_info,desc_set)!=VK_SUCCESS)
{
delete[] desc_set;
return(nullptr);
}
return(new DescriptorSets(device,count,pipeline_layout,desc_set,index_by_binding));
}
VK_NAMESPACE_END

View File

@ -0,0 +1,57 @@
#pragma once
#include<hgl/graph/vulkan/VK.h>
#include<hgl/type/Map.h>
VK_NAMESPACE_BEGIN
class Device;
class DescriptorSets;
/**
*
*/
class DescriptorSetLayoutCreater
{
Device *device;
List<VkDescriptorSetLayoutBinding> layout_binding_list;
VkDescriptorSetLayout *dsl_list=nullptr;
Map<uint32_t,int> index_by_binding;
VkPipelineLayout pipeline_layout=nullptr;
public:
DescriptorSetLayoutCreater(Device *dev):device(dev){}
~DescriptorSetLayoutCreater();
void Bind(const uint32_t binding,VkDescriptorType,VkShaderStageFlagBits);
void Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType type,VkShaderStageFlagBits stage);
#define DESC_SET_BIND_FUNC(name,vkname) void Bind##name(const uint32_t binding,VkShaderStageFlagBits stage_flag){Bind(binding,VK_DESCRIPTOR_TYPE_##vkname,stage_flag);} \
void Bind##name(const uint32_t *binding,const uint32_t count,VkShaderStageFlagBits stage_flag){Bind(binding,count,VK_DESCRIPTOR_TYPE_##vkname,stage_flag);}
DESC_SET_BIND_FUNC(Sampler, SAMPLER);
DESC_SET_BIND_FUNC(UBO, UNIFORM_BUFFER);
DESC_SET_BIND_FUNC(SSBO, STORAGE_BUFFER);
DESC_SET_BIND_FUNC(CombinedImageSampler, COMBINED_IMAGE_SAMPLER);
DESC_SET_BIND_FUNC(SampledImage, SAMPLED_IMAGE);
DESC_SET_BIND_FUNC(StorageImage, STORAGE_IMAGE);
DESC_SET_BIND_FUNC(UniformTexelBuffer, UNIFORM_TEXEL_BUFFER);
DESC_SET_BIND_FUNC(StorageTexelBuffer, STORAGE_TEXEL_BUFFER);
DESC_SET_BIND_FUNC(UBODynamic, UNIFORM_BUFFER_DYNAMIC);
DESC_SET_BIND_FUNC(SBODynamic, STORAGE_BUFFER_DYNAMIC);
DESC_SET_BIND_FUNC(InputAttachment, INPUT_ATTACHMENT);
#undef DESC_SET_BIND_FUNC
bool CreatePipelineLayout();
const VkPipelineLayout GetPipelineLayout()const{return pipeline_layout;}
DescriptorSets *Create();
};//class DescriptorSetLayoutCreater
VK_NAMESPACE_END

View File

@ -2,22 +2,7 @@
#include<hgl/graph/vulkan/VKDevice.h>
VK_NAMESPACE_BEGIN
namespace
{
void DestroyDescriptorSetLayout(VkDevice device,const int count,VkDescriptorSetLayout *dsl_list)
{
if(count<=0)
return;
for(int i=0;i<count;i++)
{
vkDestroyDescriptorSetLayout(device,*dsl_list,nullptr);
++dsl_list;
}
}
}//namespace
VkDescriptorSet DescriptorSetLayout::GetDescriptorSet(const uint32_t binding)
VkDescriptorSet DescriptorSets::GetDescriptorSet(const uint32_t binding)
{
int index;
@ -27,111 +12,28 @@ VkDescriptorSet DescriptorSetLayout::GetDescriptorSet(const uint32_t binding)
return desc_set_list[index];
}
DescriptorSetLayout::~DescriptorSetLayout()
DescriptorSets::~DescriptorSets()
{
//if(count>0)
// vkFreeDescriptorSets(device->GetDevice(),device->GetDescriptorPool(),count,desc_set_list);
if(pipeline_layout)
vkDestroyPipelineLayout(*device,pipeline_layout,nullptr);
delete[] desc_set_list;
DestroyDescriptorSetLayout(*device,count,desc_set_layout_list);
delete[] desc_set_layout_list;
}
void DescriptorSetLayoutCreater::Bind(const uint32_t binding,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags)
bool DescriptorSets::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info)
{
VkDescriptorSetLayoutBinding layout_binding = {};
layout_binding.binding = binding;
layout_binding.descriptorType = desc_type;
layout_binding.descriptorCount = 1;
layout_binding.stageFlags = stageFlags;
layout_binding.pImmutableSamplers = nullptr;
VkDescriptorSet set=GetDescriptorSet(binding);
const int index=layout_binding_list.Add(layout_binding);
VkWriteDescriptorSet writeDescriptorSet = {};
index_by_binding.Add(binding,index);
}
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSet.dstSet = set;
writeDescriptorSet.descriptorCount = 1;
writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
writeDescriptorSet.pBufferInfo = buf_info;
writeDescriptorSet.dstBinding = binding;
void DescriptorSetLayoutCreater::Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags)
{
const uint old_count=layout_binding_list.GetCount();
layout_binding_list.SetCount(old_count+count);
VkDescriptorSetLayoutBinding *p=layout_binding_list.GetData()+old_count;
for(uint i=old_count;i<old_count+count;i++)
{
p->binding = *binding;
p->descriptorType = desc_type;
p->descriptorCount = 1;
p->stageFlags = stageFlags;
p->pImmutableSamplers = nullptr;
index_by_binding.Add(*binding,i);
++binding;
++p;
}
}
DescriptorSetLayout *DescriptorSetLayoutCreater::Create()
{
const int count=layout_binding_list.GetCount();
if(count<=0)
return(nullptr);
VkDescriptorSetLayoutCreateInfo descriptor_layout = {};
descriptor_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptor_layout.pNext = nullptr;
descriptor_layout.bindingCount = count;
descriptor_layout.pBindings = layout_binding_list.GetData();
VkDescriptorSetLayout *dsl_list=new VkDescriptorSetLayout[count];
if(vkCreateDescriptorSetLayout(*device,&descriptor_layout, nullptr, dsl_list)!=VK_SUCCESS)
{
delete[] dsl_list;
return(nullptr);
}
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pPipelineLayoutCreateInfo.pNext = nullptr;
pPipelineLayoutCreateInfo.pushConstantRangeCount = 0;
pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
pPipelineLayoutCreateInfo.setLayoutCount = count;
pPipelineLayoutCreateInfo.pSetLayouts = dsl_list;
VkPipelineLayout pipeline_layout;
if(vkCreatePipelineLayout(*device, &pPipelineLayoutCreateInfo, nullptr, &pipeline_layout)!=VK_SUCCESS)
{
delete[] dsl_list;
return(nullptr);
}
VkDescriptorSetAllocateInfo alloc_info;
alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
alloc_info.pNext = nullptr;
alloc_info.descriptorPool = device->GetDescriptorPool();
alloc_info.descriptorSetCount = count;
alloc_info.pSetLayouts = dsl_list;
VkDescriptorSet *desc_set=new VkDescriptorSet[count];
if(vkAllocateDescriptorSets(*device,&alloc_info,desc_set)!=VK_SUCCESS)
{
vkDestroyPipelineLayout(*device,pipeline_layout,nullptr);
delete[] desc_set;
DestroyDescriptorSetLayout(*device,count,dsl_list);
delete[] dsl_list;
return(nullptr);
}
return(new DescriptorSetLayout(device,count,dsl_list,pipeline_layout,desc_set,index_by_binding));
vkUpdateDescriptorSets(*device,1,&writeDescriptorSet,0,nullptr);
return(true);
}
VK_NAMESPACE_END

View File

@ -5,82 +5,75 @@
#include<hgl/graph/vulkan/VKVertexAttributeBinding.h>
#include<hgl/graph/vulkan/VKRenderable.h>
#include<hgl/graph/vulkan/VKBuffer.h>
#include"VKDescriptorSetLayoutCreater.h"
VK_NAMESPACE_BEGIN
Material *CreateMaterial(Device *dev,ShaderModuleMap *shader_maps)
{
DescriptorSetLayoutCreater *dsl_creater=new DescriptorSetLayoutCreater(dev);
const int shader_count=shader_maps->GetCount();
if(shader_count<2)
return(nullptr);
const ShaderModule *sm;
if(!shader_maps->Get(VK_SHADER_STAGE_VERTEX_BIT,sm))
return(nullptr);
const VertexShaderModule *vertex_sm=(VertexShaderModule *)sm;
DescriptorSetLayoutCreater *dsl_creater=new DescriptorSetLayoutCreater(dev);
List<VkPipelineShaderStageCreateInfo> *shader_stage_list=new List<VkPipelineShaderStageCreateInfo>;
shader_stage_list->SetCount(shader_count);
VkPipelineShaderStageCreateInfo *p=shader_stage_list->GetData();
VertexShaderModule *vertex_sm;
const ShaderModule *sm;
auto **itp=shader_maps->GetDataList();
for(int i=0;i<shader_count;i++)
{
sm=(*itp)->right;
if(sm->GetStage()==VK_SHADER_STAGE_VERTEX_BIT)
vertex_sm=(VertexShaderModule *)sm;
memcpy(p,sm->GetCreateInfo(),sizeof(VkPipelineShaderStageCreateInfo));
++p;
++itp;
{
const ShaderBindingList &ubo_list=sm->GetUBOBindingList();
dsl_creater->BindUBO(ubo_list.GetData(),ubo_list.GetCount(),sm->GetStage());
}
++p;
++itp;
}
VertexAttributeBinding *vab=vertex_sm->CreateVertexAttributeBinding();
dsl_creater->CreatePipelineLayout();
DescriptorSetLayout *dsl=dsl_creater->Create();
if(dsl)
{
return(new Material(dev,shader_maps,vertex_sm,shader_stage_list,dsl_creater,dsl,vab));
}
return(nullptr);
return(new Material(dev,shader_maps,shader_stage_list,dsl_creater));
}
Material::Material(Device *dev,ShaderModuleMap *smm,VertexShaderModule *vsm,List<VkPipelineShaderStageCreateInfo> *psci_list,DescriptorSetLayoutCreater *dslc,DescriptorSetLayout *dsl,VertexAttributeBinding *v)
Material::Material(Device *dev,ShaderModuleMap *smm,List<VkPipelineShaderStageCreateInfo> *psci_list,DescriptorSetLayoutCreater *dslc)
{
device=*dev;
device=dev;
shader_maps=smm;
vertex_sm=vsm;
shader_stage_list=psci_list;
dsl_creater=dslc;
desc_set_layout=dsl;
vab=v;
const ShaderModule *sm;
if(smm->Get(VK_SHADER_STAGE_VERTEX_BIT,sm))
{
vertex_sm=(VertexShaderModule *)sm;
vab=vertex_sm->CreateVertexAttributeBinding();
}
else
{
//理论上不可能到达这里前面CreateMaterial已经检测过了
vertex_sm=nullptr;
vab=nullptr;
}
}
Material::~Material()
{
delete vab;
delete desc_set_layout;
delete dsl_creater;
const int count=shader_stage_list->GetCount();
if(count>0)
{
VkPipelineShaderStageCreateInfo *ss=shader_stage_list->GetData();
for(int i=0;i<count;i++)
{
vkDestroyShaderModule(device,ss->module,nullptr);
++ss;
}
}
delete vab;
delete shader_stage_list;
delete shader_maps;
}
@ -114,34 +107,12 @@ const int Material::GetVBOBinding(const UTF8String &name)const
const VkPipelineLayout Material::GetPipelineLayout()const
{
return desc_set_layout->GetPipelineLayout();
return dsl_creater->GetPipelineLayout();
}
const uint32_t Material::GetDescriptorSetCount()const
DescriptorSets *Material::CreateDescriptorSets()const
{
return desc_set_layout->GetCount();
}
const VkDescriptorSet *Material::GetDescriptorSets()const
{
return desc_set_layout->GetDescriptorSets();
}
bool Material::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info)
{
VkDescriptorSet set=desc_set_layout->GetDescriptorSet(binding);
VkWriteDescriptorSet writeDescriptorSet = {};
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSet.dstSet = set;
writeDescriptorSet.descriptorCount = 1;
writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
writeDescriptorSet.pBufferInfo = buf_info;
writeDescriptorSet.dstBinding = binding;
vkUpdateDescriptorSets(device,1,&writeDescriptorSet,0,nullptr);
return(true);
return dsl_creater->Create();
}
void Material::Write(VkPipelineVertexInputStateCreateInfo &vis)const

View File

@ -3,8 +3,9 @@
#include<hgl/graph/vulkan/VKShaderParse.h>
VK_NAMESPACE_BEGIN
ShaderModule::ShaderModule(int id,VkPipelineShaderStageCreateInfo *sci,const ShaderParse *sp)
ShaderModule::ShaderModule(VkDevice dev,int id,VkPipelineShaderStageCreateInfo *sci,const ShaderParse *sp)
{
device=dev;
shader_id=id;
ref_count=0;
@ -15,6 +16,7 @@ ShaderModule::ShaderModule(int id,VkPipelineShaderStageCreateInfo *sci,const Sha
ShaderModule::~ShaderModule()
{
vkDestroyShaderModule(device,stage_create_info->module,nullptr);
delete stage_create_info;
}
@ -30,11 +32,11 @@ void ShaderModule::ParseUBO(const ShaderParse *parse)
}
}
void VertexShaderModule::ParseVertexInput(const ShaderParse *parse)
VertexShaderModule::VertexShaderModule(VkDevice dev,int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *parse):ShaderModule(dev,id,pssci,parse)
{
const auto &stage_inputs=parse->GetStageInput();
attr_count=stage_inputs.size();
attr_count=(uint32_t)stage_inputs.size();
binding_list=new VkVertexInputBindingDescription[attr_count];
attribute_list=new VkVertexInputAttributeDescription[attr_count];

View File

@ -49,9 +49,9 @@ const ShaderModule *ShaderModuleManage::CreateShader(const VkShaderStageFlagBits
ShaderParse *parse=new ShaderParse(spv_data,spv_size);
if(shader_stage_bit==VK_SHADER_STAGE_VERTEX_BIT)
sm=new VertexShaderModule(shader_count,shader_stage,parse);
sm=new VertexShaderModule(*device,shader_count,shader_stage,parse);
else
sm=new ShaderModule(shader_count,shader_stage,parse);
sm=new ShaderModule(*device,shader_count,shader_stage,parse);
delete parse;
shader_list.Add(shader_count,sm);