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 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 40 | #include "Worklist.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 41 | #include "./../glslang/Include/ShHandle.h" |
John Kessenich | 0da9eaa | 2015-08-01 17:10:02 -0600 | [diff] [blame] | 42 | #include "./../glslang/Include/revision.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 43 | #include "./../glslang/Public/ShaderLang.h" |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 44 | #include "../SPIRV/GlslangToSpv.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 45 | #include "../SPIRV/GLSL.std.450.h" |
John Kessenich | acba772 | 2015-03-04 03:48:38 +0000 | [diff] [blame] | 46 | #include "../SPIRV/doc.h" |
| 47 | #include "../SPIRV/disassemble.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 48 | #include <string.h> |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 49 | #include <stdlib.h> |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 50 | #include <math.h> |
| 51 | |
baldurk | 876a0e3 | 2015-11-16 18:03:28 +0100 | [diff] [blame] | 52 | #include "../glslang/OSDependent/osinclude.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 53 | |
| 54 | extern "C" { |
| 55 | SH_IMPORT_EXPORT void ShOutputHtml(); |
| 56 | } |
| 57 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 58 | // Command-line options |
| 59 | enum TOptions { |
John Kessenich | acba772 | 2015-03-04 03:48:38 +0000 | [diff] [blame] | 60 | EOptionNone = 0x0000, |
| 61 | EOptionIntermediate = 0x0001, |
| 62 | EOptionSuppressInfolog = 0x0002, |
| 63 | EOptionMemoryLeakMode = 0x0004, |
| 64 | EOptionRelaxedErrors = 0x0008, |
| 65 | EOptionGiveWarnings = 0x0010, |
| 66 | EOptionLinkProgram = 0x0020, |
| 67 | EOptionMultiThreaded = 0x0040, |
| 68 | EOptionDumpConfig = 0x0080, |
| 69 | EOptionDumpReflection = 0x0100, |
| 70 | EOptionSuppressWarnings = 0x0200, |
| 71 | EOptionDumpVersions = 0x0400, |
| 72 | EOptionSpv = 0x0800, |
| 73 | EOptionHumanReadableSpv = 0x1000, |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 74 | EOptionVulkanRules = 0x2000, |
| 75 | EOptionDefaultDesktop = 0x4000, |
| 76 | EOptionOutputPreprocessed = 0x8000, |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame^] | 77 | EOptionReadHlsl = 0x10000, |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 80 | // |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 81 | // Return codes from main/exit(). |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 82 | // |
| 83 | enum TFailCode { |
| 84 | ESuccess = 0, |
| 85 | EFailUsage, |
| 86 | EFailCompile, |
| 87 | EFailLink, |
| 88 | EFailCompilerCreate, |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 89 | EFailThreadCreate, |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 90 | EFailLinkerCreate |
| 91 | }; |
| 92 | |
| 93 | // |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 94 | // Forward declarations. |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 95 | // |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 96 | EShLanguage FindLanguage(const std::string& name); |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 97 | void CompileFile(const char* fileName, ShHandle); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 98 | void usage(); |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 99 | void FreeFileData(char** data); |
| 100 | char** ReadFileData(const char* fileName); |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 101 | void InfoLogMsg(const char* msg, const char* name, const int num); |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 102 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 103 | // Globally track if any compile or link failure. |
| 104 | bool CompileFailed = false; |
| 105 | bool LinkFailed = false; |
| 106 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 107 | // Use to test breaking up a single shader file into multiple strings. |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 108 | // Set in ReadFileData(). |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 109 | int NumShaderStrings; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 110 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 111 | TBuiltInResource Resources; |
| 112 | std::string ConfigFile; |
| 113 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 114 | // |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 115 | // These are the default resources for TBuiltInResources, used for both |
| 116 | // - parsing this string for the case where the user didn't supply one |
| 117 | // - dumping out a template for user construction of a config file |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 118 | // |
John Kessenich | 284231c | 2013-10-22 01:50:39 +0000 | [diff] [blame] | 119 | const char* DefaultConfig = |
| 120 | "MaxLights 32\n" |
| 121 | "MaxClipPlanes 6\n" |
| 122 | "MaxTextureUnits 32\n" |
| 123 | "MaxTextureCoords 32\n" |
| 124 | "MaxVertexAttribs 64\n" |
| 125 | "MaxVertexUniformComponents 4096\n" |
| 126 | "MaxVaryingFloats 64\n" |
| 127 | "MaxVertexTextureImageUnits 32\n" |
John Kessenich | 623833f | 2013-12-11 18:57:40 +0000 | [diff] [blame] | 128 | "MaxCombinedTextureImageUnits 80\n" |
John Kessenich | 284231c | 2013-10-22 01:50:39 +0000 | [diff] [blame] | 129 | "MaxTextureImageUnits 32\n" |
| 130 | "MaxFragmentUniformComponents 4096\n" |
| 131 | "MaxDrawBuffers 32\n" |
| 132 | "MaxVertexUniformVectors 128\n" |
| 133 | "MaxVaryingVectors 8\n" |
| 134 | "MaxFragmentUniformVectors 16\n" |
| 135 | "MaxVertexOutputVectors 16\n" |
| 136 | "MaxFragmentInputVectors 15\n" |
| 137 | "MinProgramTexelOffset -8\n" |
| 138 | "MaxProgramTexelOffset 7\n" |
| 139 | "MaxClipDistances 8\n" |
| 140 | "MaxComputeWorkGroupCountX 65535\n" |
| 141 | "MaxComputeWorkGroupCountY 65535\n" |
| 142 | "MaxComputeWorkGroupCountZ 65535\n" |
| 143 | "MaxComputeWorkGroupSizeX 1024\n" |
John Kessenich | da66bc7 | 2014-08-19 20:32:48 +0000 | [diff] [blame] | 144 | "MaxComputeWorkGroupSizeY 1024\n" |
John Kessenich | 284231c | 2013-10-22 01:50:39 +0000 | [diff] [blame] | 145 | "MaxComputeWorkGroupSizeZ 64\n" |
| 146 | "MaxComputeUniformComponents 1024\n" |
| 147 | "MaxComputeTextureImageUnits 16\n" |
| 148 | "MaxComputeImageUniforms 8\n" |
| 149 | "MaxComputeAtomicCounters 8\n" |
| 150 | "MaxComputeAtomicCounterBuffers 1\n" |
| 151 | "MaxVaryingComponents 60\n" |
| 152 | "MaxVertexOutputComponents 64\n" |
| 153 | "MaxGeometryInputComponents 64\n" |
| 154 | "MaxGeometryOutputComponents 128\n" |
| 155 | "MaxFragmentInputComponents 128\n" |
| 156 | "MaxImageUnits 8\n" |
| 157 | "MaxCombinedImageUnitsAndFragmentOutputs 8\n" |
John Kessenich | ddea678 | 2014-08-10 18:19:36 +0000 | [diff] [blame] | 158 | "MaxCombinedShaderOutputResources 8\n" |
John Kessenich | 284231c | 2013-10-22 01:50:39 +0000 | [diff] [blame] | 159 | "MaxImageSamples 0\n" |
| 160 | "MaxVertexImageUniforms 0\n" |
| 161 | "MaxTessControlImageUniforms 0\n" |
| 162 | "MaxTessEvaluationImageUniforms 0\n" |
| 163 | "MaxGeometryImageUniforms 0\n" |
| 164 | "MaxFragmentImageUniforms 8\n" |
| 165 | "MaxCombinedImageUniforms 8\n" |
| 166 | "MaxGeometryTextureImageUnits 16\n" |
| 167 | "MaxGeometryOutputVertices 256\n" |
| 168 | "MaxGeometryTotalOutputComponents 1024\n" |
| 169 | "MaxGeometryUniformComponents 1024\n" |
| 170 | "MaxGeometryVaryingComponents 64\n" |
| 171 | "MaxTessControlInputComponents 128\n" |
| 172 | "MaxTessControlOutputComponents 128\n" |
| 173 | "MaxTessControlTextureImageUnits 16\n" |
| 174 | "MaxTessControlUniformComponents 1024\n" |
| 175 | "MaxTessControlTotalOutputComponents 4096\n" |
| 176 | "MaxTessEvaluationInputComponents 128\n" |
| 177 | "MaxTessEvaluationOutputComponents 128\n" |
| 178 | "MaxTessEvaluationTextureImageUnits 16\n" |
| 179 | "MaxTessEvaluationUniformComponents 1024\n" |
| 180 | "MaxTessPatchComponents 120\n" |
| 181 | "MaxPatchVertices 32\n" |
| 182 | "MaxTessGenLevel 64\n" |
| 183 | "MaxViewports 16\n" |
| 184 | "MaxVertexAtomicCounters 0\n" |
| 185 | "MaxTessControlAtomicCounters 0\n" |
| 186 | "MaxTessEvaluationAtomicCounters 0\n" |
| 187 | "MaxGeometryAtomicCounters 0\n" |
| 188 | "MaxFragmentAtomicCounters 8\n" |
| 189 | "MaxCombinedAtomicCounters 8\n" |
| 190 | "MaxAtomicCounterBindings 1\n" |
| 191 | "MaxVertexAtomicCounterBuffers 0\n" |
| 192 | "MaxTessControlAtomicCounterBuffers 0\n" |
| 193 | "MaxTessEvaluationAtomicCounterBuffers 0\n" |
| 194 | "MaxGeometryAtomicCounterBuffers 0\n" |
| 195 | "MaxFragmentAtomicCounterBuffers 1\n" |
| 196 | "MaxCombinedAtomicCounterBuffers 1\n" |
| 197 | "MaxAtomicCounterBufferSize 16384\n" |
John Kessenich | c7776ec | 2014-01-26 01:37:13 +0000 | [diff] [blame] | 198 | "MaxTransformFeedbackBuffers 4\n" |
| 199 | "MaxTransformFeedbackInterleavedComponents 64\n" |
John Kessenich | 6996841 | 2014-08-13 06:37:59 +0000 | [diff] [blame] | 200 | "MaxCullDistances 8\n" |
| 201 | "MaxCombinedClipAndCullDistances 8\n" |
John Kessenich | cd77f8e | 2014-08-13 16:54:02 +0000 | [diff] [blame] | 202 | "MaxSamples 4\n" |
John Kessenich | 284231c | 2013-10-22 01:50:39 +0000 | [diff] [blame] | 203 | |
| 204 | "nonInductiveForLoops 1\n" |
| 205 | "whileLoops 1\n" |
| 206 | "doWhileLoops 1\n" |
| 207 | "generalUniformIndexing 1\n" |
| 208 | "generalAttributeMatrixVectorIndexing 1\n" |
| 209 | "generalVaryingIndexing 1\n" |
| 210 | "generalSamplerIndexing 1\n" |
| 211 | "generalVariableIndexing 1\n" |
| 212 | "generalConstantMatrixVectorIndexing 1\n" |
| 213 | ; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 214 | |
| 215 | // |
| 216 | // Parse either a .conf file provided by the user or the default string above. |
| 217 | // |
| 218 | void ProcessConfigFile() |
John Kessenich | b51f62c | 2013-04-11 16:31:09 +0000 | [diff] [blame] | 219 | { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 220 | char** configStrings = 0; |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 221 | char* config = 0; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 222 | if (ConfigFile.size() > 0) { |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 223 | configStrings = ReadFileData(ConfigFile.c_str()); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 224 | if (configStrings) |
| 225 | config = *configStrings; |
| 226 | else { |
| 227 | printf("Error opening configuration file; will instead use the default configuration\n"); |
| 228 | usage(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (config == 0) { |
John Kessenich | 6494baf | 2014-02-19 00:08:59 +0000 | [diff] [blame] | 233 | config = new char[strlen(DefaultConfig) + 1]; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 234 | strcpy(config, DefaultConfig); |
| 235 | } |
| 236 | |
| 237 | const char* delims = " \t\n\r"; |
| 238 | const char* token = strtok(config, delims); |
| 239 | while (token) { |
| 240 | const char* valueStr = strtok(0, delims); |
| 241 | if (valueStr == 0 || ! (valueStr[0] == '-' || (valueStr[0] >= '0' && valueStr[0] <= '9'))) { |
| 242 | printf("Error: '%s' bad .conf file. Each name must be followed by one number.\n", valueStr ? valueStr : ""); |
| 243 | return; |
| 244 | } |
| 245 | int value = atoi(valueStr); |
| 246 | |
| 247 | if (strcmp(token, "MaxLights") == 0) |
| 248 | Resources.maxLights = value; |
| 249 | else if (strcmp(token, "MaxClipPlanes") == 0) |
| 250 | Resources.maxClipPlanes = value; |
| 251 | else if (strcmp(token, "MaxTextureUnits") == 0) |
| 252 | Resources.maxTextureUnits = value; |
| 253 | else if (strcmp(token, "MaxTextureCoords") == 0) |
| 254 | Resources.maxTextureCoords = value; |
| 255 | else if (strcmp(token, "MaxVertexAttribs") == 0) |
| 256 | Resources.maxVertexAttribs = value; |
| 257 | else if (strcmp(token, "MaxVertexUniformComponents") == 0) |
| 258 | Resources.maxVertexUniformComponents = value; |
| 259 | else if (strcmp(token, "MaxVaryingFloats") == 0) |
| 260 | Resources.maxVaryingFloats = value; |
| 261 | else if (strcmp(token, "MaxVertexTextureImageUnits") == 0) |
| 262 | Resources.maxVertexTextureImageUnits = value; |
| 263 | else if (strcmp(token, "MaxCombinedTextureImageUnits") == 0) |
| 264 | Resources.maxCombinedTextureImageUnits = value; |
| 265 | else if (strcmp(token, "MaxTextureImageUnits") == 0) |
| 266 | Resources.maxTextureImageUnits = value; |
| 267 | else if (strcmp(token, "MaxFragmentUniformComponents") == 0) |
| 268 | Resources.maxFragmentUniformComponents = value; |
| 269 | else if (strcmp(token, "MaxDrawBuffers") == 0) |
| 270 | Resources.maxDrawBuffers = value; |
| 271 | else if (strcmp(token, "MaxVertexUniformVectors") == 0) |
| 272 | Resources.maxVertexUniformVectors = value; |
| 273 | else if (strcmp(token, "MaxVaryingVectors") == 0) |
| 274 | Resources.maxVaryingVectors = value; |
| 275 | else if (strcmp(token, "MaxFragmentUniformVectors") == 0) |
| 276 | Resources.maxFragmentUniformVectors = value; |
| 277 | else if (strcmp(token, "MaxVertexOutputVectors") == 0) |
| 278 | Resources.maxVertexOutputVectors = value; |
| 279 | else if (strcmp(token, "MaxFragmentInputVectors") == 0) |
| 280 | Resources.maxFragmentInputVectors = value; |
| 281 | else if (strcmp(token, "MinProgramTexelOffset") == 0) |
| 282 | Resources.minProgramTexelOffset = value; |
| 283 | else if (strcmp(token, "MaxProgramTexelOffset") == 0) |
| 284 | Resources.maxProgramTexelOffset = value; |
John Kessenich | e7c59c1 | 2013-10-16 22:28:35 +0000 | [diff] [blame] | 285 | else if (strcmp(token, "MaxClipDistances") == 0) |
| 286 | Resources.maxClipDistances = value; |
John Kessenich | 284231c | 2013-10-22 01:50:39 +0000 | [diff] [blame] | 287 | else if (strcmp(token, "MaxComputeWorkGroupCountX") == 0) |
| 288 | Resources.maxComputeWorkGroupCountX = value; |
| 289 | else if (strcmp(token, "MaxComputeWorkGroupCountY") == 0) |
| 290 | Resources.maxComputeWorkGroupCountY = value; |
| 291 | else if (strcmp(token, "MaxComputeWorkGroupCountZ") == 0) |
| 292 | Resources.maxComputeWorkGroupCountZ = value; |
| 293 | else if (strcmp(token, "MaxComputeWorkGroupSizeX") == 0) |
| 294 | Resources.maxComputeWorkGroupSizeX = value; |
| 295 | else if (strcmp(token, "MaxComputeWorkGroupSizeY") == 0) |
| 296 | Resources.maxComputeWorkGroupSizeY = value; |
| 297 | else if (strcmp(token, "MaxComputeWorkGroupSizeZ") == 0) |
| 298 | Resources.maxComputeWorkGroupSizeZ = value; |
| 299 | else if (strcmp(token, "MaxComputeUniformComponents") == 0) |
| 300 | Resources.maxComputeUniformComponents = value; |
| 301 | else if (strcmp(token, "MaxComputeTextureImageUnits") == 0) |
| 302 | Resources.maxComputeTextureImageUnits = value; |
| 303 | else if (strcmp(token, "MaxComputeImageUniforms") == 0) |
| 304 | Resources.maxComputeImageUniforms = value; |
| 305 | else if (strcmp(token, "MaxComputeAtomicCounters") == 0) |
| 306 | Resources.maxComputeAtomicCounters = value; |
| 307 | else if (strcmp(token, "MaxComputeAtomicCounterBuffers") == 0) |
| 308 | Resources.maxComputeAtomicCounterBuffers = value; |
| 309 | else if (strcmp(token, "MaxVaryingComponents") == 0) |
| 310 | Resources.maxVaryingComponents = value; |
| 311 | else if (strcmp(token, "MaxVertexOutputComponents") == 0) |
| 312 | Resources.maxVertexOutputComponents = value; |
| 313 | else if (strcmp(token, "MaxGeometryInputComponents") == 0) |
| 314 | Resources.maxGeometryInputComponents = value; |
| 315 | else if (strcmp(token, "MaxGeometryOutputComponents") == 0) |
| 316 | Resources.maxGeometryOutputComponents = value; |
| 317 | else if (strcmp(token, "MaxFragmentInputComponents") == 0) |
| 318 | Resources.maxFragmentInputComponents = value; |
| 319 | else if (strcmp(token, "MaxImageUnits") == 0) |
| 320 | Resources.maxImageUnits = value; |
| 321 | else if (strcmp(token, "MaxCombinedImageUnitsAndFragmentOutputs") == 0) |
| 322 | Resources.maxCombinedImageUnitsAndFragmentOutputs = value; |
John Kessenich | ddea678 | 2014-08-10 18:19:36 +0000 | [diff] [blame] | 323 | else if (strcmp(token, "MaxCombinedShaderOutputResources") == 0) |
| 324 | Resources.maxCombinedShaderOutputResources = value; |
John Kessenich | 284231c | 2013-10-22 01:50:39 +0000 | [diff] [blame] | 325 | else if (strcmp(token, "MaxImageSamples") == 0) |
| 326 | Resources.maxImageSamples = value; |
| 327 | else if (strcmp(token, "MaxVertexImageUniforms") == 0) |
| 328 | Resources.maxVertexImageUniforms = value; |
| 329 | else if (strcmp(token, "MaxTessControlImageUniforms") == 0) |
| 330 | Resources.maxTessControlImageUniforms = value; |
| 331 | else if (strcmp(token, "MaxTessEvaluationImageUniforms") == 0) |
| 332 | Resources.maxTessEvaluationImageUniforms = value; |
| 333 | else if (strcmp(token, "MaxGeometryImageUniforms") == 0) |
| 334 | Resources.maxGeometryImageUniforms = value; |
| 335 | else if (strcmp(token, "MaxFragmentImageUniforms") == 0) |
| 336 | Resources.maxFragmentImageUniforms = value; |
| 337 | else if (strcmp(token, "MaxCombinedImageUniforms") == 0) |
| 338 | Resources.maxCombinedImageUniforms = value; |
| 339 | else if (strcmp(token, "MaxGeometryTextureImageUnits") == 0) |
| 340 | Resources.maxGeometryTextureImageUnits = value; |
| 341 | else if (strcmp(token, "MaxGeometryOutputVertices") == 0) |
| 342 | Resources.maxGeometryOutputVertices = value; |
| 343 | else if (strcmp(token, "MaxGeometryTotalOutputComponents") == 0) |
| 344 | Resources.maxGeometryTotalOutputComponents = value; |
| 345 | else if (strcmp(token, "MaxGeometryUniformComponents") == 0) |
| 346 | Resources.maxGeometryUniformComponents = value; |
| 347 | else if (strcmp(token, "MaxGeometryVaryingComponents") == 0) |
| 348 | Resources.maxGeometryVaryingComponents = value; |
| 349 | else if (strcmp(token, "MaxTessControlInputComponents") == 0) |
| 350 | Resources.maxTessControlInputComponents = value; |
| 351 | else if (strcmp(token, "MaxTessControlOutputComponents") == 0) |
| 352 | Resources.maxTessControlOutputComponents = value; |
| 353 | else if (strcmp(token, "MaxTessControlTextureImageUnits") == 0) |
| 354 | Resources.maxTessControlTextureImageUnits = value; |
| 355 | else if (strcmp(token, "MaxTessControlUniformComponents") == 0) |
| 356 | Resources.maxTessControlUniformComponents = value; |
| 357 | else if (strcmp(token, "MaxTessControlTotalOutputComponents") == 0) |
| 358 | Resources.maxTessControlTotalOutputComponents = value; |
| 359 | else if (strcmp(token, "MaxTessEvaluationInputComponents") == 0) |
| 360 | Resources.maxTessEvaluationInputComponents = value; |
| 361 | else if (strcmp(token, "MaxTessEvaluationOutputComponents") == 0) |
| 362 | Resources.maxTessEvaluationOutputComponents = value; |
| 363 | else if (strcmp(token, "MaxTessEvaluationTextureImageUnits") == 0) |
| 364 | Resources.maxTessEvaluationTextureImageUnits = value; |
| 365 | else if (strcmp(token, "MaxTessEvaluationUniformComponents") == 0) |
| 366 | Resources.maxTessEvaluationUniformComponents = value; |
| 367 | else if (strcmp(token, "MaxTessPatchComponents") == 0) |
| 368 | Resources.maxTessPatchComponents = value; |
| 369 | else if (strcmp(token, "MaxPatchVertices") == 0) |
| 370 | Resources.maxPatchVertices = value; |
| 371 | else if (strcmp(token, "MaxTessGenLevel") == 0) |
| 372 | Resources.maxTessGenLevel = value; |
| 373 | else if (strcmp(token, "MaxViewports") == 0) |
| 374 | Resources.maxViewports = value; |
| 375 | else if (strcmp(token, "MaxVertexAtomicCounters") == 0) |
| 376 | Resources.maxVertexAtomicCounters = value; |
| 377 | else if (strcmp(token, "MaxTessControlAtomicCounters") == 0) |
| 378 | Resources.maxTessControlAtomicCounters = value; |
| 379 | else if (strcmp(token, "MaxTessEvaluationAtomicCounters") == 0) |
| 380 | Resources.maxTessEvaluationAtomicCounters = value; |
| 381 | else if (strcmp(token, "MaxGeometryAtomicCounters") == 0) |
| 382 | Resources.maxGeometryAtomicCounters = value; |
| 383 | else if (strcmp(token, "MaxFragmentAtomicCounters") == 0) |
| 384 | Resources.maxFragmentAtomicCounters = value; |
| 385 | else if (strcmp(token, "MaxCombinedAtomicCounters") == 0) |
| 386 | Resources.maxCombinedAtomicCounters = value; |
| 387 | else if (strcmp(token, "MaxAtomicCounterBindings") == 0) |
| 388 | Resources.maxAtomicCounterBindings = value; |
| 389 | else if (strcmp(token, "MaxVertexAtomicCounterBuffers") == 0) |
| 390 | Resources.maxVertexAtomicCounterBuffers = value; |
| 391 | else if (strcmp(token, "MaxTessControlAtomicCounterBuffers") == 0) |
| 392 | Resources.maxTessControlAtomicCounterBuffers = value; |
| 393 | else if (strcmp(token, "MaxTessEvaluationAtomicCounterBuffers") == 0) |
| 394 | Resources.maxTessEvaluationAtomicCounterBuffers = value; |
| 395 | else if (strcmp(token, "MaxGeometryAtomicCounterBuffers") == 0) |
| 396 | Resources.maxGeometryAtomicCounterBuffers = value; |
| 397 | else if (strcmp(token, "MaxFragmentAtomicCounterBuffers") == 0) |
| 398 | Resources.maxFragmentAtomicCounterBuffers = value; |
| 399 | else if (strcmp(token, "MaxCombinedAtomicCounterBuffers") == 0) |
| 400 | Resources.maxCombinedAtomicCounterBuffers = value; |
| 401 | else if (strcmp(token, "MaxAtomicCounterBufferSize") == 0) |
| 402 | Resources.maxAtomicCounterBufferSize = value; |
John Kessenich | c7776ec | 2014-01-26 01:37:13 +0000 | [diff] [blame] | 403 | else if (strcmp(token, "MaxTransformFeedbackBuffers") == 0) |
| 404 | Resources.maxTransformFeedbackBuffers = value; |
| 405 | else if (strcmp(token, "MaxTransformFeedbackInterleavedComponents") == 0) |
| 406 | Resources.maxTransformFeedbackInterleavedComponents = value; |
John Kessenich | 6996841 | 2014-08-13 06:37:59 +0000 | [diff] [blame] | 407 | else if (strcmp(token, "MaxCullDistances") == 0) |
| 408 | Resources.maxCullDistances = value; |
| 409 | else if (strcmp(token, "MaxCombinedClipAndCullDistances") == 0) |
| 410 | Resources.maxCombinedClipAndCullDistances = value; |
John Kessenich | cd77f8e | 2014-08-13 16:54:02 +0000 | [diff] [blame] | 411 | else if (strcmp(token, "MaxSamples") == 0) |
| 412 | Resources.maxSamples = value; |
John Kessenich | 284231c | 2013-10-22 01:50:39 +0000 | [diff] [blame] | 413 | |
John Kessenich | a5830df | 2013-10-02 05:10:48 +0000 | [diff] [blame] | 414 | else if (strcmp(token, "nonInductiveForLoops") == 0) |
| 415 | Resources.limits.nonInductiveForLoops = (value != 0); |
| 416 | else if (strcmp(token, "whileLoops") == 0) |
| 417 | Resources.limits.whileLoops = (value != 0); |
| 418 | else if (strcmp(token, "doWhileLoops") == 0) |
| 419 | Resources.limits.doWhileLoops = (value != 0); |
| 420 | else if (strcmp(token, "generalUniformIndexing") == 0) |
| 421 | Resources.limits.generalUniformIndexing = (value != 0); |
| 422 | else if (strcmp(token, "generalAttributeMatrixVectorIndexing") == 0) |
| 423 | Resources.limits.generalAttributeMatrixVectorIndexing = (value != 0); |
| 424 | else if (strcmp(token, "generalVaryingIndexing") == 0) |
| 425 | Resources.limits.generalVaryingIndexing = (value != 0); |
| 426 | else if (strcmp(token, "generalSamplerIndexing") == 0) |
| 427 | Resources.limits.generalSamplerIndexing = (value != 0); |
| 428 | else if (strcmp(token, "generalVariableIndexing") == 0) |
| 429 | Resources.limits.generalVariableIndexing = (value != 0); |
| 430 | else if (strcmp(token, "generalConstantMatrixVectorIndexing") == 0) |
| 431 | Resources.limits.generalConstantMatrixVectorIndexing = (value != 0); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 432 | else |
| 433 | printf("Warning: unrecognized limit (%s) in configuration file.\n", token); |
| 434 | |
| 435 | token = strtok(0, delims); |
| 436 | } |
| 437 | if (configStrings) |
| 438 | FreeFileData(configStrings); |
Andrew Woloszyn | b891c2b | 2016-01-18 09:26:25 -0500 | [diff] [blame] | 439 | else |
| 440 | delete[] config; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 441 | } |
| 442 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 443 | // thread-safe list of shaders to asynchronously grab and compile |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 444 | glslang::TWorklist Worklist; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 445 | |
| 446 | // 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] | 447 | glslang::TWorkItem** Work = 0; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 448 | int NumWorkItems = 0; |
| 449 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 450 | int Options = 0; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 451 | const char* ExecutableName = nullptr; |
| 452 | const char* binaryFileName = nullptr; |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 453 | const char* entryPointName = nullptr; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 454 | |
| 455 | // |
| 456 | // Create the default name for saving a binary if -o is not provided. |
| 457 | // |
| 458 | const char* GetBinaryName(EShLanguage stage) |
| 459 | { |
| 460 | const char* name; |
| 461 | if (binaryFileName == nullptr) { |
| 462 | switch (stage) { |
| 463 | case EShLangVertex: name = "vert.spv"; break; |
| 464 | case EShLangTessControl: name = "tesc.spv"; break; |
| 465 | case EShLangTessEvaluation: name = "tese.spv"; break; |
| 466 | case EShLangGeometry: name = "geom.spv"; break; |
| 467 | case EShLangFragment: name = "frag.spv"; break; |
| 468 | case EShLangCompute: name = "comp.spv"; break; |
| 469 | default: name = "unknown"; break; |
| 470 | } |
| 471 | } else |
| 472 | name = binaryFileName; |
| 473 | |
| 474 | return name; |
| 475 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 476 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 477 | // |
| 478 | // *.conf => this is a config file that can set limits/resources |
| 479 | // |
| 480 | bool SetConfigFile(const std::string& name) |
| 481 | { |
| 482 | if (name.size() < 5) |
| 483 | return false; |
| 484 | |
John Kessenich | 4c70685 | 2013-10-11 16:28:43 +0000 | [diff] [blame] | 485 | if (name.compare(name.size() - 5, 5, ".conf") == 0) { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 486 | ConfigFile = name; |
| 487 | return true; |
| 488 | } |
| 489 | |
| 490 | return false; |
| 491 | } |
| 492 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 493 | // |
| 494 | // Give error and exit with failure code. |
| 495 | // |
| 496 | void Error(const char* message) |
| 497 | { |
| 498 | printf("%s: Error %s (use -h for usage)\n", ExecutableName, message); |
| 499 | exit(EFailUsage); |
| 500 | } |
| 501 | |
| 502 | // |
| 503 | // Do all command-line argument parsing. This includes building up the work-items |
| 504 | // to be processed later, and saving all the command-line options. |
| 505 | // |
| 506 | // Does not return (it exits) if command-line is fatally flawed. |
| 507 | // |
| 508 | void ProcessArguments(int argc, char* argv[]) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 509 | { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 510 | ExecutableName = argv[0]; |
| 511 | 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] | 512 | Work = new glslang::TWorkItem*[NumWorkItems]; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 513 | for (int w = 0; w < NumWorkItems; ++w) |
| 514 | Work[w] = 0; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 515 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 516 | argc--; |
| 517 | argv++; |
| 518 | for (; argc >= 1; argc--, argv++) { |
| 519 | if (argv[0][0] == '-') { |
John Kessenich | 2aa7f3a | 2015-05-15 16:02:07 +0000 | [diff] [blame] | 520 | switch (argv[0][1]) { |
| 521 | case 'H': |
| 522 | Options |= EOptionHumanReadableSpv; |
| 523 | // fall through to -V |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 524 | case 'V': |
| 525 | Options |= EOptionSpv; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 526 | Options |= EOptionVulkanRules; |
| 527 | Options |= EOptionLinkProgram; |
| 528 | break; |
| 529 | case 'G': |
| 530 | Options |= EOptionSpv; |
John Kessenich | d78e351 | 2014-08-25 20:07:55 +0000 | [diff] [blame] | 531 | Options |= EOptionLinkProgram; |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 532 | break; |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 533 | case 'E': |
| 534 | Options |= EOptionOutputPreprocessed; |
| 535 | break; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 536 | case 'c': |
| 537 | Options |= EOptionDumpConfig; |
| 538 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 539 | case 'd': |
John Kessenich | 26ad268 | 2014-08-13 20:17:19 +0000 | [diff] [blame] | 540 | Options |= EOptionDefaultDesktop; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 541 | break; |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame^] | 542 | case 'D': |
| 543 | Options |= EOptionReadHlsl; |
| 544 | break; |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 545 | case 'e': |
| 546 | // HLSL todo: entry point handle needs much more sophistication. |
| 547 | // This is okay for one compilation unit with one entry point. |
| 548 | entryPointName = argv[1]; |
| 549 | if (argc > 0) { |
| 550 | argc--; |
| 551 | argv++; |
| 552 | } else |
| 553 | Error("no <entry-point> provided for -e"); |
| 554 | break; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 555 | case 'h': |
| 556 | usage(); |
| 557 | break; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 558 | case 'i': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 559 | Options |= EOptionIntermediate; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 560 | break; |
| 561 | case 'l': |
John Kessenich | d78e351 | 2014-08-25 20:07:55 +0000 | [diff] [blame] | 562 | Options |= EOptionLinkProgram; |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 563 | break; |
| 564 | case 'm': |
| 565 | Options |= EOptionMemoryLeakMode; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 566 | break; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 567 | case 'o': |
| 568 | binaryFileName = argv[1]; |
| 569 | if (argc > 0) { |
| 570 | argc--; |
| 571 | argv++; |
| 572 | } else |
| 573 | Error("no <file> provided for -o"); |
| 574 | break; |
John Kessenich | 11f9fc7 | 2013-11-07 01:06:34 +0000 | [diff] [blame] | 575 | case 'q': |
| 576 | Options |= EOptionDumpReflection; |
| 577 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 578 | case 'r': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 579 | Options |= EOptionRelaxedErrors; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 580 | break; |
| 581 | case 's': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 582 | Options |= EOptionSuppressInfolog; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 583 | break; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 584 | case 't': |
| 585 | #ifdef _WIN32 |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 586 | Options |= EOptionMultiThreaded; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 587 | #endif |
| 588 | break; |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 589 | case 'v': |
| 590 | Options |= EOptionDumpVersions; |
| 591 | break; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 592 | case 'w': |
| 593 | Options |= EOptionSuppressWarnings; |
| 594 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 595 | default: |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 596 | usage(); |
| 597 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 598 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 599 | } else { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 600 | std::string name(argv[0]); |
| 601 | if (! SetConfigFile(name)) { |
| 602 | Work[argc] = new glslang::TWorkItem(name); |
| 603 | Worklist.add(Work[argc]); |
| 604 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 605 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 606 | } |
| 607 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 608 | // Make sure that -E is not specified alongside linking (which includes SPV generation) |
| 609 | if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram)) |
| 610 | Error("can't use -E when linking is selected"); |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 611 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 612 | // -o makes no sense if there is no target binary |
| 613 | if (binaryFileName && (Options & EOptionSpv) == 0) |
| 614 | Error("no binary generation requested (e.g., -V)"); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 615 | } |
| 616 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 617 | // |
| 618 | // Translate the meaningful subset of command-line options to parser-behavior options. |
| 619 | // |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 620 | void SetMessageOptions(EShMessages& messages) |
| 621 | { |
| 622 | if (Options & EOptionRelaxedErrors) |
| 623 | messages = (EShMessages)(messages | EShMsgRelaxedErrors); |
| 624 | if (Options & EOptionIntermediate) |
| 625 | messages = (EShMessages)(messages | EShMsgAST); |
| 626 | if (Options & EOptionSuppressWarnings) |
| 627 | messages = (EShMessages)(messages | EShMsgSuppressWarnings); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 628 | if (Options & EOptionSpv) |
| 629 | messages = (EShMessages)(messages | EShMsgSpvRules); |
| 630 | if (Options & EOptionVulkanRules) |
| 631 | messages = (EShMessages)(messages | EShMsgVulkanRules); |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 632 | if (Options & EOptionOutputPreprocessed) |
| 633 | messages = (EShMessages)(messages | EShMsgOnlyPreprocessor); |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame^] | 634 | if (Options & EOptionReadHlsl) |
| 635 | messages = (EShMessages)(messages | EShMsgReadHlsl); |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 636 | } |
| 637 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 638 | // |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 639 | // Thread entry point, for non-linking asynchronous mode. |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 640 | // |
| 641 | // Return 0 for failure, 1 for success. |
| 642 | // |
Pyry Haulos | 5f6892e | 2015-12-01 12:59:53 -0800 | [diff] [blame] | 643 | unsigned int CompileShaders(void*) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 644 | { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 645 | glslang::TWorkItem* workItem; |
| 646 | while (Worklist.remove(workItem)) { |
| 647 | ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 648 | if (compiler == 0) |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 649 | return 0; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 650 | |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 651 | CompileFile(workItem->name.c_str(), compiler); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 652 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 653 | if (! (Options & EOptionSuppressInfolog)) |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 654 | workItem->results = ShGetInfoLog(compiler); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 655 | |
| 656 | ShDestruct(compiler); |
| 657 | } |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
John Kessenich | 6626cad | 2015-06-19 05:14:19 +0000 | [diff] [blame] | 662 | // Outputs the given string, but only if it is non-null and non-empty. |
| 663 | // This prevents erroneous newlines from appearing. |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 664 | void PutsIfNonEmpty(const char* str) |
John Kessenich | 6626cad | 2015-06-19 05:14:19 +0000 | [diff] [blame] | 665 | { |
| 666 | if (str && str[0]) { |
| 667 | puts(str); |
| 668 | } |
| 669 | } |
| 670 | |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 671 | // Outputs the given string to stderr, but only if it is non-null and non-empty. |
| 672 | // This prevents erroneous newlines from appearing. |
| 673 | void StderrIfNonEmpty(const char* str) |
| 674 | { |
| 675 | if (str && str[0]) { |
| 676 | fprintf(stderr, "%s\n", str); |
| 677 | } |
| 678 | } |
| 679 | |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 680 | // Simple bundling of what makes a compilation unit for ease in passing around, |
| 681 | // and separation of handling file IO versus API (programmatic) compilation. |
| 682 | struct ShaderCompUnit { |
| 683 | EShLanguage stage; |
| 684 | std::string fileName; |
| 685 | char** text; // memory owned/managed externally |
| 686 | }; |
| 687 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 688 | // |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 689 | // For linking mode: Will independently parse each compilation unit, but then put them |
| 690 | // in the same program and link them together, making at most one linked module per |
| 691 | // pipeline stage. |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 692 | // |
| 693 | // Uses the new C++ interface instead of the old handle-based interface. |
| 694 | // |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 695 | |
| 696 | void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits) |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 697 | { |
| 698 | // keep track of what to free |
| 699 | std::list<glslang::TShader*> shaders; |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 700 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 701 | EShMessages messages = EShMsgDefault; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 702 | SetMessageOptions(messages); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 703 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 704 | // |
| 705 | // Per-shader processing... |
| 706 | // |
| 707 | |
John Kessenich | 5b0f13a | 2013-11-01 03:08:40 +0000 | [diff] [blame] | 708 | glslang::TProgram& program = *new glslang::TProgram; |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 709 | for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) { |
| 710 | const auto &compUnit = *it; |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 711 | glslang::TShader* shader = new glslang::TShader(compUnit.stage); |
| 712 | shader->setStrings(compUnit.text, 1); |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 713 | if (entryPointName) // HLSL todo: this needs to be tracked per compUnits |
| 714 | shader->setEntryPoint(entryPointName); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 715 | shaders.push_back(shader); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 716 | |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 717 | const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 718 | |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 719 | if (Options & EOptionOutputPreprocessed) { |
| 720 | std::string str; |
Dejan Mircevski | 7be4b82 | 2015-06-17 11:40:33 -0400 | [diff] [blame] | 721 | if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, |
| 722 | messages, &str, glslang::TShader::ForbidInclude())) { |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 723 | PutsIfNonEmpty(str.c_str()); |
| 724 | } else { |
| 725 | CompileFailed = true; |
| 726 | } |
| 727 | StderrIfNonEmpty(shader->getInfoLog()); |
| 728 | StderrIfNonEmpty(shader->getInfoDebugLog()); |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 729 | continue; |
| 730 | } |
| 731 | if (! shader->parse(&Resources, defaultVersion, false, messages)) |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 732 | CompileFailed = true; |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 733 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 734 | program.addShader(shader); |
| 735 | |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 736 | if (! (Options & EOptionSuppressInfolog) && |
| 737 | ! (Options & EOptionMemoryLeakMode)) { |
| 738 | PutsIfNonEmpty(compUnit.fileName.c_str()); |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 739 | PutsIfNonEmpty(shader->getInfoLog()); |
| 740 | PutsIfNonEmpty(shader->getInfoDebugLog()); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 741 | } |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | // |
| 745 | // Program-level processing... |
| 746 | // |
| 747 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 748 | // Link |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 749 | if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages)) |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 750 | LinkFailed = true; |
| 751 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 752 | // Report |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 753 | if (! (Options & EOptionSuppressInfolog) && |
| 754 | ! (Options & EOptionMemoryLeakMode)) { |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 755 | PutsIfNonEmpty(program.getInfoLog()); |
| 756 | PutsIfNonEmpty(program.getInfoDebugLog()); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 757 | } |
| 758 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 759 | // Reflect |
John Kessenich | 11f9fc7 | 2013-11-07 01:06:34 +0000 | [diff] [blame] | 760 | if (Options & EOptionDumpReflection) { |
| 761 | program.buildReflection(); |
| 762 | program.dumpReflection(); |
| 763 | } |
| 764 | |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 765 | // Dump SPIR-V |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 766 | if (Options & EOptionSpv) { |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 767 | if (CompileFailed || LinkFailed) |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 768 | printf("SPIR-V is not generated for failed compile or link\n"); |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 769 | else { |
| 770 | for (int stage = 0; stage < EShLangCount; ++stage) { |
John Kessenich | a7a68a9 | 2014-08-24 18:21:00 +0000 | [diff] [blame] | 771 | if (program.getIntermediate((EShLanguage)stage)) { |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 772 | std::vector<unsigned int> spirv; |
| 773 | glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv); |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 774 | |
| 775 | // Dump the spv to a file or stdout, etc., but only if not doing |
| 776 | // memory/perf testing, as it's not internal to programmatic use. |
| 777 | if (! (Options & EOptionMemoryLeakMode)) { |
| 778 | glslang::OutputSpv(spirv, GetBinaryName((EShLanguage)stage)); |
| 779 | if (Options & EOptionHumanReadableSpv) { |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 780 | spv::Disassemble(std::cout, spirv); |
| 781 | } |
John Kessenich | acba772 | 2015-03-04 03:48:38 +0000 | [diff] [blame] | 782 | } |
John Kessenich | a7a68a9 | 2014-08-24 18:21:00 +0000 | [diff] [blame] | 783 | } |
John Kessenich | 92f9038 | 2014-07-28 04:21:04 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
John Kessenich | 5b0f13a | 2013-11-01 03:08:40 +0000 | [diff] [blame] | 788 | // Free everything up, program has to go before the shaders |
| 789 | // because it might have merged stuff from the shaders, and |
| 790 | // the stuff from the shaders has to have its destructors called |
| 791 | // before the pools holding the memory in the shaders is freed. |
| 792 | delete &program; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 793 | while (shaders.size() > 0) { |
| 794 | delete shaders.back(); |
| 795 | shaders.pop_back(); |
| 796 | } |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 797 | } |
| 798 | |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 799 | // |
| 800 | // Do file IO part of compile and link, handing off the pure |
| 801 | // API/programmatic mode to CompileAndLinkShaderUnits(), which can |
| 802 | // be put in a loop for testing memory footprint and performance. |
| 803 | // |
| 804 | // This is just for linking mode: meaning all the shaders will be put into the |
| 805 | // the same program linked together. |
| 806 | // |
| 807 | // This means there are a limited number of work items (not multi-threading mode) |
| 808 | // and that the point is testing at the linking level. Hence, to enable |
| 809 | // performance and memory testing, the actual compile/link can be put in |
| 810 | // a loop, independent of processing the work items and file IO. |
| 811 | // |
| 812 | void CompileAndLinkShaderFiles() |
| 813 | { |
| 814 | std::vector<ShaderCompUnit> compUnits; |
| 815 | |
| 816 | // Transfer all the work items from to a simple list of |
| 817 | // of compilation units. (We don't care about the thread |
| 818 | // work-item distribution properties in this path, which |
| 819 | // is okay due to the limited number of shaders, know since |
| 820 | // they are all getting linked together.) |
| 821 | glslang::TWorkItem* workItem; |
| 822 | while (Worklist.remove(workItem)) { |
| 823 | ShaderCompUnit compUnit = { |
| 824 | FindLanguage(workItem->name), |
| 825 | workItem->name, |
| 826 | ReadFileData(workItem->name.c_str()) |
| 827 | }; |
| 828 | |
| 829 | if (! compUnit.text) { |
| 830 | usage(); |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | compUnits.push_back(compUnit); |
| 835 | } |
| 836 | |
| 837 | // Actual call to programmatic processing of compile and link, |
| 838 | // in a loop for testing memory and performance. This part contains |
| 839 | // all the perf/memory that a programmatic consumer will care about. |
| 840 | for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) { |
| 841 | for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) |
| 842 | CompileAndLinkShaderUnits(compUnits); |
| 843 | |
| 844 | if (Options & EOptionMemoryLeakMode) |
| 845 | glslang::OS_DumpMemoryCounters(); |
| 846 | } |
| 847 | |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 848 | for (auto it = compUnits.begin(); it != compUnits.end(); ++it) |
| 849 | FreeFileData(it->text); |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 850 | } |
| 851 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 852 | int C_DECL main(int argc, char* argv[]) |
| 853 | { |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 854 | ProcessArguments(argc, argv); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 855 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 856 | if (Options & EOptionDumpConfig) { |
| 857 | printf("%s", DefaultConfig); |
| 858 | if (Worklist.empty()) |
| 859 | return ESuccess; |
| 860 | } |
| 861 | |
John Kessenich | 0da9eaa | 2015-08-01 17:10:02 -0600 | [diff] [blame] | 862 | if (Options & EOptionDumpVersions) { |
| 863 | printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE); |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 864 | printf("ESSL Version: %s\n", glslang::GetEsslVersionString()); |
| 865 | printf("GLSL Version: %s\n", glslang::GetGlslVersionString()); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 866 | std::string spirvVersion; |
| 867 | glslang::GetSpirvVersion(spirvVersion); |
John Kessenich | 0da9eaa | 2015-08-01 17:10:02 -0600 | [diff] [blame] | 868 | printf("SPIR-V Version %s\n", spirvVersion.c_str()); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 869 | printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 870 | printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId()); |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 871 | if (Worklist.empty()) |
| 872 | return ESuccess; |
| 873 | } |
| 874 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 875 | if (Worklist.empty()) { |
| 876 | usage(); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | ProcessConfigFile(); |
| 880 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 881 | // |
| 882 | // Two modes: |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 883 | // 1) linking all arguments together, single-threaded, new C++ interface |
| 884 | // 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] | 885 | // |
John Kessenich | c555ddd | 2015-06-17 02:38:44 +0000 | [diff] [blame] | 886 | if (Options & EOptionLinkProgram || |
| 887 | Options & EOptionOutputPreprocessed) { |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 888 | glslang::InitializeProcess(); |
John Kessenich | c57b2a9 | 2016-01-16 15:30:03 -0700 | [diff] [blame] | 889 | CompileAndLinkShaderFiles(); |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 890 | glslang::FinalizeProcess(); |
Andrew Woloszyn | b891c2b | 2016-01-18 09:26:25 -0500 | [diff] [blame] | 891 | for (int w = 0; w < NumWorkItems; ++w) { |
| 892 | if (Work[w]) { |
| 893 | delete Work[w]; |
| 894 | } |
| 895 | } |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 896 | } else { |
| 897 | ShInitialize(); |
| 898 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 899 | bool printShaderNames = Worklist.size() > 1; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 900 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 901 | if (Options & EOptionMultiThreaded) { |
| 902 | const int NumThreads = 16; |
| 903 | void* threads[NumThreads]; |
| 904 | for (int t = 0; t < NumThreads; ++t) { |
| 905 | threads[t] = glslang::OS_CreateThread(&CompileShaders); |
| 906 | if (! threads[t]) { |
| 907 | printf("Failed to create thread\n"); |
| 908 | return EFailThreadCreate; |
| 909 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 910 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 911 | glslang::OS_WaitForAllThreads(threads, NumThreads); |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 912 | } else |
| 913 | CompileShaders(0); |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 914 | |
| 915 | // Print out all the resulting infologs |
| 916 | for (int w = 0; w < NumWorkItems; ++w) { |
| 917 | if (Work[w]) { |
Kenneth Perry | b07e6be | 2015-12-15 10:52:34 -0600 | [diff] [blame] | 918 | if (printShaderNames || Work[w]->results.size() > 0) |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 919 | PutsIfNonEmpty(Work[w]->name.c_str()); |
| 920 | PutsIfNonEmpty(Work[w]->results.c_str()); |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 921 | delete Work[w]; |
| 922 | } |
| 923 | } |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 924 | |
| 925 | ShFinalize(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 926 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 927 | |
Andrew Woloszyn | b891c2b | 2016-01-18 09:26:25 -0500 | [diff] [blame] | 928 | delete[] Work; |
| 929 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 930 | if (CompileFailed) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 931 | return EFailCompile; |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 932 | if (LinkFailed) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 933 | return EFailLink; |
| 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | // |
| 939 | // Deduce the language from the filename. Files must end in one of the |
| 940 | // following extensions: |
| 941 | // |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 942 | // .vert = vertex |
| 943 | // .tesc = tessellation control |
| 944 | // .tese = tessellation evaluation |
| 945 | // .geom = geometry |
| 946 | // .frag = fragment |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 947 | // .comp = compute |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 948 | // |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 949 | EShLanguage FindLanguage(const std::string& name) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 950 | { |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 951 | size_t ext = name.rfind('.'); |
| 952 | if (ext == std::string::npos) { |
| 953 | usage(); |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 954 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 955 | } |
| 956 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 957 | std::string suffix = name.substr(ext + 1, std::string::npos); |
| 958 | if (suffix == "vert") |
| 959 | return EShLangVertex; |
| 960 | else if (suffix == "tesc") |
| 961 | return EShLangTessControl; |
| 962 | else if (suffix == "tese") |
| 963 | return EShLangTessEvaluation; |
| 964 | else if (suffix == "geom") |
| 965 | return EShLangGeometry; |
| 966 | else if (suffix == "frag") |
| 967 | return EShLangFragment; |
John Kessenich | c027579 | 2013-08-09 17:14:49 +0000 | [diff] [blame] | 968 | else if (suffix == "comp") |
| 969 | return EShLangCompute; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 970 | |
| 971 | usage(); |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 972 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 973 | } |
| 974 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 975 | // |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 976 | // Read a file's data into a string, and compile it using the old interface ShCompile, |
| 977 | // for non-linkable results. |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 978 | // |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 979 | void CompileFile(const char* fileName, ShHandle compiler) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 980 | { |
John Kessenich | ca3457f | 2015-05-18 01:59:45 +0000 | [diff] [blame] | 981 | int ret = 0; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 982 | char** shaderStrings = ReadFileData(fileName); |
John Kessenich | db4cd54 | 2013-06-26 22:42:55 +0000 | [diff] [blame] | 983 | if (! shaderStrings) { |
| 984 | usage(); |
John Kessenich | db4cd54 | 2013-06-26 22:42:55 +0000 | [diff] [blame] | 985 | } |
| 986 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 987 | int* lengths = new int[NumShaderStrings]; |
| 988 | |
| 989 | // move to length-based strings, rather than null-terminated strings |
| 990 | for (int s = 0; s < NumShaderStrings; ++s) |
John Kessenich | 35f04bd | 2014-02-19 02:47:20 +0000 | [diff] [blame] | 991 | lengths[s] = (int)strlen(shaderStrings[s]); |
John Kessenich | 09da79e | 2013-04-17 19:34:23 +0000 | [diff] [blame] | 992 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 993 | if (! shaderStrings) { |
| 994 | CompileFailed = true; |
| 995 | return; |
| 996 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 997 | |
John Kessenich | 52ac67e | 2013-05-05 23:46:22 +0000 | [diff] [blame] | 998 | EShMessages messages = EShMsgDefault; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 999 | SetMessageOptions(messages); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 1000 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 1001 | for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) { |
| 1002 | for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) { |
John Kessenich | 26ad268 | 2014-08-13 20:17:19 +0000 | [diff] [blame] | 1003 | //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] | 1004 | 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] | 1005 | //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err", |
| 1006 | // "or should be l", "ine 1", "string 5\n", "float glo", "bal", |
| 1007 | // ";\n#error should be line 2\n void main() {", "global = 2.3;}" }; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 1008 | //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" }; |
John Kessenich | ca3457f | 2015-05-18 01:59:45 +0000 | [diff] [blame] | 1009 | //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] | 1010 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1011 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 1012 | if (Options & EOptionMemoryLeakMode) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 1013 | glslang::OS_DumpMemoryCounters(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1014 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1015 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 1016 | delete [] lengths; |
| 1017 | FreeFileData(shaderStrings); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1018 | |
John Kessenich | c999ba2 | 2013-11-07 23:33:24 +0000 | [diff] [blame] | 1019 | if (ret == 0) |
| 1020 | CompileFailed = true; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1023 | // |
| 1024 | // print usage to stdout |
| 1025 | // |
| 1026 | void usage() |
| 1027 | { |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 1028 | printf("Usage: glslangValidator [option]... [file]...\n" |
| 1029 | "\n" |
John Kessenich | 0df0cde | 2015-03-03 17:09:43 +0000 | [diff] [blame] | 1030 | "Where: each 'file' ends in .<stage>, where <stage> is one of\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1031 | " .conf to provide an optional config file that replaces the default configuration\n" |
| 1032 | " (see -c option below for generating a template)\n" |
| 1033 | " .vert for a vertex shader\n" |
| 1034 | " .tesc for a tessellation control shader\n" |
| 1035 | " .tese for a tessellation evaluation shader\n" |
| 1036 | " .geom for a geometry shader\n" |
| 1037 | " .frag for a fragment shader\n" |
| 1038 | " .comp for a compute shader\n" |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 1039 | "\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 1040 | "Compilation warnings and errors will be printed to stdout.\n" |
John Kessenich | 319de23 | 2013-12-04 04:43:40 +0000 | [diff] [blame] | 1041 | "\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 1042 | "To get other information, use one of the following options:\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1043 | "Each option must be specified separately.\n" |
| 1044 | " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n" |
| 1045 | " default file name is <stage>.spv (-o overrides this)\n" |
| 1046 | " (unless -o is specified, which overrides the default file name)\n" |
| 1047 | " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n" |
| 1048 | " default file name is <stage>.spv (-o overrides this)\n" |
| 1049 | " -H print human readable form of SPIR-V; turns on -V\n" |
Andrew Woloszyn | aae1ad8 | 2015-06-24 17:00:46 -0400 | [diff] [blame] | 1050 | " -E print pre-processed GLSL; cannot be used with -l;\n" |
| 1051 | " errors will appear on stderr.\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1052 | " -c configuration dump;\n" |
| 1053 | " creates the default configuration file (redirect to a .conf file)\n" |
| 1054 | " -d default to desktop (#version 110) when there is no shader #version\n" |
| 1055 | " (default is ES version 100)\n" |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame^] | 1056 | " -D input is HLSL\n" |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 1057 | " -e specify entry-point name\n" |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1058 | " -h print this usage message\n" |
| 1059 | " -i intermediate tree (glslang AST) is printed out\n" |
| 1060 | " -l link all input files together to form a single module\n" |
| 1061 | " -m memory leak mode\n" |
| 1062 | " -o <file> save binary into <file>, requires a binary option (e.g., -V)\n" |
| 1063 | " -q dump reflection query database\n" |
| 1064 | " -r relaxed semantic error-checking mode\n" |
| 1065 | " -s silent mode\n" |
| 1066 | " -t multi-threaded mode\n" |
| 1067 | " -v print version strings\n" |
| 1068 | " -w suppress warnings (except as required by #extension : warn)\n" |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame] | 1069 | ); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1070 | |
| 1071 | exit(EFailUsage); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
John Kessenich | 3ce4e59 | 2014-10-06 19:57:34 +0000 | [diff] [blame] | 1074 | #if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 1075 | |
| 1076 | #include <errno.h> |
| 1077 | |
| 1078 | int fopen_s( |
| 1079 | FILE** pFile, |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 1080 | const char* filename, |
| 1081 | const char* mode |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 1082 | ) |
| 1083 | { |
| 1084 | if (!pFile || !filename || !mode) { |
| 1085 | return EINVAL; |
| 1086 | } |
| 1087 | |
| 1088 | FILE* f = fopen(filename, mode); |
| 1089 | if (! f) { |
| 1090 | if (errno != 0) { |
| 1091 | return errno; |
| 1092 | } else { |
| 1093 | return ENOENT; |
| 1094 | } |
| 1095 | } |
| 1096 | *pFile = f; |
| 1097 | |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
| 1101 | #endif |
| 1102 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1103 | // |
| 1104 | // Malloc a string of sufficient size and read a string into it. |
| 1105 | // |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 1106 | char** ReadFileData(const char* fileName) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1107 | { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1108 | FILE *in = nullptr; |
John Kessenich | 3ce4e59 | 2014-10-06 19:57:34 +0000 | [diff] [blame] | 1109 | int errorCode = fopen_s(&in, fileName, "r"); |
John Kessenich | d6c72a4 | 2014-08-18 19:42:35 +0000 | [diff] [blame] | 1110 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1111 | int count = 0; |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1112 | const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings |
| 1113 | char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData() |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1114 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1115 | if (errorCode || in == nullptr) |
| 1116 | Error("unable to open input file"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1117 | |
| 1118 | while (fgetc(in) != EOF) |
| 1119 | count++; |
| 1120 | |
John Kessenich | d6c72a4 | 2014-08-18 19:42:35 +0000 | [diff] [blame] | 1121 | fseek(in, 0, SEEK_SET); |
John Kessenich | ca3457f | 2015-05-18 01:59:45 +0000 | [diff] [blame] | 1122 | |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1123 | char *fdata = (char*)malloc(count+2); // freed before return of this function |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1124 | if (! fdata) |
| 1125 | Error("can't allocate memory"); |
| 1126 | |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1127 | if ((int)fread(fdata, 1, count, in) != count) { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1128 | free(fdata); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1129 | Error("can't read input file"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1130 | } |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 1131 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1132 | fdata[count] = '\0'; |
| 1133 | fclose(in); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1134 | |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 1135 | if (count == 0) { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1136 | // recover from empty file |
| 1137 | return_data[0] = (char*)malloc(count+2); // freed in FreeFileData() |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1138 | return_data[0][0]='\0'; |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 1139 | NumShaderStrings = 0; |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1140 | free(fdata); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1141 | |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1142 | return return_data; |
| 1143 | } else |
| 1144 | NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings |
| 1145 | |
| 1146 | // 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] | 1147 | int len = (int)(ceil)((float)count/(float)NumShaderStrings); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1148 | int ptr_len = 0; |
| 1149 | int i = 0; |
| 1150 | while (count > 0) { |
| 1151 | return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData() |
| 1152 | memcpy(return_data[i], fdata + ptr_len, len); |
| 1153 | return_data[i][len] = '\0'; |
| 1154 | count -= len; |
| 1155 | ptr_len += len; |
| 1156 | if (count < len) { |
| 1157 | if (count == 0) { |
| 1158 | NumShaderStrings = i + 1; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1159 | break; |
| 1160 | } |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1161 | len = count; |
John Kessenich | d6c72a4 | 2014-08-18 19:42:35 +0000 | [diff] [blame] | 1162 | } |
| 1163 | ++i; |
| 1164 | } |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1165 | |
| 1166 | free(fdata); |
| 1167 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1168 | return return_data; |
| 1169 | } |
| 1170 | |
John Kessenich | 51cdd90 | 2014-02-18 23:37:57 +0000 | [diff] [blame] | 1171 | void FreeFileData(char** data) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1172 | { |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1173 | for(int i = 0; i < NumShaderStrings; i++) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1174 | free(data[i]); |
John Kessenich | b329715 | 2015-07-11 18:01:03 -0600 | [diff] [blame] | 1175 | |
| 1176 | free(data); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 1179 | void InfoLogMsg(const char* msg, const char* name, const int num) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1180 | { |
John Kessenich | fae38ee | 2015-06-10 23:23:12 +0000 | [diff] [blame] | 1181 | if (num >= 0 ) |
| 1182 | printf("#### %s %s %d INFO LOG ####\n", msg, name, num); |
| 1183 | else |
| 1184 | printf("#### %s %s INFO LOG ####\n", msg, name); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1185 | } |