shaderc.cpp 44 KB

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