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; VkDescriptorType desc_type;
DescriptorSetType set_type; DescriptorSetType set_type;
uint32_t set; uint8 set;
uint32_t binding; uint8 binding;
uint32_t stage_flag; uint32 stage_flag;
}; };
using ShaderDescriptorList=List<ShaderDescriptor *>; using ShaderDescriptorList=List<ShaderDescriptor *>;

View File

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

View File

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

View File

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