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
9.0 KiB270 linesraw
1#if 0
2// Duplicated: This test is identical to slang-rhi\tests\test-precompiled-module-cache.cpp
3// TODO_TESTING port
4
5#include "core/slang-basic.h"
6#include "core/slang-blob.h"
7#include "core/slang-io.h"
8#include "core/slang-memory-file-system.h"
9#include "gfx-test-util.h"
10#include "slang-rhi.h"
11#include "slang-rhi/shader-cursor.h"
12#include "unit-test/slang-unit-test.h"
13
14using namespace rhi;
15
16namespace gfx_test
17{
18// Test that mixing precompiled and non-precompiled modules is working.
19
20static Slang::Result precompileProgram(
21    rhi::IDevice* device,
22    ISlangMutableFileSystem* fileSys,
23    const char* shaderModuleName,
24    PrecompilationMode precompilationMode)
25{
26    Slang::ComPtr<slang::ISession> slangSession;
27    SLANG_RETURN_ON_FAIL(device->getSlangSession(slangSession.writeRef()));
28    slang::SessionDesc sessionDesc = {};
29    auto searchPaths = getSlangSearchPaths();
30    sessionDesc.searchPathCount = searchPaths.getCount();
31    sessionDesc.searchPaths = searchPaths.getBuffer();
32    auto globalSession = slangSession->getGlobalSession();
33    globalSession->createSession(sessionDesc, slangSession.writeRef());
34
35    slang::IModule* module;
36    {
37        Slang::ComPtr<slang::IBlob> diagnosticsBlob;
38        module = slangSession->loadModule(shaderModuleName, diagnosticsBlob.writeRef());
39        diagnoseIfNeeded(diagnosticsBlob);
40    }
41    if (!module)
42        return SLANG_FAIL;
43
44    if (precompilationMode == PrecompilationMode::InternalLink ||
45        precompilationMode == PrecompilationMode::ExternalLink)
46    {
47        SlangCompileTarget target;
48        switch (device->getInfo().deviceType)
49        {
50        case rhi::DeviceType::D3D12:
51            target = SLANG_DXIL;
52            break;
53        case rhi::DeviceType::Vulkan:
54            target = SLANG_SPIRV;
55            break;
56        default:
57            return SLANG_FAIL;
58        }
59
60        ComPtr<slang::IModulePrecompileService_Experimental> precompileService;
61        if (module->queryInterface(
62                slang::SLANG_UUID_IModulePrecompileService_Experimental,
63                (void**)precompileService.writeRef()) == SLANG_OK)
64        {
65            Slang::ComPtr<slang::IBlob> diagnosticsBlob;
66            auto res = precompileService->precompileForTarget(target, diagnosticsBlob.writeRef());
67            diagnoseIfNeeded(diagnosticsBlob);
68            SLANG_RETURN_ON_FAIL(res);
69
70            // compile a second time to check for driver bugs.
71            diagnosticsBlob = nullptr;
72            res = precompileService->precompileForTarget(target, diagnosticsBlob.writeRef());
73            diagnoseIfNeeded(diagnosticsBlob);
74            SLANG_RETURN_ON_FAIL(res);
75        }
76    }
77
78    // Write loaded modules to file system.
79    for (SlangInt i = 0; i < slangSession->getLoadedModuleCount(); i++)
80    {
81        auto module = slangSession->getLoadedModule(i);
82        auto path = module->getFilePath();
83        if (path)
84        {
85            auto name = module->getName();
86            ComPtr<ISlangBlob> outBlob;
87            module->serialize(outBlob.writeRef());
88            fileSys->saveFileBlob((Slang::String(name) + ".slang-module").getBuffer(), outBlob);
89        }
90    }
91    return SLANG_OK;
92}
93
94void precompiledModule2TestImplCommon(
95    IDevice* device,
96    UnitTestContext* context,
97    PrecompilationMode precompilationMode)
98{
99    // First, load and compile the slang source.
100    ComPtr<ISlangMutableFileSystem> memoryFileSystem =
101        ComPtr<ISlangMutableFileSystem>(new Slang::MemoryFileSystem());
102
103    ComPtr<IShaderProgram> shaderProgram;
104    slang::ProgramLayout* slangReflection;
105    GFX_CHECK_CALL_ABORT(precompileProgram(
106        device,
107        memoryFileSystem.get(),
108        "precompiled-module-imported",
109        precompilationMode));
110
111    // Next, load the precompiled slang program.
112    Slang::ComPtr<slang::ISession> slangSession;
113    device->getSlangSession(slangSession.writeRef());
114    slang::SessionDesc sessionDesc = {};
115    sessionDesc.targetCount = 1;
116    slang::TargetDesc targetDesc = {};
117    switch (device->getInfo().deviceType)
118    {
119    case rhi::DeviceType::D3D12:
120        targetDesc.format = SLANG_DXIL;
121        targetDesc.profile = device->getSlangSession()->getGlobalSession()->findProfile("sm_6_6");
122        break;
123    case rhi::DeviceType::Vulkan:
124        targetDesc.format = SLANG_SPIRV;
125        targetDesc.profile = device->getSlangSession()->getGlobalSession()->findProfile("GLSL_460");
126        break;
127    }
128    sessionDesc.targets = &targetDesc;
129    sessionDesc.fileSystem = memoryFileSystem.get();
130
131    Slang::List<slang::CompilerOptionEntry> options;
132    slang::CompilerOptionEntry skipDownstreamLinkingOption;
133    skipDownstreamLinkingOption.name = slang::CompilerOptionName::SkipDownstreamLinking;
134    skipDownstreamLinkingOption.value.kind = slang::CompilerOptionValueKind::Int;
135    skipDownstreamLinkingOption.value.intValue0 =
136        precompilationMode == PrecompilationMode::ExternalLink;
137    options.add(skipDownstreamLinkingOption);
138
139    sessionDesc.compilerOptionEntries = options.getBuffer();
140    sessionDesc.compilerOptionEntryCount = options.getCount();
141    auto globalSession = slangSession->getGlobalSession();
142    globalSession->createSession(sessionDesc, slangSession.writeRef());
143
144    const char* moduleSrc = R"(
145            import "precompiled-module-imported";
146
147            // Main entry-point. 
148
149            using namespace ns;
150
151            [shader("compute")]
152            [numthreads(4, 1, 1)]
153            void computeMain(
154                uint3 sv_dispatchThreadID : SV_DispatchThreadID,
155                uniform RWStructuredBuffer <float> buffer)
156            {
157                buffer[sv_dispatchThreadID.x] = helperFunc() + helperFunc1();
158            }
159        )";
160    memoryFileSystem->saveFile("precompiled-module.slang", moduleSrc, strlen(moduleSrc));
161    GFX_CHECK_CALL_ABORT(loadComputeProgram(
162        device,
163        slangSession,
164        shaderProgram,
165        "precompiled-module",
166        "computeMain",
167        slangReflection,
168        precompilationMode));
169
170    ComputePipelineDesc pipelineDesc = {};
171    pipelineDesc.program = shaderProgram.get();
172    ComPtr<rhi::IComputePipeline> pipeline = device->createComputePipeline(pipelineDesc);
173    const int numberCount = 4;
174    float initialData[] = {0.0f, 0.0f, 0.0f, 0.0f};
175    BufferDesc bufferDesc = {};
176    bufferDesc.size = numberCount * sizeof(float);
177    bufferDesc.format = rhi::Format::Undefined;
178    bufferDesc.elementSize = sizeof(float);
179    bufferDesc.defaultState = ResourceState::UnorderedAccess;
180    bufferDesc.memoryType = MemoryType::DeviceLocal;
181
182    ComPtr<IBuffer> numbersBuffer;
183    GFX_CHECK_CALL_ABORT(
184        device->createBuffer(bufferDesc, (void*)initialData, numbersBuffer.writeRef()));
185
186    // We have done all the set up work, now it is time to start recording a command buffer for
187    // GPU execution.
188    {
189        auto queue = device->getQueue(QueueType::Graphics);
190
191        auto commandBuffer = queue->createCommandEncoder();
192        auto encoder = commandBuffer->beginComputePass();
193
194        auto rootObject = encoder->bindPipeline(pipeline);
195
196        ShaderCursor entryPointCursor(
197            rootObject->getEntryPoint(0)); // get a cursor the the first entry-point.
198        // Bind buffer view to the entry point.
199        entryPointCursor.getPath("buffer").setBinding(numbersBuffer);
200
201        encoder->dispatchCompute(1, 1, 1);
202        encoder->end();
203        queue->submit(commandBuffer->finish());
204        queue->waitOnHost();
205    }
206
207    compareComputeResult(device, numbersBuffer, std::array{3.0f, 3.0f, 3.0f, 3.0f});
208}
209
210void precompiledModule2TestImpl(IDevice* device, UnitTestContext* context)
211{
212    precompiledModule2TestImplCommon(device, context, PrecompilationMode::SlangIR);
213}
214
215void precompiledTargetModule2InternalLinkTestImpl(IDevice* device, UnitTestContext* context)
216{
217    precompiledModule2TestImplCommon(device, context, PrecompilationMode::InternalLink);
218}
219
220void precompiledTargetModule2ExternalLinkTestImpl(IDevice* device, UnitTestContext* context)
221{
222    precompiledModule2TestImplCommon(device, context, PrecompilationMode::ExternalLink);
223}
224
225SLANG_UNIT_TEST(precompiledModule2D3D12)
226{
227    runTestImpl(precompiledModule2TestImpl, unitTestContext, DeviceType::D3D12);
228}
229
230SLANG_UNIT_TEST(precompiledTargetModuleInternalLink2D3D12)
231{
232    runTestImpl(
233        precompiledTargetModule2InternalLinkTestImpl,
234        unitTestContext,
235        DeviceType::D3D12);
236}
237
238/*
239// Unavailable on D3D12/DXIL currently
240SLANG_UNIT_TEST(precompiledTargetModuleExternalLink2D3D12)
241{
242    runTestImpl(precompiledTargetModule2ExternalLinkTestImpl, unitTestContext,
243DeviceType::D3D12);
244}
245*/
246
247SLANG_UNIT_TEST(precompiledModule2Vulkan)
248{
249    runTestImpl(precompiledModule2TestImpl, unitTestContext, DeviceType::Vulkan);
250}
251
252SLANG_UNIT_TEST(precompiledTargetModule2InternalLinkVulkan)
253{
254    runTestImpl(
255        precompiledTargetModule2InternalLinkTestImpl,
256        unitTestContext,
257        DeviceType::Vulkan);
258}
259
260SLANG_UNIT_TEST(precompiledTargetModule2ExternalLinkVulkan)
261{
262    runTestImpl(
263        precompiledTargetModule2ExternalLinkTestImpl,
264        unitTestContext,
265        DeviceType::Vulkan);
266}
267
268} // namespace gfx_test
269
270#endif