DxilShaderModel.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. bool ShaderModel::IsValidForModule() const {
  61. // Ray tracing shader model should only be used on functions in a lib
  62. return IsValid() && !IsRay();
  63. }
  64. const ShaderModel *ShaderModel::Get(unsigned Idx) {
  65. DXASSERT_NOMSG(Idx < kNumShaderModels - 1);
  66. if (Idx < kNumShaderModels - 1)
  67. return &ms_ShaderModels[Idx];
  68. else
  69. return GetInvalid();
  70. }
  71. const ShaderModel *ShaderModel::Get(Kind Kind, unsigned Major, unsigned Minor) {
  72. // Linear search. Replaced by binary search if necessary.
  73. for (unsigned i = 0; i < kNumShaderModels; i++) {
  74. const ShaderModel *pSM = &ms_ShaderModels[i];
  75. if (pSM->m_Kind == Kind && pSM->m_Major == Major && pSM->m_Minor == Minor)
  76. return pSM;
  77. }
  78. return GetInvalid();
  79. }
  80. const ShaderModel *ShaderModel::GetByName(const char *pszName) {
  81. // [ps|vs|gs|hs|ds|cs]_[major]_[minor]
  82. Kind kind;
  83. switch (pszName[0]) {
  84. case 'p': kind = Kind::Pixel; break;
  85. case 'v': kind = Kind::Vertex; break;
  86. case 'g': kind = Kind::Geometry; break;
  87. case 'h': kind = Kind::Hull; break;
  88. case 'd': kind = Kind::Domain; break;
  89. case 'c': kind = Kind::Compute; break;
  90. case 'l': kind = Kind::Library; break;
  91. default: return GetInvalid();
  92. }
  93. unsigned Idx = 3;
  94. if (kind != Kind::Library) {
  95. if (pszName[1] != 's' || pszName[2] != '_')
  96. return GetInvalid();
  97. } else {
  98. if (pszName[1] != 'i' || pszName[2] != 'b' || pszName[3] != '_')
  99. return GetInvalid();
  100. Idx = 4;
  101. }
  102. unsigned Major;
  103. switch (pszName[Idx++]) {
  104. case '4': Major = 4; break;
  105. case '5': Major = 5; break;
  106. case '6': Major = 6; break;
  107. default: return GetInvalid();
  108. }
  109. if (pszName[Idx++] != '_')
  110. return GetInvalid();
  111. unsigned Minor;
  112. switch (pszName[Idx++]) {
  113. case '0': Minor = 0; break;
  114. case '1': Minor = 1; break;
  115. case '2':
  116. if (Major == 6) {
  117. Minor = 2;
  118. break;
  119. }
  120. else return GetInvalid();
  121. case '3':
  122. if (Major == 6) {
  123. Minor = 3;
  124. break;
  125. }
  126. else return GetInvalid();
  127. default: return GetInvalid();
  128. }
  129. if (pszName[Idx++] != 0)
  130. return GetInvalid();
  131. return Get(kind, Major, Minor);
  132. }
  133. void ShaderModel::GetDxilVersion(unsigned &DxilMajor, unsigned &DxilMinor) const {
  134. DXASSERT(IsValidForDxil(), "invalid shader model");
  135. DxilMajor = 1;
  136. switch (m_Minor) {
  137. case 0:
  138. DxilMinor = 0;
  139. break;
  140. case 1:
  141. DxilMinor = 1;
  142. break;
  143. case 2:
  144. DxilMinor = 2;
  145. break;
  146. case 3:
  147. DxilMinor = 3;
  148. break;
  149. default:
  150. DXASSERT(0, "IsValidForDxil() should have caught this.");
  151. break;
  152. }
  153. }
  154. void ShaderModel::GetMinValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  155. DXASSERT(IsValidForDxil(), "invalid shader model");
  156. ValMajor = 1;
  157. switch (m_Minor) {
  158. case 0:
  159. ValMinor = 0;
  160. break;
  161. case 1:
  162. ValMinor = 1;
  163. break;
  164. case 2:
  165. ValMinor = 2;
  166. break;
  167. case 3:
  168. ValMinor = 3;
  169. break;
  170. default:
  171. DXASSERT(0, "IsValidForDxil() should have caught this.");
  172. break;
  173. }
  174. }
  175. static const char *ShaderModelKindNames[] = {
  176. "ps", "vs", "gs", "hs", "ds", "cs", "lib", "invalid",
  177. };
  178. std::string ShaderModel::GetKindName() const {
  179. return GetKindName(m_Kind);
  180. }
  181. std::string ShaderModel::GetKindName(Kind kind) {
  182. return std::string(ShaderModelKindNames[static_cast<unsigned int>(kind)]);
  183. }
  184. const ShaderModel *ShaderModel::GetInvalid() {
  185. return &ms_ShaderModels[kNumShaderModels - 1];
  186. }
  187. typedef ShaderModel SM;
  188. typedef Semantic SE;
  189. const ShaderModel ShaderModel::ms_ShaderModels[kNumShaderModels] = {
  190. // IR OR UAV? TyUAV? UAV base
  191. SM(Kind::Compute, 4, 0, "cs_4_0", 0, 0, true, false, 1),
  192. SM(Kind::Compute, 4, 1, "cs_4_1", 0, 0, true, false, 1),
  193. SM(Kind::Compute, 5, 0, "cs_5_0", 0, 0, true, true, 64),
  194. SM(Kind::Compute, 5, 1, "cs_5_1", 0, 0, true, true, UINT_MAX),
  195. SM(Kind::Compute, 6, 0, "cs_6_0", 0, 0, true, true, UINT_MAX),
  196. SM(Kind::Compute, 6, 1, "cs_6_1", 0, 0, true, true, UINT_MAX),
  197. SM(Kind::Compute, 6, 2, "cs_6_2", 0, 0, true, true, UINT_MAX),
  198. SM(Kind::Compute, 6, 3, "cs_6_3", 0, 0, true, true, UINT_MAX),
  199. SM(Kind::Domain, 5, 0, "ds_5_0", 32, 32, true, true, 64),
  200. SM(Kind::Domain, 5, 1, "ds_5_1", 32, 32, true, true, UINT_MAX),
  201. SM(Kind::Domain, 6, 0, "ds_6_0", 32, 32, true, true, UINT_MAX),
  202. SM(Kind::Domain, 6, 1, "ds_6_1", 32, 32, true, true, UINT_MAX),
  203. SM(Kind::Domain, 6, 2, "ds_6_2", 32, 32, true, true, UINT_MAX),
  204. SM(Kind::Domain, 6, 3, "ds_6_3", 32, 32, true, true, UINT_MAX),
  205. SM(Kind::Geometry, 4, 0, "gs_4_0", 16, 32, false, false, 0),
  206. SM(Kind::Geometry, 4, 1, "gs_4_1", 32, 32, false, false, 0),
  207. SM(Kind::Geometry, 5, 0, "gs_5_0", 32, 32, true, true, 64),
  208. SM(Kind::Geometry, 5, 1, "gs_5_1", 32, 32, true, true, UINT_MAX),
  209. SM(Kind::Geometry, 6, 0, "gs_6_0", 32, 32, true, true, UINT_MAX),
  210. SM(Kind::Geometry, 6, 1, "gs_6_1", 32, 32, true, true, UINT_MAX),
  211. SM(Kind::Geometry, 6, 2, "gs_6_2", 32, 32, true, true, UINT_MAX),
  212. SM(Kind::Geometry, 6, 3, "gs_6_3", 32, 32, true, true, UINT_MAX),
  213. SM(Kind::Hull, 5, 0, "hs_5_0", 32, 32, true, true, 64),
  214. SM(Kind::Hull, 5, 1, "hs_5_1", 32, 32, true, true, UINT_MAX),
  215. SM(Kind::Hull, 6, 0, "hs_6_0", 32, 32, true, true, UINT_MAX),
  216. SM(Kind::Hull, 6, 1, "hs_6_1", 32, 32, true, true, UINT_MAX),
  217. SM(Kind::Hull, 6, 2, "hs_6_2", 32, 32, true, true, UINT_MAX),
  218. SM(Kind::Hull, 6, 3, "hs_6_3", 32, 32, true, true, UINT_MAX),
  219. SM(Kind::Pixel, 4, 0, "ps_4_0", 32, 8, false, false, 0),
  220. SM(Kind::Pixel, 4, 1, "ps_4_1", 32, 8, false, false, 0),
  221. SM(Kind::Pixel, 5, 0, "ps_5_0", 32, 8, true, true, 64),
  222. SM(Kind::Pixel, 5, 1, "ps_5_1", 32, 8, true, true, UINT_MAX),
  223. SM(Kind::Pixel, 6, 0, "ps_6_0", 32, 8, true, true, UINT_MAX),
  224. SM(Kind::Pixel, 6, 1, "ps_6_1", 32, 8, true, true, UINT_MAX),
  225. SM(Kind::Pixel, 6, 2, "ps_6_2", 32, 8, true, true, UINT_MAX),
  226. SM(Kind::Pixel, 6, 3, "ps_6_3", 32, 8, true, true, UINT_MAX),
  227. SM(Kind::Vertex, 4, 0, "vs_4_0", 16, 16, false, false, 0),
  228. SM(Kind::Vertex, 4, 1, "vs_4_1", 32, 32, false, false, 0),
  229. SM(Kind::Vertex, 5, 0, "vs_5_0", 32, 32, true, true, 64),
  230. SM(Kind::Vertex, 5, 1, "vs_5_1", 32, 32, true, true, UINT_MAX),
  231. SM(Kind::Vertex, 6, 0, "vs_6_0", 32, 32, true, true, UINT_MAX),
  232. SM(Kind::Vertex, 6, 1, "vs_6_1", 32, 32, true, true, UINT_MAX),
  233. SM(Kind::Vertex, 6, 2, "vs_6_2", 32, 32, true, true, UINT_MAX),
  234. SM(Kind::Vertex, 6, 3, "vs_6_3", 32, 32, true, true, UINT_MAX),
  235. SM(Kind::Library, 6, 1, "lib_6_1", 32, 32, true, true, UINT_MAX),
  236. SM(Kind::Library, 6, 2, "lib_6_2", 32, 32, true, true, UINT_MAX),
  237. SM(Kind::Library, 6, 3, "lib_6_3", 32, 32, true, true, UINT_MAX),
  238. SM(Kind::Invalid, 0, 0, "invalid", 0, 0, false, false, 0),
  239. };
  240. } // namespace hlsl