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 | |
| 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>
| -rw-r--r-- | build/visual-studio/gfx-test-tool/gfx-test-tool.vcxproj | 197 | ||||
| -rw-r--r-- | build/visual-studio/gfx-test-tool/gfx-test-tool.vcxproj.filters | 35 | ||||
| -rw-r--r-- | premake5.lua | 5 | ||||
| -rw-r--r-- | slang.sln | 11 | ||||
| -rw-r--r-- | tools/gfx-test/compute-smoke.cpp | 145 | ||||
| -rw-r--r-- | tools/gfx-test/compute-smoke.slang | 68 | ||||
| -rw-r--r-- | tools/gfx-test/gfx-test-util.cpp | 79 | ||||
| -rw-r--r-- | tools/gfx-test/gfx-test-util.h | 38 | ||||
| -rw-r--r-- | tools/render-test/render-test-main.cpp | 7 | ||||
| -rw-r--r-- | tools/slang-test/slang-test-main.cpp | 64 | ||||
| -rw-r--r-- | tools/unit-test/slang-unit-test.cpp | 48 | ||||
| -rw-r--r-- | tools/unit-test/slang-unit-test.h | 38 |
12 files changed, 735 insertions, 0 deletions
diff --git a/build/visual-studio/gfx-test-tool/gfx-test-tool.vcxproj b/build/visual-studio/gfx-test-tool/gfx-test-tool.vcxproj new file mode 100644 index 000000000..ee8887538 --- /dev/null +++ b/build/visual-studio/gfx-test-tool/gfx-test-tool.vcxproj @@ -0,0 +1,197 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <ProjectGuid>{092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519}</ProjectGuid> + <IgnoreWarnCompileDuplicatedFilename>true</IgnoreWarnCompileDuplicatedFilename> + <Keyword>Win32Proj</Keyword> + <RootNamespace>gfx-test-tool</RootNamespace> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <CharacterSet>Unicode</CharacterSet> + <PlatformToolset>v142</PlatformToolset> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <CharacterSet>Unicode</CharacterSet> + <PlatformToolset>v142</PlatformToolset> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <CharacterSet>Unicode</CharacterSet> + <PlatformToolset>v142</PlatformToolset> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <CharacterSet>Unicode</CharacterSet> + <PlatformToolset>v142</PlatformToolset> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <LinkIncremental>true</LinkIncremental> + <OutDir>..\..\..\bin\windows-x86\debug\</OutDir> + <IntDir>..\..\..\intermediate\windows-x86\debug\gfx-test-tool\</IntDir> + <TargetName>gfx-test-tool</TargetName> + <TargetExt>.dll</TargetExt> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <LinkIncremental>true</LinkIncremental> + <OutDir>..\..\..\bin\windows-x64\debug\</OutDir> + <IntDir>..\..\..\intermediate\windows-x64\debug\gfx-test-tool\</IntDir> + <TargetName>gfx-test-tool</TargetName> + <TargetExt>.dll</TargetExt> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <LinkIncremental>false</LinkIncremental> + <OutDir>..\..\..\bin\windows-x86\release\</OutDir> + <IntDir>..\..\..\intermediate\windows-x86\release\gfx-test-tool\</IntDir> + <TargetName>gfx-test-tool</TargetName> + <TargetExt>.dll</TargetExt> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <LinkIncremental>false</LinkIncremental> + <OutDir>..\..\..\bin\windows-x64\release\</OutDir> + <IntDir>..\..\..\intermediate\windows-x64\release\gfx-test-tool\</IntDir> + <TargetName>gfx-test-tool</TargetName> + <TargetExt>.dll</TargetExt> + </PropertyGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <ClCompile> + <PrecompiledHeader>NotUsing</PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <PreprocessorDefinitions>_DEBUG;SLANG_SHARED_LIBRARY_TOOL;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <AdditionalIncludeDirectories>..\..\..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <DebugInformationFormat>EditAndContinue</DebugInformationFormat> + <Optimization>Disabled</Optimization> + <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> + </ClCompile> + <Link> + <SubSystem>Windows</SubSystem> + <GenerateDebugInformation>true</GenerateDebugInformation> + <ImportLibrary>..\..\..\bin\windows-x86\debug\gfx-test-tool.lib</ImportLibrary> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <ClCompile> + <PrecompiledHeader>NotUsing</PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <PreprocessorDefinitions>_DEBUG;SLANG_SHARED_LIBRARY_TOOL;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <AdditionalIncludeDirectories>..\..\..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <DebugInformationFormat>EditAndContinue</DebugInformationFormat> + <Optimization>Disabled</Optimization> + <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> + </ClCompile> + <Link> + <SubSystem>Windows</SubSystem> + <GenerateDebugInformation>true</GenerateDebugInformation> + <ImportLibrary>..\..\..\bin\windows-x64\debug\gfx-test-tool.lib</ImportLibrary> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <ClCompile> + <PrecompiledHeader>NotUsing</PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <PreprocessorDefinitions>NDEBUG;SLANG_SHARED_LIBRARY_TOOL;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <AdditionalIncludeDirectories>..\..\..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <Optimization>Full</Optimization> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <MinimalRebuild>false</MinimalRebuild> + <StringPooling>true</StringPooling> + <RuntimeLibrary>MultiThreaded</RuntimeLibrary> + </ClCompile> + <Link> + <SubSystem>Windows</SubSystem> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + <ImportLibrary>..\..\..\bin\windows-x86\release\gfx-test-tool.lib</ImportLibrary> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <ClCompile> + <PrecompiledHeader>NotUsing</PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <PreprocessorDefinitions>NDEBUG;SLANG_SHARED_LIBRARY_TOOL;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <AdditionalIncludeDirectories>..\..\..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <Optimization>Full</Optimization> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <MinimalRebuild>false</MinimalRebuild> + <StringPooling>true</StringPooling> + <RuntimeLibrary>MultiThreaded</RuntimeLibrary> + </ClCompile> + <Link> + <SubSystem>Windows</SubSystem> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + <ImportLibrary>..\..\..\bin\windows-x64\release\gfx-test-tool.lib</ImportLibrary> + </Link> + </ItemDefinitionGroup> + <ItemGroup> + <ClInclude Include="..\..\..\tools\gfx-test\gfx-test-util.h" /> + <ClInclude Include="..\..\..\tools\unit-test\slang-unit-test.h" /> + </ItemGroup> + <ItemGroup> + <ClCompile Include="..\..\..\tools\gfx-test\compute-smoke.cpp" /> + <ClCompile Include="..\..\..\tools\gfx-test\gfx-test-util.cpp" /> + <ClCompile Include="..\..\..\tools\unit-test\slang-unit-test.cpp" /> + </ItemGroup> + <ItemGroup> + <None Include="..\..\..\tools\gfx-test\compute-smoke.slang" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\gfx\gfx.vcxproj"> + <Project>{222F7498-B40C-4F3F-A704-DDEB91A4484A}</Project> + </ProjectReference> + <ProjectReference Include="..\gfx-util\gfx-util.vcxproj"> + <Project>{F5ADB74E-02A7-44FB-AA3B-FC02F8AC7A4B}</Project> + </ProjectReference> + <ProjectReference Include="..\slang\slang.vcxproj"> + <Project>{DB00DA62-0533-4AFD-B59F-A67D5B3A0808}</Project> + </ProjectReference> + <ProjectReference Include="..\core\core.vcxproj"> + <Project>{F9BE7957-8399-899E-0C49-E714FDDD4B65}</Project> + </ProjectReference> + </ItemGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> + <ImportGroup Label="ExtensionTargets"> + </ImportGroup> +</Project>
\ No newline at end of file diff --git a/build/visual-studio/gfx-test-tool/gfx-test-tool.vcxproj.filters b/build/visual-studio/gfx-test-tool/gfx-test-tool.vcxproj.filters new file mode 100644 index 000000000..9e4542487 --- /dev/null +++ b/build/visual-studio/gfx-test-tool/gfx-test-tool.vcxproj.filters @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="Header Files"> + <UniqueIdentifier>{21EB8090-0D4E-1035-B6D3-48EBA215DCB7}</UniqueIdentifier> + </Filter> + <Filter Include="Source Files"> + <UniqueIdentifier>{E9C7FDCE-D52A-8D73-7EB0-C5296AF258F6}</UniqueIdentifier> + </Filter> + </ItemGroup> + <ItemGroup> + <ClInclude Include="..\..\..\tools\gfx-test\gfx-test-util.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="..\..\..\tools\unit-test\slang-unit-test.h"> + <Filter>Header Files</Filter> + </ClInclude> + </ItemGroup> + <ItemGroup> + <ClCompile Include="..\..\..\tools\gfx-test\compute-smoke.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="..\..\..\tools\gfx-test\gfx-test-util.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="..\..\..\tools\unit-test\slang-unit-test.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + </ItemGroup> + <ItemGroup> + <None Include="..\..\..\tools\gfx-test\compute-smoke.slang"> + <Filter>Source Files</Filter> + </None> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/premake5.lua b/premake5.lua index 38cb78a9a..02f8f3600 100644 --- a/premake5.lua +++ b/premake5.lua @@ -1251,6 +1251,11 @@ standardProject("slang", "source/slang") defines { "SLANG_ENABLE_IR_BREAK_ALLOC=1" } filter {} +toolSharedLibrary "gfx-test" + uuid "092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519" + includedirs { "." } + addSourceDir "tools/unit-test" + links { "gfx", "gfx-util", "slang", "core" } if enableProfile then tool "slang-profile" @@ -67,6 +67,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "slangc", "build\visual-stud EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test-tool", "test-tool", "{57B5AA5E-C340-1823-CC51-9B17385C7423}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "gfx-test-tool", "build\visual-studio\gfx-test-tool\gfx-test-tool.vcxproj", "{092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519}" +EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "render-test-tool", "build\visual-studio\render-test-tool\render-test-tool.vcxproj", "{61F7EB00-7281-4BF3-9470-7C2EA92620C3}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "slang-reflection-test-tool", "build\visual-studio\slang-reflection-test-tool\slang-reflection-test-tool.vcxproj", "{C5ACCA6E-C04D-4B36-8516-3752B3C13C2F}" @@ -279,6 +281,14 @@ Global {D56CBCEB-1EB5-4CA8-AEC4-48EA35ED61C7}.Release|Win32.Build.0 = Release|Win32 {D56CBCEB-1EB5-4CA8-AEC4-48EA35ED61C7}.Release|x64.ActiveCfg = Release|x64 {D56CBCEB-1EB5-4CA8-AEC4-48EA35ED61C7}.Release|x64.Build.0 = Release|x64 + {092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519}.Debug|Win32.ActiveCfg = Debug|Win32 + {092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519}.Debug|Win32.Build.0 = Debug|Win32 + {092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519}.Debug|x64.ActiveCfg = Debug|x64 + {092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519}.Debug|x64.Build.0 = Debug|x64 + {092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519}.Release|Win32.ActiveCfg = Release|Win32 + {092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519}.Release|Win32.Build.0 = Release|Win32 + {092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519}.Release|x64.ActiveCfg = Release|x64 + {092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519}.Release|x64.Build.0 = Release|x64 {61F7EB00-7281-4BF3-9470-7C2EA92620C3}.Debug|Win32.ActiveCfg = Debug|Win32 {61F7EB00-7281-4BF3-9470-7C2EA92620C3}.Debug|Win32.Build.0 = Debug|Win32 {61F7EB00-7281-4BF3-9470-7C2EA92620C3}.Debug|x64.ActiveCfg = Debug|x64 @@ -319,6 +329,7 @@ Global {0FC5DE93-FBEA-A8FA-E430-2EC6D0F5CDC6} = {EB5FC2C6-D72D-B6CC-C0C1-26F3AC2E9231} {3BB99068-27C9-3C39-9082-A1577CB12BD2} = {EB5FC2C6-D72D-B6CC-C0C1-26F3AC2E9231} {E145B2B8-CD13-A6BE-B6A7-16E5A2148223} = {F3AB4ED5-5F37-BC99-6848-3F8ED452189A} + {092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519} = {57B5AA5E-C340-1823-CC51-9B17385C7423} {61F7EB00-7281-4BF3-9470-7C2EA92620C3} = {57B5AA5E-C340-1823-CC51-9B17385C7423} {C5ACCA6E-C04D-4B36-8516-3752B3C13C2F} = {57B5AA5E-C340-1823-CC51-9B17385C7423} EndGlobalSection diff --git a/tools/gfx-test/compute-smoke.cpp b/tools/gfx-test/compute-smoke.cpp new file mode 100644 index 000000000..bbba72348 --- /dev/null +++ b/tools/gfx-test/compute-smoke.cpp @@ -0,0 +1,145 @@ +#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 new file mode 100644 index 000000000..7ecdb4177 --- /dev/null +++ b/tools/gfx-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-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; + } +} diff --git a/tools/gfx-test/gfx-test-util.h b/tools/gfx-test/gfx-test-util.h new file mode 100644 index 000000000..5223ba773 --- /dev/null +++ b/tools/gfx-test/gfx-test-util.h @@ -0,0 +1,38 @@ +#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); + } +} diff --git a/tools/render-test/render-test-main.cpp b/tools/render-test/render-test-main.cpp index 271356cc3..7c65da5e7 100644 --- a/tools/render-test/render-test-main.cpp +++ b/tools/render-test/render-test-main.cpp @@ -1264,6 +1264,13 @@ static SlangResult _innerMain(Slang::StdWriters* stdWriters, SlangSession* sessi StdWritersDebugCallback debugCallback; debugCallback.writers = stdWriters; gfxSetDebugCallback(&debugCallback); + struct ResetDebugCallbackRAII + { + ~ResetDebugCallbackRAII() + { + gfxSetDebugCallback(nullptr); + } + } resetDebugCallbackRAII; // Use the profile name set on options if set input.profile = options.profileName.getLength() ? options.profileName : input.profile; diff --git a/tools/slang-test/slang-test-main.cpp b/tools/slang-test/slang-test-main.cpp index 18ac05477..9c194afd4 100644 --- a/tools/slang-test/slang-test-main.cpp +++ b/tools/slang-test/slang-test-main.cpp @@ -17,6 +17,8 @@ #include "../../source/core/slang-process-util.h" #include "../../source/core/slang-render-api-util.h" +#include "tools/unit-test/slang-unit-test.h" +#undef SLANG_UNIT_TEST #include "directory-util.h" #include "test-context.h" @@ -39,6 +41,7 @@ #define SLANG_PRELUDE_NAMESPACE CPPPrelude #include "../../prelude/slang-cpp-types.h" + using namespace Slang; // Options for a particular test @@ -3389,6 +3392,62 @@ static void _disableCPPBackends(TestContext* context) } } + /// Loads a DLL containing unit test functions and run them one by one. +static SlangResult runUnitTestModule(TestContext* context, TestOptions& testOptions, const char* moduleName) +{ + SharedLibrary::Handle moduleHandle; + SLANG_RETURN_ON_FAIL(SharedLibrary::load( + Path::combine(context->exeDirectoryPath, moduleName).getBuffer(), + moduleHandle)); + slang::UnitTestGetModuleFunc getModuleFunc = + (slang::UnitTestGetModuleFunc) SharedLibrary::findSymbolAddressByName( + moduleHandle, "slangUnitTestGetModule"); + if (!getModuleFunc) + return SLANG_FAIL; + + slang::IUnitTestModule* testModule = getModuleFunc(); + if (!testModule) + return SLANG_FAIL; + + slang::UnitTestContext unitTestContext; + unitTestContext.slangGlobalSession = context->getSession(); + unitTestContext.workDirectory = ""; + unitTestContext.enabledApis = context->options.enabledApis; + auto testCount = testModule->getTestCount(); + for (SlangInt i = 0; i < testCount; i++) + { + StringBuilder sb; + StringWriter messageWriter(&sb, WriterFlag::Enum::AutoFlush); + auto testFunc = testModule->getTestFunc(i); + auto testName = testModule->getTestName(i); + + StringBuilder filePath; + filePath << moduleName << "/" << testName << ".internal"; + + testOptions.command = filePath; + + if (shouldRunTest(context, testOptions.command)) + { + if (testPassesCategoryMask(context, testOptions)) + { + unitTestContext.outputWriter = &messageWriter; + TestReporter::get()->startTest(testOptions.command); + auto result = testFunc(&unitTestContext); + if (sb.getLength()) + TestReporter::get()->message(TestMessageType::Info, sb.ProduceString()); + + if (result == SLANG_E_NOT_AVAILABLE) + TestReporter::get()->addResult(TestResult::Ignored); + else if (SLANG_FAILED(result)) + TestReporter::get()->addResult(TestResult::Fail); + else + TestReporter::get()->addResult(TestResult::Pass); + TestReporter::get()->endTest(); + } + } + } + return SLANG_OK; +} SlangResult innerMain(int argc, char** argv) { @@ -3594,6 +3653,11 @@ SlangResult innerMain(int argc, char** argv) cur = cur->m_next; } + { + TestOptions testOptions; + testOptions.categories.add(unitTestCategory); + runUnitTestModule(&context, testOptions, "gfx-test-tool"); + } TestReporter::set(nullptr); } diff --git a/tools/unit-test/slang-unit-test.cpp b/tools/unit-test/slang-unit-test.cpp new file mode 100644 index 000000000..a31614c05 --- /dev/null +++ b/tools/unit-test/slang-unit-test.cpp @@ -0,0 +1,48 @@ +#include "slang-unit-test.h" +#include "slang.h" +#include "source/core/slang-basic.h" + +struct SlangUnitTest +{ + const char* name; + slang::UnitTestFunc func; +}; + +class SlangUnitTestModule : public slang::IUnitTestModule +{ +public: + Slang::List<SlangUnitTest> tests; + + virtual SlangInt getTestCount() override + { + return tests.getCount(); + } + virtual const char* getTestName(SlangInt index) override + { + return tests[index].name; + } + + virtual slang::UnitTestFunc getTestFunc(SlangInt index) override + { + return tests[index].func; + } +}; + +SlangUnitTestModule* _getTestModule() +{ + static SlangUnitTestModule testModule; + return &testModule; +} + +extern "C" +{ +SLANG_DLL_EXPORT slang::IUnitTestModule* slangUnitTestGetModule() +{ + return _getTestModule(); +} +} + +slang::UnitTestRegisterHelper::UnitTestRegisterHelper(const char* name, UnitTestFunc testFunc) +{ + _getTestModule()->tests.add(SlangUnitTest{ name, testFunc }); +} diff --git a/tools/unit-test/slang-unit-test.h b/tools/unit-test/slang-unit-test.h new file mode 100644 index 000000000..7651e6b46 --- /dev/null +++ b/tools/unit-test/slang-unit-test.h @@ -0,0 +1,38 @@ +#pragma once + +#include "slang.h" +#include "source/core/slang-render-api-util.h" + +namespace slang +{ + struct UnitTestContext + { + slang::IGlobalSession* slangGlobalSession; + const char* workDirectory; + ISlangWriter* outputWriter; + Slang::RenderApiFlags enabledApis; + }; + + typedef SlangResult (*UnitTestFunc)(UnitTestContext*); + + class IUnitTestModule + { + public: + virtual SlangInt getTestCount() = 0; + virtual const char* getTestName(SlangInt index) = 0; + virtual UnitTestFunc getTestFunc(SlangInt index) = 0; + }; + + class UnitTestRegisterHelper + { + public: + UnitTestRegisterHelper(const char* name, UnitTestFunc testFunc); + }; + + typedef slang::IUnitTestModule* (*UnitTestGetModuleFunc)(); + +#define SLANG_UNIT_TEST(name) \ + SlangResult name(slang::UnitTestContext* context); \ + slang::UnitTestRegisterHelper _##name##RegisterHelper(#name, name); \ + SlangResult name(slang::UnitTestContext* context) +} |
