diff options
| author | Yong He <yonghe@outlook.com> | 2021-09-22 10:06:59 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-22 10:06:59 -0700 |
| commit | b9b398d038b524f15a86ff27cd6888d54e8754e0 (patch) | |
| tree | 4fe46f864065a58db20edccd261e5794326ab2a1 /tools/gfx-test/gfx-test-util.cpp | |
| parent | 6e9cee69b3588ddae09b08b9f580f59ad899983f (diff) | |
Add gfx unit testing framework. (#1943)
* Add gfx unit testing framework.
* Fix compilation error.
* Reset gfxDebugCallback after render_test.
* Pass enabledApi flags through.
* Fix for code review suggestions.
Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'tools/gfx-test/gfx-test-util.cpp')
| -rw-r--r-- | tools/gfx-test/gfx-test-util.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/tools/gfx-test/gfx-test-util.cpp b/tools/gfx-test/gfx-test-util.cpp new file mode 100644 index 000000000..5e77879a9 --- /dev/null +++ b/tools/gfx-test/gfx-test-util.cpp @@ -0,0 +1,79 @@ +#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; + } +} |
