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