Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

passing to mojoshader preprocessor not only open/close callback, but also defines and malloc/free callbacks #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hlslang/MachineIndependent/HLSL2GLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -337,7 +337,7 @@ int C_DECL Hlsl2Glsl_Parse(
parseContext.infoSink.info.message(EPrefixInternalError, "Wrong symbol table level");


int ret = PaParseString(const_cast<char*>(shaderString), parseContext, callbacks);
int ret = PaParseString(const_cast<char*>(shaderString), parseContext, preprocessorData );
if (ret)
success = false;

Expand Down
2 changes: 1 addition & 1 deletion hlslang/MachineIndependent/ParseHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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&);
Expand Down
46 changes: 25 additions & 21 deletions hlslang/MachineIndependent/hlslang.l
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hlsl2Glsl_ParseCallbacks*>(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<Hlsl2Glsl_PreprocessorData*>(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<char*>(data), NULL);
Hlsl2Glsl_PreprocessorData* preprocessorData = reinterpret_cast<Hlsl2Glsl_PreprocessorData*>(d);
preprocessorData->includeCloseCallback( data );
}

//
Expand All @@ -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;

Expand All @@ -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;
Expand Down
27 changes: 20 additions & 7 deletions include/hlsl2glsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
43 changes: 36 additions & 7 deletions tests/hlsl2glsltest/hlsl2glsltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<IncludeContext*>(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;
}


Expand Down Expand Up @@ -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)
{
Expand Down