finished dynamic UBO for RenderList,,,but rendering breakout error.

This commit is contained in:
hyzboy 2021-06-22 21:33:47 +08:00
parent bbab22304d
commit c3e9015d95
16 changed files with 152 additions and 80 deletions

View File

@ -70,7 +70,7 @@ private:
struct SubpassParam
{
Material * material;
MaterialParameters * material_instance;
MaterialInstance * material_instance;
Pipeline * pipeline_fan;
Pipeline * pipeline_triangles;
};//

View File

@ -29,7 +29,7 @@ private:
RenderList render_list;
Material * material =nullptr;
MaterialParameters * material_instance =nullptr;
MaterialInstance * material_instance =nullptr;
Renderable * ro_rectangle =nullptr;
Renderable * ro_circle =nullptr;
@ -119,7 +119,7 @@ private:
cam.Refresh();
ubo_camera_info =CreateUBO("camera", sizeof(CameraInfo),&cam.info);
ubo_color_material =CreateUBO("color_material",sizeof(Vector4f),&color);
ubo_color_material =CreateUBO("color_material", sizeof(Vector4f),&color);
material_instance->Update();
return(true);

View File

@ -22,14 +22,14 @@ class TestApp:public CameraAppFramework
private:
SceneNode render_root;
RenderList render_list;
RenderList *render_list=nullptr;
struct MDP
{
Material * material =nullptr;
MaterialParameters * material_instance =nullptr;
MaterialInstance * material_instance =nullptr;
Pipeline * pipeline =nullptr;
}m3d,m2d;
}m3d;//,m2d;
Renderable *ro_plane_grid[3],
*ro_round_rectangle =nullptr;
@ -46,10 +46,17 @@ private:
mdp->pipeline=CreatePipeline(mdp->material_instance,InlinePipeline::Solid3D,primitive);
if(!mdp->material_instance->BindUBO("camera",GetCameraInfoBuffer()))
return(false);
{
MaterialParameters *mp_global=mdp->material_instance->GetMP(DescriptorSetType::Global);
if(!mp_global)
return(false);
if(!mp_global->BindUBO("g_camera",GetCameraInfoBuffer()))return(false);
mp_global->Update();
}
mdp->material_instance->Update();
return(true);
}
@ -57,14 +64,14 @@ private:
{
auto ri=db->CreateRenderableInstance(r,mdp.material_instance,mdp.pipeline);
render_root.Add(ri);
render_root.CreateSubNode(ri);
}
void Add(Renderable *r,MDP &mdp,const Matrix4f &mat)
{
auto ri=db->CreateRenderableInstance(r,mdp.material_instance,mdp.pipeline);
render_root.Add(ri,mat);
render_root.CreateSubNode(mat,ri);
}
void CreateRenderObject()
@ -96,54 +103,61 @@ private:
pgci.side_color.Set(0,0,1,1);
ro_plane_grid[2]=CreateRenderablePlaneGrid(db,m3d.material,&pgci);
{
struct RoundRectangleCreateInfo rrci;
//{
// struct RoundRectangleCreateInfo rrci;
rrci.scope.Set(SCREEN_WIDTH-30,10,20,20);
rrci.radius=5;
rrci.round_per=5;
// rrci.scope.Set(SCREEN_WIDTH-30,10,20,20);
// rrci.radius=5;
// rrci.round_per=5;
ro_round_rectangle=CreateRenderableRoundRectangle(db,m2d.material,&rrci);
}
// ro_round_rectangle=CreateRenderableRoundRectangle(db,m2d.material,&rrci);
//}
camera->pos.Set(200,200,200,1.0);
}
bool InitScene()
{
Add(ro_round_rectangle,m2d);
// Add(ro_round_rectangle,m2d);
Add(ro_plane_grid[0],m3d);
Add(ro_plane_grid[1],m3d,rotate(HGL_RAD_90,0,1,0));
Add(ro_plane_grid[2],m3d,rotate(HGL_RAD_90,1,0,0));
render_root.RefreshMatrix();
render_root.ExpendToList(&render_list);
render_list->Expend(camera->info,&render_root);
return(true);
}
public:
~TestApp()
{
SAFE_CLEAR(render_list);
}
bool Init()
{
if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
render_list=new RenderList(device);
if(!InitMDP(&m3d,Prim::Lines,OS_TEXT("res/material/VertexColor3D")))
return(false);
if(!InitMDP(&m2d,Prim::Fan, OS_TEXT("res/material/PureColor2D")))
return(false);
//if(!InitMDP(&m2d,Prim::Fan, OS_TEXT("res/material/PureColor2D")))
// return(false);
{
color.Set(1,1,0,1);
ubo_color=device->CreateUBO(sizeof(Vector4f),&color);
//{
// color.Set(1,1,0,1);
// ubo_color=device->CreateUBO(sizeof(Vector4f),&color);
m2d.material_instance->BindUBO("color_material",ubo_color);
m2d.material_instance->Update();
// m2d.material_instance->BindUBO("color_material",ubo_color);
// m2d.material_instance->Update();
db->Add(ubo_color);
}
// db->Add(ubo_color);
//}
CreateRenderObject();
@ -155,7 +169,7 @@ public:
void BuildCommandBuffer(uint32 index)
{
VulkanApplicationFramework::BuildCommandBuffer(index,&render_list);
VulkanApplicationFramework::BuildCommandBuffer(index,render_list);
}
};//class TestApp:public CameraAppFramework

