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
4.1 KiB120 linesraw
1#include "core/slang-basic.h"
2#include "gfx-test-util.h"
3#include "slang-rhi.h"
4#include "slang-rhi/shader-cursor.h"
5#include "unit-test/slang-unit-test.h"
6
7using namespace rhi;
8
9namespace gfx_test
10{
11void existingDeviceHandleTestImpl(IDevice* device, UnitTestContext* context)
12{
13    ComPtr<IShaderProgram> shaderProgram;
14    slang::ProgramLayout* slangReflection;
15    GFX_CHECK_CALL_ABORT(loadComputeProgram(
16        device,
17        shaderProgram,
18        "compute-trivial",
19        "computeMain",
20        slangReflection));
21
22    ComputePipelineDesc pipelineDesc = {};
23    pipelineDesc.program = shaderProgram.get();
24    ComPtr<IComputePipeline> pipelineState;
25    GFX_CHECK_CALL_ABORT(device->createComputePipeline(pipelineDesc, pipelineState.writeRef()));
26
27    const int numberCount = 4;
28    float initialData[] = {0.0f, 1.0f, 2.0f, 3.0f};
29    BufferDesc bufferDesc = {};
30    bufferDesc.size = numberCount * sizeof(float);
31    bufferDesc.format = Format::Undefined;
32    bufferDesc.elementSize = sizeof(float);
33    bufferDesc.usage = BufferUsage::ShaderResource | BufferUsage::UnorderedAccess |
34                       BufferUsage::CopyDestination | BufferUsage::CopySource;
35    bufferDesc.defaultState = ResourceState::UnorderedAccess;
36    bufferDesc.memoryType = MemoryType::DeviceLocal;
37
38    ComPtr<IBuffer> numbersBuffer;
39    GFX_CHECK_CALL_ABORT(
40        device->createBuffer(bufferDesc, (void*)initialData, numbersBuffer.writeRef()));
41
42    // We have done all the set up work, now it is time to start recording a command buffer for
43    // GPU execution.
44    {
45        auto queue = device->getQueue(QueueType::Graphics);
46        auto commandEncoder = queue->createCommandEncoder();
47        {
48            auto encoder = commandEncoder->beginComputePass();
49            auto rootObject = encoder->bindPipeline(pipelineState);
50
51            ShaderCursor rootCursor(rootObject);
52            // Bind buffer directly to the root.
53            rootCursor.getPath("buffer").setBinding(Binding(numbersBuffer));
54
55            encoder->dispatchCompute(1, 1, 1);
56            encoder->end();
57        }
58
59        auto commandBuffer = commandEncoder->finish();
60        queue->submit(commandBuffer);
61        queue->waitOnHost();
62    }
63
64    compareComputeResult(device, numbersBuffer, std::array{1.0f, 2.0f, 3.0f, 4.0f});
65}
66
67void existingDeviceHandleTestAPI(UnitTestContext* context, DeviceType deviceType)
68{
69    if (!deviceTypeInEnabledApis(deviceType, context->enabledApis))
70    {
71        SLANG_IGNORE_TEST
72    }
73    Slang::ComPtr<IDevice> device;
74    DeviceDesc deviceDesc = {};
75    deviceDesc.deviceType = deviceType;
76    deviceDesc.slang.slangGlobalSession = context->slangGlobalSession;
77    const char* searchPaths[] = {"", "../../tools/gfx-unit-test", "tools/gfx-unit-test"};
78    deviceDesc.slang.searchPathCount = (SlangInt)SLANG_COUNT_OF(searchPaths);
79    deviceDesc.slang.searchPaths = searchPaths;
80    auto createDeviceResult = getRHI()->createDevice(deviceDesc, device.writeRef());
81    if (SLANG_FAILED(createDeviceResult) || !device)
82    {
83        SLANG_IGNORE_TEST;
84    }
85
86    DeviceNativeHandles handles;
87    GFX_CHECK_CALL_ABORT(device->getNativeDeviceHandles(&handles));
88    Slang::ComPtr<IDevice> testDevice;
89    DeviceDesc testDeviceDesc = deviceDesc;
90    testDeviceDesc.existingDeviceHandles.handles[0] = handles.handles[0];
91    if (deviceType == DeviceType::Vulkan)
92    {
93        testDeviceDesc.existingDeviceHandles.handles[1] = handles.handles[1];
94        testDeviceDesc.existingDeviceHandles.handles[2] = handles.handles[2];
95    }
96    auto createTestDeviceResult = getRHI()->createDevice(testDeviceDesc, testDevice.writeRef());
97    if (SLANG_FAILED(createTestDeviceResult) || !testDevice)
98    {
99        SLANG_IGNORE_TEST;
100    }
101
102    existingDeviceHandleTestImpl(testDevice, context);
103}
104
105SLANG_UNIT_TEST(existingDeviceHandleD3D12)
106{
107    return existingDeviceHandleTestAPI(unitTestContext, DeviceType::D3D12);
108}
109
110SLANG_UNIT_TEST(existingDeviceHandleVulkan)
111{
112    return existingDeviceHandleTestAPI(unitTestContext, DeviceType::Vulkan);
113}
114#if SLANG_WIN64
115SLANG_UNIT_TEST(existingDeviceHandleCUDA)
116{
117    return existingDeviceHandleTestAPI(unitTestContext, DeviceType::CUDA);
118}
119#endif
120} // namespace gfx_test