shaderc.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <string>
  10. #include <vector>
  11. #include "glsl_optimizer.h"
  12. #define MAX_TAGS 256
  13. extern "C" {
  14. # include <fpp.h>
  15. } // extern "C"
  16. #if 0
  17. # define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  18. #endif // DEBUG
  19. #define BX_NAMESPACE 1
  20. #include <bx/bx.h>
  21. #if BX_PLATFORM_LINUX
  22. # define _stricmp strcasecmp
  23. #endif // BX_PLATFORM_LINUX
  24. #include <bx/commandline.h>
  25. #include <bx/countof.h>
  26. #include <bx/endian.h>
  27. #include <bx/uint32_t.h>
  28. #if BX_PLATFORM_WINDOWS
  29. # include <d3dx9.h>
  30. #endif // BX_PLATFORM_WINDOWS
  31. long int fsize(FILE* _file)
  32. {
  33. long int pos = ftell(_file);
  34. fseek(_file, 0L, SEEK_END);
  35. long int size = ftell(_file);
  36. fseek(_file, pos, SEEK_SET);
  37. return size;
  38. }
  39. struct ConstantType
  40. {
  41. enum Enum
  42. {
  43. Uniform1i,
  44. Uniform1f,
  45. End,
  46. Uniform1iv,
  47. Uniform1fv,
  48. Uniform2fv,
  49. Uniform3fv,
  50. Uniform4fv,
  51. Uniform3x3fv,
  52. Uniform4x4fv,
  53. Count,
  54. TypeMask = 0x7f,
  55. FragmentBit = 0x80
  56. };
  57. };
  58. static const char* s_constantTypeName[ConstantType::Count] =
  59. {
  60. "int",
  61. "float",
  62. NULL,
  63. "int",
  64. "float",
  65. "float2",
  66. "float3",
  67. "float4",
  68. "float3x3",
  69. "float4x4",
  70. };
  71. struct Uniform
  72. {
  73. std::string name;
  74. ConstantType::Enum type;
  75. uint8_t num;
  76. uint16_t regIndex;
  77. uint16_t regCount;
  78. };
  79. typedef std::vector<Uniform> UniformArray;
  80. #if BX_PLATFORM_WINDOWS
  81. struct ConstRemap
  82. {
  83. ConstantType::Enum id;
  84. D3DXPARAMETER_CLASS paramClass;
  85. D3DXPARAMETER_TYPE paramType;
  86. uint32_t paramBytes;
  87. };
  88. static const ConstRemap s_constRemap[7] =
  89. {
  90. { ConstantType::Uniform1iv, D3DXPC_SCALAR, D3DXPT_INT, 4 },
  91. { ConstantType::Uniform1fv, D3DXPC_SCALAR, D3DXPT_FLOAT, 4 },
  92. { ConstantType::Uniform2fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 8 },
  93. { ConstantType::Uniform3fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 12 },
  94. { ConstantType::Uniform4fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 16 },
  95. { ConstantType::Uniform3x3fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 36 },
  96. { ConstantType::Uniform4x4fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 64 },
  97. };
  98. ConstantType::Enum findConstantType(const D3DXCONSTANT_DESC& constDesc)
  99. {
  100. uint32_t count = sizeof(s_constRemap)/sizeof(ConstRemap);
  101. for (uint32_t ii = 0; ii < count; ++ii)
  102. {
  103. const ConstRemap& remap = s_constRemap[ii];
  104. if (remap.paramClass == constDesc.Class
  105. && remap.paramType == constDesc.Type
  106. && (constDesc.Bytes%remap.paramBytes) == 0)
  107. {
  108. return remap.id;
  109. }
  110. }
  111. return ConstantType::Count;
  112. }
  113. static uint32_t s_optimizationLevel[4] =
  114. {
  115. D3DXSHADER_OPTIMIZATION_LEVEL0,
  116. D3DXSHADER_OPTIMIZATION_LEVEL1,
  117. D3DXSHADER_OPTIMIZATION_LEVEL2,
  118. D3DXSHADER_OPTIMIZATION_LEVEL3,
  119. };
  120. #endif // BX_PLATFORM_WINDOWS
  121. class Stream
  122. {
  123. public:
  124. Stream(FILE* _file, bool _bigEndian = false)
  125. : m_file(_file)
  126. , m_bigEndian(_bigEndian)
  127. {
  128. }
  129. ~Stream()
  130. {
  131. }
  132. void close()
  133. {
  134. m_file = NULL;
  135. }
  136. void writef(const char* _format, ...)
  137. {
  138. if (NULL != m_file)
  139. {
  140. va_list argList;
  141. va_start(argList, _format);
  142. char temp[2048];
  143. int len = vsnprintf(temp, sizeof(temp), _format, argList);
  144. fwrite(temp, len, 1, m_file);
  145. va_end(argList);
  146. }
  147. }
  148. void write(const char* _str)
  149. {
  150. if (NULL != m_file)
  151. {
  152. fwrite(_str, strlen(_str), 1, m_file);
  153. }
  154. }
  155. void write(const void* _data, size_t _size)
  156. {
  157. if (NULL != m_file)
  158. {
  159. fwrite(_data, _size, 1, m_file);
  160. }
  161. }
  162. template<typename Ty>
  163. void write(Ty _value)
  164. {
  165. Ty temp = m_bigEndian ? bx::bigEndian<Ty>(_value) : _value;
  166. write(&temp, sizeof(Ty) );
  167. }
  168. private:
  169. FILE* m_file;
  170. bool m_bigEndian;
  171. };
  172. bool compileGLSLShader(CommandLine& _cmdLine, const std::string& _code, const char* _outFilePath)
  173. {
  174. const glslopt_shader_type type = (0 == _stricmp(_cmdLine.findOption('\0', "type"), "fragment") ) ? kGlslOptShaderFragment : kGlslOptShaderVertex;
  175. glslopt_ctx* ctx = glslopt_initialize(false);
  176. glslopt_shader* shader = glslopt_optimize(ctx, type, _code.c_str(), 0);
  177. if( !glslopt_get_status(shader) )
  178. {
  179. fprintf(stderr, "Code:\n---\n%s\n---\n", _code.c_str() );
  180. fprintf(stderr, "Error: %s\n", glslopt_get_log(shader) );
  181. glslopt_cleanup(ctx);
  182. return false;
  183. }
  184. const char* optimizedShader = glslopt_get_output(shader);
  185. FILE* out = fopen(_outFilePath, "wb");
  186. if (NULL == out)
  187. {
  188. fprintf(stderr, "Unable to open output file '%s'.", _outFilePath);
  189. glslopt_cleanup(ctx);
  190. return false;
  191. }
  192. Stream stream(out);
  193. const char* profile = _cmdLine.findOption('p');
  194. if (NULL == profile)
  195. {
  196. stream.write("#ifdef GL_ES\n");
  197. stream.write("precision highp float;\n");
  198. stream.write("#endif // GL_ES\n\n");
  199. }
  200. else
  201. {
  202. stream.writef("#version %s\n\n", profile);
  203. }
  204. stream.write(optimizedShader, strlen(optimizedShader) );
  205. uint8_t nul = 0;
  206. stream.write(nul);
  207. stream.close();
  208. fclose(out);
  209. glslopt_cleanup(ctx);
  210. return true;
  211. }
  212. bool compileHLSLShader(CommandLine& _cmdLine, const std::string& _code, const char* _outFilePath)
  213. {
  214. #if BX_PLATFORM_WINDOWS
  215. const char* profile = _cmdLine.findOption('p');
  216. if (NULL == profile)
  217. {
  218. printf("Shader profile must be specified.\n");
  219. return false;
  220. }
  221. bool bigEndian = _cmdLine.hasArg('\0', "xbox360");
  222. uint32_t flags = 0;
  223. flags |= _cmdLine.hasArg('\0', "debug") ? D3DXSHADER_DEBUG : 0;
  224. flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DXSHADER_AVOID_FLOW_CONTROL : 0;
  225. flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DXSHADER_NO_PRESHADER : 0;
  226. flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DXSHADER_PARTIALPRECISION : 0;
  227. flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DXSHADER_PREFER_FLOW_CONTROL : 0;
  228. uint32_t optimization = 3;
  229. if (_cmdLine.hasArg(optimization, 'O') )
  230. {
  231. optimization = bx::uint32_min(optimization, countof(s_optimizationLevel)-1);
  232. flags |= s_optimizationLevel[optimization];
  233. }
  234. else
  235. {
  236. flags |= D3DXSHADER_SKIPOPTIMIZATION;
  237. }
  238. BX_TRACE("Profile: %s", profile);
  239. BX_TRACE("Flags: 0x%08x", flags);
  240. BX_TRACE("Big Endian: %s", bigEndian?"true":"false");
  241. LPD3DXBUFFER code;
  242. LPD3DXBUFFER errorMsg;
  243. LPD3DXCONSTANTTABLE constantTable;
  244. HRESULT hr = D3DXCompileShader(_code.c_str()
  245. , _code.size()
  246. , NULL
  247. , NULL
  248. , "main"
  249. , profile
  250. , flags
  251. , &code
  252. , &errorMsg
  253. , &constantTable
  254. );
  255. if (FAILED(hr) )
  256. {
  257. fprintf(stderr, "Code:\n---\n%s\n---\n", _code.c_str() );
  258. fprintf(stderr, "Error: 0x%08x %s\n", hr, errorMsg->GetBufferPointer() );
  259. return false;
  260. }
  261. D3DXCONSTANTTABLE_DESC desc;
  262. hr = constantTable->GetDesc(&desc);
  263. if (FAILED(hr) )
  264. {
  265. fprintf(stderr, "Error 0x%08x\n", hr);
  266. return false;
  267. }
  268. BX_TRACE("Creator: %s 0x%08x", desc.Creator, desc.Version);
  269. BX_TRACE("Num constants: %d", desc.Constants);
  270. BX_TRACE("# cl ty RxC S By Name");
  271. UniformArray uniforms;
  272. for (uint32_t ii = 0; ii < desc.Constants; ++ii)
  273. {
  274. D3DXHANDLE handle = constantTable->GetConstant(NULL, ii);
  275. D3DXCONSTANT_DESC constDesc;
  276. uint32_t count;
  277. constantTable->GetConstantDesc(handle, &constDesc, &count);
  278. BX_TRACE("%3d %2d %2d [%dx%d] %d %3d %s[%d] c%d (%d)"
  279. , ii
  280. , constDesc.Class
  281. , constDesc.Type
  282. , constDesc.Rows
  283. , constDesc.Columns
  284. , constDesc.StructMembers
  285. , constDesc.Bytes
  286. , constDesc.Name
  287. , constDesc.Elements
  288. , constDesc.RegisterIndex
  289. , constDesc.RegisterCount
  290. );
  291. ConstantType::Enum type = findConstantType(constDesc);
  292. if (ConstantType::Count != type)
  293. {
  294. Uniform un;
  295. un.name = '$' == constDesc.Name[0] ? constDesc.Name+1 : constDesc.Name;
  296. un.type = type;
  297. un.num = constDesc.Elements;
  298. un.regIndex = constDesc.RegisterIndex;
  299. un.regCount = constDesc.RegisterCount;
  300. uniforms.push_back(un);
  301. }
  302. }
  303. FILE* out = fopen(_outFilePath, "wb");
  304. if (NULL == out)
  305. {
  306. fprintf(stderr, "Unable to open output file '%s'.", _outFilePath);
  307. return false;
  308. }
  309. Stream stream(out, bigEndian);
  310. uint16_t count = (uint16_t)uniforms.size();
  311. stream.write(count);
  312. uint32_t fragmentBit = profile[0] == 'p' ? ConstantType::FragmentBit : 0;
  313. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  314. {
  315. const Uniform& un = *it;
  316. uint8_t nameSize = (uint8_t)un.name.size();
  317. stream.write(nameSize);
  318. stream.write(un.name.c_str(), nameSize);
  319. stream.write<uint8_t>(un.type|fragmentBit);
  320. stream.write(un.num);
  321. stream.write(un.regIndex);
  322. stream.write(un.regCount);
  323. BX_TRACE("%s, %s, %d, %d, %d"
  324. , un.name.c_str()
  325. , s_constantTypeName[un.type]
  326. , un.num
  327. , un.regIndex
  328. , un.regCount
  329. );
  330. }
  331. uint16_t shaderSize = (uint16_t)code->GetBufferSize();
  332. stream.write(shaderSize);
  333. stream.write(code->GetBufferPointer(), shaderSize);
  334. uint8_t nul = 0;
  335. stream.write(nul);
  336. stream.close();
  337. fclose(out);
  338. if (NULL != code)
  339. {
  340. code->Release();
  341. }
  342. if (NULL != errorMsg)
  343. {
  344. errorMsg->Release();
  345. }
  346. if (NULL != constantTable)
  347. {
  348. constantTable->Release();
  349. }
  350. return true;
  351. #else
  352. fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
  353. return false;
  354. #endif // BX_PLATFORM_WINDOWS
  355. }
  356. struct Preprocessor
  357. {
  358. Preprocessor(const char* _filePath)
  359. : m_tagptr(m_tags)
  360. , m_scratchPos(0)
  361. , m_fgetsPos(0)
  362. {
  363. m_filePath = scratch(_filePath);
  364. m_tagptr->tag = FPPTAG_USERDATA;
  365. m_tagptr->data = this;
  366. m_tagptr++;
  367. m_tagptr->tag = FPPTAG_INPUT;
  368. m_tagptr->data = (void*)fppInput;
  369. m_tagptr++;
  370. m_tagptr->tag = FPPTAG_OUTPUT;
  371. m_tagptr->data = (void*)fppOutput;
  372. m_tagptr++;
  373. m_tagptr->tag = FPPTAG_ERROR;
  374. m_tagptr->data = (void*)fppError;
  375. m_tagptr++;
  376. m_tagptr->tag = FPPTAG_IGNOREVERSION;
  377. m_tagptr->data = (void*)0;
  378. m_tagptr++;
  379. m_tagptr->tag = FPPTAG_LINE;
  380. m_tagptr->data = (void*)0;
  381. m_tagptr++;
  382. m_tagptr->tag = FPPTAG_INPUT_NAME;
  383. m_tagptr->data = m_filePath;
  384. m_tagptr++;
  385. m_default = "#define lowp\n#define mediump\n#define highp\n";
  386. }
  387. void setDefine(const char* _define)
  388. {
  389. m_tagptr->tag = FPPTAG_DEFINE;
  390. m_tagptr->data = scratch(_define);
  391. m_tagptr++;
  392. }
  393. void setDefaultDefine(const char* _name)
  394. {
  395. char temp[1024];
  396. _snprintf(temp, countof(temp)
  397. , "#ifndef %s\n"
  398. "# define %s 0\n"
  399. "#endif // %s\n"
  400. "\n"
  401. , _name
  402. , _name
  403. , _name
  404. );
  405. m_default += temp;
  406. }
  407. bool run()
  408. {
  409. m_fgetsPos = 0;
  410. FILE* file = fopen(m_filePath, "r");
  411. long int size = fsize(file);
  412. char* input = new char[size+1];
  413. size = fread(input, 1, size, file);
  414. input[size] = '\0';
  415. fclose(file);
  416. m_input = m_default;
  417. m_input += input;
  418. fppTag* tagptr = m_tagptr;
  419. tagptr->tag = FPPTAG_END;
  420. tagptr->data = 0;
  421. tagptr++;
  422. int result = fppPreProcess(m_tags);
  423. return 0 == result;
  424. }
  425. char* fgets(char* _buffer, int _size)
  426. {
  427. int ii = 0;
  428. for (char ch = m_input[m_fgetsPos]; m_fgetsPos < m_input.size() && ii < _size-1; ch = m_input[++m_fgetsPos])
  429. {
  430. _buffer[ii++] = ch;
  431. if (ch == '\n' || ii == _size)
  432. {
  433. _buffer[ii] = '\0';
  434. m_fgetsPos++;
  435. return _buffer;
  436. }
  437. }
  438. return NULL;
  439. }
  440. static char* fppInput(char* _buffer, int _size, void* _userData)
  441. {
  442. Preprocessor* thisClass = (Preprocessor*)_userData;
  443. return thisClass->fgets(_buffer, _size);
  444. }
  445. static void fppOutput(int _ch, void* _userData)
  446. {
  447. Preprocessor* thisClass = (Preprocessor*)_userData;
  448. thisClass->m_preprocessed += _ch;
  449. }
  450. static void fppError(void* _userData, char* _format, va_list _vargs)
  451. {
  452. vfprintf(stderr, _format, _vargs);
  453. }
  454. char* scratch(const char* _str)
  455. {
  456. char* result = &m_scratch[m_scratchPos];
  457. strcpy(result, _str);
  458. m_scratchPos += strlen(_str)+1;
  459. return result;
  460. }
  461. fppTag m_tags[MAX_TAGS];
  462. fppTag* m_tagptr;
  463. char* m_filePath;
  464. std::string m_default;
  465. std::string m_input;
  466. std::string m_preprocessed;
  467. char m_scratch[16<<10];
  468. uint32_t m_scratchPos;
  469. uint32_t m_fgetsPos;
  470. };
  471. // OpenGL #version Features Direct3D Features Shader Model
  472. // 2.1 120 vf 9.0 vf 2.0
  473. // 3.0 130
  474. // 3.1 140
  475. // 3.2 150 vgf
  476. // 3.3 330 10.0 vgf 4.0
  477. // 4.0 400 vhdgf
  478. // 4.1 410
  479. // 4.2 420 11.0 vhdgf 5.0
  480. int main(int _argc, const char* _argv[])
  481. {
  482. CommandLine cmdLine(_argc, _argv);
  483. const char* filePath = cmdLine.findOption('f');
  484. if (NULL == filePath)
  485. {
  486. fprintf(stderr, "Shader file name must be specified.\n");
  487. return EXIT_FAILURE;
  488. }
  489. const char* outFilePath = cmdLine.findOption('o');
  490. if (NULL == outFilePath)
  491. {
  492. fprintf(stderr, "Output file name must be specified.\n");
  493. return EXIT_FAILURE;
  494. }
  495. const char* type = cmdLine.findOption('\0', "type");
  496. if (NULL == type)
  497. {
  498. fprintf(stderr, "Must specify shader type.");
  499. return EXIT_FAILURE;
  500. }
  501. const char* platform = cmdLine.findOption('\0', "platform");
  502. if (NULL == platform)
  503. {
  504. fprintf(stderr, "Must specify platform.\n");
  505. return EXIT_FAILURE;
  506. }
  507. bool preprocessOnly = cmdLine.hasArg("preprocess");
  508. Preprocessor preprocessor(filePath);
  509. preprocessor.setDefaultDefine("BX_PLATFORM_ANDROID");
  510. preprocessor.setDefaultDefine("BX_PLATFORM_NACL");
  511. preprocessor.setDefaultDefine("BX_PLATFORM_WINDOWS");
  512. preprocessor.setDefaultDefine("BX_PLATFORM_XBOX360");
  513. preprocessor.setDefaultDefine("BX_PLATFORM_LINUX");
  514. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_GLSL");
  515. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_HLSL");
  516. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_FRAGMENT");
  517. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_VERTEX");
  518. bool glsl = false;
  519. if (0 == _stricmp(platform, "android") )
  520. {
  521. preprocessor.setDefine("BX_PLATFORM_ANDROID=1");
  522. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  523. glsl = true;
  524. }
  525. else if (0 == _stricmp(platform, "nacl") )
  526. {
  527. preprocessor.setDefine("BX_PLATFORM_NACL=1");
  528. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  529. glsl = true;
  530. }
  531. else if (0 == _stricmp(platform, "linux"))
  532. {
  533. preprocessor.setDefine("BX_PLATFORM_LINUX=1");
  534. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  535. glsl = true;
  536. }
  537. else if (0 == _stricmp(platform, "windows") )
  538. {
  539. preprocessor.setDefine("BX_PLATFORM_WINDOWS=1");
  540. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=1");
  541. }
  542. else if (0 == _stricmp(platform, "xbox360") )
  543. {
  544. preprocessor.setDefine("BX_PLATFORM_XBOX360=1");
  545. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=1");
  546. }
  547. else
  548. {
  549. fprintf(stderr, "Unknown platform %s?!", platform);
  550. return EXIT_FAILURE;
  551. }
  552. switch (tolower(type[0]) )
  553. {
  554. case 'f':
  555. preprocessor.setDefine("BGFX_SHADER_TYPE_FRAGMENT=1");
  556. break;
  557. case 'v':
  558. preprocessor.setDefine("BGFX_SHADER_TYPE_VERTEX=1");
  559. break;
  560. default:
  561. fprintf(stderr, "Unknown type: %s?!", type);
  562. return EXIT_FAILURE;
  563. }
  564. if (preprocessor.run() )
  565. {
  566. BX_TRACE("Input file: %s", filePath);
  567. BX_TRACE("Output file: %s", outFilePath);
  568. if (preprocessOnly)
  569. {
  570. FILE* out = fopen(outFilePath, "wb");
  571. if (NULL == out)
  572. {
  573. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  574. return false;
  575. }
  576. Stream stream(out);
  577. if (glsl)
  578. {
  579. const char* profile = cmdLine.findOption('p');
  580. if (NULL == profile)
  581. {
  582. stream.write("#ifdef GL_ES\n");
  583. stream.write("precision highp float;\n");
  584. stream.write("#endif // GL_ES\n\n");
  585. }
  586. else
  587. {
  588. stream.writef("#version %s\n\n", profile);
  589. }
  590. }
  591. stream.write(preprocessor.m_preprocessed.c_str(), preprocessor.m_preprocessed.size() );
  592. stream.close();
  593. fclose(out);
  594. return EXIT_SUCCESS;
  595. }
  596. if (glsl)
  597. {
  598. if (compileGLSLShader(cmdLine, preprocessor.m_preprocessed, outFilePath) )
  599. {
  600. return EXIT_SUCCESS;
  601. }
  602. }
  603. else
  604. {
  605. if (compileHLSLShader(cmdLine, preprocessor.m_preprocessed, outFilePath) )
  606. {
  607. return EXIT_SUCCESS;
  608. }
  609. }
  610. }
  611. fprintf(stderr, "Failed to build shader.\n");
  612. return EXIT_FAILURE;
  613. }