diff --git a/hlslang/MachineIndependent/HLSL2GLSL.cpp b/hlslang/MachineIndependent/HLSL2GLSL.cpp index ef63fa2..d2dbb59 100644 --- a/hlslang/MachineIndependent/HLSL2GLSL.cpp +++ b/hlslang/MachineIndependent/HLSL2GLSL.cpp @@ -297,7 +297,7 @@ int C_DECL Hlsl2Glsl_Parse( const ShHandle handle, const char* shaderString, ETargetVersion targetVersion, - Hlsl2Glsl_ParseCallbacks* callbacks, + Hlsl2Glsl_PreprocessorData* preprocessorData, unsigned options) { if (!InitThread()) @@ -337,7 +337,7 @@ int C_DECL Hlsl2Glsl_Parse( parseContext.infoSink.info.message(EPrefixInternalError, "Wrong symbol table level"); - int ret = PaParseString(const_cast(shaderString), parseContext, callbacks); + int ret = PaParseString(const_cast(shaderString), parseContext, preprocessorData ); if (ret) success = false; diff --git a/hlslang/MachineIndependent/ParseHelper.h b/hlslang/MachineIndependent/ParseHelper.h index fb9099e..f448ce3 100644 --- a/hlslang/MachineIndependent/ParseHelper.h +++ b/hlslang/MachineIndependent/ParseHelper.h @@ -102,7 +102,7 @@ struct TParseContext bool AfterEOF; }; -int PaParseString(char* source, TParseContext&, Hlsl2Glsl_ParseCallbacks* = NULL); +int PaParseString(char* source, TParseContext&, Hlsl2Glsl_PreprocessorData* = NULL); void PaReservedWord(); int PaIdentOrType(TString& id, TParseContext&, TSymbol*&); int PaParseComment(TSourceLoc &lineno, TParseContext&); diff --git a/hlslang/MachineIndependent/hlslang.l b/hlslang/MachineIndependent/hlslang.l index 1cc2736..795284d 100644 --- a/hlslang/MachineIndependent/hlslang.l +++ b/hlslang/MachineIndependent/hlslang.l @@ -391,25 +391,16 @@ int IncludeOpenCallback(MOJOSHADER_hlslang_includeType inctype, const char **outdataPtr, unsigned int *outbytesPtr, MOJOSHADER_hlslang_malloc m, MOJOSHADER_hlslang_free f, void *d) { - Hlsl2Glsl_ParseCallbacks* callbacks = reinterpret_cast(d); - std::string out; - if (!callbacks->includeOpenCallback(inctype == MOJOSHADER_hlslang_INCLUDETYPE_SYSTEM, - fname, out, callbacks->data)) - { - return 0; - } - - char* outdata = (char*) m(out.size() + 1, NULL); - std::memcpy(outdata, out.data(), out.size()); - *outdataPtr = outdata; - *outbytesPtr = out.size(); - return 1; + Hlsl2Glsl_PreprocessorData* preprocessorData = reinterpret_cast(d); + return preprocessorData->includeOpenCallback(inctype == MOJOSHADER_hlslang_INCLUDETYPE_SYSTEM, + fname, outdataPtr, outbytesPtr, preprocessorData->callbackData) ? 1 : 0; } void IncludeCloseCallback(const char *data, MOJOSHADER_hlslang_malloc m, MOJOSHADER_hlslang_free f, void *d) { - f(const_cast(data), NULL); + Hlsl2Glsl_PreprocessorData* preprocessorData = reinterpret_cast(d); + preprocessorData->includeCloseCallback( data ); } // @@ -418,7 +409,7 @@ void IncludeCloseCallback(const char *data, // // Returns 0 for success, as per yyparse(). // -int PaParseString(char* source, TParseContext& parseContextLocal, Hlsl2Glsl_ParseCallbacks* callbacks) +int PaParseString(char* source, TParseContext& parseContextLocal, Hlsl2Glsl_PreprocessorData* preprocessorData ) { int sourceLen; @@ -432,21 +423,34 @@ int PaParseString(char* source, TParseContext& parseContextLocal, Hlsl2Glsl_Pars MOJOSHADER_hlslang_includeOpen openCallback = NULL; MOJOSHADER_hlslang_includeClose closeCallback = NULL; + const MOJOSHADER_hlslang_preprocessorDefine* defines = NULL; + MOJOSHADER_hlslang_malloc mojoMalloc = MOJOSHADER_hlslang_internal_malloc; + MOJOSHADER_hlslang_free mojoFree = MOJOSHADER_hlslang_internal_free; + unsigned int definesCount = 0; void* data = NULL; - if (callbacks) + if (preprocessorData) { openCallback = IncludeOpenCallback; closeCallback = IncludeCloseCallback; - data = callbacks; + + if( preprocessorData->malloc ) + mojoMalloc = ( MOJOSHADER_hlslang_malloc )preprocessorData->malloc; + if( preprocessorData->free ) + mojoFree = ( MOJOSHADER_hlslang_free )preprocessorData->free; + + defines = ( const MOJOSHADER_hlslang_preprocessorDefine* )preprocessorData->defines; + definesCount = preprocessorData->defineCount; + + data = preprocessorData; } hlmojo_Preprocessor* pp = hlmojo_preprocessor_start("", source, sourceLen, openCallback, closeCallback, - NULL, // defines - 0, // define count - MOJOSHADER_hlslang_internal_malloc, - MOJOSHADER_hlslang_internal_free, + defines, + definesCount, + mojoMalloc, + mojoFree, data); g_cpp = pp; g_parseContext = &parseContextLocal; diff --git a/include/hlsl2glsl.h b/include/hlsl2glsl.h index 071e697..1917ca2 100644 --- a/include/hlsl2glsl.h +++ b/include/hlsl2glsl.h @@ -234,23 +234,36 @@ SH_IMPORT_EXPORT void C_DECL Hlsl2Glsl_DestructCompiler( ShHandle handle ); /// File read callback for #include processing. -typedef bool (C_DECL *Hlsl2Glsl_IncludeOpenFunc)(bool isSystem, const char* fname, std::string& output, void* data); -struct Hlsl2Glsl_ParseCallbacks +typedef bool ( C_DECL *Hlsl2Glsl_IncludeOpenFunc )( bool isSystem, const char* fname, const char** outdata, unsigned int* outbytes, void* data ); +typedef void ( C_DECL *Hlsl2Glsl_IncludeCloseFunc )( const char* data ); +typedef void* ( C_DECL *Hlsl2Glsl_PreprocessorMalloc )( int bytes, void *data ); +typedef void ( C_DECL *Hlsl2Glsl_PreprocessorFree )( void *ptr, void *data ); +struct Hlsl2Glsl_PreprocessorDefine { - Hlsl2Glsl_IncludeOpenFunc includeOpenCallback; - void* data; + const char* identifier; + const char* definition; +}; +struct Hlsl2Glsl_PreprocessorData +{ + Hlsl2Glsl_IncludeOpenFunc includeOpenCallback; + Hlsl2Glsl_IncludeCloseFunc includeCloseCallback; + Hlsl2Glsl_PreprocessorMalloc malloc; + Hlsl2Glsl_PreprocessorFree free; + const Hlsl2Glsl_PreprocessorDefine* defines; + unsigned int defineCount; + void* callbackData; }; /// Parse HLSL shader to prepare it for final translation. -/// \param callbacks -/// File read callback for #include processing. If NULL is passed, then #include directives will result in error. +/// \param preprocessor +/// Hlsl2Glsl_PreprocessorData for macros processing. If NULL is passed, then some macros will result in error. /// \param options /// Flags of TTranslateOptions SH_IMPORT_EXPORT int C_DECL Hlsl2Glsl_Parse( const ShHandle handle, const char* shaderString, ETargetVersion targetVersion, - Hlsl2Glsl_ParseCallbacks* callbacks, + Hlsl2Glsl_PreprocessorData* preprocessorData, unsigned options); diff --git a/tests/hlsl2glsltest/hlsl2glsltest.cpp b/tests/hlsl2glsltest/hlsl2glsltest.cpp index 5769a38..d2d3d14 100644 --- a/tests/hlsl2glsltest/hlsl2glsltest.cpp +++ b/tests/hlsl2glsltest/hlsl2glsltest.cpp @@ -140,7 +140,7 @@ static void DeleteFile (const std::string& path) #endif } -static bool ReadStringFromFile (const char* pathName, std::string& output) +static bool ReadStringFromFile (const char* pathName, std::string& output ) { # ifdef _MSC_VER wchar_t widePath[MAX_PATH]; @@ -181,6 +181,24 @@ static bool ReadStringFromFile (const char* pathName, std::string& output) } +static bool ReadStringFromFile (const char* pathName, const char** outdata, unsigned int* outBytes ) +{ + *outdata = NULL; + *outBytes = 0; + + std::string output; + if( !ReadStringFromFile( pathName, output ) ) + return false; + + *outdata = new char[ output.length() + 1 ]; + memset( ( void* )*outdata, output.length() + 1, 0 ); + memcpy( ( void* )*outdata, output.c_str(), output.length() ); + *outBytes = output.length(); + + return true; +} + + #if defined(__APPLE__) static CGLContextObj s_GLContext; static CGLContextObj s_GLContext3; @@ -515,13 +533,19 @@ struct IncludeContext }; -static bool C_DECL IncludeOpenCallback(bool isSystem, const char* fname, std::string& output, void* d) +static bool C_DECL IncludeOpenCallback( bool isSystem, const char* fname, const char** outdata, unsigned int* outbytes, void* d ) { const IncludeContext* data = reinterpret_cast(d); std::string pathName = data->currentFolder + "/" + fname; - return ReadStringFromFile(pathName.c_str(), output); + return ReadStringFromFile(pathName.c_str(), outdata, outbytes ); +} + + +static void C_DECL IncludeCloseCallback( const char* data ) +{ + delete[] data; } @@ -553,11 +577,16 @@ static bool TestFile (TestRun type, IncludeContext includeCtx; includeCtx.currentFolder = inputPath.substr(0, inputPath.rfind('/')); - Hlsl2Glsl_ParseCallbacks includeCB; - includeCB.includeOpenCallback = IncludeOpenCallback; - includeCB.data = &includeCtx; + Hlsl2Glsl_PreprocessorData preprocessorData; + preprocessorData.includeOpenCallback = IncludeOpenCallback; + preprocessorData.includeCloseCallback = IncludeCloseCallback; + preprocessorData.malloc = NULL; + preprocessorData.free = NULL; + preprocessorData.defines = NULL; + preprocessorData.defineCount = 0; + preprocessorData.callbackData = &includeCtx; - int parseOk = Hlsl2Glsl_Parse (parser, sourceStr, version, &includeCB, options); + int parseOk = Hlsl2Glsl_Parse (parser, sourceStr, version, &preprocessorData, options); const char* infoLog = Hlsl2Glsl_GetInfoLog( parser ); if (kDumpShaderAST) {