diff options
Diffstat (limited to 'examples/cpu-hello-world/main.cpp')
| -rw-r--r-- | examples/cpu-hello-world/main.cpp | 66 |
1 files changed, 37 insertions, 29 deletions
diff --git a/examples/cpu-hello-world/main.cpp b/examples/cpu-hello-world/main.cpp index 059efc8d3..60a24fa8c 100644 --- a/examples/cpu-hello-world/main.cpp +++ b/examples/cpu-hello-world/main.cpp @@ -3,7 +3,7 @@ #include <stdio.h> // This file implements an extremely simple example of loading and -// executing a Slang shader program on the CPU. +// executing a Slang shader program on the CPU. // // More information about generation C++ or CPU code can be found in docs/cpu-target.md // @@ -20,7 +20,7 @@ // Allows use of ComPtr - which we can use to scope any 'com-like' pointers easily #include "slang-com-ptr.h" -// Provides macros for handling SlangResult values easily +// Provides macros for handling SlangResult values easily #include "slang-com-helper.h" // This includes a useful small function for setting up the prelude (described more further below). @@ -34,10 +34,10 @@ using namespace Slang; // Slang source is converted into C++ code which is compiled by a backend compiler. // That process uses a 'prelude' which defines types and functions that are needed // for everything else to work. -// +// // We include the prelude here, so we can directly use the types as were used by the // compiled code. It is not necessary to include the prelude, as long as memory is -// laid out in the manner that the generated slang code expects. +// laid out in the manner that the generated slang code expects. #define SLANG_PRELUDE_NAMESPACE CPPPrelude #include "../../prelude/slang-cpp-types.h" @@ -75,11 +75,13 @@ static SlangResult _innerMain(int argc, char** argv) // // Most downstream C++ compilers work on files. In that case slang may generate temporary // files that contain the generated code. Typically the generated files will not be in the - // same directory as the original source so handling includes becomes awkward. The mechanism used here - // is for the prelude code to be an *absolute* path to the 'slang-cpp-prelude.h' - which means - // this will work wherever the generated code is, and allows accessing other files via relative paths. + // same directory as the original source so handling includes becomes awkward. The mechanism + // used here is for the prelude code to be an *absolute* path to the 'slang-cpp-prelude.h' - + // which means this will work wherever the generated code is, and allows accessing other files + // via relative paths. // - // Look at the source to TestToolUtil::setSessionDefaultPreludeFromExePath to see what's involed. + // Look at the source to TestToolUtil::setSessionDefaultPreludeFromExePath to see what's + // involed. TestToolUtil::setSessionDefaultPreludeFromExePath(argv[0], slangSession); slang::SessionDesc sessionDesc = {}; @@ -122,13 +124,16 @@ static SlangResult _innerMain(int argc, char** argv) SLANG_RETURN_ON_FAIL(result); } - // 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). + // 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; { ComPtr<slang::IBlob> diagnosticsBlob; SlangResult result = composedProgram->getEntryPointHostCallable( - 0, 0, sharedLibrary.writeRef(), diagnosticsBlob.writeRef()); + 0, + 0, + sharedLibrary.writeRef(), + diagnosticsBlob.writeRef()); diagnoseIfNeeded(diagnosticsBlob); SLANG_RETURN_ON_FAIL(result); if (testBase.isTestMode()) @@ -137,55 +142,58 @@ static SlangResult _innerMain(int argc, char** argv) } } // 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 - // at runtime. We don't do that here - as we hard code the structures. + // unless we want to use reflection, to for example workout how 'UniformState' and + // 'UniformEntryPointParams' are laid out at runtime. We don't do that here - as we hard code + // the structures. // Get the function we are going to execute const char entryPointName[] = "computeMain"; - CPPPrelude::ComputeFunc func = (CPPPrelude::ComputeFunc)sharedLibrary->findFuncByName(entryPointName); + CPPPrelude::ComputeFunc func = + (CPPPrelude::ComputeFunc)sharedLibrary->findFuncByName(entryPointName); if (!func) { return SLANG_FAIL; } // Define the uniform state structure that is *specific* to our shader defined in shader.slang - // That the layout of the structure can be determined through reflection, or can be inferred from - // the original slang source. Look at the documentation in docs/cpu-target.md which describes - // how different resources map. - // The order of the resources is in the order that they are defined in the source. + // That the layout of the structure can be determined through reflection, or can be inferred + // from the original slang source. Look at the documentation in docs/cpu-target.md which + // describes how different resources map. The order of the resources is in the order that they + // are defined in the source. struct UniformState { CPPPrelude::RWStructuredBuffer<float> ioBuffer; }; - // the uniformState will be passed as a pointer to the CPU code + // the uniformState will be passed as a pointer to the CPU code UniformState uniformState; // The contents of the buffer are modified, so we'll copy it - const float startBufferContents[] = { 2.0f, -10.0f, -3.0f, 5.0f }; + const float startBufferContents[] = {2.0f, -10.0f, -3.0f, 5.0f}; float bufferContents[SLANG_COUNT_OF(startBufferContents)]; memcpy(bufferContents, startBufferContents, sizeof(startBufferContents)); // Set up the ioBuffer such that it uses bufferContents. It is important to set the .count - // such that bounds checking can be performed in the kernel. + // such that bounds checking can be performed in the kernel. uniformState.ioBuffer.data = bufferContents; uniformState.ioBuffer.count = SLANG_COUNT_OF(bufferContents); - // In shader.slang, then entry point is attributed with `[numthreads(4, 1, 1)]` meaning each group - // consists of 4 'thread' in x. Our input buffer is 4 wide, and we index the input array via `SV_DispatchThreadID` - // so we only need to run a single group to execute over all of the 4 elements here. - // The group range from { 0, 0, 0 } -> { 1, 1, 1 } means it will execute over the single group { 0, 0, 0 }. + // In shader.slang, then entry point is attributed with `[numthreads(4, 1, 1)]` meaning each + // group consists of 4 'thread' in x. Our input buffer is 4 wide, and we index the input array + // via `SV_DispatchThreadID` so we only need to run a single group to execute over all of the 4 + // elements here. The group range from { 0, 0, 0 } -> { 1, 1, 1 } means it will execute over the + // single group { 0, 0, 0 }. - const CPPPrelude::uint3 startGroupID = { 0, 0, 0}; - const CPPPrelude::uint3 endGroupID = { 1, 1, 1 }; + const CPPPrelude::uint3 startGroupID = {0, 0, 0}; + const CPPPrelude::uint3 endGroupID = {1, 1, 1}; CPPPrelude::ComputeVaryingInput varyingInput; varyingInput.startGroupID = startGroupID; varyingInput.endGroupID = endGroupID; // 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. + // 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, &uniformState); // bufferContents holds the output |
