shaderc.cpp 63 KB

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