ShaderProgramDump.cpp 11 KB

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