shaderc.cpp 52 KB

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