shaderc.cpp 53 KB

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