shaderc.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  1. /*
  2. * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef SHADERC_DEBUG
  6. # define SHADERC_DEBUG 0
  7. #endif // SHADERC_DEBUG
  8. #define NOMINMAX
  9. #include <alloca.h>
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <string>
  15. #include <vector>
  16. #include <unordered_map>
  17. namespace std { namespace tr1 {} using namespace tr1; } // namespace std
  18. #define MAX_TAGS 256
  19. extern "C"
  20. {
  21. #include <fpp.h>
  22. } // extern "C"
  23. #if SHADERC_DEBUG
  24. # define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  25. #endif // DEBUG
  26. #define BX_NAMESPACE 1
  27. #include <bx/bx.h>
  28. #if BX_PLATFORM_LINUX
  29. # include <stdarg.h>
  30. # define _stricmp strcasecmp
  31. # define _snprintf snprintf
  32. #endif // BX_PLATFORM_LINUX
  33. #include <bx/commandline.h>
  34. #include <bx/countof.h>
  35. #include <bx/endian.h>
  36. #include <bx/uint32_t.h>
  37. #include "glsl_optimizer.h"
  38. #if BX_PLATFORM_WINDOWS
  39. # if BX_COMPILER_GCC
  40. # include <sal.h>
  41. # endif // BX_COMPILER_GCC
  42. # include <d3dx9.h>
  43. # include <d3dcompiler.h>
  44. #endif // BX_PLATFORM_WINDOWS
  45. long int fsize(FILE* _file)
  46. {
  47. long int pos = ftell(_file);
  48. fseek(_file, 0L, SEEK_END);
  49. long int size = ftell(_file);
  50. fseek(_file, pos, SEEK_SET);
  51. return size;
  52. }
  53. struct Attrib
  54. {
  55. enum Enum
  56. {
  57. Position = 0,
  58. Normal,
  59. Tangent,
  60. Color0,
  61. Color1,
  62. Indices,
  63. Weight,
  64. TexCoord0,
  65. TexCoord1,
  66. TexCoord2,
  67. TexCoord3,
  68. TexCoord4,
  69. TexCoord5,
  70. TexCoord6,
  71. TexCoord7,
  72. Count,
  73. };
  74. };
  75. struct RemapInputSemantic
  76. {
  77. Attrib::Enum m_attr;
  78. const char* m_name;
  79. uint8_t m_index;
  80. };
  81. static const RemapInputSemantic s_remapInputSemantic[Attrib::Count+1] =
  82. {
  83. { Attrib::Position, "POSITION", 0 },
  84. { Attrib::Normal, "NORMAL", 0 },
  85. { Attrib::Tangent, "TANGENT", 0 },
  86. { Attrib::Color0, "COLOR", 0 },
  87. { Attrib::Color1, "COLOR", 1 },
  88. { Attrib::Indices, "BLENDINDICES", 0 },
  89. { Attrib::Weight, "BLENDWEIGHT", 0 },
  90. { Attrib::TexCoord0, "TEXCOORD", 0 },
  91. { Attrib::TexCoord1, "TEXCOORD", 1 },
  92. { Attrib::TexCoord2, "TEXCOORD", 2 },
  93. { Attrib::TexCoord3, "TEXCOORD", 3 },
  94. { Attrib::TexCoord4, "TEXCOORD", 4 },
  95. { Attrib::TexCoord5, "TEXCOORD", 5 },
  96. { Attrib::TexCoord6, "TEXCOORD", 6 },
  97. { Attrib::TexCoord7, "TEXCOORD", 7 },
  98. { Attrib::Count, "", 0 },
  99. };
  100. const RemapInputSemantic& findInputSemantic(const char* _name, uint8_t _index)
  101. {
  102. for (uint32_t ii = 0; ii < Attrib::Count; ++ii)
  103. {
  104. const RemapInputSemantic& ris = s_remapInputSemantic[ii];
  105. if (0 == strcmp(ris.m_name, _name)
  106. && ris.m_index == _index)
  107. {
  108. return ris;
  109. }
  110. }
  111. return s_remapInputSemantic[Attrib::Count];
  112. }
  113. struct ConstantType
  114. {
  115. enum Enum
  116. {
  117. Uniform1i,
  118. Uniform1f,
  119. End,
  120. Uniform1iv,
  121. Uniform1fv,
  122. Uniform2fv,
  123. Uniform3fv,
  124. Uniform4fv,
  125. Uniform3x3fv,
  126. Uniform4x4fv,
  127. Count,
  128. TypeMask = 0x7f,
  129. FragmentBit = 0x80
  130. };
  131. };
  132. static const char* s_constantTypeName[ConstantType::Count] =
  133. {
  134. "int",
  135. "float",
  136. NULL,
  137. "int",
  138. "float",
  139. "float2",
  140. "float3",
  141. "float4",
  142. "float3x3",
  143. "float4x4",
  144. };
  145. struct Uniform
  146. {
  147. std::string name;
  148. ConstantType::Enum type;
  149. uint8_t num;
  150. uint16_t regIndex;
  151. uint16_t regCount;
  152. };
  153. typedef std::vector<Uniform> UniformArray;
  154. #if BX_PLATFORM_WINDOWS
  155. struct ConstRemapDx9
  156. {
  157. ConstantType::Enum id;
  158. D3DXPARAMETER_CLASS paramClass;
  159. D3DXPARAMETER_TYPE paramType;
  160. uint32_t paramBytes;
  161. };
  162. static const ConstRemapDx9 s_constRemapDx9[7] =
  163. {
  164. { ConstantType::Uniform1iv, D3DXPC_SCALAR, D3DXPT_INT, 4 },
  165. { ConstantType::Uniform1fv, D3DXPC_SCALAR, D3DXPT_FLOAT, 4 },
  166. { ConstantType::Uniform2fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 8 },
  167. { ConstantType::Uniform3fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 12 },
  168. { ConstantType::Uniform4fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 16 },
  169. { ConstantType::Uniform3x3fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 36 },
  170. { ConstantType::Uniform4x4fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 64 },
  171. };
  172. ConstantType::Enum findConstantTypeDx9(const D3DXCONSTANT_DESC& constDesc)
  173. {
  174. uint32_t count = sizeof(s_constRemapDx9)/sizeof(ConstRemapDx9);
  175. for (uint32_t ii = 0; ii < count; ++ii)
  176. {
  177. const ConstRemapDx9& remap = s_constRemapDx9[ii];
  178. if (remap.paramClass == constDesc.Class
  179. && remap.paramType == constDesc.Type
  180. && (constDesc.Bytes%remap.paramBytes) == 0)
  181. {
  182. return remap.id;
  183. }
  184. }
  185. return ConstantType::Count;
  186. }
  187. static uint32_t s_optimizationLevelDx9[4] =
  188. {
  189. D3DXSHADER_OPTIMIZATION_LEVEL0,
  190. D3DXSHADER_OPTIMIZATION_LEVEL1,
  191. D3DXSHADER_OPTIMIZATION_LEVEL2,
  192. D3DXSHADER_OPTIMIZATION_LEVEL3,
  193. };
  194. struct ConstRemapDx11
  195. {
  196. ConstantType::Enum id;
  197. D3D_SHADER_VARIABLE_CLASS paramClass;
  198. D3D_SHADER_VARIABLE_TYPE paramType;
  199. uint32_t paramBytes;
  200. };
  201. static const ConstRemapDx11 s_constRemapDx11[7] =
  202. {
  203. { ConstantType::Uniform1iv, D3D_SVC_SCALAR, D3D_SVT_INT, 4 },
  204. { ConstantType::Uniform1fv, D3D_SVC_SCALAR, D3D_SVT_FLOAT, 4 },
  205. { ConstantType::Uniform2fv, D3D_SVC_VECTOR, D3D_SVT_FLOAT, 8 },
  206. { ConstantType::Uniform3fv, D3D_SVC_VECTOR, D3D_SVT_FLOAT, 12 },
  207. { ConstantType::Uniform4fv, D3D_SVC_VECTOR, D3D_SVT_FLOAT, 16 },
  208. { ConstantType::Uniform3x3fv, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 36 },
  209. { ConstantType::Uniform4x4fv, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 64 },
  210. };
  211. ConstantType::Enum findConstantTypeDx11(const D3D11_SHADER_TYPE_DESC& constDesc, uint32_t _size)
  212. {
  213. uint32_t count = sizeof(s_constRemapDx11)/sizeof(ConstRemapDx9);
  214. for (uint32_t ii = 0; ii < count; ++ii)
  215. {
  216. const ConstRemapDx11& remap = s_constRemapDx11[ii];
  217. if (remap.paramClass == constDesc.Class
  218. && remap.paramType == constDesc.Type
  219. && (_size%remap.paramBytes) == 0)
  220. {
  221. return remap.id;
  222. }
  223. }
  224. return ConstantType::Count;
  225. }
  226. static uint32_t s_optimizationLevelDx11[4] =
  227. {
  228. D3DCOMPILE_OPTIMIZATION_LEVEL0,
  229. D3DCOMPILE_OPTIMIZATION_LEVEL1,
  230. D3DCOMPILE_OPTIMIZATION_LEVEL2,
  231. D3DCOMPILE_OPTIMIZATION_LEVEL3,
  232. };
  233. #endif // BX_PLATFORM_WINDOWS
  234. class IStreamWriter
  235. {
  236. public:
  237. virtual ~IStreamWriter() = 0;
  238. virtual bool open() = 0;
  239. virtual void close() = 0;
  240. virtual void writef(const char* _format, ...) = 0;
  241. virtual void write(const char* _str) = 0;
  242. virtual void write(const void* _data, size_t _size) = 0;
  243. template<typename Ty>
  244. void write(Ty _value)
  245. {
  246. write(&_value, sizeof(Ty) );
  247. }
  248. void writeString(const char* _str)
  249. {
  250. uint16_t len = (uint16_t)strlen(_str);
  251. write(len);
  252. write(_str);
  253. char term = '\0';
  254. write(term);
  255. }
  256. };
  257. IStreamWriter::~IStreamWriter()
  258. {
  259. }
  260. class FileWriter : public IStreamWriter
  261. {
  262. public:
  263. FileWriter(const char* _filePath, bool _bigEndian = false)
  264. : m_filePath(_filePath)
  265. , m_file(NULL)
  266. , m_bigEndian(_bigEndian)
  267. {
  268. }
  269. ~FileWriter()
  270. {
  271. }
  272. bool open()
  273. {
  274. BX_CHECK(NULL == m_file, "Still open!");
  275. m_file = fopen(m_filePath.c_str(), "wb");
  276. return NULL != m_file;
  277. }
  278. void close()
  279. {
  280. if (NULL != m_file)
  281. {
  282. fclose(m_file);
  283. m_file = NULL;
  284. }
  285. }
  286. void writef(const char* _format, ...)
  287. {
  288. if (NULL != m_file)
  289. {
  290. va_list argList;
  291. va_start(argList, _format);
  292. char temp[2048];
  293. int len = vsnprintf(temp, sizeof(temp), _format, argList);
  294. fwrite(temp, len, 1, m_file);
  295. va_end(argList);
  296. }
  297. }
  298. void write(const char* _str)
  299. {
  300. if (NULL != m_file)
  301. {
  302. fwrite(_str, strlen(_str), 1, m_file);
  303. }
  304. }
  305. void write(const void* _data, size_t _size)
  306. {
  307. if (NULL != m_file)
  308. {
  309. fwrite(_data, _size, 1, m_file);
  310. }
  311. }
  312. private:
  313. std::string m_filePath;
  314. FILE* m_file;
  315. bool m_bigEndian;
  316. };
  317. class Bin2cStream : public IStreamWriter
  318. {
  319. public:
  320. Bin2cStream(const char* _filePath, const char* _name)
  321. : m_filePath(_filePath)
  322. , m_name(_name)
  323. , m_file(NULL)
  324. {
  325. }
  326. ~Bin2cStream()
  327. {
  328. }
  329. bool open()
  330. {
  331. BX_CHECK(NULL == m_file, "Still open!");
  332. m_file = fopen(m_filePath.c_str(), "wb");
  333. return NULL != m_file;
  334. }
  335. void close()
  336. {
  337. if (NULL != m_file)
  338. {
  339. #define HEX_DUMP_WIDTH 16
  340. #define HEX_DUMP_SPACE_WIDTH 96
  341. #define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
  342. const uint8_t* data = &m_buffer[0];
  343. uint32_t size = m_buffer.size();
  344. fprintf(m_file, "static const uint8_t %s[%d] =\n{\n", m_name.c_str(), size);
  345. if (NULL != data)
  346. {
  347. char hex[HEX_DUMP_SPACE_WIDTH+1];
  348. char ascii[HEX_DUMP_WIDTH+1];
  349. uint32_t hexPos = 0;
  350. uint32_t asciiPos = 0;
  351. for (uint32_t ii = 0; ii < size; ++ii)
  352. {
  353. _snprintf(&hex[hexPos], sizeof(hex)-hexPos, "0x%02x, ", data[asciiPos]);
  354. hexPos += 6;
  355. ascii[asciiPos] = isprint(data[asciiPos]) && data[asciiPos] != '\\' ? data[asciiPos] : '.';
  356. asciiPos++;
  357. if (HEX_DUMP_WIDTH == asciiPos)
  358. {
  359. ascii[asciiPos] = '\0';
  360. fprintf(m_file, "\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
  361. data += asciiPos;
  362. hexPos = 0;
  363. asciiPos = 0;
  364. }
  365. }
  366. if (0 != asciiPos)
  367. {
  368. ascii[asciiPos] = '\0';
  369. fprintf(m_file, "\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
  370. }
  371. }
  372. fprintf(m_file, "};\n");
  373. #undef HEX_DUMP_WIDTH
  374. #undef HEX_DUMP_SPACE_WIDTH
  375. #undef HEX_DUMP_FORMAT
  376. fclose(m_file);
  377. m_file = NULL;
  378. }
  379. }
  380. void writef(const char* _format, ...)
  381. {
  382. va_list argList;
  383. va_start(argList, _format);
  384. char temp[2048];
  385. int len = vsnprintf(temp, sizeof(temp), _format, argList);
  386. m_buffer.insert(m_buffer.end(), temp, temp+len);
  387. va_end(argList);
  388. }
  389. void write(const char* _str)
  390. {
  391. m_buffer.insert(m_buffer.end(), _str, _str+strlen(_str) );
  392. }
  393. void write(const void* _data, size_t _size)
  394. {
  395. const char* data = (const char*)_data;
  396. m_buffer.insert(m_buffer.end(), data, data+_size);
  397. }
  398. private:
  399. std::string m_filePath;
  400. std::string m_name;
  401. typedef std::vector<uint8_t> Buffer;
  402. Buffer m_buffer;
  403. FILE* m_file;
  404. };
  405. struct Varying
  406. {
  407. std::string m_name;
  408. std::string m_type;
  409. std::string m_init;
  410. std::string m_semantics;
  411. };
  412. typedef std::unordered_map<std::string, Varying> VaryingMap;
  413. class File
  414. {
  415. public:
  416. File(const char* _filePath)
  417. : m_data(NULL)
  418. {
  419. FILE* file = fopen(_filePath, "r");
  420. if (NULL != file)
  421. {
  422. m_size = fsize(file);
  423. m_data = new char[m_size+1];
  424. m_size = fread(m_data, 1, m_size, file);
  425. m_data[m_size] = '\0';
  426. fclose(file);
  427. }
  428. }
  429. ~File()
  430. {
  431. delete [] m_data;
  432. }
  433. const char* getData() const
  434. {
  435. return m_data;
  436. }
  437. long int getSize() const
  438. {
  439. return m_size;
  440. }
  441. private:
  442. char* m_data;
  443. long int m_size;
  444. };
  445. const char* strnl(const char* _str)
  446. {
  447. const char* eol = strstr(_str, "\n\r");
  448. if (NULL != eol)
  449. {
  450. return eol + 2;
  451. }
  452. eol = strstr(_str, "\n");
  453. if (NULL != eol)
  454. {
  455. return eol + 1;
  456. }
  457. return eol + strlen(_str);
  458. }
  459. const char* streol(const char* _str)
  460. {
  461. const char* eol = strstr(_str, "\n\r");
  462. if (NULL != eol)
  463. {
  464. return eol;
  465. }
  466. eol = strstr(_str, "\n");
  467. if (NULL != eol)
  468. {
  469. return eol;
  470. }
  471. return eol + strlen(_str);
  472. }
  473. const char* strws(const char* _str)
  474. {
  475. for (; isspace(*_str); ++_str);
  476. return _str;
  477. }
  478. const char* strnws(const char* _str)
  479. {
  480. for (; !isspace(*_str); ++_str);
  481. return _str;
  482. }
  483. const char* strword(const char* _str)
  484. {
  485. for (char ch = *_str++; isalnum(ch) || '_' == ch; ch = *_str++);
  486. return _str-1;
  487. }
  488. const char* strmb(const char* _str, char _open, char _close)
  489. {
  490. int count = 0;
  491. for (char ch = *_str++; ch != '\0' && count >= 0; ch = *_str++)
  492. {
  493. if (ch == _open)
  494. {
  495. count++;
  496. }
  497. else if (ch == _close)
  498. {
  499. count--;
  500. if (0 == count)
  501. {
  502. return _str-1;
  503. }
  504. }
  505. }
  506. return NULL;
  507. }
  508. void strins(char* _str, const char* _insert)
  509. {
  510. size_t len = strlen(_insert);
  511. memmove(&_str[len], _str, strlen(_str) );
  512. memcpy(_str, _insert, len);
  513. }
  514. class LineReader
  515. {
  516. public:
  517. LineReader(const char* _str)
  518. : m_str(_str)
  519. , m_pos(0)
  520. , m_size( (uint32_t)strlen(_str) )
  521. {
  522. }
  523. std::string getLine()
  524. {
  525. const char* str = &m_str[m_pos];
  526. skipLine();
  527. const char* eol = &m_str[m_pos];
  528. std::string tmp;
  529. tmp.assign(str, eol-str);
  530. return tmp;
  531. }
  532. bool isEof() const
  533. {
  534. return m_str[m_pos] == '\0';
  535. }
  536. private:
  537. void skipLine()
  538. {
  539. const char* str = &m_str[m_pos];
  540. const char* nl = strnl(str);
  541. m_pos += (uint32_t)(nl - str);
  542. }
  543. const char* m_str;
  544. uint32_t m_pos;
  545. uint32_t m_size;
  546. };
  547. void printCode(const char* _code)
  548. {
  549. fprintf(stderr, "Code:\n---\n");
  550. LineReader lr(_code);
  551. for (uint32_t line = 1; !lr.isEof(); ++line)
  552. {
  553. fprintf(stderr, "%3d: %s", line, lr.getLine().c_str() );
  554. }
  555. fprintf(stderr, "---\n");
  556. }
  557. void writeFile(const char* _filePath, void* _data, uint32_t _size)
  558. {
  559. FILE* file = fopen(_filePath, "wb");
  560. if (NULL != file)
  561. {
  562. fwrite(_data, 1, _size, file);
  563. fclose(file);
  564. }
  565. }
  566. bool compileGLSLShader(CommandLine& _cmdLine, const std::string& _code, IStreamWriter& _stream)
  567. {
  568. const glslopt_shader_type type = tolower(_cmdLine.findOption('\0', "type")[0]) == 'f' ? kGlslOptShaderFragment : kGlslOptShaderVertex;
  569. glslopt_ctx* ctx = glslopt_initialize(false);
  570. glslopt_shader* shader = glslopt_optimize(ctx, type, _code.c_str(), 0);
  571. if( !glslopt_get_status(shader) )
  572. {
  573. printCode(_code.c_str() );
  574. fprintf(stderr, "Error: %s\n", glslopt_get_log(shader) );
  575. glslopt_cleanup(ctx);
  576. return false;
  577. }
  578. const char* optimizedShader = glslopt_get_output(shader);
  579. const char* profile = _cmdLine.findOption('p');
  580. if (NULL == profile)
  581. {
  582. _stream.write("#ifdef GL_ES\n");
  583. _stream.write("precision highp float;\n");
  584. _stream.write("#endif // GL_ES\n\n");
  585. }
  586. else
  587. {
  588. _stream.writef("#version %s\n\n", profile);
  589. }
  590. _stream.write(optimizedShader, strlen(optimizedShader) );
  591. uint8_t nul = 0;
  592. _stream.write(nul);
  593. glslopt_cleanup(ctx);
  594. return true;
  595. }
  596. bool compileHLSLShaderDx9(CommandLine& _cmdLine, const std::string& _code, IStreamWriter& _stream)
  597. {
  598. #if BX_PLATFORM_WINDOWS
  599. const char* profile = _cmdLine.findOption('p');
  600. if (NULL == profile)
  601. {
  602. printf("Shader profile must be specified.\n");
  603. return false;
  604. }
  605. bool bigEndian = _cmdLine.hasArg('\0', "xbox360");
  606. uint32_t flags = 0;
  607. flags |= _cmdLine.hasArg('\0', "debug") ? D3DXSHADER_DEBUG : 0;
  608. flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DXSHADER_AVOID_FLOW_CONTROL : 0;
  609. flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DXSHADER_NO_PRESHADER : 0;
  610. flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DXSHADER_PARTIALPRECISION : 0;
  611. flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DXSHADER_PREFER_FLOW_CONTROL : 0;
  612. flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY : 0;
  613. bool werror = _cmdLine.hasArg('\0', "Werror");
  614. uint32_t optimization = 3;
  615. if (_cmdLine.hasArg(optimization, 'O') )
  616. {
  617. optimization = bx::uint32_min(optimization, countof(s_optimizationLevelDx9)-1);
  618. flags |= s_optimizationLevelDx9[optimization];
  619. }
  620. else
  621. {
  622. flags |= D3DXSHADER_SKIPOPTIMIZATION;
  623. }
  624. BX_TRACE("Profile: %s", profile);
  625. BX_TRACE("Flags: 0x%08x", flags);
  626. BX_TRACE("Big Endian: %s", bigEndian?"true":"false");
  627. LPD3DXBUFFER code;
  628. LPD3DXBUFFER errorMsg;
  629. LPD3DXCONSTANTTABLE constantTable;
  630. HRESULT hr = D3DXCompileShader(_code.c_str()
  631. , _code.size()
  632. , NULL
  633. , NULL
  634. , "main"
  635. , profile
  636. , flags
  637. , &code
  638. , &errorMsg
  639. , &constantTable
  640. );
  641. if (FAILED(hr)
  642. || werror && NULL != errorMsg)
  643. {
  644. printCode(_code.c_str() );
  645. fprintf(stderr, "Error: 0x%08x %s\n", hr, errorMsg->GetBufferPointer() );
  646. return false;
  647. }
  648. D3DXCONSTANTTABLE_DESC desc;
  649. hr = constantTable->GetDesc(&desc);
  650. if (FAILED(hr) )
  651. {
  652. fprintf(stderr, "Error 0x%08x\n", hr);
  653. return false;
  654. }
  655. BX_TRACE("Creator: %s 0x%08x", desc.Creator, desc.Version);
  656. BX_TRACE("Num constants: %d", desc.Constants);
  657. BX_TRACE("# cl ty RxC S By Name");
  658. UniformArray uniforms;
  659. for (uint32_t ii = 0; ii < desc.Constants; ++ii)
  660. {
  661. D3DXHANDLE handle = constantTable->GetConstant(NULL, ii);
  662. D3DXCONSTANT_DESC constDesc;
  663. uint32_t count;
  664. constantTable->GetConstantDesc(handle, &constDesc, &count);
  665. BX_TRACE("%3d %2d %2d [%dx%d] %d %3d %s[%d] c%d (%d)"
  666. , ii
  667. , constDesc.Class
  668. , constDesc.Type
  669. , constDesc.Rows
  670. , constDesc.Columns
  671. , constDesc.StructMembers
  672. , constDesc.Bytes
  673. , constDesc.Name
  674. , constDesc.Elements
  675. , constDesc.RegisterIndex
  676. , constDesc.RegisterCount
  677. );
  678. ConstantType::Enum type = findConstantTypeDx9(constDesc);
  679. if (ConstantType::Count != type)
  680. {
  681. Uniform un;
  682. un.name = '$' == constDesc.Name[0] ? constDesc.Name+1 : constDesc.Name;
  683. un.type = type;
  684. un.num = constDesc.Elements;
  685. un.regIndex = constDesc.RegisterIndex;
  686. un.regCount = constDesc.RegisterCount;
  687. uniforms.push_back(un);
  688. }
  689. }
  690. uint16_t count = (uint16_t)uniforms.size();
  691. _stream.write(count);
  692. uint32_t fragmentBit = profile[0] == 'p' ? ConstantType::FragmentBit : 0;
  693. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  694. {
  695. const Uniform& un = *it;
  696. uint8_t nameSize = (uint8_t)un.name.size();
  697. _stream.write(nameSize);
  698. _stream.write(un.name.c_str(), nameSize);
  699. _stream.write<uint8_t>(un.type|fragmentBit);
  700. _stream.write(un.num);
  701. _stream.write(un.regIndex);
  702. _stream.write(un.regCount);
  703. BX_TRACE("%s, %s, %d, %d, %d"
  704. , un.name.c_str()
  705. , s_constantTypeName[un.type]
  706. , un.num
  707. , un.regIndex
  708. , un.regCount
  709. );
  710. }
  711. uint16_t shaderSize = (uint16_t)code->GetBufferSize();
  712. _stream.write(shaderSize);
  713. _stream.write(code->GetBufferPointer(), shaderSize);
  714. uint8_t nul = 0;
  715. _stream.write(nul);
  716. if (_cmdLine.hasArg('\0', "disasm") )
  717. {
  718. LPD3DXBUFFER disasm;
  719. D3DXDisassembleShader( (const DWORD*)code->GetBufferPointer()
  720. , false
  721. , NULL
  722. , &disasm
  723. );
  724. if (NULL != disasm)
  725. {
  726. std::string ofp = _cmdLine.findOption('o');
  727. ofp += ".disasm";
  728. writeFile(ofp.c_str(), disasm->GetBufferPointer(), disasm->GetBufferSize() );
  729. disasm->Release();
  730. }
  731. }
  732. if (NULL != code)
  733. {
  734. code->Release();
  735. }
  736. if (NULL != errorMsg)
  737. {
  738. errorMsg->Release();
  739. }
  740. if (NULL != constantTable)
  741. {
  742. constantTable->Release();
  743. }
  744. return true;
  745. #else
  746. fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
  747. return false;
  748. #endif // BX_PLATFORM_WINDOWS
  749. }
  750. bool compileHLSLShaderDx11(CommandLine& _cmdLine, const std::string& _code, IStreamWriter& _stream)
  751. {
  752. #if BX_PLATFORM_WINDOWS
  753. const char* profile = _cmdLine.findOption('p');
  754. if (NULL == profile)
  755. {
  756. printf("Shader profile must be specified.\n");
  757. return false;
  758. }
  759. bool bigEndian = _cmdLine.hasArg('\0', "xbox360");
  760. uint32_t flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  761. flags |= _cmdLine.hasArg('\0', "debug") ? D3DCOMPILE_DEBUG : 0;
  762. flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DCOMPILE_AVOID_FLOW_CONTROL : 0;
  763. flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DCOMPILE_NO_PRESHADER : 0;
  764. flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DCOMPILE_PARTIAL_PRECISION : 0;
  765. flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DCOMPILE_PREFER_FLOW_CONTROL : 0;
  766. flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY : 0;
  767. bool werror = _cmdLine.hasArg('\0', "Werror");
  768. if (werror)
  769. {
  770. flags |= D3DCOMPILE_WARNINGS_ARE_ERRORS;
  771. }
  772. uint32_t optimization = 3;
  773. if (_cmdLine.hasArg(optimization, 'O') )
  774. {
  775. optimization = bx::uint32_min(optimization, countof(s_optimizationLevelDx11)-1);
  776. flags |= s_optimizationLevelDx11[optimization];
  777. }
  778. else
  779. {
  780. flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  781. }
  782. BX_TRACE("Profile: %s", profile);
  783. BX_TRACE("Flags: 0x%08x", flags);
  784. BX_TRACE("Big Endian: %s", bigEndian?"true":"false");
  785. ID3DBlob* code;
  786. ID3DBlob* errorMsg;
  787. HRESULT hr = D3DCompile(_code.c_str()
  788. , _code.size()
  789. , NULL
  790. , NULL
  791. , NULL
  792. , "main"
  793. , profile
  794. , flags
  795. , 0
  796. , &code
  797. , &errorMsg
  798. );
  799. if (FAILED(hr)
  800. || werror && NULL != errorMsg)
  801. {
  802. printCode(_code.c_str() );
  803. fprintf(stderr, BX_FILE_LINE_LITERAL "Error: 0x%08x %s\n", hr, (char*)errorMsg->GetBufferPointer() );
  804. errorMsg->Release();
  805. return false;
  806. }
  807. UniformArray uniforms;
  808. ID3D11ShaderReflection* reflect = NULL;
  809. hr = D3DReflect(code->GetBufferPointer()
  810. , code->GetBufferSize()
  811. , IID_ID3D11ShaderReflection
  812. , (void**)&reflect
  813. );
  814. if (FAILED(hr) )
  815. {
  816. fprintf(stderr, BX_FILE_LINE_LITERAL "Error: 0x%08x\n", hr);
  817. return false;
  818. }
  819. D3D11_SHADER_DESC desc;
  820. hr = reflect->GetDesc(&desc);
  821. if (FAILED(hr) )
  822. {
  823. fprintf(stderr, BX_FILE_LINE_LITERAL "Error: 0x%08x\n", hr);
  824. return false;
  825. }
  826. BX_TRACE("Creator: %s 0x%08x", desc.Creator, desc.Version);
  827. BX_TRACE("Num constant buffers: %d", desc.ConstantBuffers);
  828. BX_TRACE("Input:");
  829. uint8_t attrMask[Attrib::Count];
  830. memset(attrMask, 0, sizeof(attrMask) );
  831. for (uint32_t ii = 0; ii < desc.InputParameters; ++ii)
  832. {
  833. D3D11_SIGNATURE_PARAMETER_DESC spd;
  834. reflect->GetInputParameterDesc(ii, &spd);
  835. BX_TRACE("\t%2d: %s%d, vt %d, ct %d, mask %x, reg %d"
  836. , ii
  837. , spd.SemanticName
  838. , spd.SemanticIndex
  839. , spd.SystemValueType
  840. , spd.ComponentType
  841. , spd.Mask
  842. , spd.Register
  843. );
  844. const RemapInputSemantic& ris = findInputSemantic(spd.SemanticName, spd.SemanticIndex);
  845. if (ris.m_attr != Attrib::Count)
  846. {
  847. attrMask[ris.m_attr] = 0xff;
  848. }
  849. }
  850. _stream.write(attrMask, sizeof(attrMask) );
  851. BX_TRACE("Output:");
  852. for (uint32_t ii = 0; ii < desc.OutputParameters; ++ii)
  853. {
  854. D3D11_SIGNATURE_PARAMETER_DESC spd;
  855. reflect->GetOutputParameterDesc(ii, &spd);
  856. BX_TRACE("\t%2d: %s%d, %d, %d", ii, spd.SemanticName, spd.SemanticIndex, spd.SystemValueType, spd.ComponentType);
  857. }
  858. uint16_t size = 0;
  859. for (uint32_t ii = 0; ii < bx::uint32_min(1, desc.ConstantBuffers); ++ii)
  860. {
  861. ID3D11ShaderReflectionConstantBuffer* cbuffer = reflect->GetConstantBufferByIndex(ii);
  862. D3D11_SHADER_BUFFER_DESC bufferDesc;
  863. hr = cbuffer->GetDesc(&bufferDesc);
  864. size = (uint16_t)bufferDesc.Size;
  865. if (SUCCEEDED(hr) )
  866. {
  867. BX_TRACE("%s, %d, vars %d, size %d"
  868. , bufferDesc.Name
  869. , bufferDesc.Type
  870. , bufferDesc.Variables
  871. , bufferDesc.Size
  872. );
  873. for (uint32_t jj = 0; jj < bufferDesc.Variables; ++jj)
  874. {
  875. ID3D11ShaderReflectionVariable* var = cbuffer->GetVariableByIndex(jj);
  876. ID3D11ShaderReflectionType* type = var->GetType();
  877. D3D11_SHADER_VARIABLE_DESC varDesc;
  878. hr = var->GetDesc(&varDesc);
  879. if (SUCCEEDED(hr) )
  880. {
  881. D3D11_SHADER_TYPE_DESC constDesc;
  882. hr = type->GetDesc(&constDesc);
  883. if (SUCCEEDED(hr) )
  884. {
  885. ConstantType::Enum type = findConstantTypeDx11(constDesc, varDesc.Size);
  886. if (ConstantType::Count != type
  887. && 0 != (varDesc.uFlags & D3D_SVF_USED) )
  888. {
  889. Uniform un;
  890. un.name = varDesc.Name;
  891. un.type = type;
  892. un.num = constDesc.Elements;
  893. un.regIndex = varDesc.StartOffset;
  894. un.regCount = BX_ALIGN_16(varDesc.Size)/16;
  895. uniforms.push_back(un);
  896. BX_TRACE("\t%s, %d, size %d, flags 0x%08x, %d"
  897. , varDesc.Name
  898. , varDesc.StartOffset
  899. , varDesc.Size
  900. , varDesc.uFlags
  901. , type
  902. );
  903. }
  904. }
  905. }
  906. }
  907. }
  908. }
  909. BX_TRACE("Bound:");
  910. for (uint32_t ii = 0; ii < desc.BoundResources; ++ii)
  911. {
  912. D3D11_SHADER_INPUT_BIND_DESC bindDesc;
  913. hr = reflect->GetResourceBindingDesc(ii, &bindDesc);
  914. if (SUCCEEDED(hr) )
  915. {
  916. // if (bindDesc.Type == D3D_SIT_SAMPLER)
  917. {
  918. BX_TRACE("\t%s, %d, %d, %d"
  919. , bindDesc.Name
  920. , bindDesc.Type
  921. , bindDesc.BindPoint
  922. , bindDesc.BindCount
  923. );
  924. }
  925. }
  926. }
  927. uint16_t count = (uint16_t)uniforms.size();
  928. _stream.write(count);
  929. _stream.write(size);
  930. uint32_t fragmentBit = profile[0] == 'p' ? ConstantType::FragmentBit : 0;
  931. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  932. {
  933. const Uniform& un = *it;
  934. uint8_t nameSize = (uint8_t)un.name.size();
  935. _stream.write(nameSize);
  936. _stream.write(un.name.c_str(), nameSize);
  937. _stream.write<uint8_t>(un.type|fragmentBit);
  938. _stream.write(un.num);
  939. _stream.write(un.regIndex);
  940. _stream.write(un.regCount);
  941. BX_TRACE("%s, %s, %d, %d, %d"
  942. , un.name.c_str()
  943. , s_constantTypeName[un.type]
  944. , un.num
  945. , un.regIndex
  946. , un.regCount
  947. );
  948. }
  949. uint16_t shaderSize = (uint16_t)code->GetBufferSize();
  950. _stream.write(shaderSize);
  951. _stream.write(code->GetBufferPointer(), shaderSize);
  952. uint8_t nul = 0;
  953. _stream.write(nul);
  954. if (_cmdLine.hasArg('\0', "disasm") )
  955. {
  956. ID3DBlob* disasm;
  957. D3DDisassemble(code->GetBufferPointer()
  958. , code->GetBufferSize()
  959. , 0
  960. , NULL
  961. , &disasm
  962. );
  963. if (NULL != disasm)
  964. {
  965. std::string ofp = _cmdLine.findOption('o');
  966. ofp += ".disasm";
  967. writeFile(ofp.c_str(), disasm->GetBufferPointer(), disasm->GetBufferSize() );
  968. disasm->Release();
  969. }
  970. }
  971. if (NULL != reflect)
  972. {
  973. reflect->Release();
  974. }
  975. if (NULL != errorMsg)
  976. {
  977. errorMsg->Release();
  978. }
  979. code->Release();
  980. return true;
  981. #else
  982. fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
  983. return false;
  984. #endif // BX_PLATFORM_WINDOWS
  985. }
  986. struct Preprocessor
  987. {
  988. Preprocessor(const char* _filePath)
  989. : m_tagptr(m_tags)
  990. , m_scratchPos(0)
  991. , m_fgetsPos(0)
  992. {
  993. m_tagptr->tag = FPPTAG_USERDATA;
  994. m_tagptr->data = this;
  995. m_tagptr++;
  996. m_tagptr->tag = FPPTAG_DEPENDS;
  997. m_tagptr->data = (void*)fppDepends;
  998. m_tagptr++;
  999. m_tagptr->tag = FPPTAG_INPUT;
  1000. m_tagptr->data = (void*)fppInput;
  1001. m_tagptr++;
  1002. m_tagptr->tag = FPPTAG_OUTPUT;
  1003. m_tagptr->data = (void*)fppOutput;
  1004. m_tagptr++;
  1005. m_tagptr->tag = FPPTAG_ERROR;
  1006. m_tagptr->data = (void*)fppError;
  1007. m_tagptr++;
  1008. m_tagptr->tag = FPPTAG_IGNOREVERSION;
  1009. m_tagptr->data = (void*)0;
  1010. m_tagptr++;
  1011. m_tagptr->tag = FPPTAG_LINE;
  1012. m_tagptr->data = (void*)0;
  1013. m_tagptr++;
  1014. m_tagptr->tag = FPPTAG_INPUT_NAME;
  1015. m_tagptr->data = scratch(_filePath);
  1016. m_tagptr++;
  1017. m_default = "#define lowp\n#define mediump\n#define highp\n";
  1018. }
  1019. void setDefine(const char* _define)
  1020. {
  1021. m_tagptr->tag = FPPTAG_DEFINE;
  1022. m_tagptr->data = scratch(_define);
  1023. m_tagptr++;
  1024. }
  1025. void setDefaultDefine(const char* _name)
  1026. {
  1027. char temp[1024];
  1028. _snprintf(temp, countof(temp)
  1029. , "#ifndef %s\n"
  1030. "# define %s 0\n"
  1031. "#endif // %s\n"
  1032. "\n"
  1033. , _name
  1034. , _name
  1035. , _name
  1036. );
  1037. m_default += temp;
  1038. }
  1039. void writef(const char* _format, ...)
  1040. {
  1041. va_list argList;
  1042. va_start(argList, _format);
  1043. char temp[2048];
  1044. int len = vsnprintf(temp, sizeof(temp), _format, argList);
  1045. m_default += temp;
  1046. va_end(argList);
  1047. }
  1048. void addDependency(const char* _fileName)
  1049. {
  1050. m_depends += " \\\n ";
  1051. m_depends += _fileName;
  1052. }
  1053. bool run(const char* _input)
  1054. {
  1055. m_fgetsPos = 0;
  1056. m_input = m_default;
  1057. m_input += _input;
  1058. fppTag* tagptr = m_tagptr;
  1059. tagptr->tag = FPPTAG_END;
  1060. tagptr->data = 0;
  1061. tagptr++;
  1062. int result = fppPreProcess(m_tags);
  1063. return 0 == result;
  1064. }
  1065. char* fgets(char* _buffer, int _size)
  1066. {
  1067. int ii = 0;
  1068. for (char ch = m_input[m_fgetsPos]; m_fgetsPos < m_input.size() && ii < _size-1; ch = m_input[++m_fgetsPos])
  1069. {
  1070. _buffer[ii++] = ch;
  1071. if (ch == '\n' || ii == _size)
  1072. {
  1073. _buffer[ii] = '\0';
  1074. m_fgetsPos++;
  1075. return _buffer;
  1076. }
  1077. }
  1078. return NULL;
  1079. }
  1080. static void fppDepends(char* _fileName, void* _userData)
  1081. {
  1082. Preprocessor* thisClass = (Preprocessor*)_userData;
  1083. thisClass->addDependency(_fileName);
  1084. }
  1085. static char* fppInput(char* _buffer, int _size, void* _userData)
  1086. {
  1087. Preprocessor* thisClass = (Preprocessor*)_userData;
  1088. return thisClass->fgets(_buffer, _size);
  1089. }
  1090. static void fppOutput(int _ch, void* _userData)
  1091. {
  1092. Preprocessor* thisClass = (Preprocessor*)_userData;
  1093. thisClass->m_preprocessed += _ch;
  1094. }
  1095. static void fppError(void* _userData, char* _format, va_list _vargs)
  1096. {
  1097. vfprintf(stderr, _format, _vargs);
  1098. }
  1099. char* scratch(const char* _str)
  1100. {
  1101. char* result = &m_scratch[m_scratchPos];
  1102. strcpy(result, _str);
  1103. m_scratchPos += strlen(_str)+1;
  1104. return result;
  1105. }
  1106. fppTag m_tags[MAX_TAGS];
  1107. fppTag* m_tagptr;
  1108. std::string m_depends;
  1109. std::string m_default;
  1110. std::string m_input;
  1111. std::string m_preprocessed;
  1112. char m_scratch[16<<10];
  1113. uint32_t m_scratchPos;
  1114. uint32_t m_fgetsPos;
  1115. };
  1116. const char* baseName(const char* _filePath)
  1117. {
  1118. const char* bs = strrchr(_filePath, '\\');
  1119. const char* fs = strrchr(_filePath, '/');
  1120. const char* column = strrchr(_filePath, ':');
  1121. const char* basename = std::max(std::max(bs, fs), column);
  1122. if (NULL != basename)
  1123. {
  1124. return basename+1;
  1125. }
  1126. return _filePath;
  1127. }
  1128. // OpenGL #version Features Direct3D Features Shader Model
  1129. // 2.1 120 vf 9.0 vf 2.0
  1130. // 3.0 130
  1131. // 3.1 140
  1132. // 3.2 150 vgf
  1133. // 3.3 330 10.0 vgf 4.0
  1134. // 4.0 400 vhdgf
  1135. // 4.1 410
  1136. // 4.2 420 11.0 vhdgf 5.0
  1137. void help(const char* _error = NULL)
  1138. {
  1139. if (NULL != _error)
  1140. {
  1141. fprintf(stderr, "Error:\n%s\n\n", _error);
  1142. }
  1143. fprintf(stderr
  1144. , "shaderc, bgfx shader compiler tool\n"
  1145. "Copyright 2011-2012 Branimir Karadzic. All rights reserved.\n"
  1146. "License: http://www.opensource.org/licenses/BSD-2-Clause\n\n"
  1147. );
  1148. fprintf(stderr
  1149. , "Usage: shaderc -f <in> -o <out> --type <v/f> --platform <platform>\n\n"
  1150. "Options:\n"
  1151. "\t-f Input file path.\n"
  1152. "\t-o Output file path.\n"
  1153. "\t --bin2c Generate C header file.\n"
  1154. "\t --depends Generate makefile style depends file.\n"
  1155. "\t --platform Target platform.\n"
  1156. "\t android\n"
  1157. "\t ios\n"
  1158. "\t linux\n"
  1159. "\t nacl\n"
  1160. "\t osx\n"
  1161. "\t windows\n"
  1162. "\t --type Shader type (vertex, fragment)\n"
  1163. "\t --varyingdef Path to varying.def.sc file.\n"
  1164. "\n"
  1165. "Options (DX9 and DX11 only):\n"
  1166. "\n"
  1167. "\t --disasm Disassemble compiled shader.\n"
  1168. "\t-p, --profile Shader model (f.e. ps_3_0).\n"
  1169. "\n"
  1170. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  1171. );
  1172. }
  1173. int main(int _argc, const char* _argv[])
  1174. {
  1175. CommandLine cmdLine(_argc, _argv);
  1176. if (cmdLine.hasArg('h', "help") )
  1177. {
  1178. help();
  1179. return EXIT_FAILURE;
  1180. }
  1181. const char* filePath = cmdLine.findOption('f');
  1182. if (NULL == filePath)
  1183. {
  1184. help("Shader file name must be specified.");
  1185. return EXIT_FAILURE;
  1186. }
  1187. const char* outFilePath = cmdLine.findOption('o');
  1188. if (NULL == outFilePath)
  1189. {
  1190. help("Output file name must be specified.");
  1191. return EXIT_FAILURE;
  1192. }
  1193. const char* type = cmdLine.findOption('\0', "type");
  1194. if (NULL == type)
  1195. {
  1196. help("Must specify shader type.");
  1197. return EXIT_FAILURE;
  1198. }
  1199. const char* platform = cmdLine.findOption('\0', "platform");
  1200. if (NULL == platform)
  1201. {
  1202. help("Must specify platform.");
  1203. return EXIT_FAILURE;
  1204. }
  1205. uint32_t hlsl = 2;
  1206. const char* profile = cmdLine.findOption('p', "profile");
  1207. if (NULL != profile)
  1208. {
  1209. if (0 == strncmp(&profile[1], "s_3", 3) )
  1210. {
  1211. hlsl = 3;
  1212. }
  1213. else if (0 == strncmp(&profile[1], "s_4", 3) )
  1214. {
  1215. hlsl = 4;
  1216. }
  1217. else if (0 == strncmp(&profile[1], "s_5", 3) )
  1218. {
  1219. hlsl = 5;
  1220. }
  1221. }
  1222. const char* bin2c = NULL;
  1223. if (cmdLine.hasArg("bin2c") )
  1224. {
  1225. bin2c = cmdLine.findOption("bin2c");
  1226. if (NULL == bin2c)
  1227. {
  1228. bin2c = baseName(outFilePath);
  1229. uint32_t len = strlen(bin2c);
  1230. char* temp = (char*)alloca(len+1);
  1231. for (char *out = temp; *bin2c != '\0';)
  1232. {
  1233. char ch = *bin2c++;
  1234. if (isalnum(ch) )
  1235. {
  1236. *out++ = ch;
  1237. }
  1238. else
  1239. {
  1240. *out++ = '_';
  1241. }
  1242. }
  1243. temp[len] = '\0';
  1244. bin2c = temp;
  1245. }
  1246. }
  1247. bool depends = cmdLine.hasArg("depends");
  1248. bool preprocessOnly = cmdLine.hasArg("preprocess");
  1249. Preprocessor preprocessor(filePath);
  1250. preprocessor.setDefaultDefine("BX_PLATFORM_ANDROID");
  1251. preprocessor.setDefaultDefine("BX_PLATFORM_IOS");
  1252. preprocessor.setDefaultDefine("BX_PLATFORM_LINUX");
  1253. preprocessor.setDefaultDefine("BX_PLATFORM_NACL");
  1254. preprocessor.setDefaultDefine("BX_PLATFORM_OSX");
  1255. preprocessor.setDefaultDefine("BX_PLATFORM_WINDOWS");
  1256. preprocessor.setDefaultDefine("BX_PLATFORM_XBOX360");
  1257. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_GLSL");
  1258. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_HLSL");
  1259. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_FRAGMENT");
  1260. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_VERTEX");
  1261. bool glsl = false;
  1262. if (0 == _stricmp(platform, "android") )
  1263. {
  1264. preprocessor.setDefine("BX_PLATFORM_ANDROID=1");
  1265. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1266. glsl = true;
  1267. }
  1268. else if (0 == _stricmp(platform, "ios") )
  1269. {
  1270. preprocessor.setDefine("BX_PLATFORM_IOS=1");
  1271. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1272. glsl = true;
  1273. }
  1274. else if (0 == _stricmp(platform, "linux") )
  1275. {
  1276. preprocessor.setDefine("BX_PLATFORM_IOS=1");
  1277. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1278. glsl = true;
  1279. }
  1280. else if (0 == _stricmp(platform, "nacl") )
  1281. {
  1282. preprocessor.setDefine("BX_PLATFORM_NACL=1");
  1283. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1284. glsl = true;
  1285. }
  1286. else if (0 == _stricmp(platform, "osx") )
  1287. {
  1288. preprocessor.setDefine("BX_PLATFORM_OSX=1");
  1289. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  1290. glsl = true;
  1291. }
  1292. else if (0 == _stricmp(platform, "windows") )
  1293. {
  1294. preprocessor.setDefine("BX_PLATFORM_WINDOWS=1");
  1295. char temp[256];
  1296. _snprintf(temp, sizeof(temp), "BGFX_SHADER_LANGUAGE_HLSL=%d", hlsl);
  1297. preprocessor.setDefine(temp);
  1298. }
  1299. else if (0 == _stricmp(platform, "xbox360") )
  1300. {
  1301. preprocessor.setDefine("BX_PLATFORM_XBOX360=1");
  1302. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=3");
  1303. }
  1304. else
  1305. {
  1306. fprintf(stderr, "Unknown platform %s?!", platform);
  1307. return EXIT_FAILURE;
  1308. }
  1309. preprocessor.setDefine("M_PI=3.1415926535897932384626433832795");
  1310. bool fragment = false;
  1311. switch (tolower(type[0]) )
  1312. {
  1313. case 'f':
  1314. preprocessor.setDefine("BGFX_SHADER_TYPE_FRAGMENT=1");
  1315. fragment = true;
  1316. break;
  1317. case 'v':
  1318. preprocessor.setDefine("BGFX_SHADER_TYPE_VERTEX=1");
  1319. break;
  1320. default:
  1321. fprintf(stderr, "Unknown type: %s?!", type);
  1322. return EXIT_FAILURE;
  1323. }
  1324. FILE* file = fopen(filePath, "r");
  1325. if (NULL != file)
  1326. {
  1327. VaryingMap varyingMap;
  1328. File attribdef(cmdLine.findOption("varyingdef", "varying.def.sc") );
  1329. const char* parse = attribdef.getData();
  1330. while (NULL != parse
  1331. && *parse != '\0')
  1332. {
  1333. parse = strws(parse);
  1334. const char* eol = strchr(parse, ';');
  1335. if (NULL != eol)
  1336. {
  1337. const char* type = parse;
  1338. const char* name = parse = strws(strword(parse) );
  1339. const char* column = parse = strws(strword(parse) );
  1340. const char* semantics = parse = strws(strnws(parse) );
  1341. const char* assign = parse = strws(strword(parse) );
  1342. const char* init = parse = strws(strnws(parse) );
  1343. if (type < eol
  1344. && name < eol
  1345. && column < eol
  1346. && ':' == *column
  1347. && semantics < eol)
  1348. {
  1349. Varying var;
  1350. var.m_type.assign(type, strword(type)-type);
  1351. var.m_name.assign(name, strword(name)-name);
  1352. var.m_semantics.assign(semantics, strword(semantics)-semantics);
  1353. if (assign < eol
  1354. && '=' == *assign
  1355. && init < eol)
  1356. {
  1357. var.m_init.assign(init, eol-init);
  1358. }
  1359. varyingMap.insert(std::make_pair(var.m_name, var) );
  1360. }
  1361. parse = eol + 1;
  1362. }
  1363. }
  1364. const size_t padding = 16;
  1365. long int size = fsize(file);
  1366. char* data = new char[size+padding];
  1367. size = fread(data, 1, size, file);
  1368. memset(&data[size], 0, padding);
  1369. fclose(file);
  1370. typedef std::vector<std::string> InOut;
  1371. InOut shaderInputs;
  1372. InOut shaderOutputs;
  1373. const char* input = data;
  1374. while (input[0] == '$')
  1375. {
  1376. const char* str = input+1;
  1377. const char* eol = streol(str);
  1378. const char* nl = strnl(eol);
  1379. input = nl;
  1380. if (0 == strncmp(str, "input", 5) )
  1381. {
  1382. str += 5;
  1383. str = strws(str);
  1384. if (str < eol)
  1385. {
  1386. const char* delim;
  1387. do
  1388. {
  1389. delim = strpbrk(str, " ,");
  1390. if (NULL != delim)
  1391. {
  1392. delim = delim > eol ? eol : delim;
  1393. std::string token;
  1394. token.assign(str, delim-str);
  1395. shaderInputs.push_back(token);
  1396. str = strws(delim + 1);
  1397. }
  1398. }
  1399. while (delim < eol && NULL != delim);
  1400. }
  1401. }
  1402. else if (0 == strncmp(str, "output", 6) )
  1403. {
  1404. str += 6;
  1405. str = strws(str);
  1406. if (str < eol)
  1407. {
  1408. const char* delim;
  1409. do
  1410. {
  1411. delim = strpbrk(str, " ,");
  1412. if (NULL != delim)
  1413. {
  1414. delim = delim > eol ? eol : delim;
  1415. std::string token;
  1416. token.assign(str, delim-str);
  1417. shaderOutputs.push_back(token);
  1418. str = strws(delim + 1);
  1419. }
  1420. }
  1421. while (delim < eol && NULL != delim);
  1422. }
  1423. }
  1424. }
  1425. if (glsl)
  1426. {
  1427. preprocessor.writef(
  1428. "#define ivec2 vec2\n"
  1429. "#define ivec3 vec3\n"
  1430. "#define ivec4 vec4\n"
  1431. );
  1432. for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
  1433. {
  1434. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1435. if (varyingIt != varyingMap.end() )
  1436. {
  1437. const Varying& var = varyingIt->second;
  1438. const char* name = var.m_name.c_str();
  1439. if (0 == strncmp(name, "a_", 2)
  1440. || 0 == strncmp(name, "i_", 2) )
  1441. {
  1442. preprocessor.writef("attribute %s %s;\n", var.m_type.c_str(), name);
  1443. }
  1444. else
  1445. {
  1446. preprocessor.writef("varying %s %s;\n", var.m_type.c_str(), name);
  1447. }
  1448. }
  1449. }
  1450. for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
  1451. {
  1452. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1453. if (varyingIt != varyingMap.end() )
  1454. {
  1455. const Varying& var = varyingIt->second;
  1456. preprocessor.writef("varying %s %s;\n", var.m_type.c_str(), var.m_name.c_str() );
  1457. }
  1458. }
  1459. }
  1460. else
  1461. {
  1462. preprocessor.writef(
  1463. "#define lowp\n"
  1464. "#define mediump\n"
  1465. "#define highp\n"
  1466. "#define ivec2 int2\n"
  1467. "#define ivec3 int3\n"
  1468. "#define ivec4 int4\n"
  1469. "#define vec2 float2\n"
  1470. "#define vec3 float3\n"
  1471. "#define vec4 float4\n"
  1472. "#define mat2 float2x2\n"
  1473. "#define mat3 float3x3\n"
  1474. "#define mat4 float4x4\n"
  1475. );
  1476. char* entry = strstr(data, "void main()");
  1477. if (NULL != entry)
  1478. {
  1479. entry[4] = '_';
  1480. if (fragment)
  1481. {
  1482. preprocessor.writef("#define void_main() \\\n");
  1483. preprocessor.writef("\tvoid main(vec4 gl_FragCoord : SV_POSITION \\\n");
  1484. for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
  1485. {
  1486. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1487. if (varyingIt != varyingMap.end() )
  1488. {
  1489. const Varying& var = varyingIt->second;
  1490. preprocessor.writef("\t, %s %s : %s \\\n", var.m_type.c_str(), var.m_name.c_str(), var.m_semantics.c_str() );
  1491. }
  1492. }
  1493. preprocessor.writef(
  1494. ", out vec4 gl_FragColor : SV_TARGET \\\n"
  1495. );
  1496. if (NULL != strstr(data, "gl_FragDepth") )
  1497. {
  1498. preprocessor.writef(
  1499. ", out float gl_FragDepth : SV_DEPTH \\\n"
  1500. );
  1501. }
  1502. preprocessor.writef(
  1503. ")\n"
  1504. );
  1505. }
  1506. else
  1507. {
  1508. const char* brace = strstr(entry, "{");
  1509. if (NULL != brace)
  1510. {
  1511. const char* end = strmb(brace, '{', '}');
  1512. if (NULL != end)
  1513. {
  1514. strins(const_cast<char*>(end), "__RETURN__;\n");
  1515. }
  1516. }
  1517. preprocessor.writef(
  1518. "struct Output\n"
  1519. "{\n"
  1520. "\tvec4 gl_Position : SV_POSITION;\n"
  1521. "#define gl_Position _varying_.gl_Position\n"
  1522. );
  1523. for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
  1524. {
  1525. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1526. if (varyingIt != varyingMap.end() )
  1527. {
  1528. const Varying& var = varyingIt->second;
  1529. preprocessor.writef("\t%s %s : %s;\n", var.m_type.c_str(), var.m_name.c_str(), var.m_semantics.c_str() );
  1530. preprocessor.writef("#define %s _varying_.%s\n", var.m_name.c_str(), var.m_name.c_str() );
  1531. }
  1532. }
  1533. preprocessor.writef(
  1534. "};\n"
  1535. );
  1536. preprocessor.writef("#define void_main() \\\n");
  1537. preprocessor.writef("Output main(");
  1538. bool first = true;
  1539. for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
  1540. {
  1541. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1542. if (varyingIt != varyingMap.end() )
  1543. {
  1544. const Varying& var = varyingIt->second;
  1545. preprocessor.writef("%s%s %s : %s\\\n", first ? "" : "\t, ", var.m_type.c_str(), var.m_name.c_str(), var.m_semantics.c_str() );
  1546. first = false;
  1547. }
  1548. }
  1549. preprocessor.writef(
  1550. ") \\\n"
  1551. "{ \\\n"
  1552. "\tOutput _varying_;"
  1553. );
  1554. for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
  1555. {
  1556. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1557. if (varyingIt != varyingMap.end() )
  1558. {
  1559. const Varying& var = varyingIt->second;
  1560. preprocessor.writef(" \\\n\t%s = %s;", var.m_name.c_str(), var.m_init.c_str() );
  1561. }
  1562. }
  1563. preprocessor.writef(
  1564. "\n#define __RETURN__ \\\n"
  1565. "\t} \\\n"
  1566. "\treturn _varying_"
  1567. );
  1568. }
  1569. }
  1570. }
  1571. if (preprocessor.run(input) )
  1572. {
  1573. BX_TRACE("Input file: %s", filePath);
  1574. BX_TRACE("Output file: %s", outFilePath);
  1575. if (preprocessOnly)
  1576. {
  1577. FileWriter stream(outFilePath);
  1578. if (!stream.open() )
  1579. {
  1580. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  1581. return false;
  1582. }
  1583. if (glsl)
  1584. {
  1585. const char* profile = cmdLine.findOption('p');
  1586. if (NULL == profile)
  1587. {
  1588. stream.write("#ifdef GL_ES\n");
  1589. stream.write("precision highp float;\n");
  1590. stream.write("#endif // GL_ES\n\n");
  1591. }
  1592. else
  1593. {
  1594. stream.writef("#version %s\n\n", profile);
  1595. }
  1596. }
  1597. stream.write(preprocessor.m_preprocessed.c_str(), preprocessor.m_preprocessed.size() );
  1598. stream.close();
  1599. return EXIT_SUCCESS;
  1600. }
  1601. bool compiled = false;
  1602. {
  1603. IStreamWriter* stream = NULL;
  1604. if (NULL != bin2c)
  1605. {
  1606. stream = new Bin2cStream(outFilePath, bin2c);
  1607. }
  1608. else
  1609. {
  1610. stream = new FileWriter(outFilePath);
  1611. }
  1612. if (!stream->open() )
  1613. {
  1614. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  1615. return false;
  1616. }
  1617. if (glsl)
  1618. {
  1619. compiled = compileGLSLShader(cmdLine, preprocessor.m_preprocessed, *stream);
  1620. }
  1621. else
  1622. {
  1623. if (hlsl > 3)
  1624. {
  1625. compiled = compileHLSLShaderDx11(cmdLine, preprocessor.m_preprocessed, *stream);
  1626. }
  1627. else
  1628. {
  1629. compiled = compileHLSLShaderDx9(cmdLine, preprocessor.m_preprocessed, *stream);
  1630. }
  1631. }
  1632. #if SHADERC_DEBUG
  1633. stream->writeString(filePath);
  1634. #endif // SHADERC_DEBUG
  1635. stream->close();
  1636. delete stream;
  1637. }
  1638. if (compiled)
  1639. {
  1640. if (depends)
  1641. {
  1642. std::string ofp = outFilePath;
  1643. ofp += ".d";
  1644. FileWriter stream(ofp.c_str() );
  1645. if (stream.open() )
  1646. {
  1647. stream.write(outFilePath);
  1648. stream.write(":");
  1649. stream.write(preprocessor.m_depends.c_str() );
  1650. stream.write("\n");
  1651. stream.close();
  1652. }
  1653. }
  1654. return EXIT_SUCCESS;
  1655. }
  1656. }
  1657. delete [] data;
  1658. }
  1659. remove(outFilePath);
  1660. fprintf(stderr, "Failed to build shader.\n");
  1661. return EXIT_FAILURE;
  1662. }