blob: ec493fded3d26fb6dec14b7f5402298fe7ee5d3d [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 Kessenich2b07c7e2013-07-31 18:44:13 +000036#include "Worklist.h"
John Kessenicha0af4732012-12-12 21:15:54 +000037#include "./../glslang/Include/ShHandle.h"
38#include "./../glslang/Public/ShaderLang.h"
39#include <string.h>
John Kessenichcfd643e2013-03-08 23:14:42 +000040#include <stdlib.h>
John Kessenicha0af4732012-12-12 21:15:54 +000041#include <math.h>
42
John Kessenich2b07c7e2013-07-31 18:44:13 +000043#include "osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000044
45extern "C" {
46 SH_IMPORT_EXPORT void ShOutputHtml();
47}
48
John Kessenicha0af4732012-12-12 21:15:54 +000049//
50// Return codes from main.
51//
52enum TFailCode {
53 ESuccess = 0,
54 EFailUsage,
55 EFailCompile,
56 EFailLink,
57 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000058 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000059 EFailLinkerCreate
60};
61
62//
63// Just placeholders for testing purposes. The stand-alone environment
64// can't actually do a full link without something specifying real
65// attribute bindings.
66//
67ShBinding FixedAttributeBindings[] = {
68 { "gl_Vertex", 15 },
69 { "gl_Color", 10 },
70 { "gl_Normal", 7 },
71};
72
73ShBindingTable FixedAttributeTable = { 3, FixedAttributeBindings };
74
John Kessenich2b07c7e2013-07-31 18:44:13 +000075static EShLanguage FindLanguage(const std::string& name);
John Kessenich52ac67e2013-05-05 23:46:22 +000076bool CompileFile(const char *fileName, ShHandle, int options, const TBuiltInResource*);
John Kessenicha0af4732012-12-12 21:15:54 +000077void usage();
78void FreeFileData(char **data);
John Kessenich54d8cda2013-02-11 22:36:01 +000079char** ReadFileData(const char *fileName);
80void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +000081
82// Use to test breaking a single shader file into multiple strings.
83int NumShaderStrings = 1;
John Kessenicha0af4732012-12-12 21:15:54 +000084
85//
86// Set up the per compile resources
87//
88void GenerateResources(TBuiltInResource& resources)
John Kessenichb51f62c2013-04-11 16:31:09 +000089{
John Kessenicha0af4732012-12-12 21:15:54 +000090 resources.maxLights = 32;
91 resources.maxClipPlanes = 6;
92 resources.maxTextureUnits = 32;
93 resources.maxTextureCoords = 32;
94 resources.maxVertexAttribs = 64;
95 resources.maxVertexUniformComponents = 4096;
96 resources.maxVaryingFloats = 64;
97 resources.maxVertexTextureImageUnits = 32;
98 resources.maxCombinedTextureImageUnits = 32;
99 resources.maxTextureImageUnits = 32;
100 resources.maxFragmentUniformComponents = 4096;
101 resources.maxDrawBuffers = 32;
John Kessenichbd0747d2013-02-17 06:01:50 +0000102 resources.maxVertexUniformVectors = 128;
103 resources.maxVaryingVectors = 8;
104 resources.maxFragmentUniformVectors = 16;
105 resources.maxVertexOutputVectors = 16;
John Kessenich1f2a36b2013-02-20 04:42:42 +0000106 resources.maxFragmentInputVectors = 15;
John Kessenichbd0747d2013-02-17 06:01:50 +0000107 resources.minProgramTexelOffset = -8;
108 resources.maxProgramTexelOffset = 7;
John Kessenicha0af4732012-12-12 21:15:54 +0000109}
110
John Kessenich2b07c7e2013-07-31 18:44:13 +0000111glslang::TWorklist Worklist;
112int DebugOptions = 0;
113bool Delay = false;
114
115bool ProcessArguments(int argc, char* argv[])
116{
117 argc--;
118 argv++;
119 for (; argc >= 1; argc--, argv++) {
120 if (argv[0][0] == '-') {
121 switch (argv[0][1]) {
122 case 'd':
123 Delay = true;
124 break;
125 case 'i':
126 DebugOptions |= EDebugOpIntermediate;
127 break;
128 case 'l':
129 DebugOptions |= EDebugOpMemoryLeakMode;
130 break;
131 case 'r':
132 DebugOptions |= EDebugOpRelaxedErrors;
133 break;
134 case 's':
135 DebugOptions |= EDebugOpSuppressInfolog;
136 break;
137 case 't':
138 DebugOptions |= EDebugOpTexturePrototypes;
139 break;
140 default:
John Kessenich2b07c7e2013-07-31 18:44:13 +0000141 return false;
142 }
143 } else
144 Worklist.add(std::string(argv[0]));
145 }
146
John Kessenich54f6e562013-08-03 00:04:10 +0000147 if (Worklist.empty())
148 return false;
149
John Kessenich2b07c7e2013-07-31 18:44:13 +0000150 return true;
151}
152
153// Thread entry point
John Kessenichee6a9c82013-07-31 23:19:17 +0000154unsigned int
155#ifdef _WIN32
156 __stdcall
157#endif
158 CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000159{
160 ShHandle compiler;
161
162 std::string shaderName;
163 while (Worklist.remove(shaderName)) {
164 compiler = ShConstructCompiler(FindLanguage(shaderName), DebugOptions);
165 if (compiler == 0)
166 return false;
167
168 TBuiltInResource resources;
169 GenerateResources(resources);
170 CompileFile(shaderName.c_str(), compiler, DebugOptions, &resources);
171
172 if (! (DebugOptions & EDebugOpSuppressInfolog))
173 puts(ShGetInfoLog(compiler));
174
175 ShDestruct(compiler);
176 }
177
178 return 0;
179}
180
John Kessenicha0af4732012-12-12 21:15:54 +0000181int C_DECL main(int argc, char* argv[])
182{
John Kessenicha0af4732012-12-12 21:15:54 +0000183 bool compileFailed = false;
184 bool linkFailed = false;
John Kessenicha0af4732012-12-12 21:15:54 +0000185
John Kessenich2b07c7e2013-07-31 18:44:13 +0000186 // Init for front-end proper
John Kessenicha0af4732012-12-12 21:15:54 +0000187 ShInitialize();
188
John Kessenich2b07c7e2013-07-31 18:44:13 +0000189 // Init for for standalone
190 glslang::InitGlobalLock();
John Kessenicha0af4732012-12-12 21:15:54 +0000191
John Kessenich54f6e562013-08-03 00:04:10 +0000192 if (! ProcessArguments(argc, argv)) {
193 usage();
John Kessenich2b07c7e2013-07-31 18:44:13 +0000194 return EFailUsage;
John Kessenich54f6e562013-08-03 00:04:10 +0000195 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000196
197 // TODO: finish threading, allow external control over number of threads
198 const int NumThreads = 1;
199 if (NumThreads > 1) {
200 void* threads[NumThreads];
201 for (int t = 0; t < NumThreads; ++t) {
202 threads[t] = glslang::OS_CreateThread(&CompileShaders);
203 if (! threads[t]) {
204 printf("Failed to create thread\n");
205 return EFailThreadCreate;
John Kessenicha0af4732012-12-12 21:15:54 +0000206 }
207 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000208 glslang::OS_WaitForAllThreads(threads, NumThreads);
209 } else {
210 if (! CompileShaders(0))
211 compileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000212 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000213
214 if (Delay)
215 glslang::OS_Sleep(1000000);
John Kessenicha0af4732012-12-12 21:15:54 +0000216
217 if (compileFailed)
218 return EFailCompile;
219 if (linkFailed)
220 return EFailLink;
221
222 return 0;
223}
224
225//
226// Deduce the language from the filename. Files must end in one of the
227// following extensions:
228//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000229// .vert = vertex
230// .tesc = tessellation control
231// .tese = tessellation evaluation
232// .geom = geometry
233// .frag = fragment
John Kessenicha0af4732012-12-12 21:15:54 +0000234//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000235static EShLanguage FindLanguage(const std::string& name)
John Kessenicha0af4732012-12-12 21:15:54 +0000236{
John Kessenich2b07c7e2013-07-31 18:44:13 +0000237 size_t ext = name.rfind('.');
238 if (ext == std::string::npos) {
239 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000240 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000241 }
242
John Kessenich2b07c7e2013-07-31 18:44:13 +0000243 std::string suffix = name.substr(ext + 1, std::string::npos);
244 if (suffix == "vert")
245 return EShLangVertex;
246 else if (suffix == "tesc")
247 return EShLangTessControl;
248 else if (suffix == "tese")
249 return EShLangTessEvaluation;
250 else if (suffix == "geom")
251 return EShLangGeometry;
252 else if (suffix == "frag")
253 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000254 else if (suffix == "comp")
255 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000256
257 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000258 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000259}
260
John Kessenicha0af4732012-12-12 21:15:54 +0000261//
262// Read a file's data into a string, and compile it using ShCompile
263//
John Kessenich54d8cda2013-02-11 22:36:01 +0000264bool CompileFile(const char *fileName, ShHandle compiler, int debugOptions, const TBuiltInResource *resources)
John Kessenicha0af4732012-12-12 21:15:54 +0000265{
266 int ret;
John Kessenich41cf6b52013-06-25 18:10:05 +0000267 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000268 if (! shaderStrings) {
269 usage();
270 return false;
271 }
272
John Kessenich41cf6b52013-06-25 18:10:05 +0000273 int* lengths = new int[NumShaderStrings];
274
275 // move to length-based strings, rather than null-terminated strings
276 for (int s = 0; s < NumShaderStrings; ++s)
277 lengths[s] = strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000278
John Kessenich41cf6b52013-06-25 18:10:05 +0000279 if (! shaderStrings)
John Kessenicha0af4732012-12-12 21:15:54 +0000280 return false;
281
John Kessenich52ac67e2013-05-05 23:46:22 +0000282 EShMessages messages = EShMsgDefault;
283 if (debugOptions & EDebugOpRelaxedErrors)
284 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
John Kessenich8df53cc2013-04-14 19:23:50 +0000285 for (int i = 0; i < ((debugOptions & EDebugOpMemoryLeakMode) ? 100 : 1); ++i) {
John Kessenich41cf6b52013-06-25 18:10:05 +0000286 for (int j = 0; j < ((debugOptions & EDebugOpMemoryLeakMode) ? 100 : 1); ++j) {
287 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, resources, debugOptions, 100, false, messages);
288 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, 0, EShOptNone, resources, debugOptions, 100, false, messages);
289 //const char* multi[4] = { "# ve", "rsion", " 300 e", "s" };
290 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
291 //ret = ShCompile(compiler, multi, 4, 0, EShOptNone, resources, debugOptions, 100, false, messages);
292 }
John Kessenicha0af4732012-12-12 21:15:54 +0000293
John Kessenich2b07c7e2013-07-31 18:44:13 +0000294 if (debugOptions & EDebugOpMemoryLeakMode)
295 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000296 }
John Kessenicha0af4732012-12-12 21:15:54 +0000297
John Kessenich41cf6b52013-06-25 18:10:05 +0000298 delete [] lengths;
299 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000300
301 return ret ? true : false;
302}
303
304
305//
306// print usage to stdout
307//
308void usage()
309{
John Kessenich8df53cc2013-04-14 19:23:50 +0000310 printf("Usage: standalone [ options ] filename\n"
John Kessenichc0275792013-08-09 17:14:49 +0000311 "Where: filename is a name ending in\n"
312 " .vert for a vertex shader\n"
313 " .tesc for a tessellation control shader\n"
314 " .tese for a tessellation evaluation shader\n"
315 " .geom for a geometry shader\n"
316 " .frag for a fragment shader\n"
317 " .comp for a compute shader\n\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000318 "Compilation warnings and errors will be printed to stdout.\n"
319 "To get other information, use one of the following options:\n"
320 "-i: intermediate tree (glslang AST) is printed out\n"
321 "-d: delay exit\n"
John Kessenich8df53cc2013-04-14 19:23:50 +0000322 "-l: memory leak mode\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000323 "-s: silent mode\n"
324 "-r: relaxed semantic error-checking mode\n");
John Kessenicha0af4732012-12-12 21:15:54 +0000325}
326
John Kessenichcfd643e2013-03-08 23:14:42 +0000327#ifndef _WIN32
328
329#include <errno.h>
330
331int fopen_s(
332 FILE** pFile,
333 const char *filename,
334 const char *mode
335)
336{
337 if (!pFile || !filename || !mode) {
338 return EINVAL;
339 }
340
341 FILE* f = fopen(filename, mode);
342 if (! f) {
343 if (errno != 0) {
344 return errno;
345 } else {
346 return ENOENT;
347 }
348 }
349 *pFile = f;
350
351 return 0;
352}
353
354#endif
355
John Kessenicha0af4732012-12-12 21:15:54 +0000356//
357// Malloc a string of sufficient size and read a string into it.
358//
John Kessenich54d8cda2013-02-11 22:36:01 +0000359char** ReadFileData(const char *fileName)
John Kessenicha0af4732012-12-12 21:15:54 +0000360{
John Kessenich200b2732012-12-12 21:21:23 +0000361 FILE *in;
362 int errorCode = fopen_s(&in, fileName, "r");
John Kessenicha0af4732012-12-12 21:15:54 +0000363 char *fdata;
364 int count = 0;
John Kessenichcfd643e2013-03-08 23:14:42 +0000365 const int maxSourceStrings = 5;
366 char** return_data = (char**)malloc(maxSourceStrings+1);
John Kessenicha0af4732012-12-12 21:15:54 +0000367
368 //return_data[MAX_SOURCE_STRINGS]=NULL;
John Kessenich200b2732012-12-12 21:21:23 +0000369 if (errorCode) {
John Kessenicha0af4732012-12-12 21:15:54 +0000370 printf("Error: unable to open input file: %s\n", fileName);
371 return 0;
372 }
373
374 while (fgetc(in) != EOF)
375 count++;
376
377 fseek(in, 0, SEEK_SET);
378
379
380 if (!(fdata = (char *)malloc(count+2))) {
381 printf("Error allocating memory\n");
382 return 0;
383 }
384 if (fread(fdata,1,count, in)!=count) {
385 printf("Error reading input file: %s\n", fileName);
386 return 0;
387 }
388 fdata[count] = '\0';
389 fclose(in);
390 if(count==0){
391 return_data[0]=(char*)malloc(count+2);
392 return_data[0][0]='\0';
John Kessenich41cf6b52013-06-25 18:10:05 +0000393 NumShaderStrings=0;
John Kessenicha0af4732012-12-12 21:15:54 +0000394 return return_data;
395 }
396
John Kessenich41cf6b52013-06-25 18:10:05 +0000397 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000398 int ptr_len=0,i=0;
399 while(count>0){
400 return_data[i]=(char*)malloc(len+2);
401 memcpy(return_data[i],fdata+ptr_len,len);
402 return_data[i][len]='\0';
403 count-=(len);
404 ptr_len+=(len);
405 if(count<len){
406 if(count==0){
John Kessenich41cf6b52013-06-25 18:10:05 +0000407 NumShaderStrings=(i+1);
John Kessenicha0af4732012-12-12 21:15:54 +0000408 break;
409 }
410 len = count;
411 }
412 ++i;
413 }
414 return return_data;
415}
416
417
418
419void FreeFileData(char **data)
420{
John Kessenich41cf6b52013-06-25 18:10:05 +0000421 for(int i=0;i<NumShaderStrings;i++)
John Kessenicha0af4732012-12-12 21:15:54 +0000422 free(data[i]);
423}
424
425
426
John Kessenich54d8cda2013-02-11 22:36:01 +0000427void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +0000428{
429 printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" :
430 "#### %s %s INFO LOG ####\n", msg, name, num);
431}