John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1 | // |
| 2 | //Copyright (C) 2002-2005 3Dlabs Inc. Ltd. |
John Kessenich | bd0747d | 2013-02-17 06:01:50 +0000 | [diff] [blame] | 3 | //Copyright (C) 2013 LunarG, Inc. |
| 4 | // |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 5 | //All rights reserved. |
| 6 | // |
| 7 | //Redistribution and use in source and binary forms, with or without |
| 8 | //modification, are permitted provided that the following conditions |
| 9 | //are met: |
| 10 | // |
| 11 | // Redistributions of source code must retain the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer. |
| 13 | // |
| 14 | // Redistributions in binary form must reproduce the above |
| 15 | // copyright notice, this list of conditions and the following |
| 16 | // disclaimer in the documentation and/or other materials provided |
| 17 | // with the distribution. |
| 18 | // |
| 19 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 20 | // contributors may be used to endorse or promote products derived |
| 21 | // from this software without specific prior written permission. |
| 22 | // |
| 23 | //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 | //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 | //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 26 | //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 27 | //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 | //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 29 | //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 31 | //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 33 | //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 | //POSSIBILITY OF SUCH DAMAGE. |
| 35 | // |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 36 | |
| 37 | // this only applies to the standalone wrapper, not the front end in general |
| 38 | #define _CRT_SECURE_NO_WARNINGS |
| 39 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 40 | #include "Worklist.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 41 | #include "./../glslang/Include/ShHandle.h" |
| 42 | #include "./../glslang/Public/ShaderLang.h" |
| 43 | #include <string.h> |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 44 | #include <stdlib.h> |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 45 | #include <math.h> |
| 46 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 47 | #include "osinclude.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 48 | |
| 49 | extern "C" { |
| 50 | SH_IMPORT_EXPORT void ShOutputHtml(); |
| 51 | } |
| 52 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 53 | // Command-line options |
| 54 | enum TOptions { |
| 55 | EOptionNone = 0x000, |
| 56 | EOptionIntermediate = 0x001, |
| 57 | EOptionSuppressInfolog = 0x002, |
| 58 | EOptionMemoryLeakMode = 0x004, |
| 59 | EOptionRelaxedErrors = 0x008, |
| 60 | EOptionGiveWarnings = 0x010, |
| 61 | EOptionsLinkProgram = 0x020, |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 62 | EOptionMultiThreaded = 0x040, |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 63 | EOptionDumpConfig = 0x080, |
John Kessenich | 11f9fc7 | 2013-11-07 01:06:34 +0000 | [diff] [blame] | 64 | EOptionDumpReflection = 0x100, |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame^] | 65 | EOptionSuppressWarnings = 0x200, |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 68 | // |
| 69 | // Return codes from main. |
| 70 | // |
| 71 | enum TFailCode { |
| 72 | ESuccess = 0, |
| 73 | EFailUsage, |
| 74 | EFailCompile, |
| 75 | EFailLink, |
| 76 | EFailCompilerCreate, |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 77 | EFailThreadCreate, |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 78 | EFailLinkerCreate |
| 79 | }; |
| 80 | |
| 81 | // |
| 82 | // Just placeholders for testing purposes. The stand-alone environment |
| 83 | // can't actually do a full link without something specifying real |
| 84 | // attribute bindings. |
| 85 | // |
| 86 | ShBinding FixedAttributeBindings[] = { |
| 87 | { "gl_Vertex", 15 }, |
| 88 | { "gl_Color", 10 }, |
| 89 | { "gl_Normal", 7 }, |
| 90 | }; |
| 91 | |
| 92 | ShBindingTable FixedAttributeTable = { 3, FixedAttributeBindings }; |
| 93 | |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 94 | EShLanguage FindLanguage(const std::string& name); |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame^] | 95 | bool CompileFile(const char *fileName, ShHandle); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 96 | void usage(); |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 97 | void FreeFileData(char** data); |
| 98 | char** ReadFileData(const char* fileName); |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 99 | void InfoLogMsg(const char* msg, const char* name, const int num); |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 100 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 101 | // Use to test breaking up a single shader file into multiple strings. |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 102 | int NumShaderStrings; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 103 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 104 | TBuiltInResource Resources; |
| 105 | std::string ConfigFile; |
| 106 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 107 | // |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 108 | // These are the default resources for TBuiltInResources, used for both |
| 109 | // - parsing this string for the case where the user didn't supply one |
| 110 | // - dumping out a template for user construction of a config file |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 111 | // |
John Kessenich | 284231c | 2013-10-22 01:50:39 +0000 | [diff] [blame] | 112 | const char* DefaultConfig = |
| 113 | "MaxLights 32\n" |
| 114 | "MaxClipPlanes 6\n" |
| 115 | "MaxTextureUnits 32\n" |
| 116 | "MaxTextureCoords 32\n" |
| 117 | "MaxVertexAttribs 64\n" |
| 118 | "MaxVertexUniformComponents 4096\n" |
| 119 | "MaxVaryingFloats 64\n" |
| 120 | "MaxVertexTextureImageUnits 32\n" |
| 121 | "MaxCombinedTextureImageUnits 32\n" |
| 122 | "MaxTextureImageUnits 32\n" |
| 123 | "MaxFragmentUniformComponents 4096\n" |
| 124 | "MaxDrawBuffers 32\n" |
| 125 | "MaxVertexUniformVectors 128\n" |
| 126 | "MaxVaryingVectors 8\n" |
| 127 | "MaxFragmentUniformVectors 16\n" |
| 128 | "MaxVertexOutputVectors 16\n" |
| 129 | "MaxFragmentInputVectors 15\n" |
| 130 | "MinProgramTexelOffset -8\n" |
| 131 | "MaxProgramTexelOffset 7\n" |
| 132 | "MaxClipDistances 8\n" |
| 133 | "MaxComputeWorkGroupCountX 65535\n" |
| 134 | "MaxComputeWorkGroupCountY 65535\n" |
| 135 | "MaxComputeWorkGroupCountZ 65535\n" |
| 136 | "MaxComputeWorkGroupSizeX 1024\n" |
| 137 | "MaxComputeWorkGroupSizeX 1024\n" |
| 138 | "MaxComputeWorkGroupSizeZ 64\n" |
| 139 | "MaxComputeUniformComponents 1024\n" |
| 140 | "MaxComputeTextureImageUnits 16\n" |
| 141 | "MaxComputeImageUniforms 8\n" |
| 142 | "MaxComputeAtomicCounters 8\n" |
| 143 | "MaxComputeAtomicCounterBuffers 1\n" |
| 144 | "MaxVaryingComponents 60\n" |
| 145 | "MaxVertexOutputComponents 64\n" |
| 146 | "MaxGeometryInputComponents 64\n" |
| 147 | "MaxGeometryOutputComponents 128\n" |
| 148 | "MaxFragmentInputComponents 128\n" |
| 149 | "MaxImageUnits 8\n" |
| 150 | "MaxCombinedImageUnitsAndFragmentOutputs 8\n" |
| 151 | "MaxImageSamples 0\n" |
| 152 | "MaxVertexImageUniforms 0\n" |
| 153 | "MaxTessControlImageUniforms 0\n" |
| 154 | "MaxTessEvaluationImageUniforms 0\n" |
| 155 | "MaxGeometryImageUniforms 0\n" |
| 156 | "MaxFragmentImageUniforms 8\n" |
| 157 | "MaxCombinedImageUniforms 8\n" |
| 158 | "MaxGeometryTextureImageUnits 16\n" |
| 159 | "MaxGeometryOutputVertices 256\n" |
| 160 | "MaxGeometryTotalOutputComponents 1024\n" |
| 161 | "MaxGeometryUniformComponents 1024\n" |
| 162 | "MaxGeometryVaryingComponents 64\n" |
| 163 | "MaxTessControlInputComponents 128\n" |
| 164 | "MaxTessControlOutputComponents 128\n" |
| 165 | "MaxTessControlTextureImageUnits 16\n" |
| 166 | "MaxTessControlUniformComponents 1024\n" |
| 167 | "MaxTessControlTotalOutputComponents 4096\n" |
| 168 | "MaxTessEvaluationInputComponents 128\n" |
| 169 | "MaxTessEvaluationOutputComponents 128\n" |
| 170 | "MaxTessEvaluationTextureImageUnits 16\n" |
| 171 | "MaxTessEvaluationUniformComponents 1024\n" |
| 172 | "MaxTessPatchComponents 120\n" |
| 173 | "MaxPatchVertices 32\n" |
| 174 | "MaxTessGenLevel 64\n" |
| 175 | "MaxViewports 16\n" |
| 176 | "MaxVertexAtomicCounters 0\n" |
| 177 | "MaxTessControlAtomicCounters 0\n" |
| 178 | "MaxTessEvaluationAtomicCounters 0\n" |
| 179 | "MaxGeometryAtomicCounters 0\n" |
| 180 | "MaxFragmentAtomicCounters 8\n" |
| 181 | "MaxCombinedAtomicCounters 8\n" |
| 182 | "MaxAtomicCounterBindings 1\n" |
| 183 | "MaxVertexAtomicCounterBuffers 0\n" |
| 184 | "MaxTessControlAtomicCounterBuffers 0\n" |
| 185 | "MaxTessEvaluationAtomicCounterBuffers 0\n" |
| 186 | "MaxGeometryAtomicCounterBuffers 0\n" |
| 187 | "MaxFragmentAtomicCounterBuffers 1\n" |
| 188 | "MaxCombinedAtomicCounterBuffers 1\n" |
| 189 | "MaxAtomicCounterBufferSize 16384\n" |
| 190 | |
| 191 | "nonInductiveForLoops 1\n" |
| 192 | "whileLoops 1\n" |
| 193 | "doWhileLoops 1\n" |
| 194 | "generalUniformIndexing 1\n" |
| 195 | "generalAttributeMatrixVectorIndexing 1\n" |
| 196 | "generalVaryingIndexing 1\n" |
| 197 | "generalSamplerIndexing 1\n" |
| 198 | "generalVariableIndexing 1\n" |
| 199 | "generalConstantMatrixVectorIndexing 1\n" |
| 200 | ; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 201 | |
| 202 | // |
| 203 | // Parse either a .conf file provided by the user or the default string above. |
| 204 | // |
| 205 | void ProcessConfigFile() |
John Kessenich | b51f62c | 2013-04-11 16:31:09 +0000 | [diff] [blame] | 206 | { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 207 | char** configStrings = 0; |
| 208 | char *config = 0; |
| 209 | if (ConfigFile.size() > 0) { |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 210 | configStrings = ReadFileData(ConfigFile.c_str()); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 211 | if (configStrings) |
| 212 | config = *configStrings; |
| 213 | else { |
| 214 | printf("Error opening configuration file; will instead use the default configuration\n"); |
| 215 | usage(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (config == 0) { |
| 220 | config = new char[strlen(DefaultConfig)]; |
| 221 | strcpy(config, DefaultConfig); |
| 222 | } |
| 223 | |
| 224 | const char* delims = " \t\n\r"; |
| 225 | const char* token = strtok(config, delims); |
| 226 | while (token) { |
| 227 | const char* valueStr = strtok(0, delims); |
| 228 | if (valueStr == 0 || ! (valueStr[0] == '-' || (valueStr[0] >= '0' && valueStr[0] <= '9'))) { |
| 229 | printf("Error: '%s' bad .conf file. Each name must be followed by one number.\n", valueStr ? valueStr : ""); |
| 230 | return; |
| 231 | } |
| 232 | int value = atoi(valueStr); |
| 233 | |
| 234 | if (strcmp(token, "MaxLights") == 0) |
| 235 | Resources.maxLights = value; |
| 236 | else if (strcmp(token, "MaxClipPlanes") == 0) |
| 237 | Resources.maxClipPlanes = value; |
| 238 | else if (strcmp(token, "MaxTextureUnits") == 0) |
| 239 | Resources.maxTextureUnits = value; |
| 240 | else if (strcmp(token, "MaxTextureCoords") == 0) |
| 241 | Resources.maxTextureCoords = value; |
| 242 | else if (strcmp(token, "MaxVertexAttribs") == 0) |
| 243 | Resources.maxVertexAttribs = value; |
| 244 | else if (strcmp(token, "MaxVertexUniformComponents") == 0) |
| 245 | Resources.maxVertexUniformComponents = value; |
| 246 | else if (strcmp(token, "MaxVaryingFloats") == 0) |
| 247 | Resources.maxVaryingFloats = value; |
| 248 | else if (strcmp(token, "MaxVertexTextureImageUnits") == 0) |
| 249 | Resources.maxVertexTextureImageUnits = value; |
| 250 | else if (strcmp(token, "MaxCombinedTextureImageUnits") == 0) |
| 251 | Resources.maxCombinedTextureImageUnits = value; |
| 252 | else if (strcmp(token, "MaxTextureImageUnits") == 0) |
| 253 | Resources.maxTextureImageUnits = value; |
| 254 | else if (strcmp(token, "MaxFragmentUniformComponents") == 0) |
| 255 | Resources.maxFragmentUniformComponents = value; |
| 256 | else if (strcmp(token, "MaxDrawBuffers") == 0) |
| 257 | Resources.maxDrawBuffers = value; |
| 258 | else if (strcmp(token, "MaxVertexUniformVectors") == 0) |
| 259 | Resources.maxVertexUniformVectors = value; |
| 260 | else if (strcmp(token, "MaxVaryingVectors") == 0) |
| 261 | Resources.maxVaryingVectors = value; |
| 262 | else if (strcmp(token, "MaxFragmentUniformVectors") == 0) |
| 263 | Resources.maxFragmentUniformVectors = value; |
| 264 | else if (strcmp(token, "MaxVertexOutputVectors") == 0) |
| 265 | Resources.maxVertexOutputVectors = value; |
| 266 | else if (strcmp(token, "MaxFragmentInputVectors") == 0) |
| 267 | Resources.maxFragmentInputVectors = value; |
| 268 | else if (strcmp(token, "MinProgramTexelOffset") == 0) |
| 269 | Resources.minProgramTexelOffset = value; |
| 270 | else if (strcmp(token, "MaxProgramTexelOffset") == 0) |
| 271 | Resources.maxProgramTexelOffset = value; |
John Kessenich | e7c59c1 | 2013-10-16 22:28:35 +0000 | [diff] [blame] | 272 | else if (strcmp(token, "MaxClipDistances") == 0) |
| 273 | Resources.maxClipDistances = value; |
John Kessenich | 284231c | 2013-10-22 01:50:39 +0000 | [diff] [blame] | 274 | else if (strcmp(token, "MaxComputeWorkGroupCountX") == 0) |
| 275 | Resources.maxComputeWorkGroupCountX = value; |
| 276 | else if (strcmp(token, "MaxComputeWorkGroupCountY") == 0) |
| 277 | Resources.maxComputeWorkGroupCountY = value; |
| 278 | else if (strcmp(token, "MaxComputeWorkGroupCountZ") == 0) |
| 279 | Resources.maxComputeWorkGroupCountZ = value; |
| 280 | else if (strcmp(token, "MaxComputeWorkGroupSizeX") == 0) |
| 281 | Resources.maxComputeWorkGroupSizeX = value; |
| 282 | else if (strcmp(token, "MaxComputeWorkGroupSizeY") == 0) |
| 283 | Resources.maxComputeWorkGroupSizeY = value; |
| 284 | else if (strcmp(token, "MaxComputeWorkGroupSizeZ") == 0) |
| 285 | Resources.maxComputeWorkGroupSizeZ = value; |
| 286 | else if (strcmp(token, "MaxComputeUniformComponents") == 0) |
| 287 | Resources.maxComputeUniformComponents = value; |
| 288 | else if (strcmp(token, "MaxComputeTextureImageUnits") == 0) |
| 289 | Resources.maxComputeTextureImageUnits = value; |
| 290 | else if (strcmp(token, "MaxComputeImageUniforms") == 0) |
| 291 | Resources.maxComputeImageUniforms = value; |
| 292 | else if (strcmp(token, "MaxComputeAtomicCounters") == 0) |
| 293 | Resources.maxComputeAtomicCounters = value; |
| 294 | else if (strcmp(token, "MaxComputeAtomicCounterBuffers") == 0) |
| 295 | Resources.maxComputeAtomicCounterBuffers = value; |
| 296 | else if (strcmp(token, "MaxVaryingComponents") == 0) |
| 297 | Resources.maxVaryingComponents = value; |
| 298 | else if (strcmp(token, "MaxVertexOutputComponents") == 0) |
| 299 | Resources.maxVertexOutputComponents = value; |
| 300 | else if (strcmp(token, "MaxGeometryInputComponents") == 0) |
| 301 | Resources.maxGeometryInputComponents = value; |
| 302 | else if (strcmp(token, "MaxGeometryOutputComponents") == 0) |
| 303 | Resources.maxGeometryOutputComponents = value; |
| 304 | else if (strcmp(token, "MaxFragmentInputComponents") == 0) |
| 305 | Resources.maxFragmentInputComponents = value; |
| 306 | else if (strcmp(token, "MaxImageUnits") == 0) |
| 307 | Resources.maxImageUnits = value; |
| 308 | else if (strcmp(token, "MaxCombinedImageUnitsAndFragmentOutputs") == 0) |
| 309 | Resources.maxCombinedImageUnitsAndFragmentOutputs = value; |
| 310 | else if (strcmp(token, "MaxImageSamples") == 0) |
| 311 | Resources.maxImageSamples = value; |
| 312 | else if (strcmp(token, "MaxVertexImageUniforms") == 0) |
| 313 | Resources.maxVertexImageUniforms = value; |
| 314 | else if (strcmp(token, "MaxTessControlImageUniforms") == 0) |
| 315 | Resources.maxTessControlImageUniforms = value; |
| 316 | else if (strcmp(token, "MaxTessEvaluationImageUniforms") == 0) |
| 317 | Resources.maxTessEvaluationImageUniforms = value; |
| 318 | else if (strcmp(token, "MaxGeometryImageUniforms") == 0) |
| 319 | Resources.maxGeometryImageUniforms = value; |
| 320 | else if (strcmp(token, "MaxFragmentImageUniforms") == 0) |
| 321 | Resources.maxFragmentImageUniforms = value; |
| 322 | else if (strcmp(token, "MaxCombinedImageUniforms") == 0) |
| 323 | Resources.maxCombinedImageUniforms = value; |
| 324 | else if (strcmp(token, "MaxGeometryTextureImageUnits") == 0) |
| 325 | Resources.maxGeometryTextureImageUnits = value; |
| 326 | else if (strcmp(token, "MaxGeometryOutputVertices") == 0) |
| 327 | Resources.maxGeometryOutputVertices = value; |
| 328 | else if (strcmp(token, "MaxGeometryTotalOutputComponents") == 0) |
| 329 | Resources.maxGeometryTotalOutputComponents = value; |
| 330 | else if (strcmp(token, "MaxGeometryUniformComponents") == 0) |
| 331 | Resources.maxGeometryUniformComponents = value; |
| 332 | else if (strcmp(token, "MaxGeometryVaryingComponents") == 0) |
| 333 | Resources.maxGeometryVaryingComponents = value; |
| 334 | else if (strcmp(token, "MaxTessControlInputComponents") == 0) |
| 335 | Resources.maxTessControlInputComponents = value; |
| 336 | else if (strcmp(token, "MaxTessControlOutputComponents") == 0) |
| 337 | Resources.maxTessControlOutputComponents = value; |
| 338 | else if (strcmp(token, "MaxTessControlTextureImageUnits") == 0) |
| 339 | Resources.maxTessControlTextureImageUnits = value; |
| 340 | else if (strcmp(token, "MaxTessControlUniformComponents") == 0) |
| 341 | Resources.maxTessControlUniformComponents = value; |
| 342 | else if (strcmp(token, "MaxTessControlTotalOutputComponents") == 0) |
| 343 | Resources.maxTessControlTotalOutputComponents = value; |
| 344 | else if (strcmp(token, "MaxTessEvaluationInputComponents") == 0) |
| 345 | Resources.maxTessEvaluationInputComponents = value; |
| 346 | else if (strcmp(token, "MaxTessEvaluationOutputComponents") == 0) |
| 347 | Resources.maxTessEvaluationOutputComponents = value; |
| 348 | else if (strcmp(token, "MaxTessEvaluationTextureImageUnits") == 0) |
| 349 | Resources.maxTessEvaluationTextureImageUnits = value; |
| 350 | else if (strcmp(token, "MaxTessEvaluationUniformComponents") == 0) |
| 351 | Resources.maxTessEvaluationUniformComponents = value; |
| 352 | else if (strcmp(token, "MaxTessPatchComponents") == 0) |
| 353 | Resources.maxTessPatchComponents = value; |
| 354 | else if (strcmp(token, "MaxPatchVertices") == 0) |
| 355 | Resources.maxPatchVertices = value; |
| 356 | else if (strcmp(token, "MaxTessGenLevel") == 0) |
| 357 | Resources.maxTessGenLevel = value; |
| 358 | else if (strcmp(token, "MaxViewports") == 0) |
| 359 | Resources.maxViewports = value; |
| 360 | else if (strcmp(token, "MaxVertexAtomicCounters") == 0) |
| 361 | Resources.maxVertexAtomicCounters = value; |
| 362 | else if (strcmp(token, "MaxTessControlAtomicCounters") == 0) |
| 363 | Resources.maxTessControlAtomicCounters = value; |
| 364 | else if (strcmp(token, "MaxTessEvaluationAtomicCounters") == 0) |
| 365 | Resources.maxTessEvaluationAtomicCounters = value; |
| 366 | else if (strcmp(token, "MaxGeometryAtomicCounters") == 0) |
| 367 | Resources.maxGeometryAtomicCounters = value; |
| 368 | else if (strcmp(token, "MaxFragmentAtomicCounters") == 0) |
| 369 | Resources.maxFragmentAtomicCounters = value; |
| 370 | else if (strcmp(token, "MaxCombinedAtomicCounters") == 0) |
| 371 | Resources.maxCombinedAtomicCounters = value; |
| 372 | else if (strcmp(token, "MaxAtomicCounterBindings") == 0) |
| 373 | Resources.maxAtomicCounterBindings = value; |
| 374 | else if (strcmp(token, "MaxVertexAtomicCounterBuffers") == 0) |
| 375 | Resources.maxVertexAtomicCounterBuffers = value; |
| 376 | else if (strcmp(token, "MaxTessControlAtomicCounterBuffers") == 0) |
| 377 | Resources.maxTessControlAtomicCounterBuffers = value; |
| 378 | else if (strcmp(token, "MaxTessEvaluationAtomicCounterBuffers") == 0) |
| 379 | Resources.maxTessEvaluationAtomicCounterBuffers = value; |
| 380 | else if (strcmp(token, "MaxGeometryAtomicCounterBuffers") == 0) |
| 381 | Resources.maxGeometryAtomicCounterBuffers = value; |
| 382 | else if (strcmp(token, "MaxFragmentAtomicCounterBuffers") == 0) |
| 383 | Resources.maxFragmentAtomicCounterBuffers = value; |
| 384 | else if (strcmp(token, "MaxCombinedAtomicCounterBuffers") == 0) |
| 385 | Resources.maxCombinedAtomicCounterBuffers = value; |
| 386 | else if (strcmp(token, "MaxAtomicCounterBufferSize") == 0) |
| 387 | Resources.maxAtomicCounterBufferSize = value; |
| 388 | |
John Kessenich | a5830df | 2013-10-02 05:10:48 +0000 | [diff] [blame] | 389 | else if (strcmp(token, "nonInductiveForLoops") == 0) |
| 390 | Resources.limits.nonInductiveForLoops = (value != 0); |
| 391 | else if (strcmp(token, "whileLoops") == 0) |
| 392 | Resources.limits.whileLoops = (value != 0); |
| 393 | else if (strcmp(token, "doWhileLoops") == 0) |
| 394 | Resources.limits.doWhileLoops = (value != 0); |
| 395 | else if (strcmp(token, "generalUniformIndexing") == 0) |
| 396 | Resources.limits.generalUniformIndexing = (value != 0); |
| 397 | else if (strcmp(token, "generalAttributeMatrixVectorIndexing") == 0) |
| 398 | Resources.limits.generalAttributeMatrixVectorIndexing = (value != 0); |
| 399 | else if (strcmp(token, "generalVaryingIndexing") == 0) |
| 400 | Resources.limits.generalVaryingIndexing = (value != 0); |
| 401 | else if (strcmp(token, "generalSamplerIndexing") == 0) |
| 402 | Resources.limits.generalSamplerIndexing = (value != 0); |
| 403 | else if (strcmp(token, "generalVariableIndexing") == 0) |
| 404 | Resources.limits.generalVariableIndexing = (value != 0); |
| 405 | else if (strcmp(token, "generalConstantMatrixVectorIndexing") == 0) |
| 406 | Resources.limits.generalConstantMatrixVectorIndexing = (value != 0); |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 407 | else |
| 408 | printf("Warning: unrecognized limit (%s) in configuration file.\n", token); |
| 409 | |
| 410 | token = strtok(0, delims); |
| 411 | } |
| 412 | if (configStrings) |
| 413 | FreeFileData(configStrings); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 414 | } |
| 415 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 416 | // thread-safe list of shaders to asynchronously grab and compile |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 417 | glslang::TWorklist Worklist; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 418 | |
| 419 | // array of unique places to leave the shader names and infologs for the asynchronous compiles |
| 420 | glslang::TWorkItem **Work = 0; |
| 421 | int NumWorkItems = 0; |
| 422 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 423 | int Options = 0; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 424 | bool Delay = false; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 425 | const char* ExecutableName; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 426 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 427 | // |
| 428 | // *.conf => this is a config file that can set limits/resources |
| 429 | // |
| 430 | bool SetConfigFile(const std::string& name) |
| 431 | { |
| 432 | if (name.size() < 5) |
| 433 | return false; |
| 434 | |
John Kessenich | 4c70685 | 2013-10-11 16:28:43 +0000 | [diff] [blame] | 435 | if (name.compare(name.size() - 5, 5, ".conf") == 0) { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 436 | ConfigFile = name; |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | return false; |
| 441 | } |
| 442 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 443 | bool ProcessArguments(int argc, char* argv[]) |
| 444 | { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 445 | ExecutableName = argv[0]; |
| 446 | NumWorkItems = argc; // will include some empties where the '-' options were, but it doesn't matter, they'll be 0 |
| 447 | Work = new glslang::TWorkItem*[NumWorkItems]; |
| 448 | Work[0] = 0; |
| 449 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 450 | argc--; |
| 451 | argv++; |
| 452 | for (; argc >= 1; argc--, argv++) { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 453 | Work[argc] = 0; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 454 | if (argv[0][0] == '-') { |
| 455 | switch (argv[0][1]) { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 456 | case 'c': |
| 457 | Options |= EOptionDumpConfig; |
| 458 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 459 | case 'd': |
| 460 | Delay = true; |
| 461 | break; |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 462 | case 'i': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 463 | Options |= EOptionIntermediate; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 464 | break; |
| 465 | case 'l': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 466 | Options |= EOptionsLinkProgram; |
| 467 | break; |
| 468 | case 'm': |
| 469 | Options |= EOptionMemoryLeakMode; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 470 | break; |
John Kessenich | 11f9fc7 | 2013-11-07 01:06:34 +0000 | [diff] [blame] | 471 | case 'q': |
| 472 | Options |= EOptionDumpReflection; |
| 473 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 474 | case 'r': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 475 | Options |= EOptionRelaxedErrors; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 476 | break; |
| 477 | case 's': |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 478 | Options |= EOptionSuppressInfolog; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 479 | break; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 480 | case 't': |
| 481 | #ifdef _WIN32 |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame^] | 482 | Options |= EOptionMultiThreaded; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 483 | #endif |
| 484 | break; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame^] | 485 | case 'w': |
| 486 | Options |= EOptionSuppressWarnings; |
| 487 | break; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 488 | default: |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 489 | return false; |
| 490 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 491 | } else { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 492 | std::string name(argv[0]); |
| 493 | if (! SetConfigFile(name)) { |
| 494 | Work[argc] = new glslang::TWorkItem(name); |
| 495 | Worklist.add(Work[argc]); |
| 496 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 497 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | return true; |
| 501 | } |
| 502 | |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame^] | 503 | void SetMessageOptions(EShMessages& messages) |
| 504 | { |
| 505 | if (Options & EOptionRelaxedErrors) |
| 506 | messages = (EShMessages)(messages | EShMsgRelaxedErrors); |
| 507 | if (Options & EOptionIntermediate) |
| 508 | messages = (EShMessages)(messages | EShMsgAST); |
| 509 | if (Options & EOptionSuppressWarnings) |
| 510 | messages = (EShMessages)(messages | EShMsgSuppressWarnings); |
| 511 | } |
| 512 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 513 | // Thread entry point, for non-linking asynchronous mode. |
John Kessenich | ee6a9c8 | 2013-07-31 23:19:17 +0000 | [diff] [blame] | 514 | unsigned int |
| 515 | #ifdef _WIN32 |
| 516 | __stdcall |
| 517 | #endif |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 518 | CompileShaders(void*) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 519 | { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 520 | glslang::TWorkItem* workItem; |
| 521 | while (Worklist.remove(workItem)) { |
| 522 | ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 523 | if (compiler == 0) |
| 524 | return false; |
| 525 | |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame^] | 526 | CompileFile(workItem->name.c_str(), compiler); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 527 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 528 | if (! (Options & EOptionSuppressInfolog)) |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 529 | workItem->results = ShGetInfoLog(compiler); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 530 | |
| 531 | ShDestruct(compiler); |
| 532 | } |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 537 | // |
| 538 | // For linking mode: Will independently parse each item in the worklist, but then put them |
| 539 | // in the same program and link them together. |
| 540 | // |
| 541 | // Uses the new C++ interface instead of the old handle-based interface. |
| 542 | // |
| 543 | void CompileAndLinkShaders() |
| 544 | { |
| 545 | // keep track of what to free |
| 546 | std::list<glslang::TShader*> shaders; |
| 547 | |
| 548 | EShMessages messages = EShMsgDefault; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame^] | 549 | SetMessageOptions(messages); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 550 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 551 | // |
| 552 | // Per-shader processing... |
| 553 | // |
| 554 | |
John Kessenich | 5b0f13a | 2013-11-01 03:08:40 +0000 | [diff] [blame] | 555 | glslang::TProgram& program = *new glslang::TProgram; |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 556 | glslang::TWorkItem* workItem; |
| 557 | while (Worklist.remove(workItem)) { |
| 558 | EShLanguage stage = FindLanguage(workItem->name); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 559 | glslang::TShader* shader = new glslang::TShader(stage); |
| 560 | shaders.push_back(shader); |
| 561 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 562 | char** shaderStrings = ReadFileData(workItem->name.c_str()); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 563 | if (! shaderStrings) { |
| 564 | usage(); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | shader->setStrings(shaderStrings, 1); |
| 569 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 570 | shader->parse(&Resources, 100, false, messages); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 571 | |
| 572 | program.addShader(shader); |
| 573 | |
| 574 | if (! (Options & EOptionSuppressInfolog)) { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 575 | puts(workItem->name.c_str()); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 576 | puts(shader->getInfoLog()); |
| 577 | puts(shader->getInfoDebugLog()); |
| 578 | } |
| 579 | |
| 580 | FreeFileData(shaderStrings); |
| 581 | } |
| 582 | |
| 583 | // |
| 584 | // Program-level processing... |
| 585 | // |
| 586 | |
| 587 | program.link(messages); |
| 588 | if (! (Options & EOptionSuppressInfolog)) { |
| 589 | puts(program.getInfoLog()); |
| 590 | puts(program.getInfoDebugLog()); |
| 591 | } |
| 592 | |
John Kessenich | 11f9fc7 | 2013-11-07 01:06:34 +0000 | [diff] [blame] | 593 | if (Options & EOptionDumpReflection) { |
| 594 | program.buildReflection(); |
| 595 | program.dumpReflection(); |
| 596 | } |
| 597 | |
John Kessenich | 5b0f13a | 2013-11-01 03:08:40 +0000 | [diff] [blame] | 598 | // Free everything up, program has to go before the shaders |
| 599 | // because it might have merged stuff from the shaders, and |
| 600 | // the stuff from the shaders has to have its destructors called |
| 601 | // before the pools holding the memory in the shaders is freed. |
| 602 | delete &program; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 603 | while (shaders.size() > 0) { |
| 604 | delete shaders.back(); |
| 605 | shaders.pop_back(); |
| 606 | } |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 607 | } |
| 608 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 609 | int C_DECL main(int argc, char* argv[]) |
| 610 | { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 611 | bool compileFailed = false; |
| 612 | bool linkFailed = false; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 613 | |
John Kessenich | 54f6e56 | 2013-08-03 00:04:10 +0000 | [diff] [blame] | 614 | if (! ProcessArguments(argc, argv)) { |
| 615 | usage(); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 616 | return EFailUsage; |
John Kessenich | 54f6e56 | 2013-08-03 00:04:10 +0000 | [diff] [blame] | 617 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 618 | |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 619 | if (Options & EOptionDumpConfig) { |
| 620 | printf("%s", DefaultConfig); |
| 621 | if (Worklist.empty()) |
| 622 | return ESuccess; |
| 623 | } |
| 624 | |
| 625 | if (Worklist.empty()) { |
| 626 | usage(); |
| 627 | return EFailUsage; |
| 628 | } |
| 629 | |
| 630 | ProcessConfigFile(); |
| 631 | |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 632 | // |
| 633 | // Two modes: |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 634 | // 1) linking all arguments together, single-threaded, new C++ interface |
| 635 | // 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] | 636 | // |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 637 | if (Options & EOptionsLinkProgram) { |
| 638 | glslang::InitializeProcess(); |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 639 | CompileAndLinkShaders(); |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 640 | glslang::FinalizeProcess(); |
| 641 | } else { |
| 642 | ShInitialize(); |
| 643 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 644 | bool printShaderNames = Worklist.size() > 1; |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 645 | |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 646 | if (Options & EOptionMultiThreaded) { |
| 647 | const int NumThreads = 16; |
| 648 | void* threads[NumThreads]; |
| 649 | for (int t = 0; t < NumThreads; ++t) { |
| 650 | threads[t] = glslang::OS_CreateThread(&CompileShaders); |
| 651 | if (! threads[t]) { |
| 652 | printf("Failed to create thread\n"); |
| 653 | return EFailThreadCreate; |
| 654 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 655 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 656 | glslang::OS_WaitForAllThreads(threads, NumThreads); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 657 | } else { |
| 658 | if (! CompileShaders(0)) |
| 659 | compileFailed = true; |
| 660 | } |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 661 | |
| 662 | // Print out all the resulting infologs |
| 663 | for (int w = 0; w < NumWorkItems; ++w) { |
| 664 | if (Work[w]) { |
| 665 | if (printShaderNames) |
| 666 | puts(Work[w]->name.c_str()); |
| 667 | puts(Work[w]->results.c_str()); |
| 668 | delete Work[w]; |
| 669 | } |
| 670 | } |
John Kessenich | c36e1d8 | 2013-11-01 17:41:52 +0000 | [diff] [blame] | 671 | |
| 672 | ShFinalize(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 673 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 674 | |
| 675 | if (Delay) |
| 676 | glslang::OS_Sleep(1000000); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 677 | |
| 678 | if (compileFailed) |
| 679 | return EFailCompile; |
| 680 | if (linkFailed) |
| 681 | return EFailLink; |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | // |
| 687 | // Deduce the language from the filename. Files must end in one of the |
| 688 | // following extensions: |
| 689 | // |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 690 | // .vert = vertex |
| 691 | // .tesc = tessellation control |
| 692 | // .tese = tessellation evaluation |
| 693 | // .geom = geometry |
| 694 | // .frag = fragment |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 695 | // .comp = compute |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 696 | // |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 697 | EShLanguage FindLanguage(const std::string& name) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 698 | { |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 699 | size_t ext = name.rfind('.'); |
| 700 | if (ext == std::string::npos) { |
| 701 | usage(); |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 702 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 703 | } |
| 704 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 705 | std::string suffix = name.substr(ext + 1, std::string::npos); |
| 706 | if (suffix == "vert") |
| 707 | return EShLangVertex; |
| 708 | else if (suffix == "tesc") |
| 709 | return EShLangTessControl; |
| 710 | else if (suffix == "tese") |
| 711 | return EShLangTessEvaluation; |
| 712 | else if (suffix == "geom") |
| 713 | return EShLangGeometry; |
| 714 | else if (suffix == "frag") |
| 715 | return EShLangFragment; |
John Kessenich | c027579 | 2013-08-09 17:14:49 +0000 | [diff] [blame] | 716 | else if (suffix == "comp") |
| 717 | return EShLangCompute; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 718 | |
| 719 | usage(); |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 720 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 721 | } |
| 722 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 723 | // |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 724 | // Read a file's data into a string, and compile it using the old interface ShCompile, |
| 725 | // for non-linkable results. |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 726 | // |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame^] | 727 | bool CompileFile(const char *fileName, ShHandle compiler) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 728 | { |
| 729 | int ret; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 730 | char** shaderStrings = ReadFileData(fileName); |
John Kessenich | db4cd54 | 2013-06-26 22:42:55 +0000 | [diff] [blame] | 731 | if (! shaderStrings) { |
| 732 | usage(); |
| 733 | return false; |
| 734 | } |
| 735 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 736 | int* lengths = new int[NumShaderStrings]; |
| 737 | |
| 738 | // move to length-based strings, rather than null-terminated strings |
| 739 | for (int s = 0; s < NumShaderStrings; ++s) |
| 740 | lengths[s] = strlen(shaderStrings[s]); |
John Kessenich | 09da79e | 2013-04-17 19:34:23 +0000 | [diff] [blame] | 741 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 742 | if (! shaderStrings) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 743 | return false; |
| 744 | |
John Kessenich | 52ac67e | 2013-05-05 23:46:22 +0000 | [diff] [blame] | 745 | EShMessages messages = EShMsgDefault; |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame^] | 746 | SetMessageOptions(messages); |
John Kessenich | 69f4b51 | 2013-09-04 21:19:27 +0000 | [diff] [blame] | 747 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 748 | for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) { |
| 749 | for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) { |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 750 | //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, 100, false, messages); |
| 751 | ret = ShCompile(compiler, shaderStrings, NumShaderStrings, 0, EShOptNone, &Resources, Options, 100, false, messages); |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 752 | //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err", |
| 753 | // "or should be l", "ine 1", "string 5\n", "float glo", "bal", |
| 754 | // ";\n#error should be line 2\n void main() {", "global = 2.3;}" }; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 755 | //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" }; |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 756 | //ret = ShCompile(compiler, multi, 7, 0, EShOptNone, &Resources, Options, 100, false, messages); |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 757 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 758 | |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 759 | if (Options & EOptionMemoryLeakMode) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 760 | glslang::OS_DumpMemoryCounters(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 761 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 762 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 763 | delete [] lengths; |
| 764 | FreeFileData(shaderStrings); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 765 | |
| 766 | return ret ? true : false; |
| 767 | } |
| 768 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 769 | // |
| 770 | // print usage to stdout |
| 771 | // |
| 772 | void usage() |
| 773 | { |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 774 | printf("Usage: glslangValidator [ options ] filename\n" |
John Kessenich | c027579 | 2013-08-09 17:14:49 +0000 | [diff] [blame] | 775 | "Where: filename is a name ending in\n" |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 776 | " .conf provides an optional config file that replaces the default configuration\n" |
| 777 | " (see -c option below for generating a template)\n" |
John Kessenich | c027579 | 2013-08-09 17:14:49 +0000 | [diff] [blame] | 778 | " .vert for a vertex shader\n" |
| 779 | " .tesc for a tessellation control shader\n" |
| 780 | " .tese for a tessellation evaluation shader\n" |
| 781 | " .geom for a geometry shader\n" |
| 782 | " .frag for a fragment shader\n" |
| 783 | " .comp for a compute shader\n\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 784 | "Compilation warnings and errors will be printed to stdout.\n" |
| 785 | "To get other information, use one of the following options:\n" |
John Kessenich | 05a7063 | 2013-09-17 19:26:08 +0000 | [diff] [blame] | 786 | "-c: configuration dump; use to create default configuration file (redirect to a .conf file)\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 787 | "-i: intermediate tree (glslang AST) is printed out\n" |
John Kessenich | 94a81fb | 2013-08-31 02:41:30 +0000 | [diff] [blame] | 788 | "-l: link validation of all input files\n" |
| 789 | "-m: memory leak mode\n" |
John Kessenich | 11f9fc7 | 2013-11-07 01:06:34 +0000 | [diff] [blame] | 790 | "-q: dump reflection query database\n" |
John Kessenich | 38f3b89 | 2013-09-06 19:52:57 +0000 | [diff] [blame] | 791 | "-r: relaxed semantic error-checking mode\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 792 | "-s: silent mode\n" |
John Kessenich | b0a7eb5 | 2013-11-07 17:44:20 +0000 | [diff] [blame^] | 793 | "-t: multi-threaded mode\n" |
| 794 | "-w: suppress warnings (except as required by #extension : warn)\n" |
| 795 | ); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 796 | } |
| 797 | |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 798 | #ifndef _WIN32 |
| 799 | |
| 800 | #include <errno.h> |
| 801 | |
| 802 | int fopen_s( |
| 803 | FILE** pFile, |
| 804 | const char *filename, |
| 805 | const char *mode |
| 806 | ) |
| 807 | { |
| 808 | if (!pFile || !filename || !mode) { |
| 809 | return EINVAL; |
| 810 | } |
| 811 | |
| 812 | FILE* f = fopen(filename, mode); |
| 813 | if (! f) { |
| 814 | if (errno != 0) { |
| 815 | return errno; |
| 816 | } else { |
| 817 | return ENOENT; |
| 818 | } |
| 819 | } |
| 820 | *pFile = f; |
| 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | #endif |
| 826 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 827 | // |
| 828 | // Malloc a string of sufficient size and read a string into it. |
| 829 | // |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 830 | char** ReadFileData(const char *fileName) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 831 | { |
John Kessenich | 200b273 | 2012-12-12 21:21:23 +0000 | [diff] [blame] | 832 | FILE *in; |
| 833 | int errorCode = fopen_s(&in, fileName, "r"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 834 | char *fdata; |
| 835 | int count = 0; |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 836 | const int maxSourceStrings = 5; |
| 837 | char** return_data = (char**)malloc(maxSourceStrings+1); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 838 | |
| 839 | //return_data[MAX_SOURCE_STRINGS]=NULL; |
John Kessenich | 200b273 | 2012-12-12 21:21:23 +0000 | [diff] [blame] | 840 | if (errorCode) { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 841 | printf("Error: unable to open input file: %s\n", fileName); |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | while (fgetc(in) != EOF) |
| 846 | count++; |
| 847 | |
| 848 | fseek(in, 0, SEEK_SET); |
| 849 | |
| 850 | |
| 851 | if (!(fdata = (char *)malloc(count+2))) { |
| 852 | printf("Error allocating memory\n"); |
| 853 | return 0; |
| 854 | } |
| 855 | if (fread(fdata,1,count, in)!=count) { |
| 856 | printf("Error reading input file: %s\n", fileName); |
| 857 | return 0; |
| 858 | } |
| 859 | fdata[count] = '\0'; |
| 860 | fclose(in); |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 861 | if (count == 0) { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 862 | return_data[0]=(char*)malloc(count+2); |
| 863 | return_data[0][0]='\0'; |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 864 | NumShaderStrings = 0; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 865 | return return_data; |
John Kessenich | ea869fb | 2013-10-28 18:12:06 +0000 | [diff] [blame] | 866 | } else |
| 867 | NumShaderStrings = 1; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 868 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 869 | int len = (int)(ceil)((float)count/(float)NumShaderStrings); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 870 | int ptr_len=0,i=0; |
| 871 | while(count>0){ |
| 872 | return_data[i]=(char*)malloc(len+2); |
| 873 | memcpy(return_data[i],fdata+ptr_len,len); |
| 874 | return_data[i][len]='\0'; |
| 875 | count-=(len); |
| 876 | ptr_len+=(len); |
| 877 | if(count<len){ |
| 878 | if(count==0){ |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 879 | NumShaderStrings=(i+1); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 880 | break; |
| 881 | } |
| 882 | len = count; |
| 883 | } |
| 884 | ++i; |
| 885 | } |
| 886 | return return_data; |
| 887 | } |
| 888 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 889 | void FreeFileData(char **data) |
| 890 | { |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 891 | for(int i=0;i<NumShaderStrings;i++) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 892 | free(data[i]); |
| 893 | } |
| 894 | |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 895 | void InfoLogMsg(const char* msg, const char* name, const int num) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 896 | { |
| 897 | printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" : |
| 898 | "#### %s %s INFO LOG ####\n", msg, name, num); |
| 899 | } |