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.9 KiB129 linesraw
1#include "core/slang-basic.h"
2#include "core/slang-blob.h"
3#include "gfx-test-util.h"
4#include "slang-rhi.h"
5#include "slang-rhi/shader-cursor.h"
6#include "unit-test/slang-unit-test.h"
7
8using namespace rhi;
9
10namespace gfx_test
11{
12// In this test,
13// we will run a compute shader that compiles to HLSL with a reference to the macro
14// "DOWNSTREAM_VALUE" that will be provided to dxc through slang's link-time compiler options. The
15// test verifies that `IComponentType2::linkWithOptions()` is able to produce a linked
16// IComponentType with additional compiler options. Here we will specify a DownstreamArg compiler
17// option to define the value of DOWNSTREAM_VALUE when running dxc.
18//
19static Slang::Result loadProgram(
20    rhi::IDevice* device,
21    Slang::ComPtr<rhi::IShaderProgram>& outShaderProgram,
22    const char* shaderModuleName,
23    const char* entryPointName,
24    slang::ProgramLayout*& slangReflection)
25{
26    Slang::ComPtr<slang::ISession> slangSession;
27    SLANG_RETURN_ON_FAIL(device->getSlangSession(slangSession.writeRef()));
28    Slang::ComPtr<slang::IBlob> diagnosticsBlob;
29    slang::IModule* module = slangSession->loadModule(shaderModuleName, diagnosticsBlob.writeRef());
30    diagnoseIfNeeded(diagnosticsBlob);
31    if (!module)
32        return SLANG_FAIL;
33
34    ComPtr<slang::IEntryPoint> computeEntryPoint;
35    SLANG_RETURN_ON_FAIL(
36        module->findEntryPointByName(entryPointName, computeEntryPoint.writeRef()));
37
38    Slang::List<slang::IComponentType*> componentTypes;
39    componentTypes.add(module);
40    componentTypes.add(computeEntryPoint);
41
42    Slang::ComPtr<slang::IComponentType> composedProgram;
43    SlangResult result = slangSession->createCompositeComponentType(
44        componentTypes.getBuffer(),
45        componentTypes.getCount(),
46        composedProgram.writeRef(),
47        diagnosticsBlob.writeRef());
48    diagnoseIfNeeded(diagnosticsBlob);
49    SLANG_RETURN_ON_FAIL(result);
50
51    ComPtr<slang::IComponentType> linkedProgram;
52    slang::CompilerOptionEntry entry;
53    entry.name = slang::CompilerOptionName::DownstreamArgs;
54    entry.value.kind = slang::CompilerOptionValueKind::String;
55    entry.value.stringValue0 = "dxc";
56    entry.value.stringValue1 = "-DDOWNSTREAM_VALUE=4.0";
57    result = composedProgram
58                 ->linkWithOptions(linkedProgram.writeRef(), 1, &entry, diagnosticsBlob.writeRef());
59    diagnoseIfNeeded(diagnosticsBlob);
60    SLANG_RETURN_ON_FAIL(result);
61
62    composedProgram = linkedProgram;
63    slangReflection = composedProgram->getLayout();
64
65    ShaderProgramDesc programDesc = {};
66    programDesc.slangGlobalScope = composedProgram.get();
67
68    auto shaderProgram = device->createShaderProgram(programDesc);
69
70    outShaderProgram = shaderProgram;
71    return SLANG_OK;
72}
73
74void linkTimeOptionTestImpl(IDevice* device, UnitTestContext* context)
75{
76    ComPtr<IShaderProgram> shaderProgram;
77    slang::ProgramLayout* slangReflection;
78    GFX_CHECK_CALL_ABORT(
79        loadProgram(device, shaderProgram, "link-time-options", "computeMain", slangReflection));
80
81    ComputePipelineDesc pipelineDesc = {};
82    pipelineDesc.program = shaderProgram.get();
83    ComPtr<IComputePipeline> pipelineState;
84    GFX_CHECK_CALL_ABORT(device->createComputePipeline(pipelineDesc, pipelineState.writeRef()));
85
86    const int numberCount = 4;
87    float initialData[] = {0.0f, 0.0f, 0.0f, 0.0f};
88    BufferDesc bufferDesc = {};
89    bufferDesc.size = numberCount * sizeof(float);
90    bufferDesc.format = rhi::Format::Undefined;
91    bufferDesc.elementSize = sizeof(float);
92    bufferDesc.usage = BufferUsage::ShaderResource | BufferUsage::UnorderedAccess |
93                       BufferUsage::CopyDestination | BufferUsage::CopySource;
94    bufferDesc.defaultState = ResourceState::UnorderedAccess;
95    bufferDesc.memoryType = MemoryType::DeviceLocal;
96
97    ComPtr<IBuffer> numbersBuffer;
98    GFX_CHECK_CALL_ABORT(
99        device->createBuffer(bufferDesc, (void*)initialData, numbersBuffer.writeRef()));
100
101    // We have done all the set up work, now it is time to start recording a command buffer for
102    // GPU execution.
103    {
104        auto queue = device->getQueue(QueueType::Graphics);
105        auto commandEncoder = queue->createCommandEncoder();
106        auto computePassEncoder = commandEncoder->beginComputePass();
107
108        auto rootObject = computePassEncoder->bindPipeline(pipelineState);
109
110        ShaderCursor entryPointCursor(
111            rootObject->getEntryPoint(0)); // get a cursor the the first entry-point.
112        // Bind buffer to the entry point.
113        entryPointCursor.getPath("buffer").setBinding(Binding(numbersBuffer));
114
115        computePassEncoder->dispatchCompute(1, 1, 1);
116        computePassEncoder->end();
117        auto commandBuffer = commandEncoder->finish();
118        queue->submit(commandBuffer);
119        queue->waitOnHost();
120    }
121
122    compareComputeResult(device, numbersBuffer, std::array{4.0f});
123}
124
125SLANG_UNIT_TEST(linkTimeOptionD3D12)
126{
127    runTestImpl(linkTimeOptionTestImpl, unitTestContext, DeviceType::D3D12);
128}
129} // namespace gfx_test