summaryrefslogtreecommitdiffstats
path: root/tools/render-test/options.cpp
blob: 2277a6ad740d0b6c1ff23e20e9df349883d0cb99 (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
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
// options.cpp

#include "options.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../../source/core/slang-writer.h"
#include "../../source/core/slang-render-api-util.h"

#include "../../source/core/list.h"
#include "../../source/core/slang-string-util.h"

namespace renderer_test {
using namespace Slang;

static const Options gDefaultOptions;

Options gOptions;

static gfx::RendererType _toRenderType(Slang::RenderApiType apiType)
{
    using namespace Slang;
    switch (apiType)
    {
    case RenderApiType::D3D11: return gfx::RendererType::DirectX11;
    case RenderApiType::D3D12: return gfx::RendererType::DirectX12;
    case RenderApiType::OpenGl: return gfx::RendererType::OpenGl;
    case RenderApiType::Vulkan: return gfx::RendererType::Vulkan;
    default: return gfx::RendererType::Unknown;
    }
}

static SlangResult _setRendererType(RendererType type, const char* arg, Slang::WriterHelper stdError)
{
    if (gOptions.rendererType != RendererType::Unknown)
    {
        stdError.print("Already has renderer option set. Found '%s'\n", arg);
        return SLANG_FAIL;
    }
    gOptions.rendererType = type;
    return SLANG_OK;
}

SlangResult parseOptions(int argc, const char*const* argv, Slang::WriterHelper stdError)
{
    using namespace Slang;

    // Reset the options
    gOptions = gDefaultOptions;

    List<const char*> positionalArgs;

    typedef Options::ShaderProgramType ShaderProgramType;
    typedef Options::InputLanguageID InputLanguageID;

    //int argCount = argc;

    char const* const* argCursor = argv;
    char const* const* argEnd = argCursor + argc;

    // first argument is the application name
    if( argCursor != argEnd )
    {
        gOptions.appName = *argCursor++;
    }

    // now iterate over arguments to collect options
    while(argCursor != argEnd)
    {
        char const* arg = *argCursor++;
        if( arg[0] != '-' )
        {
            positionalArgs.Add(arg);
            continue;
        }

        if( strcmp(arg, "--") == 0 )
        {
            while(argCursor != argEnd)
            {
                positionalArgs.Add(*argCursor++);
            }
            break;
        }
        else if( strcmp(arg, "-o") == 0 )
        {
            if( argCursor == argEnd )
            {
                stdError.print("expected argument for '%s' option\n", arg);
                return SLANG_FAIL;
            }
            gOptions.outputPath = *argCursor++;
        }
        else if (strcmp(arg, "-profile") == 0)
        {
            if (argCursor == argEnd)
            {
                stdError.print("expected argument for '%s' option\n", arg);
                return SLANG_FAIL;
            }
            gOptions.profileName = *argCursor++;
        }
        else if (strcmp(arg, "-render-features") == 0 || strcmp(arg, "-render-feature") == 0)
        {
            if (argCursor == argEnd)
            {
                stdError.print("expected argument for '%s' option\n", arg);
                return SLANG_FAIL;
            }
            const char* value = *argCursor++;

            List<UnownedStringSlice> values;
            StringUtil::split(UnownedStringSlice(value), ',', values);

            for (const auto& value : values)
            {
                gOptions.renderFeatures.Add(value);
            }
        }
        else if( strcmp(arg, "-xslang") == 0 )
        {
            // This is an option that we want to pass along to Slang

            if( argCursor == argEnd )
            {
                stdError.print("expected argument for '%s' option\n", arg);
                return SLANG_FAIL;
            }
            if( gOptions.slangArgCount == Options::kMaxSlangArgs )
            {
                stdError.print("maximum number of '%s' options exceeded (%d)\n", arg, Options::kMaxSlangArgs);
                return SLANG_FAIL;
            }
            gOptions.slangArgs[gOptions.slangArgCount++] = *argCursor++;
        }
		else if (strcmp(arg, "-compute") == 0)
		{
			gOptions.shaderType = ShaderProgramType::Compute;
		}
		else if (strcmp(arg, "-graphics") == 0)
		{
			gOptions.shaderType = ShaderProgramType::Graphics;
		}
        else if (strcmp(arg, "-gcompute") == 0)
        {
            gOptions.shaderType = ShaderProgramType::GraphicsCompute;
        }
        else if( strcmp(arg, "-use-dxil") == 0 )
        {
            gOptions.useDXIL = true;
        }
        else if (strcmp(arg, "-only-startup") == 0)
        {
            gOptions.onlyStartup = true;
        }
        else if (strcmp(arg, "-adapter") == 0)
        {
            if (argCursor == argEnd)
            {
                stdError.print("expected argument for '%s' option\n", arg);
                return SLANG_FAIL;
            }

            gOptions.adapter = *argCursor++;
        }
        else
        {
            // Lookup
            Slang::UnownedStringSlice argSlice(arg);
            if (argSlice.size() && argSlice[0] == '-')
            {
                // Look up the rendering API if set
                UnownedStringSlice argName = UnownedStringSlice(argSlice.begin() + 1, argSlice.end());
                RendererType rendererType = _toRenderType(RenderApiUtil::findApiTypeByName(argName));

                if (rendererType != RendererType::Unknown)
                {
                    gOptions.rendererType = rendererType;
                    continue;
                }

                // Lookup the target language type
                RendererType languageRenderType = _toRenderType(RenderApiUtil::findImplicitLanguageRenderApiType(argName));
                if (languageRenderType != RendererType::Unknown)
                {
                    gOptions.targetLanguageRendererType = languageRenderType;
                    gOptions.inputLanguageID = (argName == "hlsl" || argName == "glsl") ?  InputLanguageID::Native : InputLanguageID::Slang;
                    continue;
                }
            }

            stdError.print("unknown option '%s'\n", arg);
            return SLANG_FAIL;
        }
    }

    // If a render option isn't set use defaultRenderType 
    gOptions.rendererType = (gOptions.rendererType == RendererType::Unknown) ? gOptions.targetLanguageRendererType : gOptions.rendererType;

    // first positional argument is source shader path
    if(positionalArgs.Count())
    {
        gOptions.sourcePath = positionalArgs[0];
        positionalArgs.RemoveAt(0);
    }

    // any remaining arguments represent an error
    if(positionalArgs.Count() != 0)
    {
        stdError.print("unexpected arguments\n");
        return SLANG_FAIL;
    }

	return SLANG_OK;
}

} // renderer_test