shaderc_spirv.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "shaderc.h"
  6. #include <iostream> // std::cout
  7. BX_PRAGMA_DIAGNOSTIC_PUSH()
  8. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // error C4100: 'inclusionDepth' : unreferenced formal parameter
  9. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4265) // error C4265: 'spv::spirvbin_t': class has virtual functions, but destructor is not virtual
  10. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wattributes") // warning: attribute ignored
  11. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wdeprecated-declarations") // warning: ‘MSLVertexAttr’ is deprecated
  12. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits") // warning: comparison of unsigned expression in ‘< 0’ is always false
  13. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow") // warning: declaration of 'userData' shadows a member of 'glslang::TShader::Includer::IncludeResult'
  14. #define SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
  15. #include <spirv_common.hpp>
  16. #include <spirv_msl.hpp>
  17. #include <spirv_reflect.hpp>
  18. #define ENABLE_OPT 1
  19. #include <ShaderLang.h>
  20. #include <ResourceLimits.h>
  21. #include <SPIRV/SPVRemapper.h>
  22. #include <SPIRV/GlslangToSpv.h>
  23. #include <SPIRV/SpvTools.h>
  24. #include <spirv-tools/optimizer.hpp>
  25. BX_PRAGMA_DIAGNOSTIC_POP()
  26. namespace bgfx
  27. {
  28. static bx::DefaultAllocator s_allocator;
  29. bx::AllocatorI* g_allocator = &s_allocator;
  30. struct TinyStlAllocator
  31. {
  32. static void* static_allocate(size_t _bytes);
  33. static void static_deallocate(void* _ptr, size_t /*_bytes*/);
  34. };
  35. void* TinyStlAllocator::static_allocate(size_t _bytes)
  36. {
  37. return bx::alloc(g_allocator, _bytes);
  38. }
  39. void TinyStlAllocator::static_deallocate(void* _ptr, size_t /*_bytes*/)
  40. {
  41. if (NULL != _ptr)
  42. {
  43. bx::free(g_allocator, _ptr);
  44. }
  45. }
  46. } // namespace bgfx
  47. #define TINYSTL_ALLOCATOR bgfx::TinyStlAllocator
  48. #include <tinystl/allocator.h>
  49. #include <tinystl/string.h>
  50. #include <tinystl/unordered_map.h>
  51. #include <tinystl/vector.h>
  52. namespace stl = tinystl;
  53. #include "../../src/shader.h"
  54. #include "../../src/shader_spirv.h"
  55. #include "../../3rdparty/khronos/vulkan-local/vulkan.h"
  56. namespace bgfx { namespace spirv
  57. {
  58. const TBuiltInResource resourceLimits =
  59. {
  60. 32, // MaxLights
  61. 6, // MaxClipPlanes
  62. 32, // MaxTextureUnits
  63. 32, // MaxTextureCoords
  64. 64, // MaxVertexAttribs
  65. 4096, // MaxVertexUniformComponents
  66. 64, // MaxVaryingFloats
  67. 32, // MaxVertexTextureImageUnits
  68. 80, // MaxCombinedTextureImageUnits
  69. 32, // MaxTextureImageUnits
  70. 4096, // MaxFragmentUniformComponents
  71. 32, // MaxDrawBuffers
  72. 128, // MaxVertexUniformVectors
  73. 8, // MaxVaryingVectors
  74. 16, // MaxFragmentUniformVectors
  75. 16, // MaxVertexOutputVectors
  76. 15, // MaxFragmentInputVectors
  77. -8, // MinProgramTexelOffset
  78. 7, // MaxProgramTexelOffset
  79. 8, // MaxClipDistances
  80. 65535, // MaxComputeWorkGroupCountX
  81. 65535, // MaxComputeWorkGroupCountY
  82. 65535, // MaxComputeWorkGroupCountZ
  83. 1024, // MaxComputeWorkGroupSizeX
  84. 1024, // MaxComputeWorkGroupSizeY
  85. 64, // MaxComputeWorkGroupSizeZ
  86. 1024, // MaxComputeUniformComponents
  87. 16, // MaxComputeTextureImageUnits
  88. 8, // MaxComputeImageUniforms
  89. 8, // MaxComputeAtomicCounters
  90. 1, // MaxComputeAtomicCounterBuffers
  91. 60, // MaxVaryingComponents
  92. 64, // MaxVertexOutputComponents
  93. 64, // MaxGeometryInputComponents
  94. 128, // MaxGeometryOutputComponents
  95. 128, // MaxFragmentInputComponents
  96. 8, // MaxImageUnits
  97. 8, // MaxCombinedImageUnitsAndFragmentOutputs
  98. 8, // MaxCombinedShaderOutputResources
  99. 0, // MaxImageSamples
  100. 0, // MaxVertexImageUniforms
  101. 0, // MaxTessControlImageUniforms
  102. 0, // MaxTessEvaluationImageUniforms
  103. 0, // MaxGeometryImageUniforms
  104. 8, // MaxFragmentImageUniforms
  105. 8, // MaxCombinedImageUniforms
  106. 16, // MaxGeometryTextureImageUnits
  107. 256, // MaxGeometryOutputVertices
  108. 1024, // MaxGeometryTotalOutputComponents
  109. 1024, // MaxGeometryUniformComponents
  110. 64, // MaxGeometryVaryingComponents
  111. 128, // MaxTessControlInputComponents
  112. 128, // MaxTessControlOutputComponents
  113. 16, // MaxTessControlTextureImageUnits
  114. 1024, // MaxTessControlUniformComponents
  115. 4096, // MaxTessControlTotalOutputComponents
  116. 128, // MaxTessEvaluationInputComponents
  117. 128, // MaxTessEvaluationOutputComponents
  118. 16, // MaxTessEvaluationTextureImageUnits
  119. 1024, // MaxTessEvaluationUniformComponents
  120. 120, // MaxTessPatchComponents
  121. 32, // MaxPatchVertices
  122. 64, // MaxTessGenLevel
  123. 16, // MaxViewports
  124. 0, // MaxVertexAtomicCounters
  125. 0, // MaxTessControlAtomicCounters
  126. 0, // MaxTessEvaluationAtomicCounters
  127. 0, // MaxGeometryAtomicCounters
  128. 8, // MaxFragmentAtomicCounters
  129. 8, // MaxCombinedAtomicCounters
  130. 1, // MaxAtomicCounterBindings
  131. 0, // MaxVertexAtomicCounterBuffers
  132. 0, // MaxTessControlAtomicCounterBuffers
  133. 0, // MaxTessEvaluationAtomicCounterBuffers
  134. 0, // MaxGeometryAtomicCounterBuffers
  135. 1, // MaxFragmentAtomicCounterBuffers
  136. 1, // MaxCombinedAtomicCounterBuffers
  137. 16384, // MaxAtomicCounterBufferSize
  138. 4, // MaxTransformFeedbackBuffers
  139. 64, // MaxTransformFeedbackInterleavedComponents
  140. 8, // MaxCullDistances
  141. 8, // MaxCombinedClipAndCullDistances
  142. 4, // MaxSamples
  143. 0, // maxMeshOutputVerticesNV
  144. 0, // maxMeshOutputPrimitivesNV
  145. 0, // maxMeshWorkGroupSizeX_NV
  146. 0, // maxMeshWorkGroupSizeY_NV
  147. 0, // maxMeshWorkGroupSizeZ_NV
  148. 0, // maxTaskWorkGroupSizeX_NV
  149. 0, // maxTaskWorkGroupSizeY_NV
  150. 0, // maxTaskWorkGroupSizeZ_NV
  151. 0, // maxMeshViewCountNV
  152. 0, // maxMeshOutputVerticesEXT
  153. 0, // maxMeshOutputPrimitivesEXT
  154. 0, // maxMeshWorkGroupSizeX_EXT
  155. 0, // maxMeshWorkGroupSizeY_EXT
  156. 0, // maxMeshWorkGroupSizeZ_EXT
  157. 0, // maxTaskWorkGroupSizeX_EXT
  158. 0, // maxTaskWorkGroupSizeY_EXT
  159. 0, // maxTaskWorkGroupSizeZ_EXT
  160. 0, // maxMeshViewCountEXT
  161. 0, // maxDualSourceDrawBuffersEXT
  162. { // limits
  163. true, // nonInductiveForLoops
  164. true, // whileLoops
  165. true, // doWhileLoops
  166. true, // generalUniformIndexing
  167. true, // generalAttributeMatrixVectorIndexing
  168. true, // generalVaryingIndexing
  169. true, // generalSamplerIndexing
  170. true, // generalVariableIndexing
  171. true, // generalConstantMatrixVectorIndexing
  172. },
  173. };
  174. bgfx::TextureComponentType::Enum SpirvCrossBaseTypeToFormatType(spirv_cross::SPIRType::BaseType spirvBaseType, bool depth)
  175. {
  176. if (depth)
  177. return bgfx::TextureComponentType::Depth;
  178. switch (spirvBaseType)
  179. {
  180. case spirv_cross::SPIRType::Float:
  181. return bgfx::TextureComponentType::Float;
  182. case spirv_cross::SPIRType::Int:
  183. return bgfx::TextureComponentType::Int;
  184. case spirv_cross::SPIRType::UInt:
  185. return bgfx::TextureComponentType::Uint;
  186. default:
  187. return bgfx::TextureComponentType::Float;
  188. }
  189. }
  190. bgfx::TextureDimension::Enum SpirvDimToTextureViewDimension(spv::Dim _dim, bool _arrayed)
  191. {
  192. switch (_dim)
  193. {
  194. case spv::Dim::Dim1D:
  195. return bgfx::TextureDimension::Dimension1D;
  196. case spv::Dim::Dim2D:
  197. return _arrayed
  198. ? bgfx::TextureDimension::Dimension2DArray
  199. : bgfx::TextureDimension::Dimension2D
  200. ;
  201. case spv::Dim::Dim3D:
  202. return bgfx::TextureDimension::Dimension3D;
  203. case spv::Dim::DimCube:
  204. return _arrayed
  205. ? bgfx::TextureDimension::DimensionCubeArray
  206. : bgfx::TextureDimension::DimensionCube
  207. ;
  208. default:
  209. BX_ASSERT(false, "Unknown texture dimension %d", _dim);
  210. return bgfx::TextureDimension::Dimension2D;
  211. }
  212. }
  213. static bgfx::TextureFormat::Enum s_textureFormats[] =
  214. {
  215. bgfx::TextureFormat::Unknown, // spv::ImageFormatUnknown = 0
  216. bgfx::TextureFormat::RGBA32F, // spv::ImageFormatRgba32f = 1
  217. bgfx::TextureFormat::RGBA16F, // spv::ImageFormatRgba16f = 2
  218. bgfx::TextureFormat::R32F, // spv::ImageFormatR32f = 3
  219. bgfx::TextureFormat::RGBA8, // spv::ImageFormatRgba8 = 4
  220. bgfx::TextureFormat::RGBA8S, // spv::ImageFormatRgba8Snorm = 5
  221. bgfx::TextureFormat::RG32F, // spv::ImageFormatRg32f = 6
  222. bgfx::TextureFormat::RG16F, // spv::ImageFormatRg16f = 7
  223. bgfx::TextureFormat::RG11B10F, // spv::ImageFormatR11fG11fB10f = 8
  224. bgfx::TextureFormat::R16F, // spv::ImageFormatR16f = 9
  225. bgfx::TextureFormat::RGBA16, // spv::ImageFormatRgba16 = 10
  226. bgfx::TextureFormat::RGB10A2, // spv::ImageFormatRgb10A2 = 11
  227. bgfx::TextureFormat::RG16, // spv::ImageFormatRg16 = 12
  228. bgfx::TextureFormat::RG8, // spv::ImageFormatRg8 = 13
  229. bgfx::TextureFormat::R16, // spv::ImageFormatR16 = 14
  230. bgfx::TextureFormat::R8, // spv::ImageFormatR8 = 15
  231. bgfx::TextureFormat::RGBA16S, // spv::ImageFormatRgba16Snorm = 16
  232. bgfx::TextureFormat::RG16S, // spv::ImageFormatRg16Snorm = 17
  233. bgfx::TextureFormat::RG8S, // spv::ImageFormatRg8Snorm = 18
  234. bgfx::TextureFormat::R16S, // spv::ImageFormatR16Snorm = 19
  235. bgfx::TextureFormat::R8S, // spv::ImageFormatR8Snorm = 20
  236. bgfx::TextureFormat::RGBA32I, // spv::ImageFormatRgba32i = 21
  237. bgfx::TextureFormat::RGBA16I, // spv::ImageFormatRgba16i = 22
  238. bgfx::TextureFormat::RGBA8I, // spv::ImageFormatRgba8i = 23
  239. bgfx::TextureFormat::R32I, // spv::ImageFormatR32i = 24
  240. bgfx::TextureFormat::RG32I, // spv::ImageFormatRg32i = 25
  241. bgfx::TextureFormat::RG16I, // spv::ImageFormatRg16i = 26
  242. bgfx::TextureFormat::RG8I, // spv::ImageFormatRg8i = 27
  243. bgfx::TextureFormat::R16I, // spv::ImageFormatR16i = 28
  244. bgfx::TextureFormat::R8I, // spv::ImageFormatR8i = 29
  245. bgfx::TextureFormat::RGBA32U, // spv::ImageFormatRgba32ui = 30
  246. bgfx::TextureFormat::RGBA16U, // spv::ImageFormatRgba16ui = 31
  247. bgfx::TextureFormat::RGBA8U, // spv::ImageFormatRgba8ui = 32
  248. bgfx::TextureFormat::R32U, // spv::ImageFormatR32ui = 33
  249. bgfx::TextureFormat::Unknown, // spv::ImageFormatRgb10a2ui = 34
  250. bgfx::TextureFormat::RG32U, // spv::ImageFormatRg32ui = 35
  251. bgfx::TextureFormat::RG16U, // spv::ImageFormatRg16ui = 36
  252. bgfx::TextureFormat::RG8U, // spv::ImageFormatRg8ui = 37
  253. bgfx::TextureFormat::R16U, // spv::ImageFormatR16ui = 38
  254. bgfx::TextureFormat::R8U, // spv::ImageFormatR8ui = 39
  255. bgfx::TextureFormat::Unknown, // spv::ImageFormatR64ui = 40
  256. bgfx::TextureFormat::Unknown, // spv::ImageFormatR64i = 41
  257. };
  258. static EShLanguage getLang(char _p)
  259. {
  260. switch (_p)
  261. {
  262. case 'c': return EShLangCompute;
  263. case 'f': return EShLangFragment;
  264. case 'v': return EShLangVertex;
  265. default: return EShLangCount;
  266. }
  267. }
  268. static const char* s_attribName[] =
  269. {
  270. "a_position",
  271. "a_normal",
  272. "a_tangent",
  273. "a_bitangent",
  274. "a_color0",
  275. "a_color1",
  276. "a_color2",
  277. "a_color3",
  278. "a_indices",
  279. "a_weight",
  280. "a_texcoord0",
  281. "a_texcoord1",
  282. "a_texcoord2",
  283. "a_texcoord3",
  284. "a_texcoord4",
  285. "a_texcoord5",
  286. "a_texcoord6",
  287. "a_texcoord7",
  288. };
  289. static_assert(bgfx::Attrib::Count == BX_COUNTOF(s_attribName) );
  290. bgfx::Attrib::Enum toAttribEnum(const bx::StringView& _name)
  291. {
  292. for (uint8_t ii = 0; ii < Attrib::Count; ++ii)
  293. {
  294. if (0 == bx::strCmp(s_attribName[ii], _name) )
  295. {
  296. return bgfx::Attrib::Enum(ii);
  297. }
  298. }
  299. return bgfx::Attrib::Count;
  300. }
  301. static const char* s_samplerTypes[] =
  302. {
  303. "BgfxSampler2D",
  304. "BgfxISampler2D",
  305. "BgfxUSampler2D",
  306. "BgfxSampler2DArray",
  307. "BgfxSampler2DShadow",
  308. "BgfxSampler2DArrayShadow",
  309. "BgfxSampler3D",
  310. "BgfxISampler3D",
  311. "BgfxUSampler3D",
  312. "BgfxSamplerCube",
  313. "BgfxSamplerCubeShadow",
  314. "BgfxSampler2DMS",
  315. };
  316. static uint16_t writeUniformArray(bx::WriterI* _shaderWriter, const UniformArray& uniforms, bool isFragmentShader)
  317. {
  318. uint16_t size = 0;
  319. bx::ErrorAssert err;
  320. uint16_t count = uint16_t(uniforms.size());
  321. bx::write(_shaderWriter, count, &err);
  322. uint32_t fragmentBit = isFragmentShader ? kUniformFragmentBit : 0;
  323. for (uint16_t ii = 0; ii < count; ++ii)
  324. {
  325. const Uniform& un = uniforms[ii];
  326. if ( (un.type & ~kUniformMask) > UniformType::End)
  327. {
  328. size = bx::max(size, (uint16_t)(un.regIndex + un.regCount*16) );
  329. }
  330. uint8_t nameSize = (uint8_t)un.name.size();
  331. bx::write(_shaderWriter, nameSize, &err);
  332. bx::write(_shaderWriter, un.name.c_str(), nameSize, &err);
  333. bx::write(_shaderWriter, uint8_t(un.type | fragmentBit), &err);
  334. bx::write(_shaderWriter, un.num, &err);
  335. bx::write(_shaderWriter, un.regIndex, &err);
  336. bx::write(_shaderWriter, un.regCount, &err);
  337. bx::write(_shaderWriter, un.texComponent, &err);
  338. bx::write(_shaderWriter, un.texDimension, &err);
  339. bx::write(_shaderWriter, un.texFormat, &err);
  340. BX_TRACE("%s, %s, %d, %d, %d"
  341. , un.name.c_str()
  342. , getUniformTypeName(UniformType::Enum(un.type & ~kUniformMask))
  343. , un.num
  344. , un.regIndex
  345. , un.regCount
  346. );
  347. }
  348. return size;
  349. }
  350. static spv_target_env getSpirvTargetVersion(uint32_t _version, bx::WriterI* _messageWriter)
  351. {
  352. bx::ErrorAssert err;
  353. switch (_version)
  354. {
  355. case 1010:
  356. return SPV_ENV_VULKAN_1_0;
  357. case 1311:
  358. return SPV_ENV_VULKAN_1_1;
  359. case 1411:
  360. return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
  361. case 1512:
  362. return SPV_ENV_VULKAN_1_2;
  363. case 1613:
  364. return SPV_ENV_VULKAN_1_3;
  365. default:
  366. bx::write(_messageWriter, &err, "Warning: Unknown SPIR-V version requested. Returning SPV_ENV_VULKAN_1_0 as default.\n");
  367. return SPV_ENV_VULKAN_1_0;
  368. }
  369. }
  370. static glslang::EShTargetClientVersion getGlslangTargetVulkanVersion(uint32_t _version, bx::WriterI* _messageWriter)
  371. {
  372. bx::ErrorAssert err;
  373. switch (_version)
  374. {
  375. case 1010:
  376. return glslang::EShTargetVulkan_1_0;
  377. case 1311:
  378. case 1411:
  379. return glslang::EShTargetVulkan_1_1;
  380. case 1512:
  381. return glslang::EShTargetVulkan_1_2;
  382. case 1613:
  383. return glslang::EShTargetVulkan_1_3;
  384. default:
  385. bx::write(_messageWriter, &err, "Warning: Unknown SPIR-V version requested. Returning EShTargetVulkan_1_0 as default.\n");
  386. return glslang::EShTargetVulkan_1_0;
  387. }
  388. }
  389. static glslang::EShTargetLanguageVersion getGlslangTargetSpirvVersion(uint32_t _version, bx::WriterI* _messageWriter)
  390. {
  391. bx::ErrorAssert err;
  392. switch (_version)
  393. {
  394. case 1010:
  395. return glslang::EShTargetSpv_1_0;
  396. case 1311:
  397. return glslang::EShTargetSpv_1_3;
  398. case 1411:
  399. return glslang::EShTargetSpv_1_4;
  400. case 1512:
  401. return glslang::EShTargetSpv_1_5;
  402. case 1613:
  403. return glslang::EShTargetSpv_1_6;
  404. default:
  405. bx::write(_messageWriter, &err, "Warning: Unknown SPIR-V version requested. Returning EShTargetSpv_1_0 as default.\n");
  406. return glslang::EShTargetSpv_1_0;
  407. }
  408. }
  409. /// This is the value used to fill out GLSLANG's SpvVersion object.
  410. /// The required value is that which is defined by GL_KHR_vulkan_glsl, which is defined here:
  411. /// https://github.com/KhronosGroup/GLSL/blob/master/extensions/khr/GL_KHR_vulkan_glsl.txt
  412. /// The value is 100.
  413. constexpr int s_GLSL_VULKAN_CLIENT_VERSION = 100;
  414. static bool compile(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _shaderWriter, bx::WriterI* _messageWriter, bool _firstPass)
  415. {
  416. BX_UNUSED(_version);
  417. bx::ErrorAssert messageErr;
  418. glslang::InitializeProcess();
  419. EShLanguage stage = getLang(_options.shaderType);
  420. if (EShLangCount == stage)
  421. {
  422. bx::write(_messageWriter, &messageErr, "Error: Unknown shader type '%c'.\n", _options.shaderType);
  423. return false;
  424. }
  425. glslang::TProgram* program = new glslang::TProgram;
  426. glslang::TShader* shader = new glslang::TShader(stage);
  427. EShMessages messages = EShMessages(0
  428. | EShMsgDefault
  429. | EShMsgReadHlsl
  430. | EShMsgVulkanRules
  431. | EShMsgSpvRules
  432. | EShMsgDebugInfo
  433. );
  434. shader->setEntryPoint("main");
  435. shader->setAutoMapBindings(true);
  436. shader->setEnvInput(glslang::EShSourceHlsl, stage, glslang::EShClientVulkan, s_GLSL_VULKAN_CLIENT_VERSION);
  437. shader->setEnvClient(glslang::EShClientVulkan, getGlslangTargetVulkanVersion(_version, _messageWriter));
  438. shader->setEnvTarget(glslang::EShTargetSpv, getGlslangTargetSpirvVersion(_version, _messageWriter));
  439. // Reserve two spots for the stage UBOs
  440. shader->setShiftBinding(glslang::EResUbo, (stage == EShLanguage::EShLangFragment ? kSpirvFragmentBinding : kSpirvVertexBinding));
  441. shader->setShiftBinding(glslang::EResTexture, kSpirvBindShift);
  442. shader->setShiftBinding(glslang::EResSampler, kSpirvBindShift + kSpirvSamplerShift);
  443. shader->setShiftBinding(glslang::EResSsbo, kSpirvBindShift);
  444. shader->setShiftBinding(glslang::EResImage, kSpirvBindShift);
  445. const char* shaderStrings[] = { _code.c_str() };
  446. shader->setStrings(
  447. shaderStrings
  448. , BX_COUNTOF(shaderStrings)
  449. );
  450. bool compiled = shader->parse(&resourceLimits
  451. , 110
  452. , false
  453. , messages
  454. );
  455. bool linked = false;
  456. bool validated = true;
  457. if (!compiled)
  458. {
  459. const char* log = shader->getInfoLog();
  460. if (NULL != log)
  461. {
  462. int32_t source = 0;
  463. int32_t line = 0;
  464. int32_t column = 0;
  465. int32_t start = 0;
  466. int32_t end = INT32_MAX;
  467. bx::StringView err = bx::strFind(log, "ERROR:");
  468. bool found = false;
  469. if (!err.isEmpty() )
  470. {
  471. found = 2 == sscanf(err.getPtr(), "ERROR: %u:%u: '", &source, &line);
  472. if (found)
  473. {
  474. ++line;
  475. }
  476. }
  477. if (found)
  478. {
  479. start = bx::uint32_imax(1, line-10);
  480. end = start + 20;
  481. }
  482. printCode(_code.c_str(), bx::uint32_satsub(line, 1), start, end, column);
  483. bx::write(_messageWriter, &messageErr, "%s\n", log);
  484. }
  485. }
  486. else
  487. {
  488. program->addShader(shader);
  489. linked = true
  490. && program->link(messages)
  491. && program->mapIO()
  492. ;
  493. if (!linked)
  494. {
  495. const char* log = program->getInfoLog();
  496. if (NULL != log)
  497. {
  498. bx::write(_messageWriter, &messageErr, "%s\n", log);
  499. }
  500. }
  501. else
  502. {
  503. program->buildReflection();
  504. if (_firstPass)
  505. {
  506. // first time through, we just find unused uniforms and get rid of them
  507. std::string output;
  508. struct Uniform
  509. {
  510. std::string name;
  511. std::string decl;
  512. };
  513. std::vector<Uniform> uniforms;
  514. bx::LineReader reader(_code.c_str() );
  515. while (!reader.isDone() )
  516. {
  517. bx::StringView strLine = reader.next();
  518. bool moved = false;
  519. bx::StringView str = strFind(strLine, "uniform ");
  520. if (!str.isEmpty() )
  521. {
  522. bool found = false;
  523. bool sampler = false;
  524. std::string name = "";
  525. // add to samplers
  526. for (uint32_t ii = 0; ii < BX_COUNTOF(s_samplerTypes); ++ii)
  527. {
  528. if (!bx::findIdentifierMatch(strLine, s_samplerTypes[ii]).isEmpty() )
  529. {
  530. found = true;
  531. sampler = true;
  532. break;
  533. }
  534. }
  535. if (!found)
  536. {
  537. for (int32_t ii = 0, num = program->getNumLiveUniformVariables(); ii < num; ++ii)
  538. {
  539. // matching lines like: uniform u_name;
  540. // we want to replace "uniform" with "static" so that it's no longer
  541. // included in the uniform blob that the application must upload
  542. // we can't just remove them, because unused functions might still reference
  543. // them and cause a compile error when they're gone
  544. if (!bx::findIdentifierMatch(strLine, program->getUniformName(ii) ).isEmpty() )
  545. {
  546. found = true;
  547. name = program->getUniformName(ii);
  548. break;
  549. }
  550. }
  551. }
  552. if (!found)
  553. {
  554. output.append(strLine.getPtr(), str.getPtr() );
  555. output += "static ";
  556. output.append(str.getTerm(), strLine.getTerm() );
  557. output += "\n";
  558. moved = true;
  559. }
  560. else if (!sampler)
  561. {
  562. Uniform uniform;
  563. uniform.name = name;
  564. uniform.decl = std::string(strLine.getPtr(), strLine.getTerm() );
  565. uniforms.push_back(uniform);
  566. moved = true;
  567. }
  568. }
  569. if (!moved)
  570. {
  571. output.append(strLine.getPtr(), strLine.getTerm() );
  572. output += "\n";
  573. }
  574. }
  575. std::string uniformBlock;
  576. uniformBlock += "cbuffer UniformBlock\n";
  577. uniformBlock += "{\n";
  578. for (const Uniform& uniform : uniforms)
  579. {
  580. uniformBlock += uniform.decl.substr(7 /* uniform */);
  581. uniformBlock += "\n";
  582. }
  583. uniformBlock += "};\n";
  584. output = uniformBlock + output;
  585. // recompile with the unused uniforms converted to statics
  586. delete program;
  587. delete shader;
  588. return compile(_options, _version, output.c_str(), _shaderWriter, _messageWriter, false);
  589. }
  590. UniformArray uniforms;
  591. {
  592. uint16_t count = (uint16_t)program->getNumLiveUniformVariables();
  593. for (uint16_t ii = 0; ii < count; ++ii)
  594. {
  595. Uniform un;
  596. un.name = program->getUniformName(ii);
  597. if (bx::hasSuffix(un.name.c_str(), ".@data") )
  598. {
  599. continue;
  600. }
  601. un.num = uint8_t(program->getUniformArraySize(ii) );
  602. const uint32_t offset = program->getUniformBufferOffset(ii);
  603. un.regIndex = uint16_t(offset);
  604. un.regCount = un.num;
  605. switch (program->getUniformType(ii) )
  606. {
  607. case 0x1404: // GL_INT:
  608. un.type = UniformType::Sampler;
  609. break;
  610. case 0x8B52: // GL_FLOAT_VEC4:
  611. un.type = UniformType::Vec4;
  612. break;
  613. case 0x8B5B: // GL_FLOAT_MAT3:
  614. un.type = UniformType::Mat3;
  615. un.regCount *= 3;
  616. break;
  617. case 0x8B5C: // GL_FLOAT_MAT4:
  618. un.type = UniformType::Mat4;
  619. un.regCount *= 4;
  620. break;
  621. default:
  622. continue;
  623. }
  624. uniforms.push_back(un);
  625. }
  626. }
  627. if (g_verbose)
  628. {
  629. program->dumpReflection();
  630. }
  631. glslang::TIntermediate* intermediate = program->getIntermediate(stage);
  632. std::vector<uint32_t> spirv;
  633. glslang::SpvOptions options;
  634. options.disableOptimizer = _options.debugInformation;
  635. options.generateDebugInfo = _options.debugInformation;
  636. options.emitNonSemanticShaderDebugInfo = _options.debugInformation;
  637. options.emitNonSemanticShaderDebugSource = _options.debugInformation;
  638. glslang::GlslangToSpv(*intermediate, spirv, &options);
  639. spvtools::Optimizer opt(getSpirvTargetVersion(_version, _messageWriter));
  640. auto print_msg_to_stderr = [_messageWriter, &messageErr](
  641. spv_message_level_t
  642. , const char*
  643. , const spv_position_t&
  644. , const char* m
  645. )
  646. {
  647. bx::write(_messageWriter, &messageErr, "Error: %s\n", m);
  648. };
  649. opt.SetMessageConsumer(print_msg_to_stderr);
  650. opt.RegisterLegalizationPasses();
  651. spvtools::ValidatorOptions validatorOptions;
  652. validatorOptions.SetBeforeHlslLegalization(true);
  653. if (!opt.Run(
  654. spirv.data()
  655. , spirv.size()
  656. , &spirv
  657. , validatorOptions
  658. , false
  659. ) )
  660. {
  661. compiled = false;
  662. }
  663. else
  664. {
  665. if (g_verbose)
  666. {
  667. glslang::SpirvToolsDisassemble(std::cout, spirv, getSpirvTargetVersion(_version, _messageWriter));
  668. }
  669. spirv_cross::CompilerReflection refl(spirv);
  670. spirv_cross::ShaderResources resourcesrefl = refl.get_shader_resources();
  671. // Loop through the separate_images, and extract the uniform names:
  672. for (auto &resource : resourcesrefl.separate_images)
  673. {
  674. std::string name = refl.get_name(resource.id);
  675. if (name.size() > 7
  676. && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
  677. {
  678. name = name.substr(0, name.length() - 7);
  679. }
  680. uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  681. auto imageType = refl.get_type(resource.base_type_id).image;
  682. auto componentType = refl.get_type(imageType.type).basetype;
  683. bool isCompareSampler = false;
  684. for (auto& sampler : resourcesrefl.separate_samplers)
  685. {
  686. if (binding_index + 16 == refl.get_decoration(sampler.id, spv::Decoration::DecorationBinding) )
  687. {
  688. std::string samplerName = refl.get_name(sampler.id);
  689. isCompareSampler = refl.variable_is_depth_or_compare(sampler.id) || samplerName.find("Comparison") != std::string::npos;
  690. break;
  691. }
  692. }
  693. Uniform un;
  694. un.name = name;
  695. un.type = UniformType::Enum(UniformType::Sampler
  696. | kUniformSamplerBit
  697. | (isCompareSampler ? kUniformCompareBit : 0)
  698. );
  699. un.texComponent = textureComponentTypeToId(SpirvCrossBaseTypeToFormatType(componentType, imageType.depth) );
  700. un.texDimension = textureDimensionToId(SpirvDimToTextureViewDimension(imageType.dim, imageType.arrayed) );
  701. un.texFormat = uint16_t(s_textureFormats[imageType.format]);
  702. un.regIndex = uint16_t(binding_index);
  703. un.regCount = 0; // unused
  704. uniforms.push_back(un);
  705. }
  706. // Loop through the storage_images, and extract the uniform names:
  707. for (auto &resource : resourcesrefl.storage_images)
  708. {
  709. std::string name = refl.get_name(resource.id);
  710. uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  711. auto imageType = refl.get_type(resource.base_type_id).image;
  712. auto componentType = refl.get_type(imageType.type).basetype;
  713. spirv_cross::Bitset flags = refl.get_decoration_bitset(resource.id);
  714. UniformType::Enum type = flags.get(spv::DecorationNonWritable)
  715. ? UniformType::Enum(kUniformReadOnlyBit | UniformType::End)
  716. : UniformType::End;
  717. Uniform un;
  718. un.name = name;
  719. un.type = type;
  720. un.texComponent = textureComponentTypeToId(SpirvCrossBaseTypeToFormatType(componentType, imageType.depth) );
  721. un.texDimension = textureDimensionToId(SpirvDimToTextureViewDimension(imageType.dim, imageType.arrayed) );
  722. un.texFormat = uint16_t(s_textureFormats[imageType.format]);
  723. un.regIndex = uint16_t(binding_index);
  724. un.regCount = descriptorTypeToId(DescriptorType::StorageImage);
  725. uniforms.push_back(un);
  726. }
  727. bx::Error err;
  728. // Loop through the storage buffer, and extract the uniform names:
  729. for (auto& resource : resourcesrefl.storage_buffers)
  730. {
  731. std::string name = refl.get_name(resource.id);
  732. uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  733. spirv_cross::Bitset flags = refl.get_buffer_block_flags(resource.id);
  734. UniformType::Enum type = flags.get(spv::DecorationNonWritable)
  735. ? UniformType::Enum(kUniformReadOnlyBit | UniformType::End)
  736. : UniformType::End;
  737. Uniform un;
  738. un.name = name;
  739. un.type = type;
  740. un.num = 0;
  741. un.regIndex = uint16_t(binding_index);
  742. un.regCount = descriptorTypeToId(DescriptorType::StorageBuffer);
  743. uniforms.push_back(un);
  744. }
  745. uint16_t size = writeUniformArray(_shaderWriter, uniforms, _options.shaderType == 'f');
  746. uint32_t shaderSize = (uint32_t)spirv.size() * sizeof(uint32_t);
  747. bx::write(_shaderWriter, shaderSize, &err);
  748. bx::write(_shaderWriter, spirv.data(), shaderSize, &err);
  749. uint8_t nul = 0;
  750. bx::write(_shaderWriter, nul, &err);
  751. const uint8_t numAttr = (uint8_t)program->getNumLiveAttributes();
  752. bx::write(_shaderWriter, numAttr, &err);
  753. for (uint8_t ii = 0; ii < numAttr; ++ii)
  754. {
  755. bgfx::Attrib::Enum attr = toAttribEnum(program->getAttributeName(ii) );
  756. if (bgfx::Attrib::Count != attr)
  757. {
  758. bx::write(_shaderWriter, bgfx::attribToId(attr), &err);
  759. }
  760. else
  761. {
  762. bx::write(_shaderWriter, uint16_t(UINT16_MAX), &err);
  763. }
  764. }
  765. bx::write(_shaderWriter, size, &err);
  766. }
  767. }
  768. }
  769. delete program;
  770. delete shader;
  771. glslang::FinalizeProcess();
  772. return compiled && linked && validated;
  773. }
  774. } // namespace spirv
  775. bool compileSPIRVShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _shaderWriter, bx::WriterI* _messageWriter)
  776. {
  777. return spirv::compile(_options, _version, _code, _shaderWriter, _messageWriter, true);
  778. }
  779. } // namespace bgfx