DxilResource.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/HLSL/DxilResource.h"
  10. #include "dxc/Support/Global.h"
  11. #include "dxc/HLSL/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_bGloballyCoherent(false)
  25. , m_bHasCounter(false)
  26. , m_bROV(false) {
  27. }
  28. CompType DxilResource::GetCompType() const {
  29. return m_CompType;
  30. }
  31. void DxilResource::SetCompType(const CompType CT) {
  32. m_CompType = CT;
  33. }
  34. Type *DxilResource::GetRetType() const {
  35. Constant *GV = GetGlobalSymbol();
  36. Type *Ty = GV->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. bool DxilResource::IsGloballyCoherent() const {
  59. return m_bGloballyCoherent;
  60. }
  61. void DxilResource::SetGloballyCoherent(bool b) {
  62. m_bGloballyCoherent = b;
  63. }
  64. bool DxilResource::HasCounter() const {
  65. return m_bHasCounter;
  66. }
  67. void DxilResource::SetHasCounter(bool b) {
  68. m_bHasCounter = b;
  69. }
  70. bool DxilResource::IsRO() const {
  71. return GetClass() == DxilResourceBase::Class::SRV;
  72. }
  73. bool DxilResource::IsRW() const {
  74. return GetClass() == DxilResourceBase::Class::UAV;
  75. }
  76. void DxilResource::SetRW(bool bRW) {
  77. SetClass(bRW ? DxilResourceBase::Class::UAV : DxilResourceBase::Class::SRV);
  78. }
  79. bool DxilResource::IsROV() const {
  80. return m_bROV;
  81. }
  82. void DxilResource::SetROV(bool bROV) {
  83. m_bROV = bROV;
  84. }
  85. bool DxilResource::IsAnyTexture() const {
  86. return Kind::Texture1D <= GetKind() && GetKind() <= Kind::TextureCubeArray;
  87. }
  88. bool DxilResource::IsStructuredBuffer() const {
  89. return GetKind() == Kind::StructuredBuffer;
  90. }
  91. bool DxilResource::IsTypedBuffer() const {
  92. return GetKind() == Kind::TypedBuffer;
  93. }
  94. bool DxilResource::IsRawBuffer() const {
  95. return GetKind() == Kind::RawBuffer;
  96. }
  97. bool DxilResource::IsTBuffer() const {
  98. return GetKind() == Kind::TBuffer;
  99. }
  100. unsigned DxilResource::GetNumCoords(Kind ResourceKind) {
  101. const unsigned CoordSizeTab[] = {
  102. 0, // Invalid = 0,
  103. 1, // Texture1D,
  104. 2, // Texture2D,
  105. 2, // Texture2DMS,
  106. 3, // Texture3D,
  107. 3, // TextureCube,
  108. 2, // Texture1DArray,
  109. 3, // Texture2DArray,
  110. 3, // Texture2DMSArray,
  111. 4, // TextureCubeArray,
  112. 1, // TypedBuffer,
  113. 1, // RawBuffer,
  114. 2, // StructuredBuffer,
  115. 0, // CBuffer,
  116. 0, // Sampler,
  117. 1, // TBuffer,
  118. };
  119. static_assert(_countof(CoordSizeTab) == (unsigned)Kind::NumEntries, "check helper array size");
  120. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  121. return CoordSizeTab[(unsigned)ResourceKind];
  122. }
  123. unsigned DxilResource::GetNumDimensions(Kind ResourceKind) {
  124. const unsigned NumDimTab[] = {
  125. 0, // Invalid = 0,
  126. 1, // Texture1D,
  127. 2, // Texture2D,
  128. 2, // Texture2DMS,
  129. 3, // Texture3D,
  130. 2, // TextureCube,
  131. 1, // Texture1DArray,
  132. 2, // Texture2DArray,
  133. 2, // Texture2DMSArray,
  134. 3, // TextureCubeArray,
  135. 1, // TypedBuffer,
  136. 1, // RawBuffer,
  137. 2, // StructuredBuffer,
  138. 0, // CBuffer,
  139. 0, // Sampler,
  140. 1, // TBuffer,
  141. };
  142. static_assert(_countof(NumDimTab) == (unsigned)Kind::NumEntries, "check helper array size");
  143. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  144. return NumDimTab[(unsigned)ResourceKind];
  145. }
  146. unsigned DxilResource::GetNumDimensionsForCalcLOD(Kind ResourceKind) {
  147. const unsigned NumDimTab[] = {
  148. 0, // Invalid = 0,
  149. 1, // Texture1D,
  150. 2, // Texture2D,
  151. 2, // Texture2DMS,
  152. 3, // Texture3D,
  153. 3, // TextureCube,
  154. 1, // Texture1DArray,
  155. 2, // Texture2DArray,
  156. 2, // Texture2DMSArray,
  157. 3, // TextureCubeArray,
  158. 1, // TypedBuffer,
  159. 1, // RawBuffer,
  160. 2, // StructuredBuffer,
  161. 0, // CBuffer,
  162. 0, // Sampler,
  163. 1, // TBuffer,
  164. };
  165. static_assert(_countof(NumDimTab) == (unsigned)Kind::NumEntries, "check helper array size");
  166. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  167. return NumDimTab[(unsigned)ResourceKind];
  168. }
  169. unsigned DxilResource::GetNumOffsets(Kind ResourceKind) {
  170. const unsigned OffsetSizeTab[] = {
  171. 0, // Invalid = 0,
  172. 1, // Texture1D,
  173. 2, // Texture2D,
  174. 0, // Texture2DMS,
  175. 3, // Texture3D,
  176. 0, // TextureCube,
  177. 1, // Texture1DArray,
  178. 2, // Texture2DArray,
  179. 0, // Texture2DMSArray,
  180. 0, // TextureCubeArray,
  181. 0, // TypedBuffer,
  182. 0, // RawBuffer,
  183. 0, // StructuredBuffer,
  184. 0, // CBuffer,
  185. 0, // Sampler,
  186. 1, // TBuffer,
  187. };
  188. static_assert(_countof(OffsetSizeTab) == (unsigned)Kind::NumEntries, "check helper array size");
  189. DXASSERT(ResourceKind > Kind::Invalid && ResourceKind < Kind::NumEntries, "otherwise the caller passed wrong resource type");
  190. return OffsetSizeTab[(unsigned)ResourceKind];
  191. }
  192. } // namespace hlsl