DxilRootSignature.cpp 6.3 KB

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