LoadMaterial use ConstBufferReader

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-02-23 19:01:01 +08:00
parent 6536b715c3
commit 0abe38de98
6 changed files with 50 additions and 63 deletions

2
CMCore

@ -1 +1 @@
Subproject commit ac5931ce2602d45149e1c8baacf0805fa39f73f6
Subproject commit ba7176099f0fed43becb0e5bb62bc061eec2b8a5

View File

@ -11,9 +11,9 @@ struct ShaderDescriptor
VkDescriptorType desc_type;
DescriptorSetType set_type;
uint32_t set;
uint32_t binding;
uint32_t stage_flag;
uint8 set;
uint8 binding;
uint32 stage_flag;
};
using ShaderDescriptorList=List<ShaderDescriptor *>;

View File

@ -39,9 +39,9 @@ class RenderResource
IDResManage<MaterialID, Material> rm_material; ///<材质合集
IDResManage<MaterialInstanceID, MaterialInstance> rm_material_instance; ///<材质实例合集
IDResManage<DescriptorSetID, DescriptorSet> rm_desc_sets; ///<描述符合集
IDResManage<DescriptorSetID, DescriptorSet> rm_desc_sets; ///<描述符合集
IDResManage<PrimitiveID, Primitive> rm_primitives; ///<图元合集
IDResManage<BufferID, DeviceBuffer> rm_buffers; ///<顶点缓冲区合集
IDResManage<BufferID, DeviceBuffer> rm_buffers; ///<顶点缓冲区合集
IDResManage<SamplerID, Sampler> rm_samplers; ///<采样器合集
IDResManage<TextureID, Texture> rm_textures; ///<纹理合集
IDResManage<RenderableID, Renderable> rm_renderables; ///<渲染实例集合集

View File

