summaryrefslogtreecommitdiff
path: root/tools/gfx/slang-context.h
blob: 0a878e0f55828d0702f7271aae22b9d4e8647a4a (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
#pragma once

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

namespace gfx
{
    class SlangContext
    {
    public:
        Slang::ComPtr<slang::IGlobalSession> globalSession;
        Slang::ComPtr<slang::ISession> session;
        Result initialize(const gfx::IDevice::SlangDesc& desc, 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()));
            }

            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.optimizationLevel = desc.optimizationLevel;
            targetDesc.floatingPointMode = desc.floatingPointMode;
            targetDesc.lineDirectiveMode = desc.lineDirectiveMode;
            targetDesc.flags = desc.targetFlags;

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

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