CompilerTest.cpp 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // CompilerTest.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. // Provides tests for the compiler API. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef UNICODE
  12. #define UNICODE
  13. #endif
  14. #include <memory>
  15. #include <vector>
  16. #include <string>
  17. #include <map>
  18. #include <cassert>
  19. #include <sstream>
  20. #include <algorithm>
  21. #include <cfloat>
  22. #include "dxc/DxilContainer/DxilContainer.h"
  23. #include "dxc/Support/WinIncludes.h"
  24. #include "dxc/dxcapi.h"
  25. #include "dxc/dxcpix.h"
  26. #ifdef _WIN32
  27. #include <atlfile.h>
  28. #include "dia2.h"
  29. #endif
  30. #include "dxc/Test/HLSLTestData.h"
  31. #include "dxc/Test/HlslTestUtils.h"
  32. #include "dxc/Test/DxcTestUtils.h"
  33. #include "llvm/Support/raw_os_ostream.h"
  34. #include "dxc/Support/Global.h"
  35. #include "dxc/Support/dxcapi.use.h"
  36. #include "dxc/Support/microcom.h"
  37. #include "dxc/Support/HLSLOptions.h"
  38. #include "dxc/Support/Unicode.h"
  39. #include <fstream>
  40. #include "llvm/Support/FileSystem.h"
  41. #include "llvm/Support/MSFileSystem.h"
  42. #include "llvm/Support/Path.h"
  43. #include "llvm/ADT/SmallString.h"
  44. #include "llvm/ADT/StringSwitch.h"
  45. using namespace std;
  46. using namespace hlsl_test;
  47. class TestIncludeHandler : public IDxcIncludeHandler {
  48. DXC_MICROCOM_REF_FIELD(m_dwRef)
  49. public:
  50. DXC_MICROCOM_ADDREF_RELEASE_IMPL(m_dwRef)
  51. dxc::DxcDllSupport &m_dllSupport;
  52. HRESULT m_defaultErrorCode = E_FAIL;
  53. TestIncludeHandler(dxc::DxcDllSupport &dllSupport) : m_dwRef(0), m_dllSupport(dllSupport), callIndex(0) { }
  54. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override {
  55. return DoBasicQueryInterface<IDxcIncludeHandler>(this, iid, ppvObject);
  56. }
  57. struct LoadSourceCallInfo {
  58. std::wstring Filename; // Filename as written in #include statement
  59. LoadSourceCallInfo(LPCWSTR pFilename) :
  60. Filename(pFilename) { }
  61. };
  62. std::vector<LoadSourceCallInfo> CallInfos;
  63. std::wstring GetAllFileNames() const {
  64. std::wstringstream s;
  65. for (size_t i = 0; i < CallInfos.size(); ++i) {
  66. s << CallInfos[i].Filename << ';';
  67. }
  68. return s.str();
  69. }
  70. struct LoadSourceCallResult {
  71. HRESULT hr;
  72. std::string source;
  73. UINT32 codePage;
  74. LoadSourceCallResult() : hr(E_FAIL), codePage(0) { }
  75. LoadSourceCallResult(const char *pSource, UINT32 codePage = CP_UTF8) : hr(S_OK), source(pSource), codePage(codePage) { }
  76. };
  77. std::vector<LoadSourceCallResult> CallResults;
  78. size_t callIndex;
  79. HRESULT STDMETHODCALLTYPE LoadSource(
  80. _In_ LPCWSTR pFilename, // Filename as written in #include statement
  81. _COM_Outptr_ IDxcBlob **ppIncludeSource // Resultant source object for included file
  82. ) override {
  83. CallInfos.push_back(LoadSourceCallInfo(pFilename));
  84. *ppIncludeSource = nullptr;
  85. if (callIndex >= CallResults.size()) {
  86. return m_defaultErrorCode;
  87. }
  88. if (FAILED(CallResults[callIndex].hr)) {
  89. return CallResults[callIndex++].hr;
  90. }
  91. MultiByteStringToBlob(m_dllSupport, CallResults[callIndex].source,
  92. CallResults[callIndex].codePage, ppIncludeSource);
  93. return CallResults[callIndex++].hr;
  94. }
  95. };
  96. #ifdef _WIN32
  97. class CompilerTest {
  98. #else
  99. class CompilerTest : public ::testing::Test {
  100. #endif
  101. public:
  102. BEGIN_TEST_CLASS(CompilerTest)
  103. TEST_CLASS_PROPERTY(L"Parallel", L"true")
  104. TEST_METHOD_PROPERTY(L"Priority", L"0")
  105. END_TEST_CLASS()
  106. TEST_CLASS_SETUP(InitSupport);
  107. TEST_METHOD(CompileWhenDefinesThenApplied)
  108. TEST_METHOD(CompileWhenDefinesManyThenApplied)
  109. TEST_METHOD(CompileWhenEmptyThenFails)
  110. TEST_METHOD(CompileWhenIncorrectThenFails)
  111. TEST_METHOD(CompileWhenWorksThenDisassembleWorks)
  112. TEST_METHOD(CompileWhenDebugWorksThenStripDebug)
  113. TEST_METHOD(CompileWhenWorksThenAddRemovePrivate)
  114. TEST_METHOD(CompileThenAddCustomDebugName)
  115. TEST_METHOD(CompileWithRootSignatureThenStripRootSignature)
  116. TEST_METHOD(CompileWhenIncludeThenLoadInvoked)
  117. TEST_METHOD(CompileWhenIncludeThenLoadUsed)
  118. TEST_METHOD(CompileWhenIncludeAbsoluteThenLoadAbsolute)
  119. TEST_METHOD(CompileWhenIncludeLocalThenLoadRelative)
  120. TEST_METHOD(CompileWhenIncludeSystemThenLoadNotRelative)
  121. TEST_METHOD(CompileWhenIncludeSystemMissingThenLoadAttempt)
  122. TEST_METHOD(CompileWhenIncludeFlagsThenIncludeUsed)
  123. TEST_METHOD(CompileWhenIncludeMissingThenFail)
  124. TEST_METHOD(CompileWhenIncludeHasPathThenOK)
  125. TEST_METHOD(CompileWhenIncludeEmptyThenOK)
  126. TEST_METHOD(CompileWhenODumpThenPassConfig)
  127. TEST_METHOD(CompileWhenODumpThenOptimizerMatch)
  128. TEST_METHOD(CompileWhenVdThenProducesDxilContainer)
  129. #if _ITERATOR_DEBUG_LEVEL==0
  130. // CompileWhenNoMemThenOOM can properly detect leaks only when debug iterators are disabled
  131. BEGIN_TEST_METHOD(CompileWhenNoMemThenOOM)
  132. // Disabled because there are problems where we try to allocate memory in destructors,
  133. // which causes more bad_alloc() throws while unwinding bad_alloc(), which asserts
  134. // If only failing one allocation, there are allocations where failing them is lost,
  135. // such as in ~raw_string_ostream(), where it flushes, then eats bad_alloc(), if thrown.
  136. TEST_METHOD_PROPERTY(L"Ignore", L"true")
  137. END_TEST_METHOD()
  138. #endif
  139. TEST_METHOD(CompileWhenShaderModelMismatchAttributeThenFail)
  140. TEST_METHOD(CompileBadHlslThenFail)
  141. TEST_METHOD(CompileLegacyShaderModelThenFail)
  142. TEST_METHOD(CompileWhenRecursiveAlbeitStaticTermThenFail)
  143. TEST_METHOD(CompileWhenRecursiveThenFail)
  144. TEST_METHOD(CompileHlsl2015ThenFail)
  145. TEST_METHOD(CompileHlsl2016ThenOK)
  146. TEST_METHOD(CompileHlsl2017ThenOK)
  147. TEST_METHOD(CompileHlsl2018ThenOK)
  148. TEST_METHOD(CompileHlsl2019ThenFail)
  149. TEST_METHOD(CodeGenFloatingPointEnvironment)
  150. TEST_METHOD(CodeGenInclude)
  151. TEST_METHOD(CodeGenLibCsEntry)
  152. TEST_METHOD(CodeGenLibCsEntry2)
  153. TEST_METHOD(CodeGenLibCsEntry3)
  154. TEST_METHOD(CodeGenLibEntries)
  155. TEST_METHOD(CodeGenLibEntries2)
  156. TEST_METHOD(CodeGenLibNoAlias)
  157. TEST_METHOD(CodeGenLibResource)
  158. TEST_METHOD(CodeGenLibUnusedFunc)
  159. TEST_METHOD(CodeGenRootSigProfile)
  160. TEST_METHOD(CodeGenRootSigProfile2)
  161. TEST_METHOD(CodeGenRootSigProfile5)
  162. TEST_METHOD(PreprocessWhenValidThenOK)
  163. TEST_METHOD(LibGVStore)
  164. TEST_METHOD(PreprocessWhenExpandTokenPastingOperandThenAccept)
  165. TEST_METHOD(PreprocessWithDebugOptsThenOk)
  166. TEST_METHOD(WhenSigMismatchPCFunctionThenFail)
  167. TEST_METHOD(CompileOtherModesWithDebugOptsThenOk)
  168. TEST_METHOD(BatchSamples)
  169. TEST_METHOD(BatchD3DReflect)
  170. TEST_METHOD(BatchDxil)
  171. TEST_METHOD(BatchHLSL)
  172. TEST_METHOD(BatchInfra)
  173. TEST_METHOD(BatchPasses)
  174. TEST_METHOD(BatchShaderTargets)
  175. TEST_METHOD(BatchValidation)
  176. TEST_METHOD(SubobjectCodeGenErrors)
  177. BEGIN_TEST_METHOD(ManualFileCheckTest)
  178. TEST_METHOD_PROPERTY(L"Ignore", L"true")
  179. END_TEST_METHOD()
  180. // Batch directories
  181. BEGIN_TEST_METHOD(CodeGenHashStability)
  182. TEST_METHOD_PROPERTY(L"Priority", L"2")
  183. END_TEST_METHOD()
  184. dxc::DxcDllSupport m_dllSupport;
  185. VersionSupportInfo m_ver;
  186. void CreateBlobPinned(_In_bytecount_(size) LPCVOID data, SIZE_T size,
  187. UINT32 codePage, _Outptr_ IDxcBlobEncoding **ppBlob) {
  188. CComPtr<IDxcLibrary> library;
  189. IFT(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  190. IFT(library->CreateBlobWithEncodingFromPinned(data, size, codePage,
  191. ppBlob));
  192. }
  193. void CreateBlobFromFile(LPCWSTR name, _Outptr_ IDxcBlobEncoding **ppBlob) {
  194. CComPtr<IDxcLibrary> library;
  195. IFT(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  196. const std::wstring path = hlsl_test::GetPathToHlslDataFile(name);
  197. IFT(library->CreateBlobFromFile(path.c_str(), nullptr, ppBlob));
  198. }
  199. void CreateBlobFromText(_In_z_ const char *pText,
  200. _Outptr_ IDxcBlobEncoding **ppBlob) {
  201. CreateBlobPinned(pText, strlen(pText) + 1, CP_UTF8, ppBlob);
  202. }
  203. HRESULT CreateCompiler(IDxcCompiler **ppResult) {
  204. return m_dllSupport.CreateInstance(CLSID_DxcCompiler, ppResult);
  205. }
  206. #ifdef _WIN32 // No ContainerBuilder support yet
  207. HRESULT CreateContainerBuilder(IDxcContainerBuilder **ppResult) {
  208. return m_dllSupport.CreateInstance(CLSID_DxcContainerBuilder, ppResult);
  209. }
  210. #endif
  211. template <typename T, typename TDefault, typename TIface>
  212. void WriteIfValue(TIface *pSymbol, std::wstringstream &o,
  213. TDefault defaultValue, LPCWSTR valueLabel,
  214. HRESULT (__stdcall TIface::*pFn)(T *)) {
  215. T value;
  216. HRESULT hr = (pSymbol->*(pFn))(&value);
  217. if (SUCCEEDED(hr) && value != defaultValue) {
  218. o << L", " << valueLabel << L": " << value;
  219. }
  220. }
  221. std::string GetOption(std::string &cmd, char *opt) {
  222. std::string option = cmd.substr(cmd.find(opt));
  223. option = option.substr(option.find_first_of(' '));
  224. option = option.substr(option.find_first_not_of(' '));
  225. return option.substr(0, option.find_first_of(' '));
  226. }
  227. void CodeGenTest(std::wstring name) {
  228. CComPtr<IDxcCompiler> pCompiler;
  229. CComPtr<IDxcOperationResult> pResult;
  230. CComPtr<IDxcBlobEncoding> pSource;
  231. name.insert(0, L"..\\CodeGenHLSL\\");
  232. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  233. CreateBlobFromFile(name.c_str(), &pSource);
  234. std::string cmdLine = GetFirstLine(name.c_str());
  235. llvm::StringRef argsRef = cmdLine;
  236. llvm::SmallVector<llvm::StringRef, 8> splitArgs;
  237. argsRef.split(splitArgs, " ");
  238. hlsl::options::MainArgs argStrings(splitArgs);
  239. std::string errorString;
  240. llvm::raw_string_ostream errorStream(errorString);
  241. hlsl::options::DxcOpts opts;
  242. IFT(ReadDxcOpts(hlsl::options::getHlslOptTable(), /*flagsToInclude*/ 0,
  243. argStrings, opts, errorStream));
  244. std::wstring entry =
  245. Unicode::UTF8ToUTF16StringOrThrow(opts.EntryPoint.str().c_str());
  246. std::wstring profile =
  247. Unicode::UTF8ToUTF16StringOrThrow(opts.TargetProfile.str().c_str());
  248. std::vector<std::wstring> argLists;
  249. CopyArgsToWStrings(opts.Args, hlsl::options::CoreOption, argLists);
  250. std::vector<LPCWSTR> args;
  251. args.reserve(argLists.size());
  252. for (const std::wstring &a : argLists)
  253. args.push_back(a.data());
  254. VERIFY_SUCCEEDED(pCompiler->Compile(
  255. pSource, name.c_str(), entry.c_str(), profile.c_str(), args.data(), args.size(),
  256. opts.Defines.data(), opts.Defines.size(), nullptr, &pResult));
  257. VERIFY_IS_NOT_NULL(pResult, L"Failed to compile - pResult NULL");
  258. HRESULT result;
  259. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  260. if (FAILED(result)) {
  261. CComPtr<IDxcBlobEncoding> pErr;
  262. IFT(pResult->GetErrorBuffer(&pErr));
  263. std::string errString(BlobToUtf8(pErr));
  264. CA2W errStringW(errString.c_str(), CP_UTF8);
  265. WEX::Logging::Log::Comment(L"Failed to compile - errors follow");
  266. WEX::Logging::Log::Comment(errStringW);
  267. }
  268. VERIFY_SUCCEEDED(result);
  269. CComPtr<IDxcBlob> pProgram;
  270. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  271. if (opts.IsRootSignatureProfile())
  272. return;
  273. CComPtr<IDxcBlobEncoding> pDisassembleBlob;
  274. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembleBlob));
  275. std::string disassembleString(BlobToUtf8(pDisassembleBlob));
  276. VERIFY_ARE_NOT_EQUAL(0U, disassembleString.size());
  277. }
  278. void CodeGenTestHashFullPath(LPCWSTR fullPath) {
  279. FileRunTestResult t = FileRunTestResult::RunHashTestFromFileCommands(fullPath);
  280. if (t.RunResult != 0) {
  281. CA2W commentWide(t.ErrorMessage.c_str(), CP_UTF8);
  282. WEX::Logging::Log::Comment(commentWide);
  283. WEX::Logging::Log::Error(L"Run result is not zero");
  284. }
  285. }
  286. void CodeGenTestHash(LPCWSTR name, bool implicitDir) {
  287. std::wstring path = name;
  288. if (implicitDir) {
  289. path.insert(0, L"..\\CodeGenHLSL\\");
  290. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  291. }
  292. CodeGenTestHashFullPath(path.c_str());
  293. }
  294. void CodeGenTestCheckBatchHash(std::wstring suitePath, bool implicitDir = true) {
  295. using namespace llvm;
  296. using namespace WEX::TestExecution;
  297. if (implicitDir) suitePath.insert(0, L"..\\HLSLFileCheck\\");
  298. ::llvm::sys::fs::MSFileSystem *msfPtr;
  299. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  300. std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr);
  301. ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  302. IFTLLVM(pts.error_code());
  303. CW2A pUtf8Filename(suitePath.c_str());
  304. if (!llvm::sys::path::is_absolute(pUtf8Filename.m_psz)) {
  305. suitePath = hlsl_test::GetPathToHlslDataFile(suitePath.c_str());
  306. }
  307. CW2A utf8SuitePath(suitePath.c_str());
  308. unsigned numTestsRun = 0;
  309. std::error_code EC;
  310. llvm::SmallString<128> DirNative;
  311. llvm::sys::path::native(utf8SuitePath.m_psz, DirNative);
  312. for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC), DirEnd;
  313. Dir != DirEnd && !EC; Dir.increment(EC)) {
  314. // Check whether this entry has an extension typically associated with
  315. // headers.
  316. if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
  317. .Cases(".hlsl", ".ll", true).Default(false))
  318. continue;
  319. StringRef filename = Dir->path();
  320. std::string filetag = Dir->path();
  321. filetag += "<HASH>";
  322. CA2W wRelTag(filetag.data());
  323. CA2W wRelPath(filename.data());
  324. WEX::Logging::Log::StartGroup(wRelTag);
  325. CodeGenTestHash(wRelPath, /*implicitDir*/ false);
  326. WEX::Logging::Log::EndGroup(wRelTag);
  327. numTestsRun++;
  328. }
  329. VERIFY_IS_GREATER_THAN(numTestsRun, (unsigned)0, L"No test files found in batch directory.");
  330. }
  331. void CodeGenTestCheckFullPath(LPCWSTR fullPath) {
  332. FileRunTestResult t = FileRunTestResult::RunFromFileCommands(fullPath);
  333. if (t.RunResult != 0) {
  334. CA2W commentWide(t.ErrorMessage.c_str(), CP_UTF8);
  335. WEX::Logging::Log::Comment(commentWide);
  336. WEX::Logging::Log::Error(L"Run result is not zero");
  337. }
  338. }
  339. void CodeGenTestCheck(LPCWSTR name, bool implicitDir = true) {
  340. std::wstring path = name;
  341. if (implicitDir) {
  342. path.insert(0, L"..\\CodeGenHLSL\\");
  343. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  344. }
  345. CodeGenTestCheckFullPath(path.c_str());
  346. }
  347. void CodeGenTestCheckBatchDir(std::wstring suitePath, bool implicitDir = true) {
  348. using namespace llvm;
  349. using namespace WEX::TestExecution;
  350. if (implicitDir) suitePath.insert(0, L"..\\HLSLFileCheck\\");
  351. ::llvm::sys::fs::MSFileSystem *msfPtr;
  352. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  353. std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr);
  354. ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  355. IFTLLVM(pts.error_code());
  356. CW2A pUtf8Filename(suitePath.c_str());
  357. if (!llvm::sys::path::is_absolute(pUtf8Filename.m_psz)) {
  358. suitePath = hlsl_test::GetPathToHlslDataFile(suitePath.c_str());
  359. }
  360. CW2A utf8SuitePath(suitePath.c_str());
  361. unsigned numTestsRun = 0;
  362. std::error_code EC;
  363. llvm::SmallString<128> DirNative;
  364. llvm::sys::path::native(utf8SuitePath.m_psz, DirNative);
  365. for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC), DirEnd;
  366. Dir != DirEnd && !EC; Dir.increment(EC)) {
  367. // Check whether this entry has an extension typically associated with
  368. // headers.
  369. if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
  370. .Cases(".hlsl", ".ll", true).Default(false))
  371. continue;
  372. StringRef filename = Dir->path();
  373. CA2W wRelPath(filename.data());
  374. WEX::Logging::Log::StartGroup(wRelPath);
  375. CodeGenTestCheck(wRelPath, /*implicitDir*/ false);
  376. WEX::Logging::Log::EndGroup(wRelPath);
  377. numTestsRun++;
  378. }
  379. VERIFY_IS_GREATER_THAN(numTestsRun, (unsigned)0, L"No test files found in batch directory.");
  380. }
  381. std::string VerifyCompileFailed(LPCSTR pText, LPCWSTR pTargetProfile, LPCSTR pErrorMsg) {
  382. return VerifyCompileFailed(pText, pTargetProfile, pErrorMsg, L"main");
  383. }
  384. std::string VerifyCompileFailed(LPCSTR pText, LPCWSTR pTargetProfile, LPCSTR pErrorMsg, LPCWSTR pEntryPoint) {
  385. CComPtr<IDxcCompiler> pCompiler;
  386. CComPtr<IDxcOperationResult> pResult;
  387. CComPtr<IDxcBlobEncoding> pSource;
  388. CComPtr<IDxcBlobEncoding> pErrors;
  389. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  390. CreateBlobFromText(pText, &pSource);
  391. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  392. pTargetProfile, nullptr, 0, nullptr, 0, nullptr, &pResult));
  393. HRESULT status;
  394. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  395. VERIFY_FAILED(status);
  396. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  397. if (pErrorMsg && *pErrorMsg) {
  398. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  399. }
  400. return BlobToUtf8(pErrors);
  401. }
  402. void VerifyOperationSucceeded(IDxcOperationResult *pResult) {
  403. HRESULT result;
  404. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  405. if (FAILED(result)) {
  406. CComPtr<IDxcBlobEncoding> pErrors;
  407. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  408. CA2W errorsWide(BlobToUtf8(pErrors).c_str(), CP_UTF8);
  409. WEX::Logging::Log::Comment(errorsWide);
  410. }
  411. VERIFY_SUCCEEDED(result);
  412. }
  413. std::string VerifyOperationFailed(IDxcOperationResult *pResult) {
  414. HRESULT result;
  415. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  416. VERIFY_FAILED(result);
  417. CComPtr<IDxcBlobEncoding> pErrors;
  418. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  419. return BlobToUtf8(pErrors);
  420. }
  421. #ifdef _WIN32 // - exclude dia stuff
  422. HRESULT CreateDiaSourceForCompile(const char *hlsl, IDiaDataSource **ppDiaSource)
  423. {
  424. if (!ppDiaSource)
  425. return E_POINTER;
  426. CComPtr<IDxcCompiler> pCompiler;
  427. CComPtr<IDxcOperationResult> pResult;
  428. CComPtr<IDxcBlobEncoding> pSource;
  429. CComPtr<IDxcBlob> pProgram;
  430. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  431. CreateBlobFromText(hlsl, &pSource);
  432. LPCWSTR args[] = { L"/Zi", L"/Qembed_debug" };
  433. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  434. L"ps_6_0", args, _countof(args), nullptr, 0, nullptr, &pResult));
  435. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  436. // Disassemble the compiled (stripped) program.
  437. {
  438. CComPtr<IDxcBlobEncoding> pDisassembly;
  439. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembly));
  440. std::string disText = BlobToUtf8(pDisassembly);
  441. CA2W disTextW(disText.c_str(), CP_UTF8);
  442. //WEX::Logging::Log::Comment(disTextW);
  443. }
  444. // CONSIDER: have the dia data source look for the part if passed a whole container.
  445. CComPtr<IDiaDataSource> pDiaSource;
  446. CComPtr<IStream> pProgramStream;
  447. CComPtr<IDxcLibrary> pLib;
  448. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  449. const hlsl::DxilContainerHeader *pContainer = hlsl::IsDxilContainerLike(
  450. pProgram->GetBufferPointer(), pProgram->GetBufferSize());
  451. VERIFY_IS_NOT_NULL(pContainer);
  452. hlsl::DxilPartIterator partIter =
  453. std::find_if(hlsl::begin(pContainer), hlsl::end(pContainer),
  454. hlsl::DxilPartIsType(hlsl::DFCC_ShaderDebugInfoDXIL));
  455. const hlsl::DxilProgramHeader *pProgramHeader =
  456. (const hlsl::DxilProgramHeader *)hlsl::GetDxilPartData(*partIter);
  457. uint32_t bitcodeLength;
  458. const char *pBitcode;
  459. CComPtr<IDxcBlob> pProgramPdb;
  460. hlsl::GetDxilProgramBitcode(pProgramHeader, &pBitcode, &bitcodeLength);
  461. VERIFY_SUCCEEDED(pLib->CreateBlobFromBlob(
  462. pProgram, pBitcode - (char *)pProgram->GetBufferPointer(), bitcodeLength,
  463. &pProgramPdb));
  464. // Disassemble the program with debug information.
  465. {
  466. CComPtr<IDxcBlobEncoding> pDbgDisassembly;
  467. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgramPdb, &pDbgDisassembly));
  468. std::string disText = BlobToUtf8(pDbgDisassembly);
  469. CA2W disTextW(disText.c_str(), CP_UTF8);
  470. //WEX::Logging::Log::Comment(disTextW);
  471. }
  472. // Create a short text dump of debug information.
  473. VERIFY_SUCCEEDED(pLib->CreateStreamFromBlobReadOnly(pProgramPdb, &pProgramStream));
  474. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcDiaDataSource, &pDiaSource));
  475. VERIFY_SUCCEEDED(pDiaSource->loadDataFromIStream(pProgramStream));
  476. *ppDiaSource = pDiaSource.Detach();
  477. return S_OK;
  478. }
  479. #endif // _WIN32 - exclude dia stuff
  480. };
  481. // Useful for debugging.
  482. #if SUPPORT_FXC_PDB
  483. #include <d3dcompiler.h>
  484. #pragma comment(lib, "d3dcompiler.lib")
  485. HRESULT GetBlobPdb(IDxcBlob *pBlob, IDxcBlob **ppDebugInfo) {
  486. return D3DGetBlobPart(pBlob->GetBufferPointer(), pBlob->GetBufferSize(),
  487. D3D_BLOB_PDB, 0, (ID3DBlob **)ppDebugInfo);
  488. }
  489. std::string FourCCStr(uint32_t val) {
  490. std::stringstream o;
  491. char c[5];
  492. c[0] = val & 0xFF;
  493. c[1] = (val & 0xFF00) >> 8;
  494. c[2] = (val & 0xFF0000) >> 16;
  495. c[3] = (val & 0xFF000000) >> 24;
  496. c[4] = '\0';
  497. o << c << " (" << std::hex << val << std::dec << ")";
  498. return o.str();
  499. }
  500. std::string DumpParts(IDxcBlob *pBlob) {
  501. std::stringstream o;
  502. hlsl::DxilContainerHeader *pContainer = (hlsl::DxilContainerHeader *)pBlob->GetBufferPointer();
  503. o << "Container:" << std::endl
  504. << " Size: " << pContainer->ContainerSizeInBytes << std::endl
  505. << " FourCC: " << FourCCStr(pContainer->HeaderFourCC) << std::endl
  506. << " Part count: " << pContainer->PartCount << std::endl;
  507. for (uint32_t i = 0; i < pContainer->PartCount; ++i) {
  508. hlsl::DxilPartHeader *pPart = hlsl::GetDxilContainerPart(pContainer, i);
  509. o << "Part " << i << std::endl
  510. << " FourCC: " << FourCCStr(pPart->PartFourCC) << std::endl
  511. << " Size: " << pPart->PartSize << std::endl;
  512. }
  513. return o.str();
  514. }
  515. HRESULT CreateDiaSourceFromDxbcBlob(IDxcLibrary *pLib, IDxcBlob *pDxbcBlob,
  516. IDiaDataSource **ppDiaSource) {
  517. HRESULT hr = S_OK;
  518. CComPtr<IDxcBlob> pdbBlob;
  519. CComPtr<IStream> pPdbStream;
  520. CComPtr<IDiaDataSource> pDiaSource;
  521. IFR(GetBlobPdb(pDxbcBlob, &pdbBlob));
  522. IFR(pLib->CreateStreamFromBlobReadOnly(pdbBlob, &pPdbStream));
  523. IFR(CoCreateInstance(CLSID_DiaSource, NULL, CLSCTX_INPROC_SERVER,
  524. __uuidof(IDiaDataSource), (void **)&pDiaSource));
  525. IFR(pDiaSource->loadDataFromIStream(pPdbStream));
  526. *ppDiaSource = pDiaSource.Detach();
  527. return hr;
  528. }
  529. #endif
  530. bool CompilerTest::InitSupport() {
  531. if (!m_dllSupport.IsEnabled()) {
  532. VERIFY_SUCCEEDED(m_dllSupport.Initialize());
  533. m_ver.Initialize(m_dllSupport);
  534. }
  535. return true;
  536. }
  537. TEST_F(CompilerTest, CompileWhenDefinesThenApplied) {
  538. CComPtr<IDxcCompiler> pCompiler;
  539. CComPtr<IDxcOperationResult> pResult;
  540. CComPtr<IDxcBlobEncoding> pSource;
  541. DxcDefine defines[] = {{L"F4", L"float4"}};
  542. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  543. CreateBlobFromText("F4 main() : SV_Target { return 0; }", &pSource);
  544. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  545. L"ps_6_0", nullptr, 0, defines,
  546. _countof(defines), nullptr, &pResult));
  547. }
  548. TEST_F(CompilerTest, CompileWhenDefinesManyThenApplied) {
  549. CComPtr<IDxcCompiler> pCompiler;
  550. CComPtr<IDxcOperationResult> pResult;
  551. CComPtr<IDxcBlobEncoding> pSource;
  552. LPCWSTR args[] = {L"/DVAL1=1", L"/DVAL2=2", L"/DVAL3=3", L"/DVAL4=2",
  553. L"/DVAL5=4", L"/DNVAL1", L"/DNVAL2", L"/DNVAL3",
  554. L"/DNVAL4", L"/DNVAL5", L"/DCVAL1=1", L"/DCVAL2=2",
  555. L"/DCVAL3=3", L"/DCVAL4=2", L"/DCVAL5=4", L"/DCVALNONE="};
  556. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  557. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  558. "#ifndef VAL1\r\n"
  559. "#error VAL1 not defined\r\n"
  560. "#endif\r\n"
  561. "#ifndef NVAL5\r\n"
  562. "#error NVAL5 not defined\r\n"
  563. "#endif\r\n"
  564. "#ifndef CVALNONE\r\n"
  565. "#error CVALNONE not defined\r\n"
  566. "#endif\r\n"
  567. "return 0; }",
  568. &pSource);
  569. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  570. L"ps_6_0", args, _countof(args), nullptr,
  571. 0, nullptr, &pResult));
  572. HRESULT compileStatus;
  573. VERIFY_SUCCEEDED(pResult->GetStatus(&compileStatus));
  574. if (FAILED(compileStatus)) {
  575. CComPtr<IDxcBlobEncoding> pErrors;
  576. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  577. OutputDebugStringA((LPCSTR)pErrors->GetBufferPointer());
  578. }
  579. VERIFY_SUCCEEDED(compileStatus);
  580. }
  581. TEST_F(CompilerTest, CompileWhenEmptyThenFails) {
  582. CComPtr<IDxcCompiler> pCompiler;
  583. CComPtr<IDxcOperationResult> pResult;
  584. CComPtr<IDxcBlobEncoding> pSource;
  585. CComPtr<IDxcBlobEncoding> pSourceBad;
  586. LPCWSTR pProfile = L"ps_6_0";
  587. LPCWSTR pEntryPoint = L"main";
  588. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  589. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  590. CreateBlobFromText("float4 main() : SV_Target { return undef; }", &pSourceBad);
  591. // correct version
  592. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  593. pProfile, nullptr, 0, nullptr, 0, nullptr,
  594. &pResult));
  595. pResult.Release();
  596. // correct version with compilation errors
  597. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
  598. pProfile, nullptr, 0, nullptr, 0, nullptr,
  599. &pResult));
  600. pResult.Release();
  601. // null source
  602. VERIFY_FAILED(pCompiler->Compile(nullptr, L"source.hlsl", pEntryPoint, pProfile,
  603. nullptr, 0, nullptr, 0, nullptr, &pResult));
  604. // null profile
  605. VERIFY_FAILED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
  606. nullptr, nullptr, 0, nullptr, 0, nullptr,
  607. &pResult));
  608. // null source name succeeds
  609. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, nullptr, pEntryPoint, pProfile,
  610. nullptr, 0, nullptr, 0, nullptr, &pResult));
  611. pResult.Release();
  612. // empty source name (as opposed to null) also suceeds
  613. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"", pEntryPoint, pProfile,
  614. nullptr, 0, nullptr, 0, nullptr,
  615. &pResult));
  616. pResult.Release();
  617. // null result
  618. VERIFY_FAILED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  619. pProfile, nullptr, 0, nullptr, 0, nullptr,
  620. nullptr));
  621. }
  622. TEST_F(CompilerTest, CompileWhenIncorrectThenFails) {
  623. CComPtr<IDxcCompiler> pCompiler;
  624. CComPtr<IDxcOperationResult> pResult;
  625. CComPtr<IDxcBlobEncoding> pSource;
  626. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  627. CreateBlobFromText("float4_undefined main() : SV_Target { return 0; }",
  628. &pSource);
  629. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main", L"ps_6_0",
  630. nullptr, 0, nullptr, 0, nullptr,
  631. &pResult));
  632. HRESULT result;
  633. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  634. VERIFY_FAILED(result);
  635. CComPtr<IDxcBlobEncoding> pErrorBuffer;
  636. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrorBuffer));
  637. std::string errorString(BlobToUtf8(pErrorBuffer));
  638. VERIFY_ARE_NOT_EQUAL(0U, errorString.size());
  639. // Useful for examining actual error message:
  640. // CA2W errorStringW(errorString.c_str(), CP_UTF8);
  641. // WEX::Logging::Log::Comment(errorStringW.m_psz);
  642. }
  643. TEST_F(CompilerTest, CompileWhenWorksThenDisassembleWorks) {
  644. CComPtr<IDxcCompiler> pCompiler;
  645. CComPtr<IDxcOperationResult> pResult;
  646. CComPtr<IDxcBlobEncoding> pSource;
  647. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  648. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  649. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  650. L"ps_6_0", nullptr, 0, nullptr, 0,
  651. nullptr, &pResult));
  652. HRESULT result;
  653. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  654. VERIFY_SUCCEEDED(result);
  655. CComPtr<IDxcBlob> pProgram;
  656. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  657. CComPtr<IDxcBlobEncoding> pDisassembleBlob;
  658. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembleBlob));
  659. std::string disassembleString(BlobToUtf8(pDisassembleBlob));
  660. VERIFY_ARE_NOT_EQUAL(0U, disassembleString.size());
  661. // Useful for examining disassembly:
  662. // CA2W disassembleStringW(disassembleString.c_str(), CP_UTF8);
  663. // WEX::Logging::Log::Comment(disassembleStringW.m_psz);
  664. }
  665. #ifdef _WIN32 // Container builder unsupported
  666. TEST_F(CompilerTest, CompileWhenDebugWorksThenStripDebug) {
  667. CComPtr<IDxcCompiler> pCompiler;
  668. CComPtr<IDxcOperationResult> pResult;
  669. CComPtr<IDxcBlobEncoding> pSource;
  670. CComPtr<IDxcBlob> pProgram;
  671. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  672. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target {\r\n"
  673. " float4 local = abs(pos);\r\n"
  674. " return local;\r\n"
  675. "}",
  676. &pSource);
  677. LPCWSTR args[] = {L"/Zi", L"/Qembed_debug"};
  678. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  679. L"ps_6_0", args, _countof(args), nullptr,
  680. 0, nullptr, &pResult));
  681. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  682. // Check if it contains debug blob
  683. hlsl::DxilContainerHeader *pHeader =
  684. hlsl::IsDxilContainerLike(pProgram->GetBufferPointer(), pProgram->GetBufferSize());
  685. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pHeader, pProgram->GetBufferSize()));
  686. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  687. pHeader, hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL);
  688. VERIFY_IS_NOT_NULL(pPartHeader);
  689. // Check debug info part does not exist after strip debug info
  690. CComPtr<IDxcBlob> pNewProgram;
  691. CComPtr<IDxcContainerBuilder> pBuilder;
  692. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  693. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  694. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL));
  695. pResult.Release();
  696. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  697. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  698. pHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  699. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pHeader, pNewProgram->GetBufferSize()));
  700. pPartHeader = hlsl::GetDxilPartByType(
  701. pHeader, hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL);
  702. VERIFY_IS_NULL(pPartHeader);
  703. }
  704. TEST_F(CompilerTest, CompileWhenWorksThenAddRemovePrivate) {
  705. CComPtr<IDxcCompiler> pCompiler;
  706. CComPtr<IDxcOperationResult> pResult;
  707. CComPtr<IDxcBlobEncoding> pSource;
  708. CComPtr<IDxcBlob> pProgram;
  709. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  710. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  711. " return 0;\r\n"
  712. "}",
  713. &pSource);
  714. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  715. L"ps_6_0", nullptr, 0, nullptr, 0,
  716. nullptr, &pResult));
  717. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  718. // Append private data blob
  719. CComPtr<IDxcContainerBuilder> pBuilder;
  720. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  721. std::string privateTxt("private data");
  722. CComPtr<IDxcBlobEncoding> pPrivate;
  723. CreateBlobFromText(privateTxt.c_str(), &pPrivate);
  724. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  725. VERIFY_SUCCEEDED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_PrivateData, pPrivate));
  726. pResult.Release();
  727. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  728. CComPtr<IDxcBlob> pNewProgram;
  729. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  730. hlsl::DxilContainerHeader *pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  731. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  732. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  733. pContainerHeader, hlsl::DxilFourCC::DFCC_PrivateData);
  734. VERIFY_IS_NOT_NULL(pPartHeader);
  735. // compare data
  736. std::string privatePart((const char *)(pPartHeader + 1), privateTxt.size());
  737. VERIFY_IS_TRUE(strcmp(privatePart.c_str(), privateTxt.c_str()) == 0);
  738. // Remove private data blob
  739. pBuilder.Release();
  740. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  741. VERIFY_SUCCEEDED(pBuilder->Load(pNewProgram));
  742. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_PrivateData));
  743. pResult.Release();
  744. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  745. pNewProgram.Release();
  746. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  747. pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  748. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  749. pPartHeader = hlsl::GetDxilPartByType(
  750. pContainerHeader, hlsl::DxilFourCC::DFCC_PrivateData);
  751. VERIFY_IS_NULL(pPartHeader);
  752. }
  753. TEST_F(CompilerTest, CompileThenAddCustomDebugName) {
  754. // container builders prior to 1.3 did not support adding debug name parts
  755. if (m_ver.SkipDxilVersion(1, 3)) return;
  756. CComPtr<IDxcCompiler> pCompiler;
  757. CComPtr<IDxcOperationResult> pResult;
  758. CComPtr<IDxcBlobEncoding> pSource;
  759. CComPtr<IDxcBlob> pProgram;
  760. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  761. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  762. " return 0;\r\n"
  763. "}",
  764. &pSource);
  765. LPCWSTR args[] = { L"/Zi", L"/Qembed_debug", L"/Zss" };
  766. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  767. L"ps_6_0", args, _countof(args), nullptr, 0,
  768. nullptr, &pResult));
  769. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  770. // Append private data blob
  771. CComPtr<IDxcContainerBuilder> pBuilder;
  772. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  773. const char pNewName[] = "MyOwnUniqueName.lld";
  774. //include null terminator:
  775. size_t nameBlobPartSize = sizeof(hlsl::DxilShaderDebugName) + _countof(pNewName);
  776. // round up to four-byte size:
  777. size_t allocatedSize = (nameBlobPartSize + 3) & ~3;
  778. auto pNameBlobContent = reinterpret_cast<hlsl::DxilShaderDebugName*>(malloc(allocatedSize));
  779. ZeroMemory(pNameBlobContent, allocatedSize); //just to make sure trailing nulls are nulls.
  780. pNameBlobContent->Flags = 0;
  781. pNameBlobContent->NameLength = _countof(pNewName) - 1; //this is not supposed to include null terminator
  782. memcpy(pNameBlobContent + 1, pNewName, _countof(pNewName));
  783. CComPtr<IDxcBlobEncoding> pDebugName;
  784. CreateBlobPinned(pNameBlobContent, allocatedSize, CP_UTF8, &pDebugName);
  785. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  786. // should fail since it already exists:
  787. VERIFY_FAILED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_ShaderDebugName, pDebugName));
  788. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugName));
  789. VERIFY_SUCCEEDED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_ShaderDebugName, pDebugName));
  790. pResult.Release();
  791. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  792. CComPtr<IDxcBlob> pNewProgram;
  793. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  794. hlsl::DxilContainerHeader *pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  795. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  796. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  797. pContainerHeader, hlsl::DxilFourCC::DFCC_ShaderDebugName);
  798. VERIFY_IS_NOT_NULL(pPartHeader);
  799. // compare data
  800. VERIFY_IS_TRUE(memcmp(pPartHeader + 1, pNameBlobContent, allocatedSize) == 0);
  801. free(pNameBlobContent);
  802. // Remove private data blob
  803. pBuilder.Release();
  804. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  805. VERIFY_SUCCEEDED(pBuilder->Load(pNewProgram));
  806. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugName));
  807. pResult.Release();
  808. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  809. pNewProgram.Release();
  810. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  811. pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  812. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  813. pPartHeader = hlsl::GetDxilPartByType(
  814. pContainerHeader, hlsl::DxilFourCC::DFCC_ShaderDebugName);
  815. VERIFY_IS_NULL(pPartHeader);
  816. }
  817. TEST_F(CompilerTest, CompileWithRootSignatureThenStripRootSignature) {
  818. CComPtr<IDxcCompiler> pCompiler;
  819. CComPtr<IDxcOperationResult> pResult;
  820. CComPtr<IDxcBlobEncoding> pSource;
  821. CComPtr<IDxcBlob> pProgram;
  822. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  823. CreateBlobFromText("[RootSignature(\"\")] \r\n"
  824. "float4 main(float a : A) : SV_Target {\r\n"
  825. " return a;\r\n"
  826. "}",
  827. &pSource);
  828. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  829. L"ps_6_0", nullptr, 0, nullptr,
  830. 0, nullptr, &pResult));
  831. VERIFY_IS_NOT_NULL(pResult);
  832. HRESULT status;
  833. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  834. VERIFY_SUCCEEDED(status);
  835. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  836. VERIFY_IS_NOT_NULL(pProgram);
  837. hlsl::DxilContainerHeader *pContainerHeader = hlsl::IsDxilContainerLike(pProgram->GetBufferPointer(), pProgram->GetBufferSize());
  838. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pProgram->GetBufferSize()));
  839. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  840. pContainerHeader, hlsl::DxilFourCC::DFCC_RootSignature);
  841. VERIFY_IS_NOT_NULL(pPartHeader);
  842. pResult.Release();
  843. // Remove root signature
  844. CComPtr<IDxcBlob> pProgramRootSigRemoved;
  845. CComPtr<IDxcContainerBuilder> pBuilder;
  846. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  847. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  848. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_RootSignature));
  849. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  850. VERIFY_SUCCEEDED(pResult->GetResult(&pProgramRootSigRemoved));
  851. pContainerHeader = hlsl::IsDxilContainerLike(pProgramRootSigRemoved->GetBufferPointer(), pProgramRootSigRemoved->GetBufferSize());
  852. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pProgramRootSigRemoved->GetBufferSize()));
  853. hlsl::DxilPartHeader *pPartHeaderShouldBeNull = hlsl::GetDxilPartByType(pContainerHeader,
  854. hlsl::DxilFourCC::DFCC_RootSignature);
  855. VERIFY_IS_NULL(pPartHeaderShouldBeNull);
  856. pBuilder.Release();
  857. pResult.Release();
  858. // Add root signature back
  859. CComPtr<IDxcBlobEncoding> pRootSignatureBlob;
  860. CComPtr<IDxcLibrary> pLibrary;
  861. CComPtr<IDxcBlob> pProgramRootSigAdded;
  862. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  863. VERIFY_SUCCEEDED(pLibrary->CreateBlobWithEncodingFromPinned(
  864. hlsl::GetDxilPartData(pPartHeader), pPartHeader->PartSize, 0, &pRootSignatureBlob));
  865. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  866. VERIFY_SUCCEEDED(pBuilder->Load(pProgramRootSigRemoved));
  867. pBuilder->AddPart(hlsl::DxilFourCC::DFCC_RootSignature, pRootSignatureBlob);
  868. pBuilder->SerializeContainer(&pResult);
  869. VERIFY_SUCCEEDED(pResult->GetResult(&pProgramRootSigAdded));
  870. pContainerHeader = hlsl::IsDxilContainerLike(pProgramRootSigAdded->GetBufferPointer(), pProgramRootSigAdded->GetBufferSize());
  871. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pProgramRootSigAdded->GetBufferSize()));
  872. pPartHeader = hlsl::GetDxilPartByType(pContainerHeader,
  873. hlsl::DxilFourCC::DFCC_RootSignature);
  874. VERIFY_IS_NOT_NULL(pPartHeader);
  875. }
  876. #endif // Container builder unsupported
  877. TEST_F(CompilerTest, CompileWhenIncludeThenLoadInvoked) {
  878. CComPtr<IDxcCompiler> pCompiler;
  879. CComPtr<IDxcOperationResult> pResult;
  880. CComPtr<IDxcBlobEncoding> pSource;
  881. CComPtr<TestIncludeHandler> pInclude;
  882. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  883. CreateBlobFromText(
  884. "#include \"helper.h\"\r\n"
  885. "float4 main() : SV_Target { return 0; }", &pSource);
  886. pInclude = new TestIncludeHandler(m_dllSupport);
  887. pInclude->CallResults.emplace_back("");
  888. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  889. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  890. VerifyOperationSucceeded(pResult);
  891. VERIFY_ARE_EQUAL_WSTR(L"./helper.h;", pInclude->GetAllFileNames().c_str());
  892. }
  893. TEST_F(CompilerTest, CompileWhenIncludeThenLoadUsed) {
  894. CComPtr<IDxcCompiler> pCompiler;
  895. CComPtr<IDxcOperationResult> pResult;
  896. CComPtr<IDxcBlobEncoding> pSource;
  897. CComPtr<TestIncludeHandler> pInclude;
  898. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  899. CreateBlobFromText(
  900. "#include \"helper.h\"\r\n"
  901. "float4 main() : SV_Target { return ZERO; }", &pSource);
  902. pInclude = new TestIncludeHandler(m_dllSupport);
  903. pInclude->CallResults.emplace_back("#define ZERO 0");
  904. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  905. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  906. VerifyOperationSucceeded(pResult);
  907. VERIFY_ARE_EQUAL_WSTR(L"./helper.h;", pInclude->GetAllFileNames().c_str());
  908. }
  909. TEST_F(CompilerTest, CompileWhenIncludeAbsoluteThenLoadAbsolute) {
  910. CComPtr<IDxcCompiler> pCompiler;
  911. CComPtr<IDxcOperationResult> pResult;
  912. CComPtr<IDxcBlobEncoding> pSource;
  913. CComPtr<TestIncludeHandler> pInclude;
  914. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  915. #ifdef _WIN32 // OS-specific root
  916. CreateBlobFromText(
  917. "#include \"C:\\helper.h\"\r\n"
  918. "float4 main() : SV_Target { return ZERO; }", &pSource);
  919. #else
  920. CreateBlobFromText(
  921. "#include \"/helper.h\"\n"
  922. "float4 main() : SV_Target { return ZERO; }", &pSource);
  923. #endif
  924. pInclude = new TestIncludeHandler(m_dllSupport);
  925. pInclude->CallResults.emplace_back("#define ZERO 0");
  926. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  927. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  928. VerifyOperationSucceeded(pResult);
  929. #ifdef _WIN32 // OS-specific root
  930. VERIFY_ARE_EQUAL_WSTR(L"C:\\helper.h;", pInclude->GetAllFileNames().c_str());
  931. #else
  932. VERIFY_ARE_EQUAL_WSTR(L"/helper.h;", pInclude->GetAllFileNames().c_str());
  933. #endif
  934. }
  935. TEST_F(CompilerTest, CompileWhenIncludeLocalThenLoadRelative) {
  936. CComPtr<IDxcCompiler> pCompiler;
  937. CComPtr<IDxcOperationResult> pResult;
  938. CComPtr<IDxcBlobEncoding> pSource;
  939. CComPtr<TestIncludeHandler> pInclude;
  940. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  941. CreateBlobFromText(
  942. "#include \"..\\helper.h\"\r\n"
  943. "float4 main() : SV_Target { return ZERO; }", &pSource);
  944. pInclude = new TestIncludeHandler(m_dllSupport);
  945. pInclude->CallResults.emplace_back("#define ZERO 0");
  946. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  947. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  948. VerifyOperationSucceeded(pResult);
  949. #ifdef _WIN32 // OS-specific directory dividers
  950. VERIFY_ARE_EQUAL_WSTR(L"./..\\helper.h;", pInclude->GetAllFileNames().c_str());
  951. #else
  952. VERIFY_ARE_EQUAL_WSTR(L"./../helper.h;", pInclude->GetAllFileNames().c_str());
  953. #endif
  954. }
  955. TEST_F(CompilerTest, CompileWhenIncludeSystemThenLoadNotRelative) {
  956. CComPtr<IDxcCompiler> pCompiler;
  957. CComPtr<IDxcOperationResult> pResult;
  958. CComPtr<IDxcBlobEncoding> pSource;
  959. CComPtr<TestIncludeHandler> pInclude;
  960. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  961. CreateBlobFromText(
  962. "#include \"subdir/other/file.h\"\r\n"
  963. "float4 main() : SV_Target { return ZERO; }", &pSource);
  964. LPCWSTR args[] = {
  965. L"-Ifoo"
  966. };
  967. pInclude = new TestIncludeHandler(m_dllSupport);
  968. pInclude->CallResults.emplace_back("#include <helper.h>");
  969. pInclude->CallResults.emplace_back("#define ZERO 0");
  970. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  971. L"ps_6_0", args, _countof(args), nullptr, 0, pInclude, &pResult));
  972. VerifyOperationSucceeded(pResult);
  973. #ifdef _WIN32 // OS-specific directory dividers
  974. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./foo\\helper.h;", pInclude->GetAllFileNames().c_str());
  975. #else
  976. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./foo/helper.h;", pInclude->GetAllFileNames().c_str());
  977. #endif
  978. }
  979. TEST_F(CompilerTest, CompileWhenIncludeSystemMissingThenLoadAttempt) {
  980. CComPtr<IDxcCompiler> pCompiler;
  981. CComPtr<IDxcOperationResult> pResult;
  982. CComPtr<IDxcBlobEncoding> pSource;
  983. CComPtr<TestIncludeHandler> pInclude;
  984. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  985. CreateBlobFromText(
  986. "#include \"subdir/other/file.h\"\r\n"
  987. "float4 main() : SV_Target { return ZERO; }", &pSource);
  988. pInclude = new TestIncludeHandler(m_dllSupport);
  989. pInclude->CallResults.emplace_back("#include <helper.h>");
  990. pInclude->CallResults.emplace_back("#define ZERO 0");
  991. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  992. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  993. std::string failLog(VerifyOperationFailed(pResult));
  994. VERIFY_ARE_NOT_EQUAL(std::string::npos, failLog.find("<angled>")); // error message should prompt to use <angled> rather than "quotes"
  995. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./subdir/other/helper.h;", pInclude->GetAllFileNames().c_str());
  996. }
  997. TEST_F(CompilerTest, CompileWhenIncludeFlagsThenIncludeUsed) {
  998. CComPtr<IDxcCompiler> pCompiler;
  999. CComPtr<IDxcOperationResult> pResult;
  1000. CComPtr<IDxcBlobEncoding> pSource;
  1001. CComPtr<TestIncludeHandler> pInclude;
  1002. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1003. CreateBlobFromText(
  1004. "#include <helper.h>\r\n"
  1005. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1006. pInclude = new TestIncludeHandler(m_dllSupport);
  1007. pInclude->CallResults.emplace_back("#define ZERO 0");
  1008. #ifdef _WIN32 // OS-specific root
  1009. LPCWSTR args[] = { L"-I\\\\server\\share" };
  1010. #else
  1011. LPCWSTR args[] = { L"-I/server/share" };
  1012. #endif
  1013. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1014. L"ps_6_0", args, _countof(args), nullptr, 0, pInclude, &pResult));
  1015. VerifyOperationSucceeded(pResult);
  1016. #ifdef _WIN32 // OS-specific root
  1017. VERIFY_ARE_EQUAL_WSTR(L"\\\\server\\share\\helper.h;", pInclude->GetAllFileNames().c_str());
  1018. #else
  1019. VERIFY_ARE_EQUAL_WSTR(L"/server/share/helper.h;", pInclude->GetAllFileNames().c_str());
  1020. #endif
  1021. }
  1022. TEST_F(CompilerTest, CompileWhenIncludeMissingThenFail) {
  1023. CComPtr<IDxcCompiler> pCompiler;
  1024. CComPtr<IDxcOperationResult> pResult;
  1025. CComPtr<IDxcBlobEncoding> pSource;
  1026. CComPtr<TestIncludeHandler> pInclude;
  1027. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1028. CreateBlobFromText(
  1029. "#include \"file.h\"\r\n"
  1030. "float4 main() : SV_Target { return 0; }", &pSource);
  1031. pInclude = new TestIncludeHandler(m_dllSupport);
  1032. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1033. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1034. HRESULT hr;
  1035. VERIFY_SUCCEEDED(pResult->GetStatus(&hr));
  1036. VERIFY_FAILED(hr);
  1037. }
  1038. TEST_F(CompilerTest, CompileWhenIncludeHasPathThenOK) {
  1039. CComPtr<IDxcCompiler> pCompiler;
  1040. LPCWSTR Source = L"c:\\temp\\OddIncludes\\main.hlsl";
  1041. LPCWSTR Args[] = { L"/I", L"c:\\temp" };
  1042. LPCWSTR ArgsUp[] = { L"/I", L"c:\\Temp" };
  1043. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1044. bool useUpValues[] = { false, true };
  1045. for (bool useUp : useUpValues) {
  1046. CComPtr<IDxcOperationResult> pResult;
  1047. CComPtr<IDxcBlobEncoding> pSource;
  1048. #if TEST_ON_DISK
  1049. CComPtr<IDxcLibrary> pLibrary;
  1050. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  1051. VERIFY_SUCCEEDED(pLibrary->CreateIncludeHandler(&pInclude));
  1052. VERIFY_SUCCEEDED(pLibrary->CreateBlobFromFile(Source, nullptr, &pSource));
  1053. #else
  1054. CComPtr<TestIncludeHandler> pInclude;
  1055. pInclude = new TestIncludeHandler(m_dllSupport);
  1056. pInclude->CallResults.emplace_back("// Empty");
  1057. CreateBlobFromText("#include \"include.hlsl\"\r\n"
  1058. "float4 main() : SV_Target { return 0; }",
  1059. &pSource);
  1060. #endif
  1061. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, Source, L"main",
  1062. L"ps_6_0", useUp ? ArgsUp : Args, _countof(Args), nullptr, 0, pInclude, &pResult));
  1063. HRESULT hr;
  1064. VERIFY_SUCCEEDED(pResult->GetStatus(&hr));
  1065. VERIFY_SUCCEEDED(hr);
  1066. }
  1067. }
  1068. TEST_F(CompilerTest, CompileWhenIncludeEmptyThenOK) {
  1069. CComPtr<IDxcCompiler> pCompiler;
  1070. CComPtr<IDxcOperationResult> pResult;
  1071. CComPtr<IDxcBlobEncoding> pSource;
  1072. CComPtr<TestIncludeHandler> pInclude;
  1073. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1074. CreateBlobFromText("#include \"empty.h\"\r\n"
  1075. "float4 main() : SV_Target { return 0; }",
  1076. &pSource);
  1077. pInclude = new TestIncludeHandler(m_dllSupport);
  1078. pInclude->CallResults.emplace_back("", CP_ACP); // An empty file would get detected as ACP code page
  1079. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1080. L"ps_6_0", nullptr, 0, nullptr, 0,
  1081. pInclude, &pResult));
  1082. VerifyOperationSucceeded(pResult);
  1083. VERIFY_ARE_EQUAL_WSTR(L"./empty.h;", pInclude->GetAllFileNames().c_str());
  1084. }
  1085. static const char EmptyCompute[] = "[numthreads(8,8,1)] void main() { }";
  1086. TEST_F(CompilerTest, CompileWhenODumpThenPassConfig) {
  1087. CComPtr<IDxcCompiler> pCompiler;
  1088. CComPtr<IDxcOperationResult> pResult;
  1089. CComPtr<IDxcBlobEncoding> pSource;
  1090. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1091. CreateBlobFromText(EmptyCompute, &pSource);
  1092. LPCWSTR Args[] = { L"/Odump" };
  1093. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1094. L"cs_6_0", Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1095. VerifyOperationSucceeded(pResult);
  1096. CComPtr<IDxcBlob> pResultBlob;
  1097. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1098. wstring passes = BlobToUtf16(pResultBlob);
  1099. VERIFY_ARE_NOT_EQUAL(wstring::npos, passes.find(L"inline"));
  1100. }
  1101. TEST_F(CompilerTest, CompileWhenVdThenProducesDxilContainer) {
  1102. CComPtr<IDxcCompiler> pCompiler;
  1103. CComPtr<IDxcOperationResult> pResult;
  1104. CComPtr<IDxcBlobEncoding> pSource;
  1105. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1106. CreateBlobFromText(EmptyCompute, &pSource);
  1107. LPCWSTR Args[] = { L"/Vd" };
  1108. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1109. L"cs_6_0", Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1110. VerifyOperationSucceeded(pResult);
  1111. CComPtr<IDxcBlob> pResultBlob;
  1112. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1113. VERIFY_IS_TRUE(hlsl::IsValidDxilContainer(reinterpret_cast<hlsl::DxilContainerHeader *>(pResultBlob->GetBufferPointer()), pResultBlob->GetBufferSize()));
  1114. }
  1115. TEST_F(CompilerTest, CompileWhenODumpThenOptimizerMatch) {
  1116. LPCWSTR OptLevels[] = { L"/Od", L"/O1", L"/O2" };
  1117. CComPtr<IDxcCompiler> pCompiler;
  1118. CComPtr<IDxcOptimizer> pOptimizer;
  1119. CComPtr<IDxcAssembler> pAssembler;
  1120. CComPtr<IDxcValidator> pValidator;
  1121. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  1122. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  1123. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcOptimizer, &pOptimizer));
  1124. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcValidator, &pValidator));
  1125. for (LPCWSTR OptLevel : OptLevels) {
  1126. CComPtr<IDxcOperationResult> pResult;
  1127. CComPtr<IDxcBlobEncoding> pSource;
  1128. CComPtr<IDxcBlob> pHighLevelBlob;
  1129. CComPtr<IDxcBlob> pOptimizedModule;
  1130. CComPtr<IDxcBlob> pAssembledBlob;
  1131. // Could use EmptyCompute and cs_6_0, but there is an issue where properties
  1132. // don't round-trip properly at high-level, so validation fails because
  1133. // dimensions are set to zero. Workaround by using pixel shader instead.
  1134. LPCWSTR Target = L"ps_6_0";
  1135. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  1136. LPCWSTR Args[2] = { OptLevel, L"/Odump" };
  1137. // Get the passes for this optimization level.
  1138. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1139. Target, Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1140. VerifyOperationSucceeded(pResult);
  1141. CComPtr<IDxcBlob> pResultBlob;
  1142. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1143. wstring passes = BlobToUtf16(pResultBlob);
  1144. // Get wchar_t version and prepend hlsl-hlensure, to do a split high-level/opt compilation pass.
  1145. std::vector<LPCWSTR> Options;
  1146. SplitPassList(const_cast<LPWSTR>(passes.data()), Options);
  1147. // Now compile directly.
  1148. pResult.Release();
  1149. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1150. Target, Args, 1, nullptr, 0, nullptr, &pResult));
  1151. VerifyOperationSucceeded(pResult);
  1152. // Now compile via a high-level compile followed by the optimization passes.
  1153. pResult.Release();
  1154. Args[_countof(Args)-1] = L"/fcgl";
  1155. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1156. Target, Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1157. VerifyOperationSucceeded(pResult);
  1158. VERIFY_SUCCEEDED(pResult->GetResult(&pHighLevelBlob));
  1159. VERIFY_SUCCEEDED(pOptimizer->RunOptimizer(pHighLevelBlob, Options.data(),
  1160. Options.size(), &pOptimizedModule,
  1161. nullptr));
  1162. string text = DisassembleProgram(m_dllSupport, pOptimizedModule);
  1163. WEX::Logging::Log::Comment(L"Final program:");
  1164. WEX::Logging::Log::Comment(CA2W(text.c_str()));
  1165. // At the very least, the module should be valid.
  1166. pResult.Release();
  1167. VERIFY_SUCCEEDED(pAssembler->AssembleToContainer(pOptimizedModule, &pResult));
  1168. VerifyOperationSucceeded(pResult);
  1169. VERIFY_SUCCEEDED(pResult->GetResult(&pAssembledBlob));
  1170. pResult.Release();
  1171. VERIFY_SUCCEEDED(pValidator->Validate(pAssembledBlob, DxcValidatorFlags_Default, &pResult));
  1172. VerifyOperationSucceeded(pResult);
  1173. }
  1174. }
  1175. static const UINT CaptureStacks = 0; // Set to 1 to enable captures
  1176. static const UINT StackFrameCount = 12;
  1177. struct InstrumentedHeapMalloc : public IMalloc {
  1178. private:
  1179. HANDLE m_Handle; // Heap handle.
  1180. ULONG m_RefCount = 0; // Reference count. Used for reference leaks, not for lifetime.
  1181. ULONG m_AllocCount = 0; // Total # of alloc and realloc requests.
  1182. ULONG m_AllocSize = 0; // Total # of alloc and realloc bytes.
  1183. ULONG m_Size = 0; // Current # of alloc'ed bytes.
  1184. ULONG m_FailAlloc = 0; // If nonzero, the alloc/realloc call to fail.
  1185. // Each allocation also tracks the following information:
  1186. // - allocation callstack
  1187. // - deallocation callstack
  1188. // - prior/next blocks in a list of allocated blocks
  1189. LIST_ENTRY AllocList;
  1190. struct PtrData {
  1191. LIST_ENTRY Entry;
  1192. LPVOID AllocFrames[CaptureStacks ? StackFrameCount * CaptureStacks : 1];
  1193. LPVOID FreeFrames[CaptureStacks ? StackFrameCount * CaptureStacks : 1];
  1194. UINT64 AllocAtCount;
  1195. DWORD AllocFrameCount;
  1196. DWORD FreeFrameCount;
  1197. SIZE_T Size;
  1198. PtrData *Self;
  1199. };
  1200. PtrData *DataFromPtr(void *p) {
  1201. if (p == nullptr) return nullptr;
  1202. PtrData *R = ((PtrData *)p) - 1;
  1203. if (R != R->Self) {
  1204. VERIFY_FAIL(); // p is invalid or underrun
  1205. }
  1206. return R;
  1207. }
  1208. public:
  1209. InstrumentedHeapMalloc() : m_Handle(nullptr) {
  1210. ResetCounts();
  1211. }
  1212. ~InstrumentedHeapMalloc() {
  1213. if (m_Handle)
  1214. HeapDestroy(m_Handle);
  1215. }
  1216. void ResetHeap() {
  1217. if (m_Handle) {
  1218. HeapDestroy(m_Handle);
  1219. m_Handle = nullptr;
  1220. }
  1221. m_Handle = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
  1222. }
  1223. ULONG GetRefCount() const { return m_RefCount; }
  1224. ULONG GetAllocCount() const { return m_AllocCount; }
  1225. ULONG GetAllocSize() const { return m_AllocSize; }
  1226. ULONG GetSize() const { return m_Size; }
  1227. void ResetCounts() {
  1228. m_RefCount = m_AllocCount = m_AllocSize = m_Size = 0;
  1229. AllocList.Blink = AllocList.Flink = &AllocList;
  1230. }
  1231. void SetFailAlloc(ULONG index) {
  1232. m_FailAlloc = index;
  1233. }
  1234. ULONG STDMETHODCALLTYPE AddRef() {
  1235. return ++m_RefCount;
  1236. }
  1237. ULONG STDMETHODCALLTYPE Release() {
  1238. if (m_RefCount == 0) VERIFY_FAIL();
  1239. return --m_RefCount;
  1240. }
  1241. STDMETHODIMP QueryInterface(REFIID iid, void** ppvObject) {
  1242. return DoBasicQueryInterface<IMalloc>(this, iid, ppvObject);
  1243. }
  1244. virtual void *STDMETHODCALLTYPE Alloc(_In_ SIZE_T cb) {
  1245. ++m_AllocCount;
  1246. if (m_FailAlloc && m_AllocCount >= m_FailAlloc) {
  1247. return nullptr; // breakpoint for i failure - m_FailAlloc == 1+VAL
  1248. }
  1249. m_AllocSize += cb;
  1250. m_Size += cb;
  1251. PtrData *P = (PtrData *)HeapAlloc(m_Handle, HEAP_ZERO_MEMORY, sizeof(PtrData) + cb);
  1252. P->Entry.Flink = AllocList.Flink;
  1253. P->Entry.Blink = &AllocList;
  1254. AllocList.Flink->Blink = &(P->Entry);
  1255. AllocList.Flink = &(P->Entry);
  1256. // breakpoint for i failure on NN alloc - m_FailAlloc == 1+VAL && m_AllocCount == NN
  1257. // breakpoint for happy path for NN alloc - m_AllocCount == NN
  1258. P->AllocAtCount = m_AllocCount;
  1259. if (CaptureStacks)
  1260. P->AllocFrameCount = CaptureStackBackTrace(1, StackFrameCount, P->AllocFrames, nullptr);
  1261. P->Size = cb;
  1262. P->Self = P;
  1263. return P + 1;
  1264. }
  1265. virtual void *STDMETHODCALLTYPE Realloc(_In_opt_ void *pv, _In_ SIZE_T cb) {
  1266. SIZE_T priorSize = pv == nullptr ? (SIZE_T)0 : GetSize(pv);
  1267. void *R = Alloc(cb);
  1268. if (!R)
  1269. return nullptr;
  1270. SIZE_T copySize = std::min(cb, priorSize);
  1271. memcpy(R, pv, copySize);
  1272. Free(pv);
  1273. return R;
  1274. }
  1275. virtual void STDMETHODCALLTYPE Free(_In_opt_ void *pv) {
  1276. if (!pv)
  1277. return;
  1278. PtrData *P = DataFromPtr(pv);
  1279. if (P->FreeFrameCount)
  1280. VERIFY_FAIL(); // double-free detected
  1281. m_Size -= P->Size;
  1282. P->Entry.Flink->Blink = P->Entry.Blink;
  1283. P->Entry.Blink->Flink = P->Entry.Flink;
  1284. if (CaptureStacks)
  1285. P->FreeFrameCount =
  1286. CaptureStackBackTrace(1, StackFrameCount, P->FreeFrames, nullptr);
  1287. }
  1288. virtual SIZE_T STDMETHODCALLTYPE GetSize(
  1289. /* [annotation][in] */
  1290. _In_opt_ _Post_writable_byte_size_(return) void *pv)
  1291. {
  1292. if (pv == nullptr) return 0;
  1293. return DataFromPtr(pv)->Size;
  1294. }
  1295. virtual int STDMETHODCALLTYPE DidAlloc(
  1296. _In_opt_ void *pv) {
  1297. return -1; // don't know
  1298. }
  1299. virtual void STDMETHODCALLTYPE HeapMinimize(void) {}
  1300. void DumpLeaks() {
  1301. PtrData *ptr = (PtrData*)AllocList.Flink;;
  1302. PtrData *end = (PtrData*)AllocList.Blink;;
  1303. WEX::Logging::Log::Comment(FormatToWString(L"Leaks total size: %d", (signed int)m_Size).data());
  1304. while (ptr != end) {
  1305. WEX::Logging::Log::Comment(FormatToWString(L"Memory leak at 0x0%X, size %d, alloc# %d", ptr + 1, ptr->Size, ptr->AllocAtCount).data());
  1306. ptr = (PtrData*)ptr->Entry.Flink;
  1307. }
  1308. }
  1309. };
  1310. #if _ITERATOR_DEBUG_LEVEL==0
  1311. // CompileWhenNoMemThenOOM can properly detect leaks only when debug iterators are disabled
  1312. TEST_F(CompilerTest, CompileWhenNoMemThenOOM) {
  1313. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  1314. CComPtr<IDxcBlobEncoding> pSource;
  1315. CreateBlobFromText(EmptyCompute, &pSource);
  1316. InstrumentedHeapMalloc InstrMalloc;
  1317. CComPtr<IDxcCompiler> pCompiler;
  1318. CComPtr<IDxcOperationResult> pResult;
  1319. ULONG allocCount = 0;
  1320. ULONG allocSize = 0;
  1321. ULONG initialRefCount;
  1322. InstrMalloc.ResetHeap();
  1323. VERIFY_IS_TRUE(m_dllSupport.HasCreateWithMalloc());
  1324. // Verify a simple object creation.
  1325. initialRefCount = InstrMalloc.GetRefCount();
  1326. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler));
  1327. pCompiler.Release();
  1328. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1329. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1330. InstrMalloc.ResetCounts();
  1331. InstrMalloc.ResetHeap();
  1332. // First time, run to completion and capture stats.
  1333. initialRefCount = InstrMalloc.GetRefCount();
  1334. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler));
  1335. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1336. L"cs_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1337. allocCount = InstrMalloc.GetAllocCount();
  1338. allocSize = InstrMalloc.GetAllocSize();
  1339. HRESULT hrWithMemory;
  1340. VERIFY_SUCCEEDED(pResult->GetStatus(&hrWithMemory));
  1341. VERIFY_SUCCEEDED(hrWithMemory);
  1342. pCompiler.Release();
  1343. pResult.Release();
  1344. VERIFY_IS_TRUE(allocSize > allocCount);
  1345. // Ensure that after all resources are released, there are no outstanding
  1346. // allocations or references.
  1347. //
  1348. // First leak is in ((InstrumentedHeapMalloc::PtrData *)InstrMalloc.AllocList.Flink)
  1349. if (InstrMalloc.GetSize() != 0) {
  1350. WEX::Logging::Log::Comment(L"Memory leak(s) detected");
  1351. InstrMalloc.DumpLeaks();
  1352. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1353. }
  1354. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1355. // In Debug, without /D_ITERATOR_DEBUG_LEVEL=0, debug iterators will be used;
  1356. // this causes a problem where std::string is specified as noexcept, and yet
  1357. // a sentinel is allocated that may fail and throw.
  1358. if (m_ver.SkipOutOfMemoryTest()) return;
  1359. // Now, fail each allocation and make sure we get an error.
  1360. for (ULONG i = 0; i <= allocCount; ++i) {
  1361. // LogCommentFmt(L"alloc fail %u", i);
  1362. bool isLast = i == allocCount;
  1363. InstrMalloc.ResetCounts();
  1364. InstrMalloc.ResetHeap();
  1365. InstrMalloc.SetFailAlloc(i + 1);
  1366. HRESULT hrOp = m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler);
  1367. if (SUCCEEDED(hrOp)) {
  1368. hrOp = pCompiler->Compile(pSource, L"source.hlsl", L"main", L"cs_6_0",
  1369. nullptr, 0, nullptr, 0, nullptr, &pResult);
  1370. if (SUCCEEDED(hrOp)) {
  1371. pResult->GetStatus(&hrOp);
  1372. }
  1373. }
  1374. if (FAILED(hrOp)) {
  1375. // This is true in *almost* every case. When the OOM happens during stream
  1376. // handling, there is no specific error set; by the time it's detected,
  1377. // it propagates as E_FAIL.
  1378. //VERIFY_ARE_EQUAL(hrOp, E_OUTOFMEMORY);
  1379. VERIFY_IS_TRUE(hrOp == E_OUTOFMEMORY || hrOp == E_FAIL);
  1380. }
  1381. if (isLast)
  1382. VERIFY_SUCCEEDED(hrOp);
  1383. else
  1384. VERIFY_FAILED(hrOp);
  1385. pCompiler.Release();
  1386. pResult.Release();
  1387. if (InstrMalloc.GetSize() != 0) {
  1388. WEX::Logging::Log::Comment(FormatToWString(L"Memory leak(s) detected, allocCount = %d", i).data());
  1389. InstrMalloc.DumpLeaks();
  1390. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1391. }
  1392. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1393. }
  1394. }
  1395. #endif
  1396. TEST_F(CompilerTest, CompileWhenShaderModelMismatchAttributeThenFail) {
  1397. CComPtr<IDxcCompiler> pCompiler;
  1398. CComPtr<IDxcOperationResult> pResult;
  1399. CComPtr<IDxcBlobEncoding> pSource;
  1400. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1401. CreateBlobFromText(EmptyCompute, &pSource);
  1402. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1403. L"ps_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1404. std::string failLog(VerifyOperationFailed(pResult));
  1405. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find("attribute numthreads only valid for CS"));
  1406. }
  1407. TEST_F(CompilerTest, CompileBadHlslThenFail) {
  1408. CComPtr<IDxcCompiler> pCompiler;
  1409. CComPtr<IDxcOperationResult> pResult;
  1410. CComPtr<IDxcBlobEncoding> pSource;
  1411. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1412. CreateBlobFromText(
  1413. "bad hlsl", &pSource);
  1414. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1415. L"ps_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1416. HRESULT status;
  1417. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1418. VERIFY_FAILED(status);
  1419. }
  1420. TEST_F(CompilerTest, CompileLegacyShaderModelThenFail) {
  1421. VerifyCompileFailed(
  1422. "float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", L"ps_5_1", nullptr);
  1423. }
  1424. TEST_F(CompilerTest, CompileWhenRecursiveAlbeitStaticTermThenFail) {
  1425. // This shader will compile under fxc because if execution is
  1426. // simulated statically, it does terminate. dxc changes this behavior
  1427. // to avoid imposing the requirement on the compiler.
  1428. const char ShaderText[] =
  1429. "static int i = 10;\r\n"
  1430. "float4 f(); // Forward declaration\r\n"
  1431. "float4 g() { if (i > 10) { i--; return f(); } else return 0; } // Recursive call to 'f'\r\n"
  1432. "float4 f() { return g(); } // First call to 'g'\r\n"
  1433. "float4 VS() : SV_Position{\r\n"
  1434. " return f(); // First call to 'f'\r\n"
  1435. "}\r\n";
  1436. VerifyCompileFailed(ShaderText, L"vs_6_0", "recursive functions not allowed", L"VS");
  1437. }
  1438. TEST_F(CompilerTest, CompileWhenRecursiveThenFail) {
  1439. const char ShaderTextSimple[] =
  1440. "float4 f(); // Forward declaration\r\n"
  1441. "float4 g() { return f(); } // Recursive call to 'f'\r\n"
  1442. "float4 f() { return g(); } // First call to 'g'\r\n"
  1443. "float4 main() : SV_Position{\r\n"
  1444. " return f(); // First call to 'f'\r\n"
  1445. "}\r\n";
  1446. VerifyCompileFailed(ShaderTextSimple, L"vs_6_0", "recursive functions not allowed");
  1447. const char ShaderTextIndirect[] =
  1448. "float4 f(); // Forward declaration\r\n"
  1449. "float4 g() { return f(); } // Recursive call to 'f'\r\n"
  1450. "float4 f() { return g(); } // First call to 'g'\r\n"
  1451. "float4 main() : SV_Position{\r\n"
  1452. " return f(); // First call to 'f'\r\n"
  1453. "}\r\n";
  1454. VerifyCompileFailed(ShaderTextIndirect, L"vs_6_0", "recursive functions not allowed");
  1455. const char ShaderTextSelf[] =
  1456. "float4 main() : SV_Position{\r\n"
  1457. " return main();\r\n"
  1458. "}\r\n";
  1459. VerifyCompileFailed(ShaderTextSelf, L"vs_6_0", "recursive functions not allowed");
  1460. const char ShaderTextMissing[] =
  1461. "float4 mainz() : SV_Position{\r\n"
  1462. " return 1;\r\n"
  1463. "}\r\n";
  1464. VerifyCompileFailed(ShaderTextMissing, L"vs_6_0", "missing entry point definition");
  1465. }
  1466. TEST_F(CompilerTest, CompileHlsl2015ThenFail) {
  1467. CComPtr<IDxcCompiler> pCompiler;
  1468. CComPtr<IDxcOperationResult> pResult;
  1469. CComPtr<IDxcBlobEncoding> pSource;
  1470. CComPtr<IDxcBlobEncoding> pErrors;
  1471. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1472. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1473. LPCWSTR args[2] = { L"-HV", L"2015" };
  1474. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1475. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1476. HRESULT status;
  1477. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1478. VERIFY_ARE_EQUAL(status, E_INVALIDARG);
  1479. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  1480. LPCSTR pErrorMsg = "HLSL Version 2015 is only supported for language services";
  1481. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  1482. }
  1483. TEST_F(CompilerTest, CompileHlsl2016ThenOK) {
  1484. CComPtr<IDxcCompiler> pCompiler;
  1485. CComPtr<IDxcOperationResult> pResult;
  1486. CComPtr<IDxcBlobEncoding> pSource;
  1487. CComPtr<IDxcBlobEncoding> pErrors;
  1488. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1489. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1490. LPCWSTR args[2] = { L"-HV", L"2016" };
  1491. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1492. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1493. HRESULT status;
  1494. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1495. VERIFY_SUCCEEDED(status);
  1496. }
  1497. TEST_F(CompilerTest, CompileHlsl2017ThenOK) {
  1498. CComPtr<IDxcCompiler> pCompiler;
  1499. CComPtr<IDxcOperationResult> pResult;
  1500. CComPtr<IDxcBlobEncoding> pSource;
  1501. CComPtr<IDxcBlobEncoding> pErrors;
  1502. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1503. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1504. LPCWSTR args[2] = { L"-HV", L"2017" };
  1505. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1506. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1507. HRESULT status;
  1508. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1509. VERIFY_SUCCEEDED(status);
  1510. }
  1511. TEST_F(CompilerTest, CompileHlsl2018ThenOK) {
  1512. CComPtr<IDxcCompiler> pCompiler;
  1513. CComPtr<IDxcOperationResult> pResult;
  1514. CComPtr<IDxcBlobEncoding> pSource;
  1515. CComPtr<IDxcBlobEncoding> pErrors;
  1516. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1517. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1518. LPCWSTR args[2] = { L"-HV", L"2018" };
  1519. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1520. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1521. HRESULT status;
  1522. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1523. VERIFY_SUCCEEDED(status);
  1524. }
  1525. TEST_F(CompilerTest, CompileHlsl2019ThenFail) {
  1526. CComPtr<IDxcCompiler> pCompiler;
  1527. CComPtr<IDxcOperationResult> pResult;
  1528. CComPtr<IDxcBlobEncoding> pSource;
  1529. CComPtr<IDxcBlobEncoding> pErrors;
  1530. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1531. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1532. LPCWSTR args[2] = { L"-HV", L"2019" };
  1533. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1534. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1535. HRESULT status;
  1536. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1537. VERIFY_ARE_EQUAL(status, E_INVALIDARG);
  1538. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  1539. LPCSTR pErrorMsg = "Unknown HLSL version";
  1540. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  1541. }
  1542. #ifdef _WIN32
  1543. #pragma fenv_access(on)
  1544. #pragma optimize("", off)
  1545. #pragma warning(disable : 4723)
  1546. // Define test state as something weird that we can verify was restored
  1547. static const unsigned int fpTestState =
  1548. (_MCW_EM & (~_EM_ZERODIVIDE)) | // throw on div by zero
  1549. _DN_FLUSH_OPERANDS_SAVE_RESULTS | // denorm flush operands & save results
  1550. _RC_UP; // round up
  1551. static const unsigned int fpTestMask = _MCW_EM | _MCW_DN | _MCW_RC;
  1552. struct FPTestScope
  1553. {
  1554. // _controlfp_s is non-standard and <cfenv> doesn't have a function to enable exceptions
  1555. unsigned int fpSavedState;
  1556. FPTestScope() {
  1557. VERIFY_IS_TRUE(_controlfp_s(&fpSavedState, 0, 0) == 0);
  1558. unsigned int newValue;
  1559. VERIFY_IS_TRUE(_controlfp_s(&newValue, fpTestState, fpTestMask) == 0);
  1560. }
  1561. ~FPTestScope() {
  1562. unsigned int newValue;
  1563. errno_t error = _controlfp_s(&newValue, fpSavedState, fpTestMask);
  1564. DXASSERT_LOCALVAR(error, error == 0, "Failed to restore floating-point environment.");
  1565. }
  1566. };
  1567. void VerifyDivByZeroThrows() {
  1568. bool bCaughtExpectedException = false;
  1569. __try {
  1570. float one = 1.0;
  1571. float zero = 0.0;
  1572. float val = one / zero;
  1573. (void)val;
  1574. } __except(EXCEPTION_EXECUTE_HANDLER) {
  1575. bCaughtExpectedException = true;
  1576. }
  1577. VERIFY_IS_TRUE(bCaughtExpectedException);
  1578. }
  1579. TEST_F(CompilerTest, CodeGenFloatingPointEnvironment) {
  1580. unsigned int fpOriginal;
  1581. VERIFY_IS_TRUE(_controlfp_s(&fpOriginal, 0, 0) == 0);
  1582. {
  1583. FPTestScope fpTestScope;
  1584. // Get state before/after compilation, making sure it's our test state,
  1585. // and that it is restored after the compile.
  1586. unsigned int fpBeforeCompile;
  1587. VERIFY_IS_TRUE(_controlfp_s(&fpBeforeCompile, 0, 0) == 0);
  1588. VERIFY_ARE_EQUAL((fpBeforeCompile & fpTestMask), fpTestState);
  1589. CodeGenTestCheck(L"fpexcept.hlsl");
  1590. // Verify excpetion environment was restored
  1591. unsigned int fpAfterCompile;
  1592. VERIFY_IS_TRUE(_controlfp_s(&fpAfterCompile, 0, 0) == 0);
  1593. VERIFY_ARE_EQUAL((fpBeforeCompile & fpTestMask), (fpAfterCompile & fpTestMask));
  1594. // Make sure round up is set
  1595. VERIFY_ARE_EQUAL(rint(12.25), 13);
  1596. // Make sure we actually enabled div-by-zero exception
  1597. VerifyDivByZeroThrows();
  1598. }
  1599. // Verify original state has been restored
  1600. unsigned int fpLocal;
  1601. VERIFY_IS_TRUE(_controlfp_s(&fpLocal, 0, 0) == 0);
  1602. VERIFY_ARE_EQUAL(fpLocal, fpOriginal);
  1603. }
  1604. #pragma optimize("", on)
  1605. #else // _WIN32
  1606. // Only implemented on Win32
  1607. TEST_F(CompilerTest, CodeGenFloatingPointEnvironment) {
  1608. VERIFY_IS_TRUE(true);
  1609. }
  1610. #endif // _WIN32
  1611. TEST_F(CompilerTest, CodeGenInclude) {
  1612. CodeGenTestCheck(L"Include.hlsl");
  1613. }
  1614. TEST_F(CompilerTest, CodeGenLibCsEntry) {
  1615. CodeGenTestCheck(L"lib_cs_entry.hlsl");
  1616. }
  1617. TEST_F(CompilerTest, CodeGenLibCsEntry2) {
  1618. CodeGenTestCheck(L"lib_cs_entry2.hlsl");
  1619. }
  1620. TEST_F(CompilerTest, CodeGenLibCsEntry3) {
  1621. CodeGenTestCheck(L"lib_cs_entry3.hlsl");
  1622. }
  1623. TEST_F(CompilerTest, CodeGenLibEntries) {
  1624. CodeGenTestCheck(L"lib_entries.hlsl");
  1625. }
  1626. TEST_F(CompilerTest, CodeGenLibEntries2) {
  1627. CodeGenTestCheck(L"lib_entries2.hlsl");
  1628. }
  1629. TEST_F(CompilerTest, CodeGenLibNoAlias) {
  1630. CodeGenTestCheck(L"lib_no_alias.hlsl");
  1631. }
  1632. TEST_F(CompilerTest, CodeGenLibResource) {
  1633. CodeGenTestCheck(L"lib_resource.hlsl");
  1634. }
  1635. TEST_F(CompilerTest, CodeGenLibUnusedFunc) {
  1636. CodeGenTestCheck(L"lib_unused_func.hlsl");
  1637. }
  1638. TEST_F(CompilerTest, CodeGenRootSigProfile) {
  1639. if (m_ver.SkipDxilVersion(1, 5)) return;
  1640. CodeGenTest(L"rootSigProfile.hlsl");
  1641. }
  1642. TEST_F(CompilerTest, CodeGenRootSigProfile2) {
  1643. if (m_ver.SkipDxilVersion(1, 5)) return;
  1644. // TODO: Verify the result when reflect the structures.
  1645. CodeGenTest(L"rootSigProfile2.hlsl");
  1646. }
  1647. TEST_F(CompilerTest, CodeGenRootSigProfile5) {
  1648. if (m_ver.SkipDxilVersion(1, 5)) return;
  1649. CodeGenTest(L"rootSigProfile5.hlsl");
  1650. }
  1651. TEST_F(CompilerTest, LibGVStore) {
  1652. CComPtr<IDxcCompiler> pCompiler;
  1653. CComPtr<IDxcOperationResult> pResult;
  1654. CComPtr<IDxcBlobEncoding> pSource;
  1655. CComPtr<IDxcContainerReflection> pReflection;
  1656. CComPtr<IDxcAssembler> pAssembler;
  1657. VERIFY_SUCCEEDED(this->m_dllSupport.CreateInstance(CLSID_DxcContainerReflection, &pReflection));
  1658. VERIFY_SUCCEEDED(this->m_dllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  1659. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1660. CreateBlobFromText(
  1661. R"(
  1662. struct T {
  1663. RWByteAddressBuffer outputBuffer;
  1664. RWByteAddressBuffer outputBuffer2;
  1665. };
  1666. struct D {
  1667. float4 a;
  1668. int4 b;
  1669. };
  1670. struct T2 {
  1671. RWStructuredBuffer<D> uav;
  1672. };
  1673. T2 resStruct(T t, uint2 id);
  1674. RWByteAddressBuffer outputBuffer;
  1675. RWByteAddressBuffer outputBuffer2;
  1676. [numthreads(8, 8, 1)]
  1677. void main( uint2 id : SV_DispatchThreadID )
  1678. {
  1679. T t = {outputBuffer,outputBuffer2};
  1680. T2 t2 = resStruct(t, id);
  1681. uint counter = t2.uav.IncrementCounter();
  1682. t2.uav[counter].b.xy = id;
  1683. }
  1684. )", &pSource);
  1685. const WCHAR *pArgs[] = {
  1686. L"/Zi",
  1687. };
  1688. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"file.hlsl", L"", L"lib_6_x",
  1689. pArgs, _countof(pArgs), nullptr, 0, nullptr,
  1690. &pResult));
  1691. CComPtr<IDxcBlob> pShader;
  1692. VERIFY_SUCCEEDED(pResult->GetResult(&pShader));
  1693. VERIFY_SUCCEEDED(pReflection->Load(pShader));
  1694. UINT32 index = 0;
  1695. VERIFY_SUCCEEDED(pReflection->FindFirstPartKind(hlsl::DFCC_DXIL, &index));
  1696. CComPtr<IDxcBlob> pBitcode;
  1697. VERIFY_SUCCEEDED(pReflection->GetPartContent(index, &pBitcode));
  1698. const char *bitcode = hlsl::GetDxilBitcodeData((hlsl::DxilProgramHeader *)pBitcode->GetBufferPointer());
  1699. unsigned bitcode_size = hlsl::GetDxilBitcodeSize((hlsl::DxilProgramHeader *)pBitcode->GetBufferPointer());
  1700. CComPtr<IDxcBlobEncoding> pBitcodeBlob;
  1701. CreateBlobPinned(bitcode, bitcode_size, CP_UTF8, &pBitcodeBlob);
  1702. CComPtr<IDxcBlob> pReassembled;
  1703. CComPtr<IDxcOperationResult> pReassembleResult;
  1704. VERIFY_SUCCEEDED(pAssembler->AssembleToContainer(pBitcodeBlob, &pReassembleResult));
  1705. VERIFY_SUCCEEDED(pReassembleResult->GetResult(&pReassembled));
  1706. CComPtr<IDxcBlobEncoding> pTextBlob;
  1707. VERIFY_SUCCEEDED(pCompiler->Disassemble(pReassembled, &pTextBlob));
  1708. std::wstring Text = BlobToUtf16(pTextBlob);
  1709. VERIFY_ARE_NOT_EQUAL(std::wstring::npos, Text.find(L"store"));
  1710. }
  1711. TEST_F(CompilerTest, PreprocessWhenValidThenOK) {
  1712. CComPtr<IDxcCompiler> pCompiler;
  1713. CComPtr<IDxcOperationResult> pResult;
  1714. CComPtr<IDxcBlobEncoding> pSource;
  1715. DxcDefine defines[2];
  1716. defines[0].Name = L"MYDEF";
  1717. defines[0].Value = L"int";
  1718. defines[1].Name = L"MYOTHERDEF";
  1719. defines[1].Value = L"123";
  1720. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1721. CreateBlobFromText(
  1722. "// First line\r\n"
  1723. "MYDEF g_int = MYOTHERDEF;\r\n"
  1724. "#define FOO BAR\r\n"
  1725. "int FOO;", &pSource);
  1726. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl", nullptr, 0,
  1727. defines, _countof(defines), nullptr,
  1728. &pResult));
  1729. HRESULT hrOp;
  1730. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  1731. VERIFY_SUCCEEDED(hrOp);
  1732. CComPtr<IDxcBlob> pOutText;
  1733. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  1734. std::string text(BlobToUtf8(pOutText));
  1735. VERIFY_ARE_EQUAL_STR(
  1736. "#line 1 \"file.hlsl\"\n"
  1737. "\n"
  1738. "int g_int = 123;\n"
  1739. "\n"
  1740. "int BAR;\n", text.c_str());
  1741. }
  1742. TEST_F(CompilerTest, PreprocessWhenExpandTokenPastingOperandThenAccept) {
  1743. // Tests that we can turn on fxc's behavior (pre-expanding operands before
  1744. // performing token-pasting) using -flegacy-macro-expansion
  1745. CComPtr<IDxcCompiler> pCompiler;
  1746. CComPtr<IDxcOperationResult> pResult;
  1747. CComPtr<IDxcBlobEncoding> pSource;
  1748. LPCWSTR expandOption = L"-flegacy-macro-expansion";
  1749. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1750. CreateBlobFromText(R"(
  1751. #define SET_INDEX0 10
  1752. #define BINDING_INDEX0 5
  1753. #define SET(INDEX) SET_INDEX##INDEX
  1754. #define BINDING(INDEX) BINDING_INDEX##INDEX
  1755. #define SET_BIND(NAME,SET,BIND) resource_set_##SET##_bind_##BIND##_##NAME
  1756. #define RESOURCE(NAME,INDEX) SET_BIND(NAME, SET(INDEX), BINDING(INDEX))
  1757. Texture2D<float4> resource_set_10_bind_5_tex;
  1758. float4 main() : SV_Target{
  1759. return RESOURCE(tex, 0)[uint2(1, 2)];
  1760. }
  1761. )",
  1762. &pSource);
  1763. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl", &expandOption,
  1764. 1, nullptr, 0, nullptr, &pResult));
  1765. HRESULT hrOp;
  1766. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  1767. VERIFY_SUCCEEDED(hrOp);
  1768. CComPtr<IDxcBlob> pOutText;
  1769. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  1770. std::string text(BlobToUtf8(pOutText));
  1771. VERIFY_ARE_EQUAL_STR(R"(#line 1 "file.hlsl"
  1772. #line 12 "file.hlsl"
  1773. Texture2D<float4> resource_set_10_bind_5_tex;
  1774. float4 main() : SV_Target{
  1775. return resource_set_10_bind_5_tex[uint2(1, 2)];
  1776. }
  1777. )",
  1778. text.c_str());
  1779. }
  1780. TEST_F(CompilerTest, PreprocessWithDebugOptsThenOk) {
  1781. // Make sure debug options, such as -Zi and -Fd,
  1782. // are simply ignored when preprocessing
  1783. CComPtr<IDxcCompiler> pCompiler;
  1784. CComPtr<IDxcOperationResult> pResult;
  1785. CComPtr<IDxcBlobEncoding> pSource;
  1786. DxcDefine defines[2];
  1787. defines[0].Name = L"MYDEF";
  1788. defines[0].Value = L"int";
  1789. defines[1].Name = L"MYOTHERDEF";
  1790. defines[1].Value = L"123";
  1791. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1792. CreateBlobFromText(
  1793. "// First line\r\n"
  1794. "MYDEF g_int = MYOTHERDEF;\r\n"
  1795. "#define FOO BAR\r\n"
  1796. "int FOO;", &pSource);
  1797. LPCWSTR extraOptions[] = {L"-Zi", L"-Fd", L"file.pdb", L"-Qembed_debug"};
  1798. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl",
  1799. extraOptions, _countof(extraOptions),
  1800. defines, _countof(defines), nullptr,
  1801. &pResult));
  1802. HRESULT hrOp;
  1803. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  1804. VERIFY_SUCCEEDED(hrOp);
  1805. CComPtr<IDxcBlob> pOutText;
  1806. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  1807. std::string text(BlobToUtf8(pOutText));
  1808. VERIFY_ARE_EQUAL_STR(
  1809. "#line 1 \"file.hlsl\"\n"
  1810. "\n"
  1811. "int g_int = 123;\n"
  1812. "\n"
  1813. "int BAR;\n", text.c_str());
  1814. }
  1815. TEST_F(CompilerTest, CompileOtherModesWithDebugOptsThenOk) {
  1816. // Make sure debug options, such as -Zi and -Fd,
  1817. // are simply ignored when compiling in modes:
  1818. // /Odump -ast-dump -fcgl -rootsig_1_0
  1819. CComPtr<IDxcCompiler> pCompiler;
  1820. CComPtr<IDxcBlobEncoding> pSource;
  1821. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1822. CreateBlobFromText(
  1823. "#define RS \"CBV(b0)\"\n"
  1824. "[RootSignature(RS)]\n"
  1825. "float main(float i : IN) : OUT { return i * 2.0F; }",
  1826. &pSource);
  1827. auto testWithOpts = [&](LPCWSTR entry, LPCWSTR target, llvm::ArrayRef<LPCWSTR> mainOpts) -> HRESULT {
  1828. std::vector<LPCWSTR> opts(mainOpts);
  1829. opts.insert(opts.end(), {L"-Zi", L"-Fd", L"file.pdb"});
  1830. CComPtr<IDxcOperationResult> pResult;
  1831. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"file.hlsl",
  1832. entry, target, opts.data(), opts.size(),
  1833. nullptr, 0, nullptr, &pResult));
  1834. HRESULT hrOp;
  1835. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  1836. return hrOp;
  1837. };
  1838. VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"/Odump"}));
  1839. VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"-ast-dump"}));
  1840. VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"-fcgl"}));
  1841. VERIFY_SUCCEEDED(testWithOpts(L"RS", L"rootsig_1_0", {}));
  1842. }
  1843. TEST_F(CompilerTest, WhenSigMismatchPCFunctionThenFail) {
  1844. CComPtr<IDxcCompiler> pCompiler;
  1845. CComPtr<IDxcOperationResult> pResult;
  1846. CComPtr<IDxcBlobEncoding> pSource;
  1847. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1848. CreateBlobFromText(
  1849. "struct PSSceneIn \n\
  1850. { \n\
  1851. float4 pos : SV_Position; \n\
  1852. float2 tex : TEXCOORD0; \n\
  1853. float3 norm : NORMAL; \n\
  1854. }; \n"
  1855. "struct HSPerPatchData { \n\
  1856. float edges[ 3 ] : SV_TessFactor; \n\
  1857. float inside : SV_InsideTessFactor; \n\
  1858. float foo : FOO; \n\
  1859. }; \n"
  1860. "HSPerPatchData HSPerPatchFunc( InputPatch< PSSceneIn, 3 > points, \n\
  1861. OutputPatch<PSSceneIn, 3> outpoints) { \n\
  1862. HSPerPatchData d = (HSPerPatchData)0; \n\
  1863. d.edges[ 0 ] = points[0].tex.x + outpoints[0].tex.x; \n\
  1864. d.edges[ 1 ] = 1; \n\
  1865. d.edges[ 2 ] = 1; \n\
  1866. d.inside = 1; \n\
  1867. return d; \n\
  1868. } \n"
  1869. "[domain(\"tri\")] \n\
  1870. [partitioning(\"fractional_odd\")] \n\
  1871. [outputtopology(\"triangle_cw\")] \n\
  1872. [patchconstantfunc(\"HSPerPatchFunc\")] \n\
  1873. [outputcontrolpoints(3)] \n"
  1874. "void main(const uint id : SV_OutputControlPointID, \n\
  1875. const InputPatch< PSSceneIn, 3 > points ) { \n\
  1876. } \n"
  1877. , &pSource);
  1878. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1879. L"hs_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1880. std::string failLog(VerifyOperationFailed(pResult));
  1881. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find(
  1882. "Signature element SV_Position, referred to by patch constant function, is not found in corresponding hull shader output."));
  1883. }
  1884. TEST_F(CompilerTest, SubobjectCodeGenErrors) {
  1885. struct SubobjectErrorTestCase {
  1886. const char *shaderText;
  1887. const char *expectedError;
  1888. };
  1889. SubobjectErrorTestCase testCases[] = {
  1890. { "GlobalRootSignature grs;", "1:1: error: subobject needs to be initialized" },
  1891. { "StateObjectConfig soc;", "1:1: error: subobject needs to be initialized" },
  1892. { "LocalRootSignature lrs;", "1:1: error: subobject needs to be initialized" },
  1893. { "SubobjectToExportsAssociation sea;", "1:1: error: subobject needs to be initialized" },
  1894. { "RaytracingShaderConfig rsc;", "1:1: error: subobject needs to be initialized" },
  1895. { "RaytracingPipelineConfig rpc;", "1:1: error: subobject needs to be initialized" },
  1896. { "RaytracingPipelineConfig1 rpc1;", "1:1: error: subobject needs to be initialized" },
  1897. { "TriangleHitGroup hitGt;", "1:1: error: subobject needs to be initialized" },
  1898. { "ProceduralPrimitiveHitGroup hitGt;", "1:1: error: subobject needs to be initialized" },
  1899. { "GlobalRootSignature grs2 = {\"\"};", "1:29: error: empty string not expected here" },
  1900. { "LocalRootSignature lrs2 = {\"\"};", "1:28: error: empty string not expected here" },
  1901. { "SubobjectToExportsAssociation sea2 = { \"\", \"x\" };", "1:40: error: empty string not expected here" },
  1902. { "string s; SubobjectToExportsAssociation sea4 = { \"x\", s };", "1:55: error: cannot convert to constant string" },
  1903. { "extern int v; RaytracingPipelineConfig rpc2 = { v + 16 };", "1:49: error: cannot convert to constant unsigned int" },
  1904. { "string s; TriangleHitGroup trHitGt2_8 = { s, \"foo\" };", "1:43: error: cannot convert to constant string" },
  1905. { "string s; ProceduralPrimitiveHitGroup ppHitGt2_8 = { s, \"\", s };", "1:54: error: cannot convert to constant string" },
  1906. { "ProceduralPrimitiveHitGroup ppHitGt2_9 = { \"a\", \"b\", \"\"};", "1:54: error: empty string not expected here" }
  1907. };
  1908. for (unsigned i = 0; i < _countof(testCases); i++) {
  1909. CComPtr<IDxcCompiler> pCompiler;
  1910. CComPtr<IDxcOperationResult> pResult;
  1911. CComPtr<IDxcBlobEncoding> pSource;
  1912. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1913. CreateBlobFromText(testCases[i].shaderText, &pSource);
  1914. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"", L"lib_6_4", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1915. std::string failLog(VerifyOperationFailed(pResult));
  1916. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find(testCases[i].expectedError));
  1917. }
  1918. }
  1919. #ifdef _WIN32
  1920. TEST_F(CompilerTest, ManualFileCheckTest) {
  1921. #else
  1922. TEST_F(CompilerTest, DISABLED_ManualFileCheckTest) {
  1923. #endif
  1924. using namespace llvm;
  1925. using namespace WEX::TestExecution;
  1926. WEX::Common::String value;
  1927. VERIFY_SUCCEEDED(RuntimeParameters::TryGetValue(L"InputPath", value));
  1928. std::wstring path = value;
  1929. if (!llvm::sys::path::is_absolute(CW2A(path.c_str()).m_psz)) {
  1930. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  1931. }
  1932. bool isDirectory;
  1933. {
  1934. // Temporarily setup the filesystem for testing whether the path is a directory.
  1935. // If it is, CodeGenTestCheckBatchDir will create its own instance.
  1936. llvm::sys::fs::MSFileSystem *msfPtr;
  1937. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  1938. std::unique_ptr<llvm::sys::fs::MSFileSystem> msf(msfPtr);
  1939. llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  1940. IFTLLVM(pts.error_code());
  1941. isDirectory = llvm::sys::fs::is_directory(CW2A(path.c_str()).m_psz);
  1942. }
  1943. if (isDirectory) {
  1944. CodeGenTestCheckBatchDir(path, /*implicitDir*/ false);
  1945. } else {
  1946. CodeGenTestCheck(path.c_str(), /*implicitDir*/ false);
  1947. }
  1948. }
  1949. TEST_F(CompilerTest, CodeGenHashStability) {
  1950. CodeGenTestCheckBatchHash(L"");
  1951. }
  1952. TEST_F(CompilerTest, BatchD3DReflect) {
  1953. CodeGenTestCheckBatchDir(L"d3dreflect");
  1954. }
  1955. TEST_F(CompilerTest, BatchDxil) {
  1956. CodeGenTestCheckBatchDir(L"dxil");
  1957. }
  1958. TEST_F(CompilerTest, BatchHLSL) {
  1959. CodeGenTestCheckBatchDir(L"hlsl");
  1960. }
  1961. TEST_F(CompilerTest, BatchInfra) {
  1962. CodeGenTestCheckBatchDir(L"infra");
  1963. }
  1964. TEST_F(CompilerTest, BatchPasses) {
  1965. CodeGenTestCheckBatchDir(L"passes");
  1966. }
  1967. TEST_F(CompilerTest, BatchShaderTargets) {
  1968. CodeGenTestCheckBatchDir(L"shader_targets");
  1969. }
  1970. TEST_F(CompilerTest, BatchValidation) {
  1971. CodeGenTestCheckBatchDir(L"validation");
  1972. }
  1973. TEST_F(CompilerTest, BatchSamples) {
  1974. CodeGenTestCheckBatchDir(L"samples");
  1975. }