diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2022-08-16 16:24:37 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-16 13:24:37 -0700 |
| commit | a10a94dcb86e51d74d7c545d99d98110f88e3081 (patch) | |
| tree | 03da2e66ff793aa4f68a23a47a81d5cbb097206e /source | |
| parent | 42de00db3ffe07599fff6d47d0d7228181ee3082 (diff) | |
Add some simple macro defines including __HLSL_VERSION (#2363)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Add standard macros including __HLSL_VERSION
* Added hlsl test.
* Defined standard macro names to values, so not undefined. Allows more convenient #if style.
Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/slang.cpp | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 4f135d144..710cc0b45 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -1964,6 +1964,9 @@ void FrontEndCompileRequest::parseTranslationUnit( break; } + // TODO(JS): + // Note! that a adding a define twice will cause an exception in debug builds + // that may be desirable or not... Dictionary<String, String> combinedPreprocessorDefinitions; for(auto& def : getLinkage()->preprocessorDefinitions) combinedPreprocessorDefinitions.Add(def.Key, def.Value); @@ -1972,12 +1975,45 @@ void FrontEndCompileRequest::parseTranslationUnit( for(auto& def : translationUnit->preprocessorDefinitions) combinedPreprocessorDefinitions.Add(def.Key, def.Value); + // Define standard macros, if not already defined. This style assumes using `#if __SOME_VAR` style, as in + // + // ``` + // #if __SLANG_COMPILER__ + // ``` + // + // This choice is made because slang outputs a warning on using a variable in an #if if not defined + // + // Of course this means using #ifndef/#ifdef/defined() is probably not appropraite with thes variables. + { + // Used to identify level of HLSL language compatibility + combinedPreprocessorDefinitions.AddIfNotExists("__HLSL_VERSION", "2020"); + + // Indicates this is being compiled by the slang *compiler* + combinedPreprocessorDefinitions.AddIfNotExists("__SLANG_COMPILER__", "1"); + + // Set macro depending on source type + switch (translationUnit->sourceLanguage) + { + case SourceLanguage::HLSL: + // Used to indicate compiled as HLSL language + combinedPreprocessorDefinitions.AddIfNotExists("__HLSL__", "1"); + break; + case SourceLanguage::Slang: + // Used to indicate compiled as Slang language + combinedPreprocessorDefinitions.AddIfNotExists("__SLANG__", "1"); + break; + default: break; + } + + // If not set, define as 0. + combinedPreprocessorDefinitions.AddIfNotExists("__HLSL__", "0"); + combinedPreprocessorDefinitions.AddIfNotExists("__SLANG__", "0"); + } + auto module = translationUnit->getModule(); ASTBuilder* astBuilder = module->getASTBuilder(); - //ASTBuilder* astBuilder = linkage->getASTBuilder(); - ModuleDecl* translationUnitSyntax = astBuilder->create<ModuleDecl>(); translationUnitSyntax->nameAndLoc.name = translationUnit->moduleName; @@ -2060,8 +2096,6 @@ void FrontEndCompileRequest::parseTranslationUnit( } } - - #if 0 // Test serialization { |
