DxilModuleTest.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // Copyright (C) Microsoft Corporation. All rights reserved. //
  4. // DxilModuleTest.cpp //
  5. // //
  6. // Provides unit tests for DxilModule. //
  7. // //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "dxc/Test/CompilationResult.h"
  10. #include "dxc/Test/HlslTestUtils.h"
  11. #include "dxc/Test/DxcTestUtils.h"
  12. #include "dxc/Support/microcom.h"
  13. #include "dxc/dxcapi.internal.h"
  14. #include "dxc/HLSL/HLOperationLowerExtension.h"
  15. #include "dxc/HlslIntrinsicOp.h"
  16. #include "dxc/DXIL/DxilOperations.h"
  17. #include "dxc/DXIL/DxilInstructions.h"
  18. #include "dxc/DxilContainer/DxilContainer.h"
  19. #include "dxc/DXIL/DxilModule.h"
  20. #include "llvm/Support/Regex.h"
  21. #include "llvm/Support/MSFileSystem.h"
  22. #include "llvm/Support/FileSystem.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include "llvm/Support/ErrorOr.h"
  25. #include "llvm/Bitcode/ReaderWriter.h"
  26. #include "llvm/IR/LLVMContext.h"
  27. #include "llvm/IR/InstIterator.h"
  28. using namespace hlsl;
  29. using namespace llvm;
  30. ///////////////////////////////////////////////////////////////////////////////
  31. // DxilModule unit tests.
  32. #ifdef _WIN32
  33. class DxilModuleTest {
  34. #else
  35. class DxilModuleTest : public ::testing::Test {
  36. #endif
  37. public:
  38. BEGIN_TEST_CLASS(DxilModuleTest)
  39. TEST_CLASS_PROPERTY(L"Parallel", L"true")
  40. TEST_METHOD_PROPERTY(L"Priority", L"0")
  41. END_TEST_CLASS()
  42. TEST_CLASS_SETUP(InitSupport);
  43. dxc::DxcDllSupport m_dllSupport;
  44. // Basic loading tests.
  45. TEST_METHOD(LoadDxilModule_1_0)
  46. TEST_METHOD(LoadDxilModule_1_1)
  47. TEST_METHOD(LoadDxilModule_1_2)
  48. // Precise query tests.
  49. TEST_METHOD(Precise1)
  50. TEST_METHOD(Precise2)
  51. TEST_METHOD(Precise3)
  52. TEST_METHOD(Precise4)
  53. TEST_METHOD(Precise5)
  54. TEST_METHOD(Precise6)
  55. TEST_METHOD(Precise7)
  56. TEST_METHOD(CSGetNumThreads)
  57. TEST_METHOD(MSGetNumThreads)
  58. TEST_METHOD(ASGetNumThreads)
  59. TEST_METHOD(SetValidatorVersion)
  60. TEST_METHOD(PayloadQualifier)
  61. void VerifyValidatorVersionFails(
  62. LPCWSTR shaderModel, const std::vector<LPCWSTR> &arguments,
  63. const std::vector<LPCSTR> &expectedErrors);
  64. void VerifyValidatorVersionMatches(
  65. LPCWSTR shaderModel, const std::vector<LPCWSTR> &arguments,
  66. unsigned expectedMajor = UINT_MAX, unsigned expectedMinor = UINT_MAX);
  67. };
  68. bool DxilModuleTest::InitSupport() {
  69. if (!m_dllSupport.IsEnabled()) {
  70. VERIFY_SUCCEEDED(m_dllSupport.Initialize());
  71. }
  72. return true;
  73. }
  74. ///////////////////////////////////////////////////////////////////////////////
  75. // Compilation and dxil module loading support.
  76. namespace {
  77. class Compiler {
  78. public:
  79. Compiler(dxc::DxcDllSupport &dll)
  80. : m_dllSupport(dll)
  81. , m_msf(CreateMSFileSystem())
  82. , m_pts(m_msf.get())
  83. {
  84. m_ver.Initialize(m_dllSupport);
  85. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  86. }
  87. bool SkipDxil_Test(unsigned major, unsigned minor) {
  88. return m_ver.SkipDxilVersion(major, minor);
  89. }
  90. IDxcOperationResult *Compile(const char *program, LPCWSTR shaderModel = L"ps_6_0") {
  91. return Compile(program, shaderModel, {}, {});
  92. }
  93. IDxcOperationResult *Compile(const char *program, LPCWSTR shaderModel, const std::vector<LPCWSTR> &arguments, const std::vector<DxcDefine> defs ) {
  94. Utf8ToBlob(m_dllSupport, program, &pCodeBlob);
  95. VERIFY_SUCCEEDED(pCompiler->Compile(pCodeBlob, L"hlsl.hlsl", L"main",
  96. shaderModel,
  97. const_cast<LPCWSTR *>(arguments.data()), arguments.size(),
  98. defs.data(), defs.size(),
  99. nullptr, &pCompileResult));
  100. return pCompileResult;
  101. }
  102. std::string Disassemble() {
  103. CComPtr<IDxcBlob> pBlob;
  104. CheckOperationSucceeded(pCompileResult, &pBlob);
  105. return DisassembleProgram(m_dllSupport, pBlob);
  106. }
  107. DxilModule &GetDxilModule() {
  108. // Make sure we compiled successfully.
  109. CComPtr<IDxcBlob> pBlob;
  110. CheckOperationSucceeded(pCompileResult, &pBlob);
  111. // Verify we have a valid dxil container.
  112. const DxilContainerHeader *pContainer =
  113. IsDxilContainerLike(pBlob->GetBufferPointer(), pBlob->GetBufferSize());
  114. VERIFY_IS_NOT_NULL(pContainer);
  115. VERIFY_IS_TRUE(IsValidDxilContainer(pContainer, pBlob->GetBufferSize()));
  116. // Get Dxil part from container.
  117. DxilPartIterator it = std::find_if(begin(pContainer), end(pContainer), DxilPartIsType(DFCC_DXIL));
  118. VERIFY_IS_FALSE(it == end(pContainer));
  119. const DxilProgramHeader *pProgramHeader =
  120. reinterpret_cast<const DxilProgramHeader *>(GetDxilPartData(*it));
  121. VERIFY_IS_TRUE(IsValidDxilProgramHeader(pProgramHeader, (*it)->PartSize));
  122. // Get a pointer to the llvm bitcode.
  123. const char *pIL;
  124. uint32_t pILLength;
  125. GetDxilProgramBitcode(pProgramHeader, &pIL, &pILLength);
  126. // Parse llvm bitcode into a module.
  127. std::unique_ptr<llvm::MemoryBuffer> pBitcodeBuf(
  128. llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(pIL, pILLength), "", false));
  129. llvm::ErrorOr<std::unique_ptr<llvm::Module>>
  130. pModule(llvm::parseBitcodeFile(pBitcodeBuf->getMemBufferRef(), m_llvmContext));
  131. if (std::error_code ec = pModule.getError()) {
  132. VERIFY_FAIL();
  133. }
  134. m_module = std::move(pModule.get());
  135. // Grab the dxil module;
  136. DxilModule *DM = DxilModule::TryGetDxilModule(m_module.get());
  137. VERIFY_IS_NOT_NULL(DM);
  138. return *DM;
  139. }
  140. public:
  141. static ::llvm::sys::fs::MSFileSystem *CreateMSFileSystem() {
  142. ::llvm::sys::fs::MSFileSystem *msfPtr;
  143. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  144. return msfPtr;
  145. }
  146. dxc::DxcDllSupport &m_dllSupport;
  147. VersionSupportInfo m_ver;
  148. CComPtr<IDxcCompiler> pCompiler;
  149. CComPtr<IDxcBlobEncoding> pCodeBlob;
  150. CComPtr<IDxcOperationResult> pCompileResult;
  151. llvm::LLVMContext m_llvmContext;
  152. std::unique_ptr<llvm::Module> m_module;
  153. std::unique_ptr<::llvm::sys::fs::MSFileSystem> m_msf;
  154. ::llvm::sys::fs::AutoPerThreadSystem m_pts;
  155. };
  156. }
  157. ///////////////////////////////////////////////////////////////////////////////
  158. // Unit Test Implementation
  159. TEST_F(DxilModuleTest, LoadDxilModule_1_0) {
  160. Compiler c(m_dllSupport);
  161. c.Compile(
  162. "float4 main() : SV_Target {\n"
  163. " return 0;\n"
  164. "}\n"
  165. ,
  166. L"ps_6_0"
  167. );
  168. // Basic sanity check on dxil version in dxil module.
  169. DxilModule &DM = c.GetDxilModule();
  170. unsigned vMajor, vMinor;
  171. DM.GetDxilVersion(vMajor, vMinor);
  172. VERIFY_IS_TRUE(vMajor == 1);
  173. VERIFY_IS_TRUE(vMinor == 0);
  174. }
  175. TEST_F(DxilModuleTest, LoadDxilModule_1_1) {
  176. Compiler c(m_dllSupport);
  177. if (c.SkipDxil_Test(1,1)) return;
  178. c.Compile(
  179. "float4 main() : SV_Target {\n"
  180. " return 0;\n"
  181. "}\n"
  182. ,
  183. L"ps_6_1"
  184. );
  185. // Basic sanity check on dxil version in dxil module.
  186. DxilModule &DM = c.GetDxilModule();
  187. unsigned vMajor, vMinor;
  188. DM.GetDxilVersion(vMajor, vMinor);
  189. VERIFY_IS_TRUE(vMajor == 1);
  190. VERIFY_IS_TRUE(vMinor == 1);
  191. }
  192. TEST_F(DxilModuleTest, LoadDxilModule_1_2) {
  193. Compiler c(m_dllSupport);
  194. if (c.SkipDxil_Test(1,2)) return;
  195. c.Compile(
  196. "float4 main() : SV_Target {\n"
  197. " return 0;\n"
  198. "}\n"
  199. ,
  200. L"ps_6_2"
  201. );
  202. // Basic sanity check on dxil version in dxil module.
  203. DxilModule &DM = c.GetDxilModule();
  204. unsigned vMajor, vMinor;
  205. DM.GetDxilVersion(vMajor, vMinor);
  206. VERIFY_IS_TRUE(vMajor == 1);
  207. VERIFY_IS_TRUE(vMinor == 2);
  208. }
  209. TEST_F(DxilModuleTest, Precise1) {
  210. Compiler c(m_dllSupport);
  211. c.Compile(
  212. "precise float main(float x : X, float y : Y) : SV_Target {\n"
  213. " return sqrt(x) + y;\n"
  214. "}\n"
  215. );
  216. // Make sure sqrt and add are marked precise.
  217. DxilModule &DM = c.GetDxilModule();
  218. Function *F = DM.GetEntryFunction();
  219. int numChecks = 0;
  220. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  221. Instruction *Inst = &*I;
  222. if (DxilInst_Sqrt(Inst)) {
  223. numChecks++;
  224. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  225. }
  226. else if (LlvmInst_FAdd(Inst)) {
  227. numChecks++;
  228. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  229. }
  230. }
  231. VERIFY_ARE_EQUAL(numChecks, 2);
  232. }
  233. TEST_F(DxilModuleTest, Precise2) {
  234. Compiler c(m_dllSupport);
  235. c.Compile(
  236. "float main(float x : X, float y : Y) : SV_Target {\n"
  237. " return sqrt(x) + y;\n"
  238. "}\n"
  239. );
  240. // Make sure sqrt and add are not marked precise.
  241. DxilModule &DM = c.GetDxilModule();
  242. Function *F = DM.GetEntryFunction();
  243. int numChecks = 0;
  244. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  245. Instruction *Inst = &*I;
  246. if (DxilInst_Sqrt(Inst)) {
  247. numChecks++;
  248. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  249. }
  250. else if (LlvmInst_FAdd(Inst)) {
  251. numChecks++;
  252. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  253. }
  254. }
  255. VERIFY_ARE_EQUAL(numChecks, 2);
  256. }
  257. TEST_F(DxilModuleTest, Precise3) {
  258. // TODO: Enable this test when precise metadata is inserted for Gis.
  259. if (const bool GisIsBroken = true) return;
  260. Compiler c(m_dllSupport);
  261. c.Compile(
  262. "float main(float x : X, float y : Y) : SV_Target {\n"
  263. " return sqrt(x) + y;\n"
  264. "}\n",
  265. L"ps_6_0",
  266. { L"/Gis" }, {}
  267. );
  268. // Make sure sqrt and add are marked precise.
  269. DxilModule &DM = c.GetDxilModule();
  270. Function *F = DM.GetEntryFunction();
  271. int numChecks = 0;
  272. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  273. Instruction *Inst = &*I;
  274. if (DxilInst_Sqrt(Inst)) {
  275. numChecks++;
  276. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  277. }
  278. else if (LlvmInst_FAdd(Inst)) {
  279. numChecks++;
  280. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  281. }
  282. }
  283. VERIFY_ARE_EQUAL(numChecks, 2);
  284. }
  285. TEST_F(DxilModuleTest, Precise4) {
  286. Compiler c(m_dllSupport);
  287. c.Compile(
  288. "float main(float x : X, float y : Y) : SV_Target {\n"
  289. " precise float sx = 1 / sqrt(x);\n"
  290. " return sx + y;\n"
  291. "}\n"
  292. );
  293. // Make sure sqrt and div are marked precise, and add is not.
  294. DxilModule &DM = c.GetDxilModule();
  295. Function *F = DM.GetEntryFunction();
  296. int numChecks = 0;
  297. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  298. Instruction *Inst = &*I;
  299. if (DxilInst_Sqrt(Inst)) {
  300. numChecks++;
  301. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  302. }
  303. else if (LlvmInst_FDiv(Inst)) {
  304. numChecks++;
  305. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  306. }
  307. else if (LlvmInst_FAdd(Inst)) {
  308. numChecks++;
  309. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  310. }
  311. }
  312. VERIFY_ARE_EQUAL(numChecks, 3);
  313. }
  314. TEST_F(DxilModuleTest, Precise5) {
  315. Compiler c(m_dllSupport);
  316. c.Compile(
  317. "float C[10];\n"
  318. "float main(float x : X, float y : Y, int i : I) : SV_Target {\n"
  319. " float A[2];\n"
  320. " A[0] = x;\n"
  321. " A[1] = y;\n"
  322. " return A[i] + C[i];\n"
  323. "}\n"
  324. );
  325. // Make sure load and extract value are not reported as precise.
  326. DxilModule &DM = c.GetDxilModule();
  327. Function *F = DM.GetEntryFunction();
  328. int numChecks = 0;
  329. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  330. Instruction *Inst = &*I;
  331. if (LlvmInst_ExtractValue(Inst)) {
  332. numChecks++;
  333. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  334. }
  335. else if (LlvmInst_Load(Inst)) {
  336. numChecks++;
  337. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  338. }
  339. else if (LlvmInst_FAdd(Inst)) {
  340. numChecks++;
  341. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  342. }
  343. }
  344. VERIFY_ARE_EQUAL(numChecks, 3);
  345. }
  346. TEST_F(DxilModuleTest, Precise6) {
  347. Compiler c(m_dllSupport);
  348. c.Compile(
  349. "precise float2 main(float2 x : A, float2 y : B) : SV_Target {\n"
  350. " return sqrt(x * y);\n"
  351. "}\n"
  352. );
  353. // Make sure sqrt and mul are marked precise.
  354. DxilModule &DM = c.GetDxilModule();
  355. Function *F = DM.GetEntryFunction();
  356. int numChecks = 0;
  357. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  358. Instruction *Inst = &*I;
  359. if (DxilInst_Sqrt(Inst)) {
  360. numChecks++;
  361. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  362. }
  363. else if (LlvmInst_FMul(Inst)) {
  364. numChecks++;
  365. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  366. }
  367. }
  368. VERIFY_ARE_EQUAL(numChecks, 4);
  369. }
  370. TEST_F(DxilModuleTest, Precise7) {
  371. Compiler c(m_dllSupport);
  372. c.Compile(
  373. "float2 main(float2 x : A, float2 y : B) : SV_Target {\n"
  374. " return sqrt(x * y);\n"
  375. "}\n"
  376. );
  377. // Make sure sqrt and mul are not marked precise.
  378. DxilModule &DM = c.GetDxilModule();
  379. Function *F = DM.GetEntryFunction();
  380. int numChecks = 0;
  381. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  382. Instruction *Inst = &*I;
  383. if (DxilInst_Sqrt(Inst)) {
  384. numChecks++;
  385. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  386. }
  387. else if (LlvmInst_FMul(Inst)) {
  388. numChecks++;
  389. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  390. }
  391. }
  392. VERIFY_ARE_EQUAL(numChecks, 4);
  393. }
  394. TEST_F(DxilModuleTest, CSGetNumThreads) {
  395. Compiler c(m_dllSupport);
  396. c.Compile(
  397. "[numthreads(8, 4, 2)]\n"
  398. "void main() {\n"
  399. "}\n"
  400. ,
  401. L"cs_6_0"
  402. );
  403. DxilModule &DM = c.GetDxilModule();
  404. VERIFY_ARE_EQUAL(8u, DM.GetNumThreads(0));
  405. VERIFY_ARE_EQUAL(4u, DM.GetNumThreads(1));
  406. VERIFY_ARE_EQUAL(2u, DM.GetNumThreads(2));
  407. }
  408. TEST_F(DxilModuleTest, MSGetNumThreads) {
  409. Compiler c(m_dllSupport);
  410. if (c.SkipDxil_Test(1,5)) return;
  411. c.Compile(
  412. "struct MeshPerVertex { float4 pos : SV_Position; };\n"
  413. "[numthreads(8, 4, 2)]\n"
  414. "[outputtopology(\"triangle\")]\n"
  415. "void main(\n"
  416. " out indices uint3 primIndices[1]\n"
  417. ") {\n"
  418. " SetMeshOutputCounts(0, 0);\n"
  419. "}\n"
  420. ,
  421. L"ms_6_5"
  422. );
  423. DxilModule &DM = c.GetDxilModule();
  424. VERIFY_ARE_EQUAL(8u, DM.GetNumThreads(0));
  425. VERIFY_ARE_EQUAL(4u, DM.GetNumThreads(1));
  426. VERIFY_ARE_EQUAL(2u, DM.GetNumThreads(2));
  427. }
  428. TEST_F(DxilModuleTest, ASGetNumThreads) {
  429. Compiler c(m_dllSupport);
  430. if (c.SkipDxil_Test(1,5)) return;
  431. c.Compile(
  432. "struct Payload { uint i; };\n"
  433. "[numthreads(8, 4, 2)]\n"
  434. "void main() {\n"
  435. " Payload pld = {0};\n"
  436. " DispatchMesh(1, 1, 1, pld);\n"
  437. "}\n"
  438. ,
  439. L"as_6_5"
  440. );
  441. DxilModule &DM = c.GetDxilModule();
  442. VERIFY_ARE_EQUAL(8u, DM.GetNumThreads(0));
  443. VERIFY_ARE_EQUAL(4u, DM.GetNumThreads(1));
  444. VERIFY_ARE_EQUAL(2u, DM.GetNumThreads(2));
  445. }
  446. void DxilModuleTest::VerifyValidatorVersionFails(
  447. LPCWSTR shaderModel, const std::vector<LPCWSTR> &arguments,
  448. const std::vector<LPCSTR> &expectedErrors) {
  449. LPCSTR shader =
  450. "[shader(\"pixel\")]"
  451. "float4 main() : SV_Target {\n"
  452. " return 0;\n"
  453. "}\n";
  454. Compiler c(m_dllSupport);
  455. c.Compile(shader, shaderModel, arguments, {});
  456. CheckOperationResultMsgs(c.pCompileResult, expectedErrors, false, false);
  457. }
  458. void DxilModuleTest::VerifyValidatorVersionMatches(
  459. LPCWSTR shaderModel, const std::vector<LPCWSTR> &arguments,
  460. unsigned expectedMajor, unsigned expectedMinor) {
  461. LPCSTR shader =
  462. "[shader(\"pixel\")]"
  463. "float4 main() : SV_Target {\n"
  464. " return 0;\n"
  465. "}\n";
  466. Compiler c(m_dllSupport);
  467. c.Compile(shader, shaderModel, arguments, {});
  468. DxilModule &DM = c.GetDxilModule();
  469. unsigned vMajor, vMinor;
  470. DM.GetValidatorVersion(vMajor, vMinor);
  471. if (expectedMajor == UINT_MAX) {
  472. // Expect current version
  473. VERIFY_ARE_EQUAL(vMajor, c.m_ver.m_ValMajor);
  474. VERIFY_ARE_EQUAL(vMinor, c.m_ver.m_ValMinor);
  475. } else {
  476. VERIFY_ARE_EQUAL(vMajor, expectedMajor);
  477. VERIFY_ARE_EQUAL(vMinor, expectedMinor);
  478. }
  479. }
  480. TEST_F(DxilModuleTest, SetValidatorVersion) {
  481. Compiler c(m_dllSupport);
  482. if (c.SkipDxil_Test(1, 4)) return;
  483. // Current version
  484. VerifyValidatorVersionMatches(L"ps_6_2", {});
  485. VerifyValidatorVersionMatches(L"lib_6_3", {});
  486. // Current version, with validation disabled
  487. VerifyValidatorVersionMatches(L"ps_6_2", {L"-Vd"});
  488. VerifyValidatorVersionMatches(L"lib_6_3", {L"-Vd"});
  489. // Override validator version
  490. VerifyValidatorVersionMatches(L"ps_6_2", {L"-validator-version", L"1.2"}, 1,2);
  491. VerifyValidatorVersionMatches(L"lib_6_3", {L"-validator-version", L"1.3"}, 1,3);
  492. // Override validator version, with validation disabled
  493. VerifyValidatorVersionMatches(L"ps_6_2", {L"-Vd", L"-validator-version", L"1.2"}, 1,2);
  494. VerifyValidatorVersionMatches(L"lib_6_3", {L"-Vd", L"-validator-version", L"1.3"}, 1,3);
  495. // Never can validate (version 0,0):
  496. VerifyValidatorVersionMatches(L"lib_6_1", {L"-Vd"}, 0, 0);
  497. VerifyValidatorVersionMatches(L"lib_6_2", {L"-Vd"}, 0, 0);
  498. VerifyValidatorVersionMatches(L"lib_6_2", {L"-Vd", L"-validator-version", L"0.0"}, 0, 0);
  499. VerifyValidatorVersionMatches(L"lib_6_x", {}, 0, 0);
  500. VerifyValidatorVersionMatches(L"lib_6_x", {L"-validator-version", L"0.0"}, 0, 0);
  501. // Failure cases:
  502. VerifyValidatorVersionFails(L"ps_6_2", {L"-validator-version", L"1.1"}, {
  503. "validator version 1,1 does not support target profile."});
  504. VerifyValidatorVersionFails(L"lib_6_2", {L"-Tlib_6_2"}, {
  505. "Must disable validation for unsupported lib_6_1 or lib_6_2 targets"});
  506. VerifyValidatorVersionFails(L"lib_6_2", {L"-Vd", L"-validator-version", L"1.2"}, {
  507. "-validator-version cannot be used with library profiles lib_6_1 or lib_6_2."});
  508. VerifyValidatorVersionFails(L"lib_6_x", {L"-validator-version", L"1.3"}, {
  509. "Offline library profile cannot be used with non-zero -validator-version."});
  510. }
  511. TEST_F(DxilModuleTest, PayloadQualifier) {
  512. std::vector<LPCWSTR> arguments = { L"-enable-payload-qualifiers" };
  513. Compiler c(m_dllSupport);
  514. LPCSTR shader = "struct [raypayload] Payload\n"
  515. "{\n"
  516. " double a : read(caller, closesthit, anyhit) : write(caller, miss, closesthit);\n"
  517. "};\n\n"
  518. "[shader(\"miss\")]\n"
  519. "void Miss( inout Payload payload ) { payload.a = 4.2; }\n";
  520. c.Compile(shader, L"lib_6_6", arguments, {});
  521. DxilModule &DM = c.GetDxilModule();
  522. const DxilTypeSystem &DTS = DM.GetTypeSystem();
  523. for (auto &p : DTS.GetPayloadAnnotationMap()) {
  524. const DxilPayloadAnnotation &plAnnotation = *p.second;
  525. for (unsigned i = 0; i < plAnnotation.GetNumFields(); ++i) {
  526. const DxilPayloadFieldAnnotation &fieldAnnotation =
  527. plAnnotation.GetFieldAnnotation(i);
  528. VERIFY_IS_TRUE(fieldAnnotation.HasAnnotations());
  529. VERIFY_ARE_EQUAL(DXIL::PayloadAccessQualifier::ReadWrite,
  530. fieldAnnotation.GetPayloadFieldQualifier(
  531. DXIL::PayloadAccessShaderStage::Caller));
  532. VERIFY_ARE_EQUAL(DXIL::PayloadAccessQualifier::ReadWrite,
  533. fieldAnnotation.GetPayloadFieldQualifier(
  534. DXIL::PayloadAccessShaderStage::Closesthit));
  535. VERIFY_ARE_EQUAL(DXIL::PayloadAccessQualifier::Write,
  536. fieldAnnotation.GetPayloadFieldQualifier(
  537. DXIL::PayloadAccessShaderStage::Miss));
  538. VERIFY_ARE_EQUAL(DXIL::PayloadAccessQualifier::Read,
  539. fieldAnnotation.GetPayloadFieldQualifier(
  540. DXIL::PayloadAccessShaderStage::Anyhit));
  541. }
  542. }
  543. }