blob: ae0465c8c22436f1c3c734d142f7df6f611cf44a [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
2//Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
John Kessenichbd0747d2013-02-17 06:01:50 +00003//Copyright (C) 2013 LunarG, Inc.
4//
John Kessenicha0af4732012-12-12 21:15:54 +00005//All rights reserved.
6//
7//Redistribution and use in source and binary forms, with or without
8//modification, are permitted provided that the following conditions
9//are met:
10//
11// Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following
16// disclaimer in the documentation and/or other materials provided
17// with the distribution.
18//
19// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20// contributors may be used to endorse or promote products derived
21// from this software without specific prior written permission.
22//
23//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34//POSSIBILITY OF SUCH DAMAGE.
35//
John Kessenich05a70632013-09-17 19:26:08 +000036
37// this only applies to the standalone wrapper, not the front end in general
38#define _CRT_SECURE_NO_WARNINGS
39
John Kessenich2b07c7e2013-07-31 18:44:13 +000040#include "Worklist.h"
John Kessenicha0af4732012-12-12 21:15:54 +000041#include "./../glslang/Include/ShHandle.h"
42#include "./../glslang/Public/ShaderLang.h"
43#include <string.h>
John Kessenichcfd643e2013-03-08 23:14:42 +000044#include <stdlib.h>
John Kessenicha0af4732012-12-12 21:15:54 +000045#include <math.h>
46
John Kessenich2b07c7e2013-07-31 18:44:13 +000047#include "osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000048
49extern "C" {
50 SH_IMPORT_EXPORT void ShOutputHtml();
51}
52
John Kessenich94a81fb2013-08-31 02:41:30 +000053// Command-line options
54enum TOptions {
55 EOptionNone = 0x000,
56 EOptionIntermediate = 0x001,
57 EOptionSuppressInfolog = 0x002,
58 EOptionMemoryLeakMode = 0x004,
59 EOptionRelaxedErrors = 0x008,
60 EOptionGiveWarnings = 0x010,
61 EOptionsLinkProgram = 0x020,
John Kessenich38f3b892013-09-06 19:52:57 +000062 EOptionMultiThreaded = 0x040,
John Kessenich05a70632013-09-17 19:26:08 +000063 EOptionDumpConfig = 0x080,
John Kessenich11f9fc72013-11-07 01:06:34 +000064 EOptionDumpReflection = 0x100,
John Kessenichb0a7eb52013-11-07 17:44:20 +000065 EOptionSuppressWarnings = 0x200,
John Kessenich94a81fb2013-08-31 02:41:30 +000066};
67
John Kessenicha0af4732012-12-12 21:15:54 +000068//
69// Return codes from main.
70//
71enum TFailCode {
72 ESuccess = 0,
73 EFailUsage,
74 EFailCompile,
75 EFailLink,
76 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000077 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000078 EFailLinkerCreate
79};
80
81//
82// Just placeholders for testing purposes. The stand-alone environment
83// can't actually do a full link without something specifying real
84// attribute bindings.
85//
86ShBinding FixedAttributeBindings[] = {
87 { "gl_Vertex", 15 },
88 { "gl_Color", 10 },
89 { "gl_Normal", 7 },
90};
91
92ShBindingTable FixedAttributeTable = { 3, FixedAttributeBindings };
93
John Kessenichb603f912013-08-29 00:39:25 +000094EShLanguage FindLanguage(const std::string& name);
John Kessenichb0a7eb52013-11-07 17:44:20 +000095bool CompileFile(const char *fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +000096void usage();
John Kessenichea869fb2013-10-28 18:12:06 +000097void FreeFileData(char** data);
98char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +000099void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000100
John Kessenich05a70632013-09-17 19:26:08 +0000101// Use to test breaking up a single shader file into multiple strings.
John Kessenichea869fb2013-10-28 18:12:06 +0000102int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000103
John Kessenich05a70632013-09-17 19:26:08 +0000104TBuiltInResource Resources;
105std::string ConfigFile;
106
John Kessenicha0af4732012-12-12 21:15:54 +0000107//
John Kessenich05a70632013-09-17 19:26:08 +0000108// These are the default resources for TBuiltInResources, used for both
109// - parsing this string for the case where the user didn't supply one
110// - dumping out a template for user construction of a config file
John Kessenicha0af4732012-12-12 21:15:54 +0000111//
John Kessenich284231c2013-10-22 01:50:39 +0000112const char* DefaultConfig =
113 "MaxLights 32\n"
114 "MaxClipPlanes 6\n"
115 "MaxTextureUnits 32\n"
116 "MaxTextureCoords 32\n"
117 "MaxVertexAttribs 64\n"
118 "MaxVertexUniformComponents 4096\n"
119 "MaxVaryingFloats 64\n"
120 "MaxVertexTextureImageUnits 32\n"
121 "MaxCombinedTextureImageUnits 32\n"
122 "MaxTextureImageUnits 32\n"
123 "MaxFragmentUniformComponents 4096\n"
124 "MaxDrawBuffers 32\n"
125 "MaxVertexUniformVectors 128\n"
126 "MaxVaryingVectors 8\n"
127 "MaxFragmentUniformVectors 16\n"
128 "MaxVertexOutputVectors 16\n"
129 "MaxFragmentInputVectors 15\n"
130 "MinProgramTexelOffset -8\n"
131 "MaxProgramTexelOffset 7\n"
132 "MaxClipDistances 8\n"
133 "MaxComputeWorkGroupCountX 65535\n"
134 "MaxComputeWorkGroupCountY 65535\n"
135 "MaxComputeWorkGroupCountZ 65535\n"
136 "MaxComputeWorkGroupSizeX 1024\n"
137 "MaxComputeWorkGroupSizeX 1024\n"
138 "MaxComputeWorkGroupSizeZ 64\n"
139 "MaxComputeUniformComponents 1024\n"
140 "MaxComputeTextureImageUnits 16\n"
141 "MaxComputeImageUniforms 8\n"
142 "MaxComputeAtomicCounters 8\n"
143 "MaxComputeAtomicCounterBuffers 1\n"
144 "MaxVaryingComponents 60\n"
145 "MaxVertexOutputComponents 64\n"
146 "MaxGeometryInputComponents 64\n"
147 "MaxGeometryOutputComponents 128\n"
148 "MaxFragmentInputComponents 128\n"
149 "MaxImageUnits 8\n"
150 "MaxCombinedImageUnitsAndFragmentOutputs 8\n"
151 "MaxImageSamples 0\n"
152 "MaxVertexImageUniforms 0\n"
153 "MaxTessControlImageUniforms 0\n"
154 "MaxTessEvaluationImageUniforms 0\n"
155 "MaxGeometryImageUniforms 0\n"
156 "MaxFragmentImageUniforms 8\n"
157 "MaxCombinedImageUniforms 8\n"
158 "MaxGeometryTextureImageUnits 16\n"
159 "MaxGeometryOutputVertices 256\n"
160 "MaxGeometryTotalOutputComponents 1024\n"
161 "MaxGeometryUniformComponents 1024\n"
162 "MaxGeometryVaryingComponents 64\n"
163 "MaxTessControlInputComponents 128\n"
164 "MaxTessControlOutputComponents 128\n"
165 "MaxTessControlTextureImageUnits 16\n"
166 "MaxTessControlUniformComponents 1024\n"
167 "MaxTessControlTotalOutputComponents 4096\n"
168 "MaxTessEvaluationInputComponents 128\n"
169 "MaxTessEvaluationOutputComponents 128\n"
170 "MaxTessEvaluationTextureImageUnits 16\n"
171 "MaxTessEvaluationUniformComponents 1024\n"
172 "MaxTessPatchComponents 120\n"
173 "MaxPatchVertices 32\n"
174 "MaxTessGenLevel 64\n"
175 "MaxViewports 16\n"
176 "MaxVertexAtomicCounters 0\n"
177 "MaxTessControlAtomicCounters 0\n"
178 "MaxTessEvaluationAtomicCounters 0\n"
179 "MaxGeometryAtomicCounters 0\n"
180 "MaxFragmentAtomicCounters 8\n"
181 "MaxCombinedAtomicCounters 8\n"
182 "MaxAtomicCounterBindings 1\n"
183 "MaxVertexAtomicCounterBuffers 0\n"
184 "MaxTessControlAtomicCounterBuffers 0\n"
185 "MaxTessEvaluationAtomicCounterBuffers 0\n"
186 "MaxGeometryAtomicCounterBuffers 0\n"
187 "MaxFragmentAtomicCounterBuffers 1\n"
188 "MaxCombinedAtomicCounterBuffers 1\n"
189 "MaxAtomicCounterBufferSize 16384\n"
190
191 "nonInductiveForLoops 1\n"
192 "whileLoops 1\n"
193 "doWhileLoops 1\n"
194 "generalUniformIndexing 1\n"
195 "generalAttributeMatrixVectorIndexing 1\n"
196 "generalVaryingIndexing 1\n"
197 "generalSamplerIndexing 1\n"
198 "generalVariableIndexing 1\n"
199 "generalConstantMatrixVectorIndexing 1\n"
200 ;
John Kessenich05a70632013-09-17 19:26:08 +0000201
202//
203// Parse either a .conf file provided by the user or the default string above.
204//
205void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000206{
John Kessenich05a70632013-09-17 19:26:08 +0000207 char** configStrings = 0;
208 char *config = 0;
209 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000210 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000211 if (configStrings)
212 config = *configStrings;
213 else {
214 printf("Error opening configuration file; will instead use the default configuration\n");
215 usage();
216 }
217 }
218
219 if (config == 0) {
220 config = new char[strlen(DefaultConfig)];
221 strcpy(config, DefaultConfig);
222 }
223
224 const char* delims = " \t\n\r";
225 const char* token = strtok(config, delims);
226 while (token) {
227 const char* valueStr = strtok(0, delims);
228 if (valueStr == 0 || ! (valueStr[0] == '-' || (valueStr[0] >= '0' && valueStr[0] <= '9'))) {
229 printf("Error: '%s' bad .conf file. Each name must be followed by one number.\n", valueStr ? valueStr : "");
230 return;
231 }
232 int value = atoi(valueStr);
233
234 if (strcmp(token, "MaxLights") == 0)
235 Resources.maxLights = value;
236 else if (strcmp(token, "MaxClipPlanes") == 0)
237 Resources.maxClipPlanes = value;
238 else if (strcmp(token, "MaxTextureUnits") == 0)
239 Resources.maxTextureUnits = value;
240 else if (strcmp(token, "MaxTextureCoords") == 0)
241 Resources.maxTextureCoords = value;
242 else if (strcmp(token, "MaxVertexAttribs") == 0)
243 Resources.maxVertexAttribs = value;
244 else if (strcmp(token, "MaxVertexUniformComponents") == 0)
245 Resources.maxVertexUniformComponents = value;
246 else if (strcmp(token, "MaxVaryingFloats") == 0)
247 Resources.maxVaryingFloats = value;
248 else if (strcmp(token, "MaxVertexTextureImageUnits") == 0)
249 Resources.maxVertexTextureImageUnits = value;
250 else if (strcmp(token, "MaxCombinedTextureImageUnits") == 0)
251 Resources.maxCombinedTextureImageUnits = value;
252 else if (strcmp(token, "MaxTextureImageUnits") == 0)
253 Resources.maxTextureImageUnits = value;
254 else if (strcmp(token, "MaxFragmentUniformComponents") == 0)
255 Resources.maxFragmentUniformComponents = value;
256 else if (strcmp(token, "MaxDrawBuffers") == 0)
257 Resources.maxDrawBuffers = value;
258 else if (strcmp(token, "MaxVertexUniformVectors") == 0)
259 Resources.maxVertexUniformVectors = value;
260 else if (strcmp(token, "MaxVaryingVectors") == 0)
261 Resources.maxVaryingVectors = value;
262 else if (strcmp(token, "MaxFragmentUniformVectors") == 0)
263 Resources.maxFragmentUniformVectors = value;
264 else if (strcmp(token, "MaxVertexOutputVectors") == 0)
265 Resources.maxVertexOutputVectors = value;
266 else if (strcmp(token, "MaxFragmentInputVectors") == 0)
267 Resources.maxFragmentInputVectors = value;
268 else if (strcmp(token, "MinProgramTexelOffset") == 0)
269 Resources.minProgramTexelOffset = value;
270 else if (strcmp(token, "MaxProgramTexelOffset") == 0)
271 Resources.maxProgramTexelOffset = value;
John Kesseniche7c59c12013-10-16 22:28:35 +0000272 else if (strcmp(token, "MaxClipDistances") == 0)
273 Resources.maxClipDistances = value;
John Kessenich284231c2013-10-22 01:50:39 +0000274 else if (strcmp(token, "MaxComputeWorkGroupCountX") == 0)
275 Resources.maxComputeWorkGroupCountX = value;
276 else if (strcmp(token, "MaxComputeWorkGroupCountY") == 0)
277 Resources.maxComputeWorkGroupCountY = value;
278 else if (strcmp(token, "MaxComputeWorkGroupCountZ") == 0)
279 Resources.maxComputeWorkGroupCountZ = value;
280 else if (strcmp(token, "MaxComputeWorkGroupSizeX") == 0)
281 Resources.maxComputeWorkGroupSizeX = value;
282 else if (strcmp(token, "MaxComputeWorkGroupSizeY") == 0)
283 Resources.maxComputeWorkGroupSizeY = value;
284 else if (strcmp(token, "MaxComputeWorkGroupSizeZ") == 0)
285 Resources.maxComputeWorkGroupSizeZ = value;
286 else if (strcmp(token, "MaxComputeUniformComponents") == 0)
287 Resources.maxComputeUniformComponents = value;
288 else if (strcmp(token, "MaxComputeTextureImageUnits") == 0)
289 Resources.maxComputeTextureImageUnits = value;
290 else if (strcmp(token, "MaxComputeImageUniforms") == 0)
291 Resources.maxComputeImageUniforms = value;
292 else if (strcmp(token, "MaxComputeAtomicCounters") == 0)
293 Resources.maxComputeAtomicCounters = value;
294 else if (strcmp(token, "MaxComputeAtomicCounterBuffers") == 0)
295 Resources.maxComputeAtomicCounterBuffers = value;
296 else if (strcmp(token, "MaxVaryingComponents") == 0)
297 Resources.maxVaryingComponents = value;
298 else if (strcmp(token, "MaxVertexOutputComponents") == 0)
299 Resources.maxVertexOutputComponents = value;
300 else if (strcmp(token, "MaxGeometryInputComponents") == 0)
301 Resources.maxGeometryInputComponents = value;
302 else if (strcmp(token, "MaxGeometryOutputComponents") == 0)
303 Resources.maxGeometryOutputComponents = value;
304 else if (strcmp(token, "MaxFragmentInputComponents") == 0)
305 Resources.maxFragmentInputComponents = value;
306 else if (strcmp(token, "MaxImageUnits") == 0)
307 Resources.maxImageUnits = value;
308 else if (strcmp(token, "MaxCombinedImageUnitsAndFragmentOutputs") == 0)
309 Resources.maxCombinedImageUnitsAndFragmentOutputs = value;
310 else if (strcmp(token, "MaxImageSamples") == 0)
311 Resources.maxImageSamples = value;
312 else if (strcmp(token, "MaxVertexImageUniforms") == 0)
313 Resources.maxVertexImageUniforms = value;
314 else if (strcmp(token, "MaxTessControlImageUniforms") == 0)
315 Resources.maxTessControlImageUniforms = value;
316 else if (strcmp(token, "MaxTessEvaluationImageUniforms") == 0)
317 Resources.maxTessEvaluationImageUniforms = value;
318 else if (strcmp(token, "MaxGeometryImageUniforms") == 0)
319 Resources.maxGeometryImageUniforms = value;
320 else if (strcmp(token, "MaxFragmentImageUniforms") == 0)
321 Resources.maxFragmentImageUniforms = value;
322 else if (strcmp(token, "MaxCombinedImageUniforms") == 0)
323 Resources.maxCombinedImageUniforms = value;
324 else if (strcmp(token, "MaxGeometryTextureImageUnits") == 0)
325 Resources.maxGeometryTextureImageUnits = value;
326 else if (strcmp(token, "MaxGeometryOutputVertices") == 0)
327 Resources.maxGeometryOutputVertices = value;
328 else if (strcmp(token, "MaxGeometryTotalOutputComponents") == 0)
329 Resources.maxGeometryTotalOutputComponents = value;
330 else if (strcmp(token, "MaxGeometryUniformComponents") == 0)
331 Resources.maxGeometryUniformComponents = value;
332 else if (strcmp(token, "MaxGeometryVaryingComponents") == 0)
333 Resources.maxGeometryVaryingComponents = value;
334 else if (strcmp(token, "MaxTessControlInputComponents") == 0)
335 Resources.maxTessControlInputComponents = value;
336 else if (strcmp(token, "MaxTessControlOutputComponents") == 0)
337 Resources.maxTessControlOutputComponents = value;
338 else if (strcmp(token, "MaxTessControlTextureImageUnits") == 0)
339 Resources.maxTessControlTextureImageUnits = value;
340 else if (strcmp(token, "MaxTessControlUniformComponents") == 0)
341 Resources.maxTessControlUniformComponents = value;
342 else if (strcmp(token, "MaxTessControlTotalOutputComponents") == 0)
343 Resources.maxTessControlTotalOutputComponents = value;
344 else if (strcmp(token, "MaxTessEvaluationInputComponents") == 0)
345 Resources.maxTessEvaluationInputComponents = value;
346 else if (strcmp(token, "MaxTessEvaluationOutputComponents") == 0)
347 Resources.maxTessEvaluationOutputComponents = value;
348 else if (strcmp(token, "MaxTessEvaluationTextureImageUnits") == 0)
349 Resources.maxTessEvaluationTextureImageUnits = value;
350 else if (strcmp(token, "MaxTessEvaluationUniformComponents") == 0)
351 Resources.maxTessEvaluationUniformComponents = value;
352 else if (strcmp(token, "MaxTessPatchComponents") == 0)
353 Resources.maxTessPatchComponents = value;
354 else if (strcmp(token, "MaxPatchVertices") == 0)
355 Resources.maxPatchVertices = value;
356 else if (strcmp(token, "MaxTessGenLevel") == 0)
357 Resources.maxTessGenLevel = value;
358 else if (strcmp(token, "MaxViewports") == 0)
359 Resources.maxViewports = value;
360 else if (strcmp(token, "MaxVertexAtomicCounters") == 0)
361 Resources.maxVertexAtomicCounters = value;
362 else if (strcmp(token, "MaxTessControlAtomicCounters") == 0)
363 Resources.maxTessControlAtomicCounters = value;
364 else if (strcmp(token, "MaxTessEvaluationAtomicCounters") == 0)
365 Resources.maxTessEvaluationAtomicCounters = value;
366 else if (strcmp(token, "MaxGeometryAtomicCounters") == 0)
367 Resources.maxGeometryAtomicCounters = value;
368 else if (strcmp(token, "MaxFragmentAtomicCounters") == 0)
369 Resources.maxFragmentAtomicCounters = value;
370 else if (strcmp(token, "MaxCombinedAtomicCounters") == 0)
371 Resources.maxCombinedAtomicCounters = value;
372 else if (strcmp(token, "MaxAtomicCounterBindings") == 0)
373 Resources.maxAtomicCounterBindings = value;
374 else if (strcmp(token, "MaxVertexAtomicCounterBuffers") == 0)
375 Resources.maxVertexAtomicCounterBuffers = value;
376 else if (strcmp(token, "MaxTessControlAtomicCounterBuffers") == 0)
377 Resources.maxTessControlAtomicCounterBuffers = value;
378 else if (strcmp(token, "MaxTessEvaluationAtomicCounterBuffers") == 0)
379 Resources.maxTessEvaluationAtomicCounterBuffers = value;
380 else if (strcmp(token, "MaxGeometryAtomicCounterBuffers") == 0)
381 Resources.maxGeometryAtomicCounterBuffers = value;
382 else if (strcmp(token, "MaxFragmentAtomicCounterBuffers") == 0)
383 Resources.maxFragmentAtomicCounterBuffers = value;
384 else if (strcmp(token, "MaxCombinedAtomicCounterBuffers") == 0)
385 Resources.maxCombinedAtomicCounterBuffers = value;
386 else if (strcmp(token, "MaxAtomicCounterBufferSize") == 0)
387 Resources.maxAtomicCounterBufferSize = value;
388
John Kessenicha5830df2013-10-02 05:10:48 +0000389 else if (strcmp(token, "nonInductiveForLoops") == 0)
390 Resources.limits.nonInductiveForLoops = (value != 0);
391 else if (strcmp(token, "whileLoops") == 0)
392 Resources.limits.whileLoops = (value != 0);
393 else if (strcmp(token, "doWhileLoops") == 0)
394 Resources.limits.doWhileLoops = (value != 0);
395 else if (strcmp(token, "generalUniformIndexing") == 0)
396 Resources.limits.generalUniformIndexing = (value != 0);
397 else if (strcmp(token, "generalAttributeMatrixVectorIndexing") == 0)
398 Resources.limits.generalAttributeMatrixVectorIndexing = (value != 0);
399 else if (strcmp(token, "generalVaryingIndexing") == 0)
400 Resources.limits.generalVaryingIndexing = (value != 0);
401 else if (strcmp(token, "generalSamplerIndexing") == 0)
402 Resources.limits.generalSamplerIndexing = (value != 0);
403 else if (strcmp(token, "generalVariableIndexing") == 0)
404 Resources.limits.generalVariableIndexing = (value != 0);
405 else if (strcmp(token, "generalConstantMatrixVectorIndexing") == 0)
406 Resources.limits.generalConstantMatrixVectorIndexing = (value != 0);
John Kessenich05a70632013-09-17 19:26:08 +0000407 else
408 printf("Warning: unrecognized limit (%s) in configuration file.\n", token);
409
410 token = strtok(0, delims);
411 }
412 if (configStrings)
413 FreeFileData(configStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000414}
415
John Kessenich38f3b892013-09-06 19:52:57 +0000416// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000417glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000418
419// array of unique places to leave the shader names and infologs for the asynchronous compiles
420glslang::TWorkItem **Work = 0;
421int NumWorkItems = 0;
422
John Kessenich94a81fb2013-08-31 02:41:30 +0000423int Options = 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000424bool Delay = false;
John Kessenich38f3b892013-09-06 19:52:57 +0000425const char* ExecutableName;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000426
John Kessenich05a70632013-09-17 19:26:08 +0000427//
428// *.conf => this is a config file that can set limits/resources
429//
430bool SetConfigFile(const std::string& name)
431{
432 if (name.size() < 5)
433 return false;
434
John Kessenich4c706852013-10-11 16:28:43 +0000435 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000436 ConfigFile = name;
437 return true;
438 }
439
440 return false;
441}
442
John Kessenich2b07c7e2013-07-31 18:44:13 +0000443bool ProcessArguments(int argc, char* argv[])
444{
John Kessenich38f3b892013-09-06 19:52:57 +0000445 ExecutableName = argv[0];
446 NumWorkItems = argc; // will include some empties where the '-' options were, but it doesn't matter, they'll be 0
447 Work = new glslang::TWorkItem*[NumWorkItems];
448 Work[0] = 0;
449
John Kessenich2b07c7e2013-07-31 18:44:13 +0000450 argc--;
451 argv++;
452 for (; argc >= 1; argc--, argv++) {
John Kessenich05a70632013-09-17 19:26:08 +0000453 Work[argc] = 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000454 if (argv[0][0] == '-') {
455 switch (argv[0][1]) {
John Kessenich05a70632013-09-17 19:26:08 +0000456 case 'c':
457 Options |= EOptionDumpConfig;
458 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000459 case 'd':
460 Delay = true;
461 break;
John Kessenich05a70632013-09-17 19:26:08 +0000462 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000463 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000464 break;
465 case 'l':
John Kessenich94a81fb2013-08-31 02:41:30 +0000466 Options |= EOptionsLinkProgram;
467 break;
468 case 'm':
469 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000470 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000471 case 'q':
472 Options |= EOptionDumpReflection;
473 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000474 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000475 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000476 break;
477 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000478 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000479 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000480 case 't':
481 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000482 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000483 #endif
484 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000485 case 'w':
486 Options |= EOptionSuppressWarnings;
487 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000488 default:
John Kessenich2b07c7e2013-07-31 18:44:13 +0000489 return false;
490 }
John Kessenich38f3b892013-09-06 19:52:57 +0000491 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000492 std::string name(argv[0]);
493 if (! SetConfigFile(name)) {
494 Work[argc] = new glslang::TWorkItem(name);
495 Worklist.add(Work[argc]);
496 }
John Kessenich38f3b892013-09-06 19:52:57 +0000497 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000498 }
499
500 return true;
501}
502
John Kessenichb0a7eb52013-11-07 17:44:20 +0000503void SetMessageOptions(EShMessages& messages)
504{
505 if (Options & EOptionRelaxedErrors)
506 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
507 if (Options & EOptionIntermediate)
508 messages = (EShMessages)(messages | EShMsgAST);
509 if (Options & EOptionSuppressWarnings)
510 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
511}
512
John Kessenich69f4b512013-09-04 21:19:27 +0000513// Thread entry point, for non-linking asynchronous mode.
John Kessenichee6a9c82013-07-31 23:19:17 +0000514unsigned int
515#ifdef _WIN32
516 __stdcall
517#endif
John Kessenich94a81fb2013-08-31 02:41:30 +0000518CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000519{
John Kessenich38f3b892013-09-06 19:52:57 +0000520 glslang::TWorkItem* workItem;
521 while (Worklist.remove(workItem)) {
522 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000523 if (compiler == 0)
524 return false;
525
John Kessenichb0a7eb52013-11-07 17:44:20 +0000526 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000527
John Kessenich94a81fb2013-08-31 02:41:30 +0000528 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000529 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000530
531 ShDestruct(compiler);
532 }
533
534 return 0;
535}
536
John Kessenich69f4b512013-09-04 21:19:27 +0000537//
538// For linking mode: Will independently parse each item in the worklist, but then put them
539// in the same program and link them together.
540//
541// Uses the new C++ interface instead of the old handle-based interface.
542//
543void CompileAndLinkShaders()
544{
545 // keep track of what to free
546 std::list<glslang::TShader*> shaders;
547
548 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000549 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000550
John Kessenich69f4b512013-09-04 21:19:27 +0000551 //
552 // Per-shader processing...
553 //
554
John Kessenich5b0f13a2013-11-01 03:08:40 +0000555 glslang::TProgram& program = *new glslang::TProgram;
John Kessenich38f3b892013-09-06 19:52:57 +0000556 glslang::TWorkItem* workItem;
557 while (Worklist.remove(workItem)) {
558 EShLanguage stage = FindLanguage(workItem->name);
John Kessenich69f4b512013-09-04 21:19:27 +0000559 glslang::TShader* shader = new glslang::TShader(stage);
560 shaders.push_back(shader);
561
John Kessenich38f3b892013-09-06 19:52:57 +0000562 char** shaderStrings = ReadFileData(workItem->name.c_str());
John Kessenich69f4b512013-09-04 21:19:27 +0000563 if (! shaderStrings) {
564 usage();
565 return;
566 }
567
568 shader->setStrings(shaderStrings, 1);
569
John Kessenich05a70632013-09-17 19:26:08 +0000570 shader->parse(&Resources, 100, false, messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000571
572 program.addShader(shader);
573
574 if (! (Options & EOptionSuppressInfolog)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000575 puts(workItem->name.c_str());
John Kessenich69f4b512013-09-04 21:19:27 +0000576 puts(shader->getInfoLog());
577 puts(shader->getInfoDebugLog());
578 }
579
580 FreeFileData(shaderStrings);
581 }
582
583 //
584 // Program-level processing...
585 //
586
587 program.link(messages);
588 if (! (Options & EOptionSuppressInfolog)) {
589 puts(program.getInfoLog());
590 puts(program.getInfoDebugLog());
591 }
592
John Kessenich11f9fc72013-11-07 01:06:34 +0000593 if (Options & EOptionDumpReflection) {
594 program.buildReflection();
595 program.dumpReflection();
596 }
597
John Kessenich5b0f13a2013-11-01 03:08:40 +0000598 // Free everything up, program has to go before the shaders
599 // because it might have merged stuff from the shaders, and
600 // the stuff from the shaders has to have its destructors called
601 // before the pools holding the memory in the shaders is freed.
602 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000603 while (shaders.size() > 0) {
604 delete shaders.back();
605 shaders.pop_back();
606 }
John Kessenich69f4b512013-09-04 21:19:27 +0000607}
608
John Kessenicha0af4732012-12-12 21:15:54 +0000609int C_DECL main(int argc, char* argv[])
610{
John Kessenicha0af4732012-12-12 21:15:54 +0000611 bool compileFailed = false;
612 bool linkFailed = false;
John Kessenicha0af4732012-12-12 21:15:54 +0000613
John Kessenich54f6e562013-08-03 00:04:10 +0000614 if (! ProcessArguments(argc, argv)) {
615 usage();
John Kessenich2b07c7e2013-07-31 18:44:13 +0000616 return EFailUsage;
John Kessenich54f6e562013-08-03 00:04:10 +0000617 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000618
John Kessenich05a70632013-09-17 19:26:08 +0000619 if (Options & EOptionDumpConfig) {
620 printf("%s", DefaultConfig);
621 if (Worklist.empty())
622 return ESuccess;
623 }
624
625 if (Worklist.empty()) {
626 usage();
627 return EFailUsage;
628 }
629
630 ProcessConfigFile();
631
John Kessenich69f4b512013-09-04 21:19:27 +0000632 //
633 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000634 // 1) linking all arguments together, single-threaded, new C++ interface
635 // 2) independent arguments, can be tackled by multiple asynchronous threads, for testing thread safety, using the old handle interface
John Kessenich69f4b512013-09-04 21:19:27 +0000636 //
John Kessenichc36e1d82013-11-01 17:41:52 +0000637 if (Options & EOptionsLinkProgram) {
638 glslang::InitializeProcess();
John Kessenich38f3b892013-09-06 19:52:57 +0000639 CompileAndLinkShaders();
John Kessenichc36e1d82013-11-01 17:41:52 +0000640 glslang::FinalizeProcess();
641 } else {
642 ShInitialize();
643
John Kessenich38f3b892013-09-06 19:52:57 +0000644 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000645
John Kessenich38f3b892013-09-06 19:52:57 +0000646 if (Options & EOptionMultiThreaded) {
647 const int NumThreads = 16;
648 void* threads[NumThreads];
649 for (int t = 0; t < NumThreads; ++t) {
650 threads[t] = glslang::OS_CreateThread(&CompileShaders);
651 if (! threads[t]) {
652 printf("Failed to create thread\n");
653 return EFailThreadCreate;
654 }
John Kessenicha0af4732012-12-12 21:15:54 +0000655 }
John Kessenich38f3b892013-09-06 19:52:57 +0000656 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenich69f4b512013-09-04 21:19:27 +0000657 } else {
658 if (! CompileShaders(0))
659 compileFailed = true;
660 }
John Kessenich38f3b892013-09-06 19:52:57 +0000661
662 // Print out all the resulting infologs
663 for (int w = 0; w < NumWorkItems; ++w) {
664 if (Work[w]) {
665 if (printShaderNames)
666 puts(Work[w]->name.c_str());
667 puts(Work[w]->results.c_str());
668 delete Work[w];
669 }
670 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000671
672 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000673 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000674
675 if (Delay)
676 glslang::OS_Sleep(1000000);
John Kessenicha0af4732012-12-12 21:15:54 +0000677
678 if (compileFailed)
679 return EFailCompile;
680 if (linkFailed)
681 return EFailLink;
682
683 return 0;
684}
685
686//
687// Deduce the language from the filename. Files must end in one of the
688// following extensions:
689//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000690// .vert = vertex
691// .tesc = tessellation control
692// .tese = tessellation evaluation
693// .geom = geometry
694// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000695// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000696//
John Kessenichb603f912013-08-29 00:39:25 +0000697EShLanguage FindLanguage(const std::string& name)
John Kessenicha0af4732012-12-12 21:15:54 +0000698{
John Kessenich2b07c7e2013-07-31 18:44:13 +0000699 size_t ext = name.rfind('.');
700 if (ext == std::string::npos) {
701 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000702 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000703 }
704
John Kessenich2b07c7e2013-07-31 18:44:13 +0000705 std::string suffix = name.substr(ext + 1, std::string::npos);
706 if (suffix == "vert")
707 return EShLangVertex;
708 else if (suffix == "tesc")
709 return EShLangTessControl;
710 else if (suffix == "tese")
711 return EShLangTessEvaluation;
712 else if (suffix == "geom")
713 return EShLangGeometry;
714 else if (suffix == "frag")
715 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000716 else if (suffix == "comp")
717 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000718
719 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000720 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000721}
722
John Kessenicha0af4732012-12-12 21:15:54 +0000723//
John Kessenich69f4b512013-09-04 21:19:27 +0000724// Read a file's data into a string, and compile it using the old interface ShCompile,
725// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000726//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000727bool CompileFile(const char *fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000728{
729 int ret;
John Kessenich41cf6b52013-06-25 18:10:05 +0000730 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000731 if (! shaderStrings) {
732 usage();
733 return false;
734 }
735
John Kessenich41cf6b52013-06-25 18:10:05 +0000736 int* lengths = new int[NumShaderStrings];
737
738 // move to length-based strings, rather than null-terminated strings
739 for (int s = 0; s < NumShaderStrings; ++s)
740 lengths[s] = strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000741
John Kessenich41cf6b52013-06-25 18:10:05 +0000742 if (! shaderStrings)
John Kessenicha0af4732012-12-12 21:15:54 +0000743 return false;
744
John Kessenich52ac67e2013-05-05 23:46:22 +0000745 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000746 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000747
John Kessenich94a81fb2013-08-31 02:41:30 +0000748 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
749 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich05a70632013-09-17 19:26:08 +0000750 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, 100, false, messages);
751 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, 0, EShOptNone, &Resources, Options, 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000752 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
753 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
754 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000755 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichea869fb2013-10-28 18:12:06 +0000756 //ret = ShCompile(compiler, multi, 7, 0, EShOptNone, &Resources, Options, 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000757 }
John Kessenicha0af4732012-12-12 21:15:54 +0000758
John Kessenich94a81fb2013-08-31 02:41:30 +0000759 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000760 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000761 }
John Kessenicha0af4732012-12-12 21:15:54 +0000762
John Kessenich41cf6b52013-06-25 18:10:05 +0000763 delete [] lengths;
764 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000765
766 return ret ? true : false;
767}
768
John Kessenicha0af4732012-12-12 21:15:54 +0000769//
770// print usage to stdout
771//
772void usage()
773{
John Kessenich38f3b892013-09-06 19:52:57 +0000774 printf("Usage: glslangValidator [ options ] filename\n"
John Kessenichc0275792013-08-09 17:14:49 +0000775 "Where: filename is a name ending in\n"
John Kessenich05a70632013-09-17 19:26:08 +0000776 " .conf provides an optional config file that replaces the default configuration\n"
777 " (see -c option below for generating a template)\n"
John Kessenichc0275792013-08-09 17:14:49 +0000778 " .vert for a vertex shader\n"
779 " .tesc for a tessellation control shader\n"
780 " .tese for a tessellation evaluation shader\n"
781 " .geom for a geometry shader\n"
782 " .frag for a fragment shader\n"
783 " .comp for a compute shader\n\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000784 "Compilation warnings and errors will be printed to stdout.\n"
785 "To get other information, use one of the following options:\n"
John Kessenich05a70632013-09-17 19:26:08 +0000786 "-c: configuration dump; use to create default configuration file (redirect to a .conf file)\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000787 "-i: intermediate tree (glslang AST) is printed out\n"
John Kessenich94a81fb2013-08-31 02:41:30 +0000788 "-l: link validation of all input files\n"
789 "-m: memory leak mode\n"
John Kessenich11f9fc72013-11-07 01:06:34 +0000790 "-q: dump reflection query database\n"
John Kessenich38f3b892013-09-06 19:52:57 +0000791 "-r: relaxed semantic error-checking mode\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000792 "-s: silent mode\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000793 "-t: multi-threaded mode\n"
794 "-w: suppress warnings (except as required by #extension : warn)\n"
795 );
John Kessenicha0af4732012-12-12 21:15:54 +0000796}
797
John Kessenichcfd643e2013-03-08 23:14:42 +0000798#ifndef _WIN32
799
800#include <errno.h>
801
802int fopen_s(
803 FILE** pFile,
804 const char *filename,
805 const char *mode
806)
807{
808 if (!pFile || !filename || !mode) {
809 return EINVAL;
810 }
811
812 FILE* f = fopen(filename, mode);
813 if (! f) {
814 if (errno != 0) {
815 return errno;
816 } else {
817 return ENOENT;
818 }
819 }
820 *pFile = f;
821
822 return 0;
823}
824
825#endif
826
John Kessenicha0af4732012-12-12 21:15:54 +0000827//
828// Malloc a string of sufficient size and read a string into it.
829//
John Kessenich54d8cda2013-02-11 22:36:01 +0000830char** ReadFileData(const char *fileName)
John Kessenicha0af4732012-12-12 21:15:54 +0000831{
John Kessenich200b2732012-12-12 21:21:23 +0000832 FILE *in;
833 int errorCode = fopen_s(&in, fileName, "r");
John Kessenicha0af4732012-12-12 21:15:54 +0000834 char *fdata;
835 int count = 0;
John Kessenichcfd643e2013-03-08 23:14:42 +0000836 const int maxSourceStrings = 5;
837 char** return_data = (char**)malloc(maxSourceStrings+1);
John Kessenicha0af4732012-12-12 21:15:54 +0000838
839 //return_data[MAX_SOURCE_STRINGS]=NULL;
John Kessenich200b2732012-12-12 21:21:23 +0000840 if (errorCode) {
John Kessenicha0af4732012-12-12 21:15:54 +0000841 printf("Error: unable to open input file: %s\n", fileName);
842 return 0;
843 }
844
845 while (fgetc(in) != EOF)
846 count++;
847
848 fseek(in, 0, SEEK_SET);
849
850
851 if (!(fdata = (char *)malloc(count+2))) {
852 printf("Error allocating memory\n");
853 return 0;
854 }
855 if (fread(fdata,1,count, in)!=count) {
856 printf("Error reading input file: %s\n", fileName);
857 return 0;
858 }
859 fdata[count] = '\0';
860 fclose(in);
John Kessenichea869fb2013-10-28 18:12:06 +0000861 if (count == 0) {
John Kessenicha0af4732012-12-12 21:15:54 +0000862 return_data[0]=(char*)malloc(count+2);
863 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +0000864 NumShaderStrings = 0;
John Kessenicha0af4732012-12-12 21:15:54 +0000865 return return_data;
John Kessenichea869fb2013-10-28 18:12:06 +0000866 } else
867 NumShaderStrings = 1;
John Kessenicha0af4732012-12-12 21:15:54 +0000868
John Kessenich41cf6b52013-06-25 18:10:05 +0000869 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000870 int ptr_len=0,i=0;
871 while(count>0){
872 return_data[i]=(char*)malloc(len+2);
873 memcpy(return_data[i],fdata+ptr_len,len);
874 return_data[i][len]='\0';
875 count-=(len);
876 ptr_len+=(len);
877 if(count<len){
878 if(count==0){
John Kessenich41cf6b52013-06-25 18:10:05 +0000879 NumShaderStrings=(i+1);
John Kessenicha0af4732012-12-12 21:15:54 +0000880 break;
881 }
882 len = count;
883 }
884 ++i;
885 }
886 return return_data;
887}
888
John Kessenicha0af4732012-12-12 21:15:54 +0000889void FreeFileData(char **data)
890{
John Kessenich41cf6b52013-06-25 18:10:05 +0000891 for(int i=0;i<NumShaderStrings;i++)
John Kessenicha0af4732012-12-12 21:15:54 +0000892 free(data[i]);
893}
894
John Kessenich54d8cda2013-02-11 22:36:01 +0000895void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +0000896{
897 printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" :
898 "#### %s %s INFO LOG ####\n", msg, name, num);
899}