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