shaderc_spirv.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * Copyright 2011-2022 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, // maxDualSourceDrawBuffersEXT
  151. { // limits
  152. true, // nonInductiveForLoops
  153. true, // whileLoops
  154. true, // doWhileLoops
  155. true, // generalUniformIndexing
  156. true, // generalAttributeMatrixVectorIndexing
  157. true, // generalVaryingIndexing
  158. true, // generalSamplerIndexing
  159. true, // generalVariableIndexing
  160. true, // generalConstantMatrixVectorIndexing
  161. },
  162. };
  163. bgfx::TextureComponentType::Enum SpirvCrossBaseTypeToFormatType(spirv_cross::SPIRType::BaseType spirvBaseType, bool depth)
  164. {
  165. if (depth)
  166. return bgfx::TextureComponentType::Depth;
  167. switch (spirvBaseType)
  168. {
  169. case spirv_cross::SPIRType::Float:
  170. return bgfx::TextureComponentType::Float;
  171. case spirv_cross::SPIRType::Int:
  172. return bgfx::TextureComponentType::Int;
  173. case spirv_cross::SPIRType::UInt:
  174. return bgfx::TextureComponentType::Uint;
  175. default:
  176. return bgfx::TextureComponentType::Float;
  177. }
  178. }
  179. bgfx::TextureDimension::Enum SpirvDimToTextureViewDimension(spv::Dim _dim, bool _arrayed)
  180. {
  181. switch (_dim)
  182. {
  183. case spv::Dim::Dim1D:
  184. return bgfx::TextureDimension::Dimension1D;
  185. case spv::Dim::Dim2D:
  186. return _arrayed
  187. ? bgfx::TextureDimension::Dimension2DArray
  188. : bgfx::TextureDimension::Dimension2D
  189. ;
  190. case spv::Dim::Dim3D:
  191. return bgfx::TextureDimension::Dimension3D;
  192. case spv::Dim::DimCube:
  193. return _arrayed
  194. ? bgfx::TextureDimension::DimensionCubeArray
  195. : bgfx::TextureDimension::DimensionCube
  196. ;
  197. default:
  198. BX_ASSERT(false, "Unknown texture dimension %d", _dim);
  199. return bgfx::TextureDimension::Dimension2D;
  200. }
  201. }
  202. static bgfx::TextureFormat::Enum s_textureFormats[] =
  203. {
  204. bgfx::TextureFormat::Unknown, // spv::ImageFormatUnknown = 0
  205. bgfx::TextureFormat::RGBA32F, // spv::ImageFormatRgba32f = 1
  206. bgfx::TextureFormat::RGBA16F, // spv::ImageFormatRgba16f = 2
  207. bgfx::TextureFormat::R32F, // spv::ImageFormatR32f = 3
  208. bgfx::TextureFormat::RGBA8, // spv::ImageFormatRgba8 = 4
  209. bgfx::TextureFormat::RGBA8S, // spv::ImageFormatRgba8Snorm = 5
  210. bgfx::TextureFormat::RG32F, // spv::ImageFormatRg32f = 6
  211. bgfx::TextureFormat::RG16F, // spv::ImageFormatRg16f = 7
  212. bgfx::TextureFormat::RG11B10F, // spv::ImageFormatR11fG11fB10f = 8
  213. bgfx::TextureFormat::R16F, // spv::ImageFormatR16f = 9
  214. bgfx::TextureFormat::RGBA16, // spv::ImageFormatRgba16 = 10
  215. bgfx::TextureFormat::RGB10A2, // spv::ImageFormatRgb10A2 = 11
  216. bgfx::TextureFormat::RG16, // spv::ImageFormatRg16 = 12
  217. bgfx::TextureFormat::RG8, // spv::ImageFormatRg8 = 13
  218. bgfx::TextureFormat::R16, // spv::ImageFormatR16 = 14
  219. bgfx::TextureFormat::R8, // spv::ImageFormatR8 = 15
  220. bgfx::TextureFormat::RGBA16S, // spv::ImageFormatRgba16Snorm = 16
  221. bgfx::TextureFormat::RG16S, // spv::ImageFormatRg16Snorm = 17
  222. bgfx::TextureFormat::RG8S, // spv::ImageFormatRg8Snorm = 18
  223. bgfx::TextureFormat::R16S, // spv::ImageFormatR16Snorm = 19
  224. bgfx::TextureFormat::R8S, // spv::ImageFormatR8Snorm = 20
  225. bgfx::TextureFormat::RGBA32I, // spv::ImageFormatRgba32i = 21
  226. bgfx::TextureFormat::RGBA16I, // spv::ImageFormatRgba16i = 22
  227. bgfx::TextureFormat::RGBA8I, // spv::ImageFormatRgba8i = 23
  228. bgfx::TextureFormat::R32I, // spv::ImageFormatR32i = 24
  229. bgfx::TextureFormat::RG32I, // spv::ImageFormatRg32i = 25
  230. bgfx::TextureFormat::RG16I, // spv::ImageFormatRg16i = 26
  231. bgfx::TextureFormat::RG8I, // spv::ImageFormatRg8i = 27
  232. bgfx::TextureFormat::R16I, // spv::ImageFormatR16i = 28
  233. bgfx::TextureFormat::R8I, // spv::ImageFormatR8i = 29
  234. bgfx::TextureFormat::RGBA32U, // spv::ImageFormatRgba32ui = 30
  235. bgfx::TextureFormat::RGBA16U, // spv::ImageFormatRgba16ui = 31
  236. bgfx::TextureFormat::RGBA8U, // spv::ImageFormatRgba8ui = 32
  237. bgfx::TextureFormat::R32U, // spv::ImageFormatR32ui = 33
  238. bgfx::TextureFormat::Unknown, // spv::ImageFormatRgb10a2ui = 34
  239. bgfx::TextureFormat::RG32U, // spv::ImageFormatRg32ui = 35
  240. bgfx::TextureFormat::RG16U, // spv::ImageFormatRg16ui = 36
  241. bgfx::TextureFormat::RG8U, // spv::ImageFormatRg8ui = 37
  242. bgfx::TextureFormat::R16U, // spv::ImageFormatR16ui = 38
  243. bgfx::TextureFormat::R8U, // spv::ImageFormatR8ui = 39
  244. bgfx::TextureFormat::Unknown, // spv::ImageFormatR64ui = 40
  245. bgfx::TextureFormat::Unknown, // spv::ImageFormatR64i = 41
  246. };
  247. static EShLanguage getLang(char _p)
  248. {
  249. switch (_p)
  250. {
  251. case 'c': return EShLangCompute;
  252. case 'f': return EShLangFragment;
  253. case 'v': return EShLangVertex;
  254. default: return EShLangCount;
  255. }
  256. }
  257. static const char* s_attribName[] =
  258. {
  259. "a_position",
  260. "a_normal",
  261. "a_tangent",
  262. "a_bitangent",
  263. "a_color0",
  264. "a_color1",
  265. "a_color2",
  266. "a_color3",
  267. "a_indices",
  268. "a_weight",
  269. "a_texcoord0",
  270. "a_texcoord1",
  271. "a_texcoord2",
  272. "a_texcoord3",
  273. "a_texcoord4",
  274. "a_texcoord5",
  275. "a_texcoord6",
  276. "a_texcoord7",
  277. };
  278. BX_STATIC_ASSERT(bgfx::Attrib::Count == BX_COUNTOF(s_attribName) );
  279. bgfx::Attrib::Enum toAttribEnum(const bx::StringView& _name)
  280. {
  281. for (uint8_t ii = 0; ii < Attrib::Count; ++ii)
  282. {
  283. if (0 == bx::strCmp(s_attribName[ii], _name) )
  284. {
  285. return bgfx::Attrib::Enum(ii);
  286. }
  287. }
  288. return bgfx::Attrib::Count;
  289. }
  290. static const char* s_samplerTypes[] =
  291. {
  292. "BgfxSampler2D",
  293. "BgfxISampler2D",
  294. "BgfxUSampler2D",
  295. "BgfxSampler2DArray",
  296. "BgfxSampler2DShadow",
  297. "BgfxSampler2DArrayShadow",
  298. "BgfxSampler3D",
  299. "BgfxISampler3D",
  300. "BgfxUSampler3D",
  301. "BgfxSamplerCube",
  302. "BgfxSamplerCubeShadow",
  303. "BgfxSampler2DMS",
  304. };
  305. static uint16_t writeUniformArray(bx::WriterI* _writer, const UniformArray& uniforms, bool isFragmentShader)
  306. {
  307. uint16_t size = 0;
  308. bx::ErrorAssert err;
  309. uint16_t count = uint16_t(uniforms.size());
  310. bx::write(_writer, count, &err);
  311. uint32_t fragmentBit = isFragmentShader ? kUniformFragmentBit : 0;
  312. for (uint16_t ii = 0; ii < count; ++ii)
  313. {
  314. const Uniform& un = uniforms[ii];
  315. if ( (un.type & ~kUniformMask) > UniformType::End)
  316. {
  317. size = bx::max(size, (uint16_t)(un.regIndex + un.regCount*16) );
  318. }
  319. uint8_t nameSize = (uint8_t)un.name.size();
  320. bx::write(_writer, nameSize, &err);
  321. bx::write(_writer, un.name.c_str(), nameSize, &err);
  322. bx::write(_writer, uint8_t(un.type | fragmentBit), &err);
  323. bx::write(_writer, un.num, &err);
  324. bx::write(_writer, un.regIndex, &err);
  325. bx::write(_writer, un.regCount, &err);
  326. bx::write(_writer, un.texComponent, &err);
  327. bx::write(_writer, un.texDimension, &err);
  328. bx::write(_writer, un.texFormat, &err);
  329. BX_TRACE("%s, %s, %d, %d, %d"
  330. , un.name.c_str()
  331. , getUniformTypeName(UniformType::Enum(un.type & ~kUniformMask))
  332. , un.num
  333. , un.regIndex
  334. , un.regCount
  335. );
  336. }
  337. return size;
  338. }
  339. static spv_target_env getSpirvTargetVersion(uint32_t version)
  340. {
  341. switch (version)
  342. {
  343. case 1010:
  344. return SPV_ENV_VULKAN_1_0;
  345. case 1311:
  346. return SPV_ENV_VULKAN_1_1;
  347. case 1411:
  348. return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
  349. case 1512:
  350. return SPV_ENV_VULKAN_1_2;
  351. default:
  352. BX_ASSERT(0, "Unknown SPIR-V version requested. Returning SPV_ENV_VULKAN_1_0 as default.");
  353. return SPV_ENV_VULKAN_1_0;
  354. }
  355. }
  356. static glslang::EShTargetClientVersion getGlslangTargetVulkanVersion(uint32_t version)
  357. {
  358. switch (version)
  359. {
  360. case 1010:
  361. return glslang::EShTargetVulkan_1_0;
  362. case 1311:
  363. case 1411:
  364. return glslang::EShTargetVulkan_1_1;
  365. case 1512:
  366. return glslang::EShTargetVulkan_1_2;
  367. default:
  368. BX_ASSERT(0, "Unknown SPIR-V version requested. Returning EShTargetVulkan_1_0 as default.");
  369. return glslang::EShTargetVulkan_1_0;
  370. }
  371. }
  372. static glslang::EShTargetLanguageVersion getGlslangTargetSpirvVersion(uint32_t version)
  373. {
  374. switch (version)
  375. {
  376. case 1010:
  377. return glslang::EShTargetSpv_1_0;
  378. case 1311:
  379. return glslang::EShTargetSpv_1_3;
  380. case 1411:
  381. return glslang::EShTargetSpv_1_4;
  382. case 1512:
  383. return glslang::EShTargetSpv_1_5;
  384. default:
  385. BX_ASSERT(0, "Unknown SPIR-V version requested. Returning EShTargetSpv_1_0 as default.");
  386. return glslang::EShTargetSpv_1_0;
  387. }
  388. }
  389. /// This is the value used to fill out GLSLANG's SpvVersion object.
  390. /// The required value is that which is defined by GL_KHR_vulkan_glsl, which is defined here:
  391. /// https://github.com/KhronosGroup/GLSL/blob/master/extensions/khr/GL_KHR_vulkan_glsl.txt
  392. /// The value is 100.
  393. constexpr int s_GLSL_VULKAN_CLIENT_VERSION = 100;
  394. static bool compile(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bool _firstPass)
  395. {
  396. BX_UNUSED(_version);
  397. glslang::InitializeProcess();
  398. EShLanguage stage = getLang(_options.shaderType);
  399. if (EShLangCount == stage)
  400. {
  401. bx::printf("Error: Unknown shader type '%c'.\n", _options.shaderType);
  402. return false;
  403. }
  404. glslang::TProgram* program = new glslang::TProgram;
  405. glslang::TShader* shader = new glslang::TShader(stage);
  406. EShMessages messages = EShMessages(0
  407. | EShMsgDefault
  408. | EShMsgReadHlsl
  409. | EShMsgVulkanRules
  410. | EShMsgSpvRules
  411. );
  412. shader->setEntryPoint("main");
  413. shader->setAutoMapBindings(true);
  414. shader->setEnvInput(glslang::EShSourceHlsl, stage, glslang::EShClientVulkan, s_GLSL_VULKAN_CLIENT_VERSION);
  415. shader->setEnvClient(glslang::EShClientVulkan, getGlslangTargetVulkanVersion(_version));
  416. shader->setEnvTarget(glslang::EShTargetSpv, getGlslangTargetSpirvVersion(_version));
  417. // Reserve two spots for the stage UBOs
  418. shader->setShiftBinding(glslang::EResUbo, (stage == EShLanguage::EShLangFragment ? kSpirvFragmentBinding : kSpirvVertexBinding));
  419. shader->setShiftBinding(glslang::EResTexture, kSpirvBindShift);
  420. shader->setShiftBinding(glslang::EResSampler, kSpirvBindShift + kSpirvSamplerShift);
  421. shader->setShiftBinding(glslang::EResSsbo, kSpirvBindShift);
  422. shader->setShiftBinding(glslang::EResImage, kSpirvBindShift);
  423. const char* shaderStrings[] = { _code.c_str() };
  424. shader->setStrings(
  425. shaderStrings
  426. , BX_COUNTOF(shaderStrings)
  427. );
  428. bool compiled = shader->parse(&resourceLimits
  429. , 110
  430. , false
  431. , messages
  432. );
  433. bool linked = false;
  434. bool validated = true;
  435. if (!compiled)
  436. {
  437. const char* log = shader->getInfoLog();
  438. if (NULL != log)
  439. {
  440. int32_t source = 0;
  441. int32_t line = 0;
  442. int32_t column = 0;
  443. int32_t start = 0;
  444. int32_t end = INT32_MAX;
  445. bx::StringView err = bx::strFind(log, "ERROR:");
  446. bool found = false;
  447. if (!err.isEmpty() )
  448. {
  449. found = 2 == sscanf(err.getPtr(), "ERROR: %u:%u: '", &source, &line);
  450. if (found)
  451. {
  452. ++line;
  453. }
  454. }
  455. if (found)
  456. {
  457. start = bx::uint32_imax(1, line-10);
  458. end = start + 20;
  459. }
  460. printCode(_code.c_str(), line, start, end, column);
  461. bx::printf("%s\n", log);
  462. }
  463. }
  464. else
  465. {
  466. program->addShader(shader);
  467. linked = true
  468. && program->link(messages)
  469. && program->mapIO()
  470. ;
  471. if (!linked)
  472. {
  473. const char* log = program->getInfoLog();
  474. if (NULL != log)
  475. {
  476. bx::printf("%s\n", log);
  477. }
  478. }
  479. else
  480. {
  481. program->buildReflection();
  482. if (_firstPass)
  483. {
  484. // first time through, we just find unused uniforms and get rid of them
  485. std::string output;
  486. struct Uniform
  487. {
  488. std::string name;
  489. std::string decl;
  490. };
  491. std::vector<Uniform> uniforms;
  492. bx::LineReader reader(_code.c_str() );
  493. while (!reader.isDone() )
  494. {
  495. bx::StringView strLine = reader.next();
  496. bool moved = false;
  497. bx::StringView str = strFind(strLine, "uniform ");
  498. if (!str.isEmpty() )
  499. {
  500. bool found = false;
  501. bool sampler = false;
  502. std::string name = "";
  503. // add to samplers
  504. for (uint32_t ii = 0; ii < BX_COUNTOF(s_samplerTypes); ++ii)
  505. {
  506. if (!bx::findIdentifierMatch(strLine, s_samplerTypes[ii]).isEmpty() )
  507. {
  508. found = true;
  509. sampler = true;
  510. break;
  511. }
  512. }
  513. if (!found)
  514. {
  515. for (int32_t ii = 0, num = program->getNumLiveUniformVariables(); ii < num; ++ii)
  516. {
  517. // matching lines like: uniform u_name;
  518. // we want to replace "uniform" with "static" so that it's no longer
  519. // included in the uniform blob that the application must upload
  520. // we can't just remove them, because unused functions might still reference
  521. // them and cause a compile error when they're gone
  522. if (!bx::findIdentifierMatch(strLine, program->getUniformName(ii) ).isEmpty() )
  523. {
  524. found = true;
  525. name = program->getUniformName(ii);
  526. break;
  527. }
  528. }
  529. }
  530. if (!found)
  531. {
  532. output.append(strLine.getPtr(), str.getPtr() );
  533. output += "static ";
  534. output.append(str.getTerm(), strLine.getTerm() );
  535. output += "\n";
  536. moved = true;
  537. }
  538. else if (!sampler)
  539. {
  540. Uniform uniform;
  541. uniform.name = name;
  542. uniform.decl = std::string(strLine.getPtr(), strLine.getTerm() );
  543. uniforms.push_back(uniform);
  544. moved = true;
  545. }
  546. }
  547. if (!moved)
  548. {
  549. output.append(strLine.getPtr(), strLine.getTerm() );
  550. output += "\n";
  551. }
  552. }
  553. std::string uniformBlock;
  554. uniformBlock += "cbuffer UniformBlock\n";
  555. uniformBlock += "{\n";
  556. for (const Uniform& uniform : uniforms)
  557. {
  558. uniformBlock += uniform.decl.substr(7 /* uniform */);
  559. uniformBlock += "\n";
  560. }
  561. uniformBlock += "};\n";
  562. output = uniformBlock + output;
  563. // recompile with the unused uniforms converted to statics
  564. delete program;
  565. delete shader;
  566. return compile(_options, _version, output.c_str(), _writer, false);
  567. }
  568. UniformArray uniforms;
  569. {
  570. uint16_t count = (uint16_t)program->getNumLiveUniformVariables();
  571. for (uint16_t ii = 0; ii < count; ++ii)
  572. {
  573. Uniform un;
  574. un.name = program->getUniformName(ii);
  575. if (bx::hasSuffix(un.name.c_str(), ".@data") )
  576. {
  577. continue;
  578. }
  579. un.num = 0;
  580. const uint32_t offset = program->getUniformBufferOffset(ii);
  581. un.regIndex = uint16_t(offset);
  582. un.regCount = uint16_t(program->getUniformArraySize(ii));
  583. switch (program->getUniformType(ii) )
  584. {
  585. case 0x1404: // GL_INT:
  586. un.type = UniformType::Sampler;
  587. break;
  588. case 0x8B52: // GL_FLOAT_VEC4:
  589. un.type = UniformType::Vec4;
  590. break;
  591. case 0x8B5B: // GL_FLOAT_MAT3:
  592. un.type = UniformType::Mat3;
  593. un.regCount *= 3;
  594. break;
  595. case 0x8B5C: // GL_FLOAT_MAT4:
  596. un.type = UniformType::Mat4;
  597. un.regCount *= 4;
  598. break;
  599. default:
  600. continue;
  601. }
  602. uniforms.push_back(un);
  603. }
  604. }
  605. if (g_verbose)
  606. {
  607. program->dumpReflection();
  608. }
  609. BX_UNUSED(spv::MemorySemanticsAllMemory);
  610. glslang::TIntermediate* intermediate = program->getIntermediate(stage);
  611. std::vector<uint32_t> spirv;
  612. glslang::SpvOptions options;
  613. options.disableOptimizer = false;
  614. glslang::GlslangToSpv(*intermediate, spirv, &options);
  615. spvtools::Optimizer opt(getSpirvTargetVersion(_version));
  616. auto print_msg_to_stderr = [](
  617. spv_message_level_t
  618. , const char*
  619. , const spv_position_t&
  620. , const char* m
  621. )
  622. {
  623. bx::printf("Error: %s\n", m);
  624. };
  625. opt.SetMessageConsumer(print_msg_to_stderr);
  626. opt.RegisterLegalizationPasses();
  627. spvtools::ValidatorOptions validatorOptions;
  628. validatorOptions.SetBeforeHlslLegalization(true);
  629. if (!opt.Run(
  630. spirv.data()
  631. , spirv.size()
  632. , &spirv
  633. , validatorOptions
  634. , false
  635. ) )
  636. {
  637. compiled = false;
  638. }
  639. else
  640. {
  641. if (g_verbose)
  642. {
  643. glslang::SpirvToolsDisassemble(std::cout, spirv, getSpirvTargetVersion(_version));
  644. }
  645. spirv_cross::CompilerReflection refl(spirv);
  646. spirv_cross::ShaderResources resourcesrefl = refl.get_shader_resources();
  647. // Loop through the separate_images, and extract the uniform names:
  648. for (auto &resource : resourcesrefl.separate_images)
  649. {
  650. std::string name = refl.get_name(resource.id);
  651. if (name.size() > 7
  652. && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
  653. {
  654. name = name.substr(0, name.length() - 7);
  655. }
  656. uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  657. auto imageType = refl.get_type(resource.base_type_id).image;
  658. auto componentType = refl.get_type(imageType.type).basetype;
  659. bool isCompareSampler = false;
  660. for (auto& sampler : resourcesrefl.separate_samplers)
  661. {
  662. if (binding_index + 16 == refl.get_decoration(sampler.id, spv::Decoration::DecorationBinding) )
  663. {
  664. std::string samplerName = refl.get_name(sampler.id);
  665. isCompareSampler = refl.variable_is_depth_or_compare(sampler.id) || samplerName.find("Comparison") != std::string::npos;
  666. break;
  667. }
  668. }
  669. Uniform un;
  670. un.name = name;
  671. un.type = UniformType::Enum(UniformType::Sampler
  672. | kUniformSamplerBit
  673. | (isCompareSampler ? kUniformCompareBit : 0)
  674. );
  675. un.texComponent = textureComponentTypeToId(SpirvCrossBaseTypeToFormatType(componentType, imageType.depth) );
  676. un.texDimension = textureDimensionToId(SpirvDimToTextureViewDimension(imageType.dim, imageType.arrayed) );
  677. un.texFormat = uint16_t(s_textureFormats[imageType.format]);
  678. un.regIndex = uint16_t(binding_index);
  679. un.regCount = 0; // unused
  680. uniforms.push_back(un);
  681. }
  682. // Loop through the storage_images, and extract the uniform names:
  683. for (auto &resource : resourcesrefl.storage_images)
  684. {
  685. std::string name = refl.get_name(resource.id);
  686. uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  687. auto imageType = refl.get_type(resource.base_type_id).image;
  688. auto componentType = refl.get_type(imageType.type).basetype;
  689. spirv_cross::Bitset flags = refl.get_decoration_bitset(resource.id);
  690. UniformType::Enum type = flags.get(spv::DecorationNonWritable)
  691. ? UniformType::Enum(kUniformReadOnlyBit | UniformType::End)
  692. : UniformType::End;
  693. Uniform un;
  694. un.name = name;
  695. un.type = type;
  696. un.texComponent = textureComponentTypeToId(SpirvCrossBaseTypeToFormatType(componentType, imageType.depth) );
  697. un.texDimension = textureDimensionToId(SpirvDimToTextureViewDimension(imageType.dim, imageType.arrayed) );
  698. un.texFormat = uint16_t(s_textureFormats[imageType.format]);
  699. un.regIndex = uint16_t(binding_index);
  700. un.regCount = descriptorTypeToId(DescriptorType::StorageImage);
  701. uniforms.push_back(un);
  702. }
  703. bx::Error err;
  704. // Loop through the storage buffer, and extract the uniform names:
  705. for (auto& resource : resourcesrefl.storage_buffers)
  706. {
  707. std::string name = refl.get_name(resource.id);
  708. uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
  709. spirv_cross::Bitset flags = refl.get_buffer_block_flags(resource.id);
  710. UniformType::Enum type = flags.get(spv::DecorationNonWritable)
  711. ? UniformType::Enum(kUniformReadOnlyBit | UniformType::End)
  712. : UniformType::End;
  713. Uniform un;
  714. un.name = name;
  715. un.type = type;
  716. un.num = 0;
  717. un.regIndex = uint16_t(binding_index);
  718. un.regCount = descriptorTypeToId(DescriptorType::StorageBuffer);
  719. uniforms.push_back(un);
  720. }
  721. uint16_t size = writeUniformArray( _writer, uniforms, _options.shaderType == 'f');
  722. uint32_t shaderSize = (uint32_t)spirv.size() * sizeof(uint32_t);
  723. bx::write(_writer, shaderSize, &err);
  724. bx::write(_writer, spirv.data(), shaderSize, &err);
  725. uint8_t nul = 0;
  726. bx::write(_writer, nul, &err);
  727. const uint8_t numAttr = (uint8_t)program->getNumLiveAttributes();
  728. bx::write(_writer, numAttr, &err);
  729. for (uint8_t ii = 0; ii < numAttr; ++ii)
  730. {
  731. bgfx::Attrib::Enum attr = toAttribEnum(program->getAttributeName(ii) );
  732. if (bgfx::Attrib::Count != attr)
  733. {
  734. bx::write(_writer, bgfx::attribToId(attr), &err);
  735. }
  736. else
  737. {
  738. bx::write(_writer, uint16_t(UINT16_MAX), &err);
  739. }
  740. }
  741. bx::write(_writer, size, &err);
  742. }
  743. }
  744. }
  745. delete program;
  746. delete shader;
  747. glslang::FinalizeProcess();
  748. return compiled && linked && validated;
  749. }
  750. } // namespace spirv
  751. bool compileSPIRVShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
  752. {
  753. return spirv::compile(_options, _version, _code, _writer, true);
  754. }
  755. } // namespace bgfx