2023-06-01 15:08:45 +08:00
|
|
|
|
#pragma once
|
2023-09-06 15:57:52 +08:00
|
|
|
|
//#include<hgl/graph/VK.h>
|
2023-06-01 15:08:45 +08:00
|
|
|
|
#include<hgl/graph/VKVertexAttribBuffer.h>
|
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
|
|
|
|
// ubo_range大致分为三档:
|
|
|
|
|
//
|
2023-06-01 21:25:50 +08:00
|
|
|
|
// 16k: Mali-T系列或更早、Mali-G71为16k
|
2023-06-01 15:08:45 +08:00
|
|
|
|
//
|
|
|
|
|
// 64k: 大部分手机与PC均为64k
|
|
|
|
|
//
|
|
|
|
|
// >64k: Intel 核显与 PowerVR 为128MB,AMD显卡为4GB,可视为随显存无上限。
|
|
|
|
|
//
|
|
|
|
|
// 我们使用uint8类型在vertex input中保存MaterialInstance ID,表示范围0-255。
|
|
|
|
|
// 所以MaterialInstance结构容量按16k/64k分为两个档次,64字节和256字节
|
|
|
|
|
|
|
|
|
|
// 如果一定要使用超过16K/64K硬件限制的容量,有两种办法
|
|
|
|
|
// 一、分多次渲染,使用UBO Offset偏移UBO数据区。
|
|
|
|
|
// 二、使用SSBO,但这样会导致性能下降,所以不推荐使用。
|
|
|
|
|
|
|
|
|
|
// 但我们不解决这个问题
|
|
|
|
|
// 我们天然要求将材质实例数据分为两个等级,同时要求一次渲染不能超过256种材质实例。
|
|
|
|
|
// 所以 UBO Range为16k时,实例数据不能超过64字节。UBO Range为64k时,实例数据不能超过256字节。
|
|
|
|
|
|
2023-09-06 15:57:52 +08:00
|
|
|
|
constexpr const uint ASSIGNS_VBO_STRIP_BYTES=2; ///<Assign VBO的每个节点的字节数
|
|
|
|
|
|
2023-06-01 15:08:45 +08:00
|
|
|
|
/*
|
|
|
|
|
* 渲染节点额外提供的数据
|
|
|
|
|
*/
|
2023-09-05 20:48:47 +08:00
|
|
|
|
struct RenderAssignBuffer
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
uint node_count; ///<渲染节点数量
|
2023-09-05 10:28:03 +08:00
|
|
|
|
// uint mi_count; ///<材质实例数量
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-09-05 10:28:03 +08:00
|
|
|
|
//uint mi_size; ///<单个材质实例数量长度
|
|
|
|
|
//DeviceBuffer *mi_data_buffer; ///<材质实例数据UBO/SSBO
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-09-05 10:28:03 +08:00
|
|
|
|
//VBO *mi_id;
|
|
|
|
|
//VkBuffer mi_id_buffer;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-06-01 21:25:50 +08:00
|
|
|
|
// VBO *bone_id,*bone_weight;
|
|
|
|
|
// VkBuffer bone_id_buffer,bone_weight_buffer;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-09-05 20:19:53 +08:00
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
//Assign UBO
|
2023-09-06 15:57:52 +08:00
|
|
|
|
DeviceBuffer *ubo_l2w;
|
|
|
|
|
DeviceBuffer *ubo_mi;
|
2023-09-05 20:19:53 +08:00
|
|
|
|
|
|
|
|
|
//Assign VBO
|
2023-09-06 15:57:52 +08:00
|
|
|
|
VBO *vbo_assigns; ///<RG16UI格式的VertexInputStream,,,,X:L2W ID,Y:MI ID
|
2023-09-05 20:19:53 +08:00
|
|
|
|
|
2023-06-01 15:08:45 +08:00
|
|
|
|
public:
|
|
|
|
|
|
2023-09-05 20:48:47 +08:00
|
|
|
|
RenderAssignBuffer()
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
hgl_zero(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 20:48:47 +08:00
|
|
|
|
~RenderAssignBuffer()
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 21:25:50 +08:00
|
|
|
|
void ClearNode()
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
2023-09-06 15:57:52 +08:00
|
|
|
|
SAFE_CLEAR(ubo_l2w);
|
|
|
|
|
SAFE_CLEAR(ubo_mi);
|
|
|
|
|
SAFE_CLEAR(vbo_assigns);
|
2023-09-05 20:19:53 +08:00
|
|
|
|
|
2023-06-01 15:08:45 +08:00
|
|
|
|
node_count=0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 10:28:03 +08:00
|
|
|
|
//void ClearMI()
|
|
|
|
|
//{
|
|
|
|
|
// SAFE_CLEAR(mi_id)
|
|
|
|
|
// SAFE_CLEAR(mi_data_buffer);
|
|
|
|
|
// mi_count=0;
|
|
|
|
|
// mi_size=0;
|
|
|
|
|
//}
|
2023-06-01 21:25:50 +08:00
|
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
|
{
|
|
|
|
|
ClearNode();
|
2023-09-05 10:28:03 +08:00
|
|
|
|
// ClearMI();
|
2023-06-01 21:25:50 +08:00
|
|
|
|
|
|
|
|
|
// SAFE_CLEAR(bone_id)
|
|
|
|
|
// SAFE_CLEAR(bone_weight)
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 15:08:45 +08:00
|
|
|
|
void NodeAlloc(GPUDevice *dev,const uint c)
|
|
|
|
|
{
|
2023-06-01 21:25:50 +08:00
|
|
|
|
ClearNode();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
node_count=power_to_2(c);
|
|
|
|
|
|
2023-09-06 15:57:52 +08:00
|
|
|
|
ubo_l2w=dev->CreateUBO(node_count*sizeof(Matrix4f));
|
|
|
|
|
//ubo_mi=dev->CreateUBO(node_count*sizeof(uint8));
|
|
|
|
|
vbo_assigns=dev->CreateVBO(VF_V1U16,node_count);
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 10:28:03 +08:00
|
|
|
|
//void MIAlloc(GPUDevice *dev,const uint c,const uint mis)
|
|
|
|
|
//{
|
|
|
|
|
// ClearMI();
|
|
|
|
|
// if(c<=0||mi_size<=0)return;
|
|
|
|
|
//
|
|
|
|
|
// mi_count=power_to_2(c);
|
|
|
|
|
// mi_size=mis;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-09-05 10:28:03 +08:00
|
|
|
|
// mi_id=dev->CreateVBO(VF_V1U8,mi_count);
|
|
|
|
|
// mi_id_buffer=mi_id->GetBuffer();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-09-05 10:28:03 +08:00
|
|
|
|
// mi_data_buffer=dev->CreateUBO(mi_count*mi_size);
|
|
|
|
|
//}
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-06-01 21:25:50 +08:00
|
|
|
|
void WriteLocalToWorld(RenderNode *render_node,const uint count)
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
RenderNode *rn;
|
|
|
|
|
|
2023-09-05 20:19:53 +08:00
|
|
|
|
//new l2w array in ubo
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
2023-09-06 15:57:52 +08:00
|
|
|
|
Matrix4f *tp=(hgl::Matrix4f *)(ubo_l2w->Map());
|
|
|
|
|
uint16 *idp=(uint16 *)(vbo_assigns->Map());
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
rn=render_node;
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<count;i++)
|
|
|
|
|
{
|
2023-09-05 20:19:53 +08:00
|
|
|
|
*tp=rn->local_to_world;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
++tp;
|
2023-09-05 20:19:53 +08:00
|
|
|
|
|
|
|
|
|
*idp=i;
|
|
|
|
|
++idp;
|
|
|
|
|
|
2023-06-01 15:08:45 +08:00
|
|
|
|
++rn;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 15:57:52 +08:00
|
|
|
|
vbo_assigns->Unmap();
|
|
|
|
|
ubo_l2w->Unmap();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-01 21:25:50 +08:00
|
|
|
|
|
2023-09-05 10:28:03 +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();
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2023-09-05 20:48:47 +08:00
|
|
|
|
};//struct RenderAssignBuffer
|
2023-06-01 15:08:45 +08:00
|
|
|
|
VK_NAMESPACE_END
|