DxilResource.cpp 7.5 KB

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