diff --git a/glsl2spv.cpp b/glsl2spv.cpp index 408c030..f0b096d 100644 --- a/glsl2spv.cpp +++ b/glsl2spv.cpp @@ -184,301 +184,300 @@ EShLanguage FindLanguage(const VkShaderStageFlagBits shader_type) } } -extern "C" +enum class ShaderLanguageType { - bool InitShaderCompiler() - { - init_default_build_in_resource(); + GLSL=0, + HLSL, - return glslang::InitializeProcess(); - } + MAX=0xff +};//enum class ShaderType - void CloseShaderCompiler() - { - glslang::FinalizeProcess(); - } +struct CompileInfo +{ + ShaderLanguageType shader_type; + const char * entrypoint; + uint32_t includes_count; + const char ** includes; + const char * preamble; +}; + +enum class VertexAttribBaseType +{ + Bool=0, + Int, + UInt, + Float, + Double, - bool GetLimit(TBuiltInResource *bir,const int size) - { - if(!bir)return(false); - if(size!=sizeof(TBuiltInResource))return(false); + MAX=0xff +};//enum class VertexAttribBaseType - memcpy(bir,&default_build_in_resource,size); - return(true); - } +VertexAttribBaseType FromSPIRType(const spirv_cross::SPIRType::BaseType type) +{ + if(type==spirv_cross::SPIRType::BaseType::Boolean) return VertexAttribBaseType::Bool; + if(type==spirv_cross::SPIRType::BaseType::SByte + ||type==spirv_cross::SPIRType::BaseType::Short + ||type==spirv_cross::SPIRType::BaseType::Int + ||type==spirv_cross::SPIRType::BaseType::Int64) return VertexAttribBaseType::Int; + if(type==spirv_cross::SPIRType::BaseType::UByte + ||type==spirv_cross::SPIRType::BaseType::UShort + ||type==spirv_cross::SPIRType::BaseType::UInt + ||type==spirv_cross::SPIRType::BaseType::UInt64) return VertexAttribBaseType::UInt; + if(type==spirv_cross::SPIRType::BaseType::Half + ||type==spirv_cross::SPIRType::BaseType::Float) return VertexAttribBaseType::Float; + if(type==spirv_cross::SPIRType::BaseType::Double) return VertexAttribBaseType::Double; + + return VertexAttribBaseType::MAX; +} + +char *new_strcpy(const char *src) +{ + size_t len=1+strlen(src); - bool SetLimit(TBuiltInResource *bir,const int size) - { - if(!bir)return(false); - if(size!=sizeof(TBuiltInResource))return(false); + char *str=new char[len]; + + memcpy(str,src,len); - memcpy(&default_build_in_resource,bir,size); - return(true); - } + return str; +} - enum class ShaderLanguageType - { - GLSL=0, - HLSL, +constexpr size_t SHADER_RESOURCE_NAME_MAX_LENGTH=128; - MAX=0xff - };//enum class ShaderType +struct ShaderStage +{ + char name[SHADER_RESOURCE_NAME_MAX_LENGTH]; + uint8_t location; + uint32_t basetype; + uint32_t vec_size; +};// - struct CompileInfo - { - ShaderLanguageType shader_type; - const char * entrypoint; - uint32_t includes_count; - const char ** includes; - const char * preamble; - }; +struct ShaderStageData +{ + uint32_t count; + ShaderStage *items; +}; - enum class VertexAttribBaseType - { - Bool=0, - Int, - UInt, - Float, - Double, +struct Descriptor +{ + char name[SHADER_RESOURCE_NAME_MAX_LENGTH]; + uint8_t set; + uint8_t binding; +}; - MAX=0xff - };//enum class VertexAttribBaseType +struct PushConstant +{ + char name[SHADER_RESOURCE_NAME_MAX_LENGTH]; + uint8_t offset; + uint8_t size; +}; - VertexAttribBaseType FromSPIRType(const spirv_cross::SPIRType::BaseType type) - { - if(type==spirv_cross::SPIRType::BaseType::Boolean) return VertexAttribBaseType::Bool; - if(type==spirv_cross::SPIRType::BaseType::SByte - ||type==spirv_cross::SPIRType::BaseType::Short - ||type==spirv_cross::SPIRType::BaseType::Int - ||type==spirv_cross::SPIRType::BaseType::Int64) return VertexAttribBaseType::Int; - if(type==spirv_cross::SPIRType::BaseType::UByte - ||type==spirv_cross::SPIRType::BaseType::UShort - ||type==spirv_cross::SPIRType::BaseType::UInt - ||type==spirv_cross::SPIRType::BaseType::UInt64) return VertexAttribBaseType::UInt; - if(type==spirv_cross::SPIRType::BaseType::Half - ||type==spirv_cross::SPIRType::BaseType::Float) return VertexAttribBaseType::Float; - if(type==spirv_cross::SPIRType::BaseType::Double) return VertexAttribBaseType::Double; - - return VertexAttribBaseType::MAX; - } - - char *new_strcpy(const char *src) - { - size_t len=1+strlen(src); +struct SubpassInput +{ + char name[SHADER_RESOURCE_NAME_MAX_LENGTH]; + uint8_t input_attachment_index; + uint8_t binding; +}; - char *str=new char[len]; - - memcpy(str,src,len); +template +struct ShaderResourceData +{ + uint32_t count; + T *items; +}; - return str; - } +struct SPVData +{ + bool result; + char *log; + char *debug_log; - struct ShaderStage - { - char name[128]; - uint8_t location; - uint32_t basetype; - uint32_t vec_size; - };// + uint32_t *spv_data; + uint32_t spv_length; + + ShaderStageData input,output; + ShaderResourceData resource[VK_DESCRIPTOR_TYPE_COUNT]; + ShaderResourceData push_constant; + ShaderResourceData subpass_input; - struct ShaderStageData + void Init() { - uint32_t count; - ShaderStage *items; - }; + memset(this,0,sizeof(SPVData)); + } - struct ShaderResource +public: + + SPVData(const char *l,const char *dl) { - char name[128]; + Init(); - union - { - struct - { - uint8_t set; - uint8_t binding; - }; + result=false; - struct - { - uint8_t offset; - uint8_t size; - }; + log=new_strcpy(l); + debug_log=new_strcpy(dl); - struct - { - uint8_t input_attachment_index; - }; - }; - }; + spv_data=nullptr; + } - struct ShaderResourceData + SPVData(const char* log) : SPVData(log, log) { - uint32_t count; - ShaderResource *items; - }; + } - struct SPVData + SPVData(const std::vector &spirv) { - bool result; - char *log; - char *debug_log; - - uint32_t *spv_data; - uint32_t spv_length; + Init(); - ShaderStageData input,output; - ShaderResourceData resource[VK_DESCRIPTOR_TYPE_COUNT]; - ShaderResourceData push_constant; - ShaderResourceData subpass_input; + result=true; - void Init() - { - memset(this,0,sizeof(SPVData)); - } + log=nullptr; + debug_log=nullptr; - public: - - SPVData(const char *l,const char *dl) - { - Init(); + spv_length=(uint32_t)spirv.size(); + spv_data=new uint32_t[spv_length]; + spv_length*=sizeof(uint32_t); + memcpy(spv_data,spirv.data(),spv_length); + } - result=false; + ~SPVData() + { + for(uint32_t i=0;i &spirv) - { - Init(); +void OutputShaderStage(ShaderStageData *ssd,ShaderParse *sp,const SPVResVector &stages) +{ + size_t attr_count=stages.size(); - result=true; + ssd->count=(uint32_t)attr_count; - log=nullptr; - debug_log=nullptr; + if(attr_count<=0)return; - spv_length=(uint32_t)spirv.size(); - spv_data=new uint32_t[spv_length]; - spv_length*=sizeof(uint32_t); - memcpy(spv_data,spirv.data(),spv_length); - } + spirv_cross::SPIRType::BaseType base_type; + uint8_t vec_size; + std::string name; - ~SPVData() - { - for(uint32_t i=0;iitems=new ShaderStage[attr_count]; + ShaderStage *ss=ssd->items; - delete[] push_constant.items; - delete[] subpass_input.items; + for(const spirv_cross::Resource &si:stages) + { + sp->GetFormat(si,&base_type,&vec_size); - delete[] input.items; - delete[] output.items; + ss->basetype =(uint8_t)FromSPIRType(base_type); + ss->vec_size =vec_size; + ss->location =sp->GetLocation(si); - delete[] log; - delete[] debug_log; - delete[] spv_data; - } - };//struct SPVData + strcpy(ss->name,sp->GetName(si).c_str()); - void FreeSPVData(SPVData *spv) - { - delete spv; + ++ss; } +} - void OutputShaderStage(ShaderStageData *ssd,ShaderParse *sp,const SPVResVector &stages) - { - size_t attr_count=stages.size(); - - ssd->count=(uint32_t)attr_count; +void OutputShaderResource(ShaderResourceData *ssd,ShaderParse *sp,const SPVResVector &res) +{ + size_t count=res.size(); - if(attr_count<=0)return; + if(count<=0)return; - spirv_cross::SPIRType::BaseType base_type; - uint8_t vec_size; - std::string name; + ssd->count=(uint32_t)count; + ssd->items=new Descriptor[count]; + Descriptor *sr=ssd->items; - ssd->items=new ShaderStage[attr_count]; - ShaderStage *ss=ssd->items; + for(const spirv_cross::Resource &obj:res) + { + strcpy(sr->name,sp->GetName(obj).c_str()); + sr->set = sp->GetSet(obj); + sr->binding=sp->GetBinding(obj); - for(const spirv_cross::Resource &si:stages) - { - sp->GetFormat(si,&base_type,&vec_size); + ++sr; + } +} - ss->basetype =(uint8_t)FromSPIRType(base_type); - ss->vec_size =vec_size; - ss->location =sp->GetLocation(si); +void OutputPushConstant(ShaderResourceData *ssd, ShaderParse* sp, const SPVResVector& res) +{ + size_t count = res.size(); - strcpy(ss->name,sp->GetName(si).c_str()); + if (count <= 0)return; - ++ss; - } - } + ssd->count = (uint32_t)count; + ssd->items = new PushConstant[count]; + PushConstant *sr=ssd->items; - void OutputShaderResource(ShaderResourceData *ssd,ShaderParse *sp,const SPVResVector &res) + for (const spirv_cross::Resource& obj : res) { - size_t count=res.size(); + strcpy(sr->name, sp->GetName(obj).c_str()); + sr->offset = sp->GetOffset(obj); + sr->size = sp->GetBufferSize(obj); - if(count<=0)return; + ++sr; + } +} - ssd->count=(uint32_t)count; - ssd->items=new ShaderResource[count]; - ShaderResource *sr=ssd->items; +void OutputSubpassInput(ShaderResourceData *ssd, ShaderParse* sp, const SPVResVector& res) +{ + size_t count = res.size(); - for(const spirv_cross::Resource &obj:res) - { - strcpy(sr->name,sp->GetName(obj).c_str()); - sr->set = sp->GetSet(obj); - sr->binding=sp->GetBinding(obj); + if (count <= 0)return; - ++sr; - } - } + ssd->count = (uint32_t)count; + ssd->items = new SubpassInput[count]; + SubpassInput *sr = ssd->items; - void OutputPushConstant(ShaderResourceData* ssd, ShaderParse* sp, const SPVResVector& res) + for (const spirv_cross::Resource& obj : res) { - size_t count = res.size(); + strcpy(sr->name, sp->GetName(obj).c_str()); + sr->input_attachment_index = sp->GetInputAttachmentIndex(obj); + sr->binding=sp->GetBinding(obj); - if (count <= 0)return; + ++sr; + } +} - ssd->count = (uint32_t)count; - ssd->items = new ShaderResource[count]; - ShaderResource* sr = ssd->items; +extern "C" +{ + bool InitShaderCompiler() + { + init_default_build_in_resource(); - for (const spirv_cross::Resource& obj : res) - { - strcpy(sr->name, sp->GetName(obj).c_str()); - sr->offset = sp->GetOffset(obj); - sr->size = sp->GetBufferSize(obj); + return glslang::InitializeProcess(); + } - ++sr; - } + void CloseShaderCompiler() + { + glslang::FinalizeProcess(); } - void OutputSubpassInput(ShaderResourceData* ssd, ShaderParse* sp, const SPVResVector& res) + bool GetLimit(TBuiltInResource *bir,const int size) { - size_t count = res.size(); + if(!bir)return(false); + if(size!=sizeof(TBuiltInResource))return(false); - if (count <= 0)return; + memcpy(bir,&default_build_in_resource,size); + return(true); + } - ssd->count = (uint32_t)count; - ssd->items = new ShaderResource[count]; - ShaderResource* sr = ssd->items; + bool SetLimit(TBuiltInResource *bir,const int size) + { + if(!bir)return(false); + if(size!=sizeof(TBuiltInResource))return(false); - for (const spirv_cross::Resource& obj : res) - { - strcpy(sr->name, sp->GetName(obj).c_str()); - sr->input_attachment_index = sp->GetInputAttachmentIndex(obj); - sr->binding=sp->GetBinding(obj); + memcpy(&default_build_in_resource,bir,size); + return(true); + } - ++sr; - } + void FreeSPVData(SPVData *spv) + { + delete spv; } SPVData *Shader2SPV(