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

#include "options.h"

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

namespace renderer_test {

Options gOptions;

// Only set it, if the 
void setDefaultRendererID(RendererID id)
{
    gOptions.rendererID = (gOptions.rendererID == RendererID::NONE) ? id : gOptions.rendererID;
}

SlangResult parseOptions(int* argc, char** argv)
{
    int argCount = *argc;
    char const* const* argCursor = argv;
    char const* const* argEnd = argCursor + argCount;

    char const** writeCursor = (char const**) argv;

    // 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] != '-' )
        {
            *writeCursor++ = arg;
            continue;
        }

        if( strcmp(arg, "--") == 0 )
        {
            while(argCursor != argEnd)
            {
                char const* arg = *argCursor++;
                *writeCursor++ = arg;
            }
            break;
        }
        else if( strcmp(arg, "-o") == 0 )
        {
            if( argCursor == argEnd )
            {
                fprintf(stderr, "expected argument for '%s' option\n", arg);
                return SLANG_FAIL;
            }
            gOptions.outputPath = *argCursor++;
        }
        else if( strcmp(arg, "-hlsl") == 0 )
        {
            setDefaultRendererID( RendererID::D3D11);
            gOptions.inputLanguageID = InputLanguageID::Native;
        }
        else if( strcmp(arg, "-glsl") == 0 )
        {
            setDefaultRendererID(RendererID::GL);
            gOptions.inputLanguageID = InputLanguageID::Native;
        }
        else if( strcmp(arg, "-hlsl-rewrite") == 0 )
        {
            setDefaultRendererID(RendererID::D3D11);
            gOptions.inputLanguageID = InputLanguageID::NativeRewrite;
        }
        else if( strcmp(arg, "-glsl-rewrite") == 0 )
        {
            setDefaultRendererID(RendererID::GL);
            gOptions.inputLanguageID = InputLanguageID::NativeRewrite;
        }
        else if( strcmp(arg, "-slang") == 0 )
        {
            setDefaultRendererID(RendererID::D3D11);
            gOptions.inputLanguageID = InputLanguageID::Slang;
        }
        else if( strcmp(arg, "-glsl-cross") == 0 )
        {
            setDefaultRendererID(RendererID::GL);
            gOptions.inputLanguageID = InputLanguageID::Slang;
        }
        else if( strcmp(arg, "-xslang") == 0 )
        {
            // This is an option that we want to pass along to Slang

            if( argCursor == argEnd )
            {
                fprintf(stderr, "expected argument for '%s' option\n", arg);
                return SLANG_FAIL;
            }
            if( gOptions.slangArgCount == kMaxSlangArgs )
            {
                fprintf(stderr, "maximum number of '%s' options exceeded (%d)\n", arg, 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, "-vk") == 0
            || strcmp(arg, "-vulkan") == 0)
        {
            gOptions.rendererID = RendererID::VK;
        }
        else if (strcmp(arg, "-d3d12") == 0
            || strcmp(arg, "-dx12") == 0)
        {
            gOptions.rendererID = RendererID::D3D12;
        }
        else if(strcmp(arg, "-gl") == 0)
        {
            gOptions.rendererID = RendererID::GL;
        }
        else if (strcmp(arg, "-d3d11") == 0 
            || strcmp(arg, "-dx11") == 0)
        {
            gOptions.rendererID = RendererID::D3D11;
        }
        else
        {
            fprintf(stderr, "unknown option '%s'\n", arg);
            return SLANG_FAIL;
        }
    }
    
    // any arguments left over were positional arguments
    argCount = (int)(writeCursor - argv);
    argCursor = argv;
    argEnd = argCursor + argCount;

    // first positional argument is source shader path
    if( argCursor != argEnd )
    {
        gOptions.sourcePath = *argCursor++;
    }

    // any remaining arguments represent an error
    if(argCursor != argEnd)
    {
        fprintf(stderr, "unexpected arguments\n");
        return SLANG_FAIL;
    }

    *argc = 0;
	return SLANG_OK;
}

} // renderer_test