Compare commits

...

10 Commits

4 changed files with 104 additions and 56 deletions

View File

@ -45,3 +45,7 @@ set(VULKAN_SPIRV_LIBS GenericCodeGen
add_library(GLSLCompiler SHARED glsl2spv.cpp VKShaderParse.h) add_library(GLSLCompiler SHARED glsl2spv.cpp VKShaderParse.h)
target_link_libraries(GLSLCompiler PRIVATE ${VULKAN_SPIRV_LIBS}) target_link_libraries(GLSLCompiler PRIVATE ${VULKAN_SPIRV_LIBS})
add_library(GLSLCompilerStatic STATIC glsl2spv.cpp VKShaderParse.h)
target_link_libraries(GLSLCompilerStatic PRIVATE ${VULKAN_SPIRV_LIBS})

@ -1 +1 @@
Subproject commit ac5524ef523e663e19493e77013873391e866340 Subproject commit 12542fc6fc05000e04742daf93892a0b10edbe80

View File

@ -49,7 +49,12 @@ public:
return compiler->get_name(res.id); return compiler->get_name(res.id);
} }
const uint32_t GetSet(const spirv_cross::Resource &res)const const uint32_t GetIndex(const spirv_cross::Resource &res)const
{
return compiler->get_decoration(res.id,spv::DecorationIndex);
}
const uint32_t GetDescriptorSet(const spirv_cross::Resource &res)const
{ {
return compiler->get_decoration(res.id,spv::DecorationDescriptorSet); return compiler->get_decoration(res.id,spv::DecorationDescriptorSet);
} }
@ -64,6 +69,16 @@ public:
return compiler->get_decoration(res.id,spv::DecorationLocation); return compiler->get_decoration(res.id,spv::DecorationLocation);
} }
const uint32_t GetOffset(const spirv_cross::Resource& res)const
{
return compiler->get_decoration(res.id,spv::DecorationOffset);
}
const uint32_t GetInputAttachmentIndex(const spirv_cross::Resource& res)const
{
return compiler->get_decoration(res.id,spv::DecorationInputAttachmentIndex);
}
void GetFormat(const spirv_cross::Resource &res,spirv_cross::SPIRType::BaseType *base_type,uint8_t *vecsize) void GetFormat(const spirv_cross::Resource &res,spirv_cross::SPIRType::BaseType *base_type,uint8_t *vecsize)
{ {
const spirv_cross::SPIRType &type=compiler->get_type(res.type_id); const spirv_cross::SPIRType &type=compiler->get_type(res.type_id);
@ -72,18 +87,8 @@ public:
*vecsize =type.vecsize; *vecsize =type.vecsize;
} }
const uint32_t GetOffset(const spirv_cross::Resource& res)const
{
return compiler->get_decoration(res.id,spv::DecorationOffset);
}
const uint32_t GetBufferSize(const spirv_cross::Resource& res)const const uint32_t GetBufferSize(const spirv_cross::Resource& res)const
{ {
return (uint32_t)(compiler->get_declared_struct_size(compiler->get_type(res.base_type_id))); return (uint32_t)(compiler->get_declared_struct_size(compiler->get_type(res.base_type_id)));
} }
const uint32_t GetInputAttachmentIndex(const spirv_cross::Resource& res)const
{
return compiler->get_decoration(res.id,spv::DecorationInputAttachmentIndex);
}
};//class ShaderParse };//class ShaderParse

View File

