CompilerTest.cpp 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // CompilerTest.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides tests for the compiler API. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef UNICODE
  12. #define UNICODE
  13. #endif
  14. #include <memory>
  15. #include <vector>
  16. #include <string>
  17. #include <map>
  18. #include <cassert>
  19. #include <sstream>
  20. #include <algorithm>
  21. #include <cfloat>
  22. #include "dxc/DxilContainer/DxilContainer.h"
  23. #include "dxc/Support/WinIncludes.h"
  24. #include "dxc/dxcapi.h"
  25. #ifdef _WIN32
  26. #include <atlfile.h>
  27. #include "dia2.h"
  28. #endif
  29. #include "HLSLTestData.h"
  30. #include "HlslTestUtils.h"
  31. #include "DxcTestUtils.h"
  32. #include "llvm/Support/raw_os_ostream.h"
  33. #include "dxc/Support/Global.h"
  34. #include "dxc/Support/dxcapi.use.h"
  35. #include "dxc/Support/microcom.h"
  36. #include "dxc/Support/HLSLOptions.h"
  37. #include "dxc/Support/Unicode.h"
  38. #include <fstream>
  39. #include "llvm/Support/FileSystem.h"
  40. #include "llvm/Support/MSFileSystem.h"
  41. #include "llvm/Support/Path.h"
  42. #include "llvm/ADT/SmallString.h"
  43. #include "llvm/ADT/StringSwitch.h"
  44. using namespace std;
  45. using namespace hlsl_test;
  46. // Aligned to SymTagEnum.
  47. const char *SymTagEnumText[] =
  48. {
  49. "Null", // SymTagNull
  50. "Exe", // SymTagExe
  51. "Compiland", // SymTagCompiland
  52. "CompilandDetails", // SymTagCompilandDetails
  53. "CompilandEnv", // SymTagCompilandEnv
  54. "Function", // SymTagFunction
  55. "Block", // SymTagBlock
  56. "Data", // SymTagData
  57. "Annotation", // SymTagAnnotation
  58. "Label", // SymTagLabel
  59. "PublicSymbol", // SymTagPublicSymbol
  60. "UDT", // SymTagUDT
  61. "Enum", // SymTagEnum
  62. "FunctionType", // SymTagFunctionType
  63. "PointerType", // SymTagPointerType
  64. "ArrayType", // SymTagArrayType
  65. "BaseType", // SymTagBaseType
  66. "Typedef", // SymTagTypedef
  67. "BaseClass", // SymTagBaseClass
  68. "Friend", // SymTagFriend
  69. "FunctionArgType", // SymTagFunctionArgType
  70. "FuncDebugStart", // SymTagFuncDebugStart
  71. "FuncDebugEnd", // SymTagFuncDebugEnd
  72. "UsingNamespace", // SymTagUsingNamespace
  73. "VTableShape", // SymTagVTableShape
  74. "VTable", // SymTagVTable
  75. "Custom", // SymTagCustom
  76. "Thunk", // SymTagThunk
  77. "CustomType", // SymTagCustomType
  78. "ManagedType", // SymTagManagedType
  79. "Dimension", // SymTagDimension
  80. "CallSite", // SymTagCallSite
  81. "InlineSite", // SymTagInlineSite
  82. "BaseInterface", // SymTagBaseInterface
  83. "VectorType", // SymTagVectorType
  84. "MatrixType", // SymTagMatrixType
  85. "HLSLType", // SymTagHLSLType
  86. "Caller", // SymTagCaller
  87. "Callee", // SymTagCallee
  88. "Export", // SymTagExport
  89. "HeapAllocationSite", // SymTagHeapAllocationSite
  90. "CoffGroup", // SymTagCoffGroup
  91. };
  92. // Aligned to LocationType.
  93. const char *LocationTypeText[] =
  94. {
  95. "Null",
  96. "Static",
  97. "TLS",
  98. "RegRel",
  99. "ThisRel",
  100. "Enregistered",
  101. "BitField",
  102. "Slot",
  103. "IlRel",
  104. "MetaData",
  105. "Constant",
  106. };
  107. // Aligned to DataKind.
  108. const char *DataKindText[] =
  109. {
  110. "Unknown",
  111. "Local",
  112. "StaticLocal",
  113. "Param",
  114. "ObjectPtr",
  115. "FileStatic",
  116. "Global",
  117. "Member",
  118. "StaticMember",
  119. "Constant",
  120. };
  121. // Aligned to UdtKind.
  122. const char *UdtKindText[] =
  123. {
  124. "Struct",
  125. "Class",
  126. "Union",
  127. "Interface",
  128. };
  129. class TestIncludeHandler : public IDxcIncludeHandler {
  130. DXC_MICROCOM_REF_FIELD(m_dwRef)
  131. public:
  132. DXC_MICROCOM_ADDREF_RELEASE_IMPL(m_dwRef)
  133. dxc::DxcDllSupport &m_dllSupport;
  134. HRESULT m_defaultErrorCode = E_FAIL;
  135. TestIncludeHandler(dxc::DxcDllSupport &dllSupport) : m_dwRef(0), m_dllSupport(dllSupport), callIndex(0) { }
  136. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override {
  137. return DoBasicQueryInterface<IDxcIncludeHandler>(this, iid, ppvObject);
  138. }
  139. struct LoadSourceCallInfo {
  140. std::wstring Filename; // Filename as written in #include statement
  141. LoadSourceCallInfo(LPCWSTR pFilename) :
  142. Filename(pFilename) { }
  143. };
  144. std::vector<LoadSourceCallInfo> CallInfos;
  145. std::wstring GetAllFileNames() const {
  146. std::wstringstream s;
  147. for (size_t i = 0; i < CallInfos.size(); ++i) {
  148. s << CallInfos[i].Filename << ';';
  149. }
  150. return s.str();
  151. }
  152. struct LoadSourceCallResult {
  153. HRESULT hr;
  154. std::string source;
  155. UINT32 codePage;
  156. LoadSourceCallResult() : hr(E_FAIL), codePage(0) { }
  157. LoadSourceCallResult(const char *pSource, UINT32 codePage = CP_UTF8) : hr(S_OK), source(pSource), codePage(codePage) { }
  158. };
  159. std::vector<LoadSourceCallResult> CallResults;
  160. size_t callIndex;
  161. HRESULT STDMETHODCALLTYPE LoadSource(
  162. _In_ LPCWSTR pFilename, // Filename as written in #include statement
  163. _COM_Outptr_ IDxcBlob **ppIncludeSource // Resultant source object for included file
  164. ) override {
  165. CallInfos.push_back(LoadSourceCallInfo(pFilename));
  166. *ppIncludeSource = nullptr;
  167. if (callIndex >= CallResults.size()) {
  168. return m_defaultErrorCode;
  169. }
  170. if (FAILED(CallResults[callIndex].hr)) {
  171. return CallResults[callIndex++].hr;
  172. }
  173. MultiByteStringToBlob(m_dllSupport, CallResults[callIndex].source,
  174. CallResults[callIndex].codePage, ppIncludeSource);
  175. return CallResults[callIndex++].hr;
  176. }
  177. };
  178. #ifdef _WIN32
  179. class CompilerTest {
  180. #else
  181. class CompilerTest : public ::testing::Test {
  182. #endif
  183. public:
  184. BEGIN_TEST_CLASS(CompilerTest)
  185. TEST_CLASS_PROPERTY(L"Parallel", L"true")
  186. TEST_METHOD_PROPERTY(L"Priority", L"0")
  187. END_TEST_CLASS()
  188. TEST_CLASS_SETUP(InitSupport);
  189. TEST_METHOD(CompileWhenDebugThenDIPresent)
  190. TEST_METHOD(CompileDebugLines)
  191. TEST_METHOD(CompileWhenDefinesThenApplied)
  192. TEST_METHOD(CompileWhenDefinesManyThenApplied)
  193. TEST_METHOD(CompileWhenEmptyThenFails)
  194. TEST_METHOD(CompileWhenIncorrectThenFails)
  195. TEST_METHOD(CompileWhenWorksThenDisassembleWorks)
  196. TEST_METHOD(CompileWhenDebugWorksThenStripDebug)
  197. TEST_METHOD(CompileWhenWorksThenAddRemovePrivate)
  198. TEST_METHOD(CompileThenAddCustomDebugName)
  199. TEST_METHOD(CompileWithRootSignatureThenStripRootSignature)
  200. TEST_METHOD(CompileWhenIncludeThenLoadInvoked)
  201. TEST_METHOD(CompileWhenIncludeThenLoadUsed)
  202. TEST_METHOD(CompileWhenIncludeAbsoluteThenLoadAbsolute)
  203. TEST_METHOD(CompileWhenIncludeLocalThenLoadRelative)
  204. TEST_METHOD(CompileWhenIncludeSystemThenLoadNotRelative)
  205. TEST_METHOD(CompileWhenIncludeSystemMissingThenLoadAttempt)
  206. TEST_METHOD(CompileWhenIncludeFlagsThenIncludeUsed)
  207. TEST_METHOD(CompileWhenIncludeMissingThenFail)
  208. TEST_METHOD(CompileWhenIncludeHasPathThenOK)
  209. TEST_METHOD(CompileWhenIncludeEmptyThenOK)
  210. TEST_METHOD(CompileWhenODumpThenPassConfig)
  211. TEST_METHOD(CompileWhenODumpThenOptimizerMatch)
  212. TEST_METHOD(CompileWhenVdThenProducesDxilContainer)
  213. #if _ITERATOR_DEBUG_LEVEL==0
  214. // CompileWhenNoMemThenOOM can properly detect leaks only when debug iterators are disabled
  215. TEST_METHOD(CompileWhenNoMemThenOOM)
  216. #endif
  217. TEST_METHOD(CompileWhenShaderModelMismatchAttributeThenFail)
  218. TEST_METHOD(CompileBadHlslThenFail)
  219. TEST_METHOD(CompileLegacyShaderModelThenFail)
  220. TEST_METHOD(CompileWhenRecursiveAlbeitStaticTermThenFail)
  221. TEST_METHOD(CompileWhenRecursiveThenFail)
  222. TEST_METHOD(CompileHlsl2015ThenFail)
  223. TEST_METHOD(CompileHlsl2016ThenOK)
  224. TEST_METHOD(CompileHlsl2017ThenOK)
  225. TEST_METHOD(CompileHlsl2018ThenOK)
  226. TEST_METHOD(CompileHlsl2019ThenFail)
  227. TEST_METHOD(DiaLoadBadBitcodeThenFail)
  228. TEST_METHOD(DiaLoadDebugThenOK)
  229. TEST_METHOD(DiaTableIndexThenOK)
  230. TEST_METHOD(CodeGenPix)
  231. TEST_METHOD(CodeGenAttributeAtVertex)
  232. TEST_METHOD(CodeGenAttributeAtVertexNoOpt)
  233. TEST_METHOD(CodeGenBarycentrics)
  234. TEST_METHOD(CodeGenBarycentrics1)
  235. TEST_METHOD(CodeGenBarycentricsThreeSV)
  236. TEST_METHOD(CodeGenBitCast16Bits)
  237. TEST_METHOD(CodeGenCbuffer64Types)
  238. TEST_METHOD(CodeGenCbufferHalf)
  239. TEST_METHOD(CodeGenCbufferHalfStruct)
  240. TEST_METHOD(CodeGenCbufferInt16)
  241. TEST_METHOD(CodeGenCbufferInt16Struct)
  242. TEST_METHOD(CodeGenDataLayoutHalf)
  243. TEST_METHOD(CodeGenEnum3)
  244. TEST_METHOD(CodeGenExpandTrig)
  245. TEST_METHOD(CodeGenFloatingPointEnvironment)
  246. TEST_METHOD(CodeGenFixedWidthTypes)
  247. TEST_METHOD(CodeGenFixedWidthTypes16Bit)
  248. TEST_METHOD(CodeGenFunctionAttribute)
  249. TEST_METHOD(CodeGenInclude)
  250. TEST_METHOD(CodeGenInt16Op)
  251. TEST_METHOD(CodeGenInt16OpBits)
  252. TEST_METHOD(CodeGenLibCsEntry)
  253. TEST_METHOD(CodeGenLibCsEntry2)
  254. TEST_METHOD(CodeGenLibCsEntry3)
  255. TEST_METHOD(CodeGenLibEntries)
  256. TEST_METHOD(CodeGenLibEntries2)
  257. TEST_METHOD(CodeGenLibNoAlias)
  258. TEST_METHOD(CodeGenLibResource)
  259. TEST_METHOD(CodeGenLibUnusedFunc)
  260. TEST_METHOD(CodeGenMatIn1)
  261. TEST_METHOD(CodeGenMatIn2)
  262. TEST_METHOD(CodeGenMultiUAVLoad2)
  263. TEST_METHOD(CodeGenMultiUAVLoad4)
  264. TEST_METHOD(CodeGenMultiUAVLoad5)
  265. TEST_METHOD(CodeGenMultiUAVLoad6)
  266. TEST_METHOD(CodeGenMultiUAVLoad7)
  267. TEST_METHOD(CodeGenRaw_Buf2)
  268. TEST_METHOD(CodeGenRaw_Buf4)
  269. TEST_METHOD(CodeGenRaw_Buf5)
  270. TEST_METHOD(CodeGenSignaturePackingByWidth)
  271. TEST_METHOD(CodeGenStruct_Buf2)
  272. TEST_METHOD(CodeGenStruct_Buf3)
  273. TEST_METHOD(CodeGenStruct_Buf4)
  274. TEST_METHOD(CodeGenStruct_Buf5)
  275. TEST_METHOD(CodeGenStruct_Buf6)
  276. TEST_METHOD(CodeGenStruct_Buf_New_Layout)
  277. TEST_METHOD(CodeGenUav_Typed_Load_Store3)
  278. TEST_METHOD(CodeGenUint64_2)
  279. TEST_METHOD(CodeGenLiterals_Exact_Precision_Mod)
  280. TEST_METHOD(CodeGenTypedBufferHalf)
  281. TEST_METHOD(CodeGenRootSigProfile)
  282. TEST_METHOD(CodeGenRootSigProfile2)
  283. TEST_METHOD(CodeGenRootSigProfile5)
  284. TEST_METHOD(PreprocessWhenValidThenOK)
  285. TEST_METHOD(PreprocessWhenExpandTokenPastingOperandThenAccept)
  286. TEST_METHOD(WhenSigMismatchPCFunctionThenFail)
  287. TEST_METHOD(CodeGenDX11Sample)
  288. TEST_METHOD(CodeGenSamplesD12)
  289. TEST_METHOD(CodeGenDx12MiniEngine)
  290. TEST_METHOD(HoistConstantArray)
  291. TEST_METHOD(ViewID)
  292. TEST_METHOD(SubobjectCodeGenErrors)
  293. TEST_METHOD(ShaderCompatSuite)
  294. TEST_METHOD(Unroll)
  295. TEST_METHOD(DebugInfo)
  296. TEST_METHOD(QuickTest)
  297. TEST_METHOD(QuickLlTest)
  298. BEGIN_TEST_METHOD(ManualFileCheckTest)
  299. TEST_METHOD_PROPERTY(L"Ignore", L"true")
  300. END_TEST_METHOD()
  301. // Batch directories
  302. TEST_METHOD(CodeGenDeclarations)
  303. TEST_METHOD(CodeGenExpressions)
  304. TEST_METHOD(CodeGenPreprocessor)
  305. TEST_METHOD(CodeGenShaderStages)
  306. dxc::DxcDllSupport m_dllSupport;
  307. VersionSupportInfo m_ver;
  308. void CreateBlobPinned(_In_bytecount_(size) LPCVOID data, SIZE_T size,
  309. UINT32 codePage, _Outptr_ IDxcBlobEncoding **ppBlob) {
  310. CComPtr<IDxcLibrary> library;
  311. IFT(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  312. IFT(library->CreateBlobWithEncodingFromPinned(data, size, codePage,
  313. ppBlob));
  314. }
  315. void CreateBlobFromFile(LPCWSTR name, _Outptr_ IDxcBlobEncoding **ppBlob) {
  316. CComPtr<IDxcLibrary> library;
  317. IFT(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  318. const std::wstring path = hlsl_test::GetPathToHlslDataFile(name);
  319. IFT(library->CreateBlobFromFile(path.c_str(), nullptr, ppBlob));
  320. }
  321. void CreateBlobFromText(_In_z_ const char *pText,
  322. _Outptr_ IDxcBlobEncoding **ppBlob) {
  323. CreateBlobPinned(pText, strlen(pText), CP_UTF8, ppBlob);
  324. }
  325. HRESULT CreateCompiler(IDxcCompiler **ppResult) {
  326. return m_dllSupport.CreateInstance(CLSID_DxcCompiler, ppResult);
  327. }
  328. #ifdef _WIN32 // No ContainerBuilder support yet
  329. HRESULT CreateContainerBuilder(IDxcContainerBuilder **ppResult) {
  330. return m_dllSupport.CreateInstance(CLSID_DxcContainerBuilder, ppResult);
  331. }
  332. #endif
  333. template <typename T, typename TDefault, typename TIface>
  334. void WriteIfValue(TIface *pSymbol, std::wstringstream &o,
  335. TDefault defaultValue, LPCWSTR valueLabel,
  336. HRESULT (__stdcall TIface::*pFn)(T *)) {
  337. T value;
  338. HRESULT hr = (pSymbol->*(pFn))(&value);
  339. if (SUCCEEDED(hr) && value != defaultValue) {
  340. o << L", " << valueLabel << L": " << value;
  341. }
  342. }
  343. #ifdef _WIN32 // exclude dia stuff
  344. template <typename TIface>
  345. void WriteIfValue(TIface *pSymbol, std::wstringstream &o,
  346. LPCWSTR valueLabel, HRESULT(__stdcall TIface::*pFn)(BSTR *)) {
  347. CComBSTR value;
  348. HRESULT hr = (pSymbol->*(pFn))(&value);
  349. if (SUCCEEDED(hr) && value.Length()) {
  350. o << L", " << valueLabel << L": " << (LPCWSTR)value;
  351. }
  352. }
  353. template <typename TIface>
  354. void WriteIfValue(TIface *pSymbol, std::wstringstream &o,
  355. LPCWSTR valueLabel, HRESULT(__stdcall TIface::*pFn)(VARIANT *)) {
  356. CComVariant value;
  357. HRESULT hr = (pSymbol->*(pFn))(&value);
  358. if (SUCCEEDED(hr) && value.vt != VT_NULL && value.vt != VT_EMPTY) {
  359. if (SUCCEEDED(value.ChangeType(VT_BSTR))) {
  360. o << L", " << valueLabel << L": " << (LPCWSTR)value.bstrVal;
  361. }
  362. }
  363. }
  364. template <typename TIface>
  365. void WriteIfValue(TIface *pSymbol, std::wstringstream &o,
  366. LPCWSTR valueLabel, HRESULT(__stdcall TIface::*pFn)(IDiaSymbol **)) {
  367. CComPtr<IDiaSymbol> value;
  368. HRESULT hr = (pSymbol->*(pFn))(&value);
  369. if (SUCCEEDED(hr) && value.p != nullptr) {
  370. DWORD symId;
  371. value->get_symIndexId(&symId);
  372. o << L", " << valueLabel << L": id=" << symId;
  373. }
  374. }
  375. std::wstring GetDebugInfoAsText(_In_ IDiaDataSource* pDataSource) {
  376. CComPtr<IDiaSession> pSession;
  377. CComPtr<IDiaTable> pTable;
  378. CComPtr<IDiaEnumTables> pEnumTables;
  379. std::wstringstream o;
  380. VERIFY_SUCCEEDED(pDataSource->openSession(&pSession));
  381. VERIFY_SUCCEEDED(pSession->getEnumTables(&pEnumTables));
  382. LONG count;
  383. VERIFY_SUCCEEDED(pEnumTables->get_Count(&count));
  384. for (LONG i = 0; i < count; ++i) {
  385. pTable.Release();
  386. ULONG fetched;
  387. VERIFY_SUCCEEDED(pEnumTables->Next(1, &pTable, &fetched));
  388. VERIFY_ARE_EQUAL(fetched, 1);
  389. CComBSTR tableName;
  390. VERIFY_SUCCEEDED(pTable->get_name(&tableName));
  391. o << L"Table: " << (LPWSTR)tableName << std::endl;
  392. LONG rowCount;
  393. IFT(pTable->get_Count(&rowCount));
  394. o << L" Row count: " << rowCount << std::endl;
  395. for (LONG rowIndex = 0; rowIndex < rowCount; ++rowIndex) {
  396. CComPtr<IUnknown> item;
  397. o << L'#' << rowIndex;
  398. IFT(pTable->Item(rowIndex, &item));
  399. CComPtr<IDiaSymbol> pSymbol;
  400. if (SUCCEEDED(item.QueryInterface(&pSymbol))) {
  401. DWORD symTag;
  402. DWORD dataKind;
  403. DWORD locationType;
  404. DWORD registerId;
  405. pSymbol->get_symTag(&symTag);
  406. pSymbol->get_dataKind(&dataKind);
  407. pSymbol->get_locationType(&locationType);
  408. pSymbol->get_registerId(&registerId);
  409. //pSymbol->get_value(&value);
  410. WriteIfValue(pSymbol.p, o, 0, L"symIndexId", &IDiaSymbol::get_symIndexId);
  411. o << L", " << SymTagEnumText[symTag];
  412. if (dataKind != 0) o << L", " << DataKindText[dataKind];
  413. WriteIfValue(pSymbol.p, o, L"name", &IDiaSymbol::get_name);
  414. WriteIfValue(pSymbol.p, o, L"lexicalParent", &IDiaSymbol::get_lexicalParent);
  415. WriteIfValue(pSymbol.p, o, L"type", &IDiaSymbol::get_type);
  416. WriteIfValue(pSymbol.p, o, 0, L"slot", &IDiaSymbol::get_slot);
  417. WriteIfValue(pSymbol.p, o, 0, L"platform", &IDiaSymbol::get_platform);
  418. WriteIfValue(pSymbol.p, o, 0, L"language", &IDiaSymbol::get_language);
  419. WriteIfValue(pSymbol.p, o, 0, L"frontEndMajor", &IDiaSymbol::get_frontEndMajor);
  420. WriteIfValue(pSymbol.p, o, 0, L"frontEndMinor", &IDiaSymbol::get_frontEndMinor);
  421. WriteIfValue(pSymbol.p, o, 0, L"token", &IDiaSymbol::get_token);
  422. WriteIfValue(pSymbol.p, o, L"value", &IDiaSymbol::get_value);
  423. WriteIfValue(pSymbol.p, o, 0, L"code", &IDiaSymbol::get_code);
  424. WriteIfValue(pSymbol.p, o, 0, L"function", &IDiaSymbol::get_function);
  425. WriteIfValue(pSymbol.p, o, 0, L"udtKind", &IDiaSymbol::get_udtKind);
  426. WriteIfValue(pSymbol.p, o, 0, L"hasDebugInfo", &IDiaSymbol::get_hasDebugInfo);
  427. WriteIfValue(pSymbol.p, o, L"compilerName", &IDiaSymbol::get_compilerName);
  428. WriteIfValue(pSymbol.p, o, 0, L"isLocationControlFlowDependent", &IDiaSymbol::get_isLocationControlFlowDependent);
  429. WriteIfValue(pSymbol.p, o, 0, L"numberOfRows", &IDiaSymbol::get_numberOfRows);
  430. WriteIfValue(pSymbol.p, o, 0, L"numberOfColumns", &IDiaSymbol::get_numberOfColumns);
  431. WriteIfValue(pSymbol.p, o, 0, L"length", &IDiaSymbol::get_length);
  432. WriteIfValue(pSymbol.p, o, 0, L"isMatrixRowMajor", &IDiaSymbol::get_isMatrixRowMajor);
  433. WriteIfValue(pSymbol.p, o, 0, L"builtInKind", &IDiaSymbol::get_builtInKind);
  434. WriteIfValue(pSymbol.p, o, 0, L"textureSlot", &IDiaSymbol::get_textureSlot);
  435. WriteIfValue(pSymbol.p, o, 0, L"memorySpaceKind", &IDiaSymbol::get_memorySpaceKind);
  436. WriteIfValue(pSymbol.p, o, 0, L"isHLSLData", &IDiaSymbol::get_isHLSLData);
  437. }
  438. CComPtr<IDiaSourceFile> pSourceFile;
  439. if (SUCCEEDED(item.QueryInterface(&pSourceFile))) {
  440. WriteIfValue(pSourceFile.p, o, 0, L"uniqueId", &IDiaSourceFile::get_uniqueId);
  441. WriteIfValue(pSourceFile.p, o, L"fileName", &IDiaSourceFile::get_fileName);
  442. }
  443. CComPtr<IDiaLineNumber> pLineNumber;
  444. if (SUCCEEDED(item.QueryInterface(&pLineNumber))) {
  445. WriteIfValue(pLineNumber.p, o, L"compiland", &IDiaLineNumber::get_compiland);
  446. //WriteIfValue(pLineNumber.p, o, L"sourceFile", &IDiaLineNumber::get_sourceFile);
  447. WriteIfValue(pLineNumber.p, o, 0, L"lineNumber", &IDiaLineNumber::get_lineNumber);
  448. WriteIfValue(pLineNumber.p, o, 0, L"lineNumberEnd", &IDiaLineNumber::get_lineNumberEnd);
  449. WriteIfValue(pLineNumber.p, o, 0, L"columnNumber", &IDiaLineNumber::get_columnNumber);
  450. WriteIfValue(pLineNumber.p, o, 0, L"columnNumberEnd", &IDiaLineNumber::get_columnNumberEnd);
  451. WriteIfValue(pLineNumber.p, o, 0, L"addressSection", &IDiaLineNumber::get_addressSection);
  452. WriteIfValue(pLineNumber.p, o, 0, L"addressOffset", &IDiaLineNumber::get_addressOffset);
  453. WriteIfValue(pLineNumber.p, o, 0, L"relativeVirtualAddress", &IDiaLineNumber::get_relativeVirtualAddress);
  454. WriteIfValue(pLineNumber.p, o, 0, L"virtualAddress", &IDiaLineNumber::get_virtualAddress);
  455. WriteIfValue(pLineNumber.p, o, 0, L"length", &IDiaLineNumber::get_length);
  456. WriteIfValue(pLineNumber.p, o, 0, L"sourceFileId", &IDiaLineNumber::get_sourceFileId);
  457. WriteIfValue(pLineNumber.p, o, 0, L"statement", &IDiaLineNumber::get_statement);
  458. WriteIfValue(pLineNumber.p, o, 0, L"compilandId", &IDiaLineNumber::get_compilandId);
  459. }
  460. CComPtr<IDiaSectionContrib> pSectionContrib;
  461. if (SUCCEEDED(item.QueryInterface(&pSectionContrib))) {
  462. WriteIfValue(pSectionContrib.p, o, L"compiland", &IDiaSectionContrib::get_compiland);
  463. WriteIfValue(pSectionContrib.p, o, 0, L"addressSection", &IDiaSectionContrib::get_addressSection);
  464. WriteIfValue(pSectionContrib.p, o, 0, L"addressOffset", &IDiaSectionContrib::get_addressOffset);
  465. WriteIfValue(pSectionContrib.p, o, 0, L"relativeVirtualAddress", &IDiaSectionContrib::get_relativeVirtualAddress);
  466. WriteIfValue(pSectionContrib.p, o, 0, L"virtualAddress", &IDiaSectionContrib::get_virtualAddress);
  467. WriteIfValue(pSectionContrib.p, o, 0, L"length", &IDiaSectionContrib::get_length);
  468. WriteIfValue(pSectionContrib.p, o, 0, L"notPaged", &IDiaSectionContrib::get_notPaged);
  469. WriteIfValue(pSectionContrib.p, o, 0, L"code", &IDiaSectionContrib::get_code);
  470. WriteIfValue(pSectionContrib.p, o, 0, L"initializedData", &IDiaSectionContrib::get_initializedData);
  471. WriteIfValue(pSectionContrib.p, o, 0, L"uninitializedData", &IDiaSectionContrib::get_uninitializedData);
  472. WriteIfValue(pSectionContrib.p, o, 0, L"remove", &IDiaSectionContrib::get_remove);
  473. WriteIfValue(pSectionContrib.p, o, 0, L"comdat", &IDiaSectionContrib::get_comdat);
  474. WriteIfValue(pSectionContrib.p, o, 0, L"discardable", &IDiaSectionContrib::get_discardable);
  475. WriteIfValue(pSectionContrib.p, o, 0, L"notCached", &IDiaSectionContrib::get_notCached);
  476. WriteIfValue(pSectionContrib.p, o, 0, L"share", &IDiaSectionContrib::get_share);
  477. WriteIfValue(pSectionContrib.p, o, 0, L"execute", &IDiaSectionContrib::get_execute);
  478. WriteIfValue(pSectionContrib.p, o, 0, L"read", &IDiaSectionContrib::get_read);
  479. WriteIfValue(pSectionContrib.p, o, 0, L"write", &IDiaSectionContrib::get_write);
  480. WriteIfValue(pSectionContrib.p, o, 0, L"dataCrc", &IDiaSectionContrib::get_dataCrc);
  481. WriteIfValue(pSectionContrib.p, o, 0, L"relocationsCrc", &IDiaSectionContrib::get_relocationsCrc);
  482. WriteIfValue(pSectionContrib.p, o, 0, L"compilandId", &IDiaSectionContrib::get_compilandId);
  483. }
  484. CComPtr<IDiaSegment> pSegment;
  485. if (SUCCEEDED(item.QueryInterface(&pSegment))) {
  486. WriteIfValue(pSegment.p, o, 0, L"frame", &IDiaSegment::get_frame);
  487. WriteIfValue(pSegment.p, o, 0, L"offset", &IDiaSegment::get_offset);
  488. WriteIfValue(pSegment.p, o, 0, L"length", &IDiaSegment::get_length);
  489. WriteIfValue(pSegment.p, o, 0, L"read", &IDiaSegment::get_read);
  490. WriteIfValue(pSegment.p, o, 0, L"write", &IDiaSegment::get_write);
  491. WriteIfValue(pSegment.p, o, 0, L"execute", &IDiaSegment::get_execute);
  492. WriteIfValue(pSegment.p, o, 0, L"addressSection", &IDiaSegment::get_addressSection);
  493. WriteIfValue(pSegment.p, o, 0, L"relativeVirtualAddress", &IDiaSegment::get_relativeVirtualAddress);
  494. WriteIfValue(pSegment.p, o, 0, L"virtualAddress", &IDiaSegment::get_virtualAddress);
  495. }
  496. CComPtr<IDiaInjectedSource> pInjectedSource;
  497. if (SUCCEEDED(item.QueryInterface(&pInjectedSource))) {
  498. WriteIfValue(pInjectedSource.p, o, 0, L"crc", &IDiaInjectedSource::get_crc);
  499. WriteIfValue(pInjectedSource.p, o, 0, L"length", &IDiaInjectedSource::get_length);
  500. WriteIfValue(pInjectedSource.p, o, L"filename", &IDiaInjectedSource::get_filename);
  501. WriteIfValue(pInjectedSource.p, o, L"objectFilename", &IDiaInjectedSource::get_objectFilename);
  502. WriteIfValue(pInjectedSource.p, o, L"virtualFilename", &IDiaInjectedSource::get_virtualFilename);
  503. WriteIfValue(pInjectedSource.p, o, 0, L"sourceCompression", &IDiaInjectedSource::get_sourceCompression);
  504. // get_source is also available
  505. }
  506. CComPtr<IDiaFrameData> pFrameData;
  507. if (SUCCEEDED(item.QueryInterface(&pFrameData))) {
  508. }
  509. o << std::endl;
  510. }
  511. }
  512. return o.str();
  513. }
  514. std::wstring GetDebugFileContent(_In_ IDiaDataSource *pDataSource) {
  515. CComPtr<IDiaSession> pSession;
  516. CComPtr<IDiaTable> pTable;
  517. CComPtr<IDiaTable> pSourcesTable;
  518. CComPtr<IDiaEnumTables> pEnumTables;
  519. std::wstringstream o;
  520. VERIFY_SUCCEEDED(pDataSource->openSession(&pSession));
  521. VERIFY_SUCCEEDED(pSession->getEnumTables(&pEnumTables));
  522. ULONG fetched = 0;
  523. while (pEnumTables->Next(1, &pTable, &fetched) == S_OK && fetched == 1) {
  524. CComBSTR name;
  525. IFT(pTable->get_name(&name));
  526. if (wcscmp(name, L"SourceFiles") == 0) {
  527. pSourcesTable = pTable.Detach();
  528. continue;
  529. }
  530. pTable.Release();
  531. }
  532. if (!pSourcesTable) {
  533. return L"cannot find source";
  534. }
  535. // Get source file contents.
  536. // NOTE: "SourceFiles" has the root file first while "InjectedSources" is in
  537. // alphabetical order.
  538. // It is important to keep the root file first for recompilation, so
  539. // iterate "SourceFiles" and look up the corresponding injected
  540. // source.
  541. LONG count;
  542. IFT(pSourcesTable->get_Count(&count));
  543. CComPtr<IDiaSourceFile> pSourceFile;
  544. CComBSTR pName;
  545. CComPtr<IUnknown> pSymbolUnk;
  546. CComPtr<IDiaEnumInjectedSources> pEnumInjectedSources;
  547. CComPtr<IDiaInjectedSource> pInjectedSource;
  548. std::wstring sourceText, sourceFilename;
  549. while (SUCCEEDED(pSourcesTable->Next(1, &pSymbolUnk, &fetched)) &&
  550. fetched == 1) {
  551. sourceText = sourceFilename = L"";
  552. IFT(pSymbolUnk->QueryInterface(&pSourceFile));
  553. IFT(pSourceFile->get_fileName(&pName));
  554. IFT(pSession->findInjectedSource(pName, &pEnumInjectedSources));
  555. if (SUCCEEDED(pEnumInjectedSources->get_Count(&count)) && count == 1) {
  556. IFT(pEnumInjectedSources->Item(0, &pInjectedSource));
  557. DWORD cbData = 0;
  558. std::string tempString;
  559. CComBSTR bstr;
  560. IFT(pInjectedSource->get_filename(&bstr));
  561. IFT(pInjectedSource->get_source(0, &cbData, nullptr));
  562. tempString.resize(cbData);
  563. IFT(pInjectedSource->get_source(
  564. cbData, &cbData, reinterpret_cast<BYTE *>(&tempString[0])));
  565. CA2W tempWString(tempString.data());
  566. o << tempWString.m_psz;
  567. }
  568. pSymbolUnk.Release();
  569. }
  570. return o.str();
  571. }
  572. struct LineNumber { DWORD line; DWORD rva; };
  573. std::vector<LineNumber> ReadLineNumbers(IDiaEnumLineNumbers *pEnumLineNumbers) {
  574. std::vector<LineNumber> lines;
  575. CComPtr<IDiaLineNumber> pLineNumber;
  576. DWORD lineCount;
  577. while (SUCCEEDED(pEnumLineNumbers->Next(1, &pLineNumber, &lineCount)) && lineCount == 1)
  578. {
  579. DWORD line;
  580. DWORD rva;
  581. VERIFY_SUCCEEDED(pLineNumber->get_lineNumber(&line));
  582. VERIFY_SUCCEEDED(pLineNumber->get_relativeVirtualAddress(&rva));
  583. lines.push_back({ line, rva });
  584. pLineNumber.Release();
  585. }
  586. return lines;
  587. }
  588. #endif // _WIN32 - exclude dia stuff
  589. std::string GetOption(std::string &cmd, char *opt) {
  590. std::string option = cmd.substr(cmd.find(opt));
  591. option = option.substr(option.find_first_of(' '));
  592. option = option.substr(option.find_first_not_of(' '));
  593. return option.substr(0, option.find_first_of(' '));
  594. }
  595. void CodeGenTest(std::wstring name) {
  596. CComPtr<IDxcCompiler> pCompiler;
  597. CComPtr<IDxcOperationResult> pResult;
  598. CComPtr<IDxcBlobEncoding> pSource;
  599. name.insert(0, L"..\\CodeGenHLSL\\");
  600. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  601. CreateBlobFromFile(name.c_str(), &pSource);
  602. std::string cmdLine = GetFirstLine(name.c_str());
  603. llvm::StringRef argsRef = cmdLine;
  604. llvm::SmallVector<llvm::StringRef, 8> splitArgs;
  605. argsRef.split(splitArgs, " ");
  606. hlsl::options::MainArgs argStrings(splitArgs);
  607. std::string errorString;
  608. llvm::raw_string_ostream errorStream(errorString);
  609. hlsl::options::DxcOpts opts;
  610. IFT(ReadDxcOpts(hlsl::options::getHlslOptTable(), /*flagsToInclude*/ 0,
  611. argStrings, opts, errorStream));
  612. std::wstring entry =
  613. Unicode::UTF8ToUTF16StringOrThrow(opts.EntryPoint.str().c_str());
  614. std::wstring profile =
  615. Unicode::UTF8ToUTF16StringOrThrow(opts.TargetProfile.str().c_str());
  616. std::vector<std::wstring> argLists;
  617. CopyArgsToWStrings(opts.Args, hlsl::options::CoreOption, argLists);
  618. std::vector<LPCWSTR> args;
  619. args.reserve(argLists.size());
  620. for (const std::wstring &a : argLists)
  621. args.push_back(a.data());
  622. VERIFY_SUCCEEDED(pCompiler->Compile(
  623. pSource, name.c_str(), entry.c_str(), profile.c_str(), args.data(), args.size(),
  624. opts.Defines.data(), opts.Defines.size(), nullptr, &pResult));
  625. VERIFY_IS_NOT_NULL(pResult, L"Failed to compile - pResult NULL");
  626. HRESULT result;
  627. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  628. if (FAILED(result)) {
  629. CComPtr<IDxcBlobEncoding> pErr;
  630. IFT(pResult->GetErrorBuffer(&pErr));
  631. std::string errString(BlobToUtf8(pErr));
  632. CA2W errStringW(errString.c_str(), CP_UTF8);
  633. WEX::Logging::Log::Comment(L"Failed to compile - errors follow");
  634. WEX::Logging::Log::Comment(errStringW);
  635. }
  636. VERIFY_SUCCEEDED(result);
  637. CComPtr<IDxcBlob> pProgram;
  638. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  639. if (opts.IsRootSignatureProfile())
  640. return;
  641. CComPtr<IDxcBlobEncoding> pDisassembleBlob;
  642. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembleBlob));
  643. std::string disassembleString(BlobToUtf8(pDisassembleBlob));
  644. VERIFY_ARE_NOT_EQUAL(0U, disassembleString.size());
  645. }
  646. void CodeGenTestCheckFullPath(LPCWSTR fullPath) {
  647. FileRunTestResult t = FileRunTestResult::RunFromFileCommands(fullPath);
  648. if (t.RunResult != 0) {
  649. CA2W commentWide(t.ErrorMessage.c_str(), CP_UTF8);
  650. WEX::Logging::Log::Comment(commentWide);
  651. WEX::Logging::Log::Error(L"Run result is not zero");
  652. }
  653. }
  654. void CodeGenTestCheck(LPCWSTR name, bool implicitDir = true) {
  655. std::wstring path = name;
  656. if (implicitDir) {
  657. path.insert(0, L"..\\CodeGenHLSL\\");
  658. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  659. }
  660. CodeGenTestCheckFullPath(path.c_str());
  661. }
  662. void CodeGenTestCheckBatchDir(std::wstring suitePath, bool implicitDir = true) {
  663. using namespace llvm;
  664. using namespace WEX::TestExecution;
  665. if (implicitDir) suitePath.insert(0, L"..\\CodeGenHLSL\\");
  666. ::llvm::sys::fs::MSFileSystem *msfPtr;
  667. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  668. std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr);
  669. ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  670. IFTLLVM(pts.error_code());
  671. CW2A pUtf8Filename(suitePath.c_str());
  672. if (!llvm::sys::path::is_absolute(pUtf8Filename.m_psz)) {
  673. suitePath = hlsl_test::GetPathToHlslDataFile(suitePath.c_str());
  674. }
  675. CW2A utf8SuitePath(suitePath.c_str());
  676. unsigned numTestsRun = 0;
  677. std::error_code EC;
  678. llvm::SmallString<128> DirNative;
  679. llvm::sys::path::native(utf8SuitePath.m_psz, DirNative);
  680. for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC), DirEnd;
  681. Dir != DirEnd && !EC; Dir.increment(EC)) {
  682. // Check whether this entry has an extension typically associated with
  683. // headers.
  684. if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
  685. .Cases(".hlsl", ".ll", true).Default(false))
  686. continue;
  687. StringRef filename = Dir->path();
  688. CA2W wRelPath(filename.data());
  689. WEX::Logging::Log::StartGroup(wRelPath);
  690. CodeGenTestCheck(wRelPath, /*implicitDir*/ false);
  691. WEX::Logging::Log::EndGroup(wRelPath);
  692. numTestsRun++;
  693. }
  694. VERIFY_IS_GREATER_THAN(numTestsRun, (unsigned)0, L"No test files found in batch directory.");
  695. }
  696. std::string VerifyCompileFailed(LPCSTR pText, LPCWSTR pTargetProfile, LPCSTR pErrorMsg) {
  697. return VerifyCompileFailed(pText, pTargetProfile, pErrorMsg, L"main");
  698. }
  699. std::string VerifyCompileFailed(LPCSTR pText, LPCWSTR pTargetProfile, LPCSTR pErrorMsg, LPCWSTR pEntryPoint) {
  700. CComPtr<IDxcCompiler> pCompiler;
  701. CComPtr<IDxcOperationResult> pResult;
  702. CComPtr<IDxcBlobEncoding> pSource;
  703. CComPtr<IDxcBlobEncoding> pErrors;
  704. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  705. CreateBlobFromText(pText, &pSource);
  706. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  707. pTargetProfile, nullptr, 0, nullptr, 0, nullptr, &pResult));
  708. HRESULT status;
  709. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  710. VERIFY_FAILED(status);
  711. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  712. if (pErrorMsg && *pErrorMsg) {
  713. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  714. }
  715. return BlobToUtf8(pErrors);
  716. }
  717. void VerifyOperationSucceeded(IDxcOperationResult *pResult) {
  718. HRESULT result;
  719. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  720. if (FAILED(result)) {
  721. CComPtr<IDxcBlobEncoding> pErrors;
  722. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  723. CA2W errorsWide(BlobToUtf8(pErrors).c_str(), CP_UTF8);
  724. WEX::Logging::Log::Comment(errorsWide);
  725. }
  726. VERIFY_SUCCEEDED(result);
  727. }
  728. std::string VerifyOperationFailed(IDxcOperationResult *pResult) {
  729. HRESULT result;
  730. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  731. VERIFY_FAILED(result);
  732. CComPtr<IDxcBlobEncoding> pErrors;
  733. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  734. return BlobToUtf8(pErrors);
  735. }
  736. #ifdef _WIN32 // - exclude dia stuff
  737. HRESULT CreateDiaSourceForCompile(const char *hlsl, IDiaDataSource **ppDiaSource)
  738. {
  739. if (!ppDiaSource)
  740. return E_POINTER;
  741. CComPtr<IDxcCompiler> pCompiler;
  742. CComPtr<IDxcOperationResult> pResult;
  743. CComPtr<IDxcBlobEncoding> pSource;
  744. CComPtr<IDxcBlob> pProgram;
  745. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  746. CreateBlobFromText(hlsl, &pSource);
  747. LPCWSTR args[] = { L"/Zi" };
  748. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  749. L"ps_6_0", args, _countof(args), nullptr, 0, nullptr, &pResult));
  750. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  751. // Disassemble the compiled (stripped) program.
  752. {
  753. CComPtr<IDxcBlobEncoding> pDisassembly;
  754. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembly));
  755. std::string disText = BlobToUtf8(pDisassembly);
  756. CA2W disTextW(disText.c_str(), CP_UTF8);
  757. //WEX::Logging::Log::Comment(disTextW);
  758. }
  759. // CONSIDER: have the dia data source look for the part if passed a whole container.
  760. CComPtr<IDiaDataSource> pDiaSource;
  761. CComPtr<IStream> pProgramStream;
  762. CComPtr<IDxcLibrary> pLib;
  763. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  764. const hlsl::DxilContainerHeader *pContainer = hlsl::IsDxilContainerLike(
  765. pProgram->GetBufferPointer(), pProgram->GetBufferSize());
  766. VERIFY_IS_NOT_NULL(pContainer);
  767. hlsl::DxilPartIterator partIter =
  768. std::find_if(hlsl::begin(pContainer), hlsl::end(pContainer),
  769. hlsl::DxilPartIsType(hlsl::DFCC_ShaderDebugInfoDXIL));
  770. const hlsl::DxilProgramHeader *pProgramHeader =
  771. (const hlsl::DxilProgramHeader *)hlsl::GetDxilPartData(*partIter);
  772. uint32_t bitcodeLength;
  773. const char *pBitcode;
  774. CComPtr<IDxcBlob> pProgramPdb;
  775. hlsl::GetDxilProgramBitcode(pProgramHeader, &pBitcode, &bitcodeLength);
  776. VERIFY_SUCCEEDED(pLib->CreateBlobFromBlob(
  777. pProgram, pBitcode - (char *)pProgram->GetBufferPointer(), bitcodeLength,
  778. &pProgramPdb));
  779. // Disassemble the program with debug information.
  780. {
  781. CComPtr<IDxcBlobEncoding> pDbgDisassembly;
  782. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgramPdb, &pDbgDisassembly));
  783. std::string disText = BlobToUtf8(pDbgDisassembly);
  784. CA2W disTextW(disText.c_str(), CP_UTF8);
  785. //WEX::Logging::Log::Comment(disTextW);
  786. }
  787. // Create a short text dump of debug information.
  788. VERIFY_SUCCEEDED(pLib->CreateStreamFromBlobReadOnly(pProgramPdb, &pProgramStream));
  789. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcDiaDataSource, &pDiaSource));
  790. VERIFY_SUCCEEDED(pDiaSource->loadDataFromIStream(pProgramStream));
  791. *ppDiaSource = pDiaSource.Detach();
  792. return S_OK;
  793. }
  794. #endif // _WIN32 - exclude dia stuff
  795. };
  796. // Useful for debugging.
  797. #if SUPPORT_FXC_PDB
  798. #include <d3dcompiler.h>
  799. #pragma comment(lib, "d3dcompiler.lib")
  800. HRESULT GetBlobPdb(IDxcBlob *pBlob, IDxcBlob **ppDebugInfo) {
  801. return D3DGetBlobPart(pBlob->GetBufferPointer(), pBlob->GetBufferSize(),
  802. D3D_BLOB_PDB, 0, (ID3DBlob **)ppDebugInfo);
  803. }
  804. std::string FourCCStr(uint32_t val) {
  805. std::stringstream o;
  806. char c[5];
  807. c[0] = val & 0xFF;
  808. c[1] = (val & 0xFF00) >> 8;
  809. c[2] = (val & 0xFF0000) >> 16;
  810. c[3] = (val & 0xFF000000) >> 24;
  811. c[4] = '\0';
  812. o << c << " (" << std::hex << val << std::dec << ")";
  813. return o.str();
  814. }
  815. std::string DumpParts(IDxcBlob *pBlob) {
  816. std::stringstream o;
  817. hlsl::DxilContainerHeader *pContainer = (hlsl::DxilContainerHeader *)pBlob->GetBufferPointer();
  818. o << "Container:" << std::endl
  819. << " Size: " << pContainer->ContainerSizeInBytes << std::endl
  820. << " FourCC: " << FourCCStr(pContainer->HeaderFourCC) << std::endl
  821. << " Part count: " << pContainer->PartCount << std::endl;
  822. for (uint32_t i = 0; i < pContainer->PartCount; ++i) {
  823. hlsl::DxilPartHeader *pPart = hlsl::GetDxilContainerPart(pContainer, i);
  824. o << "Part " << i << std::endl
  825. << " FourCC: " << FourCCStr(pPart->PartFourCC) << std::endl
  826. << " Size: " << pPart->PartSize << std::endl;
  827. }
  828. return o.str();
  829. }
  830. HRESULT CreateDiaSourceFromDxbcBlob(IDxcLibrary *pLib, IDxcBlob *pDxbcBlob,
  831. IDiaDataSource **ppDiaSource) {
  832. HRESULT hr = S_OK;
  833. CComPtr<IDxcBlob> pdbBlob;
  834. CComPtr<IStream> pPdbStream;
  835. CComPtr<IDiaDataSource> pDiaSource;
  836. IFR(GetBlobPdb(pDxbcBlob, &pdbBlob));
  837. IFR(pLib->CreateStreamFromBlobReadOnly(pdbBlob, &pPdbStream));
  838. IFR(CoCreateInstance(CLSID_DiaSource, NULL, CLSCTX_INPROC_SERVER,
  839. __uuidof(IDiaDataSource), (void **)&pDiaSource));
  840. IFR(pDiaSource->loadDataFromIStream(pPdbStream));
  841. *ppDiaSource = pDiaSource.Detach();
  842. return hr;
  843. }
  844. #endif
  845. bool CompilerTest::InitSupport() {
  846. if (!m_dllSupport.IsEnabled()) {
  847. VERIFY_SUCCEEDED(m_dllSupport.Initialize());
  848. m_ver.Initialize(m_dllSupport);
  849. }
  850. return true;
  851. }
  852. #if _WIN32 // - exclude dia stuff
  853. TEST_F(CompilerTest, CompileWhenDebugThenDIPresent) {
  854. // BUG: the first test written was of this form:
  855. // float4 local = 0; return local;
  856. //
  857. // However we get no numbers because of the _wrapper form
  858. // that exports the zero initialization from main into
  859. // a global can't be attributed to any particular location
  860. // within main, and everything in main is eventually folded away.
  861. //
  862. // Making the function do a bit more work by calling an intrinsic
  863. // helps this case.
  864. CComPtr<IDiaDataSource> pDiaSource;
  865. VERIFY_SUCCEEDED(CreateDiaSourceForCompile(
  866. "float4 main(float4 pos : SV_Position) : SV_Target {\r\n"
  867. " float4 local = abs(pos);\r\n"
  868. " return local;\r\n"
  869. "}", &pDiaSource));
  870. std::wstring diaDump = GetDebugInfoAsText(pDiaSource).c_str();
  871. //WEX::Logging::Log::Comment(GetDebugInfoAsText(pDiaSource).c_str());
  872. // Very basic tests - we have basic symbols, line numbers, and files with sources.
  873. VERIFY_IS_NOT_NULL(wcsstr(diaDump.c_str(), L"symIndexId: 5, CompilandEnv, name: hlslTarget, value: ps_6_0"));
  874. VERIFY_IS_NOT_NULL(wcsstr(diaDump.c_str(), L"lineNumber: 2"));
  875. VERIFY_IS_NOT_NULL(wcsstr(diaDump.c_str(), L"length: 99, filename: source.hlsl"));
  876. std::wstring diaFileContent = GetDebugFileContent(pDiaSource).c_str();
  877. VERIFY_IS_NOT_NULL(wcsstr(diaFileContent.c_str(), L"loat4 main(float4 pos : SV_Position) : SV_Target"));
  878. #if SUPPORT_FXC_PDB
  879. // Now, fake it by loading from a .pdb!
  880. VERIFY_SUCCEEDED(CoInitializeEx(0, COINITBASE_MULTITHREADED));
  881. const wchar_t path[] = L"path-to-fxc-blob.bin";
  882. pDiaSource.Release();
  883. pProgramStream.Release();
  884. CComPtr<IDxcBlobEncoding> fxcBlob;
  885. CComPtr<IDxcBlob> pdbBlob;
  886. VERIFY_SUCCEEDED(pLib->CreateBlobFromFile(path, nullptr, &fxcBlob));
  887. std::string s = DumpParts(fxcBlob);
  888. CA2W sW(s.c_str(), CP_UTF8);
  889. WEX::Logging::Log::Comment(sW);
  890. VERIFY_SUCCEEDED(CreateDiaSourceFromDxbcBlob(pLib, fxcBlob, &pDiaSource));
  891. WEX::Logging::Log::Comment(GetDebugInfoAsText(pDiaSource).c_str());
  892. #endif
  893. }
  894. TEST_F(CompilerTest, CompileDebugLines) {
  895. CComPtr<IDiaDataSource> pDiaSource;
  896. VERIFY_SUCCEEDED(CreateDiaSourceForCompile(
  897. "float main(float pos : A) : SV_Target {\r\n"
  898. " float x = abs(pos);\r\n"
  899. " float y = sin(pos);\r\n"
  900. " float z = x + y;\r\n"
  901. " return z;\r\n"
  902. "}", &pDiaSource));
  903. const int numExpectedRVAs = 6;
  904. auto verifyLines = [=](const std::vector<LineNumber> lines) {
  905. VERIFY_ARE_EQUAL(lines.size(), numExpectedRVAs);
  906. // 0: loadInput
  907. VERIFY_ARE_EQUAL(lines[0].rva, 0);
  908. VERIFY_ARE_EQUAL(lines[0].line, 1);
  909. // 1: abs
  910. VERIFY_ARE_EQUAL(lines[1].rva, 1);
  911. VERIFY_ARE_EQUAL(lines[1].line, 2);
  912. // 2: sin
  913. VERIFY_ARE_EQUAL(lines[2].rva, 2);
  914. VERIFY_ARE_EQUAL(lines[2].line, 3);
  915. // 3: add
  916. VERIFY_ARE_EQUAL(lines[3].rva, 3);
  917. VERIFY_ARE_EQUAL(lines[3].line, 4);
  918. // 4: storeOutput
  919. VERIFY_ARE_EQUAL(lines[4].rva, 4);
  920. VERIFY_ARE_EQUAL(lines[4].line, 5);
  921. // 5: ret
  922. VERIFY_ARE_EQUAL(lines[5].rva, 5);
  923. VERIFY_ARE_EQUAL(lines[5].line, 5);
  924. };
  925. CComPtr<IDiaSession> pSession;
  926. CComPtr<IDiaEnumLineNumbers> pEnumLineNumbers;
  927. // Verify lines are ok when getting one RVA at a time.
  928. std::vector<LineNumber> linesOneByOne;
  929. VERIFY_SUCCEEDED(pDiaSource->openSession(&pSession));
  930. for (int i = 0; i < numExpectedRVAs; ++i) {
  931. VERIFY_SUCCEEDED(pSession->findLinesByRVA(i, 1, &pEnumLineNumbers));
  932. std::vector<LineNumber> lines = ReadLineNumbers(pEnumLineNumbers);
  933. std::copy(lines.begin(), lines.end(), std::back_inserter(linesOneByOne));
  934. pEnumLineNumbers.Release();
  935. }
  936. verifyLines(linesOneByOne);
  937. // Verify lines are ok when getting all RVAs at once.
  938. std::vector<LineNumber> linesAllAtOnce;
  939. pEnumLineNumbers.Release();
  940. VERIFY_SUCCEEDED(pSession->findLinesByRVA(0, numExpectedRVAs, &pEnumLineNumbers));
  941. linesAllAtOnce = ReadLineNumbers(pEnumLineNumbers);
  942. verifyLines(linesAllAtOnce);
  943. // Verify lines are ok when getting all lines through enum tables.
  944. std::vector<LineNumber> linesFromTable;
  945. pEnumLineNumbers.Release();
  946. CComPtr<IDiaEnumTables> pTables;
  947. CComPtr<IDiaTable> pTable;
  948. VERIFY_SUCCEEDED(pSession->getEnumTables(&pTables));
  949. DWORD celt;
  950. while (SUCCEEDED(pTables->Next(1, &pTable, &celt)) && celt == 1)
  951. {
  952. if (SUCCEEDED(pTable->QueryInterface(&pEnumLineNumbers))) {
  953. linesFromTable = ReadLineNumbers(pEnumLineNumbers);
  954. break;
  955. }
  956. pTable.Release();
  957. }
  958. verifyLines(linesFromTable);
  959. // Verify lines are ok when getting by address.
  960. std::vector<LineNumber> linesByAddr;
  961. pEnumLineNumbers.Release();
  962. VERIFY_SUCCEEDED(pSession->findLinesByAddr(0, 0, numExpectedRVAs, &pEnumLineNumbers));
  963. linesByAddr = ReadLineNumbers(pEnumLineNumbers);
  964. verifyLines(linesByAddr);
  965. // Verify findFileById.
  966. CComPtr<IDiaSourceFile> pFile;
  967. VERIFY_SUCCEEDED(pSession->findFileById(0, &pFile));
  968. CComBSTR pName;
  969. VERIFY_SUCCEEDED(pFile->get_fileName(&pName));
  970. VERIFY_ARE_EQUAL_WSTR(pName, L"source.hlsl");
  971. }
  972. #endif // _WIN32 - exclude dia stuff
  973. TEST_F(CompilerTest, CompileWhenDefinesThenApplied) {
  974. CComPtr<IDxcCompiler> pCompiler;
  975. CComPtr<IDxcOperationResult> pResult;
  976. CComPtr<IDxcBlobEncoding> pSource;
  977. DxcDefine defines[] = {{L"F4", L"float4"}};
  978. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  979. CreateBlobFromText("F4 main() : SV_Target { return 0; }", &pSource);
  980. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  981. L"ps_6_0", nullptr, 0, defines,
  982. _countof(defines), nullptr, &pResult));
  983. }
  984. TEST_F(CompilerTest, CompileWhenDefinesManyThenApplied) {
  985. CComPtr<IDxcCompiler> pCompiler;
  986. CComPtr<IDxcOperationResult> pResult;
  987. CComPtr<IDxcBlobEncoding> pSource;
  988. LPCWSTR args[] = {L"/DVAL1=1", L"/DVAL2=2", L"/DVAL3=3", L"/DVAL4=2",
  989. L"/DVAL5=4", L"/DNVAL1", L"/DNVAL2", L"/DNVAL3",
  990. L"/DNVAL4", L"/DNVAL5", L"/DCVAL1=1", L"/DCVAL2=2",
  991. L"/DCVAL3=3", L"/DCVAL4=2", L"/DCVAL5=4", L"/DCVALNONE="};
  992. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  993. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  994. "#ifndef VAL1\r\n"
  995. "#error VAL1 not defined\r\n"
  996. "#endif\r\n"
  997. "#ifndef NVAL5\r\n"
  998. "#error NVAL5 not defined\r\n"
  999. "#endif\r\n"
  1000. "#ifndef CVALNONE\r\n"
  1001. "#error CVALNONE not defined\r\n"
  1002. "#endif\r\n"
  1003. "return 0; }",
  1004. &pSource);
  1005. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1006. L"ps_6_0", args, _countof(args), nullptr,
  1007. 0, nullptr, &pResult));
  1008. HRESULT compileStatus;
  1009. VERIFY_SUCCEEDED(pResult->GetStatus(&compileStatus));
  1010. if (FAILED(compileStatus)) {
  1011. CComPtr<IDxcBlobEncoding> pErrors;
  1012. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  1013. OutputDebugStringA((LPCSTR)pErrors->GetBufferPointer());
  1014. }
  1015. VERIFY_SUCCEEDED(compileStatus);
  1016. }
  1017. TEST_F(CompilerTest, CompileWhenEmptyThenFails) {
  1018. CComPtr<IDxcCompiler> pCompiler;
  1019. CComPtr<IDxcOperationResult> pResult;
  1020. CComPtr<IDxcBlobEncoding> pSource;
  1021. CComPtr<IDxcBlobEncoding> pSourceBad;
  1022. LPCWSTR pProfile = L"ps_6_0";
  1023. LPCWSTR pEntryPoint = L"main";
  1024. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1025. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  1026. CreateBlobFromText("float4 main() : SV_Target { return undef; }", &pSourceBad);
  1027. // correct version
  1028. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  1029. pProfile, nullptr, 0, nullptr, 0, nullptr,
  1030. &pResult));
  1031. pResult.Release();
  1032. // correct version with compilation errors
  1033. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
  1034. pProfile, nullptr, 0, nullptr, 0, nullptr,
  1035. &pResult));
  1036. pResult.Release();
  1037. // null source
  1038. VERIFY_FAILED(pCompiler->Compile(nullptr, L"source.hlsl", pEntryPoint, pProfile,
  1039. nullptr, 0, nullptr, 0, nullptr, &pResult));
  1040. // null profile
  1041. VERIFY_FAILED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
  1042. nullptr, nullptr, 0, nullptr, 0, nullptr,
  1043. &pResult));
  1044. // null source name succeeds
  1045. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, nullptr, pEntryPoint, pProfile,
  1046. nullptr, 0, nullptr, 0, nullptr, &pResult));
  1047. pResult.Release();
  1048. // empty source name (as opposed to null) also suceeds
  1049. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"", pEntryPoint, pProfile,
  1050. nullptr, 0, nullptr, 0, nullptr,
  1051. &pResult));
  1052. pResult.Release();
  1053. // null result
  1054. VERIFY_FAILED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  1055. pProfile, nullptr, 0, nullptr, 0, nullptr,
  1056. nullptr));
  1057. }
  1058. TEST_F(CompilerTest, CompileWhenIncorrectThenFails) {
  1059. CComPtr<IDxcCompiler> pCompiler;
  1060. CComPtr<IDxcOperationResult> pResult;
  1061. CComPtr<IDxcBlobEncoding> pSource;
  1062. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1063. CreateBlobFromText("float4_undefined main() : SV_Target { return 0; }",
  1064. &pSource);
  1065. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main", L"ps_6_0",
  1066. nullptr, 0, nullptr, 0, nullptr,
  1067. &pResult));
  1068. HRESULT result;
  1069. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  1070. VERIFY_FAILED(result);
  1071. CComPtr<IDxcBlobEncoding> pErrorBuffer;
  1072. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrorBuffer));
  1073. std::string errorString(BlobToUtf8(pErrorBuffer));
  1074. VERIFY_ARE_NOT_EQUAL(0U, errorString.size());
  1075. // Useful for examining actual error message:
  1076. // CA2W errorStringW(errorString.c_str(), CP_UTF8);
  1077. // WEX::Logging::Log::Comment(errorStringW.m_psz);
  1078. }
  1079. TEST_F(CompilerTest, CompileWhenWorksThenDisassembleWorks) {
  1080. CComPtr<IDxcCompiler> pCompiler;
  1081. CComPtr<IDxcOperationResult> pResult;
  1082. CComPtr<IDxcBlobEncoding> pSource;
  1083. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1084. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  1085. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1086. L"ps_6_0", nullptr, 0, nullptr, 0,
  1087. nullptr, &pResult));
  1088. HRESULT result;
  1089. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  1090. VERIFY_SUCCEEDED(result);
  1091. CComPtr<IDxcBlob> pProgram;
  1092. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1093. CComPtr<IDxcBlobEncoding> pDisassembleBlob;
  1094. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembleBlob));
  1095. std::string disassembleString(BlobToUtf8(pDisassembleBlob));
  1096. VERIFY_ARE_NOT_EQUAL(0U, disassembleString.size());
  1097. // Useful for examining disassembly:
  1098. // CA2W disassembleStringW(disassembleString.c_str(), CP_UTF8);
  1099. // WEX::Logging::Log::Comment(disassembleStringW.m_psz);
  1100. }
  1101. #ifdef _WIN32 // Container builder unsupported
  1102. TEST_F(CompilerTest, CompileWhenDebugWorksThenStripDebug) {
  1103. CComPtr<IDxcCompiler> pCompiler;
  1104. CComPtr<IDxcOperationResult> pResult;
  1105. CComPtr<IDxcBlobEncoding> pSource;
  1106. CComPtr<IDxcBlob> pProgram;
  1107. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1108. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target {\r\n"
  1109. " float4 local = abs(pos);\r\n"
  1110. " return local;\r\n"
  1111. "}",
  1112. &pSource);
  1113. LPCWSTR args[] = {L"/Zi"};
  1114. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1115. L"ps_6_0", args, _countof(args), nullptr,
  1116. 0, nullptr, &pResult));
  1117. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1118. // Check if it contains debug blob
  1119. hlsl::DxilContainerHeader *pHeader =
  1120. (hlsl::DxilContainerHeader *)(pProgram->GetBufferPointer());
  1121. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  1122. pHeader, hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL);
  1123. VERIFY_IS_NOT_NULL(pPartHeader);
  1124. // Check debug info part does not exist after strip debug info
  1125. CComPtr<IDxcBlob> pNewProgram;
  1126. CComPtr<IDxcContainerBuilder> pBuilder;
  1127. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1128. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  1129. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL));
  1130. pResult.Release();
  1131. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1132. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1133. pHeader = (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1134. pPartHeader = hlsl::GetDxilPartByType(
  1135. pHeader, hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL);
  1136. VERIFY_IS_NULL(pPartHeader);
  1137. }
  1138. TEST_F(CompilerTest, CompileWhenWorksThenAddRemovePrivate) {
  1139. CComPtr<IDxcCompiler> pCompiler;
  1140. CComPtr<IDxcOperationResult> pResult;
  1141. CComPtr<IDxcBlobEncoding> pSource;
  1142. CComPtr<IDxcBlob> pProgram;
  1143. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1144. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  1145. " return 0;\r\n"
  1146. "}",
  1147. &pSource);
  1148. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1149. L"ps_6_0", nullptr, 0, nullptr, 0,
  1150. nullptr, &pResult));
  1151. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1152. // Append private data blob
  1153. CComPtr<IDxcContainerBuilder> pBuilder;
  1154. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1155. std::string privateTxt("private data");
  1156. CComPtr<IDxcBlobEncoding> pPrivate;
  1157. CreateBlobFromText(privateTxt.c_str(), &pPrivate);
  1158. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  1159. VERIFY_SUCCEEDED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_PrivateData, pPrivate));
  1160. pResult.Release();
  1161. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1162. CComPtr<IDxcBlob> pNewProgram;
  1163. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1164. hlsl::DxilContainerHeader *pContainerHeader =
  1165. (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1166. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  1167. pContainerHeader, hlsl::DxilFourCC::DFCC_PrivateData);
  1168. VERIFY_IS_NOT_NULL(pPartHeader);
  1169. // compare data
  1170. std::string privatePart((const char *)(pPartHeader + 1), privateTxt.size());
  1171. VERIFY_IS_TRUE(strcmp(privatePart.c_str(), privateTxt.c_str()) == 0);
  1172. // Remove private data blob
  1173. pBuilder.Release();
  1174. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1175. VERIFY_SUCCEEDED(pBuilder->Load(pNewProgram));
  1176. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_PrivateData));
  1177. pResult.Release();
  1178. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1179. pNewProgram.Release();
  1180. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1181. pContainerHeader =
  1182. (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1183. pPartHeader = hlsl::GetDxilPartByType(
  1184. pContainerHeader, hlsl::DxilFourCC::DFCC_PrivateData);
  1185. VERIFY_IS_NULL(pPartHeader);
  1186. }
  1187. TEST_F(CompilerTest, CompileThenAddCustomDebugName) {
  1188. // container builders prior to 1.3 did not support adding debug name parts
  1189. if (m_ver.SkipDxilVersion(1, 3)) return;
  1190. CComPtr<IDxcCompiler> pCompiler;
  1191. CComPtr<IDxcOperationResult> pResult;
  1192. CComPtr<IDxcBlobEncoding> pSource;
  1193. CComPtr<IDxcBlob> pProgram;
  1194. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1195. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  1196. " return 0;\r\n"
  1197. "}",
  1198. &pSource);
  1199. LPCWSTR args[] = { L"/Zi", L"/Zss" };
  1200. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1201. L"ps_6_0", args, _countof(args), nullptr, 0,
  1202. nullptr, &pResult));
  1203. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1204. // Append private data blob
  1205. CComPtr<IDxcContainerBuilder> pBuilder;
  1206. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1207. const char pNewName[] = "MyOwnUniqueName.lld";
  1208. //include null terminator:
  1209. size_t nameBlobPartSize = sizeof(hlsl::DxilShaderDebugName) + _countof(pNewName);
  1210. // round up to four-byte size:
  1211. size_t allocatedSize = (nameBlobPartSize + 3) & ~3;
  1212. auto pNameBlobContent = reinterpret_cast<hlsl::DxilShaderDebugName*>(malloc(allocatedSize));
  1213. ZeroMemory(pNameBlobContent, allocatedSize); //just to make sure trailing nulls are nulls.
  1214. pNameBlobContent->Flags = 0;
  1215. pNameBlobContent->NameLength = _countof(pNewName) - 1; //this is not supposed to include null terminator
  1216. memcpy(pNameBlobContent + 1, pNewName, _countof(pNewName));
  1217. CComPtr<IDxcBlobEncoding> pDebugName;
  1218. CreateBlobPinned(pNameBlobContent, allocatedSize, CP_UTF8, &pDebugName);
  1219. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  1220. // should fail since it already exists:
  1221. VERIFY_FAILED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_ShaderDebugName, pDebugName));
  1222. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugName));
  1223. VERIFY_SUCCEEDED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_ShaderDebugName, pDebugName));
  1224. pResult.Release();
  1225. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1226. CComPtr<IDxcBlob> pNewProgram;
  1227. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1228. hlsl::DxilContainerHeader *pContainerHeader =
  1229. (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1230. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  1231. pContainerHeader, hlsl::DxilFourCC::DFCC_ShaderDebugName);
  1232. VERIFY_IS_NOT_NULL(pPartHeader);
  1233. // compare data
  1234. VERIFY_IS_TRUE(memcmp(pPartHeader + 1, pNameBlobContent, allocatedSize) == 0);
  1235. free(pNameBlobContent);
  1236. // Remove private data blob
  1237. pBuilder.Release();
  1238. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1239. VERIFY_SUCCEEDED(pBuilder->Load(pNewProgram));
  1240. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugName));
  1241. pResult.Release();
  1242. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1243. pNewProgram.Release();
  1244. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1245. pContainerHeader =
  1246. (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1247. pPartHeader = hlsl::GetDxilPartByType(
  1248. pContainerHeader, hlsl::DxilFourCC::DFCC_ShaderDebugName);
  1249. VERIFY_IS_NULL(pPartHeader);
  1250. }
  1251. TEST_F(CompilerTest, CompileWithRootSignatureThenStripRootSignature) {
  1252. CComPtr<IDxcCompiler> pCompiler;
  1253. CComPtr<IDxcOperationResult> pResult;
  1254. CComPtr<IDxcBlobEncoding> pSource;
  1255. CComPtr<IDxcBlob> pProgram;
  1256. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1257. CreateBlobFromText("[RootSignature(\"\")] \r\n"
  1258. "float4 main(float a : A) : SV_Target {\r\n"
  1259. " return a;\r\n"
  1260. "}",
  1261. &pSource);
  1262. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1263. L"ps_6_0", nullptr, 0, nullptr,
  1264. 0, nullptr, &pResult));
  1265. VERIFY_IS_NOT_NULL(pResult);
  1266. HRESULT status;
  1267. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1268. VERIFY_SUCCEEDED(status);
  1269. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1270. VERIFY_IS_NOT_NULL(pProgram);
  1271. hlsl::DxilContainerHeader *pContainerHeader =
  1272. (hlsl::DxilContainerHeader *)(pProgram->GetBufferPointer());
  1273. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  1274. pContainerHeader, hlsl::DxilFourCC::DFCC_RootSignature);
  1275. VERIFY_IS_NOT_NULL(pPartHeader);
  1276. // Remove root signature
  1277. CComPtr<IDxcBlob> pNewProgram;
  1278. CComPtr<IDxcContainerBuilder> pBuilder;
  1279. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1280. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  1281. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_RootSignature));
  1282. pResult.Release();
  1283. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1284. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1285. pContainerHeader = (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1286. pPartHeader = hlsl::GetDxilPartByType(pContainerHeader,
  1287. hlsl::DxilFourCC::DFCC_RootSignature);
  1288. VERIFY_IS_NULL(pPartHeader);
  1289. }
  1290. #endif // Container builder unsupported
  1291. TEST_F(CompilerTest, CompileWhenIncludeThenLoadInvoked) {
  1292. CComPtr<IDxcCompiler> pCompiler;
  1293. CComPtr<IDxcOperationResult> pResult;
  1294. CComPtr<IDxcBlobEncoding> pSource;
  1295. CComPtr<TestIncludeHandler> pInclude;
  1296. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1297. CreateBlobFromText(
  1298. "#include \"helper.h\"\r\n"
  1299. "float4 main() : SV_Target { return 0; }", &pSource);
  1300. pInclude = new TestIncludeHandler(m_dllSupport);
  1301. pInclude->CallResults.emplace_back("");
  1302. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1303. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1304. VerifyOperationSucceeded(pResult);
  1305. VERIFY_ARE_EQUAL_WSTR(L"./helper.h;", pInclude->GetAllFileNames().c_str());
  1306. }
  1307. TEST_F(CompilerTest, CompileWhenIncludeThenLoadUsed) {
  1308. CComPtr<IDxcCompiler> pCompiler;
  1309. CComPtr<IDxcOperationResult> pResult;
  1310. CComPtr<IDxcBlobEncoding> pSource;
  1311. CComPtr<TestIncludeHandler> pInclude;
  1312. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1313. CreateBlobFromText(
  1314. "#include \"helper.h\"\r\n"
  1315. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1316. pInclude = new TestIncludeHandler(m_dllSupport);
  1317. pInclude->CallResults.emplace_back("#define ZERO 0");
  1318. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1319. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1320. VerifyOperationSucceeded(pResult);
  1321. VERIFY_ARE_EQUAL_WSTR(L"./helper.h;", pInclude->GetAllFileNames().c_str());
  1322. }
  1323. TEST_F(CompilerTest, CompileWhenIncludeAbsoluteThenLoadAbsolute) {
  1324. CComPtr<IDxcCompiler> pCompiler;
  1325. CComPtr<IDxcOperationResult> pResult;
  1326. CComPtr<IDxcBlobEncoding> pSource;
  1327. CComPtr<TestIncludeHandler> pInclude;
  1328. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1329. #ifdef _WIN32 // OS-specific root
  1330. CreateBlobFromText(
  1331. "#include \"C:\\helper.h\"\r\n"
  1332. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1333. #else
  1334. CreateBlobFromText(
  1335. "#include \"/helper.h\"\n"
  1336. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1337. #endif
  1338. pInclude = new TestIncludeHandler(m_dllSupport);
  1339. pInclude->CallResults.emplace_back("#define ZERO 0");
  1340. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1341. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1342. VerifyOperationSucceeded(pResult);
  1343. #ifdef _WIN32 // OS-specific root
  1344. VERIFY_ARE_EQUAL_WSTR(L"C:\\helper.h;", pInclude->GetAllFileNames().c_str());
  1345. #else
  1346. VERIFY_ARE_EQUAL_WSTR(L"/helper.h;", pInclude->GetAllFileNames().c_str());
  1347. #endif
  1348. }
  1349. TEST_F(CompilerTest, CompileWhenIncludeLocalThenLoadRelative) {
  1350. CComPtr<IDxcCompiler> pCompiler;
  1351. CComPtr<IDxcOperationResult> pResult;
  1352. CComPtr<IDxcBlobEncoding> pSource;
  1353. CComPtr<TestIncludeHandler> pInclude;
  1354. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1355. CreateBlobFromText(
  1356. "#include \"..\\helper.h\"\r\n"
  1357. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1358. pInclude = new TestIncludeHandler(m_dllSupport);
  1359. pInclude->CallResults.emplace_back("#define ZERO 0");
  1360. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1361. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1362. VerifyOperationSucceeded(pResult);
  1363. #ifdef _WIN32 // OS-specific directory dividers
  1364. VERIFY_ARE_EQUAL_WSTR(L"./..\\helper.h;", pInclude->GetAllFileNames().c_str());
  1365. #else
  1366. VERIFY_ARE_EQUAL_WSTR(L"./../helper.h;", pInclude->GetAllFileNames().c_str());
  1367. #endif
  1368. }
  1369. TEST_F(CompilerTest, CompileWhenIncludeSystemThenLoadNotRelative) {
  1370. CComPtr<IDxcCompiler> pCompiler;
  1371. CComPtr<IDxcOperationResult> pResult;
  1372. CComPtr<IDxcBlobEncoding> pSource;
  1373. CComPtr<TestIncludeHandler> pInclude;
  1374. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1375. CreateBlobFromText(
  1376. "#include \"subdir/other/file.h\"\r\n"
  1377. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1378. LPCWSTR args[] = {
  1379. L"-Ifoo"
  1380. };
  1381. pInclude = new TestIncludeHandler(m_dllSupport);
  1382. pInclude->CallResults.emplace_back("#include <helper.h>");
  1383. pInclude->CallResults.emplace_back("#define ZERO 0");
  1384. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1385. L"ps_6_0", args, _countof(args), nullptr, 0, pInclude, &pResult));
  1386. VerifyOperationSucceeded(pResult);
  1387. #ifdef _WIN32 // OS-specific directory dividers
  1388. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./foo\\helper.h;", pInclude->GetAllFileNames().c_str());
  1389. #else
  1390. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./foo/helper.h;", pInclude->GetAllFileNames().c_str());
  1391. #endif
  1392. }
  1393. TEST_F(CompilerTest, CompileWhenIncludeSystemMissingThenLoadAttempt) {
  1394. CComPtr<IDxcCompiler> pCompiler;
  1395. CComPtr<IDxcOperationResult> pResult;
  1396. CComPtr<IDxcBlobEncoding> pSource;
  1397. CComPtr<TestIncludeHandler> pInclude;
  1398. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1399. CreateBlobFromText(
  1400. "#include \"subdir/other/file.h\"\r\n"
  1401. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1402. pInclude = new TestIncludeHandler(m_dllSupport);
  1403. pInclude->CallResults.emplace_back("#include <helper.h>");
  1404. pInclude->CallResults.emplace_back("#define ZERO 0");
  1405. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1406. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1407. std::string failLog(VerifyOperationFailed(pResult));
  1408. VERIFY_ARE_NOT_EQUAL(std::string::npos, failLog.find("<angled>")); // error message should prompt to use <angled> rather than "quotes"
  1409. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./subdir/other/helper.h;", pInclude->GetAllFileNames().c_str());
  1410. }
  1411. TEST_F(CompilerTest, CompileWhenIncludeFlagsThenIncludeUsed) {
  1412. CComPtr<IDxcCompiler> pCompiler;
  1413. CComPtr<IDxcOperationResult> pResult;
  1414. CComPtr<IDxcBlobEncoding> pSource;
  1415. CComPtr<TestIncludeHandler> pInclude;
  1416. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1417. CreateBlobFromText(
  1418. "#include <helper.h>\r\n"
  1419. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1420. pInclude = new TestIncludeHandler(m_dllSupport);
  1421. pInclude->CallResults.emplace_back("#define ZERO 0");
  1422. #ifdef _WIN32 // OS-specific root
  1423. LPCWSTR args[] = { L"-I\\\\server\\share" };
  1424. #else
  1425. LPCWSTR args[] = { L"-I/server/share" };
  1426. #endif
  1427. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1428. L"ps_6_0", args, _countof(args), nullptr, 0, pInclude, &pResult));
  1429. VerifyOperationSucceeded(pResult);
  1430. #ifdef _WIN32 // OS-specific root
  1431. VERIFY_ARE_EQUAL_WSTR(L"\\\\server\\share\\helper.h;", pInclude->GetAllFileNames().c_str());
  1432. #else
  1433. VERIFY_ARE_EQUAL_WSTR(L"/server/share/helper.h;", pInclude->GetAllFileNames().c_str());
  1434. #endif
  1435. }
  1436. TEST_F(CompilerTest, CompileWhenIncludeMissingThenFail) {
  1437. CComPtr<IDxcCompiler> pCompiler;
  1438. CComPtr<IDxcOperationResult> pResult;
  1439. CComPtr<IDxcBlobEncoding> pSource;
  1440. CComPtr<TestIncludeHandler> pInclude;
  1441. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1442. CreateBlobFromText(
  1443. "#include \"file.h\"\r\n"
  1444. "float4 main() : SV_Target { return 0; }", &pSource);
  1445. pInclude = new TestIncludeHandler(m_dllSupport);
  1446. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1447. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1448. HRESULT hr;
  1449. VERIFY_SUCCEEDED(pResult->GetStatus(&hr));
  1450. VERIFY_FAILED(hr);
  1451. }
  1452. TEST_F(CompilerTest, CompileWhenIncludeHasPathThenOK) {
  1453. CComPtr<IDxcCompiler> pCompiler;
  1454. LPCWSTR Source = L"c:\\temp\\OddIncludes\\main.hlsl";
  1455. LPCWSTR Args[] = { L"/I", L"c:\\temp" };
  1456. LPCWSTR ArgsUp[] = { L"/I", L"c:\\Temp" };
  1457. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1458. bool useUpValues[] = { false, true };
  1459. for (bool useUp : useUpValues) {
  1460. CComPtr<IDxcOperationResult> pResult;
  1461. CComPtr<IDxcBlobEncoding> pSource;
  1462. #if TEST_ON_DISK
  1463. CComPtr<IDxcLibrary> pLibrary;
  1464. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  1465. VERIFY_SUCCEEDED(pLibrary->CreateIncludeHandler(&pInclude));
  1466. VERIFY_SUCCEEDED(pLibrary->CreateBlobFromFile(Source, nullptr, &pSource));
  1467. #else
  1468. CComPtr<TestIncludeHandler> pInclude;
  1469. pInclude = new TestIncludeHandler(m_dllSupport);
  1470. pInclude->CallResults.emplace_back("// Empty");
  1471. CreateBlobFromText("#include \"include.hlsl\"\r\n"
  1472. "float4 main() : SV_Target { return 0; }",
  1473. &pSource);
  1474. #endif
  1475. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, Source, L"main",
  1476. L"ps_6_0", useUp ? ArgsUp : Args, _countof(Args), nullptr, 0, pInclude, &pResult));
  1477. HRESULT hr;
  1478. VERIFY_SUCCEEDED(pResult->GetStatus(&hr));
  1479. VERIFY_SUCCEEDED(hr);
  1480. }
  1481. }
  1482. TEST_F(CompilerTest, CompileWhenIncludeEmptyThenOK) {
  1483. CComPtr<IDxcCompiler> pCompiler;
  1484. CComPtr<IDxcOperationResult> pResult;
  1485. CComPtr<IDxcBlobEncoding> pSource;
  1486. CComPtr<TestIncludeHandler> pInclude;
  1487. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1488. CreateBlobFromText("#include \"empty.h\"\r\n"
  1489. "float4 main() : SV_Target { return 0; }",
  1490. &pSource);
  1491. pInclude = new TestIncludeHandler(m_dllSupport);
  1492. pInclude->CallResults.emplace_back("", CP_ACP); // An empty file would get detected as ACP code page
  1493. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1494. L"ps_6_0", nullptr, 0, nullptr, 0,
  1495. pInclude, &pResult));
  1496. VerifyOperationSucceeded(pResult);
  1497. VERIFY_ARE_EQUAL_WSTR(L"./empty.h;", pInclude->GetAllFileNames().c_str());
  1498. }
  1499. static const char EmptyCompute[] = "[numthreads(8,8,1)] void main() { }";
  1500. TEST_F(CompilerTest, CompileWhenODumpThenPassConfig) {
  1501. CComPtr<IDxcCompiler> pCompiler;
  1502. CComPtr<IDxcOperationResult> pResult;
  1503. CComPtr<IDxcBlobEncoding> pSource;
  1504. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1505. CreateBlobFromText(EmptyCompute, &pSource);
  1506. LPCWSTR Args[] = { L"/Odump" };
  1507. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1508. L"cs_6_0", Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1509. VerifyOperationSucceeded(pResult);
  1510. CComPtr<IDxcBlob> pResultBlob;
  1511. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1512. string passes((char *)pResultBlob->GetBufferPointer(), pResultBlob->GetBufferSize());
  1513. VERIFY_ARE_NOT_EQUAL(string::npos, passes.find("inline"));
  1514. }
  1515. TEST_F(CompilerTest, CompileWhenVdThenProducesDxilContainer) {
  1516. CComPtr<IDxcCompiler> pCompiler;
  1517. CComPtr<IDxcOperationResult> pResult;
  1518. CComPtr<IDxcBlobEncoding> pSource;
  1519. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1520. CreateBlobFromText(EmptyCompute, &pSource);
  1521. LPCWSTR Args[] = { L"/Vd" };
  1522. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1523. L"cs_6_0", Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1524. VerifyOperationSucceeded(pResult);
  1525. CComPtr<IDxcBlob> pResultBlob;
  1526. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1527. VERIFY_IS_TRUE(hlsl::IsValidDxilContainer(reinterpret_cast<hlsl::DxilContainerHeader *>(pResultBlob->GetBufferPointer()), pResultBlob->GetBufferSize()));
  1528. }
  1529. TEST_F(CompilerTest, CompileWhenODumpThenOptimizerMatch) {
  1530. LPCWSTR OptLevels[] = { L"/Od", L"/O1", L"/O2" };
  1531. CComPtr<IDxcCompiler> pCompiler;
  1532. CComPtr<IDxcOptimizer> pOptimizer;
  1533. CComPtr<IDxcAssembler> pAssembler;
  1534. CComPtr<IDxcValidator> pValidator;
  1535. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  1536. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  1537. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcOptimizer, &pOptimizer));
  1538. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcValidator, &pValidator));
  1539. for (LPCWSTR OptLevel : OptLevels) {
  1540. CComPtr<IDxcOperationResult> pResult;
  1541. CComPtr<IDxcBlobEncoding> pSource;
  1542. CComPtr<IDxcBlob> pHighLevelBlob;
  1543. CComPtr<IDxcBlob> pOptimizedModule;
  1544. CComPtr<IDxcBlob> pAssembledBlob;
  1545. // Could use EmptyCompute and cs_6_0, but there is an issue where properties
  1546. // don't round-trip properly at high-level, so validation fails because
  1547. // dimensions are set to zero. Workaround by using pixel shader instead.
  1548. LPCWSTR Target = L"ps_6_0";
  1549. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  1550. LPCWSTR Args[2] = { OptLevel, L"/Odump" };
  1551. // Get the passes for this optimization level.
  1552. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1553. Target, Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1554. VerifyOperationSucceeded(pResult);
  1555. CComPtr<IDxcBlob> pResultBlob;
  1556. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1557. string passes((char *)pResultBlob->GetBufferPointer(), pResultBlob->GetBufferSize());
  1558. // Get wchar_t version and prepend hlsl-hlensure, to do a split high-level/opt compilation pass.
  1559. CA2W passesW(passes.c_str(), CP_UTF8);
  1560. std::vector<LPCWSTR> Options;
  1561. SplitPassList(passesW.m_psz, Options);
  1562. // Now compile directly.
  1563. pResult.Release();
  1564. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1565. Target, Args, 1, nullptr, 0, nullptr, &pResult));
  1566. VerifyOperationSucceeded(pResult);
  1567. // Now compile via a high-level compile followed by the optimization passes.
  1568. pResult.Release();
  1569. Args[_countof(Args)-1] = L"/fcgl";
  1570. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1571. Target, Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1572. VerifyOperationSucceeded(pResult);
  1573. VERIFY_SUCCEEDED(pResult->GetResult(&pHighLevelBlob));
  1574. VERIFY_SUCCEEDED(pOptimizer->RunOptimizer(pHighLevelBlob, Options.data(),
  1575. Options.size(), &pOptimizedModule,
  1576. nullptr));
  1577. string text = DisassembleProgram(m_dllSupport, pOptimizedModule);
  1578. WEX::Logging::Log::Comment(L"Final program:");
  1579. WEX::Logging::Log::Comment(CA2W(text.c_str()));
  1580. // At the very least, the module should be valid.
  1581. pResult.Release();
  1582. VERIFY_SUCCEEDED(pAssembler->AssembleToContainer(pOptimizedModule, &pResult));
  1583. VerifyOperationSucceeded(pResult);
  1584. VERIFY_SUCCEEDED(pResult->GetResult(&pAssembledBlob));
  1585. pResult.Release();
  1586. VERIFY_SUCCEEDED(pValidator->Validate(pAssembledBlob, DxcValidatorFlags_Default, &pResult));
  1587. VerifyOperationSucceeded(pResult);
  1588. }
  1589. }
  1590. static const UINT CaptureStacks = 0; // Set to 1 to enable captures
  1591. static const UINT StackFrameCount = 12;
  1592. struct InstrumentedHeapMalloc : public IMalloc {
  1593. private:
  1594. HANDLE m_Handle; // Heap handle.
  1595. ULONG m_RefCount = 0; // Reference count. Used for reference leaks, not for lifetime.
  1596. ULONG m_AllocCount = 0; // Total # of alloc and realloc requests.
  1597. ULONG m_AllocSize = 0; // Total # of alloc and realloc bytes.
  1598. ULONG m_Size = 0; // Current # of alloc'ed bytes.
  1599. ULONG m_FailAlloc = 0; // If nonzero, the alloc/realloc call to fail.
  1600. // Each allocation also tracks the following information:
  1601. // - allocation callstack
  1602. // - deallocation callstack
  1603. // - prior/next blocks in a list of allocated blocks
  1604. LIST_ENTRY AllocList;
  1605. struct PtrData {
  1606. LIST_ENTRY Entry;
  1607. LPVOID AllocFrames[CaptureStacks ? StackFrameCount * CaptureStacks : 1];
  1608. LPVOID FreeFrames[CaptureStacks ? StackFrameCount * CaptureStacks : 1];
  1609. UINT64 AllocAtCount;
  1610. DWORD AllocFrameCount;
  1611. DWORD FreeFrameCount;
  1612. SIZE_T Size;
  1613. PtrData *Self;
  1614. };
  1615. PtrData *DataFromPtr(void *p) {
  1616. if (p == nullptr) return nullptr;
  1617. PtrData *R = ((PtrData *)p) - 1;
  1618. if (R != R->Self) {
  1619. VERIFY_FAIL(); // p is invalid or underrun
  1620. }
  1621. return R;
  1622. }
  1623. public:
  1624. InstrumentedHeapMalloc() : m_Handle(nullptr) {
  1625. ResetCounts();
  1626. }
  1627. ~InstrumentedHeapMalloc() {
  1628. if (m_Handle)
  1629. HeapDestroy(m_Handle);
  1630. }
  1631. void ResetHeap() {
  1632. if (m_Handle) {
  1633. HeapDestroy(m_Handle);
  1634. m_Handle = nullptr;
  1635. }
  1636. m_Handle = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
  1637. }
  1638. ULONG GetRefCount() const { return m_RefCount; }
  1639. ULONG GetAllocCount() const { return m_AllocCount; }
  1640. ULONG GetAllocSize() const { return m_AllocSize; }
  1641. ULONG GetSize() const { return m_Size; }
  1642. void ResetCounts() {
  1643. m_RefCount = m_AllocCount = m_AllocSize = m_Size = 0;
  1644. AllocList.Blink = AllocList.Flink = &AllocList;
  1645. }
  1646. void SetFailAlloc(ULONG index) {
  1647. m_FailAlloc = index;
  1648. }
  1649. ULONG STDMETHODCALLTYPE AddRef() {
  1650. return ++m_RefCount;
  1651. }
  1652. ULONG STDMETHODCALLTYPE Release() {
  1653. if (m_RefCount == 0) VERIFY_FAIL();
  1654. return --m_RefCount;
  1655. }
  1656. STDMETHODIMP QueryInterface(REFIID iid, void** ppvObject) {
  1657. return DoBasicQueryInterface<IMalloc>(this, iid, ppvObject);
  1658. }
  1659. virtual void *STDMETHODCALLTYPE Alloc(_In_ SIZE_T cb) {
  1660. ++m_AllocCount;
  1661. if (m_FailAlloc && m_AllocCount >= m_FailAlloc) {
  1662. return nullptr; // breakpoint for i failure - m_FailAlloc == 1+VAL
  1663. }
  1664. m_AllocSize += cb;
  1665. m_Size += cb;
  1666. PtrData *P = (PtrData *)HeapAlloc(m_Handle, HEAP_ZERO_MEMORY, sizeof(PtrData) + cb);
  1667. P->Entry.Flink = AllocList.Flink;
  1668. P->Entry.Blink = &AllocList;
  1669. AllocList.Flink->Blink = &(P->Entry);
  1670. AllocList.Flink = &(P->Entry);
  1671. // breakpoint for i failure on NN alloc - m_FailAlloc == 1+VAL && m_AllocCount == NN
  1672. // breakpoint for happy path for NN alloc - m_AllocCount == NN
  1673. P->AllocAtCount = m_AllocCount;
  1674. if (CaptureStacks)
  1675. P->AllocFrameCount = CaptureStackBackTrace(1, StackFrameCount, P->AllocFrames, nullptr);
  1676. P->Size = cb;
  1677. P->Self = P;
  1678. return P + 1;
  1679. }
  1680. virtual void *STDMETHODCALLTYPE Realloc(_In_opt_ void *pv, _In_ SIZE_T cb) {
  1681. SIZE_T priorSize = pv == nullptr ? (SIZE_T)0 : GetSize(pv);
  1682. void *R = Alloc(cb);
  1683. if (!R)
  1684. return nullptr;
  1685. SIZE_T copySize = std::min(cb, priorSize);
  1686. memcpy(R, pv, copySize);
  1687. Free(pv);
  1688. return R;
  1689. }
  1690. virtual void STDMETHODCALLTYPE Free(_In_opt_ void *pv) {
  1691. if (!pv)
  1692. return;
  1693. PtrData *P = DataFromPtr(pv);
  1694. if (P->FreeFrameCount)
  1695. VERIFY_FAIL(); // double-free detected
  1696. m_Size -= P->Size;
  1697. P->Entry.Flink->Blink = P->Entry.Blink;
  1698. P->Entry.Blink->Flink = P->Entry.Flink;
  1699. if (CaptureStacks)
  1700. P->FreeFrameCount =
  1701. CaptureStackBackTrace(1, StackFrameCount, P->FreeFrames, nullptr);
  1702. }
  1703. virtual SIZE_T STDMETHODCALLTYPE GetSize(
  1704. /* [annotation][in] */
  1705. _In_opt_ _Post_writable_byte_size_(return) void *pv)
  1706. {
  1707. if (pv == nullptr) return 0;
  1708. return DataFromPtr(pv)->Size;
  1709. }
  1710. virtual int STDMETHODCALLTYPE DidAlloc(
  1711. _In_opt_ void *pv) {
  1712. return -1; // don't know
  1713. }
  1714. virtual void STDMETHODCALLTYPE HeapMinimize(void) {}
  1715. void DumpLeaks() {
  1716. PtrData *ptr = (PtrData*)AllocList.Flink;;
  1717. PtrData *end = (PtrData*)AllocList.Blink;;
  1718. WEX::Logging::Log::Comment(FormatToWString(L"Leaks total size: %d", (signed int)m_Size).data());
  1719. while (ptr != end) {
  1720. WEX::Logging::Log::Comment(FormatToWString(L"Memory leak at 0x0%X, size %d, alloc# %d", ptr + 1, ptr->Size, ptr->AllocAtCount).data());
  1721. ptr = (PtrData*)ptr->Entry.Flink;
  1722. }
  1723. }
  1724. };
  1725. #if _ITERATOR_DEBUG_LEVEL==0
  1726. // CompileWhenNoMemThenOOM can properly detect leaks only when debug iterators are disabled
  1727. TEST_F(CompilerTest, CompileWhenNoMemThenOOM) {
  1728. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  1729. CComPtr<IDxcBlobEncoding> pSource;
  1730. CreateBlobFromText(EmptyCompute, &pSource);
  1731. InstrumentedHeapMalloc InstrMalloc;
  1732. CComPtr<IDxcCompiler> pCompiler;
  1733. CComPtr<IDxcOperationResult> pResult;
  1734. ULONG allocCount = 0;
  1735. ULONG allocSize = 0;
  1736. ULONG initialRefCount;
  1737. InstrMalloc.ResetHeap();
  1738. VERIFY_IS_TRUE(m_dllSupport.HasCreateWithMalloc());
  1739. // Verify a simple object creation.
  1740. initialRefCount = InstrMalloc.GetRefCount();
  1741. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler));
  1742. pCompiler.Release();
  1743. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1744. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1745. InstrMalloc.ResetCounts();
  1746. InstrMalloc.ResetHeap();
  1747. // First time, run to completion and capture stats.
  1748. initialRefCount = InstrMalloc.GetRefCount();
  1749. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler));
  1750. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1751. L"cs_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1752. allocCount = InstrMalloc.GetAllocCount();
  1753. allocSize = InstrMalloc.GetAllocSize();
  1754. HRESULT hrWithMemory;
  1755. VERIFY_SUCCEEDED(pResult->GetStatus(&hrWithMemory));
  1756. VERIFY_SUCCEEDED(hrWithMemory);
  1757. pCompiler.Release();
  1758. pResult.Release();
  1759. VERIFY_IS_TRUE(allocSize > allocCount);
  1760. // Ensure that after all resources are released, there are no outstanding
  1761. // allocations or references.
  1762. //
  1763. // First leak is in ((InstrumentedHeapMalloc::PtrData *)InstrMalloc.AllocList.Flink)
  1764. if (InstrMalloc.GetSize() != 0) {
  1765. WEX::Logging::Log::Comment(L"Memory leak(s) detected");
  1766. InstrMalloc.DumpLeaks();
  1767. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1768. }
  1769. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1770. // In Debug, without /D_ITERATOR_DEBUG_LEVEL=0, debug iterators will be used;
  1771. // this causes a problem where std::string is specified as noexcept, and yet
  1772. // a sentinel is allocated that may fail and throw.
  1773. if (m_ver.SkipOutOfMemoryTest()) return;
  1774. // Now, fail each allocation and make sure we get an error.
  1775. for (ULONG i = 0; i <= allocCount; ++i) {
  1776. // LogCommentFmt(L"alloc fail %u", i);
  1777. bool isLast = i == allocCount;
  1778. InstrMalloc.ResetCounts();
  1779. InstrMalloc.ResetHeap();
  1780. InstrMalloc.SetFailAlloc(i + 1);
  1781. HRESULT hrOp = m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler);
  1782. if (SUCCEEDED(hrOp)) {
  1783. hrOp = pCompiler->Compile(pSource, L"source.hlsl", L"main", L"cs_6_0",
  1784. nullptr, 0, nullptr, 0, nullptr, &pResult);
  1785. if (SUCCEEDED(hrOp)) {
  1786. pResult->GetStatus(&hrOp);
  1787. }
  1788. }
  1789. if (FAILED(hrOp)) {
  1790. // This is true in *almost* every case. When the OOM happens during stream
  1791. // handling, there is no specific error set; by the time it's detected,
  1792. // it propagates as E_FAIL.
  1793. //VERIFY_ARE_EQUAL(hrOp, E_OUTOFMEMORY);
  1794. VERIFY_IS_TRUE(hrOp == E_OUTOFMEMORY || hrOp == E_FAIL);
  1795. }
  1796. if (isLast)
  1797. VERIFY_SUCCEEDED(hrOp);
  1798. else
  1799. VERIFY_FAILED(hrOp);
  1800. pCompiler.Release();
  1801. pResult.Release();
  1802. if (InstrMalloc.GetSize() != 0) {
  1803. WEX::Logging::Log::Comment(FormatToWString(L"Memory leak(s) detected, allocCount = %d", i).data());
  1804. InstrMalloc.DumpLeaks();
  1805. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1806. }
  1807. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1808. }
  1809. }
  1810. #endif
  1811. TEST_F(CompilerTest, CompileWhenShaderModelMismatchAttributeThenFail) {
  1812. CComPtr<IDxcCompiler> pCompiler;
  1813. CComPtr<IDxcOperationResult> pResult;
  1814. CComPtr<IDxcBlobEncoding> pSource;
  1815. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1816. CreateBlobFromText(EmptyCompute, &pSource);
  1817. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1818. L"ps_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1819. std::string failLog(VerifyOperationFailed(pResult));
  1820. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find("attribute numthreads only valid for CS"));
  1821. }
  1822. TEST_F(CompilerTest, CompileBadHlslThenFail) {
  1823. CComPtr<IDxcCompiler> pCompiler;
  1824. CComPtr<IDxcOperationResult> pResult;
  1825. CComPtr<IDxcBlobEncoding> pSource;
  1826. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1827. CreateBlobFromText(
  1828. "bad hlsl", &pSource);
  1829. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1830. L"ps_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1831. HRESULT status;
  1832. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1833. VERIFY_FAILED(status);
  1834. }
  1835. TEST_F(CompilerTest, CompileLegacyShaderModelThenFail) {
  1836. VerifyCompileFailed(
  1837. "float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", L"ps_5_1", nullptr);
  1838. }
  1839. TEST_F(CompilerTest, CompileWhenRecursiveAlbeitStaticTermThenFail) {
  1840. // This shader will compile under fxc because if execution is
  1841. // simulated statically, it does terminate. dxc changes this behavior
  1842. // to avoid imposing the requirement on the compiler.
  1843. const char ShaderText[] =
  1844. "static int i = 10;\r\n"
  1845. "float4 f(); // Forward declaration\r\n"
  1846. "float4 g() { if (i > 10) { i--; return f(); } else return 0; } // Recursive call to 'f'\r\n"
  1847. "float4 f() { return g(); } // First call to 'g'\r\n"
  1848. "float4 VS() : SV_Position{\r\n"
  1849. " return f(); // First call to 'f'\r\n"
  1850. "}\r\n";
  1851. VerifyCompileFailed(ShaderText, L"vs_6_0", "recursive functions not allowed", L"VS");
  1852. }
  1853. TEST_F(CompilerTest, CompileWhenRecursiveThenFail) {
  1854. const char ShaderTextSimple[] =
  1855. "float4 f(); // Forward declaration\r\n"
  1856. "float4 g() { return f(); } // Recursive call to 'f'\r\n"
  1857. "float4 f() { return g(); } // First call to 'g'\r\n"
  1858. "float4 main() : SV_Position{\r\n"
  1859. " return f(); // First call to 'f'\r\n"
  1860. "}\r\n";
  1861. VerifyCompileFailed(ShaderTextSimple, L"vs_6_0", "recursive functions not allowed");
  1862. const char ShaderTextIndirect[] =
  1863. "float4 f(); // Forward declaration\r\n"
  1864. "float4 g() { return f(); } // Recursive call to 'f'\r\n"
  1865. "float4 f() { return g(); } // First call to 'g'\r\n"
  1866. "float4 main() : SV_Position{\r\n"
  1867. " return f(); // First call to 'f'\r\n"
  1868. "}\r\n";
  1869. VerifyCompileFailed(ShaderTextIndirect, L"vs_6_0", "recursive functions not allowed");
  1870. const char ShaderTextSelf[] =
  1871. "float4 main() : SV_Position{\r\n"
  1872. " return main();\r\n"
  1873. "}\r\n";
  1874. VerifyCompileFailed(ShaderTextSelf, L"vs_6_0", "recursive functions not allowed");
  1875. const char ShaderTextMissing[] =
  1876. "float4 mainz() : SV_Position{\r\n"
  1877. " return 1;\r\n"
  1878. "}\r\n";
  1879. VerifyCompileFailed(ShaderTextMissing, L"vs_6_0", "missing entry point definition");
  1880. }
  1881. TEST_F(CompilerTest, CompileHlsl2015ThenFail) {
  1882. CComPtr<IDxcCompiler> pCompiler;
  1883. CComPtr<IDxcOperationResult> pResult;
  1884. CComPtr<IDxcBlobEncoding> pSource;
  1885. CComPtr<IDxcBlobEncoding> pErrors;
  1886. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1887. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1888. LPCWSTR args[2] = { L"-HV", L"2015" };
  1889. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1890. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1891. HRESULT status;
  1892. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1893. VERIFY_ARE_EQUAL(status, E_INVALIDARG);
  1894. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  1895. LPCSTR pErrorMsg = "HLSL Version 2015 is only supported for language services";
  1896. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  1897. }
  1898. TEST_F(CompilerTest, CompileHlsl2016ThenOK) {
  1899. CComPtr<IDxcCompiler> pCompiler;
  1900. CComPtr<IDxcOperationResult> pResult;
  1901. CComPtr<IDxcBlobEncoding> pSource;
  1902. CComPtr<IDxcBlobEncoding> pErrors;
  1903. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1904. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1905. LPCWSTR args[2] = { L"-HV", L"2016" };
  1906. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1907. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1908. HRESULT status;
  1909. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1910. VERIFY_SUCCEEDED(status);
  1911. }
  1912. TEST_F(CompilerTest, CompileHlsl2017ThenOK) {
  1913. CComPtr<IDxcCompiler> pCompiler;
  1914. CComPtr<IDxcOperationResult> pResult;
  1915. CComPtr<IDxcBlobEncoding> pSource;
  1916. CComPtr<IDxcBlobEncoding> pErrors;
  1917. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1918. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1919. LPCWSTR args[2] = { L"-HV", L"2017" };
  1920. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1921. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1922. HRESULT status;
  1923. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1924. VERIFY_SUCCEEDED(status);
  1925. }
  1926. TEST_F(CompilerTest, CompileHlsl2018ThenOK) {
  1927. CComPtr<IDxcCompiler> pCompiler;
  1928. CComPtr<IDxcOperationResult> pResult;
  1929. CComPtr<IDxcBlobEncoding> pSource;
  1930. CComPtr<IDxcBlobEncoding> pErrors;
  1931. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1932. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1933. LPCWSTR args[2] = { L"-HV", L"2018" };
  1934. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1935. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1936. HRESULT status;
  1937. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1938. VERIFY_SUCCEEDED(status);
  1939. }
  1940. TEST_F(CompilerTest, CompileHlsl2019ThenFail) {
  1941. CComPtr<IDxcCompiler> pCompiler;
  1942. CComPtr<IDxcOperationResult> pResult;
  1943. CComPtr<IDxcBlobEncoding> pSource;
  1944. CComPtr<IDxcBlobEncoding> pErrors;
  1945. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1946. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1947. LPCWSTR args[2] = { L"-HV", L"2019" };
  1948. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1949. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1950. HRESULT status;
  1951. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1952. VERIFY_ARE_EQUAL(status, E_INVALIDARG);
  1953. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  1954. LPCSTR pErrorMsg = "Unknown HLSL version";
  1955. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  1956. }
  1957. #ifdef _WIN32 // - exclude dia stuff
  1958. TEST_F(CompilerTest, DiaLoadBadBitcodeThenFail) {
  1959. CComPtr<IDxcBlob> pBadBitcode;
  1960. CComPtr<IDiaDataSource> pDiaSource;
  1961. CComPtr<IStream> pStream;
  1962. CComPtr<IDxcLibrary> pLib;
  1963. Utf8ToBlob(m_dllSupport, "badcode", &pBadBitcode);
  1964. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  1965. VERIFY_SUCCEEDED(pLib->CreateStreamFromBlobReadOnly(pBadBitcode, &pStream));
  1966. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcDiaDataSource, &pDiaSource));
  1967. VERIFY_FAILED(pDiaSource->loadDataFromIStream(pStream));
  1968. }
  1969. static void CompileTestAndLoadDia(dxc::DxcDllSupport &dllSupport, IDiaDataSource **ppDataSource) {
  1970. CComPtr<IDxcBlob> pContainer;
  1971. CComPtr<IDxcBlob> pDebugContent;
  1972. CComPtr<IDiaDataSource> pDiaSource;
  1973. CComPtr<IStream> pStream;
  1974. CComPtr<IDxcLibrary> pLib;
  1975. CComPtr<IDxcContainerReflection> pReflection;
  1976. UINT32 index;
  1977. VerifyCompileOK(dllSupport, EmptyCompute, L"cs_6_0", L"/Zi", &pContainer);
  1978. VERIFY_SUCCEEDED(dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  1979. VERIFY_SUCCEEDED(dllSupport.CreateInstance(CLSID_DxcContainerReflection, &pReflection));
  1980. VERIFY_SUCCEEDED(pReflection->Load(pContainer));
  1981. VERIFY_SUCCEEDED(pReflection->FindFirstPartKind(hlsl::DFCC_ShaderDebugInfoDXIL, &index));
  1982. VERIFY_SUCCEEDED(pReflection->GetPartContent(index, &pDebugContent));
  1983. VERIFY_SUCCEEDED(pLib->CreateStreamFromBlobReadOnly(pDebugContent, &pStream));
  1984. VERIFY_SUCCEEDED(dllSupport.CreateInstance(CLSID_DxcDiaDataSource, &pDiaSource));
  1985. VERIFY_SUCCEEDED(pDiaSource->loadDataFromIStream(pStream));
  1986. if (ppDataSource) {
  1987. *ppDataSource = pDiaSource.Detach();
  1988. }
  1989. }
  1990. TEST_F(CompilerTest, DiaLoadDebugThenOK) {
  1991. CompileTestAndLoadDia(m_dllSupport, nullptr);
  1992. }
  1993. TEST_F(CompilerTest, DiaTableIndexThenOK) {
  1994. CComPtr<IDiaDataSource> pDiaSource;
  1995. CComPtr<IDiaSession> pDiaSession;
  1996. CComPtr<IDiaEnumTables> pEnumTables;
  1997. CComPtr<IDiaTable> pTable;
  1998. VARIANT vtIndex;
  1999. CompileTestAndLoadDia(m_dllSupport, &pDiaSource);
  2000. VERIFY_SUCCEEDED(pDiaSource->openSession(&pDiaSession));
  2001. VERIFY_SUCCEEDED(pDiaSession->getEnumTables(&pEnumTables));
  2002. vtIndex.vt = VT_EMPTY;
  2003. VERIFY_FAILED(pEnumTables->Item(vtIndex, &pTable));
  2004. vtIndex.vt = VT_I4;
  2005. vtIndex.intVal = 1;
  2006. VERIFY_SUCCEEDED(pEnumTables->Item(vtIndex, &pTable));
  2007. VERIFY_IS_NOT_NULL(pTable.p);
  2008. pTable.Release();
  2009. vtIndex.vt = VT_UI4;
  2010. vtIndex.uintVal = 1;
  2011. VERIFY_SUCCEEDED(pEnumTables->Item(vtIndex, &pTable));
  2012. VERIFY_IS_NOT_NULL(pTable.p);
  2013. pTable.Release();
  2014. vtIndex.uintVal = 100;
  2015. VERIFY_FAILED(pEnumTables->Item(vtIndex, &pTable));
  2016. }
  2017. #endif // _WIN32 - exclude dia stuff
  2018. TEST_F(CompilerTest, CodeGenPix) {
  2019. CodeGenTestCheckBatchDir(L"pix");
  2020. }
  2021. TEST_F(CompilerTest, CodeGenAttributeAtVertex) {
  2022. if (m_ver.SkipDxilVersion(1,1)) return;
  2023. CodeGenTestCheck(L"attributeAtVertex.hlsl");
  2024. }
  2025. TEST_F(CompilerTest, CodeGenAttributeAtVertexNoOpt) {
  2026. if (m_ver.SkipDxilVersion(1,1)) return;
  2027. CodeGenTestCheck(L"attributeAtVertexNoOpt.hlsl");
  2028. }
  2029. TEST_F(CompilerTest, CodeGenBarycentrics) {
  2030. if (m_ver.SkipDxilVersion(1,1)) return;
  2031. CodeGenTestCheck(L"barycentrics.hlsl");
  2032. }
  2033. TEST_F(CompilerTest, CodeGenBarycentrics1) {
  2034. if (m_ver.SkipDxilVersion(1,1)) return;
  2035. CodeGenTestCheck(L"barycentrics1.hlsl");
  2036. }
  2037. TEST_F(CompilerTest, CodeGenBarycentricsThreeSV) {
  2038. if (m_ver.SkipDxilVersion(1,1)) return;
  2039. CodeGenTestCheck(L"barycentricsThreeSV.hlsl");
  2040. }
  2041. TEST_F(CompilerTest, CodeGenBitCast16Bits) {
  2042. if (m_ver.SkipDxilVersion(1, 2)) return;
  2043. CodeGenTestCheck(L"bitcast_16bits.hlsl");
  2044. }
  2045. TEST_F(CompilerTest, CodeGenCbuffer64Types) {
  2046. if (m_ver.SkipDxilVersion(1, 2)) return;
  2047. CodeGenTestCheck(L"cbuffer64Types.hlsl");
  2048. }
  2049. TEST_F(CompilerTest, CodeGenCbufferHalf) {
  2050. if (m_ver.SkipDxilVersion(1, 2)) return;
  2051. CodeGenTestCheck(L"cbufferHalf.hlsl");
  2052. }
  2053. TEST_F(CompilerTest, CodeGenCbufferHalfStruct) {
  2054. if (m_ver.SkipDxilVersion(1, 2)) return;
  2055. CodeGenTestCheck(L"cbufferHalf-struct.hlsl");
  2056. }
  2057. TEST_F(CompilerTest, CodeGenCbufferInt16) {
  2058. if (m_ver.SkipDxilVersion(1, 2)) return;
  2059. CodeGenTestCheck(L"cbufferInt16.hlsl");
  2060. }
  2061. TEST_F(CompilerTest, CodeGenCbufferInt16Struct) {
  2062. if (m_ver.SkipDxilVersion(1, 2)) return;
  2063. CodeGenTestCheck(L"cbufferInt16-struct.hlsl");
  2064. }
  2065. TEST_F(CompilerTest, CodeGenDataLayoutHalf) {
  2066. if (m_ver.SkipDxilVersion(1, 2)) return;
  2067. CodeGenTestCheck(L"dataLayoutHalf.hlsl");
  2068. }
  2069. TEST_F(CompilerTest, CodeGenEnum3) {
  2070. if (m_ver.SkipDxilVersion(1,1)) return;
  2071. CodeGenTestCheck(L"enum3.hlsl");
  2072. }
  2073. TEST_F(CompilerTest, CodeGenExpandTrig) {
  2074. CodeGenTestCheckBatchDir(L"expand_trig");
  2075. }
  2076. struct FPEnableExceptionsScope
  2077. {
  2078. // _controlfp_s is non-standard and <cfenv> doesn't have a function to enable exceptions
  2079. #ifdef _WIN32
  2080. unsigned int previousValue;
  2081. FPEnableExceptionsScope() {
  2082. VERIFY_IS_TRUE(_controlfp_s(&previousValue, 0, _MCW_EM) == 0); // _MCW_EM == 0 means enable all exceptions
  2083. }
  2084. ~FPEnableExceptionsScope() {
  2085. unsigned int newValue;
  2086. errno_t error = _controlfp_s(&newValue, previousValue, _MCW_EM);
  2087. DXASSERT(error == 0, "Failed to restore floating-point environment.");
  2088. (void)error;
  2089. }
  2090. #endif
  2091. };
  2092. TEST_F(CompilerTest, CodeGenFloatingPointEnvironment) {
  2093. FPEnableExceptionsScope fpEnableExceptions;
  2094. CodeGenTestCheck(L"fpexcept.hlsl");
  2095. }
  2096. TEST_F(CompilerTest, CodeGenFixedWidthTypes) {
  2097. if (m_ver.SkipDxilVersion(1, 2)) return;
  2098. CodeGenTestCheck(L"fixedWidth.hlsl");
  2099. }
  2100. TEST_F(CompilerTest, CodeGenFixedWidthTypes16Bit) {
  2101. if (m_ver.SkipDxilVersion(1, 2)) return;
  2102. CodeGenTestCheck(L"fixedWidth16Bit.hlsl");
  2103. }
  2104. TEST_F(CompilerTest, CodeGenFunctionAttribute) {
  2105. if (m_ver.SkipDxilVersion(1, 2)) return;
  2106. CodeGenTestCheck(L"functionAttribute.hlsl");
  2107. }
  2108. TEST_F(CompilerTest, CodeGenInclude) {
  2109. CodeGenTestCheck(L"Include.hlsl");
  2110. }
  2111. TEST_F(CompilerTest, CodeGenInt16Op) {
  2112. if (m_ver.SkipDxilVersion(1, 2)) return;
  2113. CodeGenTestCheck(L"int16Op.hlsl");
  2114. }
  2115. TEST_F(CompilerTest, CodeGenInt16OpBits) {
  2116. if (m_ver.SkipDxilVersion(1, 2)) return;
  2117. CodeGenTestCheck(L"int16OpBits.hlsl");
  2118. }
  2119. TEST_F(CompilerTest, CodeGenLibCsEntry) {
  2120. CodeGenTestCheck(L"lib_cs_entry.hlsl");
  2121. }
  2122. TEST_F(CompilerTest, CodeGenLibCsEntry2) {
  2123. CodeGenTestCheck(L"lib_cs_entry2.hlsl");
  2124. }
  2125. TEST_F(CompilerTest, CodeGenLibCsEntry3) {
  2126. CodeGenTestCheck(L"lib_cs_entry3.hlsl");
  2127. }
  2128. TEST_F(CompilerTest, CodeGenLibEntries) {
  2129. CodeGenTestCheck(L"lib_entries.hlsl");
  2130. }
  2131. TEST_F(CompilerTest, CodeGenLibEntries2) {
  2132. CodeGenTestCheck(L"lib_entries2.hlsl");
  2133. }
  2134. TEST_F(CompilerTest, CodeGenLibNoAlias) {
  2135. CodeGenTestCheck(L"lib_no_alias.hlsl");
  2136. }
  2137. TEST_F(CompilerTest, CodeGenLibResource) {
  2138. CodeGenTestCheck(L"lib_resource.hlsl");
  2139. }
  2140. TEST_F(CompilerTest, CodeGenLibUnusedFunc) {
  2141. CodeGenTestCheck(L"lib_unused_func.hlsl");
  2142. }
  2143. TEST_F(CompilerTest, CodeGenMatIn1) {
  2144. if (m_ver.SkipIRSensitiveTest()) return;
  2145. CodeGenTestCheck(L"matrixIn1.hlsl");
  2146. }
  2147. TEST_F(CompilerTest, CodeGenMatIn2) {
  2148. if (m_ver.SkipIRSensitiveTest()) return;
  2149. CodeGenTestCheck(L"matrixIn2.hlsl");
  2150. }
  2151. TEST_F(CompilerTest, CodeGenMultiUAVLoad2) {
  2152. if (m_ver.SkipDxilVersion(1,1)) return;
  2153. CodeGenTestCheck(L"multiUAVLoad2.hlsl");
  2154. }
  2155. TEST_F(CompilerTest, CodeGenMultiUAVLoad4) {
  2156. if (m_ver.SkipDxilVersion(1,1)) return;
  2157. CodeGenTestCheck(L"multiUAVLoad4.hlsl");
  2158. }
  2159. TEST_F(CompilerTest, CodeGenMultiUAVLoad5) {
  2160. if (m_ver.SkipDxilVersion(1,1)) return;
  2161. CodeGenTestCheck(L"multiUAVLoad5.hlsl");
  2162. }
  2163. TEST_F(CompilerTest, CodeGenMultiUAVLoad6) {
  2164. if (m_ver.SkipDxilVersion(1,1)) return;
  2165. CodeGenTestCheck(L"multiUAVLoad6.hlsl");
  2166. }
  2167. TEST_F(CompilerTest, CodeGenMultiUAVLoad7) {
  2168. if (m_ver.SkipDxilVersion(1,1)) return;
  2169. CodeGenTestCheck(L"multiUAVLoad7.hlsl");
  2170. }
  2171. TEST_F(CompilerTest, CodeGenRaw_Buf2) {
  2172. if (m_ver.SkipDxilVersion(1, 2)) return;
  2173. CodeGenTestCheck(L"raw_buf2.hlsl");
  2174. }
  2175. TEST_F(CompilerTest, CodeGenRaw_Buf4) {
  2176. if (m_ver.SkipDxilVersion(1, 2)) return;
  2177. CodeGenTestCheck(L"raw_buf4.hlsl");
  2178. }
  2179. TEST_F(CompilerTest, CodeGenRaw_Buf5) {
  2180. if (m_ver.SkipDxilVersion(1, 2)) return;
  2181. CodeGenTestCheck(L"raw_buf5.hlsl");
  2182. }
  2183. TEST_F(CompilerTest, CodeGenSignaturePackingByWidth) {
  2184. if (m_ver.SkipDxilVersion(1, 2)) return;
  2185. CodeGenTestCheck(L"signature_packing_by_width.hlsl");
  2186. }
  2187. TEST_F(CompilerTest, CodeGenStruct_Buf2) {
  2188. if (m_ver.SkipDxilVersion(1, 2)) return;
  2189. CodeGenTestCheck(L"struct_buf2.hlsl");
  2190. }
  2191. TEST_F(CompilerTest, CodeGenStruct_Buf3) {
  2192. if (m_ver.SkipDxilVersion(1, 2)) return;
  2193. CodeGenTestCheck(L"struct_buf3.hlsl");
  2194. }
  2195. TEST_F(CompilerTest, CodeGenStruct_Buf4) {
  2196. if (m_ver.SkipDxilVersion(1, 2)) return;
  2197. CodeGenTestCheck(L"struct_buf4.hlsl");
  2198. }
  2199. TEST_F(CompilerTest, CodeGenStruct_Buf5) {
  2200. if (m_ver.SkipDxilVersion(1, 2)) return;
  2201. CodeGenTestCheck(L"struct_buf5.hlsl");
  2202. }
  2203. TEST_F(CompilerTest, CodeGenStruct_Buf6) {
  2204. if (m_ver.SkipDxilVersion(1, 2)) return;
  2205. CodeGenTestCheck(L"struct_buf6.hlsl");
  2206. }
  2207. TEST_F(CompilerTest, CodeGenStruct_Buf_New_Layout) {
  2208. if (m_ver.SkipDxilVersion(1, 2)) return;
  2209. CodeGenTestCheck(L"struct_buf_new_layout.hlsl");
  2210. }
  2211. TEST_F(CompilerTest, CodeGenUav_Typed_Load_Store3) {
  2212. if (m_ver.SkipDxilVersion(1,2)) return;
  2213. CodeGenTestCheck(L"uav_typed_load_store3.hlsl");
  2214. }
  2215. TEST_F(CompilerTest, CodeGenUint64_2) {
  2216. if (m_ver.SkipDxilVersion(1,1)) return;
  2217. CodeGenTestCheck(L"uint64_2.hlsl");
  2218. }
  2219. TEST_F(CompilerTest, CodeGenLiterals_Exact_Precision_Mod) {
  2220. if (m_ver.SkipDxilVersion(1, 2)) return;
  2221. CodeGenTestCheck(L"literals_exact_precision_Mod.hlsl");
  2222. }
  2223. TEST_F(CompilerTest, CodeGenTypedBufferHalf) {
  2224. if (m_ver.SkipDxilVersion(1, 2)) return;
  2225. CodeGenTestCheck(L"typed_buffer_half.hlsl");
  2226. }
  2227. TEST_F(CompilerTest, CodeGenRootSigProfile) {
  2228. CodeGenTest(L"rootSigProfile.hlsl");
  2229. }
  2230. TEST_F(CompilerTest, CodeGenRootSigProfile2) {
  2231. // TODO: Verify the result when reflect the structures.
  2232. CodeGenTest(L"rootSigProfile2.hlsl");
  2233. }
  2234. TEST_F(CompilerTest, CodeGenRootSigProfile5) {
  2235. CodeGenTest(L"rootSigProfile5.hlsl");
  2236. }
  2237. TEST_F(CompilerTest, CodeGenDX11Sample){
  2238. CodeGenTestCheckBatchDir(L"Samples\\DX11");
  2239. }
  2240. TEST_F(CompilerTest, CodeGenSamplesD12) {
  2241. CodeGenTestCheckBatchDir(L"Samples\\D12");
  2242. }
  2243. TEST_F(CompilerTest, CodeGenDx12MiniEngine) {
  2244. CodeGenTestCheckBatchDir(L"Samples\\MiniEngine");
  2245. }
  2246. TEST_F(CompilerTest, HoistConstantArray) {
  2247. CodeGenTestCheckBatchDir(L"hoist_constant_array");
  2248. }
  2249. TEST_F(CompilerTest, PreprocessWhenValidThenOK) {
  2250. CComPtr<IDxcCompiler> pCompiler;
  2251. CComPtr<IDxcOperationResult> pResult;
  2252. CComPtr<IDxcBlobEncoding> pSource;
  2253. DxcDefine defines[2];
  2254. defines[0].Name = L"MYDEF";
  2255. defines[0].Value = L"int";
  2256. defines[1].Name = L"MYOTHERDEF";
  2257. defines[1].Value = L"123";
  2258. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2259. CreateBlobFromText(
  2260. "// First line\r\n"
  2261. "MYDEF g_int = MYOTHERDEF;\r\n"
  2262. "#define FOO BAR\r\n"
  2263. "int FOO;", &pSource);
  2264. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl", nullptr, 0,
  2265. defines, _countof(defines), nullptr,
  2266. &pResult));
  2267. HRESULT hrOp;
  2268. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  2269. VERIFY_SUCCEEDED(hrOp);
  2270. CComPtr<IDxcBlob> pOutText;
  2271. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  2272. std::string text(BlobToUtf8(pOutText));
  2273. VERIFY_ARE_EQUAL_STR(
  2274. "#line 1 \"file.hlsl\"\n"
  2275. "\n"
  2276. "int g_int = 123;\n"
  2277. "\n"
  2278. "int BAR;\n", text.c_str());
  2279. }
  2280. TEST_F(CompilerTest, PreprocessWhenExpandTokenPastingOperandThenAccept) {
  2281. // Tests that we can turn on fxc's behavior (pre-expanding operands before
  2282. // performing token-pasting) using -flegacy-macro-expansion
  2283. CComPtr<IDxcCompiler> pCompiler;
  2284. CComPtr<IDxcOperationResult> pResult;
  2285. CComPtr<IDxcBlobEncoding> pSource;
  2286. LPCWSTR expandOption = L"-flegacy-macro-expansion";
  2287. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2288. CreateBlobFromText(R"(
  2289. #define SET_INDEX0 10
  2290. #define BINDING_INDEX0 5
  2291. #define SET(INDEX) SET_INDEX##INDEX
  2292. #define BINDING(INDEX) BINDING_INDEX##INDEX
  2293. #define SET_BIND(NAME,SET,BIND) resource_set_##SET##_bind_##BIND##_##NAME
  2294. #define RESOURCE(NAME,INDEX) SET_BIND(NAME, SET(INDEX), BINDING(INDEX))
  2295. Texture2D<float4> resource_set_10_bind_5_tex;
  2296. float4 main() : SV_Target{
  2297. return RESOURCE(tex, 0)[uint2(1, 2)];
  2298. }
  2299. )",
  2300. &pSource);
  2301. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl", &expandOption,
  2302. 1, nullptr, 0, nullptr, &pResult));
  2303. HRESULT hrOp;
  2304. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  2305. VERIFY_SUCCEEDED(hrOp);
  2306. CComPtr<IDxcBlob> pOutText;
  2307. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  2308. std::string text(BlobToUtf8(pOutText));
  2309. VERIFY_ARE_EQUAL_STR(R"(#line 1 "file.hlsl"
  2310. #line 12 "file.hlsl"
  2311. Texture2D<float4> resource_set_10_bind_5_tex;
  2312. float4 main() : SV_Target{
  2313. return resource_set_10_bind_5_tex[uint2(1, 2)];
  2314. }
  2315. )",
  2316. text.c_str());
  2317. }
  2318. TEST_F(CompilerTest, WhenSigMismatchPCFunctionThenFail) {
  2319. CComPtr<IDxcCompiler> pCompiler;
  2320. CComPtr<IDxcOperationResult> pResult;
  2321. CComPtr<IDxcBlobEncoding> pSource;
  2322. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2323. CreateBlobFromText(
  2324. "struct PSSceneIn \n\
  2325. { \n\
  2326. float4 pos : SV_Position; \n\
  2327. float2 tex : TEXCOORD0; \n\
  2328. float3 norm : NORMAL; \n\
  2329. }; \n"
  2330. "struct HSPerPatchData { \n\
  2331. float edges[ 3 ] : SV_TessFactor; \n\
  2332. float inside : SV_InsideTessFactor; \n\
  2333. float foo : FOO; \n\
  2334. }; \n"
  2335. "HSPerPatchData HSPerPatchFunc( InputPatch< PSSceneIn, 3 > points, \n\
  2336. OutputPatch<PSSceneIn, 3> outpoints) { \n\
  2337. HSPerPatchData d = (HSPerPatchData)0; \n\
  2338. d.edges[ 0 ] = points[0].tex.x + outpoints[0].tex.x; \n\
  2339. d.edges[ 1 ] = 1; \n\
  2340. d.edges[ 2 ] = 1; \n\
  2341. d.inside = 1; \n\
  2342. return d; \n\
  2343. } \n"
  2344. "[domain(\"tri\")] \n\
  2345. [partitioning(\"fractional_odd\")] \n\
  2346. [outputtopology(\"triangle_cw\")] \n\
  2347. [patchconstantfunc(\"HSPerPatchFunc\")] \n\
  2348. [outputcontrolpoints(3)] \n"
  2349. "void main(const uint id : SV_OutputControlPointID, \n\
  2350. const InputPatch< PSSceneIn, 3 > points ) { \n\
  2351. } \n"
  2352. , &pSource);
  2353. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2354. L"hs_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  2355. std::string failLog(VerifyOperationFailed(pResult));
  2356. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find(
  2357. "Signature element SV_Position, referred to by patch constant function, is not found in corresponding hull shader output."));
  2358. }
  2359. TEST_F(CompilerTest, ViewID) {
  2360. if (m_ver.SkipDxilVersion(1,1)) return;
  2361. CodeGenTestCheckBatchDir(L"viewid");
  2362. }
  2363. TEST_F(CompilerTest, SubobjectCodeGenErrors) {
  2364. struct SubobjectErrorTestCase {
  2365. const char *shaderText;
  2366. const char *expectedError;
  2367. };
  2368. SubobjectErrorTestCase testCases[] = {
  2369. { "GlobalRootSignature grs;", "1:1: error: subobject needs to be initialized" },
  2370. { "StateObjectConfig soc;", "1:1: error: subobject needs to be initialized" },
  2371. { "LocalRootSignature lrs;", "1:1: error: subobject needs to be initialized" },
  2372. { "SubobjectToExportsAssociation sea;", "1:1: error: subobject needs to be initialized" },
  2373. { "RaytracingShaderConfig rsc;", "1:1: error: subobject needs to be initialized" },
  2374. { "RaytracingPipelineConfig rpc;", "1:1: error: subobject needs to be initialized" },
  2375. { "TriangleHitGroup hitGt;", "1:1: error: subobject needs to be initialized" },
  2376. { "ProceduralPrimitiveHitGroup hitGt;", "1:1: error: subobject needs to be initialized" },
  2377. { "GlobalRootSignature grs2 = {\"\"};", "1:29: error: empty string not expected here" },
  2378. { "LocalRootSignature lrs2 = {\"\"};", "1:28: error: empty string not expected here" },
  2379. { "SubobjectToExportsAssociation sea2 = { \"\", \"x\" };", "1:40: error: empty string not expected here" },
  2380. { "string s; SubobjectToExportsAssociation sea4 = { \"x\", s };", "1:55: error: cannot convert to constant string" },
  2381. { "extern int v; RaytracingPipelineConfig rpc2 = { v + 16 };", "1:49: error: cannot convert to constant unsigned int" },
  2382. { "string s; TriangleHitGroup trHitGt2_8 = { s, \"foo\" };", "1:43: error: cannot convert to constant string" },
  2383. { "string s; ProceduralPrimitiveHitGroup ppHitGt2_8 = { s, \"\", s };", "1:54: error: cannot convert to constant string" },
  2384. { "ProceduralPrimitiveHitGroup ppHitGt2_9 = { \"a\", \"b\", \"\"};", "1:54: error: empty string not expected here" }
  2385. };
  2386. for (unsigned i = 0; i < _countof(testCases); i++) {
  2387. CComPtr<IDxcCompiler> pCompiler;
  2388. CComPtr<IDxcOperationResult> pResult;
  2389. CComPtr<IDxcBlobEncoding> pSource;
  2390. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2391. CreateBlobFromText(testCases[i].shaderText, &pSource);
  2392. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"", L"lib_6_4", nullptr, 0, nullptr, 0, nullptr, &pResult));
  2393. std::string failLog(VerifyOperationFailed(pResult));
  2394. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find(testCases[i].expectedError));
  2395. }
  2396. }
  2397. TEST_F(CompilerTest, Unroll) {
  2398. CodeGenTestCheckBatchDir(L"unroll");
  2399. }
  2400. TEST_F(CompilerTest, DebugInfo) {
  2401. CodeGenTestCheckBatchDir(L"..\\CodeGenHLSL\\debug");
  2402. }
  2403. TEST_F(CompilerTest, ShaderCompatSuite) {
  2404. CodeGenTestCheckBatchDir(L"shader-compat-suite");
  2405. }
  2406. TEST_F(CompilerTest, QuickTest) {
  2407. CodeGenTestCheckBatchDir(L"quick-test");
  2408. }
  2409. TEST_F(CompilerTest, QuickLlTest) {
  2410. CodeGenTestCheckBatchDir(L"quick-ll-test");
  2411. }
  2412. #ifdef _WIN32
  2413. TEST_F(CompilerTest, ManualFileCheckTest) {
  2414. #else
  2415. TEST_F(CompilerTest, DISABLED_ManualFileCheckTest) {
  2416. #endif
  2417. using namespace llvm;
  2418. using namespace WEX::TestExecution;
  2419. WEX::Common::String value;
  2420. VERIFY_SUCCEEDED(RuntimeParameters::TryGetValue(L"InputPath", value));
  2421. std::wstring path = value;
  2422. if (!llvm::sys::path::is_absolute(CW2A(path.c_str()).m_psz)) {
  2423. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  2424. }
  2425. bool isDirectory;
  2426. {
  2427. // Temporarily setup the filesystem for testing whether the path is a directory.
  2428. // If it is, CodeGenTestCheckBatchDir will create its own instance.
  2429. llvm::sys::fs::MSFileSystem *msfPtr;
  2430. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  2431. std::unique_ptr<llvm::sys::fs::MSFileSystem> msf(msfPtr);
  2432. llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  2433. IFTLLVM(pts.error_code());
  2434. isDirectory = llvm::sys::fs::is_directory(CW2A(path.c_str()).m_psz);
  2435. }
  2436. if (isDirectory) {
  2437. CodeGenTestCheckBatchDir(path, /*implicitDir*/ false);
  2438. } else {
  2439. CodeGenTestCheck(path.c_str(), /*implicitDir*/ false);
  2440. }
  2441. }
  2442. TEST_F(CompilerTest, CodeGenDeclarations) {
  2443. CodeGenTestCheckBatchDir(L"declarations");
  2444. }
  2445. TEST_F(CompilerTest, CodeGenExpressions) {
  2446. CodeGenTestCheckBatchDir(L"expressions");
  2447. }
  2448. TEST_F(CompilerTest, CodeGenPreprocessor) {
  2449. CodeGenTestCheckBatchDir(L"preprocessor");
  2450. }
  2451. TEST_F(CompilerTest, CodeGenShaderStages) {
  2452. CodeGenTestCheckBatchDir(L"..\\CodeGenHLSL\\shader_stages");
  2453. }