aboutsummaryrefslogtreecommitdiff
path: root/build/tools/ShaderBuildTool/ShaderBuildTool.cpp
blob: f30f9f9f8a04ffa48fde33d0220567c13d82f4ca (plain) (blame)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// ShaderBuildTool.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <string>
#include <sstream>

#define PERMUTATION_KEYWORD "#permutation "
#define SEPARATORS " \n"
#define INDENT "    "

namespace
{
    struct CShaderPermutation
    {
        std::string Key;
        std::vector<std::string> Values;
    };

    struct CShaderDefine
    {
        std::string Key;
        std::string Value;
    };

    struct CShaderInstance
    {
        std::string GlobalVariableName;
        std::vector<CShaderDefine> Defines;
    };

    typedef std::vector<CShaderInstance> CShaderInstances;
    typedef std::vector<CShaderPermutation> CShaderPermutations;
};

FILE *OpenFile(std::string Path, const char* Mode)
{
    FILE *fp = NULL;
    if (fopen_s(&fp, Path.c_str(), Mode) || !fp)
    {
        fprintf(stderr, "Failed to open file %s\n", Path.c_str());
        exit(1);
    }
    return fp;
}

CShaderInstances ExpandShaderInstances(CShaderInstances In, CShaderPermutation Permutation)
{
    CShaderInstances Out;
    for (unsigned int InstanceIndex = 0; InstanceIndex < In.size(); ++InstanceIndex)
    {
        for (unsigned int ValueIndex = 0; ValueIndex < Permutation.Values.size(); ++ValueIndex)
        {
            CShaderDefine D;
            D.Key = Permutation.Key;
            D.Value = Permutation.Values[ValueIndex];

            CShaderInstance Tmp = In[InstanceIndex];
            Tmp.Defines.push_back(D);
            Out.push_back(Tmp);
        }
    }

    return Out;
}

void MySystem(const char *Command)
{
    //printf("%s\n", Command);

    if (system(Command) != 0)
    {
        fprintf(stderr, "Failed command: %s\n", Command);
        exit(1);
    }
}

struct CArgs
{
    std::string Profile;
    std::string Input;
    std::string EntryPoint;
    std::string OutputDir;
    std::string Output_H;
    std::string Output_CPP;
    std::string CompileTool;
    std::string API;
    std::string ClassName;
};

void ParseInput(
    const CArgs& Args,
    CShaderPermutations& ShaderPermutations)
{
    FILE *fp = OpenFile(Args.Input, "r");

    char row[1024];
    while (fgets(row, sizeof(row), fp))
    {
        if (strncmp(row, PERMUTATION_KEYWORD, strlen(PERMUTATION_KEYWORD)) == 0)
        {
            char *Keyword = strtok(row, SEPARATORS);
            char *DefineKey = strtok(NULL, SEPARATORS);
            if (!DefineKey) break;

            CShaderPermutation P;
            P.Key = DefineKey;

            while (1)
            {
                char *DefineValue = strtok(NULL, SEPARATORS);
                if (!DefineValue) break;

                P.Values.push_back(DefineValue);
            }

            ShaderPermutations.push_back(P);
        }
    }

    fclose(fp);
}

void InitShaderInstances(
    const CShaderPermutations& ShaderPermutations,
    CShaderInstances& ShaderInstances
    )
{
    ShaderInstances.clear();
    ShaderInstances.push_back(CShaderInstance());

    for (unsigned int PermutationIndex = 0; PermutationIndex < ShaderPermutations.size(); ++PermutationIndex)
    {
        CShaderPermutation P = ShaderPermutations[PermutationIndex];
        ShaderInstances = ExpandShaderInstances(ShaderInstances, P);
    }
}

