diff --git a/src/ShaderGen/GLSLCompiler.cpp b/src/ShaderGen/GLSLCompiler.cpp index effe5404..de9d79b4 100644 --- a/src/ShaderGen/GLSLCompiler.cpp +++ b/src/ShaderGen/GLSLCompiler.cpp @@ -3,196 +3,151 @@ #include #include -using namespace hgl; -using namespace hgl::io; - -namespace glsl_compiler +namespace hgl { - enum class ShaderLanguageType + namespace graph { - GLSL=0, - HLSL, - - MAX=0xff - };//enum class ShaderType - - struct CompileInfo - { - ShaderLanguageType shader_type = ShaderLanguageType::GLSL; - const char * entrypoint = nullptr; //it only used in HLSL - - uint32_t includes_count = 0; - const char ** includes = nullptr; - - const char * preamble = nullptr; - }; - - UTF8StringList include_list; - CompileInfo compile_info; - - struct GLSLCompilerInterface - { - bool (*Init)(); - void (*Close)(); - - bool (*GetLimit)(void *,const int); - bool (*SetLimit)(void *,const int); - - uint32_t (*GetType)(const char *ext_name); - SPVData * (*Compile)(const uint32_t stage,const char *shader_source, const CompileInfo *compile_info); - SPVData * (*CompileFromPath)(const uint32_t stage,const char *shader_filename, const CompileInfo *compile_info); - - void (*Free)(SPVData *); - - SPVParseData *(*ParseSPV)(SPVData *spv_data); - void (*FreeParseSPVData)(SPVParseData *); - }; - - ExternalModule *gsi_module=nullptr; - - static GLSLCompilerInterface *gsi=nullptr; - - typedef GLSLCompilerInterface *(*GetInterfaceFUNC)(); - - bool Init() - { - compile_info.includes=nullptr; - compile_info.includes_count=0; - - if(gsi)return(true); - - OSString cur_path; - OSString glsl_compiler_fullname; - - if(!filesystem::GetCurrentPath(cur_path)) - return(false); - glsl_compiler_fullname=filesystem::MergeFilename(cur_path,OS_TEXT("GLSLCompiler") HGL_PLUGIN_EXTNAME); - - gsi_module=LoadExternalModule(glsl_compiler_fullname); - - if(!gsi_module)return(false); - - GetInterfaceFUNC get_func; - - get_func=GetInterfaceFUNC(gsi_module->GetFunc("GetInterface")); - - if(get_func) + enum class ShaderLanguageType { - gsi=get_func(); + GLSL=0, + HLSL, + + MAX=0xff + };//enum class ShaderType + + struct CompileInfo + { + ShaderLanguageType shader_type = ShaderLanguageType::GLSL; + const char * entrypoint = nullptr; //it only used in HLSL + + uint32_t includes_count = 0; + const char ** includes = nullptr; + + const char * preamble = nullptr; + }; + + CompileInfo compile_info; + + struct SPVParseData; + + struct GLSLCompilerInterface + { + bool (*Init)(); + void (*Close)(); + + bool (*GetLimit)(void *,const int); + bool (*SetLimit)(void *,const int); + + uint32_t (*GetType)(const char *ext_name); + SPVData * (*Compile)(const uint32_t stage,const char *shader_source, const CompileInfo *compile_info); + SPVData * (*CompileFromPath)(const uint32_t stage,const char *shader_filename, const CompileInfo *compile_info); + + void (*Free)(SPVData *); + + SPVParseData *(*ParseSPV)(SPVData *spv_data); + void (*FreeParseSPVData)(SPVParseData *); + }; + + ExternalModule *gsi_module=nullptr; + + static GLSLCompilerInterface *gsi=nullptr; + + typedef GLSLCompilerInterface *(*GetInterfaceFUNC)(); + + bool InitShaderCompiler() + { + compile_info.includes=nullptr; + compile_info.includes_count=0; + + if(gsi)return(true); + + OSString cur_path; + OSString glsl_compiler_fullname; + + if(!filesystem::GetCurrentPath(cur_path)) + return(false); + glsl_compiler_fullname=filesystem::MergeFilename(cur_path,OS_TEXT("GLSLCompiler") HGL_PLUGIN_EXTNAME); + + gsi_module=LoadExternalModule(glsl_compiler_fullname); + + if(!gsi_module)return(false); + + GetInterfaceFUNC get_func; + + get_func=GetInterfaceFUNC(gsi_module->GetFunc("GetInterface")); + + if(get_func) + { + gsi=get_func(); + if(gsi) + { + if(gsi->Init()) + return(true); + } + } + + delete gsi_module; + gsi_module=nullptr; + return(false); + } + + void CloseShaderCompiler() + { + delete[] compile_info.includes; + compile_info.includes=nullptr; + if(gsi) { - if(gsi->Init()) - return(true); + gsi->Close(); + gsi=nullptr; + } + + if(gsi_module) + { + delete gsi_module; + gsi_module=nullptr; } } - delete gsi_module; - gsi_module=nullptr; - return(false); - } + const char PreambleString[]="";//#extension GL_GOOGLE_include_directive : require\n"; - void Close() - { - delete[] compile_info.includes; - compile_info.includes=nullptr; - - if(gsi) + void RebuildGLSLIncludePath() { - gsi->Close(); - gsi=nullptr; - } - - if(gsi_module) - { - delete gsi_module; - gsi_module=nullptr; - } - } - - void AddGLSLIncludePath(const char *path) - { - if(include_list.Find(path)==-1) - include_list.Add(path); - } - - const char PreambleString[]="#extension GL_GOOGLE_include_directive : require\n"; - - void RebuildGLSLIncludePath() - { - delete[] compile_info.includes; - - compile_info.includes_count=include_list.GetCount(); - compile_info.includes=new const char *[compile_info.includes_count]; - - for(uint32_t i=0;i0) compile_info.preamble=PreambleString; - } - - uint32_t GetType (const char *ext_name) - { - if(gsi) - return gsi->GetType(ext_name); - - return 0; - } - - void Free (SPVData *spv_data) - { - if(gsi) - gsi->Free(spv_data); - } - - SPVParseData *ParseSPV(SPVData *spv_data) - { - return gsi?gsi->ParseSPV(spv_data):nullptr; - } - - void FreeSPVParse(SPVParseData *spv) - { - if(gsi&&spv) - gsi->FreeParseSPVData(spv); - } - - SPVData *Compile(const uint32_t type,const char *source) - { - if(!gsi) - return(nullptr); - - ByteOrderMask bom=CheckBOM(source); - - if(bom==ByteOrderMask::UTF8) - source+=3; - else - if(bom!=ByteOrderMask::NONE) - { - std::cerr<<"GLSLCompiler does not support BOMHeader outside of UTF8"<Compile(type,source,&compile_info); - - if(!spv)return(nullptr); - - const bool result=spv->result; - - if(!result) + void FreeSPVData(SPVData *spv_data) { - std::cerr<<"glsl compile failed."<log) - std::cerr<<"info: "<log<debug_log) - std::cerr<<"debug info: "<debug_log<Free(spv_data); } - std::cout<<"Compile successed! spv length "<spv_length<<" bytes."<Compile(type,source,&compile_info); + + if(!spv)return(nullptr); + + const bool result=spv->result; + + if(!result) + { + FreeSPVData(spv); + return(nullptr); + } + + return spv; + } + }//namespace graph +}//namespace hgl diff --git a/src/ShaderGen/GLSLCompiler.h b/src/ShaderGen/GLSLCompiler.h index d651af7b..faab83a4 100644 --- a/src/ShaderGen/GLSLCompiler.h +++ b/src/ShaderGen/GLSLCompiler.h @@ -1,102 +1,26 @@ #ifndef HGL_GLSL_COMPILER_INCLUDE #define HGL_GLSL_COMPILER_INCLUDE -#include -#include -#include -#include - +#include namespace hgl { - namespace io + namespace graph { - class MemoryOutputStream; - class DataOutputStream; - } -} + struct SPVData + { + bool result; + char *log; + char *debug_log; -namespace glsl_compiler -{ - using namespace hgl; - using namespace hgl::graph; + uint32 *spv_data; + uint32 spv_length; + }; - enum class DescriptorType //等同VkDescriptorType - { - SAMPLER = 0, - COMBINED_IMAGE_SAMPLER, - SAMPLED_IMAGE, - STORAGE_IMAGE, - UNIFORM_TEXEL_BUFFER, - STORAGE_TEXEL_BUFFER, - UNIFORM_BUFFER, - STORAGE_BUFFER, - UNIFORM_BUFFER_DYNAMIC, - STORAGE_BUFFER_DYNAMIC, - INPUT_ATTACHMENT, - - ENUM_CLASS_RANGE(SAMPLER,INPUT_ATTACHMENT) - }; + bool InitShaderCompiler(); + void CloseShaderCompiler(); - struct Descriptor - { - char name[SHADER_RESOURCE_NAME_MAX_LENGTH]; - uint8_t set; - uint8_t binding; - }; - - struct PushConstant - { - char name[SHADER_RESOURCE_NAME_MAX_LENGTH]; - uint8_t offset; - uint8_t size; - }; - - struct SubpassInput - { - char name[SHADER_RESOURCE_NAME_MAX_LENGTH]; - uint8_t input_attachment_index; - uint8_t binding; - }; - - template - struct ShaderResourceData - { - uint32_t count; - T *items; - }; - - using ShaderDescriptorResource=ShaderResourceData[size_t(DescriptorType::RANGE_SIZE)]; - - struct SPVData - { - bool result; - char *log; - char *debug_log; - - uint32_t *spv_data; - uint32_t spv_length; - }; - - struct SPVParseData - { - ShaderStageIO stage_io; - ShaderDescriptorResource resource; - ShaderResourceData push_constant; - ShaderResourceData subpass_input; - }; - - bool Init(); - void Close(); - - void AddGLSLIncludePath(const char *); - void RebuildGLSLIncludePath(); - - uint32_t GetType (const char *ext_name); - - SPVData * Compile (const uint32_t type,const char *source); - void Free (SPVData *spv_data); - - SPVParseData * ParseSPV(SPVData *spv_data); - void FreeSPVParse(SPVParseData *spv); -}//namespace glsl_compiler + SPVData * CompileShader (const uint32 type,const char *source); + void FreeSPVData (SPVData *spv_data); + }//namespace graph +}//namespace hgl #endif//HGL_GLSL_COMPILER_INCLUDE