John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1 | // |
| 2 | //Copyright (C) 2002-2005 3Dlabs Inc. Ltd. |
John Kessenich | bd0747d | 2013-02-17 06:01:50 +0000 | [diff] [blame] | 3 | //Copyright (C) 2013 LunarG, Inc. |
| 4 | // |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 5 | //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 | // |
| 36 | #include "./../glslang/Include/ShHandle.h" |
| 37 | #include "./../glslang/Public/ShaderLang.h" |
| 38 | #include <string.h> |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 40 | #include <math.h> |
| 41 | |
| 42 | #ifdef _WIN32 |
| 43 | #include <windows.h> |
| 44 | #include <psapi.h> |
| 45 | #endif |
| 46 | |
| 47 | extern "C" { |
| 48 | SH_IMPORT_EXPORT void ShOutputHtml(); |
| 49 | } |
| 50 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 51 | // |
| 52 | // Return codes from main. |
| 53 | // |
| 54 | enum TFailCode { |
| 55 | ESuccess = 0, |
| 56 | EFailUsage, |
| 57 | EFailCompile, |
| 58 | EFailLink, |
| 59 | EFailCompilerCreate, |
| 60 | EFailLinkerCreate |
| 61 | }; |
| 62 | |
| 63 | // |
| 64 | // Just placeholders for testing purposes. The stand-alone environment |
| 65 | // can't actually do a full link without something specifying real |
| 66 | // attribute bindings. |
| 67 | // |
| 68 | ShBinding FixedAttributeBindings[] = { |
| 69 | { "gl_Vertex", 15 }, |
| 70 | { "gl_Color", 10 }, |
| 71 | { "gl_Normal", 7 }, |
| 72 | }; |
| 73 | |
| 74 | ShBindingTable FixedAttributeTable = { 3, FixedAttributeBindings }; |
| 75 | |
| 76 | static EShLanguage FindLanguage(char *lang); |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 77 | bool CompileFile(const char *fileName, ShHandle, int, const TBuiltInResource*); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 78 | void usage(); |
| 79 | void FreeFileData(char **data); |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 80 | char** ReadFileData(const char *fileName); |
| 81 | void InfoLogMsg(const char* msg, const char* name, const int num); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 82 | //Added to accomodate the multiple strings. |
| 83 | int OutputMultipleStrings = 1; |
| 84 | |
| 85 | // |
| 86 | // Set up the per compile resources |
| 87 | // |
| 88 | void GenerateResources(TBuiltInResource& resources) |
John Kessenich | b51f62c | 2013-04-11 16:31:09 +0000 | [diff] [blame] | 89 | { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 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; |
John Kessenich | bd0747d | 2013-02-17 06:01:50 +0000 | [diff] [blame] | 102 | resources.maxVertexUniformVectors = 128; |
| 103 | resources.maxVaryingVectors = 8; |
| 104 | resources.maxFragmentUniformVectors = 16; |
| 105 | resources.maxVertexOutputVectors = 16; |
John Kessenich | 1f2a36b | 2013-02-20 04:42:42 +0000 | [diff] [blame] | 106 | resources.maxFragmentInputVectors = 15; |
John Kessenich | bd0747d | 2013-02-17 06:01:50 +0000 | [diff] [blame] | 107 | resources.minProgramTexelOffset = -8; |
| 108 | resources.maxProgramTexelOffset = 7; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | int C_DECL main(int argc, char* argv[]) |
| 112 | { |
| 113 | bool delay = false; |
| 114 | int numCompilers = 0; |
| 115 | bool compileFailed = false; |
| 116 | bool linkFailed = false; |
| 117 | int debugOptions = 0; |
| 118 | int i; |
| 119 | |
| 120 | ShHandle linker = 0; |
| 121 | ShHandle uniformMap = 0; |
| 122 | ShHandle compilers[EShLangCount]; |
| 123 | |
| 124 | ShInitialize(); |
| 125 | |
| 126 | #ifdef _WIN32 |
| 127 | __try { |
| 128 | #endif |
| 129 | argc--; |
| 130 | argv++; |
| 131 | for (; argc >= 1; argc--, argv++) { |
John Kessenich | d7c120f | 2013-03-12 17:52:59 +0000 | [diff] [blame] | 132 | if (argv[0][0] == '-') { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 133 | switch (argv[0][1]) { |
John Kessenich | 8df53cc | 2013-04-14 19:23:50 +0000 | [diff] [blame] | 134 | case 'd': |
| 135 | delay = true; |
| 136 | break; |
| 137 | case 'i': |
| 138 | debugOptions |= EDebugOpIntermediate; |
| 139 | break; |
| 140 | case 'a': |
| 141 | debugOptions |= EDebugOpAssembly; |
| 142 | break; |
| 143 | case 'l': |
| 144 | debugOptions |= EDebugOpMemoryLeakMode; |
| 145 | break; |
| 146 | case 's': |
| 147 | debugOptions |= EDebugOpSuppressInfolog; |
| 148 | break; |
| 149 | case 't': |
| 150 | debugOptions |= EDebugOpTexturePrototypes; |
| 151 | break; |
| 152 | default: |
| 153 | usage(); |
| 154 | return EFailUsage; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 155 | } |
| 156 | } else { |
| 157 | compilers[numCompilers] = ShConstructCompiler(FindLanguage(argv[0]), debugOptions); |
| 158 | if (compilers[numCompilers] == 0) |
| 159 | return EFailCompilerCreate; |
| 160 | ++numCompilers; |
| 161 | |
| 162 | TBuiltInResource resources; |
| 163 | GenerateResources(resources); |
| 164 | if (! CompileFile(argv[0], compilers[numCompilers-1], debugOptions, &resources)) |
| 165 | compileFailed = true; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (!numCompilers) { |
| 170 | usage(); |
| 171 | return EFailUsage; |
| 172 | } |
| 173 | |
| 174 | linker = ShConstructLinker(EShExVertexFragment, debugOptions); |
| 175 | if (linker == 0) |
| 176 | return EFailLinkerCreate; |
| 177 | |
| 178 | uniformMap = ShConstructUniformMap(); |
| 179 | if (uniformMap == 0) |
| 180 | return EFailLinkerCreate; |
| 181 | |
| 182 | if (numCompilers > 0) { |
| 183 | ShSetFixedAttributeBindings(linker, &FixedAttributeTable); |
| 184 | if (! ShLink(linker, compilers, numCompilers, uniformMap, 0, 0)) |
| 185 | linkFailed = true; |
| 186 | } |
| 187 | |
John Kessenich | 8df53cc | 2013-04-14 19:23:50 +0000 | [diff] [blame] | 188 | if (! (debugOptions & EDebugOpSuppressInfolog)) { |
| 189 | for (i = 0; i < numCompilers; ++i) { |
| 190 | InfoLogMsg("BEGIN", "COMPILER", i); |
| 191 | puts(ShGetInfoLog(compilers[i])); |
| 192 | InfoLogMsg("END", "COMPILER", i); |
| 193 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 194 | |
John Kessenich | 8df53cc | 2013-04-14 19:23:50 +0000 | [diff] [blame] | 195 | InfoLogMsg("BEGIN", "LINKER", -1); |
| 196 | puts(ShGetInfoLog(linker)); |
| 197 | InfoLogMsg("END", "LINKER", -1); |
| 198 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 199 | |
| 200 | #ifdef _WIN32 |
| 201 | } __finally { |
| 202 | #endif |
| 203 | for (i = 0; i < numCompilers; ++i) |
| 204 | ShDestruct(compilers[i]); |
| 205 | |
| 206 | ShDestruct(linker); |
| 207 | ShDestruct(uniformMap); |
| 208 | |
| 209 | #ifdef _WIN32 |
| 210 | if (delay) |
| 211 | Sleep(1000000); |
| 212 | |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | if (compileFailed) |
| 217 | return EFailCompile; |
| 218 | if (linkFailed) |
| 219 | return EFailLink; |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | // |
| 225 | // Deduce the language from the filename. Files must end in one of the |
| 226 | // following extensions: |
| 227 | // |
| 228 | // .frag* = fragment programs |
| 229 | // .vert* = vertex programs |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 230 | // |
| 231 | static EShLanguage FindLanguage(char *name) |
| 232 | { |
| 233 | if (!name) |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 234 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 235 | |
| 236 | char *ext = strrchr(name, '.'); |
| 237 | |
| 238 | if (ext && strcmp(ext, ".sl") == 0) |
| 239 | for (; ext > name && ext[0] != '.'; ext--); |
| 240 | |
| 241 | if (ext = strrchr(name, '.')) { |
| 242 | if (strncmp(ext, ".frag", 4) == 0) return EShLangFragment; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 243 | } |
| 244 | |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 245 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
| 249 | // |
| 250 | // Read a file's data into a string, and compile it using ShCompile |
| 251 | // |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 252 | bool CompileFile(const char *fileName, ShHandle compiler, int debugOptions, const TBuiltInResource *resources) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 253 | { |
| 254 | int ret; |
| 255 | char **data = ReadFileData(fileName); |
John Kessenich | 09da79e | 2013-04-17 19:34:23 +0000 | [diff] [blame^] | 256 | |
| 257 | #ifdef _WIN32 |
John Kessenich | 8df53cc | 2013-04-14 19:23:50 +0000 | [diff] [blame] | 258 | PROCESS_MEMORY_COUNTERS counters; // just for memory leak testing |
John Kessenich | 09da79e | 2013-04-17 19:34:23 +0000 | [diff] [blame^] | 259 | #endif |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 260 | |
| 261 | if (!data) |
| 262 | return false; |
| 263 | |
John Kessenich | 8df53cc | 2013-04-14 19:23:50 +0000 | [diff] [blame] | 264 | for (int i = 0; i < ((debugOptions & EDebugOpMemoryLeakMode) ? 100 : 1); ++i) { |
| 265 | for (int j = 0; j < ((debugOptions & EDebugOpMemoryLeakMode) ? 100 : 1); ++j) |
John Kessenich | 3782702 | 2013-03-08 19:26:11 +0000 | [diff] [blame] | 266 | ret = ShCompile(compiler, data, OutputMultipleStrings, EShOptNone, resources, debugOptions, 100, false, EShMsgDefault); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 267 | |
John Kessenich | 09da79e | 2013-04-17 19:34:23 +0000 | [diff] [blame^] | 268 | #ifdef _WIN32 |
John Kessenich | 8df53cc | 2013-04-14 19:23:50 +0000 | [diff] [blame] | 269 | if (debugOptions & EDebugOpMemoryLeakMode) { |
| 270 | GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters)); |
| 271 | printf("Working set size: %d\n", counters.WorkingSetSize); |
| 272 | } |
John Kessenich | 09da79e | 2013-04-17 19:34:23 +0000 | [diff] [blame^] | 273 | #endif |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 274 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 275 | |
| 276 | FreeFileData(data); |
| 277 | |
| 278 | return ret ? true : false; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | // |
| 283 | // print usage to stdout |
| 284 | // |
| 285 | void usage() |
| 286 | { |
John Kessenich | 8df53cc | 2013-04-14 19:23:50 +0000 | [diff] [blame] | 287 | printf("Usage: standalone [ options ] filename\n" |
| 288 | "Where: filename = filename ending in .frag* or .vert*\n" |
| 289 | "-i: intermediate (glslang AST)\n" |
| 290 | "-a: assembly dump (LLVM IR)\n" |
| 291 | "-d: delay end (keeps output up in debugger, WIN32)\n" |
| 292 | "-l: memory leak mode\n" |
| 293 | "-s: silent mode (no info log)\n"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 294 | } |
| 295 | |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 296 | #ifndef _WIN32 |
| 297 | |
| 298 | #include <errno.h> |
| 299 | |
| 300 | int fopen_s( |
| 301 | FILE** pFile, |
| 302 | const char *filename, |
| 303 | const char *mode |
| 304 | ) |
| 305 | { |
| 306 | if (!pFile || !filename || !mode) { |
| 307 | return EINVAL; |
| 308 | } |
| 309 | |
| 310 | FILE* f = fopen(filename, mode); |
| 311 | if (! f) { |
| 312 | if (errno != 0) { |
| 313 | return errno; |
| 314 | } else { |
| 315 | return ENOENT; |
| 316 | } |
| 317 | } |
| 318 | *pFile = f; |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | #endif |
| 324 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 325 | // |
| 326 | // Malloc a string of sufficient size and read a string into it. |
| 327 | // |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 328 | char** ReadFileData(const char *fileName) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 329 | { |
John Kessenich | 200b273 | 2012-12-12 21:21:23 +0000 | [diff] [blame] | 330 | FILE *in; |
| 331 | int errorCode = fopen_s(&in, fileName, "r"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 332 | char *fdata; |
| 333 | int count = 0; |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 334 | const int maxSourceStrings = 5; |
| 335 | char** return_data = (char**)malloc(maxSourceStrings+1); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 336 | |
| 337 | //return_data[MAX_SOURCE_STRINGS]=NULL; |
John Kessenich | 200b273 | 2012-12-12 21:21:23 +0000 | [diff] [blame] | 338 | if (errorCode) { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 339 | printf("Error: unable to open input file: %s\n", fileName); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | while (fgetc(in) != EOF) |
| 344 | count++; |
| 345 | |
| 346 | fseek(in, 0, SEEK_SET); |
| 347 | |
| 348 | |
| 349 | if (!(fdata = (char *)malloc(count+2))) { |
| 350 | printf("Error allocating memory\n"); |
| 351 | return 0; |
| 352 | } |
| 353 | if (fread(fdata,1,count, in)!=count) { |
| 354 | printf("Error reading input file: %s\n", fileName); |
| 355 | return 0; |
| 356 | } |
| 357 | fdata[count] = '\0'; |
| 358 | fclose(in); |
| 359 | if(count==0){ |
| 360 | return_data[0]=(char*)malloc(count+2); |
| 361 | return_data[0][0]='\0'; |
| 362 | OutputMultipleStrings=0; |
| 363 | return return_data; |
| 364 | } |
| 365 | |
| 366 | int len = (int)(ceil)((float)count/(float)OutputMultipleStrings); |
| 367 | int ptr_len=0,i=0; |
| 368 | while(count>0){ |
| 369 | return_data[i]=(char*)malloc(len+2); |
| 370 | memcpy(return_data[i],fdata+ptr_len,len); |
| 371 | return_data[i][len]='\0'; |
| 372 | count-=(len); |
| 373 | ptr_len+=(len); |
| 374 | if(count<len){ |
| 375 | if(count==0){ |
| 376 | OutputMultipleStrings=(i+1); |
| 377 | break; |
| 378 | } |
| 379 | len = count; |
| 380 | } |
| 381 | ++i; |
| 382 | } |
| 383 | return return_data; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | |
| 388 | void FreeFileData(char **data) |
| 389 | { |
| 390 | for(int i=0;i<OutputMultipleStrings;i++) |
| 391 | free(data[i]); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 396 | void InfoLogMsg(const char* msg, const char* name, const int num) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 397 | { |
| 398 | printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" : |
| 399 | "#### %s %s INFO LOG ####\n", msg, name, num); |
| 400 | } |