@ -5,13 +5,14 @@
#include<hgl/type/List.h>
#include<hgl/type/StringList.h>
#include<hgl/graph/VK.h>
#include<hgl/io/ConstBufferReader.h>
VK_NAMESPACE_BEGIN
struct ShaderStage
{
AnsiString name;
uint location;
uint8 location;
VertexAttribType type; ///<成份数量(如vec4中的4)
@ -51,7 +52,7 @@ public:
const int GetStageInputBinding(const AnsiString &)const;
};//class ShaderResource
ShaderResource *LoadShaderResource(const uint8 *origin_filedata,const int64 filesize);
ShaderResource *LoadShaderResource(io::ConstBufferReader &);
struct ShaderModuleCreateInfo:public vkstruct_flag<VkShaderModuleCreateInfo,VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO>
{

View File

@ -6,6 +6,7 @@
#include<hgl/graph/VKMaterialDescriptorSets.h>
#include<hgl/filesystem/FileSystem.h>
#include<hgl/graph/VKRenderResource.h>
#include<hgl/io/ConstBufferReader.h>
VK_NAMESPACE_BEGIN
@ -40,23 +41,17 @@ const ShaderModule *RenderResource::CreateShaderModule(const OSString &filename,
return sm;
}
void LoadShaderDescriptor(const uint8 *data,ShaderDescriptor *sd_list,const uint count,const uint8 ver)
void LoadShaderDescriptor(io::ConstBufferReader &cbr,ShaderDescriptor *sd_list,const uint count,const uint8 ver)
{
ShaderDescriptor *sd=sd_list;
uint str_len;
for(uint i=0;i<count;i++)
{
sd->desc_type=VkDescriptorType(*data++);
{
str_len=*data++;
memcpy(sd->name,(char *)data,str_len);
data+=str_len;
}
cbr.CastRead<uint8>(sd->desc_type);
cbr.ReadTinyString(sd->name);
if(ver==3)
sd->set_type =(DescriptorSetType)*data++;
cbr.CastRead<uint8>(sd->set_type);
else
if(ver==2) //以下是旧的,未来不用了,现仅保证能运行
{
@ -66,10 +61,9 @@ void LoadShaderDescriptor(const uint8 *data,ShaderDescriptor *sd_list,const uint
sd->set_type=DescriptorSetType::PerFrame;
}
sd->set =*data++;
sd->binding =*data++;
sd->stage_flag =*(uint32 *)data;
data+=sizeof(uint32);
cbr.Read(sd->set);
cbr.Read(sd->binding);
cbr.Read(sd->stage_flag);
if(sd->set_type==DescriptorSetType::PerObject)
{
@ -105,22 +99,23 @@ Material *RenderResource::CreateMaterial(const OSString &filename)
if(filesize<MaterialFileHeaderLength)
return(nullptr);
const uint8 *sp=origin_filedata;
const uint8 *end=sp+filesize;
io::ConstBufferReader cbr(filedata,filesize);
if(memcmp(sp,MaterialFileHeader,MaterialFileHeaderLength)!=0)
if(memcmp(cbr.CurPointer(),MaterialFileHeader,MaterialFileHeaderLength)!=0)
return(nullptr);
sp+=MaterialFileHeaderLength;
cbr.Skip(MaterialFileHeaderLength);
const uint8 ver=*sp;
++sp;
uint8 ver;
cbr.Read(ver);
if(ver<2||ver>3)
return(nullptr);
const uint32_t shader_bits=*(uint32_t *)sp;
sp+=sizeof(uint32_t);
uint32_t shader_bits;
cbr.Read(shader_bits);
const uint count=GetShaderCountByBits(shader_bits);
uint32_t size;
@ -135,11 +130,10 @@ Material *RenderResource::CreateMaterial(const OSString &filename)
for(uint i=0;i<count;i++)
{
size=*(uint32_t *)sp;
sp+=sizeof(uint32_t);
cbr.Read(size);
sr=LoadShaderResource(sp,size);
sp+=size;
sr=LoadShaderResource(io::ConstBufferReader(cbr,size)); //有一个stage output没读放弃了所以不能直接用上一级的cbr
cbr.Skip(size);
if(sr)
{
@ -162,14 +156,14 @@ Material *RenderResource::CreateMaterial(const OSString &filename)
MaterialDescriptorSets *mds=nullptr;
{
const uint8 count=*sp;
++sp;
uint8 count;
cbr.Read(count);
if(count>0)
{
ShaderDescriptor *sd_list=new ShaderDescriptor[count];
LoadShaderDescriptor(sp,sd_list,count,ver);
LoadShaderDescriptor(cbr,sd_list,count,ver);
mds=new MaterialDescriptorSets(mtl_name,sd_list,count);
}

View File

@ -2,23 +2,22 @@
#include<hgl/graph/VKFormat.h>
#include<hgl/filesystem/FileSystem.h>
#include<hgl/type/Map.h>
#include<hgl/io/ConstBufferReader.h>
VK_NAMESPACE_BEGIN
#define AccessByPointer(data,type) *(type *)data;data+=sizeof(type);
namespace
{
ObjectMap<OSString,ShaderResource> shader_resource_by_filename;
const uint8 *LoadShaderStages(ShaderStageList &ss_list,const uint8 *data)
const bool LoadShaderStages(ShaderStageList &ss_list,io::ConstBufferReader &cbr)
{
const uint count=*data++;
uint count;
cbr.CastRead<uint8>(count);
if(count<=0)
return(data);
int str_len;
return(false);
ShaderStage *ss;
@ -26,18 +25,16 @@ VK_NAMESPACE_BEGIN
{
ss=new ShaderStage;
ss->location =*data++;
ss->type.basetype =(VertexAttribBaseType)*data++;
ss->type.vec_size =*data++;
cbr.Read(ss->location);
cbr.CastRead<uint8>(ss->type.basetype);
cbr.CastRead<uint8>(ss->type.vec_size);
str_len=*data++;
ss->name.SetString((char *)data,str_len);
data+=str_len;
cbr.ReadTinyString(ss->name);
ss_list.Add(ss);
}
return data;
return true;
}
}//namespcae
@ -108,25 +105,20 @@ VK_NAMESPACE_BEGIN
return -1;
}
ShaderResource *LoadShaderResource(const uint8 *origin_filedata,const int64 filesize)
ShaderResource *LoadShaderResource(io::ConstBufferReader &cbr)
{
if(!origin_filedata)return(nullptr);
const uint8 *filedata=origin_filedata;
const uint8 *file_end=filedata+filesize;
VkShaderStageFlagBits flag;
uint32 spv_size;
flag =(const VkShaderStageFlagBits)AccessByPointer(filedata,uint32);
spv_size=AccessByPointer(filedata,uint32);
cbr.CastRead<uint32>(flag);
cbr.Read(spv_size);
ShaderResource *sr=new ShaderResource(flag,filedata,spv_size);
ShaderResource *sr=new ShaderResource(flag,cbr.CurPointer(),spv_size);
filedata+=spv_size;
cbr.Skip(spv_size);
filedata=LoadShaderStages(sr->GetStageInputs(),filedata);
//filedata=LoadShaderStages(sr->GetStageOutputs(),filedata);
LoadShaderStages(sr->GetStageInputs(),cbr);
// LoadShaderStages(sr->GetStageOutputs(),cbr);
return sr;
}