shaderc.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "shaderc.h"
  6. #include <bx/tokenizecmd.h>
  7. #define MAX_TAGS 256
  8. extern "C"
  9. {
  10. #include <fpp.h>
  11. } // extern "C"
  12. namespace bgfx
  13. {
  14. bool g_verbose = false;
  15. #define BGFX_CHUNK_MAGIC_CSH BX_MAKEFOURCC('C', 'S', 'H', 0x2)
  16. #define BGFX_CHUNK_MAGIC_FSH BX_MAKEFOURCC('F', 'S', 'H', 0x4)
  17. #define BGFX_CHUNK_MAGIC_VSH BX_MAKEFOURCC('V', 'S', 'H', 0x4)
  18. static const char* s_ARB_shader_texture_lod[] =
  19. {
  20. "texture2DLod",
  21. "texture2DArrayLod", // BK - interacts with ARB_texture_array.
  22. "texture2DProjLod",
  23. "texture3DLod",
  24. "texture3DProjLod",
  25. "textureCubeLod",
  26. "shadow2DLod",
  27. "shadow2DProjLod",
  28. NULL
  29. // "texture1DLod",
  30. // "texture1DProjLod",
  31. // "shadow1DLod",
  32. // "shadow1DProjLod",
  33. };
  34. static const char* s_EXT_shadow_samplers[] =
  35. {
  36. "shadow2D",
  37. "shadow2DProj",
  38. "sampler2DShadow",
  39. NULL
  40. };
  41. static const char* s_OES_standard_derivatives[] =
  42. {
  43. "dFdx",
  44. "dFdy",
  45. "fwidth",
  46. NULL
  47. };
  48. static const char* s_OES_texture_3D[] =
  49. {
  50. "texture3D",
  51. "texture3DProj",
  52. "texture3DLod",
  53. "texture3DProjLod",
  54. NULL
  55. };
  56. static const char* s_ARB_gpu_shader5[] =
  57. {
  58. "bitfieldReverse",
  59. "floatBitsToInt",
  60. "floatBitsToUint",
  61. "intBitsToFloat",
  62. "uintBitsToFloat",
  63. NULL
  64. };
  65. static const char* s_ARB_shading_language_packing[] =
  66. {
  67. "packHalf2x16",
  68. "unpackHalf2x16",
  69. NULL
  70. };
  71. static const char* s_130[] =
  72. {
  73. "uint",
  74. "uint2",
  75. "uint3",
  76. "uint4",
  77. "isampler3D",
  78. "usampler3D",
  79. NULL
  80. };
  81. static const char* s_textureArray[] =
  82. {
  83. "texture2DArray",
  84. "texture2DArrayLod",
  85. "shadow2DArray",
  86. NULL
  87. };
  88. static const char* s_ARB_texture_multisample[] =
  89. {
  90. "sampler2DMS",
  91. "isampler2DMS",
  92. "usampler2DMS",
  93. NULL
  94. };
  95. static const char* s_texelFetch[] =
  96. {
  97. "texelFetch",
  98. "texelFetchOffset",
  99. NULL
  100. };
  101. const char* s_uniformTypeName[] =
  102. {
  103. "int", "int",
  104. NULL, NULL,
  105. "vec4", "float4",
  106. "mat3", "float3x3",
  107. "mat4", "float4x4",
  108. };
  109. BX_STATIC_ASSERT(BX_COUNTOF(s_uniformTypeName) == UniformType::Count*2);
  110. const char* interpolationDx11(const char* _glsl)
  111. {
  112. if (0 == strcmp(_glsl, "smooth") )
  113. {
  114. return "linear";
  115. }
  116. else if (0 == strcmp(_glsl, "flat") )
  117. {
  118. return "nointerpolation";
  119. }
  120. return _glsl; // centroid, noperspective
  121. }
  122. const char* getUniformTypeName(UniformType::Enum _enum)
  123. {
  124. uint32_t idx = _enum & ~(BGFX_UNIFORM_FRAGMENTBIT|BGFX_UNIFORM_SAMPLERBIT);
  125. if (idx < UniformType::Count)
  126. {
  127. return s_uniformTypeName[idx];
  128. }
  129. return "Unknown uniform type?!";
  130. }
  131. UniformType::Enum nameToUniformTypeEnum(const char* _name)
  132. {
  133. for (uint32_t ii = 0; ii < UniformType::Count*2; ++ii)
  134. {
  135. if (NULL != s_uniformTypeName[ii]
  136. && 0 == strcmp(_name, s_uniformTypeName[ii]) )
  137. {
  138. return UniformType::Enum(ii/2);
  139. }
  140. }
  141. return UniformType::Count;
  142. }
  143. int32_t writef(bx::WriterI* _writer, const char* _format, ...)
  144. {
  145. va_list argList;
  146. va_start(argList, _format);
  147. char temp[2048];
  148. char* out = temp;
  149. int32_t max = sizeof(temp);
  150. int32_t len = bx::vsnprintf(out, max, _format, argList);
  151. if (len > max)
  152. {
  153. out = (char*)alloca(len);
  154. len = bx::vsnprintf(out, len, _format, argList);
  155. }
  156. len = bx::write(_writer, out, len);
  157. va_end(argList);
  158. return len;
  159. }
  160. class Bin2cWriter : public bx::CrtFileWriter
  161. {
  162. public:
  163. Bin2cWriter(const char* _name)
  164. : m_name(_name)
  165. {
  166. }
  167. virtual ~Bin2cWriter()
  168. {
  169. }
  170. virtual void close() BX_OVERRIDE
  171. {
  172. generate();
  173. return bx::CrtFileWriter::close();
  174. }
  175. virtual int32_t write(const void* _data, int32_t _size, bx::Error*) BX_OVERRIDE
  176. {
  177. const char* data = (const char*)_data;
  178. m_buffer.insert(m_buffer.end(), data, data+_size);
  179. return _size;
  180. }
  181. private:
  182. void generate()
  183. {
  184. #define HEX_DUMP_WIDTH 16
  185. #define HEX_DUMP_SPACE_WIDTH 96
  186. #define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
  187. const uint8_t* data = &m_buffer[0];
  188. uint32_t size = (uint32_t)m_buffer.size();
  189. outf("static const uint8_t %s[%d] =\n{\n", m_name.c_str(), size);
  190. if (NULL != data)
  191. {
  192. char hex[HEX_DUMP_SPACE_WIDTH+1];
  193. char ascii[HEX_DUMP_WIDTH+1];
  194. uint32_t hexPos = 0;
  195. uint32_t asciiPos = 0;
  196. for (uint32_t ii = 0; ii < size; ++ii)
  197. {
  198. bx::snprintf(&hex[hexPos], sizeof(hex)-hexPos, "0x%02x, ", data[asciiPos]);
  199. hexPos += 6;
  200. ascii[asciiPos] = isprint(data[asciiPos]) && data[asciiPos] != '\\' ? data[asciiPos] : '.';
  201. asciiPos++;
  202. if (HEX_DUMP_WIDTH == asciiPos)
  203. {
  204. ascii[asciiPos] = '\0';
  205. outf("\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
  206. data += asciiPos;
  207. hexPos = 0;
  208. asciiPos = 0;
  209. }
  210. }
  211. if (0 != asciiPos)
  212. {
  213. ascii[asciiPos] = '\0';
  214. outf("\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
  215. }
  216. }
  217. outf("};\n");
  218. #undef HEX_DUMP_WIDTH
  219. #undef HEX_DUMP_SPACE_WIDTH
  220. #undef HEX_DUMP_FORMAT
  221. }
  222. int32_t outf(const char* _format, ...)
  223. {
  224. va_list argList;
  225. va_start(argList, _format);
  226. char temp[2048];
  227. char* out = temp;
  228. int32_t max = sizeof(temp);
  229. int32_t len = bx::vsnprintf(out, max, _format, argList);
  230. if (len > max)
  231. {
  232. out = (char*)alloca(len);
  233. len = bx::vsnprintf(out, len, _format, argList);
  234. }
  235. bx::Error err;
  236. int32_t size = bx::CrtFileWriter::write(out, len, &err);
  237. va_end(argList);
  238. return size;
  239. }
  240. std::string m_filePath;
  241. std::string m_name;
  242. typedef std::vector<uint8_t> Buffer;
  243. Buffer m_buffer;
  244. };
  245. struct Varying
  246. {
  247. std::string m_precision;
  248. std::string m_interpolation;
  249. std::string m_name;
  250. std::string m_type;
  251. std::string m_init;
  252. std::string m_semantics;
  253. };
  254. typedef std::unordered_map<std::string, Varying> VaryingMap;
  255. class File
  256. {
  257. public:
  258. File(const char* _filePath)
  259. : m_data(NULL)
  260. {
  261. bx::CrtFileReader reader;
  262. if (bx::open(&reader, _filePath) )
  263. {
  264. m_size = (uint32_t)bx::getSize(&reader);
  265. m_data = new char[m_size+1];
  266. m_size = (uint32_t)bx::read(&reader, m_data, m_size);
  267. bx::close(&reader);
  268. if (m_data[0] == '\xef'
  269. && m_data[1] == '\xbb'
  270. && m_data[2] == '\xbf')
  271. {
  272. memmove(m_data, &m_data[3], m_size-3);
  273. m_size -= 3;
  274. }
  275. m_data[m_size] = '\0';
  276. }
  277. }
  278. ~File()
  279. {
  280. delete [] m_data;
  281. }
  282. const char* getData() const
  283. {
  284. return m_data;
  285. }
  286. uint32_t getSize() const
  287. {
  288. return m_size;
  289. }
  290. private:
  291. char* m_data;
  292. uint32_t m_size;
  293. };
  294. char* strInsert(char* _str, const char* _insert)
  295. {
  296. size_t len = strlen(_insert);
  297. memmove(&_str[len], _str, strlen(_str) );
  298. memcpy(_str, _insert, len);
  299. return _str + len;
  300. }
  301. void strReplace(char* _str, const char* _find, const char* _replace)
  302. {
  303. const size_t len = strlen(_find);
  304. char* replace = (char*)alloca(len+1);
  305. bx::strlcpy(replace, _replace, len+1);
  306. for (size_t ii = strlen(replace); ii < len; ++ii)
  307. {
  308. replace[ii] = ' ';
  309. }
  310. replace[len] = '\0';
  311. BX_CHECK(len >= strlen(_replace), "");
  312. for (char* ptr = strstr(_str, _find); NULL != ptr; ptr = strstr(ptr + len, _find) )
  313. {
  314. memcpy(ptr, replace, len);
  315. }
  316. }
  317. void strNormalizeEol(char* _str)
  318. {
  319. strReplace(_str, "\r\n", "\n");
  320. strReplace(_str, "\r", "\n");
  321. }
  322. void printCode(const char* _code, int32_t _line, int32_t _start, int32_t _end, int32_t _column)
  323. {
  324. fprintf(stderr, "Code:\n---\n");
  325. LineReader lr(_code);
  326. for (int32_t line = 1; !lr.isEof() && line < _end; ++line)
  327. {
  328. if (line >= _start)
  329. {
  330. if (_line == line)
  331. {
  332. fprintf(stderr, "\n");
  333. fprintf(stderr, ">>> %3d: %s", line, lr.getLine().c_str() );
  334. if (-1 != _column)
  335. {
  336. fprintf(stderr, ">>> %3d: %*s\n", _column, _column, "^");
  337. }
  338. fprintf(stderr, "\n");
  339. }
  340. else
  341. {
  342. fprintf(stderr, " %3d: %s", line, lr.getLine().c_str() );
  343. }
  344. }
  345. else
  346. {
  347. lr.skipLine();
  348. }
  349. }
  350. fprintf(stderr, "---\n");
  351. }
  352. void writeFile(const char* _filePath, const void* _data, int32_t _size)
  353. {
  354. bx::CrtFileWriter out;
  355. if (bx::open(&out, _filePath) )
  356. {
  357. bx::write(&out, _data, _size);
  358. bx::close(&out);
  359. }
  360. }
  361. struct Preprocessor
  362. {
  363. Preprocessor(const char* _filePath, bool _essl)
  364. : m_tagptr(m_tags)
  365. , m_scratchPos(0)
  366. , m_fgetsPos(0)
  367. {
  368. m_tagptr->tag = FPPTAG_USERDATA;
  369. m_tagptr->data = this;
  370. m_tagptr++;
  371. m_tagptr->tag = FPPTAG_DEPENDS;
  372. m_tagptr->data = (void*)fppDepends;
  373. m_tagptr++;
  374. m_tagptr->tag = FPPTAG_INPUT;
  375. m_tagptr->data = (void*)fppInput;
  376. m_tagptr++;
  377. m_tagptr->tag = FPPTAG_OUTPUT;
  378. m_tagptr->data = (void*)fppOutput;
  379. m_tagptr++;
  380. m_tagptr->tag = FPPTAG_ERROR;
  381. m_tagptr->data = (void*)fppError;
  382. m_tagptr++;
  383. m_tagptr->tag = FPPTAG_IGNOREVERSION;
  384. m_tagptr->data = (void*)0;
  385. m_tagptr++;
  386. m_tagptr->tag = FPPTAG_LINE;
  387. m_tagptr->data = (void*)0;
  388. m_tagptr++;
  389. m_tagptr->tag = FPPTAG_INPUT_NAME;
  390. m_tagptr->data = scratch(_filePath);
  391. m_tagptr++;
  392. if (!_essl)
  393. {
  394. m_default = "#define lowp\n#define mediump\n#define highp\n";
  395. }
  396. }
  397. void setDefine(const char* _define)
  398. {
  399. m_tagptr->tag = FPPTAG_DEFINE;
  400. m_tagptr->data = scratch(_define);
  401. m_tagptr++;
  402. }
  403. void setDefaultDefine(const char* _name)
  404. {
  405. char temp[1024];
  406. bx::snprintf(temp, BX_COUNTOF(temp)
  407. , "#ifndef %s\n"
  408. "# define %s 0\n"
  409. "#endif // %s\n"
  410. "\n"
  411. , _name
  412. , _name
  413. , _name
  414. );
  415. m_default += temp;
  416. }
  417. void writef(const char* _format, ...)
  418. {
  419. va_list argList;
  420. va_start(argList, _format);
  421. bx::stringPrintfVargs(m_default, _format, argList);
  422. va_end(argList);
  423. }
  424. void addInclude(const char* _includeDir)
  425. {
  426. char* start = scratch(_includeDir);
  427. for (char* split = strchr(start, ';'); NULL != split; split = strchr(start, ';') )
  428. {
  429. *split = '\0';
  430. m_tagptr->tag = FPPTAG_INCLUDE_DIR;
  431. m_tagptr->data = start;
  432. m_tagptr++;
  433. start = split + 1;
  434. }
  435. m_tagptr->tag = FPPTAG_INCLUDE_DIR;
  436. m_tagptr->data = start;
  437. m_tagptr++;
  438. }
  439. void addDependency(const char* _fileName)
  440. {
  441. m_depends += " \\\n ";
  442. m_depends += _fileName;
  443. }
  444. bool run(const char* _input)
  445. {
  446. m_fgetsPos = 0;
  447. m_preprocessed.clear();
  448. m_input = m_default;
  449. m_input += "\n\n";
  450. size_t len = strlen(_input)+1;
  451. char* temp = new char[len];
  452. bx::eolLF(temp, len, _input);
  453. m_input += temp;
  454. delete [] temp;
  455. fppTag* tagptr = m_tagptr;
  456. tagptr->tag = FPPTAG_END;
  457. tagptr->data = 0;
  458. tagptr++;
  459. int result = fppPreProcess(m_tags);
  460. return 0 == result;
  461. }
  462. char* fgets(char* _buffer, int _size)
  463. {
  464. int ii = 0;
  465. for (char ch = m_input[m_fgetsPos]; m_fgetsPos < m_input.size() && ii < _size-1; ch = m_input[++m_fgetsPos])
  466. {
  467. _buffer[ii++] = ch;
  468. if (ch == '\n' || ii == _size)
  469. {
  470. _buffer[ii] = '\0';
  471. m_fgetsPos++;
  472. return _buffer;
  473. }
  474. }
  475. return NULL;
  476. }
  477. static void fppDepends(char* _fileName, void* _userData)
  478. {
  479. Preprocessor* thisClass = (Preprocessor*)_userData;
  480. thisClass->addDependency(_fileName);
  481. }
  482. static char* fppInput(char* _buffer, int _size, void* _userData)
  483. {
  484. Preprocessor* thisClass = (Preprocessor*)_userData;
  485. return thisClass->fgets(_buffer, _size);
  486. }
  487. static void fppOutput(int _ch, void* _userData)
  488. {
  489. Preprocessor* thisClass = (Preprocessor*)_userData;
  490. thisClass->m_preprocessed += _ch;
  491. }
  492. static void fppError(void* /*_userData*/, char* _format, va_list _vargs)
  493. {
  494. vfprintf(stderr, _format, _vargs);
  495. }
  496. char* scratch(const char* _str)
  497. {
  498. char* result = &m_scratch[m_scratchPos];
  499. strcpy(result, _str);
  500. m_scratchPos += (uint32_t)strlen(_str)+1;
  501. return result;
  502. }
  503. fppTag m_tags[MAX_TAGS];
  504. fppTag* m_tagptr;
  505. std::string m_depends;
  506. std::string m_default;
  507. std::string m_input;
  508. std::string m_preprocessed;
  509. char m_scratch[16<<10];
  510. uint32_t m_scratchPos;
  511. uint32_t m_fgetsPos;
  512. };
  513. typedef std::vector<std::string> InOut;
  514. uint32_t parseInOut(InOut& _inout, const char* _str, const char* _eol)
  515. {
  516. uint32_t hash = 0;
  517. _str = bx::strws(_str);
  518. if (_str < _eol)
  519. {
  520. const char* delim;
  521. do
  522. {
  523. delim = strpbrk(_str, " ,");
  524. if (NULL != delim)
  525. {
  526. delim = delim > _eol ? _eol : delim;
  527. std::string token;
  528. token.assign(_str, delim-_str);
  529. _inout.push_back(token);
  530. _str = bx::strws(delim + 1);
  531. }
  532. }
  533. while (delim < _eol && _str < _eol && NULL != delim);
  534. std::sort(_inout.begin(), _inout.end() );
  535. bx::HashMurmur2A murmur;
  536. murmur.begin();
  537. for (InOut::const_iterator it = _inout.begin(), itEnd = _inout.end(); it != itEnd; ++it)
  538. {
  539. murmur.add(it->c_str(), (uint32_t)it->size() );
  540. }
  541. hash = murmur.end();
  542. }
  543. return hash;
  544. }
  545. void addFragData(Preprocessor& _preprocessor, char* _data, uint32_t _idx, bool _comma)
  546. {
  547. char find[32];
  548. bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx);
  549. char replace[32];
  550. bx::snprintf(replace, sizeof(replace), "gl_FragData_%d_", _idx);
  551. strReplace(_data, find, replace);
  552. _preprocessor.writef(
  553. " \\\n\t%sout vec4 gl_FragData_%d_ : SV_TARGET%d"
  554. , _comma ? ", " : " "
  555. , _idx
  556. , _idx
  557. );
  558. }
  559. void voidFragData(char* _data, uint32_t _idx)
  560. {
  561. char find[32];
  562. bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx);
  563. strReplace(_data, find, "bgfx_VoidFrag");
  564. }
  565. // c - compute
  566. // d - domain
  567. // f - fragment
  568. // g - geometry
  569. // h - hull
  570. // v - vertex
  571. //
  572. // OpenGL #version Features Direct3D Features Shader Model
  573. // 2.1 120 vf 9.0 vf 2.0
  574. // 3.0 130
  575. // 3.1 140
  576. // 3.2 150 vgf
  577. // 3.3 330 10.0 vgf 4.0
  578. // 4.0 400 vhdgf
  579. // 4.1 410
  580. // 4.2 420 11.0 vhdgf+c 5.0
  581. // 4.3 430 vhdgf+c
  582. // 4.4 440
  583. void help(const char* _error = NULL)
  584. {
  585. if (NULL != _error)
  586. {
  587. fprintf(stderr, "Error:\n%s\n\n", _error);
  588. }
  589. fprintf(stderr
  590. , "shaderc, bgfx shader compiler tool\n"
  591. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  592. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  593. );
  594. fprintf(stderr
  595. , "Usage: shaderc -f <in> -o <out> --type <v/f> --platform <platform>\n"
  596. "\n"
  597. "Options:\n"
  598. " -f <file path> Input file path.\n"
  599. " -i <include path> Include path (for multiple paths use semicolon).\n"
  600. " -o <file path> Output file path.\n"
  601. " --bin2c <file path> Generate C header file.\n"
  602. " --depends Generate makefile style depends file.\n"
  603. " --platform <platform> Target platform.\n"
  604. " android\n"
  605. " asm.js\n"
  606. " ios\n"
  607. " linux\n"
  608. " nacl\n"
  609. " osx\n"
  610. " windows\n"
  611. " --preprocess Preprocess only.\n"
  612. " --define <defines> Add defines to preprocessor (semicolon separated).\n"
  613. " --raw Do not process shader. No preprocessor, and no glsl-optimizer (GLSL only).\n"
  614. " --type <type> Shader type (vertex, fragment)\n"
  615. " --varyingdef <file path> Path to varying.def.sc file.\n"
  616. " --verbose Verbose.\n"
  617. "\n"
  618. "Options (DX9 and DX11 only):\n"
  619. "\n"
  620. " --debug Debug information.\n"
  621. " --disasm Disassemble compiled shader.\n"
  622. " -p, --profile <profile> Shader model (f.e. ps_3_0).\n"
  623. " -O <level> Optimization level (0, 1, 2, 3).\n"
  624. " --Werror Treat warnings as errors.\n"
  625. "\n"
  626. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  627. );
  628. }
  629. int compileShader(int _argc, const char* _argv[])
  630. {
  631. bx::CommandLine cmdLine(_argc, _argv);
  632. if (cmdLine.hasArg('h', "help") )
  633. {
  634. help();
  635. return EXIT_FAILURE;
  636. }
  637. g_verbose = cmdLine.hasArg("verbose");
  638. const char* filePath = cmdLine.findOption('f');
  639. if (NULL == filePath)
  640. {
  641. help("Shader file name must be specified.");
  642. return EXIT_FAILURE;
  643. }
  644. const char* outFilePath = cmdLine.findOption('o');
  645. if (NULL == outFilePath)
  646. {
  647. help("Output file name must be specified.");
  648. return EXIT_FAILURE;
  649. }
  650. const char* type = cmdLine.findOption('\0', "type");
  651. if (NULL == type)
  652. {
  653. help("Must specify shader type.");
  654. return EXIT_FAILURE;
  655. }
  656. const char* platform = cmdLine.findOption('\0', "platform");
  657. if (NULL == platform)
  658. {
  659. platform = "";
  660. }
  661. bool raw = cmdLine.hasArg('\0', "raw");
  662. uint32_t glsl = 0;
  663. uint32_t essl = 0;
  664. uint32_t hlsl = 0;
  665. uint32_t d3d = 11;
  666. uint32_t metal = 0;
  667. uint32_t pssl = 0;
  668. uint32_t spirv = 0;
  669. const char* profile = cmdLine.findOption('p', "profile");
  670. if (NULL != profile)
  671. {
  672. if (0 == strncmp(&profile[1], "s_4_0_level", 11) )
  673. {
  674. hlsl = 2;
  675. }
  676. else if (0 == strncmp(&profile[1], "s_3", 3) )
  677. {
  678. hlsl = 3;
  679. d3d = 9;
  680. }
  681. else if (0 == strncmp(&profile[1], "s_4", 3) )
  682. {
  683. hlsl = 4;
  684. }
  685. else if (0 == strncmp(&profile[1], "s_5", 3) )
  686. {
  687. hlsl = 5;
  688. }
  689. else if (0 == strcmp(profile, "metal") )
  690. {
  691. metal = 1;
  692. }
  693. else if (0 == strcmp(profile, "pssl") )
  694. {
  695. pssl = 1;
  696. }
  697. else if (0 == strcmp(profile, "spirv") )
  698. {
  699. spirv = 1;
  700. }
  701. else
  702. {
  703. glsl = atoi(profile);
  704. }
  705. }
  706. else
  707. {
  708. essl = 2;
  709. }
  710. const char* bin2c = NULL;
  711. if (cmdLine.hasArg("bin2c") )
  712. {
  713. bin2c = cmdLine.findOption("bin2c");
  714. if (NULL == bin2c)
  715. {
  716. bin2c = bx::baseName(outFilePath);
  717. uint32_t len = (uint32_t)strlen(bin2c);
  718. char* temp = (char*)alloca(len+1);
  719. for (char *out = temp; *bin2c != '\0';)
  720. {
  721. char ch = *bin2c++;
  722. if (isalnum(ch) )
  723. {
  724. *out++ = ch;
  725. }
  726. else
  727. {
  728. *out++ = '_';
  729. }
  730. }
  731. temp[len] = '\0';
  732. bin2c = temp;
  733. }
  734. }
  735. bool depends = cmdLine.hasArg("depends");
  736. bool preprocessOnly = cmdLine.hasArg("preprocess");
  737. const char* includeDir = cmdLine.findOption('i');
  738. Preprocessor preprocessor(filePath, 0 != essl);
  739. for (int ii = 1; NULL != includeDir; ++ii)
  740. {
  741. preprocessor.addInclude(includeDir);
  742. includeDir = cmdLine.findOption(ii, 'i');
  743. }
  744. std::string dir;
  745. {
  746. const char* base = bx::baseName(filePath);
  747. if (base != filePath)
  748. {
  749. dir.assign(filePath, base-filePath);
  750. preprocessor.addInclude(dir.c_str() );
  751. }
  752. }
  753. const char* defines = cmdLine.findOption("define");
  754. while (NULL != defines
  755. && '\0' != *defines)
  756. {
  757. defines = bx::strws(defines);
  758. const char* eol = strchr(defines, ';');
  759. if (NULL == eol)
  760. {
  761. eol = defines + strlen(defines);
  762. }
  763. std::string define(defines, eol);
  764. preprocessor.setDefine(define.c_str() );
  765. defines = ';' == *eol ? eol+1 : eol;
  766. }
  767. preprocessor.setDefaultDefine("BX_PLATFORM_ANDROID");
  768. preprocessor.setDefaultDefine("BX_PLATFORM_EMSCRIPTEN");
  769. preprocessor.setDefaultDefine("BX_PLATFORM_IOS");
  770. preprocessor.setDefaultDefine("BX_PLATFORM_LINUX");
  771. preprocessor.setDefaultDefine("BX_PLATFORM_NACL");
  772. preprocessor.setDefaultDefine("BX_PLATFORM_OSX");
  773. preprocessor.setDefaultDefine("BX_PLATFORM_PS4");
  774. preprocessor.setDefaultDefine("BX_PLATFORM_WINDOWS");
  775. preprocessor.setDefaultDefine("BX_PLATFORM_XBOX360");
  776. preprocessor.setDefaultDefine("BX_PLATFORM_XBOXONE");
  777. // preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_ESSL");
  778. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_GLSL");
  779. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_HLSL");
  780. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_METAL");
  781. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_PSSL");
  782. preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_SPIRV");
  783. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_COMPUTE");
  784. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_FRAGMENT");
  785. preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_VERTEX");
  786. char glslDefine[128];
  787. bx::snprintf(glslDefine, BX_COUNTOF(glslDefine)
  788. , "BGFX_SHADER_LANGUAGE_GLSL=%d"
  789. , essl ? 1 : glsl
  790. );
  791. if (0 == bx::stricmp(platform, "android") )
  792. {
  793. preprocessor.setDefine("BX_PLATFORM_ANDROID=1");
  794. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  795. }
  796. else if (0 == bx::stricmp(platform, "asm.js") )
  797. {
  798. preprocessor.setDefine("BX_PLATFORM_EMSCRIPTEN=1");
  799. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  800. }
  801. else if (0 == bx::stricmp(platform, "ios") )
  802. {
  803. preprocessor.setDefine("BX_PLATFORM_IOS=1");
  804. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  805. }
  806. else if (0 == bx::stricmp(platform, "linux") )
  807. {
  808. preprocessor.setDefine("BX_PLATFORM_LINUX=1");
  809. if (0 != spirv)
  810. {
  811. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_SPIRV=1");
  812. }
  813. else
  814. {
  815. preprocessor.setDefine(glslDefine);
  816. }
  817. }
  818. else if (0 == bx::stricmp(platform, "nacl") )
  819. {
  820. preprocessor.setDefine("BX_PLATFORM_NACL=1");
  821. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
  822. }
  823. else if (0 == bx::stricmp(platform, "osx") )
  824. {
  825. preprocessor.setDefine("BX_PLATFORM_OSX=1");
  826. preprocessor.setDefine(glslDefine);
  827. char temp[256];
  828. bx::snprintf(temp, sizeof(temp), "BGFX_SHADER_LANGUAGE_METAL=%d", metal);
  829. preprocessor.setDefine(temp);
  830. }
  831. else if (0 == bx::stricmp(platform, "windows") )
  832. {
  833. preprocessor.setDefine("BX_PLATFORM_WINDOWS=1");
  834. char temp[256];
  835. bx::snprintf(temp, sizeof(temp), "BGFX_SHADER_LANGUAGE_HLSL=%d", hlsl);
  836. preprocessor.setDefine(temp);
  837. }
  838. else if (0 == bx::stricmp(platform, "xbox360") )
  839. {
  840. preprocessor.setDefine("BX_PLATFORM_XBOX360=1");
  841. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=3");
  842. }
  843. else if (0 == bx::stricmp(platform, "orbis") )
  844. {
  845. preprocessor.setDefine("BX_PLATFORM_PS4=1");
  846. preprocessor.setDefine("BGFX_SHADER_LANGUAGE_PSSL=1");
  847. preprocessor.setDefine("lit=lit_reserved");
  848. }
  849. preprocessor.setDefine("M_PI=3.1415926535897932384626433832795");
  850. char shaderType = tolower(type[0]);
  851. switch (shaderType)
  852. {
  853. case 'c':
  854. preprocessor.setDefine("BGFX_SHADER_TYPE_COMPUTE=1");
  855. break;
  856. case 'f':
  857. preprocessor.setDefine("BGFX_SHADER_TYPE_FRAGMENT=1");
  858. break;
  859. case 'v':
  860. preprocessor.setDefine("BGFX_SHADER_TYPE_VERTEX=1");
  861. break;
  862. default:
  863. fprintf(stderr, "Unknown type: %s?!", type);
  864. return EXIT_FAILURE;
  865. }
  866. bool compiled = false;
  867. bx::CrtFileReader reader;
  868. if (!bx::open(&reader, filePath) )
  869. {
  870. fprintf(stderr, "Unable to open file '%s'.\n", filePath);
  871. }
  872. else
  873. {
  874. VaryingMap varyingMap;
  875. std::string defaultVarying = dir + "varying.def.sc";
  876. const char* varyingdef = cmdLine.findOption("varyingdef", defaultVarying.c_str() );
  877. File attribdef(varyingdef);
  878. const char* parse = attribdef.getData();
  879. if (NULL != parse
  880. && *parse != '\0')
  881. {
  882. preprocessor.addDependency(varyingdef);
  883. }
  884. else
  885. {
  886. fprintf(stderr, "ERROR: Failed to parse varying def file: \"%s\" No input/output semantics will be generated in the code!\n", varyingdef);
  887. }
  888. while (NULL != parse
  889. && *parse != '\0')
  890. {
  891. parse = bx::strws(parse);
  892. const char* eol = strchr(parse, ';');
  893. if (NULL == eol)
  894. {
  895. eol = bx::streol(parse);
  896. }
  897. if (NULL != eol)
  898. {
  899. const char* precision = NULL;
  900. const char* interpolation = NULL;
  901. const char* typen = parse;
  902. if (0 == strncmp(typen, "lowp", 4)
  903. || 0 == strncmp(typen, "mediump", 7)
  904. || 0 == strncmp(typen, "highp", 5) )
  905. {
  906. precision = typen;
  907. typen = parse = bx::strws(bx::strword(parse) );
  908. }
  909. if (0 == strncmp(typen, "flat", 4)
  910. || 0 == strncmp(typen, "smooth", 6)
  911. || 0 == strncmp(typen, "noperspective", 13)
  912. || 0 == strncmp(typen, "centroid", 8) )
  913. {
  914. interpolation = typen;
  915. typen = parse = bx::strws(bx::strword(parse) );
  916. }
  917. const char* name = parse = bx::strws(bx::strword(parse) );
  918. const char* column = parse = bx::strws(bx::strword(parse) );
  919. const char* semantics = parse = bx::strws( (*parse == ':' ? ++parse : parse) );
  920. const char* assign = parse = bx::strws(bx::strword(parse) );
  921. const char* init = parse = bx::strws( (*parse == '=' ? ++parse : parse) );
  922. if (typen < eol
  923. && name < eol
  924. && column < eol
  925. && ':' == *column
  926. && semantics < eol)
  927. {
  928. Varying var;
  929. if (NULL != precision)
  930. {
  931. var.m_precision.assign(precision, bx::strword(precision)-precision);
  932. }
  933. if (NULL != interpolation)
  934. {
  935. var.m_interpolation.assign(interpolation, bx::strword(interpolation)-interpolation);
  936. }
  937. var.m_type.assign(typen, bx::strword(typen)-typen);
  938. var.m_name.assign(name, bx::strword(name)-name);
  939. var.m_semantics.assign(semantics, bx::strword(semantics)-semantics);
  940. if (d3d == 9
  941. && var.m_semantics == "BITANGENT")
  942. {
  943. var.m_semantics = "BINORMAL";
  944. }
  945. if (assign < eol
  946. && '=' == *assign
  947. && init < eol)
  948. {
  949. var.m_init.assign(init, eol-init);
  950. }
  951. varyingMap.insert(std::make_pair(var.m_name, var) );
  952. }
  953. parse = bx::strws(bx::strnl(eol) );
  954. }
  955. }
  956. InOut shaderInputs;
  957. InOut shaderOutputs;
  958. uint32_t inputHash = 0;
  959. uint32_t outputHash = 0;
  960. char* data;
  961. char* input;
  962. {
  963. const size_t padding = 4096;
  964. uint32_t size = (uint32_t)bx::getSize(&reader);
  965. data = new char[size+padding+1];
  966. size = (uint32_t)bx::read(&reader, data, size);
  967. if (data[0] == '\xef'
  968. && data[1] == '\xbb'
  969. && data[2] == '\xbf')
  970. {
  971. memmove(data, &data[3], size-3);
  972. size -= 3;
  973. }
  974. // Compiler generates "error X3000: syntax error: unexpected end of file"
  975. // if input doesn't have empty line at EOF.
  976. data[size] = '\n';
  977. memset(&data[size+1], 0, padding);
  978. bx::close(&reader);
  979. if (!raw)
  980. {
  981. // To avoid commented code being recognized as used feature,
  982. // first preprocess pass is used to strip all comments before
  983. // substituting code.
  984. preprocessor.run(data);
  985. delete [] data;
  986. size = (uint32_t)preprocessor.m_preprocessed.size();
  987. data = new char[size+padding+1];
  988. memcpy(data, preprocessor.m_preprocessed.c_str(), size);
  989. memset(&data[size], 0, padding+1);
  990. }
  991. strNormalizeEol(data);
  992. input = const_cast<char*>(bx::strws(data) );
  993. while (input[0] == '$')
  994. {
  995. const char* str = bx::strws(input+1);
  996. const char* eol = bx::streol(str);
  997. const char* nl = bx::strnl(eol);
  998. input = const_cast<char*>(nl);
  999. if (0 == strncmp(str, "input", 5) )
  1000. {
  1001. str += 5;
  1002. const char* comment = strstr(str, "//");
  1003. eol = NULL != comment && comment < eol ? comment : eol;
  1004. inputHash = parseInOut(shaderInputs, str, eol);
  1005. }
  1006. else if (0 == strncmp(str, "output", 6) )
  1007. {
  1008. str += 6;
  1009. const char* comment = strstr(str, "//");
  1010. eol = NULL != comment && comment < eol ? comment : eol;
  1011. outputHash = parseInOut(shaderOutputs, str, eol);
  1012. }
  1013. else if (0 == strncmp(str, "raw", 3) )
  1014. {
  1015. raw = true;
  1016. str += 3;
  1017. }
  1018. input = const_cast<char*>(bx::strws(input) );
  1019. }
  1020. }
  1021. if (raw)
  1022. {
  1023. bx::CrtFileWriter* writer = NULL;
  1024. if (NULL != bin2c)
  1025. {
  1026. writer = new Bin2cWriter(bin2c);
  1027. }
  1028. else
  1029. {
  1030. writer = new bx::CrtFileWriter;
  1031. }
  1032. if (!bx::open(writer, outFilePath) )
  1033. {
  1034. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  1035. return EXIT_FAILURE;
  1036. }
  1037. if ('f' == shaderType)
  1038. {
  1039. bx::write(writer, BGFX_CHUNK_MAGIC_FSH);
  1040. bx::write(writer, inputHash);
  1041. }
  1042. else if ('v' == shaderType)
  1043. {
  1044. bx::write(writer, BGFX_CHUNK_MAGIC_VSH);
  1045. bx::write(writer, outputHash);
  1046. }
  1047. else
  1048. {
  1049. bx::write(writer, BGFX_CHUNK_MAGIC_CSH);
  1050. bx::write(writer, outputHash);
  1051. }
  1052. if (0 != glsl)
  1053. {
  1054. bx::write(writer, uint16_t(0) );
  1055. uint32_t shaderSize = (uint32_t)strlen(input);
  1056. bx::write(writer, shaderSize);
  1057. bx::write(writer, input, shaderSize);
  1058. bx::write(writer, uint8_t(0) );
  1059. compiled = true;
  1060. }
  1061. else if (0 != pssl)
  1062. {
  1063. compiled = compilePSSLShader(cmdLine, 0, input, writer);
  1064. }
  1065. else
  1066. {
  1067. compiled = compileHLSLShader(cmdLine, d3d, input, writer);
  1068. }
  1069. bx::close(writer);
  1070. delete writer;
  1071. }
  1072. else if ('c' == shaderType) // Compute
  1073. {
  1074. char* entry = strstr(input, "void main()");
  1075. if (NULL == entry)
  1076. {
  1077. fprintf(stderr, "Shader entry point 'void main()' is not found.\n");
  1078. }
  1079. else
  1080. {
  1081. if (0 != glsl
  1082. || 0 != essl
  1083. || 0 != metal)
  1084. {
  1085. }
  1086. else
  1087. {
  1088. preprocessor.writef(
  1089. "#define lowp\n"
  1090. "#define mediump\n"
  1091. "#define highp\n"
  1092. "#define ivec2 int2\n"
  1093. "#define ivec3 int3\n"
  1094. "#define ivec4 int4\n"
  1095. "#define uvec2 uint2\n"
  1096. "#define uvec3 uint3\n"
  1097. "#define uvec4 uint4\n"
  1098. "#define vec2 float2\n"
  1099. "#define vec3 float3\n"
  1100. "#define vec4 float4\n"
  1101. "#define mat2 float2x2\n"
  1102. "#define mat3 float3x3\n"
  1103. "#define mat4 float4x4\n"
  1104. );
  1105. entry[4] = '_';
  1106. preprocessor.writef("#define void_main()");
  1107. preprocessor.writef(" \\\n\tvoid main(");
  1108. uint32_t arg = 0;
  1109. const bool hasLocalInvocationID = NULL != strstr(input, "gl_LocalInvocationID");
  1110. const bool hasLocalInvocationIndex = NULL != strstr(input, "gl_LocalInvocationIndex");
  1111. const bool hasGlobalInvocationID = NULL != strstr(input, "gl_GlobalInvocationID");
  1112. const bool hasWorkGroupID = NULL != strstr(input, "gl_WorkGroupID");
  1113. if (hasLocalInvocationID)
  1114. {
  1115. preprocessor.writef(
  1116. " \\\n\t%sint3 gl_LocalInvocationID : SV_GroupThreadID"
  1117. , arg++ > 0 ? ", " : " "
  1118. );
  1119. }
  1120. if (hasLocalInvocationIndex)
  1121. {
  1122. preprocessor.writef(
  1123. " \\\n\t%sint gl_LocalInvocationIndex : SV_GroupIndex"
  1124. , arg++ > 0 ? ", " : " "
  1125. );
  1126. }
  1127. if (hasGlobalInvocationID)
  1128. {
  1129. preprocessor.writef(
  1130. " \\\n\t%sint3 gl_GlobalInvocationID : SV_DispatchThreadID"
  1131. , arg++ > 0 ? ", " : " "
  1132. );
  1133. }
  1134. if (hasWorkGroupID)
  1135. {
  1136. preprocessor.writef(
  1137. " \\\n\t%sint3 gl_WorkGroupID : SV_GroupID"
  1138. , arg++ > 0 ? ", " : " "
  1139. );
  1140. }
  1141. preprocessor.writef(
  1142. " \\\n\t)\n"
  1143. );
  1144. }
  1145. if (preprocessor.run(input) )
  1146. {
  1147. BX_TRACE("Input file: %s", filePath);
  1148. BX_TRACE("Output file: %s", outFilePath);
  1149. if (preprocessOnly)
  1150. {
  1151. bx::CrtFileWriter writer;
  1152. if (!bx::open(&writer, outFilePath) )
  1153. {
  1154. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  1155. return EXIT_FAILURE;
  1156. }
  1157. bx::write(&writer, preprocessor.m_preprocessed.c_str(), (int32_t)preprocessor.m_preprocessed.size() );
  1158. bx::close(&writer);
  1159. return EXIT_SUCCESS;
  1160. }
  1161. {
  1162. bx::CrtFileWriter* writer = NULL;
  1163. if (NULL != bin2c)
  1164. {
  1165. writer = new Bin2cWriter(bin2c);
  1166. }
  1167. else
  1168. {
  1169. writer = new bx::CrtFileWriter;
  1170. }
  1171. if (!bx::open(writer, outFilePath) )
  1172. {
  1173. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  1174. return EXIT_FAILURE;
  1175. }
  1176. bx::write(writer, BGFX_CHUNK_MAGIC_CSH);
  1177. bx::write(writer, outputHash);
  1178. if (0 != glsl
  1179. || 0 != essl)
  1180. {
  1181. std::string code;
  1182. if (essl)
  1183. {
  1184. bx::stringPrintf(code, "#version 310 es\n");
  1185. }
  1186. else
  1187. {
  1188. bx::stringPrintf(code, "#version %d\n", glsl == 0 ? 430 : glsl);
  1189. }
  1190. code += preprocessor.m_preprocessed;
  1191. #if 1
  1192. bx::write(writer, uint16_t(0) );
  1193. uint32_t shaderSize = (uint32_t)code.size();
  1194. bx::write(writer, shaderSize);
  1195. bx::write(writer, code.c_str(), shaderSize);
  1196. bx::write(writer, uint8_t(0) );
  1197. compiled = true;
  1198. #else
  1199. compiled = compileGLSLShader(cmdLine, essl, code, writer);
  1200. #endif // 0
  1201. }
  1202. else if (0 != spirv)
  1203. {
  1204. compiled = compileSPIRVShader(cmdLine, 0, preprocessor.m_preprocessed, writer);
  1205. }
  1206. else if (0 != pssl)
  1207. {
  1208. compiled = compilePSSLShader(cmdLine, 0, preprocessor.m_preprocessed, writer);
  1209. }
  1210. else
  1211. {
  1212. compiled = compileHLSLShader(cmdLine, d3d, preprocessor.m_preprocessed, writer);
  1213. }
  1214. bx::close(writer);
  1215. delete writer;
  1216. }
  1217. if (compiled)
  1218. {
  1219. if (depends)
  1220. {
  1221. std::string ofp = outFilePath;
  1222. ofp += ".d";
  1223. bx::CrtFileWriter writer;
  1224. if (bx::open(&writer, ofp.c_str() ) )
  1225. {
  1226. writef(&writer, "%s : %s\n", outFilePath, preprocessor.m_depends.c_str() );
  1227. bx::close(&writer);
  1228. }
  1229. }
  1230. }
  1231. }
  1232. }
  1233. }
  1234. else // Vertex/Fragment
  1235. {
  1236. char* entry = strstr(input, "void main()");
  1237. if (NULL == entry)
  1238. {
  1239. fprintf(stderr, "Shader entry point 'void main()' is not found.\n");
  1240. }
  1241. else
  1242. {
  1243. if (0 != glsl
  1244. || 0 != essl
  1245. || 0 != metal)
  1246. {
  1247. if (0 == essl)
  1248. {
  1249. // bgfx shadow2D/Proj behave like EXT_shadow_samplers
  1250. // not as GLSL language 1.2 specs shadow2D/Proj.
  1251. preprocessor.writef(
  1252. "#define shadow2D(_sampler, _coord) bgfxShadow2D(_sampler, _coord).x\n"
  1253. "#define shadow2DProj(_sampler, _coord) bgfxShadow2DProj(_sampler, _coord).x\n"
  1254. );
  1255. }
  1256. for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
  1257. {
  1258. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1259. if (varyingIt != varyingMap.end() )
  1260. {
  1261. const Varying& var = varyingIt->second;
  1262. const char* name = var.m_name.c_str();
  1263. if (0 == strncmp(name, "a_", 2)
  1264. || 0 == strncmp(name, "i_", 2) )
  1265. {
  1266. preprocessor.writef("attribute %s %s %s %s;\n"
  1267. , var.m_precision.c_str()
  1268. , var.m_interpolation.c_str()
  1269. , var.m_type.c_str()
  1270. , name
  1271. );
  1272. }
  1273. else
  1274. {
  1275. preprocessor.writef("%s varying %s %s %s;\n"
  1276. , var.m_interpolation.c_str()
  1277. , var.m_precision.c_str()
  1278. , var.m_type.c_str()
  1279. , name
  1280. );
  1281. }
  1282. }
  1283. }
  1284. for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
  1285. {
  1286. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1287. if (varyingIt != varyingMap.end() )
  1288. {
  1289. const Varying& var = varyingIt->second;
  1290. preprocessor.writef("%s varying %s %s;\n"
  1291. , var.m_interpolation.c_str()
  1292. , var.m_type.c_str()
  1293. , var.m_name.c_str()
  1294. );
  1295. }
  1296. }
  1297. }
  1298. else
  1299. {
  1300. preprocessor.writef(
  1301. "#define lowp\n"
  1302. "#define mediump\n"
  1303. "#define highp\n"
  1304. "#define ivec2 int2\n"
  1305. "#define ivec3 int3\n"
  1306. "#define ivec4 int4\n"
  1307. "#define uvec2 uint2\n"
  1308. "#define uvec3 uint3\n"
  1309. "#define uvec4 uint4\n"
  1310. "#define vec2 float2\n"
  1311. "#define vec3 float3\n"
  1312. "#define vec4 float4\n"
  1313. "#define mat2 float2x2\n"
  1314. "#define mat3 float3x3\n"
  1315. "#define mat4 float4x4\n"
  1316. );
  1317. if (hlsl != 0
  1318. && hlsl < 4)
  1319. {
  1320. preprocessor.writef(
  1321. "#define centroid\n"
  1322. "#define flat\n"
  1323. "#define noperspective\n"
  1324. "#define smooth\n"
  1325. );
  1326. }
  1327. entry[4] = '_';
  1328. if ('f' == shaderType)
  1329. {
  1330. const char* insert = strstr(entry, "{");
  1331. if (NULL != insert)
  1332. {
  1333. insert = strInsert(const_cast<char*>(insert+1), "\nvec4 bgfx_VoidFrag = vec4_splat(0.0);\n");
  1334. }
  1335. const bool hasFragColor = NULL != strstr(input, "gl_FragColor");
  1336. const bool hasFragCoord = NULL != strstr(input, "gl_FragCoord") || hlsl > 3 || hlsl == 2;
  1337. const bool hasFragDepth = NULL != strstr(input, "gl_FragDepth");
  1338. const bool hasFrontFacing = NULL != strstr(input, "gl_FrontFacing");
  1339. const bool hasPrimitiveId = NULL != strstr(input, "gl_PrimitiveID");
  1340. bool hasFragData[8] = {};
  1341. uint32_t numFragData = 0;
  1342. for (uint32_t ii = 0; ii < BX_COUNTOF(hasFragData); ++ii)
  1343. {
  1344. char temp[32];
  1345. bx::snprintf(temp, BX_COUNTOF(temp), "gl_FragData[%d]", ii);
  1346. hasFragData[ii] = NULL != strstr(input, temp);
  1347. numFragData += hasFragData[ii];
  1348. }
  1349. if (0 == numFragData)
  1350. {
  1351. // GL errors when both gl_FragColor and gl_FragData is used.
  1352. // This will trigger the same error with HLSL compiler too.
  1353. preprocessor.writef("#define gl_FragColor gl_FragData_0_\n");
  1354. // If it has gl_FragData or gl_FragColor, color target at
  1355. // index 0 exists, otherwise shader is not modifying color
  1356. // targets.
  1357. hasFragData[0] |= hasFragColor || d3d < 11;
  1358. if (NULL != insert
  1359. && d3d < 11
  1360. && !hasFragColor)
  1361. {
  1362. insert = strInsert(const_cast<char*>(insert+1), "\ngl_FragColor = bgfx_VoidFrag;\n");
  1363. }
  1364. }
  1365. preprocessor.writef("#define void_main()");
  1366. preprocessor.writef(" \\\n\tvoid main(");
  1367. uint32_t arg = 0;
  1368. if (hasFragCoord)
  1369. {
  1370. preprocessor.writef(" \\\n\tvec4 gl_FragCoord : SV_POSITION");
  1371. ++arg;
  1372. }
  1373. for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
  1374. {
  1375. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1376. if (varyingIt != varyingMap.end() )
  1377. {
  1378. const Varying& var = varyingIt->second;
  1379. preprocessor.writef(" \\\n\t%s%s %s %s : %s"
  1380. , arg++ > 0 ? ", " : " "
  1381. , interpolationDx11(var.m_interpolation.c_str() )
  1382. , var.m_type.c_str()
  1383. , var.m_name.c_str()
  1384. , var.m_semantics.c_str()
  1385. );
  1386. }
  1387. }
  1388. const uint32_t maxRT = d3d > 9 ? BX_COUNTOF(hasFragData) : 4;
  1389. for (uint32_t ii = 0; ii < BX_COUNTOF(hasFragData); ++ii)
  1390. {
  1391. if (ii < maxRT)
  1392. {
  1393. if (hasFragData[ii])
  1394. {
  1395. addFragData(preprocessor, input, ii, arg++ > 0);
  1396. }
  1397. }
  1398. else
  1399. {
  1400. voidFragData(input, ii);
  1401. }
  1402. }
  1403. if (hasFragDepth)
  1404. {
  1405. preprocessor.writef(
  1406. " \\\n\t%sout float gl_FragDepth : SV_DEPTH"
  1407. , arg++ > 0 ? ", " : " "
  1408. );
  1409. }
  1410. if (hasFrontFacing
  1411. && hlsl >= 3)
  1412. {
  1413. preprocessor.writef(
  1414. " \\\n\t%sfloat __vface : VFACE"
  1415. , arg++ > 0 ? ", " : " "
  1416. );
  1417. }
  1418. if (hasPrimitiveId)
  1419. {
  1420. if (d3d > 9)
  1421. {
  1422. preprocessor.writef(
  1423. " \\\n\t%suint gl_PrimitiveID : SV_PrimitiveID"
  1424. , arg++ > 0 ? ", " : " "
  1425. );
  1426. }
  1427. else
  1428. {
  1429. fprintf(stderr, "PrimitiveID builtin is not supported by this D3D9 HLSL.\n");
  1430. return EXIT_FAILURE;
  1431. }
  1432. }
  1433. preprocessor.writef(
  1434. " \\\n\t)\n"
  1435. );
  1436. if (hasFrontFacing)
  1437. {
  1438. if (hlsl >= 3)
  1439. {
  1440. preprocessor.writef(
  1441. "#define gl_FrontFacing (__vface <= 0.0)\n"
  1442. );
  1443. }
  1444. else
  1445. {
  1446. preprocessor.writef(
  1447. "#define gl_FrontFacing false\n"
  1448. );
  1449. }
  1450. }
  1451. }
  1452. else if ('v' == shaderType)
  1453. {
  1454. const char* brace = strstr(entry, "{");
  1455. if (NULL != brace)
  1456. {
  1457. const char* end = bx::strmb(brace, '{', '}');
  1458. if (NULL != end)
  1459. {
  1460. strInsert(const_cast<char*>(end), "__RETURN__;\n");
  1461. }
  1462. }
  1463. preprocessor.writef(
  1464. "struct Output\n"
  1465. "{\n"
  1466. "\tvec4 gl_Position : SV_POSITION;\n"
  1467. "#define gl_Position _varying_.gl_Position\n"
  1468. );
  1469. for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
  1470. {
  1471. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1472. if (varyingIt != varyingMap.end() )
  1473. {
  1474. const Varying& var = varyingIt->second;
  1475. preprocessor.writef("\t%s %s : %s;\n", var.m_type.c_str(), var.m_name.c_str(), var.m_semantics.c_str() );
  1476. preprocessor.writef("#define %s _varying_.%s\n", var.m_name.c_str(), var.m_name.c_str() );
  1477. }
  1478. }
  1479. preprocessor.writef(
  1480. "};\n"
  1481. );
  1482. preprocessor.writef("#define void_main() \\\n");
  1483. preprocessor.writef("Output main(");
  1484. bool first = true;
  1485. for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
  1486. {
  1487. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1488. if (varyingIt != varyingMap.end() )
  1489. {
  1490. const Varying& var = varyingIt->second;
  1491. preprocessor.writef("%s%s %s : %s\\\n", first ? "" : "\t, ", var.m_type.c_str(), var.m_name.c_str(), var.m_semantics.c_str() );
  1492. first = false;
  1493. }
  1494. }
  1495. preprocessor.writef(
  1496. ") \\\n"
  1497. "{ \\\n"
  1498. "\tOutput _varying_;"
  1499. );
  1500. for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
  1501. {
  1502. VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
  1503. if (varyingIt != varyingMap.end() )
  1504. {
  1505. const Varying& var = varyingIt->second;
  1506. preprocessor.writef(" \\\n\t%s", var.m_name.c_str() );
  1507. if (!var.m_init.empty() )
  1508. {
  1509. preprocessor.writef(" = %s", var.m_init.c_str() );
  1510. }
  1511. preprocessor.writef(";");
  1512. }
  1513. }
  1514. preprocessor.writef(
  1515. "\n#define __RETURN__ \\\n"
  1516. "\t} \\\n"
  1517. );
  1518. if (hlsl != 0
  1519. && hlsl <= 3)
  1520. {
  1521. // preprocessor.writef(
  1522. // "\tgl_Position.xy += u_viewTexel.xy * gl_Position.w; \\\n"
  1523. // );
  1524. }
  1525. preprocessor.writef(
  1526. "\treturn _varying_"
  1527. );
  1528. }
  1529. }
  1530. if (preprocessor.run(input) )
  1531. {
  1532. BX_TRACE("Input file: %s", filePath);
  1533. BX_TRACE("Output file: %s", outFilePath);
  1534. if (preprocessOnly)
  1535. {
  1536. bx::CrtFileWriter writer;
  1537. if (!bx::open(&writer, outFilePath) )
  1538. {
  1539. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  1540. return EXIT_FAILURE;
  1541. }
  1542. if (0 != glsl)
  1543. {
  1544. if (NULL == profile)
  1545. {
  1546. writef(&writer
  1547. , "#ifdef GL_ES\n"
  1548. "precision highp float;\n"
  1549. "#endif // GL_ES\n\n"
  1550. );
  1551. }
  1552. }
  1553. bx::write(&writer, preprocessor.m_preprocessed.c_str(), (int32_t)preprocessor.m_preprocessed.size() );
  1554. bx::close(&writer);
  1555. return EXIT_SUCCESS;
  1556. }
  1557. {
  1558. bx::CrtFileWriter* writer = NULL;
  1559. if (NULL != bin2c)
  1560. {
  1561. writer = new Bin2cWriter(bin2c);
  1562. }
  1563. else
  1564. {
  1565. writer = new bx::CrtFileWriter;
  1566. }
  1567. if (!bx::open(writer, outFilePath) )
  1568. {
  1569. fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
  1570. return EXIT_FAILURE;
  1571. }
  1572. if ('f' == shaderType)
  1573. {
  1574. bx::write(writer, BGFX_CHUNK_MAGIC_FSH);
  1575. bx::write(writer, inputHash);
  1576. }
  1577. else if ('v' == shaderType)
  1578. {
  1579. bx::write(writer, BGFX_CHUNK_MAGIC_VSH);
  1580. bx::write(writer, outputHash);
  1581. }
  1582. else
  1583. {
  1584. bx::write(writer, BGFX_CHUNK_MAGIC_CSH);
  1585. bx::write(writer, outputHash);
  1586. }
  1587. if (0 != glsl
  1588. || 0 != essl
  1589. || 0 != metal)
  1590. {
  1591. std::string code;
  1592. const bool usesGpuShader5 = !!bx::findIdentifierMatch(input, s_ARB_gpu_shader5);
  1593. const bool usesTexelFetch = !!bx::findIdentifierMatch(input, s_texelFetch);
  1594. const bool usesTextureLod = !!bx::findIdentifierMatch(input, s_ARB_shader_texture_lod /*EXT_shader_texture_lod*/);
  1595. const bool usesTextureMS = !!bx::findIdentifierMatch(input, s_ARB_texture_multisample);
  1596. const bool usesTextureArray = !!bx::findIdentifierMatch(input, s_textureArray);
  1597. const bool usesPacking = !!bx::findIdentifierMatch(input, s_ARB_shading_language_packing);
  1598. if (0 == essl)
  1599. {
  1600. const bool need130 = 120 == glsl && (false
  1601. || bx::findIdentifierMatch(input, s_130)
  1602. || usesTexelFetch
  1603. );
  1604. if (0 != metal)
  1605. {
  1606. bx::stringPrintf(code, "#version 120\n");
  1607. }
  1608. else
  1609. {
  1610. bx::stringPrintf(code, "#version %s\n", need130 ? "130" : profile);
  1611. }
  1612. if (usesGpuShader5)
  1613. {
  1614. bx::stringPrintf(code
  1615. , "#extension GL_ARB_gpu_shader5 : enable\n"
  1616. );
  1617. }
  1618. if (usesPacking)
  1619. {
  1620. bx::stringPrintf(code
  1621. , "#extension GL_ARB_shading_language_packing : enable\n"
  1622. );
  1623. }
  1624. if (usesTextureLod
  1625. && 130 > glsl)
  1626. {
  1627. bx::stringPrintf(code
  1628. , "#extension GL_ARB_shader_texture_lod : enable\n"
  1629. );
  1630. }
  1631. if (usesTextureMS)
  1632. {
  1633. bx::stringPrintf(code
  1634. , "#extension GL_ARB_texture_multisample : enable\n"
  1635. );
  1636. }
  1637. if (usesTextureArray)
  1638. {
  1639. bx::stringPrintf(code
  1640. , "#extension GL_EXT_texture_array : enable\n"
  1641. );
  1642. }
  1643. if (130 > glsl)
  1644. {
  1645. bx::stringPrintf(code,
  1646. "#define ivec2 vec2\n"
  1647. "#define ivec3 vec3\n"
  1648. "#define ivec4 vec4\n"
  1649. );
  1650. }
  1651. bx::stringPrintf(code
  1652. , "#define bgfxShadow2D shadow2D\n"
  1653. "#define bgfxShadow2DProj shadow2DProj\n"
  1654. );
  1655. }
  1656. else
  1657. {
  1658. // Pretend that all extensions are available.
  1659. // This will be stripped later.
  1660. if (usesTextureLod)
  1661. {
  1662. bx::stringPrintf(code
  1663. , "#extension GL_EXT_shader_texture_lod : enable\n"
  1664. "#define texture2DLod texture2DLodEXT\n"
  1665. "#define texture2DProjLod texture2DProjLodEXT\n"
  1666. "#define textureCubeLod textureCubeLodEXT\n"
  1667. // "#define texture2DGrad texture2DGradEXT\n"
  1668. // "#define texture2DProjGrad texture2DProjGradEXT\n"
  1669. // "#define textureCubeGrad textureCubeGradEXT\n"
  1670. );
  1671. }
  1672. if (NULL != bx::findIdentifierMatch(input, s_OES_standard_derivatives) )
  1673. {
  1674. bx::stringPrintf(code, "#extension GL_OES_standard_derivatives : enable\n");
  1675. }
  1676. if (NULL != bx::findIdentifierMatch(input, s_OES_texture_3D) )
  1677. {
  1678. bx::stringPrintf(code, "#extension GL_OES_texture_3D : enable\n");
  1679. }
  1680. if (NULL != bx::findIdentifierMatch(input, s_EXT_shadow_samplers) )
  1681. {
  1682. bx::stringPrintf(code
  1683. , "#extension GL_EXT_shadow_samplers : enable\n"
  1684. "#define shadow2D shadow2DEXT\n"
  1685. "#define shadow2DProj shadow2DProjEXT\n"
  1686. );
  1687. }
  1688. if (usesGpuShader5)
  1689. {
  1690. bx::stringPrintf(code
  1691. , "#extension GL_ARB_gpu_shader5 : enable\n"
  1692. );
  1693. }
  1694. if (usesPacking)
  1695. {
  1696. bx::stringPrintf(code
  1697. , "#extension GL_ARB_shading_language_packing : enable\n"
  1698. );
  1699. }
  1700. if (NULL != bx::findIdentifierMatch(input, "gl_FragDepth") )
  1701. {
  1702. bx::stringPrintf(code
  1703. , "#extension GL_EXT_frag_depth : enable\n"
  1704. "#define gl_FragDepth gl_FragDepthEXT\n"
  1705. );
  1706. }
  1707. if (usesTextureArray)
  1708. {
  1709. bx::stringPrintf(code
  1710. , "#extension GL_EXT_texture_array : enable\n"
  1711. );
  1712. }
  1713. bx::stringPrintf(code,
  1714. "#define ivec2 vec2\n"
  1715. "#define ivec3 vec3\n"
  1716. "#define ivec4 vec4\n"
  1717. );
  1718. }
  1719. code += preprocessor.m_preprocessed;
  1720. compiled = compileGLSLShader(cmdLine
  1721. , metal ? BX_MAKEFOURCC('M', 'T', 'L', 0) : essl
  1722. , code
  1723. , writer
  1724. );
  1725. }
  1726. else if (0 != spirv)
  1727. {
  1728. compiled = compileSPIRVShader(cmdLine
  1729. , 0
  1730. , preprocessor.m_preprocessed
  1731. , writer
  1732. );
  1733. }
  1734. else if (0 != pssl)
  1735. {
  1736. compiled = compilePSSLShader(cmdLine
  1737. , 0
  1738. , preprocessor.m_preprocessed
  1739. , writer
  1740. );
  1741. }
  1742. else
  1743. {
  1744. compiled = compileHLSLShader(cmdLine
  1745. , d3d
  1746. , preprocessor.m_preprocessed
  1747. , writer
  1748. );
  1749. }
  1750. bx::close(writer);
  1751. delete writer;
  1752. }
  1753. if (compiled)
  1754. {
  1755. if (depends)
  1756. {
  1757. std::string ofp = outFilePath;
  1758. ofp += ".d";
  1759. bx::CrtFileWriter writer;
  1760. if (bx::open(&writer, ofp.c_str() ) )
  1761. {
  1762. writef(&writer, "%s : %s\n", outFilePath, preprocessor.m_depends.c_str() );
  1763. bx::close(&writer);
  1764. }
  1765. }
  1766. }
  1767. }
  1768. }
  1769. }
  1770. delete [] data;
  1771. }
  1772. if (compiled)
  1773. {
  1774. return EXIT_SUCCESS;
  1775. }
  1776. remove(outFilePath);
  1777. fprintf(stderr, "Failed to build shader.\n");
  1778. return EXIT_FAILURE;
  1779. }
  1780. } // namespace bgfx
  1781. int main(int _argc, const char* _argv[])
  1782. {
  1783. return bgfx::compileShader(_argc, _argv);
  1784. }