void WriteCPP(
    const CArgs& Args,
    const CShaderPermutations& ShaderPermutations,
    CShaderInstances& ShaderInstances)
{
    remove(Args.Output_CPP.c_str());

    for (unsigned int InstanceIndex = 0; InstanceIndex < ShaderInstances.size(); ++InstanceIndex)
    {
        CShaderInstance& Instance = ShaderInstances[InstanceIndex];
        std::string DefineString = "/DAPI_" + Args.API + "=1";
        std::string ShaderName = Args.EntryPoint;

        for (unsigned int i = 0; i < ShaderInstances[i].Defines.size(); ++i)
        {
            DefineString += " /D" + Instance.Defines[i].Key + "=" + Instance.Defines[i].Value;
            ShaderName += "_" + Instance.Defines[i].Key + "_" + Instance.Defines[i].Value;
        }

        Instance.GlobalVariableName = "g_" + ShaderName + "_" + Args.API;

        std::string OutputTmp = Args.Output_CPP + ".tmp";
        std::string Command;
        Command += "SET SBT_PROFILE=" + Args.Profile;
        Command += "& SET SBT_INPUT=" + Args.Input;
        Command += "& SET SBT_ENTRY_POINT=" + Args.EntryPoint;
        Command += "& SET SBT_DEFINES=" + DefineString;
        Command += "& SET SBT_VARIABLE_NAME=" + Instance.GlobalVariableName;
        Command += "& SET SBT_OUTPUT=" + OutputTmp;
        Command += "& " + Args.CompileTool;
        MySystem(Command.c_str());

        Command = "type " + OutputTmp + " >> " + Args.Output_CPP;
        MySystem(Command.c_str());

        Command = "del " + OutputTmp;
        MySystem(Command.c_str());
    }

    FILE *fp = OpenFile(Args.Output_CPP, "a+");

    fprintf(fp, "\n");
    fprintf(fp, "namespace Generated\n");
    fprintf(fp, "{\n");

    fprintf(fp, INDENT "void %s::Create(DevicePointer Device)\n", Args.EntryPoint.c_str());
    fprintf(fp, INDENT "{\n");
    for (unsigned int i = 0; i < ShaderInstances.size(); ++i)
    {
        CShaderInstance Instance = ShaderInstances[i];

        fprintf(fp, INDENT INDENT "m_Shader");
        for (unsigned int j = 0; j < Instance.Defines.size(); ++j)
        {
            fprintf(fp, "[ShaderPermutations::%s", Instance.Defines[j].Key.c_str());
            fprintf(fp, "_%s]", Instance.Defines[j].Value.c_str());
        }
        fprintf(fp, ".Create(Device, %s, sizeof(%s));\n", Instance.GlobalVariableName.c_str(), Instance.GlobalVariableName.c_str());
    }
    fprintf(fp, INDENT "}\n");
    fprintf(fp, "\n");

    fprintf(fp, INDENT "void %s::Release(DevicePointer Device)\n", Args.EntryPoint.c_str());
    fprintf(fp, INDENT "{\n");
    for (unsigned int i = 0; i < ShaderInstances.size(); ++i)
    {
        fprintf(fp, INDENT INDENT "m_Shader");
        CShaderInstance Instance = ShaderInstances[i];
        for (unsigned int j = 0; j < Instance.Defines.size(); ++j)
        {
            fprintf(fp, "[ShaderPermutations::%s", Instance.Defines[j].Key.c_str());
            fprintf(fp, "_%s]", Instance.Defines[j].Value.c_str());
        }
        fprintf(fp, ".Release(Device);\n");
    }
    fprintf(fp, INDENT "}\n");

    fprintf(fp, "}\n");

    fclose(fp);
}

