DxilResource.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. bool DxilResource::HasAtomic64Use() const {
  123. return m_bHasAtomic64Use;
  124. }
  125. void DxilResource::SetHasAtomic64Use(bool b) {
  126. m_bHasAtomic64Use = b;
  127. }
  128. unsigned DxilResource::GetNumCoords(Kind ResourceKind) {
  129. const unsigned CoordSizeTab[] = {
  130. 0, // Invalid = 0,
  131. 1, // Texture1D,
  132. 2, // Texture2D,
  133. 2, // Texture2DMS,
  134. 3, // Texture3D,
  135. 3, // TextureCube,
  136. 2, // Texture1DArray,
  137. 3, // Texture2DArray,
  138. 3, // Texture2DMSArray,
  139. 4, // TextureCubeArray,
  140. 1, // TypedBuffer,
  141. 1, // RawBuffer,
  142. 2, // StructuredBuffer,
  143. 0, // CBuffer,
  144. 0, // Sampler,
  145. 1, // TBuffer,
  146. 0, // RaytracingAccelerationStructure,
  147. 2, // FeedbackTexture2D,
  148. 3, // FeedbackTexture2DArray,
  149. };
  150. static_assert(_countof(CoordSizeTab) == (unsigned)Kind::NumEntries, "check helper array size");
  151. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  152. return CoordSizeTab[(unsigned)ResourceKind];
  153. }
  154. unsigned DxilResource::GetNumDimensions(Kind ResourceKind) {
  155. const unsigned NumDimTab[] = {
  156. 0, // Invalid = 0,
  157. 1, // Texture1D,
  158. 2, // Texture2D,
  159. 2, // Texture2DMS,
  160. 3, // Texture3D,
  161. 2, // TextureCube,
  162. 1, // Texture1DArray,
  163. 2, // Texture2DArray,
  164. 2, // Texture2DMSArray,
  165. 3, // TextureCubeArray,
  166. 1, // TypedBuffer,
  167. 1, // RawBuffer,
  168. 2, // StructuredBuffer,
  169. 0, // CBuffer,
  170. 0, // Sampler,
  171. 1, // TBuffer,
  172. 0, // RaytracingAccelerationStructure,
  173. 2, // FeedbackTexture2D,
  174. 2, // FeedbackTexture2DArray,
  175. };
  176. static_assert(_countof(NumDimTab) == (unsigned)Kind::NumEntries, "check helper array size");
  177. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  178. return NumDimTab[(unsigned)ResourceKind];
  179. }
  180. unsigned DxilResource::GetNumDimensionsForCalcLOD(Kind ResourceKind) {
  181. const unsigned NumDimTab[] = {
  182. 0, // Invalid = 0,
  183. 1, // Texture1D,
  184. 2, // Texture2D,
  185. 2, // Texture2DMS,
  186. 3, // Texture3D,
  187. 3, // TextureCube,
  188. 1, // Texture1DArray,
  189. 2, // Texture2DArray,
  190. 2, // Texture2DMSArray,
  191. 3, // TextureCubeArray,
  192. 1, // TypedBuffer,
  193. 1, // RawBuffer,
  194. 2, // StructuredBuffer,
  195. 0, // CBuffer,
  196. 0, // Sampler,
  197. 1, // TBuffer,
  198. 0, // RaytracingAccelerationStructure,
  199. 2, // FeedbackTexture2D,
  200. 2, // FeedbackTexture2DArray,
  201. };
  202. static_assert(_countof(NumDimTab) == (unsigned)Kind::NumEntries, "check helper array size");
  203. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  204. return NumDimTab[(unsigned)ResourceKind];
  205. }
  206. unsigned DxilResource::GetNumOffsets(Kind ResourceKind) {
  207. const unsigned OffsetSizeTab[] = {
  208. 0, // Invalid = 0,
  209. 1, // Texture1D,
  210. 2, // Texture2D,
  211. 2, // Texture2DMS,
  212. 3, // Texture3D,
  213. 0, // TextureCube,
  214. 1, // Texture1DArray,
  215. 2, // Texture2DArray,
  216. 2, // Texture2DMSArray,
  217. 0, // TextureCubeArray,
  218. 0, // TypedBuffer,
  219. 0, // RawBuffer,
  220. 0, // StructuredBuffer,
  221. 0, // CBuffer,
  222. 0, // Sampler,
  223. 1, // TBuffer,
  224. 0, // RaytracingAccelerationStructure,
  225. 2, // FeedbackTexture2D,
  226. 2, // FeedbackTexture2DArray,
  227. };
  228. static_assert(_countof(OffsetSizeTab) == (unsigned)Kind::NumEntries, "check helper array size");
  229. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  230. return OffsetSizeTab[(unsigned)ResourceKind];
  231. }
  232. } // namespace hlsl