John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1 | // |
| 2 | //Copyright (C) 2002-2005 3Dlabs Inc. Ltd. |
John Kessenich | bd0747d | 2013-02-17 06:01:50 +0000 | [diff] [blame] | 3 | //Copyright (C) 2013 LunarG, Inc. |
| 4 | // |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 5 | //All rights reserved. |
| 6 | // |
| 7 | //Redistribution and use in source and binary forms, with or without |
| 8 | //modification, are permitted provided that the following conditions |
| 9 | //are met: |
| 10 | // |
| 11 | // Redistributions of source code must retain the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer. |
| 13 | // |
| 14 | // Redistributions in binary form must reproduce the above |
| 15 | // copyright notice, this list of conditions and the following |
| 16 | // disclaimer in the documentation and/or other materials provided |
| 17 | // with the distribution. |
| 18 | // |
| 19 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 20 | // contributors may be used to endorse or promote products derived |
| 21 | // from this software without specific prior written permission. |
| 22 | // |
| 23 | //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 | //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 | //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 26 | //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 27 | //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 | //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 29 | //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 31 | //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 33 | //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 | //POSSIBILITY OF SUCH DAMAGE. |
| 35 | // |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 36 | |
| 37 | // this only applies to the standalone wrapper, not the front end in general |
| 38 | #define _CRT_SECURE_NO_WARNINGS |
| 39 | |
Lei Zhang | 8a9b1ee | 2016-05-19 13:31:43 -0400 | [diff] [blame] | 40 | #include "ResourceLimits.h" |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 41 | #include "Worklist.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 42 | #include "./../glslang/Include/ShHandle.h" |
John Kessenich | 0da9eaa | 2015-08-01 17:10:02 -0600 | [diff] [blame] | 43 | #include "./../glslang/Include/revision.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 44 | #include "./../glslang/Public/ShaderLang.h" |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 45 | #include "../SPIRV/GlslangToSpv.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 46 | #include "../SPIRV/GLSL.std.450.h" |
John Kessenich | acba772 | 2015-03-04 03:48:38 +0000 | [diff] [blame] | 47 | #include "../SPIRV/doc.h" |
| 48 | #include "../SPIRV/disassemble.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 49 | #include <string.h> |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 50 | #include <stdlib.h> |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 51 | #include <math.h> |
| 52 | |
baldurk | 876a0e3 | 2015-11-16 18:03:28 +0100 | [diff] [blame] | 53 | #include "../glslang/OSDependent/osinclude.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 54 | |
| 55 | extern "C" { |
| 56 | SH_IMPORT_EXPORT void ShOutputHtml(); |
| 57 | } |
| 58 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 59 | // Command-line options |
| 60 | enum TOptions { |
John Kessenich | a86836e | 2016-07-09 14:50:57 -0600 | [diff] [blame] | 61 | EOptionNone = 0, |
| 62 | EOptionIntermediate = (1 << 0), |
| 63 | EOptionSuppressInfolog = (1 << 1), |
| 64 | EOptionMemoryLeakMode = (1 << 2), |
| 65 | EOptionRelaxedErrors = (1 << 3), |
| 66 | EOptionGiveWarnings = (1 << 4), |
| 67 | EOptionLinkProgram = (1 << 5), |
| 68 | EOptionMultiThreaded = (1 << 6), |
| 69 | EOptionDumpConfig = (1 << 7), |
| 70 | EOptionDumpReflection = (1 << 8), |
| 71 | EOptionSuppressWarnings = (1 << 9), |
| 72 | EOptionDumpVersions = (1 << 10), |
| 73 | EOptionSpv = (1 << 11), |
| 74 | EOptionHumanReadableSpv = (1 << 12), |
| 75 | EOptionVulkanRules = (1 << 13), |
| 76 | EOptionDefaultDesktop = (1 << 14), |
| 77 | EOptionOutputPreprocessed = (1 << 15), |
| 78 | EOptionOutputHexadecimal = (1 << 16), |
| 79 | EOptionReadHlsl = (1 << 17), |
| 80 | EOptionCascadingErrors = (1 << 18), |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 83 | // |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 84 | // Return codes from main/exit(). |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 85 | // |
| 86 | enum TFailCode { |
| 87 | ESuccess = 0, |
| 88 | EFailUsage, |
| 89 | EFailCompile, |
| 90 | EFailLink, |
| 91 | EFailCompilerCreate, |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 92 | EFailThreadCreate, |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 93 | EFailLinkerCreate |
| 94 | }; |
| 95 | |
| 96 | // |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 97 | // Forward declarations. |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 98 | // |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 99 | EShLanguage FindLanguage(const std::string& name); |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 100 | void CompileFile(const char* fileName, ShHandle); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 101 | void usage(); |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 102 | void FreeFileData(char** data); |
| 103 | char** ReadFileData(const char* fileName); |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 104 | void InfoLogMsg(const char* msg, const char* name, const int num); |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 105 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 106 | // Globally track if any compile or link failure. |
| 107 | bool CompileFailed = false; |
| 108 | bool LinkFailed = false; |
| 109 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 110 | // Use to test breaking up a single shader file into multiple strings. |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 111 | // Set in ReadFileData(). |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 112 | int NumShaderStrings; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 113 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 114 | TBuiltInResource Resources; |
| 115 | std::string ConfigFile; |
| 116 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 117 | // |
John Kessenich | f0bcb0a | 2016-04-02 13:09:14 -0600 | [diff] [blame] | 118 | // 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] | 119 | // |
| 120 | void ProcessConfigFile() |
John Kessenich | b51f62c | 2013-04-11 16:31:09 +0000 | [diff] [blame] | 121 | { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 122 | char** configStrings = 0; |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 123 | char* config = 0; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 124 | if (ConfigFile.size() > 0) { |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 125 | configStrings = ReadFileData(ConfigFile.c_str()); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 126 | if (configStrings) |
| 127 | config = *configStrings; |
| 128 | else { |
| 129 | printf("Error opening configuration file; will instead use the default configuration\n"); |
| 130 | usage(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (config == 0) { |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 135 | Resources = glslang::DefaultTBuiltInResource; |
| 136 | return; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Lei Zhang | 1b14172 | 2016-05-19 13:50:49 -0400 | [diff] [blame] | 139 | glslang::DecodeResourceLimits(&Resources, config); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 140 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 141 | if (configStrings) |
| 142 | FreeFileData(configStrings); |
Andrew Woloszyn | b891c2b | 2016-01-18 09:26:25 -0500 | [diff] [blame] | 143 | else |
| 144 | delete[] config; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 145 | } |
| 146 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 147 | // thread-safe list of shaders to asynchronously grab and compile |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 148 | glslang::TWorklist Worklist; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 149 | |
| 150 | // array of unique places to leave the shader names and infologs for the asynchronous compiles |
John Kessenich | fd30542 | 2014-06-05 16:30:53 +0000 | [diff] [blame] | 151 | glslang::TWorkItem** Work = 0; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 152 | int NumWorkItems = 0; |
| 153 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 154 | int Options = 0; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 155 | const char* ExecutableName = nullptr; |
| 156 | const char* binaryFileName = nullptr; |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 157 | const char* entryPointName = nullptr; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 158 | |
| 159 | // |
| 160 | // Create the default name for saving a binary if -o is not provided. |
| 161 | // |
| 162 | const char* GetBinaryName(EShLanguage stage) |
| 163 | { |
| 164 | const char* name; |
| 165 | if (binaryFileName == nullptr) { |
| 166 | switch (stage) { |
| 167 | case EShLangVertex: name = "vert.spv"; break; |
| 168 | case EShLangTessControl: name = "tesc.spv"; break; |
| 169 | case EShLangTessEvaluation: name = "tese.spv"; break; |
| 170 | case EShLangGeometry: name = "geom.spv"; break; |
| 171 | case EShLangFragment: name = "frag.spv"; break; |
| 172 | case EShLangCompute: name = "comp.spv"; break; |
| 173 | default: name = "unknown"; break; |
| 174 | } |
| 175 | } else |
| 176 | name = binaryFileName; |
| 177 | |
| 178 | return name; |
| 179 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 180 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 181 | // |
| 182 | // *.conf => this is a config file that can set limits/resources |
| 183 | // |
| 184 | bool SetConfigFile(const std::string& name) |
| 185 | { |
| 186 | if (name.size() < 5) |
| 187 | return false; |
| 188 | |
John Kessenich | 4c70685 | 2013-10-11 16:28:43 +0000 | [diff] [blame] | 189 | if (name.compare(name.size() - 5, 5, ".conf") == 0) { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 190 | ConfigFile = name; |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | return false; |
| 195 | } |
| 196 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 197 | // |
| 198 | // Give error and exit with failure code. |
| 199 | // |
| 200 | void Error(const char* message) |
| 201 | { |
| 202 | printf("%s: Error %s (use -h for usage)\n", ExecutableName, message); |
| 203 | exit(EFailUsage); |
| 204 | } |
| 205 | |
| 206 | // |
| 207 | // Do all command-line argument parsing. This includes building up the work-items |
| 208 | // to be processed later, and saving all the command-line options. |
| 209 | // |
| 210 | // Does not return (it exits) if command-line is fatally flawed. |
| 211 | // |
| 212 | void ProcessArguments(int argc, char* argv[]) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 213 | { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 214 | ExecutableName = argv[0]; |
| 215 | NumWorkItems = argc; // will include some empties where the '-' options were, but it doesn't matter, they'll be 0 |
John Kessenich | fd30542 | 2014-06-05 16:30:53 +0000 | [diff] [blame] | 216 | Work = new glslang::TWorkItem*[NumWorkItems]; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 217 | for (int w = 0; w < NumWorkItems; ++w) |
| 218 | Work[w] = 0; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 219 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 220 | argc--; |
| 221 | argv++; |
| 222 | for (; argc >= 1; argc--, argv++) { |
| 223 | if (argv[0][0] == '-') { |
John Kessenich | 2aa7f3a | 2015-05-15 16:02:07 +0000 | [diff] [blame] | 224 | switch (argv[0][1]) { |
| 225 | case 'H': |
| 226 | Options |= EOptionHumanReadableSpv; |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 227 | if ((Options & EOptionSpv) == 0) { |
| 228 | // default to Vulkan |
| 229 | Options |= EOptionSpv; |
| 230 | Options |= EOptionVulkanRules; |
| 231 | Options |= EOptionLinkProgram; |
| 232 | } |
| 233 | break; |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 234 | case 'V': |
| 235 | Options |= EOptionSpv; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 236 | Options |= EOptionVulkanRules; |
| 237 | Options |= EOptionLinkProgram; |
| 238 | break; |
| 239 | case 'G': |
| 240 | Options |= EOptionSpv; |
John Kessenich | d78e351 | 2014-08-25 20:07:55 +0000 | [diff] [blame] | 241 | Options |= EOptionLinkProgram; |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 242 | // undo a -H default to Vulkan |
| 243 | Options &= ~EOptionVulkanRules; |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 244 | break; |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 245 | case 'E': |
| 246 | Options |= EOptionOutputPreprocessed; |
| 247 | break; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 248 | case 'c': |
| 249 | Options |= EOptionDumpConfig; |
| 250 | break; |
John Kessenich | a86836e | 2016-07-09 14:50:57 -0600 | [diff] [blame] | 251 | case 'C': |
| 252 | Options |= EOptionCascadingErrors; |
| 253 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 254 | case 'd': |
John Kessenich | 26ad268 | 2014-08-13 20:17:19 +0000 | [diff] [blame] | 255 | Options |= EOptionDefaultDesktop; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 256 | break; |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 257 | case 'D': |
| 258 | Options |= EOptionReadHlsl; |
| 259 | break; |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 260 | case 'e': |
| 261 | // HLSL todo: entry point handle needs much more sophistication. |
| 262 | // This is okay for one compilation unit with one entry point. |
| 263 | entryPointName = argv[1]; |
| 264 | if (argc > 0) { |
| 265 | argc--; |
| 266 | argv++; |
| 267 | } else |
| 268 | Error("no <entry-point> provided for -e"); |
| 269 | break; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 270 | case 'h': |
| 271 | usage(); |
| 272 | break; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 273 | case 'i': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 274 | Options |= EOptionIntermediate; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 275 | break; |
| 276 | case 'l': |
John Kessenich | d78e351 | 2014-08-25 20:07:55 +0000 | [diff] [blame] | 277 | Options |= EOptionLinkProgram; |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 278 | break; |
| 279 | case 'm': |
| 280 | Options |= EOptionMemoryLeakMode; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 281 | break; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 282 | case 'o': |
| 283 | binaryFileName = argv[1]; |
| 284 | if (argc > 0) { |
| 285 | argc--; |
| 286 | argv++; |
| 287 | } else |
| 288 | Error("no <file> provided for -o"); |
| 289 | break; |
John Kessenich | 11f9fc7 | 2013-11-07 01:06:34 +0000 | [diff] [blame] | 290 | case 'q': |
| 291 | Options |= EOptionDumpReflection; |
| 292 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 293 | case 'r': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 294 | Options |= EOptionRelaxedErrors; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 295 | break; |
| 296 | case 's': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 297 | Options |= EOptionSuppressInfolog; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 298 | break; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 299 | case 't': |
| 300 | #ifdef _WIN32 |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 301 | Options |= EOptionMultiThreaded; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 302 | #endif |
| 303 | break; |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 304 | case 'v': |
| 305 | Options |= EOptionDumpVersions; |
| 306 | break; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 307 | case 'w': |
| 308 | Options |= EOptionSuppressWarnings; |
| 309 | break; |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 310 | case 'x': |
| 311 | Options |= EOptionOutputHexadecimal; |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 312 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 313 | default: |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 314 | usage(); |
| 315 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 316 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 317 | } else { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 318 | std::string name(argv[0]); |
| 319 | if (! SetConfigFile(name)) { |
| 320 | Work[argc] = new glslang::TWorkItem(name); |
| 321 | Worklist.add(Work[argc]); |
| 322 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 323 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 324 | } |
| 325 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 326 | // Make sure that -E is not specified alongside linking (which includes SPV generation) |
| 327 | if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram)) |
| 328 | Error("can't use -E when linking is selected"); |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 329 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 330 | // -o or -x makes no sense if there is no target binary |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 331 | if (binaryFileName && (Options & EOptionSpv) == 0) |
| 332 | Error("no binary generation requested (e.g., -V)"); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 333 | } |
| 334 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 335 | // |
| 336 | // Translate the meaningful subset of command-line options to parser-behavior options. |
| 337 | // |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 338 | void SetMessageOptions(EShMessages& messages) |
| 339 | { |
| 340 | if (Options & EOptionRelaxedErrors) |
| 341 | messages = (EShMessages)(messages | EShMsgRelaxedErrors); |
| 342 | if (Options & EOptionIntermediate) |
| 343 | messages = (EShMessages)(messages | EShMsgAST); |
| 344 | if (Options & EOptionSuppressWarnings) |
| 345 | messages = (EShMessages)(messages | EShMsgSuppressWarnings); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 346 | if (Options & EOptionSpv) |
| 347 | messages = (EShMessages)(messages | EShMsgSpvRules); |
| 348 | if (Options & EOptionVulkanRules) |
| 349 | messages = (EShMessages)(messages | EShMsgVulkanRules); |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 350 | if (Options & EOptionOutputPreprocessed) |
| 351 | messages = (EShMessages)(messages | EShMsgOnlyPreprocessor); |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 352 | if (Options & EOptionReadHlsl) |
| 353 | messages = (EShMessages)(messages | EShMsgReadHlsl); |
John Kessenich | a86836e | 2016-07-09 14:50:57 -0600 | [diff] [blame] | 354 | if (Options & EOptionCascadingErrors) |
| 355 | messages = (EShMessages)(messages | EShMsgCascadingErrors); |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 356 | } |
| 357 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 358 | // |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 359 | // Thread entry point, for non-linking asynchronous mode. |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 360 | // |
| 361 | // Return 0 for failure, 1 for success. |
| 362 | // |
Pyry Haulos | 5f6892e | 2015-12-01 12:59:53 -0800 | [diff] [blame] | 363 | unsigned int CompileShaders(void*) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 364 | { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 365 | glslang::TWorkItem* workItem; |
| 366 | while (Worklist.remove(workItem)) { |
| 367 | ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 368 | if (compiler == 0) |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 369 | return 0; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 370 | |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 371 | CompileFile(workItem->name.c_str(), compiler); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 372 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 373 | if (! (Options & EOptionSuppressInfolog)) |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 374 | workItem->results = ShGetInfoLog(compiler); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 375 | |
| 376 | ShDestruct(compiler); |
| 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
John Kessenich | 6626cad | 2015-06-19 05:14:19 +0000 | [diff] [blame] | 382 | // Outputs the given string, but only if it is non-null and non-empty. |
| 383 | // This prevents erroneous newlines from appearing. |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 384 | void PutsIfNonEmpty(const char* str) |
John Kessenich | 6626cad | 2015-06-19 05:14:19 +0000 | [diff] [blame] | 385 | { |
| 386 | if (str && str[0]) { |
| 387 | puts(str); |
| 388 | } |
| 389 | } |
| 390 | |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 391 | // Outputs the given string to stderr, but only if it is non-null and non-empty. |
| 392 | // This prevents erroneous newlines from appearing. |
| 393 | void StderrIfNonEmpty(const char* str) |
| 394 | { |
| 395 | if (str && str[0]) { |
| 396 | fprintf(stderr, "%s\n", str); |
| 397 | } |
| 398 | } |
| 399 | |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 400 | // Simple bundling of what makes a compilation unit for ease in passing around, |
| 401 | // and separation of handling file IO versus API (programmatic) compilation. |
| 402 | struct ShaderCompUnit { |
| 403 | EShLanguage stage; |
| 404 | std::string fileName; |
| 405 | char** text; // memory owned/managed externally |
| 406 | }; |
| 407 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 408 | // |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 409 | // For linking mode: Will independently parse each compilation unit, but then put them |
| 410 | // in the same program and link them together, making at most one linked module per |
| 411 | // pipeline stage. |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 412 | // |
| 413 | // Uses the new C++ interface instead of the old handle-based interface. |
| 414 | // |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 415 | |
| 416 | void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits) |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 417 | { |
| 418 | // keep track of what to free |
| 419 | std::list<glslang::TShader*> shaders; |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 420 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 421 | EShMessages messages = EShMsgDefault; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 422 | SetMessageOptions(messages); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 423 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 424 | // |
| 425 | // Per-shader processing... |
| 426 | // |
| 427 | |
John Kessenich | 5b0f13a | 2013-11-01 03:08:40 +0000 | [diff] [blame] | 428 | glslang::TProgram& program = *new glslang::TProgram; |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 429 | for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) { |
| 430 | const auto &compUnit = *it; |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 431 | glslang::TShader* shader = new glslang::TShader(compUnit.stage); |
| 432 | shader->setStrings(compUnit.text, 1); |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 433 | if (entryPointName) // HLSL todo: this needs to be tracked per compUnits |
| 434 | shader->setEntryPoint(entryPointName); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 435 | shaders.push_back(shader); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 436 | |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 437 | const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 438 | |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 439 | if (Options & EOptionOutputPreprocessed) { |
| 440 | std::string str; |
Andrew Woloszyn | a132af5 | 2016-03-07 13:23:09 -0500 | [diff] [blame] | 441 | glslang::TShader::ForbidInclude includer; |
Dejan Mircevski | 7be4b82 | 2015-06-17 11:40:33 -0400 | [diff] [blame] | 442 | if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, |
Andrew Woloszyn | a132af5 | 2016-03-07 13:23:09 -0500 | [diff] [blame] | 443 | messages, &str, includer)) { |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 444 | PutsIfNonEmpty(str.c_str()); |
| 445 | } else { |
| 446 | CompileFailed = true; |
| 447 | } |
| 448 | StderrIfNonEmpty(shader->getInfoLog()); |
| 449 | StderrIfNonEmpty(shader->getInfoDebugLog()); |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 450 | continue; |
| 451 | } |
| 452 | if (! shader->parse(&Resources, defaultVersion, false, messages)) |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 453 | CompileFailed = true; |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 454 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 455 | program.addShader(shader); |
| 456 | |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 457 | if (! (Options & EOptionSuppressInfolog) && |
| 458 | ! (Options & EOptionMemoryLeakMode)) { |
| 459 | PutsIfNonEmpty(compUnit.fileName.c_str()); |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 460 | PutsIfNonEmpty(shader->getInfoLog()); |
| 461 | PutsIfNonEmpty(shader->getInfoDebugLog()); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 462 | } |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // |
| 466 | // Program-level processing... |
| 467 | // |
| 468 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 469 | // Link |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 470 | if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages)) |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 471 | LinkFailed = true; |
| 472 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 473 | // Report |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 474 | if (! (Options & EOptionSuppressInfolog) && |
| 475 | ! (Options & EOptionMemoryLeakMode)) { |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 476 | PutsIfNonEmpty(program.getInfoLog()); |
| 477 | PutsIfNonEmpty(program.getInfoDebugLog()); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 478 | } |
| 479 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 480 | // Reflect |
John Kessenich | 11f9fc7 | 2013-11-07 01:06:34 +0000 | [diff] [blame] | 481 | if (Options & EOptionDumpReflection) { |
| 482 | program.buildReflection(); |
| 483 | program.dumpReflection(); |
| 484 | } |
| 485 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 486 | // Dump SPIR-V |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 487 | if (Options & EOptionSpv) { |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 488 | if (CompileFailed || LinkFailed) |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 489 | printf("SPIR-V is not generated for failed compile or link\n"); |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 490 | else { |
| 491 | for (int stage = 0; stage < EShLangCount; ++stage) { |
John Kessenich | a7a68a9 | 2014-08-24 18:21:00 +0000 | [diff] [blame] | 492 | if (program.getIntermediate((EShLanguage)stage)) { |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 493 | std::vector<unsigned int> spirv; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 494 | std::string warningsErrors; |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 495 | spv::SpvBuildLogger logger; |
| 496 | glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger); |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 497 | |
| 498 | // Dump the spv to a file or stdout, etc., but only if not doing |
| 499 | // memory/perf testing, as it's not internal to programmatic use. |
| 500 | if (! (Options & EOptionMemoryLeakMode)) { |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 501 | printf("%s", logger.getAllMessages().c_str()); |
| 502 | if (Options & EOptionOutputHexadecimal) { |
| 503 | glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage)); |
| 504 | } else { |
| 505 | glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage)); |
| 506 | } |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 507 | if (Options & EOptionHumanReadableSpv) { |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 508 | spv::Disassemble(std::cout, spirv); |
| 509 | } |
John Kessenich | acba772 | 2015-03-04 03:48:38 +0000 | [diff] [blame] | 510 | } |
John Kessenich | a7a68a9 | 2014-08-24 18:21:00 +0000 | [diff] [blame] | 511 | } |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
John Kessenich | 5b0f13a | 2013-11-01 03:08:40 +0000 | [diff] [blame] | 516 | // Free everything up, program has to go before the shaders |
| 517 | // because it might have merged stuff from the shaders, and |
| 518 | // the stuff from the shaders has to have its destructors called |
| 519 | // before the pools holding the memory in the shaders is freed. |
| 520 | delete &program; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 521 | while (shaders.size() > 0) { |
| 522 | delete shaders.back(); |
| 523 | shaders.pop_back(); |
| 524 | } |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 525 | } |
| 526 | |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 527 | // |
| 528 | // Do file IO part of compile and link, handing off the pure |
| 529 | // API/programmatic mode to CompileAndLinkShaderUnits(), which can |
| 530 | // be put in a loop for testing memory footprint and performance. |
| 531 | // |
| 532 | // This is just for linking mode: meaning all the shaders will be put into the |
| 533 | // the same program linked together. |
| 534 | // |
| 535 | // This means there are a limited number of work items (not multi-threading mode) |
| 536 | // and that the point is testing at the linking level. Hence, to enable |
| 537 | // performance and memory testing, the actual compile/link can be put in |
| 538 | // a loop, independent of processing the work items and file IO. |
| 539 | // |
| 540 | void CompileAndLinkShaderFiles() |
| 541 | { |
| 542 | std::vector<ShaderCompUnit> compUnits; |
| 543 | |
| 544 | // Transfer all the work items from to a simple list of |
| 545 | // of compilation units. (We don't care about the thread |
| 546 | // work-item distribution properties in this path, which |
| 547 | // is okay due to the limited number of shaders, know since |
| 548 | // they are all getting linked together.) |
| 549 | glslang::TWorkItem* workItem; |
| 550 | while (Worklist.remove(workItem)) { |
| 551 | ShaderCompUnit compUnit = { |
| 552 | FindLanguage(workItem->name), |
| 553 | workItem->name, |
| 554 | ReadFileData(workItem->name.c_str()) |
| 555 | }; |
| 556 | |
| 557 | if (! compUnit.text) { |
| 558 | usage(); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | compUnits.push_back(compUnit); |
| 563 | } |
| 564 | |
| 565 | // Actual call to programmatic processing of compile and link, |
| 566 | // in a loop for testing memory and performance. This part contains |
| 567 | // all the perf/memory that a programmatic consumer will care about. |
| 568 | for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) { |
| 569 | for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) |
| 570 | CompileAndLinkShaderUnits(compUnits); |
| 571 | |
| 572 | if (Options & EOptionMemoryLeakMode) |
| 573 | glslang::OS_DumpMemoryCounters(); |
| 574 | } |
| 575 | |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 576 | for (auto it = compUnits.begin(); it != compUnits.end(); ++it) |
| 577 | FreeFileData(it->text); |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 578 | } |
| 579 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 580 | int C_DECL main(int argc, char* argv[]) |
| 581 | { |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 582 | ProcessArguments(argc, argv); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 583 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 584 | if (Options & EOptionDumpConfig) { |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 585 | printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str()); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 586 | if (Worklist.empty()) |
| 587 | return ESuccess; |
| 588 | } |
| 589 | |
John Kessenich | 0da9eaa | 2015-08-01 17:10:02 -0600 | [diff] [blame] | 590 | if (Options & EOptionDumpVersions) { |
| 591 | printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE); |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 592 | printf("ESSL Version: %s\n", glslang::GetEsslVersionString()); |
| 593 | printf("GLSL Version: %s\n", glslang::GetGlslVersionString()); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 594 | std::string spirvVersion; |
| 595 | glslang::GetSpirvVersion(spirvVersion); |
John Kessenich | 0da9eaa | 2015-08-01 17:10:02 -0600 | [diff] [blame] | 596 | printf("SPIR-V Version %s\n", spirvVersion.c_str()); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 597 | printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 598 | printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId()); |
John Kessenich | b84313d | 2016-07-20 16:03:29 -0600 | [diff] [blame] | 599 | printf("GL_KHR_vulkan_glsl version %d\n", 100); |
| 600 | printf("ARB_GL_gl_spirv version %d\n", 100); |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 601 | if (Worklist.empty()) |
| 602 | return ESuccess; |
| 603 | } |
| 604 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 605 | if (Worklist.empty()) { |
| 606 | usage(); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | ProcessConfigFile(); |
| 610 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 611 | // |
| 612 | // Two modes: |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 613 | // 1) linking all arguments together, single-threaded, new C++ interface |
| 614 | // 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] | 615 | // |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 616 | if (Options & EOptionLinkProgram || |
| 617 | Options & EOptionOutputPreprocessed) { |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 618 | glslang::InitializeProcess(); |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 619 | CompileAndLinkShaderFiles(); |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 620 | glslang::FinalizeProcess(); |
Andrew Woloszyn | b891c2b | 2016-01-18 09:26:25 -0500 | [diff] [blame] | 621 | for (int w = 0; w < NumWorkItems; ++w) { |
| 622 | if (Work[w]) { |
| 623 | delete Work[w]; |
| 624 | } |
| 625 | } |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 626 | } else { |
| 627 | ShInitialize(); |
| 628 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 629 | bool printShaderNames = Worklist.size() > 1; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 630 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 631 | if (Options & EOptionMultiThreaded) { |
| 632 | const int NumThreads = 16; |
| 633 | void* threads[NumThreads]; |
| 634 | for (int t = 0; t < NumThreads; ++t) { |
| 635 | threads[t] = glslang::OS_CreateThread(&CompileShaders); |
| 636 | if (! threads[t]) { |
| 637 | printf("Failed to create thread\n"); |
| 638 | return EFailThreadCreate; |
| 639 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 640 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 641 | glslang::OS_WaitForAllThreads(threads, NumThreads); |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 642 | } else |
| 643 | CompileShaders(0); |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 644 | |
| 645 | // Print out all the resulting infologs |
| 646 | for (int w = 0; w < NumWorkItems; ++w) { |
| 647 | if (Work[w]) { |
Kenneth Perry | b07e6be | 2015-12-15 10:52:34 -0600 | [diff] [blame] | 648 | if (printShaderNames || Work[w]->results.size() > 0) |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 649 | PutsIfNonEmpty(Work[w]->name.c_str()); |
| 650 | PutsIfNonEmpty(Work[w]->results.c_str()); |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 651 | delete Work[w]; |
| 652 | } |
| 653 | } |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 654 | |
| 655 | ShFinalize(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 656 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 657 | |
Andrew Woloszyn | b891c2b | 2016-01-18 09:26:25 -0500 | [diff] [blame] | 658 | delete[] Work; |
| 659 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 660 | if (CompileFailed) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 661 | return EFailCompile; |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 662 | if (LinkFailed) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 663 | return EFailLink; |
| 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | // |
| 669 | // Deduce the language from the filename. Files must end in one of the |
| 670 | // following extensions: |
| 671 | // |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 672 | // .vert = vertex |
| 673 | // .tesc = tessellation control |
| 674 | // .tese = tessellation evaluation |
| 675 | // .geom = geometry |
| 676 | // .frag = fragment |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 677 | // .comp = compute |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 678 | // |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 679 | EShLanguage FindLanguage(const std::string& name) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 680 | { |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 681 | size_t ext = name.rfind('.'); |
| 682 | if (ext == std::string::npos) { |
| 683 | usage(); |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 684 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 685 | } |
| 686 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 687 | std::string suffix = name.substr(ext + 1, std::string::npos); |
| 688 | if (suffix == "vert") |
| 689 | return EShLangVertex; |
| 690 | else if (suffix == "tesc") |
| 691 | return EShLangTessControl; |
| 692 | else if (suffix == "tese") |
| 693 | return EShLangTessEvaluation; |
| 694 | else if (suffix == "geom") |
| 695 | return EShLangGeometry; |
| 696 | else if (suffix == "frag") |
| 697 | return EShLangFragment; |
John Kessenich | c027579 | 2013-08-09 17:14:49 +0000 | [diff] [blame] | 698 | else if (suffix == "comp") |
| 699 | return EShLangCompute; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 700 | |
| 701 | usage(); |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 702 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 703 | } |
| 704 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 705 | // |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 706 | // Read a file's data into a string, and compile it using the old interface ShCompile, |
| 707 | // for non-linkable results. |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 708 | // |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 709 | void CompileFile(const char* fileName, ShHandle compiler) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 710 | { |
John Kessenich | ca3457f | 2015-05-18 01:59:45 +0000 | [diff] [blame] | 711 | int ret = 0; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 712 | char** shaderStrings = ReadFileData(fileName); |
John Kessenich | db4cd54 | 2013-06-26 22:42:55 +0000 | [diff] [blame] | 713 | if (! shaderStrings) { |
| 714 | usage(); |
John Kessenich | db4cd54 | 2013-06-26 22:42:55 +0000 | [diff] [blame] | 715 | } |
| 716 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 717 | int* lengths = new int[NumShaderStrings]; |
| 718 | |
| 719 | // move to length-based strings, rather than null-terminated strings |
| 720 | for (int s = 0; s < NumShaderStrings; ++s) |
John Kessenich | 35f04bd | 2014-02-19 02:47:20 +0000 | [diff] [blame] | 721 | lengths[s] = (int)strlen(shaderStrings[s]); |
John Kessenich | 09da79e | 2013-04-17 19:34:23 +0000 | [diff] [blame] | 722 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 723 | if (! shaderStrings) { |
| 724 | CompileFailed = true; |
| 725 | return; |
| 726 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 727 | |
John Kessenich | 52ac67e | 2013-05-05 23:46:22 +0000 | [diff] [blame] | 728 | EShMessages messages = EShMsgDefault; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 729 | SetMessageOptions(messages); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 730 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 731 | for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) { |
| 732 | for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) { |
John Kessenich | 26ad268 | 2014-08-13 20:17:19 +0000 | [diff] [blame] | 733 | //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] | 734 | ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages); |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 735 | //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err", |
| 736 | // "or should be l", "ine 1", "string 5\n", "float glo", "bal", |
| 737 | // ";\n#error should be line 2\n void main() {", "global = 2.3;}" }; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 738 | //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" }; |
John Kessenich | ca3457f | 2015-05-18 01:59:45 +0000 | [diff] [blame] | 739 | //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] | 740 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 741 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 742 | if (Options & EOptionMemoryLeakMode) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 743 | glslang::OS_DumpMemoryCounters(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 744 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 745 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 746 | delete [] lengths; |
| 747 | FreeFileData(shaderStrings); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 748 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 749 | if (ret == 0) |
| 750 | CompileFailed = true; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 751 | } |
| 752 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 753 | // |
| 754 | // print usage to stdout |
| 755 | // |
| 756 | void usage() |
| 757 | { |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 758 | printf("Usage: glslangValidator [option]... [file]...\n" |
| 759 | "\n" |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 760 | "Where: each 'file' ends in .<stage>, where <stage> is one of\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 761 | " .conf to provide an optional config file that replaces the default configuration\n" |
| 762 | " (see -c option below for generating a template)\n" |
| 763 | " .vert for a vertex shader\n" |
| 764 | " .tesc for a tessellation control shader\n" |
| 765 | " .tese for a tessellation evaluation shader\n" |
| 766 | " .geom for a geometry shader\n" |
| 767 | " .frag for a fragment shader\n" |
| 768 | " .comp for a compute shader\n" |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 769 | "\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 770 | "Compilation warnings and errors will be printed to stdout.\n" |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 771 | "\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 772 | "To get other information, use one of the following options:\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 773 | "Each option must be specified separately.\n" |
| 774 | " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n" |
| 775 | " default file name is <stage>.spv (-o overrides this)\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 776 | " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n" |
| 777 | " default file name is <stage>.spv (-o overrides this)\n" |
| 778 | " -H print human readable form of SPIR-V; turns on -V\n" |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 779 | " -E print pre-processed GLSL; cannot be used with -l;\n" |
| 780 | " errors will appear on stderr.\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 781 | " -c configuration dump;\n" |
| 782 | " creates the default configuration file (redirect to a .conf file)\n" |
John Kessenich | a86836e | 2016-07-09 14:50:57 -0600 | [diff] [blame] | 783 | " -C cascading errors; risks crashes from accumulation of error recoveries\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 784 | " -d default to desktop (#version 110) when there is no shader #version\n" |
| 785 | " (default is ES version 100)\n" |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 786 | " -D input is HLSL\n" |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 787 | " -e specify entry-point name\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 788 | " -h print this usage message\n" |
| 789 | " -i intermediate tree (glslang AST) is printed out\n" |
| 790 | " -l link all input files together to form a single module\n" |
| 791 | " -m memory leak mode\n" |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 792 | " -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] | 793 | " -q dump reflection query database\n" |
| 794 | " -r relaxed semantic error-checking mode\n" |
| 795 | " -s silent mode\n" |
| 796 | " -t multi-threaded mode\n" |
| 797 | " -v print version strings\n" |
| 798 | " -w suppress warnings (except as required by #extension : warn)\n" |
Johannes van Waveren | 1fd0175 | 2016-05-31 08:39:41 -0500 | [diff] [blame] | 799 | " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n" |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 800 | ); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 801 | |
| 802 | exit(EFailUsage); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 803 | } |
| 804 | |
John Kessenich | 3ce4e59 | 2014-10-06 19:57:34 +0000 | [diff] [blame] | 805 | #if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 806 | |
| 807 | #include <errno.h> |
| 808 | |
| 809 | int fopen_s( |
| 810 | FILE** pFile, |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 811 | const char* filename, |
| 812 | const char* mode |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 813 | ) |
| 814 | { |
| 815 | if (!pFile || !filename || !mode) { |
| 816 | return EINVAL; |
| 817 | } |
| 818 | |
| 819 | FILE* f = fopen(filename, mode); |
| 820 | if (! f) { |
| 821 | if (errno != 0) { |
| 822 | return errno; |
| 823 | } else { |
| 824 | return ENOENT; |
| 825 | } |
| 826 | } |
| 827 | *pFile = f; |
| 828 | |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | #endif |
| 833 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 834 | // |
| 835 | // Malloc a string of sufficient size and read a string into it. |
| 836 | // |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 837 | char** ReadFileData(const char* fileName) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 838 | { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 839 | FILE *in = nullptr; |
John Kessenich | 3ce4e59 | 2014-10-06 19:57:34 +0000 | [diff] [blame] | 840 | int errorCode = fopen_s(&in, fileName, "r"); |
John Kessenich | d6c72a4 | 2014-08-18 19:42:35 +0000 | [diff] [blame] | 841 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 842 | int count = 0; |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 843 | const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings |
| 844 | char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData() |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 845 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 846 | if (errorCode || in == nullptr) |
| 847 | Error("unable to open input file"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 848 | |
| 849 | while (fgetc(in) != EOF) |
| 850 | count++; |
| 851 | |
John Kessenich | d6c72a4 | 2014-08-18 19:42:35 +0000 | [diff] [blame] | 852 | fseek(in, 0, SEEK_SET); |
John Kessenich | ca3457f | 2015-05-18 01:59:45 +0000 | [diff] [blame] | 853 | |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 854 | char *fdata = (char*)malloc(count+2); // freed before return of this function |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 855 | if (! fdata) |
| 856 | Error("can't allocate memory"); |
| 857 | |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 858 | if ((int)fread(fdata, 1, count, in) != count) { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 859 | free(fdata); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 860 | Error("can't read input file"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 861 | } |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 862 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 863 | fdata[count] = '\0'; |
| 864 | fclose(in); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 865 | |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 866 | if (count == 0) { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 867 | // recover from empty file |
| 868 | return_data[0] = (char*)malloc(count+2); // freed in FreeFileData() |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 869 | return_data[0][0]='\0'; |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 870 | NumShaderStrings = 0; |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 871 | free(fdata); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 872 | |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 873 | return return_data; |
| 874 | } else |
| 875 | NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings |
| 876 | |
| 877 | // 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] | 878 | int len = (int)(ceil)((float)count/(float)NumShaderStrings); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 879 | int ptr_len = 0; |
| 880 | int i = 0; |
| 881 | while (count > 0) { |
| 882 | return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData() |
| 883 | memcpy(return_data[i], fdata + ptr_len, len); |
| 884 | return_data[i][len] = '\0'; |
| 885 | count -= len; |
| 886 | ptr_len += len; |
| 887 | if (count < len) { |
| 888 | if (count == 0) { |
| 889 | NumShaderStrings = i + 1; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 890 | break; |
| 891 | } |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 892 | len = count; |
John Kessenich | d6c72a4 | 2014-08-18 19:42:35 +0000 | [diff] [blame] | 893 | } |
| 894 | ++i; |
| 895 | } |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 896 | |
| 897 | free(fdata); |
| 898 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 899 | return return_data; |
| 900 | } |
| 901 | |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 902 | void FreeFileData(char** data) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 903 | { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 904 | for(int i = 0; i < NumShaderStrings; i++) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 905 | free(data[i]); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 906 | |
| 907 | free(data); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 908 | } |
| 909 | |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 910 | void InfoLogMsg(const char* msg, const char* name, const int num) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 911 | { |
John Kessenich | fae38ee | 2015-06-10 23:23:12 +0000 | [diff] [blame] | 912 | if (num >= 0 ) |
| 913 | printf("#### %s %s %d INFO LOG ####\n", msg, name, num); |
| 914 | else |
| 915 | printf("#### %s %s INFO LOG ####\n", msg, name); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 916 | } |