shaderc_spirv.cpp 27 KB

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