ViewIDPipelineValidation.inl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // ViewIDPipelineValidation.inl //
  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. // This file implements inter-stage validation for ViewID. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. namespace hlsl {
  12. namespace {
  13. typedef std::vector<DxilSignatureAllocator::DummyElement> ElementVec;
  14. struct ComponentMask : public PSVComponentMask {
  15. uint32_t Data[4];
  16. ComponentMask() : PSVComponentMask(Data, 0) {
  17. memset(Data, 0, sizeof(Data));
  18. }
  19. ComponentMask(const ComponentMask &other) : PSVComponentMask(Data, other.NumVectors) {
  20. *this = other;
  21. }
  22. ComponentMask(const PSVComponentMask &other) : PSVComponentMask(Data, other.NumVectors) {
  23. *this = other;
  24. }
  25. ComponentMask &operator=(const PSVComponentMask &other) {
  26. NumVectors = other.NumVectors;
  27. if (other.Mask && NumVectors) {
  28. memcpy(Data, other.Mask, sizeof(uint32_t) * PSVComputeMaskDwordsFromVectors(NumVectors));
  29. }
  30. else {
  31. memset(Data, 0, sizeof(Data));
  32. }
  33. return *this;
  34. }
  35. ComponentMask &operator=(const ComponentMask &other) {
  36. *this = (const PSVComponentMask &)other;
  37. return *this;
  38. }
  39. ComponentMask &operator|=(const PSVComponentMask &other) {
  40. NumVectors = std::max(NumVectors, other.NumVectors);
  41. PSVComponentMask::operator|=(other);
  42. return *this;
  43. }
  44. };
  45. static void InitElement(DxilSignatureAllocator::DummyElement &eOut,
  46. const PSVSignatureElement &eIn,
  47. DXIL::SigPointKind sigPoint) {
  48. eOut.rows = eIn.GetRows();
  49. eOut.cols = eIn.GetCols();
  50. eOut.row = eIn.GetStartRow();
  51. eOut.col = eIn.GetStartCol();
  52. eOut.kind = (DXIL::SemanticKind)eIn.GetSemanticKind();
  53. eOut.interpolation = (DXIL::InterpolationMode)eIn.GetInterpolationMode();
  54. eOut.interpretation = SigPoint::GetInterpretation(eOut.kind, sigPoint, 6, 1);
  55. eOut.indexFlags = eIn.GetDynamicIndexMask();
  56. // Tessfactors must be treated as dynamically indexed to prevent breaking them up
  57. if (eOut.interpretation == DXIL::SemanticInterpretationKind::TessFactor)
  58. eOut.indexFlags = (1 << eOut.cols) - 1;
  59. }
  60. static void CopyElements( ElementVec &outElements,
  61. DXIL::SigPointKind sigPoint,
  62. unsigned numElements,
  63. unsigned streamIndex,
  64. std::function<PSVSignatureElement(unsigned)> getElement) {
  65. outElements.clear();
  66. outElements.reserve(numElements);
  67. for (unsigned i = 0; i < numElements; i++) {
  68. auto inEl = getElement(i);
  69. if (!inEl.IsAllocated() || inEl.GetOutputStream() != streamIndex)
  70. continue;
  71. outElements.emplace_back(i);
  72. DxilSignatureAllocator::DummyElement &el = outElements.back();
  73. InitElement(el, inEl, sigPoint);
  74. }
  75. }
  76. static void AddViewIDElements(ElementVec &outElements,
  77. ElementVec &inElements,
  78. PSVComponentMask &mask,
  79. unsigned viewIDCount) {
  80. // Compute needed elements
  81. for (unsigned adding = 0; adding < 2; adding++) {
  82. uint32_t numElements = 0;
  83. for (auto &E : inElements) {
  84. for (uint32_t row = 0; row < E.rows; row++) {
  85. for (uint32_t col = 0; col < E.cols; col++) {
  86. bool bDynIndex = E.indexFlags & (1 << col);
  87. if (row > 0 && bDynIndex)
  88. continue;
  89. if (adding) {
  90. uint32_t componentIndex = (E.GetStartRow() + row) * 4 + E.GetStartCol() + col;
  91. DxilSignatureAllocator::DummyElement NE(E.id);
  92. NE.kind = E.kind;
  93. NE.interpolation = E.interpolation;
  94. // All elements interpreted as Arbitrary for packing purposes
  95. NE.interpretation = DXIL::SemanticInterpretationKind::Arb;
  96. NE.rows = bDynIndex ? E.GetRows() : 1;
  97. for (uint32_t indexedRow = 0; indexedRow < NE.rows; indexedRow++) {
  98. if (mask.Get(componentIndex + (4 * indexedRow))) {
  99. NE.rows *= viewIDCount;
  100. break;
  101. }
  102. }
  103. outElements.push_back(NE);
  104. }
  105. numElements++;
  106. }
  107. }
  108. }
  109. if (!adding)
  110. outElements.reserve(numElements);
  111. }
  112. }
  113. static bool CheckFit(ElementVec &elements) {
  114. std::vector<DxilSignatureAllocator::PackElement*> packElements;
  115. packElements.reserve(elements.size());
  116. for (auto &E : elements)
  117. packElements.push_back(&E);
  118. // Since we are putting an upper limit of 4x32 registers regardless of actual element size,
  119. // we can just have allocator to use the default behavior.
  120. // This should be fixed if we enforce loose upper limit on total number of signature registers based on element size.
  121. DxilSignatureAllocator alloc(32, true);
  122. alloc.SetIgnoreIndexing(true);
  123. alloc.PackOptimized(packElements, 0, 32);
  124. for (auto &E : elements) {
  125. if (!E.IsAllocated())
  126. return false;
  127. }
  128. return true;
  129. }
  130. static bool CheckMaxVertexCount(const ElementVec &elements, unsigned maxVertexCount) {
  131. unsigned numComponents = 0;
  132. for (auto &E : elements)
  133. numComponents += E.GetRows() * E.GetCols();
  134. return numComponents <= 1024 && maxVertexCount <= 1024 && numComponents * maxVertexCount <= 1024;
  135. }
  136. static bool MergeElements(const ElementVec &priorElements,
  137. ElementVec &inputElements,
  138. uint32_t &numVectors,
  139. unsigned &mismatchElementId) {
  140. inputElements.reserve(std::max(priorElements.size(), inputElements.size()));
  141. unsigned minElements = (unsigned)std::min(priorElements.size(), inputElements.size());
  142. for (unsigned i = 0; i < minElements; i++) {
  143. const DxilSignatureAllocator::DummyElement &priorEl = priorElements[i];
  144. DxilSignatureAllocator::DummyElement &inputEl = inputElements[i];
  145. // Verify elements match
  146. if (priorEl.rows != inputEl.rows ||
  147. priorEl.cols != inputEl.cols ||
  148. priorEl.row != inputEl.row ||
  149. priorEl.col != inputEl.col ||
  150. priorEl.kind != inputEl.kind ||
  151. // don't care about interpolation since normal signature matching ignores it: priorEl.interpolation != inputEl.interpolation ||
  152. priorEl.interpretation != inputEl.interpretation) {
  153. mismatchElementId = inputEl.id;
  154. return false;
  155. }
  156. // OR prior dynamic index flags into input element
  157. inputEl.indexFlags |= priorEl.indexFlags;
  158. }
  159. // Add extra incoming elements if there are more
  160. for (unsigned i = (unsigned)inputElements.size(); i < (unsigned)priorElements.size(); i++) {
  161. inputElements.push_back(priorElements[i]);
  162. }
  163. // Update numVectors to max
  164. for (unsigned i = 0; i < inputElements.size(); i++) {
  165. DxilSignatureAllocator::DummyElement &inputEl = inputElements[i];
  166. numVectors = std::max(numVectors, inputEl.row + inputEl.rows);
  167. }
  168. return true;
  169. }
  170. static void PropagateMask(const ComponentMask &priorMask,
  171. ElementVec &inputElements,
  172. ComponentMask &outMask,
  173. std::function<PSVComponentMask(unsigned)> getMask) {
  174. // Iterate elements
  175. for (auto &E : inputElements) {
  176. for (unsigned row = 0; row < E.GetRows(); row++) {
  177. for (unsigned col = 0; col < E.GetCols(); col++) {
  178. uint32_t componentIndex = (E.GetStartRow() + row) * 4 + E.GetStartCol() + col;
  179. // If bit set in priorMask
  180. if (priorMask.Get(componentIndex)) {
  181. // get mask of outputs affected by inputs and OR into outMask
  182. outMask |= getMask(componentIndex);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. bool DetectViewIDDependentTessFactor(const ElementVec &pcElements, ComponentMask &mask) {
  189. for (auto &E : pcElements) {
  190. if (E.GetKind() == DXIL::SemanticKind::TessFactor || E.GetKind() == DXIL::SemanticKind::InsideTessFactor) {
  191. for (unsigned row = 0; row < E.GetRows(); row++) {
  192. for (unsigned col = 0; col < E.GetCols(); col++) {
  193. uint32_t componentIndex = (E.GetStartRow() + row) * 4 + E.GetStartCol() + col;
  194. if (mask.Get(componentIndex))
  195. return true;
  196. }
  197. }
  198. }
  199. }
  200. return false;
  201. }
  202. class ViewIDValidator_impl : public hlsl::ViewIDValidator {
  203. ComponentMask m_PriorOutputMask;
  204. ComponentMask m_PriorPCMask;
  205. ElementVec m_PriorOutputSignature;
  206. ElementVec m_PriorPCSignature;
  207. unsigned m_ViewIDCount;
  208. unsigned m_GSRastStreamIndex;
  209. void ClearPriorState() {
  210. m_PriorOutputMask = ComponentMask();
  211. m_PriorPCMask = ComponentMask();
  212. m_PriorOutputSignature.clear();
  213. m_PriorPCSignature.clear();
  214. }
  215. public:
  216. ViewIDValidator_impl(unsigned viewIDCount, unsigned gsRastStreamIndex)
  217. : m_PriorOutputMask(),
  218. m_ViewIDCount(viewIDCount),
  219. m_GSRastStreamIndex(gsRastStreamIndex)
  220. {}
  221. virtual ~ViewIDValidator_impl() {}
  222. Result ValidateStage(const DxilPipelineStateValidation &PSV,
  223. bool bFinalStage,
  224. bool bExpandInputOnly,
  225. unsigned &mismatchElementId) override {
  226. if (!PSV.GetPSVRuntimeInfo0())
  227. return Result::InvalidPSV;
  228. if (!PSV.GetPSVRuntimeInfo1())
  229. return Result::InvalidPSVVersion;
  230. switch (PSV.GetShaderKind()) {
  231. case PSVShaderKind::Vertex: {
  232. if (bExpandInputOnly)
  233. return Result::InvalidUsage;
  234. // Initialize mask with direct ViewID dependent outputs
  235. ComponentMask mask(PSV.GetViewIDOutputMask(0));
  236. // capture output signature
  237. ElementVec outSig;
  238. CopyElements( outSig, DXIL::SigPointKind::VSOut, PSV.GetSigOutputElements(), 0,
  239. [&](unsigned i) -> PSVSignatureElement {
  240. return PSV.GetSignatureElement(PSV.GetOutputElement0(i));
  241. });
  242. // Copy mask to prior mask
  243. m_PriorOutputMask = mask;
  244. // Capture output signature for next stage
  245. m_PriorOutputSignature = std::move(outSig);
  246. break;
  247. }
  248. case PSVShaderKind::Hull: {
  249. if (bFinalStage)
  250. return Result::InvalidUsage;
  251. // Initialize mask with direct ViewID dependent outputs
  252. ComponentMask outputMask(PSV.GetViewIDOutputMask(0));
  253. ComponentMask pcMask(PSV.GetViewIDPCOutputMask());
  254. // capture signatures
  255. ElementVec inSig, outSig, pcSig;
  256. CopyElements( inSig, DXIL::SigPointKind::HSCPIn, PSV.GetSigInputElements(), 0,
  257. [&](unsigned i) -> PSVSignatureElement {
  258. return PSV.GetSignatureElement(PSV.GetInputElement0(i));
  259. });
  260. // Merge prior and input signatures, update prior mask size if necessary
  261. if (!MergeElements(m_PriorOutputSignature, inSig, m_PriorOutputMask.NumVectors, mismatchElementId))
  262. return Result::MismatchedSignatures;
  263. // Create new version with ViewID elements from merged signature
  264. ElementVec viewIDSig;
  265. AddViewIDElements(viewIDSig, inSig, m_PriorOutputMask, m_ViewIDCount);
  266. // Verify fit
  267. if (!CheckFit(viewIDSig))
  268. return Result::InsufficientInputSpace;
  269. if (bExpandInputOnly) {
  270. ClearPriorState();
  271. return Result::Success;
  272. }
  273. CopyElements(outSig, DXIL::SigPointKind::HSCPOut, PSV.GetSigOutputElements(), 0,
  274. [&](unsigned i) -> PSVSignatureElement {
  275. return PSV.GetSignatureElement(PSV.GetOutputElement0(i));
  276. });
  277. CopyElements(pcSig, DXIL::SigPointKind::PCOut, PSV.GetSigPatchConstOrPrimElements(), 0,
  278. [&](unsigned i) -> PSVSignatureElement {
  279. return PSV.GetSignatureElement(PSV.GetPatchConstOrPrimElement0(i));
  280. });
  281. // Propagate prior mask through input-output dependencies
  282. if (PSV.GetInputToOutputTable(0).IsValid()) {
  283. PropagateMask(m_PriorOutputMask, inSig, outputMask,
  284. [&](unsigned i) -> PSVComponentMask { return PSV.GetInputToOutputTable(0).GetMaskForInput(i); });
  285. }
  286. if (PSV.GetInputToPCOutputTable().IsValid()) {
  287. PropagateMask(m_PriorOutputMask, inSig, pcMask,
  288. [&](unsigned i) -> PSVComponentMask { return PSV.GetInputToPCOutputTable().GetMaskForInput(i); });
  289. }
  290. // Copy mask to prior mask
  291. m_PriorOutputMask = outputMask;
  292. m_PriorPCMask = pcMask;
  293. // Capture output signature for next stage
  294. m_PriorOutputSignature = std::move(outSig);
  295. m_PriorPCSignature = std::move(pcSig);
  296. if (DetectViewIDDependentTessFactor(pcSig, pcMask)) {
  297. return Result::SuccessWithViewIDDependentTessFactor;
  298. }
  299. break;
  300. }
  301. case PSVShaderKind::Domain: {
  302. // Initialize mask with direct ViewID dependent outputs
  303. ComponentMask mask(PSV.GetViewIDOutputMask(0));
  304. // capture signatures
  305. ElementVec inSig, pcSig, outSig;
  306. CopyElements( inSig, DXIL::SigPointKind::DSCPIn, PSV.GetSigInputElements(), 0,
  307. [&](unsigned i) -> PSVSignatureElement {
  308. return PSV.GetSignatureElement(PSV.GetInputElement0(i));
  309. });
  310. CopyElements( pcSig, DXIL::SigPointKind::DSIn, PSV.GetSigPatchConstOrPrimElements(), 0,
  311. [&](unsigned i) -> PSVSignatureElement {
  312. return PSV.GetSignatureElement(PSV.GetPatchConstOrPrimElement0(i));
  313. });
  314. // Merge prior and input signatures, update prior mask size if necessary
  315. if (!MergeElements(m_PriorOutputSignature, inSig, m_PriorOutputMask.NumVectors, mismatchElementId))
  316. return Result::MismatchedSignatures;
  317. if (!MergeElements(m_PriorPCSignature, pcSig, m_PriorPCMask.NumVectors, mismatchElementId))
  318. return Result::MismatchedPCSignatures;
  319. {
  320. // Create new version with ViewID elements from merged signature
  321. ElementVec viewIDSig;
  322. AddViewIDElements(viewIDSig, inSig, m_PriorOutputMask, m_ViewIDCount);
  323. // Verify fit
  324. if (!CheckFit(viewIDSig))
  325. return Result::InsufficientInputSpace;
  326. }
  327. {
  328. // Create new version with ViewID elements from merged signature
  329. ElementVec viewIDSig;
  330. AddViewIDElements(viewIDSig, pcSig, m_PriorPCMask, m_ViewIDCount);
  331. // Verify fit
  332. if (!CheckFit(viewIDSig))
  333. return Result::InsufficientPCSpace;
  334. }
  335. if (bExpandInputOnly) {
  336. ClearPriorState();
  337. return Result::Success;
  338. }
  339. CopyElements(outSig, DXIL::SigPointKind::DSOut, PSV.GetSigOutputElements(), 0,
  340. [&](unsigned i) -> PSVSignatureElement {
  341. return PSV.GetSignatureElement(PSV.GetOutputElement0(i));
  342. });
  343. // Propagate prior mask through input-output dependencies
  344. if (PSV.GetInputToOutputTable(0).IsValid()) {
  345. PropagateMask(m_PriorOutputMask, inSig, mask,
  346. [&](unsigned i) -> PSVComponentMask { return PSV.GetInputToOutputTable(0).GetMaskForInput(i); });
  347. }
  348. if (PSV.GetPCInputToOutputTable().IsValid()) {
  349. PropagateMask(m_PriorPCMask, pcSig, mask,
  350. [&](unsigned i) -> PSVComponentMask { return PSV.GetPCInputToOutputTable().GetMaskForInput(i); });
  351. }
  352. // Copy mask to prior mask
  353. m_PriorOutputMask = mask;
  354. m_PriorPCMask = ComponentMask();
  355. // Capture output signature for next stage
  356. m_PriorOutputSignature = std::move(outSig);
  357. m_PriorPCSignature.clear();
  358. break;
  359. }
  360. case PSVShaderKind::Geometry: {
  361. // capture signatures
  362. ElementVec inSig, outSig[4];
  363. CopyElements( inSig, DXIL::SigPointKind::GSVIn, PSV.GetSigInputElements(), 0,
  364. [&](unsigned i) -> PSVSignatureElement {
  365. return PSV.GetSignatureElement(PSV.GetInputElement0(i));
  366. });
  367. // Merge prior and input signatures, update prior mask size if necessary
  368. if (!MergeElements(m_PriorOutputSignature, inSig, m_PriorOutputMask.NumVectors, mismatchElementId))
  369. return Result::MismatchedSignatures;
  370. // Create new version with ViewID elements from merged signature
  371. ElementVec viewIDSig;
  372. AddViewIDElements(viewIDSig, inSig, m_PriorOutputMask, m_ViewIDCount);
  373. // Verify fit
  374. if (!CheckFit(viewIDSig))
  375. return Result::InsufficientInputSpace;
  376. if (bExpandInputOnly) {
  377. ClearPriorState();
  378. return Result::Success;
  379. }
  380. for (unsigned streamIndex = 0; streamIndex < 4; streamIndex++) {
  381. // Initialize mask with direct ViewID dependent outputs
  382. ComponentMask mask(PSV.GetViewIDOutputMask(streamIndex));
  383. CopyElements( outSig[streamIndex], DXIL::SigPointKind::GSOut, PSV.GetSigOutputElements(), streamIndex,
  384. [&](unsigned i) -> PSVSignatureElement {
  385. return PSV.GetSignatureElement(PSV.GetOutputElement0(i));
  386. });
  387. if (!outSig[streamIndex].empty()) {
  388. // Propagate prior mask through input-output dependencies
  389. if (PSV.GetInputToOutputTable(streamIndex).IsValid()) {
  390. PropagateMask(m_PriorOutputMask, inSig, mask,
  391. [&](unsigned i) -> PSVComponentMask { return PSV.GetInputToOutputTable(streamIndex).GetMaskForInput(i); });
  392. }
  393. // Create new version with ViewID elements from prior signature
  394. ElementVec viewIDSig;
  395. AddViewIDElements(viewIDSig, outSig[streamIndex], mask, m_ViewIDCount);
  396. // Verify fit
  397. if (!CheckMaxVertexCount(viewIDSig, PSV.GetPSVRuntimeInfo1()->MaxVertexCount))
  398. return Result::InsufficientOutputSpace;
  399. if (!CheckFit(viewIDSig))
  400. return Result::InsufficientOutputSpace;
  401. }
  402. // Capture this mask for the next stage
  403. if (m_GSRastStreamIndex == streamIndex)
  404. m_PriorOutputMask = mask;
  405. }
  406. if (m_GSRastStreamIndex < 4 && !bFinalStage) {
  407. m_PriorOutputSignature = std::move(outSig[m_GSRastStreamIndex]);
  408. } else {
  409. ClearPriorState();
  410. if (!bFinalStage)
  411. return Result::InvalidUsage;
  412. }
  413. return Result::Success;
  414. }
  415. case PSVShaderKind::Pixel: {
  416. // capture signatures
  417. ElementVec inSig;
  418. CopyElements( inSig, DXIL::SigPointKind::PSIn, PSV.GetSigInputElements(), 0,
  419. [&](unsigned i) -> PSVSignatureElement {
  420. return PSV.GetSignatureElement(PSV.GetInputElement0(i));
  421. });
  422. // Merge prior and input signatures, update prior mask size if necessary
  423. if (!MergeElements(m_PriorOutputSignature, inSig, m_PriorOutputMask.NumVectors, mismatchElementId))
  424. return Result::MismatchedSignatures;
  425. // Create new version with ViewID elements from merged signature
  426. ElementVec viewIDSig;
  427. AddViewIDElements(viewIDSig, inSig, m_PriorOutputMask, m_ViewIDCount);
  428. // Verify fit
  429. if (!CheckFit(viewIDSig))
  430. return Result::InsufficientInputSpace;
  431. // Final stage, so clear output state.
  432. m_PriorOutputMask = ComponentMask();
  433. m_PriorOutputSignature.clear();
  434. // PS has to be the last stage, so return.
  435. return Result::Success;
  436. }
  437. case PSVShaderKind::Compute:
  438. default:
  439. return Result::InvalidUsage;
  440. }
  441. if (bFinalStage) {
  442. // Last stage was not pixel shader, so output has not yet been validated.
  443. // Create new version with ViewID elements from prior signature
  444. ElementVec viewIDSig;
  445. AddViewIDElements(viewIDSig, m_PriorOutputSignature, m_PriorOutputMask, m_ViewIDCount);
  446. // Verify fit
  447. if (!CheckFit(viewIDSig))
  448. return Result::InsufficientOutputSpace;
  449. }
  450. return Result::Success;
  451. }
  452. };
  453. } // namespace anonymous
  454. ViewIDValidator* NewViewIDValidator(unsigned viewIDCount, unsigned gsRastStreamIndex) {
  455. return new ViewIDValidator_impl(viewIDCount, gsRastStreamIndex);
  456. }
  457. } // namespace hlsl