View File

@ -54,7 +54,7 @@ private:
struct MIR
{
MaterialParameters * material_instance =nullptr;
MaterialInstance * material_instance =nullptr;
RenderableInstance *renderable_instance =nullptr;
}mir_nearest,mir_linear,mir_nearest_hq,mir_linear_hq;
@ -64,8 +64,9 @@ private:
VAB * tex_coord_buffer =nullptr;
IndexBuffer * index_buffer =nullptr;
SceneNode render_root;
RenderList render_list;
SceneNode render_root;
RenderList render_list;
public:
~TestApp()
@ -161,8 +162,16 @@ private:
mir->material_instance=db->CreateMaterialInstance(mp->material);
if(!mir->material_instance)return(false);
if(!mir->material_instance->BindSampler("tex",texture,sampler))return(false);
mir->material_instance->Update();
{
MaterialParameters *mp_texture=mir->material_instance->GetMP(DescriptorSetType::Value);
if(!mp_texture)
return(false);
if(!mp_texture->BindSampler("tex",texture,sampler))return(false);
mp_texture->Update();
}
mir->renderable_instance=db->CreateRenderableInstance(render_obj,mir->material_instance,mp->pipeline);
@ -174,7 +183,7 @@ private:
bool Add(struct MIR *mir,const Matrix4f &offset)
{
render_root.Add(mir->renderable_instance,offset);
render_root.CreateSubNode(offset,mir->renderable_instance);
return(true);
}
@ -187,7 +196,7 @@ private:
Add(&mir_linear_hq, translate( 0, 0,0));
render_root.RefreshMatrix();
render_root.ExpendToList(&render_list);
render_list.Expend(&render_root);
BuildCommandBuffer(&render_list);
return(true);
}

View File

@ -105,7 +105,7 @@ public:
cili.lunarg.standard_validation = true;
cili.khronos.validation = true;
// cili.RenderDoc.Capture = true;
cili.RenderDoc.Capture = true;
inst=CreateInstance("VulkanTest",nullptr,&cili);

View File

@ -31,6 +31,8 @@ namespace hgl
CompOperatorMemcmp(const MVPMatrix &);
};//struct MVPMatrix
constexpr size_t MVPMatrixBytes=sizeof(MVPMatrix);
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_SCENE_INFO_INCLUDE

