From 011a743668e7cd0b7cf97d27e3bed7d519794aeb Mon Sep 17 00:00:00 2001 From: Dietrich Geisler Date: Fri, 31 Jul 2020 17:51:52 -0400 Subject: Binary for Heterogeneous Example (#1467) * Binary Heterogeneous Example This PR introduces the ability to insert the binary of a non-CPU target by using the -heterogeneous flag. Specifically, this PR updates the emitting logic to produce a variable of name `__[name_of_entryPoint]` when the heterogeneous flag is present. * Prelude path fix Co-authored-by: Tim Foley --- examples/heterogeneous-hello-world/main.cpp | 103 ++-------------------------- 1 file changed, 5 insertions(+), 98 deletions(-) (limited to 'examples/heterogeneous-hello-world/main.cpp') diff --git a/examples/heterogeneous-hello-world/main.cpp b/examples/heterogeneous-hello-world/main.cpp index a590f8c4b..47df20dc5 100644 --- a/examples/heterogeneous-hello-world/main.cpp +++ b/examples/heterogeneous-hello-world/main.cpp @@ -63,103 +63,15 @@ struct gfx_DescriptorSet_0; struct gfx_PipelineState_0; bool executeComputation_0(); +extern unsigned char __computeMain[]; +extern size_t __computeMainSize; gfx::ShaderProgram* loadShaderProgram(gfx::Renderer* renderer) { - // First, we need to create a "session" for interacting with the Slang - // compiler. This scopes all of our application's interactions - // with the Slang library. At the moment, creating a session causes - // Slang to load and validate its standard library, so this is a - // somewhat heavy-weight operation. When possible, an application - // should try to re-use the same session across multiple compiles. + // We extract the begin/end pointers to the output code buffers directly // - SlangSession* slangSession = spCreateSession(NULL); - - // A compile request represents a single invocation of the compiler, - // to process some inputs and produce outputs (or errors). - // - SlangCompileRequest* slangRequest = spCreateCompileRequest(slangSession); - - // We would like to request a single target (output) format: DirectX shader bytecode (DXBC) - int targetIndex = spAddCodeGenTarget(slangRequest, SLANG_DXBC); - - // We will specify the desired "profile" for this one target in terms of the - // DirectX "shader model" that should be supported. - // - spSetTargetProfile(slangRequest, targetIndex, spFindProfile(slangSession, "sm_4_0")); - - // 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). - // - // For this example, our code will all be in the Slang language. The user may - // also specify HLSL input here, but that currently doesn't affect the compiler's - // behavior much. - // - int translationUnitIndex = spAddTranslationUnit(slangRequest, SLANG_SOURCE_LANGUAGE_SLANG, nullptr); - - // We will load source code for our translation unit from the file `shaders.slang`. - // 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. - // - // For each entry point, we need to specify the name of a function, the - // translation unit in which that function can be found, and the stage - // that we need to compile for (e.g., vertex, fragment, geometry, ...). - // - char const* computeEntryPointName = "computeMain"; - int computeIndex = spAddEntryPoint(slangRequest, translationUnitIndex, computeEntryPointName, 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 - // were detected. - // - const SlangResult compileRes = spCompile(slangRequest); - - // Even if there were no errors that forced compilation to fail, the - // compiler may have produced "diagnostic" output such as warnings. - // We will go ahead and print that output here. - // - if(auto diagnostics = spGetDiagnosticOutput(slangRequest)) - { - reportError("%s", diagnostics); - } - - // If compilation failed, there is no point in continuing any further. - if(SLANG_FAILED(compileRes)) - { - spDestroyCompileRequest(slangRequest); - spDestroySession(slangSession); - return nullptr; - } - - // If compilation was successful, then we will extract the code for - // our two entry points as "blobs". - // - // If you are using a D3D API, then your application may want to - // take advantage of the fact taht these blobs are binary compatible - // with the `ID3DBlob`, `ID3D10Blob`, etc. interfaces. - // - - ISlangBlob* computeShaderBlob = nullptr; - spGetEntryPointCodeBlob(slangRequest, computeIndex, 0, &computeShaderBlob); - - // We extract the begin/end pointers to the output code buffers - // using operations on the `ISlangBlob` interface. - // - char const* computeCode = (char const*) computeShaderBlob->getBufferPointer(); - char const* computeCodeEnd = computeCode + computeShaderBlob->getBufferSize(); - - // Once we have extracted the output blobs, it is safe to destroy - // the compile request and even the session. - // - spDestroyCompileRequest(slangRequest); - spDestroySession(slangSession); + char unsigned const* computeCode = __computeMain; + char unsigned const* computeCodeEnd = computeCode + __computeMainSize; // Now we use the operations of the example graphics API abstraction // layer to load shader code into the underlying API. @@ -179,11 +91,6 @@ gfx::ShaderProgram* loadShaderProgram(gfx::Renderer* renderer) gShaderProgram = renderer->createProgram(programDesc); - // Once we've used the output blobs from the Slang compiler to initialize - // the API-specific shader program, we can release their memory. - // - computeShaderBlob->release(); - return gShaderProgram; } -- cgit v1.2.3