DxilResource.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. DXIL::SamplerFeedbackType DxilResource::GetSamplerFeedbackType() const {
  69. return m_SamplerFeedbackType;
  70. }
  71. void DxilResource::SetSamplerFeedbackType(DXIL::SamplerFeedbackType Value) {
  72. m_SamplerFeedbackType = Value;
  73. }
  74. bool DxilResource::IsGloballyCoherent() const {
  75. return m_bGloballyCoherent;
  76. }
  77. void DxilResource::SetGloballyCoherent(bool b) {
  78. m_bGloballyCoherent = b;
  79. }
  80. bool DxilResource::HasCounter() const {
  81. return m_bHasCounter;
  82. }
  83. void DxilResource::SetHasCounter(bool b) {
  84. m_bHasCounter = b;
  85. }
  86. bool DxilResource::IsRO() const {
  87. return GetClass() == DxilResourceBase::Class::SRV;
  88. }
  89. bool DxilResource::IsRW() const {
  90. return GetClass() == DxilResourceBase::Class::UAV;
  91. }
  92. void DxilResource::SetRW(bool bRW) {
  93. SetClass(bRW ? DxilResourceBase::Class::UAV : DxilResourceBase::Class::SRV);
  94. }
  95. bool DxilResource::IsROV() const {
  96. return m_bROV;
  97. }
  98. void DxilResource::SetROV(bool bROV) {
  99. m_bROV = bROV;
  100. }
  101. bool DxilResource::IsAnyTexture() const {
  102. return IsAnyTexture(GetKind());
  103. }
  104. bool DxilResource::IsAnyTexture(Kind ResourceKind) {
  105. return Kind::Texture1D <= ResourceKind &&
  106. ResourceKind <= Kind::TextureCubeArray;
  107. }
  108. bool DxilResource::IsStructuredBuffer() const {
  109. return GetKind() == Kind::StructuredBuffer;
  110. }
  111. bool DxilResource::IsTypedBuffer() const {
  112. return GetKind() == Kind::TypedBuffer;
  113. }
  114. bool DxilResource::IsRawBuffer() const {
  115. return GetKind() == Kind::RawBuffer;
  116. }
  117. bool DxilResource::IsTBuffer() const {
  118. return GetKind() == Kind::TBuffer;
  119. }
  120. bool DxilResource::IsFeedbackTexture() const {
  121. return GetKind() == Kind::FeedbackTexture2D || GetKind() == Kind::FeedbackTexture2DArray;
  122. }
  123. bool DxilResource::HasAtomic64Use() const {
  124. return m_bHasAtomic64Use;
  125. }
  126. void DxilResource::SetHasAtomic64Use(bool b) {
  127. m_bHasAtomic64Use = b;
  128. }
  129. unsigned DxilResource::GetNumCoords(Kind ResourceKind) {
  130. const unsigned CoordSizeTab[] = {
  131. 0, // Invalid = 0,
  132. 1, // Texture1D,
  133. 2, // Texture2D,
  134. 2, // Texture2DMS,
  135. 3, // Texture3D,
  136. 3, // TextureCube,
  137. 2, // Texture1DArray,
  138. 3, // Texture2DArray,
  139. 3, // Texture2DMSArray,
  140. 4, // TextureCubeArray,
  141. 1, // TypedBuffer,
  142. 1, // RawBuffer,
  143. 2, // StructuredBuffer,
  144. 0, // CBuffer,
  145. 0, // Sampler,
  146. 1, // TBuffer,
  147. 0, // RaytracingAccelerationStructure,
  148. 2, // FeedbackTexture2D,
  149. 3, // FeedbackTexture2DArray,
  150. };
  151. static_assert(_countof(CoordSizeTab) == (unsigned)Kind::NumEntries, "check helper array size");
  152. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  153. return CoordSizeTab[(unsigned)ResourceKind];
  154. }
  155. unsigned DxilResource::GetNumDimensions(Kind ResourceKind) {
  156. const unsigned NumDimTab[] = {
  157. 0, // Invalid = 0,
  158. 1, // Texture1D,
  159. 2, // Texture2D,
  160. 2, // Texture2DMS,
  161. 3, // Texture3D,
  162. 2, // TextureCube,
  163. 1, // Texture1DArray,
  164. 2, // Texture2DArray,
  165. 2, // Texture2DMSArray,
  166. 3, // TextureCubeArray,
  167. 1, // TypedBuffer,
  168. 1, // RawBuffer,
  169. 2, // StructuredBuffer,
  170. 0, // CBuffer,
  171. 0, // Sampler,
  172. 1, // TBuffer,
  173. 0, // RaytracingAccelerationStructure,
  174. 2, // FeedbackTexture2D,
  175. 2, // FeedbackTexture2DArray,
  176. };
  177. static_assert(_countof(NumDimTab) == (unsigned)Kind::NumEntries, "check helper array size");
  178. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  179. return NumDimTab[(unsigned)ResourceKind];
  180. }
  181. unsigned DxilResource::GetNumDimensionsForCalcLOD(Kind ResourceKind) {
  182. const unsigned NumDimTab[] = {
  183. 0, // Invalid = 0,
  184. 1, // Texture1D,
  185. 2, // Texture2D,
  186. 2, // Texture2DMS,
  187. 3, // Texture3D,
  188. 3, // TextureCube,
  189. 1, // Texture1DArray,
  190. 2, // Texture2DArray,
  191. 2, // Texture2DMSArray,
  192. 3, // TextureCubeArray,
  193. 1, // TypedBuffer,
  194. 1, // RawBuffer,
  195. 2, // StructuredBuffer,
  196. 0, // CBuffer,
  197. 0, // Sampler,
  198. 1, // TBuffer,
  199. 0, // RaytracingAccelerationStructure,
  200. 2, // FeedbackTexture2D,
  201. 2, // FeedbackTexture2DArray,
  202. };
  203. static_assert(_countof(NumDimTab) == (unsigned)Kind::NumEntries, "check helper array size");
  204. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  205. return NumDimTab[(unsigned)ResourceKind];
  206. }
  207. unsigned DxilResource::GetNumOffsets(Kind ResourceKind) {
  208. const unsigned OffsetSizeTab[] = {
  209. 0, // Invalid = 0,
  210. 1, // Texture1D,
  211. 2, // Texture2D,
  212. 2, // Texture2DMS,
  213. 3, // Texture3D,
  214. 0, // TextureCube,
  215. 1, // Texture1DArray,
  216. 2, // Texture2DArray,
  217. 2, // Texture2DMSArray,
  218. 0, // TextureCubeArray,
  219. 0, // TypedBuffer,
  220. 0, // RawBuffer,
  221. 0, // StructuredBuffer,
  222. 0, // CBuffer,
  223. 0, // Sampler,
  224. 1, // TBuffer,
  225. 0, // RaytracingAccelerationStructure,
  226. 2, // FeedbackTexture2D,
  227. 2, // FeedbackTexture2DArray,
  228. };
  229. static_assert(_countof(OffsetSizeTab) == (unsigned)Kind::NumEntries, "check helper array size");
  230. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  231. return OffsetSizeTab[(unsigned)ResourceKind];
  232. }
  233. } // namespace hlsl