diff options
| author | Ellie Hermaszewska <ellieh@nvidia.com> | 2024-10-29 14:49:26 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-29 14:49:26 +0800 |
| commit | f65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch) | |
| tree | ea1d61342cd29368e19135000ec2948813096205 /examples/gpu-printing/main.cpp | |
| parent | a729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff) | |
format
* format
* Minor test fixes
* enable checking cpp format in ci
Diffstat (limited to 'examples/gpu-printing/main.cpp')
| -rw-r--r-- | examples/gpu-printing/main.cpp | 209 |
1 files changed, 109 insertions, 100 deletions
diff --git a/examples/gpu-printing/main.cpp b/examples/gpu-printing/main.cpp index fd15661dd..fa9f919dc 100644 --- a/examples/gpu-printing/main.cpp +++ b/examples/gpu-printing/main.cpp @@ -1,17 +1,16 @@ // main.cpp -#include <string> - +#include "slang-com-ptr.h" #include "slang.h" -#include "slang-com-ptr.h" +#include <string> using Slang::ComPtr; +#include "examples/example-base/example-base.h" +#include "gfx-util/shader-cursor.h" #include "gpu-printing.h" #include "slang-gfx.h" -#include "gfx-util/shader-cursor.h" -#include "tools/platform/window.h" #include "source/core/slang-basic.h" -#include "examples/example-base/example-base.h" +#include "tools/platform/window.h" using namespace gfx; @@ -23,7 +22,9 @@ ComPtr<slang::ISession> createSlangSession(gfx::IDevice* device) return slangSession; } -ComPtr<slang::IModule> compileShaderModuleFromFile(slang::ISession* slangSession, char const* filePath) +ComPtr<slang::IModule> compileShaderModuleFromFile( + slang::ISession* slangSession, + char const* filePath) { ComPtr<slang::IModule> slangModule; ComPtr<slang::IBlob> diagnosticBlob; @@ -34,113 +35,121 @@ ComPtr<slang::IModule> compileShaderModuleFromFile(slang::ISession* slangSession return slangModule; } -struct ExampleProgram: public TestBase +struct ExampleProgram : public TestBase { -int gWindowWidth = 640; -int gWindowHeight = 480; + int gWindowWidth = 640; + int gWindowHeight = 480; -ComPtr<gfx::IDevice> gDevice; + ComPtr<gfx::IDevice> gDevice; -ComPtr<slang::ISession> gSlangSession; -ComPtr<slang::IModule> gSlangModule; -ComPtr<gfx::IShaderProgram> gProgram; + ComPtr<slang::ISession> gSlangSession; + ComPtr<slang::IModule> gSlangModule; + ComPtr<gfx::IShaderProgram> gProgram; -ComPtr<gfx::IPipelineState> gPipelineState; + ComPtr<gfx::IPipelineState> gPipelineState; -Slang::Dictionary<int, std::string> gHashedStrings; + Slang::Dictionary<int, std::string> gHashedStrings; -GPUPrinting gGPUPrinting; + GPUPrinting gGPUPrinting; -ComPtr<gfx::IShaderProgram> loadComputeProgram(slang::IModule* slangModule, char const* entryPointName) -{ - ComPtr<slang::IEntryPoint> entryPoint; - slangModule->findEntryPointByName(entryPointName, entryPoint.writeRef()); - - ComPtr<slang::IComponentType> linkedProgram; - entryPoint->link(linkedProgram.writeRef()); - - if (isTestMode()) + ComPtr<gfx::IShaderProgram> loadComputeProgram( + slang::IModule* slangModule, + char const* entryPointName) { - printEntrypointHashes(1, 1, linkedProgram); - } + ComPtr<slang::IEntryPoint> entryPoint; + slangModule->findEntryPointByName(entryPointName, entryPoint.writeRef()); - gGPUPrinting.loadStrings(linkedProgram->getLayout()); + ComPtr<slang::IComponentType> linkedProgram; + entryPoint->link(linkedProgram.writeRef()); - gfx::IShaderProgram::Desc programDesc = {}; - programDesc.slangGlobalScope = linkedProgram; + if (isTestMode()) + { + printEntrypointHashes(1, 1, linkedProgram); + } - auto shaderProgram = gDevice->createProgram(programDesc); + gGPUPrinting.loadStrings(linkedProgram->getLayout()); - return shaderProgram; -} + gfx::IShaderProgram::Desc programDesc = {}; + programDesc.slangGlobalScope = linkedProgram; -Result execute(int argc, char* argv[]) -{ - parseOption(argc, argv); - IDevice::Desc deviceDesc; - Result res = gfxCreateDevice(&deviceDesc, gDevice.writeRef()); - if(SLANG_FAILED(res)) return res; - - Slang::String path = resourceBase.resolveResource("kernels.slang"); - - gSlangSession = createSlangSession(gDevice); - gSlangModule = compileShaderModuleFromFile(gSlangSession, path.getBuffer()); - if(!gSlangModule) - return SLANG_FAIL; - - gProgram = loadComputeProgram(gSlangModule, "computeMain"); - if(!gProgram) - return SLANG_FAIL; - - ComputePipelineStateDesc desc; - desc.program = gProgram; - auto pipelineState = gDevice->createComputePipelineState(desc); - if(!pipelineState) return SLANG_FAIL; - - gPipelineState = pipelineState; - - size_t printBufferSize = 4 * 1024; // use a small-ish (4KB) buffer for print output - - IBufferResource::Desc printBufferDesc = {}; - printBufferDesc.type = IResource::Type::Buffer; - printBufferDesc.sizeInBytes = printBufferSize; - printBufferDesc.elementSize = sizeof(uint32_t); - printBufferDesc.defaultState = ResourceState::UnorderedAccess; - printBufferDesc.allowedStates = ResourceStateSet( - ResourceState::CopySource, ResourceState::CopyDestination, ResourceState::UnorderedAccess); - printBufferDesc.memoryType = MemoryType::DeviceLocal; - auto printBuffer = gDevice->createBufferResource(printBufferDesc); - - IResourceView::Desc printBufferViewDesc = {}; - printBufferViewDesc.type = IResourceView::Type::UnorderedAccess; - printBufferViewDesc.format = Format::Unknown; - auto printBufferView = gDevice->createBufferView(printBuffer, nullptr, printBufferViewDesc); - - ITransientResourceHeap::Desc transientResourceHeapDesc = {}; - transientResourceHeapDesc.constantBufferSize = 256; - auto transientHeap = gDevice->createTransientResourceHeap(transientResourceHeapDesc); - - ICommandQueue::Desc queueDesc = {ICommandQueue::QueueType::Graphics}; - auto queue = gDevice->createCommandQueue(queueDesc); - auto commandBuffer = transientHeap->createCommandBuffer(); - auto encoder = commandBuffer->encodeComputeCommands(); - auto rootShaderObject = encoder->bindPipeline(gPipelineState); - auto cursor = ShaderCursor(rootShaderObject); - cursor["gPrintBuffer"].setResource(printBufferView); - encoder->dispatchCompute(1, 1, 1); - encoder->bufferBarrier(printBuffer, ResourceState::UnorderedAccess, ResourceState::CopySource); - encoder->endEncoding(); - commandBuffer->close(); - queue->executeCommandBuffer(commandBuffer); - - ComPtr<ISlangBlob> blob; - gDevice->readBufferResource(printBuffer, 0, printBufferSize, blob.writeRef()); - - gGPUPrinting.processGPUPrintCommands(blob->getBufferPointer(), printBufferSize); - - return SLANG_OK; -} + auto shaderProgram = gDevice->createProgram(programDesc); + + return shaderProgram; + } + Result execute(int argc, char* argv[]) + { + parseOption(argc, argv); + IDevice::Desc deviceDesc; + Result res = gfxCreateDevice(&deviceDesc, gDevice.writeRef()); + if (SLANG_FAILED(res)) + return res; + + Slang::String path = resourceBase.resolveResource("kernels.slang"); + + gSlangSession = createSlangSession(gDevice); + gSlangModule = compileShaderModuleFromFile(gSlangSession, path.getBuffer()); + if (!gSlangModule) + return SLANG_FAIL; + + gProgram = loadComputeProgram(gSlangModule, "computeMain"); + if (!gProgram) + return SLANG_FAIL; + + ComputePipelineStateDesc desc; + desc.program = gProgram; + auto pipelineState = gDevice->createComputePipelineState(desc); + if (!pipelineState) + return SLANG_FAIL; + + gPipelineState = pipelineState; + + size_t printBufferSize = 4 * 1024; // use a small-ish (4KB) buffer for print output + + IBufferResource::Desc printBufferDesc = {}; + printBufferDesc.type = IResource::Type::Buffer; + printBufferDesc.sizeInBytes = printBufferSize; + printBufferDesc.elementSize = sizeof(uint32_t); + printBufferDesc.defaultState = ResourceState::UnorderedAccess; + printBufferDesc.allowedStates = ResourceStateSet( + ResourceState::CopySource, + ResourceState::CopyDestination, + ResourceState::UnorderedAccess); + printBufferDesc.memoryType = MemoryType::DeviceLocal; + auto printBuffer = gDevice->createBufferResource(printBufferDesc); + + IResourceView::Desc printBufferViewDesc = {}; + printBufferViewDesc.type = IResourceView::Type::UnorderedAccess; + printBufferViewDesc.format = Format::Unknown; + auto printBufferView = gDevice->createBufferView(printBuffer, nullptr, printBufferViewDesc); + + ITransientResourceHeap::Desc transientResourceHeapDesc = {}; + transientResourceHeapDesc.constantBufferSize = 256; + auto transientHeap = gDevice->createTransientResourceHeap(transientResourceHeapDesc); + + ICommandQueue::Desc queueDesc = {ICommandQueue::QueueType::Graphics}; + auto queue = gDevice->createCommandQueue(queueDesc); + auto commandBuffer = transientHeap->createCommandBuffer(); + auto encoder = commandBuffer->encodeComputeCommands(); + auto rootShaderObject = encoder->bindPipeline(gPipelineState); + auto cursor = ShaderCursor(rootShaderObject); + cursor["gPrintBuffer"].setResource(printBufferView); + encoder->dispatchCompute(1, 1, 1); + encoder->bufferBarrier( + printBuffer, + ResourceState::UnorderedAccess, + ResourceState::CopySource); + encoder->endEncoding(); + commandBuffer->close(); + queue->executeCommandBuffer(commandBuffer); + + ComPtr<ISlangBlob> blob; + gDevice->readBufferResource(printBuffer, 0, printBufferSize, blob.writeRef()); + + gGPUPrinting.processGPUPrintCommands(blob->getBufferPointer(), printBufferSize); + + return SLANG_OK; + } }; int main(int argc, char* argv[]) |
