DxilShaderModel.cpp 8.8 KB

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