RewriterTest.cpp 27 KB

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