shaderc.cpp 49 KB

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