CompilerTest.cpp 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079
  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 <set>
  19. #include <cassert>
  20. #include <sstream>
  21. #include <algorithm>
  22. #include <cfloat>
  23. #include "dxc/DxilContainer/DxilContainer.h"
  24. #include "dxc/Support/WinIncludes.h"
  25. #include "dxc/dxcapi.h"
  26. #include "dxc/dxcpix.h"
  27. #ifdef _WIN32
  28. #include <atlfile.h>
  29. #include <d3dcompiler.h>
  30. #include "dia2.h"
  31. #endif
  32. #include "dxc/Test/HLSLTestData.h"
  33. #include "dxc/Test/HlslTestUtils.h"
  34. #include "dxc/Test/DxcTestUtils.h"
  35. #include "llvm/Support/raw_os_ostream.h"
  36. #include "dxc/Support/Global.h"
  37. #include "dxc/Support/dxcapi.use.h"
  38. #include "dxc/Support/microcom.h"
  39. #include "dxc/Support/HLSLOptions.h"
  40. #include "dxc/Support/Unicode.h"
  41. #include <fstream>
  42. #include "llvm/Support/FileSystem.h"
  43. #include "llvm/Support/MSFileSystem.h"
  44. #include "llvm/Support/Path.h"
  45. #include "llvm/ADT/SmallString.h"
  46. #include "llvm/ADT/StringSwitch.h"
  47. using namespace std;
  48. using namespace hlsl_test;
  49. class TestIncludeHandler : public IDxcIncludeHandler {
  50. DXC_MICROCOM_REF_FIELD(m_dwRef)
  51. public:
  52. DXC_MICROCOM_ADDREF_RELEASE_IMPL(m_dwRef)
  53. dxc::DxcDllSupport &m_dllSupport;
  54. HRESULT m_defaultErrorCode = E_FAIL;
  55. TestIncludeHandler(dxc::DxcDllSupport &dllSupport) : m_dwRef(0), m_dllSupport(dllSupport), callIndex(0) { }
  56. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override {
  57. return DoBasicQueryInterface<IDxcIncludeHandler>(this, iid, ppvObject);
  58. }
  59. struct LoadSourceCallInfo {
  60. std::wstring Filename; // Filename as written in #include statement
  61. LoadSourceCallInfo(LPCWSTR pFilename) :
  62. Filename(pFilename) { }
  63. };
  64. std::vector<LoadSourceCallInfo> CallInfos;
  65. std::wstring GetAllFileNames() const {
  66. std::wstringstream s;
  67. for (size_t i = 0; i < CallInfos.size(); ++i) {
  68. s << CallInfos[i].Filename << ';';
  69. }
  70. return s.str();
  71. }
  72. struct LoadSourceCallResult {
  73. HRESULT hr;
  74. std::string source;
  75. UINT32 codePage;
  76. LoadSourceCallResult() : hr(E_FAIL), codePage(0) { }
  77. LoadSourceCallResult(const char *pSource, UINT32 codePage = CP_UTF8) : hr(S_OK), source(pSource), codePage(codePage) { }
  78. };
  79. std::vector<LoadSourceCallResult> CallResults;
  80. size_t callIndex;
  81. HRESULT STDMETHODCALLTYPE LoadSource(
  82. _In_ LPCWSTR pFilename, // Filename as written in #include statement
  83. _COM_Outptr_ IDxcBlob **ppIncludeSource // Resultant source object for included file
  84. ) override {
  85. CallInfos.push_back(LoadSourceCallInfo(pFilename));
  86. *ppIncludeSource = nullptr;
  87. if (callIndex >= CallResults.size()) {
  88. return m_defaultErrorCode;
  89. }
  90. if (FAILED(CallResults[callIndex].hr)) {
  91. return CallResults[callIndex++].hr;
  92. }
  93. MultiByteStringToBlob(m_dllSupport, CallResults[callIndex].source,
  94. CallResults[callIndex].codePage, ppIncludeSource);
  95. return CallResults[callIndex++].hr;
  96. }
  97. };
  98. #ifdef _WIN32
  99. class CompilerTest {
  100. #else
  101. class CompilerTest : public ::testing::Test {
  102. #endif
  103. public:
  104. BEGIN_TEST_CLASS(CompilerTest)
  105. TEST_CLASS_PROPERTY(L"Parallel", L"true")
  106. TEST_METHOD_PROPERTY(L"Priority", L"0")
  107. END_TEST_CLASS()
  108. TEST_CLASS_SETUP(InitSupport);
  109. TEST_METHOD(CompileWhenDefinesThenApplied)
  110. TEST_METHOD(CompileWhenDefinesManyThenApplied)
  111. TEST_METHOD(CompileWhenEmptyThenFails)
  112. TEST_METHOD(CompileWhenIncorrectThenFails)
  113. TEST_METHOD(CompileWhenWorksThenDisassembleWorks)
  114. TEST_METHOD(CompileWhenDebugWorksThenStripDebug)
  115. TEST_METHOD(CompileWhenWorksThenAddRemovePrivate)
  116. TEST_METHOD(CompileThenAddCustomDebugName)
  117. TEST_METHOD(CompileThenTestPdbUtils)
  118. TEST_METHOD(CompileThenTestPdbInPrivate)
  119. TEST_METHOD(CompileThenTestPdbUtilsStripped)
  120. TEST_METHOD(CompileThenTestPdbUtilsEmptyEntry)
  121. TEST_METHOD(CompileThenTestPdbUtilsRelativePath)
  122. TEST_METHOD(CompileWithRootSignatureThenStripRootSignature)
  123. TEST_METHOD(CompileWhenIncludeThenLoadInvoked)
  124. TEST_METHOD(CompileWhenIncludeThenLoadUsed)
  125. TEST_METHOD(CompileWhenIncludeAbsoluteThenLoadAbsolute)
  126. TEST_METHOD(CompileWhenIncludeLocalThenLoadRelative)
  127. TEST_METHOD(CompileWhenIncludeSystemThenLoadNotRelative)
  128. TEST_METHOD(CompileWhenIncludeSystemMissingThenLoadAttempt)
  129. TEST_METHOD(CompileWhenIncludeFlagsThenIncludeUsed)
  130. TEST_METHOD(CompileWhenIncludeMissingThenFail)
  131. TEST_METHOD(CompileWhenIncludeHasPathThenOK)
  132. TEST_METHOD(CompileWhenIncludeEmptyThenOK)
  133. TEST_METHOD(CompileWhenODumpThenPassConfig)
  134. TEST_METHOD(CompileWhenODumpThenOptimizerMatch)
  135. TEST_METHOD(CompileWhenVdThenProducesDxilContainer)
  136. #if _ITERATOR_DEBUG_LEVEL==0
  137. // CompileWhenNoMemThenOOM can properly detect leaks only when debug iterators are disabled
  138. BEGIN_TEST_METHOD(CompileWhenNoMemThenOOM)
  139. // Disabled because there are problems where we try to allocate memory in destructors,
  140. // which causes more bad_alloc() throws while unwinding bad_alloc(), which asserts
  141. // If only failing one allocation, there are allocations where failing them is lost,
  142. // such as in ~raw_string_ostream(), where it flushes, then eats bad_alloc(), if thrown.
  143. TEST_METHOD_PROPERTY(L"Ignore", L"true")
  144. END_TEST_METHOD()
  145. #endif
  146. TEST_METHOD(CompileWhenShaderModelMismatchAttributeThenFail)
  147. TEST_METHOD(CompileBadHlslThenFail)
  148. TEST_METHOD(CompileLegacyShaderModelThenFail)
  149. TEST_METHOD(CompileWhenRecursiveAlbeitStaticTermThenFail)
  150. TEST_METHOD(CompileWhenRecursiveThenFail)
  151. TEST_METHOD(CompileHlsl2015ThenFail)
  152. TEST_METHOD(CompileHlsl2016ThenOK)
  153. TEST_METHOD(CompileHlsl2017ThenOK)
  154. TEST_METHOD(CompileHlsl2018ThenOK)
  155. TEST_METHOD(CompileHlsl2019ThenFail)
  156. TEST_METHOD(CodeGenFloatingPointEnvironment)
  157. TEST_METHOD(CodeGenInclude)
  158. TEST_METHOD(CodeGenLibCsEntry)
  159. TEST_METHOD(CodeGenLibCsEntry2)
  160. TEST_METHOD(CodeGenLibCsEntry3)
  161. TEST_METHOD(CodeGenLibEntries)
  162. TEST_METHOD(CodeGenLibEntries2)
  163. TEST_METHOD(CodeGenLibNoAlias)
  164. TEST_METHOD(CodeGenLibResource)
  165. TEST_METHOD(CodeGenLibUnusedFunc)
  166. TEST_METHOD(CodeGenRootSigProfile)
  167. TEST_METHOD(CodeGenRootSigProfile2)
  168. TEST_METHOD(CodeGenRootSigProfile5)
  169. TEST_METHOD(CodeGenWaveSize)
  170. TEST_METHOD(PreprocessWhenValidThenOK)
  171. TEST_METHOD(LibGVStore)
  172. TEST_METHOD(PreprocessWhenExpandTokenPastingOperandThenAccept)
  173. TEST_METHOD(PreprocessWithDebugOptsThenOk)
  174. TEST_METHOD(WhenSigMismatchPCFunctionThenFail)
  175. TEST_METHOD(CompileOtherModesWithDebugOptsThenOk)
  176. TEST_METHOD(BatchSamples)
  177. TEST_METHOD(BatchD3DReflect)
  178. TEST_METHOD(BatchDxil)
  179. TEST_METHOD(BatchHLSL)
  180. TEST_METHOD(BatchInfra)
  181. TEST_METHOD(BatchPasses)
  182. TEST_METHOD(BatchShaderTargets)
  183. TEST_METHOD(BatchValidation)
  184. TEST_METHOD(SubobjectCodeGenErrors)
  185. BEGIN_TEST_METHOD(ManualFileCheckTest)
  186. TEST_METHOD_PROPERTY(L"Ignore", L"true")
  187. END_TEST_METHOD()
  188. // Batch directories
  189. BEGIN_TEST_METHOD(CodeGenHashStability)
  190. TEST_METHOD_PROPERTY(L"Priority", L"2")
  191. END_TEST_METHOD()
  192. dxc::DxcDllSupport m_dllSupport;
  193. VersionSupportInfo m_ver;
  194. void CreateBlobPinned(_In_bytecount_(size) LPCVOID data, SIZE_T size,
  195. UINT32 codePage, _Outptr_ IDxcBlobEncoding **ppBlob) {
  196. CComPtr<IDxcLibrary> library;
  197. IFT(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  198. IFT(library->CreateBlobWithEncodingFromPinned(data, size, codePage,
  199. ppBlob));
  200. }
  201. void CreateBlobFromFile(LPCWSTR name, _Outptr_ IDxcBlobEncoding **ppBlob) {
  202. CComPtr<IDxcLibrary> library;
  203. IFT(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  204. const std::wstring path = hlsl_test::GetPathToHlslDataFile(name);
  205. IFT(library->CreateBlobFromFile(path.c_str(), nullptr, ppBlob));
  206. }
  207. void CreateBlobFromText(_In_z_ const char *pText,
  208. _Outptr_ IDxcBlobEncoding **ppBlob) {
  209. CreateBlobPinned(pText, strlen(pText) + 1, CP_UTF8, ppBlob);
  210. }
  211. HRESULT CreateCompiler(IDxcCompiler **ppResult) {
  212. return m_dllSupport.CreateInstance(CLSID_DxcCompiler, ppResult);
  213. }
  214. void TestPdbUtils(bool bSlim, bool bLegacy, bool bStrip);
  215. #ifdef _WIN32 // No ContainerBuilder support yet
  216. HRESULT CreateContainerBuilder(IDxcContainerBuilder **ppResult) {
  217. return m_dllSupport.CreateInstance(CLSID_DxcContainerBuilder, ppResult);
  218. }
  219. #endif
  220. template <typename T, typename TDefault, typename TIface>
  221. void WriteIfValue(TIface *pSymbol, std::wstringstream &o,
  222. TDefault defaultValue, LPCWSTR valueLabel,
  223. HRESULT (__stdcall TIface::*pFn)(T *)) {
  224. T value;
  225. HRESULT hr = (pSymbol->*(pFn))(&value);
  226. if (SUCCEEDED(hr) && value != defaultValue) {
  227. o << L", " << valueLabel << L": " << value;
  228. }
  229. }
  230. std::string GetOption(std::string &cmd, char *opt) {
  231. std::string option = cmd.substr(cmd.find(opt));
  232. option = option.substr(option.find_first_of(' '));
  233. option = option.substr(option.find_first_not_of(' '));
  234. return option.substr(0, option.find_first_of(' '));
  235. }
  236. void CodeGenTest(std::wstring name) {
  237. CComPtr<IDxcCompiler> pCompiler;
  238. CComPtr<IDxcOperationResult> pResult;
  239. CComPtr<IDxcBlobEncoding> pSource;
  240. name.insert(0, L"..\\CodeGenHLSL\\");
  241. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  242. CreateBlobFromFile(name.c_str(), &pSource);
  243. std::string cmdLine = GetFirstLine(name.c_str());
  244. llvm::StringRef argsRef = cmdLine;
  245. llvm::SmallVector<llvm::StringRef, 8> splitArgs;
  246. argsRef.split(splitArgs, " ");
  247. hlsl::options::MainArgs argStrings(splitArgs);
  248. std::string errorString;
  249. llvm::raw_string_ostream errorStream(errorString);
  250. hlsl::options::DxcOpts opts;
  251. IFT(ReadDxcOpts(hlsl::options::getHlslOptTable(), /*flagsToInclude*/ 0,
  252. argStrings, opts, errorStream));
  253. std::wstring entry =
  254. Unicode::UTF8ToUTF16StringOrThrow(opts.EntryPoint.str().c_str());
  255. std::wstring profile =
  256. Unicode::UTF8ToUTF16StringOrThrow(opts.TargetProfile.str().c_str());
  257. std::vector<std::wstring> argLists;
  258. CopyArgsToWStrings(opts.Args, hlsl::options::CoreOption, argLists);
  259. std::vector<LPCWSTR> args;
  260. args.reserve(argLists.size());
  261. for (const std::wstring &a : argLists)
  262. args.push_back(a.data());
  263. VERIFY_SUCCEEDED(pCompiler->Compile(
  264. pSource, name.c_str(), entry.c_str(), profile.c_str(), args.data(), args.size(),
  265. opts.Defines.data(), opts.Defines.size(), nullptr, &pResult));
  266. VERIFY_IS_NOT_NULL(pResult, L"Failed to compile - pResult NULL");
  267. HRESULT result;
  268. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  269. if (FAILED(result)) {
  270. CComPtr<IDxcBlobEncoding> pErr;
  271. IFT(pResult->GetErrorBuffer(&pErr));
  272. std::string errString(BlobToUtf8(pErr));
  273. CA2W errStringW(errString.c_str(), CP_UTF8);
  274. WEX::Logging::Log::Comment(L"Failed to compile - errors follow");
  275. WEX::Logging::Log::Comment(errStringW);
  276. }
  277. VERIFY_SUCCEEDED(result);
  278. CComPtr<IDxcBlob> pProgram;
  279. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  280. if (opts.IsRootSignatureProfile())
  281. return;
  282. CComPtr<IDxcBlobEncoding> pDisassembleBlob;
  283. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembleBlob));
  284. std::string disassembleString(BlobToUtf8(pDisassembleBlob));
  285. VERIFY_ARE_NOT_EQUAL(0U, disassembleString.size());
  286. }
  287. void CodeGenTestHashFullPath(LPCWSTR fullPath) {
  288. FileRunTestResult t = FileRunTestResult::RunHashTestFromFileCommands(fullPath);
  289. if (t.RunResult != 0) {
  290. CA2W commentWide(t.ErrorMessage.c_str(), CP_UTF8);
  291. WEX::Logging::Log::Comment(commentWide);
  292. WEX::Logging::Log::Error(L"Run result is not zero");
  293. }
  294. }
  295. void CodeGenTestHash(LPCWSTR name, bool implicitDir) {
  296. std::wstring path = name;
  297. if (implicitDir) {
  298. path.insert(0, L"..\\CodeGenHLSL\\");
  299. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  300. }
  301. CodeGenTestHashFullPath(path.c_str());
  302. }
  303. void CodeGenTestCheckBatchHash(std::wstring suitePath, bool implicitDir = true) {
  304. using namespace llvm;
  305. using namespace WEX::TestExecution;
  306. if (implicitDir) suitePath.insert(0, L"..\\HLSLFileCheck\\");
  307. ::llvm::sys::fs::MSFileSystem *msfPtr;
  308. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  309. std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr);
  310. ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  311. IFTLLVM(pts.error_code());
  312. CW2A pUtf8Filename(suitePath.c_str());
  313. if (!llvm::sys::path::is_absolute(pUtf8Filename.m_psz)) {
  314. suitePath = hlsl_test::GetPathToHlslDataFile(suitePath.c_str());
  315. }
  316. CW2A utf8SuitePath(suitePath.c_str());
  317. unsigned numTestsRun = 0;
  318. std::error_code EC;
  319. llvm::SmallString<128> DirNative;
  320. llvm::sys::path::native(utf8SuitePath.m_psz, DirNative);
  321. for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC), DirEnd;
  322. Dir != DirEnd && !EC; Dir.increment(EC)) {
  323. // Check whether this entry has an extension typically associated with
  324. // headers.
  325. if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
  326. .Cases(".hlsl", ".ll", true).Default(false))
  327. continue;
  328. StringRef filename = Dir->path();
  329. std::string filetag = Dir->path();
  330. filetag += "<HASH>";
  331. CA2W wRelTag(filetag.data());
  332. CA2W wRelPath(filename.data());
  333. WEX::Logging::Log::StartGroup(wRelTag);
  334. CodeGenTestHash(wRelPath, /*implicitDir*/ false);
  335. WEX::Logging::Log::EndGroup(wRelTag);
  336. numTestsRun++;
  337. }
  338. VERIFY_IS_GREATER_THAN(numTestsRun, (unsigned)0, L"No test files found in batch directory.");
  339. }
  340. void CodeGenTestCheckFullPath(LPCWSTR fullPath, LPCWSTR dumpPath = nullptr) {
  341. // Create file system if needed
  342. llvm::sys::fs::MSFileSystem *msfPtr = llvm::sys::fs::GetCurrentThreadFileSystem();
  343. std::unique_ptr<llvm::sys::fs::MSFileSystem> msf;
  344. if (!msfPtr) {
  345. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  346. msf.reset(msfPtr);
  347. }
  348. llvm::sys::fs::AutoPerThreadSystem pts(msfPtr);
  349. IFTLLVM(pts.error_code());
  350. FileRunTestResult t = FileRunTestResult::RunFromFileCommands(fullPath,
  351. /*pPluginToolsPaths*/nullptr, dumpPath);
  352. if (t.RunResult != 0) {
  353. CA2W commentWide(t.ErrorMessage.c_str(), CP_UTF8);
  354. WEX::Logging::Log::Comment(commentWide);
  355. WEX::Logging::Log::Error(L"Run result is not zero");
  356. }
  357. }
  358. void CodeGenTestCheck(LPCWSTR name, bool implicitDir = true, LPCWSTR dumpPath = nullptr) {
  359. std::wstring path = name;
  360. std::wstring dumpStr;
  361. if (implicitDir) {
  362. path.insert(0, L"..\\CodeGenHLSL\\");
  363. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  364. if (!dumpPath) {
  365. dumpStr = hlsl_test::GetPathToHlslDataFile(path.c_str(), FILECHECKDUMPDIRPARAM);
  366. dumpPath = dumpStr.empty() ? nullptr : dumpStr.c_str();
  367. }
  368. }
  369. CodeGenTestCheckFullPath(path.c_str(), dumpPath);
  370. }
  371. void CodeGenTestCheckBatchDir(std::wstring suitePath, bool implicitDir = true) {
  372. using namespace llvm;
  373. using namespace WEX::TestExecution;
  374. if (implicitDir) suitePath.insert(0, L"..\\HLSLFileCheck\\");
  375. ::llvm::sys::fs::MSFileSystem *msfPtr;
  376. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  377. std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr);
  378. ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  379. IFTLLVM(pts.error_code());
  380. std::wstring dumpPath;
  381. CW2A pUtf8Filename(suitePath.c_str());
  382. if (!llvm::sys::path::is_absolute(pUtf8Filename.m_psz)) {
  383. dumpPath = hlsl_test::GetPathToHlslDataFile(suitePath.c_str(), FILECHECKDUMPDIRPARAM);
  384. suitePath = hlsl_test::GetPathToHlslDataFile(suitePath.c_str());
  385. }
  386. CW2A utf8SuitePath(suitePath.c_str());
  387. unsigned numTestsRun = 0;
  388. std::error_code EC;
  389. llvm::SmallString<128> DirNative;
  390. llvm::sys::path::native(utf8SuitePath.m_psz, DirNative);
  391. for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC), DirEnd;
  392. Dir != DirEnd && !EC; Dir.increment(EC)) {
  393. // Check whether this entry has an extension typically associated with
  394. // headers.
  395. if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
  396. .Cases(".hlsl", ".ll", true).Default(false))
  397. continue;
  398. StringRef filename = Dir->path();
  399. CA2W wRelPath(filename.data());
  400. std::wstring dumpStr;
  401. if (!dumpPath.empty() && suitePath.compare(0, suitePath.size(), wRelPath.m_psz, suitePath.size()) == 0) {
  402. dumpStr = dumpPath + (wRelPath.m_psz + suitePath.size());
  403. }
  404. WEX::Logging::Log::StartGroup(wRelPath);
  405. CodeGenTestCheck(wRelPath, /*implicitDir*/ false,
  406. dumpStr.empty() ? nullptr : dumpStr.c_str());
  407. WEX::Logging::Log::EndGroup(wRelPath);
  408. numTestsRun++;
  409. }
  410. VERIFY_IS_GREATER_THAN(numTestsRun, (unsigned)0, L"No test files found in batch directory.");
  411. }
  412. std::string VerifyCompileFailed(LPCSTR pText, LPCWSTR pTargetProfile, LPCSTR pErrorMsg) {
  413. return VerifyCompileFailed(pText, pTargetProfile, pErrorMsg, L"main");
  414. }
  415. std::string VerifyCompileFailed(LPCSTR pText, LPCWSTR pTargetProfile, LPCSTR pErrorMsg, LPCWSTR pEntryPoint) {
  416. CComPtr<IDxcCompiler> pCompiler;
  417. CComPtr<IDxcOperationResult> pResult;
  418. CComPtr<IDxcBlobEncoding> pSource;
  419. CComPtr<IDxcBlobEncoding> pErrors;
  420. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  421. CreateBlobFromText(pText, &pSource);
  422. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  423. pTargetProfile, nullptr, 0, nullptr, 0, nullptr, &pResult));
  424. HRESULT status;
  425. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  426. VERIFY_FAILED(status);
  427. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  428. if (pErrorMsg && *pErrorMsg) {
  429. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  430. }
  431. return BlobToUtf8(pErrors);
  432. }
  433. void VerifyOperationSucceeded(IDxcOperationResult *pResult) {
  434. HRESULT result;
  435. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  436. if (FAILED(result)) {
  437. CComPtr<IDxcBlobEncoding> pErrors;
  438. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  439. CA2W errorsWide(BlobToUtf8(pErrors).c_str(), CP_UTF8);
  440. WEX::Logging::Log::Comment(errorsWide);
  441. }
  442. VERIFY_SUCCEEDED(result);
  443. }
  444. std::string VerifyOperationFailed(IDxcOperationResult *pResult) {
  445. HRESULT result;
  446. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  447. VERIFY_FAILED(result);
  448. CComPtr<IDxcBlobEncoding> pErrors;
  449. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  450. return BlobToUtf8(pErrors);
  451. }
  452. #ifdef _WIN32 // - exclude dia stuff
  453. HRESULT CreateDiaSourceForCompile(const char *hlsl, IDiaDataSource **ppDiaSource)
  454. {
  455. if (!ppDiaSource)
  456. return E_POINTER;
  457. CComPtr<IDxcCompiler> pCompiler;
  458. CComPtr<IDxcOperationResult> pResult;
  459. CComPtr<IDxcBlobEncoding> pSource;
  460. CComPtr<IDxcBlob> pProgram;
  461. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  462. CreateBlobFromText(hlsl, &pSource);
  463. LPCWSTR args[] = { L"/Zi", L"/Qembed_debug" };
  464. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  465. L"ps_6_0", args, _countof(args), nullptr, 0, nullptr, &pResult));
  466. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  467. // Disassemble the compiled (stripped) program.
  468. {
  469. CComPtr<IDxcBlobEncoding> pDisassembly;
  470. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembly));
  471. std::string disText = BlobToUtf8(pDisassembly);
  472. CA2W disTextW(disText.c_str(), CP_UTF8);
  473. //WEX::Logging::Log::Comment(disTextW);
  474. }
  475. // CONSIDER: have the dia data source look for the part if passed a whole container.
  476. CComPtr<IDiaDataSource> pDiaSource;
  477. CComPtr<IStream> pProgramStream;
  478. CComPtr<IDxcLibrary> pLib;
  479. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  480. const hlsl::DxilContainerHeader *pContainer = hlsl::IsDxilContainerLike(
  481. pProgram->GetBufferPointer(), pProgram->GetBufferSize());
  482. VERIFY_IS_NOT_NULL(pContainer);
  483. hlsl::DxilPartIterator partIter =
  484. std::find_if(hlsl::begin(pContainer), hlsl::end(pContainer),
  485. hlsl::DxilPartIsType(hlsl::DFCC_ShaderDebugInfoDXIL));
  486. const hlsl::DxilProgramHeader *pProgramHeader =
  487. (const hlsl::DxilProgramHeader *)hlsl::GetDxilPartData(*partIter);
  488. uint32_t bitcodeLength;
  489. const char *pBitcode;
  490. CComPtr<IDxcBlob> pProgramPdb;
  491. hlsl::GetDxilProgramBitcode(pProgramHeader, &pBitcode, &bitcodeLength);
  492. VERIFY_SUCCEEDED(pLib->CreateBlobFromBlob(
  493. pProgram, pBitcode - (char *)pProgram->GetBufferPointer(), bitcodeLength,
  494. &pProgramPdb));
  495. // Disassemble the program with debug information.
  496. {
  497. CComPtr<IDxcBlobEncoding> pDbgDisassembly;
  498. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgramPdb, &pDbgDisassembly));
  499. std::string disText = BlobToUtf8(pDbgDisassembly);
  500. CA2W disTextW(disText.c_str(), CP_UTF8);
  501. //WEX::Logging::Log::Comment(disTextW);
  502. }
  503. // Create a short text dump of debug information.
  504. VERIFY_SUCCEEDED(pLib->CreateStreamFromBlobReadOnly(pProgramPdb, &pProgramStream));
  505. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcDiaDataSource, &pDiaSource));
  506. VERIFY_SUCCEEDED(pDiaSource->loadDataFromIStream(pProgramStream));
  507. *ppDiaSource = pDiaSource.Detach();
  508. return S_OK;
  509. }
  510. #endif // _WIN32 - exclude dia stuff
  511. };
  512. // Useful for debugging.
  513. #if SUPPORT_FXC_PDB
  514. #include <d3dcompiler.h>
  515. #pragma comment(lib, "d3dcompiler.lib")
  516. HRESULT GetBlobPdb(IDxcBlob *pBlob, IDxcBlob **ppDebugInfo) {
  517. return D3DGetBlobPart(pBlob->GetBufferPointer(), pBlob->GetBufferSize(),
  518. D3D_BLOB_PDB, 0, (ID3DBlob **)ppDebugInfo);
  519. }
  520. std::string FourCCStr(uint32_t val) {
  521. std::stringstream o;
  522. char c[5];
  523. c[0] = val & 0xFF;
  524. c[1] = (val & 0xFF00) >> 8;
  525. c[2] = (val & 0xFF0000) >> 16;
  526. c[3] = (val & 0xFF000000) >> 24;
  527. c[4] = '\0';
  528. o << c << " (" << std::hex << val << std::dec << ")";
  529. return o.str();
  530. }
  531. std::string DumpParts(IDxcBlob *pBlob) {
  532. std::stringstream o;
  533. hlsl::DxilContainerHeader *pContainer = (hlsl::DxilContainerHeader *)pBlob->GetBufferPointer();
  534. o << "Container:" << std::endl
  535. << " Size: " << pContainer->ContainerSizeInBytes << std::endl
  536. << " FourCC: " << FourCCStr(pContainer->HeaderFourCC) << std::endl
  537. << " Part count: " << pContainer->PartCount << std::endl;
  538. for (uint32_t i = 0; i < pContainer->PartCount; ++i) {
  539. hlsl::DxilPartHeader *pPart = hlsl::GetDxilContainerPart(pContainer, i);
  540. o << "Part " << i << std::endl
  541. << " FourCC: " << FourCCStr(pPart->PartFourCC) << std::endl
  542. << " Size: " << pPart->PartSize << std::endl;
  543. }
  544. return o.str();
  545. }
  546. HRESULT CreateDiaSourceFromDxbcBlob(IDxcLibrary *pLib, IDxcBlob *pDxbcBlob,
  547. IDiaDataSource **ppDiaSource) {
  548. HRESULT hr = S_OK;
  549. CComPtr<IDxcBlob> pdbBlob;
  550. CComPtr<IStream> pPdbStream;
  551. CComPtr<IDiaDataSource> pDiaSource;
  552. IFR(GetBlobPdb(pDxbcBlob, &pdbBlob));
  553. IFR(pLib->CreateStreamFromBlobReadOnly(pdbBlob, &pPdbStream));
  554. IFR(CoCreateInstance(CLSID_DiaSource, NULL, CLSCTX_INPROC_SERVER,
  555. __uuidof(IDiaDataSource), (void **)&pDiaSource));
  556. IFR(pDiaSource->loadDataFromIStream(pPdbStream));
  557. *ppDiaSource = pDiaSource.Detach();
  558. return hr;
  559. }
  560. #endif
  561. bool CompilerTest::InitSupport() {
  562. if (!m_dllSupport.IsEnabled()) {
  563. VERIFY_SUCCEEDED(m_dllSupport.Initialize());
  564. m_ver.Initialize(m_dllSupport);
  565. }
  566. return true;
  567. }
  568. TEST_F(CompilerTest, CompileWhenDefinesThenApplied) {
  569. CComPtr<IDxcCompiler> pCompiler;
  570. CComPtr<IDxcOperationResult> pResult;
  571. CComPtr<IDxcBlobEncoding> pSource;
  572. DxcDefine defines[] = {{L"F4", L"float4"}};
  573. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  574. CreateBlobFromText("F4 main() : SV_Target { return 0; }", &pSource);
  575. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  576. L"ps_6_0", nullptr, 0, defines,
  577. _countof(defines), nullptr, &pResult));
  578. }
  579. TEST_F(CompilerTest, CompileWhenDefinesManyThenApplied) {
  580. CComPtr<IDxcCompiler> pCompiler;
  581. CComPtr<IDxcOperationResult> pResult;
  582. CComPtr<IDxcBlobEncoding> pSource;
  583. LPCWSTR args[] = {L"/DVAL1=1", L"/DVAL2=2", L"/DVAL3=3", L"/DVAL4=2",
  584. L"/DVAL5=4", L"/DNVAL1", L"/DNVAL2", L"/DNVAL3",
  585. L"/DNVAL4", L"/DNVAL5", L"/DCVAL1=1", L"/DCVAL2=2",
  586. L"/DCVAL3=3", L"/DCVAL4=2", L"/DCVAL5=4", L"/DCVALNONE="};
  587. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  588. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  589. "#ifndef VAL1\r\n"
  590. "#error VAL1 not defined\r\n"
  591. "#endif\r\n"
  592. "#ifndef NVAL5\r\n"
  593. "#error NVAL5 not defined\r\n"
  594. "#endif\r\n"
  595. "#ifndef CVALNONE\r\n"
  596. "#error CVALNONE not defined\r\n"
  597. "#endif\r\n"
  598. "return 0; }",
  599. &pSource);
  600. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  601. L"ps_6_0", args, _countof(args), nullptr,
  602. 0, nullptr, &pResult));
  603. HRESULT compileStatus;
  604. VERIFY_SUCCEEDED(pResult->GetStatus(&compileStatus));
  605. if (FAILED(compileStatus)) {
  606. CComPtr<IDxcBlobEncoding> pErrors;
  607. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  608. OutputDebugStringA((LPCSTR)pErrors->GetBufferPointer());
  609. }
  610. VERIFY_SUCCEEDED(compileStatus);
  611. }
  612. TEST_F(CompilerTest, CompileWhenEmptyThenFails) {
  613. CComPtr<IDxcCompiler> pCompiler;
  614. CComPtr<IDxcOperationResult> pResult;
  615. CComPtr<IDxcBlobEncoding> pSource;
  616. CComPtr<IDxcBlobEncoding> pSourceBad;
  617. LPCWSTR pProfile = L"ps_6_0";
  618. LPCWSTR pEntryPoint = L"main";
  619. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  620. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  621. CreateBlobFromText("float4 main() : SV_Target { return undef; }", &pSourceBad);
  622. // correct version
  623. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  624. pProfile, nullptr, 0, nullptr, 0, nullptr,
  625. &pResult));
  626. pResult.Release();
  627. // correct version with compilation errors
  628. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
  629. pProfile, nullptr, 0, nullptr, 0, nullptr,
  630. &pResult));
  631. pResult.Release();
  632. // null source
  633. VERIFY_FAILED(pCompiler->Compile(nullptr, L"source.hlsl", pEntryPoint, pProfile,
  634. nullptr, 0, nullptr, 0, nullptr, &pResult));
  635. // null profile
  636. VERIFY_FAILED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
  637. nullptr, nullptr, 0, nullptr, 0, nullptr,
  638. &pResult));
  639. // null source name succeeds
  640. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, nullptr, pEntryPoint, pProfile,
  641. nullptr, 0, nullptr, 0, nullptr, &pResult));
  642. pResult.Release();
  643. // empty source name (as opposed to null) also suceeds
  644. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"", pEntryPoint, pProfile,
  645. nullptr, 0, nullptr, 0, nullptr,
  646. &pResult));
  647. pResult.Release();
  648. // null result
  649. VERIFY_FAILED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  650. pProfile, nullptr, 0, nullptr, 0, nullptr,
  651. nullptr));
  652. }
  653. TEST_F(CompilerTest, CompileWhenIncorrectThenFails) {
  654. CComPtr<IDxcCompiler> pCompiler;
  655. CComPtr<IDxcOperationResult> pResult;
  656. CComPtr<IDxcBlobEncoding> pSource;
  657. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  658. CreateBlobFromText("float4_undefined main() : SV_Target { return 0; }",
  659. &pSource);
  660. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main", L"ps_6_0",
  661. nullptr, 0, nullptr, 0, nullptr,
  662. &pResult));
  663. HRESULT result;
  664. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  665. VERIFY_FAILED(result);
  666. CComPtr<IDxcBlobEncoding> pErrorBuffer;
  667. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrorBuffer));
  668. std::string errorString(BlobToUtf8(pErrorBuffer));
  669. VERIFY_ARE_NOT_EQUAL(0U, errorString.size());
  670. // Useful for examining actual error message:
  671. // CA2W errorStringW(errorString.c_str(), CP_UTF8);
  672. // WEX::Logging::Log::Comment(errorStringW.m_psz);
  673. }
  674. TEST_F(CompilerTest, CompileWhenWorksThenDisassembleWorks) {
  675. CComPtr<IDxcCompiler> pCompiler;
  676. CComPtr<IDxcOperationResult> pResult;
  677. CComPtr<IDxcBlobEncoding> pSource;
  678. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  679. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  680. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  681. L"ps_6_0", nullptr, 0, nullptr, 0,
  682. nullptr, &pResult));
  683. HRESULT result;
  684. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  685. VERIFY_SUCCEEDED(result);
  686. CComPtr<IDxcBlob> pProgram;
  687. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  688. CComPtr<IDxcBlobEncoding> pDisassembleBlob;
  689. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembleBlob));
  690. std::string disassembleString(BlobToUtf8(pDisassembleBlob));
  691. VERIFY_ARE_NOT_EQUAL(0U, disassembleString.size());
  692. // Useful for examining disassembly:
  693. // CA2W disassembleStringW(disassembleString.c_str(), CP_UTF8);
  694. // WEX::Logging::Log::Comment(disassembleStringW.m_psz);
  695. }
  696. #ifdef _WIN32 // Container builder unsupported
  697. TEST_F(CompilerTest, CompileWhenDebugWorksThenStripDebug) {
  698. CComPtr<IDxcCompiler> pCompiler;
  699. CComPtr<IDxcOperationResult> pResult;
  700. CComPtr<IDxcBlobEncoding> pSource;
  701. CComPtr<IDxcBlob> pProgram;
  702. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  703. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target {\r\n"
  704. " float4 local = abs(pos);\r\n"
  705. " return local;\r\n"
  706. "}",
  707. &pSource);
  708. LPCWSTR args[] = {L"/Zi", L"/Qembed_debug"};
  709. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  710. L"ps_6_0", args, _countof(args), nullptr,
  711. 0, nullptr, &pResult));
  712. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  713. // Check if it contains debug blob
  714. hlsl::DxilContainerHeader *pHeader =
  715. hlsl::IsDxilContainerLike(pProgram->GetBufferPointer(), pProgram->GetBufferSize());
  716. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pHeader, pProgram->GetBufferSize()));
  717. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  718. pHeader, hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL);
  719. VERIFY_IS_NOT_NULL(pPartHeader);
  720. // Check debug info part does not exist after strip debug info
  721. CComPtr<IDxcBlob> pNewProgram;
  722. CComPtr<IDxcContainerBuilder> pBuilder;
  723. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  724. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  725. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL));
  726. pResult.Release();
  727. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  728. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  729. pHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  730. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pHeader, pNewProgram->GetBufferSize()));
  731. pPartHeader = hlsl::GetDxilPartByType(
  732. pHeader, hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL);
  733. VERIFY_IS_NULL(pPartHeader);
  734. }
  735. TEST_F(CompilerTest, CompileWhenWorksThenAddRemovePrivate) {
  736. CComPtr<IDxcCompiler> pCompiler;
  737. CComPtr<IDxcOperationResult> pResult;
  738. CComPtr<IDxcBlobEncoding> pSource;
  739. CComPtr<IDxcBlob> pProgram;
  740. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  741. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  742. " return 0;\r\n"
  743. "}",
  744. &pSource);
  745. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  746. L"ps_6_0", nullptr, 0, nullptr, 0,
  747. nullptr, &pResult));
  748. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  749. // Append private data blob
  750. CComPtr<IDxcContainerBuilder> pBuilder;
  751. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  752. std::string privateTxt("private data");
  753. CComPtr<IDxcBlobEncoding> pPrivate;
  754. CreateBlobFromText(privateTxt.c_str(), &pPrivate);
  755. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  756. VERIFY_SUCCEEDED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_PrivateData, pPrivate));
  757. pResult.Release();
  758. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  759. CComPtr<IDxcBlob> pNewProgram;
  760. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  761. hlsl::DxilContainerHeader *pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  762. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  763. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  764. pContainerHeader, hlsl::DxilFourCC::DFCC_PrivateData);
  765. VERIFY_IS_NOT_NULL(pPartHeader);
  766. // compare data
  767. std::string privatePart((const char *)(pPartHeader + 1), privateTxt.size());
  768. VERIFY_IS_TRUE(strcmp(privatePart.c_str(), privateTxt.c_str()) == 0);
  769. // Remove private data blob
  770. pBuilder.Release();
  771. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  772. VERIFY_SUCCEEDED(pBuilder->Load(pNewProgram));
  773. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_PrivateData));
  774. pResult.Release();
  775. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  776. pNewProgram.Release();
  777. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  778. pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  779. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  780. pPartHeader = hlsl::GetDxilPartByType(
  781. pContainerHeader, hlsl::DxilFourCC::DFCC_PrivateData);
  782. VERIFY_IS_NULL(pPartHeader);
  783. }
  784. TEST_F(CompilerTest, CompileThenAddCustomDebugName) {
  785. // container builders prior to 1.3 did not support adding debug name parts
  786. if (m_ver.SkipDxilVersion(1, 3)) return;
  787. CComPtr<IDxcCompiler> pCompiler;
  788. CComPtr<IDxcOperationResult> pResult;
  789. CComPtr<IDxcBlobEncoding> pSource;
  790. CComPtr<IDxcBlob> pProgram;
  791. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  792. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  793. " return 0;\r\n"
  794. "}",
  795. &pSource);
  796. LPCWSTR args[] = { L"/Zi", L"/Qembed_debug", L"/Zss" };
  797. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  798. L"ps_6_0", args, _countof(args), nullptr, 0,
  799. nullptr, &pResult));
  800. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  801. // Append private data blob
  802. CComPtr<IDxcContainerBuilder> pBuilder;
  803. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  804. const char pNewName[] = "MyOwnUniqueName.lld";
  805. //include null terminator:
  806. size_t nameBlobPartSize = sizeof(hlsl::DxilShaderDebugName) + _countof(pNewName);
  807. // round up to four-byte size:
  808. size_t allocatedSize = (nameBlobPartSize + 3) & ~3;
  809. auto pNameBlobContent = reinterpret_cast<hlsl::DxilShaderDebugName*>(malloc(allocatedSize));
  810. ZeroMemory(pNameBlobContent, allocatedSize); //just to make sure trailing nulls are nulls.
  811. pNameBlobContent->Flags = 0;
  812. pNameBlobContent->NameLength = _countof(pNewName) - 1; //this is not supposed to include null terminator
  813. memcpy(pNameBlobContent + 1, pNewName, _countof(pNewName));
  814. CComPtr<IDxcBlobEncoding> pDebugName;
  815. CreateBlobPinned(pNameBlobContent, allocatedSize, CP_UTF8, &pDebugName);
  816. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  817. // should fail since it already exists:
  818. VERIFY_FAILED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_ShaderDebugName, pDebugName));
  819. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugName));
  820. VERIFY_SUCCEEDED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_ShaderDebugName, pDebugName));
  821. pResult.Release();
  822. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  823. CComPtr<IDxcBlob> pNewProgram;
  824. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  825. hlsl::DxilContainerHeader *pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  826. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  827. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  828. pContainerHeader, hlsl::DxilFourCC::DFCC_ShaderDebugName);
  829. VERIFY_IS_NOT_NULL(pPartHeader);
  830. // compare data
  831. VERIFY_IS_TRUE(memcmp(pPartHeader + 1, pNameBlobContent, allocatedSize) == 0);
  832. free(pNameBlobContent);
  833. // Remove private data blob
  834. pBuilder.Release();
  835. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  836. VERIFY_SUCCEEDED(pBuilder->Load(pNewProgram));
  837. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugName));
  838. pResult.Release();
  839. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  840. pNewProgram.Release();
  841. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  842. pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  843. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  844. pPartHeader = hlsl::GetDxilPartByType(
  845. pContainerHeader, hlsl::DxilFourCC::DFCC_ShaderDebugName);
  846. VERIFY_IS_NULL(pPartHeader);
  847. }
  848. static void VerifyPdbUtil(dxc::DxcDllSupport &dllSupport,
  849. IDxcBlob *pBlob, IDxcPdbUtils *pPdbUtils,
  850. const WCHAR *pMainFileName,
  851. llvm::ArrayRef<std::pair<const WCHAR *, const WCHAR *> > ExpectedArgs,
  852. llvm::ArrayRef<std::pair<const WCHAR *, const WCHAR *> > ExpectedFlags,
  853. llvm::ArrayRef<const WCHAR *> ExpectedDefines,
  854. IDxcCompiler *pCompiler,
  855. bool HasVersion,
  856. bool IsFullPDB,
  857. bool HasHashAndPdbName,
  858. bool TestReflection,
  859. const std::string &MainSource,
  860. const std::string &IncludedFile)
  861. {
  862. VERIFY_SUCCEEDED(pPdbUtils->Load(pBlob));
  863. // Compiler version comparison
  864. if (!HasVersion) {
  865. CComPtr<IDxcVersionInfo> pVersion;
  866. VERIFY_FAILED(pPdbUtils->GetVersionInfo(&pVersion));
  867. }
  868. else {
  869. CComPtr<IDxcVersionInfo> pVersion;
  870. VERIFY_SUCCEEDED(pPdbUtils->GetVersionInfo(&pVersion));
  871. CComPtr<IDxcVersionInfo2> pVersion2;
  872. VERIFY_IS_NOT_NULL(pVersion);
  873. VERIFY_SUCCEEDED(pVersion.QueryInterface(&pVersion2));
  874. CComPtr<IDxcVersionInfo3> pVersion3;
  875. VERIFY_SUCCEEDED(pVersion.QueryInterface(&pVersion3));
  876. CComPtr<IDxcVersionInfo> pCompilerVersion;
  877. pCompiler->QueryInterface(&pCompilerVersion);
  878. if (pCompilerVersion) {
  879. UINT32 uCompilerMajor = 0;
  880. UINT32 uCompilerMinor = 0;
  881. UINT32 uCompilerFlags = 0;
  882. VERIFY_SUCCEEDED(pCompilerVersion->GetVersion(&uCompilerMajor, &uCompilerMinor));
  883. VERIFY_SUCCEEDED(pCompilerVersion->GetFlags(&uCompilerFlags));
  884. UINT32 uMajor = 0;
  885. UINT32 uMinor = 0;
  886. UINT32 uFlags = 0;
  887. VERIFY_SUCCEEDED(pVersion->GetVersion(&uMajor, &uMinor));
  888. VERIFY_SUCCEEDED(pVersion->GetFlags(&uFlags));
  889. VERIFY_ARE_EQUAL(uMajor, uCompilerMajor);
  890. VERIFY_ARE_EQUAL(uMinor, uCompilerMinor);
  891. VERIFY_ARE_EQUAL(uFlags, uCompilerFlags);
  892. // IDxcVersionInfo2
  893. UINT32 uCommitCount = 0;
  894. CComHeapPtr<char> CommitVersionHash;
  895. VERIFY_SUCCEEDED(pVersion2->GetCommitInfo(&uCommitCount, &CommitVersionHash));
  896. CComPtr<IDxcVersionInfo2> pCompilerVersion2;
  897. if (SUCCEEDED(pCompiler->QueryInterface(&pCompilerVersion2))) {
  898. UINT32 uCompilerCommitCount = 0;
  899. CComHeapPtr<char> CompilerCommitVersionHash;
  900. VERIFY_SUCCEEDED(pCompilerVersion2->GetCommitInfo(&uCompilerCommitCount, &CompilerCommitVersionHash));
  901. VERIFY_IS_TRUE(0 == strcmp(CommitVersionHash, CompilerCommitVersionHash));
  902. VERIFY_ARE_EQUAL(uCommitCount, uCompilerCommitCount);
  903. }
  904. // IDxcVersionInfo3
  905. CComHeapPtr<char> VersionString;
  906. VERIFY_SUCCEEDED(pVersion3->GetCustomVersionString(&VersionString));
  907. VERIFY_IS_TRUE(VersionString && strlen(VersionString) != 0);
  908. {
  909. CComPtr<IDxcVersionInfo3> pCompilerVersion3;
  910. VERIFY_SUCCEEDED(pCompiler->QueryInterface(&pCompilerVersion3));
  911. CComHeapPtr<char> CompilerVersionString;
  912. VERIFY_SUCCEEDED(pCompilerVersion3->GetCustomVersionString(&CompilerVersionString));
  913. VERIFY_IS_TRUE(0 == strcmp(CompilerVersionString, VersionString));
  914. }
  915. }
  916. }
  917. // Target profile
  918. {
  919. CComBSTR str;
  920. VERIFY_SUCCEEDED(pPdbUtils->GetTargetProfile(&str));
  921. VERIFY_ARE_EQUAL(str, L"ps_6_0");
  922. }
  923. // Entry point
  924. {
  925. CComBSTR str;
  926. VERIFY_SUCCEEDED(pPdbUtils->GetEntryPoint(&str));
  927. VERIFY_ARE_EQUAL(str, L"PSMain");
  928. }
  929. // PDB file path
  930. if (HasHashAndPdbName) {
  931. CComBSTR pName;
  932. VERIFY_SUCCEEDED(pPdbUtils->GetName(&pName));
  933. std::wstring suffix = L".pdb";
  934. VERIFY_IS_TRUE(pName.Length() >= suffix.size());
  935. VERIFY_IS_TRUE(
  936. 0 == std::memcmp(suffix.c_str(), &pName[pName.Length() - suffix.size()], suffix.size()));
  937. }
  938. // Main file name
  939. {
  940. CComBSTR pMainFileName;
  941. VERIFY_SUCCEEDED(pPdbUtils->GetMainFileName(&pMainFileName));
  942. VERIFY_ARE_EQUAL(pMainFileName, pMainFileName);
  943. }
  944. // There is hash and hash is not empty
  945. if (HasHashAndPdbName) {
  946. CComPtr<IDxcBlob> pHash;
  947. VERIFY_SUCCEEDED(pPdbUtils->GetHash(&pHash));
  948. hlsl::DxilShaderHash EmptyHash = {};
  949. VERIFY_ARE_EQUAL(pHash->GetBufferSize(), sizeof(EmptyHash));
  950. VERIFY_IS_FALSE(0 == std::memcmp(pHash->GetBufferPointer(), &EmptyHash, sizeof(EmptyHash)));
  951. }
  952. // Source files
  953. {
  954. UINT32 uSourceCount = 0;
  955. VERIFY_SUCCEEDED(pPdbUtils->GetSourceCount(&uSourceCount));
  956. for (UINT32 i = 0; i < uSourceCount; i++) {
  957. CComBSTR pFileName;
  958. CComPtr<IDxcBlobEncoding> pFileContent;
  959. VERIFY_SUCCEEDED(pPdbUtils->GetSourceName(i, &pFileName));
  960. VERIFY_SUCCEEDED(pPdbUtils->GetSource(i, &pFileContent));
  961. if (0 == wcscmp(pFileName, pMainFileName)) {
  962. VERIFY_IS_TRUE(pFileContent->GetBufferSize() == MainSource.size());
  963. VERIFY_IS_TRUE(0 == std::memcmp(pFileContent->GetBufferPointer(), MainSource.data(), MainSource.size()));
  964. }
  965. else {
  966. VERIFY_IS_TRUE(0 == std::memcmp(pFileContent->GetBufferPointer(), IncludedFile.data(), IncludedFile.size()));
  967. }
  968. }
  969. }
  970. // Defines
  971. {
  972. UINT32 uDefineCount = 0;
  973. std::map<std::wstring, int> tally;
  974. VERIFY_SUCCEEDED(pPdbUtils->GetDefineCount(&uDefineCount));
  975. VERIFY_IS_TRUE(uDefineCount == 2);
  976. for (UINT32 i = 0; i < uDefineCount; i++) {
  977. CComBSTR def;
  978. VERIFY_SUCCEEDED(pPdbUtils->GetDefine(i, &def));
  979. tally[std::wstring(def)]++;
  980. }
  981. auto Expected = ExpectedDefines;
  982. for (size_t i = 0; i < Expected.size(); i++) {
  983. auto it = tally.find(Expected[i]);
  984. VERIFY_IS_TRUE(it != tally.end() && it->second == 1);
  985. tally.erase(it);
  986. }
  987. VERIFY_IS_TRUE(tally.size() == 0);
  988. }
  989. // Arg pairs
  990. {
  991. std::vector<std::pair< std::wstring, std::wstring > > ArgPairs;
  992. UINT32 uCount = 0;
  993. VERIFY_SUCCEEDED(pPdbUtils->GetArgPairCount(&uCount));
  994. for (unsigned i = 0; i < uCount; i++) {
  995. CComBSTR pName;
  996. CComBSTR pValue;
  997. VERIFY_SUCCEEDED(pPdbUtils->GetArgPair(i, &pName, &pValue));
  998. VERIFY_IS_TRUE(pName || pValue);
  999. std::pair<std::wstring, std::wstring> NewPair;
  1000. if (pName)
  1001. NewPair.first = std::wstring(pName);
  1002. if (pValue)
  1003. NewPair.second = std::wstring(pValue);
  1004. ArgPairs.push_back(std::move(NewPair));
  1005. }
  1006. for (size_t i = 0; i < ExpectedArgs.size(); i++) {
  1007. auto ExpectedPair = ExpectedArgs[i];
  1008. bool Found = false;
  1009. for (size_t j = 0; j < ArgPairs.size(); j++) {
  1010. auto Pair = ArgPairs[j];
  1011. if ((!ExpectedPair.first || Pair.first == ExpectedPair.first) &&
  1012. (!ExpectedPair.second || Pair.second == ExpectedPair.second))
  1013. {
  1014. Found = true;
  1015. break;
  1016. }
  1017. }
  1018. VERIFY_SUCCEEDED(Found);
  1019. }
  1020. }
  1021. auto TestArgumentPair = [](llvm::ArrayRef<std::wstring> Args, llvm::ArrayRef<std::pair<const WCHAR *, const WCHAR *> > Expected) {
  1022. for (size_t i = 0; i < Expected.size(); i++) {
  1023. auto Pair = Expected[i];
  1024. bool found = false;
  1025. for (size_t j = 0; j < Args.size(); j++) {
  1026. if (!Pair.second && Args[j] == Pair.first) {
  1027. found = true;
  1028. break;
  1029. }
  1030. else if (!Pair.first && Args[j] == Pair.second) {
  1031. found = true;
  1032. break;
  1033. }
  1034. else if (Pair.first && Pair.second &&
  1035. Args[j] == Pair.first &&
  1036. j+1 < Args.size() &&
  1037. Args[j+1] == Pair.second)
  1038. {
  1039. found = true;
  1040. break;
  1041. }
  1042. }
  1043. VERIFY_IS_TRUE(found);
  1044. }
  1045. };
  1046. // Flags
  1047. {
  1048. UINT32 uCount = 0;
  1049. std::vector<std::wstring> Flags;
  1050. VERIFY_SUCCEEDED(pPdbUtils->GetFlagCount(&uCount));
  1051. VERIFY_IS_TRUE(uCount == ExpectedFlags.size());
  1052. for (UINT32 i = 0; i < uCount; i++) {
  1053. CComBSTR item;
  1054. VERIFY_SUCCEEDED(pPdbUtils->GetFlag(i, &item));
  1055. Flags.push_back(std::wstring(item));
  1056. }
  1057. TestArgumentPair(Flags, ExpectedFlags);
  1058. }
  1059. // Args
  1060. {
  1061. UINT32 uCount = 0;
  1062. std::vector<std::wstring> Args;
  1063. VERIFY_SUCCEEDED(pPdbUtils->GetArgCount(&uCount));
  1064. for (UINT32 i = 0; i < uCount; i++) {
  1065. CComBSTR item;
  1066. VERIFY_SUCCEEDED(pPdbUtils->GetArg(i, &item));
  1067. Args.push_back( std::wstring(item) );
  1068. }
  1069. TestArgumentPair(Args, ExpectedArgs);
  1070. }
  1071. // Shader reflection
  1072. if (TestReflection) {
  1073. CComPtr<IDxcUtils> pUtils;
  1074. VERIFY_SUCCEEDED(dllSupport.CreateInstance(CLSID_DxcUtils, &pUtils));
  1075. DxcBuffer buf = {};
  1076. buf.Ptr = pBlob->GetBufferPointer();
  1077. buf.Size = pBlob->GetBufferSize();
  1078. buf.Encoding = CP_ACP;
  1079. CComPtr<ID3D12ShaderReflection> pRefl;
  1080. VERIFY_SUCCEEDED(pUtils->CreateReflection(&buf, IID_PPV_ARGS(&pRefl)));
  1081. D3D12_SHADER_DESC desc = {};
  1082. VERIFY_SUCCEEDED(pRefl->GetDesc(&desc));
  1083. VERIFY_ARE_EQUAL(desc.ConstantBuffers, 1);
  1084. ID3D12ShaderReflectionConstantBuffer *pCB = pRefl->GetConstantBufferByIndex(0);
  1085. D3D12_SHADER_BUFFER_DESC cbDesc = {};
  1086. VERIFY_SUCCEEDED(pCB->GetDesc(&cbDesc));
  1087. VERIFY_IS_TRUE(0 == strcmp(cbDesc.Name, "MyCbuffer"));
  1088. VERIFY_ARE_EQUAL(cbDesc.Variables, 1);
  1089. ID3D12ShaderReflectionVariable *pVar = pCB->GetVariableByIndex(0);
  1090. D3D12_SHADER_VARIABLE_DESC varDesc = {};
  1091. VERIFY_SUCCEEDED(pVar->GetDesc(&varDesc));
  1092. VERIFY_ARE_EQUAL(varDesc.uFlags, D3D_SVF_USED);
  1093. VERIFY_IS_TRUE(0 == strcmp(varDesc.Name, "my_cbuf_foo"));
  1094. VERIFY_ARE_EQUAL(varDesc.Size, sizeof(float) * 4);
  1095. }
  1096. // Make the pix debug info
  1097. if (IsFullPDB) {
  1098. VERIFY_IS_TRUE(pPdbUtils->IsFullPDB());
  1099. CComPtr<IDxcBlob> pPDBBlob;
  1100. VERIFY_SUCCEEDED(pPdbUtils->GetFullPDB(&pPDBBlob));
  1101. CComPtr<IDxcPixDxilDebugInfoFactory> pFactory;
  1102. VERIFY_SUCCEEDED(pPdbUtils->QueryInterface(&pFactory));
  1103. CComPtr<IDxcPixCompilationInfo> pCompInfo;
  1104. VERIFY_ARE_EQUAL(E_NOTIMPL, pFactory->NewDxcPixCompilationInfo(&pCompInfo));
  1105. CComPtr<IDxcPixDxilDebugInfo> pDebugInfo;
  1106. VERIFY_SUCCEEDED(pFactory->NewDxcPixDxilDebugInfo(&pDebugInfo));
  1107. VERIFY_ARE_NOT_EQUAL(pDebugInfo, nullptr);
  1108. }
  1109. else {
  1110. VERIFY_IS_FALSE(pPdbUtils->IsFullPDB());
  1111. CComPtr<IDxcBlob> pFullPdb;
  1112. VERIFY_SUCCEEDED(pPdbUtils->GetFullPDB(&pFullPdb));
  1113. auto ReplaceDebugFlagPair = [](const std::vector<std::pair<const WCHAR *, const WCHAR *> > &List) -> std::vector<std::pair<const WCHAR *, const WCHAR *> > {
  1114. std::vector<std::pair<const WCHAR *, const WCHAR *> > ret;
  1115. for (unsigned i = 0; i < List.size(); i++) {
  1116. if (!wcscmp(List[i].first, L"/Zs") || !wcscmp(List[i].first, L"-Zs"))
  1117. ret.push_back(std::pair<const WCHAR *, const WCHAR *>(L"-Zi", nullptr));
  1118. else
  1119. ret.push_back(List[i]);
  1120. }
  1121. return ret;
  1122. };
  1123. auto NewExpectedFlags = ReplaceDebugFlagPair(ExpectedFlags);
  1124. auto NewExpectedArgs = ReplaceDebugFlagPair(ExpectedArgs);
  1125. VerifyPdbUtil(dllSupport, pFullPdb, pPdbUtils,
  1126. pMainFileName,
  1127. NewExpectedArgs, NewExpectedFlags, ExpectedDefines,
  1128. pCompiler, HasVersion, /*IsFullPDB*/true,
  1129. /*TestReflection*/true,
  1130. HasHashAndPdbName, MainSource, IncludedFile);
  1131. }
  1132. // Now, test that dia interface doesn't crash (even if it fails).
  1133. {
  1134. CComPtr<IDiaDataSource> pDataSource;
  1135. VERIFY_SUCCEEDED(dllSupport.CreateInstance(CLSID_DxcDiaDataSource, &pDataSource));
  1136. CComPtr<IDxcLibrary> pLib;
  1137. VERIFY_SUCCEEDED(dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  1138. CComPtr<IStream> pStream;
  1139. VERIFY_SUCCEEDED(pLib->CreateStreamFromBlobReadOnly(pBlob, &pStream));
  1140. if (SUCCEEDED(pDataSource->loadDataFromIStream(pStream))) {
  1141. CComPtr<IDiaSession> pSession;
  1142. if (SUCCEEDED(pDataSource->openSession(&pSession))) {
  1143. CComPtr<IDxcPixDxilDebugInfoFactory> pFactory;
  1144. VERIFY_SUCCEEDED(pSession->QueryInterface(&pFactory));
  1145. CComPtr<IDxcPixCompilationInfo> pCompilationInfo;
  1146. if (SUCCEEDED(pFactory->NewDxcPixCompilationInfo(&pCompilationInfo))) {
  1147. CComBSTR args;
  1148. CComBSTR defs;
  1149. CComBSTR mainName;
  1150. CComBSTR entryPoint;
  1151. CComBSTR entryPointFile;
  1152. CComBSTR target;
  1153. pCompilationInfo->GetArguments(&args);
  1154. pCompilationInfo->GetMacroDefinitions(&defs);
  1155. pCompilationInfo->GetEntryPoint(&entryPoint);
  1156. pCompilationInfo->GetEntryPointFile(&entryPointFile);
  1157. pCompilationInfo->GetHlslTarget(&target);
  1158. for (DWORD i = 0;;i++) {
  1159. CComBSTR sourceName;
  1160. CComBSTR sourceContent;
  1161. if (FAILED(pCompilationInfo->GetSourceFile(i, &sourceName, &sourceContent)))
  1162. break;
  1163. }
  1164. }
  1165. CComPtr<IDxcPixDxilDebugInfo> pDebugInfo;
  1166. pFactory->NewDxcPixDxilDebugInfo(&pDebugInfo);
  1167. }
  1168. }
  1169. }
  1170. }
  1171. #ifdef _WIN32
  1172. TEST_F(CompilerTest, CompileThenTestPdbUtilsStripped) {
  1173. CComPtr<TestIncludeHandler> pInclude;
  1174. CComPtr<IDxcCompiler> pCompiler;
  1175. CComPtr<IDxcBlobEncoding> pSource;
  1176. CComPtr<IDxcOperationResult> pOperationResult;
  1177. std::string main_source = "#include \"helper.h\"\r\n"
  1178. "float4 PSMain() : SV_Target { return ZERO; }";
  1179. std::string included_File = "#define ZERO 0";
  1180. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1181. CreateBlobFromText(main_source.c_str(), &pSource);
  1182. pInclude = new TestIncludeHandler(m_dllSupport);
  1183. pInclude->CallResults.emplace_back(included_File.c_str());
  1184. const WCHAR *pArgs[] = { L"/Zi", L"/Od", L"-flegacy-macro-expansion", L"-Qstrip_debug", L"/DTHIS_IS_A_DEFINE=HELLO" };
  1185. const DxcDefine pDefines[] = { L"THIS_IS_ANOTHER_DEFINE", L"1" };
  1186. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"PSMain",
  1187. L"ps_6_0", pArgs, _countof(pArgs), pDefines, _countof(pDefines), pInclude, &pOperationResult));
  1188. CComPtr<IDxcBlob> pCompiledBlob;
  1189. VERIFY_SUCCEEDED(pOperationResult->GetResult(&pCompiledBlob));
  1190. CComPtr<IDxcPdbUtils> pPdbUtils;
  1191. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcPdbUtils, &pPdbUtils));
  1192. VERIFY_SUCCEEDED(pPdbUtils->Load(pCompiledBlob));
  1193. // PDB file path
  1194. {
  1195. CComBSTR pName;
  1196. VERIFY_SUCCEEDED(pPdbUtils->GetName(&pName));
  1197. std::wstring suffix = L".pdb";
  1198. VERIFY_IS_TRUE(pName.Length() >= suffix.size());
  1199. VERIFY_IS_TRUE(
  1200. 0 == std::memcmp(suffix.c_str(), &pName[pName.Length() - suffix.size()], suffix.size()));
  1201. }
  1202. // There is hash and hash is not empty
  1203. {
  1204. CComPtr<IDxcBlob> pHash;
  1205. VERIFY_SUCCEEDED(pPdbUtils->GetHash(&pHash));
  1206. hlsl::DxilShaderHash EmptyHash = {};
  1207. VERIFY_ARE_EQUAL(pHash->GetBufferSize(), sizeof(EmptyHash));
  1208. VERIFY_IS_FALSE(0 == std::memcmp(pHash->GetBufferPointer(), &EmptyHash, sizeof(EmptyHash)));
  1209. }
  1210. {
  1211. VERIFY_IS_FALSE(pPdbUtils->IsFullPDB());
  1212. UINT32 uSourceCount = 0;
  1213. VERIFY_SUCCEEDED(pPdbUtils->GetSourceCount(&uSourceCount));
  1214. VERIFY_ARE_EQUAL(uSourceCount, 0);
  1215. }
  1216. }
  1217. void CompilerTest::TestPdbUtils(bool bSlim, bool bSourceInDebugModule, bool bStrip) {
  1218. CComPtr<TestIncludeHandler> pInclude;
  1219. CComPtr<IDxcCompiler> pCompiler;
  1220. CComPtr<IDxcBlobEncoding> pSource;
  1221. CComPtr<IDxcOperationResult> pOperationResult;
  1222. std::string main_source = R"x(
  1223. #include "helper.h"
  1224. cbuffer MyCbuffer : register(b1) {
  1225. float4 my_cbuf_foo;
  1226. }
  1227. [RootSignature("CBV(b1)")]
  1228. float4 PSMain() : SV_Target {
  1229. return ZERO + my_cbuf_foo;
  1230. }
  1231. )x";
  1232. std::string included_File = "#define ZERO 0";
  1233. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1234. CreateBlobFromText(main_source.c_str(), &pSource);
  1235. pInclude = new TestIncludeHandler(m_dllSupport);
  1236. pInclude->CallResults.emplace_back(included_File.c_str());
  1237. std::vector<const WCHAR *> args;
  1238. std::vector<std::pair<const WCHAR *, const WCHAR *> > expectedArgs;
  1239. std::vector<std::pair<const WCHAR *, const WCHAR *> > expectedFlags;
  1240. std::vector<const WCHAR *> expectedDefines;
  1241. auto AddArg = [&args, &expectedFlags, &expectedArgs](const WCHAR *arg, const WCHAR *value, bool isDefine) {
  1242. args.push_back(arg);
  1243. if (value)
  1244. args.push_back(value);
  1245. std::pair<const WCHAR *, const WCHAR *> pair(arg, value);
  1246. expectedArgs.push_back(pair);
  1247. if (!isDefine) {
  1248. expectedFlags.push_back(pair);
  1249. }
  1250. };
  1251. AddArg(L"-Od", nullptr, false);
  1252. AddArg(L"-flegacy-macro-expansion", nullptr, false);
  1253. if (bStrip) {
  1254. AddArg(L"-Qstrip_debug", nullptr, false);
  1255. }
  1256. else {
  1257. AddArg(L"-Qembed_debug", nullptr, false);
  1258. }
  1259. if (bSourceInDebugModule) {
  1260. AddArg(L"-Qsource_in_debug_module", nullptr, false);
  1261. }
  1262. if (bSlim) {
  1263. AddArg(L"-Zs", nullptr, false);
  1264. }
  1265. else {
  1266. AddArg(L"-Zi", nullptr, false);
  1267. }
  1268. AddArg(L"-D", L"THIS_IS_A_DEFINE=HELLO", true);
  1269. const DxcDefine pDefines[] = { L"THIS_IS_ANOTHER_DEFINE", L"1" };
  1270. expectedDefines.push_back(L"THIS_IS_ANOTHER_DEFINE=1");
  1271. expectedDefines.push_back(L"THIS_IS_A_DEFINE=HELLO");
  1272. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"PSMain",
  1273. L"ps_6_0", args.data(), args.size(), pDefines, _countof(pDefines), pInclude, &pOperationResult));
  1274. HRESULT CompileStatus = S_OK;
  1275. VERIFY_SUCCEEDED(pOperationResult->GetStatus(&CompileStatus));
  1276. VERIFY_SUCCEEDED(CompileStatus);
  1277. CComPtr<IDxcBlob> pCompiledBlob;
  1278. VERIFY_SUCCEEDED(pOperationResult->GetResult(&pCompiledBlob));
  1279. CComPtr<IDxcResult> pResult;
  1280. VERIFY_SUCCEEDED(pOperationResult.QueryInterface(&pResult));
  1281. CComPtr<IDxcBlob> pPdbBlob;
  1282. VERIFY_SUCCEEDED(pResult->GetOutput(DXC_OUT_PDB, IID_PPV_ARGS(&pPdbBlob), nullptr));
  1283. CComPtr<IDxcPdbUtils> pPdbUtils;
  1284. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcPdbUtils, &pPdbUtils));
  1285. CComPtr<IDxcBlob> pProgramHeaderBlob;
  1286. if (bSourceInDebugModule) {
  1287. CComPtr<IDxcContainerReflection> pRef;
  1288. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcContainerReflection, &pRef));
  1289. VERIFY_SUCCEEDED(pRef->Load(pPdbBlob));
  1290. UINT32 uIndex = 0;
  1291. VERIFY_SUCCEEDED(pRef->FindFirstPartKind(hlsl::DFCC_ShaderDebugInfoDXIL, &uIndex));
  1292. VERIFY_SUCCEEDED(pRef->GetPartContent(uIndex, &pProgramHeaderBlob));
  1293. VerifyPdbUtil(m_dllSupport,
  1294. pProgramHeaderBlob, pPdbUtils,
  1295. L"source.hlsl",
  1296. expectedArgs, expectedFlags, expectedDefines,
  1297. pCompiler,
  1298. /*HasVersion*/ false,
  1299. /*IsFullPDB*/ true,
  1300. /*HasHashAndPdbName*/false,
  1301. /*TestReflection*/false, // Reflection creation interface doesn't support just the DxilProgramHeader.
  1302. main_source, included_File);
  1303. }
  1304. VerifyPdbUtil(m_dllSupport,
  1305. pPdbBlob, pPdbUtils,
  1306. L"source.hlsl",
  1307. expectedArgs, expectedFlags, expectedDefines,
  1308. pCompiler,
  1309. /*HasVersion*/ true,
  1310. /*IsFullPDB*/ !bSlim,
  1311. /*HasHashAndPdbName*/true,
  1312. /*TestReflection*/true,
  1313. main_source, included_File);
  1314. if (!bStrip) {
  1315. VerifyPdbUtil(m_dllSupport,
  1316. pCompiledBlob, pPdbUtils,
  1317. L"source.hlsl",
  1318. expectedArgs, expectedFlags, expectedDefines,
  1319. pCompiler,
  1320. /*HasVersion*/ false,
  1321. /*IsFullPDB*/ true,
  1322. /*HasHashAndPdbName*/true,
  1323. /*TestReflection*/true,
  1324. main_source, included_File);
  1325. }
  1326. }
  1327. TEST_F(CompilerTest, CompileThenTestPdbUtils) {
  1328. TestPdbUtils(/*bSlim*/true, /*bSourceInDebugModule*/false, /*strip*/true); // Slim PDB, where source info is stored in its own part, and debug module is NOT present
  1329. TestPdbUtils(/*bSlim*/false, /*bSourceInDebugModule*/true, /*strip*/false); // Old PDB format, where source info is embedded in the module
  1330. TestPdbUtils(/*bSlim*/false, /*bSourceInDebugModule*/false, /*strip*/false); // Full PDB, where source info is stored in its own part, and a debug module which is present
  1331. TestPdbUtils(/*bSlim*/false, /*bSourceInDebugModule*/true, /*strip*/true); // Legacy PDB, where source info is embedded in the module
  1332. TestPdbUtils(/*bSlim*/false, /*bSourceInDebugModule*/false, /*strip*/true); // Full PDB, where source info is stored in its own part, and debug module is present
  1333. }
  1334. TEST_F(CompilerTest, CompileThenTestPdbInPrivate) {
  1335. CComPtr<IDxcCompiler> pCompiler;
  1336. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1337. std::string main_source = R"x(
  1338. cbuffer MyCbuffer : register(b1) {
  1339. float4 my_cbuf_foo;
  1340. }
  1341. [RootSignature("CBV(b1)")]
  1342. float4 main() : SV_Target {
  1343. return my_cbuf_foo;
  1344. }
  1345. )x";
  1346. CComPtr<IDxcUtils> pUtils;
  1347. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcUtils, &pUtils));
  1348. CComPtr<IDxcBlobEncoding> pSource;
  1349. VERIFY_SUCCEEDED(pUtils->CreateBlobFromPinned(main_source.c_str(), main_source.size(), CP_UTF8, &pSource));
  1350. const WCHAR *args[] = {
  1351. L"/Zs",
  1352. L"/Qpdb_in_private",
  1353. };
  1354. CComPtr<IDxcOperationResult> pOpResult;
  1355. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"hlsl.hlsl", L"main", L"ps_6_0", args, _countof(args), nullptr, 0, nullptr, &pOpResult));
  1356. CComPtr<IDxcResult> pResult;
  1357. VERIFY_SUCCEEDED(pOpResult.QueryInterface(&pResult));
  1358. CComPtr<IDxcBlob> pShader;
  1359. VERIFY_SUCCEEDED(pResult->GetOutput(DXC_OUT_OBJECT, IID_PPV_ARGS(&pShader), nullptr));
  1360. CComPtr<IDxcContainerReflection> pRefl;
  1361. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcContainerReflection, &pRefl));
  1362. VERIFY_SUCCEEDED(pRefl->Load(pShader));
  1363. UINT32 uIndex = 0;
  1364. VERIFY_SUCCEEDED(pRefl->FindFirstPartKind(hlsl::DFCC_PrivateData, &uIndex));
  1365. CComPtr<IDxcBlob> pPdbBlob;
  1366. VERIFY_SUCCEEDED(pResult->GetOutput(DXC_OUT_PDB, IID_PPV_ARGS(&pPdbBlob), nullptr));
  1367. CComPtr<IDxcBlob> pPrivatePdbBlob;
  1368. VERIFY_SUCCEEDED(pRefl->GetPartContent(uIndex, &pPrivatePdbBlob));
  1369. VERIFY_ARE_EQUAL(pPdbBlob->GetBufferSize(), pPrivatePdbBlob->GetBufferSize());
  1370. VERIFY_ARE_EQUAL(0, memcmp(pPdbBlob->GetBufferPointer(), pPrivatePdbBlob->GetBufferPointer(), pPdbBlob->GetBufferSize()));
  1371. }
  1372. TEST_F(CompilerTest, CompileThenTestPdbUtilsRelativePath) {
  1373. std::string main_source = R"x(
  1374. #include "helper.h"
  1375. cbuffer MyCbuffer : register(b1) {
  1376. float4 my_cbuf_foo;
  1377. }
  1378. [RootSignature("CBV(b1)")]
  1379. float4 main() : SV_Target {
  1380. return my_cbuf_foo;
  1381. }
  1382. )x";
  1383. CComPtr<IDxcCompiler3> pCompiler;
  1384. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  1385. DxcBuffer SourceBuf = {};
  1386. SourceBuf.Ptr = main_source.c_str();
  1387. SourceBuf.Size = main_source.size();
  1388. SourceBuf.Encoding = CP_UTF8;
  1389. std::vector<const WCHAR *> args;
  1390. args.push_back(L"/Tps_6_0");
  1391. args.push_back(L"/Zs");
  1392. args.push_back(L"shaders/Shader.hlsl");
  1393. CComPtr<TestIncludeHandler> pInclude;
  1394. std::string included_File = "#define ZERO 0";
  1395. pInclude = new TestIncludeHandler(m_dllSupport);
  1396. pInclude->CallResults.emplace_back(included_File.c_str());
  1397. CComPtr<IDxcResult> pResult;
  1398. VERIFY_SUCCEEDED(pCompiler->Compile(&SourceBuf, args.data(), args.size(), pInclude, IID_PPV_ARGS(&pResult)));
  1399. CComPtr<IDxcBlob> pPdb;
  1400. CComPtr<IDxcBlobUtf16> pPdbName;
  1401. VERIFY_SUCCEEDED(pResult->GetOutput(DXC_OUT_PDB, IID_PPV_ARGS(&pPdb), &pPdbName));
  1402. CComPtr<IDxcPdbUtils> pPdbUtils;
  1403. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcPdbUtils, &pPdbUtils));
  1404. VERIFY_SUCCEEDED(pPdbUtils->Load(pPdb));
  1405. CComPtr<IDxcBlob> pFullPdb;
  1406. VERIFY_SUCCEEDED(pPdbUtils->GetFullPDB(&pFullPdb));
  1407. VERIFY_SUCCEEDED(pPdbUtils->Load(pFullPdb));
  1408. VERIFY_IS_TRUE(pPdbUtils->IsFullPDB());
  1409. }
  1410. TEST_F(CompilerTest, CompileThenTestPdbUtilsEmptyEntry) {
  1411. std::string main_source = R"x(
  1412. cbuffer MyCbuffer : register(b1) {
  1413. float4 my_cbuf_foo;
  1414. }
  1415. [RootSignature("CBV(b1)")]
  1416. float4 main() : SV_Target {
  1417. return my_cbuf_foo;
  1418. }
  1419. )x";
  1420. CComPtr<IDxcCompiler3> pCompiler;
  1421. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  1422. DxcBuffer SourceBuf = {};
  1423. SourceBuf.Ptr = main_source.c_str();
  1424. SourceBuf.Size = main_source.size();
  1425. SourceBuf.Encoding = CP_UTF8;
  1426. std::vector<const WCHAR *> args;
  1427. args.push_back(L"/Tps_6_0");
  1428. args.push_back(L"/Zi");
  1429. CComPtr<IDxcResult> pResult;
  1430. VERIFY_SUCCEEDED(pCompiler->Compile(&SourceBuf, args.data(), args.size(), nullptr, IID_PPV_ARGS(&pResult)));
  1431. CComPtr<IDxcBlob> pPdb;
  1432. CComPtr<IDxcBlobUtf16> pPdbName;
  1433. VERIFY_SUCCEEDED(pResult->GetOutput(DXC_OUT_PDB, IID_PPV_ARGS(&pPdb), &pPdbName));
  1434. CComPtr<IDxcPdbUtils> pPdbUtils;
  1435. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcPdbUtils, &pPdbUtils));
  1436. VERIFY_SUCCEEDED(pPdbUtils->Load(pPdb));
  1437. CComBSTR pEntryName;
  1438. VERIFY_SUCCEEDED(pPdbUtils->GetEntryPoint(&pEntryName));
  1439. VERIFY_ARE_EQUAL(pEntryName, L"main");
  1440. }
  1441. #endif
  1442. TEST_F(CompilerTest, CompileWithRootSignatureThenStripRootSignature) {
  1443. CComPtr<IDxcCompiler> pCompiler;
  1444. CComPtr<IDxcOperationResult> pResult;
  1445. CComPtr<IDxcBlobEncoding> pSource;
  1446. CComPtr<IDxcBlob> pProgram;
  1447. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1448. CreateBlobFromText("[RootSignature(\"\")] \r\n"
  1449. "float4 main(float a : A) : SV_Target {\r\n"
  1450. " return a;\r\n"
  1451. "}",
  1452. &pSource);
  1453. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1454. L"ps_6_0", nullptr, 0, nullptr,
  1455. 0, nullptr, &pResult));
  1456. VERIFY_IS_NOT_NULL(pResult);
  1457. HRESULT status;
  1458. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1459. VERIFY_SUCCEEDED(status);
  1460. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1461. VERIFY_IS_NOT_NULL(pProgram);
  1462. hlsl::DxilContainerHeader *pContainerHeader = hlsl::IsDxilContainerLike(pProgram->GetBufferPointer(), pProgram->GetBufferSize());
  1463. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pProgram->GetBufferSize()));
  1464. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  1465. pContainerHeader, hlsl::DxilFourCC::DFCC_RootSignature);
  1466. VERIFY_IS_NOT_NULL(pPartHeader);
  1467. pResult.Release();
  1468. // Remove root signature
  1469. CComPtr<IDxcBlob> pProgramRootSigRemoved;
  1470. CComPtr<IDxcContainerBuilder> pBuilder;
  1471. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1472. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  1473. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_RootSignature));
  1474. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1475. VERIFY_SUCCEEDED(pResult->GetResult(&pProgramRootSigRemoved));
  1476. pContainerHeader = hlsl::IsDxilContainerLike(pProgramRootSigRemoved->GetBufferPointer(), pProgramRootSigRemoved->GetBufferSize());
  1477. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pProgramRootSigRemoved->GetBufferSize()));
  1478. hlsl::DxilPartHeader *pPartHeaderShouldBeNull = hlsl::GetDxilPartByType(pContainerHeader,
  1479. hlsl::DxilFourCC::DFCC_RootSignature);
  1480. VERIFY_IS_NULL(pPartHeaderShouldBeNull);
  1481. pBuilder.Release();
  1482. pResult.Release();
  1483. // Add root signature back
  1484. CComPtr<IDxcBlobEncoding> pRootSignatureBlob;
  1485. CComPtr<IDxcLibrary> pLibrary;
  1486. CComPtr<IDxcBlob> pProgramRootSigAdded;
  1487. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  1488. VERIFY_SUCCEEDED(pLibrary->CreateBlobWithEncodingFromPinned(
  1489. hlsl::GetDxilPartData(pPartHeader), pPartHeader->PartSize, 0, &pRootSignatureBlob));
  1490. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1491. VERIFY_SUCCEEDED(pBuilder->Load(pProgramRootSigRemoved));
  1492. pBuilder->AddPart(hlsl::DxilFourCC::DFCC_RootSignature, pRootSignatureBlob);
  1493. pBuilder->SerializeContainer(&pResult);
  1494. VERIFY_SUCCEEDED(pResult->GetResult(&pProgramRootSigAdded));
  1495. pContainerHeader = hlsl::IsDxilContainerLike(pProgramRootSigAdded->GetBufferPointer(), pProgramRootSigAdded->GetBufferSize());
  1496. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pProgramRootSigAdded->GetBufferSize()));
  1497. pPartHeader = hlsl::GetDxilPartByType(pContainerHeader,
  1498. hlsl::DxilFourCC::DFCC_RootSignature);
  1499. VERIFY_IS_NOT_NULL(pPartHeader);
  1500. }
  1501. #endif // Container builder unsupported
  1502. TEST_F(CompilerTest, CompileWhenIncludeThenLoadInvoked) {
  1503. CComPtr<IDxcCompiler> pCompiler;
  1504. CComPtr<IDxcOperationResult> pResult;
  1505. CComPtr<IDxcBlobEncoding> pSource;
  1506. CComPtr<TestIncludeHandler> pInclude;
  1507. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1508. CreateBlobFromText(
  1509. "#include \"helper.h\"\r\n"
  1510. "float4 main() : SV_Target { return 0; }", &pSource);
  1511. pInclude = new TestIncludeHandler(m_dllSupport);
  1512. pInclude->CallResults.emplace_back("");
  1513. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1514. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1515. VerifyOperationSucceeded(pResult);
  1516. VERIFY_ARE_EQUAL_WSTR(L"./helper.h;", pInclude->GetAllFileNames().c_str());
  1517. }
  1518. TEST_F(CompilerTest, CompileWhenIncludeThenLoadUsed) {
  1519. CComPtr<IDxcCompiler> pCompiler;
  1520. CComPtr<IDxcOperationResult> pResult;
  1521. CComPtr<IDxcBlobEncoding> pSource;
  1522. CComPtr<TestIncludeHandler> pInclude;
  1523. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1524. CreateBlobFromText(
  1525. "#include \"helper.h\"\r\n"
  1526. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1527. pInclude = new TestIncludeHandler(m_dllSupport);
  1528. pInclude->CallResults.emplace_back("#define ZERO 0");
  1529. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1530. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1531. VerifyOperationSucceeded(pResult);
  1532. VERIFY_ARE_EQUAL_WSTR(L"./helper.h;", pInclude->GetAllFileNames().c_str());
  1533. }
  1534. TEST_F(CompilerTest, CompileWhenIncludeAbsoluteThenLoadAbsolute) {
  1535. CComPtr<IDxcCompiler> pCompiler;
  1536. CComPtr<IDxcOperationResult> pResult;
  1537. CComPtr<IDxcBlobEncoding> pSource;
  1538. CComPtr<TestIncludeHandler> pInclude;
  1539. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1540. #ifdef _WIN32 // OS-specific root
  1541. CreateBlobFromText(
  1542. "#include \"C:\\helper.h\"\r\n"
  1543. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1544. #else
  1545. CreateBlobFromText(
  1546. "#include \"/helper.h\"\n"
  1547. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1548. #endif
  1549. pInclude = new TestIncludeHandler(m_dllSupport);
  1550. pInclude->CallResults.emplace_back("#define ZERO 0");
  1551. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1552. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1553. VerifyOperationSucceeded(pResult);
  1554. #ifdef _WIN32 // OS-specific root
  1555. VERIFY_ARE_EQUAL_WSTR(L"C:\\helper.h;", pInclude->GetAllFileNames().c_str());
  1556. #else
  1557. VERIFY_ARE_EQUAL_WSTR(L"/helper.h;", pInclude->GetAllFileNames().c_str());
  1558. #endif
  1559. }
  1560. TEST_F(CompilerTest, CompileWhenIncludeLocalThenLoadRelative) {
  1561. CComPtr<IDxcCompiler> pCompiler;
  1562. CComPtr<IDxcOperationResult> pResult;
  1563. CComPtr<IDxcBlobEncoding> pSource;
  1564. CComPtr<TestIncludeHandler> pInclude;
  1565. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1566. CreateBlobFromText(
  1567. "#include \"..\\helper.h\"\r\n"
  1568. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1569. pInclude = new TestIncludeHandler(m_dllSupport);
  1570. pInclude->CallResults.emplace_back("#define ZERO 0");
  1571. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1572. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1573. VerifyOperationSucceeded(pResult);
  1574. #ifdef _WIN32 // OS-specific directory dividers
  1575. VERIFY_ARE_EQUAL_WSTR(L"./..\\helper.h;", pInclude->GetAllFileNames().c_str());
  1576. #else
  1577. VERIFY_ARE_EQUAL_WSTR(L"./../helper.h;", pInclude->GetAllFileNames().c_str());
  1578. #endif
  1579. }
  1580. TEST_F(CompilerTest, CompileWhenIncludeSystemThenLoadNotRelative) {
  1581. CComPtr<IDxcCompiler> pCompiler;
  1582. CComPtr<IDxcOperationResult> pResult;
  1583. CComPtr<IDxcBlobEncoding> pSource;
  1584. CComPtr<TestIncludeHandler> pInclude;
  1585. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1586. CreateBlobFromText(
  1587. "#include \"subdir/other/file.h\"\r\n"
  1588. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1589. LPCWSTR args[] = {
  1590. L"-Ifoo"
  1591. };
  1592. pInclude = new TestIncludeHandler(m_dllSupport);
  1593. pInclude->CallResults.emplace_back("#include <helper.h>");
  1594. pInclude->CallResults.emplace_back("#define ZERO 0");
  1595. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1596. L"ps_6_0", args, _countof(args), nullptr, 0, pInclude, &pResult));
  1597. VerifyOperationSucceeded(pResult);
  1598. #ifdef _WIN32 // OS-specific directory dividers
  1599. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./foo\\helper.h;", pInclude->GetAllFileNames().c_str());
  1600. #else
  1601. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./foo/helper.h;", pInclude->GetAllFileNames().c_str());
  1602. #endif
  1603. }
  1604. TEST_F(CompilerTest, CompileWhenIncludeSystemMissingThenLoadAttempt) {
  1605. CComPtr<IDxcCompiler> pCompiler;
  1606. CComPtr<IDxcOperationResult> pResult;
  1607. CComPtr<IDxcBlobEncoding> pSource;
  1608. CComPtr<TestIncludeHandler> pInclude;
  1609. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1610. CreateBlobFromText(
  1611. "#include \"subdir/other/file.h\"\r\n"
  1612. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1613. pInclude = new TestIncludeHandler(m_dllSupport);
  1614. pInclude->CallResults.emplace_back("#include <helper.h>");
  1615. pInclude->CallResults.emplace_back("#define ZERO 0");
  1616. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1617. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1618. std::string failLog(VerifyOperationFailed(pResult));
  1619. VERIFY_ARE_NOT_EQUAL(std::string::npos, failLog.find("<angled>")); // error message should prompt to use <angled> rather than "quotes"
  1620. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./subdir/other/helper.h;", pInclude->GetAllFileNames().c_str());
  1621. }
  1622. TEST_F(CompilerTest, CompileWhenIncludeFlagsThenIncludeUsed) {
  1623. CComPtr<IDxcCompiler> pCompiler;
  1624. CComPtr<IDxcOperationResult> pResult;
  1625. CComPtr<IDxcBlobEncoding> pSource;
  1626. CComPtr<TestIncludeHandler> pInclude;
  1627. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1628. CreateBlobFromText(
  1629. "#include <helper.h>\r\n"
  1630. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1631. pInclude = new TestIncludeHandler(m_dllSupport);
  1632. pInclude->CallResults.emplace_back("#define ZERO 0");
  1633. #ifdef _WIN32 // OS-specific root
  1634. LPCWSTR args[] = { L"-I\\\\server\\share" };
  1635. #else
  1636. LPCWSTR args[] = { L"-I/server/share" };
  1637. #endif
  1638. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1639. L"ps_6_0", args, _countof(args), nullptr, 0, pInclude, &pResult));
  1640. VerifyOperationSucceeded(pResult);
  1641. #ifdef _WIN32 // OS-specific root
  1642. VERIFY_ARE_EQUAL_WSTR(L"\\\\server\\share\\helper.h;", pInclude->GetAllFileNames().c_str());
  1643. #else
  1644. VERIFY_ARE_EQUAL_WSTR(L"/server/share/helper.h;", pInclude->GetAllFileNames().c_str());
  1645. #endif
  1646. }
  1647. TEST_F(CompilerTest, CompileWhenIncludeMissingThenFail) {
  1648. CComPtr<IDxcCompiler> pCompiler;
  1649. CComPtr<IDxcOperationResult> pResult;
  1650. CComPtr<IDxcBlobEncoding> pSource;
  1651. CComPtr<TestIncludeHandler> pInclude;
  1652. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1653. CreateBlobFromText(
  1654. "#include \"file.h\"\r\n"
  1655. "float4 main() : SV_Target { return 0; }", &pSource);
  1656. pInclude = new TestIncludeHandler(m_dllSupport);
  1657. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1658. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1659. HRESULT hr;
  1660. VERIFY_SUCCEEDED(pResult->GetStatus(&hr));
  1661. VERIFY_FAILED(hr);
  1662. }
  1663. TEST_F(CompilerTest, CompileWhenIncludeHasPathThenOK) {
  1664. CComPtr<IDxcCompiler> pCompiler;
  1665. LPCWSTR Source = L"c:\\temp\\OddIncludes\\main.hlsl";
  1666. LPCWSTR Args[] = { L"/I", L"c:\\temp" };
  1667. LPCWSTR ArgsUp[] = { L"/I", L"c:\\Temp" };
  1668. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1669. bool useUpValues[] = { false, true };
  1670. for (bool useUp : useUpValues) {
  1671. CComPtr<IDxcOperationResult> pResult;
  1672. CComPtr<IDxcBlobEncoding> pSource;
  1673. #if TEST_ON_DISK
  1674. CComPtr<IDxcLibrary> pLibrary;
  1675. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  1676. VERIFY_SUCCEEDED(pLibrary->CreateIncludeHandler(&pInclude));
  1677. VERIFY_SUCCEEDED(pLibrary->CreateBlobFromFile(Source, nullptr, &pSource));
  1678. #else
  1679. CComPtr<TestIncludeHandler> pInclude;
  1680. pInclude = new TestIncludeHandler(m_dllSupport);
  1681. pInclude->CallResults.emplace_back("// Empty");
  1682. CreateBlobFromText("#include \"include.hlsl\"\r\n"
  1683. "float4 main() : SV_Target { return 0; }",
  1684. &pSource);
  1685. #endif
  1686. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, Source, L"main",
  1687. L"ps_6_0", useUp ? ArgsUp : Args, _countof(Args), nullptr, 0, pInclude, &pResult));
  1688. HRESULT hr;
  1689. VERIFY_SUCCEEDED(pResult->GetStatus(&hr));
  1690. VERIFY_SUCCEEDED(hr);
  1691. }
  1692. }
  1693. TEST_F(CompilerTest, CompileWhenIncludeEmptyThenOK) {
  1694. CComPtr<IDxcCompiler> pCompiler;
  1695. CComPtr<IDxcOperationResult> pResult;
  1696. CComPtr<IDxcBlobEncoding> pSource;
  1697. CComPtr<TestIncludeHandler> pInclude;
  1698. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1699. CreateBlobFromText("#include \"empty.h\"\r\n"
  1700. "float4 main() : SV_Target { return 0; }",
  1701. &pSource);
  1702. pInclude = new TestIncludeHandler(m_dllSupport);
  1703. pInclude->CallResults.emplace_back("", CP_ACP); // An empty file would get detected as ACP code page
  1704. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1705. L"ps_6_0", nullptr, 0, nullptr, 0,
  1706. pInclude, &pResult));
  1707. VerifyOperationSucceeded(pResult);
  1708. VERIFY_ARE_EQUAL_WSTR(L"./empty.h;", pInclude->GetAllFileNames().c_str());
  1709. }
  1710. static const char EmptyCompute[] = "[numthreads(8,8,1)] void main() { }";
  1711. TEST_F(CompilerTest, CompileWhenODumpThenPassConfig) {
  1712. CComPtr<IDxcCompiler> pCompiler;
  1713. CComPtr<IDxcOperationResult> pResult;
  1714. CComPtr<IDxcBlobEncoding> pSource;
  1715. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1716. CreateBlobFromText(EmptyCompute, &pSource);
  1717. LPCWSTR Args[] = { L"/Odump" };
  1718. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1719. L"cs_6_0", Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1720. VerifyOperationSucceeded(pResult);
  1721. CComPtr<IDxcBlob> pResultBlob;
  1722. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1723. wstring passes = BlobToUtf16(pResultBlob);
  1724. VERIFY_ARE_NOT_EQUAL(wstring::npos, passes.find(L"inline"));
  1725. }
  1726. TEST_F(CompilerTest, CompileWhenVdThenProducesDxilContainer) {
  1727. CComPtr<IDxcCompiler> pCompiler;
  1728. CComPtr<IDxcOperationResult> pResult;
  1729. CComPtr<IDxcBlobEncoding> pSource;
  1730. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1731. CreateBlobFromText(EmptyCompute, &pSource);
  1732. LPCWSTR Args[] = { L"/Vd" };
  1733. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1734. L"cs_6_0", Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1735. VerifyOperationSucceeded(pResult);
  1736. CComPtr<IDxcBlob> pResultBlob;
  1737. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1738. VERIFY_IS_TRUE(hlsl::IsValidDxilContainer(reinterpret_cast<hlsl::DxilContainerHeader *>(pResultBlob->GetBufferPointer()), pResultBlob->GetBufferSize()));
  1739. }
  1740. TEST_F(CompilerTest, CompileWhenODumpThenOptimizerMatch) {
  1741. LPCWSTR OptLevels[] = { L"/Od", L"/O1", L"/O2" };
  1742. CComPtr<IDxcCompiler> pCompiler;
  1743. CComPtr<IDxcOptimizer> pOptimizer;
  1744. CComPtr<IDxcAssembler> pAssembler;
  1745. CComPtr<IDxcValidator> pValidator;
  1746. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  1747. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  1748. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcOptimizer, &pOptimizer));
  1749. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcValidator, &pValidator));
  1750. for (LPCWSTR OptLevel : OptLevels) {
  1751. CComPtr<IDxcOperationResult> pResult;
  1752. CComPtr<IDxcBlobEncoding> pSource;
  1753. CComPtr<IDxcBlob> pHighLevelBlob;
  1754. CComPtr<IDxcBlob> pOptimizedModule;
  1755. CComPtr<IDxcBlob> pAssembledBlob;
  1756. // Could use EmptyCompute and cs_6_0, but there is an issue where properties
  1757. // don't round-trip properly at high-level, so validation fails because
  1758. // dimensions are set to zero. Workaround by using pixel shader instead.
  1759. LPCWSTR Target = L"ps_6_0";
  1760. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  1761. LPCWSTR Args[2] = { OptLevel, L"/Odump" };
  1762. // Get the passes for this optimization level.
  1763. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1764. Target, Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1765. VerifyOperationSucceeded(pResult);
  1766. CComPtr<IDxcBlob> pResultBlob;
  1767. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1768. wstring passes = BlobToUtf16(pResultBlob);
  1769. // Get wchar_t version and prepend hlsl-hlensure, to do a split high-level/opt compilation pass.
  1770. std::vector<LPCWSTR> Options;
  1771. SplitPassList(const_cast<LPWSTR>(passes.data()), Options);
  1772. // Now compile directly.
  1773. pResult.Release();
  1774. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1775. Target, Args, 1, nullptr, 0, nullptr, &pResult));
  1776. VerifyOperationSucceeded(pResult);
  1777. // Now compile via a high-level compile followed by the optimization passes.
  1778. pResult.Release();
  1779. Args[_countof(Args)-1] = L"/fcgl";
  1780. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1781. Target, Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1782. VerifyOperationSucceeded(pResult);
  1783. VERIFY_SUCCEEDED(pResult->GetResult(&pHighLevelBlob));
  1784. VERIFY_SUCCEEDED(pOptimizer->RunOptimizer(pHighLevelBlob, Options.data(),
  1785. Options.size(), &pOptimizedModule,
  1786. nullptr));
  1787. string text = DisassembleProgram(m_dllSupport, pOptimizedModule);
  1788. WEX::Logging::Log::Comment(L"Final program:");
  1789. WEX::Logging::Log::Comment(CA2W(text.c_str()));
  1790. // At the very least, the module should be valid.
  1791. pResult.Release();
  1792. VERIFY_SUCCEEDED(pAssembler->AssembleToContainer(pOptimizedModule, &pResult));
  1793. VerifyOperationSucceeded(pResult);
  1794. VERIFY_SUCCEEDED(pResult->GetResult(&pAssembledBlob));
  1795. pResult.Release();
  1796. VERIFY_SUCCEEDED(pValidator->Validate(pAssembledBlob, DxcValidatorFlags_Default, &pResult));
  1797. VerifyOperationSucceeded(pResult);
  1798. }
  1799. }
  1800. static const UINT CaptureStacks = 0; // Set to 1 to enable captures
  1801. static const UINT StackFrameCount = 12;
  1802. struct InstrumentedHeapMalloc : public IMalloc {
  1803. private:
  1804. HANDLE m_Handle; // Heap handle.
  1805. ULONG m_RefCount = 0; // Reference count. Used for reference leaks, not for lifetime.
  1806. ULONG m_AllocCount = 0; // Total # of alloc and realloc requests.
  1807. ULONG m_AllocSize = 0; // Total # of alloc and realloc bytes.
  1808. ULONG m_Size = 0; // Current # of alloc'ed bytes.
  1809. ULONG m_FailAlloc = 0; // If nonzero, the alloc/realloc call to fail.
  1810. // Each allocation also tracks the following information:
  1811. // - allocation callstack
  1812. // - deallocation callstack
  1813. // - prior/next blocks in a list of allocated blocks
  1814. LIST_ENTRY AllocList;
  1815. struct PtrData {
  1816. LIST_ENTRY Entry;
  1817. LPVOID AllocFrames[CaptureStacks ? StackFrameCount * CaptureStacks : 1];
  1818. LPVOID FreeFrames[CaptureStacks ? StackFrameCount * CaptureStacks : 1];
  1819. UINT64 AllocAtCount;
  1820. DWORD AllocFrameCount;
  1821. DWORD FreeFrameCount;
  1822. SIZE_T Size;
  1823. PtrData *Self;
  1824. };
  1825. PtrData *DataFromPtr(void *p) {
  1826. if (p == nullptr) return nullptr;
  1827. PtrData *R = ((PtrData *)p) - 1;
  1828. if (R != R->Self) {
  1829. VERIFY_FAIL(); // p is invalid or underrun
  1830. }
  1831. return R;
  1832. }
  1833. public:
  1834. InstrumentedHeapMalloc() : m_Handle(nullptr) {
  1835. ResetCounts();
  1836. }
  1837. ~InstrumentedHeapMalloc() {
  1838. if (m_Handle)
  1839. HeapDestroy(m_Handle);
  1840. }
  1841. void ResetHeap() {
  1842. if (m_Handle) {
  1843. HeapDestroy(m_Handle);
  1844. m_Handle = nullptr;
  1845. }
  1846. m_Handle = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
  1847. }
  1848. ULONG GetRefCount() const { return m_RefCount; }
  1849. ULONG GetAllocCount() const { return m_AllocCount; }
  1850. ULONG GetAllocSize() const { return m_AllocSize; }
  1851. ULONG GetSize() const { return m_Size; }
  1852. void ResetCounts() {
  1853. m_RefCount = m_AllocCount = m_AllocSize = m_Size = 0;
  1854. AllocList.Blink = AllocList.Flink = &AllocList;
  1855. }
  1856. void SetFailAlloc(ULONG index) {
  1857. m_FailAlloc = index;
  1858. }
  1859. ULONG STDMETHODCALLTYPE AddRef() {
  1860. return ++m_RefCount;
  1861. }
  1862. ULONG STDMETHODCALLTYPE Release() {
  1863. if (m_RefCount == 0) VERIFY_FAIL();
  1864. return --m_RefCount;
  1865. }
  1866. STDMETHODIMP QueryInterface(REFIID iid, void** ppvObject) {
  1867. return DoBasicQueryInterface<IMalloc>(this, iid, ppvObject);
  1868. }
  1869. virtual void *STDMETHODCALLTYPE Alloc(_In_ SIZE_T cb) {
  1870. ++m_AllocCount;
  1871. if (m_FailAlloc && m_AllocCount >= m_FailAlloc) {
  1872. return nullptr; // breakpoint for i failure - m_FailAlloc == 1+VAL
  1873. }
  1874. m_AllocSize += cb;
  1875. m_Size += cb;
  1876. PtrData *P = (PtrData *)HeapAlloc(m_Handle, HEAP_ZERO_MEMORY, sizeof(PtrData) + cb);
  1877. P->Entry.Flink = AllocList.Flink;
  1878. P->Entry.Blink = &AllocList;
  1879. AllocList.Flink->Blink = &(P->Entry);
  1880. AllocList.Flink = &(P->Entry);
  1881. // breakpoint for i failure on NN alloc - m_FailAlloc == 1+VAL && m_AllocCount == NN
  1882. // breakpoint for happy path for NN alloc - m_AllocCount == NN
  1883. P->AllocAtCount = m_AllocCount;
  1884. if (CaptureStacks)
  1885. P->AllocFrameCount = CaptureStackBackTrace(1, StackFrameCount, P->AllocFrames, nullptr);
  1886. P->Size = cb;
  1887. P->Self = P;
  1888. return P + 1;
  1889. }
  1890. virtual void *STDMETHODCALLTYPE Realloc(_In_opt_ void *pv, _In_ SIZE_T cb) {
  1891. SIZE_T priorSize = pv == nullptr ? (SIZE_T)0 : GetSize(pv);
  1892. void *R = Alloc(cb);
  1893. if (!R)
  1894. return nullptr;
  1895. SIZE_T copySize = std::min(cb, priorSize);
  1896. memcpy(R, pv, copySize);
  1897. Free(pv);
  1898. return R;
  1899. }
  1900. virtual void STDMETHODCALLTYPE Free(_In_opt_ void *pv) {
  1901. if (!pv)
  1902. return;
  1903. PtrData *P = DataFromPtr(pv);
  1904. if (P->FreeFrameCount)
  1905. VERIFY_FAIL(); // double-free detected
  1906. m_Size -= P->Size;
  1907. P->Entry.Flink->Blink = P->Entry.Blink;
  1908. P->Entry.Blink->Flink = P->Entry.Flink;
  1909. if (CaptureStacks)
  1910. P->FreeFrameCount =
  1911. CaptureStackBackTrace(1, StackFrameCount, P->FreeFrames, nullptr);
  1912. }
  1913. virtual SIZE_T STDMETHODCALLTYPE GetSize(
  1914. /* [annotation][in] */
  1915. _In_opt_ _Post_writable_byte_size_(return) void *pv)
  1916. {
  1917. if (pv == nullptr) return 0;
  1918. return DataFromPtr(pv)->Size;
  1919. }
  1920. virtual int STDMETHODCALLTYPE DidAlloc(
  1921. _In_opt_ void *pv) {
  1922. return -1; // don't know
  1923. }
  1924. virtual void STDMETHODCALLTYPE HeapMinimize(void) {}
  1925. void DumpLeaks() {
  1926. PtrData *ptr = (PtrData*)AllocList.Flink;;
  1927. PtrData *end = (PtrData*)AllocList.Blink;;
  1928. WEX::Logging::Log::Comment(FormatToWString(L"Leaks total size: %d", (signed int)m_Size).data());
  1929. while (ptr != end) {
  1930. WEX::Logging::Log::Comment(FormatToWString(L"Memory leak at 0x0%X, size %d, alloc# %d", ptr + 1, ptr->Size, ptr->AllocAtCount).data());
  1931. ptr = (PtrData*)ptr->Entry.Flink;
  1932. }
  1933. }
  1934. };
  1935. #if _ITERATOR_DEBUG_LEVEL==0
  1936. // CompileWhenNoMemThenOOM can properly detect leaks only when debug iterators are disabled
  1937. TEST_F(CompilerTest, CompileWhenNoMemThenOOM) {
  1938. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  1939. CComPtr<IDxcBlobEncoding> pSource;
  1940. CreateBlobFromText(EmptyCompute, &pSource);
  1941. InstrumentedHeapMalloc InstrMalloc;
  1942. CComPtr<IDxcCompiler> pCompiler;
  1943. CComPtr<IDxcOperationResult> pResult;
  1944. ULONG allocCount = 0;
  1945. ULONG allocSize = 0;
  1946. ULONG initialRefCount;
  1947. InstrMalloc.ResetHeap();
  1948. VERIFY_IS_TRUE(m_dllSupport.HasCreateWithMalloc());
  1949. // Verify a simple object creation.
  1950. initialRefCount = InstrMalloc.GetRefCount();
  1951. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler));
  1952. pCompiler.Release();
  1953. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1954. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1955. InstrMalloc.ResetCounts();
  1956. InstrMalloc.ResetHeap();
  1957. // First time, run to completion and capture stats.
  1958. initialRefCount = InstrMalloc.GetRefCount();
  1959. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler));
  1960. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1961. L"cs_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1962. allocCount = InstrMalloc.GetAllocCount();
  1963. allocSize = InstrMalloc.GetAllocSize();
  1964. HRESULT hrWithMemory;
  1965. VERIFY_SUCCEEDED(pResult->GetStatus(&hrWithMemory));
  1966. VERIFY_SUCCEEDED(hrWithMemory);
  1967. pCompiler.Release();
  1968. pResult.Release();
  1969. VERIFY_IS_TRUE(allocSize > allocCount);
  1970. // Ensure that after all resources are released, there are no outstanding
  1971. // allocations or references.
  1972. //
  1973. // First leak is in ((InstrumentedHeapMalloc::PtrData *)InstrMalloc.AllocList.Flink)
  1974. if (InstrMalloc.GetSize() != 0) {
  1975. WEX::Logging::Log::Comment(L"Memory leak(s) detected");
  1976. InstrMalloc.DumpLeaks();
  1977. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1978. }
  1979. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1980. // In Debug, without /D_ITERATOR_DEBUG_LEVEL=0, debug iterators will be used;
  1981. // this causes a problem where std::string is specified as noexcept, and yet
  1982. // a sentinel is allocated that may fail and throw.
  1983. if (m_ver.SkipOutOfMemoryTest()) return;
  1984. // Now, fail each allocation and make sure we get an error.
  1985. for (ULONG i = 0; i <= allocCount; ++i) {
  1986. // LogCommentFmt(L"alloc fail %u", i);
  1987. bool isLast = i == allocCount;
  1988. InstrMalloc.ResetCounts();
  1989. InstrMalloc.ResetHeap();
  1990. InstrMalloc.SetFailAlloc(i + 1);
  1991. HRESULT hrOp = m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler);
  1992. if (SUCCEEDED(hrOp)) {
  1993. hrOp = pCompiler->Compile(pSource, L"source.hlsl", L"main", L"cs_6_0",
  1994. nullptr, 0, nullptr, 0, nullptr, &pResult);
  1995. if (SUCCEEDED(hrOp)) {
  1996. pResult->GetStatus(&hrOp);
  1997. }
  1998. }
  1999. if (FAILED(hrOp)) {
  2000. // This is true in *almost* every case. When the OOM happens during stream
  2001. // handling, there is no specific error set; by the time it's detected,
  2002. // it propagates as E_FAIL.
  2003. //VERIFY_ARE_EQUAL(hrOp, E_OUTOFMEMORY);
  2004. VERIFY_IS_TRUE(hrOp == E_OUTOFMEMORY || hrOp == E_FAIL);
  2005. }
  2006. if (isLast)
  2007. VERIFY_SUCCEEDED(hrOp);
  2008. else
  2009. VERIFY_FAILED(hrOp);
  2010. pCompiler.Release();
  2011. pResult.Release();
  2012. if (InstrMalloc.GetSize() != 0) {
  2013. WEX::Logging::Log::Comment(FormatToWString(L"Memory leak(s) detected, allocCount = %d", i).data());
  2014. InstrMalloc.DumpLeaks();
  2015. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  2016. }
  2017. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  2018. }
  2019. }
  2020. #endif
  2021. TEST_F(CompilerTest, CompileWhenShaderModelMismatchAttributeThenFail) {
  2022. CComPtr<IDxcCompiler> pCompiler;
  2023. CComPtr<IDxcOperationResult> pResult;
  2024. CComPtr<IDxcBlobEncoding> pSource;
  2025. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2026. CreateBlobFromText(EmptyCompute, &pSource);
  2027. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2028. L"ps_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  2029. std::string failLog(VerifyOperationFailed(pResult));
  2030. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find("attribute numthreads only valid for CS"));
  2031. }
  2032. TEST_F(CompilerTest, CompileBadHlslThenFail) {
  2033. CComPtr<IDxcCompiler> pCompiler;
  2034. CComPtr<IDxcOperationResult> pResult;
  2035. CComPtr<IDxcBlobEncoding> pSource;
  2036. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2037. CreateBlobFromText(
  2038. "bad hlsl", &pSource);
  2039. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2040. L"ps_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  2041. HRESULT status;
  2042. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  2043. VERIFY_FAILED(status);
  2044. }
  2045. TEST_F(CompilerTest, CompileLegacyShaderModelThenFail) {
  2046. VerifyCompileFailed(
  2047. "float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", L"ps_5_1", nullptr);
  2048. }
  2049. TEST_F(CompilerTest, CompileWhenRecursiveAlbeitStaticTermThenFail) {
  2050. // This shader will compile under fxc because if execution is
  2051. // simulated statically, it does terminate. dxc changes this behavior
  2052. // to avoid imposing the requirement on the compiler.
  2053. const char ShaderText[] =
  2054. "static int i = 10;\r\n"
  2055. "float4 f(); // Forward declaration\r\n"
  2056. "float4 g() { if (i > 10) { i--; return f(); } else return 0; } // Recursive call to 'f'\r\n"
  2057. "float4 f() { return g(); } // First call to 'g'\r\n"
  2058. "float4 VS() : SV_Position{\r\n"
  2059. " return f(); // First call to 'f'\r\n"
  2060. "}\r\n";
  2061. VerifyCompileFailed(ShaderText, L"vs_6_0", "recursive functions not allowed", L"VS");
  2062. }
  2063. TEST_F(CompilerTest, CompileWhenRecursiveThenFail) {
  2064. const char ShaderTextSimple[] =
  2065. "float4 f(); // Forward declaration\r\n"
  2066. "float4 g() { return f(); } // Recursive call to 'f'\r\n"
  2067. "float4 f() { return g(); } // First call to 'g'\r\n"
  2068. "float4 main() : SV_Position{\r\n"
  2069. " return f(); // First call to 'f'\r\n"
  2070. "}\r\n";
  2071. VerifyCompileFailed(ShaderTextSimple, L"vs_6_0", "recursive functions not allowed");
  2072. const char ShaderTextIndirect[] =
  2073. "float4 f(); // Forward declaration\r\n"
  2074. "float4 g() { return f(); } // Recursive call to 'f'\r\n"
  2075. "float4 f() { return g(); } // First call to 'g'\r\n"
  2076. "float4 main() : SV_Position{\r\n"
  2077. " return f(); // First call to 'f'\r\n"
  2078. "}\r\n";
  2079. VerifyCompileFailed(ShaderTextIndirect, L"vs_6_0", "recursive functions not allowed");
  2080. const char ShaderTextSelf[] =
  2081. "float4 main() : SV_Position{\r\n"
  2082. " return main();\r\n"
  2083. "}\r\n";
  2084. VerifyCompileFailed(ShaderTextSelf, L"vs_6_0", "recursive functions not allowed");
  2085. const char ShaderTextMissing[] =
  2086. "float4 mainz() : SV_Position{\r\n"
  2087. " return 1;\r\n"
  2088. "}\r\n";
  2089. VerifyCompileFailed(ShaderTextMissing, L"vs_6_0", "missing entry point definition");
  2090. }
  2091. TEST_F(CompilerTest, CompileHlsl2015ThenFail) {
  2092. CComPtr<IDxcCompiler> pCompiler;
  2093. CComPtr<IDxcOperationResult> pResult;
  2094. CComPtr<IDxcBlobEncoding> pSource;
  2095. CComPtr<IDxcBlobEncoding> pErrors;
  2096. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2097. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  2098. LPCWSTR args[2] = { L"-HV", L"2015" };
  2099. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2100. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  2101. HRESULT status;
  2102. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  2103. VERIFY_ARE_EQUAL(status, E_INVALIDARG);
  2104. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  2105. LPCSTR pErrorMsg = "HLSL Version 2015 is only supported for language services";
  2106. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  2107. }
  2108. TEST_F(CompilerTest, CompileHlsl2016ThenOK) {
  2109. CComPtr<IDxcCompiler> pCompiler;
  2110. CComPtr<IDxcOperationResult> pResult;
  2111. CComPtr<IDxcBlobEncoding> pSource;
  2112. CComPtr<IDxcBlobEncoding> pErrors;
  2113. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2114. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  2115. LPCWSTR args[2] = { L"-HV", L"2016" };
  2116. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2117. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  2118. HRESULT status;
  2119. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  2120. VERIFY_SUCCEEDED(status);
  2121. }
  2122. TEST_F(CompilerTest, CompileHlsl2017ThenOK) {
  2123. CComPtr<IDxcCompiler> pCompiler;
  2124. CComPtr<IDxcOperationResult> pResult;
  2125. CComPtr<IDxcBlobEncoding> pSource;
  2126. CComPtr<IDxcBlobEncoding> pErrors;
  2127. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2128. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  2129. LPCWSTR args[2] = { L"-HV", L"2017" };
  2130. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2131. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  2132. HRESULT status;
  2133. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  2134. VERIFY_SUCCEEDED(status);
  2135. }
  2136. TEST_F(CompilerTest, CompileHlsl2018ThenOK) {
  2137. CComPtr<IDxcCompiler> pCompiler;
  2138. CComPtr<IDxcOperationResult> pResult;
  2139. CComPtr<IDxcBlobEncoding> pSource;
  2140. CComPtr<IDxcBlobEncoding> pErrors;
  2141. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2142. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  2143. LPCWSTR args[2] = { L"-HV", L"2018" };
  2144. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2145. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  2146. HRESULT status;
  2147. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  2148. VERIFY_SUCCEEDED(status);
  2149. }
  2150. TEST_F(CompilerTest, CompileHlsl2019ThenFail) {
  2151. CComPtr<IDxcCompiler> pCompiler;
  2152. CComPtr<IDxcOperationResult> pResult;
  2153. CComPtr<IDxcBlobEncoding> pSource;
  2154. CComPtr<IDxcBlobEncoding> pErrors;
  2155. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2156. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  2157. LPCWSTR args[2] = { L"-HV", L"2019" };
  2158. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2159. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  2160. HRESULT status;
  2161. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  2162. VERIFY_ARE_EQUAL(status, E_INVALIDARG);
  2163. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  2164. LPCSTR pErrorMsg = "Unknown HLSL version";
  2165. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  2166. }
  2167. #ifdef _WIN32
  2168. #pragma fenv_access(on)
  2169. #pragma optimize("", off)
  2170. #pragma warning(disable : 4723)
  2171. // Define test state as something weird that we can verify was restored
  2172. static const unsigned int fpTestState =
  2173. (_MCW_EM & (~_EM_ZERODIVIDE)) | // throw on div by zero
  2174. _DN_FLUSH_OPERANDS_SAVE_RESULTS | // denorm flush operands & save results
  2175. _RC_UP; // round up
  2176. static const unsigned int fpTestMask = _MCW_EM | _MCW_DN | _MCW_RC;
  2177. struct FPTestScope
  2178. {
  2179. // _controlfp_s is non-standard and <cfenv> doesn't have a function to enable exceptions
  2180. unsigned int fpSavedState;
  2181. FPTestScope() {
  2182. VERIFY_IS_TRUE(_controlfp_s(&fpSavedState, 0, 0) == 0);
  2183. unsigned int newValue;
  2184. VERIFY_IS_TRUE(_controlfp_s(&newValue, fpTestState, fpTestMask) == 0);
  2185. }
  2186. ~FPTestScope() {
  2187. unsigned int newValue;
  2188. errno_t error = _controlfp_s(&newValue, fpSavedState, fpTestMask);
  2189. DXASSERT_LOCALVAR(error, error == 0, "Failed to restore floating-point environment.");
  2190. }
  2191. };
  2192. void VerifyDivByZeroThrows() {
  2193. bool bCaughtExpectedException = false;
  2194. __try {
  2195. float one = 1.0;
  2196. float zero = 0.0;
  2197. float val = one / zero;
  2198. (void)val;
  2199. } __except(EXCEPTION_EXECUTE_HANDLER) {
  2200. bCaughtExpectedException = true;
  2201. }
  2202. VERIFY_IS_TRUE(bCaughtExpectedException);
  2203. }
  2204. TEST_F(CompilerTest, CodeGenFloatingPointEnvironment) {
  2205. unsigned int fpOriginal;
  2206. VERIFY_IS_TRUE(_controlfp_s(&fpOriginal, 0, 0) == 0);
  2207. {
  2208. FPTestScope fpTestScope;
  2209. // Get state before/after compilation, making sure it's our test state,
  2210. // and that it is restored after the compile.
  2211. unsigned int fpBeforeCompile;
  2212. VERIFY_IS_TRUE(_controlfp_s(&fpBeforeCompile, 0, 0) == 0);
  2213. VERIFY_ARE_EQUAL((fpBeforeCompile & fpTestMask), fpTestState);
  2214. CodeGenTestCheck(L"fpexcept.hlsl");
  2215. // Verify excpetion environment was restored
  2216. unsigned int fpAfterCompile;
  2217. VERIFY_IS_TRUE(_controlfp_s(&fpAfterCompile, 0, 0) == 0);
  2218. VERIFY_ARE_EQUAL((fpBeforeCompile & fpTestMask), (fpAfterCompile & fpTestMask));
  2219. // Make sure round up is set
  2220. VERIFY_ARE_EQUAL(rint(12.25), 13);
  2221. // Make sure we actually enabled div-by-zero exception
  2222. VerifyDivByZeroThrows();
  2223. }
  2224. // Verify original state has been restored
  2225. unsigned int fpLocal;
  2226. VERIFY_IS_TRUE(_controlfp_s(&fpLocal, 0, 0) == 0);
  2227. VERIFY_ARE_EQUAL(fpLocal, fpOriginal);
  2228. }
  2229. #pragma optimize("", on)
  2230. #else // _WIN32
  2231. // Only implemented on Win32
  2232. TEST_F(CompilerTest, CodeGenFloatingPointEnvironment) {
  2233. VERIFY_IS_TRUE(true);
  2234. }
  2235. #endif // _WIN32
  2236. TEST_F(CompilerTest, CodeGenInclude) {
  2237. CodeGenTestCheck(L"Include.hlsl");
  2238. }
  2239. TEST_F(CompilerTest, CodeGenLibCsEntry) {
  2240. CodeGenTestCheck(L"lib_cs_entry.hlsl");
  2241. }
  2242. TEST_F(CompilerTest, CodeGenLibCsEntry2) {
  2243. CodeGenTestCheck(L"lib_cs_entry2.hlsl");
  2244. }
  2245. TEST_F(CompilerTest, CodeGenLibCsEntry3) {
  2246. CodeGenTestCheck(L"lib_cs_entry3.hlsl");
  2247. }
  2248. TEST_F(CompilerTest, CodeGenLibEntries) {
  2249. CodeGenTestCheck(L"lib_entries.hlsl");
  2250. }
  2251. TEST_F(CompilerTest, CodeGenLibEntries2) {
  2252. CodeGenTestCheck(L"lib_entries2.hlsl");
  2253. }
  2254. TEST_F(CompilerTest, CodeGenLibNoAlias) {
  2255. CodeGenTestCheck(L"lib_no_alias.hlsl");
  2256. }
  2257. TEST_F(CompilerTest, CodeGenLibResource) {
  2258. CodeGenTestCheck(L"lib_resource.hlsl");
  2259. }
  2260. TEST_F(CompilerTest, CodeGenLibUnusedFunc) {
  2261. CodeGenTestCheck(L"lib_unused_func.hlsl");
  2262. }
  2263. TEST_F(CompilerTest, CodeGenRootSigProfile) {
  2264. if (m_ver.SkipDxilVersion(1, 5)) return;
  2265. CodeGenTest(L"rootSigProfile.hlsl");
  2266. }
  2267. TEST_F(CompilerTest, CodeGenRootSigProfile2) {
  2268. if (m_ver.SkipDxilVersion(1, 5)) return;
  2269. // TODO: Verify the result when reflect the structures.
  2270. CodeGenTest(L"rootSigProfile2.hlsl");
  2271. }
  2272. TEST_F(CompilerTest, CodeGenRootSigProfile5) {
  2273. if (m_ver.SkipDxilVersion(1, 5)) return;
  2274. CodeGenTest(L"rootSigProfile5.hlsl");
  2275. }
  2276. TEST_F(CompilerTest, CodeGenWaveSize) {
  2277. CodeGenTestCheck(L"attributes_wavesize.hlsl");
  2278. }
  2279. TEST_F(CompilerTest, LibGVStore) {
  2280. CComPtr<IDxcCompiler> pCompiler;
  2281. CComPtr<IDxcOperationResult> pResult;
  2282. CComPtr<IDxcBlobEncoding> pSource;
  2283. CComPtr<IDxcContainerReflection> pReflection;
  2284. CComPtr<IDxcAssembler> pAssembler;
  2285. VERIFY_SUCCEEDED(this->m_dllSupport.CreateInstance(CLSID_DxcContainerReflection, &pReflection));
  2286. VERIFY_SUCCEEDED(this->m_dllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  2287. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2288. CreateBlobFromText(
  2289. R"(
  2290. struct T {
  2291. RWByteAddressBuffer outputBuffer;
  2292. RWByteAddressBuffer outputBuffer2;
  2293. };
  2294. struct D {
  2295. float4 a;
  2296. int4 b;
  2297. };
  2298. struct T2 {
  2299. RWStructuredBuffer<D> uav;
  2300. };
  2301. T2 resStruct(T t, uint2 id);
  2302. RWByteAddressBuffer outputBuffer;
  2303. RWByteAddressBuffer outputBuffer2;
  2304. [numthreads(8, 8, 1)]
  2305. void main( uint2 id : SV_DispatchThreadID )
  2306. {
  2307. T t = {outputBuffer,outputBuffer2};
  2308. T2 t2 = resStruct(t, id);
  2309. uint counter = t2.uav.IncrementCounter();
  2310. t2.uav[counter].b.xy = id;
  2311. }
  2312. )", &pSource);
  2313. const WCHAR *pArgs[] = {
  2314. L"/Zi",
  2315. };
  2316. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"file.hlsl", L"", L"lib_6_x",
  2317. pArgs, _countof(pArgs), nullptr, 0, nullptr,
  2318. &pResult));
  2319. CComPtr<IDxcBlob> pShader;
  2320. VERIFY_SUCCEEDED(pResult->GetResult(&pShader));
  2321. VERIFY_SUCCEEDED(pReflection->Load(pShader));
  2322. UINT32 index = 0;
  2323. VERIFY_SUCCEEDED(pReflection->FindFirstPartKind(hlsl::DFCC_DXIL, &index));
  2324. CComPtr<IDxcBlob> pBitcode;
  2325. VERIFY_SUCCEEDED(pReflection->GetPartContent(index, &pBitcode));
  2326. const char *bitcode = hlsl::GetDxilBitcodeData((hlsl::DxilProgramHeader *)pBitcode->GetBufferPointer());
  2327. unsigned bitcode_size = hlsl::GetDxilBitcodeSize((hlsl::DxilProgramHeader *)pBitcode->GetBufferPointer());
  2328. CComPtr<IDxcBlobEncoding> pBitcodeBlob;
  2329. CreateBlobPinned(bitcode, bitcode_size, CP_UTF8, &pBitcodeBlob);
  2330. CComPtr<IDxcBlob> pReassembled;
  2331. CComPtr<IDxcOperationResult> pReassembleResult;
  2332. VERIFY_SUCCEEDED(pAssembler->AssembleToContainer(pBitcodeBlob, &pReassembleResult));
  2333. VERIFY_SUCCEEDED(pReassembleResult->GetResult(&pReassembled));
  2334. CComPtr<IDxcBlobEncoding> pTextBlob;
  2335. VERIFY_SUCCEEDED(pCompiler->Disassemble(pReassembled, &pTextBlob));
  2336. std::wstring Text = BlobToUtf16(pTextBlob);
  2337. VERIFY_ARE_NOT_EQUAL(std::wstring::npos, Text.find(L"store"));
  2338. }
  2339. TEST_F(CompilerTest, PreprocessWhenValidThenOK) {
  2340. CComPtr<IDxcCompiler> pCompiler;
  2341. CComPtr<IDxcOperationResult> pResult;
  2342. CComPtr<IDxcBlobEncoding> pSource;
  2343. DxcDefine defines[2];
  2344. defines[0].Name = L"MYDEF";
  2345. defines[0].Value = L"int";
  2346. defines[1].Name = L"MYOTHERDEF";
  2347. defines[1].Value = L"123";
  2348. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2349. CreateBlobFromText(
  2350. "// First line\r\n"
  2351. "MYDEF g_int = MYOTHERDEF;\r\n"
  2352. "#define FOO BAR\r\n"
  2353. "int FOO;", &pSource);
  2354. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl", nullptr, 0,
  2355. defines, _countof(defines), nullptr,
  2356. &pResult));
  2357. HRESULT hrOp;
  2358. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  2359. VERIFY_SUCCEEDED(hrOp);
  2360. CComPtr<IDxcBlob> pOutText;
  2361. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  2362. std::string text(BlobToUtf8(pOutText));
  2363. VERIFY_ARE_EQUAL_STR(
  2364. "#line 1 \"file.hlsl\"\n"
  2365. "\n"
  2366. "int g_int = 123;\n"
  2367. "\n"
  2368. "int BAR;\n", text.c_str());
  2369. }
  2370. TEST_F(CompilerTest, PreprocessWhenExpandTokenPastingOperandThenAccept) {
  2371. // Tests that we can turn on fxc's behavior (pre-expanding operands before
  2372. // performing token-pasting) using -flegacy-macro-expansion
  2373. CComPtr<IDxcCompiler> pCompiler;
  2374. CComPtr<IDxcOperationResult> pResult;
  2375. CComPtr<IDxcBlobEncoding> pSource;
  2376. LPCWSTR expandOption = L"-flegacy-macro-expansion";
  2377. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2378. CreateBlobFromText(R"(
  2379. #define SET_INDEX0 10
  2380. #define BINDING_INDEX0 5
  2381. #define SET(INDEX) SET_INDEX##INDEX
  2382. #define BINDING(INDEX) BINDING_INDEX##INDEX
  2383. #define SET_BIND(NAME,SET,BIND) resource_set_##SET##_bind_##BIND##_##NAME
  2384. #define RESOURCE(NAME,INDEX) SET_BIND(NAME, SET(INDEX), BINDING(INDEX))
  2385. Texture2D<float4> resource_set_10_bind_5_tex;
  2386. float4 main() : SV_Target{
  2387. return RESOURCE(tex, 0)[uint2(1, 2)];
  2388. }
  2389. )",
  2390. &pSource);
  2391. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl", &expandOption,
  2392. 1, nullptr, 0, nullptr, &pResult));
  2393. HRESULT hrOp;
  2394. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  2395. VERIFY_SUCCEEDED(hrOp);
  2396. CComPtr<IDxcBlob> pOutText;
  2397. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  2398. std::string text(BlobToUtf8(pOutText));
  2399. VERIFY_ARE_EQUAL_STR(R"(#line 1 "file.hlsl"
  2400. #line 12 "file.hlsl"
  2401. Texture2D<float4> resource_set_10_bind_5_tex;
  2402. float4 main() : SV_Target{
  2403. return resource_set_10_bind_5_tex[uint2(1, 2)];
  2404. }
  2405. )",
  2406. text.c_str());
  2407. }
  2408. TEST_F(CompilerTest, PreprocessWithDebugOptsThenOk) {
  2409. // Make sure debug options, such as -Zi and -Fd,
  2410. // are simply ignored when preprocessing
  2411. CComPtr<IDxcCompiler> pCompiler;
  2412. CComPtr<IDxcOperationResult> pResult;
  2413. CComPtr<IDxcBlobEncoding> pSource;
  2414. DxcDefine defines[2];
  2415. defines[0].Name = L"MYDEF";
  2416. defines[0].Value = L"int";
  2417. defines[1].Name = L"MYOTHERDEF";
  2418. defines[1].Value = L"123";
  2419. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2420. CreateBlobFromText(
  2421. "// First line\r\n"
  2422. "MYDEF g_int = MYOTHERDEF;\r\n"
  2423. "#define FOO BAR\r\n"
  2424. "int FOO;", &pSource);
  2425. LPCWSTR extraOptions[] = {L"-Zi", L"-Fd", L"file.pdb", L"-Qembed_debug"};
  2426. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl",
  2427. extraOptions, _countof(extraOptions),
  2428. defines, _countof(defines), nullptr,
  2429. &pResult));
  2430. HRESULT hrOp;
  2431. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  2432. VERIFY_SUCCEEDED(hrOp);
  2433. CComPtr<IDxcBlob> pOutText;
  2434. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  2435. std::string text(BlobToUtf8(pOutText));
  2436. VERIFY_ARE_EQUAL_STR(
  2437. "#line 1 \"file.hlsl\"\n"
  2438. "\n"
  2439. "int g_int = 123;\n"
  2440. "\n"
  2441. "int BAR;\n", text.c_str());
  2442. }
  2443. TEST_F(CompilerTest, CompileOtherModesWithDebugOptsThenOk) {
  2444. // Make sure debug options, such as -Zi and -Fd,
  2445. // are simply ignored when compiling in modes:
  2446. // /Odump -ast-dump -fcgl -rootsig_1_0
  2447. CComPtr<IDxcCompiler> pCompiler;
  2448. CComPtr<IDxcBlobEncoding> pSource;
  2449. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2450. CreateBlobFromText(
  2451. "#define RS \"CBV(b0)\"\n"
  2452. "[RootSignature(RS)]\n"
  2453. "float main(float i : IN) : OUT { return i * 2.0F; }",
  2454. &pSource);
  2455. auto testWithOpts = [&](LPCWSTR entry, LPCWSTR target, llvm::ArrayRef<LPCWSTR> mainOpts) -> HRESULT {
  2456. std::vector<LPCWSTR> opts(mainOpts);
  2457. opts.insert(opts.end(), {L"-Zi", L"-Fd", L"file.pdb"});
  2458. CComPtr<IDxcOperationResult> pResult;
  2459. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"file.hlsl",
  2460. entry, target, opts.data(), opts.size(),
  2461. nullptr, 0, nullptr, &pResult));
  2462. HRESULT hrOp;
  2463. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  2464. return hrOp;
  2465. };
  2466. VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"/Odump"}));
  2467. VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"-ast-dump"}));
  2468. VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"-fcgl"}));
  2469. VERIFY_SUCCEEDED(testWithOpts(L"RS", L"rootsig_1_0", {}));
  2470. }
  2471. TEST_F(CompilerTest, WhenSigMismatchPCFunctionThenFail) {
  2472. CComPtr<IDxcCompiler> pCompiler;
  2473. CComPtr<IDxcOperationResult> pResult;
  2474. CComPtr<IDxcBlobEncoding> pSource;
  2475. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2476. CreateBlobFromText(
  2477. "struct PSSceneIn \n\
  2478. { \n\
  2479. float4 pos : SV_Position; \n\
  2480. float2 tex : TEXCOORD0; \n\
  2481. float3 norm : NORMAL; \n\
  2482. }; \n"
  2483. "struct HSPerPatchData { \n\
  2484. float edges[ 3 ] : SV_TessFactor; \n\
  2485. float inside : SV_InsideTessFactor; \n\
  2486. float foo : FOO; \n\
  2487. }; \n"
  2488. "HSPerPatchData HSPerPatchFunc( InputPatch< PSSceneIn, 3 > points, \n\
  2489. OutputPatch<PSSceneIn, 3> outpoints) { \n\
  2490. HSPerPatchData d = (HSPerPatchData)0; \n\
  2491. d.edges[ 0 ] = points[0].tex.x + outpoints[0].tex.x; \n\
  2492. d.edges[ 1 ] = 1; \n\
  2493. d.edges[ 2 ] = 1; \n\
  2494. d.inside = 1; \n\
  2495. return d; \n\
  2496. } \n"
  2497. "[domain(\"tri\")] \n\
  2498. [partitioning(\"fractional_odd\")] \n\
  2499. [outputtopology(\"triangle_cw\")] \n\
  2500. [patchconstantfunc(\"HSPerPatchFunc\")] \n\
  2501. [outputcontrolpoints(3)] \n"
  2502. "void main(const uint id : SV_OutputControlPointID, \n\
  2503. const InputPatch< PSSceneIn, 3 > points ) { \n\
  2504. } \n"
  2505. , &pSource);
  2506. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2507. L"hs_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  2508. std::string failLog(VerifyOperationFailed(pResult));
  2509. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find(
  2510. "Signature element SV_Position, referred to by patch constant function, is not found in corresponding hull shader output."));
  2511. }
  2512. TEST_F(CompilerTest, SubobjectCodeGenErrors) {
  2513. struct SubobjectErrorTestCase {
  2514. const char *shaderText;
  2515. const char *expectedError;
  2516. };
  2517. SubobjectErrorTestCase testCases[] = {
  2518. { "GlobalRootSignature grs;", "1:1: error: subobject needs to be initialized" },
  2519. { "StateObjectConfig soc;", "1:1: error: subobject needs to be initialized" },
  2520. { "LocalRootSignature lrs;", "1:1: error: subobject needs to be initialized" },
  2521. { "SubobjectToExportsAssociation sea;", "1:1: error: subobject needs to be initialized" },
  2522. { "RaytracingShaderConfig rsc;", "1:1: error: subobject needs to be initialized" },
  2523. { "RaytracingPipelineConfig rpc;", "1:1: error: subobject needs to be initialized" },
  2524. { "RaytracingPipelineConfig1 rpc1;", "1:1: error: subobject needs to be initialized" },
  2525. { "TriangleHitGroup hitGt;", "1:1: error: subobject needs to be initialized" },
  2526. { "ProceduralPrimitiveHitGroup hitGt;", "1:1: error: subobject needs to be initialized" },
  2527. { "GlobalRootSignature grs2 = {\"\"};", "1:29: error: empty string not expected here" },
  2528. { "LocalRootSignature lrs2 = {\"\"};", "1:28: error: empty string not expected here" },
  2529. { "SubobjectToExportsAssociation sea2 = { \"\", \"x\" };", "1:40: error: empty string not expected here" },
  2530. { "string s; SubobjectToExportsAssociation sea4 = { \"x\", s };", "1:55: error: cannot convert to constant string" },
  2531. { "extern int v; RaytracingPipelineConfig rpc2 = { v + 16 };", "1:49: error: cannot convert to constant unsigned int" },
  2532. { "string s; TriangleHitGroup trHitGt2_8 = { s, \"foo\" };", "1:43: error: cannot convert to constant string" },
  2533. { "string s; ProceduralPrimitiveHitGroup ppHitGt2_8 = { s, \"\", s };", "1:54: error: cannot convert to constant string" },
  2534. { "ProceduralPrimitiveHitGroup ppHitGt2_9 = { \"a\", \"b\", \"\"};", "1:54: error: empty string not expected here" }
  2535. };
  2536. for (unsigned i = 0; i < _countof(testCases); i++) {
  2537. CComPtr<IDxcCompiler> pCompiler;
  2538. CComPtr<IDxcOperationResult> pResult;
  2539. CComPtr<IDxcBlobEncoding> pSource;
  2540. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2541. CreateBlobFromText(testCases[i].shaderText, &pSource);
  2542. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"", L"lib_6_4", nullptr, 0, nullptr, 0, nullptr, &pResult));
  2543. std::string failLog(VerifyOperationFailed(pResult));
  2544. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find(testCases[i].expectedError));
  2545. }
  2546. }
  2547. #ifdef _WIN32
  2548. TEST_F(CompilerTest, ManualFileCheckTest) {
  2549. #else
  2550. TEST_F(CompilerTest, DISABLED_ManualFileCheckTest) {
  2551. #endif
  2552. using namespace llvm;
  2553. using namespace WEX::TestExecution;
  2554. WEX::Common::String value;
  2555. VERIFY_SUCCEEDED(RuntimeParameters::TryGetValue(L"InputPath", value));
  2556. std::wstring path = value;
  2557. if (!llvm::sys::path::is_absolute(CW2A(path.c_str()).m_psz)) {
  2558. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  2559. }
  2560. bool isDirectory;
  2561. {
  2562. // Temporarily setup the filesystem for testing whether the path is a directory.
  2563. // If it is, CodeGenTestCheckBatchDir will create its own instance.
  2564. llvm::sys::fs::MSFileSystem *msfPtr;
  2565. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  2566. std::unique_ptr<llvm::sys::fs::MSFileSystem> msf(msfPtr);
  2567. llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  2568. IFTLLVM(pts.error_code());
  2569. isDirectory = llvm::sys::fs::is_directory(CW2A(path.c_str()).m_psz);
  2570. }
  2571. if (isDirectory) {
  2572. CodeGenTestCheckBatchDir(path, /*implicitDir*/ false);
  2573. } else {
  2574. CodeGenTestCheck(path.c_str(), /*implicitDir*/ false);
  2575. }
  2576. }
  2577. TEST_F(CompilerTest, CodeGenHashStability) {
  2578. CodeGenTestCheckBatchHash(L"");
  2579. }
  2580. TEST_F(CompilerTest, BatchD3DReflect) {
  2581. CodeGenTestCheckBatchDir(L"d3dreflect");
  2582. }
  2583. TEST_F(CompilerTest, BatchDxil) {
  2584. CodeGenTestCheckBatchDir(L"dxil");
  2585. }
  2586. TEST_F(CompilerTest, BatchHLSL) {
  2587. CodeGenTestCheckBatchDir(L"hlsl");
  2588. }
  2589. TEST_F(CompilerTest, BatchInfra) {
  2590. CodeGenTestCheckBatchDir(L"infra");
  2591. }
  2592. TEST_F(CompilerTest, BatchPasses) {
  2593. CodeGenTestCheckBatchDir(L"passes");
  2594. }
  2595. TEST_F(CompilerTest, BatchShaderTargets) {
  2596. CodeGenTestCheckBatchDir(L"shader_targets");
  2597. }
  2598. TEST_F(CompilerTest, BatchValidation) {
  2599. CodeGenTestCheckBatchDir(L"validation");
  2600. }
  2601. TEST_F(CompilerTest, BatchSamples) {
  2602. CodeGenTestCheckBatchDir(L"samples");
  2603. }