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
2.6 KiB81 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 computeTrivialTestImpl(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            // Bind buffer directly to the entry point.
52            ShaderCursor(rootObject).getPath("buffer").setBinding(Binding(numbersBuffer));
53
54            encoder->dispatchCompute(1, 1, 1);
55            encoder->end();
56        }
57
58        auto commandBuffer = commandEncoder->finish();
59        queue->submit(commandBuffer);
60        queue->waitOnHost();
61    }
62
63    compareComputeResult(device, numbersBuffer, std::array{1.0f, 2.0f, 3.0f, 4.0f});
64}
65
66SLANG_UNIT_TEST(computeTrivialD3D12)
67{
68    runTestImpl(computeTrivialTestImpl, unitTestContext, DeviceType::D3D12);
69}
70
71SLANG_UNIT_TEST(computeTrivialD3D11)
72{
73    runTestImpl(computeTrivialTestImpl, unitTestContext, DeviceType::D3D11);
74}
75
76SLANG_UNIT_TEST(computeTrivialVulkan)
77{
78    runTestImpl(computeTrivialTestImpl, unitTestContext, DeviceType::Vulkan);
79}
80
81} // namespace gfx_test