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