summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2020-06-18 11:38:30 -0400
committerGitHub <noreply@github.com>2020-06-18 11:38:30 -0400
commit5a86cd4880f8f086631352cb5d67d60c58c087f4 (patch)
tree2f65acc5158f4c6632f299de6f4600f20b93c35a
parent31ae3467242995ab822a29c4148c2e86df2f1eb8 (diff)
Improvements around C++ code generation (#1396)
* * Remove UniformState and UniformEntryPointParams types * Put all output C++ source in an anonymous namespace * If SLANG_PRELUDE_NAMESPACE is set, make what it defines available in generated file. * Fix signature issue in performance-profile.slang * Context -> KernelContext to avoid ambiguity. * Fix issues around dynamic dispatch and anonymous namespace. * Fix typo.
-rw-r--r--examples/cpu-hello-world/main.cpp2
-rw-r--r--prelude/slang-cpp-prelude.h4
-rw-r--r--prelude/slang-cpp-types.h11
-rw-r--r--source/slang/slang-emit-cpp.cpp43
-rw-r--r--source/slang/slang-emit-cuda.cpp4
-rw-r--r--tests/compute/performance-profile.slang3
-rw-r--r--tools/render-test/cpu-compute-util.cpp4
7 files changed, 43 insertions, 28 deletions
diff --git a/examples/cpu-hello-world/main.cpp b/examples/cpu-hello-world/main.cpp
index 6e06f64bc..4302e6069 100644
--- a/examples/cpu-hello-world/main.cpp
+++ b/examples/cpu-hello-world/main.cpp
@@ -184,7 +184,7 @@ static SlangResult _innerMain(int argc, char** argv)
// We don't have any entry point parameters so that's passed as NULL
// We need to cast our definition of the uniform state to the undefined CPPPrelude::UniformState as
// that type is just a name to indicate what kind of thing needs to be passed in.
- func(&varyingInput, NULL, (CPPPrelude::UniformState*)&uniformState);
+ func(&varyingInput, NULL, &uniformState);
// bufferContents holds the output
diff --git a/prelude/slang-cpp-prelude.h b/prelude/slang-cpp-prelude.h
index 2e680f824..7da613024 100644
--- a/prelude/slang-cpp-prelude.h
+++ b/prelude/slang-cpp-prelude.h
@@ -38,6 +38,4 @@
# define SLANG_UNROLL
#endif
-#endif
-
-struct Context; \ No newline at end of file
+#endif \ No newline at end of file
diff --git a/prelude/slang-cpp-types.h b/prelude/slang-cpp-types.h
index 311fa667d..7e951fd63 100644
--- a/prelude/slang-cpp-types.h
+++ b/prelude/slang-cpp-types.h
@@ -822,14 +822,11 @@ struct ComputeVaryingInput
uint3 endGroupID; ///< Non inclusive end groupID
};
-/* Type that defines the uniform entry point params. The actual content of this type is dependent on the entry point parameters, and can be
-found via reflection or defined such that it matches the shader appropriately.
-*/
-struct UniformEntryPointParams;
-struct UniformState;
+// The uniformEntryPointParams and uniformState must be set to structures that match layout that the kernel expects.
+// This can be determined via reflection for example.
-typedef void(*ComputeThreadFunc)(ComputeThreadVaryingInput* varyingInput, UniformEntryPointParams* uniformEntryPointParams, UniformState* uniformState);
-typedef void(*ComputeFunc)(ComputeVaryingInput* varyingInput, UniformEntryPointParams* uniformEntryPointParams, UniformState* uniformState);
+typedef void(*ComputeThreadFunc)(ComputeThreadVaryingInput* varyingInput, void* uniformEntryPointParams, void* uniformState);
+typedef void(*ComputeFunc)(ComputeVaryingInput* varyingInput, void* uniformEntryPointParams, void* uniformState);
#ifdef SLANG_PRELUDE_NAMESPACE
}
diff --git a/source/slang/slang-emit-cpp.cpp b/source/slang/slang-emit-cpp.cpp
index c31ef3bc7..53a110632 100644
--- a/source/slang/slang-emit-cpp.cpp
+++ b/source/slang/slang-emit-cpp.cpp
@@ -1612,7 +1612,7 @@ void CPPSourceEmitter::_emitWitnessTableDefinitions()
m_writer->emit(",\n");
else
isFirstEntry = false;
- m_writer->emit("&Context::");
+ m_writer->emit("&KernelContext::");
m_writer->emit(getName(funcVal));
}
else
@@ -1660,7 +1660,7 @@ void CPPSourceEmitter::_maybeEmitWitnessTableTypeDefinition(
else
isFirstEntry = false;
emitType(funcVal->getResultType());
- m_writer->emit(" (Context::*");
+ m_writer->emit(" (KernelContext::*");
m_writer->emit(getName(entry->requirementKey.get()));
m_writer->emit(")");
m_writer->emit("(");
@@ -2134,6 +2134,20 @@ void CPPSourceEmitter::emitPreprocessorDirectivesImpl()
writer->emit("\n");
+
+ if (m_target == CodeGenTarget::CPPSource)
+ {
+ // Put all into an anonymous namespace
+ // This includes any generated types, and generated intrinsics
+
+ m_writer->emit("namespace { // anonymous \n\n");
+ m_writer->emit("#ifdef SLANG_PRELUDE_NAMESPACE\n");
+ m_writer->emit("using namespace SLANG_PRELUDE_NAMESPACE;\n");
+ m_writer->emit("#endif\n\n");
+
+ m_writer->emit("struct KernelContext;\n\n");
+ }
+
if (m_target == CodeGenTarget::CSource)
{
// For C output we need to emit type definitions.
@@ -2169,7 +2183,6 @@ void CPPSourceEmitter::emitPreprocessorDirectivesImpl()
{
_maybeEmitSpecializedOperationDefinition(intrinsic);
}
-
}
}
@@ -2293,14 +2306,14 @@ void CPPSourceEmitter::_emitEntryPointDefinitionStart(IRFunc* func, IRGlobalPara
m_writer->emit("(");
m_writer->emit(varyingTypeName);
- m_writer->emit("* varyingInput, UniformEntryPointParams* params, UniformState* uniformState)");
+ m_writer->emit("* varyingInput, void* params, void* uniformState)");
emitSemantics(func);
m_writer->emit("\n{\n");
m_writer->indent();
// Initialize when constructing so that globals are zeroed
- m_writer->emit("Context context = {};\n");
- m_writer->emit("context.uniformState = uniformState;\n");
+ m_writer->emit("KernelContext context = {};\n");
+ m_writer->emit("context.uniformState = (UniformState*)uniformState;\n");
if (entryPointGlobalParams)
{
@@ -2590,11 +2603,11 @@ void CPPSourceEmitter::emitModuleImpl(IRModule* module)
List<EmitAction> actions;
computeEmitActions(module, actions);
-
+
_emitForwardDeclarations(actions);
IRGlobalParam* entryPointGlobalParams = nullptr;
-
+
// Output the global parameters in a 'UniformState' structure
{
m_writer->emit("struct UniformState\n{\n");
@@ -2605,15 +2618,14 @@ void CPPSourceEmitter::emitModuleImpl(IRModule* module)
m_writer->dedent();
m_writer->emit("\n};\n\n");
}
-
+
// Output the 'Context' which will be used for execution
{
- m_writer->emit("struct Context\n{\n");
+ m_writer->emit("struct KernelContext\n{\n");
m_writer->indent();
m_writer->emit("UniformState* uniformState;\n");
-
m_writer->emit("uint3 dispatchThreadID;\n");
//if (m_semanticUsedFlags & SemanticUsedFlag::GroupID)
@@ -2659,12 +2671,19 @@ void CPPSourceEmitter::emitModuleImpl(IRModule* module)
}
m_writer->dedent();
- m_writer->emit("};\n\n");
+ m_writer->emit("};\n\n");
}
// Emit all witness table definitions.
_emitWitnessTableDefinitions();
+ if (m_target == CodeGenTarget::CPPSource)
+ {
+ // Need to close the anonymous namespace when outputting for C++
+
+ m_writer->emit("} // anonymous\n\n");
+ }
+
// Finally we need to output dll entry points
for (auto action : actions)
diff --git a/source/slang/slang-emit-cuda.cpp b/source/slang/slang-emit-cuda.cpp
index 25b06027d..06bbbea96 100644
--- a/source/slang/slang-emit-cuda.cpp
+++ b/source/slang/slang-emit-cuda.cpp
@@ -866,10 +866,10 @@ void CUDASourceEmitter::emitModuleImpl(IRModule* module)
//
// At the binary level, our generated CUDA compute kernels will take
// two pointer parameters: the first points to the per-entry-point
- // `uniform` parameter data, and the second poinst to the global-scope
+ // `uniform` parameter data, and the second points to the global-scope
// parameter data (if any).
//
- m_writer->emit("(UniformEntryPointParams* entryPointShaderParameters, UniformState* uniformState)");
+ m_writer->emit("(void* entryPointShaderParameters, void* uniformState)");
}
else
{
diff --git a/tests/compute/performance-profile.slang b/tests/compute/performance-profile.slang
index bf67a4478..22f73e075 100644
--- a/tests/compute/performance-profile.slang
+++ b/tests/compute/performance-profile.slang
@@ -44,8 +44,9 @@ static void _calc(const RWStructuredBuffer<float>& buf, int start, int end)
}
SLANG_PRELUDE_EXPORT
-void computeMain(ComputeVaryingInput* varyingInput, UniformEntryPointParams* params, LocalUniformState* uniformState)
+void computeMain(ComputeVaryingInput* varyingInput, void* inParams, void* inUniformState)
{
+ LocalUniformState* uniformState = (LocalUniformState*)inUniformState;
_calc(uniformState->outputBuffer_0, varyingInput->startGroupID.x * 16, varyingInput->endGroupID.x * 16);
}
diff --git a/tools/render-test/cpu-compute-util.cpp b/tools/render-test/cpu-compute-util.cpp
index e8b9e8b32..dc3ea1afa 100644
--- a/tools/render-test/cpu-compute-util.cpp
+++ b/tools/render-test/cpu-compute-util.cpp
@@ -619,8 +619,8 @@ static SlangResult _newTexture(const InputTextureDesc& desc, slang::TypeLayoutRe
/* static */SlangResult CPUComputeUtil::execute(const ExecuteInfo& info)
{
- CPPPrelude::UniformState* uniformState = (CPPPrelude::UniformState*)info.m_uniformState;
- CPPPrelude::UniformEntryPointParams* uniformEntryPointParams = (CPPPrelude::UniformEntryPointParams*)info.m_uniformEntryPointParams;
+ void* uniformState = info.m_uniformState;
+ void* uniformEntryPointParams = info.m_uniformEntryPointParams;
switch (info.m_style)
{