ShaderProgramDump.cpp 9.7 KB

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