blob: e970185bd6c673dbece7547ccc2890e15aaab69a [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
2//Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3//All rights reserved.
4//
5//Redistribution and use in source and binary forms, with or without
6//modification, are permitted provided that the following conditions
7//are met:
8//
9// Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11//
12// Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following
14// disclaimer in the documentation and/or other materials provided
15// with the distribution.
16//
17// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
18// contributors may be used to endorse or promote products derived
19// from this software without specific prior written permission.
20//
21//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32//POSSIBILITY OF SUCH DAMAGE.
33//
34#include "./../glslang/Include/ShHandle.h"
35#include "./../glslang/Public/ShaderLang.h"
36#include <string.h>
37#include <math.h>
38
39#ifdef _WIN32
40 #include <windows.h>
41 #include <psapi.h>
42#endif
43
44extern "C" {
45 SH_IMPORT_EXPORT void ShOutputHtml();
46}
47
48//#define MEASURE_MEMORY
49
50//
51// Return codes from main.
52//
53enum TFailCode {
54 ESuccess = 0,
55 EFailUsage,
56 EFailCompile,
57 EFailLink,
58 EFailCompilerCreate,
59 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
75static EShLanguage FindLanguage(char *lang);
76bool CompileFile(char *fileName, ShHandle, int, const TBuiltInResource*);
77void usage();
78void FreeFileData(char **data);
79char** ReadFileData(char *fileName);
80void InfoLogMsg(char* msg, const char* name, const int num);
81int ShOutputMultipleStrings(char ** );
82//Added to accomodate the multiple strings.
83int OutputMultipleStrings = 1;
84
85//
86// Set up the per compile resources
87//
88void GenerateResources(TBuiltInResource& resources)
89{
90 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;
102}
103
104int C_DECL main(int argc, char* argv[])
105{
106 bool delay = false;
107 int numCompilers = 0;
108 bool compileFailed = false;
109 bool linkFailed = false;
110 int debugOptions = 0;
111 int i;
112
113 ShHandle linker = 0;
114 ShHandle uniformMap = 0;
115 ShHandle compilers[EShLangCount];
116
117 ShInitialize();
118
119#ifdef _WIN32
120 __try {
121#endif
122 argc--;
123 argv++;
124 for (; argc >= 1; argc--, argv++) {
125 if (argv[0][0] == '-' || argv[0][0] == '/') {
126 switch (argv[0][1]) {
127 case 'd': delay = true; break;
128
129#ifdef MEASURE_MEMORY
130 case 'i': break;
131 case 'a': break;
132 case 'h': break;
133#else
134 case 'i': debugOptions |= EDebugOpIntermediate; break;
135 case 'a': debugOptions |= EDebugOpAssembly; break;
136#endif
137 case 'c': if(!ShOutputMultipleStrings(++argv))
138 return EFailUsage;
139 --argc; break;
140 case 'm': debugOptions |= EDebugOpLinkMaps; break;
141 default: usage(); return EFailUsage;
142 }
143 } else {
144 compilers[numCompilers] = ShConstructCompiler(FindLanguage(argv[0]), debugOptions);
145 if (compilers[numCompilers] == 0)
146 return EFailCompilerCreate;
147 ++numCompilers;
148
149 TBuiltInResource resources;
150 GenerateResources(resources);
151 if (! CompileFile(argv[0], compilers[numCompilers-1], debugOptions, &resources))
152 compileFailed = true;
153 }
154 }
155
156 if (!numCompilers) {
157 usage();
158 return EFailUsage;
159 }
160
161 linker = ShConstructLinker(EShExVertexFragment, debugOptions);
162 if (linker == 0)
163 return EFailLinkerCreate;
164
165 uniformMap = ShConstructUniformMap();
166 if (uniformMap == 0)
167 return EFailLinkerCreate;
168
169 if (numCompilers > 0) {
170 ShSetFixedAttributeBindings(linker, &FixedAttributeTable);
171 if (! ShLink(linker, compilers, numCompilers, uniformMap, 0, 0))
172 linkFailed = true;
173 }
174
175 for (i = 0; i < numCompilers; ++i) {
176 InfoLogMsg("BEGIN", "COMPILER", i);
177 puts(ShGetInfoLog(compilers[i]));
178 InfoLogMsg("END", "COMPILER", i);
179 }
180
181 InfoLogMsg("BEGIN", "LINKER", -1);
182 puts(ShGetInfoLog(linker));
183 InfoLogMsg("END", "LINKER", -1);
184
185#ifdef _WIN32
186 } __finally {
187#endif
188 for (i = 0; i < numCompilers; ++i)
189 ShDestruct(compilers[i]);
190
191 ShDestruct(linker);
192 ShDestruct(uniformMap);
193
194#ifdef _WIN32
195 if (delay)
196 Sleep(1000000);
197
198 }
199#endif
200
201 if (compileFailed)
202 return EFailCompile;
203 if (linkFailed)
204 return EFailLink;
205
206 return 0;
207}
208
209//
210// Deduce the language from the filename. Files must end in one of the
211// following extensions:
212//
213// .frag* = fragment programs
214// .vert* = vertex programs
215// .pack* = pack programs
216// .unpa* = unpack pragrams
217//
218static EShLanguage FindLanguage(char *name)
219{
220 if (!name)
221 return EShLangFragment;
222
223 char *ext = strrchr(name, '.');
224
225 if (ext && strcmp(ext, ".sl") == 0)
226 for (; ext > name && ext[0] != '.'; ext--);
227
228 if (ext = strrchr(name, '.')) {
229 if (strncmp(ext, ".frag", 4) == 0) return EShLangFragment;
230 if (strncmp(ext, ".vert", 4) == 0) return EShLangVertex;
231 if (strncmp(ext, ".pack", 4) == 0) return EShLangPack;
232 if (strncmp(ext, ".unpa", 4) == 0) return EShLangUnpack;
233 }
234
235 return EShLangFragment;
236}
237
238
239//
240// Read a file's data into a string, and compile it using ShCompile
241//
242bool CompileFile(char *fileName, ShHandle compiler, int debugOptions, const TBuiltInResource *resources)
243{
244 int ret;
245 char **data = ReadFileData(fileName);
246
247#ifdef MEASURE_MEMORY
248 PROCESS_MEMORY_COUNTERS counters;
249#endif
250
251 if (!data)
252 return false;
253
254#ifdef MEASURE_MEMORY
255 for (int i = 0; i < 1000; ++i) {
256 for (int j = 0; j < 100; ++j)
257#endif
258 ret = ShCompile(compiler, data, OutputMultipleStrings, EShOptNone, resources, debugOptions);
259#ifdef MEASURE_MEMORY
260
261 GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters));
262 }
263#endif
264
265 FreeFileData(data);
266
267 return ret ? true : false;
268}
269
270
271//
272// print usage to stdout
273//
274void usage()
275{
276 printf("Usage: standalone [-i -a -c -m -d -h] file1 file2 ...\n"
277 "Where: filename = filename ending in .frag* or .vert*\n");
278}
279
280
281//
282// Malloc a string of sufficient size and read a string into it.
283//
284# define MAX_SOURCE_STRINGS 5
285char** ReadFileData(char *fileName)
286{
287 FILE *in = fopen(fileName, "r");
288 char *fdata;
289 int count = 0;
290 char**return_data=(char**)malloc(MAX_SOURCE_STRINGS+1);
291
292 //return_data[MAX_SOURCE_STRINGS]=NULL;
293 if (!in) {
294 printf("Error: unable to open input file: %s\n", fileName);
295 return 0;
296 }
297
298 while (fgetc(in) != EOF)
299 count++;
300
301 fseek(in, 0, SEEK_SET);
302
303
304 if (!(fdata = (char *)malloc(count+2))) {
305 printf("Error allocating memory\n");
306 return 0;
307 }
308 if (fread(fdata,1,count, in)!=count) {
309 printf("Error reading input file: %s\n", fileName);
310 return 0;
311 }
312 fdata[count] = '\0';
313 fclose(in);
314 if(count==0){
315 return_data[0]=(char*)malloc(count+2);
316 return_data[0][0]='\0';
317 OutputMultipleStrings=0;
318 return return_data;
319 }
320
321 int len = (int)(ceil)((float)count/(float)OutputMultipleStrings);
322 int ptr_len=0,i=0;
323 while(count>0){
324 return_data[i]=(char*)malloc(len+2);
325 memcpy(return_data[i],fdata+ptr_len,len);
326 return_data[i][len]='\0';
327 count-=(len);
328 ptr_len+=(len);
329 if(count<len){
330 if(count==0){
331 OutputMultipleStrings=(i+1);
332 break;
333 }
334 len = count;
335 }
336 ++i;
337 }
338 return return_data;
339}
340
341
342
343void FreeFileData(char **data)
344{
345 for(int i=0;i<OutputMultipleStrings;i++)
346 free(data[i]);
347}
348
349
350
351void InfoLogMsg(char* msg, const char* name, const int num)
352{
353 printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" :
354 "#### %s %s INFO LOG ####\n", msg, name, num);
355}
356
357int ShOutputMultipleStrings(char **argv)
358{
359 if(!(abs(OutputMultipleStrings = atoi(*argv)))||((OutputMultipleStrings >5 || OutputMultipleStrings < 1)? 1:0)){
360 printf("Invalid Command Line Argument after -c option.\n"
361 "Usage: -c <integer> where integer =[1,5]\n"
362 "This option must be specified before the input file path\n");
363 return 0;
364 }
365 return 1;
366}