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 | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 36 | #include "Worklist.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 37 | #include "./../glslang/Include/ShHandle.h" |
| 38 | #include "./../glslang/Public/ShaderLang.h" |
| 39 | #include <string.h> |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 40 | #include <stdlib.h> |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 41 | #include <math.h> |
| 42 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 43 | #include "osinclude.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 44 | |
| 45 | extern "C" { |
| 46 | SH_IMPORT_EXPORT void ShOutputHtml(); |
| 47 | } |
| 48 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 49 | // |
| 50 | // Return codes from main. |
| 51 | // |
| 52 | enum TFailCode { |
| 53 | ESuccess = 0, |
| 54 | EFailUsage, |
| 55 | EFailCompile, |
| 56 | EFailLink, |
| 57 | EFailCompilerCreate, |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 58 | EFailThreadCreate, |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 59 | EFailLinkerCreate |
| 60 | }; |
| 61 | |
| 62 | // |
| 63 | // Just placeholders for testing purposes. The stand-alone environment |
| 64 | // can't actually do a full link without something specifying real |
| 65 | // attribute bindings. |
| 66 | // |
| 67 | ShBinding FixedAttributeBindings[] = { |
| 68 | { "gl_Vertex", 15 }, |
| 69 | { "gl_Color", 10 }, |
| 70 | { "gl_Normal", 7 }, |
| 71 | }; |
| 72 | |
| 73 | ShBindingTable FixedAttributeTable = { 3, FixedAttributeBindings }; |
| 74 | |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 75 | EShLanguage FindLanguage(const std::string& name); |
John Kessenich | 52ac67e | 2013-05-05 23:46:22 +0000 | [diff] [blame] | 76 | bool CompileFile(const char *fileName, ShHandle, int options, const TBuiltInResource*); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 77 | void usage(); |
| 78 | void FreeFileData(char **data); |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 79 | char** ReadFileData(const char *fileName); |
| 80 | void InfoLogMsg(const char* msg, const char* name, const int num); |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 81 | |
| 82 | // Use to test breaking a single shader file into multiple strings. |
| 83 | int NumShaderStrings = 1; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 84 | |
| 85 | // |
| 86 | // Set up the per compile resources |
| 87 | // |
| 88 | void GenerateResources(TBuiltInResource& resources) |
John Kessenich | b51f62c | 2013-04-11 16:31:09 +0000 | [diff] [blame] | 89 | { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 90 | resources.maxLights = 32; |
| 91 | resources.maxClipPlanes = 6; |
| 92 | resources.maxTextureUnits = 32; |
| 93 | resources.maxTextureCoords = 32; |
| 94 | resources.maxVertexAttribs = 64; |
| 95 | resources.maxVertexUniformComponents = 4096; |
| 96 | resources.maxVaryingFloats = 64; |
| 97 | resources.maxVertexTextureImageUnits = 32; |
| 98 | resources.maxCombinedTextureImageUnits = 32; |
| 99 | resources.maxTextureImageUnits = 32; |
| 100 | resources.maxFragmentUniformComponents = 4096; |
| 101 | resources.maxDrawBuffers = 32; |
John Kessenich | bd0747d | 2013-02-17 06:01:50 +0000 | [diff] [blame] | 102 | resources.maxVertexUniformVectors = 128; |
| 103 | resources.maxVaryingVectors = 8; |
| 104 | resources.maxFragmentUniformVectors = 16; |
| 105 | resources.maxVertexOutputVectors = 16; |
John Kessenich | 1f2a36b | 2013-02-20 04:42:42 +0000 | [diff] [blame] | 106 | resources.maxFragmentInputVectors = 15; |
John Kessenich | bd0747d | 2013-02-17 06:01:50 +0000 | [diff] [blame] | 107 | resources.minProgramTexelOffset = -8; |
| 108 | resources.maxProgramTexelOffset = 7; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 109 | } |
| 110 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 111 | glslang::TWorklist Worklist; |
| 112 | int DebugOptions = 0; |
| 113 | bool Delay = false; |
| 114 | |
| 115 | bool ProcessArguments(int argc, char* argv[]) |
| 116 | { |
| 117 | argc--; |
| 118 | argv++; |
| 119 | for (; argc >= 1; argc--, argv++) { |
| 120 | if (argv[0][0] == '-') { |
| 121 | switch (argv[0][1]) { |
| 122 | case 'd': |
| 123 | Delay = true; |
| 124 | break; |
| 125 | case 'i': |
| 126 | DebugOptions |= EDebugOpIntermediate; |
| 127 | break; |
| 128 | case 'l': |
| 129 | DebugOptions |= EDebugOpMemoryLeakMode; |
| 130 | break; |
| 131 | case 'r': |
| 132 | DebugOptions |= EDebugOpRelaxedErrors; |
| 133 | break; |
| 134 | case 's': |
| 135 | DebugOptions |= EDebugOpSuppressInfolog; |
| 136 | break; |
| 137 | case 't': |
| 138 | DebugOptions |= EDebugOpTexturePrototypes; |
| 139 | break; |
| 140 | default: |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | } else |
| 144 | Worklist.add(std::string(argv[0])); |
| 145 | } |
| 146 | |
John Kessenich | 54f6e56 | 2013-08-03 00:04:10 +0000 | [diff] [blame] | 147 | if (Worklist.empty()) |
| 148 | return false; |
| 149 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | |
| 153 | // Thread entry point |
John Kessenich | ee6a9c8 | 2013-07-31 23:19:17 +0000 | [diff] [blame] | 154 | unsigned int |
| 155 | #ifdef _WIN32 |
| 156 | __stdcall |
| 157 | #endif |
| 158 | CompileShaders(void*) |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 159 | { |
| 160 | ShHandle compiler; |
| 161 | |
| 162 | std::string shaderName; |
| 163 | while (Worklist.remove(shaderName)) { |
| 164 | compiler = ShConstructCompiler(FindLanguage(shaderName), DebugOptions); |
| 165 | if (compiler == 0) |
| 166 | return false; |
| 167 | |
| 168 | TBuiltInResource resources; |
| 169 | GenerateResources(resources); |
| 170 | CompileFile(shaderName.c_str(), compiler, DebugOptions, &resources); |
| 171 | |
| 172 | if (! (DebugOptions & EDebugOpSuppressInfolog)) |
| 173 | puts(ShGetInfoLog(compiler)); |
| 174 | |
| 175 | ShDestruct(compiler); |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 181 | int C_DECL main(int argc, char* argv[]) |
| 182 | { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 183 | bool compileFailed = false; |
| 184 | bool linkFailed = false; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 185 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 186 | // Init for front-end proper |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 187 | ShInitialize(); |
| 188 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 189 | // Init for for standalone |
| 190 | glslang::InitGlobalLock(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 191 | |
John Kessenich | 54f6e56 | 2013-08-03 00:04:10 +0000 | [diff] [blame] | 192 | if (! ProcessArguments(argc, argv)) { |
| 193 | usage(); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 194 | return EFailUsage; |
John Kessenich | 54f6e56 | 2013-08-03 00:04:10 +0000 | [diff] [blame] | 195 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 196 | |
| 197 | // TODO: finish threading, allow external control over number of threads |
| 198 | const int NumThreads = 1; |
| 199 | if (NumThreads > 1) { |
| 200 | void* threads[NumThreads]; |
| 201 | for (int t = 0; t < NumThreads; ++t) { |
| 202 | threads[t] = glslang::OS_CreateThread(&CompileShaders); |
| 203 | if (! threads[t]) { |
| 204 | printf("Failed to create thread\n"); |
| 205 | return EFailThreadCreate; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 208 | glslang::OS_WaitForAllThreads(threads, NumThreads); |
| 209 | } else { |
| 210 | if (! CompileShaders(0)) |
| 211 | compileFailed = true; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 212 | } |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 213 | |
| 214 | if (Delay) |
| 215 | glslang::OS_Sleep(1000000); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 216 | |
| 217 | if (compileFailed) |
| 218 | return EFailCompile; |
| 219 | if (linkFailed) |
| 220 | return EFailLink; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | // |
| 226 | // Deduce the language from the filename. Files must end in one of the |
| 227 | // following extensions: |
| 228 | // |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 229 | // .vert = vertex |
| 230 | // .tesc = tessellation control |
| 231 | // .tese = tessellation evaluation |
| 232 | // .geom = geometry |
| 233 | // .frag = fragment |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 234 | // |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 235 | EShLanguage FindLanguage(const std::string& name) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 236 | { |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 237 | size_t ext = name.rfind('.'); |
| 238 | if (ext == std::string::npos) { |
| 239 | usage(); |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 240 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 241 | } |
| 242 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 243 | std::string suffix = name.substr(ext + 1, std::string::npos); |
| 244 | if (suffix == "vert") |
| 245 | return EShLangVertex; |
| 246 | else if (suffix == "tesc") |
| 247 | return EShLangTessControl; |
| 248 | else if (suffix == "tese") |
| 249 | return EShLangTessEvaluation; |
| 250 | else if (suffix == "geom") |
| 251 | return EShLangGeometry; |
| 252 | else if (suffix == "frag") |
| 253 | return EShLangFragment; |
John Kessenich | c027579 | 2013-08-09 17:14:49 +0000 | [diff] [blame] | 254 | else if (suffix == "comp") |
| 255 | return EShLangCompute; |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 256 | |
| 257 | usage(); |
John Kessenich | e95ecc5 | 2012-12-12 21:34:14 +0000 | [diff] [blame] | 258 | return EShLangVertex; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 259 | } |
| 260 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 261 | // |
| 262 | // Read a file's data into a string, and compile it using ShCompile |
| 263 | // |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 264 | bool CompileFile(const char *fileName, ShHandle compiler, int debugOptions, const TBuiltInResource *resources) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 265 | { |
| 266 | int ret; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 267 | char** shaderStrings = ReadFileData(fileName); |
John Kessenich | db4cd54 | 2013-06-26 22:42:55 +0000 | [diff] [blame] | 268 | if (! shaderStrings) { |
| 269 | usage(); |
| 270 | return false; |
| 271 | } |
| 272 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 273 | int* lengths = new int[NumShaderStrings]; |
| 274 | |
| 275 | // move to length-based strings, rather than null-terminated strings |
| 276 | for (int s = 0; s < NumShaderStrings; ++s) |
| 277 | lengths[s] = strlen(shaderStrings[s]); |
John Kessenich | 09da79e | 2013-04-17 19:34:23 +0000 | [diff] [blame] | 278 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 279 | if (! shaderStrings) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 280 | return false; |
| 281 | |
John Kessenich | 52ac67e | 2013-05-05 23:46:22 +0000 | [diff] [blame] | 282 | EShMessages messages = EShMsgDefault; |
| 283 | if (debugOptions & EDebugOpRelaxedErrors) |
| 284 | messages = (EShMessages)(messages | EShMsgRelaxedErrors); |
John Kessenich | 8df53cc | 2013-04-14 19:23:50 +0000 | [diff] [blame] | 285 | for (int i = 0; i < ((debugOptions & EDebugOpMemoryLeakMode) ? 100 : 1); ++i) { |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 286 | for (int j = 0; j < ((debugOptions & EDebugOpMemoryLeakMode) ? 100 : 1); ++j) { |
| 287 | //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, resources, debugOptions, 100, false, messages); |
| 288 | ret = ShCompile(compiler, shaderStrings, NumShaderStrings, 0, EShOptNone, resources, debugOptions, 100, false, messages); |
| 289 | //const char* multi[4] = { "# ve", "rsion", " 300 e", "s" }; |
| 290 | //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" }; |
| 291 | //ret = ShCompile(compiler, multi, 4, 0, EShOptNone, resources, debugOptions, 100, false, messages); |
| 292 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 293 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 294 | if (debugOptions & EDebugOpMemoryLeakMode) |
| 295 | glslang::OS_DumpMemoryCounters(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 296 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 297 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 298 | delete [] lengths; |
| 299 | FreeFileData(shaderStrings); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 300 | |
| 301 | return ret ? true : false; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | // |
| 306 | // print usage to stdout |
| 307 | // |
| 308 | void usage() |
| 309 | { |
John Kessenich | 8df53cc | 2013-04-14 19:23:50 +0000 | [diff] [blame] | 310 | printf("Usage: standalone [ options ] filename\n" |
John Kessenich | c027579 | 2013-08-09 17:14:49 +0000 | [diff] [blame] | 311 | "Where: filename is a name ending in\n" |
| 312 | " .vert for a vertex shader\n" |
| 313 | " .tesc for a tessellation control shader\n" |
| 314 | " .tese for a tessellation evaluation shader\n" |
| 315 | " .geom for a geometry shader\n" |
| 316 | " .frag for a fragment shader\n" |
| 317 | " .comp for a compute shader\n\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 318 | "Compilation warnings and errors will be printed to stdout.\n" |
| 319 | "To get other information, use one of the following options:\n" |
| 320 | "-i: intermediate tree (glslang AST) is printed out\n" |
| 321 | "-d: delay exit\n" |
John Kessenich | 8df53cc | 2013-04-14 19:23:50 +0000 | [diff] [blame] | 322 | "-l: memory leak mode\n" |
John Kessenich | 4586dbd | 2013-08-05 15:52:03 +0000 | [diff] [blame] | 323 | "-s: silent mode\n" |
| 324 | "-r: relaxed semantic error-checking mode\n"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 325 | } |
| 326 | |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 327 | #ifndef _WIN32 |
| 328 | |
| 329 | #include <errno.h> |
| 330 | |
| 331 | int fopen_s( |
| 332 | FILE** pFile, |
| 333 | const char *filename, |
| 334 | const char *mode |
| 335 | ) |
| 336 | { |
| 337 | if (!pFile || !filename || !mode) { |
| 338 | return EINVAL; |
| 339 | } |
| 340 | |
| 341 | FILE* f = fopen(filename, mode); |
| 342 | if (! f) { |
| 343 | if (errno != 0) { |
| 344 | return errno; |
| 345 | } else { |
| 346 | return ENOENT; |
| 347 | } |
| 348 | } |
| 349 | *pFile = f; |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | #endif |
| 355 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 356 | // |
| 357 | // Malloc a string of sufficient size and read a string into it. |
| 358 | // |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 359 | char** ReadFileData(const char *fileName) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 360 | { |
John Kessenich | 200b273 | 2012-12-12 21:21:23 +0000 | [diff] [blame] | 361 | FILE *in; |
| 362 | int errorCode = fopen_s(&in, fileName, "r"); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 363 | char *fdata; |
| 364 | int count = 0; |
John Kessenich | cfd643e | 2013-03-08 23:14:42 +0000 | [diff] [blame] | 365 | const int maxSourceStrings = 5; |
| 366 | char** return_data = (char**)malloc(maxSourceStrings+1); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 367 | |
| 368 | //return_data[MAX_SOURCE_STRINGS]=NULL; |
John Kessenich | 200b273 | 2012-12-12 21:21:23 +0000 | [diff] [blame] | 369 | if (errorCode) { |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 370 | printf("Error: unable to open input file: %s\n", fileName); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | while (fgetc(in) != EOF) |
| 375 | count++; |
| 376 | |
| 377 | fseek(in, 0, SEEK_SET); |
| 378 | |
| 379 | |
| 380 | if (!(fdata = (char *)malloc(count+2))) { |
| 381 | printf("Error allocating memory\n"); |
| 382 | return 0; |
| 383 | } |
| 384 | if (fread(fdata,1,count, in)!=count) { |
| 385 | printf("Error reading input file: %s\n", fileName); |
| 386 | return 0; |
| 387 | } |
| 388 | fdata[count] = '\0'; |
| 389 | fclose(in); |
| 390 | if(count==0){ |
| 391 | return_data[0]=(char*)malloc(count+2); |
| 392 | return_data[0][0]='\0'; |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 393 | NumShaderStrings=0; |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 394 | return return_data; |
| 395 | } |
| 396 | |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 397 | int len = (int)(ceil)((float)count/(float)NumShaderStrings); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 398 | int ptr_len=0,i=0; |
| 399 | while(count>0){ |
| 400 | return_data[i]=(char*)malloc(len+2); |
| 401 | memcpy(return_data[i],fdata+ptr_len,len); |
| 402 | return_data[i][len]='\0'; |
| 403 | count-=(len); |
| 404 | ptr_len+=(len); |
| 405 | if(count<len){ |
| 406 | if(count==0){ |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 407 | NumShaderStrings=(i+1); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 408 | break; |
| 409 | } |
| 410 | len = count; |
| 411 | } |
| 412 | ++i; |
| 413 | } |
| 414 | return return_data; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | |
| 419 | void FreeFileData(char **data) |
| 420 | { |
John Kessenich | 41cf6b5 | 2013-06-25 18:10:05 +0000 | [diff] [blame] | 421 | for(int i=0;i<NumShaderStrings;i++) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 422 | free(data[i]); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | |
John Kessenich | 54d8cda | 2013-02-11 22:36:01 +0000 | [diff] [blame] | 427 | void InfoLogMsg(const char* msg, const char* name, const int num) |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 428 | { |
| 429 | printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" : |
| 430 | "#### %s %s INFO LOG ####\n", msg, name, num); |
| 431 | } |