View File

@ -33,16 +33,17 @@ namespace hgl
buffer_usage_flags=flags;
{
uint32_t unit_size=sizeof(T);
vk_ma=new VKMemoryAllocator(device,buffer_usage_flags); // construct function is going to set AllocUnitSize by minUniformOffsetAlignment
MemoryBlock *mb=new MemoryBlock(vk_ma);
ubo_offset_alignment=device->GetUBOAlign();
ubo_offset_alignment=vk_ma->GetAllocUnitSize();
uint32_t unit_size=sizeof(T);
const uint32_t align_size=ubo_offset_alignment-1;
unit_size=(unit_size+align_size)&(~align_size);
vk_ma=new VKMemoryAllocator(device,buffer_usage_flags,unit_size); // construct function is going to set AllocUnitSize by minUniformOffsetAlignment
MemoryBlock *mb=new MemoryBlock(vk_ma);
coll=new Collection(unit_size,mb);
}
}
@ -84,6 +85,11 @@ namespace hgl
{
return (T *)(coll->Map(start,count));
}
void Flush(const uint32 count)
{
vk_ma->Flush(count*GetUnitSize());
}
};//class GPUArrayBuffer
}//namespace graph
}//namespace hgl

View File

@ -100,12 +100,16 @@ public: //内存相关
private: //Buffer相关
bool CreateBuffer(GPUBufferData *buf,VkBufferUsageFlags buf_usage,VkDeviceSize size,const void *data,SharingMode sharing_mode);
bool CreateBuffer(GPUBufferData *buf,VkBufferUsageFlags buf_usage,VkDeviceSize range,VkDeviceSize size,const void *data,SharingMode sharing_mode);
bool CreateBuffer(GPUBufferData *buf,VkBufferUsageFlags buf_usage, VkDeviceSize size,const void *data,SharingMode sharing_mode){return CreateBuffer(buf,buf_usage,size,size,data,sharing_mode);}
public: //Buffer相关
GPUBuffer * CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize size,const void *data, SharingMode sm=SharingMode::Exclusive);
GPUBuffer * CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize size, SharingMode sm=SharingMode::Exclusive){return CreateBuffer(buf_usage,size,nullptr,sm);}
GPUBuffer * CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize range,VkDeviceSize size,const void *data, SharingMode sm=SharingMode::Exclusive);
GPUBuffer * CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize range,VkDeviceSize size, SharingMode sm=SharingMode::Exclusive){return CreateBuffer(buf_usage,range,size,nullptr,sm);}
GPUBuffer * CreateBuffer(VkBufferUsageFlags buf_usage, VkDeviceSize size,const void *data, SharingMode sm=SharingMode::Exclusive){return CreateBuffer(buf_usage,size,size,data,sm);}
GPUBuffer * CreateBuffer(VkBufferUsageFlags buf_usage, VkDeviceSize size, SharingMode sm=SharingMode::Exclusive){return CreateBuffer(buf_usage,size,size,nullptr,sm);}
VAB * CreateVAB (VkFormat format, uint32_t count,const void *data, SharingMode sm=SharingMode::Exclusive);
VAB * CreateVAB (VkFormat format, uint32_t count, SharingMode sm=SharingMode::Exclusive){return CreateVAB(format,count,nullptr,sm);}
@ -119,8 +123,12 @@ public: //Buffer相关
IndexBuffer * CreateIBO16 ( uint32_t count, SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U16, count,nullptr,sm);}
IndexBuffer * CreateIBO32 ( uint32_t count, SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U32, count,nullptr,sm);}
#define CREATE_BUFFER_OBJECT(LargeName,type) GPUBuffer *Create##LargeName( VkDeviceSize size,void *data, SharingMode sm=SharingMode::Exclusive) {return CreateBuffer(VK_BUFFER_USAGE_##type##_BUFFER_BIT,size,data, sm);} \
GPUBuffer *Create##LargeName( VkDeviceSize size, SharingMode sm=SharingMode::Exclusive) {return CreateBuffer(VK_BUFFER_USAGE_##type##_BUFFER_BIT,size,nullptr, sm);}
const VkDeviceSize GetUBOAlign();
#define CREATE_BUFFER_OBJECT(LargeName,type) GPUBuffer *Create##LargeName( VkDeviceSize size,void *data, SharingMode sm=SharingMode::Exclusive) {return CreateBuffer(VK_BUFFER_USAGE_##type##_BUFFER_BIT,size ,size,data, sm);} \
GPUBuffer *Create##LargeName( VkDeviceSize size, SharingMode sm=SharingMode::Exclusive) {return CreateBuffer(VK_BUFFER_USAGE_##type##_BUFFER_BIT,size ,size,nullptr, sm);} \
GPUBuffer *Create##LargeName(VkDeviceSize range,VkDeviceSize size,void *data, SharingMode sm=SharingMode::Exclusive) {return CreateBuffer(VK_BUFFER_USAGE_##type##_BUFFER_BIT,range,size,data, sm);} \
GPUBuffer *Create##LargeName(VkDeviceSize range,VkDeviceSize size, SharingMode sm=SharingMode::Exclusive) {return CreateBuffer(VK_BUFFER_USAGE_##type##_BUFFER_BIT,range,size,nullptr, sm);}
CREATE_BUFFER_OBJECT(UBO,UNIFORM)
CREATE_BUFFER_OBJECT(SSBO,STORAGE)