@ -199,6 +199,9 @@ struct CompileInfo
uint32_t includes_count; uint32_t includes_count;
const char ** includes; const char ** includes;
const char * preamble; const char * preamble;
const uint32_t vulkan_version;
const uint32_t spv_version;
}; };
enum class VertexAttribBaseType enum class VertexAttribBaseType
@ -241,20 +244,20 @@ char *new_strcpy(const char *src)
return str; return str;
} }
constexpr size_t SHADER_RESOURCE_NAME_MAX_LENGTH=128; constexpr size_t SHADER_RESOURCE_NAME_MAX_LENGTH=32;
struct ShaderStage struct ShaderAttribute
{ {
char name[SHADER_RESOURCE_NAME_MAX_LENGTH]; char name[SHADER_RESOURCE_NAME_MAX_LENGTH];
uint8_t location; uint8_t location;
uint32_t basetype; uint8_t basetype;
uint32_t vec_size; uint8_t vec_size;
};// };//
struct ShaderStageData struct ShaderAttributeArray
{ {
uint32_t count; uint32_t count;
ShaderStage *items; ShaderAttribute *items;
}; };
struct Descriptor struct Descriptor
@ -285,6 +288,8 @@ struct ShaderResourceData
T *items; T *items;
}; };
using ShaderDescriptorResource=ShaderResourceData<Descriptor>[VK_DESCRIPTOR_TYPE_COUNT];
struct SPVData struct SPVData
{ {
bool result; bool result;
@ -294,11 +299,6 @@ struct SPVData
uint32_t *spv_data; uint32_t *spv_data;
uint32_t spv_length; uint32_t spv_length;
ShaderStageData input,output;
ShaderResourceData<Descriptor> resource[VK_DESCRIPTOR_TYPE_COUNT];
ShaderResourceData<PushConstant> push_constant;
ShaderResourceData<SubpassInput> subpass_input;
void Init() void Init()
{ {
memset(this,0,sizeof(SPVData)); memset(this,0,sizeof(SPVData));
@ -338,23 +338,46 @@ public:
} }
~SPVData() ~SPVData()
{ {
for(uint32_t i=0;i<VK_DESCRIPTOR_TYPE_COUNT;i++)
delete[] resource[i].items;
delete[] push_constant.items;
delete[] subpass_input.items;
delete[] input.items;
delete[] output.items;
delete[] log; delete[] log;
delete[] debug_log; delete[] debug_log;
delete[] spv_data; delete[] spv_data;
} }
};//struct SPVData };//struct SPVData
void OutputShaderStage(ShaderStageData *ssd,ShaderParse *sp,const SPVResVector &stages) struct ShaderStageIO
{
ShaderAttributeArray input,output;
};//struct ShaderStageIO
struct SPVParseData
{
ShaderStageIO stage_io;
ShaderDescriptorResource resource;
ShaderResourceData<PushConstant> push_constant;
ShaderResourceData<SubpassInput> subpass_input;
public:
SPVParseData()
{
memset(this,0,sizeof(SPVParseData));
}
~SPVParseData()
{
for(uint32_t i=0;i<VK_DESCRIPTOR_TYPE_COUNT;i++)
delete[] resource[i].items;
delete[] push_constant.items;
delete[] subpass_input.items;
delete[] stage_io.input.items;
delete[] stage_io.output.items;
}
};
void OutputShaderAttributes(ShaderAttributeArray *ssd,ShaderParse *sp,const SPVResVector &stages)
{ {
size_t attr_count=stages.size(); size_t attr_count=stages.size();
@ -366,8 +389,8 @@ void OutputShaderStage(ShaderStageData *ssd,ShaderParse *sp,const SPVResVector &
uint8_t vec_size; uint8_t vec_size;
std::string name; std::string name;
ssd->items=new ShaderStage[attr_count]; ssd->items=new ShaderAttribute[attr_count];
ShaderStage *ss=ssd->items; ShaderAttribute *ss=ssd->items;
for(const spirv_cross::Resource &si:stages) for(const spirv_cross::Resource &si:stages)
{ {
@ -396,7 +419,7 @@ void OutputShaderResource(ShaderResourceData<Descriptor> *ssd,ShaderParse *sp,co
for(const spirv_cross::Resource &obj:res) for(const spirv_cross::Resource &obj:res)
{ {
strcpy(sr->name,sp->GetName(obj).c_str()); strcpy(sr->name,sp->GetName(obj).c_str());
sr->set = sp->GetSet(obj); sr->set = sp->GetDescriptorSet(obj);
sr->binding=sp->GetBinding(obj); sr->binding=sp->GetBinding(obj);
++sr; ++sr;
@ -525,7 +548,9 @@ extern "C"
// shader.setEnvInput(source,stage,glslang::EShClientVulkan,); // shader.setEnvInput(source,stage,glslang::EShClientVulkan,);
// shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_0); // shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_0);
// shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_0);
shader.setEnvInput(source,stage,glslang::EShClientVulkan,compile_info->vulkan_version);
shader.setEnvTarget(glslang::EShTargetSpv, (glslang::EShTargetLanguageVersion)(compile_info->spv_version));
if (!shader.parse(&Resources, if (!shader.parse(&Resources,
110, // use 100 for ES environment, 110 for desktop; this is the GLSL version, not SPIR-V or Vulkan 110, // use 100 for ES environment, 110 for desktop; this is the GLSL version, not SPIR-V or Vulkan
@ -550,26 +575,34 @@ extern "C"
glslang::GlslangToSpv(*program.getIntermediate(stage),spirv); glslang::GlslangToSpv(*program.getIntermediate(stage),spirv);
SPVData *spv=new SPVData(spirv); return(new SPVData(spirv));
}
{
ShaderParse sp(spirv.data(),(uint32_t)spirv.size()*sizeof(uint32_t));
OutputShaderStage(&(spv->input),&sp,sp.GetStageInputs());
OutputShaderStage(&(spv->output),&sp,sp.GetStageOutputs());
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &sp,sp.GetUBO()); SPVParseData *ParseSPV(SPVData *spv_data)
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, &sp,sp.GetSSBO()); {
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, &sp,sp.GetSampledImages()); ShaderParse sp(spv_data->spv_data,spv_data->spv_length);
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_SAMPLER, &sp,sp.GetSeparateSamplers());
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, &sp,sp.GetSeparateImages());
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &sp,sp.GetStorageImages());
OutputPushConstant (&(spv->push_constant), &sp,sp.GetPushConstant()); SPVParseData *spv=new SPVParseData;
OutputSubpassInput (&(spv->subpass_input), &sp,sp.GetSubpassInputs());
}
return(spv); OutputShaderAttributes(&(spv->stage_io.input),&sp,sp.GetStageInputs());
OutputShaderAttributes(&(spv->stage_io.output),&sp,sp.GetStageOutputs());
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &sp,sp.GetUBO());
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, &sp,sp.GetSSBO());
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, &sp,sp.GetSampledImages());
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_SAMPLER, &sp,sp.GetSeparateSamplers());
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, &sp,sp.GetSeparateImages());
OutputShaderResource(spv->resource+VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &sp,sp.GetStorageImages());
OutputPushConstant (&(spv->push_constant), &sp,sp.GetPushConstant());
OutputSubpassInput (&(spv->subpass_input), &sp,sp.GetSubpassInputs());
return spv;
}
void FreeSPVParse(SPVParseData *spv)
{
delete spv;
} }
SPVData *CompileFromPath( SPVData *CompileFromPath(
@ -639,6 +672,9 @@ extern "C"
SPVData * (*CompileFromPath)(const uint32_t stage,const char *shader_filename, const CompileInfo *compile_info); SPVData * (*CompileFromPath)(const uint32_t stage,const char *shader_filename, const CompileInfo *compile_info);
void (*Free)(SPVData *); void (*Free)(SPVData *);
SPVParseData *(*ParseSPV)(SPVData *spv_data);
void (*FreeParseSPVData)(SPVParseData *);
}; };
static GLSLCompilerInterface plug_in_interface static GLSLCompilerInterface plug_in_interface
@ -650,7 +686,10 @@ extern "C"
&GetShaderStageFlagByExtName, &GetShaderStageFlagByExtName,
&Shader2SPV, &Shader2SPV,
&CompileFromPath, &CompileFromPath,
&FreeSPVData &FreeSPVData,
&ParseSPV,
&FreeSPVParse
}; };
#ifdef WIN32 #ifdef WIN32