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