HLSLRootSignature.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  1. //===--- HLSLRootSignature.cpp -- HLSL root signature parsing -------------===//
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // HLSLRootSignature.cpp //
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. // This file is distributed under the University of Illinois Open Source //
  7. // License. See LICENSE.TXT for details. //
  8. // //
  9. // This file implements the parser for the root signature mini-language. //
  10. // //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. #include "clang/Basic/Diagnostic.h"
  15. #include "clang/Parse/ParseHLSL.h"
  16. #include "clang/Parse/ParseDiagnostic.h"
  17. #include "dxc/Support/Global.h"
  18. #include "dxc/Support/WinIncludes.h"
  19. #include "dxc/Support/WinFunctions.h"
  20. #include "HLSLRootSignature.h"
  21. #include <float.h>
  22. using namespace llvm;
  23. using namespace hlsl;
  24. // Error codes. These could be useful for localization at some point.
  25. #define ERR_RS_UNEXPECTED_TOKEN 4612
  26. #define ERR_RS_ROOTFLAGS_MORE_THAN_ONCE 4613
  27. #define ERR_RS_CREATION_FAILED 4614
  28. #define ERR_RS_BAD_SM 4615
  29. #define ERR_RS_UNDEFINED_REGISTER 4616
  30. #define ERR_RS_INCORRECT_REGISTER_TYPE 4617
  31. #define ERR_RS_NOT_ALLOWED_FOR_HS_PATCH_CONST 4618
  32. #define ERR_RS_WRONG_ROOT_DESC_FLAG 4619
  33. #define ERR_RS_WRONG_DESC_RANGE_FLAG 4620
  34. #define ERR_RS_LOCAL_FLAG_ON_GLOBAL 4621
  35. #define ERR_RS_BAD_FLOAT 5000
  36. // Non-throwing new
  37. #define NEW new (std::nothrow)
  38. DEFINE_ENUM_FLAG_OPERATORS(DxilRootSignatureFlags)
  39. DEFINE_ENUM_FLAG_OPERATORS(DxilRootDescriptorFlags)
  40. DEFINE_ENUM_FLAG_OPERATORS(DxilDescriptorRangeFlags)
  41. DEFINE_ENUM_FLAG_OPERATORS(DxilRootSignatureCompilationFlags)
  42. RootSignatureTokenizer::RootSignatureTokenizer(const char *pStr, size_t len)
  43. : m_pStrPos(pStr)
  44. , m_pEndPos(pStr + len)
  45. {
  46. m_TokenBufferIdx = 0;
  47. ReadNextToken(m_TokenBufferIdx);
  48. }
  49. RootSignatureTokenizer::RootSignatureTokenizer(const char *pStr)
  50. : m_pStrPos(pStr)
  51. , m_pEndPos(pStr + strlen(pStr))
  52. {
  53. m_TokenBufferIdx = 0;
  54. ReadNextToken(m_TokenBufferIdx);
  55. }
  56. RootSignatureTokenizer::Token
  57. RootSignatureTokenizer::GetToken()
  58. {
  59. uint32_t CurBufferIdx = m_TokenBufferIdx;
  60. m_TokenBufferIdx = (m_TokenBufferIdx + 1) % kNumBuffers;
  61. ReadNextToken(m_TokenBufferIdx);
  62. return m_Tokens[CurBufferIdx];
  63. }
  64. RootSignatureTokenizer::Token
  65. RootSignatureTokenizer::PeekToken()
  66. {
  67. return m_Tokens[m_TokenBufferIdx];
  68. }
  69. bool RootSignatureTokenizer::IsDone() const
  70. {
  71. return m_pStrPos == m_pEndPos;
  72. }
  73. void RootSignatureTokenizer::ReadNextToken(uint32_t BufferIdx)
  74. {
  75. char *pBuffer = m_TokenStrings[BufferIdx];
  76. Token &T = m_Tokens[BufferIdx];
  77. bool bFloat = false;
  78. bool bKW = false;
  79. char c = 0;
  80. EatSpace();
  81. // Identify token
  82. uint32_t TokLen = 0;
  83. char ch;
  84. // Any time m_pStrPos moves, update ch; no null termination assumptions made.
  85. // Test ch, not m_StrPos[0], which may be at end.
  86. #define STRPOS_MOVED() ch = m_pStrPos == m_pEndPos ? '\0' : m_pStrPos[0];
  87. STRPOS_MOVED();
  88. if(IsSeparator(ch))
  89. {
  90. pBuffer[TokLen++] = *m_pStrPos++;
  91. }
  92. else if(IsDigit(ch) || ch == '+' || ch == '-' || ch == '.')
  93. {
  94. if(ch == '+' || ch == '-')
  95. {
  96. pBuffer[TokLen++] = *m_pStrPos++;
  97. STRPOS_MOVED();
  98. }
  99. bool bSeenDigit = false;
  100. while(IsDigit(ch) && TokLen < kMaxTokenLength)
  101. {
  102. pBuffer[TokLen++] = *m_pStrPos++;
  103. STRPOS_MOVED();
  104. bSeenDigit = true;
  105. }
  106. if(ch == '.')
  107. {
  108. bFloat = true;
  109. pBuffer[TokLen++] = *m_pStrPos++;
  110. STRPOS_MOVED();
  111. if(!bSeenDigit && !IsDigit(ch))
  112. {
  113. goto lUnknownToken;
  114. }
  115. while(IsDigit(ch) && TokLen < kMaxTokenLength)
  116. {
  117. pBuffer[TokLen++] = *m_pStrPos++;
  118. STRPOS_MOVED();
  119. bSeenDigit = true;
  120. }
  121. }
  122. if(!bSeenDigit)
  123. {
  124. goto lUnknownToken;
  125. }
  126. if(ch == 'e' || ch == 'E')
  127. {
  128. bFloat = true;
  129. pBuffer[TokLen++] = *m_pStrPos++;
  130. STRPOS_MOVED()
  131. if(ch == '+' || ch == '-')
  132. {
  133. pBuffer[TokLen++] = *m_pStrPos++;
  134. STRPOS_MOVED();
  135. }
  136. if(!IsDigit(ch))
  137. {
  138. goto lUnknownToken;
  139. }
  140. while(IsDigit(ch) && TokLen < kMaxTokenLength)
  141. {
  142. pBuffer[TokLen++] = *m_pStrPos++;
  143. STRPOS_MOVED();
  144. }
  145. }
  146. if(ch == 'f' || ch == 'F')
  147. {
  148. bFloat = true;
  149. pBuffer[TokLen++] = *m_pStrPos++;
  150. STRPOS_MOVED();
  151. }
  152. }
  153. else if(IsAlpha(ch) || ch == '_')
  154. {
  155. while((IsAlpha(ch) || ch == '_' || IsDigit(ch)) && TokLen < kMaxTokenLength)
  156. {
  157. pBuffer[TokLen++] = *m_pStrPos++;
  158. STRPOS_MOVED();
  159. }
  160. }
  161. else
  162. {
  163. while(m_pStrPos < m_pEndPos && TokLen < kMaxTokenLength)
  164. {
  165. pBuffer[TokLen++] = *m_pStrPos++;
  166. STRPOS_MOVED(); // not really needed, but good for consistency
  167. }
  168. }
  169. pBuffer[TokLen] = '\0';
  170. #undef STRPOS_MOVED
  171. //
  172. // Classify token
  173. //
  174. c = pBuffer[0];
  175. // Delimiters
  176. switch(c)
  177. {
  178. case '\0': T = Token(Token::Type::EOL, pBuffer); return;
  179. case ',': T = Token(Token::Type::Comma, pBuffer); return;
  180. case '(': T = Token(Token::Type::LParen, pBuffer); return;
  181. case ')': T = Token(Token::Type::RParen, pBuffer); return;
  182. case '=': T = Token(Token::Type::EQ, pBuffer); return;
  183. case '|': T = Token(Token::Type::OR, pBuffer); return;
  184. }
  185. // Number
  186. if(IsDigit(c) || c == '+' || c == '-' || c == '.')
  187. {
  188. if(bFloat)
  189. {
  190. float v = 0.f;
  191. if(ToFloat(pBuffer, v))
  192. {
  193. T = Token(Token::Type::NumberFloat, pBuffer, v);
  194. return;
  195. }
  196. }
  197. else
  198. {
  199. if(c == '-')
  200. {
  201. int n = 0;
  202. if(ToI32(pBuffer, n))
  203. {
  204. T = Token(Token::Type::NumberI32, pBuffer, (uint32_t)n);
  205. return;
  206. }
  207. }
  208. else
  209. {
  210. uint32_t n = 0;
  211. if(ToU32(pBuffer, n))
  212. {
  213. T = Token(Token::Type::NumberU32, pBuffer, n);
  214. return;
  215. }
  216. }
  217. }
  218. goto lUnknownToken;
  219. }
  220. // Register
  221. if(IsDigit(pBuffer[1]) && (c == 't' || c == 's' || c == 'u' || c == 'b'))
  222. {
  223. if(ToRegister(pBuffer, T))
  224. return;
  225. }
  226. // Keyword
  227. #define KW(__name) ToKeyword(pBuffer, T, #__name, Token::Type::__name)
  228. // Case-incensitive
  229. switch(toupper(c))
  230. {
  231. case 'A':
  232. bKW = KW(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT) ||
  233. KW(ALLOW_STREAM_OUTPUT) ||
  234. KW(addressU) ||
  235. KW(addressV) ||
  236. KW(addressW);
  237. break;
  238. case 'B':
  239. bKW = KW(borderColor);
  240. break;
  241. case 'C':
  242. bKW = KW(CBV) ||
  243. KW(comparisonFunc) ||
  244. KW(COMPARISON_NEVER) ||
  245. KW(COMPARISON_LESS) ||
  246. KW(COMPARISON_EQUAL) ||
  247. KW(COMPARISON_LESS_EQUAL) ||
  248. KW(COMPARISON_GREATER) ||
  249. KW(COMPARISON_NOT_EQUAL) ||
  250. KW(COMPARISON_GREATER_EQUAL) ||
  251. KW(COMPARISON_ALWAYS);
  252. break;
  253. case 'D':
  254. bKW = KW(DescriptorTable) ||
  255. KW(DESCRIPTOR_RANGE_OFFSET_APPEND) ||
  256. KW(DENY_VERTEX_SHADER_ROOT_ACCESS) ||
  257. KW(DENY_HULL_SHADER_ROOT_ACCESS) ||
  258. KW(DENY_DOMAIN_SHADER_ROOT_ACCESS) ||
  259. KW(DENY_GEOMETRY_SHADER_ROOT_ACCESS) ||
  260. KW(DENY_PIXEL_SHADER_ROOT_ACCESS) ||
  261. KW(DESCRIPTORS_VOLATILE) ||
  262. KW(DATA_VOLATILE) ||
  263. KW(DATA_STATIC) ||
  264. KW(DATA_STATIC_WHILE_SET_AT_EXECUTE) ||
  265. KW(DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS);
  266. break;
  267. case 'F':
  268. bKW = KW(flags) ||
  269. KW(filter) ||
  270. KW(FILTER_MIN_MAG_MIP_POINT) ||
  271. KW(FILTER_MIN_MAG_POINT_MIP_LINEAR) ||
  272. KW(FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT) ||
  273. KW(FILTER_MIN_POINT_MAG_MIP_LINEAR) ||
  274. KW(FILTER_MIN_LINEAR_MAG_MIP_POINT) ||
  275. KW(FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR) ||
  276. KW(FILTER_MIN_MAG_LINEAR_MIP_POINT) ||
  277. KW(FILTER_MIN_MAG_MIP_LINEAR) ||
  278. KW(FILTER_ANISOTROPIC) ||
  279. KW(FILTER_COMPARISON_MIN_MAG_MIP_POINT) ||
  280. KW(FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR) ||
  281. KW(FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT) ||
  282. KW(FILTER_COMPARISON_MIN_POINT_MAG_MIP_LINEAR) ||
  283. KW(FILTER_COMPARISON_MIN_LINEAR_MAG_MIP_POINT) ||
  284. KW(FILTER_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR) ||
  285. KW(FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT) ||
  286. KW(FILTER_COMPARISON_MIN_MAG_MIP_LINEAR) ||
  287. KW(FILTER_COMPARISON_ANISOTROPIC) ||
  288. KW(FILTER_MINIMUM_MIN_MAG_MIP_POINT) ||
  289. KW(FILTER_MINIMUM_MIN_MAG_POINT_MIP_LINEAR) ||
  290. KW(FILTER_MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT) ||
  291. KW(FILTER_MINIMUM_MIN_POINT_MAG_MIP_LINEAR) ||
  292. KW(FILTER_MINIMUM_MIN_LINEAR_MAG_MIP_POINT) ||
  293. KW(FILTER_MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR) ||
  294. KW(FILTER_MINIMUM_MIN_MAG_LINEAR_MIP_POINT) ||
  295. KW(FILTER_MINIMUM_MIN_MAG_MIP_LINEAR) ||
  296. KW(FILTER_MINIMUM_ANISOTROPIC) ||
  297. KW(FILTER_MAXIMUM_MIN_MAG_MIP_POINT) ||
  298. KW(FILTER_MAXIMUM_MIN_MAG_POINT_MIP_LINEAR) ||
  299. KW(FILTER_MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT) ||
  300. KW(FILTER_MAXIMUM_MIN_POINT_MAG_MIP_LINEAR) ||
  301. KW(FILTER_MAXIMUM_MIN_LINEAR_MAG_MIP_POINT) ||
  302. KW(FILTER_MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR) ||
  303. KW(FILTER_MAXIMUM_MIN_MAG_LINEAR_MIP_POINT) ||
  304. KW(FILTER_MAXIMUM_MIN_MAG_MIP_LINEAR) ||
  305. KW(FILTER_MAXIMUM_ANISOTROPIC);
  306. break;
  307. case 'L':
  308. bKW = KW(LOCAL_ROOT_SIGNATURE);
  309. break;
  310. case 'M':
  311. bKW = KW(maxAnisotropy) || KW(mipLODBias) || KW(minLOD) || KW(maxLOD);
  312. break;
  313. case 'N':
  314. bKW = KW(numDescriptors) || KW(num32BitConstants);
  315. break;
  316. case 'O':
  317. bKW = KW(offset);
  318. break;
  319. case 'R':
  320. bKW = KW(RootFlags) || KW(RootConstants);
  321. break;
  322. case 'S':
  323. bKW = KW(space) || KW(Sampler) || KW(StaticSampler) || KW(SRV) ||
  324. KW(SHADER_VISIBILITY_ALL) || KW(SHADER_VISIBILITY_VERTEX) ||
  325. KW(SHADER_VISIBILITY_HULL) || KW(SHADER_VISIBILITY_DOMAIN) ||
  326. KW(SHADER_VISIBILITY_GEOMETRY) || KW(SHADER_VISIBILITY_PIXEL) ||
  327. KW(STATIC_BORDER_COLOR_TRANSPARENT_BLACK) ||
  328. KW(STATIC_BORDER_COLOR_OPAQUE_BLACK) ||
  329. KW(STATIC_BORDER_COLOR_OPAQUE_WHITE);
  330. break;
  331. case 'T':
  332. bKW = KW(TEXTURE_ADDRESS_WRAP) ||
  333. KW(TEXTURE_ADDRESS_MIRROR) ||
  334. KW(TEXTURE_ADDRESS_CLAMP) ||
  335. KW(TEXTURE_ADDRESS_BORDER) ||
  336. KW(TEXTURE_ADDRESS_MIRROR_ONCE);
  337. break;
  338. case 'U':
  339. bKW = KW(unbounded) || KW(UAV);
  340. break;
  341. case 'V':
  342. bKW = KW(visibility);
  343. break;
  344. }
  345. #undef KW
  346. if(!bKW) goto lUnknownToken;
  347. return;
  348. lUnknownToken:
  349. T = Token(Token::Type::Unknown, pBuffer);
  350. }
  351. void RootSignatureTokenizer::EatSpace()
  352. {
  353. while(m_pStrPos < m_pEndPos && *m_pStrPos == ' ')
  354. m_pStrPos++;
  355. }
  356. bool RootSignatureTokenizer::ToI32(LPCSTR pBuf, int & n)
  357. {
  358. if(pBuf[0] == '\0') return false;
  359. long long N = _atoi64(pBuf);
  360. if(N > INT_MAX || N < INT_MIN) return false;
  361. n = static_cast<int>(N);
  362. return true;
  363. }
  364. bool RootSignatureTokenizer::ToU32(LPCSTR pBuf, uint32_t & n)
  365. {
  366. if(pBuf[0] == '\0') return false;
  367. long long N = _atoi64(pBuf);
  368. if(N > UINT32_MAX || N < 0) return false;
  369. n = static_cast<uint32_t>(N);
  370. return true;
  371. }
  372. bool RootSignatureTokenizer::ToFloat(LPCSTR pBuf, float & n)
  373. {
  374. if(pBuf[0] == '\0') return false;
  375. errno = 0;
  376. double N = strtod(pBuf, NULL);
  377. if (errno == ERANGE || (N > FLT_MAX || N < -FLT_MAX)) {
  378. return false;
  379. }
  380. n = static_cast<float>(N);
  381. return true;
  382. }
  383. bool RootSignatureTokenizer::ToRegister(LPCSTR pBuf, Token & T)
  384. {
  385. uint32_t n;
  386. if(ToU32(&pBuf[1], n))
  387. {
  388. switch(pBuf[0])
  389. {
  390. case 't': T = Token(Token::Type::TReg, pBuf, n); return true;
  391. case 's': T = Token(Token::Type::SReg, pBuf, n); return true;
  392. case 'u': T = Token(Token::Type::UReg, pBuf, n); return true;
  393. case 'b': T = Token(Token::Type::BReg, pBuf, n); return true;
  394. }
  395. }
  396. return false;
  397. }
  398. bool RootSignatureTokenizer::ToKeyword(LPCSTR pBuf, Token & T, const char *pKeyword, Token::Type Type)
  399. {
  400. // Tokens are case-insencitive to allow more flexibility for programmers
  401. if(_stricmp(pBuf, pKeyword) == 0)
  402. {
  403. T = Token(Type, pBuf);
  404. return true;
  405. }
  406. else
  407. {
  408. T = Token(Token::Type::Unknown, pBuf);
  409. return false;
  410. }
  411. }
  412. bool RootSignatureTokenizer::IsSeparator(char c) const
  413. {
  414. return (c == ',' || c == '=' || c == '|' || c == '(' || c == ')' || c == ' ' || c == '\t' || c == '\n');
  415. }
  416. bool RootSignatureTokenizer::IsDigit(char c) const
  417. {
  418. return isdigit(c) > 0;
  419. }
  420. bool RootSignatureTokenizer::IsAlpha(char c) const
  421. {
  422. return isalpha(c) > 0;
  423. }
  424. RootSignatureParser::RootSignatureParser(
  425. RootSignatureTokenizer *pTokenizer, DxilRootSignatureVersion DefaultVersion,
  426. DxilRootSignatureCompilationFlags CompilationFlags, llvm::raw_ostream &OS)
  427. : m_pTokenizer(pTokenizer), m_Version(DefaultVersion), m_CompilationFlags(CompilationFlags), m_OS(OS) {}
  428. HRESULT RootSignatureParser::Parse(DxilVersionedRootSignatureDesc **ppRootSignature)
  429. {
  430. HRESULT hr = S_OK;
  431. DxilVersionedRootSignatureDesc *pRS = NULL;
  432. DXASSERT(!((bool)(m_CompilationFlags & DxilRootSignatureCompilationFlags::GlobalRootSignature) &&
  433. (bool)(m_CompilationFlags & DxilRootSignatureCompilationFlags::LocalRootSignature)),
  434. "global and local cannot be both set");
  435. if(ppRootSignature != NULL)
  436. {
  437. IFC(ParseRootSignature(&pRS));
  438. *ppRootSignature = pRS;
  439. }
  440. Cleanup:
  441. return hr;
  442. }
  443. HRESULT RootSignatureParser::GetAndMatchToken(TokenType & Token, TokenType::Type Type)
  444. {
  445. HRESULT hr = S_OK;
  446. Token = m_pTokenizer->GetToken();
  447. if(Token.GetType() != Type)
  448. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s'", Token.GetStr()));
  449. Cleanup:
  450. return hr;
  451. }
  452. HRESULT RootSignatureParser::Error(uint32_t uErrorNum, LPCSTR pError, ...)
  453. {
  454. va_list Args;
  455. char msg[512];
  456. va_start(Args, pError);
  457. vsprintf_s(msg, pError, Args);
  458. va_end(Args);
  459. try {
  460. m_OS << msg;
  461. }
  462. CATCH_CPP_RETURN_HRESULT();
  463. return E_FAIL;
  464. }
  465. HRESULT RootSignatureParser::ParseRootSignature(DxilVersionedRootSignatureDesc **ppRootSignature)
  466. {
  467. HRESULT hr = S_OK;
  468. TokenType Token;
  469. bool bSeenFlags = false;
  470. SmallVector<DxilRootParameter1, 8> RSParameters;
  471. DxilRootParameter1 *pRSParams = NULL;
  472. SmallVector<DxilStaticSamplerDesc, 8> StaticSamplers;
  473. DxilStaticSamplerDesc *pStaticSamplers = NULL;
  474. DxilVersionedRootSignatureDesc *pRS = NULL;
  475. *ppRootSignature = NULL;
  476. IFCOOM(pRS = NEW DxilVersionedRootSignatureDesc);
  477. // Always parse root signature string to the latest version.
  478. pRS->Version = DxilRootSignatureVersion::Version_1_1;
  479. memset(&pRS->Desc_1_1, 0, sizeof(pRS->Desc_1_1));
  480. Token = m_pTokenizer->PeekToken();
  481. while(Token.GetType() != TokenType::EOL)
  482. {
  483. switch(Token.GetType())
  484. {
  485. case TokenType::RootFlags:
  486. if(!bSeenFlags)
  487. {
  488. IFC(ParseRootSignatureFlags(pRS->Desc_1_1.Flags));
  489. bSeenFlags = true;
  490. }
  491. else
  492. {
  493. IFC(Error(ERR_RS_ROOTFLAGS_MORE_THAN_ONCE, "RootFlags cannot be specified more than once"));
  494. }
  495. break;
  496. case TokenType::RootConstants:
  497. {
  498. DxilRootParameter1 P;
  499. IFC(ParseRootConstants(P));
  500. RSParameters.push_back(P);
  501. break;
  502. }
  503. case TokenType::CBV:
  504. {
  505. DxilRootParameter1 P;
  506. IFC(ParseRootShaderResource(Token.GetType(), TokenType::BReg, DxilRootParameterType::CBV, P));
  507. RSParameters.push_back(P);
  508. break;
  509. }
  510. case TokenType::SRV:
  511. {
  512. DxilRootParameter1 P;
  513. IFC(ParseRootShaderResource(Token.GetType(), TokenType::TReg, DxilRootParameterType::SRV, P));
  514. RSParameters.push_back(P);
  515. break;
  516. }
  517. case TokenType::UAV:
  518. {
  519. DxilRootParameter1 P;
  520. IFC(ParseRootShaderResource(Token.GetType(), TokenType::UReg, DxilRootParameterType::UAV, P));
  521. RSParameters.push_back(P);
  522. break;
  523. }
  524. case TokenType::DescriptorTable:
  525. {
  526. DxilRootParameter1 P;
  527. IFC(ParseRootDescriptorTable(P));
  528. RSParameters.push_back(P);
  529. break;
  530. }
  531. case TokenType::StaticSampler:
  532. {
  533. DxilStaticSamplerDesc P;
  534. IFC(ParseStaticSampler(P));
  535. StaticSamplers.push_back(P);
  536. break;
  537. }
  538. default:
  539. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s' when parsing root signature", Token.GetStr()));
  540. }
  541. Token = m_pTokenizer->GetToken();
  542. if(Token.GetType() == TokenType::EOL)
  543. break;
  544. // Consume ','
  545. if(Token.GetType() != TokenType::Comma)
  546. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Expected ',', found: '%s'", Token.GetStr()));
  547. Token = m_pTokenizer->PeekToken();
  548. }
  549. if(RSParameters.size() > 0)
  550. {
  551. IFCOOM(pRSParams = NEW DxilRootParameter1[RSParameters.size()]);
  552. pRS->Desc_1_1.NumParameters = RSParameters.size();
  553. memcpy(pRSParams, RSParameters.data(), pRS->Desc_1_1.NumParameters*sizeof(DxilRootParameter1));
  554. pRS->Desc_1_1.pParameters = pRSParams;
  555. }
  556. if(StaticSamplers.size() > 0)
  557. {
  558. IFCOOM(pStaticSamplers = NEW DxilStaticSamplerDesc[StaticSamplers.size()]);
  559. pRS->Desc_1_1.NumStaticSamplers = StaticSamplers.size();
  560. memcpy(pStaticSamplers, StaticSamplers.data(), pRS->Desc_1_1.NumStaticSamplers*sizeof(DxilStaticSamplerDesc));
  561. pRS->Desc_1_1.pStaticSamplers = pStaticSamplers;
  562. }
  563. // Set local signature flag if not already on
  564. if ((bool)(m_CompilationFlags & DxilRootSignatureCompilationFlags::LocalRootSignature))
  565. pRS->Desc_1_1.Flags |= DxilRootSignatureFlags::LocalRootSignature;
  566. // Down-convert root signature to the right version, if needed.
  567. if(pRS->Version != m_Version)
  568. {
  569. DxilVersionedRootSignatureDesc *pRS1 = NULL;
  570. try {
  571. hlsl::ConvertRootSignature(pRS, m_Version, (const DxilVersionedRootSignatureDesc **)&pRS1);
  572. }
  573. CATCH_CPP_ASSIGN_HRESULT();
  574. IFC(hr);
  575. hlsl::DeleteRootSignature(pRS);
  576. pRS = pRS1;
  577. }
  578. *ppRootSignature = pRS;
  579. Cleanup:
  580. if(FAILED(hr))
  581. {
  582. hlsl::DeleteRootSignature(pRS);
  583. }
  584. return hr;
  585. }
  586. HRESULT RootSignatureParser::ParseRootSignatureFlags(DxilRootSignatureFlags & Flags)
  587. {
  588. //RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT | DENY_VERTEX_SHADER_ROOT_ACCESS)
  589. // ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT
  590. // DENY_VERTEX_SHADER_ROOT_ACCESS
  591. // DENY_HULL_SHADER_ROOT_ACCESS
  592. // DENY_DOMAIN_SHADER_ROOT_ACCESS
  593. // DENY_GEOMETRY_SHADER_ROOT_ACCESS
  594. // DENY_PIXEL_SHADER_ROOT_ACCESS
  595. // ALLOW_STREAM_OUTPUT
  596. // LOCAL_ROOT_SIGNATURE
  597. HRESULT hr = S_OK;
  598. TokenType Token;
  599. IFC(GetAndMatchToken(Token, TokenType::RootFlags));
  600. IFC(GetAndMatchToken(Token, TokenType::LParen));
  601. Flags = DxilRootSignatureFlags::None;
  602. Token = m_pTokenizer->PeekToken();
  603. if(Token.GetType() == TokenType::NumberU32)
  604. {
  605. IFC(GetAndMatchToken(Token, TokenType::NumberU32));
  606. uint32_t n = Token.GetU32Value();
  607. if(n != 0)
  608. {
  609. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Root signature flag values can only be 0 or flag enum values, found: '%s'", Token.GetStr()));
  610. }
  611. }
  612. else
  613. {
  614. for(;;)
  615. {
  616. Token = m_pTokenizer->GetToken();
  617. switch(Token.GetType())
  618. {
  619. case TokenType::ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT:
  620. Flags |= DxilRootSignatureFlags::AllowInputAssemblerInputLayout;
  621. break;
  622. case TokenType::DENY_VERTEX_SHADER_ROOT_ACCESS:
  623. Flags |= DxilRootSignatureFlags::DenyVertexShaderRootAccess;
  624. break;
  625. case TokenType::DENY_HULL_SHADER_ROOT_ACCESS:
  626. Flags |= DxilRootSignatureFlags::DenyHullShaderRootAccess;
  627. break;
  628. case TokenType::DENY_DOMAIN_SHADER_ROOT_ACCESS:
  629. Flags |= DxilRootSignatureFlags::DenyDomainShaderRootAccess;
  630. break;
  631. case TokenType::DENY_GEOMETRY_SHADER_ROOT_ACCESS:
  632. Flags |= DxilRootSignatureFlags::DenyGeometryShaderRootAccess;
  633. break;
  634. case TokenType::DENY_PIXEL_SHADER_ROOT_ACCESS:
  635. Flags |= DxilRootSignatureFlags::DenyPixelShaderRootAccess;
  636. break;
  637. case TokenType::ALLOW_STREAM_OUTPUT:
  638. Flags |= DxilRootSignatureFlags::AllowStreamOutput;
  639. break;
  640. case TokenType::LOCAL_ROOT_SIGNATURE:
  641. if ((bool)(m_CompilationFlags & DxilRootSignatureCompilationFlags::GlobalRootSignature))
  642. IFC(Error(ERR_RS_LOCAL_FLAG_ON_GLOBAL, "LOCAL_ROOT_SIGNATURE flag used in global root signature"));
  643. Flags |= DxilRootSignatureFlags::LocalRootSignature;
  644. break;
  645. default:
  646. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Expected a root signature flag value, found: '%s'", Token.GetStr()));
  647. }
  648. Token = m_pTokenizer->PeekToken();
  649. if(Token.GetType() == TokenType::RParen)
  650. break;
  651. IFC(GetAndMatchToken(Token, TokenType::OR));
  652. }
  653. }
  654. IFC(GetAndMatchToken(Token, TokenType::RParen));
  655. Cleanup:
  656. return hr;
  657. }
  658. HRESULT RootSignatureParser::ParseRootConstants(DxilRootParameter1 &P)
  659. {
  660. //"RootConstants(num32BitConstants=3, b2 [, space=1, visibility=SHADER_VISIBILITY_ALL ] ), "
  661. HRESULT hr = S_OK;
  662. TokenType Token;
  663. memset(&P, 0, sizeof(P));
  664. DXASSERT(P.ShaderVisibility == DxilShaderVisibility::All, "else default isn't zero");
  665. P.ParameterType = DxilRootParameterType::Constants32Bit;
  666. bool bSeenNum32BitConstants = false;
  667. bool bSeenBReg = false;
  668. bool bSeenSpace = false;
  669. bool bSeenVisibility = false;
  670. IFC(GetAndMatchToken(Token, TokenType::RootConstants));
  671. IFC(GetAndMatchToken(Token, TokenType::LParen));
  672. for(;;)
  673. {
  674. Token = m_pTokenizer->PeekToken();
  675. switch(Token.GetType())
  676. {
  677. case TokenType::num32BitConstants:
  678. IFC(MarkParameter(bSeenNum32BitConstants, "num32BitConstants"));
  679. IFC(ParseNum32BitConstants(P.Constants.Num32BitValues));
  680. break;
  681. case TokenType::BReg:
  682. IFC(MarkParameter(bSeenBReg, "cbuffer register b#"));
  683. IFC(ParseRegister(TokenType::BReg, P.Constants.ShaderRegister));
  684. break;
  685. case TokenType::space:
  686. IFC(MarkParameter(bSeenSpace, "space"));
  687. IFC(ParseSpace(P.Constants.RegisterSpace));
  688. break;
  689. case TokenType::visibility:
  690. IFC(MarkParameter(bSeenVisibility, "visibility"));
  691. IFC(ParseVisibility(P.ShaderVisibility));
  692. break;
  693. default:
  694. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s'", Token.GetStr()));
  695. break;
  696. }
  697. Token = m_pTokenizer->GetToken();
  698. if(Token.GetType() == TokenType::RParen)
  699. break;
  700. else if(Token.GetType() != TokenType::Comma)
  701. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s'", Token.GetStr()));
  702. }
  703. if(!bSeenNum32BitConstants)
  704. {
  705. IFC(Error(ERR_RS_UNDEFINED_REGISTER, "num32BitConstants must be defined for each RootConstants"));
  706. }
  707. if(!bSeenBReg)
  708. {
  709. IFC(Error(ERR_RS_UNDEFINED_REGISTER, "Constant buffer register b# must be defined for each RootConstants"));
  710. }
  711. Cleanup:
  712. return hr;
  713. }
  714. HRESULT RootSignatureParser::ParseRootShaderResource(TokenType::Type TokType,
  715. TokenType::Type RegType,
  716. DxilRootParameterType ResType,
  717. DxilRootParameter1 &P)
  718. {
  719. // CBV(b0 [, space=3, flags=0, visibility=VISIBILITY_ALL] )
  720. HRESULT hr = S_OK;
  721. TokenType Token;
  722. P.ParameterType = ResType;
  723. P.ShaderVisibility = DxilShaderVisibility::All;
  724. P.Descriptor.ShaderRegister = 0;
  725. P.Descriptor.RegisterSpace = 0;
  726. P.Descriptor.Flags = DxilRootDescriptorFlags::None;
  727. bool bSeenReg = false;
  728. bool bSeenFlags = false;
  729. bool bSeenSpace = false;
  730. bool bSeenVisibility = false;
  731. IFC(GetAndMatchToken(Token, TokType));
  732. IFC(GetAndMatchToken(Token, TokenType::LParen));
  733. for(;;)
  734. {
  735. Token = m_pTokenizer->PeekToken();
  736. switch(Token.GetType())
  737. {
  738. case TokenType::BReg:
  739. case TokenType::TReg:
  740. case TokenType::UReg:
  741. IFC(MarkParameter(bSeenReg, "shader register"));
  742. IFC(ParseRegister(RegType, P.Descriptor.ShaderRegister));
  743. break;
  744. case TokenType::flags:
  745. IFC(MarkParameter(bSeenFlags, "flags"));
  746. IFC(ParseRootDescFlags(P.Descriptor.Flags));
  747. break;
  748. case TokenType::space:
  749. IFC(MarkParameter(bSeenSpace, "space"));
  750. IFC(ParseSpace(P.Descriptor.RegisterSpace));
  751. break;
  752. case TokenType::visibility:
  753. IFC(MarkParameter(bSeenVisibility, "visibility"));
  754. IFC(ParseVisibility(P.ShaderVisibility));
  755. break;
  756. default:
  757. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s'", Token.GetStr()));
  758. break;
  759. }
  760. Token = m_pTokenizer->GetToken();
  761. if(Token.GetType() == TokenType::RParen)
  762. break;
  763. else if(Token.GetType() != TokenType::Comma)
  764. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s'", Token.GetStr()));
  765. }
  766. if(!bSeenReg)
  767. {
  768. IFC(Error(ERR_RS_UNDEFINED_REGISTER, "shader register must be defined for each CBV/SRV/UAV"));
  769. }
  770. Cleanup:
  771. return hr;
  772. }
  773. HRESULT RootSignatureParser::ParseRootDescriptorTable(DxilRootParameter1 &P)
  774. {
  775. //DescriptorTable( SRV(t2, numDescriptors = 6), UAV(u0, numDescriptors = 4) [, visibility = SHADER_VISIBILITY_ALL ] )
  776. HRESULT hr = S_OK;
  777. TokenType Token;
  778. memset(&P, 0, sizeof(P));
  779. DXASSERT(P.ShaderVisibility == DxilShaderVisibility::All, "else default isn't zero");
  780. P.ParameterType = DxilRootParameterType::DescriptorTable;
  781. bool bSeenVisibility = false;
  782. DxilDescriptorRange1 * pDescs = NULL;
  783. SmallVector<DxilDescriptorRange1, 4> Ranges;
  784. IFC(GetAndMatchToken(Token, TokenType::DescriptorTable));
  785. IFC(GetAndMatchToken(Token, TokenType::LParen));
  786. for(;;)
  787. {
  788. Token = m_pTokenizer->PeekToken();
  789. switch(Token.GetType())
  790. {
  791. case TokenType::CBV:
  792. {
  793. DxilDescriptorRange1 R;
  794. IFC(ParseDescTableResource(Token.GetType(), TokenType::BReg, DxilDescriptorRangeType::CBV, R));
  795. Ranges.push_back(R);
  796. break;
  797. }
  798. case TokenType::SRV:
  799. {
  800. DxilDescriptorRange1 R;
  801. IFC(ParseDescTableResource(Token.GetType(), TokenType::TReg, DxilDescriptorRangeType::SRV, R));
  802. Ranges.push_back(R);
  803. break;
  804. }
  805. case TokenType::UAV:
  806. {
  807. DxilDescriptorRange1 R;
  808. IFC(ParseDescTableResource(Token.GetType(), TokenType::UReg, DxilDescriptorRangeType::UAV, R));
  809. Ranges.push_back(R);
  810. break;
  811. }
  812. case TokenType::Sampler:
  813. {
  814. DxilDescriptorRange1 R;
  815. IFC(ParseDescTableResource(Token.GetType(), TokenType::SReg, DxilDescriptorRangeType::Sampler, R));
  816. Ranges.push_back(R);
  817. break;
  818. }
  819. case TokenType::visibility:
  820. IFC(MarkParameter(bSeenVisibility, "visibility"));
  821. IFC(ParseVisibility(P.ShaderVisibility));
  822. break;
  823. default:
  824. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s'", Token.GetStr()));
  825. break;
  826. }
  827. Token = m_pTokenizer->GetToken();
  828. if(Token.GetType() == TokenType::RParen)
  829. break;
  830. else if(Token.GetType() != TokenType::Comma)
  831. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s'", Token.GetStr()));
  832. }
  833. if(Ranges.size() > 0)
  834. {
  835. IFCOOM(pDescs = NEW DxilDescriptorRange1[Ranges.size()]);
  836. for(uint32_t i = 0; i < Ranges.size(); i++)
  837. {
  838. pDescs[i] = Ranges[i];
  839. }
  840. P.DescriptorTable.pDescriptorRanges = pDescs;
  841. P.DescriptorTable.NumDescriptorRanges = Ranges.size();
  842. }
  843. Cleanup:
  844. if(FAILED(hr))
  845. {
  846. delete[] pDescs;
  847. }
  848. return hr;
  849. }
  850. HRESULT RootSignatureParser::ParseDescTableResource(TokenType::Type TokType,
  851. TokenType::Type RegType,
  852. DxilDescriptorRangeType RangeType,
  853. DxilDescriptorRange1 &R)
  854. {
  855. HRESULT hr = S_OK;
  856. TokenType Token;
  857. // CBV(b0 [, numDescriptors = 1, space=0, flags=0, offset = DESCRIPTOR_RANGE_OFFSET_APPEND] )
  858. R.RangeType = RangeType;
  859. R.NumDescriptors = 1;
  860. R.BaseShaderRegister = 0;
  861. R.RegisterSpace = 0;
  862. R.Flags = DxilDescriptorRangeFlags::None;
  863. R.OffsetInDescriptorsFromTableStart = DxilDescriptorRangeOffsetAppend;
  864. bool bSeenReg = false;
  865. bool bSeenNumDescriptors = false;
  866. bool bSeenSpace = false;
  867. bool bSeenFlags = false;
  868. bool bSeenOffset = false;
  869. IFC(GetAndMatchToken(Token, TokType));
  870. IFC(GetAndMatchToken(Token, TokenType::LParen));
  871. for(;;)
  872. {
  873. Token = m_pTokenizer->PeekToken();
  874. switch(Token.GetType())
  875. {
  876. case TokenType::BReg:
  877. case TokenType::TReg:
  878. case TokenType::UReg:
  879. case TokenType::SReg:
  880. IFC(MarkParameter(bSeenReg, "shader register"));
  881. IFC(ParseRegister(RegType, R.BaseShaderRegister));
  882. break;
  883. case TokenType::numDescriptors:
  884. IFC(MarkParameter(bSeenNumDescriptors, "numDescriptors"));
  885. IFC(ParseNumDescriptors(R.NumDescriptors));
  886. break;
  887. case TokenType::space:
  888. IFC(MarkParameter(bSeenSpace, "space"));
  889. IFC(ParseSpace(R.RegisterSpace));
  890. break;
  891. case TokenType::flags:
  892. IFC(MarkParameter(bSeenFlags, "flags"));
  893. IFC(ParseDescRangeFlags(RangeType, R.Flags));
  894. break;
  895. case TokenType::offset:
  896. IFC(MarkParameter(bSeenOffset, "offset"));
  897. IFC(ParseOffset(R.OffsetInDescriptorsFromTableStart));
  898. break;
  899. default:
  900. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s'", Token.GetStr()));
  901. break;
  902. }
  903. Token = m_pTokenizer->GetToken();
  904. if(Token.GetType() == TokenType::RParen)
  905. break;
  906. else if(Token.GetType() != TokenType::Comma)
  907. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s'", Token.GetStr()));
  908. }
  909. if(!bSeenReg)
  910. {
  911. IFC(Error(ERR_RS_UNDEFINED_REGISTER, "shader register must be defined for each CBV/SRV/UAV"));
  912. }
  913. Cleanup:
  914. return hr;
  915. }
  916. HRESULT RootSignatureParser::ParseRegister(TokenType::Type RegType, uint32_t & Reg)
  917. {
  918. HRESULT hr = S_OK;
  919. TokenType Token = m_pTokenizer->PeekToken();
  920. switch(Token.GetType())
  921. {
  922. case TokenType::BReg:
  923. IFC(GetAndMatchToken(Token, TokenType::BReg));
  924. break;
  925. case TokenType::TReg:
  926. IFC(GetAndMatchToken(Token, TokenType::TReg));
  927. break;
  928. case TokenType::UReg:
  929. IFC(GetAndMatchToken(Token, TokenType::UReg));
  930. break;
  931. case TokenType::SReg:
  932. IFC(GetAndMatchToken(Token, TokenType::SReg));
  933. break;
  934. default:
  935. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Expected a register token (CBV, SRV, UAV, Sampler), found: '%s'", Token.GetStr()));
  936. }
  937. if(Token.GetType() != RegType)
  938. {
  939. switch(RegType)
  940. {
  941. case TokenType::BReg:
  942. IFC(Error(ERR_RS_INCORRECT_REGISTER_TYPE, "Incorrect register type '%s' in CBV (expected b#)", Token.GetStr()));
  943. break;
  944. case TokenType::TReg:
  945. IFC(Error(ERR_RS_INCORRECT_REGISTER_TYPE, "Incorrect register type '%s' in SRV (expected t#)", Token.GetStr()));
  946. break;
  947. case TokenType::UReg:
  948. IFC(Error(ERR_RS_INCORRECT_REGISTER_TYPE, "Incorrect register type '%s' in UAV (expected u#)", Token.GetStr()));
  949. break;
  950. case TokenType::SReg:
  951. IFC(Error(ERR_RS_INCORRECT_REGISTER_TYPE, "Incorrect register type '%s' in Sampler/StaticSampler (expected s#)", Token.GetStr()));
  952. break;
  953. default:
  954. // Only Register types are relevant.
  955. break;
  956. }
  957. }
  958. Reg = Token.GetU32Value();
  959. Cleanup:
  960. return hr;
  961. }
  962. HRESULT RootSignatureParser::ParseSpace(uint32_t & Space)
  963. {
  964. HRESULT hr = S_OK;
  965. TokenType Token;
  966. IFC(GetAndMatchToken(Token, TokenType::space));
  967. IFC(GetAndMatchToken(Token, TokenType::EQ));
  968. IFC(GetAndMatchToken(Token, TokenType::NumberU32));
  969. Space = Token.GetU32Value();
  970. Cleanup:
  971. return hr;
  972. }
  973. HRESULT RootSignatureParser::ParseNumDescriptors(uint32_t & NumDescriptors)
  974. {
  975. HRESULT hr = S_OK;
  976. TokenType Token;
  977. IFC(GetAndMatchToken(Token, TokenType::numDescriptors));
  978. IFC(GetAndMatchToken(Token, TokenType::EQ));
  979. Token = m_pTokenizer->PeekToken();
  980. if(Token.GetType() == TokenType::unbounded)
  981. {
  982. IFC(GetAndMatchToken(Token, TokenType::unbounded));
  983. NumDescriptors = UINT32_MAX;
  984. }
  985. else
  986. {
  987. IFC(GetAndMatchToken(Token, TokenType::NumberU32));
  988. NumDescriptors = Token.GetU32Value();
  989. }
  990. Cleanup:
  991. return hr;
  992. }
  993. HRESULT RootSignatureParser::ParseRootDescFlags(DxilRootDescriptorFlags &Flags)
  994. {
  995. // flags=DATA_VOLATILE | DATA_STATIC | DATA_STATIC_WHILE_SET_AT_EXECUTE
  996. HRESULT hr = S_OK;
  997. TokenType Token;
  998. if (m_Version == DxilRootSignatureVersion::Version_1_0)
  999. {
  1000. IFC(Error(ERR_RS_WRONG_ROOT_DESC_FLAG, "Root descriptor flags cannot be specified for root_sig_1_0"));
  1001. }
  1002. IFC(GetAndMatchToken(Token, TokenType::flags));
  1003. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1004. Flags = DxilRootDescriptorFlags::None;
  1005. Token = m_pTokenizer->PeekToken();
  1006. if(Token.GetType() == TokenType::NumberU32)
  1007. {
  1008. IFC(GetAndMatchToken(Token, TokenType::NumberU32));
  1009. uint32_t n = Token.GetU32Value();
  1010. if(n != 0)
  1011. {
  1012. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Root descriptor flag values can only be 0 or flag enum values, found: '%s'", Token.GetStr()));
  1013. }
  1014. }
  1015. else
  1016. {
  1017. for(;;)
  1018. {
  1019. Token = m_pTokenizer->GetToken();
  1020. switch(Token.GetType())
  1021. {
  1022. case TokenType::DATA_VOLATILE:
  1023. Flags |= DxilRootDescriptorFlags::DataVolatile;
  1024. break;
  1025. case TokenType::DATA_STATIC:
  1026. Flags |= DxilRootDescriptorFlags::DataStatic;
  1027. break;
  1028. case TokenType::DATA_STATIC_WHILE_SET_AT_EXECUTE:
  1029. Flags |= DxilRootDescriptorFlags::DataStaticWhileSetAtExecute;
  1030. break;
  1031. default:
  1032. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Expected a root descriptor flag value, found: '%s'", Token.GetStr()));
  1033. }
  1034. Token = m_pTokenizer->PeekToken();
  1035. if(Token.GetType() == TokenType::RParen || Token.GetType() == TokenType::Comma)
  1036. break;
  1037. IFC(GetAndMatchToken(Token, TokenType::OR));
  1038. }
  1039. }
  1040. Cleanup:
  1041. return hr;
  1042. }
  1043. HRESULT RootSignatureParser::ParseDescRangeFlags(DxilDescriptorRangeType,
  1044. DxilDescriptorRangeFlags & Flags)
  1045. {
  1046. // flags=DESCRIPTORS_VOLATILE | DATA_VOLATILE | DATA_STATIC | DATA_STATIC_WHILE_SET_AT_EXECUTE | DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS
  1047. HRESULT hr = S_OK;
  1048. TokenType Token;
  1049. if (m_Version == DxilRootSignatureVersion::Version_1_0)
  1050. {
  1051. IFC(Error(ERR_RS_WRONG_DESC_RANGE_FLAG, "Descriptor range flags cannot be specified for root_sig_1_0"));
  1052. }
  1053. IFC(GetAndMatchToken(Token, TokenType::flags));
  1054. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1055. Flags = DxilDescriptorRangeFlags::None;
  1056. Token = m_pTokenizer->PeekToken();
  1057. if(Token.GetType() == TokenType::NumberU32)
  1058. {
  1059. IFC(GetAndMatchToken(Token, TokenType::NumberU32));
  1060. uint32_t n = Token.GetU32Value();
  1061. if(n != 0)
  1062. {
  1063. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Descriptor range flag values can only be 0 or flag enum values, found: '%s'", Token.GetStr()));
  1064. }
  1065. }
  1066. else
  1067. {
  1068. for(;;)
  1069. {
  1070. Token = m_pTokenizer->GetToken();
  1071. switch(Token.GetType())
  1072. {
  1073. case TokenType::DESCRIPTORS_VOLATILE:
  1074. Flags |= DxilDescriptorRangeFlags::DescriptorsVolatile;
  1075. break;
  1076. case TokenType::DATA_VOLATILE:
  1077. Flags |= DxilDescriptorRangeFlags::DataVolatile;
  1078. break;
  1079. case TokenType::DATA_STATIC:
  1080. Flags |= DxilDescriptorRangeFlags::DataStatic;
  1081. break;
  1082. case TokenType::DATA_STATIC_WHILE_SET_AT_EXECUTE:
  1083. Flags |= DxilDescriptorRangeFlags::DataStaticWhileSetAtExecute;
  1084. break;
  1085. case TokenType::DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS:
  1086. Flags |= DxilDescriptorRangeFlags::DescriptorsStaticKeepingBufferBoundsChecks;
  1087. break;
  1088. default:
  1089. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Expected a descriptor range flag value, found: '%s'", Token.GetStr()));
  1090. }
  1091. Token = m_pTokenizer->PeekToken();
  1092. if(Token.GetType() == TokenType::RParen || Token.GetType() == TokenType::Comma)
  1093. break;
  1094. IFC(GetAndMatchToken(Token, TokenType::OR));
  1095. }
  1096. }
  1097. Cleanup:
  1098. return hr;
  1099. }
  1100. HRESULT RootSignatureParser::ParseOffset(uint32_t & Offset)
  1101. {
  1102. HRESULT hr = S_OK;
  1103. TokenType Token;
  1104. IFC(GetAndMatchToken(Token, TokenType::offset));
  1105. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1106. Token = m_pTokenizer->PeekToken();
  1107. if(Token.GetType() == TokenType::DESCRIPTOR_RANGE_OFFSET_APPEND)
  1108. {
  1109. IFC(GetAndMatchToken(Token, TokenType::DESCRIPTOR_RANGE_OFFSET_APPEND));
  1110. Offset = DxilDescriptorRangeOffsetAppend;
  1111. }
  1112. else
  1113. {
  1114. IFC(GetAndMatchToken(Token, TokenType::NumberU32));
  1115. Offset = Token.GetU32Value();
  1116. }
  1117. Cleanup:
  1118. return hr;
  1119. }
  1120. HRESULT RootSignatureParser::ParseVisibility(DxilShaderVisibility & Vis)
  1121. {
  1122. HRESULT hr = S_OK;
  1123. TokenType Token;
  1124. IFC(GetAndMatchToken(Token, TokenType::visibility));
  1125. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1126. Token = m_pTokenizer->GetToken();
  1127. switch(Token.GetType())
  1128. {
  1129. case TokenType::SHADER_VISIBILITY_ALL: Vis = DxilShaderVisibility::All; break;
  1130. case TokenType::SHADER_VISIBILITY_VERTEX: Vis = DxilShaderVisibility::Vertex; break;
  1131. case TokenType::SHADER_VISIBILITY_HULL: Vis = DxilShaderVisibility::Hull; break;
  1132. case TokenType::SHADER_VISIBILITY_DOMAIN: Vis = DxilShaderVisibility::Domain; break;
  1133. case TokenType::SHADER_VISIBILITY_GEOMETRY: Vis = DxilShaderVisibility::Geometry; break;
  1134. case TokenType::SHADER_VISIBILITY_PIXEL: Vis = DxilShaderVisibility::Pixel; break;
  1135. default:
  1136. IFC(Error(ERR_RS_UNEXPECTED_TOKEN,
  1137. "Unexpected visibility value: '%s'.", Token.GetStr()));
  1138. }
  1139. Cleanup:
  1140. return hr;
  1141. }
  1142. HRESULT RootSignatureParser::ParseNum32BitConstants(uint32_t & NumConst)
  1143. {
  1144. HRESULT hr = S_OK;
  1145. TokenType Token;
  1146. IFC(GetAndMatchToken(Token, TokenType::num32BitConstants));
  1147. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1148. IFC(GetAndMatchToken(Token, TokenType::NumberU32));
  1149. NumConst = Token.GetU32Value();
  1150. Cleanup:
  1151. return hr;
  1152. }
  1153. HRESULT RootSignatureParser::ParseStaticSampler(DxilStaticSamplerDesc &P)
  1154. {
  1155. // StaticSampler( s0,
  1156. // [ Filter = FILTER_ANISOTROPIC,
  1157. // AddressU = TEXTURE_ADDRESS_WRAP,
  1158. // AddressV = TEXTURE_ADDRESS_WRAP,
  1159. // AddressW = TEXTURE_ADDRESS_WRAP,
  1160. // MipLODBias = 0,
  1161. // MaxAnisotropy = 16,
  1162. // ComparisonFunc = COMPARISON_LESS_EQUAL,
  1163. // BorderColor = STATIC_BORDER_COLOR_OPAQUE_WHITE,
  1164. // MinLOD = 0.f,
  1165. // MaxLOD = 3.402823466e+38f
  1166. // space = 0,
  1167. // visibility = SHADER_VISIBILITY_ALL ] )
  1168. HRESULT hr = S_OK;
  1169. TokenType Token;
  1170. memset(&P, 0, sizeof(P));
  1171. P.Filter = DxilFilter::ANISOTROPIC;
  1172. P.AddressU = P.AddressV = P.AddressW = DxilTextureAddressMode::Wrap;
  1173. P.MaxAnisotropy = 16;
  1174. P.ComparisonFunc = DxilComparisonFunc::LessEqual;
  1175. P.BorderColor = DxilStaticBorderColor::OpaqueWhite;
  1176. P.MaxLOD = DxilFloat32Max;
  1177. bool bSeenFilter = false;
  1178. bool bSeenAddressU = false;
  1179. bool bSeenAddressV = false;
  1180. bool bSeenAddressW = false;
  1181. bool bSeenMipLODBias = false;
  1182. bool bSeenMaxAnisotropy = false;
  1183. bool bSeenComparisonFunc = false;
  1184. bool bSeenBorderColor = false;
  1185. bool bSeenMinLOD = false;
  1186. bool bSeenMaxLOD = false;
  1187. bool bSeenSReg = false;
  1188. bool bSeenSpace = false;
  1189. bool bSeenVisibility = false;
  1190. IFC(GetAndMatchToken(Token, TokenType::StaticSampler));
  1191. IFC(GetAndMatchToken(Token, TokenType::LParen));
  1192. for(;;)
  1193. {
  1194. Token = m_pTokenizer->PeekToken();
  1195. switch(Token.GetType())
  1196. {
  1197. case TokenType::filter:
  1198. IFC(MarkParameter(bSeenFilter, "filter"));
  1199. IFC(ParseFilter(P.Filter));
  1200. break;
  1201. case TokenType::addressU:
  1202. IFC(MarkParameter(bSeenAddressU, "addressU"));
  1203. IFC(ParseTextureAddressMode(P.AddressU));
  1204. break;
  1205. case TokenType::addressV:
  1206. IFC(MarkParameter(bSeenAddressV, "addressV"));
  1207. IFC(ParseTextureAddressMode(P.AddressV));
  1208. break;
  1209. case TokenType::addressW:
  1210. IFC(MarkParameter(bSeenAddressW, "addressW"));
  1211. IFC(ParseTextureAddressMode(P.AddressW));
  1212. break;
  1213. case TokenType::mipLODBias:
  1214. IFC(MarkParameter(bSeenMipLODBias, "mipLODBias"));
  1215. IFC(ParseMipLODBias(P.MipLODBias));
  1216. break;
  1217. case TokenType::maxAnisotropy:
  1218. IFC(MarkParameter(bSeenMaxAnisotropy, "maxAnisotropy"));
  1219. IFC(ParseMaxAnisotropy(P.MaxAnisotropy));
  1220. break;
  1221. case TokenType::comparisonFunc:
  1222. IFC(MarkParameter(bSeenComparisonFunc, "comparisonFunc"));
  1223. IFC(ParseComparisonFunction(P.ComparisonFunc));
  1224. break;
  1225. case TokenType::borderColor:
  1226. IFC(MarkParameter(bSeenBorderColor, "borderColor"));
  1227. IFC(ParseBorderColor(P.BorderColor));
  1228. break;
  1229. case TokenType::minLOD:
  1230. IFC(MarkParameter(bSeenMinLOD, "minLOD"));
  1231. IFC(ParseMinLOD(P.MinLOD));
  1232. break;
  1233. case TokenType::maxLOD:
  1234. IFC(MarkParameter(bSeenMaxLOD, "maxLOD"));
  1235. IFC(ParseMaxLOD(P.MaxLOD));
  1236. break;
  1237. case TokenType::SReg:
  1238. IFC(MarkParameter(bSeenSReg, "sampler register s#"));
  1239. IFC(ParseRegister(TokenType::SReg, P.ShaderRegister));
  1240. break;
  1241. case TokenType::space:
  1242. IFC(MarkParameter(bSeenSpace, "space"));
  1243. IFC(ParseSpace(P.RegisterSpace));
  1244. break;
  1245. case TokenType::visibility:
  1246. IFC(MarkParameter(bSeenVisibility, "visibility"));
  1247. IFC(ParseVisibility(P.ShaderVisibility));
  1248. break;
  1249. default:
  1250. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s'", Token.GetStr()));
  1251. break;
  1252. }
  1253. Token = m_pTokenizer->GetToken();
  1254. if(Token.GetType() == TokenType::RParen)
  1255. break;
  1256. else if(Token.GetType() != TokenType::Comma)
  1257. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Unexpected token '%s'", Token.GetStr()));
  1258. }
  1259. if(!bSeenSReg)
  1260. {
  1261. IFC(Error(ERR_RS_UNDEFINED_REGISTER, "Sampler register s# must be defined for each static sampler"));
  1262. }
  1263. Cleanup:
  1264. return hr;
  1265. }
  1266. HRESULT RootSignatureParser::ParseFilter(DxilFilter & Filter)
  1267. {
  1268. HRESULT hr = S_OK;
  1269. TokenType Token;
  1270. IFC(GetAndMatchToken(Token, TokenType::filter));
  1271. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1272. Token = m_pTokenizer->GetToken();
  1273. switch(Token.GetType())
  1274. {
  1275. case TokenType::FILTER_MIN_MAG_MIP_POINT: Filter = DxilFilter::MIN_MAG_MIP_POINT; break;
  1276. case TokenType::FILTER_MIN_MAG_POINT_MIP_LINEAR: Filter = DxilFilter::MIN_MAG_POINT_MIP_LINEAR; break;
  1277. case TokenType::FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT: Filter = DxilFilter::MIN_POINT_MAG_LINEAR_MIP_POINT; break;
  1278. case TokenType::FILTER_MIN_POINT_MAG_MIP_LINEAR: Filter = DxilFilter::MIN_POINT_MAG_MIP_LINEAR; break;
  1279. case TokenType::FILTER_MIN_LINEAR_MAG_MIP_POINT: Filter = DxilFilter::MIN_LINEAR_MAG_MIP_POINT; break;
  1280. case TokenType::FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR: Filter = DxilFilter::MIN_LINEAR_MAG_POINT_MIP_LINEAR; break;
  1281. case TokenType::FILTER_MIN_MAG_LINEAR_MIP_POINT: Filter = DxilFilter::MIN_MAG_LINEAR_MIP_POINT; break;
  1282. case TokenType::FILTER_MIN_MAG_MIP_LINEAR: Filter = DxilFilter::MIN_MAG_MIP_LINEAR; break;
  1283. case TokenType::FILTER_ANISOTROPIC: Filter = DxilFilter::ANISOTROPIC; break;
  1284. case TokenType::FILTER_COMPARISON_MIN_MAG_MIP_POINT: Filter = DxilFilter::COMPARISON_MIN_MAG_MIP_POINT; break;
  1285. case TokenType::FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR: Filter = DxilFilter::COMPARISON_MIN_MAG_POINT_MIP_LINEAR; break;
  1286. case TokenType::FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT: Filter = DxilFilter::COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT; break;
  1287. case TokenType::FILTER_COMPARISON_MIN_POINT_MAG_MIP_LINEAR: Filter = DxilFilter::COMPARISON_MIN_POINT_MAG_MIP_LINEAR; break;
  1288. case TokenType::FILTER_COMPARISON_MIN_LINEAR_MAG_MIP_POINT: Filter = DxilFilter::COMPARISON_MIN_LINEAR_MAG_MIP_POINT; break;
  1289. case TokenType::FILTER_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR: Filter = DxilFilter::COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR; break;
  1290. case TokenType::FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT: Filter = DxilFilter::COMPARISON_MIN_MAG_LINEAR_MIP_POINT; break;
  1291. case TokenType::FILTER_COMPARISON_MIN_MAG_MIP_LINEAR: Filter = DxilFilter::COMPARISON_MIN_MAG_MIP_LINEAR; break;
  1292. case TokenType::FILTER_COMPARISON_ANISOTROPIC: Filter = DxilFilter::COMPARISON_ANISOTROPIC; break;
  1293. case TokenType::FILTER_MINIMUM_MIN_MAG_MIP_POINT: Filter = DxilFilter::MINIMUM_MIN_MAG_MIP_POINT; break;
  1294. case TokenType::FILTER_MINIMUM_MIN_MAG_POINT_MIP_LINEAR: Filter = DxilFilter::MINIMUM_MIN_MAG_POINT_MIP_LINEAR; break;
  1295. case TokenType::FILTER_MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT: Filter = DxilFilter::MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT; break;
  1296. case TokenType::FILTER_MINIMUM_MIN_POINT_MAG_MIP_LINEAR: Filter = DxilFilter::MINIMUM_MIN_POINT_MAG_MIP_LINEAR; break;
  1297. case TokenType::FILTER_MINIMUM_MIN_LINEAR_MAG_MIP_POINT: Filter = DxilFilter::MINIMUM_MIN_LINEAR_MAG_MIP_POINT; break;
  1298. case TokenType::FILTER_MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR: Filter = DxilFilter::MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR; break;
  1299. case TokenType::FILTER_MINIMUM_MIN_MAG_LINEAR_MIP_POINT: Filter = DxilFilter::MINIMUM_MIN_MAG_LINEAR_MIP_POINT; break;
  1300. case TokenType::FILTER_MINIMUM_MIN_MAG_MIP_LINEAR: Filter = DxilFilter::MINIMUM_MIN_MAG_MIP_LINEAR; break;
  1301. case TokenType::FILTER_MINIMUM_ANISOTROPIC: Filter = DxilFilter::MINIMUM_ANISOTROPIC; break;
  1302. case TokenType::FILTER_MAXIMUM_MIN_MAG_MIP_POINT: Filter = DxilFilter::MAXIMUM_MIN_MAG_MIP_POINT; break;
  1303. case TokenType::FILTER_MAXIMUM_MIN_MAG_POINT_MIP_LINEAR: Filter = DxilFilter::MAXIMUM_MIN_MAG_POINT_MIP_LINEAR; break;
  1304. case TokenType::FILTER_MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT: Filter = DxilFilter::MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT; break;
  1305. case TokenType::FILTER_MAXIMUM_MIN_POINT_MAG_MIP_LINEAR: Filter = DxilFilter::MAXIMUM_MIN_POINT_MAG_MIP_LINEAR; break;
  1306. case TokenType::FILTER_MAXIMUM_MIN_LINEAR_MAG_MIP_POINT: Filter = DxilFilter::MAXIMUM_MIN_LINEAR_MAG_MIP_POINT; break;
  1307. case TokenType::FILTER_MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR: Filter = DxilFilter::MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR; break;
  1308. case TokenType::FILTER_MAXIMUM_MIN_MAG_LINEAR_MIP_POINT: Filter = DxilFilter::MAXIMUM_MIN_MAG_LINEAR_MIP_POINT; break;
  1309. case TokenType::FILTER_MAXIMUM_MIN_MAG_MIP_LINEAR: Filter = DxilFilter::MAXIMUM_MIN_MAG_MIP_LINEAR; break;
  1310. case TokenType::FILTER_MAXIMUM_ANISOTROPIC: Filter = DxilFilter::MAXIMUM_ANISOTROPIC; break;
  1311. default:
  1312. IFC(Error(ERR_RS_UNEXPECTED_TOKEN,
  1313. "Unexpected filter value: '%s'.", Token.GetStr()));
  1314. }
  1315. Cleanup:
  1316. return hr;
  1317. }
  1318. HRESULT RootSignatureParser::ParseTextureAddressMode(DxilTextureAddressMode & AddressMode)
  1319. {
  1320. HRESULT hr = S_OK;
  1321. TokenType Token = m_pTokenizer->GetToken();
  1322. DXASSERT_NOMSG(Token.GetType() == TokenType::addressU ||
  1323. Token.GetType() == TokenType::addressV ||
  1324. Token.GetType() == TokenType::addressW);
  1325. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1326. Token = m_pTokenizer->GetToken();
  1327. switch(Token.GetType())
  1328. {
  1329. case TokenType::TEXTURE_ADDRESS_WRAP: AddressMode = DxilTextureAddressMode::Wrap; break;
  1330. case TokenType::TEXTURE_ADDRESS_MIRROR: AddressMode = DxilTextureAddressMode::Mirror; break;
  1331. case TokenType::TEXTURE_ADDRESS_CLAMP: AddressMode = DxilTextureAddressMode::Clamp; break;
  1332. case TokenType::TEXTURE_ADDRESS_BORDER: AddressMode = DxilTextureAddressMode::Border; break;
  1333. case TokenType::TEXTURE_ADDRESS_MIRROR_ONCE: AddressMode = DxilTextureAddressMode::MirrorOnce; break;
  1334. default:
  1335. IFC(Error(ERR_RS_UNEXPECTED_TOKEN,
  1336. "Unexpected texture address mode value: '%s'.", Token.GetStr()));
  1337. }
  1338. Cleanup:
  1339. return hr;
  1340. }
  1341. HRESULT RootSignatureParser::ParseMipLODBias(float & MipLODBias)
  1342. {
  1343. HRESULT hr = S_OK;
  1344. TokenType Token;
  1345. IFC(GetAndMatchToken(Token, TokenType::mipLODBias));
  1346. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1347. IFC(ParseFloat(MipLODBias));
  1348. Cleanup:
  1349. return hr;
  1350. }
  1351. HRESULT RootSignatureParser::ParseMaxAnisotropy(uint32_t & MaxAnisotropy)
  1352. {
  1353. HRESULT hr = S_OK;
  1354. TokenType Token;
  1355. IFC(GetAndMatchToken(Token, TokenType::maxAnisotropy));
  1356. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1357. IFC(GetAndMatchToken(Token, TokenType::NumberU32));
  1358. MaxAnisotropy = Token.GetU32Value();
  1359. Cleanup:
  1360. return hr;
  1361. }
  1362. HRESULT RootSignatureParser::ParseComparisonFunction(DxilComparisonFunc & ComparisonFunc)
  1363. {
  1364. HRESULT hr = S_OK;
  1365. TokenType Token;
  1366. IFC(GetAndMatchToken(Token, TokenType::comparisonFunc));
  1367. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1368. Token = m_pTokenizer->GetToken();
  1369. switch(Token.GetType())
  1370. {
  1371. case TokenType::COMPARISON_NEVER: ComparisonFunc = DxilComparisonFunc::Never; break;
  1372. case TokenType::COMPARISON_LESS: ComparisonFunc = DxilComparisonFunc::Less; break;
  1373. case TokenType::COMPARISON_EQUAL: ComparisonFunc = DxilComparisonFunc::Equal; break;
  1374. case TokenType::COMPARISON_LESS_EQUAL: ComparisonFunc = DxilComparisonFunc::LessEqual; break;
  1375. case TokenType::COMPARISON_GREATER: ComparisonFunc = DxilComparisonFunc::Greater; break;
  1376. case TokenType::COMPARISON_NOT_EQUAL: ComparisonFunc = DxilComparisonFunc::NotEqual; break;
  1377. case TokenType::COMPARISON_GREATER_EQUAL: ComparisonFunc = DxilComparisonFunc::GreaterEqual; break;
  1378. case TokenType::COMPARISON_ALWAYS: ComparisonFunc = DxilComparisonFunc::Always; break;
  1379. default:
  1380. IFC(Error(ERR_RS_UNEXPECTED_TOKEN,
  1381. "Unexpected texture address mode value: '%s'.", Token.GetStr()));
  1382. }
  1383. Cleanup:
  1384. return hr;
  1385. }
  1386. HRESULT RootSignatureParser::ParseBorderColor(DxilStaticBorderColor & BorderColor)
  1387. {
  1388. HRESULT hr = S_OK;
  1389. TokenType Token;
  1390. IFC(GetAndMatchToken(Token, TokenType::borderColor));
  1391. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1392. Token = m_pTokenizer->GetToken();
  1393. switch(Token.GetType())
  1394. {
  1395. case TokenType::STATIC_BORDER_COLOR_TRANSPARENT_BLACK: BorderColor = DxilStaticBorderColor::TransparentBlack; break;
  1396. case TokenType::STATIC_BORDER_COLOR_OPAQUE_BLACK: BorderColor = DxilStaticBorderColor::OpaqueBlack; break;
  1397. case TokenType::STATIC_BORDER_COLOR_OPAQUE_WHITE: BorderColor = DxilStaticBorderColor::OpaqueWhite; break;
  1398. default:
  1399. IFC(Error(ERR_RS_UNEXPECTED_TOKEN,
  1400. "Unexpected texture address mode value: '%s'.", Token.GetStr()));
  1401. }
  1402. Cleanup:
  1403. return hr;
  1404. }
  1405. HRESULT RootSignatureParser::ParseMinLOD(float & MinLOD)
  1406. {
  1407. HRESULT hr = S_OK;
  1408. TokenType Token;
  1409. IFC(GetAndMatchToken(Token, TokenType::minLOD));
  1410. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1411. IFC(ParseFloat(MinLOD));
  1412. Cleanup:
  1413. return hr;
  1414. }
  1415. HRESULT RootSignatureParser::ParseMaxLOD(float & MaxLOD)
  1416. {
  1417. HRESULT hr = S_OK;
  1418. TokenType Token;
  1419. IFC(GetAndMatchToken(Token, TokenType::maxLOD));
  1420. IFC(GetAndMatchToken(Token, TokenType::EQ));
  1421. IFC(ParseFloat(MaxLOD));
  1422. Cleanup:
  1423. return hr;
  1424. }
  1425. HRESULT RootSignatureParser::ParseFloat(float & v)
  1426. {
  1427. HRESULT hr = S_OK;
  1428. TokenType Token = m_pTokenizer->GetToken();
  1429. if(Token.GetType() == TokenType::NumberU32)
  1430. {
  1431. v = (float)Token.GetU32Value();
  1432. }
  1433. else if(Token.GetType() == TokenType::NumberI32)
  1434. {
  1435. v = (float)Token.GetI32Value();
  1436. }
  1437. else if(Token.GetType() == TokenType::NumberFloat)
  1438. {
  1439. v = Token.GetFloatValue();
  1440. }
  1441. else
  1442. {
  1443. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Expected float, found token '%s'", Token.GetStr()));
  1444. }
  1445. Cleanup:
  1446. return hr;
  1447. }
  1448. HRESULT RootSignatureParser::MarkParameter(bool & bSeen, LPCSTR pName)
  1449. {
  1450. HRESULT hr = S_OK;
  1451. if(bSeen)
  1452. {
  1453. IFC(Error(ERR_RS_UNEXPECTED_TOKEN, "Parameter '%s' can be specified only once", pName));
  1454. }
  1455. bSeen = true;
  1456. Cleanup:
  1457. return hr;
  1458. }
  1459. _Use_decl_annotations_
  1460. bool clang::ParseHLSLRootSignature(
  1461. const char *pData, unsigned Len, hlsl::DxilRootSignatureVersion Ver,
  1462. hlsl::DxilRootSignatureCompilationFlags Flags, hlsl::DxilVersionedRootSignatureDesc **ppDesc,
  1463. SourceLocation Loc, clang::DiagnosticsEngine &Diags) {
  1464. *ppDesc = nullptr;
  1465. std::string OSStr;
  1466. llvm::raw_string_ostream OS(OSStr);
  1467. hlsl::RootSignatureTokenizer RST(pData, Len);
  1468. hlsl::RootSignatureParser RSP(&RST, Ver, Flags, OS);
  1469. hlsl::DxilVersionedRootSignatureDesc *D = nullptr;
  1470. if (SUCCEEDED(RSP.Parse(&D))) {
  1471. *ppDesc = D;
  1472. return true;
  1473. }
  1474. else {
  1475. // Create diagnostic error message.
  1476. OS.flush();
  1477. if (OSStr.empty()) {
  1478. Diags.Report(Loc, clang::diag::err_hlsl_rootsig) << "unexpected";
  1479. }
  1480. else {
  1481. Diags.Report(Loc, clang::diag::err_hlsl_rootsig) << OSStr.c_str();
  1482. }
  1483. return false;
  1484. }
  1485. }
  1486. _Use_decl_annotations_
  1487. void clang::ReportHLSLRootSigError(clang::DiagnosticsEngine &Diags,
  1488. clang::SourceLocation Loc, const char *pData,
  1489. unsigned Len) {
  1490. Diags.Report(Loc, clang::diag::err_hlsl_rootsig) << StringRef(pData, Len);
  1491. return;
  1492. }