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