summaryrefslogtreecommitdiff
path: root/tools/render-test/slang-support.h
blob: d52e94b478865b31ba4f8f915c14f73e0889b9c8 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// slang-support.h
#pragma once

#include "options.h"
#include "shader-input-layout.h"
#include "slang.h"

#include <slang-rhi.h>

namespace renderer_test
{

struct ShaderCompileRequest
{
    struct SourceInfo
    {
        char const* path;

        // The data may either be source text (in which
        // case it can be assumed to be nul-terminated with
        // `dataEnd` pointing at the terminator), or
        // raw binary data (in which case `dataEnd` points
        // at the end of the buffer).
        char const* dataBegin;
        char const* dataEnd;
    };

    struct EntryPoint
    {
        char const* name = nullptr;
        SlangStage slangStage;
    };

    struct TypeConformance
    {
    public:
        Slang::String derivedTypeName;
        Slang::String baseTypeName;
        Int idOverride;
    };

    SourceInfo source;
    Slang::List<EntryPoint> entryPoints;

    Slang::List<Slang::String> globalSpecializationArgs;
    Slang::List<Slang::String> entryPointSpecializationArgs;
    Slang::List<TypeConformance> typeConformances;
};


struct ShaderCompilerUtil
{
    struct Input
    {
        SlangCompileTarget target;
        SlangSourceLanguage sourceLanguage;
        SlangPassThrough passThrough;
        Slang::String profile;
    };

    struct Output
    {
        void set(slang::IComponentType* slangProgram);
        void reset();
        ~Output() { reset(); }

        ComPtr<slang::IComponentType> slangProgram;
        ShaderProgramDesc desc = {};

        /// Compile request that owns the lifetime of compiled kernel code.
        ComPtr<SlangCompileRequest> m_requestForKernels = nullptr;

        /// Compile request that owns the lifetime of reflection information.
        ComPtr<SlangCompileRequest> m_extraRequestForReflection = nullptr;

        SlangCompileRequest* getRequestForKernels() const { return m_requestForKernels; }
        SlangCompileRequest* getRequestForReflection() const
        {
            return m_extraRequestForReflection ? m_extraRequestForReflection : m_requestForKernels;
        }

        SlangSession* session = nullptr;
    };

    struct OutputAndLayout
    {
        Output output;
        ShaderInputLayout layout;
        Slang::String sourcePath;
    };

    static SlangResult compileWithLayout(
        slang::IGlobalSession* globalSession,
        const Options& options,
        const ShaderCompilerUtil::Input& input,
        OutputAndLayout& output);

    static SlangResult readSource(
        const Slang::String& inSourcePath,
        Slang::List<char>& outSourceText);

    static SlangResult _compileProgramImpl(
        slang::IGlobalSession* globalSession,
        const Options& options,
        const Input& input,
        const ShaderCompileRequest& request,
        Output& out);
    static SlangResult compileProgram(
        slang::IGlobalSession* globalSession,
        const Options& options,
        const Input& input,
        const ShaderCompileRequest& request,
        Output& out);
};


} // namespace renderer_test