RewriterTest.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // RewriterTest.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // The following HLSL tests contain static_asserts and are not useful for //
  9. // the HLSL rewriter: more-operators.hlsl, object-operators.hlsl, //
  10. // scalar-operators-assign.hlsl, scalar-operators.hlsl, string.hlsl. //
  11. // They have been omitted. //
  12. // //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #ifndef UNICODE
  15. #define UNICODE
  16. #endif
  17. #ifndef NOMINMAX
  18. #define NOMINMAX
  19. #endif
  20. #include <memory>
  21. #include <vector>
  22. #include <string>
  23. #include <cassert>
  24. #include <sstream>
  25. #include <algorithm>
  26. #include <windows.h>
  27. #include <unknwn.h>
  28. #include "dxc/dxcapi.h"
  29. #include <atlbase.h>
  30. #include <atlfile.h>
  31. #include "WexTestClass.h"
  32. #include "dxc/Test/HLSLTestData.h"
  33. #include "dxc/Test/HlslTestUtils.h"
  34. #include "dxc/Test/DxcTestUtils.h"
  35. #include "dxc/Support/Global.h"
  36. #include "dxc/dxctools.h"
  37. #include "dxc/Support/dxcapi.use.h"
  38. #include "dxc/dxcapi.internal.h"
  39. using namespace std;
  40. using namespace hlsl_test;
  41. class RewriterTest {
  42. public:
  43. BEGIN_TEST_CLASS(RewriterTest)
  44. TEST_CLASS_PROPERTY(L"Parallel", L"true")
  45. TEST_METHOD_PROPERTY(L"Priority", L"0")
  46. END_TEST_CLASS()
  47. TEST_METHOD(RunArrayLength);
  48. TEST_METHOD(RunAttributes);
  49. TEST_METHOD(RunAnonymousStruct);
  50. TEST_METHOD(RunCppErrors);
  51. TEST_METHOD(RunForceExtern);
  52. TEST_METHOD(RunIndexingOperator);
  53. TEST_METHOD(RunIntrinsicExamples);
  54. TEST_METHOD(RunMatrixAssignments);
  55. TEST_METHOD(RunMatrixPackOrientation);
  56. TEST_METHOD(RunMatrixSyntax);
  57. TEST_METHOD(RunPackReg);
  58. TEST_METHOD(RunScalarAssignments);
  59. TEST_METHOD(RunShared);
  60. TEST_METHOD(RunStructAssignments);
  61. TEST_METHOD(RunTemplateChecks);
  62. TEST_METHOD(RunTypemodsSyntax);
  63. TEST_METHOD(RunVarmodsSyntax);
  64. TEST_METHOD(RunVectorAssignments);
  65. TEST_METHOD(RunVectorSyntaxMix);
  66. TEST_METHOD(RunVectorSyntax);
  67. TEST_METHOD(RunIncludes);
  68. TEST_METHOD(RunStructMethods);
  69. TEST_METHOD(RunPredefines);
  70. TEST_METHOD(RunUTF16OneByte);
  71. TEST_METHOD(RunUTF16TwoByte);
  72. TEST_METHOD(RunUTF16ThreeByteBadChar);
  73. TEST_METHOD(RunUTF16ThreeByte);
  74. TEST_METHOD(RunNonUnicode);
  75. TEST_METHOD(RunEffect);
  76. TEST_METHOD(RunSemanticDefines);
  77. TEST_METHOD(RunNoFunctionBody);
  78. TEST_METHOD(RunNoFunctionBodyInclude);
  79. TEST_METHOD(RunNoStatic);
  80. TEST_METHOD(RunKeepUserMacro);
  81. TEST_METHOD(RunExtractUniforms);
  82. TEST_METHOD(RunRewriterFails)
  83. dxc::DxcDllSupport m_dllSupport;
  84. CComPtr<IDxcIncludeHandler> m_pIncludeHandler;
  85. struct VerifyResult {
  86. std::string warnings; // warnings from first compilation
  87. std::string rewrite; // output of rewrite
  88. bool HasSubstringInRewrite(const char* val) {
  89. return std::string::npos != rewrite.find(val);
  90. }
  91. bool HasSubstringInWarnings(const char* val) {
  92. return std::string::npos != warnings.find(val);
  93. }
  94. };
  95. void CreateBlobPinned(_In_bytecount_(size) LPCVOID data, SIZE_T size,
  96. UINT32 codePage, _In_ IDxcBlobEncoding **ppBlob) {
  97. CComPtr<IDxcLibrary> library;
  98. IFT(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  99. IFT(library->CreateBlobWithEncodingFromPinned(data, size, codePage,
  100. ppBlob));
  101. }
  102. VerifyResult CheckVerifies(LPCWSTR path, LPCWSTR goldPath) {
  103. CComPtr<IDxcRewriter> pRewriter;
  104. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  105. return CheckVerifies(pRewriter, path, goldPath);
  106. }
  107. VerifyResult CheckVerifies(IDxcRewriter *pRewriter, LPCWSTR path, LPCWSTR goldPath) {
  108. CComPtr<IDxcOperationResult> pRewriteResult;
  109. RewriteCompareGold(path, goldPath, &pRewriteResult, pRewriter);
  110. VerifyResult toReturn;
  111. CComPtr<IDxcBlob> pResultBlob;
  112. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&pResultBlob));
  113. toReturn.rewrite = BlobToUtf8(pResultBlob);
  114. CComPtr<IDxcBlobEncoding> pErrorsBlob;
  115. VERIFY_SUCCEEDED(pRewriteResult->GetErrorBuffer(&pErrorsBlob));
  116. toReturn.warnings = BlobToUtf8(pErrorsBlob);
  117. return toReturn;
  118. }
  119. HRESULT CreateRewriter(IDxcRewriter** pRewriter) {
  120. if (!m_dllSupport.IsEnabled()) {
  121. VERIFY_SUCCEEDED(m_dllSupport.Initialize());
  122. CComPtr<IDxcLibrary> library;
  123. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  124. VERIFY_SUCCEEDED(library->CreateIncludeHandler(&m_pIncludeHandler));
  125. }
  126. return m_dllSupport.CreateInstance(CLSID_DxcRewriter, pRewriter);
  127. }
  128. HRESULT CreateRewriterWithSemanticDefines(IDxcRewriter** pRewriter, std::vector<LPCWSTR> defines) {
  129. VERIFY_SUCCEEDED(CreateRewriter(pRewriter));
  130. CComPtr<IDxcLangExtensions> pLangExtensions;
  131. VERIFY_SUCCEEDED((*pRewriter)->QueryInterface(&pLangExtensions));
  132. for (LPCWSTR define : defines)
  133. VERIFY_SUCCEEDED(pLangExtensions->RegisterSemanticDefine(define));
  134. return S_OK;
  135. }
  136. VerifyResult CheckVerifiesHLSL(LPCWSTR name, LPCWSTR goldName) {
  137. return CheckVerifies(GetPathToHlslDataFile(name).c_str(),
  138. GetPathToHlslDataFile(goldName).c_str());
  139. }
  140. struct FileWithBlob {
  141. CAtlFile file;
  142. CAtlFileMapping<char> mapping;
  143. CComPtr<IDxcBlobEncoding> BlobEncoding;
  144. FileWithBlob(dxc::DxcDllSupport &support, LPCWSTR path) {
  145. IFT(file.Create(path, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING));
  146. IFT(mapping.MapFile(file));
  147. CComPtr<IDxcLibrary> library;
  148. IFT(support.CreateInstance(CLSID_DxcLibrary, &library));
  149. IFT(library->CreateBlobWithEncodingFromPinned(mapping.GetData(),
  150. mapping.GetMappingSize(),
  151. CP_UTF8, &BlobEncoding));
  152. }
  153. };
  154. bool CompareGold(std::string &firstPass, LPCWSTR goldPath) {
  155. HANDLE goldHandle = CreateFileW(goldPath, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
  156. VERIFY_ARE_NOT_EQUAL(goldHandle, INVALID_HANDLE_VALUE);
  157. CHandle checkedGoldHandle(goldHandle);
  158. DWORD gFileSize = GetFileSize(goldHandle, NULL);
  159. CComHeapPtr<char> gReadBuff;
  160. VERIFY_IS_TRUE(gReadBuff.AllocateBytes(gFileSize));
  161. DWORD gnumActualRead;
  162. VERIFY_WIN32_BOOL_SUCCEEDED(ReadFile(checkedGoldHandle, gReadBuff.m_pData,
  163. gFileSize, &gnumActualRead, NULL));
  164. std::string gold = std::string((LPSTR)gReadBuff, gnumActualRead);
  165. gold.erase(std::remove(gold.begin(), gold.end(), '\r'), gold.end());
  166. // Kept because useful for debugging
  167. //int atChar = 0;
  168. //int numDiffChar = 0;
  169. //while (atChar < result.size){
  170. // char rewriteChar = (firstPass.data())[atChar];
  171. // char goldChar = (gold.data())[atChar];
  172. //
  173. // if (rewriteChar != goldChar){
  174. // numDiffChar++;
  175. // }
  176. // atChar++;
  177. //}
  178. return firstPass.compare(gold) == 0;
  179. }
  180. // Note: Previous versions of this file included a RewriteCompareRewrite method here that rewrote twice and compared
  181. // to check for stable output. It has now been replaced by a new test that checks against a gold baseline.
  182. void RewriteCompareGold(LPCWSTR path, LPCWSTR goldPath,
  183. _COM_Outptr_ IDxcOperationResult **ppResult,
  184. _In_ IDxcRewriter *rewriter) {
  185. // Get the source text from a file
  186. FileWithBlob source(m_dllSupport, path);
  187. const int myDefinesCount = 3;
  188. DxcDefine myDefines[myDefinesCount] = {
  189. {L"myDefine", L"2"}, {L"myDefine3", L"1994"}, {L"myDefine4", nullptr}};
  190. LPCWSTR args[] = {L"-HV", L"2016"};
  191. CComPtr<IDxcRewriter2> rewriter2;
  192. VERIFY_SUCCEEDED(rewriter->QueryInterface(&rewriter2));
  193. // Run rewrite unchanged on the source code
  194. VERIFY_SUCCEEDED(rewriter2->RewriteWithOptions( source.BlobEncoding, path,
  195. args, _countof(args),
  196. myDefines, myDefinesCount,
  197. nullptr, ppResult));
  198. // check for compilation errors
  199. HRESULT hrStatus;
  200. VERIFY_SUCCEEDED((*ppResult)->GetStatus(&hrStatus));
  201. if (!(SUCCEEDED(hrStatus))) {
  202. ::WEX::Logging::Log::Error(L"\nCompilation failed.\n");
  203. CComPtr<IDxcBlobEncoding> pErrorBuffer;
  204. IFT((*ppResult)->GetErrorBuffer(&pErrorBuffer));
  205. std::wstring errorStr = BlobToUtf16(pErrorBuffer);
  206. ::WEX::Logging::Log::Error(errorStr.data());
  207. VERIFY_SUCCEEDED(hrStatus);
  208. return;
  209. }
  210. CComPtr<IDxcBlob> pRewriteResult;
  211. IFT((*ppResult)->GetResult(&pRewriteResult));
  212. std::string firstPass = BlobToUtf8(pRewriteResult);
  213. if (CompareGold(firstPass, goldPath)) {
  214. return;
  215. }
  216. // Log things out before failing.
  217. std::wstring TestFileName(path);
  218. int index1 = TestFileName.find_last_of(L"\\");
  219. int index2 = TestFileName.find_last_of(L".");
  220. TestFileName = TestFileName.substr(index1+1, index2 - (index1+1));
  221. wchar_t TempPath[MAX_PATH];
  222. DWORD length = GetTempPathW(MAX_PATH, TempPath);
  223. VERIFY_WIN32_BOOL_SUCCEEDED(length != 0);
  224. std::wstring PrintName(TempPath);
  225. PrintName += TestFileName;
  226. PrintName += L"_rewrite_test_pass.txt";
  227. CHandle checkedWHandle(CreateNewFileForReadWrite(PrintName.data()));
  228. LPDWORD wnumWrite = 0;
  229. VERIFY_WIN32_BOOL_SUCCEEDED(WriteFile(checkedWHandle, firstPass.data(),
  230. firstPass.size(), wnumWrite, NULL));
  231. std::wstringstream ss;
  232. ss << L"\nMismatch occurred between rewriter output and expected "
  233. L"output. To see the differences, run:\n"
  234. L"diff " << goldPath << L" " << PrintName << L"\n";
  235. ::WEX::Logging::Log::Error(ss.str().c_str());
  236. }
  237. bool RewriteCompareGoldInclude(LPCWSTR path, LPCWSTR goldPath,
  238. unsigned rewriteOption) {
  239. CComPtr<IDxcRewriter> pRewriter;
  240. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  241. CComPtr<IDxcOperationResult> pRewriteResult;
  242. std::wstring fileName = GetPathToHlslDataFile(path);
  243. // Get the source text from a file
  244. FileWithBlob source(m_dllSupport, fileName.c_str());
  245. const int myDefinesCount = 3;
  246. DxcDefine myDefines[myDefinesCount] = {
  247. {L"myDefine", L"2"}, {L"myDefine3", L"1994"}, {L"myDefine4", nullptr}};
  248. // Run rewrite no function body on the source code
  249. VERIFY_SUCCEEDED(pRewriter->RewriteUnchangedWithInclude(
  250. source.BlobEncoding, fileName.c_str(), myDefines, myDefinesCount,
  251. m_pIncludeHandler, rewriteOption, &pRewriteResult));
  252. CComPtr<IDxcBlob> result;
  253. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
  254. std::string rewriteText = BlobToUtf8(result);
  255. return CompareGold(rewriteText, GetPathToHlslDataFile(goldPath).c_str());
  256. }
  257. };
  258. TEST_F(RewriterTest, RunArrayLength) {
  259. CheckVerifiesHLSL(L"rewriter\\array-length-rw.hlsl", L"rewriter\\correct_rewrites\\array-length-rw_gold.hlsl");
  260. }
  261. TEST_F(RewriterTest, RunAttributes) {
  262. CheckVerifiesHLSL(L"rewriter\\attributes_noerr.hlsl", L"rewriter\\correct_rewrites\\attributes_gold.hlsl");
  263. }
  264. TEST_F(RewriterTest, RunAnonymousStruct) {
  265. CheckVerifiesHLSL(L"rewriter\\anonymous_struct.hlsl", L"rewriter\\correct_rewrites\\anonymous_struct_gold.hlsl");
  266. }
  267. TEST_F(RewriterTest, RunCppErrors) {
  268. CheckVerifiesHLSL(L"rewriter\\cpp-errors_noerr.hlsl", L"rewriter\\correct_rewrites\\cpp-errors_gold.hlsl");
  269. }
  270. TEST_F(RewriterTest, RunIndexingOperator) {
  271. CheckVerifiesHLSL(L"rewriter\\indexing-operator_noerr.hlsl", L"rewriter\\correct_rewrites\\indexing-operator_gold.hlsl");
  272. }
  273. TEST_F(RewriterTest, RunIntrinsicExamples) {
  274. CheckVerifiesHLSL(L"rewriter\\intrinsic-examples_noerr.hlsl", L"rewriter\\correct_rewrites\\intrinsic-examples_gold.hlsl");
  275. }
  276. TEST_F(RewriterTest, RunMatrixAssignments) {
  277. CheckVerifiesHLSL(L"rewriter\\matrix-assignments_noerr.hlsl", L"rewriter\\correct_rewrites\\matrix-assignments_gold.hlsl");
  278. }
  279. TEST_F(RewriterTest, RunMatrixPackOrientation) {
  280. CheckVerifiesHLSL(L"rewriter\\matrix-pack-orientation.hlsl", L"rewriter\\correct_rewrites\\matrix-pack-orientation_gold.hlsl");
  281. }
  282. TEST_F(RewriterTest, RunMatrixSyntax) {
  283. CheckVerifiesHLSL(L"rewriter\\matrix-syntax_noerr.hlsl", L"rewriter\\correct_rewrites\\matrix-syntax_gold.hlsl");
  284. }
  285. TEST_F(RewriterTest, RunPackReg) {
  286. CheckVerifiesHLSL(L"rewriter\\packreg_noerr.hlsl", L"rewriter\\correct_rewrites\\packreg_gold.hlsl");
  287. }
  288. TEST_F(RewriterTest, RunScalarAssignments) {
  289. CheckVerifiesHLSL(L"rewriter\\scalar-assignments_noerr.hlsl", L"rewriter\\correct_rewrites\\scalar-assignments_gold.hlsl");
  290. }
  291. TEST_F(RewriterTest, RunShared) {
  292. CheckVerifiesHLSL(L"rewriter\\shared.hlsl", L"rewriter\\correct_rewrites\\shared.hlsl");
  293. }
  294. TEST_F(RewriterTest, RunStructAssignments) {
  295. CheckVerifiesHLSL(L"rewriter\\struct-assignments_noerr.hlsl", L"rewriter\\correct_rewrites\\struct-assignments_gold.hlsl");
  296. }
  297. TEST_F(RewriterTest, RunTemplateChecks) {
  298. CheckVerifiesHLSL(L"rewriter\\template-checks_noerr.hlsl", L"rewriter\\correct_rewrites\\template-checks_gold.hlsl");
  299. }
  300. TEST_F(RewriterTest, RunTypemodsSyntax) {
  301. CheckVerifiesHLSL(L"rewriter\\typemods-syntax_noerr.hlsl", L"rewriter\\correct_rewrites\\typemods-syntax_gold.hlsl");
  302. }
  303. TEST_F(RewriterTest, RunVarmodsSyntax) {
  304. CheckVerifiesHLSL(L"rewriter\\varmods-syntax_noerr.hlsl", L"rewriter\\correct_rewrites\\varmods-syntax_gold.hlsl");
  305. }
  306. TEST_F(RewriterTest, RunVectorAssignments) {
  307. CheckVerifiesHLSL(L"rewriter\\vector-assignments_noerr.hlsl", L"rewriter\\correct_rewrites\\vector-assignments_gold.hlsl");
  308. }
  309. TEST_F(RewriterTest, RunVectorSyntaxMix) {
  310. CheckVerifiesHLSL(L"rewriter\\vector-syntax-mix_noerr.hlsl", L"rewriter\\correct_rewrites\\vector-syntax-mix_gold.hlsl");
  311. }
  312. TEST_F(RewriterTest, RunVectorSyntax) {
  313. CheckVerifiesHLSL(L"rewriter\\vector-syntax_noerr.hlsl", L"rewriter\\correct_rewrites\\vector-syntax_gold.hlsl");
  314. }
  315. TEST_F(RewriterTest, RunIncludes) {
  316. VERIFY_IS_TRUE(RewriteCompareGoldInclude(
  317. L"rewriter\\includes.hlsl",
  318. L"rewriter\\correct_rewrites\\includes_gold.hlsl",
  319. RewriterOptionMask::Default));
  320. }
  321. TEST_F(RewriterTest, RunNoFunctionBodyInclude) {
  322. VERIFY_IS_TRUE(RewriteCompareGoldInclude(
  323. L"rewriter\\includes.hlsl",
  324. L"rewriter\\correct_rewrites\\includes_gold_nobody.hlsl",
  325. RewriterOptionMask::SkipFunctionBody));
  326. }
  327. TEST_F(RewriterTest, RunStructMethods) {
  328. CheckVerifiesHLSL(L"rewriter\\struct-methods.hlsl", L"rewriter\\correct_rewrites\\struct-methods_gold.hlsl");
  329. }
  330. TEST_F(RewriterTest, RunPredefines) {
  331. CheckVerifiesHLSL(L"rewriter\\predefines.hlsl", L"rewriter\\correct_rewrites\\predefines_gold.hlsl");
  332. }
  333. static const UINT32 CP_UTF16 = 1200;
  334. TEST_F(RewriterTest, RunUTF16OneByte) {
  335. CComPtr<IDxcRewriter> pRewriter;
  336. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  337. CComPtr<IDxcOperationResult> pRewriteResult;
  338. WCHAR utf16text[] = { L"\x0069\x006e\x0074\x0020\x0069\x003b" }; // "int i;"
  339. CComPtr<IDxcBlobEncoding> source;
  340. CreateBlobPinned(utf16text, sizeof(utf16text), CP_UTF16, &source);
  341. VERIFY_SUCCEEDED(pRewriter->RewriteUnchanged(source, 0, 0, &pRewriteResult));
  342. CComPtr<IDxcBlob> result;
  343. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
  344. VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x63\x6f\x6e\x73\x74\x20\x69\x6e\x74\x20\x69\x3b\n") == 0); // const added by default
  345. }
  346. TEST_F(RewriterTest, RunUTF16TwoByte) {
  347. CComPtr<IDxcRewriter> pRewriter;
  348. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  349. CComPtr<IDxcOperationResult> pRewriteResult;
  350. WCHAR utf16text[] = { L"\x0069\x006e\x0074\x0020\x00ed\x00f1\x0167\x003b" }; // "int (i w/ acute)(n w/tilde)(t w/ 2 strokes);"
  351. CComPtr<IDxcBlobEncoding> source;
  352. CreateBlobPinned(utf16text, sizeof(utf16text), CP_UTF16, &source);
  353. VERIFY_SUCCEEDED(pRewriter->RewriteUnchanged(source, 0, 0, &pRewriteResult));
  354. CComPtr<IDxcBlob> result;
  355. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
  356. VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x63\x6f\x6e\x73\x74\x20\x69\x6e\x74\x20\xc3\xad\xc3\xb1\xc5\xa7\x3b\n") == 0); // const added by default
  357. }
  358. TEST_F(RewriterTest, RunUTF16ThreeByteBadChar) {
  359. CComPtr<IDxcRewriter> pRewriter;
  360. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  361. CComPtr<IDxcOperationResult> pRewriteResult;
  362. WCHAR utf16text[] = { L"\x0069\x006e\x0074\x0020\x0041\x2655\x265a\x003b" }; // "int A(white queen)(black king);"
  363. CComPtr<IDxcBlobEncoding> source;
  364. CreateBlobPinned(utf16text, sizeof(utf16text), CP_UTF16, &source);
  365. VERIFY_SUCCEEDED(pRewriter->RewriteUnchanged(source, 0, 0, &pRewriteResult));
  366. CComPtr<IDxcBlob> result;
  367. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
  368. VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x63\x6f\x6e\x73\x74\x20\x69\x6e\x74\x20\x41\x3b\n") == 0); //"const int A;" -> should remove the weird characters
  369. }
  370. TEST_F(RewriterTest, RunUTF16ThreeByte) {
  371. CComPtr<IDxcRewriter> pRewriter;
  372. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  373. CComPtr<IDxcOperationResult> pRewriteResult;
  374. WCHAR utf16text[] = { L"\x0069\x006e\x0074\x0020\x1e8b\x003b" }; // "int (x with dot above);"
  375. CComPtr<IDxcBlobEncoding> source;
  376. CreateBlobPinned(utf16text, sizeof(utf16text), CP_UTF16, &source);
  377. VERIFY_SUCCEEDED(pRewriter->RewriteUnchanged(source, 0, 0, &pRewriteResult));
  378. CComPtr<IDxcBlob> result;
  379. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
  380. VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x63\x6f\x6e\x73\x74\x20\x69\x6e\x74\x20\xe1\xba\x8b\x3b\n") == 0); // const added by default
  381. }
  382. TEST_F(RewriterTest, RunNonUnicode) {
  383. CComPtr<IDxcRewriter> pRewriter;
  384. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  385. CComPtr<IDxcOperationResult> pRewriteResult;
  386. char greektext[] = { "\x69\x6e\x74\x20\xe1\xe2\xe3\x3b" }; // "int (small alpha)(small beta)(small kappa);"
  387. CComPtr<IDxcBlobEncoding> source;
  388. CreateBlobPinned(greektext, sizeof(greektext), 1253, &source); // 1253 == ANSI Greek
  389. VERIFY_SUCCEEDED(pRewriter->RewriteUnchanged(source, 0, 0, &pRewriteResult));
  390. CComPtr<IDxcBlob> result;
  391. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
  392. VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x63\x6f\x6e\x73\x74\x20\x69\x6e\x74\x20\xce\xb1\xce\xb2\xce\xb3\x3b\n") == 0); // const added by default
  393. }
  394. TEST_F(RewriterTest, RunEffect) {
  395. CheckVerifiesHLSL(L"rewriter\\effects-syntax_noerr.hlsl", L"rewriter\\correct_rewrites\\effects-syntax_gold.hlsl");
  396. }
  397. TEST_F(RewriterTest, RunSemanticDefines) {
  398. CComPtr<IDxcRewriter> pRewriter;
  399. VERIFY_SUCCEEDED(CreateRewriterWithSemanticDefines(&pRewriter, {L"SD_*"}));
  400. CheckVerifies(pRewriter, hlsl_test::GetPathToHlslDataFile(L"rewriter\\semantic-defines.hlsl").c_str(),
  401. hlsl_test::GetPathToHlslDataFile(L"rewriter\\correct_rewrites\\semantic-defines_gold.hlsl").c_str());
  402. }
  403. TEST_F(RewriterTest, RunNoFunctionBody) {
  404. CComPtr<IDxcRewriter> pRewriter;
  405. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  406. CComPtr<IDxcOperationResult> pRewriteResult;
  407. // Get the source text from a file
  408. FileWithBlob source(
  409. m_dllSupport,
  410. GetPathToHlslDataFile(L"rewriter\\vector-assignments_noerr.hlsl")
  411. .c_str());
  412. const int myDefinesCount = 3;
  413. DxcDefine myDefines[myDefinesCount] = {
  414. {L"myDefine", L"2"}, {L"myDefine3", L"1994"}, {L"myDefine4", nullptr}};
  415. // Run rewrite no function body on the source code
  416. VERIFY_SUCCEEDED(pRewriter->RewriteUnchangedWithInclude(
  417. source.BlobEncoding, L"vector-assignments_noerr.hlsl", myDefines,
  418. myDefinesCount, /*pIncludeHandler*/ nullptr, RewriterOptionMask::SkipFunctionBody,
  419. &pRewriteResult));
  420. CComPtr<IDxcBlob> result;
  421. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
  422. // Function decl only.
  423. VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(),
  424. "// Rewrite unchanged result:\nfloat pick_one(float2 "
  425. "f2);\nvoid main();\n") == 0);
  426. }
  427. TEST_F(RewriterTest, RunNoStatic) {
  428. CComPtr<IDxcRewriter> pRewriter;
  429. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  430. CComPtr<IDxcOperationResult> pRewriteResult;
  431. // Get the source text from a file
  432. FileWithBlob source(
  433. m_dllSupport,
  434. GetPathToHlslDataFile(L"rewriter\\attributes_noerr.hlsl")
  435. .c_str());
  436. const int myDefinesCount = 3;
  437. DxcDefine myDefines[myDefinesCount] = {
  438. {L"myDefine", L"2"}, {L"myDefine3", L"1994"}, {L"myDefine4", nullptr}};
  439. // Run rewrite no function body on the source code
  440. VERIFY_SUCCEEDED(pRewriter->RewriteUnchangedWithInclude(
  441. source.BlobEncoding, L"attributes_noerr.hlsl", myDefines, myDefinesCount,
  442. /*pIncludeHandler*/ nullptr,
  443. RewriterOptionMask::SkipFunctionBody | RewriterOptionMask::SkipStatic,
  444. &pRewriteResult));
  445. CComPtr<IDxcBlob> result;
  446. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
  447. std::string strResult = BlobToUtf8(result);
  448. // No static.
  449. VERIFY_IS_TRUE(strResult.find("static") == std::string::npos);
  450. }
  451. TEST_F(RewriterTest, RunForceExtern) { CComPtr<IDxcRewriter> pRewriter;
  452. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  453. CComPtr<IDxcOperationResult> pRewriteResult;
  454. // Get the source text from a file
  455. FileWithBlob source(
  456. m_dllSupport,
  457. GetPathToHlslDataFile(L"rewriter\\force_extern.hlsl")
  458. .c_str());
  459. const int myDefinesCount = 3;
  460. DxcDefine myDefines[myDefinesCount] = {
  461. {L"myDefine", L"2"}, {L"myDefine3", L"1994"}, {L"myDefine4", nullptr}};
  462. // Run rewrite no function body on the source code
  463. VERIFY_SUCCEEDED(pRewriter->RewriteUnchangedWithInclude(
  464. source.BlobEncoding, L"vector-assignments_noerr.hlsl", myDefines,
  465. myDefinesCount, /*pIncludeHandler*/ nullptr,
  466. RewriterOptionMask::SkipFunctionBody |
  467. RewriterOptionMask::GlobalExternByDefault,
  468. &pRewriteResult));
  469. CComPtr<IDxcBlob> result;
  470. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
  471. // Function decl only.
  472. VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(),
  473. "// Rewrite unchanged result:\n\
  474. extern const float a;\n\
  475. namespace b {\n\
  476. extern const float c;\n\
  477. namespace d {\n\
  478. extern const float e;\n\
  479. }\n\
  480. }\n\
  481. static int f;\n\
  482. float4 main() : SV_Target;\n") == 0);
  483. }
  484. TEST_F(RewriterTest, RunKeepUserMacro) { CComPtr<IDxcRewriter> pRewriter;
  485. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  486. CComPtr<IDxcOperationResult> pRewriteResult;
  487. // Get the source text from a file
  488. FileWithBlob source(
  489. m_dllSupport,
  490. GetPathToHlslDataFile(L"rewriter\\predefines2.hlsl")
  491. .c_str());
  492. const int myDefinesCount = 3;
  493. DxcDefine myDefines[myDefinesCount] = {
  494. {L"myDefine", L"2"}, {L"myDefine3", L"1994"}, {L"myDefine4", nullptr}};
  495. // Run rewrite no function body on the source code
  496. VERIFY_SUCCEEDED(pRewriter->RewriteUnchangedWithInclude(
  497. source.BlobEncoding, L"vector-assignments_noerr.hlsl", myDefines,
  498. myDefinesCount, /*pIncludeHandler*/ nullptr,
  499. RewriterOptionMask::KeepUserMacro,
  500. &pRewriteResult));
  501. CComPtr<IDxcBlob> result;
  502. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
  503. // Function decl only.
  504. VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(),
  505. "// Rewrite unchanged result:\n\
  506. const float x = 1;\n\
  507. float test(float a, float b) {\n\
  508. return ((a) + (b));\n\
  509. }\n\
  510. \n\n\n\
  511. // Macros:\n\
  512. #define X 1\n\
  513. #define Y(A, B) ( ( A ) + ( B ) )\n\
  514. ") == 0);
  515. }
  516. TEST_F(RewriterTest, RunExtractUniforms) {
  517. CComPtr<IDxcRewriter> pRewriter;
  518. CComPtr<IDxcRewriter2> pRewriter2;
  519. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  520. VERIFY_SUCCEEDED(pRewriter->QueryInterface(&pRewriter2));
  521. CComPtr<IDxcOperationResult> pRewriteResult;
  522. // Get the source text from a file
  523. FileWithBlob source(
  524. m_dllSupport,
  525. GetPathToHlslDataFile(L"rewriter\\rewrite-uniforms.hlsl")
  526. .c_str());
  527. LPCWSTR compileOptions[] = {L"-E", L"FloatFunc", L"-extract-entry-uniforms"};
  528. // Run rewrite on the source code to move uniform params to globals
  529. VERIFY_SUCCEEDED(pRewriter2->RewriteWithOptions(
  530. source.BlobEncoding, L"rewrite-uniforms.hlsl",
  531. compileOptions, _countof(compileOptions),
  532. nullptr, 0, nullptr, &pRewriteResult));
  533. CComPtr<IDxcBlob> result;
  534. VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
  535. VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(),
  536. "// Rewrite unchanged result:\n\
  537. [RootSignature(\"RootFlags(0),DescriptorTable(UAV(u0, numDescriptors = 1), CBV(b0, numDescriptors = 1))\")]\n\
  538. [numthreads(4, 8, 16)]\n\
  539. void IntFunc(uint3 id : SV_DispatchThreadID, uniform RWStructuredBuffer<int> buf, uniform uint ui) {\n\
  540. buf[id.x + id.y + id.z] = id.x + ui;\n\
  541. }\n\
  542. \n\
  543. \n\
  544. uniform RWStructuredBuffer<float> buf;\n\
  545. cbuffer _Params {\n\
  546. uniform uint ui;\n\
  547. }\n\
  548. [RootSignature(\"RootFlags(0),DescriptorTable(UAV(u0, numDescriptors = 1), CBV(b0, numDescriptors = 1))\")]\n\
  549. [numthreads(4, 8, 16)]\n\
  550. void FloatFunc(uint3 id : SV_DispatchThreadID) {\n\
  551. buf[id.x + id.y + id.z] = id.x;\n\
  552. }\n\
  553. \n\
  554. ") == 0);
  555. }
  556. TEST_F(RewriterTest, RunRewriterFails) {
  557. CComPtr<IDxcRewriter> pRewriter;
  558. CComPtr<IDxcRewriter2> pRewriter2;
  559. VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
  560. VERIFY_SUCCEEDED(pRewriter->QueryInterface(&pRewriter2));
  561. // Get the source text from a file
  562. std::wstring sourceName = GetPathToHlslDataFile(L"rewriter\\array-length-rw.hlsl");
  563. FileWithBlob source(m_dllSupport, sourceName.c_str());
  564. // Compilation should fail with these options
  565. CComPtr<IDxcOperationResult> pRewriteResult;
  566. LPCWSTR compileOptions[] = {L"-HV", L"2018"};
  567. // Run rewrite on the source code
  568. VERIFY_SUCCEEDED(pRewriter2->RewriteWithOptions(source.BlobEncoding, sourceName.c_str(), compileOptions, 2,
  569. nullptr, 0, nullptr, &pRewriteResult));
  570. // Verify it failed
  571. HRESULT hrStatus;
  572. VERIFY_SUCCEEDED(pRewriteResult->GetStatus(&hrStatus));
  573. VERIFY_FAILED(hrStatus);
  574. ::WEX::Logging::Log::Comment(L"\nCompilation failed as expected.\n");
  575. CComPtr<IDxcBlobEncoding> pErrorBuffer;
  576. IFT(pRewriteResult->GetErrorBuffer(&pErrorBuffer));
  577. std::wstring errorStr = BlobToUtf16(pErrorBuffer);
  578. ::WEX::Logging::Log::Comment(errorStr.data());
  579. VERIFY_IS_TRUE(errorStr.find(L"Length is only allowed for HLSL 2016 and lower.") >= 0);
  580. }