John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1 | // |
| 2 | //Copyright (C) 2002-2005 3Dlabs Inc. Ltd. |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 3 | //Copyright (C) 2013-2016 LunarG, Inc. |
John Kessenich | bd0747d | 2013-02-17 06:01:50 +0000 | [diff] [blame] | 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 | |
Lei Zhang | 8a9b1ee | 2016-05-19 13:31:43 -0400 | [diff] [blame] | 40 | #include "ResourceLimits.h" |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 41 | #include "Worklist.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 42 | #include "./../glslang/Include/ShHandle.h" |
John Kessenich | 0da9eaa | 2015-08-01 17:10:02 -0600 | [diff] [blame] | 43 | #include "./../glslang/Include/revision.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 44 | #include "./../glslang/Public/ShaderLang.h" |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 45 | #include "../SPIRV/GlslangToSpv.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 46 | #include "../SPIRV/GLSL.std.450.h" |
John Kessenich | acba772 | 2015-03-04 03:48:38 +0000 | [diff] [blame] | 47 | #include "../SPIRV/doc.h" |
| 48 | #include "../SPIRV/disassemble.h" |
John Kessenich | 66ec80e | 2016-08-05 14:04:23 -0600 | [diff] [blame] | 49 | #include <cstring> |
| 50 | #include <cstdlib> |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 51 | #include <cctype> |
John Kessenich | 66ec80e | 2016-08-05 14:04:23 -0600 | [diff] [blame] | 52 | #include <cmath> |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 53 | #include <array> |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 54 | |
baldurk | 876a0e3 | 2015-11-16 18:03:28 +0100 | [diff] [blame] | 55 | #include "../glslang/OSDependent/osinclude.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 56 | |
| 57 | extern "C" { |
| 58 | SH_IMPORT_EXPORT void ShOutputHtml(); |
| 59 | } |
| 60 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 61 | // Command-line options |
| 62 | enum TOptions { |
John Kessenich | a86836e | 2016-07-09 14:50:57 -0600 | [diff] [blame] | 63 | EOptionNone = 0, |
| 64 | EOptionIntermediate = (1 << 0), |
| 65 | EOptionSuppressInfolog = (1 << 1), |
| 66 | EOptionMemoryLeakMode = (1 << 2), |
| 67 | EOptionRelaxedErrors = (1 << 3), |
| 68 | EOptionGiveWarnings = (1 << 4), |
| 69 | EOptionLinkProgram = (1 << 5), |
| 70 | EOptionMultiThreaded = (1 << 6), |
| 71 | EOptionDumpConfig = (1 << 7), |
| 72 | EOptionDumpReflection = (1 << 8), |
| 73 | EOptionSuppressWarnings = (1 << 9), |
| 74 | EOptionDumpVersions = (1 << 10), |
| 75 | EOptionSpv = (1 << 11), |
| 76 | EOptionHumanReadableSpv = (1 << 12), |
| 77 | EOptionVulkanRules = (1 << 13), |
| 78 | EOptionDefaultDesktop = (1 << 14), |
| 79 | EOptionOutputPreprocessed = (1 << 15), |
| 80 | EOptionOutputHexadecimal = (1 << 16), |
| 81 | EOptionReadHlsl = (1 << 17), |
| 82 | EOptionCascadingErrors = (1 << 18), |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 83 | EOptionAutoMapBindings = (1 << 19), |
steve-lunarg | e0b9deb | 2016-09-16 13:26:37 -0600 | [diff] [blame] | 84 | EOptionFlattenUniformArrays = (1 << 20), |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 87 | // |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 88 | // Return codes from main/exit(). |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 89 | // |
| 90 | enum TFailCode { |
| 91 | ESuccess = 0, |
| 92 | EFailUsage, |
| 93 | EFailCompile, |
| 94 | EFailLink, |
| 95 | EFailCompilerCreate, |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 96 | EFailThreadCreate, |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 97 | EFailLinkerCreate |
| 98 | }; |
| 99 | |
| 100 | // |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 101 | // Forward declarations. |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 102 | // |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 103 | EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true); |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 104 | void CompileFile(const char* fileName, ShHandle); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 105 | void usage(); |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 106 | void FreeFileData(char** data); |
| 107 | char** ReadFileData(const char* fileName); |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 108 | void InfoLogMsg(const char* msg, const char* name, const int num); |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 109 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 110 | // Globally track if any compile or link failure. |
| 111 | bool CompileFailed = false; |
| 112 | bool LinkFailed = false; |
| 113 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 114 | // Use to test breaking up a single shader file into multiple strings. |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 115 | // Set in ReadFileData(). |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 116 | int NumShaderStrings; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 117 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 118 | TBuiltInResource Resources; |
| 119 | std::string ConfigFile; |
| 120 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 121 | // |
John Kessenich | f0bcb0a | 2016-04-02 13:09:14 -0600 | [diff] [blame] | 122 | // Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 123 | // |
| 124 | void ProcessConfigFile() |
John Kessenich | b51f62c | 2013-04-11 16:31:09 +0000 | [diff] [blame] | 125 | { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 126 | char** configStrings = 0; |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 127 | char* config = 0; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 128 | if (ConfigFile.size() > 0) { |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 129 | configStrings = ReadFileData(ConfigFile.c_str()); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 130 | if (configStrings) |
| 131 | config = *configStrings; |
| 132 | else { |
| 133 | printf("Error opening configuration file; will instead use the default configuration\n"); |
| 134 | usage(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (config == 0) { |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 139 | Resources = glslang::DefaultTBuiltInResource; |
| 140 | return; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Lei Zhang | 1b14172 | 2016-05-19 13:50:49 -0400 | [diff] [blame] | 143 | glslang::DecodeResourceLimits(&Resources, config); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 144 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 145 | if (configStrings) |
| 146 | FreeFileData(configStrings); |
Andrew Woloszyn | b891c2b | 2016-01-18 09:26:25 -0500 | [diff] [blame] | 147 | else |
| 148 | delete[] config; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 149 | } |
| 150 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 151 | // thread-safe list of shaders to asynchronously grab and compile |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 152 | glslang::TWorklist Worklist; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 153 | |
| 154 | // array of unique places to leave the shader names and infologs for the asynchronous compiles |
John Kessenich | fd30542 | 2014-06-05 16:30:53 +0000 | [diff] [blame] | 155 | glslang::TWorkItem** Work = 0; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 156 | int NumWorkItems = 0; |
| 157 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 158 | int Options = 0; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 159 | const char* ExecutableName = nullptr; |
| 160 | const char* binaryFileName = nullptr; |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 161 | const char* entryPointName = nullptr; |
Dan Baker | c6ede89 | 2016-08-11 14:06:06 -0400 | [diff] [blame] | 162 | const char* shaderStageName = nullptr; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 163 | |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 164 | std::array<unsigned int, EShLangCount> baseSamplerBinding; |
| 165 | std::array<unsigned int, EShLangCount> baseTextureBinding; |
| 166 | std::array<unsigned int, EShLangCount> baseUboBinding; |
| 167 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 168 | // |
| 169 | // Create the default name for saving a binary if -o is not provided. |
| 170 | // |
| 171 | const char* GetBinaryName(EShLanguage stage) |
| 172 | { |
| 173 | const char* name; |
| 174 | if (binaryFileName == nullptr) { |
| 175 | switch (stage) { |
| 176 | case EShLangVertex: name = "vert.spv"; break; |
| 177 | case EShLangTessControl: name = "tesc.spv"; break; |
| 178 | case EShLangTessEvaluation: name = "tese.spv"; break; |
| 179 | case EShLangGeometry: name = "geom.spv"; break; |
| 180 | case EShLangFragment: name = "frag.spv"; break; |
| 181 | case EShLangCompute: name = "comp.spv"; break; |
| 182 | default: name = "unknown"; break; |
| 183 | } |
| 184 | } else |
| 185 | name = binaryFileName; |
| 186 | |
| 187 | return name; |
| 188 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 189 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 190 | // |
| 191 | // *.conf => this is a config file that can set limits/resources |
| 192 | // |
| 193 | bool SetConfigFile(const std::string& name) |
| 194 | { |
| 195 | if (name.size() < 5) |
| 196 | return false; |
| 197 | |
John Kessenich | 4c70685 | 2013-10-11 16:28:43 +0000 | [diff] [blame] | 198 | if (name.compare(name.size() - 5, 5, ".conf") == 0) { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 199 | ConfigFile = name; |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | return false; |
| 204 | } |
| 205 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 206 | // |
| 207 | // Give error and exit with failure code. |
| 208 | // |
| 209 | void Error(const char* message) |
| 210 | { |
| 211 | printf("%s: Error %s (use -h for usage)\n", ExecutableName, message); |
| 212 | exit(EFailUsage); |
| 213 | } |
| 214 | |
| 215 | // |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 216 | // Process an optional binding base of the form: |
| 217 | // --argname [stage] base |
| 218 | // Where stage is one of the forms accepted by FindLanguage, and base is an integer |
| 219 | // |
| 220 | void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base) |
| 221 | { |
| 222 | if (argc < 2) |
| 223 | usage(); |
| 224 | |
| 225 | if (!isdigit(argv[1][0])) { |
| 226 | if (argc < 3) // this form needs one more argument |
| 227 | usage(); |
| 228 | |
| 229 | // Parse form: --argname stage base |
| 230 | const EShLanguage lang = FindLanguage(argv[1], false); |
| 231 | base[lang] = atoi(argv[2]); |
| 232 | argc-= 2; |
| 233 | argv+= 2; |
| 234 | } else { |
| 235 | // Parse form: --argname base |
| 236 | for (int lang=0; lang<EShLangCount; ++lang) |
| 237 | base[lang] = atoi(argv[1]); |
| 238 | |
| 239 | argc--; |
| 240 | argv++; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 245 | // Do all command-line argument parsing. This includes building up the work-items |
| 246 | // to be processed later, and saving all the command-line options. |
| 247 | // |
| 248 | // Does not return (it exits) if command-line is fatally flawed. |
| 249 | // |
| 250 | void ProcessArguments(int argc, char* argv[]) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 251 | { |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 252 | baseSamplerBinding.fill(0); |
| 253 | baseTextureBinding.fill(0); |
| 254 | baseUboBinding.fill(0); |
| 255 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 256 | ExecutableName = argv[0]; |
| 257 | NumWorkItems = argc; // will include some empties where the '-' options were, but it doesn't matter, they'll be 0 |
John Kessenich | fd30542 | 2014-06-05 16:30:53 +0000 | [diff] [blame] | 258 | Work = new glslang::TWorkItem*[NumWorkItems]; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 259 | for (int w = 0; w < NumWorkItems; ++w) |
| 260 | Work[w] = 0; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 261 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 262 | argc--; |
| 263 | argv++; |
| 264 | for (; argc >= 1; argc--, argv++) { |
| 265 | if (argv[0][0] == '-') { |
John Kessenich | 2aa7f3a | 2015-05-15 16:02:07 +0000 | [diff] [blame] | 266 | switch (argv[0][1]) { |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 267 | case '-': |
| 268 | { |
| 269 | std::string lowerword(argv[0]+2); |
| 270 | std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower); |
| 271 | |
| 272 | // handle --word style options |
| 273 | if (lowerword == "shift-sampler-bindings" || // synonyms |
| 274 | lowerword == "shift-sampler-binding" || |
| 275 | lowerword == "ssb") { |
| 276 | ProcessBindingBase(argc, argv, baseSamplerBinding); |
| 277 | } else if (lowerword == "shift-texture-bindings" || // synonyms |
| 278 | lowerword == "shift-texture-binding" || |
| 279 | lowerword == "stb") { |
| 280 | ProcessBindingBase(argc, argv, baseTextureBinding); |
| 281 | } else if (lowerword == "shift-ubo-bindings" || // synonyms |
| 282 | lowerword == "shift-ubo-binding" || |
| 283 | lowerword == "sub") { |
| 284 | ProcessBindingBase(argc, argv, baseUboBinding); |
| 285 | } else if (lowerword == "auto-map-bindings" || // synonyms |
| 286 | lowerword == "auto-map-binding" || |
| 287 | lowerword == "amb") { |
| 288 | Options |= EOptionAutoMapBindings; |
steve-lunarg | e0b9deb | 2016-09-16 13:26:37 -0600 | [diff] [blame] | 289 | } else if (lowerword == "flatten-uniform-arrays" || // synonyms |
| 290 | lowerword == "flatten-uniform-array" || |
| 291 | lowerword == "fua") { |
| 292 | Options |= EOptionFlattenUniformArrays; |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 293 | } else { |
| 294 | usage(); |
| 295 | } |
| 296 | } |
| 297 | break; |
John Kessenich | 2aa7f3a | 2015-05-15 16:02:07 +0000 | [diff] [blame] | 298 | case 'H': |
| 299 | Options |= EOptionHumanReadableSpv; |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 300 | if ((Options & EOptionSpv) == 0) { |
| 301 | // default to Vulkan |
| 302 | Options |= EOptionSpv; |
| 303 | Options |= EOptionVulkanRules; |
| 304 | Options |= EOptionLinkProgram; |
| 305 | } |
| 306 | break; |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 307 | case 'V': |
| 308 | Options |= EOptionSpv; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 309 | Options |= EOptionVulkanRules; |
| 310 | Options |= EOptionLinkProgram; |
| 311 | break; |
Dan Baker | 5afdd78 | 2016-08-11 17:53:57 -0400 | [diff] [blame] | 312 | case 'S': |
Dan Baker | c6ede89 | 2016-08-11 14:06:06 -0400 | [diff] [blame] | 313 | shaderStageName = argv[1]; |
dankbaker | 45d49bc | 2016-08-08 21:43:07 -0400 | [diff] [blame] | 314 | if (argc > 0) { |
| 315 | argc--; |
| 316 | argv++; |
| 317 | } |
| 318 | else |
Dan Baker | 5afdd78 | 2016-08-11 17:53:57 -0400 | [diff] [blame] | 319 | Error("no <stage> specified for -S"); |
dankbaker | 45d49bc | 2016-08-08 21:43:07 -0400 | [diff] [blame] | 320 | break; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 321 | case 'G': |
| 322 | Options |= EOptionSpv; |
John Kessenich | d78e351 | 2014-08-25 20:07:55 +0000 | [diff] [blame] | 323 | Options |= EOptionLinkProgram; |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 324 | // undo a -H default to Vulkan |
| 325 | Options &= ~EOptionVulkanRules; |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 326 | break; |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 327 | case 'E': |
| 328 | Options |= EOptionOutputPreprocessed; |
| 329 | break; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 330 | case 'c': |
| 331 | Options |= EOptionDumpConfig; |
| 332 | break; |
John Kessenich | a86836e | 2016-07-09 14:50:57 -0600 | [diff] [blame] | 333 | case 'C': |
| 334 | Options |= EOptionCascadingErrors; |
| 335 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 336 | case 'd': |
John Kessenich | 26ad268 | 2014-08-13 20:17:19 +0000 | [diff] [blame] | 337 | Options |= EOptionDefaultDesktop; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 338 | break; |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 339 | case 'D': |
| 340 | Options |= EOptionReadHlsl; |
| 341 | break; |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 342 | case 'e': |
| 343 | // HLSL todo: entry point handle needs much more sophistication. |
| 344 | // This is okay for one compilation unit with one entry point. |
| 345 | entryPointName = argv[1]; |
| 346 | if (argc > 0) { |
| 347 | argc--; |
| 348 | argv++; |
| 349 | } else |
| 350 | Error("no <entry-point> provided for -e"); |
| 351 | break; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 352 | case 'h': |
| 353 | usage(); |
| 354 | break; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 355 | case 'i': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 356 | Options |= EOptionIntermediate; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 357 | break; |
| 358 | case 'l': |
John Kessenich | d78e351 | 2014-08-25 20:07:55 +0000 | [diff] [blame] | 359 | Options |= EOptionLinkProgram; |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 360 | break; |
| 361 | case 'm': |
| 362 | Options |= EOptionMemoryLeakMode; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 363 | break; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 364 | case 'o': |
| 365 | binaryFileName = argv[1]; |
| 366 | if (argc > 0) { |
| 367 | argc--; |
| 368 | argv++; |
| 369 | } else |
| 370 | Error("no <file> provided for -o"); |
| 371 | break; |
John Kessenich | 11f9fc7 | 2013-11-07 01:06:34 +0000 | [diff] [blame] | 372 | case 'q': |
| 373 | Options |= EOptionDumpReflection; |
| 374 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 375 | case 'r': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 376 | Options |= EOptionRelaxedErrors; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 377 | break; |
| 378 | case 's': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 379 | Options |= EOptionSuppressInfolog; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 380 | break; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 381 | case 't': |
| 382 | #ifdef _WIN32 |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 383 | Options |= EOptionMultiThreaded; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 384 | #endif |
| 385 | break; |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 386 | case 'v': |
| 387 | Options |= EOptionDumpVersions; |
| 388 | break; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 389 | case 'w': |
| 390 | Options |= EOptionSuppressWarnings; |
| 391 | break; |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 392 | case 'x': |
| 393 | Options |= EOptionOutputHexadecimal; |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 394 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 395 | default: |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 396 | usage(); |
| 397 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 398 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 399 | } else { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 400 | std::string name(argv[0]); |
| 401 | if (! SetConfigFile(name)) { |
| 402 | Work[argc] = new glslang::TWorkItem(name); |
| 403 | Worklist.add(Work[argc]); |
| 404 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 405 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 406 | } |
| 407 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 408 | // Make sure that -E is not specified alongside linking (which includes SPV generation) |
| 409 | if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram)) |
| 410 | Error("can't use -E when linking is selected"); |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 411 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 412 | // -o or -x makes no sense if there is no target binary |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 413 | if (binaryFileName && (Options & EOptionSpv) == 0) |
| 414 | Error("no binary generation requested (e.g., -V)"); |
steve-lunarg | e0b9deb | 2016-09-16 13:26:37 -0600 | [diff] [blame] | 415 | |
| 416 | if ((Options & EOptionFlattenUniformArrays) != 0 && |
| 417 | (Options & EOptionReadHlsl) == 0) |
| 418 | Error("uniform array flattening only valid when compiling HLSL source."); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 419 | } |
| 420 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 421 | // |
| 422 | // Translate the meaningful subset of command-line options to parser-behavior options. |
| 423 | // |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 424 | void SetMessageOptions(EShMessages& messages) |
| 425 | { |
| 426 | if (Options & EOptionRelaxedErrors) |
| 427 | messages = (EShMessages)(messages | EShMsgRelaxedErrors); |
| 428 | if (Options & EOptionIntermediate) |
| 429 | messages = (EShMessages)(messages | EShMsgAST); |
| 430 | if (Options & EOptionSuppressWarnings) |
| 431 | messages = (EShMessages)(messages | EShMsgSuppressWarnings); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 432 | if (Options & EOptionSpv) |
| 433 | messages = (EShMessages)(messages | EShMsgSpvRules); |
| 434 | if (Options & EOptionVulkanRules) |
| 435 | messages = (EShMessages)(messages | EShMsgVulkanRules); |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 436 | if (Options & EOptionOutputPreprocessed) |
| 437 | messages = (EShMessages)(messages | EShMsgOnlyPreprocessor); |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 438 | if (Options & EOptionReadHlsl) |
| 439 | messages = (EShMessages)(messages | EShMsgReadHlsl); |
John Kessenich | a86836e | 2016-07-09 14:50:57 -0600 | [diff] [blame] | 440 | if (Options & EOptionCascadingErrors) |
| 441 | messages = (EShMessages)(messages | EShMsgCascadingErrors); |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 442 | } |
| 443 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 444 | // |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 445 | // Thread entry point, for non-linking asynchronous mode. |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 446 | // |
| 447 | // Return 0 for failure, 1 for success. |
| 448 | // |
Pyry Haulos | 5f6892e | 2015-12-01 12:59:53 -0800 | [diff] [blame] | 449 | unsigned int CompileShaders(void*) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 450 | { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 451 | glslang::TWorkItem* workItem; |
| 452 | while (Worklist.remove(workItem)) { |
| 453 | ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 454 | if (compiler == 0) |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 455 | return 0; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 456 | |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 457 | CompileFile(workItem->name.c_str(), compiler); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 458 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 459 | if (! (Options & EOptionSuppressInfolog)) |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 460 | workItem->results = ShGetInfoLog(compiler); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 461 | |
| 462 | ShDestruct(compiler); |
| 463 | } |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
John Kessenich | 6626cad | 2015-06-19 05:14:19 +0000 | [diff] [blame] | 468 | // Outputs the given string, but only if it is non-null and non-empty. |
| 469 | // This prevents erroneous newlines from appearing. |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 470 | void PutsIfNonEmpty(const char* str) |
John Kessenich | 6626cad | 2015-06-19 05:14:19 +0000 | [diff] [blame] | 471 | { |
| 472 | if (str && str[0]) { |
| 473 | puts(str); |
| 474 | } |
| 475 | } |
| 476 | |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 477 | // Outputs the given string to stderr, but only if it is non-null and non-empty. |
| 478 | // This prevents erroneous newlines from appearing. |
| 479 | void StderrIfNonEmpty(const char* str) |
| 480 | { |
| 481 | if (str && str[0]) { |
| 482 | fprintf(stderr, "%s\n", str); |
| 483 | } |
| 484 | } |
| 485 | |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 486 | // Simple bundling of what makes a compilation unit for ease in passing around, |
| 487 | // and separation of handling file IO versus API (programmatic) compilation. |
| 488 | struct ShaderCompUnit { |
| 489 | EShLanguage stage; |
| 490 | std::string fileName; |
dankbaker | afe6e9c | 2016-08-21 12:29:08 -0400 | [diff] [blame] | 491 | char** text; // memory owned/managed externally |
| 492 | const char* fileNameList[1]; |
| 493 | |
John Kessenich | 219b025 | 2016-08-23 17:51:13 -0600 | [diff] [blame] | 494 | // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs |
dankbaker | afe6e9c | 2016-08-21 12:29:08 -0400 | [diff] [blame] | 495 | ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext) |
| 496 | { |
| 497 | stage = istage; |
| 498 | fileName = ifileName; |
| 499 | text = itext; |
John Kessenich | 219b025 | 2016-08-23 17:51:13 -0600 | [diff] [blame] | 500 | fileNameList[0] = fileName.c_str(); |
dankbaker | afe6e9c | 2016-08-21 12:29:08 -0400 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | ShaderCompUnit(const ShaderCompUnit &rhs) |
| 504 | { |
| 505 | stage = rhs.stage; |
| 506 | fileName = rhs.fileName; |
| 507 | text = rhs.text; |
John Kessenich | 219b025 | 2016-08-23 17:51:13 -0600 | [diff] [blame] | 508 | fileNameList[0] = fileName.c_str(); |
dankbaker | afe6e9c | 2016-08-21 12:29:08 -0400 | [diff] [blame] | 509 | } |
| 510 | |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 511 | }; |
| 512 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 513 | // |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 514 | // For linking mode: Will independently parse each compilation unit, but then put them |
| 515 | // in the same program and link them together, making at most one linked module per |
| 516 | // pipeline stage. |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 517 | // |
| 518 | // Uses the new C++ interface instead of the old handle-based interface. |
| 519 | // |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 520 | |
| 521 | void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits) |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 522 | { |
| 523 | // keep track of what to free |
| 524 | std::list<glslang::TShader*> shaders; |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 525 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 526 | EShMessages messages = EShMsgDefault; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 527 | SetMessageOptions(messages); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 528 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 529 | // |
| 530 | // Per-shader processing... |
| 531 | // |
| 532 | |
John Kessenich | 5b0f13a | 2013-11-01 03:08:40 +0000 | [diff] [blame] | 533 | glslang::TProgram& program = *new glslang::TProgram; |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 534 | for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) { |
| 535 | const auto &compUnit = *it; |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 536 | glslang::TShader* shader = new glslang::TShader(compUnit.stage); |
dankbaker | afe6e9c | 2016-08-21 12:29:08 -0400 | [diff] [blame] | 537 | shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1); |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 538 | if (entryPointName) // HLSL todo: this needs to be tracked per compUnits |
| 539 | shader->setEntryPoint(entryPointName); |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 540 | |
| 541 | shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]); |
| 542 | shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]); |
| 543 | shader->setShiftUboBinding(baseUboBinding[compUnit.stage]); |
steve-lunarg | e0b9deb | 2016-09-16 13:26:37 -0600 | [diff] [blame] | 544 | shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0); |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 545 | |
| 546 | if (Options & EOptionAutoMapBindings) |
| 547 | shader->setAutoMapBindings(true); |
| 548 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 549 | shaders.push_back(shader); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 550 | |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 551 | const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 552 | |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 553 | if (Options & EOptionOutputPreprocessed) { |
| 554 | std::string str; |
Andrew Woloszyn | a132af5 | 2016-03-07 13:23:09 -0500 | [diff] [blame] | 555 | glslang::TShader::ForbidInclude includer; |
Dejan Mircevski | 7be4b82 | 2015-06-17 11:40:33 -0400 | [diff] [blame] | 556 | if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, |
Andrew Woloszyn | a132af5 | 2016-03-07 13:23:09 -0500 | [diff] [blame] | 557 | messages, &str, includer)) { |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 558 | PutsIfNonEmpty(str.c_str()); |
| 559 | } else { |
| 560 | CompileFailed = true; |
| 561 | } |
| 562 | StderrIfNonEmpty(shader->getInfoLog()); |
| 563 | StderrIfNonEmpty(shader->getInfoDebugLog()); |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 564 | continue; |
| 565 | } |
| 566 | if (! shader->parse(&Resources, defaultVersion, false, messages)) |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 567 | CompileFailed = true; |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 568 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 569 | program.addShader(shader); |
| 570 | |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 571 | if (! (Options & EOptionSuppressInfolog) && |
| 572 | ! (Options & EOptionMemoryLeakMode)) { |
| 573 | PutsIfNonEmpty(compUnit.fileName.c_str()); |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 574 | PutsIfNonEmpty(shader->getInfoLog()); |
| 575 | PutsIfNonEmpty(shader->getInfoDebugLog()); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 576 | } |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | // |
| 580 | // Program-level processing... |
| 581 | // |
| 582 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 583 | // Link |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 584 | if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages)) |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 585 | LinkFailed = true; |
| 586 | |
steve-lunarg | 9ae3474 | 2016-10-05 13:40:13 -0600 | [diff] [blame^] | 587 | // Map IO |
| 588 | if (Options & EOptionSpv) { |
| 589 | if (!program.mapIO()) |
| 590 | LinkFailed = true; |
| 591 | } |
| 592 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 593 | // Report |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 594 | if (! (Options & EOptionSuppressInfolog) && |
| 595 | ! (Options & EOptionMemoryLeakMode)) { |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 596 | PutsIfNonEmpty(program.getInfoLog()); |
| 597 | PutsIfNonEmpty(program.getInfoDebugLog()); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 598 | } |
| 599 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 600 | // Reflect |
John Kessenich | 11f9fc7 | 2013-11-07 01:06:34 +0000 | [diff] [blame] | 601 | if (Options & EOptionDumpReflection) { |
| 602 | program.buildReflection(); |
| 603 | program.dumpReflection(); |
| 604 | } |
| 605 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 606 | // Dump SPIR-V |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 607 | if (Options & EOptionSpv) { |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 608 | if (CompileFailed || LinkFailed) |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 609 | printf("SPIR-V is not generated for failed compile or link\n"); |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 610 | else { |
| 611 | for (int stage = 0; stage < EShLangCount; ++stage) { |
John Kessenich | a7a68a9 | 2014-08-24 18:21:00 +0000 | [diff] [blame] | 612 | if (program.getIntermediate((EShLanguage)stage)) { |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 613 | std::vector<unsigned int> spirv; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 614 | std::string warningsErrors; |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 615 | spv::SpvBuildLogger logger; |
| 616 | glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger); |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 617 | |
| 618 | // Dump the spv to a file or stdout, etc., but only if not doing |
| 619 | // memory/perf testing, as it's not internal to programmatic use. |
| 620 | if (! (Options & EOptionMemoryLeakMode)) { |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 621 | printf("%s", logger.getAllMessages().c_str()); |
| 622 | if (Options & EOptionOutputHexadecimal) { |
| 623 | glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage)); |
| 624 | } else { |
| 625 | glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage)); |
| 626 | } |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 627 | if (Options & EOptionHumanReadableSpv) { |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 628 | spv::Disassemble(std::cout, spirv); |
| 629 | } |
John Kessenich | acba772 | 2015-03-04 03:48:38 +0000 | [diff] [blame] | 630 | } |
John Kessenich | a7a68a9 | 2014-08-24 18:21:00 +0000 | [diff] [blame] | 631 | } |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
John Kessenich | 5b0f13a | 2013-11-01 03:08:40 +0000 | [diff] [blame] | 636 | // Free everything up, program has to go before the shaders |
| 637 | // because it might have merged stuff from the shaders, and |
| 638 | // the stuff from the shaders has to have its destructors called |
| 639 | // before the pools holding the memory in the shaders is freed. |
| 640 | delete &program; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 641 | while (shaders.size() > 0) { |
| 642 | delete shaders.back(); |
| 643 | shaders.pop_back(); |
| 644 | } |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 645 | } |
| 646 | |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 647 | // |
| 648 | // Do file IO part of compile and link, handing off the pure |
| 649 | // API/programmatic mode to CompileAndLinkShaderUnits(), which can |
| 650 | // be put in a loop for testing memory footprint and performance. |
| 651 | // |
| 652 | // This is just for linking mode: meaning all the shaders will be put into the |
| 653 | // the same program linked together. |
| 654 | // |
| 655 | // This means there are a limited number of work items (not multi-threading mode) |
| 656 | // and that the point is testing at the linking level. Hence, to enable |
| 657 | // performance and memory testing, the actual compile/link can be put in |
| 658 | // a loop, independent of processing the work items and file IO. |
| 659 | // |
| 660 | void CompileAndLinkShaderFiles() |
| 661 | { |
| 662 | std::vector<ShaderCompUnit> compUnits; |
| 663 | |
| 664 | // Transfer all the work items from to a simple list of |
| 665 | // of compilation units. (We don't care about the thread |
| 666 | // work-item distribution properties in this path, which |
| 667 | // is okay due to the limited number of shaders, know since |
| 668 | // they are all getting linked together.) |
| 669 | glslang::TWorkItem* workItem; |
| 670 | while (Worklist.remove(workItem)) { |
| 671 | ShaderCompUnit compUnit = { |
| 672 | FindLanguage(workItem->name), |
| 673 | workItem->name, |
| 674 | ReadFileData(workItem->name.c_str()) |
| 675 | }; |
| 676 | |
| 677 | if (! compUnit.text) { |
| 678 | usage(); |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | compUnits.push_back(compUnit); |
| 683 | } |
| 684 | |
| 685 | // Actual call to programmatic processing of compile and link, |
| 686 | // in a loop for testing memory and performance. This part contains |
| 687 | // all the perf/memory that a programmatic consumer will care about. |
| 688 | for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) { |
| 689 | for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) |
| 690 | CompileAndLinkShaderUnits(compUnits); |
| 691 | |
| 692 | if (Options & EOptionMemoryLeakMode) |
| 693 | glslang::OS_DumpMemoryCounters(); |
| 694 | } |
| 695 | |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 696 | for (auto it = compUnits.begin(); it != compUnits.end(); ++it) |
| 697 | FreeFileData(it->text); |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 698 | } |
| 699 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 700 | int C_DECL main(int argc, char* argv[]) |
| 701 | { |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 702 | ProcessArguments(argc, argv); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 703 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 704 | if (Options & EOptionDumpConfig) { |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 705 | printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str()); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 706 | if (Worklist.empty()) |
| 707 | return ESuccess; |
| 708 | } |
| 709 | |
John Kessenich | 0da9eaa | 2015-08-01 17:10:02 -0600 | [diff] [blame] | 710 | if (Options & EOptionDumpVersions) { |
| 711 | printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE); |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 712 | printf("ESSL Version: %s\n", glslang::GetEsslVersionString()); |
| 713 | printf("GLSL Version: %s\n", glslang::GetGlslVersionString()); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 714 | std::string spirvVersion; |
| 715 | glslang::GetSpirvVersion(spirvVersion); |
John Kessenich | 0da9eaa | 2015-08-01 17:10:02 -0600 | [diff] [blame] | 716 | printf("SPIR-V Version %s\n", spirvVersion.c_str()); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 717 | printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 718 | printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId()); |
John Kessenich | b84313d | 2016-07-20 16:03:29 -0600 | [diff] [blame] | 719 | printf("GL_KHR_vulkan_glsl version %d\n", 100); |
| 720 | printf("ARB_GL_gl_spirv version %d\n", 100); |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 721 | if (Worklist.empty()) |
| 722 | return ESuccess; |
| 723 | } |
| 724 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 725 | if (Worklist.empty()) { |
| 726 | usage(); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | ProcessConfigFile(); |
| 730 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 731 | // |
| 732 | // Two modes: |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 733 | // 1) linking all arguments together, single-threaded, new C++ interface |
| 734 | // 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] | 735 | // |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 736 | if (Options & EOptionLinkProgram || |
| 737 | Options & EOptionOutputPreprocessed) { |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 738 | glslang::InitializeProcess(); |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 739 | CompileAndLinkShaderFiles(); |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 740 | glslang::FinalizeProcess(); |
Andrew Woloszyn | b891c2b | 2016-01-18 09:26:25 -0500 | [diff] [blame] | 741 | for (int w = 0; w < NumWorkItems; ++w) { |
| 742 | if (Work[w]) { |
| 743 | delete Work[w]; |
| 744 | } |
| 745 | } |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 746 | } else { |
| 747 | ShInitialize(); |
| 748 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 749 | bool printShaderNames = Worklist.size() > 1; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 750 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 751 | if (Options & EOptionMultiThreaded) { |
| 752 | const int NumThreads = 16; |
| 753 | void* threads[NumThreads]; |
| 754 | for (int t = 0; t < NumThreads; ++t) { |
| 755 | threads[t] = glslang::OS_CreateThread(&CompileShaders); |
| 756 | if (! threads[t]) { |
| 757 | printf("Failed to create thread\n"); |
| 758 | return EFailThreadCreate; |
| 759 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 760 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 761 | glslang::OS_WaitForAllThreads(threads, NumThreads); |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 762 | } else |
| 763 | CompileShaders(0); |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 764 | |
| 765 | // Print out all the resulting infologs |
| 766 | for (int w = 0; w < NumWorkItems; ++w) { |
| 767 | if (Work[w]) { |
Kenneth Perry | b07e6be | 2015-12-15 10:52:34 -0600 | [diff] [blame] | 768 | if (printShaderNames || Work[w]->results.size() > 0) |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 769 | PutsIfNonEmpty(Work[w]->name.c_str()); |
| 770 | PutsIfNonEmpty(Work[w]->results.c_str()); |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 771 | delete Work[w]; |
| 772 | } |
| 773 | } |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 774 | |
| 775 | ShFinalize(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 776 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 777 | |
Andrew Woloszyn | b891c2b | 2016-01-18 09:26:25 -0500 | [diff] [blame] | 778 | delete[] Work; |
| 779 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 780 | if (CompileFailed) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 781 | return EFailCompile; |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 782 | if (LinkFailed) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 783 | return EFailLink; |
| 784 | |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | // |
| 789 | // Deduce the language from the filename. Files must end in one of the |
| 790 | // following extensions: |
| 791 | // |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 792 | // .vert = vertex |
| 793 | // .tesc = tessellation control |
| 794 | // .tese = tessellation evaluation |
| 795 | // .geom = geometry |
| 796 | // .frag = fragment |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 797 | // .comp = compute |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 798 | // |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 799 | EShLanguage FindLanguage(const std::string& name, bool parseSuffix) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 800 | { |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 801 | size_t ext = 0; |
| 802 | |
| 803 | // Search for a suffix on a filename: e.g, "myfile.frag". If given |
| 804 | // the suffix directly, we skip looking the '.' |
| 805 | if (parseSuffix) { |
| 806 | ext = name.rfind('.'); |
| 807 | if (ext == std::string::npos) { |
| 808 | usage(); |
| 809 | return EShLangVertex; |
| 810 | } |
| 811 | ++ext; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 812 | } |
| 813 | |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 814 | std::string suffix = name.substr(ext, std::string::npos); |
Dan Baker | c6ede89 | 2016-08-11 14:06:06 -0400 | [diff] [blame] | 815 | if (shaderStageName) |
| 816 | suffix = shaderStageName; |
dankbaker | 45d49bc | 2016-08-08 21:43:07 -0400 | [diff] [blame] | 817 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 818 | if (suffix == "vert") |
| 819 | return EShLangVertex; |
| 820 | else if (suffix == "tesc") |
| 821 | return EShLangTessControl; |
| 822 | else if (suffix == "tese") |
| 823 | return EShLangTessEvaluation; |
| 824 | else if (suffix == "geom") |
| 825 | return EShLangGeometry; |
| 826 | else if (suffix == "frag") |
| 827 | return EShLangFragment; |
John Kessenich | c027579 | 2013-08-09 17:14:49 +0000 | [diff] [blame] | 828 | else if (suffix == "comp") |
| 829 | return EShLangCompute; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 830 | |
| 831 | usage(); |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 832 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 833 | } |
| 834 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 835 | // |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 836 | // Read a file's data into a string, and compile it using the old interface ShCompile, |
| 837 | // for non-linkable results. |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 838 | // |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 839 | void CompileFile(const char* fileName, ShHandle compiler) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 840 | { |
John Kessenich | ca3457f | 2015-05-18 01:59:45 +0000 | [diff] [blame] | 841 | int ret = 0; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 842 | char** shaderStrings = ReadFileData(fileName); |
John Kessenich | db4cd54 | 2013-06-26 22:42:55 +0000 | [diff] [blame] | 843 | if (! shaderStrings) { |
| 844 | usage(); |
John Kessenich | db4cd54 | 2013-06-26 22:42:55 +0000 | [diff] [blame] | 845 | } |
| 846 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 847 | int* lengths = new int[NumShaderStrings]; |
| 848 | |
| 849 | // move to length-based strings, rather than null-terminated strings |
| 850 | for (int s = 0; s < NumShaderStrings; ++s) |
John Kessenich | 35f04bd | 2014-02-19 02:47:20 +0000 | [diff] [blame] | 851 | lengths[s] = (int)strlen(shaderStrings[s]); |
John Kessenich | 09da79e | 2013-04-17 19:34:23 +0000 | [diff] [blame] | 852 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 853 | if (! shaderStrings) { |
| 854 | CompileFailed = true; |
| 855 | return; |
| 856 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 857 | |
John Kessenich | 52ac67e | 2013-05-05 23:46:22 +0000 | [diff] [blame] | 858 | EShMessages messages = EShMsgDefault; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 859 | SetMessageOptions(messages); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 860 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 861 | for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) { |
| 862 | for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) { |
John Kessenich | 26ad268 | 2014-08-13 20:17:19 +0000 | [diff] [blame] | 863 | //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages); |
John Kessenich | ca3457f | 2015-05-18 01:59:45 +0000 | [diff] [blame] | 864 | ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages); |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 865 | //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err", |
| 866 | // "or should be l", "ine 1", "string 5\n", "float glo", "bal", |
| 867 | // ";\n#error should be line 2\n void main() {", "global = 2.3;}" }; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 868 | //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" }; |
John Kessenich | ca3457f | 2015-05-18 01:59:45 +0000 | [diff] [blame] | 869 | //ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages); |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 870 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 871 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 872 | if (Options & EOptionMemoryLeakMode) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 873 | glslang::OS_DumpMemoryCounters(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 874 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 875 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 876 | delete [] lengths; |
| 877 | FreeFileData(shaderStrings); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 878 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 879 | if (ret == 0) |
| 880 | CompileFailed = true; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 881 | } |
| 882 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 883 | // |
| 884 | // print usage to stdout |
| 885 | // |
| 886 | void usage() |
| 887 | { |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 888 | printf("Usage: glslangValidator [option]... [file]...\n" |
| 889 | "\n" |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 890 | "Where: each 'file' ends in .<stage>, where <stage> is one of\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 891 | " .conf to provide an optional config file that replaces the default configuration\n" |
| 892 | " (see -c option below for generating a template)\n" |
| 893 | " .vert for a vertex shader\n" |
| 894 | " .tesc for a tessellation control shader\n" |
| 895 | " .tese for a tessellation evaluation shader\n" |
| 896 | " .geom for a geometry shader\n" |
| 897 | " .frag for a fragment shader\n" |
| 898 | " .comp for a compute shader\n" |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 899 | "\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 900 | "Compilation warnings and errors will be printed to stdout.\n" |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 901 | "\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 902 | "To get other information, use one of the following options:\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 903 | "Each option must be specified separately.\n" |
| 904 | " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n" |
| 905 | " default file name is <stage>.spv (-o overrides this)\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 906 | " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n" |
| 907 | " default file name is <stage>.spv (-o overrides this)\n" |
| 908 | " -H print human readable form of SPIR-V; turns on -V\n" |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 909 | " -E print pre-processed GLSL; cannot be used with -l;\n" |
| 910 | " errors will appear on stderr.\n" |
Dan Baker | c6ede89 | 2016-08-11 14:06:06 -0400 | [diff] [blame] | 911 | " -S <stage> uses explicit stage specified, rather then the file extension.\n" |
Dan Baker | 895275e | 2016-08-11 14:55:49 -0400 | [diff] [blame] | 912 | " valid choices are vert, tesc, tese, geom, frag, or comp\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 913 | " -c configuration dump;\n" |
| 914 | " creates the default configuration file (redirect to a .conf file)\n" |
John Kessenich | a86836e | 2016-07-09 14:50:57 -0600 | [diff] [blame] | 915 | " -C cascading errors; risks crashes from accumulation of error recoveries\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 916 | " -d default to desktop (#version 110) when there is no shader #version\n" |
| 917 | " (default is ES version 100)\n" |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 918 | " -D input is HLSL\n" |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 919 | " -e specify entry-point name\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 920 | " -h print this usage message\n" |
| 921 | " -i intermediate tree (glslang AST) is printed out\n" |
| 922 | " -l link all input files together to form a single module\n" |
| 923 | " -m memory leak mode\n" |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 924 | " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 925 | " -q dump reflection query database\n" |
| 926 | " -r relaxed semantic error-checking mode\n" |
| 927 | " -s silent mode\n" |
| 928 | " -t multi-threaded mode\n" |
| 929 | " -v print version strings\n" |
| 930 | " -w suppress warnings (except as required by #extension : warn)\n" |
Johannes van Waveren | 1fd0175 | 2016-05-31 08:39:41 -0500 | [diff] [blame] | 931 | " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n" |
steve-lunarg | 7f7c2ed | 2016-09-07 15:20:19 -0600 | [diff] [blame] | 932 | "\n" |
| 933 | " --shift-sampler-binding [stage] num set base binding number for samplers\n" |
| 934 | " --ssb [stage] num synonym for --shift-sampler-binding\n" |
| 935 | "\n" |
| 936 | " --shift-texture-binding [stage] num set base binding number for textures\n" |
| 937 | " --stb [stage] num synonym for --shift-texture-binding\n" |
| 938 | "\n" |
| 939 | " --shift-UBO-binding [stage] num set base binding number for UBOs\n" |
| 940 | " --sub [stage] num synonym for --shift-UBO-binding\n" |
| 941 | "\n" |
| 942 | " --auto-map-bindings automatically bind uniform variables without\n" |
| 943 | " explicit bindings.\n" |
| 944 | " --amb synonym for --auto-map-bindings\n" |
steve-lunarg | e0b9deb | 2016-09-16 13:26:37 -0600 | [diff] [blame] | 945 | "\n" |
steve-lunarg | bc9b765 | 2016-09-29 08:43:22 -0600 | [diff] [blame] | 946 | " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n" |
steve-lunarg | e0b9deb | 2016-09-16 13:26:37 -0600 | [diff] [blame] | 947 | " --fua synonym for --flatten-uniform-arrays\n" |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 948 | ); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 949 | |
| 950 | exit(EFailUsage); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 951 | } |
| 952 | |
John Kessenich | 3ce4e59 | 2014-10-06 19:57:34 +0000 | [diff] [blame] | 953 | #if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 954 | |
| 955 | #include <errno.h> |
| 956 | |
| 957 | int fopen_s( |
| 958 | FILE** pFile, |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 959 | const char* filename, |
| 960 | const char* mode |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 961 | ) |
| 962 | { |
| 963 | if (!pFile || !filename || !mode) { |
| 964 | return EINVAL; |
| 965 | } |
| 966 | |
| 967 | FILE* f = fopen(filename, mode); |
| 968 | if (! f) { |
| 969 | if (errno != 0) { |
| 970 | return errno; |
| 971 | } else { |
| 972 | return ENOENT; |
| 973 | } |
| 974 | } |
| 975 | *pFile = f; |
| 976 | |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | #endif |
| 981 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 982 | // |
| 983 | // Malloc a string of sufficient size and read a string into it. |
| 984 | // |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 985 | char** ReadFileData(const char* fileName) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 986 | { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 987 | FILE *in = nullptr; |
John Kessenich | 3ce4e59 | 2014-10-06 19:57:34 +0000 | [diff] [blame] | 988 | int errorCode = fopen_s(&in, fileName, "r"); |
John Kessenich | d6c72a4 | 2014-08-18 19:42:35 +0000 | [diff] [blame] | 989 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 990 | int count = 0; |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 991 | const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings |
| 992 | char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData() |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 993 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 994 | if (errorCode || in == nullptr) |
| 995 | Error("unable to open input file"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 996 | |
| 997 | while (fgetc(in) != EOF) |
| 998 | count++; |
| 999 | |
John Kessenich | d6c72a4 | 2014-08-18 19:42:35 +0000 | [diff] [blame] | 1000 | fseek(in, 0, SEEK_SET); |
John Kessenich | ca3457f | 2015-05-18 01:59:45 +0000 | [diff] [blame] | 1001 | |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1002 | char *fdata = (char*)malloc(count+2); // freed before return of this function |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1003 | if (! fdata) |
| 1004 | Error("can't allocate memory"); |
| 1005 | |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1006 | if ((int)fread(fdata, 1, count, in) != count) { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1007 | free(fdata); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1008 | Error("can't read input file"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1009 | } |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1010 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1011 | fdata[count] = '\0'; |
| 1012 | fclose(in); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1013 | |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 1014 | if (count == 0) { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1015 | // recover from empty file |
| 1016 | return_data[0] = (char*)malloc(count+2); // freed in FreeFileData() |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1017 | return_data[0][0]='\0'; |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 1018 | NumShaderStrings = 0; |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1019 | free(fdata); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1020 | |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1021 | return return_data; |
| 1022 | } else |
| 1023 | NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings |
| 1024 | |
| 1025 | // compute how to split up the file into multiple strings, for testing multiple strings |
John Kessenich | d6c72a4 | 2014-08-18 19:42:35 +0000 | [diff] [blame] | 1026 | int len = (int)(ceil)((float)count/(float)NumShaderStrings); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1027 | int ptr_len = 0; |
| 1028 | int i = 0; |
| 1029 | while (count > 0) { |
| 1030 | return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData() |
| 1031 | memcpy(return_data[i], fdata + ptr_len, len); |
| 1032 | return_data[i][len] = '\0'; |
| 1033 | count -= len; |
| 1034 | ptr_len += len; |
| 1035 | if (count < len) { |
| 1036 | if (count == 0) { |
| 1037 | NumShaderStrings = i + 1; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1038 | break; |
| 1039 | } |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1040 | len = count; |
John Kessenich | d6c72a4 | 2014-08-18 19:42:35 +0000 | [diff] [blame] | 1041 | } |
| 1042 | ++i; |
| 1043 | } |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1044 | |
| 1045 | free(fdata); |
| 1046 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1047 | return return_data; |
| 1048 | } |
| 1049 | |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 1050 | void FreeFileData(char** data) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1051 | { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1052 | for(int i = 0; i < NumShaderStrings; i++) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1053 | free(data[i]); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1054 | |
| 1055 | free(data); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 1058 | void InfoLogMsg(const char* msg, const char* name, const int num) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1059 | { |
John Kessenich | fae38ee | 2015-06-10 23:23:12 +0000 | [diff] [blame] | 1060 | if (num >= 0 ) |
| 1061 | printf("#### %s %s %d INFO LOG ####\n", msg, name, num); |
| 1062 | else |
| 1063 | printf("#### %s %s INFO LOG ####\n", msg, name); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1064 | } |