ShaderProgramDump.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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**BINARIES**\n");
  128. U32 count = 0;
  129. for(const ShaderProgramBinaryCodeBlock& code : binary.m_codeBlocks)
  130. {
  131. spirv_cross::CompilerGLSL::Options options;
  132. options.vulkan_semantics = true;
  133. options.version = 460;
  134. const unsigned int* spvb = reinterpret_cast<const unsigned int*>(code.m_binary.getBegin());
  135. ANKI_ASSERT((code.m_binary.getSize() % (sizeof(unsigned int))) == 0);
  136. std::vector<unsigned int> spv(spvb, spvb + code.m_binary.getSize() / sizeof(unsigned int));
  137. spirv_cross::CompilerGLSL compiler(spv);
  138. compiler.set_common_options(options);
  139. std::string glsl = compiler.compile();
  140. StringListAuto sourceLines(alloc);
  141. sourceLines.splitString(glsl.c_str(), '\n');
  142. StringAuto newGlsl(alloc);
  143. sourceLines.join("\n" ANKI_TAB ANKI_TAB, newGlsl);
  144. lines.pushBackSprintf(ANKI_TAB "#%u \n" ANKI_TAB ANKI_TAB "%s\n", count++, newGlsl.cstr());
  145. }
  146. lines.pushBack("\n**SHADER VARIANTS**\n");
  147. count = 0;
  148. for(const ShaderProgramBinaryVariant& variant : binary.m_variants)
  149. {
  150. lines.pushBackSprintf(ANKI_TAB "#%u\n", count++);
  151. // Uniform blocks
  152. if(variant.m_uniformBlocks.getSize() > 0)
  153. {
  154. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Uniform blocks\n");
  155. for(const ShaderProgramBinaryBlockInstance& instance : variant.m_uniformBlocks)
  156. {
  157. disassembleBlockInstance(instance, binary.m_uniformBlocks[instance.m_index], lines);
  158. }
  159. }
  160. // Storage blocks
  161. if(variant.m_storageBlocks.getSize() > 0)
  162. {
  163. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Storage blocks\n");
  164. for(const ShaderProgramBinaryBlockInstance& instance : variant.m_storageBlocks)
  165. {
  166. disassembleBlockInstance(instance, binary.m_storageBlocks[instance.m_index], lines);
  167. }
  168. }
  169. // Opaque
  170. if(variant.m_opaques.getSize() > 0)
  171. {
  172. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Opaque\n");
  173. for(const ShaderProgramBinaryOpaqueInstance& instance : variant.m_opaques)
  174. {
  175. const ShaderProgramBinaryOpaque& o = binary.m_opaques[instance.m_index];
  176. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB "%-32s set %4u binding %4u type %12s arraySize %4u\n",
  177. o.m_name.getBegin(), o.m_set, o.m_binding,
  178. shaderVariableDataTypeToString(o.m_type).cstr(), instance.m_arraySize);
  179. }
  180. }
  181. // Push constants
  182. if(variant.m_pushConstantBlock)
  183. {
  184. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Push constants\n");
  185. disassembleBlockInstance(*variant.m_pushConstantBlock, *binary.m_pushConstantBlock, lines);
  186. }
  187. // Constants
  188. if(variant.m_constants.getSize() > 0)
  189. {
  190. lines.pushBackSprintf(ANKI_TAB ANKI_TAB "Specialization constants\n");
  191. for(const ShaderProgramBinaryConstantInstance& instance : variant.m_constants)
  192. {
  193. const ShaderProgramBinaryConstant& c = binary.m_constants[instance.m_index];
  194. lines.pushBackSprintf(ANKI_TAB ANKI_TAB ANKI_TAB "%-32s type %8s id %4u\n", c.m_name.getBegin(),
  195. shaderVariableDataTypeToString(c.m_type).cstr(), c.m_constantId);
  196. }
  197. }
  198. // Binary indices
  199. lines.pushBack(ANKI_TAB ANKI_TAB "Binaries ");
  200. for(ShaderType shaderType : EnumIterable<ShaderType>())
  201. {
  202. if(variant.m_codeBlockIndices[shaderType] < MAX_U32)
  203. {
  204. lines.pushBackSprintf("%u", variant.m_codeBlockIndices[shaderType]);
  205. }
  206. else
  207. {
  208. lines.pushBack("-");
  209. }
  210. if(shaderType != ShaderType::LAST)
  211. {
  212. lines.pushBack(",");
  213. }
  214. }
  215. lines.pushBack("\n");
  216. }
  217. // Mutations
  218. lines.pushBack("\n**MUTATIONS**\n");
  219. count = 0;
  220. for(const ShaderProgramBinaryMutation& mutation : binary.m_mutations)
  221. {
  222. lines.pushBackSprintf(ANKI_TAB "#%-4u variantIndex %5u values (", count++, mutation.m_variantIndex);
  223. if(mutation.m_values.getSize() > 0)
  224. {
  225. for(U32 i = 0; i < mutation.m_values.getSize(); ++i)
  226. {
  227. lines.pushBackSprintf((i < mutation.m_values.getSize() - 1) ? "%s %4d, " : "%s %4d",
  228. binary.m_mutators[i].m_name.getBegin(), I32(mutation.m_values[i]));
  229. }
  230. lines.pushBack(")");
  231. }
  232. else
  233. {
  234. lines.pushBack("N/A)");
  235. }
  236. lines.pushBack("\n");
  237. }
  238. lines.join("", humanReadable);
  239. }
  240. #undef ANKI_TAB
  241. } // end namespace anki