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.3 KiB93 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 computeSmokeTestImpl(IDevice* device, UnitTestContext* context)
12{
13    ComPtr<IShaderProgram> shaderProgram;
14    slang::ProgramLayout* slangReflection;
15    GFX_CHECK_CALL_ABORT(
16        loadComputeProgram(device, shaderProgram, "compute-smoke", "computeMain", slangReflection));
17
18    ComputePipelineDesc pipelineDesc = {};
19    pipelineDesc.program = shaderProgram.get();
20    ComPtr<IComputePipeline> pipelineState;
21    GFX_CHECK_CALL_ABORT(device->createComputePipeline(pipelineDesc, pipelineState.writeRef()));
22
23    const int numberCount = 4;
24    float initialData[] = {0.0f, 1.0f, 2.0f, 3.0f};
25    BufferDesc bufferDesc = {};
26    bufferDesc.size = numberCount * sizeof(float);
27    bufferDesc.format = rhi::Format::Undefined;
28    bufferDesc.elementSize = sizeof(float);
29    bufferDesc.usage = BufferUsage::ShaderResource | BufferUsage::UnorderedAccess |
30                       BufferUsage::CopyDestination | BufferUsage::CopySource;
31    bufferDesc.defaultState = ResourceState::UnorderedAccess;
32    bufferDesc.memoryType = MemoryType::DeviceLocal;
33
34    ComPtr<IBuffer> numbersBuffer;
35    GFX_CHECK_CALL_ABORT(
36        device->createBuffer(bufferDesc, (void*)initialData, numbersBuffer.writeRef()));
37
38    // We have done all the set up work, now it is time to start recording a command buffer for
39    // GPU execution.
40    {
41        auto queue = device->getQueue(QueueType::Graphics);
42        auto commandEncoder = queue->createCommandEncoder();
43        auto encoder = commandEncoder->beginComputePass();
44
45        auto rootObject = encoder->bindPipeline(pipelineState);
46
47        slang::TypeReflection* addTransformerType =
48            slangReflection->findTypeByName("AddTransformer");
49
50        // Now we can use this type to create a shader object that can be bound to the root object.
51        ComPtr<IShaderObject> transformer;
52        GFX_CHECK_CALL_ABORT(device->createShaderObject(
53            addTransformerType,
54            ShaderObjectContainerType::None,
55            transformer.writeRef()));
56        // Set the `c` field of the `AddTransformer`.
57        float c = 1.0f;
58        ShaderCursor(transformer).getPath("c").setData(&c, sizeof(float));
59
60        ShaderCursor entryPointCursor(
61            rootObject->getEntryPoint(0)); // get a cursor the the first entry-point.
62        // Bind buffer to the entry point.
63        entryPointCursor.getPath("buffer").setBinding(Binding(numbersBuffer));
64
65        // Bind the previously created transformer object to root object.
66        entryPointCursor.getPath("transformer").setObject(transformer);
67
68        encoder->dispatchCompute(1, 1, 1);
69        encoder->end();
70        auto commandBuffer = commandEncoder->finish();
71        queue->submit(commandBuffer);
72        queue->waitOnHost();
73    }
74
75    compareComputeResult(device, numbersBuffer, std::array{11.0f, 12.0f, 13.0f, 14.0f});
76}
77
78SLANG_UNIT_TEST(computeSmokeD3D12)
79{
80    runTestImpl(computeSmokeTestImpl, unitTestContext, DeviceType::D3D12);
81}
82
83SLANG_UNIT_TEST(computeSmokeD3D11)
84{
85    runTestImpl(computeSmokeTestImpl, unitTestContext, DeviceType::D3D11);
86}
87
88SLANG_UNIT_TEST(computeSmokeVulkan)
89{
90    runTestImpl(computeSmokeTestImpl, unitTestContext, DeviceType::Vulkan);
91}
92
93} // namespace gfx_test