yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Gangzheng TongConvert gfx unit tests and examples to use slang-rhi (#7577)43d0c2100

master
3.1 KiB103 linesraw
1#include "core/slang-basic.h"
2#include "gfx-test-util.h"
3#include "slang-rhi/shader-cursor.h"
4#include "unit-test/slang-unit-test.h"
5
6#include <slang-rhi.h>
7
8#if SLANG_WINDOWS_FAMILY
9#include <d3d12.h>
10#endif
11
12using namespace rhi;
13
14namespace gfx_test
15{
16void getTextureResourceHandleTestImpl(IDevice* device, UnitTestContext* context)
17{
18    TextureDesc desc = {};
19    desc.type = TextureType::Texture2D;
20    desc.mipCount = 1;
21    desc.size.width = 1;
22    desc.size.height = 1;
23    desc.size.depth = 1;
24    desc.defaultState = ResourceState::UnorderedAccess;
25    desc.format = Format::RGBA16Float;
26    desc.usage = TextureUsage::UnorderedAccess;
27
28    Slang::ComPtr<ITexture> texture;
29    GFX_CHECK_CALL_ABORT(device->createTexture(desc, nullptr, texture.writeRef()));
30
31    NativeHandle handle;
32    GFX_CHECK_CALL_ABORT(texture->getNativeHandle(&handle));
33    if (device->getInfo().deviceType == rhi::DeviceType::Vulkan)
34    {
35        SLANG_CHECK(handle.value != 0);
36    }
37
38#if SLANG_WINDOWS_FAMILY
39    else
40    {
41        auto d3d12Handle = (ID3D12Resource*)handle.value;
42        Slang::ComPtr<IUnknown> testHandle1;
43        GFX_CHECK_CALL_ABORT(d3d12Handle->QueryInterface<IUnknown>(testHandle1.writeRef()));
44        Slang::ComPtr<ID3D12Resource> testHandle2;
45        GFX_CHECK_CALL_ABORT(testHandle1->QueryInterface<ID3D12Resource>(testHandle2.writeRef()));
46        SLANG_CHECK(d3d12Handle == testHandle2.get());
47    }
48#endif
49}
50
51void getTextureResourceHandleTestAPI(UnitTestContext* context, Slang::RenderApiFlag::Enum api)
52{
53    if (context->enableDebugLayers)
54        getRHI()->enableDebugLayers();
55    if ((api & context->enabledApis) == 0)
56    {
57        SLANG_IGNORE_TEST;
58    }
59    Slang::ComPtr<IDevice> device;
60    DeviceDesc deviceDesc = {};
61    switch (api)
62    {
63    case Slang::RenderApiFlag::D3D11:
64        deviceDesc.deviceType = rhi::DeviceType::D3D11;
65        break;
66    case Slang::RenderApiFlag::D3D12:
67        deviceDesc.deviceType = rhi::DeviceType::D3D12;
68        break;
69    case Slang::RenderApiFlag::Vulkan:
70        deviceDesc.deviceType = rhi::DeviceType::Vulkan;
71        break;
72    default:
73        SLANG_IGNORE_TEST;
74    }
75    deviceDesc.slang.slangGlobalSession = context->slangGlobalSession;
76    const char* searchPaths[] = {"", "../../tools/gfx-unit-test", "tools/gfx-unit-test"};
77    deviceDesc.slang.searchPathCount = (SlangInt)SLANG_COUNT_OF(searchPaths);
78    deviceDesc.slang.searchPaths = searchPaths;
79    auto createDeviceResult = getRHI()->createDevice(deviceDesc, device.writeRef());
80    if (SLANG_FAILED(createDeviceResult))
81    {
82        SLANG_IGNORE_TEST;
83    }
84    // Ignore this test on swiftshader. Swiftshader seems to have a bug that causes the test
85    // to crash.
86    if (Slang::String(device->getInfo().adapterName).toLower().contains("swiftshader"))
87    {
88        SLANG_IGNORE_TEST;
89    }
90    getTextureResourceHandleTestImpl(device, context);
91}
92
93SLANG_UNIT_TEST(getTextureResourceHandleD3D12)
94{
95    return getTextureResourceHandleTestAPI(unitTestContext, Slang::RenderApiFlag::D3D12);
96}
97
98SLANG_UNIT_TEST(getTextureResourceHandleVulkan)
99{
100    return getTextureResourceHandleTestAPI(unitTestContext, Slang::RenderApiFlag::Vulkan);
101}
102
103} // namespace gfx_test