void WriteHeader(
    const CArgs& Args,
    const CShaderPermutations& ShaderPermutations,
    const CShaderInstances& ShaderInstances)
{
    remove(Args.Output_H.c_str());

    std::string ClassName = Args.ClassName;

    FILE *fp = OpenFile(Args.Output_H, "a+");

    fprintf(fp, "//! This file was auto-generated. Do not modify manually.\n");
    fprintf(fp, "#pragma once");
    fprintf(fp, "\n");

    fprintf(fp, "\n");
    fprintf(fp, "namespace Generated\n");
    fprintf(fp, "{\n");
    fprintf(fp, "\n");

    fprintf(fp, "namespace ShaderPermutations\n");
    fprintf(fp, "{\n");
    fprintf(fp, "\n");

    for (unsigned int i = 0; i < ShaderPermutations.size(); ++i)
    {
        const CShaderPermutation& Permutation = ShaderPermutations[i];

        std::string PermutationDefine = Permutation.Key + "_DEFINED";
        fprintf(fp, "#ifndef %s\n", PermutationDefine.c_str());
        fprintf(fp, "#define %s\n", PermutationDefine.c_str());

        fprintf(fp, INDENT "enum %s\n", Permutation.Key.c_str());
        fprintf(fp, INDENT "{\n");
        for (unsigned int j = 0; j < Permutation.Values.size(); ++j)
        {
            fprintf(fp, INDENT INDENT "%s_%s,\n", Permutation.Key.c_str(), Permutation.Values[j].c_str());
        }
        fprintf(fp, INDENT INDENT "%s_COUNT,\n", Permutation.Key.c_str());
        fprintf(fp, INDENT "};\n");

        fprintf(fp, "#endif\n");
        fprintf(fp, "\n");
    }

    fprintf(fp, "};\n");
    fprintf(fp, "\n");

    fprintf(fp, "struct %s\n", Args.EntryPoint.c_str());
    fprintf(fp, "{\n");

    fprintf(fp, INDENT "void Create(DevicePointer Device);\n");
    fprintf(fp, INDENT "void Release(DevicePointer Device);\n");

    fprintf(fp, INDENT "%s& Get(", ClassName.c_str());
    for (unsigned int i = 0; i < ShaderPermutations.size(); ++i)
    {
        if (i != 0)
        {
            fprintf(fp, ", ");
        }
        fprintf(fp, "ShaderPermutations::%s %c", ShaderPermutations[i].Key.c_str(), 'A' + i);
    }
    fprintf(fp, ")\n");
    fprintf(fp, INDENT "{\n");
    fprintf(fp, INDENT INDENT "return m_Shader");
    for (unsigned int i = 0; i < ShaderPermutations.size(); ++i)
    {
        fprintf(fp, "[%c]", 'A' + i);
    }
    fprintf(fp, ";\n");
    fprintf(fp, INDENT "}\n");

    fprintf(fp, "\n");
    fprintf(fp, "private:\n");

    fprintf(fp, INDENT "%s m_Shader", ClassName.c_str());
    for (unsigned int i = 0; i < ShaderPermutations.size(); ++i)
    {
        fprintf(fp, "[ShaderPermutations::%s_COUNT]", ShaderPermutations[i].Key.c_str());
    }
    fprintf(fp, ";\n");

    fprintf(fp, "#if _WIN32\n");
    for (unsigned int i = 0; i < ShaderPermutations.size(); ++i)
    {
        fprintf(fp, INDENT "static_assert(ShaderPermutations::%s_COUNT == %d, \"\");\n",
            ShaderPermutations[i].Key.c_str(),
            (int) ShaderPermutations[i].Values.size());
    }
    fprintf(fp, "#endif\n");

    fprintf(fp, "};\n");
    fprintf(fp, "\n");

    fprintf(fp, "};\n");

    fclose(fp);
}

int main(int argc, char **argv)
{
    if (argc < 7)
    {
        fprintf(stderr, "Usage: %s PROFILE INPUT_HLSL ENTRY_POINT OUTPUT_DIR COMPILE_TOOL API [CLASS_NAME]\n", argv[0]);
        exit(1);
    }

    CArgs Args;
    int ArgIndex = 1;
    if (ArgIndex < argc)
    {
        Args.Profile = argv[ArgIndex++];
    }
    if (ArgIndex < argc)
    {
        Args.Input = argv[ArgIndex++];
    }
    if (ArgIndex < argc)
    {
        Args.EntryPoint = argv[ArgIndex++];
    }
    if (ArgIndex < argc)
    {
        Args.OutputDir = argv[ArgIndex++];
    }
    if (ArgIndex < argc)
    {
        Args.CompileTool = argv[ArgIndex++];
    }
    if (ArgIndex < argc)
    {
        Args.API = argv[ArgIndex++];
    }
    if (ArgIndex < argc)
    {
        Args.ClassName = argv[ArgIndex++];
    }

    if (Args.ClassName.size() == 0)
    {
        const bool IsVS = !strcmp(Args.Profile.c_str(), "vs_5_0");
        const bool IsGS = !strcmp(Args.Profile.c_str(), "gs_5_0");
        Args.ClassName =
            IsVS ? "VertexShader" :
            IsGS ? "GeometryShader" :
            "PixelShader";
    }

    CShaderPermutations ShaderPermutations;
    ParseInput(Args, ShaderPermutations);

    Args.Output_H = Args.OutputDir + "\\" + Args.EntryPoint + ".h";
    Args.Output_CPP = Args.OutputDir + "\\" + Args.EntryPoint + ".cpp";

    CShaderInstances ShaderInstances;
    InitShaderInstances(ShaderPermutations, ShaderInstances);

    WriteHeader(Args, ShaderPermutations, ShaderInstances);

    WriteCPP(Args, ShaderPermutations, ShaderInstances);

    return 0;
}