shaderc.cpp 31 KB

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