diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/gfx-unit-test/gfx-test-util.cpp | 62 | ||||
| -rw-r--r-- | tools/gfx-unit-test/gfx-test-util.h | 10 | ||||
| -rw-r--r-- | tools/gfx-unit-test/precompiled-module-2.cpp | 181 | ||||
| -rw-r--r-- | tools/gfx-unit-test/precompiled-module-imported.slang | 11 | ||||
| -rw-r--r-- | tools/gfx-unit-test/precompiled-module-included.slang | 9 | ||||
| -rw-r--r-- | tools/gfx-unit-test/precompiled-module.cpp | 160 | ||||
| -rw-r--r-- | tools/gfx-unit-test/precompiled-module.slang | 14 |
7 files changed, 443 insertions, 4 deletions
diff --git a/tools/gfx-unit-test/gfx-test-util.cpp b/tools/gfx-unit-test/gfx-test-util.cpp index 298283a4a..748ced5eb 100644 --- a/tools/gfx-unit-test/gfx-test-util.cpp +++ b/tools/gfx-unit-test/gfx-test-util.cpp @@ -71,6 +71,54 @@ namespace gfx_test return SLANG_OK; } + Slang::Result loadComputeProgram( + gfx::IDevice* device, + slang::ISession* slangSession, + Slang::ComPtr<gfx::IShaderProgram>& outShaderProgram, + const char* shaderModuleName, + const char* entryPointName, + slang::ProgramLayout*& slangReflection) + { + Slang::ComPtr<slang::IBlob> diagnosticsBlob; + slang::IModule* module = slangSession->loadModule(shaderModuleName, diagnosticsBlob.writeRef()); + diagnoseIfNeeded(diagnosticsBlob); + if (!module) + return SLANG_FAIL; + + ComPtr<slang::IEntryPoint> computeEntryPoint; + SLANG_RETURN_ON_FAIL( + module->findEntryPointByName(entryPointName, computeEntryPoint.writeRef())); + + Slang::List<slang::IComponentType*> componentTypes; + componentTypes.add(module); + componentTypes.add(computeEntryPoint); + + Slang::ComPtr<slang::IComponentType> composedProgram; + SlangResult result = slangSession->createCompositeComponentType( + componentTypes.getBuffer(), + componentTypes.getCount(), + composedProgram.writeRef(), + diagnosticsBlob.writeRef()); + diagnoseIfNeeded(diagnosticsBlob); + SLANG_RETURN_ON_FAIL(result); + + ComPtr<slang::IComponentType> linkedProgram; + result = composedProgram->link(linkedProgram.writeRef(), diagnosticsBlob.writeRef()); + diagnoseIfNeeded(diagnosticsBlob); + SLANG_RETURN_ON_FAIL(result); + + composedProgram = linkedProgram; + slangReflection = composedProgram->getLayout(); + + gfx::IShaderProgram::Desc programDesc = {}; + programDesc.slangGlobalScope = composedProgram.get(); + + auto shaderProgram = device->createProgram(programDesc); + + outShaderProgram = shaderProgram; + return SLANG_OK; + } + Slang::Result loadComputeProgramFromSource( gfx::IDevice* device, Slang::ComPtr<gfx::IShaderProgram>& outShaderProgram, @@ -222,10 +270,7 @@ namespace gfx_test SLANG_IGNORE_TEST } deviceDesc.slang.slangGlobalSession = context->slangGlobalSession; - Slang::List<const char*> searchPaths; - searchPaths.add(""); - searchPaths.add("../../tools/gfx-unit-test"); - searchPaths.add("tools/gfx-unit-test"); + Slang::List<const char*> searchPaths = getSlangSearchPaths(); searchPaths.addRange(additionalSearchPaths); deviceDesc.slang.searchPaths = searchPaths.getBuffer(); deviceDesc.slang.searchPathCount = (gfx::GfxCount)searchPaths.getCount(); @@ -253,6 +298,15 @@ namespace gfx_test return device; } + Slang::List<const char*> getSlangSearchPaths() + { + Slang::List<const char*> searchPaths; + searchPaths.add(""); + searchPaths.add("../../tools/gfx-unit-test"); + searchPaths.add("tools/gfx-unit-test"); + return searchPaths; + } + #if GFX_ENABLE_RENDERDOC_INTEGRATION RENDERDOC_API_1_1_2* rdoc_api = NULL; void initializeRenderDoc() diff --git a/tools/gfx-unit-test/gfx-test-util.h b/tools/gfx-unit-test/gfx-test-util.h index 501deeae0..643830413 100644 --- a/tools/gfx-unit-test/gfx-test-util.h +++ b/tools/gfx-unit-test/gfx-test-util.h @@ -18,6 +18,14 @@ namespace gfx_test const char* entryPointName, slang::ProgramLayout*& slangReflection); + Slang::Result loadComputeProgram( + gfx::IDevice* device, + slang::ISession* slangSession, + Slang::ComPtr<gfx::IShaderProgram>& outShaderProgram, + const char* shaderModuleName, + const char* entryPointName, + slang::ProgramLayout*& slangReflection); + Slang::Result loadComputeProgramFromSource( gfx::IDevice* device, Slang::ComPtr<gfx::IShaderProgram>& outShaderProgram, @@ -79,6 +87,8 @@ namespace gfx_test Slang::RenderApiFlag::Enum api, Slang::List<const char*> additionalSearchPaths = {}, gfx::IDevice::ShaderCacheDesc shaderCache = {}); + + Slang::List<const char*> getSlangSearchPaths(); void initializeRenderDoc(); void renderDocBeginFrame(); diff --git a/tools/gfx-unit-test/precompiled-module-2.cpp b/tools/gfx-unit-test/precompiled-module-2.cpp new file mode 100644 index 000000000..3da77e05c --- /dev/null +++ b/tools/gfx-unit-test/precompiled-module-2.cpp @@ -0,0 +1,181 @@ +#include "tools/unit-test/slang-unit-test.h" + +#include "slang-gfx.h" +#include "gfx-test-util.h" +#include "tools/gfx-util/shader-cursor.h" +#include "source/core/slang-basic.h" +#include "source/core/slang-blob.h" +#include "source/core/slang-memory-file-system.h" +#include "source/core/slang-io.h" + +using namespace gfx; + +namespace gfx_test +{ + // Test that mixing precompiled and non-precompiled modules is working. + + static Slang::Result precompileProgram( + gfx::IDevice* device, + ISlangMutableFileSystem* fileSys, + const char* shaderModuleName) + { + Slang::ComPtr<slang::ISession> slangSession; + SLANG_RETURN_ON_FAIL(device->getSlangSession(slangSession.writeRef())); + slang::SessionDesc sessionDesc = {}; + auto searchPaths = getSlangSearchPaths(); + sessionDesc.searchPathCount = searchPaths.getCount(); + sessionDesc.searchPaths = searchPaths.getBuffer(); + auto globalSession = slangSession->getGlobalSession(); + globalSession->createSession(sessionDesc, slangSession.writeRef()); + + Slang::ComPtr<slang::IBlob> diagnosticsBlob; + slang::IModule* module = slangSession->loadModule(shaderModuleName, diagnosticsBlob.writeRef()); + diagnoseIfNeeded(diagnosticsBlob); + if (!module) + return SLANG_FAIL; + + // Write loaded modules to memory file system. + for (SlangInt i = 0; i < slangSession->getLoadedModuleCount(); i++) + { + auto module = slangSession->getLoadedModule(i); + auto path = module->getFilePath(); + if (path) + { + auto name = module->getName(); + ComPtr<ISlangBlob> outBlob; + module->serialize(outBlob.writeRef()); + fileSys->saveFileBlob((Slang::String(name) + ".slang-module").getBuffer(), outBlob); + } + } + return SLANG_OK; + } + + void precompiledModule2TestImpl(IDevice* device, UnitTestContext* context) + { + Slang::ComPtr<ITransientResourceHeap> transientHeap; + ITransientResourceHeap::Desc transientHeapDesc = {}; + transientHeapDesc.constantBufferSize = 4096; + GFX_CHECK_CALL_ABORT( + device->createTransientResourceHeap(transientHeapDesc, transientHeap.writeRef())); + + // First, load and compile the slang source. + ComPtr<ISlangMutableFileSystem> memoryFileSystem = ComPtr<ISlangMutableFileSystem>(new Slang::MemoryFileSystem()); + + ComPtr<IShaderProgram> shaderProgram; + slang::ProgramLayout* slangReflection; + GFX_CHECK_CALL_ABORT(precompileProgram(device, memoryFileSystem.get(), "precompiled-module-imported")); + + // Next, load the precompiled slang program. + Slang::ComPtr<slang::ISession> slangSession; + device->getSlangSession(slangSession.writeRef()); + slang::SessionDesc sessionDesc = {}; + sessionDesc.targetCount = 1; + slang::TargetDesc targetDesc = {}; + switch (device->getDeviceInfo().deviceType) + { + case gfx::DeviceType::DirectX12: + targetDesc.format = SLANG_DXIL; + targetDesc.profile = device->getSlangSession()->getGlobalSession()->findProfile("sm_6_1"); + break; + case gfx::DeviceType::Vulkan: + targetDesc.format = SLANG_SPIRV; + targetDesc.profile = device->getSlangSession()->getGlobalSession()->findProfile("GLSL_460"); + break; + } + sessionDesc.targets = &targetDesc; + sessionDesc.fileSystem = memoryFileSystem.get(); + auto globalSession = slangSession->getGlobalSession(); + globalSession->createSession(sessionDesc, slangSession.writeRef()); + + const char* moduleSrc = R"( + import "precompiled-module-imported"; + + // Main entry-point. + + using namespace ns; + + [shader("compute")] + [numthreads(4, 1, 1)] + void computeMain( + uint3 sv_dispatchThreadID : SV_DispatchThreadID, + uniform RWStructuredBuffer <float> buffer) + { + buffer[sv_dispatchThreadID.x] = helperFunc() + helperFunc1(); + } + )"; + memoryFileSystem->saveFile("precompiled-module.slang", moduleSrc, strlen(moduleSrc)); + GFX_CHECK_CALL_ABORT(loadComputeProgram(device, slangSession, shaderProgram, "precompiled-module", "computeMain", slangReflection)); + + ComputePipelineStateDesc pipelineDesc = {}; + pipelineDesc.program = shaderProgram.get(); + ComPtr<gfx::IPipelineState> pipelineState; + GFX_CHECK_CALL_ABORT( + device->createComputePipelineState(pipelineDesc, pipelineState.writeRef())); + + const int numberCount = 4; + float initialData[] = { 0.0f, 0.0f, 0.0f, 0.0f }; + IBufferResource::Desc bufferDesc = {}; + bufferDesc.sizeInBytes = numberCount * sizeof(float); + bufferDesc.format = gfx::Format::Unknown; + bufferDesc.elementSize = sizeof(float); + bufferDesc.allowedStates = ResourceStateSet( + ResourceState::ShaderResource, + ResourceState::UnorderedAccess, + ResourceState::CopyDestination, + ResourceState::CopySource); + bufferDesc.defaultState = ResourceState::UnorderedAccess; + bufferDesc.memoryType = MemoryType::DeviceLocal; + + ComPtr<IBufferResource> numbersBuffer; + GFX_CHECK_CALL_ABORT(device->createBufferResource( + bufferDesc, + (void*)initialData, + numbersBuffer.writeRef())); + + ComPtr<IResourceView> bufferView; + IResourceView::Desc viewDesc = {}; + viewDesc.type = IResourceView::Type::UnorderedAccess; + viewDesc.format = Format::Unknown; + GFX_CHECK_CALL_ABORT( + device->createBufferView(numbersBuffer, nullptr, viewDesc, bufferView.writeRef())); + + // We have done all the set up work, now it is time to start recording a command buffer for + // GPU execution. + { + ICommandQueue::Desc queueDesc = { ICommandQueue::QueueType::Graphics }; + auto queue = device->createCommandQueue(queueDesc); + + auto commandBuffer = transientHeap->createCommandBuffer(); + auto encoder = commandBuffer->encodeComputeCommands(); + + auto rootObject = encoder->bindPipeline(pipelineState); + + ShaderCursor entryPointCursor( + rootObject->getEntryPoint(0)); // get a cursor the the first entry-point. + // Bind buffer view to the entry point. + entryPointCursor.getPath("buffer").setResource(bufferView); + + encoder->dispatchCompute(1, 1, 1); + encoder->endEncoding(); + commandBuffer->close(); + queue->executeCommandBuffer(commandBuffer); + queue->waitOnHost(); + } + + compareComputeResult( + device, + numbersBuffer, + Slang::makeArray<float>(3.0f, 3.0f, 3.0f, 3.0f)); + } + + SLANG_UNIT_TEST(precompiledModule2D3D12) + { + runTestImpl(precompiledModule2TestImpl, unitTestContext, Slang::RenderApiFlag::D3D12); + } + + SLANG_UNIT_TEST(precompiledModule2Vulkan) + { + runTestImpl(precompiledModule2TestImpl, unitTestContext, Slang::RenderApiFlag::Vulkan); + } + +} diff --git a/tools/gfx-unit-test/precompiled-module-imported.slang b/tools/gfx-unit-test/precompiled-module-imported.slang new file mode 100644 index 000000000..5c59e99b0 --- /dev/null +++ b/tools/gfx-unit-test/precompiled-module-imported.slang @@ -0,0 +1,11 @@ +module "precompiled-module-imported"; + +__include "precompiled-module-included.slang"; + +namespace ns +{ + public int helperFunc() + { + return 1; + } +}
\ No newline at end of file diff --git a/tools/gfx-unit-test/precompiled-module-included.slang b/tools/gfx-unit-test/precompiled-module-included.slang new file mode 100644 index 000000000..88d9e57d7 --- /dev/null +++ b/tools/gfx-unit-test/precompiled-module-included.slang @@ -0,0 +1,9 @@ +implementing "precompiled-module-imported"; + +namespace ns +{ + public int helperFunc1() + { + return 2; + } +}
\ No newline at end of file diff --git a/tools/gfx-unit-test/precompiled-module.cpp b/tools/gfx-unit-test/precompiled-module.cpp new file mode 100644 index 000000000..026575120 --- /dev/null +++ b/tools/gfx-unit-test/precompiled-module.cpp @@ -0,0 +1,160 @@ +#include "tools/unit-test/slang-unit-test.h" + +#include "slang-gfx.h" +#include "gfx-test-util.h" +#include "tools/gfx-util/shader-cursor.h" +#include "source/core/slang-basic.h" +#include "source/core/slang-blob.h" +#include "source/core/slang-memory-file-system.h" + +using namespace gfx; + +namespace gfx_test +{ + static Slang::Result precompileProgram( + gfx::IDevice* device, + ISlangMutableFileSystem* fileSys, + const char* shaderModuleName) + { + Slang::ComPtr<slang::ISession> slangSession; + SLANG_RETURN_ON_FAIL(device->getSlangSession(slangSession.writeRef())); + slang::SessionDesc sessionDesc = {}; + auto searchPaths = getSlangSearchPaths(); + sessionDesc.searchPathCount = searchPaths.getCount(); + sessionDesc.searchPaths = searchPaths.getBuffer(); + auto globalSession = slangSession->getGlobalSession(); + globalSession->createSession(sessionDesc, slangSession.writeRef()); + + Slang::ComPtr<slang::IBlob> diagnosticsBlob; + slang::IModule* module = slangSession->loadModule(shaderModuleName, diagnosticsBlob.writeRef()); + diagnoseIfNeeded(diagnosticsBlob); + if (!module) + return SLANG_FAIL; + + // Write loaded modules to memory file system. + for (SlangInt i = 0; i < slangSession->getLoadedModuleCount(); i++) + { + auto module = slangSession->getLoadedModule(i); + auto path = module->getFilePath(); + if (path) + { + auto name = module->getName(); + ComPtr<ISlangBlob> outBlob; + module->serialize(outBlob.writeRef()); + fileSys->saveFileBlob((Slang::String(name) + ".slang-module").getBuffer(), outBlob); + } + } + return SLANG_OK; + } + + void precompiledModuleTestImpl(IDevice* device, UnitTestContext* context) + { + Slang::ComPtr<ITransientResourceHeap> transientHeap; + ITransientResourceHeap::Desc transientHeapDesc = {}; + transientHeapDesc.constantBufferSize = 4096; + GFX_CHECK_CALL_ABORT( + device->createTransientResourceHeap(transientHeapDesc, transientHeap.writeRef())); + + // First, load and compile the slang source. + ComPtr<ISlangMutableFileSystem> memoryFileSystem = ComPtr<ISlangMutableFileSystem>(new Slang::MemoryFileSystem()); + + ComPtr<IShaderProgram> shaderProgram; + slang::ProgramLayout* slangReflection; + GFX_CHECK_CALL_ABORT(precompileProgram(device, memoryFileSystem.get(), "precompiled-module")); + + // Next, load the precompiled slang program. + Slang::ComPtr<slang::ISession> slangSession; + device->getSlangSession(slangSession.writeRef()); + slang::SessionDesc sessionDesc = {}; + sessionDesc.targetCount = 1; + slang::TargetDesc targetDesc = {}; + switch (device->getDeviceInfo().deviceType) + { + case gfx::DeviceType::DirectX12: + targetDesc.format = SLANG_DXIL; + targetDesc.profile = device->getSlangSession()->getGlobalSession()->findProfile("sm_6_1"); + break; + case gfx::DeviceType::Vulkan: + targetDesc.format = SLANG_SPIRV; + targetDesc.profile = device->getSlangSession()->getGlobalSession()->findProfile("GLSL_460"); + break; + } + sessionDesc.targets = &targetDesc; + sessionDesc.fileSystem = memoryFileSystem.get(); + auto globalSession = slangSession->getGlobalSession(); + globalSession->createSession(sessionDesc, slangSession.writeRef()); + GFX_CHECK_CALL_ABORT(loadComputeProgram(device, slangSession, shaderProgram, "precompiled-module", "computeMain", slangReflection)); + + ComputePipelineStateDesc pipelineDesc = {}; + pipelineDesc.program = shaderProgram.get(); + ComPtr<gfx::IPipelineState> pipelineState; + GFX_CHECK_CALL_ABORT( + device->createComputePipelineState(pipelineDesc, pipelineState.writeRef())); + + const int numberCount = 4; + float initialData[] = { 0.0f, 0.0f, 0.0f, 0.0f }; + IBufferResource::Desc bufferDesc = {}; + bufferDesc.sizeInBytes = numberCount * sizeof(float); + bufferDesc.format = gfx::Format::Unknown; + bufferDesc.elementSize = sizeof(float); + bufferDesc.allowedStates = ResourceStateSet( + ResourceState::ShaderResource, + ResourceState::UnorderedAccess, + ResourceState::CopyDestination, + ResourceState::CopySource); + bufferDesc.defaultState = ResourceState::UnorderedAccess; + bufferDesc.memoryType = MemoryType::DeviceLocal; + + ComPtr<IBufferResource> numbersBuffer; + GFX_CHECK_CALL_ABORT(device->createBufferResource( + bufferDesc, + (void*)initialData, + numbersBuffer.writeRef())); + + ComPtr<IResourceView> bufferView; + IResourceView::Desc viewDesc = {}; + viewDesc.type = IResourceView::Type::UnorderedAccess; + viewDesc.format = Format::Unknown; + GFX_CHECK_CALL_ABORT( + device->createBufferView(numbersBuffer, nullptr, viewDesc, bufferView.writeRef())); + + // We have done all the set up work, now it is time to start recording a command buffer for + // GPU execution. + { + ICommandQueue::Desc queueDesc = { ICommandQueue::QueueType::Graphics }; + auto queue = device->createCommandQueue(queueDesc); + + auto commandBuffer = transientHeap->createCommandBuffer(); + auto encoder = commandBuffer->encodeComputeCommands(); + + auto rootObject = encoder->bindPipeline(pipelineState); + + ShaderCursor entryPointCursor( + rootObject->getEntryPoint(0)); // get a cursor the the first entry-point. + // Bind buffer view to the entry point. + entryPointCursor.getPath("buffer").setResource(bufferView); + + encoder->dispatchCompute(1, 1, 1); + encoder->endEncoding(); + commandBuffer->close(); + queue->executeCommandBuffer(commandBuffer); + queue->waitOnHost(); + } + + compareComputeResult( + device, + numbersBuffer, + Slang::makeArray<float>(3.0f, 3.0f, 3.0f, 3.0f)); + } + + SLANG_UNIT_TEST(precompiledModuleD3D12) + { + runTestImpl(precompiledModuleTestImpl, unitTestContext, Slang::RenderApiFlag::D3D12); + } + + SLANG_UNIT_TEST(precompiledModuleVulkan) + { + runTestImpl(precompiledModuleTestImpl, unitTestContext, Slang::RenderApiFlag::Vulkan); + } + +} diff --git a/tools/gfx-unit-test/precompiled-module.slang b/tools/gfx-unit-test/precompiled-module.slang new file mode 100644 index 000000000..be7231432 --- /dev/null +++ b/tools/gfx-unit-test/precompiled-module.slang @@ -0,0 +1,14 @@ +import "precompiled-module-imported"; + +using namespace ns; + +// Main entry-point. + +[shader("compute")] +[numthreads(4, 1, 1)] +void computeMain( + uint3 sv_dispatchThreadID : SV_DispatchThreadID, + uniform RWStructuredBuffer <float> buffer) +{ + buffer[sv_dispatchThreadID.x] = helperFunc() + helperFunc1(); +} |
