summaryrefslogtreecommitdiffstats
path: root/tools/gfx/slang-context.h
blob: 5494ccfcea7b0ecbd379f0e9596ca6b0bfa001be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once

#include "core/slang-basic.h"
#include "slang-gfx.h"

namespace gfx
{
class SlangContext
{
public:
    Slang::ComPtr<slang::IGlobalSession> globalSession;
    Slang::ComPtr<slang::ISession> session;
    SlangCompileTarget compileTarget;
    Result initialize(
        const gfx::IDevice::SlangDesc& desc,
        uint32_t extendedDescCount,
        void** extendedDescs,
        SlangCompileTarget compileTarget,
        const char* defaultProfileName,
        Slang::ConstArrayView<slang::PreprocessorMacroDesc> additionalMacros)
    {
        if (desc.slangGlobalSession)
        {
            globalSession = desc.slangGlobalSession;
        }
        else
        {
            SLANG_RETURN_ON_FAIL(slang::createGlobalSession(globalSession.writeRef()));
        }

        this->compileTarget = compileTarget;
        slang::SessionDesc slangSessionDesc = {};
        slangSessionDesc.defaultMatrixLayoutMode = desc.defaultMatrixLayoutMode;
        slangSessionDesc.searchPathCount = desc.searchPathCount;
        slangSessionDesc.searchPaths = desc.searchPaths;
        slangSessionDesc.preprocessorMacroCount =
            desc.preprocessorMacroCount + additionalMacros.getCount();
        Slang::List<slang::PreprocessorMacroDesc> macros;
        macros.addRange(desc.preprocessorMacros, desc.preprocessorMacroCount);
        macros.addRange(additionalMacros.getBuffer(), additionalMacros.getCount());
        slangSessionDesc.preprocessorMacros = macros.getBuffer();
        slang::TargetDesc targetDesc = {};
        targetDesc.format = compileTarget;
        auto targetProfile = desc.targetProfile;
        if (targetProfile == nullptr)
            targetProfile = defaultProfileName;
        targetDesc.profile = globalSession->findProfile(targetProfile);
        targetDesc.floatingPointMode = desc.floatingPointMode;
        targetDesc.lineDirectiveMode = desc.lineDirectiveMode;
        targetDesc.flags = desc.targetFlags;
        targetDesc.forceGLSLScalarBufferLayout = true;

        slangSessionDesc.targets = &targetDesc;
        slangSessionDesc.targetCount = 1;

        for (uint32_t i = 0; i < extendedDescCount; i++)
        {
            if ((*(StructType*)extendedDescs[i]) == StructType::SlangSessionExtendedDesc)
            {
                auto extDesc = (SlangSessionExtendedDesc*)extendedDescs[i];
                slangSessionDesc.compilerOptionEntryCount = extDesc->compilerOptionEntryCount;
                slangSessionDesc.compilerOptionEntries = extDesc->compilerOptionEntries;
                break;
            }
        }

        SLANG_RETURN_ON_FAIL(globalSession->createSession(slangSessionDesc, session.writeRef()));
        return SLANG_OK;
    }
};
} // namespace gfx