shaderc.cpp 50 KB

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