ULRE/src/SceneGraph/Vulkan/VKShaderResource.cpp

107 lines
2.4 KiB
C++
Raw Normal View History

2021-09-13 20:39:25 +08:00
#include<hgl/graph/VKShaderResource.h>
#include<hgl/graph/VKFormat.h>
2021-09-13 20:39:25 +08:00
#include<hgl/filesystem/FileSystem.h>
#include<hgl/type/Map.h>
2023-02-23 19:01:01 +08:00
#include<hgl/io/ConstBufferReader.h>
2020-06-06 19:45:38 +08:00
2020-06-09 19:40:08 +08:00
VK_NAMESPACE_BEGIN
2020-06-08 19:12:29 +08:00
2020-06-09 19:40:08 +08:00
namespace
{
ObjectMap<OSString,ShaderResource> shader_resource_by_filename;
2023-03-18 21:07:21 +08:00
const bool LoadShaderStageAttributes(ShaderAttributeArray &sa_array,io::ConstBufferReader &cbr)
2020-06-09 19:40:08 +08:00
{
2023-02-23 19:01:01 +08:00
uint count;
2020-06-08 19:12:29 +08:00
2023-02-23 19:01:01 +08:00
cbr.CastRead<uint8>(count);
2020-06-08 19:12:29 +08:00
2023-02-23 19:01:01 +08:00
if(count<=0)
return(false);
2020-06-08 19:12:29 +08:00
2023-03-18 21:07:21 +08:00
Init(&sa_array,count);
2023-03-17 21:06:05 +08:00
2023-03-18 21:07:21 +08:00
ShaderAttribute *ss=sa_array.items;
2020-06-08 19:12:29 +08:00
2020-06-09 19:40:08 +08:00
for(uint i=0;i<count;i++)
{
2023-02-23 19:01:01 +08:00
cbr.Read(ss->location);
cbr.CastRead<uint8>(ss->basetype);
cbr.CastRead<uint8>(ss->vec_size);
2020-06-08 19:12:29 +08:00
2023-02-23 19:01:01 +08:00
cbr.ReadTinyString(ss->name);
2020-06-08 19:12:29 +08:00
2023-03-17 21:06:05 +08:00
++ss;
2020-06-09 19:40:08 +08:00
}
2020-06-08 19:12:29 +08:00
2023-02-23 19:01:01 +08:00
return true;
2020-06-09 19:40:08 +08:00
}
}//namespcae
2023-03-19 20:18:10 +08:00
ShaderResource::ShaderResource(const VkShaderStageFlagBits &flag,const uint32_t *sd,const uint32 size)
2020-06-09 19:40:08 +08:00
{
stage_flag=flag;
spv_data=sd;
2023-03-17 21:06:05 +08:00
spv_size=size;
Init(stage_io);
}
ShaderResource::~ShaderResource()
{
Clear(stage_io);
2020-06-09 19:40:08 +08:00
}
2023-03-17 16:21:12 +08:00
const ShaderAttribute *ShaderResource::GetInput(const AnsiString &name) const
2020-07-09 20:37:34 +08:00
{
2023-03-17 21:06:05 +08:00
if(stage_io.input.count<=0)return(nullptr);
2020-07-09 20:37:34 +08:00
2023-03-17 21:06:05 +08:00
const ShaderAttribute *ss=stage_io.input.items;
for(uint i=0;i<stage_io.input.count;i++)
2020-07-09 20:37:34 +08:00
{
2023-03-17 21:06:05 +08:00
if(name==ss->name)
return ss;
2020-07-09 20:37:34 +08:00
++ss;
}
return nullptr;
}
2023-03-17 16:21:12 +08:00
const int ShaderResource::GetInputBinding(const AnsiString &name) const
2020-06-09 19:40:08 +08:00
{
2023-03-17 21:06:05 +08:00
if(stage_io.input.count<=0)return(-1);
const ShaderAttribute *ss=stage_io.input.items;
2020-06-09 14:05:20 +08:00
2023-03-17 21:06:05 +08:00
for(uint i=0;i<stage_io.input.count;i++)
2020-06-09 14:05:20 +08:00
{
2023-03-17 21:06:05 +08:00
if(name==ss->name)
return i;
2020-06-09 19:40:08 +08:00
++ss;
2020-06-09 14:05:20 +08:00
}
2020-06-09 19:40:08 +08:00
return -1;
}
2020-06-08 19:12:29 +08:00
2023-02-23 19:01:01 +08:00
ShaderResource *LoadShaderResource(io::ConstBufferReader &cbr)
2020-06-09 19:40:08 +08:00
{
VkShaderStageFlagBits flag;
uint32 spv_size;
2020-06-08 19:12:29 +08:00
2023-02-23 19:01:01 +08:00
cbr.CastRead<uint32>(flag);
cbr.Read(spv_size);
2020-06-09 19:40:08 +08:00
2023-03-19 20:18:10 +08:00
ShaderResource *sr=new ShaderResource(flag,(const uint32_t *)cbr.CurPointer(),spv_size);
2020-06-09 19:40:08 +08:00
2023-02-23 19:01:01 +08:00
cbr.Skip(spv_size);
2020-06-09 19:40:08 +08:00
2023-03-17 16:21:12 +08:00
LoadShaderStageAttributes(sr->GetInputs(),cbr);
// LoadShaderStageAttributes(sr->GetStageOutputs(),cbr);
2020-06-09 19:40:08 +08:00
return sr;
2020-09-17 21:49:23 +08:00
}
2020-09-11 17:11:32 +08:00
VK_NAMESPACE_END