DxilResource.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilResource.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. ///////////////////////////////////////////////////////////////////////////////
  9. #include "dxc/DXIL/DxilResource.h"
  10. #include "dxc/Support/Global.h"
  11. #include "dxc/DXIL/DxilResourceBase.h"
  12. #include "llvm/IR/Constant.h"
  13. #include "llvm/IR/DerivedTypes.h"
  14. using namespace llvm;
  15. namespace hlsl {
  16. //------------------------------------------------------------------------------
  17. //
  18. // Resource methods.
  19. //
  20. DxilResource::DxilResource()
  21. : DxilResourceBase(DxilResourceBase::Class::Invalid)
  22. , m_SampleCount(0)
  23. , m_ElementStride(0)
  24. , m_bGloballyCoherent(false)
  25. , m_bHasCounter(false)
  26. , m_bROV(false) {
  27. }
  28. CompType DxilResource::GetCompType() const {
  29. return m_CompType;
  30. }
  31. void DxilResource::SetCompType(const CompType CT) {
  32. m_CompType = CT;
  33. }
  34. Type *DxilResource::GetRetType() const {
  35. Type *Ty = GetGlobalSymbol()->getType()->getPointerElementType();
  36. // For resource array, use element type.
  37. while (Ty->isArrayTy())
  38. Ty = Ty->getArrayElementType();
  39. // Get the struct buffer type like this %class.StructuredBuffer = type {
  40. // %struct.mat }.
  41. StructType *ST = cast<StructType>(Ty);
  42. // Get the struct type inside struct buffer.
  43. return ST->getElementType(0);
  44. }
  45. unsigned DxilResource::GetSampleCount() const {
  46. return m_SampleCount;
  47. }
  48. void DxilResource::SetSampleCount(unsigned SampleCount) {
  49. m_SampleCount = SampleCount;
  50. }
  51. unsigned DxilResource::GetElementStride() const {
  52. return m_ElementStride;
  53. }
  54. void DxilResource::SetElementStride(unsigned ElemStride) {
  55. m_ElementStride = ElemStride;
  56. }
  57. bool DxilResource::IsGloballyCoherent() const {
  58. return m_bGloballyCoherent;
  59. }
  60. void DxilResource::SetGloballyCoherent(bool b) {
  61. m_bGloballyCoherent = b;
  62. }
  63. bool DxilResource::HasCounter() const {
  64. return m_bHasCounter;
  65. }
  66. void DxilResource::SetHasCounter(bool b) {
  67. m_bHasCounter = b;
  68. }
  69. bool DxilResource::IsRO() const {
  70. return GetClass() == DxilResourceBase::Class::SRV;
  71. }
  72. bool DxilResource::IsRW() const {
  73. return GetClass() == DxilResourceBase::Class::UAV;
  74. }
  75. void DxilResource::SetRW(bool bRW) {
  76. SetClass(bRW ? DxilResourceBase::Class::UAV : DxilResourceBase::Class::SRV);
  77. }
  78. bool DxilResource::IsROV() const {
  79. return m_bROV;
  80. }
  81. void DxilResource::SetROV(bool bROV) {
  82. m_bROV = bROV;
  83. }
  84. bool DxilResource::IsAnyTexture() const {
  85. return IsAnyTexture(GetKind());
  86. }
  87. bool DxilResource::IsAnyTexture(Kind ResourceKind) {
  88. return Kind::Texture1D <= ResourceKind &&
  89. ResourceKind <= Kind::TextureCubeArray;
  90. }
  91. bool DxilResource::IsStructuredBuffer() const {
  92. return GetKind() == Kind::StructuredBuffer;
  93. }
  94. bool DxilResource::IsTypedBuffer() const {
  95. return GetKind() == Kind::TypedBuffer;
  96. }
  97. bool DxilResource::IsRawBuffer() const {
  98. return GetKind() == Kind::RawBuffer;
  99. }
  100. bool DxilResource::IsTBuffer() const {
  101. return GetKind() == Kind::TBuffer;
  102. }
  103. unsigned DxilResource::GetNumCoords(Kind ResourceKind) {
  104. const unsigned CoordSizeTab[] = {
  105. 0, // Invalid = 0,
  106. 1, // Texture1D,
  107. 2, // Texture2D,
  108. 2, // Texture2DMS,
  109. 3, // Texture3D,
  110. 3, // TextureCube,
  111. 2, // Texture1DArray,
  112. 3, // Texture2DArray,
  113. 3, // Texture2DMSArray,
  114. 4, // TextureCubeArray,
  115. 1, // TypedBuffer,
  116. 1, // RawBuffer,
  117. 2, // StructuredBuffer,
  118. 0, // CBuffer,
  119. 0, // Sampler,
  120. 1, // TBuffer,
  121. 0, // RaytracingAccelerationStructure,
  122. };
  123. static_assert(_countof(CoordSizeTab) == (unsigned)Kind::NumEntries, "check helper array size");
  124. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  125. return CoordSizeTab[(unsigned)ResourceKind];
  126. }
  127. unsigned DxilResource::GetNumDimensions(Kind ResourceKind) {
  128. const unsigned NumDimTab[] = {
  129. 0, // Invalid = 0,
  130. 1, // Texture1D,
  131. 2, // Texture2D,
  132. 2, // Texture2DMS,
  133. 3, // Texture3D,
  134. 2, // TextureCube,
  135. 1, // Texture1DArray,
  136. 2, // Texture2DArray,
  137. 2, // Texture2DMSArray,
  138. 3, // TextureCubeArray,
  139. 1, // TypedBuffer,
  140. 1, // RawBuffer,
  141. 2, // StructuredBuffer,
  142. 0, // CBuffer,
  143. 0, // Sampler,
  144. 1, // TBuffer,
  145. 0, // RaytracingAccelerationStructure,
  146. };
  147. static_assert(_countof(NumDimTab) == (unsigned)Kind::NumEntries, "check helper array size");
  148. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  149. return NumDimTab[(unsigned)ResourceKind];
  150. }
  151. unsigned DxilResource::GetNumDimensionsForCalcLOD(Kind ResourceKind) {
  152. const unsigned NumDimTab[] = {
  153. 0, // Invalid = 0,
  154. 1, // Texture1D,
  155. 2, // Texture2D,
  156. 2, // Texture2DMS,
  157. 3, // Texture3D,
  158. 3, // TextureCube,
  159. 1, // Texture1DArray,
  160. 2, // Texture2DArray,
  161. 2, // Texture2DMSArray,
  162. 3, // TextureCubeArray,
  163. 1, // TypedBuffer,
  164. 1, // RawBuffer,
  165. 2, // StructuredBuffer,
  166. 0, // CBuffer,
  167. 0, // Sampler,
  168. 1, // TBuffer,
  169. 0, // RaytracingAccelerationStructure,
  170. };
  171. static_assert(_countof(NumDimTab) == (unsigned)Kind::NumEntries, "check helper array size");
  172. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  173. return NumDimTab[(unsigned)ResourceKind];
  174. }
  175. unsigned DxilResource::GetNumOffsets(Kind ResourceKind) {
  176. const unsigned OffsetSizeTab[] = {
  177. 0, // Invalid = 0,
  178. 1, // Texture1D,
  179. 2, // Texture2D,
  180. 2, // Texture2DMS,
  181. 3, // Texture3D,
  182. 0, // TextureCube,
  183. 1, // Texture1DArray,
  184. 2, // Texture2DArray,
  185. 2, // Texture2DMSArray,
  186. 0, // TextureCubeArray,
  187. 0, // TypedBuffer,
  188. 0, // RawBuffer,
  189. 0, // StructuredBuffer,
  190. 0, // CBuffer,
  191. 0, // Sampler,
  192. 1, // TBuffer,
  193. 0, // RaytracingAccelerationStructure,
  194. };
  195. static_assert(_countof(OffsetSizeTab) == (unsigned)Kind::NumEntries, "check helper array size");
  196. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  197. return OffsetSizeTab[(unsigned)ResourceKind];
  198. }
  199. } // namespace hlsl