DxilRootSignature.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilRootSignature.cpp //
  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. // Provides support for manipulating root signature structures. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "dxc/DXIL/DxilConstants.h"
  12. #include "dxc/DxilRootSignature/DxilRootSignature.h"
  13. #include "dxc/Support/Global.h"
  14. #include "dxc/Support/WinIncludes.h"
  15. #include "dxc/Support/WinFunctions.h"
  16. #include "dxc/Support/FileIOHelper.h"
  17. #include "dxc/dxcapi.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include "llvm/IR/DiagnosticPrinter.h"
  20. #include <string>
  21. #include <algorithm>
  22. #include <utility>
  23. #include <vector>
  24. #include <set>
  25. #include "DxilRootSignatureHelper.h"
  26. using namespace llvm;
  27. using std::string;
  28. namespace hlsl {
  29. //////////////////////////////////////////////////////////////////////////////
  30. // Root signature handler.
  31. RootSignatureHandle::RootSignatureHandle(RootSignatureHandle&& other) {
  32. m_pDesc = nullptr;
  33. m_pSerialized = nullptr;
  34. std::swap(m_pDesc, other.m_pDesc);
  35. std::swap(m_pSerialized, other.m_pSerialized);
  36. }
  37. void RootSignatureHandle::Assign(const DxilVersionedRootSignatureDesc *pDesc,
  38. IDxcBlob *pSerialized) {
  39. Clear();
  40. m_pDesc = pDesc;
  41. m_pSerialized = pSerialized;
  42. if (m_pSerialized)
  43. m_pSerialized->AddRef();
  44. }
  45. void RootSignatureHandle::Clear() {
  46. hlsl::DeleteRootSignature(m_pDesc);
  47. m_pDesc = nullptr;
  48. if (m_pSerialized != nullptr) {
  49. m_pSerialized->Release();
  50. m_pSerialized = nullptr;
  51. }
  52. }
  53. const uint8_t *RootSignatureHandle::GetSerializedBytes() const {
  54. DXASSERT_NOMSG(m_pSerialized != nullptr);
  55. return (uint8_t *)m_pSerialized->GetBufferPointer();
  56. }
  57. unsigned RootSignatureHandle::GetSerializedSize() const {
  58. DXASSERT_NOMSG(m_pSerialized != nullptr);
  59. return m_pSerialized->GetBufferSize();
  60. }
  61. void RootSignatureHandle::EnsureSerializedAvailable() {
  62. DXASSERT_NOMSG(!IsEmpty());
  63. if (m_pSerialized == nullptr) {
  64. CComPtr<IDxcBlob> pResult;
  65. hlsl::SerializeRootSignature(m_pDesc, &pResult, nullptr, false);
  66. IFTBOOL(pResult != nullptr, E_FAIL);
  67. m_pSerialized = pResult.Detach();
  68. }
  69. }
  70. void RootSignatureHandle::Deserialize() {
  71. DXASSERT_NOMSG(m_pSerialized && !m_pDesc);
  72. DeserializeRootSignature((uint8_t*)m_pSerialized->GetBufferPointer(), (uint32_t)m_pSerialized->GetBufferSize(), &m_pDesc);
  73. }
  74. void RootSignatureHandle::LoadSerialized(const uint8_t *pData,
  75. unsigned length) {
  76. DXASSERT_NOMSG(IsEmpty());
  77. IDxcBlobEncoding *pCreated;
  78. IFT(DxcCreateBlobWithEncodingOnHeapCopy(pData, length, CP_UTF8, &pCreated));
  79. m_pSerialized = pCreated;
  80. }
  81. //////////////////////////////////////////////////////////////////////////////
  82. namespace root_sig_helper {
  83. // GetFlags/SetFlags overloads.
  84. DxilRootDescriptorFlags GetFlags(const DxilRootDescriptor &) {
  85. // Upconvert root parameter flags to be volatile.
  86. return DxilRootDescriptorFlags::DataVolatile;
  87. }
  88. void SetFlags(DxilRootDescriptor &, DxilRootDescriptorFlags) {
  89. // Drop the flags; none existed in rs_1_0.
  90. }
  91. DxilRootDescriptorFlags GetFlags(const DxilRootDescriptor1 &D) {
  92. return D.Flags;
  93. }
  94. void SetFlags(DxilRootDescriptor1 &D, DxilRootDescriptorFlags Flags) {
  95. D.Flags = Flags;
  96. }
  97. void SetFlags(DxilContainerRootDescriptor1 &D, DxilRootDescriptorFlags Flags) {
  98. D.Flags = (uint32_t)Flags;
  99. }
  100. DxilDescriptorRangeFlags GetFlags(const DxilDescriptorRange &D) {
  101. // Upconvert range flags to be volatile.
  102. DxilDescriptorRangeFlags Flags =
  103. DxilDescriptorRangeFlags::DescriptorsVolatile;
  104. // Sampler does not have data.
  105. if (D.RangeType != DxilDescriptorRangeType::Sampler)
  106. Flags = (DxilDescriptorRangeFlags)(
  107. (unsigned)Flags | (unsigned)DxilDescriptorRangeFlags::DataVolatile);
  108. return Flags;
  109. }
  110. void SetFlags(DxilDescriptorRange &, DxilDescriptorRangeFlags) {}
  111. DxilDescriptorRangeFlags GetFlags(const DxilContainerDescriptorRange &D) {
  112. // Upconvert range flags to be volatile.
  113. DxilDescriptorRangeFlags Flags =
  114. DxilDescriptorRangeFlags::DescriptorsVolatile;
  115. // Sampler does not have data.
  116. if (D.RangeType != (uint32_t)DxilDescriptorRangeType::Sampler)
  117. Flags |= DxilDescriptorRangeFlags::DataVolatile;
  118. return Flags;
  119. }
  120. void SetFlags(DxilContainerDescriptorRange &, DxilDescriptorRangeFlags) {}
  121. DxilDescriptorRangeFlags GetFlags(const DxilDescriptorRange1 &D) {
  122. return D.Flags;
  123. }
  124. void SetFlags(DxilDescriptorRange1 &D, DxilDescriptorRangeFlags Flags) {
  125. D.Flags = Flags;
  126. }
  127. DxilDescriptorRangeFlags GetFlags(const DxilContainerDescriptorRange1 &D) {
  128. return (DxilDescriptorRangeFlags)D.Flags;
  129. }
  130. void SetFlags(DxilContainerDescriptorRange1 &D,
  131. DxilDescriptorRangeFlags Flags) {
  132. D.Flags = (uint32_t)Flags;
  133. }
  134. } // namespace root_sig_helper
  135. //////////////////////////////////////////////////////////////////////////////
  136. template <typename T>
  137. void DeleteRootSignatureTemplate(const T &RS) {
  138. for (unsigned i = 0; i < RS.NumParameters; i++) {
  139. const auto &P = RS.pParameters[i];
  140. if (P.ParameterType == DxilRootParameterType::DescriptorTable) {
  141. delete[] P.DescriptorTable.pDescriptorRanges;
  142. }
  143. }
  144. delete[] RS.pParameters;
  145. delete[] RS.pStaticSamplers;
  146. }
  147. void DeleteRootSignature(const DxilVersionedRootSignatureDesc * pRootSignature)
  148. {
  149. if (pRootSignature == nullptr)
  150. return;
  151. switch (pRootSignature->Version)
  152. {
  153. case DxilRootSignatureVersion::Version_1_0:
  154. DeleteRootSignatureTemplate<DxilRootSignatureDesc>(pRootSignature->Desc_1_0);
  155. break;
  156. case DxilRootSignatureVersion::Version_1_1:
  157. default:
  158. DXASSERT(pRootSignature->Version == DxilRootSignatureVersion::Version_1_1, "else version is incorrect");
  159. DeleteRootSignatureTemplate<DxilRootSignatureDesc1>(pRootSignature->Desc_1_1);
  160. break;
  161. }
  162. delete pRootSignature;
  163. }
  164. } // namespace hlsl