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