GLSLCompiler/VKShaderParse.h

74 lines
2.2 KiB
C
Raw Normal View History

2020-08-22 22:03:21 +08:00
#pragma once
2020-08-23 18:18:38 +08:00
#include"SPIRV-Cross/spirv_cross.hpp"
#include<string>
2020-08-22 22:03:21 +08:00
using SPVResVector=spirv_cross::SmallVector<spirv_cross::Resource>;
class ShaderParse
{
spirv_cross::Compiler *compiler;
spirv_cross::ShaderResources resource;
public:
ShaderParse(const void *spv_data,const uint32_t spv_size)
{
compiler=new spirv_cross::Compiler((const uint32_t *)spv_data,spv_size/sizeof(uint32_t));
resource=compiler->get_shader_resources();
}
~ShaderParse()
{
delete compiler;
}
#define SHADER_PARSE_GET_RESOURCE(name,buf_name) const SPVResVector &Get##name()const{return resource.buf_name;}
SHADER_PARSE_GET_RESOURCE(UBO, uniform_buffers)
SHADER_PARSE_GET_RESOURCE(SSBO, storage_buffers)
SHADER_PARSE_GET_RESOURCE(StageInputs, stage_inputs)
SHADER_PARSE_GET_RESOURCE(StageOutputs, stage_outputs)
SHADER_PARSE_GET_RESOURCE(Sampler, sampled_images)
SHADER_PARSE_GET_RESOURCE(Subpass, subpass_inputs)
//SmallVector<Resource> storage_images;
//SmallVector<Resource> atomic_counters;
//SmallVector<Resource> acceleration_structures;
//SmallVector<Resource> push_constant_buffers;
//SmallVector<Resource> separate_images;
//SmallVector<Resource> separate_samplers;
#undef SHADER_PARSE_GET_RESOURCE
public:
2020-08-23 18:18:38 +08:00
const std::string &GetName(const spirv_cross::Resource &res)const
{
return compiler->get_name(res.id);
}
const uint32_t GetSet(const spirv_cross::Resource &res)const
2020-08-22 22:03:21 +08:00
{
2020-08-23 18:18:38 +08:00
return compiler->get_decoration(res.id,spv::DecorationDescriptorSet);
2020-08-22 22:03:21 +08:00
}
const uint32_t GetBinding(const spirv_cross::Resource &res)const
{
return compiler->get_decoration(res.id,spv::DecorationBinding);
}
const uint32_t GetLocation(const spirv_cross::Resource &res)const
{
return compiler->get_decoration(res.id,spv::DecorationLocation);
}
2020-08-23 18:18:38 +08:00
void GetFormat(const spirv_cross::Resource &res,spirv_cross::SPIRType::BaseType *base_type,uint8_t *vecsize)
2020-08-22 22:03:21 +08:00
{
const spirv_cross::SPIRType &type=compiler->get_type(res.type_id);
*base_type =type.basetype;
*vecsize =type.vecsize;
}
};//class ShaderParse