D3DReflectionDumper.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // D3DReflectionDumper.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Use this to dump D3D Reflection data for testing. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include "dxc/Support/Global.h"
  13. #include <algorithm>
  14. #include <string>
  15. #include <ostream>
  16. #include <iomanip>
  17. #include "dxc/Support/WinIncludes.h"
  18. #include "dxc/dxcapi.h"
  19. #include <d3d12shader.h>
  20. #include "dxc/DxilContainer/DxilContainer.h"
  21. namespace refl_dump {
  22. LPCSTR ToString(D3D_CBUFFER_TYPE CBType);
  23. LPCSTR ToString(D3D_SHADER_INPUT_TYPE Type);
  24. LPCSTR ToString(D3D_RESOURCE_RETURN_TYPE ReturnType);
  25. LPCSTR ToString(D3D_SRV_DIMENSION Dimension);
  26. LPCSTR ToString(D3D_PRIMITIVE_TOPOLOGY GSOutputTopology);
  27. LPCSTR ToString(D3D_PRIMITIVE InputPrimitive);
  28. LPCSTR ToString(D3D_TESSELLATOR_OUTPUT_PRIMITIVE HSOutputPrimitive);
  29. LPCSTR ToString(D3D_TESSELLATOR_PARTITIONING HSPartitioning);
  30. LPCSTR ToString(D3D_TESSELLATOR_DOMAIN TessellatorDomain);
  31. LPCSTR ToString(D3D_SHADER_VARIABLE_CLASS Class);
  32. LPCSTR ToString(D3D_SHADER_VARIABLE_TYPE Type);
  33. LPCSTR ToString(D3D_SHADER_VARIABLE_FLAGS Flag);
  34. LPCSTR ToString(D3D_SHADER_INPUT_FLAGS Flag);
  35. LPCSTR ToString(D3D_SHADER_CBUFFER_FLAGS Flag);
  36. LPCSTR ToString(D3D_PARAMETER_FLAGS Flag);
  37. template<typename _T>
  38. struct EnumValue {
  39. public:
  40. EnumValue(const _T &e) : eValue(e) {}
  41. _T eValue;
  42. };
  43. template<typename _T, typename _StoreT = uint32_t>
  44. struct FlagsValue {
  45. public:
  46. FlagsValue(const _StoreT &f) : Flags(f) {}
  47. _StoreT Flags;
  48. };
  49. template<typename _T>
  50. std::ostream& operator<<(std::ostream& out, const EnumValue<_T> &obj) {
  51. if (LPCSTR szValue = ToString(obj.eValue))
  52. return out << szValue;
  53. else
  54. return out << "<unknown: " << std::hex << std::showbase << (UINT)obj.eValue << ">";
  55. }
  56. template<typename _T, typename _StoreT>
  57. std::ostream& operator<<(std::ostream& out, const FlagsValue<_T, _StoreT> &obj) {
  58. _StoreT Flags = obj.Flags;
  59. if (!Flags) {
  60. LPCSTR szValue = ToString((_T)0);
  61. if (szValue)
  62. return out << "0 (" << szValue << ")";
  63. else
  64. return out << "0";
  65. }
  66. uint32_t flag = 0;
  67. out << "(";
  68. while (Flags) {
  69. if (flag)
  70. out << " | ";
  71. flag = (Flags & ~(Flags - 1));
  72. Flags ^= flag;
  73. out << EnumValue<_T>((_T)flag);
  74. }
  75. out << ")";
  76. return out;
  77. }
  78. class DumperBase {
  79. private:
  80. std::ostream &m_out;
  81. unsigned m_indent = 0;
  82. bool m_bCheckByName = false;
  83. std::ostream &DoIndent() {
  84. return m_out << std::setfill(' ')
  85. << std::setw(std::min(m_indent * 2, (unsigned)32))
  86. << "";
  87. }
  88. public:
  89. DumperBase(std::ostream &outStream) : m_out(outStream) {}
  90. void Indent() { if (m_indent < (1 << 30)) m_indent++; }
  91. void Dedent() { if (m_indent > 0) m_indent--; }
  92. template<typename _T>
  93. std::ostream &Write(std::ostream &out, _T t) {
  94. return out << t;
  95. }
  96. template<typename _T, typename... Args>
  97. std::ostream &Write(std::ostream &out, _T t, Args... args) {
  98. return Write(out << t, args...);
  99. }
  100. template<typename _T>
  101. std::ostream &WriteLn(_T t) {
  102. return Write(DoIndent(), t) << std::endl
  103. << std::resetiosflags(std::ios_base::basefield | std::ios_base::showbase);
  104. }
  105. template<typename _T, typename... Args>
  106. std::ostream &WriteLn(_T t, Args... args) {
  107. return Write(Write(DoIndent(), t), args...) << std::endl
  108. << std::resetiosflags(std::ios_base::basefield | std::ios_base::showbase);
  109. }
  110. template <typename _T>
  111. std::ostream &WriteEnumValue(_T eValue) {
  112. LPCSTR szValue = ToString(eValue);
  113. if (szValue)
  114. return Write(szValue);
  115. else
  116. return Write("<unknown: ", std::hex, std::showbase, (UINT)eValue, ">");
  117. }
  118. template<typename _T>
  119. void DumpEnum(const char *Name, _T eValue) {
  120. WriteLn(Name, ": ", EnumValue<_T>(eValue));
  121. }
  122. template<typename _T, typename _StoreT = uint32_t>
  123. void DumpFlags(const char *Name, _StoreT Flags) {
  124. WriteLn(Name, ": ", FlagsValue<_T, _StoreT>(Flags));
  125. }
  126. template<typename... Args>
  127. void Failure(Args... args) {
  128. WriteLn("Failed: ", args...);
  129. }
  130. };
  131. class D3DReflectionDumper : public DumperBase {
  132. private:
  133. bool m_bCheckByName = false;
  134. const char *m_LastName = nullptr;
  135. void SetLastName(const char *Name = nullptr) { m_LastName = Name ? Name : "<nullptr>"; }
  136. public:
  137. D3DReflectionDumper(std::ostream &outStream) : DumperBase(outStream) {}
  138. void SetCheckByName(bool bCheckByName) { m_bCheckByName = bCheckByName; }
  139. void DumpShaderVersion(UINT Version);
  140. void DumpDefaultValue(LPCVOID pDefaultValue, UINT Size);
  141. void Dump(D3D12_SHADER_TYPE_DESC &tyDesc);
  142. void Dump(D3D12_SHADER_VARIABLE_DESC &varDesc);
  143. void Dump(D3D12_SHADER_BUFFER_DESC &Desc);
  144. void Dump(D3D12_SHADER_INPUT_BIND_DESC &resDesc);
  145. void Dump(D3D12_SHADER_DESC &Desc);
  146. void Dump(D3D12_FUNCTION_DESC &Desc);
  147. void Dump(D3D12_LIBRARY_DESC &Desc);
  148. void Dump(ID3D12ShaderReflectionType *pType);
  149. void Dump(ID3D12ShaderReflectionVariable *pVar);
  150. void Dump(ID3D12ShaderReflectionConstantBuffer *pCBReflection);
  151. void Dump(ID3D12ShaderReflection *pShaderReflection);
  152. void Dump(ID3D12FunctionReflection *pFunctionReflection);
  153. void Dump(ID3D12LibraryReflection *pLibraryReflection);
  154. };
  155. } // namespace refl_dump