summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-07-09 18:08:22 -0700
committerTim Foley <tfoley@nvidia.com>2017-07-09 18:08:22 -0700
commit064c28f58e845be67a31283cb885b22f32118f49 (patch)
tree559cde481de3dc37ad0e571380367bb0a6ebc3ca /source
parent54364eb1aef3fc6aa87b144ba91ca0a77818e4a4 (diff)
Pick layout rules based on target languge, not source.
The tricky bit here was that the `reflection-json` output format isn't really a code generation target like the others, and we need to be able to have multiple "targets" active to make sense of it. This needs cleaning-up.
Diffstat (limited to 'source')
-rw-r--r--source/core/smart-pointer.h2
-rw-r--r--source/slang/compiled-program.h13
-rw-r--r--source/slang/compiler.cpp24
-rw-r--r--source/slang/compiler.h7
-rw-r--r--source/slang/lower.cpp1
-rw-r--r--source/slang/options.cpp1
-rw-r--r--source/slang/parameter-binding.cpp40
-rw-r--r--source/slang/slang.cpp56
-rw-r--r--source/slang/slang.vcxproj1
-rw-r--r--source/slang/slang.vcxproj.filters1
-rw-r--r--source/slang/syntax-visitors.h1
-rw-r--r--source/slang/type-layout.cpp13
-rw-r--r--source/slang/type-layout.h3
13 files changed, 98 insertions, 65 deletions
diff --git a/source/core/smart-pointer.h b/source/core/smart-pointer.h
index 19ddde931..fea149e06 100644
--- a/source/core/smart-pointer.h
+++ b/source/core/smart-pointer.h
@@ -163,7 +163,7 @@ namespace Slang
~RefPtr()
{
- releaseReference(pointer);
+ releaseReference((Slang::RefObject*) pointer);
}
T& operator*() const
diff --git a/source/slang/compiled-program.h b/source/slang/compiled-program.h
deleted file mode 100644
index 7f5b674a0..000000000
--- a/source/slang/compiled-program.h
+++ /dev/null
@@ -1,13 +0,0 @@
-#ifndef BAKER_SL_COMPILED_PROGRAM_H
-#define BAKER_SL_COMPILED_PROGRAM_H
-
-#include "../core/basic.h"
-#include "diagnostics.h"
-#include "syntax.h"
-#include "type-layout.h"
-
-namespace Slang
-{
-}
-
-#endif \ No newline at end of file
diff --git a/source/slang/compiler.cpp b/source/slang/compiler.cpp
index 19623a7aa..f9c188764 100644
--- a/source/slang/compiler.cpp
+++ b/source/slang/compiler.cpp
@@ -520,17 +520,9 @@ namespace Slang
void generateOutput(
CompileRequest* compileRequest)
{
- switch (compileRequest->Target)
+ // Allow for an "extra" target to verride things first.
+ switch (compileRequest->extraTarget)
{
- default:
- // For most targets, we will do things per-translation-unit
- for( auto translationUnit : compileRequest->translationUnits )
- {
- TranslationUnitResult translationUnitResult = emitTranslationUnit(translationUnit.Ptr());
- translationUnit->result = translationUnitResult;
- }
- break;
-
case CodeGenTarget::ReflectionJSON:
{
String reflectionJSON = emitReflectionJSON(compileRequest->layout.Ptr());
@@ -538,8 +530,20 @@ namespace Slang
// HACK(tfoley): just print it out since that is what people probably expect.
// TODO: need a way to control where output gets routed across all possible targets.
fprintf(stdout, "%s", reflectionJSON.begin());
+
+ return;
}
break;
+
+ default:
+ break;
+ }
+
+ // For most targets, we will do things per-translation-unit
+ for( auto translationUnit : compileRequest->translationUnits )
+ {
+ TranslationUnitResult translationUnitResult = emitTranslationUnit(translationUnit.Ptr());
+ translationUnit->result = translationUnitResult;
}
}
}
diff --git a/source/slang/compiler.h b/source/slang/compiler.h
index b3bcd7ba1..e106973bc 100644
--- a/source/slang/compiler.h
+++ b/source/slang/compiler.h
@@ -3,11 +3,9 @@
#include "../core/basic.h"
-#include "compiled-program.h"
#include "diagnostics.h"
#include "profile.h"
#include "syntax.h"
-#include "type-layout.h"
#include "../../slang.h"
@@ -15,6 +13,7 @@ namespace Slang
{
struct IncludeHandler;
class CompileRequest;
+ class ProgramLayout;
enum class CompilerMode
{
@@ -170,6 +169,10 @@ namespace Slang
// What target language are we compiling to?
CodeGenTarget Target = CodeGenTarget::Unknown;
+ // An "extra" target that might override the first one
+ // when it comes to deciding output format, etc.
+ CodeGenTarget extraTarget = CodeGenTarget::Unknown;
+
// Directories to search for `#include` files or `import`ed modules
List<SearchDirectory> searchDirectories;
diff --git a/source/slang/lower.cpp b/source/slang/lower.cpp
index 0bb047791..55dabb826 100644
--- a/source/slang/lower.cpp
+++ b/source/slang/lower.cpp
@@ -1,6 +1,7 @@
// lower.cpp
#include "lower.h"
+#include "type-layout.h"
#include "visitor.h"
namespace Slang
diff --git a/source/slang/options.cpp b/source/slang/options.cpp
index db949e78d..53c441f6b 100644
--- a/source/slang/options.cpp
+++ b/source/slang/options.cpp
@@ -5,6 +5,7 @@
#include "../../slang.h"
+#include "compiler.h"
#include "profile.h"
#include <assert.h>
diff --git a/source/slang/parameter-binding.cpp b/source/slang/parameter-binding.cpp
index 5811fd9fa..228af7d99 100644
--- a/source/slang/parameter-binding.cpp
+++ b/source/slang/parameter-binding.cpp
@@ -137,9 +137,6 @@ struct SharedParameterBindingContext
// The program layout we are trying to construct
RefPtr<ProgramLayout> programLayout;
- // The source language we are trying to use
- SourceLanguage sourceLanguage;
-
// Information on what ranges of "registers" have already
// been claimed, for each resource type
UsedRanges usedResourceRanges[kLayoutResourceKindCount];
@@ -160,6 +157,9 @@ struct ParameterBindingContext
// What stage (if any) are we compiling for?
Stage stage;
+
+ // The source language we are trying to use
+ SourceLanguage sourceLanguage;
};
struct LayoutSemanticInfo
@@ -398,7 +398,7 @@ getTypeLayoutForGlobalShaderParameter(
ParameterBindingContext* context,
VarDeclBase* varDecl)
{
- switch( context->shared->sourceLanguage )
+ switch( context->sourceLanguage )
{
case SourceLanguage::Slang:
case SourceLanguage::HLSL:
@@ -1167,6 +1167,7 @@ static void collectParameters(
for( auto& translationUnit : request->translationUnits )
{
context->stage = inferStageForTranslationUnit(translationUnit.Ptr());
+ context->sourceLanguage = translationUnit->sourceLanguage;
// First look at global-scope parameters
collectGlobalScopeParameters(context, translationUnit->SyntaxNode.Ptr());
@@ -1189,31 +1190,13 @@ static void collectParameters(
void generateParameterBindings(
CompileRequest* request)
{
- // TODO: infer a language or set of language rules to use based on the
- // source files and entry points given
- auto language = SourceLanguage::Unknown;
- for( auto& translationUnit : request->translationUnits )
- {
- auto translationUnitLanguage = translationUnit->sourceLanguage;
- if( language == SourceLanguage::Unknown )
- {
- language = translationUnitLanguage;
- }
- else if( language == translationUnitLanguage )
- {
- // same language: nothing to do...
- }
- else
- {
- // mismatch!
- // TODO(tfoley): emit a diagnostic
- }
- }
+ // Try to find rules based on the selected code-generation target
+ auto rules = GetLayoutRulesFamilyImpl(request->Target);
- // TODO(tfoley): We should really be picking layout rules
- // based on the *target* language, and not the source...
- auto rules = GetLayoutRulesFamilyImpl(language);
- assert(rules);
+ // If there was no target, or there are no rules for the target,
+ // then bail out here.
+ if (!rules)
+ return;
RefPtr<ProgramLayout> programLayout = new ProgramLayout;
@@ -1222,7 +1205,6 @@ void generateParameterBindings(
SharedParameterBindingContext sharedContext;
sharedContext.defaultLayoutRules = rules;
sharedContext.programLayout = programLayout;
- sharedContext.sourceLanguage = language;
// Create a sub-context to collect parameters that get
// declared into the global scope
diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp
index e5bfcb923..af68060cd 100644
--- a/source/slang/slang.cpp
+++ b/source/slang/slang.cpp
@@ -179,6 +179,30 @@ void CompileRequest::checkAllTranslationUnits()
checkTranslationUnit(translationUnit.Ptr());
}
}
+// Try to infer a single common source language for a request
+static SourceLanguage inferSourceLanguage(CompileRequest* request)
+{
+ SourceLanguage language = SourceLanguage::Unknown;
+ for (auto& translationUnit : request->translationUnits)
+ {
+ // Allow any other language to overide Slang as a choice
+ if (language == SourceLanguage::Unknown
+ || language == SourceLanguage::Slang)
+ {
+ language = translationUnit->sourceLanguage;
+ }
+ else if (language == translationUnit->sourceLanguage)
+ {
+ // same language as we currently have, so keep going
+ }
+ else
+ {
+ // we found a mismatch, so inference fails
+ return SourceLanguage::Unknown;
+ }
+ }
+ return language;
+}
int CompileRequest::executeActionsInner()
{
@@ -197,6 +221,26 @@ int CompileRequest::executeActionsInner()
}
}
+ // If no code-generation target was specified, then try to infer one from the source language,
+ // just to make sure we can do something reasonable when `reflection-json` is specified
+ if (Target == CodeGenTarget::Unknown)
+ {
+ auto language = inferSourceLanguage(this);
+ switch (language)
+ {
+ case SourceLanguage::HLSL:
+ Target = CodeGenTarget::DXBytecodeAssembly;
+ break;
+
+ case SourceLanguage::GLSL:
+ Target = CodeGenTarget::SPIRVAssembly;
+ break;
+
+ default:
+ break;
+ }
+ }
+
#if 0
// If we are being asked to do pass-through, then we need to do that here...
if (passThrough != PassThroughMode::None)
@@ -628,7 +672,17 @@ SLANG_API void spSetCodeGenTarget(
SlangCompileRequest* request,
int target)
{
- REQ(request)->Target = (Slang::CodeGenTarget)target;
+ if (target == SLANG_REFLECTION_JSON)
+ {
+ // HACK: We special case this because reflection JSON is actually
+ // an additional output step that layers on top of an existing
+ // target
+ REQ(request)->extraTarget = Slang::CodeGenTarget::ReflectionJSON;
+ }
+ else
+ {
+ REQ(request)->Target = (Slang::CodeGenTarget)target;
+ }
}
SLANG_API void spSetPassThrough(
diff --git a/source/slang/slang.vcxproj b/source/slang/slang.vcxproj
index 441bc5e45..17fdea627 100644
--- a/source/slang/slang.vcxproj
+++ b/source/slang/slang.vcxproj
@@ -165,7 +165,6 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\slang.h" />
- <ClInclude Include="compiled-program.h" />
<ClInclude Include="compiler.h" />
<ClInclude Include="decl-defs.h" />
<ClInclude Include="diagnostic-defs.h" />
diff --git a/source/slang/slang.vcxproj.filters b/source/slang/slang.vcxproj.filters
index 21b533a10..a276ae95b 100644
--- a/source/slang/slang.vcxproj.filters
+++ b/source/slang/slang.vcxproj.filters
@@ -4,7 +4,6 @@
<Natvis Include="slang.natvis" />
</ItemGroup>
<ItemGroup>
- <ClInclude Include="compiled-program.h" />
<ClInclude Include="compiler.h" />
<ClInclude Include="diagnostic-defs.h" />
<ClInclude Include="diagnostics.h" />
diff --git a/source/slang/syntax-visitors.h b/source/slang/syntax-visitors.h
index 85a20a7cf..4ccc7fa0b 100644
--- a/source/slang/syntax-visitors.h
+++ b/source/slang/syntax-visitors.h
@@ -3,7 +3,6 @@
#include "diagnostics.h"
#include "syntax.h"
-#include "compiled-program.h"
namespace Slang
{
diff --git a/source/slang/type-layout.cpp b/source/slang/type-layout.cpp
index 10c7327ce..31c8ed607 100644
--- a/source/slang/type-layout.cpp
+++ b/source/slang/type-layout.cpp
@@ -543,15 +543,18 @@ LayoutRulesFamilyImpl* GetLayoutRulesFamilyImpl(LayoutRulesFamily rule)
}
}
-LayoutRulesFamilyImpl* GetLayoutRulesFamilyImpl(SourceLanguage language)
+LayoutRulesFamilyImpl* GetLayoutRulesFamilyImpl(CodeGenTarget target)
{
- switch (language)
+ switch (target)
{
- case SourceLanguage::Slang:
- case SourceLanguage::HLSL:
+ case CodeGenTarget::HLSL:
+ case CodeGenTarget::DXBytecode:
+ case CodeGenTarget::DXBytecodeAssembly:
return &kHLSLLayoutRulesFamilyImpl;
- case SourceLanguage::GLSL:
+ case CodeGenTarget::GLSL:
+ case CodeGenTarget::SPIRV:
+ case CodeGenTarget::SPIRVAssembly:
return &kGLSLLayoutRulesFamilyImpl;
default:
diff --git a/source/slang/type-layout.h b/source/slang/type-layout.h
index ce1f8864d..3ee12656c 100644
--- a/source/slang/type-layout.h
+++ b/source/slang/type-layout.h
@@ -2,6 +2,7 @@
#define SLANG_TYPE_LAYOUT_H
#include "../core/basic.h"
+#include "compiler.h"
#include "profile.h"
#include "syntax.h"
@@ -513,7 +514,7 @@ struct LayoutRulesFamilyImpl
LayoutRulesImpl* GetLayoutRulesImpl(LayoutRule rule);
LayoutRulesFamilyImpl* GetLayoutRulesFamilyImpl(LayoutRulesFamily rule);
-LayoutRulesFamilyImpl* GetLayoutRulesFamilyImpl(SourceLanguage language);
+LayoutRulesFamilyImpl* GetLayoutRulesFamilyImpl(CodeGenTarget target);
SimpleLayoutInfo GetLayout(ExpressionType* type, LayoutRulesImpl* rules);