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>
|
|
|
|
|
#include<hgl/graph/VKMaterialDescriptorSets.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>
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
|
|
|
|
|
2020-10-21 12:39:22 +08:00
|
|
|
|
const ShaderModule *RenderResource::CreateShaderModule(const OSString &filename,ShaderResource *shader_resource)
|
2020-09-19 23:49:32 +08:00
|
|
|
|
{
|
|
|
|
|
if(!device)return(nullptr);
|
|
|
|
|
if(filename.IsEmpty())return(nullptr);
|
|
|
|
|
if(!shader_resource)return(nullptr);
|
|
|
|
|
|
|
|
|
|
ShaderModule *sm;
|
|
|
|
|
|
|
|
|
|
if(shader_module_by_name.Get(filename,sm))
|
|
|
|
|
return sm;
|
|
|
|
|
|
|
|
|
|
sm=device->CreateShaderModule(shader_resource);
|
|
|
|
|
|
|
|
|
|
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-22 21:50:18 +08:00
|
|
|
|
void LoadShaderDescriptor(const uint8 *data,ShaderDescriptor *sd_list,const uint count,const uint8 ver)
|
2021-09-13 20:39:25 +08:00
|
|
|
|
{
|
|
|
|
|
ShaderDescriptor *sd=sd_list;
|
2021-09-09 18:20:17 +08:00
|
|
|
|
uint str_len;
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
for(uint i=0;i<count;i++)
|
|
|
|
|
{
|
2021-09-13 20:39:25 +08:00
|
|
|
|
sd->desc_type=VkDescriptorType(*data++);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
str_len=*data++;
|
|
|
|
|
memcpy(sd->name,(char *)data,str_len);
|
|
|
|
|
data+=str_len;
|
|
|
|
|
}
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2023-02-22 21:50:18 +08:00
|
|
|
|
if(ver==3)
|
|
|
|
|
sd->set_type =(DescriptorSetsType)*data++;
|
|
|
|
|
else
|
|
|
|
|
if(ver==2) //以下是旧的,未来不用了,现仅保证能运行
|
|
|
|
|
{
|
|
|
|
|
if(sd->name[0]=='g')sd->set_type=DescriptorSetsType::Global;else
|
|
|
|
|
if(sd->name[0]=='m')sd->set_type=DescriptorSetsType::PerMaterial;else
|
|
|
|
|
if(sd->name[0]=='r')sd->set_type=DescriptorSetsType::PerObject;else
|
|
|
|
|
sd->set_type=DescriptorSetsType::PerFrame;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 20:39:25 +08:00
|
|
|
|
sd->set =*data++;
|
|
|
|
|
sd->binding =*data++;
|
|
|
|
|
sd->stage_flag =*(uint32 *)data;
|
|
|
|
|
data+=sizeof(uint32);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2023-02-22 21:50:18 +08:00
|
|
|
|
if(sd->set_type==DescriptorSetsType::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
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
const uint8 *sp=origin_filedata;
|
2021-09-09 18:20:17 +08:00
|
|
|
|
const uint8 *end=sp+filesize;
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
if(memcmp(sp,MaterialFileHeader,MaterialFileHeaderLength)!=0)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
sp+=MaterialFileHeaderLength;
|
|
|
|
|
|
|
|
|
|
const uint8 ver=*sp;
|
|
|
|
|
++sp;
|
|
|
|
|
|
2023-02-22 21:50:18 +08:00
|
|
|
|
if(ver<2||ver>3)
|
2020-09-19 23:49:32 +08:00
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
const uint32_t shader_bits=*(uint32_t *)sp;
|
|
|
|
|
sp+=sizeof(uint32_t);
|
|
|
|
|
|
|
|
|
|
const uint count=GetShaderCountByBits(shader_bits);
|
|
|
|
|
uint32_t size;
|
|
|
|
|
|
|
|
|
|
ShaderResource *sr;
|
|
|
|
|
const ShaderModule *sm;
|
|
|
|
|
|
|
|
|
|
bool result=true;
|
|
|
|
|
ShaderModuleMap *smm=new ShaderModuleMap;
|
|
|
|
|
|
|
|
|
|
OSString shader_name;
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
size=*(uint32_t *)sp;
|
|
|
|
|
sp+=sizeof(uint32_t);
|
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
sr=LoadShaderResource(sp,size);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
sp+=size;
|
|
|
|
|
|
|
|
|
|
if(sr)
|
|
|
|
|
{
|
|
|
|
|
shader_name=filename+OS_TEXT("?")+OSString(sr->GetStageName());
|
|
|
|
|
|
|
|
|
|
sm=CreateShaderModule(shader_name,sr);
|
|
|
|
|
|
|
|
|
|
if(sm)
|
|
|
|
|
{
|
|
|
|
|
if(smm->Add(sm))
|
|
|
|
|
continue;
|
|
|
|
|
}
|
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);
|
|
|
|
|
|
2021-09-13 20:39:25 +08:00
|
|
|
|
MaterialDescriptorSets *mds=nullptr;
|
|
|
|
|
{
|
|
|
|
|
const uint8 count=*sp;
|
|
|
|
|
++sp;
|
|
|
|
|
|
|
|
|
|
if(count>0)
|
|
|
|
|
{
|
|
|
|
|
ShaderDescriptor *sd_list=hgl_zero_new<ShaderDescriptor>(count);
|
|
|
|
|
|
2023-02-22 21:50:18 +08:00
|
|
|
|
LoadShaderDescriptor(sp,sd_list,count,ver);
|
2021-09-13 20:39:25 +08:00
|
|
|
|
|
2021-09-27 20:56:50 +08:00
|
|
|
|
mds=new MaterialDescriptorSets(mtl_name,sd_list,count);
|
2021-09-13 20:39:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-09 18:20:17 +08:00
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
if(result)
|
|
|
|
|
{
|
2021-09-27 20:56:50 +08:00
|
|
|
|
mtl=device->CreateMaterial(mtl_name,smm,mds);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
Add(mtl);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-09-14 20:31:15 +08:00
|
|
|
|
SAFE_CLEAR(mds);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
delete smm;
|
|
|
|
|
mtl=nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
material_by_name.Add(filename,mtl);
|
|
|
|
|
return(mtl);
|
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|