summaryrefslogtreecommitdiffstats
path: root/tools/render-test/slang-support.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-06-09 11:34:21 -0700
committerTim Foley <tfoley@nvidia.com>2017-06-09 13:44:59 -0700
commitfcf83dbf9effab3bd98bad2b83b2468b7eb05cfd (patch)
tree41047c94883b86ec085a81597391ce3ef557cd43 /tools/render-test/slang-support.cpp
parent52e8d4b9a27ab0060f874c3a63ab531847be35c0 (diff)
Initial import of code.
Diffstat (limited to 'tools/render-test/slang-support.cpp')
-rw-r--r--tools/render-test/slang-support.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/tools/render-test/slang-support.cpp b/tools/render-test/slang-support.cpp
new file mode 100644
index 000000000..5aafc562e
--- /dev/null
+++ b/tools/render-test/slang-support.cpp
@@ -0,0 +1,84 @@
+// slang-support.cpp
+
+#define SLANG_INCLUDE_IMPLEMENTATION
+
+#include "slang-support.h"
+
+#include <stdio.h>
+
+namespace renderer_test {
+
+struct SlangShaderCompilerWrapper : public ShaderCompiler
+{
+ ShaderCompiler* innerCompiler;
+ SlangCompileTarget target;
+
+ virtual ShaderProgram* compileProgram(ShaderCompileRequest const& request) override
+ {
+ SlangSession* slangSession = spCreateSession(NULL);
+ SlangCompileRequest* slangRequest = spCreateCompileRequest(slangSession);
+
+ spSetCodeGenTarget(slangRequest, target);
+
+ int translationUnitIndex = spAddTranslationUnit(slangRequest, SLANG_SOURCE_LANGUAGE_SLANG, nullptr);
+
+ spAddTranslationUnitSourceString(slangRequest, translationUnitIndex, request.source.path, request.source.text);
+
+ int vertexEntryPoint = spAddTranslationUnitEntryPoint(slangRequest, translationUnitIndex, request.vertexShader.name, spFindProfile(slangSession, request.vertexShader.profile));
+ int fragmentEntryPoint = spAddTranslationUnitEntryPoint(slangRequest, translationUnitIndex, request.fragmentShader.name, spFindProfile(slangSession, request.fragmentShader.profile));
+
+ int compileErr = spCompile(slangRequest);
+ if(auto diagnostics = spGetDiagnosticOutput(slangRequest))
+ {
+ // TODO(tfoley): re-enable when I get a logging solution in place
+// OutputDebugStringA(diagnostics);
+ fprintf(stderr, "%s", diagnostics);
+ }
+ if(compileErr)
+ {
+ return nullptr;
+ }
+
+ char const* translatedCode = spGetTranslationUnitSource(slangRequest, translationUnitIndex);
+ char const* vertexCode = spGetEntryPointSource(slangRequest, translationUnitIndex, vertexEntryPoint);
+ char const* fragmentCode = spGetEntryPointSource(slangRequest, translationUnitIndex, fragmentEntryPoint);
+
+ ShaderCompileRequest innerRequest = request;
+ innerRequest.source.text = translatedCode;
+ innerRequest.vertexShader.source.text = vertexCode;
+ innerRequest.fragmentShader.source.text = fragmentCode;
+
+
+ auto result = innerCompiler->compileProgram(innerRequest);
+
+ // We clean up the Slang compilation context and result *after*
+ // we have run the downstream compiler, because Slang
+ // owns the memory allocation for the generated text, and will
+ // free it when we destroy the compilation result.
+ spDestroyCompileRequest(slangRequest);
+ spDestroySession(slangSession);
+
+ return result;
+ }
+};
+
+ShaderCompiler* createSlangShaderCompiler(ShaderCompiler* innerCompiler, SlangCompileTarget target)
+{
+ auto result = new SlangShaderCompilerWrapper();
+ result->innerCompiler = innerCompiler;
+ result->target = target;
+
+ return result;
+
+}
+
+
+} // renderer_test
+
+//
+// In order to actually use Slang in our application, we need to link in its
+// implementation. The easiest way to accomplish this is by directly inlcuding
+// the (concatenated) Slang source code into our app.
+//
+
+#include <slang.h>