shaderc_spirv.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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. BX_PRAGMA_DIAGNOSTIC_PUSH()
  7. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // error C4100: 'inclusionDepth' : unreferenced formal parameter
  8. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4265) // error C4265: 'spv::spirvbin_t': class has virtual functions, but destructor is not virtual
  9. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow") // warning: declaration of 'userData' shadows a member of 'glslang::TShader::Includer::IncludeResult'
  10. #define ENABLE_OPT 1
  11. #include <ShaderLang.h>
  12. #include <ResourceLimits.h>
  13. #include <SPIRV/SPVRemapper.h>
  14. #include <SPIRV/GlslangToSpv.h>
  15. #define SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
  16. #include <spirv_msl.hpp>
  17. #include <spirv_reflect.hpp>
  18. #include <spirv-tools/optimizer.hpp>
  19. BX_PRAGMA_DIAGNOSTIC_POP()
  20. namespace bgfx
  21. {
  22. static bx::DefaultAllocator s_allocator;
  23. bx::AllocatorI* g_allocator = &s_allocator;
  24. struct TinyStlAllocator
  25. {
  26. static void* static_allocate(size_t _bytes);
  27. static void static_deallocate(void* _ptr, size_t /*_bytes*/);
  28. };
  29. void* TinyStlAllocator::static_allocate(size_t _bytes)
  30. {
  31. return BX_ALLOC(g_allocator, _bytes);
  32. }
  33. void TinyStlAllocator::static_deallocate(void* _ptr, size_t /*_bytes*/)
  34. {
  35. if (NULL != _ptr)
  36. {
  37. BX_FREE(g_allocator, _ptr);
  38. }
  39. }
  40. } // namespace bgfx
  41. #define TINYSTL_ALLOCATOR bgfx::TinyStlAllocator
  42. #include <tinystl/allocator.h>
  43. #include <tinystl/string.h>
  44. #include <tinystl/unordered_map.h>
  45. #include <tinystl/vector.h>
  46. namespace stl = tinystl;
  47. #include "../../src/shader_spirv.h"
  48. #include "../../3rdparty/khronos/vulkan/vulkan.h"
  49. namespace bgfx { namespace spirv
  50. {
  51. const TBuiltInResource resourceLimits =
  52. {
  53. 32, // MaxLights
  54. 6, // MaxClipPlanes
  55. 32, // MaxTextureUnits
  56. 32, // MaxTextureCoords
  57. 64, // MaxVertexAttribs
  58. 4096, // MaxVertexUniformComponents
  59. 64, // MaxVaryingFloats
  60. 32, // MaxVertexTextureImageUnits
  61. 80, // MaxCombinedTextureImageUnits
  62. 32, // MaxTextureImageUnits
  63. 4096, // MaxFragmentUniformComponents
  64. 32, // MaxDrawBuffers
  65. 128, // MaxVertexUniformVectors
  66. 8, // MaxVaryingVectors
  67. 16, // MaxFragmentUniformVectors
  68. 16, // MaxVertexOutputVectors
  69. 15, // MaxFragmentInputVectors
  70. -8, // MinProgramTexelOffset
  71. 7, // MaxProgramTexelOffset
  72. 8, // MaxClipDistances
  73. 65535, // MaxComputeWorkGroupCountX
  74. 65535, // MaxComputeWorkGroupCountY
  75. 65535, // MaxComputeWorkGroupCountZ
  76. 1024, // MaxComputeWorkGroupSizeX
  77. 1024, // MaxComputeWorkGroupSizeY
  78. 64, // MaxComputeWorkGroupSizeZ
  79. 1024, // MaxComputeUniformComponents
  80. 16, // MaxComputeTextureImageUnits
  81. 8, // MaxComputeImageUniforms
  82. 8, // MaxComputeAtomicCounters
  83. 1, // MaxComputeAtomicCounterBuffers
  84. 60, // MaxVaryingComponents
  85. 64, // MaxVertexOutputComponents
  86. 64, // MaxGeometryInputComponents
  87. 128, // MaxGeometryOutputComponents
  88. 128, // MaxFragmentInputComponents
  89. 8, // MaxImageUnits
  90. 8, // MaxCombinedImageUnitsAndFragmentOutputs
  91. 8, // MaxCombinedShaderOutputResources
  92. 0, // MaxImageSamples
  93. 0, // MaxVertexImageUniforms
  94. 0, // MaxTessControlImageUniforms
  95. 0, // MaxTessEvaluationImageUniforms
  96. 0, // MaxGeometryImageUniforms
  97. 8, // MaxFragmentImageUniforms
  98. 8, // MaxCombinedImageUniforms
  99. 16, // MaxGeometryTextureImageUnits
  100. 256, // MaxGeometryOutputVertices
  101. 1024, // MaxGeometryTotalOutputComponents
  102. 1024, // MaxGeometryUniformComponents
  103. 64, // MaxGeometryVaryingComponents
  104. 128, // MaxTessControlInputComponents
  105. 128, // MaxTessControlOutputComponents
  106. 16, // MaxTessControlTextureImageUnits
  107. 1024, // MaxTessControlUniformComponents
  108. 4096, // MaxTessControlTotalOutputComponents
  109. 128, // MaxTessEvaluationInputComponents
  110. 128, // MaxTessEvaluationOutputComponents
  111. 16, // MaxTessEvaluationTextureImageUnits
  112. 1024, // MaxTessEvaluationUniformComponents
  113. 120, // MaxTessPatchComponents
  114. 32, // MaxPatchVertices
  115. 64, // MaxTessGenLevel
  116. 16, // MaxViewports
  117. 0, // MaxVertexAtomicCounters
  118. 0, // MaxTessControlAtomicCounters
  119. 0, // MaxTessEvaluationAtomicCounters
  120. 0, // MaxGeometryAtomicCounters
  121. 8, // MaxFragmentAtomicCounters
  122. 8, // MaxCombinedAtomicCounters
  123. 1, // MaxAtomicCounterBindings
  124. 0, // MaxVertexAtomicCounterBuffers
  125. 0, // MaxTessControlAtomicCounterBuffers
  126. 0, // MaxTessEvaluationAtomicCounterBuffers
  127. 0, // MaxGeometryAtomicCounterBuffers
  128. 1, // MaxFragmentAtomicCounterBuffers
  129. 1, // MaxCombinedAtomicCounterBuffers
  130. 16384, // MaxAtomicCounterBufferSize
  131. 4, // MaxTransformFeedbackBuffers
  132. 64, // MaxTransformFeedbackInterleavedComponents
  133. 8, // MaxCullDistances
  134. 8, // MaxCombinedClipAndCullDistances
  135. 4, // MaxSamples
  136. 0, // maxMeshOutputVerticesNV;
  137. 0, // maxMeshOutputPrimitivesNV;
  138. 0, // maxMeshWorkGroupSizeX_NV;
  139. 0, // maxMeshWorkGroupSizeY_NV;
  140. 0, // maxMeshWorkGroupSizeZ_NV;
  141. 0, // maxTaskWorkGroupSizeX_NV;
  142. 0, // maxTaskWorkGroupSizeY_NV;
  143. 0, // maxTaskWorkGroupSizeZ_NV;
  144. 0, // maxMeshViewCountNV
  145. { // limits
  146. true, // nonInductiveForLoops
  147. true, // whileLoops
  148. true, // doWhileLoops
  149. true, // generalUniformIndexing
  150. true, // generalAttributeMatrixVectorIndexing
  151. true, // generalVaryingIndexing
  152. true, // generalSamplerIndexing
  153. true, // generalVariableIndexing
  154. true, // generalConstantMatrixVectorIndexing
  155. },
  156. };
  157. bool printAsm(uint32_t _offset, const SpvInstruction& _instruction, void* _userData)
  158. {
  159. BX_UNUSED(_userData);
  160. char temp[512];
  161. toString(temp, sizeof(temp), _instruction);
  162. BX_TRACE("%5d: %s", _offset, temp);
  163. return true;
  164. }
  165. struct SpvReflection
  166. {
  167. struct TypeId
  168. {
  169. enum Enum
  170. {
  171. Void,
  172. Bool,
  173. Int32,
  174. Int64,
  175. Uint32,
  176. Uint64,
  177. Float,
  178. Double,
  179. Vector,
  180. Matrix,
  181. Count
  182. };
  183. TypeId()
  184. : baseType(Enum::Count)
  185. , type(Enum::Count)
  186. , numComponents(0)
  187. {
  188. }
  189. Enum baseType;
  190. Enum type;
  191. uint32_t numComponents;
  192. stl::string toString()
  193. {
  194. stl::string result;
  195. switch (type)
  196. {
  197. case Float:
  198. result.append("float");
  199. break;
  200. case Vector:
  201. bx::stringPrintf(result, "vec%d"
  202. , numComponents
  203. );
  204. break;
  205. case Matrix:
  206. bx::stringPrintf(result, "mat%d"
  207. , numComponents
  208. );
  209. default:
  210. break;
  211. }
  212. return result;
  213. }
  214. };
  215. struct Id
  216. {
  217. struct Variable
  218. {
  219. Variable()
  220. : decoration(SpvDecoration::Count)
  221. , builtin(SpvBuiltin::Count)
  222. , storageClass(SpvStorageClass::Count)
  223. , location(UINT32_MAX)
  224. , offset(UINT32_MAX)
  225. , type(UINT32_MAX)
  226. {
  227. }
  228. stl::string name;
  229. SpvDecoration::Enum decoration;
  230. SpvBuiltin::Enum builtin;
  231. SpvStorageClass::Enum storageClass;
  232. uint32_t location;
  233. uint32_t offset;
  234. uint32_t type;
  235. };
  236. typedef stl::vector<Variable> MemberArray;
  237. Variable var;
  238. MemberArray members;
  239. };
  240. typedef stl::unordered_map<uint32_t, TypeId> TypeIdMap;
  241. typedef stl::unordered_map<uint32_t, Id> IdMap;
  242. TypeIdMap typeIdMap;
  243. IdMap idMap;
  244. stl::string getTypeName(uint32_t _typeId)
  245. {
  246. return getTypeId(_typeId).toString();
  247. }
  248. Id& getId(uint32_t _id)
  249. {
  250. IdMap::iterator it = idMap.find(_id);
  251. if (it == idMap.end() )
  252. {
  253. Id id;
  254. stl::pair<IdMap::iterator, bool> result = idMap.insert(stl::make_pair(_id, id) );
  255. it = result.first;
  256. }
  257. return it->second;
  258. }
  259. Id::Variable& get(uint32_t _id, uint32_t _idx)
  260. {
  261. Id& id = getId(_id);
  262. id.members.resize(bx::uint32_max(_idx+1, uint32_t(id.members.size() ) ) );
  263. return id.members[_idx];
  264. }
  265. TypeId& getTypeId(uint32_t _id)
  266. {
  267. TypeIdMap::iterator it = typeIdMap.find(_id);
  268. if (it == typeIdMap.end() )
  269. {
  270. TypeId id;
  271. stl::pair<TypeIdMap::iterator, bool> result = typeIdMap.insert(stl::make_pair(_id, id) );
  272. it = result.first;
  273. }
  274. return it->second;
  275. }
  276. void update(uint32_t _id, const stl::string& _name)
  277. {
  278. getId(_id).var.name = _name;
  279. }
  280. BX_NO_INLINE void update(Id::Variable& _variable, SpvDecoration::Enum _decoration, uint32_t _literal)
  281. {
  282. _variable.decoration = _decoration;
  283. switch (_decoration)
  284. {
  285. case SpvDecoration::Location:
  286. _variable.location = _literal;
  287. break;
  288. case SpvDecoration::Offset:
  289. _variable.offset = _literal;
  290. break;
  291. case SpvDecoration::BuiltIn:
  292. _variable.builtin = SpvBuiltin::Enum(_literal);
  293. break;
  294. default:
  295. break;
  296. }
  297. }
  298. BX_NO_INLINE void update(Id::Variable& _variable, uint32_t _type, SpvStorageClass::Enum _storageClass)
  299. {
  300. _variable.type = _type;
  301. _variable.storageClass = _storageClass;
  302. }
  303. void update(uint32_t _id, SpvDecoration::Enum _decoration, uint32_t _literal)
  304. {
  305. update(getId(_id).var, _decoration, _literal);
  306. }
  307. void update(uint32_t _id, uint32_t _type, SpvStorageClass::Enum _storageClass)
  308. {
  309. update(getId(_id).var, _type, _storageClass);
  310. }
  311. void update(uint32_t _id, uint32_t _idx, const stl::string& _name)
  312. {
  313. Id::Variable& var = get(_id, _idx);
  314. var.name = _name;
  315. }
  316. BX_NO_INLINE void update(uint32_t _id, uint32_t _idx, SpvDecoration::Enum _decoration, uint32_t _literal)
  317. {
  318. update(get(_id, _idx), _decoration, _literal);
  319. }
  320. void update(uint32_t _id, TypeId::Enum _type)
  321. {
  322. TypeId& type = getTypeId(_id);
  323. type.type = _type;
  324. }
  325. void update(uint32_t _id, TypeId::Enum _type, uint32_t _baseTypeId, uint32_t _numComonents)
  326. {
  327. TypeId& type = getTypeId(_id);
  328. type.type = _type;
  329. type.baseType = getTypeId(_baseTypeId).type;
  330. type.numComponents = _numComonents;
  331. }
  332. };
  333. bool spvParse(uint32_t _offset, const SpvInstruction& _instruction, void* _userData)
  334. {
  335. BX_UNUSED(_offset);
  336. SpvReflection* spv = (SpvReflection*)_userData;
  337. switch (_instruction.opcode)
  338. {
  339. case SpvOpcode::Name:
  340. spv->update(_instruction.result
  341. , _instruction.operand[0].literalString
  342. );
  343. break;
  344. case SpvOpcode::Decorate:
  345. spv->update(_instruction.operand[0].data
  346. , SpvDecoration::Enum(_instruction.operand[1].data)
  347. , _instruction.operand[2].data
  348. );
  349. break;
  350. case SpvOpcode::MemberName:
  351. spv->update(_instruction.result
  352. , _instruction.operand[0].data
  353. , _instruction.operand[1].literalString
  354. );
  355. break;
  356. case SpvOpcode::MemberDecorate:
  357. spv->update(_instruction.operand[0].data
  358. , _instruction.operand[1].data
  359. , SpvDecoration::Enum(_instruction.operand[2].data)
  360. , _instruction.operand[3].data
  361. );
  362. break;
  363. case SpvOpcode::Variable:
  364. spv->update(_instruction.result
  365. , _instruction.type
  366. , SpvStorageClass::Enum(_instruction.operand[0].data)
  367. );
  368. break;
  369. case SpvOpcode::TypeVoid:
  370. spv->update(_instruction.result, SpvReflection::TypeId::Void);
  371. break;
  372. case SpvOpcode::TypeBool:
  373. spv->update(_instruction.result, SpvReflection::TypeId::Bool);
  374. break;
  375. case SpvOpcode::TypeInt:
  376. spv->update(_instruction.result
  377. , 32 == _instruction.operand[0].data
  378. ? 0 == _instruction.operand[1].data
  379. ? SpvReflection::TypeId::Uint32
  380. : SpvReflection::TypeId::Int32
  381. : 0 == _instruction.operand[1].data
  382. ? SpvReflection::TypeId::Uint64
  383. : SpvReflection::TypeId::Int64
  384. );
  385. break;
  386. case SpvOpcode::TypeFloat:
  387. spv->update(_instruction.result
  388. , 32 == _instruction.operand[0].data
  389. ? SpvReflection::TypeId::Float
  390. : SpvReflection::TypeId::Double
  391. );
  392. break;
  393. case SpvOpcode::TypeVector:
  394. spv->update(_instruction.result
  395. , SpvReflection::TypeId::Vector
  396. , _instruction.operand[0].data
  397. , _instruction.operand[1].data
  398. );
  399. break;
  400. case SpvOpcode::TypeMatrix:
  401. spv->update(_instruction.result
  402. , SpvReflection::TypeId::Matrix
  403. , _instruction.operand[0].data
  404. , _instruction.operand[1].data
  405. );
  406. break;
  407. case SpvOpcode::TypeImage:
  408. case SpvOpcode::TypeSampler:
  409. case SpvOpcode::TypeSampledImage:
  410. break;
  411. case SpvOpcode::TypeStruct:
  412. for (uint32_t ii = 0, num = _instruction.numOperands; ii < num; ++ii)
  413. {
  414. SpvReflection::Id::Variable& var = spv->get(_instruction.result, ii);
  415. var.type = _instruction.operand[ii].data;
  416. }
  417. break;
  418. default:
  419. break;
  420. }
  421. return true;
  422. }
  423. #define DBG(...) // bx::debugPrintf(__VA_ARGS__)
  424. void disassemble(bx::WriterI* _writer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  425. {
  426. BX_UNUSED(_writer);
  427. uint32_t magic;
  428. bx::peek(_reader, magic);
  429. SpvReflection spvx;
  430. if (magic == SPV_CHUNK_HEADER)
  431. {
  432. SpirV spirv;
  433. read(_reader, spirv, _err);
  434. parse(spirv.shader, spvParse, &spvx, _err);
  435. for (SpvReflection::IdMap::const_iterator it = spvx.idMap.begin(), itEnd = spvx.idMap.end(); it != itEnd; ++it)
  436. {
  437. const SpvReflection::Id& id = it->second;
  438. uint32_t num = uint32_t(id.members.size() );
  439. if (0 < num
  440. && 0 != bx::strCmp(id.var.name.c_str(), "gl_PerVertex") )
  441. {
  442. DBG("%3d: %s %d %s\n"
  443. , it->first
  444. , id.var.name.c_str()
  445. , id.var.location
  446. , getName(id.var.storageClass)
  447. );
  448. DBG("{\n");
  449. for (uint32_t ii = 0; ii < num; ++ii)
  450. {
  451. const SpvReflection::Id::Variable& var = id.members[ii];
  452. DBG("\t\t%s %s %d %s\n"
  453. , spvx.getTypeName(var.type).c_str()
  454. , var.name.c_str()
  455. , var.offset
  456. , getName(var.storageClass)
  457. );
  458. BX_UNUSED(var);
  459. }
  460. DBG("}\n");
  461. }
  462. }
  463. }
  464. }
  465. static EShLanguage getLang(char _p)
  466. {
  467. switch (_p)
  468. {
  469. case 'c': return EShLangCompute;
  470. case 'f': return EShLangFragment;
  471. case 'v': return EShLangVertex;
  472. default: return EShLangCount;
  473. }
  474. }
  475. static const char* s_attribName[] =
  476. {
  477. "a_position",
  478. "a_normal",
  479. "a_tangent",
  480. "a_bitangent",
  481. "a_color0",
  482. "a_color1",
  483. "a_color2",
  484. "a_color3",
  485. "a_indices",
  486. "a_weight",
  487. "a_texcoord0",
  488. "a_texcoord1",
  489. "a_texcoord2",
  490. "a_texcoord3",
  491. "a_texcoord4",
  492. "a_texcoord5",
  493. "a_texcoord6",
  494. "a_texcoord7",
  495. };
  496. BX_STATIC_ASSERT(bgfx::Attrib::Count == BX_COUNTOF(s_attribName) );
  497. int32_t extractStageNumber(const std::string _strLine)
  498. {
  499. bx::StringView found = bx::findIdentifierMatch(_strLine.c_str(), "register");
  500. const char* ptr = found.getPtr() + found.getLength();
  501. const char* start = NULL;
  502. const char* end = NULL;
  503. while (*ptr != ')'
  504. && ptr < _strLine.c_str() + _strLine.size() )
  505. {
  506. if (*ptr >= '0' && *ptr <= '9')
  507. {
  508. if (start == NULL)
  509. {
  510. start = ptr;
  511. }
  512. end = ptr;
  513. }
  514. ptr++;
  515. }
  516. BX_CHECK(start != NULL && end != NULL, "cannot find register number");
  517. bx::StringView numberString(start, end - start + 1);
  518. int32_t regNumber = -1;
  519. bx::fromString(&regNumber, numberString);
  520. BX_CHECK(regNumber >= 0, "register number should be semi-positive integer");
  521. return regNumber;
  522. }
  523. bgfx::Attrib::Enum toAttribEnum(const bx::StringView& _name)
  524. {
  525. for (uint8_t ii = 0; ii < Attrib::Count; ++ii)
  526. {
  527. if (0 == bx::strCmp(s_attribName[ii], _name) )
  528. {
  529. return bgfx::Attrib::Enum(ii);
  530. }
  531. }
  532. return bgfx::Attrib::Count;
  533. }
  534. static uint16_t writeUniformArray(bx::WriterI* _writer, const UniformArray& uniforms, bool isFragmentShader)
  535. {
  536. uint16_t size = 0;
  537. uint16_t count = static_cast<uint16_t>(uniforms.size());
  538. bx::write(_writer, count);
  539. uint32_t fragmentBit = isFragmentShader ? BGFX_UNIFORM_FRAGMENTBIT : 0;
  540. for (uint16_t ii = 0; ii < count; ++ii)
  541. {
  542. const Uniform& un = uniforms[ii];
  543. if (un.type != UniformType::Sampler)
  544. size = bx::max(size, (uint16_t)(un.regIndex + un.regCount*16));
  545. uint8_t nameSize = (uint8_t)un.name.size();
  546. bx::write(_writer, nameSize);
  547. bx::write(_writer, un.name.c_str(), nameSize);
  548. bx::write(_writer, uint8_t(un.type | fragmentBit));
  549. bx::write(_writer, un.num);
  550. bx::write(_writer, un.regIndex);
  551. bx::write(_writer, un.regCount);
  552. BX_TRACE("%s, %s, %d, %d, %d"
  553. , un.name.c_str()
  554. , getUniformTypeName(un.type)
  555. , un.num
  556. , un.regIndex
  557. , un.regCount
  558. );
  559. }
  560. return size;
  561. }
  562. static bool compile(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bool _firstPass)
  563. {
  564. BX_UNUSED(_version);
  565. glslang::InitializeProcess();
  566. glslang::TProgram* program = new glslang::TProgram;
  567. EShLanguage stage = getLang(_options.shaderType);
  568. if (EShLangCount == stage)
  569. {
  570. bx::printf("Error: Unknown shader type '%c'.\n", _options.shaderType);
  571. return false;
  572. }
  573. glslang::TShader* shader = new glslang::TShader(stage);
  574. EShMessages messages = EShMessages(0
  575. | EShMsgDefault
  576. | EShMsgReadHlsl
  577. | EShMsgVulkanRules
  578. | EShMsgSpvRules
  579. );
  580. shader->setEntryPoint("main");
  581. shader->setAutoMapBindings(true);
  582. uint32_t bindingOffset = (stage == EShLanguage::EShLangFragment ? 48 : 0);
  583. shader->setShiftBinding(glslang::EResUbo, bindingOffset);
  584. shader->setShiftBinding(glslang::EResTexture, bindingOffset + 16);
  585. shader->setShiftBinding(glslang::EResSampler, bindingOffset + 32);
  586. shader->setShiftBinding(glslang::EResSsbo, bindingOffset + 16);
  587. shader->setShiftBinding(glslang::EResImage, bindingOffset + 32);
  588. const char* shaderStrings[] = { _code.c_str() };
  589. shader->setStrings(
  590. shaderStrings
  591. , BX_COUNTOF(shaderStrings)
  592. );
  593. bool compiled = shader->parse(&resourceLimits
  594. , 110
  595. , false
  596. , messages
  597. );
  598. bool linked = false;
  599. bool validated = true;
  600. if (!compiled)
  601. {
  602. const char* log = shader->getInfoLog();
  603. if (NULL != log)
  604. {
  605. int32_t source = 0;
  606. int32_t line = 0;
  607. int32_t column = 0;
  608. int32_t start = 0;
  609. int32_t end = INT32_MAX;
  610. bx::StringView err = bx::strFind(log, "ERROR:");
  611. bool found = false;
  612. if (!err.isEmpty() )
  613. {
  614. found = 2 == sscanf(err.getPtr(), "ERROR: %u:%u: '", &source, &line);
  615. if (found)
  616. {
  617. ++line;
  618. }
  619. }
  620. if (found)
  621. {
  622. start = bx::uint32_imax(1, line-10);
  623. end = start + 20;
  624. }
  625. printCode(_code.c_str(), line, start, end, column);
  626. bx::printf("%s\n", log);
  627. }
  628. }
  629. else
  630. {
  631. program->addShader(shader);
  632. linked = true
  633. && program->link(messages)
  634. && program->mapIO()
  635. ;
  636. if (!linked)
  637. {
  638. const char* log = program->getInfoLog();
  639. if (NULL != log)
  640. {
  641. bx::printf("%s\n", log);
  642. }
  643. }
  644. else
  645. {
  646. program->buildReflection();
  647. std::map<std::string, uint32_t> stageMap;
  648. if (_firstPass)
  649. {
  650. // first time through, we just find unused uniforms and get rid of them
  651. std::string output;
  652. bx::Error err;
  653. LineReader reader(_code.c_str() );
  654. while (err.isOk() )
  655. {
  656. char str[4096];
  657. int32_t len = bx::read(&reader, str, BX_COUNTOF(str), &err);
  658. if (err.isOk() )
  659. {
  660. std::string strLine(str, len);
  661. size_t index = strLine.find("uniform ");
  662. if (index != std::string::npos)
  663. {
  664. bool found = false;
  665. if (!bx::findIdentifierMatch(strLine.c_str(), "SamplerState").isEmpty() ||
  666. !bx::findIdentifierMatch(strLine.c_str(), "SamplerComparisonState").isEmpty())
  667. {
  668. found = true;
  669. }
  670. else
  671. {
  672. for (int32_t ii = 0, num = program->getNumLiveUniformVariables(); ii < num; ++ii)
  673. {
  674. // matching lines like: uniform u_name;
  675. // we want to replace "uniform" with "static" so that it's no longer
  676. // included in the uniform blob that the application must upload
  677. // we can't just remove them, because unused functions might still reference
  678. // them and cause a compile error when they're gone
  679. if (!bx::findIdentifierMatch(strLine.c_str(), program->getUniformName(ii)).isEmpty())
  680. {
  681. found = true;
  682. break;
  683. }
  684. }
  685. }
  686. if (!found)
  687. {
  688. strLine = strLine.replace(index, 7 /* uniform */, "static");
  689. }
  690. }
  691. output += strLine;
  692. }
  693. }
  694. // recompile with the unused uniforms converted to statics
  695. return compile(_options, _version, output.c_str(), _writer, false);
  696. }
  697. else
  698. {
  699. // second time, find sampler state and get its stage index
  700. bx::Error err;
  701. LineReader reader(_code.c_str());
  702. while (err.isOk())
  703. {
  704. char str[4096];
  705. int32_t len = bx::read(&reader, str, BX_COUNTOF(str), &err);
  706. if (err.isOk())
  707. {
  708. std::string strLine(str, len);
  709. size_t index = strLine.find("uniform ");
  710. if (index != std::string::npos)
  711. {
  712. if (!bx::findIdentifierMatch(strLine.c_str(), "SamplerState").isEmpty() ||
  713. !bx::findIdentifierMatch(strLine.c_str(), "SamplerComparisonState").isEmpty())
  714. {
  715. int32_t regNumber = extractStageNumber(strLine);
  716. bx::StringView found = bx::findIdentifierMatch(strLine.c_str(), "SamplerState");
  717. if (found.isEmpty() )
  718. {
  719. found = bx::findIdentifierMatch(
  720. strLine.c_str()
  721. , "SamplerComparisonState"
  722. );
  723. }
  724. const char* ptr = found.getPtr() + found.getLength();
  725. const char* start = NULL;
  726. const char* end = NULL;
  727. while (ptr < strLine.c_str() + strLine.size())
  728. {
  729. if (*ptr != ' ')
  730. {
  731. if (start == NULL)
  732. {
  733. start = ptr;
  734. }
  735. end = ptr;
  736. }
  737. else if (start != NULL)
  738. {
  739. break;
  740. }
  741. ptr++;
  742. }
  743. BX_CHECK(start != NULL && end != NULL, "sampler name cannot be found");
  744. std::string samplerName(start, end - start + 1);
  745. stageMap[samplerName] = regNumber;
  746. }
  747. }
  748. else if (!bx::findIdentifierMatch(strLine.c_str(), "StructuredBuffer").isEmpty()
  749. || !bx::findIdentifierMatch(strLine.c_str(), "RWStructuredBuffer").isEmpty() )
  750. {
  751. int32_t regNumber = extractStageNumber(strLine);
  752. const char* ptr = strLine.c_str();
  753. const char* start = NULL;
  754. const char* end = NULL;
  755. while (ptr < strLine.c_str() + strLine.size())
  756. {
  757. if (*ptr == '>')
  758. {
  759. start = ptr + 1;
  760. while (*start == ' ')
  761. {
  762. start++;
  763. }
  764. }
  765. if (*ptr == ':')
  766. {
  767. end = ptr - 1;
  768. while (*end == ' ')
  769. {
  770. end--;
  771. }
  772. }
  773. if (start != NULL && end != NULL)
  774. {
  775. break;
  776. }
  777. ptr++;
  778. }
  779. BX_CHECK(start != NULL && end != NULL, "sampler name cannot be found");
  780. std::string bufferName(start, end - start + 1);
  781. stageMap[bufferName] = regNumber;
  782. }
  783. }
  784. }
  785. }
  786. UniformArray uniforms;
  787. {
  788. uint16_t count = (uint16_t)program->getNumLiveUniformVariables();
  789. for (uint16_t ii = 0; ii < count; ++ii)
  790. {
  791. Uniform un;
  792. un.name = program->getUniformName(ii);
  793. un.num = uint8_t(program->getUniformArraySize(ii) );
  794. const uint32_t offset = program->getUniformBufferOffset(ii);
  795. un.regIndex = uint16_t(offset);
  796. un.regCount = un.num;
  797. switch (program->getUniformType(ii))
  798. {
  799. case 0x1404: // GL_INT:
  800. un.type = UniformType::Sampler;
  801. break;
  802. case 0x8B52: // GL_FLOAT_VEC4:
  803. un.type = UniformType::Vec4;
  804. break;
  805. case 0x8B5B: // GL_FLOAT_MAT3:
  806. un.type = UniformType::Mat3;
  807. un.regCount *= 3;
  808. break;
  809. case 0x8B5C: // GL_FLOAT_MAT4:
  810. un.type = UniformType::Mat4;
  811. un.regCount *= 4;
  812. break;
  813. default:
  814. un.type = UniformType::End;
  815. break;
  816. }
  817. uniforms.push_back(un);
  818. }
  819. }
  820. if (g_verbose)
  821. {
  822. program->dumpReflection();
  823. }
  824. BX_UNUSED(spv::MemorySemanticsAllMemory);
  825. glslang::TIntermediate* intermediate = program->getIntermediate(stage);
  826. std::vector<uint32_t> spirv;
  827. glslang::SpvOptions options;
  828. options.disableOptimizer = false;
  829. glslang::GlslangToSpv(*intermediate, spirv, &options);
  830. spvtools::Optimizer opt(SPV_ENV_VULKAN_1_0);
  831. auto print_msg_to_stderr = [](
  832. spv_message_level_t
  833. , const char*
  834. , const spv_position_t&
  835. , const char* m
  836. )
  837. {
  838. bx::printf("Error: %s\n", m);
  839. };
  840. opt.SetMessageConsumer(print_msg_to_stderr);
  841. opt.RegisterLegalizationPasses();
  842. if (!opt.Run(spirv.data(), spirv.size(), &spirv) )
  843. {
  844. compiled = false;
  845. }
  846. else
  847. {
  848. bx::Error err;
  849. bx::WriterI* writer = bx::getDebugOut();
  850. bx::MemoryReader reader(spirv.data(), uint32_t(spirv.size()*4) );
  851. disassemble(writer, &reader, &err);
  852. spirv_cross::CompilerReflection refl(spirv);
  853. spirv_cross::ShaderResources resourcesrefl = refl.get_shader_resources();
  854. // Loop through the separate_images, and extract the uniform names:
  855. for (auto &resource : resourcesrefl.separate_images)
  856. {
  857. std::string name = refl.get_name(resource.id);
  858. if (name.size() > 7
  859. && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
  860. {
  861. auto uniform_name = name.substr(0, name.length() - 7);
  862. Uniform un;
  863. un.name = uniform_name;
  864. un.type = UniformType::Sampler;
  865. uint32_t texture_binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  866. uint32_t sampler_binding_index = 0;
  867. std::string sampler_name;
  868. for (auto& sampler_resource : resourcesrefl.separate_samplers)
  869. {
  870. sampler_name = refl.get_name(sampler_resource.id);
  871. if (sampler_name.size() > 7
  872. && !bx::strFind(sampler_name.c_str(), uniform_name.c_str()).isEmpty()
  873. && (0 == bx::strCmp(sampler_name.c_str() + name.length() - 7, "Sampler") ||
  874. 0 == bx::strCmp(sampler_name.c_str() + name.length() - 7, "SamplerComparison")
  875. ) )
  876. {
  877. sampler_binding_index = refl.get_decoration(sampler_resource.id, spv::Decoration::DecorationBinding);
  878. break;
  879. }
  880. }
  881. un.num = stageMap[sampler_name]; // want to write stage index
  882. un.regIndex = texture_binding_index; // for sampled image binding index
  883. un.regCount = sampler_binding_index; // for sampler binding index
  884. uniforms.push_back(un);
  885. }
  886. }
  887. // Loop through the separate_images, and extract the uniform names:
  888. for (auto &resource : resourcesrefl.storage_images)
  889. {
  890. std::string name = refl.get_name(resource.id);
  891. if (name.size() > 7
  892. && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
  893. {
  894. auto uniform_name = name.substr(0, name.length() - 7);
  895. uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  896. std::string sampler_name = uniform_name + "Sampler";
  897. Uniform un;
  898. un.name = uniform_name;
  899. un.type = UniformType::End;
  900. un.num = stageMap[sampler_name]; // want to write stage index
  901. un.regIndex = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; // for descriptor type
  902. un.regCount = binding_index; // for image binding index
  903. uniforms.push_back(un);
  904. }
  905. }
  906. // Loop through the storage buffer, and extract the uniform names:
  907. for (auto& resource : resourcesrefl.storage_buffers)
  908. {
  909. std::string name = refl.get_name(resource.id);
  910. for (auto& uniform : uniforms)
  911. {
  912. if (!bx::strFind(uniform.name.c_str(), name.c_str()).isEmpty())
  913. {
  914. uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  915. uniform.name = name;
  916. uniform.type = UniformType::End;
  917. uniform.num = stageMap[name];
  918. uniform.regIndex = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
  919. uniform.regCount = binding_index;
  920. break;
  921. }
  922. }
  923. }
  924. uint16_t size = writeUniformArray( _writer, uniforms, _options.shaderType == 'f');
  925. if (_version == BX_MAKEFOURCC('M', 'T', 'L', 0) )
  926. {
  927. if (g_verbose)
  928. {
  929. glslang::SpirvToolsDisassemble(std::cout, spirv);
  930. }
  931. spirv_cross::CompilerMSL msl(std::move(spirv));
  932. spirv_cross::ShaderResources resources = msl.get_shader_resources();
  933. spirv_cross::SmallVector<spirv_cross::EntryPoint> entryPoints = msl.get_entry_points_and_stages();
  934. if (!entryPoints.empty() )
  935. {
  936. msl.rename_entry_point(
  937. entryPoints[0].name
  938. , "xlatMtlMain"
  939. , entryPoints[0].execution_model
  940. );
  941. }
  942. for (auto &resource : resources.uniform_buffers)
  943. {
  944. msl.set_name(resource.id, "_mtl_u");
  945. }
  946. for (auto &resource : resources.storage_buffers)
  947. {
  948. unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
  949. msl.set_decoration(resource.id, spv::DecorationBinding, binding + 1);
  950. }
  951. for (auto &resource : resources.separate_images)
  952. {
  953. std::string name = msl.get_name(resource.id);
  954. if (name.size() > 7
  955. && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
  956. {
  957. msl.set_name(resource.id, name.substr(0, name.length() - 7));
  958. }
  959. }
  960. std::string source = msl.compile();
  961. if ('c' == _options.shaderType)
  962. {
  963. for (int i = 0; i < 3; ++i)
  964. {
  965. uint16_t dim = (uint16_t)msl.get_execution_mode_argument(spv::ExecutionMode::ExecutionModeLocalSize, i);
  966. bx::write(_writer, dim);
  967. }
  968. }
  969. uint32_t shaderSize = (uint32_t)source.size();
  970. bx::write(_writer, shaderSize);
  971. bx::write(_writer, source.c_str(), shaderSize);
  972. uint8_t nul = 0;
  973. bx::write(_writer, nul);
  974. }
  975. else
  976. {
  977. uint32_t shaderSize = (uint32_t)spirv.size() * sizeof(uint32_t);
  978. bx::write(_writer, shaderSize);
  979. bx::write(_writer, spirv.data(), shaderSize);
  980. uint8_t nul = 0;
  981. bx::write(_writer, nul);
  982. }
  983. const uint8_t numAttr = (uint8_t)program->getNumLiveAttributes();
  984. bx::write(_writer, numAttr);
  985. for (uint8_t ii = 0; ii < numAttr; ++ii)
  986. {
  987. bgfx::Attrib::Enum attr = toAttribEnum(program->getAttributeName(ii) );
  988. if (bgfx::Attrib::Count != attr)
  989. {
  990. bx::write(_writer, bgfx::attribToId(attr) );
  991. }
  992. else
  993. {
  994. bx::write(_writer, uint16_t(UINT16_MAX) );
  995. }
  996. }
  997. bx::write(_writer, size);
  998. }
  999. }
  1000. }
  1001. delete program;
  1002. delete shader;
  1003. glslang::FinalizeProcess();
  1004. return compiled && linked && validated;
  1005. }
  1006. } // namespace spirv
  1007. bool compileSPIRVShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  1008. {
  1009. return spirv::compile(_options, _version, _code, _writer, true);
  1010. }
  1011. } // namespace bgfx