2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKShaderModule.h>
|
|
|
|
|
#include<hgl/graph/VKMaterial.h>
|
|
|
|
|
#include<hgl/graph/VKDevice.h>
|
|
|
|
|
#include<hgl/graph/VKShaderModuleMap.h>
|
2021-09-13 20:39:25 +08:00
|
|
|
|
#include<hgl/graph/VKShaderResource.h>
|
2023-03-20 13:55:47 +08:00
|
|
|
|
#include<hgl/graph/VKMaterialDescriptorManager.h>
|
2023-03-19 19:41:21 +08:00
|
|
|
|
#include<hgl/graph/VKVertexInput.h>
|
2020-09-19 23:49:32 +08:00
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
2020-10-21 12:39:22 +08:00
|
|
|
|
#include<hgl/graph/VKRenderResource.h>
|
2023-02-23 19:01:01 +08:00
|
|
|
|
#include<hgl/io/ConstBufferReader.h>
|
2023-03-17 21:06:05 +08:00
|
|
|
|
#include<hgl/shadergen/MaterialCreateInfo.h>
|
2023-03-20 22:09:37 +08:00
|
|
|
|
#include<hgl/shadergen/ShaderDescriptorInfo.h>
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
|
|
|
|
|
2023-03-19 20:18:56 +08:00
|
|
|
|
const ShaderModule *RenderResource::CreateShaderModule(const OSString &filename,VkShaderStageFlagBits shader_stage,const uint32_t *spv_data,const size_t spv_size)
|
2020-09-19 23:49:32 +08:00
|
|
|
|
{
|
|
|
|
|
if(!device)return(nullptr);
|
|
|
|
|
if(filename.IsEmpty())return(nullptr);
|
2023-03-19 19:41:21 +08:00
|
|
|
|
if(!spv_data)return(nullptr);
|
|
|
|
|
if(spv_size<4)return(nullptr);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
ShaderModule *sm;
|
|
|
|
|
|
|
|
|
|
if(shader_module_by_name.Get(filename,sm))
|
|
|
|
|
return sm;
|
|
|
|
|
|
2023-03-19 19:41:21 +08:00
|
|
|
|
sm=device->CreateShaderModule(shader_stage,spv_data,spv_size);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
shader_module_by_name.Add(filename,sm);
|
|
|
|
|
|
2022-09-29 18:29:21 +08:00
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
{
|
|
|
|
|
auto da=device->GetDeviceAttribute();
|
|
|
|
|
const UTF8String sn=ToUTF8String(filename);
|
|
|
|
|
|
|
|
|
|
if(da->debug_maker)
|
|
|
|
|
da->debug_maker->SetShaderModule(*sm,sn);
|
|
|
|
|
|
|
|
|
|
if(da->debug_utils)
|
|
|
|
|
da->debug_utils->SetShaderModule(*sm,sn);
|
|
|
|
|
}
|
|
|
|
|
#endif//_DEBUG
|
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
return sm;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 19:01:01 +08:00
|
|
|
|
void LoadShaderDescriptor(io::ConstBufferReader &cbr,ShaderDescriptor *sd_list,const uint count,const uint8 ver)
|
2021-09-13 20:39:25 +08:00
|
|
|
|
{
|
|
|
|
|
ShaderDescriptor *sd=sd_list;
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
for(uint i=0;i<count;i++)
|
|
|
|
|
{
|
2023-02-23 19:01:01 +08:00
|
|
|
|
cbr.CastRead<uint8>(sd->desc_type);
|
|
|
|
|
cbr.ReadTinyString(sd->name);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2023-02-22 21:50:18 +08:00
|
|
|
|
if(ver==3)
|
2023-02-23 19:01:01 +08:00
|
|
|
|
cbr.CastRead<uint8>(sd->set_type);
|
2023-02-22 21:50:18 +08:00
|
|
|
|
else
|
|
|
|
|
if(ver==2) //以下是旧的,未来不用了,现仅保证能运行
|
|
|
|
|
{
|
2023-02-22 21:53:51 +08:00
|
|
|
|
if(sd->name[0]=='g')sd->set_type=DescriptorSetType::Global;else
|
2023-03-04 18:21:03 +08:00
|
|
|
|
if(sd->name[0]=='m')sd->set_type=DescriptorSetType::PerMaterial;else
|
2023-02-22 21:53:51 +08:00
|
|
|
|
if(sd->name[0]=='r')sd->set_type=DescriptorSetType::PerObject;else
|
|
|
|
|
sd->set_type=DescriptorSetType::PerFrame;
|
2023-02-22 21:50:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 19:01:01 +08:00
|
|
|
|
cbr.Read(sd->set);
|
|
|
|
|
cbr.Read(sd->binding);
|
|
|
|
|
cbr.Read(sd->stage_flag);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2023-03-23 21:43:10 +08:00
|
|
|
|
if(sd->set_type>=DescriptorSetType::PerObject)
|
2021-09-09 18:20:17 +08:00
|
|
|
|
{
|
2021-09-13 20:39:25 +08:00
|
|
|
|
if(sd->desc_type==VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)sd->desc_type=VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;else
|
|
|
|
|
if(sd->desc_type==VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)sd->desc_type=VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;
|
2021-09-09 18:20:17 +08:00
|
|
|
|
}
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
++sd;
|
|
|
|
|
}
|
2020-09-19 23:49:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-02 20:19:25 +08:00
|
|
|
|
ShaderResource *LoadShaderResource(io::ConstBufferReader &cbr);
|
|
|
|
|
|
2020-10-21 12:39:22 +08:00
|
|
|
|
Material *RenderResource::CreateMaterial(const OSString &filename)
|
2020-09-19 23:49:32 +08:00
|
|
|
|
{
|
|
|
|
|
Material *mtl;
|
|
|
|
|
|
|
|
|
|
if(material_by_name.Get(filename,mtl))
|
|
|
|
|
return mtl;
|
|
|
|
|
|
|
|
|
|
constexpr char MaterialFileHeader[]=u8"Material\x1A";
|
|
|
|
|
constexpr uint MaterialFileHeaderLength=sizeof(MaterialFileHeader)-1;
|
|
|
|
|
|
|
|
|
|
int64 filesize;
|
2020-10-24 19:12:32 +08:00
|
|
|
|
uint8 *filedata=(uint8 *)filesystem::LoadFileToMemory(filename+OS_TEXT(".material"),filesize);
|
|
|
|
|
|
|
|
|
|
if(!filedata)
|
|
|
|
|
{
|
|
|
|
|
material_by_name.Add(filename,nullptr);
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AutoDeleteArray<uint8> origin_filedata(filedata,filesize);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
if(filesize<MaterialFileHeaderLength)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
2023-02-23 19:01:01 +08:00
|
|
|
|
io::ConstBufferReader cbr(filedata,filesize);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2023-02-23 19:01:01 +08:00
|
|
|
|
if(memcmp(cbr.CurPointer(),MaterialFileHeader,MaterialFileHeaderLength)!=0)
|
2020-09-19 23:49:32 +08:00
|
|
|
|
return(nullptr);
|
|
|
|
|
|
2023-02-23 19:01:01 +08:00
|
|
|
|
cbr.Skip(MaterialFileHeaderLength);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2023-02-23 19:01:01 +08:00
|
|
|
|
uint8 ver;
|
|
|
|
|
|
|
|
|
|
cbr.Read(ver);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2023-02-22 21:50:18 +08:00
|
|
|
|
if(ver<2||ver>3)
|
2020-09-19 23:49:32 +08:00
|
|
|
|
return(nullptr);
|
|
|
|
|
|
2023-02-23 19:01:01 +08:00
|
|
|
|
uint32_t shader_bits;
|
|
|
|
|
|
|
|
|
|
cbr.Read(shader_bits);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
const uint count=GetShaderCountByBits(shader_bits);
|
|
|
|
|
uint32_t size;
|
|
|
|
|
|
|
|
|
|
ShaderResource *sr;
|
|
|
|
|
const ShaderModule *sm;
|
|
|
|
|
|
|
|
|
|
bool result=true;
|
|
|
|
|
ShaderModuleMap *smm=new ShaderModuleMap;
|
2023-03-19 19:41:21 +08:00
|
|
|
|
VertexInput *vertex_input=nullptr;
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
OSString shader_name;
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<count;i++)
|
|
|
|
|
{
|
2023-02-23 19:01:01 +08:00
|
|
|
|
cbr.Read(size);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2023-02-23 19:01:01 +08:00
|
|
|
|
sr=LoadShaderResource(io::ConstBufferReader(cbr,size)); //有一个stage output没读,放弃了,所以不能直接用上一级的cbr
|
|
|
|
|
cbr.Skip(size);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
if(sr)
|
|
|
|
|
{
|
2023-03-17 16:21:12 +08:00
|
|
|
|
shader_name=filename+OS_TEXT("?")+ToOSString(sr->GetStageName());
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2023-03-19 20:23:54 +08:00
|
|
|
|
sm=CreateShaderModule(shader_name,sr->GetStage(),sr->GetSPVData(),sr->GetSPVSize());
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
if(sm)
|
|
|
|
|
{
|
|
|
|
|
if(smm->Add(sm))
|
2023-03-19 19:41:21 +08:00
|
|
|
|
{
|
|
|
|
|
if(sr->GetStage()==VK_SHADER_STAGE_VERTEX_BIT)
|
|
|
|
|
vertex_input=new VertexInput(sr->GetInputs());
|
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
continue;
|
2023-03-19 19:41:21 +08:00
|
|
|
|
}
|
2020-09-19 23:49:32 +08:00
|
|
|
|
}
|
2021-09-09 18:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
result=false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 20:56:50 +08:00
|
|
|
|
const UTF8String mtl_name=ToUTF8String(filename);
|
|
|
|
|
|
2023-03-21 21:46:16 +08:00
|
|
|
|
MaterialDescriptorManager *desc_manager=nullptr;
|
2021-09-13 20:39:25 +08:00
|
|
|
|
{
|
2023-02-23 19:01:01 +08:00
|
|
|
|
uint8 count;
|
|
|
|
|
cbr.Read(count);
|
2021-09-13 20:39:25 +08:00
|
|
|
|
|
|
|
|
|
if(count>0)
|
|
|
|
|
{
|
2023-02-23 13:25:05 +08:00
|
|
|
|
ShaderDescriptor *sd_list=new ShaderDescriptor[count];
|
2021-09-13 20:39:25 +08:00
|
|
|
|
|
2023-02-23 19:01:01 +08:00
|
|
|
|
LoadShaderDescriptor(cbr,sd_list,count,ver);
|
2021-09-13 20:39:25 +08:00
|
|
|
|
|
2023-03-21 21:46:16 +08:00
|
|
|
|
desc_manager=new MaterialDescriptorManager(mtl_name,sd_list,count);
|
2023-03-19 20:41:12 +08:00
|
|
|
|
|
|
|
|
|
delete[] sd_list;
|
2021-09-13 20:39:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-09 18:20:17 +08:00
|
|
|
|
|
2023-03-19 19:41:21 +08:00
|
|
|
|
if(result&&vertex_input)
|
2020-09-19 23:49:32 +08:00
|
|
|
|
{
|
2023-03-21 21:46:16 +08:00
|
|
|
|
mtl=device->CreateMaterial(mtl_name,smm,desc_manager,vertex_input);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
Add(mtl);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-03-21 21:46:16 +08:00
|
|
|
|
SAFE_CLEAR(desc_manager);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
delete smm;
|
|
|
|
|
mtl=nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
material_by_name.Add(filename,mtl);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
return(mtl);
|
2023-03-17 21:06:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 18:05:27 +08:00
|
|
|
|
Material *RenderResource::CreateMaterial(const MaterialCreateInfo *mci)
|
2023-03-17 21:06:05 +08:00
|
|
|
|
{
|
|
|
|
|
if(!mci)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
Material *mtl;
|
|
|
|
|
|
|
|
|
|
const OSString mtl_name=ToOSString(mci->GetName());
|
|
|
|
|
|
|
|
|
|
if(material_by_name.Get(mtl_name,mtl))
|
|
|
|
|
return mtl;
|
|
|
|
|
|
2023-03-17 21:06:05 +08:00
|
|
|
|
const uint count=GetShaderCountByBits(mci->GetShaderStage());
|
2023-03-19 20:18:56 +08:00
|
|
|
|
const ShaderModule *sm;
|
|
|
|
|
|
2023-03-17 21:06:05 +08:00
|
|
|
|
ShaderModuleMap *smm=new ShaderModuleMap;
|
2023-03-19 20:18:56 +08:00
|
|
|
|
VertexInput *vertex_input=nullptr;
|
|
|
|
|
|
|
|
|
|
const ShaderCreateInfoVertex *vert=mci->GetVS();
|
|
|
|
|
|
|
|
|
|
if(vert)
|
|
|
|
|
{
|
|
|
|
|
sm=CreateShaderModule( mtl_name+OS_TEXT("?Vertex"),
|
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT,
|
2023-03-19 20:23:54 +08:00
|
|
|
|
vert->GetSPVData(),vert->GetSPVSize());
|
2023-03-19 20:18:56 +08:00
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
if(!sm)
|
|
|
|
|
return(false);
|
2023-03-19 20:18:56 +08:00
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
if(smm->Add(sm))
|
|
|
|
|
vertex_input=new VertexInput(vert->sdm->GetShaderStageIO().input);
|
2023-03-19 20:18:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ShaderCreateInfoGeometry *geom=mci->GetGS();
|
|
|
|
|
|
|
|
|
|
if(geom)
|
|
|
|
|
{
|
|
|
|
|
sm=CreateShaderModule( mtl_name+OS_TEXT("?Geometry"),
|
|
|
|
|
VK_SHADER_STAGE_GEOMETRY_BIT,
|
2023-03-19 20:23:54 +08:00
|
|
|
|
geom->GetSPVData(),geom->GetSPVSize());
|
2023-03-17 21:06:05 +08:00
|
|
|
|
|
2023-03-19 20:18:56 +08:00
|
|
|
|
smm->Add(sm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ShaderCreateInfoFragment *frag=mci->GetFS();
|
|
|
|
|
|
|
|
|
|
if(frag)
|
|
|
|
|
{
|
|
|
|
|
sm=CreateShaderModule( mtl_name+OS_TEXT("?Fragment"),
|
|
|
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
2023-03-19 20:23:54 +08:00
|
|
|
|
frag->GetSPVData(),frag->GetSPVSize());
|
2023-03-19 20:18:56 +08:00
|
|
|
|
|
|
|
|
|
smm->Add(sm);
|
|
|
|
|
}
|
2023-03-17 21:06:05 +08:00
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
MaterialDescriptorManager *mdm=nullptr;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
const auto &mdi=mci->GetMDI();
|
|
|
|
|
|
|
|
|
|
if(mdi.GetCount()>0)
|
|
|
|
|
mdm=new MaterialDescriptorManager(mci->GetName(),mdi.Get());
|
|
|
|
|
}
|
2023-03-17 21:06:05 +08:00
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
mtl=device->CreateMaterial(mci->GetName(),smm,mdm,vertex_input);
|
2023-03-20 22:09:37 +08:00
|
|
|
|
|
|
|
|
|
if(!mtl)
|
|
|
|
|
{
|
|
|
|
|
delete mdm;
|
|
|
|
|
delete smm;
|
|
|
|
|
}
|
2023-03-21 18:05:27 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Add(mtl);
|
|
|
|
|
}
|
2023-03-20 22:09:37 +08:00
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
material_by_name.Add(mtl_name,mtl);
|
2023-03-20 22:09:37 +08:00
|
|
|
|
return mtl;
|
2020-09-19 23:49:32 +08:00
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|