diff options
| author | Yong He <yonghe@outlook.com> | 2021-09-24 11:33:44 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-24 11:33:44 -0700 |
| commit | bec8e6aec85b6e3f875c58bdd59eb15613978358 (patch) | |
| tree | 0791fb2ce1be786c17e5a6ee489ed3065fc07332 /tools/gfx-unit-test | |
| parent | f2a3c933bc11a498c622fa18694c84beca8ca031 (diff) | |
Move existing unit tests to a standalone dll. (#1945)
Diffstat (limited to 'tools/gfx-unit-test')
| -rw-r--r-- | tools/gfx-unit-test/compute-smoke.cpp | 144 | ||||
| -rw-r--r-- | tools/gfx-unit-test/compute-smoke.slang | 68 | ||||
| -rw-r--r-- | tools/gfx-unit-test/gfx-test-util.cpp | 86 | ||||
| -rw-r--r-- | tools/gfx-unit-test/gfx-test-util.h | 41 |
4 files changed, 339 insertions, 0 deletions
diff --git a/tools/gfx-unit-test/compute-smoke.cpp b/tools/gfx-unit-test/compute-smoke.cpp new file mode 100644 index 000000000..b8e027b65 --- /dev/null +++ b/tools/gfx-unit-test/compute-smoke.cpp @@ -0,0 +1,144 @@ +#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" + +using namespace gfx; + +namespace gfx_test +{ + void computeSmokeTestImpl(IDevice* device, UnitTestContext* context) + { + Slang::ComPtr<ITransientResourceHeap> transientHeap; + ITransientResourceHeap::Desc transientHeapDesc = {}; + transientHeapDesc.constantBufferSize = 4096; + GFX_CHECK_CALL_ABORT( + device->createTransientResourceHeap(transientHeapDesc, transientHeap.writeRef())); + + ComPtr<IShaderProgram> shaderProgram; + slang::ProgramLayout* slangReflection; + GFX_CHECK_CALL_ABORT(loadShaderProgram(device, shaderProgram, "compute-smoke", 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, 1.0f, 2.0f, 3.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.cpuAccessFlags = AccessFlag::Write | AccessFlag::Read; + + 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, 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); + + slang::TypeReflection* addTransformerType = + slangReflection->findTypeByName("AddTransformer"); + + // Now we can use this type to create a shader object that can be bound to the root object. + ComPtr<IShaderObject> transformer; + GFX_CHECK_CALL_ABORT(device->createShaderObject( + addTransformerType, ShaderObjectContainerType::None, transformer.writeRef())); + // Set the `c` field of the `AddTransformer`. + float c = 1.0f; + ShaderCursor(transformer).getPath("c").setData(&c, sizeof(float)); + + 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); + + // Bind the previously created transformer object to root object. + entryPointCursor.getPath("transformer").setObject(transformer); + + encoder->dispatchCompute(1, 1, 1); + encoder->endEncoding(); + commandBuffer->close(); + queue->executeCommandBuffer(commandBuffer); + queue->wait(); + } + + compareComputeResult( + device, + numbersBuffer, + Slang::makeArray<float>(11.0f, 12.0f, 13.0f, 14.0f)); + } + + void computeSmokeTestAPI(UnitTestContext* context, Slang::RenderApiFlag::Enum api) + { + if ((api & context->enabledApis) == 0) + { + SLANG_IGNORE_TEST + } + Slang::ComPtr<IDevice> device; + IDevice::Desc deviceDesc = {}; + switch (api) + { + case Slang::RenderApiFlag::D3D11: + deviceDesc.deviceType = gfx::DeviceType::DirectX11; + break; + case Slang::RenderApiFlag::D3D12: + deviceDesc.deviceType = gfx::DeviceType::DirectX12; + break; + case Slang::RenderApiFlag::Vulkan: + deviceDesc.deviceType = gfx::DeviceType::Vulkan; + break; + default: + SLANG_IGNORE_TEST + } + deviceDesc.slang.slangGlobalSession = context->slangGlobalSession; + const char* searchPaths[] = { "", "../../tools/gfx-unit-test", "tools/gfx-unit-test" }; + deviceDesc.slang.searchPathCount = (SlangInt)SLANG_COUNT_OF(searchPaths); + deviceDesc.slang.searchPaths = searchPaths; + auto createDeviceResult = gfxCreateDevice(&deviceDesc, device.writeRef()); + if (SLANG_FAILED(createDeviceResult)) + { + SLANG_IGNORE_TEST + } + + computeSmokeTestImpl(device, context); + } + + SLANG_UNIT_TEST(computeSmokeD3D11) + { + computeSmokeTestAPI(unitTestContext, Slang::RenderApiFlag::D3D11); + } + + SLANG_UNIT_TEST(computeSmokeVulkan) + { + computeSmokeTestAPI(unitTestContext, Slang::RenderApiFlag::Vulkan); + } + +} diff --git a/tools/gfx-unit-test/compute-smoke.slang b/tools/gfx-unit-test/compute-smoke.slang new file mode 100644 index 000000000..7ecdb4177 --- /dev/null +++ b/tools/gfx-unit-test/compute-smoke.slang @@ -0,0 +1,68 @@ +// compute-smoke.slang + +// This is a copy of `shader-object.slang` in `shader-object` example +// for use by compute-smoke gfx unit test. + +// This file implements a simple compute shader that transforms +// input floating point numbers stored in a `RWStructuredBuffer`. +// Specifically, for each number x from input buffer, compute +// f(x) and store the result back in the same buffer. + +// The compute shader supports multiple transformation functions, +// such add(x, c) which returns x+c, or mul(x, c) which returns x*c. +// This functions are implemented as types that conforms to the +// `ITransformer` interface. + +// The main entry point function takes a parameter of `ITransformer` +// type, and applies the transformation to numbers in the input +// buffer. By defining the shader parameter using interfaces, +// we enable the flexiblity to generate either specialized compute +// kernels that performs specific transformation or a general +// kernel that can perform any transformations encoded by the +// parameter at run-time, without changing any shader code or +// host-application logic for setting and preparing shader parameters. + +// Defines the transformer interface, which implements a single +// `transform` operation. +interface ITransformer +{ + float transform(float x); +} + +// Represents a transform function f(x) = x + c. +struct AddTransformer : ITransformer +{ + float c; + float transform(float x) { return x + c + 10.0f; } +}; + +// Represents a transform function f(x) = x * c. +struct MulTransformer : ITransformer +{ + float c; + float transform(float x) { return x * c; } +}; + +// Represents a composite function f(x) = f0(f1(x)); +struct CompositeTransformer : ITransformer +{ + ITransformer func0; + ITransformer func1; + float transform(float x) + { + return func0.transform(func1.transform(x)); + } +}; + +// Main entry-point. Applies the transformation encoded by `transformer` +// to all elements in `buffer`. +[shader("compute")] +[numthreads(4,1,1)] +void computeMain( + uint3 sv_dispatchThreadID : SV_DispatchThreadID, + uniform RWStructuredBuffer<float> buffer, + uniform ITransformer transformer) +{ + var input = buffer[sv_dispatchThreadID.x]; + buffer[sv_dispatchThreadID.x] = transformer.transform(input); +} diff --git a/tools/gfx-unit-test/gfx-test-util.cpp b/tools/gfx-unit-test/gfx-test-util.cpp new file mode 100644 index 000000000..d01fcdca3 --- /dev/null +++ b/tools/gfx-unit-test/gfx-test-util.cpp @@ -0,0 +1,86 @@ +#include "gfx-test-util.h" +#include "tools/unit-test/slang-unit-test.h" + +#include <slang-com-ptr.h> + +using Slang::ComPtr; + +namespace gfx_test +{ + void diagnoseIfNeeded(slang::IBlob* diagnosticsBlob) + { + if (diagnosticsBlob != nullptr) + { + getTestReporter()->message(TestMessageType::Info, (const char*)diagnosticsBlob->getBufferPointer()); + } + } + + Slang::Result loadShaderProgram( + gfx::IDevice* device, + Slang::ComPtr<gfx::IShaderProgram>& outShaderProgram, + const char* shaderModuleName, + slang::ProgramLayout*& slangReflection) + { + Slang::ComPtr<slang::ISession> slangSession; + SLANG_RETURN_ON_FAIL(device->getSlangSession(slangSession.writeRef())); + Slang::ComPtr<slang::IBlob> diagnosticsBlob; + slang::IModule* module = slangSession->loadModule(shaderModuleName, diagnosticsBlob.writeRef()); + diagnoseIfNeeded(diagnosticsBlob); + if (!module) + return SLANG_FAIL; + + char const* computeEntryPointName = "computeMain"; + ComPtr<slang::IEntryPoint> computeEntryPoint; + SLANG_RETURN_ON_FAIL( + module->findEntryPointByName(computeEntryPointName, 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); + slangReflection = composedProgram->getLayout(); + + gfx::IShaderProgram::Desc programDesc = {}; + programDesc.pipelineType = gfx::PipelineType::Compute; + programDesc.slangProgram = composedProgram.get(); + + auto shaderProgram = device->createProgram(programDesc); + + outShaderProgram = shaderProgram; + return SLANG_OK; + } + + void compareComputeResult(gfx::IDevice* device, gfx::IBufferResource* buffer, uint8_t* expectedResult, size_t expectedBufferSize) + { + // Read back the results. + ComPtr<ISlangBlob> resultBlob; + GFX_CHECK_CALL_ABORT(device->readBufferResource( + buffer, 0, expectedBufferSize, resultBlob.writeRef())); + if (resultBlob->getBufferSize() < expectedBufferSize) + { + getTestReporter()->addResult(TestResult::Fail); + return; + } + + // Compare results. + auto result = reinterpret_cast<const uint8_t*>(resultBlob->getBufferPointer()); + for (int i = 0; i < expectedBufferSize; i++) + { + if (expectedResult[i] != result[i]) + { + getTestReporter()->addResult(TestResult::Fail); + return; + } + + } + getTestReporter()->addResult(TestResult::Pass); + } +} diff --git a/tools/gfx-unit-test/gfx-test-util.h b/tools/gfx-unit-test/gfx-test-util.h new file mode 100644 index 000000000..7709512d5 --- /dev/null +++ b/tools/gfx-unit-test/gfx-test-util.h @@ -0,0 +1,41 @@ +#pragma once + +#include "slang-gfx.h" +#include "source/core/slang-basic.h" + +namespace gfx_test +{ + /// Helper function for print out diagnostic messages output by Slang compiler. + void diagnoseIfNeeded(ISlangWriter* diagnosticWriter, slang::IBlob* diagnosticsBlob); + + /// Loads a compute shader module and produces a `gfx::IShaderProgram`. + Slang::Result loadShaderProgram( + gfx::IDevice* device, + Slang::ComPtr<gfx::IShaderProgram>& outShaderProgram, + const char* shaderModuleName, + slang::ProgramLayout*& slangReflection); + + /// Reads back the content of `buffer` and compares it against `expectedResult`. + void compareComputeResult( + gfx::IDevice* device, + gfx::IBufferResource* buffer, + uint8_t* expectedResult, + size_t expectedBufferSize); + + template<typename T, Slang::Index count> + void compareComputeResult( + gfx::IDevice* device, + gfx::IBufferResource* buffer, + Slang::Array<T, count> expectedResult) + { + Slang::List<uint8_t> expectedBuffer; + size_t bufferSize = sizeof(T) * count; + expectedBuffer.setCount(bufferSize); + memcpy(expectedBuffer.getBuffer(), expectedResult.begin(), bufferSize); + return compareComputeResult(device, buffer, expectedBuffer.getBuffer(), bufferSize); + } + +#define GFX_CHECK_CALL(x) {auto callResult = (x); SLANG_CHECK(!SLANG_FAILED(callResult))} +#define GFX_CHECK_CALL_ABORT(x) {auto callResult = (x); SLANG_CHECK_ABORT(!SLANG_FAILED(callResult))} + +} |
