ShaderProgramDump.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/ShaderCompiler/ShaderProgramDump.h>
  6. #include <AnKi/Util/Serializer.h>
  7. #include <AnKi/Util/StringList.h>
  8. #include <SpirvCross/spirv_glsl.hpp>
  9. #include <ThirdParty/SpirvTools/include/spirv-tools/libspirv.h>
  10. namespace anki {
  11. #define ANKI_TAB " "
  12. static void disassembleBlockInstance(const ShaderProgramBinaryBlockInstance& instance,
  13. const ShaderProgramBinaryBlock& block, StringListRaii& lines)
  14. {
  15. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB "%-32s set %4u binding %4u size %4u\n", block.m_name.getBegin(),
  16. block.m_set, block.m_binding, instance.m_size);
  17. for(U32 i = 0; i < instance.m_variableInstances.getSize(); ++i)
  18. {
  19. const ShaderProgramBinaryVariableInstance& varInstance = instance.m_variableInstances[i];
  20. const ShaderProgramBinaryVariable& var = block.m_variables[varInstance.m_index];
  21. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB ANKI_TAB "%-48s type %8s blockInfo %d,%d,%d,%d\n",
  22. var.m_name.getBegin(), getShaderVariableDataTypeInfo(var.m_type).m_name,
  23. varInstance.m_blockInfo.m_offset, varInstance.m_blockInfo.m_arraySize,
  24. varInstance.m_blockInfo.m_arrayStride, varInstance.m_blockInfo.m_matrixStride);
  25. }
  26. }
  27. static void disassembleBlock(const ShaderProgramBinaryBlock& block, StringListRaii& lines)
  28. {
  29. lines.pushBackSprintf(ANKI_TAB "%-32s set %4u binding %4u\n", block.m_name.getBegin(), block.m_set,
  30. block.m_binding);
  31. for(const ShaderProgramBinaryVariable& var : block.m_variables)
  32. {
  33. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "%-48s type %8s\n", var.m_name.getBegin(),
  34. getShaderVariableDataTypeInfo(var.m_type).m_name);
  35. }
  36. }
  37. void dumpShaderProgramBinary(const ShaderDumpOptions& options, const ShaderProgramBinary& binary,
  38. StringRaii& humanReadable)
  39. {
  40. BaseMemoryPool& pool = humanReadable.getMemoryPool();
  41. StringListRaii lines(&pool);
  42. if(binary.m_libraryName[0])
  43. {
  44. lines.pushBack("**LIBRARY**\n");
  45. lines.pushBackSprintf(ANKI_TAB "%s\n", &binary.m_libraryName[0]);
  46. }
  47. if(binary.m_rayType != kMaxU32)
  48. {
  49. lines.pushBack("\n**RAY TYPE**\n");
  50. lines.pushBackSprintf(ANKI_TAB "%u\n", binary.m_rayType);
  51. }
  52. lines.pushBack("\n**MUTATORS**\n");
  53. if(binary.m_mutators.getSize() > 0)
  54. {
  55. for(const ShaderProgramBinaryMutator& mutator : binary.m_mutators)
  56. {
  57. lines.pushBackSprintf(ANKI_TAB "%-32s ", &mutator.m_name[0]);
  58. for(U32 i = 0; i < mutator.m_values.getSize(); ++i)
  59. {
  60. lines.pushBackSprintf((i < mutator.m_values.getSize() - 1) ? "%d," : "%d", mutator.m_values[i]);
  61. }
  62. lines.pushBack("\n");
  63. }
  64. }
  65. else
  66. {
  67. lines.pushBack(ANKI_TAB "N/A\n");
  68. }
  69. lines.pushBack("\n**UNIFORM BLOCKS**\n");
  70. if(binary.m_uniformBlocks.getSize() > 0)
  71. {
  72. for(const ShaderProgramBinaryBlock& block : binary.m_uniformBlocks)
  73. {
  74. disassembleBlock(block, lines);
  75. }
  76. }
  77. else
  78. {
  79. lines.pushBack(ANKI_TAB "N/A\n");
  80. }
  81. lines.pushBack("\n**STORAGE BLOCKS**\n");
  82. if(binary.m_storageBlocks.getSize() > 0)
  83. {
  84. for(const ShaderProgramBinaryBlock& block : binary.m_storageBlocks)
  85. {
  86. disassembleBlock(block, lines);
  87. }
  88. }
  89. else
  90. {
  91. lines.pushBack(ANKI_TAB "N/A\n");
  92. }
  93. lines.pushBack("\n**PUSH CONSTANTS**\n");
  94. if(binary.m_pushConstantBlock)
  95. {
  96. disassembleBlock(*binary.m_pushConstantBlock, lines);
  97. }
  98. else
  99. {
  100. lines.pushBack(ANKI_TAB "N/A\n");
  101. }
  102. lines.pushBack("\n**OPAQUE**\n");
  103. if(binary.m_opaques.getSize() > 0)
  104. {
  105. for(const ShaderProgramBinaryOpaque& o : binary.m_opaques)
  106. {
  107. lines.pushBackSprintf(ANKI_TAB "%-32s set %4u binding %4u type %12s\n", o.m_name.getBegin(), o.m_set,
  108. o.m_binding, getShaderVariableDataTypeInfo(o.m_type).m_name);
  109. }
  110. }
  111. else
  112. {
  113. lines.pushBack(ANKI_TAB "N/A\n");
  114. }
  115. lines.pushBack("\n**CONSTANTS**\n");
  116. if(binary.m_constants.getSize() > 0)
  117. {
  118. for(const ShaderProgramBinaryConstant& c : binary.m_constants)
  119. {
  120. lines.pushBackSprintf(ANKI_TAB "%-32s type %8s id %4u\n", c.m_name.getBegin(),
  121. getShaderVariableDataTypeInfo(c.m_type).m_name, c.m_constantId);
  122. }
  123. }
  124. else
  125. {
  126. lines.pushBack(ANKI_TAB "N/A\n");
  127. }
  128. lines.pushBack("\n**STRUCTS**\n");
  129. if(binary.m_structs.getSize() > 0)
  130. {
  131. for(const ShaderProgramBinaryStruct& s : binary.m_structs)
  132. {
  133. lines.pushBackSprintf(ANKI_TAB "%-32s\n", s.m_name.getBegin());
  134. for(const ShaderProgramBinaryStructMember& member : s.m_members)
  135. {
  136. const CString typeStr = (member.m_type == ShaderVariableDataType::kNone)
  137. ? &binary.m_structs[member.m_structIndex].m_name[0]
  138. : getShaderVariableDataTypeInfo(member.m_type).m_name;
  139. const CString dependentMutator = (member.m_dependentMutator != kMaxU32)
  140. ? binary.m_mutators[member.m_dependentMutator].m_name.getBegin()
  141. : "None";
  142. lines.pushBackSprintf(
  143. ANKI_TAB ANKI_TAB "%-32s type %24s dependentMutator %-32s dependentMutatorValue %4d\n",
  144. member.m_name.getBegin(), typeStr.cstr(), dependentMutator.cstr(), member.m_dependentMutatorValue);
  145. }
  146. }
  147. }
  148. else
  149. {
  150. lines.pushBack(ANKI_TAB "N/A\n");
  151. }
  152. lines.pushBack("\n**BINARIES**\n");
  153. U32 count = 0;
  154. for(const ShaderProgramBinaryCodeBlock& code : binary.m_codeBlocks)
  155. {
  156. lines.pushBackSprintf(ANKI_TAB "#bin%05u \n", count++);
  157. if(options.m_writeGlsl)
  158. {
  159. spirv_cross::CompilerGLSL::Options options;
  160. options.vulkan_semantics = true;
  161. options.version = 460;
  162. const unsigned int* spvb = reinterpret_cast<const unsigned int*>(code.m_binary.getBegin());
  163. ANKI_ASSERT((code.m_binary.getSize() % (sizeof(unsigned int))) == 0);
  164. std::vector<unsigned int> spv(spvb, spvb + code.m_binary.getSize() / sizeof(unsigned int));
  165. spirv_cross::CompilerGLSL compiler(spv);
  166. compiler.set_common_options(options);
  167. std::string glsl = compiler.compile();
  168. StringListRaii sourceLines(&pool);
  169. sourceLines.splitString(glsl.c_str(), '\n');
  170. StringRaii newGlsl(&pool);
  171. sourceLines.join("\n" ANKI_TAB ANKI_TAB, newGlsl);
  172. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "%s\n", newGlsl.cstr());
  173. }
  174. if(options.m_writeSpirv)
  175. {
  176. spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_5);
  177. const U32 disOptions = SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES | SPV_BINARY_TO_TEXT_OPTION_NO_HEADER;
  178. spv_text text = nullptr;
  179. const spv_result_t error = spvBinaryToText(context, reinterpret_cast<const U32*>(code.m_binary.getBegin()),
  180. code.m_binary.getSizeInBytes() / 4, disOptions, &text, nullptr);
  181. spvContextDestroy(context);
  182. if(!error)
  183. {
  184. StringListRaii spvlines(&pool);
  185. spvlines.splitString(text->str, '\n');
  186. StringRaii final(&pool);
  187. spvlines.join("\n" ANKI_TAB ANKI_TAB, final);
  188. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "%s\n", final.cstr());
  189. }
  190. else
  191. {
  192. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "*error in spiv-dis*\n");
  193. }
  194. spvTextDestroy(text);
  195. }
  196. }
  197. lines.pushBack("\n**SHADER VARIANTS**\n");
  198. count = 0;
  199. for(const ShaderProgramBinaryVariant& variant : binary.m_variants)
  200. {
  201. lines.pushBackSprintf(ANKI_TAB "#var%05u\n", count++);
  202. // Uniform blocks
  203. if(variant.m_uniformBlocks.getSize() > 0)
  204. {
  205. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Uniform blocks\n");
  206. for(const ShaderProgramBinaryBlockInstance& instance : variant.m_uniformBlocks)
  207. {
  208. disassembleBlockInstance(instance, binary.m_uniformBlocks[instance.m_index], lines);
  209. }
  210. }
  211. // Storage blocks
  212. if(variant.m_storageBlocks.getSize() > 0)
  213. {
  214. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Storage blocks\n");
  215. for(const ShaderProgramBinaryBlockInstance& instance : variant.m_storageBlocks)
  216. {
  217. disassembleBlockInstance(instance, binary.m_storageBlocks[instance.m_index], lines);
  218. }
  219. }
  220. // Opaque
  221. if(variant.m_opaques.getSize() > 0)
  222. {
  223. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Opaque\n");
  224. for(const ShaderProgramBinaryOpaqueInstance& instance : variant.m_opaques)
  225. {
  226. const ShaderProgramBinaryOpaque& o = binary.m_opaques[instance.m_index];
  227. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB "%-32s set %4u binding %4u type %12s arraySize %4u\n",
  228. o.m_name.getBegin(), o.m_set, o.m_binding,
  229. getShaderVariableDataTypeInfo(o.m_type).m_name, instance.m_arraySize);
  230. }
  231. }
  232. // Push constants
  233. if(variant.m_pushConstantBlock)
  234. {
  235. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Push constants\n");
  236. disassembleBlockInstance(*variant.m_pushConstantBlock, *binary.m_pushConstantBlock, lines);
  237. }
  238. // Constants
  239. if(variant.m_constants.getSize() > 0)
  240. {
  241. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Specialization constants\n");
  242. for(const ShaderProgramBinaryConstantInstance& instance : variant.m_constants)
  243. {
  244. const ShaderProgramBinaryConstant& c = binary.m_constants[instance.m_index];
  245. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB "%-32s type %8s id %4u\n", c.m_name.getBegin(),
  246. getShaderVariableDataTypeInfo(c.m_type).m_name, c.m_constantId);
  247. }
  248. }
  249. // Structs
  250. if(variant.m_structs.getSize() > 0)
  251. {
  252. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Structs\n");
  253. for(const ShaderProgramBinaryStructInstance& instance : variant.m_structs)
  254. {
  255. const ShaderProgramBinaryStruct& s = binary.m_structs[instance.m_index];
  256. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB "%-32s size %4u\n", s.m_name.getBegin(),
  257. instance.m_size);
  258. for(const ShaderProgramBinaryStructMemberInstance& memberInstance : instance.m_memberInstances)
  259. {
  260. const ShaderProgramBinaryStructMember& member = s.m_members[memberInstance.m_index];
  261. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB ANKI_TAB "%-32s offset %4u arraySize %4u\n",
  262. member.m_name.getBegin(), memberInstance.m_offset,
  263. memberInstance.m_arraySize);
  264. }
  265. }
  266. }
  267. // Binary indices
  268. lines.pushBack(ANKI_TAB ANKI_TAB "Binaries ");
  269. for(ShaderType shaderType : EnumIterable<ShaderType>())
  270. {
  271. if(variant.m_codeBlockIndices[shaderType] < kMaxU32)
  272. {
  273. lines.pushBackSprintf("#bin%05u", variant.m_codeBlockIndices[shaderType]);
  274. }
  275. else
  276. {
  277. lines.pushBack("-");
  278. }
  279. if(shaderType != ShaderType::kLast)
  280. {
  281. lines.pushBack(",");
  282. }
  283. }
  284. lines.pushBack("\n");
  285. }
  286. // Mutations
  287. lines.pushBack("\n**MUTATIONS**\n");
  288. count = 0;
  289. for(const ShaderProgramBinaryMutation& mutation : binary.m_mutations)
  290. {
  291. lines.pushBackSprintf(ANKI_TAB "#mut%-4u variantIndex #var%05u hash 0x%016" PRIX64 " values (", count++,
  292. mutation.m_variantIndex, mutation.m_hash);
  293. if(mutation.m_values.getSize() > 0)
  294. {
  295. for(U32 i = 0; i < mutation.m_values.getSize(); ++i)
  296. {
  297. lines.pushBackSprintf((i < mutation.m_values.getSize() - 1) ? "%s %4d, " : "%s %4d",
  298. binary.m_mutators[i].m_name.getBegin(), I32(mutation.m_values[i]));
  299. }
  300. lines.pushBack(")");
  301. }
  302. else
  303. {
  304. lines.pushBack("N/A)");
  305. }
  306. lines.pushBack("\n");
  307. }
  308. lines.join("", humanReadable);
  309. }
  310. #undef ANKI_TAB
  311. } // end namespace anki