ShaderProgramDump.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. {
  11. #define ANKI_TAB " "
  12. static void disassembleBlockInstance(const ShaderProgramBinaryBlockInstance& instance,
  13. const ShaderProgramBinaryBlock& block, StringListAuto& 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_variables.getSize(); ++i)
  18. {
  19. const ShaderProgramBinaryVariableInstance& varInstance = instance.m_variables[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(), shaderVariableDataTypeToString(var.m_type).cstr(),
  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, StringListAuto& 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. shaderVariableDataTypeToString(var.m_type).cstr());
  35. }
  36. }
  37. void dumpShaderProgramBinary(const ShaderProgramBinary& binary, StringAuto& humanReadable)
  38. {
  39. GenericMemoryPoolAllocator<U8> alloc = humanReadable.getAllocator();
  40. StringListAuto lines(alloc);
  41. if(binary.m_libraryName[0])
  42. {
  43. lines.pushBack("**LIBRARY**\n");
  44. lines.pushBackSprintf(ANKI_TAB "%s\n", &binary.m_libraryName[0]);
  45. }
  46. if(binary.m_rayType != MAX_U32)
  47. {
  48. lines.pushBack("\n**RAY TYPE**\n");
  49. lines.pushBackSprintf(ANKI_TAB "%u\n", binary.m_rayType);
  50. }
  51. lines.pushBack("\n**MUTATORS**\n");
  52. if(binary.m_mutators.getSize() > 0)
  53. {
  54. for(const ShaderProgramBinaryMutator& mutator : binary.m_mutators)
  55. {
  56. lines.pushBackSprintf(ANKI_TAB "%-32s ", &mutator.m_name[0]);
  57. for(U32 i = 0; i < mutator.m_values.getSize(); ++i)
  58. {
  59. lines.pushBackSprintf((i < mutator.m_values.getSize() - 1) ? "%d," : "%d", mutator.m_values[i]);
  60. }
  61. lines.pushBack("\n");
  62. }
  63. }
  64. else
  65. {
  66. lines.pushBack(ANKI_TAB "N/A\n");
  67. }
  68. lines.pushBack("\n**UNIFORM BLOCKS**\n");
  69. if(binary.m_uniformBlocks.getSize() > 0)
  70. {
  71. for(const ShaderProgramBinaryBlock& block : binary.m_uniformBlocks)
  72. {
  73. disassembleBlock(block, lines);
  74. }
  75. }
  76. else
  77. {
  78. lines.pushBack(ANKI_TAB "N/A\n");
  79. }
  80. lines.pushBack("\n**STORAGE BLOCKS**\n");
  81. if(binary.m_storageBlocks.getSize() > 0)
  82. {
  83. for(const ShaderProgramBinaryBlock& block : binary.m_storageBlocks)
  84. {
  85. disassembleBlock(block, lines);
  86. }
  87. }
  88. else
  89. {
  90. lines.pushBack(ANKI_TAB "N/A\n");
  91. }
  92. lines.pushBack("\n**PUSH CONSTANTS**\n");
  93. if(binary.m_pushConstantBlock)
  94. {
  95. disassembleBlock(*binary.m_pushConstantBlock, lines);
  96. }
  97. else
  98. {
  99. lines.pushBack(ANKI_TAB "N/A\n");
  100. }
  101. lines.pushBack("\n**OPAQUE**\n");
  102. if(binary.m_opaques.getSize() > 0)
  103. {
  104. for(const ShaderProgramBinaryOpaque& o : binary.m_opaques)
  105. {
  106. lines.pushBackSprintf(ANKI_TAB "%-32s set %4u binding %4u type %12s\n", o.m_name.getBegin(), o.m_set,
  107. o.m_binding, shaderVariableDataTypeToString(o.m_type).cstr());
  108. }
  109. }
  110. else
  111. {
  112. lines.pushBack(ANKI_TAB "N/A\n");
  113. }
  114. lines.pushBack("\n**CONSTANTS**\n");
  115. if(binary.m_constants.getSize() > 0)
  116. {
  117. for(const ShaderProgramBinaryConstant& c : binary.m_constants)
  118. {
  119. lines.pushBackSprintf(ANKI_TAB "%-32s type %8s id %4u\n", c.m_name.getBegin(),
  120. shaderVariableDataTypeToString(c.m_type).cstr(), c.m_constantId);
  121. }
  122. }
  123. else
  124. {
  125. lines.pushBack(ANKI_TAB "N/A\n");
  126. }
  127. lines.pushBack("\n**STRUCTS**\n");
  128. if(binary.m_structs.getSize() > 0)
  129. {
  130. for(const ShaderProgramBinaryStruct& s : binary.m_structs)
  131. {
  132. lines.pushBackSprintf(ANKI_TAB "%-32s size %4u\n", s.m_name.getBegin(), s.m_size);
  133. for(const ShaderProgramBinaryStructMember& member : s.m_members)
  134. {
  135. CString typeStr = (member.m_type == ShaderVariableDataType::NONE)
  136. ? &binary.m_structs[member.m_structIndex].m_name[0]
  137. : shaderVariableDataTypeToString(member.m_type);
  138. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "%-32s type %24s offset %4u arraySize %4u\n",
  139. member.m_name.getBegin(), typeStr.cstr(), member.m_offset, member.m_arraySize);
  140. }
  141. }
  142. }
  143. else
  144. {
  145. lines.pushBack(ANKI_TAB "N/A\n");
  146. }
  147. lines.pushBack("\n**BINARIES**\n");
  148. U32 count = 0;
  149. for(const ShaderProgramBinaryCodeBlock& code : binary.m_codeBlocks)
  150. {
  151. spirv_cross::CompilerGLSL::Options options;
  152. options.vulkan_semantics = true;
  153. options.version = 460;
  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. StringListAuto sourceLines(alloc);
  161. sourceLines.splitString(glsl.c_str(), '\n');
  162. StringAuto newGlsl(alloc);
  163. sourceLines.join("\n" ANKI_TAB ANKI_TAB, newGlsl);
  164. lines.pushBackSprintf(ANKI_TAB "#%u \n" ANKI_TAB ANKI_TAB "%s\n", count++, newGlsl.cstr());
  165. }
  166. lines.pushBack("\n**SHADER VARIANTS**\n");
  167. count = 0;
  168. for(const ShaderProgramBinaryVariant& variant : binary.m_variants)
  169. {
  170. lines.pushBackSprintf(ANKI_TAB "#%u\n", count++);
  171. // Uniform blocks
  172. if(variant.m_uniformBlocks.getSize() > 0)
  173. {
  174. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Uniform blocks\n");
  175. for(const ShaderProgramBinaryBlockInstance& instance : variant.m_uniformBlocks)
  176. {
  177. disassembleBlockInstance(instance, binary.m_uniformBlocks[instance.m_index], lines);
  178. }
  179. }
  180. // Storage blocks
  181. if(variant.m_storageBlocks.getSize() > 0)
  182. {
  183. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Storage blocks\n");
  184. for(const ShaderProgramBinaryBlockInstance& instance : variant.m_storageBlocks)
  185. {
  186. disassembleBlockInstance(instance, binary.m_storageBlocks[instance.m_index], lines);
  187. }
  188. }
  189. // Opaque
  190. if(variant.m_opaques.getSize() > 0)
  191. {
  192. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Opaque\n");
  193. for(const ShaderProgramBinaryOpaqueInstance& instance : variant.m_opaques)
  194. {
  195. const ShaderProgramBinaryOpaque& o = binary.m_opaques[instance.m_index];
  196. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB "%-32s set %4u binding %4u type %12s arraySize %4u\n",
  197. o.m_name.getBegin(), o.m_set, o.m_binding,
  198. shaderVariableDataTypeToString(o.m_type).cstr(), instance.m_arraySize);
  199. }
  200. }
  201. // Push constants
  202. if(variant.m_pushConstantBlock)
  203. {
  204. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Push constants\n");
  205. disassembleBlockInstance(*variant.m_pushConstantBlock, *binary.m_pushConstantBlock, lines);
  206. }
  207. // Constants
  208. if(variant.m_constants.getSize() > 0)
  209. {
  210. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Specialization constants\n");
  211. for(const ShaderProgramBinaryConstantInstance& instance : variant.m_constants)
  212. {
  213. const ShaderProgramBinaryConstant& c = binary.m_constants[instance.m_index];
  214. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB "%-32s type %8s id %4u\n", c.m_name.getBegin(),
  215. shaderVariableDataTypeToString(c.m_type).cstr(), c.m_constantId);
  216. }
  217. }
  218. // Binary indices
  219. lines.pushBack(ANKI_TAB ANKI_TAB "Binaries ");
  220. for(ShaderType shaderType : EnumIterable<ShaderType>())
  221. {
  222. if(variant.m_codeBlockIndices[shaderType] < MAX_U32)
  223. {
  224. lines.pushBackSprintf("%u", variant.m_codeBlockIndices[shaderType]);
  225. }
  226. else
  227. {
  228. lines.pushBack("-");
  229. }
  230. if(shaderType != ShaderType::LAST)
  231. {
  232. lines.pushBack(",");
  233. }
  234. }
  235. lines.pushBack("\n");
  236. }
  237. // Mutations
  238. lines.pushBack("\n**MUTATIONS**\n");
  239. count = 0;
  240. for(const ShaderProgramBinaryMutation& mutation : binary.m_mutations)
  241. {
  242. lines.pushBackSprintf(ANKI_TAB "#%-4u variantIndex %5u values (", count++, mutation.m_variantIndex);
  243. if(mutation.m_values.getSize() > 0)
  244. {
  245. for(U32 i = 0; i < mutation.m_values.getSize(); ++i)
  246. {
  247. lines.pushBackSprintf((i < mutation.m_values.getSize() - 1) ? "%s %4d, " : "%s %4d",
  248. binary.m_mutators[i].m_name.getBegin(), I32(mutation.m_values[i]));
  249. }
  250. lines.pushBack(")");
  251. }
  252. else
  253. {
  254. lines.pushBack("N/A)");
  255. }
  256. lines.pushBack("\n");
  257. }
  258. lines.join("", humanReadable);
  259. }
  260. #undef ANKI_TAB
  261. } // end namespace anki