shaderc.cpp 53 KB

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