shaderc.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /*
  2. * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #define SHADERC_DEBUG 0
  6. #define NOMINMAX
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <string>
  12. #include <vector>
  13. #include "glsl_optimizer.h"
  14. #define MAX_TAGS 256
  15. extern "C"
  16. {
  17. #include <fpp.h>
  18. } // extern "C"
  19. #if SHADERC_DEBUG
  20. # define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  21. #endif // DEBUG
  22. #define BX_NAMESPACE 1
  23. #include <bx/bx.h>
  24. #if BX_PLATFORM_LINUX
  25. # include <stdarg.h>
  26. # define _stricmp strcasecmp
  27. # define _snprintf snprintf
  28. #endif // BX_PLATFORM_LINUX
  29. #include <bx/commandline.h>
  30. #include <bx/countof.h>
  31. #include <bx/endian.h>
  32. #include <bx/uint32_t.h>
  33. #if BX_PLATFORM_WINDOWS
  34. # include <d3dx9.h>
  35. # include <d3dcompiler.h>
  36. #endif // BX_PLATFORM_WINDOWS
  37. long int fsize(FILE* _file)
  38. {
  39. long int pos = ftell(_file);
  40. fseek(_file, 0L, SEEK_END);
  41. long int size = ftell(_file);
  42. fseek(_file, pos, SEEK_SET);
  43. return size;
  44. }
  45. struct Attrib
  46. {
  47. enum Enum
  48. {
  49. Position = 0,
  50. Normal,
  51. Color0,
  52. Color1,
  53. Indices,
  54. Weight,
  55. TexCoord0,
  56. TexCoord1,
  57. TexCoord2,
  58. TexCoord3,
  59. TexCoord4,
  60. TexCoord5,
  61. TexCoord6,
  62. TexCoord7,
  63. Count,
  64. };
  65. };
  66. struct RemapInputSemantic
  67. {
  68. Attrib::Enum m_attr;
  69. const char* m_name;
  70. uint8_t m_index;
  71. };
  72. static const RemapInputSemantic s_remapInputSemantic[Attrib::Count] =
  73. {
  74. { Attrib::Position, "POSITION", 0 },
  75. { Attrib::Normal, "NORMAL", 0 },
  76. { Attrib::Color0, "COLOR", 0 },
  77. { Attrib::Color1, "COLOR", 1 },
  78. { Attrib::Indices, "BLENDINDICES", 0 },
  79. { Attrib::Weight, "BLENDWEIGHT", 0 },
  80. { Attrib::TexCoord0, "TEXCOORD", 0 },
  81. { Attrib::TexCoord1, "TEXCOORD", 1 },
  82. { Attrib::TexCoord2, "TEXCOORD", 2 },
  83. { Attrib::TexCoord3, "TEXCOORD", 3 },
  84. { Attrib::TexCoord4, "TEXCOORD", 4 },
  85. { Attrib::TexCoord5, "TEXCOORD", 5 },
  86. { Attrib::TexCoord6, "TEXCOORD", 6 },
  87. { Attrib::TexCoord7, "TEXCOORD", 7 },
  88. };
  89. struct ConstantType
  90. {
  91. enum Enum
  92. {
  93. Uniform1i,
  94. Uniform1f,
  95. End,
  96. Uniform1iv,
  97. Uniform1fv,
  98. Uniform2fv,
  99. Uniform3fv,
  100. Uniform4fv,
  101. Uniform3x3fv,
  102. Uniform4x4fv,
  103. Count,
  104. TypeMask = 0x7f,
  105. FragmentBit = 0x80
  106. };
  107. };
  108. static const char* s_constantTypeName[ConstantType::Count] =
  109. {
  110. "int",
  111. "float",
  112. NULL,
  113. "int",
  114. "float",
  115. "float2",
  116. "float3",
  117. "float4",
  118. "float3x3",
  119. "float4x4",
  120. };
  121. struct Uniform
  122. {
  123. std::string name;
  124. ConstantType::Enum type;
  125. uint8_t num;
  126. uint16_t regIndex;
  127. uint16_t regCount;
  128. };
  129. typedef std::vector<Uniform> UniformArray;
  130. #if BX_PLATFORM_WINDOWS
  131. struct ConstRemapDx9
  132. {
  133. ConstantType::Enum id;
  134. D3DXPARAMETER_CLASS paramClass;
  135. D3DXPARAMETER_TYPE paramType;
  136. uint32_t paramBytes;
  137. };
  138. static const ConstRemapDx9 s_constRemapDx9[7] =
  139. {
  140. { ConstantType::Uniform1iv, D3DXPC_SCALAR, D3DXPT_INT, 4 },
  141. { ConstantType::Uniform1fv, D3DXPC_SCALAR, D3DXPT_FLOAT, 4 },
  142. { ConstantType::Uniform2fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 8 },
  143. { ConstantType::Uniform3fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 12 },
  144. { ConstantType::Uniform4fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 16 },
  145. { ConstantType::Uniform3x3fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 36 },
  146. { ConstantType::Uniform4x4fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 64 },
  147. };
  148. ConstantType::Enum findConstantTypeDx9(const D3DXCONSTANT_DESC& constDesc)
  149. {
  150. uint32_t count = sizeof(s_constRemapDx9)/sizeof(ConstRemapDx9);
  151. for (uint32_t ii = 0; ii < count; ++ii)
  152. {
  153. const ConstRemapDx9& remap = s_constRemapDx9[ii];
  154. if (remap.paramClass == constDesc.Class
  155. && remap.paramType == constDesc.Type
  156. && (constDesc.Bytes%remap.paramBytes) == 0)
  157. {
  158. return remap.id;
  159. }
  160. }
  161. return ConstantType::Count;
  162. }
  163. static uint32_t s_optimizationLevelDx9[4] =
  164. {
  165. D3DXSHADER_OPTIMIZATION_LEVEL0,
  166. D3DXSHADER_OPTIMIZATION_LEVEL1,
  167. D3DXSHADER_OPTIMIZATION_LEVEL2,
  168. D3DXSHADER_OPTIMIZATION_LEVEL3,
  169. };
  170. struct ConstRemapDx11
  171. {
  172. ConstantType::Enum id;
  173. D3D_SHADER_VARIABLE_CLASS paramClass;
  174. D3D_SHADER_VARIABLE_TYPE paramType;
  175. uint32_t paramBytes;
  176. };
  177. static const ConstRemapDx11 s_constRemapDx11[7] =
  178. {
  179. { ConstantType::Uniform1iv, D3D_SVC_SCALAR, D3D_SVT_INT, 4 },
  180. { ConstantType::Uniform1fv, D3D_SVC_SCALAR, D3D_SVT_FLOAT, 4 },
  181. { ConstantType::Uniform2fv, D3D_SVC_VECTOR, D3D_SVT_FLOAT, 8 },
  182. { ConstantType::Uniform3fv, D3D_SVC_VECTOR, D3D_SVT_FLOAT, 12 },
  183. { ConstantType::Uniform4fv, D3D_SVC_VECTOR, D3D_SVT_FLOAT, 16 },
  184. { ConstantType::Uniform3x3fv, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 36 },
  185. { ConstantType::Uniform4x4fv, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 64 },
  186. };
  187. ConstantType::Enum findConstantTypeDx11(const D3D11_SHADER_TYPE_DESC& constDesc, uint32_t _size)
  188. {
  189. uint32_t count = sizeof(s_constRemapDx11)/sizeof(ConstRemapDx9);
  190. for (uint32_t ii = 0; ii < count; ++ii)
  191. {
  192. const ConstRemapDx11& remap = s_constRemapDx11[ii];
  193. if (remap.paramClass == constDesc.Class
  194. && remap.paramType == constDesc.Type
  195. && remap.paramBytes == _size)
  196. {
  197. return remap.id;
  198. }
  199. }
  200. return ConstantType::Count;
  201. }
  202. static uint32_t s_optimizationLevelDx11[4] =
  203. {
  204. D3DCOMPILE_OPTIMIZATION_LEVEL0,
  205. D3DCOMPILE_OPTIMIZATION_LEVEL1,
  206. D3DCOMPILE_OPTIMIZATION_LEVEL2,
  207. D3DCOMPILE_OPTIMIZATION_LEVEL3,
  208. };
  209. #endif // BX_PLATFORM_WINDOWS
  210. class IStreamWriter
  211. {
  212. public:
  213. virtual ~IStreamWriter() = 0;
  214. virtual bool open() = 0;
  215. virtual void close() = 0;
  216. virtual void writef(const char* _format, ...) = 0;
  217. virtual void write(const char* _str) = 0;
  218. virtual void write(const void* _data, size_t _size) = 0;
  219. template<typename Ty>
  220. void write(Ty _value)
  221. {
  222. write(&_value, sizeof(Ty) );
  223. }
  224. void writeString(const char* _str)
  225. {
  226. uint16_t len = (uint16_t)strlen(_str);
  227. write(len);
  228. write(_str);
  229. char term = '\0';
  230. write(term);
  231. }
  232. // template<typename Ty>
  233. // void write(Ty _value)
  234. // {
  235. // Ty temp = m_bigEndian ? bx::bigEndian<Ty>(_value) : _value;
  236. // write(&temp, sizeof(Ty) );
  237. // }
  238. };
  239. IStreamWriter::~IStreamWriter()
  240. {
  241. }
  242. class FileWriter : public IStreamWriter
  243. {
  244. public:
  245. FileWriter(const char* _filePath, bool _bigEndian = false)
  246. : m_filePath(_filePath)
  247. , m_file(NULL)
  248. , m_bigEndian(_bigEndian)
  249. {
  250. }
  251. ~FileWriter()
  252. {
  253. }
  254. bool open()
  255. {
  256. BX_CHECK(NULL == m_file, "Still open!");
  257. m_file = fopen(m_filePath.c_str(), "wb");
  258. return NULL != m_file;
  259. }
  260. void close()
  261. {
  262. if (NULL != m_file)
  263. {
  264. fclose(m_file);
  265. m_file = NULL;
  266. }
  267. }
  268. void writef(const char* _format, ...)
  269. {
  270. if (NULL != m_file)
  271. {
  272. va_list argList;
  273. va_start(argList, _format);
  274. char temp[2048];
  275. int len = vsnprintf(temp, sizeof(temp), _format, argList);
  276. fwrite(temp, len, 1, m_file);
  277. va_end(argList);
  278. }
  279. }
  280. void write(const char* _str)
  281. {
  282. if (NULL != m_file)
  283. {
  284. fwrite(_str, strlen(_str), 1, m_file);
  285. }
  286. }
  287. void write(const void* _data, size_t _size)
  288. {
  289. if (NULL != m_file)
  290. {
  291. fwrite(_data, _size, 1, m_file);
  292. }
  293. }
  294. private:
  295. std::string m_filePath;
  296. FILE* m_file;
  297. bool m_bigEndian;
  298. };
  299. class Bin2cStream : public IStreamWriter
  300. {
  301. public:
  302. Bin2cStream(const char* _filePath, const char* _name)
  303. : m_filePath(_filePath)
  304. , m_name(_name)
  305. , m_file(NULL)
  306. {
  307. }
  308. ~Bin2cStream()
  309. {
  310. }
  311. bool open()
  312. {
  313. BX_CHECK(NULL == m_file, "Still open!");
  314. m_file = fopen(m_filePath.c_str(), "wb");
  315. return NULL != m_file;
  316. }
  317. void close()
  318. {
  319. if (NULL != m_file)
  320. {
  321. #define HEX_DUMP_WIDTH 16
  322. #define HEX_DUMP_SPACE_WIDTH 96
  323. #define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
  324. const uint8_t* data = &m_buffer[0];
  325. uint32_t size = m_buffer.size();
  326. fprintf(m_file, "static const uint8_t %s[%d] =\n{\n", m_name.c_str(), size);
  327. if (NULL != data)
  328. {
  329. char hex[HEX_DUMP_SPACE_WIDTH+1];
  330. char ascii[HEX_DUMP_WIDTH+1];
  331. uint32_t hexPos = 0;
  332. uint32_t asciiPos = 0;
  333. for (uint32_t ii = 0; ii < size; ++ii)
  334. {
  335. _snprintf(&hex[hexPos], sizeof(hex)-hexPos, "0x%02x, ", data[asciiPos]);
  336. hexPos += 6;
  337. ascii[asciiPos] = isprint(data[asciiPos]) && data[asciiPos] != '\\' ? data[asciiPos] : '.';
  338. asciiPos++;
  339. if (HEX_DUMP_WIDTH == asciiPos)
  340. {
  341. ascii[asciiPos] = '\0';
  342. fprintf(m_file, "\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
  343. data += asciiPos;
  344. hexPos = 0;
  345. asciiPos = 0;
  346. }
  347. }
  348. if (0 != asciiPos)
  349. {
  350. ascii[asciiPos] = '\0';
  351. fprintf(m_file, "\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
  352. }
  353. }
  354. fprintf(m_file, "};\n");
  355. #undef HEX_DUMP_WIDTH
  356. #undef HEX_DUMP_SPACE_WIDTH
  357. #undef HEX_DUMP_FORMAT
  358. fclose(m_file);
  359. m_file = NULL;
  360. }
  361. }
  362. void writef(const char* _format, ...)
  363. {
  364. va_list argList;
  365. va_start(argList, _format);
  366. char temp[2048];
  367. int len = vsnprintf(temp, sizeof(temp), _format, argList);
  368. m_buffer.insert(m_buffer.end(), temp, temp+len);
  369. va_end(argList);
  370. }
  371. void write(const char* _str)
  372. {
  373. m_buffer.insert(m_buffer.end(), _str, _str+strlen(_str) );
  374. }
  375. void write(const void* _data, size_t _size)
  376. {
  377. const char* data = (const char*)_data;
  378. m_buffer.insert(m_buffer.end(), data, data+_size);
  379. }
  380. private:
  381. std::string m_filePath;
  382. std::string m_name;
  383. typedef std::vector<uint8_t> Buffer;
  384. Buffer m_buffer;
  385. FILE* m_file;
  386. };
  387. class LineReader
  388. {
  389. public:
  390. LineReader(const char* _str)
  391. : m_str(_str)
  392. , m_pos(0)
  393. , m_size( (uint32_t)strlen(_str) )
  394. {
  395. }
  396. std::string getLine()
  397. {
  398. const char* str = &m_str[m_pos];
  399. skipLine();
  400. const char* eol = &m_str[m_pos];
  401. std::string tmp;
  402. tmp.assign(str, eol-str);
  403. return tmp;
  404. }
  405. bool isEof() const
  406. {
  407. return m_str[m_pos] == '\0';
  408. }
  409. private:
  410. void skipLine()
  411. {
  412. const char* str = &m_str[m_pos];
  413. const char* eol = strstr(str, "\n\r");
  414. if (NULL != eol)
  415. {
  416. m_pos += eol-str+2;
  417. return;
  418. }
  419. eol = strstr(str, "\n");
  420. if (NULL != eol)
  421. {
  422. m_pos += eol-str+1;
  423. return;
  424. }
  425. m_pos += (uint32_t)strlen(str);
  426. }
  427. const char* m_str;
  428. uint32_t m_pos;
  429. uint32_t m_size;
  430. };
  431. void printCode(const char* _code)
  432. {
  433. fprintf(stderr, "Code:\n---\n");
  434. LineReader lr(_code);
  435. for (uint32_t line = 1; !lr.isEof(); ++line)
  436. {
  437. fprintf(stderr, "%3d: %s", line, lr.getLine().c_str() );
  438. }
  439. fprintf(stderr, "---\n");
  440. }
  441. bool compileGLSLShader(CommandLine& _cmdLine, const std::string& _code, IStreamWriter& _stream)
  442. {
  443. const glslopt_shader_type type = tolower(_cmdLine.findOption('\0', "type")[0]) == 'f' ? kGlslOptShaderFragment : kGlslOptShaderVertex;
  444. glslopt_ctx* ctx = glslopt_initialize(false);
  445. glslopt_shader* shader = glslopt_optimize(ctx, type, _code.c_str(), 0);
  446. if( !glslopt_get_status(shader) )
  447. {
  448. printCode(_code.c_str() );
  449. fprintf(stderr, "Error: %s\n", glslopt_get_log(shader) );
  450. glslopt_cleanup(ctx);
  451. return false;
  452. }
  453. const char* optimizedShader = glslopt_get_output(shader);
  454. const char* profile = _cmdLine.findOption('p');
  455. if (NULL == profile)
  456. {
  457. _stream.write("#ifdef GL_ES\n");
  458. _stream.write("precision highp float;\n");
  459. _stream.write("#endif // GL_ES\n\n");
  460. }
  461. else
  462. {
  463. _stream.writef("#version %s\n\n", profile);
  464. }
  465. _stream.write(optimizedShader, strlen(optimizedShader) );
  466. uint8_t nul = 0;
  467. _stream.write(nul);
  468. glslopt_cleanup(ctx);
  469. return true;
  470. }
  471. bool compileHLSLShaderDx9(CommandLine& _cmdLine, const std::string& _code, IStreamWriter& _stream)
  472. {
  473. #if BX_PLATFORM_WINDOWS
  474. const char* profile = _cmdLine.findOption('p');
  475. if (NULL == profile)
  476. {
  477. printf("Shader profile must be specified.\n");
  478. return false;
  479. }
  480. bool bigEndian = _cmdLine.hasArg('\0', "xbox360");
  481. uint32_t flags = 0;
  482. flags |= _cmdLine.hasArg('\0', "debug") ? D3DXSHADER_DEBUG : 0;
  483. flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DXSHADER_AVOID_FLOW_CONTROL : 0;
  484. flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DXSHADER_NO_PRESHADER : 0;
  485. flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DXSHADER_PARTIALPRECISION : 0;
  486. flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DXSHADER_PREFER_FLOW_CONTROL : 0;
  487. flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY : 0;
  488. bool werror = _cmdLine.hasArg('\0', "Werror");
  489. uint32_t optimization = 3;
  490. if (_cmdLine.hasArg(optimization, 'O') )
  491. {
  492. optimization = bx::uint32_min(optimization, countof(s_optimizationLevelDx9)-1);
  493. flags |= s_optimizationLevelDx9[optimization];
  494. }
  495. else
  496. {
  497. flags |= D3DXSHADER_SKIPOPTIMIZATION;
  498. }
  499. BX_TRACE("Profile: %s", profile);
  500. BX_TRACE("Flags: 0x%08x", flags);
  501. BX_TRACE("Big Endian: %s", bigEndian?"true":"false");
  502. LPD3DXBUFFER code;
  503. LPD3DXBUFFER errorMsg;
  504. LPD3DXCONSTANTTABLE constantTable;
  505. HRESULT hr = D3DXCompileShader(_code.c_str()
  506. , _code.size()
  507. , NULL
  508. , NULL
  509. , "main"
  510. , profile
  511. , flags
  512. , &code
  513. , &errorMsg
  514. , &constantTable
  515. );
  516. if (FAILED(hr)
  517. || werror && NULL != errorMsg)
  518. {
  519. printCode(_code.c_str() );
  520. fprintf(stderr, "Error: 0x%08x %s\n", hr, errorMsg->GetBufferPointer() );
  521. return false;
  522. }
  523. D3DXCONSTANTTABLE_DESC desc;
  524. hr = constantTable->GetDesc(&desc);
  525. if (FAILED(hr) )
  526. {
  527. fprintf(stderr, "Error 0x%08x\n", hr);
  528. return false;
  529. }
  530. BX_TRACE("Creator: %s 0x%08x", desc.Creator, desc.Version);
  531. BX_TRACE("Num constants: %d", desc.Constants);
  532. BX_TRACE("# cl ty RxC S By Name");
  533. UniformArray uniforms;
  534. for (uint32_t ii = 0; ii < desc.Constants; ++ii)
  535. {
  536. D3DXHANDLE handle = constantTable->GetConstant(NULL, ii);
  537. D3DXCONSTANT_DESC constDesc;
  538. uint32_t count;
  539. constantTable->GetConstantDesc(handle, &constDesc, &count);
  540. BX_TRACE("%3d %2d %2d [%dx%d] %d %3d %s[%d] c%d (%d)"
  541. , ii
  542. , constDesc.Class
  543. , constDesc.Type
  544. , constDesc.Rows
  545. , constDesc.Columns
  546. , constDesc.StructMembers
  547. , constDesc.Bytes
  548. , constDesc.Name
  549. , constDesc.Elements
  550. , constDesc.RegisterIndex
  551. , constDesc.RegisterCount
  552. );
  553. ConstantType::Enum type = findConstantTypeDx9(constDesc);
  554. if (ConstantType::Count != type)
  555. {
  556. Uniform un;
  557. un.name = '$' == constDesc.Name[0] ? constDesc.Name+1 : constDesc.Name;
  558. un.type = type;
  559. un.num = constDesc.Elements;
  560. un.regIndex = constDesc.RegisterIndex;
  561. un.regCount = constDesc.RegisterCount;
  562. uniforms.push_back(un);
  563. }
  564. }
  565. uint16_t count = (uint16_t)uniforms.size();
  566. _stream.write(count);
  567. uint32_t fragmentBit = profile[0] == 'p' ? ConstantType::FragmentBit : 0;
  568. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  569. {
  570. const Uniform& un = *it;
  571. uint8_t nameSize = (uint8_t)un.name.size();
  572. _stream.write(nameSize);
  573. _stream.write(un.name.c_str(), nameSize);
  574. _stream.write<uint8_t>(un.type|fragmentBit);
  575. _stream.write(un.num);
  576. _stream.write(un.regIndex);
  577. _stream.write(un.regCount);
  578. BX_TRACE("%s, %s, %d, %d, %d"
  579. , un.name.c_str()
  580. , s_constantTypeName[un.type]
  581. , un.num
  582. , un.regIndex
  583. , un.regCount
  584. );
  585. }
  586. uint16_t shaderSize = (uint16_t)code->GetBufferSize();
  587. _stream.write(shaderSize);
  588. _stream.write(code->GetBufferPointer(), shaderSize);
  589. uint8_t nul = 0;
  590. _stream.write(nul);
  591. if (NULL != code)
  592. {
  593. code->Release();
  594. }
  595. if (NULL != errorMsg)
  596. {
  597. errorMsg->Release();
  598. }
  599. if (NULL != constantTable)
  600. {
  601. constantTable->Release();
  602. }
  603. return true;
  604. #else
  605. fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
  606. return false;
  607. #endif // BX_PLATFORM_WINDOWS
  608. }
  609. bool compileHLSLShaderDx11(CommandLine& _cmdLine, const std::string& _code, IStreamWriter& _stream)
  610. {
  611. #if BX_PLATFORM_WINDOWS
  612. const char* profile = _cmdLine.findOption('p');
  613. if (NULL == profile)
  614. {
  615. printf("Shader profile must be specified.\n");
  616. return false;
  617. }
  618. bool bigEndian = _cmdLine.hasArg('\0', "xbox360");
  619. uint32_t flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  620. flags |= _cmdLine.hasArg('\0', "debug") ? D3DCOMPILE_DEBUG : 0;
  621. flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DCOMPILE_AVOID_FLOW_CONTROL : 0;
  622. flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DCOMPILE_NO_PRESHADER : 0;
  623. flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DCOMPILE_PARTIAL_PRECISION : 0;
  624. flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DCOMPILE_PREFER_FLOW_CONTROL : 0;
  625. flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY : 0;
  626. bool werror = _cmdLine.hasArg('\0', "Werror");
  627. if (werror)
  628. {
  629. flags |= D3DCOMPILE_WARNINGS_ARE_ERRORS;
  630. }
  631. uint32_t optimization = 3;
  632. if (_cmdLine.hasArg(optimization, 'O') )
  633. {
  634. optimization = bx::uint32_min(optimization, countof(s_optimizationLevelDx11)-1);
  635. flags |= s_optimizationLevelDx11[optimization];
  636. }
  637. else
  638. {
  639. flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  640. }
  641. BX_TRACE("Profile: %s", profile);
  642. BX_TRACE("Flags: 0x%08x", flags);
  643. BX_TRACE("Big Endian: %s", bigEndian?"true":"false");
  644. ID3DBlob* code;
  645. ID3DBlob* errorMsg;
  646. HRESULT hr = D3DCompile(_code.c_str()
  647. , _code.size()
  648. , NULL
  649. , NULL
  650. , NULL
  651. , "main"
  652. , profile
  653. , flags
  654. , 0
  655. , &code
  656. , &errorMsg
  657. );
  658. if (FAILED(hr)
  659. || werror && NULL != errorMsg)
  660. {
  661. printCode(_code.c_str() );
  662. fprintf(stderr, BX_FILE_LINE_LITERAL "Error: 0x%08x %s\n", hr, errorMsg->GetBufferPointer() );
  663. return false;
  664. }
  665. UniformArray uniforms;
  666. ID3D11ShaderReflection* reflect = NULL;
  667. hr = D3DReflect(code->GetBufferPointer()
  668. , code->GetBufferSize()
  669. , IID_ID3D11ShaderReflection
  670. , (void**)&reflect
  671. );
  672. if (FAILED(hr) )
  673. {
  674. fprintf(stderr, BX_FILE_LINE_LITERAL "Error: 0x%08x\n", hr);
  675. return false;
  676. }
  677. D3D11_SHADER_DESC desc;
  678. hr = reflect->GetDesc(&desc);
  679. if (FAILED(hr) )
  680. {
  681. fprintf(stderr, BX_FILE_LINE_LITERAL "Error: 0x%08x\n", hr);
  682. return false;
  683. }
  684. BX_TRACE("Creator: %s 0x%08x", desc.Creator, desc.Version);
  685. BX_TRACE("Num constant buffers: %d", desc.ConstantBuffers);
  686. BX_TRACE("Input:");
  687. uint8_t numAttr = (uint8_t)desc.InputParameters;
  688. _stream.write(numAttr);
  689. for (uint32_t ii = 0; ii < numAttr; ++ii)
  690. {
  691. D3D11_SIGNATURE_PARAMETER_DESC spd;
  692. reflect->GetInputParameterDesc(ii, &spd);
  693. BX_TRACE("\t%2d: %s%d, %d, %d, %x, %d"
  694. , ii
  695. , spd.SemanticName
  696. , spd.SemanticIndex
  697. , spd.SystemValueType
  698. , spd.ComponentType
  699. , spd.Mask
  700. , spd.Register
  701. );
  702. uint8_t semanticIndex = spd.SemanticIndex;
  703. _stream.write(semanticIndex);
  704. uint8_t len = (uint8_t)strlen(spd.SemanticName);
  705. _stream.write(len);
  706. _stream.write(spd.SemanticName);
  707. }
  708. BX_TRACE("Output:");
  709. for (uint32_t ii = 0; ii < desc.OutputParameters; ++ii)
  710. {
  711. D3D11_SIGNATURE_PARAMETER_DESC spd;
  712. reflect->GetOutputParameterDesc(ii, &spd);
  713. BX_TRACE("\t%2d: %s%d, %d, %d", ii, spd.SemanticName, spd.SemanticIndex, spd.SystemValueType, spd.ComponentType);
  714. }
  715. uint16_t size = 0;
  716. for (uint32_t ii = 0; ii < bx::uint32_min(1, desc.ConstantBuffers); ++ii)
  717. {
  718. ID3D11ShaderReflectionConstantBuffer* cbuffer = reflect->GetConstantBufferByIndex(ii);
  719. D3D11_SHADER_BUFFER_DESC bufferDesc;
  720. hr = cbuffer->GetDesc(&bufferDesc);
  721. size = (uint16_t)bufferDesc.Size;
  722. if (SUCCEEDED(hr) )
  723. {
  724. BX_TRACE("%s, %d, vars %d, size %d"
  725. , bufferDesc.Name
  726. , bufferDesc.Type
  727. , bufferDesc.Variables
  728. , bufferDesc.Size
  729. );
  730. for (uint32_t jj = 0; jj < bufferDesc.Variables; ++jj)
  731. {
  732. ID3D11ShaderReflectionVariable* var = cbuffer->GetVariableByIndex(jj);
  733. ID3D11ShaderReflectionType* type = var->GetType();
  734. D3D11_SHADER_VARIABLE_DESC varDesc;
  735. hr = var->GetDesc(&varDesc);
  736. if (SUCCEEDED(hr) )
  737. {
  738. D3D11_SHADER_TYPE_DESC constDesc;
  739. hr = type->GetDesc(&constDesc);
  740. if (SUCCEEDED(hr) )
  741. {
  742. ConstantType::Enum type = findConstantTypeDx11(constDesc, varDesc.Size);
  743. if (ConstantType::Count != type
  744. && 0 != (varDesc.uFlags & D3D_SVF_USED) )
  745. {
  746. Uniform un;
  747. un.name = varDesc.Name;
  748. un.type = type;
  749. un.num = constDesc.Elements;
  750. un.regIndex = varDesc.StartOffset;
  751. un.regCount = varDesc.Size;
  752. uniforms.push_back(un);
  753. BX_TRACE("\t%s, %d, size %d, flags 0x%08x, %d"
  754. , varDesc.Name
  755. , varDesc.StartOffset
  756. , varDesc.Size
  757. , varDesc.uFlags
  758. , type
  759. );
  760. }
  761. }
  762. }
  763. }
  764. }
  765. }
  766. BX_TRACE("Bound:");
  767. for (uint32_t ii = 0; ii < desc.BoundResources; ++ii)
  768. {
  769. D3D11_SHADER_INPUT_BIND_DESC bindDesc;
  770. hr = reflect->GetResourceBindingDesc(ii, &bindDesc);
  771. if (SUCCEEDED(hr) )
  772. {
  773. // if (bindDesc.Type == D3D_SIT_SAMPLER)
  774. {
  775. BX_TRACE("\t%s, %d, %d, %d"
  776. , bindDesc.Name
  777. , bindDesc.Type
  778. , bindDesc.BindPoint
  779. , bindDesc.BindCount
  780. );
  781. }
  782. }
  783. }
  784. uint16_t count = (uint16_t)uniforms.size();
  785. _stream.write(count);
  786. _stream.write(size);
  787. uint32_t fragmentBit = profile[0] == 'p' ? ConstantType::FragmentBit : 0;
  788. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  789. {
  790. const Uniform& un = *it;
  791. uint8_t nameSize = (uint8_t)un.name.size();
  792. _stream.write(nameSize);
  793. _stream.write(un.name.c_str(), nameSize);
  794. _stream.write<uint8_t>(un.type|fragmentBit);
  795. _stream.write(un.num);
  796. _stream.write(un.regIndex);
  797. _stream.write(un.regCount);
  798. BX_TRACE("%s, %s, %d, %d, %d"
  799. , un.name.c_str()
  800. , s_constantTypeName[un.type]
  801. , un.num
  802. , un.regIndex
  803. , un.regCount
  804. );
  805. }
  806. uint16_t shaderSize = (uint16_t)code->GetBufferSize();
  807. _stream.write(shaderSize);
  808. _stream.write(code->GetBufferPointer(), shaderSize);
  809. uint8_t nul = 0;
  810. _stream.write(nul);
  811. if (NULL != reflect)
  812. {
  813. reflect->Release();
  814. }
  815. if (NULL != code)
  816. {
  817. code->Release();
  818. }
  819. if (NULL != errorMsg)
  820. {
  821. errorMsg->Release();
  822. }
  823. return true;
  824. #else
  825. fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
  826. return false;
  827. #endif // BX_PLATFORM_WINDOWS
  828. }
  829. struct Preprocessor
  830. {
  831. Preprocessor(const char* _filePath)
  832. : m_tagptr(m_tags)
  833. , m_scratchPos(0)
  834. , m_fgetsPos(0)
  835. {
  836. m_filePath = scratch(_filePath);
  837. m_tagptr->tag = FPPTAG_USERDATA;
  838. m_tagptr->data = this;
  839. m_tagptr++;
  840. m_tagptr->tag = FPPTAG_DEPENDS;
  841. m_tagptr->data = (void*)fppDepends;
  842. m_tagptr++;
  843. m_tagptr->tag = FPPTAG_INPUT;
  844. m_tagptr->data = (void*)fppInput;
  845. m_tagptr++;
  846. m_tagptr->tag = FPPTAG_OUTPUT;
  847. m_tagptr->data = (void*)fppOutput;
  848. m_tagptr++;
  849. m_tagptr->tag = FPPTAG_ERROR;
  850. m_tagptr->data = (void*)fppError;
  851. m_tagptr++;
  852. m_tagptr->tag = FPPTAG_IGNOREVERSION;
  853. m_tagptr->data = (void*)0;
  854. m_tagptr++;
  855. m_tagptr->tag = FPPTAG_LINE;
  856. m_tagptr->data = (void*)0;
  857. m_tagptr++;
  858. m_tagptr->tag = FPPTAG_INPUT_NAME;
  859. m_tagptr->data = m_filePath;
  860. m_tagptr++;
  861. m_default = "#define lowp\n#define mediump\n#define highp\n";
  862. }
  863. void setDefine(const char* _define)
  864. {
  865. m_tagptr->tag = FPPTAG_DEFINE;
  866. m_tagptr->data = scratch(_define);
  867. m_tagptr++;
  868. }
  869. void setDefaultDefine(const char* _name)
  870. {
  871. char temp[1024];
  872. _snprintf(temp, countof(temp)
  873. , "#ifndef %s\n"
  874. "# define %s 0\n"
  875. "#endif // %s\n"
  876. "\n"
  877. , _name
  878. , _name
  879. , _name
  880. );
  881. m_default += temp;
  882. }
  883. void addDependency(const char* _fileName)
  884. {
  885. m_depends += " \\\n ";
  886. m_depends += _fileName;
  887. }
  888. bool run()
  889. {
  890. m_fgetsPos = 0;
  891. FILE* file = fopen(m_filePath, "r");
  892. if (NULL == file)
  893. {
  894. return false;
  895. }
  896. long int size = fsize(file);
  897. char* input = new char[size+1];
  898. size = fread(input, 1, size, file);
  899. input[size] = '\0';
  900. fclose(file);
  901. m_input = m_default;
  902. m_input += input;
  903. fppTag* tagptr = m_tagptr;
  904. tagptr->tag = FPPTAG_END;
  905. tagptr->data = 0;
  906. tagptr++;
  907. int result = fppPreProcess(m_tags);
  908. return 0 == result;
  909. }
  910. char* fgets(char* _buffer, int _size)
  911. {
  912. int ii = 0;
  913. for (char ch = m_input[m_fgetsPos]; m_fgetsPos < m_input.size() && ii < _size-1; ch = m_input[++m_fgetsPos])
  914. {
  915. _buffer[ii++] = ch;
  916. if (ch == '\n' || ii == _size)
  917. {
  918. _buffer[ii] = '\0';
  919. m_fgetsPos++;
  920. return _buffer;
  921. }
  922. }
  923. return NULL;
  924. }
  925. static void fppDepends(char* _fileName, void* _userData)
  926. {
  927. Preprocessor* thisClass = (Preprocessor*)_userData;
  928. thisClass->addDependency(_fileName);
  929. }
  930. static char* fppInput(char* _buffer, int _size, void* _userData)
  931. {
  932. Preprocessor* thisClass = (Preprocessor*)_userData;
  933. return thisClass->fgets(_buffer, _size);
  934. }
  935. static void fppOutput(int _ch, void* _userData)
  936. {
  937. Preprocessor* thisClass = (Preprocessor*)_userData;
  938. thisClass->m_preprocessed += _ch;
  939. }
  940. static void fppError(void* _userData, char* _format, va_list _vargs)
  941. {
  942. vfprintf(stderr, _format, _vargs);
  943. }
  944. char* scratch(const char* _str)
  945. {
  946. char* result = &m_scratch[m_scratchPos];
  947. strcpy(result, _str);
  948. m_scratchPos += strlen(_str)+1;
  949. return result;
  950. }
  951. fppTag m_tags[MAX_TAGS];
  952. fppTag* m_tagptr;
  953. char* m_filePath;
  954. std::string m_depends;
  955. std::string m_default;
  956. std::string m_input;
  957. std::string m_preprocessed;
  958. char m_scratch[16<<10];
  959. uint32_t m_scratchPos;
  960. uint32_t m_fgetsPos;
  961. };
  962. const char* baseName(const char* _filePath)
  963. {
  964. const char* bs = strrchr(_filePath, '\\');
  965. const char* fs = strrchr(_filePath, '/');
  966. const char* column = strrchr(_filePath, ':');
  967. const char* basename = std::max(std::max(bs, fs), column);
  968. if (NULL != basename)
  969. {
  970. return basename+1;
  971. }
  972. return _filePath;
  973. }
  974. // OpenGL #version Features Direct3D Features Shader Model
  975. // 2.1 120 vf 9.0 vf 2.0
  976. // 3.0 130
  977. // 3.1 140
  978. // 3.2 150 vgf
  979. // 3.3 330 10.0 vgf 4.0
  980. // 4.0 400 vhdgf
  981. // 4.1 410
  982. // 4.2 420 11.0 vhdgf 5.0
  983. int main(int _argc, const char* _argv[])
  984. {
  985. CommandLine cmdLine(_argc, _argv);
  986. const char* filePath = cmdLine.findOption('f');
  987. if (NULL == filePath)
  988. {
  989. fprintf(stderr, "Shader file name must be specified.\n");
  990. return EXIT_FAILURE;
  991. }
  992. const char* outFilePath = cmdLine.findOption('o');
  993. if (NULL == outFilePath)
  994. {
  995. fprintf(stderr, "Output file name must be specified.\n");
  996. return EXIT_FAILURE;
  997. }
  998. const char* type = cmdLine.findOption('\0', "type");
  999. if (NULL == type)
  1000. {
  1001. fprintf(stderr, "Must specify shader type.");
  1002. return EXIT_FAILURE;
  1003. }
  1004. const char* platform = cmdLine.findOption('\0', "platform");
  1005. if (NULL == platform)
  1006. {
  1007. fprintf(stderr, "Must specify platform.\n");
  1008. return EXIT_FAILURE;
  1009. }
  1010. uint32_t hlsl = 2;
  1011. const char* profile = cmdLine.findOption('p');
  1012. if (NULL != profile)
  1013. {
  1014. if (0 == strncmp(&profile[1], "s_3", 3) )
  1015. {
  1016. hlsl = 3;
  1017. }
  1018. else if (0 == strncmp(&profile[1], "s_4", 3) )
  1019. {
  1020. hlsl = 4;
  1021. }
  1022. else if (0 == strncmp(&profile[1], "s_5", 3) )
  1023. {
  1024. hlsl = 5;
  1025. }
  1026. }
  1027. const char* bin2c = NULL;
  1028. if (cmdLine.hasArg("bin2c") )
  1029. {
  1030. bin2c = cmdLine.findOption("bin2c");
  1031. if (NULL == bin2c)
  1032. {
  1033. bin2c = baseName(outFilePath);
  1034. uint32_t len = strlen(bin2c);
  1035. char* temp = (char*)alloca(len+1);
  1036. for (char *out = temp; *bin2c != '\0';)
  1037. {
  1038. char ch = *bin2c++;
  1039. if (isalnum(ch) )
  1040. {
  1041. *out++ = ch;
  1042. }
  1043. else
  1044. {
  1045. *out++ = '_';
  1046. }
  1047. }
  1048. temp[len] = '\0';
  1049. bin2c = temp;
  1050. }
  1051. }
  1052. bool depends = cmdLine.hasArg("depends");
  1053. bool preprocessOnly = cmdLine.hasArg("preprocess");
  1054. Preprocessor preprocessor(filePath);
  1055. preprocessor.setDefaultDefine("BX_PLATFORM_ANDROID");
  1056. preprocessor.setDefaultDefine("BX_PLATFORM_IOS");
  1057. preprocessor.setDefaultDefine("BX_PLATFORM_LINUX");
  1058. preprocessor.setDefaultDefine("BX_PLATFORM_NACL");
  1059. preprocessor.setDefaultDefine("BX_PLATFORM_OSX");
  1060. preprocessor.setDefaultDefine("BX_PLATFORM_WINDOWS");
  1061. preprocessor.setDefaultDefine("BX_PLATFORM_XBOX360");
  1062. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_GLSL");
  1063. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_HLSL");
  1064. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_FRAGMENT");
  1065. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_VERTEX");
  1066. bool glsl = false;
  1067. if (0 == _stricmp(platform, "android") )
  1068. {
  1069. preprocessor.setDefine("BX_PLATFORM_ANDROID=1");
  1070. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1071. glsl = true;
  1072. }
  1073. else if (0 == _stricmp(platform, "ios") )
  1074. {
  1075. preprocessor.setDefine("BX_PLATFORM_IOS=1");
  1076. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1077. glsl = true;
  1078. }
  1079. else if (0 == _stricmp(platform, "linux") )
  1080. {
  1081. preprocessor.setDefine("BX_PLATFORM_IOS=1");
  1082. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1083. glsl = true;
  1084. }
  1085. else if (0 == _stricmp(platform, "nacl") )
  1086. {
  1087. preprocessor.setDefine("BX_PLATFORM_NACL=1");
  1088. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1089. glsl = true;
  1090. }
  1091. else if (0 == _stricmp(platform, "osx") )
  1092. {
  1093. preprocessor.setDefine("BX_PLATFORM_OSX=1");
  1094. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1095. glsl = true;
  1096. }
  1097. else if (0 == _stricmp(platform, "windows") )
  1098. {
  1099. preprocessor.setDefine("BX_PLATFORM_WINDOWS=1");
  1100. char temp[256];
  1101. _snprintf(temp, sizeof(temp), "BGFX_SHADER_LANGUAGE_HLSL=%d", hlsl);
  1102. preprocessor.setDefine(temp);
  1103. }
  1104. else if (0 == _stricmp(platform, "xbox360") )
  1105. {
  1106. preprocessor.setDefine("BX_PLATFORM_XBOX360=1");
  1107. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=3");
  1108. }
  1109. else
  1110. {
  1111. fprintf(stderr, "Unknown platform %s?!", platform);
  1112. return EXIT_FAILURE;
  1113. }
  1114. switch (tolower(type[0]) )
  1115. {
  1116. case 'f':
  1117. preprocessor.setDefine("BGFX_SHADER_TYPE_FRAGMENT=1");
  1118. break;
  1119. case 'v':
  1120. preprocessor.setDefine("BGFX_SHADER_TYPE_VERTEX=1");
  1121. break;
  1122. default:
  1123. fprintf(stderr, "Unknown type: %s?!", type);
  1124. return EXIT_FAILURE;
  1125. }
  1126. if (preprocessor.run() )
  1127. {
  1128. BX_TRACE("Input file: %s", filePath);
  1129. BX_TRACE("Output file: %s", outFilePath);
  1130. if (preprocessOnly)
  1131. {
  1132. FileWriter stream(outFilePath);
  1133. if (!stream.open() )
  1134. {
  1135. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  1136. return false;
  1137. }
  1138. if (glsl)
  1139. {
  1140. const char* profile = cmdLine.findOption('p');
  1141. if (NULL == profile)
  1142. {
  1143. stream.write("#ifdef GL_ES\n");
  1144. stream.write("precision highp float;\n");
  1145. stream.write("#endif // GL_ES\n\n");
  1146. }
  1147. else
  1148. {
  1149. stream.writef("#version %s\n\n", profile);
  1150. }
  1151. }
  1152. stream.write(preprocessor.m_preprocessed.c_str(), preprocessor.m_preprocessed.size() );
  1153. stream.close();
  1154. return EXIT_SUCCESS;
  1155. }
  1156. bool compiled = false;
  1157. {
  1158. IStreamWriter* stream = NULL;
  1159. if (NULL != bin2c)
  1160. {
  1161. stream = new Bin2cStream(outFilePath, bin2c);
  1162. }
  1163. else
  1164. {
  1165. stream = new FileWriter(outFilePath);
  1166. }
  1167. if (!stream->open() )
  1168. {
  1169. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  1170. return false;
  1171. }
  1172. if (glsl)
  1173. {
  1174. compiled = compileGLSLShader(cmdLine, preprocessor.m_preprocessed, *stream);
  1175. }
  1176. else
  1177. {
  1178. if (hlsl > 3)
  1179. {
  1180. compiled = compileHLSLShaderDx11(cmdLine, preprocessor.m_preprocessed, *stream);
  1181. }
  1182. else
  1183. {
  1184. compiled = compileHLSLShaderDx9(cmdLine, preprocessor.m_preprocessed, *stream);
  1185. }
  1186. }
  1187. #if SHADERC_DEBUG
  1188. stream->writeString(filePath);
  1189. #endif // SHADERC_DEBUG
  1190. stream->close();
  1191. delete stream;
  1192. }
  1193. if (compiled)
  1194. {
  1195. if (depends)
  1196. {
  1197. std::string ofp = outFilePath;
  1198. ofp += ".d";
  1199. FileWriter stream(ofp.c_str() );
  1200. if (stream.open() )
  1201. {
  1202. stream.write(outFilePath);
  1203. stream.write(":");
  1204. stream.write(preprocessor.m_depends.c_str() );
  1205. stream.write("\n");
  1206. stream.close();
  1207. }
  1208. }
  1209. return EXIT_SUCCESS;
  1210. }
  1211. }
  1212. fprintf(stderr, "Failed to build shader.\n");
  1213. return EXIT_FAILURE;
  1214. }