shaderc.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. class LineReader
  173. {
  174. public:
  175. LineReader(const char* _str)
  176. : m_str(_str)
  177. , m_pos(0)
  178. , m_size( (uint32_t)strlen(_str) )
  179. {
  180. }
  181. std::string getLine()
  182. {
  183. const char* str = &m_str[m_pos];
  184. skipLine();
  185. const char* eol = &m_str[m_pos];
  186. std::string tmp;
  187. tmp.assign(str, eol-str);
  188. return tmp;
  189. }
  190. bool isEof() const
  191. {
  192. return m_str[m_pos] == '\0';
  193. }
  194. private:
  195. void skipLine()
  196. {
  197. const char* str = &m_str[m_pos];
  198. const char* eol = strstr(str, "\r\n");
  199. if (NULL != eol)
  200. {
  201. m_pos += eol-str+2;
  202. return;
  203. }
  204. eol = strstr(str, "\n\r");
  205. if (NULL != eol)
  206. {
  207. m_pos += eol-str+2;
  208. return;
  209. }
  210. eol = strstr(str, "\n");
  211. if (NULL != eol)
  212. {
  213. m_pos += eol-str+1;
  214. return;
  215. }
  216. m_pos += (uint32_t)strlen(str);
  217. }
  218. const char* m_str;
  219. uint32_t m_pos;
  220. uint32_t m_size;
  221. };
  222. void printCode(const char* _code)
  223. {
  224. fprintf(stderr, "Code:\n---\n");
  225. LineReader lr(_code);
  226. for (uint32_t line = 1; !lr.isEof(); ++line)
  227. {
  228. fprintf(stderr, "%3d: %s", line, lr.getLine().c_str() );
  229. }
  230. fprintf(stderr, "---\n");
  231. }
  232. bool compileGLSLShader(CommandLine& _cmdLine, const std::string& _code, const char* _outFilePath)
  233. {
  234. const glslopt_shader_type type = (0 == _stricmp(_cmdLine.findOption('\0', "type"), "fragment") ) ? kGlslOptShaderFragment : kGlslOptShaderVertex;
  235. glslopt_ctx* ctx = glslopt_initialize(false);
  236. glslopt_shader* shader = glslopt_optimize(ctx, type, _code.c_str(), 0);
  237. if( !glslopt_get_status(shader) )
  238. {
  239. printCode(_code.c_str() );
  240. fprintf(stderr, "Error: %s\n", glslopt_get_log(shader) );
  241. glslopt_cleanup(ctx);
  242. return false;
  243. }
  244. const char* optimizedShader = glslopt_get_output(shader);
  245. FILE* out = fopen(_outFilePath, "wb");
  246. if (NULL == out)
  247. {
  248. fprintf(stderr, "Unable to open output file '%s'.", _outFilePath);
  249. glslopt_cleanup(ctx);
  250. return false;
  251. }
  252. Stream stream(out);
  253. const char* profile = _cmdLine.findOption('p');
  254. if (NULL == profile)
  255. {
  256. stream.write("#ifdef GL_ES\n");
  257. stream.write("precision highp float;\n");
  258. stream.write("#endif // GL_ES\n\n");
  259. }
  260. else
  261. {
  262. stream.writef("#version %s\n\n", profile);
  263. }
  264. stream.write(optimizedShader, strlen(optimizedShader) );
  265. uint8_t nul = 0;
  266. stream.write(nul);
  267. stream.close();
  268. fclose(out);
  269. glslopt_cleanup(ctx);
  270. return true;
  271. }
  272. bool compileHLSLShader(CommandLine& _cmdLine, const std::string& _code, const char* _outFilePath)
  273. {
  274. #if BX_PLATFORM_WINDOWS
  275. const char* profile = _cmdLine.findOption('p');
  276. if (NULL == profile)
  277. {
  278. printf("Shader profile must be specified.\n");
  279. return false;
  280. }
  281. bool bigEndian = _cmdLine.hasArg('\0', "xbox360");
  282. uint32_t flags = 0;
  283. flags |= _cmdLine.hasArg('\0', "debug") ? D3DXSHADER_DEBUG : 0;
  284. flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DXSHADER_AVOID_FLOW_CONTROL : 0;
  285. flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DXSHADER_NO_PRESHADER : 0;
  286. flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DXSHADER_PARTIALPRECISION : 0;
  287. flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DXSHADER_PREFER_FLOW_CONTROL : 0;
  288. flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY : 0;
  289. uint32_t optimization = 3;
  290. if (_cmdLine.hasArg(optimization, 'O') )
  291. {
  292. optimization = bx::uint32_min(optimization, countof(s_optimizationLevel)-1);
  293. flags |= s_optimizationLevel[optimization];
  294. }
  295. else
  296. {
  297. flags |= D3DXSHADER_SKIPOPTIMIZATION;
  298. }
  299. BX_TRACE("Profile: %s", profile);
  300. BX_TRACE("Flags: 0x%08x", flags);
  301. BX_TRACE("Big Endian: %s", bigEndian?"true":"false");
  302. LPD3DXBUFFER code;
  303. LPD3DXBUFFER errorMsg;
  304. LPD3DXCONSTANTTABLE constantTable;
  305. HRESULT hr = D3DXCompileShader(_code.c_str()
  306. , _code.size()
  307. , NULL
  308. , NULL
  309. , "main"
  310. , profile
  311. , flags
  312. , &code
  313. , &errorMsg
  314. , &constantTable
  315. );
  316. if (FAILED(hr) )
  317. {
  318. printCode(_code.c_str() );
  319. fprintf(stderr, "Error: 0x%08x %s\n", hr, errorMsg->GetBufferPointer() );
  320. return false;
  321. }
  322. D3DXCONSTANTTABLE_DESC desc;
  323. hr = constantTable->GetDesc(&desc);
  324. if (FAILED(hr) )
  325. {
  326. fprintf(stderr, "Error 0x%08x\n", hr);
  327. return false;
  328. }
  329. BX_TRACE("Creator: %s 0x%08x", desc.Creator, desc.Version);
  330. BX_TRACE("Num constants: %d", desc.Constants);
  331. BX_TRACE("# cl ty RxC S By Name");
  332. UniformArray uniforms;
  333. for (uint32_t ii = 0; ii < desc.Constants; ++ii)
  334. {
  335. D3DXHANDLE handle = constantTable->GetConstant(NULL, ii);
  336. D3DXCONSTANT_DESC constDesc;
  337. uint32_t count;
  338. constantTable->GetConstantDesc(handle, &constDesc, &count);
  339. BX_TRACE("%3d %2d %2d [%dx%d] %d %3d %s[%d] c%d (%d)"
  340. , ii
  341. , constDesc.Class
  342. , constDesc.Type
  343. , constDesc.Rows
  344. , constDesc.Columns
  345. , constDesc.StructMembers
  346. , constDesc.Bytes
  347. , constDesc.Name
  348. , constDesc.Elements
  349. , constDesc.RegisterIndex
  350. , constDesc.RegisterCount
  351. );
  352. ConstantType::Enum type = findConstantType(constDesc);
  353. if (ConstantType::Count != type)
  354. {
  355. Uniform un;
  356. un.name = '$' == constDesc.Name[0] ? constDesc.Name+1 : constDesc.Name;
  357. un.type = type;
  358. un.num = constDesc.Elements;
  359. un.regIndex = constDesc.RegisterIndex;
  360. un.regCount = constDesc.RegisterCount;
  361. uniforms.push_back(un);
  362. }
  363. }
  364. FILE* out = fopen(_outFilePath, "wb");
  365. if (NULL == out)
  366. {
  367. fprintf(stderr, "Unable to open output file '%s'.", _outFilePath);
  368. return false;
  369. }
  370. Stream stream(out, bigEndian);
  371. uint16_t count = (uint16_t)uniforms.size();
  372. stream.write(count);
  373. uint32_t fragmentBit = profile[0] == 'p' ? ConstantType::FragmentBit : 0;
  374. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  375. {
  376. const Uniform& un = *it;
  377. uint8_t nameSize = (uint8_t)un.name.size();
  378. stream.write(nameSize);
  379. stream.write(un.name.c_str(), nameSize);
  380. stream.write<uint8_t>(un.type|fragmentBit);
  381. stream.write(un.num);
  382. stream.write(un.regIndex);
  383. stream.write(un.regCount);
  384. BX_TRACE("%s, %s, %d, %d, %d"
  385. , un.name.c_str()
  386. , s_constantTypeName[un.type]
  387. , un.num
  388. , un.regIndex
  389. , un.regCount
  390. );
  391. }
  392. uint16_t shaderSize = (uint16_t)code->GetBufferSize();
  393. stream.write(shaderSize);
  394. stream.write(code->GetBufferPointer(), shaderSize);
  395. uint8_t nul = 0;
  396. stream.write(nul);
  397. stream.close();
  398. fclose(out);
  399. if (NULL != code)
  400. {
  401. code->Release();
  402. }
  403. if (NULL != errorMsg)
  404. {
  405. errorMsg->Release();
  406. }
  407. if (NULL != constantTable)
  408. {
  409. constantTable->Release();
  410. }
  411. return true;
  412. #else
  413. fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
  414. return false;
  415. #endif // BX_PLATFORM_WINDOWS
  416. }
  417. struct Preprocessor
  418. {
  419. Preprocessor(const char* _filePath)
  420. : m_tagptr(m_tags)
  421. , m_scratchPos(0)
  422. , m_fgetsPos(0)
  423. {
  424. m_filePath = scratch(_filePath);
  425. m_tagptr->tag = FPPTAG_USERDATA;
  426. m_tagptr->data = this;
  427. m_tagptr++;
  428. m_tagptr->tag = FPPTAG_INPUT;
  429. m_tagptr->data = (void*)fppInput;
  430. m_tagptr++;
  431. m_tagptr->tag = FPPTAG_OUTPUT;
  432. m_tagptr->data = (void*)fppOutput;
  433. m_tagptr++;
  434. m_tagptr->tag = FPPTAG_ERROR;
  435. m_tagptr->data = (void*)fppError;
  436. m_tagptr++;
  437. m_tagptr->tag = FPPTAG_IGNOREVERSION;
  438. m_tagptr->data = (void*)0;
  439. m_tagptr++;
  440. m_tagptr->tag = FPPTAG_LINE;
  441. m_tagptr->data = (void*)0;
  442. m_tagptr++;
  443. m_tagptr->tag = FPPTAG_INPUT_NAME;
  444. m_tagptr->data = m_filePath;
  445. m_tagptr++;
  446. m_default = "#define lowp\n#define mediump\n#define highp\n";
  447. }
  448. void setDefine(const char* _define)
  449. {
  450. m_tagptr->tag = FPPTAG_DEFINE;
  451. m_tagptr->data = scratch(_define);
  452. m_tagptr++;
  453. }
  454. void setDefaultDefine(const char* _name)
  455. {
  456. char temp[1024];
  457. _snprintf(temp, countof(temp)
  458. , "#ifndef %s\n"
  459. "# define %s 0\n"
  460. "#endif // %s\n"
  461. "\n"
  462. , _name
  463. , _name
  464. , _name
  465. );
  466. m_default += temp;
  467. }
  468. bool run()
  469. {
  470. m_fgetsPos = 0;
  471. FILE* file = fopen(m_filePath, "r");
  472. long int size = fsize(file);
  473. char* input = new char[size+1];
  474. size = fread(input, 1, size, file);
  475. input[size] = '\0';
  476. fclose(file);
  477. m_input = m_default;
  478. m_input += input;
  479. fppTag* tagptr = m_tagptr;
  480. tagptr->tag = FPPTAG_END;
  481. tagptr->data = 0;
  482. tagptr++;
  483. int result = fppPreProcess(m_tags);
  484. return 0 == result;
  485. }
  486. char* fgets(char* _buffer, int _size)
  487. {
  488. int ii = 0;
  489. for (char ch = m_input[m_fgetsPos]; m_fgetsPos < m_input.size() && ii < _size-1; ch = m_input[++m_fgetsPos])
  490. {
  491. _buffer[ii++] = ch;
  492. if (ch == '\n' || ii == _size)
  493. {
  494. _buffer[ii] = '\0';
  495. m_fgetsPos++;
  496. return _buffer;
  497. }
  498. }
  499. return NULL;
  500. }
  501. static char* fppInput(char* _buffer, int _size, void* _userData)
  502. {
  503. Preprocessor* thisClass = (Preprocessor*)_userData;
  504. return thisClass->fgets(_buffer, _size);
  505. }
  506. static void fppOutput(int _ch, void* _userData)
  507. {
  508. Preprocessor* thisClass = (Preprocessor*)_userData;
  509. thisClass->m_preprocessed += _ch;
  510. }
  511. static void fppError(void* _userData, char* _format, va_list _vargs)
  512. {
  513. vfprintf(stderr, _format, _vargs);
  514. }
  515. char* scratch(const char* _str)
  516. {
  517. char* result = &m_scratch[m_scratchPos];
  518. strcpy(result, _str);
  519. m_scratchPos += strlen(_str)+1;
  520. return result;
  521. }
  522. fppTag m_tags[MAX_TAGS];
  523. fppTag* m_tagptr;
  524. char* m_filePath;
  525. std::string m_default;
  526. std::string m_input;
  527. std::string m_preprocessed;
  528. char m_scratch[16<<10];
  529. uint32_t m_scratchPos;
  530. uint32_t m_fgetsPos;
  531. };
  532. // OpenGL #version Features Direct3D Features Shader Model
  533. // 2.1 120 vf 9.0 vf 2.0
  534. // 3.0 130
  535. // 3.1 140
  536. // 3.2 150 vgf
  537. // 3.3 330 10.0 vgf 4.0
  538. // 4.0 400 vhdgf
  539. // 4.1 410
  540. // 4.2 420 11.0 vhdgf 5.0
  541. int main(int _argc, const char* _argv[])
  542. {
  543. CommandLine cmdLine(_argc, _argv);
  544. const char* filePath = cmdLine.findOption('f');
  545. if (NULL == filePath)
  546. {
  547. fprintf(stderr, "Shader file name must be specified.\n");
  548. return EXIT_FAILURE;
  549. }
  550. const char* outFilePath = cmdLine.findOption('o');
  551. if (NULL == outFilePath)
  552. {
  553. fprintf(stderr, "Output file name must be specified.\n");
  554. return EXIT_FAILURE;
  555. }
  556. const char* type = cmdLine.findOption('\0', "type");
  557. if (NULL == type)
  558. {
  559. fprintf(stderr, "Must specify shader type.");
  560. return EXIT_FAILURE;
  561. }
  562. const char* platform = cmdLine.findOption('\0', "platform");
  563. if (NULL == platform)
  564. {
  565. fprintf(stderr, "Must specify platform.\n");
  566. return EXIT_FAILURE;
  567. }
  568. bool preprocessOnly = cmdLine.hasArg("preprocess");
  569. Preprocessor preprocessor(filePath);
  570. preprocessor.setDefaultDefine("BX_PLATFORM_ANDROID");
  571. preprocessor.setDefaultDefine("BX_PLATFORM_NACL");
  572. preprocessor.setDefaultDefine("BX_PLATFORM_WINDOWS");
  573. preprocessor.setDefaultDefine("BX_PLATFORM_XBOX360");
  574. preprocessor.setDefaultDefine("BX_PLATFORM_LINUX");
  575. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_GLSL");
  576. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_HLSL");
  577. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_FRAGMENT");
  578. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_VERTEX");
  579. bool glsl = false;
  580. if (0 == _stricmp(platform, "android") )
  581. {
  582. preprocessor.setDefine("BX_PLATFORM_ANDROID=1");
  583. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  584. glsl = true;
  585. }
  586. else if (0 == _stricmp(platform, "nacl") )
  587. {
  588. preprocessor.setDefine("BX_PLATFORM_NACL=1");
  589. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  590. glsl = true;
  591. }
  592. else if (0 == _stricmp(platform, "linux"))
  593. {
  594. preprocessor.setDefine("BX_PLATFORM_LINUX=1");
  595. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  596. glsl = true;
  597. }
  598. else if (0 == _stricmp(platform, "windows") )
  599. {
  600. preprocessor.setDefine("BX_PLATFORM_WINDOWS=1");
  601. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=1");
  602. }
  603. else if (0 == _stricmp(platform, "xbox360") )
  604. {
  605. preprocessor.setDefine("BX_PLATFORM_XBOX360=1");
  606. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=1");
  607. }
  608. else
  609. {
  610. fprintf(stderr, "Unknown platform %s?!", platform);
  611. return EXIT_FAILURE;
  612. }
  613. switch (tolower(type[0]) )
  614. {
  615. case 'f':
  616. preprocessor.setDefine("BGFX_SHADER_TYPE_FRAGMENT=1");
  617. break;
  618. case 'v':
  619. preprocessor.setDefine("BGFX_SHADER_TYPE_VERTEX=1");
  620. break;
  621. default:
  622. fprintf(stderr, "Unknown type: %s?!", type);
  623. return EXIT_FAILURE;
  624. }
  625. if (preprocessor.run() )
  626. {
  627. BX_TRACE("Input file: %s", filePath);
  628. BX_TRACE("Output file: %s", outFilePath);
  629. if (preprocessOnly)
  630. {
  631. FILE* out = fopen(outFilePath, "wb");
  632. if (NULL == out)
  633. {
  634. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  635. return false;
  636. }
  637. Stream stream(out);
  638. if (glsl)
  639. {
  640. const char* profile = cmdLine.findOption('p');
  641. if (NULL == profile)
  642. {
  643. stream.write("#ifdef GL_ES\n");
  644. stream.write("precision highp float;\n");
  645. stream.write("#endif // GL_ES\n\n");
  646. }
  647. else
  648. {
  649. stream.writef("#version %s\n\n", profile);
  650. }
  651. }
  652. stream.write(preprocessor.m_preprocessed.c_str(), preprocessor.m_preprocessed.size() );
  653. stream.close();
  654. fclose(out);
  655. return EXIT_SUCCESS;
  656. }
  657. if (glsl)
  658. {
  659. if (compileGLSLShader(cmdLine, preprocessor.m_preprocessed, outFilePath) )
  660. {
  661. return EXIT_SUCCESS;
  662. }
  663. }
  664. else
  665. {
  666. if (compileHLSLShader(cmdLine, preprocessor.m_preprocessed, outFilePath) )
  667. {
  668. return EXIT_SUCCESS;
  669. }
  670. }
  671. }
  672. fprintf(stderr, "Failed to build shader.\n");
  673. return EXIT_FAILURE;
  674. }