blob: 8d3f5cac8e58d25be8affde27f14a7c94c68fd4c [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 Kessenich92f90382014-07-28 04:21:04 +000043#include "../BIL/GlslangToBil.h"
John Kessenicha0af4732012-12-12 21:15:54 +000044#include <string.h>
John Kessenichcfd643e2013-03-08 23:14:42 +000045#include <stdlib.h>
John Kessenicha0af4732012-12-12 21:15:54 +000046#include <math.h>
47
John Kessenich2b07c7e2013-07-31 18:44:13 +000048#include "osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000049
50extern "C" {
51 SH_IMPORT_EXPORT void ShOutputHtml();
52}
53
John Kessenich94a81fb2013-08-31 02:41:30 +000054// Command-line options
55enum TOptions {
56 EOptionNone = 0x000,
57 EOptionIntermediate = 0x001,
58 EOptionSuppressInfolog = 0x002,
59 EOptionMemoryLeakMode = 0x004,
60 EOptionRelaxedErrors = 0x008,
61 EOptionGiveWarnings = 0x010,
62 EOptionsLinkProgram = 0x020,
John Kessenich38f3b892013-09-06 19:52:57 +000063 EOptionMultiThreaded = 0x040,
John Kessenich05a70632013-09-17 19:26:08 +000064 EOptionDumpConfig = 0x080,
John Kessenich11f9fc72013-11-07 01:06:34 +000065 EOptionDumpReflection = 0x100,
John Kessenichb0a7eb52013-11-07 17:44:20 +000066 EOptionSuppressWarnings = 0x200,
John Kessenich319de232013-12-04 04:43:40 +000067 EOptionDumpVersions = 0x400,
John Kessenich92f90382014-07-28 04:21:04 +000068 EOptionBil = 0x800,
John Kessenich94a81fb2013-08-31 02:41:30 +000069};
70
John Kessenicha0af4732012-12-12 21:15:54 +000071//
72// Return codes from main.
73//
74enum TFailCode {
75 ESuccess = 0,
76 EFailUsage,
77 EFailCompile,
78 EFailLink,
79 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000080 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000081 EFailLinkerCreate
82};
83
84//
85// Just placeholders for testing purposes. The stand-alone environment
86// can't actually do a full link without something specifying real
87// attribute bindings.
88//
89ShBinding FixedAttributeBindings[] = {
90 { "gl_Vertex", 15 },
91 { "gl_Color", 10 },
92 { "gl_Normal", 7 },
93};
94
95ShBindingTable FixedAttributeTable = { 3, FixedAttributeBindings };
96
John Kessenichb603f912013-08-29 00:39:25 +000097EShLanguage FindLanguage(const std::string& name);
John Kessenich51cdd902014-02-18 23:37:57 +000098void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +000099void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000100void FreeFileData(char** data);
101char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000102void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000103
John Kessenichc999ba22013-11-07 23:33:24 +0000104// Globally track if any compile or link failure.
105bool CompileFailed = false;
106bool LinkFailed = false;
107
John Kessenich05a70632013-09-17 19:26:08 +0000108// Use to test breaking up a single shader file into multiple strings.
John Kessenichea869fb2013-10-28 18:12:06 +0000109int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000110
John Kessenich05a70632013-09-17 19:26:08 +0000111TBuiltInResource Resources;
112std::string ConfigFile;
113
John Kessenicha0af4732012-12-12 21:15:54 +0000114//
John Kessenich05a70632013-09-17 19:26:08 +0000115// These are the default resources for TBuiltInResources, used for both
116// - parsing this string for the case where the user didn't supply one
117// - dumping out a template for user construction of a config file
John Kessenicha0af4732012-12-12 21:15:54 +0000118//
John Kessenich284231c2013-10-22 01:50:39 +0000119const char* DefaultConfig =
120 "MaxLights 32\n"
121 "MaxClipPlanes 6\n"
122 "MaxTextureUnits 32\n"
123 "MaxTextureCoords 32\n"
124 "MaxVertexAttribs 64\n"
125 "MaxVertexUniformComponents 4096\n"
126 "MaxVaryingFloats 64\n"
127 "MaxVertexTextureImageUnits 32\n"
John Kessenich623833f2013-12-11 18:57:40 +0000128 "MaxCombinedTextureImageUnits 80\n"
John Kessenich284231c2013-10-22 01:50:39 +0000129 "MaxTextureImageUnits 32\n"
130 "MaxFragmentUniformComponents 4096\n"
131 "MaxDrawBuffers 32\n"
132 "MaxVertexUniformVectors 128\n"
133 "MaxVaryingVectors 8\n"
134 "MaxFragmentUniformVectors 16\n"
135 "MaxVertexOutputVectors 16\n"
136 "MaxFragmentInputVectors 15\n"
137 "MinProgramTexelOffset -8\n"
138 "MaxProgramTexelOffset 7\n"
139 "MaxClipDistances 8\n"
140 "MaxComputeWorkGroupCountX 65535\n"
141 "MaxComputeWorkGroupCountY 65535\n"
142 "MaxComputeWorkGroupCountZ 65535\n"
143 "MaxComputeWorkGroupSizeX 1024\n"
144 "MaxComputeWorkGroupSizeX 1024\n"
145 "MaxComputeWorkGroupSizeZ 64\n"
146 "MaxComputeUniformComponents 1024\n"
147 "MaxComputeTextureImageUnits 16\n"
148 "MaxComputeImageUniforms 8\n"
149 "MaxComputeAtomicCounters 8\n"
150 "MaxComputeAtomicCounterBuffers 1\n"
151 "MaxVaryingComponents 60\n"
152 "MaxVertexOutputComponents 64\n"
153 "MaxGeometryInputComponents 64\n"
154 "MaxGeometryOutputComponents 128\n"
155 "MaxFragmentInputComponents 128\n"
156 "MaxImageUnits 8\n"
157 "MaxCombinedImageUnitsAndFragmentOutputs 8\n"
158 "MaxImageSamples 0\n"
159 "MaxVertexImageUniforms 0\n"
160 "MaxTessControlImageUniforms 0\n"
161 "MaxTessEvaluationImageUniforms 0\n"
162 "MaxGeometryImageUniforms 0\n"
163 "MaxFragmentImageUniforms 8\n"
164 "MaxCombinedImageUniforms 8\n"
165 "MaxGeometryTextureImageUnits 16\n"
166 "MaxGeometryOutputVertices 256\n"
167 "MaxGeometryTotalOutputComponents 1024\n"
168 "MaxGeometryUniformComponents 1024\n"
169 "MaxGeometryVaryingComponents 64\n"
170 "MaxTessControlInputComponents 128\n"
171 "MaxTessControlOutputComponents 128\n"
172 "MaxTessControlTextureImageUnits 16\n"
173 "MaxTessControlUniformComponents 1024\n"
174 "MaxTessControlTotalOutputComponents 4096\n"
175 "MaxTessEvaluationInputComponents 128\n"
176 "MaxTessEvaluationOutputComponents 128\n"
177 "MaxTessEvaluationTextureImageUnits 16\n"
178 "MaxTessEvaluationUniformComponents 1024\n"
179 "MaxTessPatchComponents 120\n"
180 "MaxPatchVertices 32\n"
181 "MaxTessGenLevel 64\n"
182 "MaxViewports 16\n"
183 "MaxVertexAtomicCounters 0\n"
184 "MaxTessControlAtomicCounters 0\n"
185 "MaxTessEvaluationAtomicCounters 0\n"
186 "MaxGeometryAtomicCounters 0\n"
187 "MaxFragmentAtomicCounters 8\n"
188 "MaxCombinedAtomicCounters 8\n"
189 "MaxAtomicCounterBindings 1\n"
190 "MaxVertexAtomicCounterBuffers 0\n"
191 "MaxTessControlAtomicCounterBuffers 0\n"
192 "MaxTessEvaluationAtomicCounterBuffers 0\n"
193 "MaxGeometryAtomicCounterBuffers 0\n"
194 "MaxFragmentAtomicCounterBuffers 1\n"
195 "MaxCombinedAtomicCounterBuffers 1\n"
196 "MaxAtomicCounterBufferSize 16384\n"
John Kessenichc7776ec2014-01-26 01:37:13 +0000197 "MaxTransformFeedbackBuffers 4\n"
198 "MaxTransformFeedbackInterleavedComponents 64\n"
John Kessenich284231c2013-10-22 01:50:39 +0000199
200 "nonInductiveForLoops 1\n"
201 "whileLoops 1\n"
202 "doWhileLoops 1\n"
203 "generalUniformIndexing 1\n"
204 "generalAttributeMatrixVectorIndexing 1\n"
205 "generalVaryingIndexing 1\n"
206 "generalSamplerIndexing 1\n"
207 "generalVariableIndexing 1\n"
208 "generalConstantMatrixVectorIndexing 1\n"
209 ;
John Kessenich05a70632013-09-17 19:26:08 +0000210
211//
212// Parse either a .conf file provided by the user or the default string above.
213//
214void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000215{
John Kessenich05a70632013-09-17 19:26:08 +0000216 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000217 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000218 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000219 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000220 if (configStrings)
221 config = *configStrings;
222 else {
223 printf("Error opening configuration file; will instead use the default configuration\n");
224 usage();
225 }
226 }
227
228 if (config == 0) {
John Kessenich6494baf2014-02-19 00:08:59 +0000229 config = new char[strlen(DefaultConfig) + 1];
John Kessenich05a70632013-09-17 19:26:08 +0000230 strcpy(config, DefaultConfig);
231 }
232
233 const char* delims = " \t\n\r";
234 const char* token = strtok(config, delims);
235 while (token) {
236 const char* valueStr = strtok(0, delims);
237 if (valueStr == 0 || ! (valueStr[0] == '-' || (valueStr[0] >= '0' && valueStr[0] <= '9'))) {
238 printf("Error: '%s' bad .conf file. Each name must be followed by one number.\n", valueStr ? valueStr : "");
239 return;
240 }
241 int value = atoi(valueStr);
242
243 if (strcmp(token, "MaxLights") == 0)
244 Resources.maxLights = value;
245 else if (strcmp(token, "MaxClipPlanes") == 0)
246 Resources.maxClipPlanes = value;
247 else if (strcmp(token, "MaxTextureUnits") == 0)
248 Resources.maxTextureUnits = value;
249 else if (strcmp(token, "MaxTextureCoords") == 0)
250 Resources.maxTextureCoords = value;
251 else if (strcmp(token, "MaxVertexAttribs") == 0)
252 Resources.maxVertexAttribs = value;
253 else if (strcmp(token, "MaxVertexUniformComponents") == 0)
254 Resources.maxVertexUniformComponents = value;
255 else if (strcmp(token, "MaxVaryingFloats") == 0)
256 Resources.maxVaryingFloats = value;
257 else if (strcmp(token, "MaxVertexTextureImageUnits") == 0)
258 Resources.maxVertexTextureImageUnits = value;
259 else if (strcmp(token, "MaxCombinedTextureImageUnits") == 0)
260 Resources.maxCombinedTextureImageUnits = value;
261 else if (strcmp(token, "MaxTextureImageUnits") == 0)
262 Resources.maxTextureImageUnits = value;
263 else if (strcmp(token, "MaxFragmentUniformComponents") == 0)
264 Resources.maxFragmentUniformComponents = value;
265 else if (strcmp(token, "MaxDrawBuffers") == 0)
266 Resources.maxDrawBuffers = value;
267 else if (strcmp(token, "MaxVertexUniformVectors") == 0)
268 Resources.maxVertexUniformVectors = value;
269 else if (strcmp(token, "MaxVaryingVectors") == 0)
270 Resources.maxVaryingVectors = value;
271 else if (strcmp(token, "MaxFragmentUniformVectors") == 0)
272 Resources.maxFragmentUniformVectors = value;
273 else if (strcmp(token, "MaxVertexOutputVectors") == 0)
274 Resources.maxVertexOutputVectors = value;
275 else if (strcmp(token, "MaxFragmentInputVectors") == 0)
276 Resources.maxFragmentInputVectors = value;
277 else if (strcmp(token, "MinProgramTexelOffset") == 0)
278 Resources.minProgramTexelOffset = value;
279 else if (strcmp(token, "MaxProgramTexelOffset") == 0)
280 Resources.maxProgramTexelOffset = value;
John Kesseniche7c59c12013-10-16 22:28:35 +0000281 else if (strcmp(token, "MaxClipDistances") == 0)
282 Resources.maxClipDistances = value;
John Kessenich284231c2013-10-22 01:50:39 +0000283 else if (strcmp(token, "MaxComputeWorkGroupCountX") == 0)
284 Resources.maxComputeWorkGroupCountX = value;
285 else if (strcmp(token, "MaxComputeWorkGroupCountY") == 0)
286 Resources.maxComputeWorkGroupCountY = value;
287 else if (strcmp(token, "MaxComputeWorkGroupCountZ") == 0)
288 Resources.maxComputeWorkGroupCountZ = value;
289 else if (strcmp(token, "MaxComputeWorkGroupSizeX") == 0)
290 Resources.maxComputeWorkGroupSizeX = value;
291 else if (strcmp(token, "MaxComputeWorkGroupSizeY") == 0)
292 Resources.maxComputeWorkGroupSizeY = value;
293 else if (strcmp(token, "MaxComputeWorkGroupSizeZ") == 0)
294 Resources.maxComputeWorkGroupSizeZ = value;
295 else if (strcmp(token, "MaxComputeUniformComponents") == 0)
296 Resources.maxComputeUniformComponents = value;
297 else if (strcmp(token, "MaxComputeTextureImageUnits") == 0)
298 Resources.maxComputeTextureImageUnits = value;
299 else if (strcmp(token, "MaxComputeImageUniforms") == 0)
300 Resources.maxComputeImageUniforms = value;
301 else if (strcmp(token, "MaxComputeAtomicCounters") == 0)
302 Resources.maxComputeAtomicCounters = value;
303 else if (strcmp(token, "MaxComputeAtomicCounterBuffers") == 0)
304 Resources.maxComputeAtomicCounterBuffers = value;
305 else if (strcmp(token, "MaxVaryingComponents") == 0)
306 Resources.maxVaryingComponents = value;
307 else if (strcmp(token, "MaxVertexOutputComponents") == 0)
308 Resources.maxVertexOutputComponents = value;
309 else if (strcmp(token, "MaxGeometryInputComponents") == 0)
310 Resources.maxGeometryInputComponents = value;
311 else if (strcmp(token, "MaxGeometryOutputComponents") == 0)
312 Resources.maxGeometryOutputComponents = value;
313 else if (strcmp(token, "MaxFragmentInputComponents") == 0)
314 Resources.maxFragmentInputComponents = value;
315 else if (strcmp(token, "MaxImageUnits") == 0)
316 Resources.maxImageUnits = value;
317 else if (strcmp(token, "MaxCombinedImageUnitsAndFragmentOutputs") == 0)
318 Resources.maxCombinedImageUnitsAndFragmentOutputs = value;
319 else if (strcmp(token, "MaxImageSamples") == 0)
320 Resources.maxImageSamples = value;
321 else if (strcmp(token, "MaxVertexImageUniforms") == 0)
322 Resources.maxVertexImageUniforms = value;
323 else if (strcmp(token, "MaxTessControlImageUniforms") == 0)
324 Resources.maxTessControlImageUniforms = value;
325 else if (strcmp(token, "MaxTessEvaluationImageUniforms") == 0)
326 Resources.maxTessEvaluationImageUniforms = value;
327 else if (strcmp(token, "MaxGeometryImageUniforms") == 0)
328 Resources.maxGeometryImageUniforms = value;
329 else if (strcmp(token, "MaxFragmentImageUniforms") == 0)
330 Resources.maxFragmentImageUniforms = value;
331 else if (strcmp(token, "MaxCombinedImageUniforms") == 0)
332 Resources.maxCombinedImageUniforms = value;
333 else if (strcmp(token, "MaxGeometryTextureImageUnits") == 0)
334 Resources.maxGeometryTextureImageUnits = value;
335 else if (strcmp(token, "MaxGeometryOutputVertices") == 0)
336 Resources.maxGeometryOutputVertices = value;
337 else if (strcmp(token, "MaxGeometryTotalOutputComponents") == 0)
338 Resources.maxGeometryTotalOutputComponents = value;
339 else if (strcmp(token, "MaxGeometryUniformComponents") == 0)
340 Resources.maxGeometryUniformComponents = value;
341 else if (strcmp(token, "MaxGeometryVaryingComponents") == 0)
342 Resources.maxGeometryVaryingComponents = value;
343 else if (strcmp(token, "MaxTessControlInputComponents") == 0)
344 Resources.maxTessControlInputComponents = value;
345 else if (strcmp(token, "MaxTessControlOutputComponents") == 0)
346 Resources.maxTessControlOutputComponents = value;
347 else if (strcmp(token, "MaxTessControlTextureImageUnits") == 0)
348 Resources.maxTessControlTextureImageUnits = value;
349 else if (strcmp(token, "MaxTessControlUniformComponents") == 0)
350 Resources.maxTessControlUniformComponents = value;
351 else if (strcmp(token, "MaxTessControlTotalOutputComponents") == 0)
352 Resources.maxTessControlTotalOutputComponents = value;
353 else if (strcmp(token, "MaxTessEvaluationInputComponents") == 0)
354 Resources.maxTessEvaluationInputComponents = value;
355 else if (strcmp(token, "MaxTessEvaluationOutputComponents") == 0)
356 Resources.maxTessEvaluationOutputComponents = value;
357 else if (strcmp(token, "MaxTessEvaluationTextureImageUnits") == 0)
358 Resources.maxTessEvaluationTextureImageUnits = value;
359 else if (strcmp(token, "MaxTessEvaluationUniformComponents") == 0)
360 Resources.maxTessEvaluationUniformComponents = value;
361 else if (strcmp(token, "MaxTessPatchComponents") == 0)
362 Resources.maxTessPatchComponents = value;
363 else if (strcmp(token, "MaxPatchVertices") == 0)
364 Resources.maxPatchVertices = value;
365 else if (strcmp(token, "MaxTessGenLevel") == 0)
366 Resources.maxTessGenLevel = value;
367 else if (strcmp(token, "MaxViewports") == 0)
368 Resources.maxViewports = value;
369 else if (strcmp(token, "MaxVertexAtomicCounters") == 0)
370 Resources.maxVertexAtomicCounters = value;
371 else if (strcmp(token, "MaxTessControlAtomicCounters") == 0)
372 Resources.maxTessControlAtomicCounters = value;
373 else if (strcmp(token, "MaxTessEvaluationAtomicCounters") == 0)
374 Resources.maxTessEvaluationAtomicCounters = value;
375 else if (strcmp(token, "MaxGeometryAtomicCounters") == 0)
376 Resources.maxGeometryAtomicCounters = value;
377 else if (strcmp(token, "MaxFragmentAtomicCounters") == 0)
378 Resources.maxFragmentAtomicCounters = value;
379 else if (strcmp(token, "MaxCombinedAtomicCounters") == 0)
380 Resources.maxCombinedAtomicCounters = value;
381 else if (strcmp(token, "MaxAtomicCounterBindings") == 0)
382 Resources.maxAtomicCounterBindings = value;
383 else if (strcmp(token, "MaxVertexAtomicCounterBuffers") == 0)
384 Resources.maxVertexAtomicCounterBuffers = value;
385 else if (strcmp(token, "MaxTessControlAtomicCounterBuffers") == 0)
386 Resources.maxTessControlAtomicCounterBuffers = value;
387 else if (strcmp(token, "MaxTessEvaluationAtomicCounterBuffers") == 0)
388 Resources.maxTessEvaluationAtomicCounterBuffers = value;
389 else if (strcmp(token, "MaxGeometryAtomicCounterBuffers") == 0)
390 Resources.maxGeometryAtomicCounterBuffers = value;
391 else if (strcmp(token, "MaxFragmentAtomicCounterBuffers") == 0)
392 Resources.maxFragmentAtomicCounterBuffers = value;
393 else if (strcmp(token, "MaxCombinedAtomicCounterBuffers") == 0)
394 Resources.maxCombinedAtomicCounterBuffers = value;
395 else if (strcmp(token, "MaxAtomicCounterBufferSize") == 0)
396 Resources.maxAtomicCounterBufferSize = value;
John Kessenichc7776ec2014-01-26 01:37:13 +0000397 else if (strcmp(token, "MaxTransformFeedbackBuffers") == 0)
398 Resources.maxTransformFeedbackBuffers = value;
399 else if (strcmp(token, "MaxTransformFeedbackInterleavedComponents") == 0)
400 Resources.maxTransformFeedbackInterleavedComponents = value;
John Kessenich284231c2013-10-22 01:50:39 +0000401
John Kessenicha5830df2013-10-02 05:10:48 +0000402 else if (strcmp(token, "nonInductiveForLoops") == 0)
403 Resources.limits.nonInductiveForLoops = (value != 0);
404 else if (strcmp(token, "whileLoops") == 0)
405 Resources.limits.whileLoops = (value != 0);
406 else if (strcmp(token, "doWhileLoops") == 0)
407 Resources.limits.doWhileLoops = (value != 0);
408 else if (strcmp(token, "generalUniformIndexing") == 0)
409 Resources.limits.generalUniformIndexing = (value != 0);
410 else if (strcmp(token, "generalAttributeMatrixVectorIndexing") == 0)
411 Resources.limits.generalAttributeMatrixVectorIndexing = (value != 0);
412 else if (strcmp(token, "generalVaryingIndexing") == 0)
413 Resources.limits.generalVaryingIndexing = (value != 0);
414 else if (strcmp(token, "generalSamplerIndexing") == 0)
415 Resources.limits.generalSamplerIndexing = (value != 0);
416 else if (strcmp(token, "generalVariableIndexing") == 0)
417 Resources.limits.generalVariableIndexing = (value != 0);
418 else if (strcmp(token, "generalConstantMatrixVectorIndexing") == 0)
419 Resources.limits.generalConstantMatrixVectorIndexing = (value != 0);
John Kessenich05a70632013-09-17 19:26:08 +0000420 else
421 printf("Warning: unrecognized limit (%s) in configuration file.\n", token);
422
423 token = strtok(0, delims);
424 }
425 if (configStrings)
426 FreeFileData(configStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000427}
428
John Kessenich38f3b892013-09-06 19:52:57 +0000429// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000430glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000431
432// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000433glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000434int NumWorkItems = 0;
435
John Kessenich94a81fb2013-08-31 02:41:30 +0000436int Options = 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000437bool Delay = false;
John Kessenich38f3b892013-09-06 19:52:57 +0000438const char* ExecutableName;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000439
John Kessenich05a70632013-09-17 19:26:08 +0000440//
441// *.conf => this is a config file that can set limits/resources
442//
443bool SetConfigFile(const std::string& name)
444{
445 if (name.size() < 5)
446 return false;
447
John Kessenich4c706852013-10-11 16:28:43 +0000448 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000449 ConfigFile = name;
450 return true;
451 }
452
453 return false;
454}
455
John Kessenich2b07c7e2013-07-31 18:44:13 +0000456bool ProcessArguments(int argc, char* argv[])
457{
John Kessenich38f3b892013-09-06 19:52:57 +0000458 ExecutableName = argv[0];
459 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 +0000460 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich38f3b892013-09-06 19:52:57 +0000461 Work[0] = 0;
462
John Kessenich2b07c7e2013-07-31 18:44:13 +0000463 argc--;
464 argv++;
465 for (; argc >= 1; argc--, argv++) {
John Kessenich05a70632013-09-17 19:26:08 +0000466 Work[argc] = 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000467 if (argv[0][0] == '-') {
468 switch (argv[0][1]) {
John Kessenich92f90382014-07-28 04:21:04 +0000469 case 'b':
470 Options |= EOptionBil;
471 break;
John Kessenich05a70632013-09-17 19:26:08 +0000472 case 'c':
473 Options |= EOptionDumpConfig;
474 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000475 case 'd':
476 Delay = true;
477 break;
John Kessenich05a70632013-09-17 19:26:08 +0000478 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000479 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000480 break;
481 case 'l':
John Kessenich94a81fb2013-08-31 02:41:30 +0000482 Options |= EOptionsLinkProgram;
483 break;
484 case 'm':
485 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000486 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000487 case 'q':
488 Options |= EOptionDumpReflection;
489 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000490 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000491 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000492 break;
493 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000494 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000495 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000496 case 't':
497 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000498 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000499 #endif
500 break;
John Kessenich319de232013-12-04 04:43:40 +0000501 case 'v':
502 Options |= EOptionDumpVersions;
503 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000504 case 'w':
505 Options |= EOptionSuppressWarnings;
506 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000507 default:
John Kessenich2b07c7e2013-07-31 18:44:13 +0000508 return false;
509 }
John Kessenich38f3b892013-09-06 19:52:57 +0000510 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000511 std::string name(argv[0]);
512 if (! SetConfigFile(name)) {
513 Work[argc] = new glslang::TWorkItem(name);
514 Worklist.add(Work[argc]);
515 }
John Kessenich38f3b892013-09-06 19:52:57 +0000516 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000517 }
518
519 return true;
520}
521
John Kessenichb0a7eb52013-11-07 17:44:20 +0000522void SetMessageOptions(EShMessages& messages)
523{
524 if (Options & EOptionRelaxedErrors)
525 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
526 if (Options & EOptionIntermediate)
527 messages = (EShMessages)(messages | EShMsgAST);
528 if (Options & EOptionSuppressWarnings)
529 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
530}
531
John Kessenich69f4b512013-09-04 21:19:27 +0000532// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000533//
534// Return 0 for failure, 1 for success.
535//
John Kessenichee6a9c82013-07-31 23:19:17 +0000536unsigned int
537#ifdef _WIN32
538 __stdcall
539#endif
John Kessenich94a81fb2013-08-31 02:41:30 +0000540CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000541{
John Kessenich38f3b892013-09-06 19:52:57 +0000542 glslang::TWorkItem* workItem;
543 while (Worklist.remove(workItem)) {
544 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000545 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000546 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000547
John Kessenichb0a7eb52013-11-07 17:44:20 +0000548 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000549
John Kessenich94a81fb2013-08-31 02:41:30 +0000550 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000551 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000552
553 ShDestruct(compiler);
554 }
555
556 return 0;
557}
558
John Kessenich69f4b512013-09-04 21:19:27 +0000559//
560// For linking mode: Will independently parse each item in the worklist, but then put them
561// in the same program and link them together.
562//
563// Uses the new C++ interface instead of the old handle-based interface.
564//
565void CompileAndLinkShaders()
566{
567 // keep track of what to free
568 std::list<glslang::TShader*> shaders;
John Kessenichbf63ef02013-11-14 00:16:43 +0000569
John Kessenich69f4b512013-09-04 21:19:27 +0000570 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000571 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000572
John Kessenich69f4b512013-09-04 21:19:27 +0000573 //
574 // Per-shader processing...
575 //
576
John Kessenich5b0f13a2013-11-01 03:08:40 +0000577 glslang::TProgram& program = *new glslang::TProgram;
John Kessenich38f3b892013-09-06 19:52:57 +0000578 glslang::TWorkItem* workItem;
579 while (Worklist.remove(workItem)) {
580 EShLanguage stage = FindLanguage(workItem->name);
John Kessenich69f4b512013-09-04 21:19:27 +0000581 glslang::TShader* shader = new glslang::TShader(stage);
582 shaders.push_back(shader);
583
John Kessenich38f3b892013-09-06 19:52:57 +0000584 char** shaderStrings = ReadFileData(workItem->name.c_str());
John Kessenich69f4b512013-09-04 21:19:27 +0000585 if (! shaderStrings) {
586 usage();
587 return;
588 }
589
590 shader->setStrings(shaderStrings, 1);
591
John Kessenichc999ba22013-11-07 23:33:24 +0000592 if (! shader->parse(&Resources, 100, false, messages))
593 CompileFailed = true;
John Kessenich69f4b512013-09-04 21:19:27 +0000594
595 program.addShader(shader);
596
597 if (! (Options & EOptionSuppressInfolog)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000598 puts(workItem->name.c_str());
John Kessenich69f4b512013-09-04 21:19:27 +0000599 puts(shader->getInfoLog());
600 puts(shader->getInfoDebugLog());
601 }
602
603 FreeFileData(shaderStrings);
604 }
605
606 //
607 // Program-level processing...
608 //
609
John Kessenichc999ba22013-11-07 23:33:24 +0000610 if (! program.link(messages))
611 LinkFailed = true;
612
John Kessenich69f4b512013-09-04 21:19:27 +0000613 if (! (Options & EOptionSuppressInfolog)) {
614 puts(program.getInfoLog());
615 puts(program.getInfoDebugLog());
616 }
617
John Kessenich11f9fc72013-11-07 01:06:34 +0000618 if (Options & EOptionDumpReflection) {
619 program.buildReflection();
620 program.dumpReflection();
621 }
622
John Kessenich92f90382014-07-28 04:21:04 +0000623 if (Options & EOptionBil) {
624 if (CompileFailed || LinkFailed)
625 printf("Bil is not generated for failed compile or link\n");
626 else {
627 for (int stage = 0; stage < EShLangCount; ++stage) {
628 if (program.getIntermediate((EShLanguage)stage))
629 glslang::GlslangToBil(*program.getIntermediate((EShLanguage)stage));
630 }
631 }
632 }
633
John Kessenich5b0f13a2013-11-01 03:08:40 +0000634 // Free everything up, program has to go before the shaders
635 // because it might have merged stuff from the shaders, and
636 // the stuff from the shaders has to have its destructors called
637 // before the pools holding the memory in the shaders is freed.
638 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000639 while (shaders.size() > 0) {
640 delete shaders.back();
641 shaders.pop_back();
642 }
John Kessenich69f4b512013-09-04 21:19:27 +0000643}
644
John Kessenicha0af4732012-12-12 21:15:54 +0000645int C_DECL main(int argc, char* argv[])
646{
John Kessenich54f6e562013-08-03 00:04:10 +0000647 if (! ProcessArguments(argc, argv)) {
648 usage();
John Kessenich2b07c7e2013-07-31 18:44:13 +0000649 return EFailUsage;
John Kessenich54f6e562013-08-03 00:04:10 +0000650 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000651
John Kessenich05a70632013-09-17 19:26:08 +0000652 if (Options & EOptionDumpConfig) {
653 printf("%s", DefaultConfig);
654 if (Worklist.empty())
655 return ESuccess;
656 }
657
John Kessenich319de232013-12-04 04:43:40 +0000658 if (Options & EOptionDumpVersions) {
659 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
660 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
661 if (Worklist.empty())
662 return ESuccess;
663 }
664
John Kessenich05a70632013-09-17 19:26:08 +0000665 if (Worklist.empty()) {
666 usage();
667 return EFailUsage;
668 }
669
670 ProcessConfigFile();
671
John Kessenich69f4b512013-09-04 21:19:27 +0000672 //
673 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000674 // 1) linking all arguments together, single-threaded, new C++ interface
675 // 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 +0000676 //
John Kessenichc36e1d82013-11-01 17:41:52 +0000677 if (Options & EOptionsLinkProgram) {
678 glslang::InitializeProcess();
John Kessenich38f3b892013-09-06 19:52:57 +0000679 CompileAndLinkShaders();
John Kessenichc36e1d82013-11-01 17:41:52 +0000680 glslang::FinalizeProcess();
681 } else {
682 ShInitialize();
683
John Kessenich38f3b892013-09-06 19:52:57 +0000684 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000685
John Kessenich38f3b892013-09-06 19:52:57 +0000686 if (Options & EOptionMultiThreaded) {
687 const int NumThreads = 16;
688 void* threads[NumThreads];
689 for (int t = 0; t < NumThreads; ++t) {
690 threads[t] = glslang::OS_CreateThread(&CompileShaders);
691 if (! threads[t]) {
692 printf("Failed to create thread\n");
693 return EFailThreadCreate;
694 }
John Kessenicha0af4732012-12-12 21:15:54 +0000695 }
John Kessenich38f3b892013-09-06 19:52:57 +0000696 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000697 } else
698 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000699
700 // Print out all the resulting infologs
701 for (int w = 0; w < NumWorkItems; ++w) {
702 if (Work[w]) {
703 if (printShaderNames)
704 puts(Work[w]->name.c_str());
705 puts(Work[w]->results.c_str());
706 delete Work[w];
707 }
708 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000709
710 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000711 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000712
713 if (Delay)
714 glslang::OS_Sleep(1000000);
John Kessenicha0af4732012-12-12 21:15:54 +0000715
John Kessenichc999ba22013-11-07 23:33:24 +0000716 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000717 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000718 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000719 return EFailLink;
720
721 return 0;
722}
723
724//
725// Deduce the language from the filename. Files must end in one of the
726// following extensions:
727//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000728// .vert = vertex
729// .tesc = tessellation control
730// .tese = tessellation evaluation
731// .geom = geometry
732// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000733// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000734//
John Kessenichb603f912013-08-29 00:39:25 +0000735EShLanguage FindLanguage(const std::string& name)
John Kessenicha0af4732012-12-12 21:15:54 +0000736{
John Kessenich2b07c7e2013-07-31 18:44:13 +0000737 size_t ext = name.rfind('.');
738 if (ext == std::string::npos) {
739 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000740 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000741 }
742
John Kessenich2b07c7e2013-07-31 18:44:13 +0000743 std::string suffix = name.substr(ext + 1, std::string::npos);
744 if (suffix == "vert")
745 return EShLangVertex;
746 else if (suffix == "tesc")
747 return EShLangTessControl;
748 else if (suffix == "tese")
749 return EShLangTessEvaluation;
750 else if (suffix == "geom")
751 return EShLangGeometry;
752 else if (suffix == "frag")
753 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000754 else if (suffix == "comp")
755 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000756
757 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000758 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000759}
760
John Kessenicha0af4732012-12-12 21:15:54 +0000761//
John Kessenich69f4b512013-09-04 21:19:27 +0000762// Read a file's data into a string, and compile it using the old interface ShCompile,
763// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000764//
John Kessenich51cdd902014-02-18 23:37:57 +0000765void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000766{
767 int ret;
John Kessenich41cf6b52013-06-25 18:10:05 +0000768 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000769 if (! shaderStrings) {
770 usage();
John Kessenichc999ba22013-11-07 23:33:24 +0000771 CompileFailed = true;
772 return;
John Kessenichdb4cd542013-06-26 22:42:55 +0000773 }
774
John Kessenich41cf6b52013-06-25 18:10:05 +0000775 int* lengths = new int[NumShaderStrings];
776
777 // move to length-based strings, rather than null-terminated strings
778 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000779 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000780
John Kessenichc999ba22013-11-07 23:33:24 +0000781 if (! shaderStrings) {
782 CompileFailed = true;
783 return;
784 }
John Kessenicha0af4732012-12-12 21:15:54 +0000785
John Kessenich52ac67e2013-05-05 23:46:22 +0000786 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000787 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000788
John Kessenich94a81fb2013-08-31 02:41:30 +0000789 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
790 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich05a70632013-09-17 19:26:08 +0000791 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, 100, false, messages);
792 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, 0, EShOptNone, &Resources, Options, 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000793 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
794 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
795 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000796 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichea869fb2013-10-28 18:12:06 +0000797 //ret = ShCompile(compiler, multi, 7, 0, EShOptNone, &Resources, Options, 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000798 }
John Kessenicha0af4732012-12-12 21:15:54 +0000799
John Kessenich94a81fb2013-08-31 02:41:30 +0000800 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000801 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000802 }
John Kessenicha0af4732012-12-12 21:15:54 +0000803
John Kessenich41cf6b52013-06-25 18:10:05 +0000804 delete [] lengths;
805 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000806
John Kessenichc999ba22013-11-07 23:33:24 +0000807 if (ret == 0)
808 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000809}
810
John Kessenicha0af4732012-12-12 21:15:54 +0000811//
812// print usage to stdout
813//
814void usage()
815{
John Kessenich319de232013-12-04 04:43:40 +0000816 printf("Usage: glslangValidator [option]... [file]...\n"
817 "\n"
818 "Where: each 'file' ends in\n"
819 " .conf to provide an optional config file that replaces the default configuration\n"
John Kessenich05a70632013-09-17 19:26:08 +0000820 " (see -c option below for generating a template)\n"
John Kessenichc0275792013-08-09 17:14:49 +0000821 " .vert for a vertex shader\n"
822 " .tesc for a tessellation control shader\n"
823 " .tese for a tessellation evaluation shader\n"
824 " .geom for a geometry shader\n"
825 " .frag for a fragment shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000826 " .comp for a compute shader\n"
827 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000828 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000829 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000830 "To get other information, use one of the following options:\n"
John Kessenich319de232013-12-04 04:43:40 +0000831 "(Each option must be specified separately, but can go anywhere in the command line.)\n"
John Kessenich92f90382014-07-28 04:21:04 +0000832 " -b create BIL\n"
John Kessenich319de232013-12-04 04:43:40 +0000833 " -c configuration dump; use to create default configuration file (redirect to a .conf file)\n"
834 " -i intermediate tree (glslang AST) is printed out\n"
835 " -l link validation of all input files\n"
836 " -m memory leak mode\n"
837 " -q dump reflection query database\n"
838 " -r relaxed semantic error-checking mode\n"
839 " -s silent mode\n"
840 " -t multi-threaded mode\n"
841 " -v print version strings\n"
842 " -w suppress warnings (except as required by #extension : warn)\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000843 );
John Kessenicha0af4732012-12-12 21:15:54 +0000844}
845
John Kessenichcfd643e2013-03-08 23:14:42 +0000846#ifndef _WIN32
847
848#include <errno.h>
849
850int fopen_s(
851 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +0000852 const char* filename,
853 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +0000854)
855{
856 if (!pFile || !filename || !mode) {
857 return EINVAL;
858 }
859
860 FILE* f = fopen(filename, mode);
861 if (! f) {
862 if (errno != 0) {
863 return errno;
864 } else {
865 return ENOENT;
866 }
867 }
868 *pFile = f;
869
870 return 0;
871}
872
873#endif
874
John Kessenicha0af4732012-12-12 21:15:54 +0000875//
876// Malloc a string of sufficient size and read a string into it.
877//
John Kessenich51cdd902014-02-18 23:37:57 +0000878char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +0000879{
John Kessenich200b2732012-12-12 21:21:23 +0000880 FILE *in;
881 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich6494baf2014-02-19 00:08:59 +0000882 char *fdata;
John Kessenicha0af4732012-12-12 21:15:54 +0000883 int count = 0;
John Kessenichcfd643e2013-03-08 23:14:42 +0000884 const int maxSourceStrings = 5;
John Kessenich6494baf2014-02-19 00:08:59 +0000885 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1));
John Kessenicha0af4732012-12-12 21:15:54 +0000886
John Kessenich200b2732012-12-12 21:21:23 +0000887 if (errorCode) {
John Kessenicha0af4732012-12-12 21:15:54 +0000888 printf("Error: unable to open input file: %s\n", fileName);
889 return 0;
890 }
891
892 while (fgetc(in) != EOF)
893 count++;
894
895 fseek(in, 0, SEEK_SET);
896
John Kessenich51cdd902014-02-18 23:37:57 +0000897 if (!(fdata = (char*)malloc(count+2))) {
John Kessenich585982e2014-03-28 17:49:10 +0000898 printf("Error allocating memory\n");
899 return 0;
John Kessenicha0af4732012-12-12 21:15:54 +0000900 }
901 if (fread(fdata,1,count, in)!=count) {
902 printf("Error reading input file: %s\n", fileName);
903 return 0;
904 }
905 fdata[count] = '\0';
906 fclose(in);
John Kessenichea869fb2013-10-28 18:12:06 +0000907 if (count == 0) {
John Kessenicha0af4732012-12-12 21:15:54 +0000908 return_data[0]=(char*)malloc(count+2);
909 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +0000910 NumShaderStrings = 0;
John Kessenicha0af4732012-12-12 21:15:54 +0000911 return return_data;
John Kessenichea869fb2013-10-28 18:12:06 +0000912 } else
913 NumShaderStrings = 1;
John Kessenicha0af4732012-12-12 21:15:54 +0000914
John Kessenich41cf6b52013-06-25 18:10:05 +0000915 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000916 int ptr_len=0,i=0;
917 while(count>0){
918 return_data[i]=(char*)malloc(len+2);
919 memcpy(return_data[i],fdata+ptr_len,len);
920 return_data[i][len]='\0';
921 count-=(len);
922 ptr_len+=(len);
923 if(count<len){
924 if(count==0){
John Kessenich41cf6b52013-06-25 18:10:05 +0000925 NumShaderStrings=(i+1);
John Kessenicha0af4732012-12-12 21:15:54 +0000926 break;
927 }
928 len = count;
929 }
930 ++i;
931 }
932 return return_data;
933}
934
John Kessenich51cdd902014-02-18 23:37:57 +0000935void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +0000936{
John Kessenich41cf6b52013-06-25 18:10:05 +0000937 for(int i=0;i<NumShaderStrings;i++)
John Kessenicha0af4732012-12-12 21:15:54 +0000938 free(data[i]);
939}
940
John Kessenich54d8cda2013-02-11 22:36:01 +0000941void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +0000942{
943 printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" :
944 "#### %s %s INFO LOG ####\n", msg, name, num);
945}