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