shaderc.cpp 50 KB

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