blob: 3a5fe558da217830212134a89d165852aec92edd [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
John Kessenicha0af4732012-12-12 21:15:54 +0000215//
216static EShLanguage FindLanguage(char *name)
217{
218 if (!name)
John Kesseniche95ecc52012-12-12 21:34:14 +0000219 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000220
221 char *ext = strrchr(name, '.');
222
223 if (ext && strcmp(ext, ".sl") == 0)
224 for (; ext > name && ext[0] != '.'; ext--);
225
226 if (ext = strrchr(name, '.')) {
227 if (strncmp(ext, ".frag", 4) == 0) return EShLangFragment;
John Kessenicha0af4732012-12-12 21:15:54 +0000228 }
229
John Kesseniche95ecc52012-12-12 21:34:14 +0000230 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000231}
232
233
234//
235// Read a file's data into a string, and compile it using ShCompile
236//
237bool CompileFile(char *fileName, ShHandle compiler, int debugOptions, const TBuiltInResource *resources)
238{
239 int ret;
240 char **data = ReadFileData(fileName);
241
242#ifdef MEASURE_MEMORY
243 PROCESS_MEMORY_COUNTERS counters;
244#endif
245
246 if (!data)
247 return false;
248
249#ifdef MEASURE_MEMORY
250 for (int i = 0; i < 1000; ++i) {
251 for (int j = 0; j < 100; ++j)
252#endif
253 ret = ShCompile(compiler, data, OutputMultipleStrings, EShOptNone, resources, debugOptions);
254#ifdef MEASURE_MEMORY
255
256 GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters));
257 }
258#endif
259
260 FreeFileData(data);
261
262 return ret ? true : false;
263}
264
265
266//
267// print usage to stdout
268//
269void usage()
270{
271 printf("Usage: standalone [-i -a -c -m -d -h] file1 file2 ...\n"
272 "Where: filename = filename ending in .frag* or .vert*\n");
273}
274
275
276//
277// Malloc a string of sufficient size and read a string into it.
278//
279# define MAX_SOURCE_STRINGS 5
280char** ReadFileData(char *fileName)
281{
John Kessenich200b2732012-12-12 21:21:23 +0000282 FILE *in;
283 int errorCode = fopen_s(&in, fileName, "r");
John Kessenicha0af4732012-12-12 21:15:54 +0000284 char *fdata;
285 int count = 0;
286 char**return_data=(char**)malloc(MAX_SOURCE_STRINGS+1);
287
288 //return_data[MAX_SOURCE_STRINGS]=NULL;
John Kessenich200b2732012-12-12 21:21:23 +0000289 if (errorCode) {
John Kessenicha0af4732012-12-12 21:15:54 +0000290 printf("Error: unable to open input file: %s\n", fileName);
291 return 0;
292 }
293
294 while (fgetc(in) != EOF)
295 count++;
296
297 fseek(in, 0, SEEK_SET);
298
299
300 if (!(fdata = (char *)malloc(count+2))) {
301 printf("Error allocating memory\n");
302 return 0;
303 }
304 if (fread(fdata,1,count, in)!=count) {
305 printf("Error reading input file: %s\n", fileName);
306 return 0;
307 }
308 fdata[count] = '\0';
309 fclose(in);
310 if(count==0){
311 return_data[0]=(char*)malloc(count+2);
312 return_data[0][0]='\0';
313 OutputMultipleStrings=0;
314 return return_data;
315 }
316
317 int len = (int)(ceil)((float)count/(float)OutputMultipleStrings);
318 int ptr_len=0,i=0;
319 while(count>0){
320 return_data[i]=(char*)malloc(len+2);
321 memcpy(return_data[i],fdata+ptr_len,len);
322 return_data[i][len]='\0';
323 count-=(len);
324 ptr_len+=(len);
325 if(count<len){
326 if(count==0){
327 OutputMultipleStrings=(i+1);
328 break;
329 }
330 len = count;
331 }
332 ++i;
333 }
334 return return_data;
335}
336
337
338
339void FreeFileData(char **data)
340{
341 for(int i=0;i<OutputMultipleStrings;i++)
342 free(data[i]);
343}
344
345
346
347void InfoLogMsg(char* msg, const char* name, const int num)
348{
349 printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" :
350 "#### %s %s INFO LOG ####\n", msg, name, num);
351}
352
353int ShOutputMultipleStrings(char **argv)
354{
355 if(!(abs(OutputMultipleStrings = atoi(*argv)))||((OutputMultipleStrings >5 || OutputMultipleStrings < 1)? 1:0)){
356 printf("Invalid Command Line Argument after -c option.\n"
357 "Usage: -c <integer> where integer =[1,5]\n"
358 "This option must be specified before the input file path\n");
359 return 0;
360 }
361 return 1;
362}