shaderc_spirv.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /*
  2. * Copyright 2011-2020 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-local/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. bgfx::Attrib::Enum toAttribEnum(const bx::StringView& _name)
  498. {
  499. for (uint8_t ii = 0; ii < Attrib::Count; ++ii)
  500. {
  501. if (0 == bx::strCmp(s_attribName[ii], _name) )
  502. {
  503. return bgfx::Attrib::Enum(ii);
  504. }
  505. }
  506. return bgfx::Attrib::Count;
  507. }
  508. static const char* s_samplerTypes[] =
  509. {
  510. "BgfxSampler2D",
  511. "BgfxISampler2D",
  512. "BgfxUSampler2D",
  513. "BgfxSampler2DArray",
  514. "BgfxSampler2DShadow",
  515. "BgfxSampler2DArrayShadow",
  516. "BgfxSampler3D",
  517. "BgfxISampler3D",
  518. "BgfxUSampler3D",
  519. "BgfxSamplerCube",
  520. "BgfxSamplerCubeShadow",
  521. "BgfxSampler2DMS",
  522. };
  523. static uint16_t writeUniformArray(bx::WriterI* _writer, const UniformArray& uniforms, bool isFragmentShader)
  524. {
  525. uint16_t size = 0;
  526. uint16_t count = static_cast<uint16_t>(uniforms.size());
  527. bx::write(_writer, count);
  528. uint32_t fragmentBit = isFragmentShader ? BGFX_UNIFORM_FRAGMENTBIT : 0;
  529. for (uint16_t ii = 0; ii < count; ++ii)
  530. {
  531. const Uniform& un = uniforms[ii];
  532. if ((un.type & ~BGFX_UNIFORM_MASK) > UniformType::End)
  533. size = bx::max(size, (uint16_t)(un.regIndex + un.regCount*16));
  534. uint8_t nameSize = (uint8_t)un.name.size();
  535. bx::write(_writer, nameSize);
  536. bx::write(_writer, un.name.c_str(), nameSize);
  537. bx::write(_writer, uint8_t(un.type | fragmentBit));
  538. bx::write(_writer, un.num);
  539. bx::write(_writer, un.regIndex);
  540. bx::write(_writer, un.regCount);
  541. BX_TRACE("%s, %s, %d, %d, %d"
  542. , un.name.c_str()
  543. , getUniformTypeName(un.type)
  544. , un.num
  545. , un.regIndex
  546. , un.regCount
  547. );
  548. }
  549. return size;
  550. }
  551. static bool compile(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bool _firstPass)
  552. {
  553. BX_UNUSED(_version);
  554. glslang::InitializeProcess();
  555. glslang::TProgram* program = new glslang::TProgram;
  556. EShLanguage stage = getLang(_options.shaderType);
  557. if (EShLangCount == stage)
  558. {
  559. bx::printf("Error: Unknown shader type '%c'.\n", _options.shaderType);
  560. return false;
  561. }
  562. glslang::TShader* shader = new glslang::TShader(stage);
  563. EShMessages messages = EShMessages(0
  564. | EShMsgDefault
  565. | EShMsgReadHlsl
  566. | EShMsgVulkanRules
  567. | EShMsgSpvRules
  568. );
  569. shader->setEntryPoint("main");
  570. shader->setAutoMapBindings(true);
  571. uint32_t bindingOffset = (stage == EShLanguage::EShLangFragment ? 48 : 0);
  572. shader->setShiftBinding(glslang::EResUbo, bindingOffset);
  573. shader->setShiftBinding(glslang::EResTexture, bindingOffset + 16);
  574. shader->setShiftBinding(glslang::EResSampler, bindingOffset + 32);
  575. shader->setShiftBinding(glslang::EResSsbo, bindingOffset + 16);
  576. shader->setShiftBinding(glslang::EResImage, bindingOffset + 32);
  577. const char* shaderStrings[] = { _code.c_str() };
  578. shader->setStrings(
  579. shaderStrings
  580. , BX_COUNTOF(shaderStrings)
  581. );
  582. bool compiled = shader->parse(&resourceLimits
  583. , 110
  584. , false
  585. , messages
  586. );
  587. bool linked = false;
  588. bool validated = true;
  589. if (!compiled)
  590. {
  591. const char* log = shader->getInfoLog();
  592. if (NULL != log)
  593. {
  594. int32_t source = 0;
  595. int32_t line = 0;
  596. int32_t column = 0;
  597. int32_t start = 0;
  598. int32_t end = INT32_MAX;
  599. bx::StringView err = bx::strFind(log, "ERROR:");
  600. bool found = false;
  601. if (!err.isEmpty() )
  602. {
  603. found = 2 == sscanf(err.getPtr(), "ERROR: %u:%u: '", &source, &line);
  604. if (found)
  605. {
  606. ++line;
  607. }
  608. }
  609. if (found)
  610. {
  611. start = bx::uint32_imax(1, line-10);
  612. end = start + 20;
  613. }
  614. printCode(_code.c_str(), line, start, end, column);
  615. bx::printf("%s\n", log);
  616. }
  617. }
  618. else
  619. {
  620. program->addShader(shader);
  621. linked = true
  622. && program->link(messages)
  623. && program->mapIO()
  624. ;
  625. if (!linked)
  626. {
  627. const char* log = program->getInfoLog();
  628. if (NULL != log)
  629. {
  630. bx::printf("%s\n", log);
  631. }
  632. }
  633. else
  634. {
  635. program->buildReflection();
  636. if (_firstPass)
  637. {
  638. // first time through, we just find unused uniforms and get rid of them
  639. std::string output;
  640. struct Uniform
  641. {
  642. std::string name;
  643. std::string decl;
  644. };
  645. std::vector<Uniform> uniforms;
  646. bx::Error err;
  647. LineReader reader(_code.c_str() );
  648. while (err.isOk() )
  649. {
  650. char str[4096];
  651. int32_t len = bx::read(&reader, str, BX_COUNTOF(str), &err);
  652. if (err.isOk() )
  653. {
  654. std::string strLine(str, len);
  655. bool moved = false;
  656. size_t index = strLine.find("uniform ");
  657. if (index != std::string::npos)
  658. {
  659. bool found = false;
  660. bool sampler = false;
  661. std::string name = "";
  662. // add to samplers
  663. for (uint32_t ii = 0; ii < BX_COUNTOF(s_samplerTypes); ++ii)
  664. {
  665. if (!bx::findIdentifierMatch(strLine.c_str(), s_samplerTypes[ii]).isEmpty())
  666. {
  667. found = true;
  668. sampler = true;
  669. break;
  670. }
  671. }
  672. if (!found)
  673. {
  674. for (int32_t ii = 0, num = program->getNumLiveUniformVariables(); ii < num; ++ii)
  675. {
  676. // matching lines like: uniform u_name;
  677. // we want to replace "uniform" with "static" so that it's no longer
  678. // included in the uniform blob that the application must upload
  679. // we can't just remove them, because unused functions might still reference
  680. // them and cause a compile error when they're gone
  681. if (!bx::findIdentifierMatch(strLine.c_str(), program->getUniformName(ii)).isEmpty())
  682. {
  683. found = true;
  684. name = program->getUniformName(ii);
  685. break;
  686. }
  687. }
  688. }
  689. if (!found)
  690. {
  691. strLine.replace(index, 7 /* uniform */, "static");
  692. }
  693. else if (!sampler)
  694. {
  695. Uniform uniform;
  696. uniform.name = name;
  697. uniform.decl = strLine;
  698. uniforms.push_back(uniform);
  699. moved = true;
  700. }
  701. }
  702. if (!moved)
  703. output += strLine;
  704. }
  705. }
  706. std::string uniformBlock;
  707. uniformBlock += "cbuffer UniformBlock\n";
  708. uniformBlock += "{\n";
  709. for (const Uniform& uniform : uniforms)
  710. {
  711. uniformBlock += uniform.decl.substr(7 /* uniform */);
  712. }
  713. uniformBlock += "};\n";
  714. output = uniformBlock + output;
  715. //std::cout << "[debug] uniforms: " << std::endl << uniformBlock << std::endl;
  716. // recompile with the unused uniforms converted to statics
  717. return compile(_options, _version, output.c_str(), _writer, false);
  718. }
  719. else
  720. {
  721. // second time, do nothing (todo remove)
  722. }
  723. UniformArray uniforms;
  724. {
  725. uint16_t count = (uint16_t)program->getNumLiveUniformVariables();
  726. for (uint16_t ii = 0; ii < count; ++ii)
  727. {
  728. Uniform un;
  729. un.name = program->getUniformName(ii);
  730. un.num = uint8_t(program->getUniformArraySize(ii) );
  731. const uint32_t offset = program->getUniformBufferOffset(ii);
  732. un.regIndex = uint16_t(offset);
  733. un.regCount = un.num;
  734. switch (program->getUniformType(ii))
  735. {
  736. case 0x1404: // GL_INT:
  737. un.type = UniformType::Sampler;
  738. break;
  739. case 0x8B52: // GL_FLOAT_VEC4:
  740. un.type = UniformType::Vec4;
  741. break;
  742. case 0x8B5B: // GL_FLOAT_MAT3:
  743. un.type = UniformType::Mat3;
  744. un.regCount *= 3;
  745. break;
  746. case 0x8B5C: // GL_FLOAT_MAT4:
  747. un.type = UniformType::Mat4;
  748. un.regCount *= 4;
  749. break;
  750. default:
  751. un.type = UniformType::End;
  752. break;
  753. }
  754. uniforms.push_back(un);
  755. }
  756. }
  757. if (g_verbose)
  758. {
  759. program->dumpReflection();
  760. }
  761. BX_UNUSED(spv::MemorySemanticsAllMemory);
  762. glslang::TIntermediate* intermediate = program->getIntermediate(stage);
  763. std::vector<uint32_t> spirv;
  764. glslang::SpvOptions options;
  765. options.disableOptimizer = false;
  766. glslang::GlslangToSpv(*intermediate, spirv, &options);
  767. spvtools::Optimizer opt(SPV_ENV_VULKAN_1_0);
  768. auto print_msg_to_stderr = [](
  769. spv_message_level_t
  770. , const char*
  771. , const spv_position_t&
  772. , const char* m
  773. )
  774. {
  775. bx::printf("Error: %s\n", m);
  776. };
  777. opt.SetMessageConsumer(print_msg_to_stderr);
  778. opt.RegisterLegalizationPasses();
  779. spvtools::ValidatorOptions validatorOptions;
  780. validatorOptions.SetBeforeHlslLegalization(true);
  781. if (!opt.Run(
  782. spirv.data()
  783. , spirv.size()
  784. , &spirv
  785. , validatorOptions
  786. , false
  787. ) )
  788. {
  789. compiled = false;
  790. }
  791. else
  792. {
  793. bx::Error err;
  794. bx::WriterI* writer = bx::getDebugOut();
  795. bx::MemoryReader reader(spirv.data(), uint32_t(spirv.size()*4) );
  796. disassemble(writer, &reader, &err);
  797. spirv_cross::CompilerReflection refl(spirv);
  798. spirv_cross::ShaderResources resourcesrefl = refl.get_shader_resources();
  799. // Loop through the separate_images, and extract the uniform names:
  800. for (auto &resource : resourcesrefl.separate_images)
  801. {
  802. std::string name = refl.get_name(resource.id);
  803. if (name.size() > 7
  804. && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
  805. {
  806. auto uniform_name = name.substr(0, name.length() - 7);
  807. Uniform un;
  808. un.name = uniform_name;
  809. un.type = UniformType::Enum(BGFX_UNIFORM_SAMPLERBIT | UniformType::Sampler);
  810. const uint32_t binding = refl.get_decoration(resource.id, spv::DecorationBinding);
  811. uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  812. un.regIndex = binding_index;
  813. un.regCount = 0; // unused
  814. uniforms.push_back(un);
  815. }
  816. }
  817. // Loop through the separate_images, and extract the uniform names:
  818. for (auto &resource : resourcesrefl.storage_images)
  819. {
  820. std::string name = refl.get_name(resource.id);
  821. if (name.size() > 7
  822. && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
  823. {
  824. auto uniform_name = name.substr(0, name.length() - 7);
  825. uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  826. std::string sampler_name = uniform_name + "Sampler";
  827. Uniform un;
  828. un.name = uniform_name;
  829. un.type = UniformType::End;
  830. un.regIndex = binding_index;
  831. un.regCount = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; // for descriptor type
  832. uniforms.push_back(un);
  833. }
  834. }
  835. // Loop through the storage buffer, and extract the uniform names:
  836. for (auto& resource : resourcesrefl.storage_buffers)
  837. {
  838. std::string name = refl.get_name(resource.id);
  839. for (auto& uniform : uniforms)
  840. {
  841. if (!bx::strFind(uniform.name.c_str(), name.c_str()).isEmpty())
  842. {
  843. uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  844. uniform.name = name;
  845. uniform.type = UniformType::End;
  846. uniform.regIndex = binding_index;
  847. uniform.regCount = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
  848. break;
  849. }
  850. }
  851. }
  852. uint16_t size = writeUniformArray( _writer, uniforms, _options.shaderType == 'f');
  853. if (_version == BX_MAKEFOURCC('M', 'T', 'L', 0) )
  854. {
  855. if (g_verbose)
  856. {
  857. glslang::SpirvToolsDisassemble(std::cout, spirv);
  858. }
  859. spirv_cross::CompilerMSL msl(std::move(spirv));
  860. spirv_cross::ShaderResources resources = msl.get_shader_resources();
  861. spirv_cross::SmallVector<spirv_cross::EntryPoint> entryPoints = msl.get_entry_points_and_stages();
  862. if (!entryPoints.empty() )
  863. {
  864. msl.rename_entry_point(
  865. entryPoints[0].name
  866. , "xlatMtlMain"
  867. , entryPoints[0].execution_model
  868. );
  869. }
  870. for (auto &resource : resources.uniform_buffers)
  871. {
  872. msl.set_name(resource.id, "_mtl_u");
  873. }
  874. for (auto &resource : resources.storage_buffers)
  875. {
  876. unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
  877. msl.set_decoration(resource.id, spv::DecorationBinding, binding + 1);
  878. }
  879. for (auto &resource : resources.separate_images)
  880. {
  881. std::string name = msl.get_name(resource.id);
  882. if (name.size() > 7
  883. && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
  884. {
  885. msl.set_name(resource.id, name.substr(0, name.length() - 7));
  886. }
  887. }
  888. std::string source = msl.compile();
  889. if ('c' == _options.shaderType)
  890. {
  891. for (int i = 0; i < 3; ++i)
  892. {
  893. uint16_t dim = (uint16_t)msl.get_execution_mode_argument(spv::ExecutionMode::ExecutionModeLocalSize, i);
  894. bx::write(_writer, dim);
  895. }
  896. }
  897. uint32_t shaderSize = (uint32_t)source.size();
  898. bx::write(_writer, shaderSize);
  899. bx::write(_writer, source.c_str(), shaderSize);
  900. uint8_t nul = 0;
  901. bx::write(_writer, nul);
  902. }
  903. else
  904. {
  905. uint32_t shaderSize = (uint32_t)spirv.size() * sizeof(uint32_t);
  906. bx::write(_writer, shaderSize);
  907. bx::write(_writer, spirv.data(), shaderSize);
  908. uint8_t nul = 0;
  909. bx::write(_writer, nul);
  910. }
  911. const uint8_t numAttr = (uint8_t)program->getNumLiveAttributes();
  912. bx::write(_writer, numAttr);
  913. for (uint8_t ii = 0; ii < numAttr; ++ii)
  914. {
  915. bgfx::Attrib::Enum attr = toAttribEnum(program->getAttributeName(ii) );
  916. if (bgfx::Attrib::Count != attr)
  917. {
  918. bx::write(_writer, bgfx::attribToId(attr) );
  919. }
  920. else
  921. {
  922. bx::write(_writer, uint16_t(UINT16_MAX) );
  923. }
  924. }
  925. bx::write(_writer, size);
  926. }
  927. }
  928. }
  929. delete program;
  930. delete shader;
  931. glslang::FinalizeProcess();
  932. return compiled && linked && validated;
  933. }
  934. } // namespace spirv
  935. bool compileSPIRVShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  936. {
  937. return spirv::compile(_options, _version, _code, _writer, true);
  938. }
  939. } // namespace bgfx