View File

@ -13,6 +13,8 @@ class VKMemoryAllocator:public AbstractMemoryAllocator
GPUBuffer *gpu_buffer;
VkDeviceSize range; //ubo之类需要一个一次访问范围
protected:
bool AllocMemory() override;
@ -27,10 +29,12 @@ public:
public:
VKMemoryAllocator(GPUDevice *,const uint32_t flags);
VKMemoryAllocator(GPUDevice *,const uint32_t flags,const VkDeviceSize r);
~VKMemoryAllocator();
void Free() override {/* DON'T RUN ANY OPERATION.*/}
void Flush(const VkDeviceSize);
};//class VKMemoryAllocator:public AbstractMemoryAllocator
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_MEMORY_ALLOCATOR_INCLUDE

View File

@ -25,7 +25,9 @@ using ShaderStageList =ObjectList<ShaderStage>;
struct ShaderDescriptor
{
char name[128];
DescriptorSetType type;
VkDescriptorType desc_type;
DescriptorSetType set_type;
uint32_t set;
uint32_t binding;
};
@ -80,13 +82,15 @@ public:
ShaderDescriptorList * GetDescriptorList (VkDescriptorType desc_type)
{
if(desc_type<VK_DESCRIPTOR_TYPE_BEGIN_RANGE
||desc_type>VK_DESCRIPTOR_TYPE_END_RANGE)return nullptr;
||desc_type>VK_DESCRIPTOR_TYPE_END_RANGE)return nullptr;
return descriptor_list+desc_type;
}
ShaderDescriptorList &GetUBO (){return descriptor_list[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER];}
ShaderDescriptorList &GetSSBO (){return descriptor_list[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER];}
ShaderDescriptorList &GetUBO (){return descriptor_list[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER];}
ShaderDescriptorList &GetSSBO (){return descriptor_list[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER];}
ShaderDescriptorList &GetUBODynamic (){return descriptor_list[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC];}
ShaderDescriptorList &GetSSBODynamic(){return descriptor_list[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC];}
ShaderDescriptorList &GetSampler(){return descriptor_list[VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER];}
const int GetBinding (VkDescriptorType desc_type,const AnsiString &name)const;

2
res

@ -1 +1 @@
Subproject commit 179d904193cfbd0a3292fd7e7d0ed034011ed15d
Subproject commit 9f031f98a611172812ceb4094af0f032218c5e91

