shaderc_spirv.cpp 27 KB

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