2023-09-06 16:24:05 +08:00
|
|
|
|
#include"RenderAssignBuffer.h"
|
|
|
|
|
#include<hgl/graph/VKVertexAttribBuffer.h>
|
|
|
|
|
#include<hgl/graph/VKDevice.h>
|
|
|
|
|
#include<hgl/graph/VKMaterialInstance.h>
|
|
|
|
|
#include<hgl/graph/RenderNode.h>
|
2023-09-20 15:55:14 +08:00
|
|
|
|
#include<hgl/graph/VKRenderable.h>
|
2023-09-06 16:55:04 +08:00
|
|
|
|
#include<hgl/graph/VKRenderAssign.h>
|
2023-09-06 16:24:05 +08:00
|
|
|
|
#include<hgl/graph/mtl/UBOCommon.h>
|
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2023-09-20 15:55:14 +08:00
|
|
|
|
RenderAssignBuffer::RenderAssignBuffer(GPUDevice *dev,const uint mi_bytes)
|
2023-09-19 22:04:34 +08:00
|
|
|
|
{
|
|
|
|
|
hgl_zero(*this);
|
|
|
|
|
|
|
|
|
|
device=dev;
|
|
|
|
|
|
2023-09-20 15:55:14 +08:00
|
|
|
|
mi_data_bytes=mi_bytes;
|
|
|
|
|
|
|
|
|
|
ubo_mi=nullptr;
|
|
|
|
|
mi_count=0;
|
2023-09-19 22:04:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 16:24:05 +08:00
|
|
|
|
VkBuffer RenderAssignBuffer::GetAssignVBO()const
|
|
|
|
|
{
|
|
|
|
|
return vbo_assigns->GetBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 02:24:58 +08:00
|
|
|
|
void RenderAssignBuffer::Bind(Material *mtl)const
|
2023-09-06 16:24:05 +08:00
|
|
|
|
{
|
2023-10-12 02:24:58 +08:00
|
|
|
|
if(!mtl)return;
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
2023-10-12 02:24:58 +08:00
|
|
|
|
if(!mtl->hasAssign())
|
|
|
|
|
return;
|
2023-09-25 21:49:35 +08:00
|
|
|
|
|
|
|
|
|
mtl->BindUBO(DescriptorSetType::PerFrame,mtl::SBS_LocalToWorld.name,ubo_l2w);
|
2023-09-26 14:50:38 +08:00
|
|
|
|
mtl->BindUBO(DescriptorSetType::PerMaterial,mtl::SBS_MaterialInstance.name,ubo_mi);
|
2023-09-06 16:24:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 18:09:31 +08:00
|
|
|
|
void RenderAssignBuffer::Clear()
|
2023-09-06 16:24:05 +08:00
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(ubo_l2w);
|
|
|
|
|
SAFE_CLEAR(ubo_mi);
|
|
|
|
|
SAFE_CLEAR(vbo_assigns);
|
|
|
|
|
|
|
|
|
|
node_count=0;
|
2023-09-20 15:55:14 +08:00
|
|
|
|
mi_count=0;
|
2023-09-06 16:24:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 18:09:31 +08:00
|
|
|
|
void RenderAssignBuffer::Alloc(const uint nc,const uint mc)
|
2023-09-06 16:24:05 +08:00
|
|
|
|
{
|
2023-09-07 18:09:31 +08:00
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
|
|
{
|
2023-09-20 15:55:14 +08:00
|
|
|
|
node_count=nc;
|
2023-09-07 18:09:31 +08:00
|
|
|
|
|
|
|
|
|
ubo_l2w=device->CreateUBO(node_count*sizeof(Matrix4f));
|
|
|
|
|
}
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
2023-09-20 15:55:14 +08:00
|
|
|
|
if(mi_data_bytes>0&&mc>0)
|
|
|
|
|
{
|
|
|
|
|
mi_count=mc;
|
2023-09-07 18:09:31 +08:00
|
|
|
|
|
2023-09-20 15:55:14 +08:00
|
|
|
|
ubo_mi=device->CreateUBO(mi_data_bytes*mi_count);
|
|
|
|
|
}
|
2023-09-07 18:09:31 +08:00
|
|
|
|
|
2023-09-20 15:55:14 +08:00
|
|
|
|
vbo_assigns=device->CreateVBO(ASSIGN_VBO_FMT,node_count);
|
2023-09-06 16:24:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 18:09:31 +08:00
|
|
|
|
void RenderAssignBuffer::WriteNode(RenderNode *render_node,const uint count,const MaterialInstanceSets &mi_set)
|
2023-09-06 16:24:05 +08:00
|
|
|
|
{
|
2023-09-07 18:09:31 +08:00
|
|
|
|
RenderNode *rn;
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
2023-09-07 18:09:31 +08:00
|
|
|
|
Alloc(count,mi_set.GetCount());
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
2023-09-19 21:07:07 +08:00
|
|
|
|
if(ubo_mi)
|
2023-09-07 18:09:31 +08:00
|
|
|
|
{
|
|
|
|
|
uint8 *mip=(uint8 *)(ubo_mi->Map());
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
2023-09-07 18:09:31 +08:00
|
|
|
|
for(MaterialInstance *mi:mi_set)
|
|
|
|
|
{
|
2023-09-20 15:55:14 +08:00
|
|
|
|
memcpy(mip,mi->GetMIData(),mi_data_bytes);
|
2023-09-07 18:09:31 +08:00
|
|
|
|
mip+=mi_data_bytes;
|
|
|
|
|
}
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
2023-09-07 18:09:31 +08:00
|
|
|
|
ubo_mi->Unmap();
|
|
|
|
|
}
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
2023-09-07 18:09:31 +08:00
|
|
|
|
uint16 *idp=(uint16 *)(vbo_assigns->Map());
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Matrix4f *tp=(hgl::Matrix4f *)(ubo_l2w->Map());
|
2023-09-20 21:53:30 +08:00
|
|
|
|
Matrix4f *l2w=tp;
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
|
|
|
|
rn=render_node;
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<count;i++)
|
|
|
|
|
{
|
2023-09-20 21:53:30 +08:00
|
|
|
|
*l2w=rn->local_to_world;
|
|
|
|
|
++l2w;
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
|
|
|
|
*idp=i;
|
|
|
|
|
++idp;
|
|
|
|
|
|
2023-09-20 15:55:14 +08:00
|
|
|
|
*idp=mi_set.Find(rn->ri->GetMaterialInstance());
|
|
|
|
|
++idp;
|
2023-09-07 18:09:31 +08:00
|
|
|
|
|
2023-09-06 16:24:05 +08:00
|
|
|
|
++rn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ubo_l2w->Unmap();
|
|
|
|
|
}
|
2023-09-07 18:09:31 +08:00
|
|
|
|
|
|
|
|
|
vbo_assigns->Unmap();
|
2023-09-06 16:24:05 +08:00
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|