John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1 | // |
| 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 | |
| 44 | extern "C" { |
| 45 | SH_IMPORT_EXPORT void ShOutputHtml(); |
| 46 | } |
| 47 | |
| 48 | //#define MEASURE_MEMORY |
| 49 | |
| 50 | // |
| 51 | // Return codes from main. |
| 52 | // |
| 53 | enum 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 | // |
| 67 | ShBinding FixedAttributeBindings[] = { |
| 68 | { "gl_Vertex", 15 }, |
| 69 | { "gl_Color", 10 }, |
| 70 | { "gl_Normal", 7 }, |
| 71 | }; |
| 72 | |
| 73 | ShBindingTable FixedAttributeTable = { 3, FixedAttributeBindings }; |
| 74 | |
| 75 | static EShLanguage FindLanguage(char *lang); |
| 76 | bool CompileFile(char *fileName, ShHandle, int, const TBuiltInResource*); |
| 77 | void usage(); |
| 78 | void FreeFileData(char **data); |
| 79 | char** ReadFileData(char *fileName); |
| 80 | void InfoLogMsg(char* msg, const char* name, const int num); |
| 81 | int ShOutputMultipleStrings(char ** ); |
| 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) |
| 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 | |
| 104 | int 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 Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 215 | // |
| 216 | static EShLanguage FindLanguage(char *name) |
| 217 | { |
| 218 | if (!name) |
| 219 | return EShLangFragment; |
| 220 | |
| 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; |
| 228 | if (strncmp(ext, ".vert", 4) == 0) return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return EShLangFragment; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | // |
| 236 | // Read a file's data into a string, and compile it using ShCompile |
| 237 | // |
| 238 | bool CompileFile(char *fileName, ShHandle compiler, int debugOptions, const TBuiltInResource *resources) |
| 239 | { |
| 240 | int ret; |
| 241 | char **data = ReadFileData(fileName); |
| 242 | |
| 243 | #ifdef MEASURE_MEMORY |
| 244 | PROCESS_MEMORY_COUNTERS counters; |
| 245 | #endif |
| 246 | |
| 247 | if (!data) |
| 248 | return false; |
| 249 | |
| 250 | #ifdef MEASURE_MEMORY |
| 251 | for (int i = 0; i < 1000; ++i) { |
| 252 | for (int j = 0; j < 100; ++j) |
| 253 | #endif |
| 254 | ret = ShCompile(compiler, data, OutputMultipleStrings, EShOptNone, resources, debugOptions); |
| 255 | #ifdef MEASURE_MEMORY |
| 256 | |
| 257 | GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters)); |
| 258 | } |
| 259 | #endif |
| 260 | |
| 261 | FreeFileData(data); |
| 262 | |
| 263 | return ret ? true : false; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | // |
| 268 | // print usage to stdout |
| 269 | // |
| 270 | void 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 |
| 281 | char** ReadFileData(char *fileName) |
| 282 | { |
John Kessenich | 200b273 | 2012-12-12 21:21:23 +0000 | [diff] [blame] | 283 | FILE *in; |
| 284 | int errorCode = fopen_s(&in, fileName, "r"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 285 | 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 Kessenich | 200b273 | 2012-12-12 21:21:23 +0000 | [diff] [blame] | 290 | if (errorCode) { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 291 | 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 | |
| 340 | void FreeFileData(char **data) |
| 341 | { |
| 342 | for(int i=0;i<OutputMultipleStrings;i++) |
| 343 | free(data[i]); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | |
| 348 | void InfoLogMsg(char* msg, const char* name, const int num) |
| 349 | { |
| 350 | printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" : |
| 351 | "#### %s %s INFO LOG ####\n", msg, name, num); |
| 352 | } |
| 353 | |
| 354 | int ShOutputMultipleStrings(char **argv) |
| 355 | { |
| 356 | if(!(abs(OutputMultipleStrings = atoi(*argv)))||((OutputMultipleStrings >5 || OutputMultipleStrings < 1)? 1:0)){ |
| 357 | 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 | } |