shaderc.cpp 53 KB

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