DxilShaderModel.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilShaderModel.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/DxilShaderModel.h"
  10. #include "dxc/HLSL/DxilSemantic.h"
  11. #include "dxc/Support/Global.h"
  12. namespace hlsl {
  13. ShaderModel::ShaderModel(Kind Kind, unsigned Major, unsigned Minor, const char *pszName,
  14. unsigned NumInputRegs, unsigned NumOutputRegs,
  15. bool bUAVs, bool bTypedUavs, unsigned NumUAVRegs)
  16. : m_Kind(Kind)
  17. , m_Major(Major)
  18. , m_Minor(Minor)
  19. , m_pszName(pszName)
  20. , m_NumInputRegs(NumInputRegs)
  21. , m_NumOutputRegs(NumOutputRegs)
  22. , m_bUAVs(bUAVs)
  23. , m_bTypedUavs(bTypedUavs)
  24. , m_NumUAVRegs(NumUAVRegs) {
  25. }
  26. bool ShaderModel::operator==(const ShaderModel &other) const {
  27. return m_Kind == other.m_Kind
  28. && m_Major == other.m_Major
  29. && m_Minor == other.m_Minor
  30. && strcmp(m_pszName, other.m_pszName) == 0
  31. && m_NumInputRegs == other.m_NumInputRegs
  32. && m_NumOutputRegs == other.m_NumOutputRegs
  33. && m_bTypedUavs == other.m_bTypedUavs
  34. && m_NumUAVRegs == other.m_NumUAVRegs;
  35. }
  36. bool ShaderModel::IsValid() const {
  37. DXASSERT(IsPS() || IsVS() || IsGS() || IsHS() || IsDS() || IsCS() ||
  38. IsLib() || m_Kind == Kind::Invalid,
  39. "invalid shader model");
  40. return m_Kind != Kind::Invalid;
  41. }
  42. bool ShaderModel::IsValidForDxil() const {
  43. if (!IsValid())
  44. return false;
  45. switch (m_Major) {
  46. case 6: {
  47. switch (m_Minor) {
  48. case 0:
  49. case 1:
  50. case 2:
  51. return true;
  52. }
  53. }
  54. break;
  55. }
  56. return false;
  57. }
  58. const ShaderModel *ShaderModel::Get(unsigned Idx) {
  59. DXASSERT_NOMSG(Idx < kNumShaderModels - 1);
  60. if (Idx < kNumShaderModels - 1)
  61. return &ms_ShaderModels[Idx];
  62. else
  63. return GetInvalid();
  64. }
  65. const ShaderModel *ShaderModel::Get(Kind Kind, unsigned Major, unsigned Minor) {
  66. // Linear search. Replaced by binary search if necessary.
  67. for (unsigned i = 0; i < kNumShaderModels; i++) {
  68. const ShaderModel *pSM = &ms_ShaderModels[i];
  69. if (pSM->m_Kind == Kind && pSM->m_Major == Major && pSM->m_Minor == Minor)
  70. return pSM;
  71. }
  72. return GetInvalid();
  73. }
  74. const ShaderModel *ShaderModel::GetByName(const char *pszName) {
  75. // [ps|vs|gs|hs|ds|cs]_[major]_[minor]
  76. Kind Kind;
  77. switch (pszName[0]) {
  78. case 'p': Kind = Kind::Pixel; break;
  79. case 'v': Kind = Kind::Vertex; break;
  80. case 'g': Kind = Kind::Geometry; break;
  81. case 'h': Kind = Kind::Hull; break;
  82. case 'd': Kind = Kind::Domain; break;
  83. case 'c': Kind = Kind::Compute; break;
  84. case 'l': Kind = Kind::Library; break;
  85. default: return GetInvalid();
  86. }
  87. unsigned Idx = 3;
  88. if (Kind != Kind::Library) {
  89. if (pszName[1] != 's' || pszName[2] != '_')
  90. return GetInvalid();
  91. } else {
  92. if (pszName[1] != 'i' || pszName[2] != 'b' || pszName[3] != '_')
  93. return GetInvalid();
  94. Idx = 4;
  95. }
  96. unsigned Major;
  97. switch (pszName[Idx++]) {
  98. case '4': Major = 4; break;
  99. case '5': Major = 5; break;
  100. case '6': Major = 6; break;
  101. default: return GetInvalid();
  102. }
  103. if (pszName[Idx++] != '_')
  104. return GetInvalid();
  105. unsigned Minor;
  106. switch (pszName[Idx++]) {
  107. case '0': Minor = 0; break;
  108. case '1': Minor = 1; break;
  109. case '2':
  110. if (Major == 6) {
  111. Minor = 2;
  112. break;
  113. }
  114. else return GetInvalid();
  115. default: return GetInvalid();
  116. }
  117. if (pszName[Idx++] != 0)
  118. return GetInvalid();
  119. return Get(Kind, Major, Minor);
  120. }
  121. void ShaderModel::GetDxilVersion(unsigned &DxilMajor, unsigned &DxilMinor) const {
  122. DXASSERT(IsValidForDxil(), "invalid shader model");
  123. DxilMajor = 1;
  124. switch (m_Minor) {
  125. case 0:
  126. DxilMinor = 0;
  127. break;
  128. case 1:
  129. DxilMinor = 1;
  130. break;
  131. case 2:
  132. DxilMinor = 2;
  133. break;
  134. default:
  135. DXASSERT(0, "IsValidForDxil() should have caught this.");
  136. break;
  137. }
  138. }
  139. void ShaderModel::GetMinValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  140. DXASSERT(IsValidForDxil(), "invalid shader model");
  141. ValMajor = 1;
  142. switch (m_Minor) {
  143. case 0:
  144. ValMinor = 0;
  145. break;
  146. case 1:
  147. ValMinor = 1;
  148. break;
  149. case 2:
  150. ValMinor = 2;
  151. break;
  152. default:
  153. DXASSERT(0, "IsValidForDxil() should have caught this.");
  154. break;
  155. }
  156. }
  157. static const char *ShaderModelKindNames[] = {
  158. "ps", "vs", "gs", "hs", "ds", "cs", "lib", "invalid",
  159. };
  160. std::string ShaderModel::GetKindName() const {
  161. return GetKindName(m_Kind);
  162. }
  163. std::string ShaderModel::GetKindName(Kind kind) {
  164. return std::string(ShaderModelKindNames[static_cast<unsigned int>(kind)]);
  165. }
  166. const ShaderModel *ShaderModel::GetInvalid() {
  167. return &ms_ShaderModels[kNumShaderModels - 1];
  168. }
  169. typedef ShaderModel SM;
  170. typedef Semantic SE;
  171. const ShaderModel ShaderModel::ms_ShaderModels[kNumShaderModels] = {
  172. // IR OR UAV? TyUAV? UAV base
  173. SM(Kind::Compute, 4, 0, "cs_4_0", 0, 0, true, false, 1),
  174. SM(Kind::Compute, 4, 1, "cs_4_1", 0, 0, true, false, 1),
  175. SM(Kind::Compute, 5, 0, "cs_5_0", 0, 0, true, true, 64),
  176. SM(Kind::Compute, 5, 1, "cs_5_1", 0, 0, true, true, UINT_MAX),
  177. SM(Kind::Compute, 6, 0, "cs_6_0", 0, 0, true, true, UINT_MAX),
  178. SM(Kind::Compute, 6, 1, "cs_6_1", 0, 0, true, true, UINT_MAX),
  179. SM(Kind::Compute, 6, 2, "cs_6_2", 0, 0, true, true, UINT_MAX),
  180. SM(Kind::Domain, 5, 0, "ds_5_0", 32, 32, true, true, 64),
  181. SM(Kind::Domain, 5, 1, "ds_5_1", 32, 32, true, true, UINT_MAX),
  182. SM(Kind::Domain, 6, 0, "ds_6_0", 32, 32, true, true, UINT_MAX),
  183. SM(Kind::Domain, 6, 1, "ds_6_1", 32, 32, true, true, UINT_MAX),
  184. SM(Kind::Domain, 6, 2, "ds_6_2", 32, 32, true, true, UINT_MAX),
  185. SM(Kind::Geometry, 4, 0, "gs_4_0", 16, 32, false, false, 0),
  186. SM(Kind::Geometry, 4, 1, "gs_4_1", 32, 32, false, false, 0),
  187. SM(Kind::Geometry, 5, 0, "gs_5_0", 32, 32, true, true, 64),
  188. SM(Kind::Geometry, 5, 1, "gs_5_1", 32, 32, true, true, UINT_MAX),
  189. SM(Kind::Geometry, 6, 0, "gs_6_0", 32, 32, true, true, UINT_MAX),
  190. SM(Kind::Geometry, 6, 1, "gs_6_1", 32, 32, true, true, UINT_MAX),
  191. SM(Kind::Geometry, 6, 2, "gs_6_2", 32, 32, true, true, UINT_MAX),
  192. SM(Kind::Hull, 5, 0, "hs_5_0", 32, 32, true, true, 64),
  193. SM(Kind::Hull, 5, 1, "hs_5_1", 32, 32, true, true, UINT_MAX),
  194. SM(Kind::Hull, 6, 0, "hs_6_0", 32, 32, true, true, UINT_MAX),
  195. SM(Kind::Hull, 6, 1, "hs_6_1", 32, 32, true, true, UINT_MAX),
  196. SM(Kind::Hull, 6, 2, "hs_6_2", 32, 32, true, true, UINT_MAX),
  197. SM(Kind::Pixel, 4, 0, "ps_4_0", 32, 8, false, false, 0),
  198. SM(Kind::Pixel, 4, 1, "ps_4_1", 32, 8, false, false, 0),
  199. SM(Kind::Pixel, 5, 0, "ps_5_0", 32, 8, true, true, 64),
  200. SM(Kind::Pixel, 5, 1, "ps_5_1", 32, 8, true, true, UINT_MAX),
  201. SM(Kind::Pixel, 6, 0, "ps_6_0", 32, 8, true, true, UINT_MAX),
  202. SM(Kind::Pixel, 6, 1, "ps_6_1", 32, 8, true, true, UINT_MAX),
  203. SM(Kind::Pixel, 6, 2, "ps_6_2", 32, 8, true, true, UINT_MAX),
  204. SM(Kind::Vertex, 4, 0, "vs_4_0", 16, 16, false, false, 0),
  205. SM(Kind::Vertex, 4, 1, "vs_4_1", 32, 32, false, false, 0),
  206. SM(Kind::Vertex, 5, 0, "vs_5_0", 32, 32, true, true, 64),
  207. SM(Kind::Vertex, 5, 1, "vs_5_1", 32, 32, true, true, UINT_MAX),
  208. SM(Kind::Vertex, 6, 0, "vs_6_0", 32, 32, true, true, UINT_MAX),
  209. SM(Kind::Vertex, 6, 1, "vs_6_1", 32, 32, true, true, UINT_MAX),
  210. SM(Kind::Vertex, 6, 2, "vs_6_2", 32, 32, true, true, UINT_MAX),
  211. SM(Kind::Library, 6, 1, "lib_6_1", 32, 32, true, true, UINT_MAX),
  212. SM(Kind::Library, 6, 2, "lib_6_2", 32, 32, true, true, UINT_MAX),
  213. SM(Kind::Invalid, 0, 0, "invalid", 0, 0, false, false, 0),
  214. };
  215. } // namespace hlsl