summaryrefslogtreecommitdiff
path: root/tools/gfx-test
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2021-09-24 11:33:44 -0700
committerGitHub <noreply@github.com>2021-09-24 11:33:44 -0700
commitbec8e6aec85b6e3f875c58bdd59eb15613978358 (patch)
tree0791fb2ce1be786c17e5a6ee489ed3065fc07332 /tools/gfx-test
parentf2a3c933bc11a498c622fa18694c84beca8ca031 (diff)
Move existing unit tests to a standalone dll. (#1945)
Diffstat (limited to 'tools/gfx-test')
-rw-r--r--tools/gfx-test/compute-smoke.cpp145
-rw-r--r--tools/gfx-test/compute-smoke.slang68
-rw-r--r--tools/gfx-test/gfx-test-util.cpp79
-rw-r--r--tools/gfx-test/gfx-test-util.h38
4 files changed, 0 insertions, 330 deletions
diff --git a/tools/gfx-test/compute-smoke.cpp b/tools/gfx-test/compute-smoke.cpp
deleted file mode 100644
index bbba72348..000000000
--- a/tools/gfx-test/compute-smoke.cpp
+++ /dev/null
@@ -1,145 +0,0 @@
-#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
-{
- SlangResult computeSmokeTestImpl(IDevice* device, slang::UnitTestContext* context)
- {
- Slang::ComPtr<ITransientResourceHeap> transientHeap;
- ITransientResourceHeap::Desc transientHeapDesc = {};
- transientHeapDesc.constantBufferSize = 4096;
- SLANG_RETURN_ON_FAIL(
- device->createTransientResourceHeap(transientHeapDesc, transientHeap.writeRef()));
-
- ComPtr<IShaderProgram> shaderProgram;
- slang::ProgramLayout* slangReflection;
- SLANG_RETURN_ON_FAIL(loadShaderProgram(device, shaderProgram, context->outputWriter, "compute-smoke", slangReflection));
-
- ComputePipelineStateDesc pipelineDesc = {};
- pipelineDesc.program = shaderProgram.get();
- ComPtr<gfx::IPipelineState> pipelineState;
- SLANG_RETURN_ON_FAIL(
- 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;
- SLANG_RETURN_ON_FAIL(device->createBufferResource(
- bufferDesc,
- (void*)initialData,
- numbersBuffer.writeRef()));
-
- ComPtr<IResourceView> bufferView;
- IResourceView::Desc viewDesc = {};
- viewDesc.type = IResourceView::Type::UnorderedAccess;
- viewDesc.format = Format::Unknown;
- SLANG_RETURN_ON_FAIL(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;
- SLANG_RETURN_ON_FAIL(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();
- }
-
- return compareComputeResult(
- device,
- numbersBuffer,
- Slang::makeArray<float>(11.0f, 12.0f, 13.0f, 14.0f));
- }
-
- SlangResult computeSmokeTestAPI(slang::UnitTestContext* context, Slang::RenderApiFlag::Enum api)
- {
- if ((api & context->enabledApis) == 0)
- {
- return SLANG_E_NOT_AVAILABLE;
- }
- 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:
- return SLANG_E_NOT_AVAILABLE;
- }
- deviceDesc.slang.slangGlobalSession = context->slangGlobalSession;
- const char* searchPaths[] = { "", "../../tools/gfx-test", "tools/gfx-test" };
- deviceDesc.slang.searchPathCount = (SlangInt)SLANG_COUNT_OF(searchPaths);
- deviceDesc.slang.searchPaths = searchPaths;
- auto createDeviceResult = gfxCreateDevice(&deviceDesc, device.writeRef());
- if (SLANG_FAILED(createDeviceResult))
- {
- return SLANG_E_NOT_AVAILABLE;
- }
-
- SLANG_RETURN_ON_FAIL(computeSmokeTestImpl(device, context));
- return SLANG_OK;
- }
-
- SLANG_UNIT_TEST(computeSmokeD3D11)
- {
- return computeSmokeTestAPI(context, Slang::RenderApiFlag::D3D11);
- }
-
- SLANG_UNIT_TEST(computeSmokeVulkan)
- {
- return computeSmokeTestAPI(context, Slang::RenderApiFlag::Vulkan);
- }
-
-}
diff --git a/tools/gfx-test/compute-smoke.slang b/tools/gfx-test/compute-smoke.slang
deleted file mode 100644
index 7ecdb4177..000000000
--- a/tools/gfx-test/compute-smoke.slang
+++ /dev/null
@@ -1,68 +0,0 @@
-// 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-test/gfx-test-util.cpp b/tools/gfx-test/gfx-test-util.cpp
deleted file mode 100644
index 5e77879a9..000000000
--- a/tools/gfx-test/gfx-test-util.cpp
+++ /dev/null
@@ -1,79 +0,0 @@
-#include "gfx-test-util.h"
-
-#include <slang-com-ptr.h>
-
-using Slang::ComPtr;
-
-namespace gfx_test
-{
- void diagnoseIfNeeded(ISlangWriter* diagnosticWriter, slang::IBlob* diagnosticsBlob)
- {
- if (diagnosticsBlob != nullptr)
- {
- diagnosticWriter->write((const char*)diagnosticsBlob->getBufferPointer(), diagnosticsBlob->getBufferSize());
- }
- }
-
- Slang::Result loadShaderProgram(
- gfx::IDevice* device,
- Slang::ComPtr<gfx::IShaderProgram>& outShaderProgram,
- ISlangWriter* diagnosticWriter,
- 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(diagnosticWriter, 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(diagnosticWriter, 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;
- }
-
- Slang::Result compareComputeResult(gfx::IDevice* device, gfx::IBufferResource* buffer, uint8_t* expectedResult, size_t expectedBufferSize)
- {
- // Read back the results.
- ComPtr<ISlangBlob> resultBlob;
- SLANG_RETURN_ON_FAIL(device->readBufferResource(
- buffer, 0, expectedBufferSize, resultBlob.writeRef()));
- if (resultBlob->getBufferSize() < expectedBufferSize)
- return SLANG_FAIL;
-
- // Compare results.
- auto result = reinterpret_cast<const uint8_t*>(resultBlob->getBufferPointer());
- for (int i = 0; i < expectedBufferSize; i++)
- {
- if (expectedResult[i] != result[i])
- return SLANG_FAIL;
- }
- return SLANG_OK;
- }
-}
diff --git a/tools/gfx-test/gfx-test-util.h b/tools/gfx-test/gfx-test-util.h
deleted file mode 100644
index 5223ba773..000000000
--- a/tools/gfx-test/gfx-test-util.h
+++ /dev/null
@@ -1,38 +0,0 @@
-#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,
- ISlangWriter* diagnosticWriter,
- const char* shaderModuleName,
- slang::ProgramLayout*& slangReflection);
-
- /// Reads back the content of `buffer` and compares it against `expectedResult`.
- Slang::Result compareComputeResult(
- gfx::IDevice* device,
- gfx::IBufferResource* buffer,
- uint8_t* expectedResult,
- size_t expectedBufferSize);
-
- template<typename T, Slang::Index count>
- Slang::Result 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);
- }
-}