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>
|
2020-09-19 23:49:32 +08:00
|
|
|
|
#include<hgl/graph/shader/ShaderResource.h>
|
|
|
|
|
#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);
|
|
|
|
|
|
|
|
|
|
return sm;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
const bool LoadShaderDescriptor(ShaderResource *sr,const uint8 *data)
|
|
|
|
|
{
|
|
|
|
|
const uint8 count=*data++;
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
if(count<=0)return(true);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
ShaderDescriptorList *sd_list;
|
|
|
|
|
const VkDescriptorType desc_type;
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
sd_list->SetCount(count);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
ShaderDescriptor *sd=sd_list->GetData();
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
for(uint i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
sd->set=*data++;
|
|
|
|
|
sd->binding=*data++;
|
|
|
|
|
str_len=*data++;
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
memcpy(sd->name,(char *)data,str_len);
|
|
|
|
|
sd->name[str_len]=0;
|
|
|
|
|
data+=str_len;
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
sd->set_type=CheckDescriptorSetType(sd->name);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
if(sd->set_type==DescriptorSetType::Renderable)
|
|
|
|
|
{
|
|
|
|
|
if(desc_type==VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)sd->desc_type=VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;else
|
|
|
|
|
if(desc_type==VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)sd->desc_type=VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;else
|
|
|
|
|
sd->desc_type=desc_type;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sd->desc_type=desc_type;
|
|
|
|
|
}
|
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
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
return data;
|
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;
|
|
|
|
|
|
2021-09-09 18:20:17 +08:00
|
|
|
|
if(ver!=2)
|
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-09 18:20:17 +08:00
|
|
|
|
if(!LoadShaderDescriptor(sr,filedata))
|
|
|
|
|
result=false;
|
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
if(result)
|
|
|
|
|
{
|
2021-05-10 15:19:16 +08:00
|
|
|
|
mtl=device->CreateMaterial(ToUTF8String(filename),smm);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
Add(mtl);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
delete smm;
|
|
|
|
|
mtl=nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
material_by_name.Add(filename,mtl);
|
|
|
|
|
return(mtl);
|
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|