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 | // |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 36 | |
| 37 | // this only applies to the standalone wrapper, not the front end in general |
| 38 | #define _CRT_SECURE_NO_WARNINGS |
| 39 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 40 | #include "Worklist.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 41 | #include "./../glslang/Include/ShHandle.h" |
| 42 | #include "./../glslang/Public/ShaderLang.h" |
| 43 | #include <string.h> |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 44 | #include <stdlib.h> |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 45 | #include <math.h> |
| 46 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 47 | #include "osinclude.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 48 | |
| 49 | extern "C" { |
| 50 | SH_IMPORT_EXPORT void ShOutputHtml(); |
| 51 | } |
| 52 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 53 | // Command-line options |
| 54 | enum TOptions { |
| 55 | EOptionNone = 0x000, |
| 56 | EOptionIntermediate = 0x001, |
| 57 | EOptionSuppressInfolog = 0x002, |
| 58 | EOptionMemoryLeakMode = 0x004, |
| 59 | EOptionRelaxedErrors = 0x008, |
| 60 | EOptionGiveWarnings = 0x010, |
| 61 | EOptionsLinkProgram = 0x020, |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 62 | EOptionMultiThreaded = 0x040, |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 63 | EOptionDumpConfig = 0x080, |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 66 | // |
| 67 | // Return codes from main. |
| 68 | // |
| 69 | enum TFailCode { |
| 70 | ESuccess = 0, |
| 71 | EFailUsage, |
| 72 | EFailCompile, |
| 73 | EFailLink, |
| 74 | EFailCompilerCreate, |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 75 | EFailThreadCreate, |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 76 | EFailLinkerCreate |
| 77 | }; |
| 78 | |
| 79 | // |
| 80 | // Just placeholders for testing purposes. The stand-alone environment |
| 81 | // can't actually do a full link without something specifying real |
| 82 | // attribute bindings. |
| 83 | // |
| 84 | ShBinding FixedAttributeBindings[] = { |
| 85 | { "gl_Vertex", 15 }, |
| 86 | { "gl_Color", 10 }, |
| 87 | { "gl_Normal", 7 }, |
| 88 | }; |
| 89 | |
| 90 | ShBindingTable FixedAttributeTable = { 3, FixedAttributeBindings }; |
| 91 | |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 92 | EShLanguage FindLanguage(const std::string& name); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 93 | bool CompileFile(const char *fileName, ShHandle, int options); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 94 | void usage(); |
| 95 | void FreeFileData(char **data); |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 96 | char** ReadFileData(const char *fileName); |
| 97 | void InfoLogMsg(const char* msg, const char* name, const int num); |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 98 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 99 | // Use to test breaking up a single shader file into multiple strings. |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 100 | int NumShaderStrings = 1; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 101 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 102 | TBuiltInResource Resources; |
| 103 | std::string ConfigFile; |
| 104 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 105 | // |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 106 | // These are the default resources for TBuiltInResources, used for both |
| 107 | // - parsing this string for the case where the user didn't supply one |
| 108 | // - dumping out a template for user construction of a config file |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 109 | // |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 110 | const char* DefaultConfig = |
| 111 | "MaxLights 32\n" |
| 112 | "MaxClipPlanes 6\n" |
| 113 | "MaxTextureUnits 32\n" |
| 114 | "MaxTextureCoords 32\n" |
| 115 | "MaxVertexAttribs 64\n" |
| 116 | "MaxVertexUniformComponents 4096\n" |
| 117 | "MaxVaryingFloats 64\n" |
| 118 | "MaxVertexTextureImageUnits 32\n" |
| 119 | "MaxCombinedTextureImageUnits 32\n" |
| 120 | "MaxTextureImageUnits 32\n" |
| 121 | "MaxFragmentUniformComponents 4096\n" |
| 122 | "MaxDrawBuffers 32\n" |
| 123 | "MaxVertexUniformVectors 128\n" |
| 124 | "MaxVaryingVectors 8\n" |
| 125 | "MaxFragmentUniformVectors 16\n" |
| 126 | "MaxVertexOutputVectors 16\n" |
| 127 | "MaxFragmentInputVectors 15\n" |
| 128 | "MinProgramTexelOffset -8\n" |
| 129 | "MaxProgramTexelOffset 7\n" |
| 130 | ; |
| 131 | |
| 132 | // |
| 133 | // Parse either a .conf file provided by the user or the default string above. |
| 134 | // |
| 135 | void ProcessConfigFile() |
John Kessenich | b51f62c | 2013-04-11 16:31:09 +0000 | [diff] [blame] | 136 | { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 137 | char** configStrings = 0; |
| 138 | char *config = 0; |
| 139 | if (ConfigFile.size() > 0) { |
| 140 | char** configStrings = ReadFileData(ConfigFile.c_str()); |
| 141 | if (configStrings) |
| 142 | config = *configStrings; |
| 143 | else { |
| 144 | printf("Error opening configuration file; will instead use the default configuration\n"); |
| 145 | usage(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (config == 0) { |
| 150 | config = new char[strlen(DefaultConfig)]; |
| 151 | strcpy(config, DefaultConfig); |
| 152 | } |
| 153 | |
| 154 | const char* delims = " \t\n\r"; |
| 155 | const char* token = strtok(config, delims); |
| 156 | while (token) { |
| 157 | const char* valueStr = strtok(0, delims); |
| 158 | if (valueStr == 0 || ! (valueStr[0] == '-' || (valueStr[0] >= '0' && valueStr[0] <= '9'))) { |
| 159 | printf("Error: '%s' bad .conf file. Each name must be followed by one number.\n", valueStr ? valueStr : ""); |
| 160 | return; |
| 161 | } |
| 162 | int value = atoi(valueStr); |
| 163 | |
| 164 | if (strcmp(token, "MaxLights") == 0) |
| 165 | Resources.maxLights = value; |
| 166 | else if (strcmp(token, "MaxClipPlanes") == 0) |
| 167 | Resources.maxClipPlanes = value; |
| 168 | else if (strcmp(token, "MaxTextureUnits") == 0) |
| 169 | Resources.maxTextureUnits = value; |
| 170 | else if (strcmp(token, "MaxTextureCoords") == 0) |
| 171 | Resources.maxTextureCoords = value; |
| 172 | else if (strcmp(token, "MaxVertexAttribs") == 0) |
| 173 | Resources.maxVertexAttribs = value; |
| 174 | else if (strcmp(token, "MaxVertexUniformComponents") == 0) |
| 175 | Resources.maxVertexUniformComponents = value; |
| 176 | else if (strcmp(token, "MaxVaryingFloats") == 0) |
| 177 | Resources.maxVaryingFloats = value; |
| 178 | else if (strcmp(token, "MaxVertexTextureImageUnits") == 0) |
| 179 | Resources.maxVertexTextureImageUnits = value; |
| 180 | else if (strcmp(token, "MaxCombinedTextureImageUnits") == 0) |
| 181 | Resources.maxCombinedTextureImageUnits = value; |
| 182 | else if (strcmp(token, "MaxTextureImageUnits") == 0) |
| 183 | Resources.maxTextureImageUnits = value; |
| 184 | else if (strcmp(token, "MaxFragmentUniformComponents") == 0) |
| 185 | Resources.maxFragmentUniformComponents = value; |
| 186 | else if (strcmp(token, "MaxDrawBuffers") == 0) |
| 187 | Resources.maxDrawBuffers = value; |
| 188 | else if (strcmp(token, "MaxVertexUniformVectors") == 0) |
| 189 | Resources.maxVertexUniformVectors = value; |
| 190 | else if (strcmp(token, "MaxVaryingVectors") == 0) |
| 191 | Resources.maxVaryingVectors = value; |
| 192 | else if (strcmp(token, "MaxFragmentUniformVectors") == 0) |
| 193 | Resources.maxFragmentUniformVectors = value; |
| 194 | else if (strcmp(token, "MaxVertexOutputVectors") == 0) |
| 195 | Resources.maxVertexOutputVectors = value; |
| 196 | else if (strcmp(token, "MaxFragmentInputVectors") == 0) |
| 197 | Resources.maxFragmentInputVectors = value; |
| 198 | else if (strcmp(token, "MinProgramTexelOffset") == 0) |
| 199 | Resources.minProgramTexelOffset = value; |
| 200 | else if (strcmp(token, "MaxProgramTexelOffset") == 0) |
| 201 | Resources.maxProgramTexelOffset = value; |
| 202 | else |
| 203 | printf("Warning: unrecognized limit (%s) in configuration file.\n", token); |
| 204 | |
| 205 | token = strtok(0, delims); |
| 206 | } |
| 207 | if (configStrings) |
| 208 | FreeFileData(configStrings); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 209 | } |
| 210 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 211 | // thread-safe list of shaders to asynchronously grab and compile |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 212 | glslang::TWorklist Worklist; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 213 | |
| 214 | // array of unique places to leave the shader names and infologs for the asynchronous compiles |
| 215 | glslang::TWorkItem **Work = 0; |
| 216 | int NumWorkItems = 0; |
| 217 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 218 | int Options = 0; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 219 | bool Delay = false; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 220 | const char* ExecutableName; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 221 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 222 | // |
| 223 | // *.conf => this is a config file that can set limits/resources |
| 224 | // |
| 225 | bool SetConfigFile(const std::string& name) |
| 226 | { |
| 227 | if (name.size() < 5) |
| 228 | return false; |
| 229 | |
| 230 | if (name.substr(name.size() - 5, std::string::npos) == ".conf") { |
| 231 | ConfigFile = name; |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | return false; |
| 236 | } |
| 237 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 238 | bool ProcessArguments(int argc, char* argv[]) |
| 239 | { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 240 | ExecutableName = argv[0]; |
| 241 | NumWorkItems = argc; // will include some empties where the '-' options were, but it doesn't matter, they'll be 0 |
| 242 | Work = new glslang::TWorkItem*[NumWorkItems]; |
| 243 | Work[0] = 0; |
| 244 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 245 | argc--; |
| 246 | argv++; |
| 247 | for (; argc >= 1; argc--, argv++) { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 248 | Work[argc] = 0; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 249 | if (argv[0][0] == '-') { |
| 250 | switch (argv[0][1]) { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 251 | case 'c': |
| 252 | Options |= EOptionDumpConfig; |
| 253 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 254 | case 'd': |
| 255 | Delay = true; |
| 256 | break; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 257 | case 'i': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 258 | Options |= EOptionIntermediate; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 259 | break; |
| 260 | case 'l': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 261 | Options |= EOptionsLinkProgram; |
| 262 | break; |
| 263 | case 'm': |
| 264 | Options |= EOptionMemoryLeakMode; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 265 | break; |
| 266 | case 'r': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 267 | Options |= EOptionRelaxedErrors; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 268 | break; |
| 269 | case 's': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 270 | Options |= EOptionSuppressInfolog; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 271 | break; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 272 | case 't': |
| 273 | #ifdef _WIN32 |
| 274 | Options |= EOptionMultiThreaded; |
| 275 | #endif |
| 276 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 277 | default: |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 278 | return false; |
| 279 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 280 | } else { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 281 | std::string name(argv[0]); |
| 282 | if (! SetConfigFile(name)) { |
| 283 | Work[argc] = new glslang::TWorkItem(name); |
| 284 | Worklist.add(Work[argc]); |
| 285 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 286 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | return true; |
| 290 | } |
| 291 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 292 | // Thread entry point, for non-linking asynchronous mode. |
John Kessenich | ee6a9c8 | 2013-07-31 23:19:17 +0000 | [diff] [blame] | 293 | unsigned int |
| 294 | #ifdef _WIN32 |
| 295 | __stdcall |
| 296 | #endif |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 297 | CompileShaders(void*) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 298 | { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 299 | glslang::TWorkItem* workItem; |
| 300 | while (Worklist.remove(workItem)) { |
| 301 | ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 302 | if (compiler == 0) |
| 303 | return false; |
| 304 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 305 | CompileFile(workItem->name.c_str(), compiler, Options); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 306 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 307 | if (! (Options & EOptionSuppressInfolog)) |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 308 | workItem->results = ShGetInfoLog(compiler); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 309 | |
| 310 | ShDestruct(compiler); |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 316 | // |
| 317 | // For linking mode: Will independently parse each item in the worklist, but then put them |
| 318 | // in the same program and link them together. |
| 319 | // |
| 320 | // Uses the new C++ interface instead of the old handle-based interface. |
| 321 | // |
| 322 | void CompileAndLinkShaders() |
| 323 | { |
| 324 | // keep track of what to free |
| 325 | std::list<glslang::TShader*> shaders; |
| 326 | |
| 327 | EShMessages messages = EShMsgDefault; |
| 328 | if (Options & EOptionRelaxedErrors) |
| 329 | messages = (EShMessages)(messages | EShMsgRelaxedErrors); |
| 330 | if (Options & EOptionIntermediate) |
| 331 | messages = (EShMessages)(messages | EShMsgAST); |
| 332 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 333 | // |
| 334 | // Per-shader processing... |
| 335 | // |
| 336 | |
| 337 | glslang::TProgram program; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 338 | glslang::TWorkItem* workItem; |
| 339 | while (Worklist.remove(workItem)) { |
| 340 | EShLanguage stage = FindLanguage(workItem->name); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 341 | glslang::TShader* shader = new glslang::TShader(stage); |
| 342 | shaders.push_back(shader); |
| 343 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 344 | char** shaderStrings = ReadFileData(workItem->name.c_str()); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 345 | if (! shaderStrings) { |
| 346 | usage(); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | shader->setStrings(shaderStrings, 1); |
| 351 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 352 | shader->parse(&Resources, 100, false, messages); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 353 | |
| 354 | program.addShader(shader); |
| 355 | |
| 356 | if (! (Options & EOptionSuppressInfolog)) { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 357 | puts(workItem->name.c_str()); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 358 | puts(shader->getInfoLog()); |
| 359 | puts(shader->getInfoDebugLog()); |
| 360 | } |
| 361 | |
| 362 | FreeFileData(shaderStrings); |
| 363 | } |
| 364 | |
| 365 | // |
| 366 | // Program-level processing... |
| 367 | // |
| 368 | |
| 369 | program.link(messages); |
| 370 | if (! (Options & EOptionSuppressInfolog)) { |
| 371 | puts(program.getInfoLog()); |
| 372 | puts(program.getInfoDebugLog()); |
| 373 | } |
| 374 | |
| 375 | // free everything up |
| 376 | while (shaders.size() > 0) { |
| 377 | delete shaders.back(); |
| 378 | shaders.pop_back(); |
| 379 | } |
| 380 | |
| 381 | // TODO: memory: for each compile, need a GetThreadPoolAllocator().pop(); |
| 382 | } |
| 383 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 384 | int C_DECL main(int argc, char* argv[]) |
| 385 | { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 386 | bool compileFailed = false; |
| 387 | bool linkFailed = false; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 388 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 389 | // Init for front-end proper |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 390 | ShInitialize(); |
| 391 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 392 | // Init for standalone |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 393 | glslang::InitGlobalLock(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 394 | |
John Kessenich | 54f6e56 | 2013-08-03 00:04:10 +0000 | [diff] [blame] | 395 | if (! ProcessArguments(argc, argv)) { |
| 396 | usage(); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 397 | return EFailUsage; |
John Kessenich | 54f6e56 | 2013-08-03 00:04:10 +0000 | [diff] [blame] | 398 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 399 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 400 | if (Options & EOptionDumpConfig) { |
| 401 | printf("%s", DefaultConfig); |
| 402 | if (Worklist.empty()) |
| 403 | return ESuccess; |
| 404 | } |
| 405 | |
| 406 | if (Worklist.empty()) { |
| 407 | usage(); |
| 408 | return EFailUsage; |
| 409 | } |
| 410 | |
| 411 | ProcessConfigFile(); |
| 412 | |
| 413 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 414 | // |
| 415 | // Two modes: |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 416 | // 1) linking all arguments together, single-threaded, new C++ interface |
| 417 | // 2) independent arguments, can be tackled by multiple asynchronous threads, for testing thread safety, using the old handle interface |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 418 | // |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 419 | if (Options & EOptionsLinkProgram) |
| 420 | CompileAndLinkShaders(); |
| 421 | else { |
| 422 | bool printShaderNames = Worklist.size() > 1; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 423 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 424 | if (Options & EOptionMultiThreaded) { |
| 425 | const int NumThreads = 16; |
| 426 | void* threads[NumThreads]; |
| 427 | for (int t = 0; t < NumThreads; ++t) { |
| 428 | threads[t] = glslang::OS_CreateThread(&CompileShaders); |
| 429 | if (! threads[t]) { |
| 430 | printf("Failed to create thread\n"); |
| 431 | return EFailThreadCreate; |
| 432 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 433 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 434 | glslang::OS_WaitForAllThreads(threads, NumThreads); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 435 | } else { |
| 436 | if (! CompileShaders(0)) |
| 437 | compileFailed = true; |
| 438 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 439 | |
| 440 | // Print out all the resulting infologs |
| 441 | for (int w = 0; w < NumWorkItems; ++w) { |
| 442 | if (Work[w]) { |
| 443 | if (printShaderNames) |
| 444 | puts(Work[w]->name.c_str()); |
| 445 | puts(Work[w]->results.c_str()); |
| 446 | delete Work[w]; |
| 447 | } |
| 448 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 449 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 450 | |
| 451 | if (Delay) |
| 452 | glslang::OS_Sleep(1000000); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 453 | |
| 454 | if (compileFailed) |
| 455 | return EFailCompile; |
| 456 | if (linkFailed) |
| 457 | return EFailLink; |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | // |
| 463 | // Deduce the language from the filename. Files must end in one of the |
| 464 | // following extensions: |
| 465 | // |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 466 | // .vert = vertex |
| 467 | // .tesc = tessellation control |
| 468 | // .tese = tessellation evaluation |
| 469 | // .geom = geometry |
| 470 | // .frag = fragment |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 471 | // .comp = compute |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 472 | // |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 473 | EShLanguage FindLanguage(const std::string& name) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 474 | { |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 475 | size_t ext = name.rfind('.'); |
| 476 | if (ext == std::string::npos) { |
| 477 | usage(); |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 478 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 479 | } |
| 480 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 481 | std::string suffix = name.substr(ext + 1, std::string::npos); |
| 482 | if (suffix == "vert") |
| 483 | return EShLangVertex; |
| 484 | else if (suffix == "tesc") |
| 485 | return EShLangTessControl; |
| 486 | else if (suffix == "tese") |
| 487 | return EShLangTessEvaluation; |
| 488 | else if (suffix == "geom") |
| 489 | return EShLangGeometry; |
| 490 | else if (suffix == "frag") |
| 491 | return EShLangFragment; |
John Kessenich | c027579 | 2013-08-09 17:14:49 +0000 | [diff] [blame] | 492 | else if (suffix == "comp") |
| 493 | return EShLangCompute; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 494 | |
| 495 | usage(); |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 496 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 497 | } |
| 498 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 499 | // |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 500 | // Read a file's data into a string, and compile it using the old interface ShCompile, |
| 501 | // for non-linkable results. |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 502 | // |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 503 | bool CompileFile(const char *fileName, ShHandle compiler, int Options) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 504 | { |
| 505 | int ret; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 506 | char** shaderStrings = ReadFileData(fileName); |
John Kessenich | db4cd54 | 2013-06-26 22:42:55 +0000 | [diff] [blame] | 507 | if (! shaderStrings) { |
| 508 | usage(); |
| 509 | return false; |
| 510 | } |
| 511 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 512 | int* lengths = new int[NumShaderStrings]; |
| 513 | |
| 514 | // move to length-based strings, rather than null-terminated strings |
| 515 | for (int s = 0; s < NumShaderStrings; ++s) |
| 516 | lengths[s] = strlen(shaderStrings[s]); |
John Kessenich | 09da79e | 2013-04-17 19:34:23 +0000 | [diff] [blame] | 517 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 518 | if (! shaderStrings) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 519 | return false; |
| 520 | |
John Kessenich | 52ac67e | 2013-05-05 23:46:22 +0000 | [diff] [blame] | 521 | EShMessages messages = EShMsgDefault; |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 522 | if (Options & EOptionRelaxedErrors) |
John Kessenich | 52ac67e | 2013-05-05 23:46:22 +0000 | [diff] [blame] | 523 | messages = (EShMessages)(messages | EShMsgRelaxedErrors); |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 524 | if (Options & EOptionIntermediate) |
| 525 | messages = (EShMessages)(messages | EShMsgAST); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 526 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 527 | for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) { |
| 528 | for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 529 | //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, 100, false, messages); |
| 530 | ret = ShCompile(compiler, shaderStrings, NumShaderStrings, 0, EShOptNone, &Resources, Options, 100, false, messages); |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 531 | //const char* multi[4] = { "# ve", "rsion", " 300 e", "s" }; |
| 532 | //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" }; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 533 | //ret = ShCompile(compiler, multi, 4, 0, EShOptNone, &Resources, Options, 100, false, messages); |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 534 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 535 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 536 | if (Options & EOptionMemoryLeakMode) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 537 | glslang::OS_DumpMemoryCounters(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 538 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 539 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 540 | delete [] lengths; |
| 541 | FreeFileData(shaderStrings); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 542 | |
| 543 | return ret ? true : false; |
| 544 | } |
| 545 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 546 | // |
| 547 | // print usage to stdout |
| 548 | // |
| 549 | void usage() |
| 550 | { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 551 | printf("Usage: glslangValidator [ options ] filename\n" |
John Kessenich | c027579 | 2013-08-09 17:14:49 +0000 | [diff] [blame] | 552 | "Where: filename is a name ending in\n" |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 553 | " .conf provides an optional config file that replaces the default configuration\n" |
| 554 | " (see -c option below for generating a template)\n" |
John Kessenich | c027579 | 2013-08-09 17:14:49 +0000 | [diff] [blame] | 555 | " .vert for a vertex shader\n" |
| 556 | " .tesc for a tessellation control shader\n" |
| 557 | " .tese for a tessellation evaluation shader\n" |
| 558 | " .geom for a geometry shader\n" |
| 559 | " .frag for a fragment shader\n" |
| 560 | " .comp for a compute shader\n\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 561 | "Compilation warnings and errors will be printed to stdout.\n" |
| 562 | "To get other information, use one of the following options:\n" |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame^] | 563 | "-c: configuration dump; use to create default configuration file (redirect to a .conf file)\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 564 | "-i: intermediate tree (glslang AST) is printed out\n" |
| 565 | "-d: delay exit\n" |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 566 | "-l: link validation of all input files\n" |
| 567 | "-m: memory leak mode\n" |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 568 | "-r: relaxed semantic error-checking mode\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 569 | "-s: silent mode\n" |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 570 | "-t: multi-threaded mode\n"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 571 | } |
| 572 | |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 573 | #ifndef _WIN32 |
| 574 | |
| 575 | #include <errno.h> |
| 576 | |
| 577 | int fopen_s( |
| 578 | FILE** pFile, |
| 579 | const char *filename, |
| 580 | const char *mode |
| 581 | ) |
| 582 | { |
| 583 | if (!pFile || !filename || !mode) { |
| 584 | return EINVAL; |
| 585 | } |
| 586 | |
| 587 | FILE* f = fopen(filename, mode); |
| 588 | if (! f) { |
| 589 | if (errno != 0) { |
| 590 | return errno; |
| 591 | } else { |
| 592 | return ENOENT; |
| 593 | } |
| 594 | } |
| 595 | *pFile = f; |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | #endif |
| 601 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 602 | // |
| 603 | // Malloc a string of sufficient size and read a string into it. |
| 604 | // |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 605 | char** ReadFileData(const char *fileName) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 606 | { |
John Kessenich | 200b273 | 2012-12-12 21:21:23 +0000 | [diff] [blame] | 607 | FILE *in; |
| 608 | int errorCode = fopen_s(&in, fileName, "r"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 609 | char *fdata; |
| 610 | int count = 0; |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 611 | const int maxSourceStrings = 5; |
| 612 | char** return_data = (char**)malloc(maxSourceStrings+1); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 613 | |
| 614 | //return_data[MAX_SOURCE_STRINGS]=NULL; |
John Kessenich | 200b273 | 2012-12-12 21:21:23 +0000 | [diff] [blame] | 615 | if (errorCode) { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 616 | printf("Error: unable to open input file: %s\n", fileName); |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | while (fgetc(in) != EOF) |
| 621 | count++; |
| 622 | |
| 623 | fseek(in, 0, SEEK_SET); |
| 624 | |
| 625 | |
| 626 | if (!(fdata = (char *)malloc(count+2))) { |
| 627 | printf("Error allocating memory\n"); |
| 628 | return 0; |
| 629 | } |
| 630 | if (fread(fdata,1,count, in)!=count) { |
| 631 | printf("Error reading input file: %s\n", fileName); |
| 632 | return 0; |
| 633 | } |
| 634 | fdata[count] = '\0'; |
| 635 | fclose(in); |
| 636 | if(count==0){ |
| 637 | return_data[0]=(char*)malloc(count+2); |
| 638 | return_data[0][0]='\0'; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 639 | NumShaderStrings=0; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 640 | return return_data; |
| 641 | } |
| 642 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 643 | int len = (int)(ceil)((float)count/(float)NumShaderStrings); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 644 | int ptr_len=0,i=0; |
| 645 | while(count>0){ |
| 646 | return_data[i]=(char*)malloc(len+2); |
| 647 | memcpy(return_data[i],fdata+ptr_len,len); |
| 648 | return_data[i][len]='\0'; |
| 649 | count-=(len); |
| 650 | ptr_len+=(len); |
| 651 | if(count<len){ |
| 652 | if(count==0){ |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 653 | NumShaderStrings=(i+1); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 654 | break; |
| 655 | } |
| 656 | len = count; |
| 657 | } |
| 658 | ++i; |
| 659 | } |
| 660 | return return_data; |
| 661 | } |
| 662 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 663 | void FreeFileData(char **data) |
| 664 | { |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 665 | for(int i=0;i<NumShaderStrings;i++) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 666 | free(data[i]); |
| 667 | } |
| 668 | |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 669 | void InfoLogMsg(const char* msg, const char* name, const int num) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 670 | { |
| 671 | printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" : |
| 672 | "#### %s %s INFO LOG ####\n", msg, name, num); |
| 673 | } |