shaderc.cpp 52 KB

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