yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
288a63dea
master
1// main.cpp 2 3#include "slang.h" 4 5SLANG_API void spSetCommandLineCompilerMode (SlangCompileRequest * request ); 6 7#include "../core/slang-io.h" 8#include "../core/slang-test-tool-util.h" 9#include "../slang/slang-internal.h" 10 11using namespace Slang ; 12 13#include <assert.h> 14 15#ifdef _WIN32 16#define MAIN slangc_main 17#else 18#define MAIN main 19#endif 20 21static void _diagnosticCallback (char const * message ,void * /*userData*/ ) 22{ 23auto stdError = StdWriters ::getError (); 24stdError .put (message ); 25stdError .flush (); 26} 27 28static SlangResult _compile (SlangCompileRequest * compileRequest ,int argc ,const char * const * argv ) 29{ 30spSetDiagnosticCallback (compileRequest ,& _diagnosticCallback ,nullptr ); 31spSetCommandLineCompilerMode (compileRequest ); 32 33SlangResult res = spProcessCommandLineArguments (compileRequest ,& argv [1 ],argc - 1 ); 34if (SLANG_FAILED (res )) 35 { 36// TODO: print usage message 37return res ; 38 } 39 40res = SLANG_OK ; 41 42#ifndef _DEBUG 43try 44#endif 45 { 46// Run the compiler (this will produce any diagnostics through 47// SLANG_WRITER_TARGET_TYPE_DIAGNOSTIC). 48res = spCompile (compileRequest ); 49// If the compilation failed, then get out of here... 50// Turn into an internal Result -> such that return code can be used to vary result to match 51// previous behavior 52res = SLANG_FAILED (res ) ?SLANG_E_INTERNAL_FAIL :res ; 53 } 54#ifndef _DEBUG 55catch (const Exception & e ) 56 { 57StdWriters ::getOut ()."internal compiler error: %S\n" ,e .Message .toWString ().begin ()); 58res = SLANG_FAIL ; 59 } 60#endif 61 62return res ; 63} 64 65bool shouldEmbedPrelude (const char * const * argv ,int argc ) 66{ 67for (int i = 0 ;i < argc ;i ++ ) 68 { 69if (UnownedStringSlice (argv [i ])== "-embed-prelude" ) 70return true; 71 } 72return false; 73} 74 75SLANG_TEST_TOOL_API SlangResult innerMain ( 76StdWriters * stdWriters , 77 slang::IGlobalSession * sharedSession , 78int argc , 79const char * const * argv ) 80{ 81StdWriters ::setSingleton (stdWriters ); 82 83// Assume we will used the shared session 84ComPtr < slang::IGlobalSession > session (sharedSession ); 85 86// The sharedSession always has a pre-loaded core module, is sharedSession is not nullptr. 87// This differed test checks if the command line has an option to setup the core module. 88// If so we *don't* use the sharedSession, and create a new session without the core module just 89// for this compilation. 90if (TestToolUtil ::hasDeferredCoreModule (Index (argc - 1 ),argv + 1 )) 91 { 92SLANG_RETURN_ON_FAIL ( 93slang_createGlobalSessionWithoutCoreModule (SLANG_API_VERSION ,session .writeRef ())); 94 } 95else if (!session ) 96 { 97// Just create the global session in the regular way if there isn't one set 98SlangGlobalSessionDesc desc = {}; 99desc .enableGLSL = true; 100Slang ::GlobalSessionInternalDesc internalDesc = {}; 101#ifdef SLANG_BOOTSTRAP 102internalDesc .isBootstrap = true; 103#endif 104SLANG_RETURN_ON_FAIL ( 105slang_createGlobalSessionImpl (& desc ,& internalDesc ,session .writeRef ())); 106 } 107 108if (!shouldEmbedPrelude (argv ,argc )) 109TestToolUtil ::setSessionDefaultPreludeFromExePath (argv [0 ],session ); 110 111SlangCompileRequest * compileRequest = spCreateCompileRequest (session ); 112compileRequest -> addSearchPath (Path ::getParentDirectory (Path ::getExecutablePath ()).getBuffer ()); 113SlangResult res = _compile (compileRequest ,argc ,argv ); 114// Now that we are done, clean up after ourselves 115spDestroyCompileRequest (compileRequest ); 116 117return res ; 118} 119 120int MAIN (int argc ,char ** argv ) 121{ 122auto stdWriters = StdWriters ::initDefaultSingleton (); 123SlangResult res = innerMain (stdWriters ,nullptr ,argc ,argv ); 124 slang::shutdown (); 125return (int )TestToolUtil ::getReturnCode (res ); 126} 127 128#ifdef _WIN32 129int wmain (int argc ,wchar_t ** argv ) 130{ 131int result = 0 ; 132 133 { 134// Convert the wide-character Unicode arguments to UTF-8, 135// since that is what Slang expects on the API side. 136 137List < String > args ; 138for (int ii = 0 ;ii < argc ;++ ii ) 139 { 140args .add (String ::fromWString (argv [ii ])); 141 } 142List < char const *> argBuffers ; 143for (int ii = 0 ;ii < argc ;++ ii ) 144 { 145argBuffers .add (args [ii ].getBuffer ()); 146 } 147 148result = MAIN (argc , (char ** )& argBuffers [0 ]); 149 } 150 151#ifdef _MSC_VER 152// _CrtXXX functions are functional only for debug build. The spec says, 153// "When _DEBUG isn't defined, calls to _CrtSetReportMode are removed 154// during preprocessing." 155_CrtSetReportMode (_CRT_WARN ,_CRTDBG_MODE_FILE |_CRTDBG_MODE_DEBUG ); 156_CrtSetReportFile (_CRT_WARN ,_CRTDBG_FILE_STDERR ); 157_CrtSetReportMode (_CRT_ERROR ,_CRTDBG_MODE_FILE |_CRTDBG_MODE_DEBUG ); 158_CrtSetReportFile (_CRT_ERROR ,_CRTDBG_FILE_STDERR ); 159_CrtSetReportMode (_CRT_ASSERT ,_CRTDBG_MODE_FILE |_CRTDBG_MODE_DEBUG ); 160_CrtSetReportFile (_CRT_ASSERT ,_CRTDBG_FILE_STDERR ); 161 162int memleakDetected = _CrtDumpMemoryLeaks (); 163SLANG_UNUSED (memleakDetected ); 164assert (!memleakDetected ); 165#endif 166 167return result ; 168} 169#endif