yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

cheneym2Fix precompiledTargetModule tests (#6455)02706dfc5

master
2.7 KiB71 linesraw
1#pragma once
2
3#include "core/slang-basic.h"
4#include "slang-gfx.h"
5
6namespace gfx
7{
8class SlangContext
9{
10public:
11    Slang::ComPtr<slang::IGlobalSession> globalSession;
12    Slang::ComPtr<slang::ISession> session;
13    SlangCompileTarget compileTarget;
14    Result initialize(
15        const gfx::IDevice::SlangDesc& desc,
16        uint32_t extendedDescCount,
17        void** extendedDescs,
18        SlangCompileTarget compileTarget,
19        const char* defaultProfileName,
20        Slang::ConstArrayView<slang::PreprocessorMacroDesc> additionalMacros)
21    {
22        if (desc.slangGlobalSession)
23        {
24            globalSession = desc.slangGlobalSession;
25        }
26        else
27        {
28            SLANG_RETURN_ON_FAIL(slang::createGlobalSession(globalSession.writeRef()));
29        }
30
31        this->compileTarget = compileTarget;
32        slang::SessionDesc slangSessionDesc = {};
33        slangSessionDesc.defaultMatrixLayoutMode = desc.defaultMatrixLayoutMode;
34        slangSessionDesc.searchPathCount = desc.searchPathCount;
35        slangSessionDesc.searchPaths = desc.searchPaths;
36        slangSessionDesc.preprocessorMacroCount =
37            desc.preprocessorMacroCount + additionalMacros.getCount();
38        Slang::List<slang::PreprocessorMacroDesc> macros;
39        macros.addRange(desc.preprocessorMacros, desc.preprocessorMacroCount);
40        macros.addRange(additionalMacros.getBuffer(), additionalMacros.getCount());
41        slangSessionDesc.preprocessorMacros = macros.getBuffer();
42        slang::TargetDesc targetDesc = {};
43        targetDesc.format = compileTarget;
44        auto targetProfile = desc.targetProfile;
45        if (targetProfile == nullptr)
46            targetProfile = defaultProfileName;
47        targetDesc.profile = globalSession->findProfile(targetProfile);
48        targetDesc.floatingPointMode = desc.floatingPointMode;
49        targetDesc.lineDirectiveMode = desc.lineDirectiveMode;
50        targetDesc.flags = desc.targetFlags;
51        targetDesc.forceGLSLScalarBufferLayout = true;
52
53        slangSessionDesc.targets = &targetDesc;
54        slangSessionDesc.targetCount = 1;
55
56        for (uint32_t i = 0; i < extendedDescCount; i++)
57        {
58            if ((*(StructType*)extendedDescs[i]) == StructType::SlangSessionExtendedDesc)
59            {
60                auto extDesc = (SlangSessionExtendedDesc*)extendedDescs[i];
61                slangSessionDesc.compilerOptionEntryCount = extDesc->compilerOptionEntryCount;
62                slangSessionDesc.compilerOptionEntries = extDesc->compilerOptionEntries;
63                break;
64            }
65        }
66
67        SLANG_RETURN_ON_FAIL(globalSession->createSession(slangSessionDesc, session.writeRef()));
68        return SLANG_OK;
69    }
70};
71} // namespace gfx