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-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-19 22:04:34 +08:00
|
|
|
|
RenderAssignBuffer::RenderAssignBuffer(GPUDevice *dev,const uint mi_total_bytes)
|
|
|
|
|
{
|
|
|
|
|
hgl_zero(*this);
|
|
|
|
|
|
|
|
|
|
device=dev;
|
|
|
|
|
|
|
|
|
|
if(mi_total_bytes>0)
|
|
|
|
|
ubo_mi=device->CreateUBO(mi_total_bytes);
|
|
|
|
|
else
|
|
|
|
|
ubo_mi=nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 16:24:05 +08:00
|
|
|
|
VkBuffer RenderAssignBuffer::GetAssignVBO()const
|
|
|
|
|
{
|
|
|
|
|
return vbo_assigns->GetBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderAssignBuffer::Bind(MaterialInstance *mi)const
|
|
|
|
|
{
|
|
|
|
|
const VIL *vil=mi->GetVIL();
|
|
|
|
|
|
|
|
|
|
const uint assign_binding_count=vil->GetCount(VertexInputGroup::Assign);
|
|
|
|
|
|
|
|
|
|
if(assign_binding_count<=0)return;
|
|
|
|
|
|
|
|
|
|
mi->BindUBO(DescriptorSetType::PerFrame,mtl::SBS_LocalToWorld.name,ubo_l2w);
|
|
|
|
|
// mi->BindUBO(DescriptorSetType::PerFrame,"Assign",assign_buffer->ubo_mi);
|
|
|
|
|
}
|
|
|
|
|
|
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-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();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
node_count=power_to_2(nc);
|
|
|
|
|
|
|
|
|
|
ubo_l2w=device->CreateUBO(node_count*sizeof(Matrix4f));
|
|
|
|
|
}
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
2023-09-19 22:04:34 +08:00
|
|
|
|
vbo_assigns=device->CreateVBO(ASSIGN_VBO_FMT,node_count);
|
|
|
|
|
}
|
2023-09-07 18:09:31 +08:00
|
|
|
|
|
2023-09-19 22:04:34 +08:00
|
|
|
|
bool RenderAssignBuffer::WriteMIData(void *mi_data,const uint bytes)
|
|
|
|
|
{
|
|
|
|
|
if(!mi_data||!bytes||!ubo_mi)return(false);
|
2023-09-07 18:09:31 +08:00
|
|
|
|
|
2023-09-19 22:04:34 +08:00
|
|
|
|
return ubo_mi->Write(mi_data,bytes);
|
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)
|
|
|
|
|
{
|
|
|
|
|
// memcpy(mip,mi->GetData(),mi_data_bytes);
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
rn=render_node;
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
*tp=rn->local_to_world;
|
|
|
|
|
++tp;
|
|
|
|
|
|
|
|
|
|
*idp=i;
|
|
|
|
|
++idp;
|
|
|
|
|
|
2023-09-07 18:09:31 +08:00
|
|
|
|
//*idp=mi_set.Find(rn->ri->GetMaterialInstance());
|
|
|
|
|
//++idp;
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//void WriteMaterialInstance(RenderNode *render_node,const uint count,const MaterialInstanceSets &mi_set)
|
|
|
|
|
//{
|
|
|
|
|
// //MaterialInstance ID
|
|
|
|
|
// {
|
|
|
|
|
// uint8 *tp=(uint8 *)(mi_id->Map());
|
|
|
|
|
|
|
|
|
|
// for(uint i=0;i<count;i++)
|
|
|
|
|
// {
|
|
|
|
|
// *tp=mi_set.Find(render_node->ri->GetMaterialInstance());
|
|
|
|
|
// ++tp;
|
|
|
|
|
// ++render_node;
|
|
|
|
|
// }
|
|
|
|
|
// mi_id->Unmap();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// //MaterialInstance Data
|
|
|
|
|
// {
|
|
|
|
|
// //const uint count=mi_set.GetCount();
|
|
|
|
|
|
|
|
|
|
// //uint8 *tp=(uint8 *)(mi_data_buffer->Map());
|
|
|
|
|
// //const MaterialInstance **mi=mi_set.GetData();
|
|
|
|
|
|
|
|
|
|
// //for(uint i=0;i<count;i++)
|
|
|
|
|
// //{
|
|
|
|
|
// // memcpy(tp,(*mi)->GetData(),mi_size);
|
|
|
|
|
//
|
|
|
|
|
// // ++mi;
|
|
|
|
|
// // tp+=mi_size;
|
|
|
|
|
// //}
|
|
|
|
|
|
|
|
|
|
// //mi_data_buffer->Unmap();
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_END
|