DxilShaderModel.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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() || m_Kind == Kind::Invalid, "invalid shader model");
  38. return m_Kind != Kind::Invalid;
  39. }
  40. bool ShaderModel::IsValidForDxil() const {
  41. if (!IsValid())
  42. return false;
  43. switch (m_Major) {
  44. case 6: {
  45. switch (m_Minor) {
  46. case 0:
  47. case 1:
  48. return true;
  49. }
  50. }
  51. break;
  52. }
  53. return false;
  54. }
  55. const ShaderModel *ShaderModel::Get(unsigned Idx) {
  56. DXASSERT_NOMSG(Idx < kNumShaderModels - 1);
  57. if (Idx < kNumShaderModels - 1)
  58. return &ms_ShaderModels[Idx];
  59. else
  60. return GetInvalid();
  61. }
  62. const ShaderModel *ShaderModel::Get(Kind Kind, unsigned Major, unsigned Minor) {
  63. // Linear search. Replaced by binary search if necessary.
  64. for (unsigned i = 0; i < kNumShaderModels; i++) {
  65. const ShaderModel *pSM = &ms_ShaderModels[i];
  66. if (pSM->m_Kind == Kind && pSM->m_Major == Major && pSM->m_Minor == Minor)
  67. return pSM;
  68. }
  69. return GetInvalid();
  70. }
  71. const ShaderModel *ShaderModel::GetByName(const char *pszName) {
  72. // [ps|vs|gs|hs|ds|cs]_[major]_[minor]
  73. Kind Kind;
  74. switch (pszName[0]) {
  75. case 'p': Kind = Kind::Pixel; break;
  76. case 'v': Kind = Kind::Vertex; break;
  77. case 'g': Kind = Kind::Geometry; break;
  78. case 'h': Kind = Kind::Hull; break;
  79. case 'd': Kind = Kind::Domain; break;
  80. case 'c': Kind = Kind::Compute; break;
  81. default: return GetInvalid();
  82. }
  83. unsigned Idx = 3;
  84. if (pszName[1] != 's' || pszName[2] != '_')
  85. return GetInvalid();
  86. unsigned Major;
  87. switch (pszName[Idx++]) {
  88. case '4': Major = 4; break;
  89. case '5': Major = 5; break;
  90. case '6': Major = 6; break;
  91. default: return GetInvalid();
  92. }
  93. if (pszName[Idx++] != '_')
  94. return GetInvalid();
  95. unsigned Minor;
  96. switch (pszName[Idx++]) {
  97. case '0': Minor = 0; break;
  98. case '1': Minor = 1; break;
  99. default: return GetInvalid();
  100. }
  101. if (pszName[Idx++] != 0)
  102. return GetInvalid();
  103. return Get(Kind, Major, Minor);
  104. }
  105. void ShaderModel::GetDxilVersion(unsigned &DxilMajor, unsigned &DxilMinor) const {
  106. DXASSERT(IsValidForDxil(), "invalid shader model");
  107. DxilMajor = 1;
  108. switch (m_Minor) {
  109. case 0:
  110. DxilMinor = 0;
  111. break;
  112. case 1:
  113. DxilMinor = 1;
  114. break;
  115. default:
  116. DXASSERT(0, "IsValidForDxil() should have caught this.");
  117. break;
  118. }
  119. }
  120. void ShaderModel::GetMinValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  121. DXASSERT(IsValidForDxil(), "invalid shader model");
  122. ValMajor = 1;
  123. switch (m_Minor) {
  124. case 0:
  125. ValMinor = 0;
  126. break;
  127. case 1:
  128. ValMinor = 1;
  129. break;
  130. default:
  131. DXASSERT(0, "IsValidForDxil() should have caught this.");
  132. break;
  133. }
  134. }
  135. std::string ShaderModel::GetKindName() const {
  136. return std::string(m_pszName).substr(0, 2);
  137. }
  138. const ShaderModel *ShaderModel::GetInvalid() {
  139. return &ms_ShaderModels[kNumShaderModels - 1];
  140. }
  141. typedef ShaderModel SM;
  142. typedef Semantic SE;
  143. const ShaderModel ShaderModel::ms_ShaderModels[kNumShaderModels] = {
  144. // IR OR UAV? TyUAV? UAV base
  145. SM(Kind::Compute, 4, 0, "cs_4_0", 0, 0, true, false, 1),
  146. SM(Kind::Compute, 4, 1, "cs_4_1", 0, 0, true, false, 1),
  147. SM(Kind::Compute, 5, 0, "cs_5_0", 0, 0, true, true, 64),
  148. SM(Kind::Compute, 5, 1, "cs_5_1", 0, 0, true, true, UINT_MAX),
  149. SM(Kind::Compute, 6, 0, "cs_6_0", 0, 0, true, true, UINT_MAX),
  150. SM(Kind::Compute, 6, 1, "cs_6_1", 0, 0, true, true, UINT_MAX),
  151. SM(Kind::Domain, 5, 0, "ds_5_0", 32, 32, true, true, 64),
  152. SM(Kind::Domain, 5, 1, "ds_5_1", 32, 32, true, true, UINT_MAX),
  153. SM(Kind::Domain, 6, 0, "ds_6_0", 32, 32, true, true, UINT_MAX),
  154. SM(Kind::Domain, 6, 1, "ds_6_1", 32, 32, true, true, UINT_MAX),
  155. SM(Kind::Geometry, 4, 0, "gs_4_0", 16, 32, false, false, 0),
  156. SM(Kind::Geometry, 4, 1, "gs_4_1", 32, 32, false, false, 0),
  157. SM(Kind::Geometry, 5, 0, "gs_5_0", 32, 32, true, true, 64),
  158. SM(Kind::Geometry, 5, 1, "gs_5_1", 32, 32, true, true, UINT_MAX),
  159. SM(Kind::Geometry, 6, 0, "gs_6_0", 32, 32, true, true, UINT_MAX),
  160. SM(Kind::Geometry, 6, 1, "gs_6_1", 32, 32, true, true, UINT_MAX),
  161. SM(Kind::Hull, 5, 0, "hs_5_0", 32, 32, true, true, 64),
  162. SM(Kind::Hull, 5, 1, "hs_5_1", 32, 32, true, true, UINT_MAX),
  163. SM(Kind::Hull, 6, 0, "hs_6_0", 32, 32, true, true, UINT_MAX),
  164. SM(Kind::Hull, 6, 1, "hs_6_1", 32, 32, true, true, UINT_MAX),
  165. SM(Kind::Pixel, 4, 0, "ps_4_0", 32, 8, false, false, 0),
  166. SM(Kind::Pixel, 4, 1, "ps_4_1", 32, 8, false, false, 0),
  167. SM(Kind::Pixel, 5, 0, "ps_5_0", 32, 8, true, true, 64),
  168. SM(Kind::Pixel, 5, 1, "ps_5_1", 32, 8, true, true, UINT_MAX),
  169. SM(Kind::Pixel, 6, 0, "ps_6_0", 32, 8, true, true, UINT_MAX),
  170. SM(Kind::Pixel, 6, 1, "ps_6_1", 32, 8, true, true, UINT_MAX),
  171. SM(Kind::Vertex, 4, 0, "vs_4_0", 16, 16, false, false, 0),
  172. SM(Kind::Vertex, 4, 1, "vs_4_1", 32, 32, false, false, 0),
  173. SM(Kind::Vertex, 5, 0, "vs_5_0", 32, 32, true, true, 64),
  174. SM(Kind::Vertex, 5, 1, "vs_5_1", 32, 32, true, true, UINT_MAX),
  175. SM(Kind::Vertex, 6, 0, "vs_6_0", 32, 32, true, true, UINT_MAX),
  176. SM(Kind::Vertex, 6, 1, "vs_6_1", 32, 32, true, true, UINT_MAX),
  177. SM(Kind::Invalid, 0, 0, "invalid", 0, 0, false, false, 0),
  178. };
  179. } // namespace hlsl