blob: 906dcbdfbd8667912d6aa82a08de159646e99512 [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"
John Kessenich0df0cde2015-03-03 17:09:43 +000043#include "../SPIRV/GlslangToSpv.h"
44#include "../SPIRV/GLSL450Lib.h"
John Kessenichacba7722015-03-04 03:48:38 +000045#include "../SPIRV/doc.h"
46#include "../SPIRV/disassemble.h"
John Kessenicha0af4732012-12-12 21:15:54 +000047#include <string.h>
John Kessenichcfd643e2013-03-08 23:14:42 +000048#include <stdlib.h>
John Kessenicha0af4732012-12-12 21:15:54 +000049#include <math.h>
50
John Kessenich2b07c7e2013-07-31 18:44:13 +000051#include "osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000052
53extern "C" {
54 SH_IMPORT_EXPORT void ShOutputHtml();
55}
56
John Kessenich94a81fb2013-08-31 02:41:30 +000057// Command-line options
58enum TOptions {
John Kessenichacba7722015-03-04 03:48:38 +000059 EOptionNone = 0x0000,
60 EOptionIntermediate = 0x0001,
61 EOptionSuppressInfolog = 0x0002,
62 EOptionMemoryLeakMode = 0x0004,
63 EOptionRelaxedErrors = 0x0008,
64 EOptionGiveWarnings = 0x0010,
65 EOptionLinkProgram = 0x0020,
66 EOptionMultiThreaded = 0x0040,
67 EOptionDumpConfig = 0x0080,
68 EOptionDumpReflection = 0x0100,
69 EOptionSuppressWarnings = 0x0200,
70 EOptionDumpVersions = 0x0400,
71 EOptionSpv = 0x0800,
72 EOptionHumanReadableSpv = 0x1000,
73 EOptionDefaultDesktop = 0x2000,
John Kessenichc555ddd2015-06-17 02:38:44 +000074 EOptionOutputPreprocessed = 0x4000,
John Kessenich94a81fb2013-08-31 02:41:30 +000075};
76
John Kessenicha0af4732012-12-12 21:15:54 +000077//
78// Return codes from main.
79//
80enum TFailCode {
81 ESuccess = 0,
82 EFailUsage,
83 EFailCompile,
84 EFailLink,
85 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000086 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000087 EFailLinkerCreate
88};
89
90//
91// Just placeholders for testing purposes. The stand-alone environment
92// can't actually do a full link without something specifying real
93// attribute bindings.
94//
95ShBinding FixedAttributeBindings[] = {
96 { "gl_Vertex", 15 },
97 { "gl_Color", 10 },
98 { "gl_Normal", 7 },
99};
100
101ShBindingTable FixedAttributeTable = { 3, FixedAttributeBindings };
102
John Kessenichb603f912013-08-29 00:39:25 +0000103EShLanguage FindLanguage(const std::string& name);
John Kessenich51cdd902014-02-18 23:37:57 +0000104void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000105void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000106void FreeFileData(char** data);
107char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000108void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000109
John Kessenichc999ba22013-11-07 23:33:24 +0000110// Globally track if any compile or link failure.
111bool CompileFailed = false;
112bool LinkFailed = false;
113
John Kessenich05a70632013-09-17 19:26:08 +0000114// Use to test breaking up a single shader file into multiple strings.
John Kessenichea869fb2013-10-28 18:12:06 +0000115int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000116
John Kessenich05a70632013-09-17 19:26:08 +0000117TBuiltInResource Resources;
118std::string ConfigFile;
119
John Kessenicha0af4732012-12-12 21:15:54 +0000120//
John Kessenich05a70632013-09-17 19:26:08 +0000121// These are the default resources for TBuiltInResources, used for both
122// - parsing this string for the case where the user didn't supply one
123// - dumping out a template for user construction of a config file
John Kessenicha0af4732012-12-12 21:15:54 +0000124//
John Kessenich284231c2013-10-22 01:50:39 +0000125const char* DefaultConfig =
126 "MaxLights 32\n"
127 "MaxClipPlanes 6\n"
128 "MaxTextureUnits 32\n"
129 "MaxTextureCoords 32\n"
130 "MaxVertexAttribs 64\n"
131 "MaxVertexUniformComponents 4096\n"
132 "MaxVaryingFloats 64\n"
133 "MaxVertexTextureImageUnits 32\n"
John Kessenich623833f2013-12-11 18:57:40 +0000134 "MaxCombinedTextureImageUnits 80\n"
John Kessenich284231c2013-10-22 01:50:39 +0000135 "MaxTextureImageUnits 32\n"
136 "MaxFragmentUniformComponents 4096\n"
137 "MaxDrawBuffers 32\n"
138 "MaxVertexUniformVectors 128\n"
139 "MaxVaryingVectors 8\n"
140 "MaxFragmentUniformVectors 16\n"
141 "MaxVertexOutputVectors 16\n"
142 "MaxFragmentInputVectors 15\n"
143 "MinProgramTexelOffset -8\n"
144 "MaxProgramTexelOffset 7\n"
145 "MaxClipDistances 8\n"
146 "MaxComputeWorkGroupCountX 65535\n"
147 "MaxComputeWorkGroupCountY 65535\n"
148 "MaxComputeWorkGroupCountZ 65535\n"
149 "MaxComputeWorkGroupSizeX 1024\n"
John Kessenichda66bc72014-08-19 20:32:48 +0000150 "MaxComputeWorkGroupSizeY 1024\n"
John Kessenich284231c2013-10-22 01:50:39 +0000151 "MaxComputeWorkGroupSizeZ 64\n"
152 "MaxComputeUniformComponents 1024\n"
153 "MaxComputeTextureImageUnits 16\n"
154 "MaxComputeImageUniforms 8\n"
155 "MaxComputeAtomicCounters 8\n"
156 "MaxComputeAtomicCounterBuffers 1\n"
157 "MaxVaryingComponents 60\n"
158 "MaxVertexOutputComponents 64\n"
159 "MaxGeometryInputComponents 64\n"
160 "MaxGeometryOutputComponents 128\n"
161 "MaxFragmentInputComponents 128\n"
162 "MaxImageUnits 8\n"
163 "MaxCombinedImageUnitsAndFragmentOutputs 8\n"
John Kessenichddea6782014-08-10 18:19:36 +0000164 "MaxCombinedShaderOutputResources 8\n"
John Kessenich284231c2013-10-22 01:50:39 +0000165 "MaxImageSamples 0\n"
166 "MaxVertexImageUniforms 0\n"
167 "MaxTessControlImageUniforms 0\n"
168 "MaxTessEvaluationImageUniforms 0\n"
169 "MaxGeometryImageUniforms 0\n"
170 "MaxFragmentImageUniforms 8\n"
171 "MaxCombinedImageUniforms 8\n"
172 "MaxGeometryTextureImageUnits 16\n"
173 "MaxGeometryOutputVertices 256\n"
174 "MaxGeometryTotalOutputComponents 1024\n"
175 "MaxGeometryUniformComponents 1024\n"
176 "MaxGeometryVaryingComponents 64\n"
177 "MaxTessControlInputComponents 128\n"
178 "MaxTessControlOutputComponents 128\n"
179 "MaxTessControlTextureImageUnits 16\n"
180 "MaxTessControlUniformComponents 1024\n"
181 "MaxTessControlTotalOutputComponents 4096\n"
182 "MaxTessEvaluationInputComponents 128\n"
183 "MaxTessEvaluationOutputComponents 128\n"
184 "MaxTessEvaluationTextureImageUnits 16\n"
185 "MaxTessEvaluationUniformComponents 1024\n"
186 "MaxTessPatchComponents 120\n"
187 "MaxPatchVertices 32\n"
188 "MaxTessGenLevel 64\n"
189 "MaxViewports 16\n"
190 "MaxVertexAtomicCounters 0\n"
191 "MaxTessControlAtomicCounters 0\n"
192 "MaxTessEvaluationAtomicCounters 0\n"
193 "MaxGeometryAtomicCounters 0\n"
194 "MaxFragmentAtomicCounters 8\n"
195 "MaxCombinedAtomicCounters 8\n"
196 "MaxAtomicCounterBindings 1\n"
197 "MaxVertexAtomicCounterBuffers 0\n"
198 "MaxTessControlAtomicCounterBuffers 0\n"
199 "MaxTessEvaluationAtomicCounterBuffers 0\n"
200 "MaxGeometryAtomicCounterBuffers 0\n"
201 "MaxFragmentAtomicCounterBuffers 1\n"
202 "MaxCombinedAtomicCounterBuffers 1\n"
203 "MaxAtomicCounterBufferSize 16384\n"
John Kessenichc7776ec2014-01-26 01:37:13 +0000204 "MaxTransformFeedbackBuffers 4\n"
205 "MaxTransformFeedbackInterleavedComponents 64\n"
John Kessenich69968412014-08-13 06:37:59 +0000206 "MaxCullDistances 8\n"
207 "MaxCombinedClipAndCullDistances 8\n"
John Kessenichcd77f8e2014-08-13 16:54:02 +0000208 "MaxSamples 4\n"
John Kessenich284231c2013-10-22 01:50:39 +0000209
210 "nonInductiveForLoops 1\n"
211 "whileLoops 1\n"
212 "doWhileLoops 1\n"
213 "generalUniformIndexing 1\n"
214 "generalAttributeMatrixVectorIndexing 1\n"
215 "generalVaryingIndexing 1\n"
216 "generalSamplerIndexing 1\n"
217 "generalVariableIndexing 1\n"
218 "generalConstantMatrixVectorIndexing 1\n"
219 ;
John Kessenich05a70632013-09-17 19:26:08 +0000220
221//
222// Parse either a .conf file provided by the user or the default string above.
223//
224void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000225{
John Kessenich05a70632013-09-17 19:26:08 +0000226 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000227 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000228 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000229 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000230 if (configStrings)
231 config = *configStrings;
232 else {
233 printf("Error opening configuration file; will instead use the default configuration\n");
234 usage();
235 }
236 }
237
238 if (config == 0) {
John Kessenich6494baf2014-02-19 00:08:59 +0000239 config = new char[strlen(DefaultConfig) + 1];
John Kessenich05a70632013-09-17 19:26:08 +0000240 strcpy(config, DefaultConfig);
241 }
242
243 const char* delims = " \t\n\r";
244 const char* token = strtok(config, delims);
245 while (token) {
246 const char* valueStr = strtok(0, delims);
247 if (valueStr == 0 || ! (valueStr[0] == '-' || (valueStr[0] >= '0' && valueStr[0] <= '9'))) {
248 printf("Error: '%s' bad .conf file. Each name must be followed by one number.\n", valueStr ? valueStr : "");
249 return;
250 }
251 int value = atoi(valueStr);
252
253 if (strcmp(token, "MaxLights") == 0)
254 Resources.maxLights = value;
255 else if (strcmp(token, "MaxClipPlanes") == 0)
256 Resources.maxClipPlanes = value;
257 else if (strcmp(token, "MaxTextureUnits") == 0)
258 Resources.maxTextureUnits = value;
259 else if (strcmp(token, "MaxTextureCoords") == 0)
260 Resources.maxTextureCoords = value;
261 else if (strcmp(token, "MaxVertexAttribs") == 0)
262 Resources.maxVertexAttribs = value;
263 else if (strcmp(token, "MaxVertexUniformComponents") == 0)
264 Resources.maxVertexUniformComponents = value;
265 else if (strcmp(token, "MaxVaryingFloats") == 0)
266 Resources.maxVaryingFloats = value;
267 else if (strcmp(token, "MaxVertexTextureImageUnits") == 0)
268 Resources.maxVertexTextureImageUnits = value;
269 else if (strcmp(token, "MaxCombinedTextureImageUnits") == 0)
270 Resources.maxCombinedTextureImageUnits = value;
271 else if (strcmp(token, "MaxTextureImageUnits") == 0)
272 Resources.maxTextureImageUnits = value;
273 else if (strcmp(token, "MaxFragmentUniformComponents") == 0)
274 Resources.maxFragmentUniformComponents = value;
275 else if (strcmp(token, "MaxDrawBuffers") == 0)
276 Resources.maxDrawBuffers = value;
277 else if (strcmp(token, "MaxVertexUniformVectors") == 0)
278 Resources.maxVertexUniformVectors = value;
279 else if (strcmp(token, "MaxVaryingVectors") == 0)
280 Resources.maxVaryingVectors = value;
281 else if (strcmp(token, "MaxFragmentUniformVectors") == 0)
282 Resources.maxFragmentUniformVectors = value;
283 else if (strcmp(token, "MaxVertexOutputVectors") == 0)
284 Resources.maxVertexOutputVectors = value;
285 else if (strcmp(token, "MaxFragmentInputVectors") == 0)
286 Resources.maxFragmentInputVectors = value;
287 else if (strcmp(token, "MinProgramTexelOffset") == 0)
288 Resources.minProgramTexelOffset = value;
289 else if (strcmp(token, "MaxProgramTexelOffset") == 0)
290 Resources.maxProgramTexelOffset = value;
John Kesseniche7c59c12013-10-16 22:28:35 +0000291 else if (strcmp(token, "MaxClipDistances") == 0)
292 Resources.maxClipDistances = value;
John Kessenich284231c2013-10-22 01:50:39 +0000293 else if (strcmp(token, "MaxComputeWorkGroupCountX") == 0)
294 Resources.maxComputeWorkGroupCountX = value;
295 else if (strcmp(token, "MaxComputeWorkGroupCountY") == 0)
296 Resources.maxComputeWorkGroupCountY = value;
297 else if (strcmp(token, "MaxComputeWorkGroupCountZ") == 0)
298 Resources.maxComputeWorkGroupCountZ = value;
299 else if (strcmp(token, "MaxComputeWorkGroupSizeX") == 0)
300 Resources.maxComputeWorkGroupSizeX = value;
301 else if (strcmp(token, "MaxComputeWorkGroupSizeY") == 0)
302 Resources.maxComputeWorkGroupSizeY = value;
303 else if (strcmp(token, "MaxComputeWorkGroupSizeZ") == 0)
304 Resources.maxComputeWorkGroupSizeZ = value;
305 else if (strcmp(token, "MaxComputeUniformComponents") == 0)
306 Resources.maxComputeUniformComponents = value;
307 else if (strcmp(token, "MaxComputeTextureImageUnits") == 0)
308 Resources.maxComputeTextureImageUnits = value;
309 else if (strcmp(token, "MaxComputeImageUniforms") == 0)
310 Resources.maxComputeImageUniforms = value;
311 else if (strcmp(token, "MaxComputeAtomicCounters") == 0)
312 Resources.maxComputeAtomicCounters = value;
313 else if (strcmp(token, "MaxComputeAtomicCounterBuffers") == 0)
314 Resources.maxComputeAtomicCounterBuffers = value;
315 else if (strcmp(token, "MaxVaryingComponents") == 0)
316 Resources.maxVaryingComponents = value;
317 else if (strcmp(token, "MaxVertexOutputComponents") == 0)
318 Resources.maxVertexOutputComponents = value;
319 else if (strcmp(token, "MaxGeometryInputComponents") == 0)
320 Resources.maxGeometryInputComponents = value;
321 else if (strcmp(token, "MaxGeometryOutputComponents") == 0)
322 Resources.maxGeometryOutputComponents = value;
323 else if (strcmp(token, "MaxFragmentInputComponents") == 0)
324 Resources.maxFragmentInputComponents = value;
325 else if (strcmp(token, "MaxImageUnits") == 0)
326 Resources.maxImageUnits = value;
327 else if (strcmp(token, "MaxCombinedImageUnitsAndFragmentOutputs") == 0)
328 Resources.maxCombinedImageUnitsAndFragmentOutputs = value;
John Kessenichddea6782014-08-10 18:19:36 +0000329 else if (strcmp(token, "MaxCombinedShaderOutputResources") == 0)
330 Resources.maxCombinedShaderOutputResources = value;
John Kessenich284231c2013-10-22 01:50:39 +0000331 else if (strcmp(token, "MaxImageSamples") == 0)
332 Resources.maxImageSamples = value;
333 else if (strcmp(token, "MaxVertexImageUniforms") == 0)
334 Resources.maxVertexImageUniforms = value;
335 else if (strcmp(token, "MaxTessControlImageUniforms") == 0)
336 Resources.maxTessControlImageUniforms = value;
337 else if (strcmp(token, "MaxTessEvaluationImageUniforms") == 0)
338 Resources.maxTessEvaluationImageUniforms = value;
339 else if (strcmp(token, "MaxGeometryImageUniforms") == 0)
340 Resources.maxGeometryImageUniforms = value;
341 else if (strcmp(token, "MaxFragmentImageUniforms") == 0)
342 Resources.maxFragmentImageUniforms = value;
343 else if (strcmp(token, "MaxCombinedImageUniforms") == 0)
344 Resources.maxCombinedImageUniforms = value;
345 else if (strcmp(token, "MaxGeometryTextureImageUnits") == 0)
346 Resources.maxGeometryTextureImageUnits = value;
347 else if (strcmp(token, "MaxGeometryOutputVertices") == 0)
348 Resources.maxGeometryOutputVertices = value;
349 else if (strcmp(token, "MaxGeometryTotalOutputComponents") == 0)
350 Resources.maxGeometryTotalOutputComponents = value;
351 else if (strcmp(token, "MaxGeometryUniformComponents") == 0)
352 Resources.maxGeometryUniformComponents = value;
353 else if (strcmp(token, "MaxGeometryVaryingComponents") == 0)
354 Resources.maxGeometryVaryingComponents = value;
355 else if (strcmp(token, "MaxTessControlInputComponents") == 0)
356 Resources.maxTessControlInputComponents = value;
357 else if (strcmp(token, "MaxTessControlOutputComponents") == 0)
358 Resources.maxTessControlOutputComponents = value;
359 else if (strcmp(token, "MaxTessControlTextureImageUnits") == 0)
360 Resources.maxTessControlTextureImageUnits = value;
361 else if (strcmp(token, "MaxTessControlUniformComponents") == 0)
362 Resources.maxTessControlUniformComponents = value;
363 else if (strcmp(token, "MaxTessControlTotalOutputComponents") == 0)
364 Resources.maxTessControlTotalOutputComponents = value;
365 else if (strcmp(token, "MaxTessEvaluationInputComponents") == 0)
366 Resources.maxTessEvaluationInputComponents = value;
367 else if (strcmp(token, "MaxTessEvaluationOutputComponents") == 0)
368 Resources.maxTessEvaluationOutputComponents = value;
369 else if (strcmp(token, "MaxTessEvaluationTextureImageUnits") == 0)
370 Resources.maxTessEvaluationTextureImageUnits = value;
371 else if (strcmp(token, "MaxTessEvaluationUniformComponents") == 0)
372 Resources.maxTessEvaluationUniformComponents = value;
373 else if (strcmp(token, "MaxTessPatchComponents") == 0)
374 Resources.maxTessPatchComponents = value;
375 else if (strcmp(token, "MaxPatchVertices") == 0)
376 Resources.maxPatchVertices = value;
377 else if (strcmp(token, "MaxTessGenLevel") == 0)
378 Resources.maxTessGenLevel = value;
379 else if (strcmp(token, "MaxViewports") == 0)
380 Resources.maxViewports = value;
381 else if (strcmp(token, "MaxVertexAtomicCounters") == 0)
382 Resources.maxVertexAtomicCounters = value;
383 else if (strcmp(token, "MaxTessControlAtomicCounters") == 0)
384 Resources.maxTessControlAtomicCounters = value;
385 else if (strcmp(token, "MaxTessEvaluationAtomicCounters") == 0)
386 Resources.maxTessEvaluationAtomicCounters = value;
387 else if (strcmp(token, "MaxGeometryAtomicCounters") == 0)
388 Resources.maxGeometryAtomicCounters = value;
389 else if (strcmp(token, "MaxFragmentAtomicCounters") == 0)
390 Resources.maxFragmentAtomicCounters = value;
391 else if (strcmp(token, "MaxCombinedAtomicCounters") == 0)
392 Resources.maxCombinedAtomicCounters = value;
393 else if (strcmp(token, "MaxAtomicCounterBindings") == 0)
394 Resources.maxAtomicCounterBindings = value;
395 else if (strcmp(token, "MaxVertexAtomicCounterBuffers") == 0)
396 Resources.maxVertexAtomicCounterBuffers = value;
397 else if (strcmp(token, "MaxTessControlAtomicCounterBuffers") == 0)
398 Resources.maxTessControlAtomicCounterBuffers = value;
399 else if (strcmp(token, "MaxTessEvaluationAtomicCounterBuffers") == 0)
400 Resources.maxTessEvaluationAtomicCounterBuffers = value;
401 else if (strcmp(token, "MaxGeometryAtomicCounterBuffers") == 0)
402 Resources.maxGeometryAtomicCounterBuffers = value;
403 else if (strcmp(token, "MaxFragmentAtomicCounterBuffers") == 0)
404 Resources.maxFragmentAtomicCounterBuffers = value;
405 else if (strcmp(token, "MaxCombinedAtomicCounterBuffers") == 0)
406 Resources.maxCombinedAtomicCounterBuffers = value;
407 else if (strcmp(token, "MaxAtomicCounterBufferSize") == 0)
408 Resources.maxAtomicCounterBufferSize = value;
John Kessenichc7776ec2014-01-26 01:37:13 +0000409 else if (strcmp(token, "MaxTransformFeedbackBuffers") == 0)
410 Resources.maxTransformFeedbackBuffers = value;
411 else if (strcmp(token, "MaxTransformFeedbackInterleavedComponents") == 0)
412 Resources.maxTransformFeedbackInterleavedComponents = value;
John Kessenich69968412014-08-13 06:37:59 +0000413 else if (strcmp(token, "MaxCullDistances") == 0)
414 Resources.maxCullDistances = value;
415 else if (strcmp(token, "MaxCombinedClipAndCullDistances") == 0)
416 Resources.maxCombinedClipAndCullDistances = value;
John Kessenichcd77f8e2014-08-13 16:54:02 +0000417 else if (strcmp(token, "MaxSamples") == 0)
418 Resources.maxSamples = value;
John Kessenich284231c2013-10-22 01:50:39 +0000419
John Kessenicha5830df2013-10-02 05:10:48 +0000420 else if (strcmp(token, "nonInductiveForLoops") == 0)
421 Resources.limits.nonInductiveForLoops = (value != 0);
422 else if (strcmp(token, "whileLoops") == 0)
423 Resources.limits.whileLoops = (value != 0);
424 else if (strcmp(token, "doWhileLoops") == 0)
425 Resources.limits.doWhileLoops = (value != 0);
426 else if (strcmp(token, "generalUniformIndexing") == 0)
427 Resources.limits.generalUniformIndexing = (value != 0);
428 else if (strcmp(token, "generalAttributeMatrixVectorIndexing") == 0)
429 Resources.limits.generalAttributeMatrixVectorIndexing = (value != 0);
430 else if (strcmp(token, "generalVaryingIndexing") == 0)
431 Resources.limits.generalVaryingIndexing = (value != 0);
432 else if (strcmp(token, "generalSamplerIndexing") == 0)
433 Resources.limits.generalSamplerIndexing = (value != 0);
434 else if (strcmp(token, "generalVariableIndexing") == 0)
435 Resources.limits.generalVariableIndexing = (value != 0);
436 else if (strcmp(token, "generalConstantMatrixVectorIndexing") == 0)
437 Resources.limits.generalConstantMatrixVectorIndexing = (value != 0);
John Kessenich05a70632013-09-17 19:26:08 +0000438 else
439 printf("Warning: unrecognized limit (%s) in configuration file.\n", token);
440
441 token = strtok(0, delims);
442 }
443 if (configStrings)
444 FreeFileData(configStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000445}
446
John Kessenich38f3b892013-09-06 19:52:57 +0000447// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000448glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000449
450// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000451glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000452int NumWorkItems = 0;
453
John Kessenich94a81fb2013-08-31 02:41:30 +0000454int Options = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000455const char* ExecutableName;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000456
John Kessenich05a70632013-09-17 19:26:08 +0000457//
458// *.conf => this is a config file that can set limits/resources
459//
460bool SetConfigFile(const std::string& name)
461{
462 if (name.size() < 5)
463 return false;
464
John Kessenich4c706852013-10-11 16:28:43 +0000465 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000466 ConfigFile = name;
467 return true;
468 }
469
470 return false;
471}
472
John Kessenich2b07c7e2013-07-31 18:44:13 +0000473bool ProcessArguments(int argc, char* argv[])
474{
John Kessenich38f3b892013-09-06 19:52:57 +0000475 ExecutableName = argv[0];
476 NumWorkItems = argc; // will include some empties where the '-' options were, but it doesn't matter, they'll be 0
John Kessenichfd305422014-06-05 16:30:53 +0000477 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich38f3b892013-09-06 19:52:57 +0000478 Work[0] = 0;
479
John Kessenich2b07c7e2013-07-31 18:44:13 +0000480 argc--;
481 argv++;
482 for (; argc >= 1; argc--, argv++) {
John Kessenich05a70632013-09-17 19:26:08 +0000483 Work[argc] = 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000484 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000485 switch (argv[0][1]) {
486 case 'H':
487 Options |= EOptionHumanReadableSpv;
488 // fall through to -V
John Kessenich0df0cde2015-03-03 17:09:43 +0000489 case 'V':
490 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000491 Options |= EOptionLinkProgram;
John Kessenich92f90382014-07-28 04:21:04 +0000492 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000493 case 'E':
494 Options |= EOptionOutputPreprocessed;
495 break;
John Kessenich05a70632013-09-17 19:26:08 +0000496 case 'c':
497 Options |= EOptionDumpConfig;
498 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000499 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000500 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000501 break;
John Kessenich05a70632013-09-17 19:26:08 +0000502 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000503 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000504 break;
505 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000506 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000507 break;
508 case 'm':
509 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000510 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000511 case 'q':
512 Options |= EOptionDumpReflection;
513 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000514 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000515 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000516 break;
517 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000518 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000519 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000520 case 't':
521 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000522 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000523 #endif
524 break;
John Kessenich319de232013-12-04 04:43:40 +0000525 case 'v':
526 Options |= EOptionDumpVersions;
527 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000528 case 'w':
529 Options |= EOptionSuppressWarnings;
530 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000531 default:
John Kessenich2b07c7e2013-07-31 18:44:13 +0000532 return false;
533 }
John Kessenich38f3b892013-09-06 19:52:57 +0000534 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000535 std::string name(argv[0]);
536 if (! SetConfigFile(name)) {
537 Work[argc] = new glslang::TWorkItem(name);
538 Worklist.add(Work[argc]);
539 }
John Kessenich38f3b892013-09-06 19:52:57 +0000540 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000541 }
542
John Kessenichc555ddd2015-06-17 02:38:44 +0000543 // Make sure that -E is not specified alongside -V -H or -l.
544 if (Options & EOptionOutputPreprocessed &&
545 ((Options &
546 (EOptionSpv | EOptionHumanReadableSpv | EOptionLinkProgram)))) {
547 return false;
548 }
549
John Kessenich2b07c7e2013-07-31 18:44:13 +0000550 return true;
551}
552
John Kessenichb0a7eb52013-11-07 17:44:20 +0000553void SetMessageOptions(EShMessages& messages)
554{
555 if (Options & EOptionRelaxedErrors)
556 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
557 if (Options & EOptionIntermediate)
558 messages = (EShMessages)(messages | EShMsgAST);
559 if (Options & EOptionSuppressWarnings)
560 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
561}
562
John Kessenich69f4b512013-09-04 21:19:27 +0000563// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000564//
565// Return 0 for failure, 1 for success.
566//
John Kessenichee6a9c82013-07-31 23:19:17 +0000567unsigned int
568#ifdef _WIN32
569 __stdcall
570#endif
John Kessenich94a81fb2013-08-31 02:41:30 +0000571CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000572{
John Kessenich38f3b892013-09-06 19:52:57 +0000573 glslang::TWorkItem* workItem;
574 while (Worklist.remove(workItem)) {
575 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000576 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000577 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000578
John Kessenichb0a7eb52013-11-07 17:44:20 +0000579 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000580
John Kessenich94a81fb2013-08-31 02:41:30 +0000581 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000582 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000583
584 ShDestruct(compiler);
585 }
586
587 return 0;
588}
589
John Kessenichacba7722015-03-04 03:48:38 +0000590const char* GlslStd450DebugNames[GLSL_STD_450::Count];
591
John Kessenich69f4b512013-09-04 21:19:27 +0000592//
593// For linking mode: Will independently parse each item in the worklist, but then put them
594// in the same program and link them together.
595//
596// Uses the new C++ interface instead of the old handle-based interface.
597//
598void CompileAndLinkShaders()
599{
600 // keep track of what to free
601 std::list<glslang::TShader*> shaders;
John Kessenichbf63ef02013-11-14 00:16:43 +0000602
John Kessenich69f4b512013-09-04 21:19:27 +0000603 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000604 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000605
John Kessenich69f4b512013-09-04 21:19:27 +0000606 //
607 // Per-shader processing...
608 //
609
John Kessenich5b0f13a2013-11-01 03:08:40 +0000610 glslang::TProgram& program = *new glslang::TProgram;
John Kessenich38f3b892013-09-06 19:52:57 +0000611 glslang::TWorkItem* workItem;
612 while (Worklist.remove(workItem)) {
613 EShLanguage stage = FindLanguage(workItem->name);
John Kessenich69f4b512013-09-04 21:19:27 +0000614 glslang::TShader* shader = new glslang::TShader(stage);
615 shaders.push_back(shader);
616
John Kessenich38f3b892013-09-06 19:52:57 +0000617 char** shaderStrings = ReadFileData(workItem->name.c_str());
John Kessenich69f4b512013-09-04 21:19:27 +0000618 if (! shaderStrings) {
619 usage();
620 return;
621 }
John Kessenichc555ddd2015-06-17 02:38:44 +0000622 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000623
624 shader->setStrings(shaderStrings, 1);
John Kessenichc555ddd2015-06-17 02:38:44 +0000625 if (Options & EOptionOutputPreprocessed) {
626 std::string str;
627 shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, messages, &str);
628 puts(str.c_str());
629 FreeFileData(shaderStrings);
630 continue;
631 }
632 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000633 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000634
John Kessenich69f4b512013-09-04 21:19:27 +0000635 program.addShader(shader);
636
637 if (! (Options & EOptionSuppressInfolog)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000638 puts(workItem->name.c_str());
John Kessenich69f4b512013-09-04 21:19:27 +0000639 puts(shader->getInfoLog());
640 puts(shader->getInfoDebugLog());
641 }
642
643 FreeFileData(shaderStrings);
644 }
645
646 //
647 // Program-level processing...
648 //
649
John Kessenichc555ddd2015-06-17 02:38:44 +0000650 if (!(Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000651 LinkFailed = true;
652
John Kessenich69f4b512013-09-04 21:19:27 +0000653 if (! (Options & EOptionSuppressInfolog)) {
654 puts(program.getInfoLog());
655 puts(program.getInfoDebugLog());
656 }
657
John Kessenich11f9fc72013-11-07 01:06:34 +0000658 if (Options & EOptionDumpReflection) {
659 program.buildReflection();
660 program.dumpReflection();
661 }
662
John Kessenich0df0cde2015-03-03 17:09:43 +0000663 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000664 if (CompileFailed || LinkFailed)
John Kessenich0df0cde2015-03-03 17:09:43 +0000665 printf("SPIRV is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000666 else {
667 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000668 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000669 std::vector<unsigned int> spirv;
670 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv);
John Kessenicha7a68a92014-08-24 18:21:00 +0000671 const char* name;
672 switch (stage) {
673 case EShLangVertex: name = "vert"; break;
674 case EShLangTessControl: name = "tesc"; break;
675 case EShLangTessEvaluation: name = "tese"; break;
676 case EShLangGeometry: name = "geom"; break;
677 case EShLangFragment: name = "frag"; break;
678 case EShLangCompute: name = "comp"; break;
679 default: name = "unknown"; break;
680 }
John Kessenichccc7acc2015-05-13 20:53:52 +0000681 glslang::OutputSpv(spirv, name);
John Kessenichacba7722015-03-04 03:48:38 +0000682 if (Options & EOptionHumanReadableSpv) {
683 spv::Parameterize();
684 GLSL_STD_450::GetDebugNames(GlslStd450DebugNames);
685 spv::Disassemble(std::cout, spirv);
686 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000687 }
John Kessenich92f90382014-07-28 04:21:04 +0000688 }
689 }
690 }
691
John Kessenich5b0f13a2013-11-01 03:08:40 +0000692 // Free everything up, program has to go before the shaders
693 // because it might have merged stuff from the shaders, and
694 // the stuff from the shaders has to have its destructors called
695 // before the pools holding the memory in the shaders is freed.
696 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000697 while (shaders.size() > 0) {
698 delete shaders.back();
699 shaders.pop_back();
700 }
John Kessenich69f4b512013-09-04 21:19:27 +0000701}
702
John Kessenicha0af4732012-12-12 21:15:54 +0000703int C_DECL main(int argc, char* argv[])
704{
John Kessenich54f6e562013-08-03 00:04:10 +0000705 if (! ProcessArguments(argc, argv)) {
706 usage();
John Kessenich2b07c7e2013-07-31 18:44:13 +0000707 return EFailUsage;
John Kessenich54f6e562013-08-03 00:04:10 +0000708 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000709
John Kessenich05a70632013-09-17 19:26:08 +0000710 if (Options & EOptionDumpConfig) {
711 printf("%s", DefaultConfig);
712 if (Worklist.empty())
713 return ESuccess;
714 }
715
John Kessenich319de232013-12-04 04:43:40 +0000716 if (Options & EOptionDumpVersions) {
717 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
718 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
719 if (Worklist.empty())
720 return ESuccess;
721 }
722
John Kessenich05a70632013-09-17 19:26:08 +0000723 if (Worklist.empty()) {
724 usage();
725 return EFailUsage;
726 }
727
728 ProcessConfigFile();
729
John Kessenich69f4b512013-09-04 21:19:27 +0000730 //
731 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000732 // 1) linking all arguments together, single-threaded, new C++ interface
733 // 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 +0000734 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000735 if (Options & EOptionLinkProgram ||
736 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000737 glslang::InitializeProcess();
John Kessenich38f3b892013-09-06 19:52:57 +0000738 CompileAndLinkShaders();
John Kessenichc36e1d82013-11-01 17:41:52 +0000739 glslang::FinalizeProcess();
740 } else {
741 ShInitialize();
742
John Kessenich38f3b892013-09-06 19:52:57 +0000743 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000744
John Kessenich38f3b892013-09-06 19:52:57 +0000745 if (Options & EOptionMultiThreaded) {
746 const int NumThreads = 16;
747 void* threads[NumThreads];
748 for (int t = 0; t < NumThreads; ++t) {
749 threads[t] = glslang::OS_CreateThread(&CompileShaders);
750 if (! threads[t]) {
751 printf("Failed to create thread\n");
752 return EFailThreadCreate;
753 }
John Kessenicha0af4732012-12-12 21:15:54 +0000754 }
John Kessenich38f3b892013-09-06 19:52:57 +0000755 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000756 } else
757 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000758
759 // Print out all the resulting infologs
760 for (int w = 0; w < NumWorkItems; ++w) {
761 if (Work[w]) {
762 if (printShaderNames)
763 puts(Work[w]->name.c_str());
764 puts(Work[w]->results.c_str());
765 delete Work[w];
766 }
767 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000768
769 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000770 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000771
John Kessenichc999ba22013-11-07 23:33:24 +0000772 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000773 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000774 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000775 return EFailLink;
776
777 return 0;
778}
779
780//
781// Deduce the language from the filename. Files must end in one of the
782// following extensions:
783//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000784// .vert = vertex
785// .tesc = tessellation control
786// .tese = tessellation evaluation
787// .geom = geometry
788// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000789// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000790//
John Kessenichb603f912013-08-29 00:39:25 +0000791EShLanguage FindLanguage(const std::string& name)
John Kessenicha0af4732012-12-12 21:15:54 +0000792{
John Kessenich2b07c7e2013-07-31 18:44:13 +0000793 size_t ext = name.rfind('.');
794 if (ext == std::string::npos) {
795 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000796 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000797 }
798
John Kessenich2b07c7e2013-07-31 18:44:13 +0000799 std::string suffix = name.substr(ext + 1, std::string::npos);
800 if (suffix == "vert")
801 return EShLangVertex;
802 else if (suffix == "tesc")
803 return EShLangTessControl;
804 else if (suffix == "tese")
805 return EShLangTessEvaluation;
806 else if (suffix == "geom")
807 return EShLangGeometry;
808 else if (suffix == "frag")
809 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000810 else if (suffix == "comp")
811 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000812
813 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000814 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000815}
816
John Kessenicha0af4732012-12-12 21:15:54 +0000817//
John Kessenich69f4b512013-09-04 21:19:27 +0000818// Read a file's data into a string, and compile it using the old interface ShCompile,
819// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000820//
John Kessenich51cdd902014-02-18 23:37:57 +0000821void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000822{
John Kessenichca3457f2015-05-18 01:59:45 +0000823 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000824 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000825 if (! shaderStrings) {
826 usage();
John Kessenichc999ba22013-11-07 23:33:24 +0000827 CompileFailed = true;
828 return;
John Kessenichdb4cd542013-06-26 22:42:55 +0000829 }
830
John Kessenich41cf6b52013-06-25 18:10:05 +0000831 int* lengths = new int[NumShaderStrings];
832
833 // move to length-based strings, rather than null-terminated strings
834 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000835 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000836
John Kessenichc999ba22013-11-07 23:33:24 +0000837 if (! shaderStrings) {
838 CompileFailed = true;
839 return;
840 }
John Kessenicha0af4732012-12-12 21:15:54 +0000841
John Kessenich52ac67e2013-05-05 23:46:22 +0000842 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000843 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000844
John Kessenich94a81fb2013-08-31 02:41:30 +0000845 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
846 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich26ad2682014-08-13 20:17:19 +0000847 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000848 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000849 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
850 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
851 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000852 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichca3457f2015-05-18 01:59:45 +0000853 //ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000854 }
John Kessenicha0af4732012-12-12 21:15:54 +0000855
John Kessenich94a81fb2013-08-31 02:41:30 +0000856 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000857 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000858 }
John Kessenicha0af4732012-12-12 21:15:54 +0000859
John Kessenich41cf6b52013-06-25 18:10:05 +0000860 delete [] lengths;
861 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000862
John Kessenichc999ba22013-11-07 23:33:24 +0000863 if (ret == 0)
864 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000865}
866
John Kessenicha0af4732012-12-12 21:15:54 +0000867//
868// print usage to stdout
869//
870void usage()
871{
John Kessenich319de232013-12-04 04:43:40 +0000872 printf("Usage: glslangValidator [option]... [file]...\n"
873 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000874 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich319de232013-12-04 04:43:40 +0000875 " .conf to provide an optional config file that replaces the default configuration\n"
John Kessenich05a70632013-09-17 19:26:08 +0000876 " (see -c option below for generating a template)\n"
John Kessenichc0275792013-08-09 17:14:49 +0000877 " .vert for a vertex shader\n"
878 " .tesc for a tessellation control shader\n"
879 " .tese for a tessellation evaluation shader\n"
880 " .geom for a geometry shader\n"
881 " .frag for a fragment shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000882 " .comp for a compute shader\n"
883 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000884 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000885 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000886 "To get other information, use one of the following options:\n"
John Kessenich319de232013-12-04 04:43:40 +0000887 "(Each option must be specified separately, but can go anywhere in the command line.)\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000888 " -V create SPIR-V in file <stage>.spv\n"
John Kessenichacba7722015-03-04 03:48:38 +0000889 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenichc555ddd2015-06-17 02:38:44 +0000890 " -E print pre-processed GLSL; cannot be used with -V, -H, or -l.\n"
John Kessenich319de232013-12-04 04:43:40 +0000891 " -c configuration dump; use to create default configuration file (redirect to a .conf file)\n"
John Kessenich26ad2682014-08-13 20:17:19 +0000892 " -d default to desktop (#version 110) when there is no version in the shader (default is ES version 100)\n"
John Kessenich319de232013-12-04 04:43:40 +0000893 " -i intermediate tree (glslang AST) is printed out\n"
894 " -l link validation of all input files\n"
895 " -m memory leak mode\n"
896 " -q dump reflection query database\n"
897 " -r relaxed semantic error-checking mode\n"
898 " -s silent mode\n"
899 " -t multi-threaded mode\n"
900 " -v print version strings\n"
901 " -w suppress warnings (except as required by #extension : warn)\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000902 );
John Kessenicha0af4732012-12-12 21:15:54 +0000903}
904
John Kessenich3ce4e592014-10-06 19:57:34 +0000905#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +0000906
907#include <errno.h>
908
909int fopen_s(
910 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +0000911 const char* filename,
912 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +0000913)
914{
915 if (!pFile || !filename || !mode) {
916 return EINVAL;
917 }
918
919 FILE* f = fopen(filename, mode);
920 if (! f) {
921 if (errno != 0) {
922 return errno;
923 } else {
924 return ENOENT;
925 }
926 }
927 *pFile = f;
928
929 return 0;
930}
931
932#endif
933
John Kessenicha0af4732012-12-12 21:15:54 +0000934//
935// Malloc a string of sufficient size and read a string into it.
936//
John Kessenich51cdd902014-02-18 23:37:57 +0000937char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +0000938{
John Kessenich200b2732012-12-12 21:21:23 +0000939 FILE *in;
John Kessenich3ce4e592014-10-06 19:57:34 +0000940 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +0000941
John Kessenicha0af4732012-12-12 21:15:54 +0000942 int count = 0;
John Kessenichcfd643e2013-03-08 23:14:42 +0000943 const int maxSourceStrings = 5;
John Kessenich6494baf2014-02-19 00:08:59 +0000944 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1));
John Kessenicha0af4732012-12-12 21:15:54 +0000945
John Kessenichd6c72a42014-08-18 19:42:35 +0000946 if (errorCode) {
John Kessenicha0af4732012-12-12 21:15:54 +0000947 printf("Error: unable to open input file: %s\n", fileName);
John Kessenichca3457f2015-05-18 01:59:45 +0000948 return nullptr;
John Kessenicha0af4732012-12-12 21:15:54 +0000949 }
950
951 while (fgetc(in) != EOF)
952 count++;
953
John Kessenichd6c72a42014-08-18 19:42:35 +0000954 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +0000955
956 char *fdata = (char*)malloc(count+2);
957 if (! fdata) {
John Kessenich585982e2014-03-28 17:49:10 +0000958 printf("Error allocating memory\n");
John Kessenichca3457f2015-05-18 01:59:45 +0000959 return nullptr;
John Kessenicha0af4732012-12-12 21:15:54 +0000960 }
John Kessenich93dfbe12015-05-15 17:30:55 +0000961 if ((int)fread(fdata,1,count, in) != count) {
John Kessenicha0af4732012-12-12 21:15:54 +0000962 printf("Error reading input file: %s\n", fileName);
John Kessenichca3457f2015-05-18 01:59:45 +0000963 return nullptr;
John Kessenicha0af4732012-12-12 21:15:54 +0000964 }
965 fdata[count] = '\0';
966 fclose(in);
John Kessenichea869fb2013-10-28 18:12:06 +0000967 if (count == 0) {
John Kessenicha0af4732012-12-12 21:15:54 +0000968 return_data[0]=(char*)malloc(count+2);
969 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +0000970 NumShaderStrings = 0;
John Kessenicha0af4732012-12-12 21:15:54 +0000971 return return_data;
John Kessenichea869fb2013-10-28 18:12:06 +0000972 } else
973 NumShaderStrings = 1;
John Kessenicha0af4732012-12-12 21:15:54 +0000974
John Kessenichd6c72a42014-08-18 19:42:35 +0000975 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000976 int ptr_len=0,i=0;
John Kessenichd6c72a42014-08-18 19:42:35 +0000977 while(count>0){
978 return_data[i]=(char*)malloc(len+2);
979 memcpy(return_data[i],fdata+ptr_len,len);
980 return_data[i][len]='\0';
981 count-=(len);
982 ptr_len+=(len);
983 if(count<len){
John Kessenicha0af4732012-12-12 21:15:54 +0000984 if(count==0){
John Kessenich41cf6b52013-06-25 18:10:05 +0000985 NumShaderStrings=(i+1);
John Kessenicha0af4732012-12-12 21:15:54 +0000986 break;
987 }
988 len = count;
John Kessenichd6c72a42014-08-18 19:42:35 +0000989 }
990 ++i;
991 }
John Kessenicha0af4732012-12-12 21:15:54 +0000992 return return_data;
993}
994
John Kessenich51cdd902014-02-18 23:37:57 +0000995void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +0000996{
John Kessenich41cf6b52013-06-25 18:10:05 +0000997 for(int i=0;i<NumShaderStrings;i++)
John Kessenicha0af4732012-12-12 21:15:54 +0000998 free(data[i]);
999}
1000
John Kessenich54d8cda2013-02-11 22:36:01 +00001001void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001002{
John Kessenichfae38ee2015-06-10 23:23:12 +00001003 if (num >= 0 )
1004 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1005 else
1006 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001007}