summaryrefslogtreecommitdiffstats
path: root/examples/cpu-hello-world
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2020-09-26 20:09:50 -0700
committerGitHub <noreply@github.com>2020-09-26 20:09:50 -0700
commit94d3f2bd9c5557658751f73bc5fc443b41230d2c (patch)
treea028c7f2a345e1e7af86aad6a63f6f0fddc74785 /examples/cpu-hello-world
parentb72353ec3fe529237828cacbe710233d31eb4837 (diff)
Add API for whole program compilation. (#1562)
* Add API for whole program compilation. This change exposes a new target flag: `SLANG_TARGET_FLAG_GENERATE_WHOLE_PROGRAM` that can be set on a target with `spSetTargetFlags`. When this flag is set, `spCompile` function generates target code for the entire input module instead of just the specified entrypoints. The resulting code will include all the entrypoints defined in the input source. The resulting whole program code can be retrieved with two new functions: `spGetTargetCodeBlob` and `spGetTargetHostCallable`. This change also cleans up the unnecessary `entryPointIndices` parameter of `TargetProgram::getOrCreateWholeProgramResult`, and modifies the `cpu-hello-world` example to make use of the new whole-program compilation API to simplify its logic. * Update comments.
Diffstat (limited to 'examples/cpu-hello-world')
-rw-r--r--examples/cpu-hello-world/main.cpp16
-rw-r--r--examples/cpu-hello-world/shader.slang1
2 files changed, 7 insertions, 10 deletions
diff --git a/examples/cpu-hello-world/main.cpp b/examples/cpu-hello-world/main.cpp
index ee919a713..cf8c57285 100644
--- a/examples/cpu-hello-world/main.cpp
+++ b/examples/cpu-hello-world/main.cpp
@@ -85,6 +85,10 @@ static SlangResult _innerMain(int argc, char** argv)
// If we wanted a just a shared library/dll, we could have used SLANG_SHARED_LIBRARY.
int targetIndex = spAddCodeGenTarget(slangRequest, SLANG_HOST_CALLABLE);
+ // Set the target flag to indicate that we want to compile all the entrypoints in the
+ // slang shader file into a library.
+ spSetTargetFlags(slangRequest, targetIndex, SLANG_TARGET_FLAG_GENERATE_WHOLE_PROGRAM);
+
// A compile request can include one or more "translation units," which more or
// less amount to individual source files (think `.c` files, not the `.h` files they
// might include).
@@ -99,14 +103,6 @@ static SlangResult _innerMain(int argc, char** argv)
// There are also variations of this API for adding source code from application-provided buffers.
//
spAddTranslationUnitSourceFile(slangRequest, translationUnitIndex, "shader.slang");
-
- // Next we will specify the entry points we'd like to compile.
- // It is often convenient to put more than one entry point in the same file,
- // and the Slang API makes it convenient to use a single run of the compiler
- // to compile all entry points.
- //
- const char entryPointName[] = "computeMain";
- int computeIndex = spAddEntryPoint(slangRequest, translationUnitIndex, entryPointName, SLANG_STAGE_COMPUTE);
// Once all of the input options for the compiler have been specified,
// we can invoke `spCompile` to run the compiler and see if any errors
@@ -133,7 +129,7 @@ static SlangResult _innerMain(int argc, char** argv)
// Get the 'shared library' (note that this doesn't necessarily have to be implemented as a shared library
// it's just an interface to executable code).
ComPtr<ISlangSharedLibrary> sharedLibrary;
- SLANG_RETURN_ON_FAIL(spGetEntryPointHostCallable(slangRequest, 0, 0, sharedLibrary.writeRef()));
+ SLANG_RETURN_ON_FAIL(spGetTargetHostCallable(slangRequest, 0, sharedLibrary.writeRef()));
// Once we have the sharedLibrary, we no longer need the request
// unless we want to use reflection, to for example workout how 'UniformState' and 'UniformEntryPointParams' are laid out
@@ -141,10 +137,10 @@ static SlangResult _innerMain(int argc, char** argv)
spDestroyCompileRequest(slangRequest);
// Get the function we are going to execute
+ const char entryPointName[] = "computeMain";
CPPPrelude::ComputeFunc func = (CPPPrelude::ComputeFunc)sharedLibrary->findFuncByName(entryPointName);
if (!func)
{
- spDestroyCompileRequest(slangRequest);
return SLANG_FAIL;
}
diff --git a/examples/cpu-hello-world/shader.slang b/examples/cpu-hello-world/shader.slang
index 6611962d7..f650c3481 100644
--- a/examples/cpu-hello-world/shader.slang
+++ b/examples/cpu-hello-world/shader.slang
@@ -3,6 +3,7 @@
//TEST_INPUT:ubuffer(random(float, 4096, -1.0, 1.0), stride=4):name=ioBuffer
RWStructuredBuffer<float> ioBuffer;
+[shader("compute")]
[numthreads(4, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{