View File

@ -137,12 +137,14 @@ namespace hgl
for(RenderNode *node:render_node_list) //未来可能要在Expend处考虑做去重
{
memcpy(mp,&(node->matrix),sizeof(MVPMatrix));
memcpy(mp,&(node->matrix),MVPMatrixBytes);
mp+=ubo_align;
(*ri)=node->ri;
++ri;
}
mvp_array->Flush(count);
}
}
@ -152,7 +154,10 @@ namespace hgl
MaterialParameters *mp=mtl->GetMP(DescriptorSetType::Renderable);
if(mp)
mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),false);
{
mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true);
mp->Update();
}
}
}
@ -218,9 +223,9 @@ namespace hgl
{
mp=ri->GetMP((DescriptorSetType)i);
if(last_mp[i]!=mp)
// if(last_mp[i]!=mp)
{
last_mp[i]=mp;
//last_mp[i]=mp;
if(mp)
{

View File

@ -34,9 +34,9 @@ void DescriptorSetLayoutCreater::Bind(const ShaderDescriptorList *sd_list,VkDesc
for(const ShaderDescriptor &sd:*sd_list)
{
all_set.Add(sd.type);
all_set.Add(sd.set_type);
++binding_count[(size_t)sd.type];
++binding_count[(size_t)sd.set_type];
}
ENUM_CLASS_FOR(DescriptorSetType,int,i)
@ -54,16 +54,16 @@ void DescriptorSetLayoutCreater::Bind(const ShaderDescriptorList *sd_list,VkDesc
if(!all_binding.IsMember(sd.binding))
{
p[(size_t)sd.type]->binding = sd.binding;
p[(size_t)sd.type]->descriptorType = desc_type;
p[(size_t)sd.type]->descriptorCount = 1;
p[(size_t)sd.type]->stageFlags = stageFlags;
p[(size_t)sd.type]->pImmutableSamplers = nullptr;
p[(size_t)sd.set_type]->binding = sd.binding;
p[(size_t)sd.set_type]->descriptorType = sd.desc_type;
p[(size_t)sd.set_type]->descriptorCount = 1;
p[(size_t)sd.set_type]->stageFlags = stageFlags;
p[(size_t)sd.set_type]->pImmutableSamplers = nullptr;
all_binding.Add(sd.binding);
++p[(size_t)sd.type];
++fin_count[(size_t)sd.type];
++p[(size_t)sd.set_type];
++fin_count[(size_t)sd.set_type];
}
}

View File

@ -1,9 +1,15 @@
#include<hgl/graph/VKDevice.h>
#include<hgl/graph/VKIndexBuffer.h>
#include<hgl/graph/VKVertexAttribBuffer.h>
#include<hgl/graph/VKPhysicalDevice.h>
VK_NAMESPACE_BEGIN
bool GPUDevice::CreateBuffer(GPUBufferData *buf,VkBufferUsageFlags buf_usage,VkDeviceSize size,const void *data,SharingMode sharing_mode)
const VkDeviceSize GPUDevice::GetUBOAlign()
{
return attr->physical_device->GetUBOAlign();
}
bool GPUDevice::CreateBuffer(GPUBufferData *buf,VkBufferUsageFlags buf_usage,VkDeviceSize range,VkDeviceSize size,const void *data,SharingMode sharing_mode)
{
BufferCreateInfo buf_info;
@ -26,14 +32,13 @@ bool GPUDevice::CreateBuffer(GPUBufferData *buf,VkBufferUsageFlags buf_usage,VkD
{
buf->info.buffer =buf->buffer;
buf->info.offset =0;
buf->info.range =size;
buf->info.range =range;
buf->memory =dm;
if(!data)
return(true);
dm->Write(data,0,size);
return(true);
}
@ -57,7 +62,7 @@ VAB *GPUDevice::CreateVAB(VkFormat format,uint32_t count,const void *data,Sharin
GPUBufferData buf;
if(!CreateBuffer(&buf,VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,size,data,sharing_mode))
if(!CreateBuffer(&buf,VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,size,size,data,sharing_mode))
return(nullptr);
return(new VertexAttribBuffer(attr->device,buf,format,stride,count));
@ -81,11 +86,11 @@ IndexBuffer *GPUDevice::CreateIBO(IndexType index_type,uint32_t count,const void
return(new IndexBuffer(attr->device,buf,index_type,count));
}
GPUBuffer *GPUDevice::CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize size,const void *data,SharingMode sharing_mode)
GPUBuffer *GPUDevice::CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize range,VkDeviceSize size,const void *data,SharingMode sharing_mode)
{
GPUBufferData buf;
if(!CreateBuffer(&buf,buf_usage,size,data,sharing_mode))
if(!CreateBuffer(&buf,buf_usage,range,size,data,sharing_mode))
return(nullptr);
return(new GPUBuffer(attr->device,buf));

View File

@ -4,15 +4,14 @@
#include<hgl/graph/VKBuffer.h>
VK_NAMESPACE_BEGIN
VKMemoryAllocator::VKMemoryAllocator(GPUDevice *d,const uint32_t flags)
VKMemoryAllocator::VKMemoryAllocator(GPUDevice *d,const uint32_t flags,const VkDeviceSize r)
{
device=d;
buffer_usage_flag_bits=flags;
gpu_buffer=nullptr;
range=r;
const GPUPhysicalDevice *pd=device->GetPhysicalDevice();
SetAllocUnitSize(pd->GetUBOAlign());
SetAllocUnitSize(range);
}
VKMemoryAllocator::~VKMemoryAllocator()
@ -29,7 +28,7 @@ bool VKMemoryAllocator::AllocMemory()
if(gpu_buffer)
delete gpu_buffer;
gpu_buffer=device->CreateBuffer(buffer_usage_flag_bits,alloc_size);
gpu_buffer=device->CreateBuffer(buffer_usage_flag_bits,range,alloc_size);
if(!gpu_buffer)
{
@ -41,4 +40,9 @@ bool VKMemoryAllocator::AllocMemory()
return(true);
}
void VKMemoryAllocator::Flush(const VkDeviceSize size)
{
gpu_buffer->Flush(size);
}
VK_NAMESPACE_END

View File

@ -68,7 +68,7 @@ VK_NAMESPACE_BEGIN
return data;
}
const uint8 *LoadShaderDescriptor(const uint8_t version,ShaderDescriptorList *sd_list,const uint8 *data)
const uint8 *LoadShaderDescriptor(const uint8_t version,ShaderDescriptorList *sd_list,const VkDescriptorType desc_type,const uint8 *data)
{
const uint32 total_bytes=AccessByPointer(data,uint32);
@ -94,7 +94,18 @@ VK_NAMESPACE_BEGIN
sd->name[str_len]=0;
data+=str_len;
sd->type=CheckDescriptorSetType(sd->name);
sd->set_type=CheckDescriptorSetType(sd->name);
if(sd->set_type==DescriptorSetType::Renderable)
{
if(desc_type==VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)sd->desc_type=VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;else
if(desc_type==VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)sd->desc_type=VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;else
sd->desc_type=desc_type;
}
else
{
sd->desc_type=desc_type;
}
++sd;
}
@ -223,7 +234,7 @@ VK_NAMESPACE_BEGIN
{
desc_type=AccessByPointer(filedata,uint32);
filedata=LoadShaderDescriptor(version,sr->GetDescriptorList((VkDescriptorType)desc_type),filedata);
filedata=LoadShaderDescriptor(version,sr->GetDescriptorList((VkDescriptorType)desc_type),(VkDescriptorType)desc_type,filedata);
}
return sr;