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