DxilResource.cpp 7.7 KB

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