forked from shader-slang/slang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.cpp
322 lines (290 loc) · 10.2 KB
/
options.cpp
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
// options.cpp
#include "options.h"
#include "../../source/core/slang-list.h"
#include "../../source/core/slang-render-api-util.h"
#include "../../source/core/slang-string-util.h"
#include "../../source/core/slang-writer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include "../../source/core/slang-downstream-compiler.h"
#include "../../source/compiler-core/slang-command-line-args.h"
#include "../../source/core/slang-type-text-util.h"
#include "diagnostics.h"
namespace renderer_test
{
using namespace Slang;
static rhi::DeviceType _toRenderType(Slang::RenderApiType apiType)
{
using namespace Slang;
switch (apiType)
{
case RenderApiType::D3D11:
return rhi::DeviceType::D3D11;
case RenderApiType::D3D12:
return rhi::DeviceType::D3D12;
case RenderApiType::Vulkan:
return rhi::DeviceType::Vulkan;
case RenderApiType::Metal:
return rhi::DeviceType::Metal;
case RenderApiType::CPU:
return rhi::DeviceType::CPU;
case RenderApiType::CUDA:
return rhi::DeviceType::CUDA;
case RenderApiType::WebGPU:
return rhi::DeviceType::WGPU;
default:
return rhi::DeviceType::Default;
}
}
/* static */ SlangResult Options::parse(
int argc,
const char* const* argv,
Slang::WriterHelper stdError,
Options& outOptions)
{
using namespace Slang;
RefPtr<CommandLineContext> cmdLineContext(new CommandLineContext);
DiagnosticSink sink(cmdLineContext->getSourceManager(), nullptr);
sink.writer = stdError.getWriter();
sink.setFlag(DiagnosticSink::Flag::SourceLocationLine);
outOptions = Options();
CommandLineArgs args(cmdLineContext);
if (argc > 0)
{
// first argument is the application name
outOptions.appName = argv[0];
args.setArgs(argv + 1, argc - 1);
}
else
{
args.setArgs(argv, argc);
}
SLANG_RETURN_ON_FAIL(outOptions.downstreamArgs.stripDownstreamArgs(args, 0, &sink));
CommandLineReader reader(&args, &sink);
List<CommandLineArg> positionalArgs;
typedef Options::ShaderProgramType ShaderProgramType;
typedef Options::InputLanguageID InputLanguageID;
// now iterate over arguments to collect options
while (reader.hasArg())
{
CommandLineArg arg = reader.getArgAndAdvance();
const auto& argValue = arg.value;
if (!argValue.startsWith("-"))
{
positionalArgs.add(arg);
continue;
}
if (argValue == "--")
{
while (reader.hasArg())
{
positionalArgs.add(reader.getArgAndAdvance());
}
break;
}
else if (argValue == "-o")
{
SLANG_RETURN_ON_FAIL(reader.expectArg(outOptions.outputPath));
}
else if (argValue == "-profile")
{
SLANG_RETURN_ON_FAIL(reader.expectArg(outOptions.profileName));
}
else if (argValue == "-render-features" || argValue == "-render-feature")
{
String features;
SLANG_RETURN_ON_FAIL(reader.expectArg(features));
List<UnownedStringSlice> values;
StringUtil::split(features.getUnownedSlice(), ',', values);
for (const auto& value : values)
{
outOptions.renderFeatures.add(value);
}
}
else if (argValue == "-xslang" || argValue == "-compile-arg")
{
// This is legacy support, should use -Xslang now
// This is an option that we want to pass along to Slang
CommandLineArg slangArg;
SLANG_RETURN_ON_FAIL(reader.expectArg(slangArg));
outOptions.downstreamArgs.getArgsByName("slang").add(slangArg);
}
else if (argValue == "-compute")
{
outOptions.shaderType = ShaderProgramType::Compute;
}
else if (argValue == "-graphics")
{
outOptions.shaderType = ShaderProgramType::Graphics;
}
else if (argValue == "-gcompute")
{
outOptions.shaderType = ShaderProgramType::GraphicsCompute;
}
else if (argValue == "-rt")
{
outOptions.shaderType = ShaderProgramType::RayTracing;
}
else if (argValue == "-mesh")
{
outOptions.shaderType = ShaderProgramType::GraphicsMeshCompute;
}
else if (argValue == "-task")
{
outOptions.shaderType = ShaderProgramType::GraphicsTaskMeshCompute;
}
else if (argValue == "-use-dxil")
{
outOptions.useDXIL = true;
}
else if (argValue == "-emit-spirv-directly")
{
outOptions.generateSPIRVDirectly = true;
}
else if (argValue == "-emit-spirv-via-glsl")
{
outOptions.generateSPIRVDirectly = false;
}
else if (argValue == "-only-startup")
{
outOptions.onlyStartup = true;
}
else if (argValue == "-performance-profile")
{
outOptions.performanceProfile = true;
}
else if (argValue == "-output-using-type")
{
outOptions.outputUsingType = true;
}
else if (argValue == "-compute-dispatch")
{
CommandLineArg dispatchSize;
SLANG_RETURN_ON_FAIL(reader.expectArg(dispatchSize));
List<UnownedStringSlice> slices;
StringUtil::split(dispatchSize.value.getUnownedSlice(), ',', slices);
if (slices.getCount() != 3)
{
sink.diagnose(
dispatchSize.loc,
RenderTestDiagnostics::expectingCommaComputeDispatch);
return SLANG_FAIL;
}
String string;
for (Index i = 0; i < 3; ++i)
{
string = slices[i];
int v = stringToInt(string);
if (v < 1)
{
sink.diagnose(
dispatchSize.loc,
RenderTestDiagnostics::expectingPositiveComputeDispatch);
return SLANG_FAIL;
}
outOptions.computeDispatchSize[i] = v;
}
}
else if (argValue == "-source-language")
{
CommandLineArg sourceLanguageName;
SLANG_RETURN_ON_FAIL(reader.expectArg(sourceLanguageName));
const SlangSourceLanguage sourceLanguage =
TypeTextUtil::findSourceLanguage(sourceLanguageName.value.getUnownedSlice());
if (sourceLanguage == SLANG_SOURCE_LANGUAGE_UNKNOWN)
{
sink.diagnose(sourceLanguageName.loc, RenderTestDiagnostics::unknownSourceLanguage);
return SLANG_FAIL;
}
outOptions.sourceLanguage = sourceLanguage;
}
else if (argValue == "-no-default-entry-point")
{
outOptions.dontAddDefaultEntryPoints = true;
}
else if (argValue == "-nvapi-slot")
{
SLANG_RETURN_ON_FAIL(reader.expectArg(outOptions.nvapiExtnSlot));
}
else if (argValue == "-shaderobj")
{
// Note: We ignore this option because it is always enabled now.
//
// TODO: At some point we could warn/error and deprecate this option.
}
else if (argValue == "-g0")
{
outOptions.disableDebugInfo = true;
}
else if (argValue == "-allow-glsl")
{
outOptions.allowGLSL = true;
}
else if (argValue == "-entry")
{
SLANG_RETURN_ON_FAIL(reader.expectArg(outOptions.entryPointName));
}
else if (argValue == "-enable-debug-layers")
{
outOptions.enableDebugLayers = true;
}
else if (argValue == "-dx12-experimental")
{
outOptions.dx12Experimental = true;
}
else if (argValue == "-show-adapter-info")
{
outOptions.showAdapterInfo = true;
}
else
{
// Lookup
Slang::UnownedStringSlice argSlice = arg.value.getUnownedSlice();
if (argSlice.getLength() && argSlice[0] == '-')
{
// Look up the rendering API if set
UnownedStringSlice argName = argSlice.tail(1);
DeviceType deviceType = _toRenderType(RenderApiUtil::findApiTypeByName(argName));
if (deviceType != DeviceType::Default)
{
outOptions.deviceType = deviceType;
continue;
}
// Lookup the target language type
DeviceType targetLanguageDeviceType =
_toRenderType(RenderApiUtil::findImplicitLanguageRenderApiType(argName));
if (targetLanguageDeviceType != DeviceType::Default || argName == "glsl")
{
outOptions.targetLanguageDeviceType = targetLanguageDeviceType;
outOptions.inputLanguageID =
(argName == "hlsl" || argName == "glsl" || argName == "cpp" ||
argName == "cxx" || argName == "c")
? InputLanguageID::Native
: InputLanguageID::Slang;
continue;
}
}
sink.diagnose(arg.loc, RenderTestDiagnostics::unknownCommandLineOption, arg.value);
return SLANG_FAIL;
}
}
// If a render option isn't set use defaultRenderType
outOptions.deviceType = (outOptions.deviceType == DeviceType::Default)
? outOptions.targetLanguageDeviceType
: outOptions.deviceType;
// first positional argument is source shader path
if (positionalArgs.getCount())
{
outOptions.sourcePath = positionalArgs[0].value;
positionalArgs.removeAt(0);
}
// any remaining arguments represent an error
if (positionalArgs.getCount() != 0)
{
sink.diagnose(positionalArgs[0].loc, RenderTestDiagnostics::unexpectedPositionalArg);
return SLANG_FAIL;
}
return SLANG_OK;
}
} // namespace renderer_test