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