summaryrefslogtreecommitdiff
path: root/source/slang/compiler.h
blob: f35d6603c11555603970fb96efbc9bf822b5093c (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef RASTER_SHADER_COMPILER_H
#define RASTER_SHADER_COMPILER_H

#include "../core/basic.h"

#include "compiled-program.h"
#include "diagnostics.h"
#include "profile.h"
#include "syntax.h"
#include "type-layout.h"

#include "../../slang.h"

namespace Slang
{
    namespace Compiler
    {
        class ILConstOperand;
        struct IncludeHandler;

        enum class CompilerMode
        {
            ProduceLibrary,
            ProduceShader,
            GenerateChoice
        };

        enum class StageTarget
        {
            Unknown,
            VertexShader,
            HullShader,
            DomainShader,
            GeometryShader,
            FragmentShader,
            ComputeShader,
        };

        enum class CodeGenTarget
        {
            Unknown             = SLANG_TARGET_UNKNOWN,
            GLSL                = SLANG_GLSL,
            GLSL_Vulkan         = SLANG_GLSL_VULKAN,
            GLSL_Vulkan_OneDesc = SLANG_GLSL_VULKAN_ONE_DESC,
            HLSL                = SLANG_HLSL,
            SPIRV               = SLANG_SPIRV,
            SPIRVAssembly       = SLANG_SPIRV_ASM,
            DXBytecode          = SLANG_DXBC,
            DXBytecodeAssembly  = SLANG_DXBC_ASM,
            ReflectionJSON      = SLANG_REFLECTION_JSON,
        };

        // Describes an entry point that we've been requested to compile
        struct EntryPointOption
        {
            String name;
            Profile profile;
        };

        enum class PassThroughMode : SlangPassThrough
        {
            None = SLANG_PASS_THROUGH_NONE,	// don't pass through: use Slang compiler
            HLSL = SLANG_PASS_THROUGH_FXC,	// pass through HLSL to `D3DCompile` API
//			GLSL,	// pass through GLSL to `glslang` library
        };

        // Represents a single source file (either an on-disk file, or a
        // "virtual" file passed in as a string)
        class SourceFile : public RefObject
        {
        public:
            // The file path for a real file, or the nominal path for a virtual file
            String path;

            // The actual contents of the file
            String content;
        };

        // Options for a single translation unit being requested by the user
        class TranslationUnitOptions
        {
        public:
            SourceLanguage sourceLanguage = SourceLanguage::Unknown;

            // All entry points we've been asked to compile for this translation unit
            List<EntryPointOption> entryPoints;

            // The source file(s) that will be compiled to form this translation unit
            List<RefPtr<SourceFile> > sourceFiles;
        };

        class CompileOptions
        {
        public:
            CompilerMode Mode = CompilerMode::ProduceShader;
            CodeGenTarget Target = CodeGenTarget::Unknown;
            StageTarget stage = StageTarget::Unknown;
            EnumerableDictionary<String, String> BackendArguments;

            String SymbolToCompile;
            String outputName;
            List<String> TemplateShaderArguments;
            List<String> SearchDirectories;
            Dictionary<String, String> PreprocessorDefinitions;

            List<TranslationUnitOptions> translationUnits;

            // the code generation profile we've been asked to use
            Profile profile;

            // should we just pass the input to another compiler?
            PassThroughMode passThrough = PassThroughMode::None;

            // Flags supplied through the API
            SlangCompileFlags flags = 0;
        };

        // This is the representation of a given translation unit
        class CompileUnit
        {
        public:
            TranslationUnitOptions      options;
            RefPtr<ProgramSyntaxNode>   SyntaxNode;
        };

        // TODO: pick an appropriate name for this...
        class CollectionOfTranslationUnits : public RefObject
        {
        public:
            List<CompileUnit> translationUnits;

            // TODO: this is more output-oriented, but maybe okay to have here...
            RefPtr<ProgramLayout> layout;
        };

        class ShaderCompiler : public CoreLib::Basic::Object
        {
        public:
            virtual void Compile(
                CompileResult&                  result,
                CollectionOfTranslationUnits*   collectionOfTranslationUnits,
                const CompileOptions&           options) = 0;

            virtual TranslationUnitResult PassThrough(
                String const&			sourceText,
                String const&			sourcePath,
                const CompileOptions &	options,
                TranslationUnitOptions const& translationUnitOptions) = 0;

        };

        ShaderCompiler * CreateShaderCompiler();
    }
}

#endif