ShaderProgramDump.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright (C) 2009-2021, 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 <SprivCross/spirv_glsl.hpp>
  9. namespace anki {
  10. #define ANKI_TAB " "
  11. static void disassembleBlockInstance(const ShaderProgramBinaryBlockInstance& instance,
  12. const ShaderProgramBinaryBlock& block, StringListAuto& 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(), shaderVariableDataTypeToString(var.m_type).cstr(),
  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, StringListAuto& 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. shaderVariableDataTypeToString(var.m_type).cstr());
  34. }
  35. }
  36. void dumpShaderProgramBinary(const ShaderProgramBinary& binary, StringAuto& humanReadable)
  37. {
  38. GenericMemoryPoolAllocator<U8> alloc = humanReadable.getAllocator();
  39. StringListAuto lines(alloc);
  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 != MAX_U32)
  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, shaderVariableDataTypeToString(o.m_type).cstr());
  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. shaderVariableDataTypeToString(c.m_type).cstr(), 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::NONE)
  135. ? &binary.m_structs[member.m_structIndex].m_name[0]
  136. : shaderVariableDataTypeToString(member.m_type);
  137. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "%-32s type %24s\n", member.m_name.getBegin(), typeStr.cstr());
  138. }
  139. }
  140. }
  141. else
  142. {
  143. lines.pushBack(ANKI_TAB "N/A\n");
  144. }
  145. lines.pushBack("\n**BINARIES**\n");
  146. U32 count = 0;
  147. for(const ShaderProgramBinaryCodeBlock& code : binary.m_codeBlocks)
  148. {
  149. spirv_cross::CompilerGLSL::Options options;
  150. options.vulkan_semantics = true;
  151. options.version = 460;
  152. const unsigned int* spvb = reinterpret_cast<const unsigned int*>(code.m_binary.getBegin());
  153. ANKI_ASSERT((code.m_binary.getSize() % (sizeof(unsigned int))) == 0);
  154. std::vector<unsigned int> spv(spvb, spvb + code.m_binary.getSize() / sizeof(unsigned int));
  155. spirv_cross::CompilerGLSL compiler(spv);
  156. compiler.set_common_options(options);
  157. std::string glsl = compiler.compile();
  158. StringListAuto sourceLines(alloc);
  159. sourceLines.splitString(glsl.c_str(), '\n');
  160. StringAuto newGlsl(alloc);
  161. sourceLines.join("\n" ANKI_TAB ANKI_TAB, newGlsl);
  162. lines.pushBackSprintf(ANKI_TAB "#%u \n" ANKI_TAB ANKI_TAB "%s\n", count++, newGlsl.cstr());
  163. }
  164. lines.pushBack("\n**SHADER VARIANTS**\n");
  165. count = 0;
  166. for(const ShaderProgramBinaryVariant& variant : binary.m_variants)
  167. {
  168. lines.pushBackSprintf(ANKI_TAB "#%u\n", count++);
  169. // Uniform blocks
  170. if(variant.m_uniformBlocks.getSize() > 0)
  171. {
  172. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Uniform blocks\n");
  173. for(const ShaderProgramBinaryBlockInstance& instance : variant.m_uniformBlocks)
  174. {
  175. disassembleBlockInstance(instance, binary.m_uniformBlocks[instance.m_index], lines);
  176. }
  177. }
  178. // Storage blocks
  179. if(variant.m_storageBlocks.getSize() > 0)
  180. {
  181. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Storage blocks\n");
  182. for(const ShaderProgramBinaryBlockInstance& instance : variant.m_storageBlocks)
  183. {
  184. disassembleBlockInstance(instance, binary.m_storageBlocks[instance.m_index], lines);
  185. }
  186. }
  187. // Opaque
  188. if(variant.m_opaques.getSize() > 0)
  189. {
  190. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Opaque\n");
  191. for(const ShaderProgramBinaryOpaqueInstance& instance : variant.m_opaques)
  192. {
  193. const ShaderProgramBinaryOpaque& o = binary.m_opaques[instance.m_index];
  194. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB "%-32s set %4u binding %4u type %12s arraySize %4u\n",
  195. o.m_name.getBegin(), o.m_set, o.m_binding,
  196. shaderVariableDataTypeToString(o.m_type).cstr(), instance.m_arraySize);
  197. }
  198. }
  199. // Push constants
  200. if(variant.m_pushConstantBlock)
  201. {
  202. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Push constants\n");
  203. disassembleBlockInstance(*variant.m_pushConstantBlock, *binary.m_pushConstantBlock, lines);
  204. }
  205. // Constants
  206. if(variant.m_constants.getSize() > 0)
  207. {
  208. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Specialization constants\n");
  209. for(const ShaderProgramBinaryConstantInstance& instance : variant.m_constants)
  210. {
  211. const ShaderProgramBinaryConstant& c = binary.m_constants[instance.m_index];
  212. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB "%-32s type %8s id %4u\n", c.m_name.getBegin(),
  213. shaderVariableDataTypeToString(c.m_type).cstr(), c.m_constantId);
  214. }
  215. }
  216. // Structs
  217. if(variant.m_structs.getSize() > 0)
  218. {
  219. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Structs\n");
  220. for(const ShaderProgramBinaryStructInstance& instance : variant.m_structs)
  221. {
  222. const ShaderProgramBinaryStruct& s = binary.m_structs[instance.m_index];
  223. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB "%-32s size %4u\n", s.m_name.getBegin(),
  224. instance.m_size);
  225. for(const ShaderProgramBinaryStructMemberInstance& memberInstance : instance.m_memberInstances)
  226. {
  227. const ShaderProgramBinaryStructMember& member = s.m_members[memberInstance.m_index];
  228. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB ANKI_TAB "%-32s offset %4u arraySize %4u\n",
  229. member.m_name.getBegin(), memberInstance.m_offset,
  230. memberInstance.m_arraySize);
  231. }
  232. }
  233. }
  234. // Binary indices
  235. lines.pushBack(ANKI_TAB ANKI_TAB "Binaries ");
  236. for(ShaderType shaderType : EnumIterable<ShaderType>())
  237. {
  238. if(variant.m_codeBlockIndices[shaderType] < MAX_U32)
  239. {
  240. lines.pushBackSprintf("%u", variant.m_codeBlockIndices[shaderType]);
  241. }
  242. else
  243. {
  244. lines.pushBack("-");
  245. }
  246. if(shaderType != ShaderType::LAST)
  247. {
  248. lines.pushBack(",");
  249. }
  250. }
  251. lines.pushBack("\n");
  252. }
  253. // Mutations
  254. lines.pushBack("\n**MUTATIONS**\n");
  255. count = 0;
  256. for(const ShaderProgramBinaryMutation& mutation : binary.m_mutations)
  257. {
  258. lines.pushBackSprintf(ANKI_TAB "#%-4u variantIndex %5u values (", count++, mutation.m_variantIndex);
  259. if(mutation.m_values.getSize() > 0)
  260. {
  261. for(U32 i = 0; i < mutation.m_values.getSize(); ++i)
  262. {
  263. lines.pushBackSprintf((i < mutation.m_values.getSize() - 1) ? "%s %4d, " : "%s %4d",
  264. binary.m_mutators[i].m_name.getBegin(), I32(mutation.m_values[i]));
  265. }
  266. lines.pushBack(")");
  267. }
  268. else
  269. {
  270. lines.pushBack("N/A)");
  271. }
  272. lines.pushBack("\n");
  273. }
  274. lines.join("", humanReadable);
  275. }
  276. #undef ANKI_TAB
  277. } // end namespace anki