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
|
2024-03-26 01:20:20 +08:00
|
|
|
|
RenderAssignBuffer::RenderAssignBuffer(GPUDevice *dev,const bool has_l2w,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;
|
2023-09-06 16:24:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2024-03-26 01:20:20 +08:00
|
|
|
|
if(!mtl->HasMI())
|
2023-10-12 02:24:58 +08:00
|
|
|
|
return;
|
2023-09-25 21:49:35 +08:00
|
|
|
|
|
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_mi);
|
2024-03-26 01:20:20 +08:00
|
|
|
|
SAFE_CLEAR(vbo_mi);
|
|
|
|
|
|
|
|
|
|
SAFE_CLEAR(l2w_vbo[0])
|
|
|
|
|
SAFE_CLEAR(l2w_vbo[1])
|
|
|
|
|
SAFE_CLEAR(l2w_vbo[2])
|
|
|
|
|
SAFE_CLEAR(l2w_vbo[3])
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
|
|
|
|
node_count=0;
|
2023-09-20 15:55:14 +08:00
|
|
|
|
mi_count=0;
|
2023-09-06 16:24:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 01:20:20 +08:00
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
constexpr const char *l2w_buffer_name[]=
|
|
|
|
|
{
|
2024-03-26 01:33:17 +08:00
|
|
|
|
"VBO:Buffer:LocalToWorld:0",
|
|
|
|
|
"VBO:Buffer:LocalToWorld:1",
|
|
|
|
|
"VBO:Buffer:LocalToWorld:2",
|
|
|
|
|
"VBO:Buffer:LocalToWorld:3"
|
2024-03-26 01:20:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr const char *l2w_memory_name[]=
|
|
|
|
|
{
|
2024-03-26 01:33:17 +08:00
|
|
|
|
"VBO:Memory:LocalToWorld:0",
|
|
|
|
|
"VBO:Memory:LocalToWorld:1",
|
|
|
|
|
"VBO:Memory:LocalToWorld:2",
|
|
|
|
|
"VBO:Memory:LocalToWorld:3"
|
2024-03-26 01:20:20 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
#endif//_DEBUG
|
|
|
|
|
|
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
|
|
|
|
|
2024-03-26 01:20:20 +08:00
|
|
|
|
for(uint i=0;i<4;i++)
|
|
|
|
|
{
|
|
|
|
|
l2w_vbo[i]=device->CreateVBO(VF_V4F,node_count);
|
|
|
|
|
l2w_buffer[i]=l2w_vbo[i]->GetBuffer();
|
|
|
|
|
}
|
2023-09-07 18:09:31 +08:00
|
|
|
|
}
|
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
|
|
|
|
|
2024-03-26 01:20:20 +08:00
|
|
|
|
vbo_mi=device->CreateVBO(MI_VBO_FMT,node_count);
|
|
|
|
|
mi_buffer=vbo_mi->GetBuffer();
|
2023-10-12 05:55:39 +08:00
|
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2023-10-13 02:01:57 +08:00
|
|
|
|
DebugUtils *du=device->GetDebugUtils();
|
2023-10-12 05:55:39 +08:00
|
|
|
|
|
2023-10-13 02:01:57 +08:00
|
|
|
|
if(du)
|
2023-10-12 05:55:39 +08:00
|
|
|
|
{
|
2024-03-26 01:20:20 +08:00
|
|
|
|
if(l2w_buffer[0])
|
|
|
|
|
{
|
|
|
|
|
for(int i=0;i<4;i++)
|
|
|
|
|
{
|
|
|
|
|
du->SetBuffer(l2w_buffer[i],l2w_buffer_name[i]);
|
|
|
|
|
du->SetDeviceMemory(l2w_vbo[i]->GetVkMemory(),l2w_memory_name[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-12 05:55:39 +08:00
|
|
|
|
|
|
|
|
|
if(ubo_mi)
|
2023-10-13 01:48:07 +08:00
|
|
|
|
{
|
2023-10-13 02:01:57 +08:00
|
|
|
|
du->SetBuffer(ubo_mi->GetBuffer(),"UBO:Buffer:MaterialInstance");
|
|
|
|
|
du->SetDeviceMemory(ubo_mi->GetVkMemory(),"UBO:Memory:MaterialInstance");
|
2023-10-13 01:48:07 +08:00
|
|
|
|
}
|
2023-10-12 05:55:39 +08:00
|
|
|
|
|
2024-03-26 01:20:20 +08:00
|
|
|
|
if(vbo_mi)
|
|
|
|
|
{
|
|
|
|
|
du->SetBuffer(vbo_mi->GetBuffer(),"VBO:Buffer:MaterialInstanceID");
|
|
|
|
|
du->SetDeviceMemory(vbo_mi->GetVkMemory(),"VBO:Memory:MaterialInstanceID");
|
|
|
|
|
}
|
2023-10-12 05:55:39 +08:00
|
|
|
|
}
|
|
|
|
|
#endif//_DEBUG
|
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
|
|
|
|
|
2024-03-26 01:20:20 +08:00
|
|
|
|
if(l2w_buffer[0])
|
|
|
|
|
{
|
|
|
|
|
glm::vec4 *tp;
|
|
|
|
|
|
|
|
|
|
for(uint col=0;col<4;col++)
|
|
|
|
|
{
|
|
|
|
|
tp=(glm::vec4 *)(l2w_vbo[col]->Map());
|
|
|
|
|
|
|
|
|
|
rn=render_node;
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
*tp=rn->local_to_world[col];
|
|
|
|
|
++tp;
|
|
|
|
|
++rn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l2w_vbo[col]->Unmap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2024-03-26 01:20:20 +08:00
|
|
|
|
uint16 *idp=(uint16 *)(vbo_mi->Map());
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
|
|
|
|
{
|
2024-03-26 01:20:20 +08:00
|
|
|
|
rn=render_node;
|
2023-09-06 16:24:05 +08:00
|
|
|
|
|
2024-03-26 01:20:20 +08:00
|
|
|
|
for(uint i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
*idp=mi_set.Find(rn->ri->GetMaterialInstance());
|
|
|
|
|
++idp;
|
2023-09-07 18:09:31 +08:00
|
|
|
|
|
2024-03-26 01:20:20 +08:00
|
|
|
|
++rn;
|
|
|
|
|
}
|
2023-09-06 16:24:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 01:20:20 +08:00
|
|
|
|
vbo_mi->Unmap();
|
2023-09-06 16:24:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|