blob: a074761fab6f0f2d167252d4ab720ce75b7c02d9 [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);
John Kessenich54d8cda2013-02-11 22:36:01 +000076bool CompileFile(const char *fileName, ShHandle, int, 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);
81int ShOutputMultipleStrings(const char *);
John Kessenicha0af4732012-12-12 21:15:54 +000082//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
John Kessenich54d8cda2013-02-11 22:36:01 +0000137 case 'c': if(!ShOutputMultipleStrings((++argv)[0]))
John Kessenicha0af4732012-12-12 21:15:54 +0000138 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//
John Kessenich54d8cda2013-02-11 22:36:01 +0000237bool CompileFile(const char *fileName, ShHandle compiler, int debugOptions, const TBuiltInResource *resources)
John Kessenicha0af4732012-12-12 21:15:54 +0000238{
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
John Kessenich38c507e2013-02-08 18:56:56 +0000253 ret = ShCompile(compiler, data, OutputMultipleStrings, EShOptNone, resources, debugOptions, 100);
John Kessenicha0af4732012-12-12 21:15:54 +0000254#ifdef MEASURE_MEMORY
255
256 GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters));
John Kessenichdadf9452013-02-11 00:54:44 +0000257 printf("Working set size: %d\n", counters.WorkingSetSize);
John Kessenicha0af4732012-12-12 21:15:54 +0000258 }
259#endif
260
261 FreeFileData(data);
262
263 return ret ? true : false;
264}
265
266
267//
268// print usage to stdout
269//
270void usage()
271{
272 printf("Usage: standalone [-i -a -c -m -d -h] file1 file2 ...\n"
273 "Where: filename = filename ending in .frag* or .vert*\n");
274}
275
276
277//
278// Malloc a string of sufficient size and read a string into it.
279//
280# define MAX_SOURCE_STRINGS 5
John Kessenich54d8cda2013-02-11 22:36:01 +0000281char** ReadFileData(const char *fileName)
John Kessenicha0af4732012-12-12 21:15:54 +0000282{
John Kessenich200b2732012-12-12 21:21:23 +0000283 FILE *in;
284 int errorCode = fopen_s(&in, fileName, "r");
John Kessenicha0af4732012-12-12 21:15:54 +0000285 char *fdata;
286 int count = 0;
287 char**return_data=(char**)malloc(MAX_SOURCE_STRINGS+1);
288
289 //return_data[MAX_SOURCE_STRINGS]=NULL;
John Kessenich200b2732012-12-12 21:21:23 +0000290 if (errorCode) {
John Kessenicha0af4732012-12-12 21:15:54 +0000291 printf("Error: unable to open input file: %s\n", fileName);
292 return 0;
293 }
294
295 while (fgetc(in) != EOF)
296 count++;
297
298 fseek(in, 0, SEEK_SET);
299
300
301 if (!(fdata = (char *)malloc(count+2))) {
302 printf("Error allocating memory\n");
303 return 0;
304 }
305 if (fread(fdata,1,count, in)!=count) {
306 printf("Error reading input file: %s\n", fileName);
307 return 0;
308 }
309 fdata[count] = '\0';
310 fclose(in);
311 if(count==0){
312 return_data[0]=(char*)malloc(count+2);
313 return_data[0][0]='\0';
314 OutputMultipleStrings=0;
315 return return_data;
316 }
317
318 int len = (int)(ceil)((float)count/(float)OutputMultipleStrings);
319 int ptr_len=0,i=0;
320 while(count>0){
321 return_data[i]=(char*)malloc(len+2);
322 memcpy(return_data[i],fdata+ptr_len,len);
323 return_data[i][len]='\0';
324 count-=(len);
325 ptr_len+=(len);
326 if(count<len){
327 if(count==0){
328 OutputMultipleStrings=(i+1);
329 break;
330 }
331 len = count;
332 }
333 ++i;
334 }
335 return return_data;
336}
337
338
339
340void FreeFileData(char **data)
341{
342 for(int i=0;i<OutputMultipleStrings;i++)
343 free(data[i]);
344}
345
346
347
John Kessenich54d8cda2013-02-11 22:36:01 +0000348void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +0000349{
350 printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" :
351 "#### %s %s INFO LOG ####\n", msg, name, num);
352}
353
John Kessenich54d8cda2013-02-11 22:36:01 +0000354int ShOutputMultipleStrings(const char *argv)
John Kessenicha0af4732012-12-12 21:15:54 +0000355{
John Kessenich54d8cda2013-02-11 22:36:01 +0000356 if(!(abs(OutputMultipleStrings = atoi(argv)))||((OutputMultipleStrings >5 || OutputMultipleStrings < 1)? 1:0)){
John Kessenicha0af4732012-12-12 21:15:54 +0000357 printf("Invalid Command Line Argument after -c option.\n"
358 "Usage: -c <integer> where integer =[1,5]\n"
359 "This option must be specified before the input file path\n");
360 return 0;
361 }
362 return 1;
363}