shaderc.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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 & D3D10_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. uint16_t count = (uint16_t)uniforms.size();
  767. _stream.write(count);
  768. _stream.write(size);
  769. uint32_t fragmentBit = profile[0] == 'p' ? ConstantType::FragmentBit : 0;
  770. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  771. {
  772. const Uniform& un = *it;
  773. uint8_t nameSize = (uint8_t)un.name.size();
  774. _stream.write(nameSize);
  775. _stream.write(un.name.c_str(), nameSize);
  776. _stream.write<uint8_t>(un.type|fragmentBit);
  777. _stream.write(un.num);
  778. _stream.write(un.regIndex);
  779. _stream.write(un.regCount);
  780. BX_TRACE("%s, %s, %d, %d, %d"
  781. , un.name.c_str()
  782. , s_constantTypeName[un.type]
  783. , un.num
  784. , un.regIndex
  785. , un.regCount
  786. );
  787. }
  788. uint16_t shaderSize = (uint16_t)code->GetBufferSize();
  789. _stream.write(shaderSize);
  790. _stream.write(code->GetBufferPointer(), shaderSize);
  791. uint8_t nul = 0;
  792. _stream.write(nul);
  793. if (NULL != reflect)
  794. {
  795. reflect->Release();
  796. }
  797. if (NULL != code)
  798. {
  799. code->Release();
  800. }
  801. if (NULL != errorMsg)
  802. {
  803. errorMsg->Release();
  804. }
  805. return true;
  806. #else
  807. fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
  808. return false;
  809. #endif // BX_PLATFORM_WINDOWS
  810. }
  811. struct Preprocessor
  812. {
  813. Preprocessor(const char* _filePath)
  814. : m_tagptr(m_tags)
  815. , m_scratchPos(0)
  816. , m_fgetsPos(0)
  817. {
  818. m_filePath = scratch(_filePath);
  819. m_tagptr->tag = FPPTAG_USERDATA;
  820. m_tagptr->data = this;
  821. m_tagptr++;
  822. m_tagptr->tag = FPPTAG_DEPENDS;
  823. m_tagptr->data = (void*)fppDepends;
  824. m_tagptr++;
  825. m_tagptr->tag = FPPTAG_INPUT;
  826. m_tagptr->data = (void*)fppInput;
  827. m_tagptr++;
  828. m_tagptr->tag = FPPTAG_OUTPUT;
  829. m_tagptr->data = (void*)fppOutput;
  830. m_tagptr++;
  831. m_tagptr->tag = FPPTAG_ERROR;
  832. m_tagptr->data = (void*)fppError;
  833. m_tagptr++;
  834. m_tagptr->tag = FPPTAG_IGNOREVERSION;
  835. m_tagptr->data = (void*)0;
  836. m_tagptr++;
  837. m_tagptr->tag = FPPTAG_LINE;
  838. m_tagptr->data = (void*)0;
  839. m_tagptr++;
  840. m_tagptr->tag = FPPTAG_INPUT_NAME;
  841. m_tagptr->data = m_filePath;
  842. m_tagptr++;
  843. m_default = "#define lowp\n#define mediump\n#define highp\n";
  844. }
  845. void setDefine(const char* _define)
  846. {
  847. m_tagptr->tag = FPPTAG_DEFINE;
  848. m_tagptr->data = scratch(_define);
  849. m_tagptr++;
  850. }
  851. void setDefaultDefine(const char* _name)
  852. {
  853. char temp[1024];
  854. _snprintf(temp, countof(temp)
  855. , "#ifndef %s\n"
  856. "# define %s 0\n"
  857. "#endif // %s\n"
  858. "\n"
  859. , _name
  860. , _name
  861. , _name
  862. );
  863. m_default += temp;
  864. }
  865. void addDependency(const char* _fileName)
  866. {
  867. m_depends += " \\\n ";
  868. m_depends += _fileName;
  869. }
  870. bool run()
  871. {
  872. m_fgetsPos = 0;
  873. FILE* file = fopen(m_filePath, "r");
  874. if (NULL == file)
  875. {
  876. return false;
  877. }
  878. long int size = fsize(file);
  879. char* input = new char[size+1];
  880. size = fread(input, 1, size, file);
  881. input[size] = '\0';
  882. fclose(file);
  883. m_input = m_default;
  884. m_input += input;
  885. fppTag* tagptr = m_tagptr;
  886. tagptr->tag = FPPTAG_END;
  887. tagptr->data = 0;
  888. tagptr++;
  889. int result = fppPreProcess(m_tags);
  890. return 0 == result;
  891. }
  892. char* fgets(char* _buffer, int _size)
  893. {
  894. int ii = 0;
  895. for (char ch = m_input[m_fgetsPos]; m_fgetsPos < m_input.size() && ii < _size-1; ch = m_input[++m_fgetsPos])
  896. {
  897. _buffer[ii++] = ch;
  898. if (ch == '\n' || ii == _size)
  899. {
  900. _buffer[ii] = '\0';
  901. m_fgetsPos++;
  902. return _buffer;
  903. }
  904. }
  905. return NULL;
  906. }
  907. static void fppDepends(char* _fileName, void* _userData)
  908. {
  909. Preprocessor* thisClass = (Preprocessor*)_userData;
  910. thisClass->addDependency(_fileName);
  911. }
  912. static char* fppInput(char* _buffer, int _size, void* _userData)
  913. {
  914. Preprocessor* thisClass = (Preprocessor*)_userData;
  915. return thisClass->fgets(_buffer, _size);
  916. }
  917. static void fppOutput(int _ch, void* _userData)
  918. {
  919. Preprocessor* thisClass = (Preprocessor*)_userData;
  920. thisClass->m_preprocessed += _ch;
  921. }
  922. static void fppError(void* _userData, char* _format, va_list _vargs)
  923. {
  924. vfprintf(stderr, _format, _vargs);
  925. }
  926. char* scratch(const char* _str)
  927. {
  928. char* result = &m_scratch[m_scratchPos];
  929. strcpy(result, _str);
  930. m_scratchPos += strlen(_str)+1;
  931. return result;
  932. }
  933. fppTag m_tags[MAX_TAGS];
  934. fppTag* m_tagptr;
  935. char* m_filePath;
  936. std::string m_depends;
  937. std::string m_default;
  938. std::string m_input;
  939. std::string m_preprocessed;
  940. char m_scratch[16<<10];
  941. uint32_t m_scratchPos;
  942. uint32_t m_fgetsPos;
  943. };
  944. const char* baseName(const char* _filePath)
  945. {
  946. const char* bs = strrchr(_filePath, '\\');
  947. const char* fs = strrchr(_filePath, '/');
  948. const char* column = strrchr(_filePath, ':');
  949. const char* basename = std::max(std::max(bs, fs), column);
  950. if (NULL != basename)
  951. {
  952. return basename+1;
  953. }
  954. return _filePath;
  955. }
  956. // OpenGL #version Features Direct3D Features Shader Model
  957. // 2.1 120 vf 9.0 vf 2.0
  958. // 3.0 130
  959. // 3.1 140
  960. // 3.2 150 vgf
  961. // 3.3 330 10.0 vgf 4.0
  962. // 4.0 400 vhdgf
  963. // 4.1 410
  964. // 4.2 420 11.0 vhdgf 5.0
  965. int main(int _argc, const char* _argv[])
  966. {
  967. CommandLine cmdLine(_argc, _argv);
  968. const char* filePath = cmdLine.findOption('f');
  969. if (NULL == filePath)
  970. {
  971. fprintf(stderr, "Shader file name must be specified.\n");
  972. return EXIT_FAILURE;
  973. }
  974. const char* outFilePath = cmdLine.findOption('o');
  975. if (NULL == outFilePath)
  976. {
  977. fprintf(stderr, "Output file name must be specified.\n");
  978. return EXIT_FAILURE;
  979. }
  980. const char* type = cmdLine.findOption('\0', "type");
  981. if (NULL == type)
  982. {
  983. fprintf(stderr, "Must specify shader type.");
  984. return EXIT_FAILURE;
  985. }
  986. const char* platform = cmdLine.findOption('\0', "platform");
  987. if (NULL == platform)
  988. {
  989. fprintf(stderr, "Must specify platform.\n");
  990. return EXIT_FAILURE;
  991. }
  992. const char* bin2c = NULL;
  993. if (cmdLine.hasArg("bin2c") )
  994. {
  995. bin2c = cmdLine.findOption("bin2c");
  996. if (NULL == bin2c)
  997. {
  998. bin2c = baseName(outFilePath);
  999. uint32_t len = strlen(bin2c);
  1000. char* temp = (char*)alloca(len+1);
  1001. for (char *out = temp; *bin2c != '\0';)
  1002. {
  1003. char ch = *bin2c++;
  1004. if (isalnum(ch) )
  1005. {
  1006. *out++ = ch;
  1007. }
  1008. else
  1009. {
  1010. *out++ = '_';
  1011. }
  1012. }
  1013. temp[len] = '\0';
  1014. bin2c = temp;
  1015. }
  1016. }
  1017. bool depends = cmdLine.hasArg("depends");
  1018. bool preprocessOnly = cmdLine.hasArg("preprocess");
  1019. Preprocessor preprocessor(filePath);
  1020. preprocessor.setDefaultDefine("BX_PLATFORM_ANDROID");
  1021. preprocessor.setDefaultDefine("BX_PLATFORM_IOS");
  1022. preprocessor.setDefaultDefine("BX_PLATFORM_LINUX");
  1023. preprocessor.setDefaultDefine("BX_PLATFORM_NACL");
  1024. preprocessor.setDefaultDefine("BX_PLATFORM_OSX");
  1025. preprocessor.setDefaultDefine("BX_PLATFORM_WINDOWS");
  1026. preprocessor.setDefaultDefine("BX_PLATFORM_XBOX360");
  1027. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_GLSL");
  1028. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_HLSL");
  1029. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_FRAGMENT");
  1030. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_VERTEX");
  1031. bool glsl = false;
  1032. if (0 == _stricmp(platform, "android") )
  1033. {
  1034. preprocessor.setDefine("BX_PLATFORM_ANDROID=1");
  1035. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1036. glsl = true;
  1037. }
  1038. else if (0 == _stricmp(platform, "ios") )
  1039. {
  1040. preprocessor.setDefine("BX_PLATFORM_IOS=1");
  1041. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1042. glsl = true;
  1043. }
  1044. else if (0 == _stricmp(platform, "linux") )
  1045. {
  1046. preprocessor.setDefine("BX_PLATFORM_IOS=1");
  1047. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1048. glsl = true;
  1049. }
  1050. else if (0 == _stricmp(platform, "nacl") )
  1051. {
  1052. preprocessor.setDefine("BX_PLATFORM_NACL=1");
  1053. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1054. glsl = true;
  1055. }
  1056. else if (0 == _stricmp(platform, "osx") )
  1057. {
  1058. preprocessor.setDefine("BX_PLATFORM_OSX=1");
  1059. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1060. glsl = true;
  1061. }
  1062. else if (0 == _stricmp(platform, "windows") )
  1063. {
  1064. preprocessor.setDefine("BX_PLATFORM_WINDOWS=1");
  1065. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=1");
  1066. }
  1067. else if (0 == _stricmp(platform, "xbox360") )
  1068. {
  1069. preprocessor.setDefine("BX_PLATFORM_XBOX360=1");
  1070. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=1");
  1071. }
  1072. else
  1073. {
  1074. fprintf(stderr, "Unknown platform %s?!", platform);
  1075. return EXIT_FAILURE;
  1076. }
  1077. switch (tolower(type[0]) )
  1078. {
  1079. case 'f':
  1080. preprocessor.setDefine("BGFX_SHADER_TYPE_FRAGMENT=1");
  1081. break;
  1082. case 'v':
  1083. preprocessor.setDefine("BGFX_SHADER_TYPE_VERTEX=1");
  1084. break;
  1085. default:
  1086. fprintf(stderr, "Unknown type: %s?!", type);
  1087. return EXIT_FAILURE;
  1088. }
  1089. if (preprocessor.run() )
  1090. {
  1091. BX_TRACE("Input file: %s", filePath);
  1092. BX_TRACE("Output file: %s", outFilePath);
  1093. if (preprocessOnly)
  1094. {
  1095. FileWriter stream(outFilePath);
  1096. if (!stream.open() )
  1097. {
  1098. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  1099. return false;
  1100. }
  1101. if (glsl)
  1102. {
  1103. const char* profile = cmdLine.findOption('p');
  1104. if (NULL == profile)
  1105. {
  1106. stream.write("#ifdef GL_ES\n");
  1107. stream.write("precision highp float;\n");
  1108. stream.write("#endif // GL_ES\n\n");
  1109. }
  1110. else
  1111. {
  1112. stream.writef("#version %s\n\n", profile);
  1113. }
  1114. }
  1115. stream.write(preprocessor.m_preprocessed.c_str(), preprocessor.m_preprocessed.size() );
  1116. stream.close();
  1117. return EXIT_SUCCESS;
  1118. }
  1119. bool compiled = false;
  1120. {
  1121. IStreamWriter* stream = NULL;
  1122. if (NULL != bin2c)
  1123. {
  1124. stream = new Bin2cStream(outFilePath, bin2c);
  1125. }
  1126. else
  1127. {
  1128. stream = new FileWriter(outFilePath);
  1129. }
  1130. if (!stream->open() )
  1131. {
  1132. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  1133. return false;
  1134. }
  1135. if (glsl)
  1136. {
  1137. compiled = compileGLSLShader(cmdLine, preprocessor.m_preprocessed, *stream);
  1138. }
  1139. else
  1140. {
  1141. const char* profile = cmdLine.findOption('p');
  1142. if (0 == strncmp(&profile[1], "s_4", 3)
  1143. || 0 == strncmp(&profile[1], "s_5", 3) )
  1144. {
  1145. compiled = compileHLSLShaderDx11(cmdLine, preprocessor.m_preprocessed, *stream);
  1146. }
  1147. else
  1148. {
  1149. compiled = compileHLSLShaderDx9(cmdLine, preprocessor.m_preprocessed, *stream);
  1150. }
  1151. }
  1152. #if SHADERC_DEBUG
  1153. stream->writeString(filePath);
  1154. #endif // SHADERC_DEBUG
  1155. stream->close();
  1156. delete stream;
  1157. }
  1158. if (compiled)
  1159. {
  1160. if (depends)
  1161. {
  1162. std::string ofp = outFilePath;
  1163. ofp += ".d";
  1164. FileWriter stream(ofp.c_str() );
  1165. if (stream.open() )
  1166. {
  1167. stream.write(outFilePath);
  1168. stream.write(":");
  1169. stream.write(preprocessor.m_depends.c_str() );
  1170. stream.write("\n");
  1171. stream.close();
  1172. }
  1173. }
  1174. return EXIT_SUCCESS;
  1175. }
  1176. }
  1177. fprintf(stderr, "Failed to build shader.\n");
  1178. return EXIT_FAILURE;
  1179. }