blob: f61389c5c4a4301322a61496761da6c2a86eda1c [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,
John Kessenich68d78fd2015-07-12 19:28:10 -060073 EOptionVulkanRules = 0x2000,
74 EOptionDefaultDesktop = 0x4000,
75 EOptionOutputPreprocessed = 0x8000,
John Kessenich94a81fb2013-08-31 02:41:30 +000076};
77
John Kessenicha0af4732012-12-12 21:15:54 +000078//
John Kessenich68d78fd2015-07-12 19:28:10 -060079// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000080//
81enum TFailCode {
82 ESuccess = 0,
83 EFailUsage,
84 EFailCompile,
85 EFailLink,
86 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000087 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000088 EFailLinkerCreate
89};
90
91//
John Kessenich68d78fd2015-07-12 19:28:10 -060092// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +000093//
John Kessenichb603f912013-08-29 00:39:25 +000094EShLanguage FindLanguage(const std::string& name);
John Kessenich51cdd902014-02-18 23:37:57 +000095void 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 Kessenichc999ba22013-11-07 23:33:24 +0000101// Globally track if any compile or link failure.
102bool CompileFailed = false;
103bool LinkFailed = false;
104
John Kessenich05a70632013-09-17 19:26:08 +0000105// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600106// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000107int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000108
John Kessenich05a70632013-09-17 19:26:08 +0000109TBuiltInResource Resources;
110std::string ConfigFile;
111
John Kessenicha0af4732012-12-12 21:15:54 +0000112//
John Kessenich05a70632013-09-17 19:26:08 +0000113// These are the default resources for TBuiltInResources, used for both
114// - parsing this string for the case where the user didn't supply one
115// - dumping out a template for user construction of a config file
John Kessenicha0af4732012-12-12 21:15:54 +0000116//
John Kessenich284231c2013-10-22 01:50:39 +0000117const char* DefaultConfig =
118 "MaxLights 32\n"
119 "MaxClipPlanes 6\n"
120 "MaxTextureUnits 32\n"
121 "MaxTextureCoords 32\n"
122 "MaxVertexAttribs 64\n"
123 "MaxVertexUniformComponents 4096\n"
124 "MaxVaryingFloats 64\n"
125 "MaxVertexTextureImageUnits 32\n"
John Kessenich623833f2013-12-11 18:57:40 +0000126 "MaxCombinedTextureImageUnits 80\n"
John Kessenich284231c2013-10-22 01:50:39 +0000127 "MaxTextureImageUnits 32\n"
128 "MaxFragmentUniformComponents 4096\n"
129 "MaxDrawBuffers 32\n"
130 "MaxVertexUniformVectors 128\n"
131 "MaxVaryingVectors 8\n"
132 "MaxFragmentUniformVectors 16\n"
133 "MaxVertexOutputVectors 16\n"
134 "MaxFragmentInputVectors 15\n"
135 "MinProgramTexelOffset -8\n"
136 "MaxProgramTexelOffset 7\n"
137 "MaxClipDistances 8\n"
138 "MaxComputeWorkGroupCountX 65535\n"
139 "MaxComputeWorkGroupCountY 65535\n"
140 "MaxComputeWorkGroupCountZ 65535\n"
141 "MaxComputeWorkGroupSizeX 1024\n"
John Kessenichda66bc72014-08-19 20:32:48 +0000142 "MaxComputeWorkGroupSizeY 1024\n"
John Kessenich284231c2013-10-22 01:50:39 +0000143 "MaxComputeWorkGroupSizeZ 64\n"
144 "MaxComputeUniformComponents 1024\n"
145 "MaxComputeTextureImageUnits 16\n"
146 "MaxComputeImageUniforms 8\n"
147 "MaxComputeAtomicCounters 8\n"
148 "MaxComputeAtomicCounterBuffers 1\n"
149 "MaxVaryingComponents 60\n"
150 "MaxVertexOutputComponents 64\n"
151 "MaxGeometryInputComponents 64\n"
152 "MaxGeometryOutputComponents 128\n"
153 "MaxFragmentInputComponents 128\n"
154 "MaxImageUnits 8\n"
155 "MaxCombinedImageUnitsAndFragmentOutputs 8\n"
John Kessenichddea6782014-08-10 18:19:36 +0000156 "MaxCombinedShaderOutputResources 8\n"
John Kessenich284231c2013-10-22 01:50:39 +0000157 "MaxImageSamples 0\n"
158 "MaxVertexImageUniforms 0\n"
159 "MaxTessControlImageUniforms 0\n"
160 "MaxTessEvaluationImageUniforms 0\n"
161 "MaxGeometryImageUniforms 0\n"
162 "MaxFragmentImageUniforms 8\n"
163 "MaxCombinedImageUniforms 8\n"
164 "MaxGeometryTextureImageUnits 16\n"
165 "MaxGeometryOutputVertices 256\n"
166 "MaxGeometryTotalOutputComponents 1024\n"
167 "MaxGeometryUniformComponents 1024\n"
168 "MaxGeometryVaryingComponents 64\n"
169 "MaxTessControlInputComponents 128\n"
170 "MaxTessControlOutputComponents 128\n"
171 "MaxTessControlTextureImageUnits 16\n"
172 "MaxTessControlUniformComponents 1024\n"
173 "MaxTessControlTotalOutputComponents 4096\n"
174 "MaxTessEvaluationInputComponents 128\n"
175 "MaxTessEvaluationOutputComponents 128\n"
176 "MaxTessEvaluationTextureImageUnits 16\n"
177 "MaxTessEvaluationUniformComponents 1024\n"
178 "MaxTessPatchComponents 120\n"
179 "MaxPatchVertices 32\n"
180 "MaxTessGenLevel 64\n"
181 "MaxViewports 16\n"
182 "MaxVertexAtomicCounters 0\n"
183 "MaxTessControlAtomicCounters 0\n"
184 "MaxTessEvaluationAtomicCounters 0\n"
185 "MaxGeometryAtomicCounters 0\n"
186 "MaxFragmentAtomicCounters 8\n"
187 "MaxCombinedAtomicCounters 8\n"
188 "MaxAtomicCounterBindings 1\n"
189 "MaxVertexAtomicCounterBuffers 0\n"
190 "MaxTessControlAtomicCounterBuffers 0\n"
191 "MaxTessEvaluationAtomicCounterBuffers 0\n"
192 "MaxGeometryAtomicCounterBuffers 0\n"
193 "MaxFragmentAtomicCounterBuffers 1\n"
194 "MaxCombinedAtomicCounterBuffers 1\n"
195 "MaxAtomicCounterBufferSize 16384\n"
John Kessenichc7776ec2014-01-26 01:37:13 +0000196 "MaxTransformFeedbackBuffers 4\n"
197 "MaxTransformFeedbackInterleavedComponents 64\n"
John Kessenich69968412014-08-13 06:37:59 +0000198 "MaxCullDistances 8\n"
199 "MaxCombinedClipAndCullDistances 8\n"
John Kessenichcd77f8e2014-08-13 16:54:02 +0000200 "MaxSamples 4\n"
John Kessenich284231c2013-10-22 01:50:39 +0000201
202 "nonInductiveForLoops 1\n"
203 "whileLoops 1\n"
204 "doWhileLoops 1\n"
205 "generalUniformIndexing 1\n"
206 "generalAttributeMatrixVectorIndexing 1\n"
207 "generalVaryingIndexing 1\n"
208 "generalSamplerIndexing 1\n"
209 "generalVariableIndexing 1\n"
210 "generalConstantMatrixVectorIndexing 1\n"
211 ;
John Kessenich05a70632013-09-17 19:26:08 +0000212
213//
214// Parse either a .conf file provided by the user or the default string above.
215//
216void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000217{
John Kessenich05a70632013-09-17 19:26:08 +0000218 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000219 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000220 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000221 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000222 if (configStrings)
223 config = *configStrings;
224 else {
225 printf("Error opening configuration file; will instead use the default configuration\n");
226 usage();
227 }
228 }
229
230 if (config == 0) {
John Kessenich6494baf2014-02-19 00:08:59 +0000231 config = new char[strlen(DefaultConfig) + 1];
John Kessenich05a70632013-09-17 19:26:08 +0000232 strcpy(config, DefaultConfig);
233 }
234
235 const char* delims = " \t\n\r";
236 const char* token = strtok(config, delims);
237 while (token) {
238 const char* valueStr = strtok(0, delims);
239 if (valueStr == 0 || ! (valueStr[0] == '-' || (valueStr[0] >= '0' && valueStr[0] <= '9'))) {
240 printf("Error: '%s' bad .conf file. Each name must be followed by one number.\n", valueStr ? valueStr : "");
241 return;
242 }
243 int value = atoi(valueStr);
244
245 if (strcmp(token, "MaxLights") == 0)
246 Resources.maxLights = value;
247 else if (strcmp(token, "MaxClipPlanes") == 0)
248 Resources.maxClipPlanes = value;
249 else if (strcmp(token, "MaxTextureUnits") == 0)
250 Resources.maxTextureUnits = value;
251 else if (strcmp(token, "MaxTextureCoords") == 0)
252 Resources.maxTextureCoords = value;
253 else if (strcmp(token, "MaxVertexAttribs") == 0)
254 Resources.maxVertexAttribs = value;
255 else if (strcmp(token, "MaxVertexUniformComponents") == 0)
256 Resources.maxVertexUniformComponents = value;
257 else if (strcmp(token, "MaxVaryingFloats") == 0)
258 Resources.maxVaryingFloats = value;
259 else if (strcmp(token, "MaxVertexTextureImageUnits") == 0)
260 Resources.maxVertexTextureImageUnits = value;
261 else if (strcmp(token, "MaxCombinedTextureImageUnits") == 0)
262 Resources.maxCombinedTextureImageUnits = value;
263 else if (strcmp(token, "MaxTextureImageUnits") == 0)
264 Resources.maxTextureImageUnits = value;
265 else if (strcmp(token, "MaxFragmentUniformComponents") == 0)
266 Resources.maxFragmentUniformComponents = value;
267 else if (strcmp(token, "MaxDrawBuffers") == 0)
268 Resources.maxDrawBuffers = value;
269 else if (strcmp(token, "MaxVertexUniformVectors") == 0)
270 Resources.maxVertexUniformVectors = value;
271 else if (strcmp(token, "MaxVaryingVectors") == 0)
272 Resources.maxVaryingVectors = value;
273 else if (strcmp(token, "MaxFragmentUniformVectors") == 0)
274 Resources.maxFragmentUniformVectors = value;
275 else if (strcmp(token, "MaxVertexOutputVectors") == 0)
276 Resources.maxVertexOutputVectors = value;
277 else if (strcmp(token, "MaxFragmentInputVectors") == 0)
278 Resources.maxFragmentInputVectors = value;
279 else if (strcmp(token, "MinProgramTexelOffset") == 0)
280 Resources.minProgramTexelOffset = value;
281 else if (strcmp(token, "MaxProgramTexelOffset") == 0)
282 Resources.maxProgramTexelOffset = value;
John Kesseniche7c59c12013-10-16 22:28:35 +0000283 else if (strcmp(token, "MaxClipDistances") == 0)
284 Resources.maxClipDistances = value;
John Kessenich284231c2013-10-22 01:50:39 +0000285 else if (strcmp(token, "MaxComputeWorkGroupCountX") == 0)
286 Resources.maxComputeWorkGroupCountX = value;
287 else if (strcmp(token, "MaxComputeWorkGroupCountY") == 0)
288 Resources.maxComputeWorkGroupCountY = value;
289 else if (strcmp(token, "MaxComputeWorkGroupCountZ") == 0)
290 Resources.maxComputeWorkGroupCountZ = value;
291 else if (strcmp(token, "MaxComputeWorkGroupSizeX") == 0)
292 Resources.maxComputeWorkGroupSizeX = value;
293 else if (strcmp(token, "MaxComputeWorkGroupSizeY") == 0)
294 Resources.maxComputeWorkGroupSizeY = value;
295 else if (strcmp(token, "MaxComputeWorkGroupSizeZ") == 0)
296 Resources.maxComputeWorkGroupSizeZ = value;
297 else if (strcmp(token, "MaxComputeUniformComponents") == 0)
298 Resources.maxComputeUniformComponents = value;
299 else if (strcmp(token, "MaxComputeTextureImageUnits") == 0)
300 Resources.maxComputeTextureImageUnits = value;
301 else if (strcmp(token, "MaxComputeImageUniforms") == 0)
302 Resources.maxComputeImageUniforms = value;
303 else if (strcmp(token, "MaxComputeAtomicCounters") == 0)
304 Resources.maxComputeAtomicCounters = value;
305 else if (strcmp(token, "MaxComputeAtomicCounterBuffers") == 0)
306 Resources.maxComputeAtomicCounterBuffers = value;
307 else if (strcmp(token, "MaxVaryingComponents") == 0)
308 Resources.maxVaryingComponents = value;
309 else if (strcmp(token, "MaxVertexOutputComponents") == 0)
310 Resources.maxVertexOutputComponents = value;
311 else if (strcmp(token, "MaxGeometryInputComponents") == 0)
312 Resources.maxGeometryInputComponents = value;
313 else if (strcmp(token, "MaxGeometryOutputComponents") == 0)
314 Resources.maxGeometryOutputComponents = value;
315 else if (strcmp(token, "MaxFragmentInputComponents") == 0)
316 Resources.maxFragmentInputComponents = value;
317 else if (strcmp(token, "MaxImageUnits") == 0)
318 Resources.maxImageUnits = value;
319 else if (strcmp(token, "MaxCombinedImageUnitsAndFragmentOutputs") == 0)
320 Resources.maxCombinedImageUnitsAndFragmentOutputs = value;
John Kessenichddea6782014-08-10 18:19:36 +0000321 else if (strcmp(token, "MaxCombinedShaderOutputResources") == 0)
322 Resources.maxCombinedShaderOutputResources = value;
John Kessenich284231c2013-10-22 01:50:39 +0000323 else if (strcmp(token, "MaxImageSamples") == 0)
324 Resources.maxImageSamples = value;
325 else if (strcmp(token, "MaxVertexImageUniforms") == 0)
326 Resources.maxVertexImageUniforms = value;
327 else if (strcmp(token, "MaxTessControlImageUniforms") == 0)
328 Resources.maxTessControlImageUniforms = value;
329 else if (strcmp(token, "MaxTessEvaluationImageUniforms") == 0)
330 Resources.maxTessEvaluationImageUniforms = value;
331 else if (strcmp(token, "MaxGeometryImageUniforms") == 0)
332 Resources.maxGeometryImageUniforms = value;
333 else if (strcmp(token, "MaxFragmentImageUniforms") == 0)
334 Resources.maxFragmentImageUniforms = value;
335 else if (strcmp(token, "MaxCombinedImageUniforms") == 0)
336 Resources.maxCombinedImageUniforms = value;
337 else if (strcmp(token, "MaxGeometryTextureImageUnits") == 0)
338 Resources.maxGeometryTextureImageUnits = value;
339 else if (strcmp(token, "MaxGeometryOutputVertices") == 0)
340 Resources.maxGeometryOutputVertices = value;
341 else if (strcmp(token, "MaxGeometryTotalOutputComponents") == 0)
342 Resources.maxGeometryTotalOutputComponents = value;
343 else if (strcmp(token, "MaxGeometryUniformComponents") == 0)
344 Resources.maxGeometryUniformComponents = value;
345 else if (strcmp(token, "MaxGeometryVaryingComponents") == 0)
346 Resources.maxGeometryVaryingComponents = value;
347 else if (strcmp(token, "MaxTessControlInputComponents") == 0)
348 Resources.maxTessControlInputComponents = value;
349 else if (strcmp(token, "MaxTessControlOutputComponents") == 0)
350 Resources.maxTessControlOutputComponents = value;
351 else if (strcmp(token, "MaxTessControlTextureImageUnits") == 0)
352 Resources.maxTessControlTextureImageUnits = value;
353 else if (strcmp(token, "MaxTessControlUniformComponents") == 0)
354 Resources.maxTessControlUniformComponents = value;
355 else if (strcmp(token, "MaxTessControlTotalOutputComponents") == 0)
356 Resources.maxTessControlTotalOutputComponents = value;
357 else if (strcmp(token, "MaxTessEvaluationInputComponents") == 0)
358 Resources.maxTessEvaluationInputComponents = value;
359 else if (strcmp(token, "MaxTessEvaluationOutputComponents") == 0)
360 Resources.maxTessEvaluationOutputComponents = value;
361 else if (strcmp(token, "MaxTessEvaluationTextureImageUnits") == 0)
362 Resources.maxTessEvaluationTextureImageUnits = value;
363 else if (strcmp(token, "MaxTessEvaluationUniformComponents") == 0)
364 Resources.maxTessEvaluationUniformComponents = value;
365 else if (strcmp(token, "MaxTessPatchComponents") == 0)
366 Resources.maxTessPatchComponents = value;
367 else if (strcmp(token, "MaxPatchVertices") == 0)
368 Resources.maxPatchVertices = value;
369 else if (strcmp(token, "MaxTessGenLevel") == 0)
370 Resources.maxTessGenLevel = value;
371 else if (strcmp(token, "MaxViewports") == 0)
372 Resources.maxViewports = value;
373 else if (strcmp(token, "MaxVertexAtomicCounters") == 0)
374 Resources.maxVertexAtomicCounters = value;
375 else if (strcmp(token, "MaxTessControlAtomicCounters") == 0)
376 Resources.maxTessControlAtomicCounters = value;
377 else if (strcmp(token, "MaxTessEvaluationAtomicCounters") == 0)
378 Resources.maxTessEvaluationAtomicCounters = value;
379 else if (strcmp(token, "MaxGeometryAtomicCounters") == 0)
380 Resources.maxGeometryAtomicCounters = value;
381 else if (strcmp(token, "MaxFragmentAtomicCounters") == 0)
382 Resources.maxFragmentAtomicCounters = value;
383 else if (strcmp(token, "MaxCombinedAtomicCounters") == 0)
384 Resources.maxCombinedAtomicCounters = value;
385 else if (strcmp(token, "MaxAtomicCounterBindings") == 0)
386 Resources.maxAtomicCounterBindings = value;
387 else if (strcmp(token, "MaxVertexAtomicCounterBuffers") == 0)
388 Resources.maxVertexAtomicCounterBuffers = value;
389 else if (strcmp(token, "MaxTessControlAtomicCounterBuffers") == 0)
390 Resources.maxTessControlAtomicCounterBuffers = value;
391 else if (strcmp(token, "MaxTessEvaluationAtomicCounterBuffers") == 0)
392 Resources.maxTessEvaluationAtomicCounterBuffers = value;
393 else if (strcmp(token, "MaxGeometryAtomicCounterBuffers") == 0)
394 Resources.maxGeometryAtomicCounterBuffers = value;
395 else if (strcmp(token, "MaxFragmentAtomicCounterBuffers") == 0)
396 Resources.maxFragmentAtomicCounterBuffers = value;
397 else if (strcmp(token, "MaxCombinedAtomicCounterBuffers") == 0)
398 Resources.maxCombinedAtomicCounterBuffers = value;
399 else if (strcmp(token, "MaxAtomicCounterBufferSize") == 0)
400 Resources.maxAtomicCounterBufferSize = value;
John Kessenichc7776ec2014-01-26 01:37:13 +0000401 else if (strcmp(token, "MaxTransformFeedbackBuffers") == 0)
402 Resources.maxTransformFeedbackBuffers = value;
403 else if (strcmp(token, "MaxTransformFeedbackInterleavedComponents") == 0)
404 Resources.maxTransformFeedbackInterleavedComponents = value;
John Kessenich69968412014-08-13 06:37:59 +0000405 else if (strcmp(token, "MaxCullDistances") == 0)
406 Resources.maxCullDistances = value;
407 else if (strcmp(token, "MaxCombinedClipAndCullDistances") == 0)
408 Resources.maxCombinedClipAndCullDistances = value;
John Kessenichcd77f8e2014-08-13 16:54:02 +0000409 else if (strcmp(token, "MaxSamples") == 0)
410 Resources.maxSamples = value;
John Kessenich284231c2013-10-22 01:50:39 +0000411
John Kessenicha5830df2013-10-02 05:10:48 +0000412 else if (strcmp(token, "nonInductiveForLoops") == 0)
413 Resources.limits.nonInductiveForLoops = (value != 0);
414 else if (strcmp(token, "whileLoops") == 0)
415 Resources.limits.whileLoops = (value != 0);
416 else if (strcmp(token, "doWhileLoops") == 0)
417 Resources.limits.doWhileLoops = (value != 0);
418 else if (strcmp(token, "generalUniformIndexing") == 0)
419 Resources.limits.generalUniformIndexing = (value != 0);
420 else if (strcmp(token, "generalAttributeMatrixVectorIndexing") == 0)
421 Resources.limits.generalAttributeMatrixVectorIndexing = (value != 0);
422 else if (strcmp(token, "generalVaryingIndexing") == 0)
423 Resources.limits.generalVaryingIndexing = (value != 0);
424 else if (strcmp(token, "generalSamplerIndexing") == 0)
425 Resources.limits.generalSamplerIndexing = (value != 0);
426 else if (strcmp(token, "generalVariableIndexing") == 0)
427 Resources.limits.generalVariableIndexing = (value != 0);
428 else if (strcmp(token, "generalConstantMatrixVectorIndexing") == 0)
429 Resources.limits.generalConstantMatrixVectorIndexing = (value != 0);
John Kessenich05a70632013-09-17 19:26:08 +0000430 else
431 printf("Warning: unrecognized limit (%s) in configuration file.\n", token);
432
433 token = strtok(0, delims);
434 }
435 if (configStrings)
436 FreeFileData(configStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000437}
438
John Kessenich38f3b892013-09-06 19:52:57 +0000439// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000440glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000441
442// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000443glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000444int NumWorkItems = 0;
445
John Kessenich94a81fb2013-08-31 02:41:30 +0000446int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600447const char* ExecutableName = nullptr;
448const char* binaryFileName = nullptr;
449
450//
451// Create the default name for saving a binary if -o is not provided.
452//
453const char* GetBinaryName(EShLanguage stage)
454{
455 const char* name;
456 if (binaryFileName == nullptr) {
457 switch (stage) {
458 case EShLangVertex: name = "vert.spv"; break;
459 case EShLangTessControl: name = "tesc.spv"; break;
460 case EShLangTessEvaluation: name = "tese.spv"; break;
461 case EShLangGeometry: name = "geom.spv"; break;
462 case EShLangFragment: name = "frag.spv"; break;
463 case EShLangCompute: name = "comp.spv"; break;
464 default: name = "unknown"; break;
465 }
466 } else
467 name = binaryFileName;
468
469 return name;
470}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000471
John Kessenich05a70632013-09-17 19:26:08 +0000472//
473// *.conf => this is a config file that can set limits/resources
474//
475bool SetConfigFile(const std::string& name)
476{
477 if (name.size() < 5)
478 return false;
479
John Kessenich4c706852013-10-11 16:28:43 +0000480 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000481 ConfigFile = name;
482 return true;
483 }
484
485 return false;
486}
487
John Kessenich68d78fd2015-07-12 19:28:10 -0600488//
489// Give error and exit with failure code.
490//
491void Error(const char* message)
492{
493 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
494 exit(EFailUsage);
495}
496
497//
498// Do all command-line argument parsing. This includes building up the work-items
499// to be processed later, and saving all the command-line options.
500//
501// Does not return (it exits) if command-line is fatally flawed.
502//
503void ProcessArguments(int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000504{
John Kessenich38f3b892013-09-06 19:52:57 +0000505 ExecutableName = argv[0];
506 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 +0000507 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich68d78fd2015-07-12 19:28:10 -0600508 for (int w = 0; w < NumWorkItems; ++w)
509 Work[w] = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000510
John Kessenich2b07c7e2013-07-31 18:44:13 +0000511 argc--;
512 argv++;
513 for (; argc >= 1; argc--, argv++) {
514 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000515 switch (argv[0][1]) {
516 case 'H':
517 Options |= EOptionHumanReadableSpv;
518 // fall through to -V
John Kessenich0df0cde2015-03-03 17:09:43 +0000519 case 'V':
520 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600521 Options |= EOptionVulkanRules;
522 Options |= EOptionLinkProgram;
523 break;
524 case 'G':
525 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000526 Options |= EOptionLinkProgram;
John Kessenich92f90382014-07-28 04:21:04 +0000527 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000528 case 'E':
529 Options |= EOptionOutputPreprocessed;
530 break;
John Kessenich05a70632013-09-17 19:26:08 +0000531 case 'c':
532 Options |= EOptionDumpConfig;
533 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000534 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000535 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000536 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600537 case 'h':
538 usage();
539 break;
John Kessenich05a70632013-09-17 19:26:08 +0000540 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000541 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000542 break;
543 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000544 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000545 break;
546 case 'm':
547 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000548 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600549 case 'o':
550 binaryFileName = argv[1];
551 if (argc > 0) {
552 argc--;
553 argv++;
554 } else
555 Error("no <file> provided for -o");
556 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000557 case 'q':
558 Options |= EOptionDumpReflection;
559 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000560 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000561 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000562 break;
563 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000564 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000565 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000566 case 't':
567 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000568 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000569 #endif
570 break;
John Kessenich319de232013-12-04 04:43:40 +0000571 case 'v':
572 Options |= EOptionDumpVersions;
573 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000574 case 'w':
575 Options |= EOptionSuppressWarnings;
576 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000577 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600578 usage();
579 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000580 }
John Kessenich38f3b892013-09-06 19:52:57 +0000581 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000582 std::string name(argv[0]);
583 if (! SetConfigFile(name)) {
584 Work[argc] = new glslang::TWorkItem(name);
585 Worklist.add(Work[argc]);
586 }
John Kessenich38f3b892013-09-06 19:52:57 +0000587 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000588 }
589
John Kessenich68d78fd2015-07-12 19:28:10 -0600590 // Make sure that -E is not specified alongside linking (which includes SPV generation)
591 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
592 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000593
John Kessenich68d78fd2015-07-12 19:28:10 -0600594 // -o makes no sense if there is no target binary
595 if (binaryFileName && (Options & EOptionSpv) == 0)
596 Error("no binary generation requested (e.g., -V)");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000597}
598
John Kessenich68d78fd2015-07-12 19:28:10 -0600599//
600// Translate the meaningful subset of command-line options to parser-behavior options.
601//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000602void SetMessageOptions(EShMessages& messages)
603{
604 if (Options & EOptionRelaxedErrors)
605 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
606 if (Options & EOptionIntermediate)
607 messages = (EShMessages)(messages | EShMsgAST);
608 if (Options & EOptionSuppressWarnings)
609 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600610 if (Options & EOptionSpv)
611 messages = (EShMessages)(messages | EShMsgSpvRules);
612 if (Options & EOptionVulkanRules)
613 messages = (EShMessages)(messages | EShMsgVulkanRules);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000614}
615
John Kessenich68d78fd2015-07-12 19:28:10 -0600616//
John Kessenich69f4b512013-09-04 21:19:27 +0000617// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000618//
619// Return 0 for failure, 1 for success.
620//
John Kessenichee6a9c82013-07-31 23:19:17 +0000621unsigned int
622#ifdef _WIN32
623 __stdcall
624#endif
John Kessenich94a81fb2013-08-31 02:41:30 +0000625CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000626{
John Kessenich38f3b892013-09-06 19:52:57 +0000627 glslang::TWorkItem* workItem;
628 while (Worklist.remove(workItem)) {
629 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000630 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000631 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000632
John Kessenichb0a7eb52013-11-07 17:44:20 +0000633 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000634
John Kessenich94a81fb2013-08-31 02:41:30 +0000635 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000636 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000637
638 ShDestruct(compiler);
639 }
640
641 return 0;
642}
643
John Kessenichacba7722015-03-04 03:48:38 +0000644const char* GlslStd450DebugNames[GLSL_STD_450::Count];
645
John Kessenich6626cad2015-06-19 05:14:19 +0000646// Outputs the given string, but only if it is non-null and non-empty.
647// This prevents erroneous newlines from appearing.
648void puts_if_non_empty(const char* str)
649{
650 if (str && str[0]) {
651 puts(str);
652 }
653}
654
John Kessenich69f4b512013-09-04 21:19:27 +0000655//
656// For linking mode: Will independently parse each item in the worklist, but then put them
657// in the same program and link them together.
658//
659// Uses the new C++ interface instead of the old handle-based interface.
660//
661void CompileAndLinkShaders()
662{
663 // keep track of what to free
664 std::list<glslang::TShader*> shaders;
John Kessenichbf63ef02013-11-14 00:16:43 +0000665
John Kessenich69f4b512013-09-04 21:19:27 +0000666 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000667 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000668
John Kessenich69f4b512013-09-04 21:19:27 +0000669 //
670 // Per-shader processing...
671 //
672
John Kessenich5b0f13a2013-11-01 03:08:40 +0000673 glslang::TProgram& program = *new glslang::TProgram;
John Kessenich38f3b892013-09-06 19:52:57 +0000674 glslang::TWorkItem* workItem;
675 while (Worklist.remove(workItem)) {
676 EShLanguage stage = FindLanguage(workItem->name);
John Kessenich69f4b512013-09-04 21:19:27 +0000677 glslang::TShader* shader = new glslang::TShader(stage);
678 shaders.push_back(shader);
679
John Kessenich38f3b892013-09-06 19:52:57 +0000680 char** shaderStrings = ReadFileData(workItem->name.c_str());
John Kessenich69f4b512013-09-04 21:19:27 +0000681 if (! shaderStrings) {
682 usage();
John Kessenichb3297152015-07-11 18:01:03 -0600683 delete &program;
684
John Kessenich69f4b512013-09-04 21:19:27 +0000685 return;
686 }
John Kessenichc555ddd2015-06-17 02:38:44 +0000687 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000688
689 shader->setStrings(shaderStrings, 1);
John Kessenichc555ddd2015-06-17 02:38:44 +0000690 if (Options & EOptionOutputPreprocessed) {
691 std::string str;
692 shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, messages, &str);
693 puts(str.c_str());
694 FreeFileData(shaderStrings);
695 continue;
696 }
697 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000698 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000699
John Kessenich69f4b512013-09-04 21:19:27 +0000700 program.addShader(shader);
701
702 if (! (Options & EOptionSuppressInfolog)) {
John Kessenich6626cad2015-06-19 05:14:19 +0000703 puts_if_non_empty(workItem->name.c_str());
704 puts_if_non_empty(shader->getInfoLog());
705 puts_if_non_empty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000706 }
707
708 FreeFileData(shaderStrings);
709 }
710
711 //
712 // Program-level processing...
713 //
714
John Kessenich68d78fd2015-07-12 19:28:10 -0600715 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000716 LinkFailed = true;
717
John Kessenich69f4b512013-09-04 21:19:27 +0000718 if (! (Options & EOptionSuppressInfolog)) {
John Kessenich6626cad2015-06-19 05:14:19 +0000719 puts_if_non_empty(program.getInfoLog());
720 puts_if_non_empty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000721 }
722
John Kessenich11f9fc72013-11-07 01:06:34 +0000723 if (Options & EOptionDumpReflection) {
724 program.buildReflection();
725 program.dumpReflection();
726 }
727
John Kessenich0df0cde2015-03-03 17:09:43 +0000728 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000729 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600730 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000731 else {
732 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000733 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000734 std::vector<unsigned int> spirv;
735 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv);
John Kessenich68d78fd2015-07-12 19:28:10 -0600736 glslang::OutputSpv(spirv, GetBinaryName((EShLanguage)stage));
John Kessenichacba7722015-03-04 03:48:38 +0000737 if (Options & EOptionHumanReadableSpv) {
738 spv::Parameterize();
739 GLSL_STD_450::GetDebugNames(GlslStd450DebugNames);
740 spv::Disassemble(std::cout, spirv);
741 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000742 }
John Kessenich92f90382014-07-28 04:21:04 +0000743 }
744 }
745 }
746
John Kessenich5b0f13a2013-11-01 03:08:40 +0000747 // Free everything up, program has to go before the shaders
748 // because it might have merged stuff from the shaders, and
749 // the stuff from the shaders has to have its destructors called
750 // before the pools holding the memory in the shaders is freed.
751 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000752 while (shaders.size() > 0) {
753 delete shaders.back();
754 shaders.pop_back();
755 }
John Kessenich69f4b512013-09-04 21:19:27 +0000756}
757
John Kessenicha0af4732012-12-12 21:15:54 +0000758int C_DECL main(int argc, char* argv[])
759{
John Kessenich68d78fd2015-07-12 19:28:10 -0600760 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000761
John Kessenich05a70632013-09-17 19:26:08 +0000762 if (Options & EOptionDumpConfig) {
763 printf("%s", DefaultConfig);
764 if (Worklist.empty())
765 return ESuccess;
766 }
767
John Kessenich319de232013-12-04 04:43:40 +0000768 if (Options & EOptionDumpVersions) {
769 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
770 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600771 std::string spirvVersion;
772 glslang::GetSpirvVersion(spirvVersion);
773 printf("SPIR-V Version %s\n", spirvVersion.c_str()); // TODO: move to consume source-generated data
John Kessenich319de232013-12-04 04:43:40 +0000774 if (Worklist.empty())
775 return ESuccess;
776 }
777
John Kessenich05a70632013-09-17 19:26:08 +0000778 if (Worklist.empty()) {
779 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000780 }
781
782 ProcessConfigFile();
783
John Kessenich69f4b512013-09-04 21:19:27 +0000784 //
785 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000786 // 1) linking all arguments together, single-threaded, new C++ interface
787 // 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 +0000788 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000789 if (Options & EOptionLinkProgram ||
790 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000791 glslang::InitializeProcess();
John Kessenich38f3b892013-09-06 19:52:57 +0000792 CompileAndLinkShaders();
John Kessenichc36e1d82013-11-01 17:41:52 +0000793 glslang::FinalizeProcess();
794 } else {
795 ShInitialize();
796
John Kessenich38f3b892013-09-06 19:52:57 +0000797 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000798
John Kessenich38f3b892013-09-06 19:52:57 +0000799 if (Options & EOptionMultiThreaded) {
800 const int NumThreads = 16;
801 void* threads[NumThreads];
802 for (int t = 0; t < NumThreads; ++t) {
803 threads[t] = glslang::OS_CreateThread(&CompileShaders);
804 if (! threads[t]) {
805 printf("Failed to create thread\n");
806 return EFailThreadCreate;
807 }
John Kessenicha0af4732012-12-12 21:15:54 +0000808 }
John Kessenich38f3b892013-09-06 19:52:57 +0000809 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000810 } else
811 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000812
813 // Print out all the resulting infologs
814 for (int w = 0; w < NumWorkItems; ++w) {
815 if (Work[w]) {
816 if (printShaderNames)
John Kessenich6626cad2015-06-19 05:14:19 +0000817 puts_if_non_empty(Work[w]->name.c_str());
818 puts_if_non_empty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000819 delete Work[w];
820 }
821 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000822
823 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000824 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000825
John Kessenichc999ba22013-11-07 23:33:24 +0000826 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000827 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000828 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000829 return EFailLink;
830
831 return 0;
832}
833
834//
835// Deduce the language from the filename. Files must end in one of the
836// following extensions:
837//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000838// .vert = vertex
839// .tesc = tessellation control
840// .tese = tessellation evaluation
841// .geom = geometry
842// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000843// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000844//
John Kessenichb603f912013-08-29 00:39:25 +0000845EShLanguage FindLanguage(const std::string& name)
John Kessenicha0af4732012-12-12 21:15:54 +0000846{
John Kessenich2b07c7e2013-07-31 18:44:13 +0000847 size_t ext = name.rfind('.');
848 if (ext == std::string::npos) {
849 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000850 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000851 }
852
John Kessenich2b07c7e2013-07-31 18:44:13 +0000853 std::string suffix = name.substr(ext + 1, std::string::npos);
854 if (suffix == "vert")
855 return EShLangVertex;
856 else if (suffix == "tesc")
857 return EShLangTessControl;
858 else if (suffix == "tese")
859 return EShLangTessEvaluation;
860 else if (suffix == "geom")
861 return EShLangGeometry;
862 else if (suffix == "frag")
863 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000864 else if (suffix == "comp")
865 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000866
867 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000868 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000869}
870
John Kessenicha0af4732012-12-12 21:15:54 +0000871//
John Kessenich69f4b512013-09-04 21:19:27 +0000872// Read a file's data into a string, and compile it using the old interface ShCompile,
873// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000874//
John Kessenich51cdd902014-02-18 23:37:57 +0000875void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000876{
John Kessenichca3457f2015-05-18 01:59:45 +0000877 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000878 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000879 if (! shaderStrings) {
880 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000881 }
882
John Kessenich41cf6b52013-06-25 18:10:05 +0000883 int* lengths = new int[NumShaderStrings];
884
885 // move to length-based strings, rather than null-terminated strings
886 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000887 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000888
John Kessenichc999ba22013-11-07 23:33:24 +0000889 if (! shaderStrings) {
890 CompileFailed = true;
891 return;
892 }
John Kessenicha0af4732012-12-12 21:15:54 +0000893
John Kessenich52ac67e2013-05-05 23:46:22 +0000894 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000895 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000896
John Kessenich94a81fb2013-08-31 02:41:30 +0000897 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
898 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich26ad2682014-08-13 20:17:19 +0000899 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000900 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000901 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
902 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
903 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000904 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichca3457f2015-05-18 01:59:45 +0000905 //ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000906 }
John Kessenicha0af4732012-12-12 21:15:54 +0000907
John Kessenich94a81fb2013-08-31 02:41:30 +0000908 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000909 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000910 }
John Kessenicha0af4732012-12-12 21:15:54 +0000911
John Kessenich41cf6b52013-06-25 18:10:05 +0000912 delete [] lengths;
913 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000914
John Kessenichc999ba22013-11-07 23:33:24 +0000915 if (ret == 0)
916 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000917}
918
John Kessenicha0af4732012-12-12 21:15:54 +0000919//
920// print usage to stdout
921//
922void usage()
923{
John Kessenich319de232013-12-04 04:43:40 +0000924 printf("Usage: glslangValidator [option]... [file]...\n"
925 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000926 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600927 " .conf to provide an optional config file that replaces the default configuration\n"
928 " (see -c option below for generating a template)\n"
929 " .vert for a vertex shader\n"
930 " .tesc for a tessellation control shader\n"
931 " .tese for a tessellation evaluation shader\n"
932 " .geom for a geometry shader\n"
933 " .frag for a fragment shader\n"
934 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000935 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000936 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000937 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000938 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600939 "Each option must be specified separately.\n"
940 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
941 " default file name is <stage>.spv (-o overrides this)\n"
942 " (unless -o is specified, which overrides the default file name)\n"
943 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
944 " default file name is <stage>.spv (-o overrides this)\n"
945 " -H print human readable form of SPIR-V; turns on -V\n"
946 " -E print pre-processed GLSL; cannot be used with -l.\n"
947 " -c configuration dump;\n"
948 " creates the default configuration file (redirect to a .conf file)\n"
949 " -d default to desktop (#version 110) when there is no shader #version\n"
950 " (default is ES version 100)\n"
951 " -h print this usage message\n"
952 " -i intermediate tree (glslang AST) is printed out\n"
953 " -l link all input files together to form a single module\n"
954 " -m memory leak mode\n"
955 " -o <file> save binary into <file>, requires a binary option (e.g., -V)\n"
956 " -q dump reflection query database\n"
957 " -r relaxed semantic error-checking mode\n"
958 " -s silent mode\n"
959 " -t multi-threaded mode\n"
960 " -v print version strings\n"
961 " -w suppress warnings (except as required by #extension : warn)\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000962 );
John Kessenich68d78fd2015-07-12 19:28:10 -0600963
964 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +0000965}
966
John Kessenich3ce4e592014-10-06 19:57:34 +0000967#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +0000968
969#include <errno.h>
970
971int fopen_s(
972 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +0000973 const char* filename,
974 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +0000975)
976{
977 if (!pFile || !filename || !mode) {
978 return EINVAL;
979 }
980
981 FILE* f = fopen(filename, mode);
982 if (! f) {
983 if (errno != 0) {
984 return errno;
985 } else {
986 return ENOENT;
987 }
988 }
989 *pFile = f;
990
991 return 0;
992}
993
994#endif
995
John Kessenicha0af4732012-12-12 21:15:54 +0000996//
997// Malloc a string of sufficient size and read a string into it.
998//
John Kessenich51cdd902014-02-18 23:37:57 +0000999char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001000{
John Kessenichb3297152015-07-11 18:01:03 -06001001 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001002 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001003
John Kessenicha0af4732012-12-12 21:15:54 +00001004 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001005 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1006 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001007
John Kessenich68d78fd2015-07-12 19:28:10 -06001008 if (errorCode || in == nullptr)
1009 Error("unable to open input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001010
1011 while (fgetc(in) != EOF)
1012 count++;
1013
John Kessenichd6c72a42014-08-18 19:42:35 +00001014 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001015
John Kessenichb3297152015-07-11 18:01:03 -06001016 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001017 if (! fdata)
1018 Error("can't allocate memory");
1019
John Kessenichb3297152015-07-11 18:01:03 -06001020 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001021 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001022 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001023 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001024
John Kessenicha0af4732012-12-12 21:15:54 +00001025 fdata[count] = '\0';
1026 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001027
John Kessenichea869fb2013-10-28 18:12:06 +00001028 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001029 // recover from empty file
1030 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001031 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001032 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001033 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001034
John Kessenichb3297152015-07-11 18:01:03 -06001035 return return_data;
1036 } else
1037 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1038
1039 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001040 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001041 int ptr_len = 0;
1042 int i = 0;
1043 while (count > 0) {
1044 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1045 memcpy(return_data[i], fdata + ptr_len, len);
1046 return_data[i][len] = '\0';
1047 count -= len;
1048 ptr_len += len;
1049 if (count < len) {
1050 if (count == 0) {
1051 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001052 break;
1053 }
John Kessenichb3297152015-07-11 18:01:03 -06001054 len = count;
John Kessenichd6c72a42014-08-18 19:42:35 +00001055 }
1056 ++i;
1057 }
John Kessenichb3297152015-07-11 18:01:03 -06001058
1059 free(fdata);
1060
John Kessenicha0af4732012-12-12 21:15:54 +00001061 return return_data;
1062}
1063
John Kessenich51cdd902014-02-18 23:37:57 +00001064void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001065{
John Kessenichb3297152015-07-11 18:01:03 -06001066 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001067 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001068
1069 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001070}
1071
John Kessenich54d8cda2013-02-11 22:36:01 +00001072void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001073{
John Kessenichfae38ee2015-06-10 23:23:12 +00001074 if (num >= 0 )
1075 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1076 else
1077 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001078}