CompilerTest.cpp 115 KB

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