ExecutionTest.cpp 177 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // ExecutionTest.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. // These tests run by executing compiled programs, and thus involve more //
  9. // moving parts, like the runtime and drivers. //
  10. // //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include <algorithm>
  13. #include <memory>
  14. #include <vector>
  15. #include <string>
  16. #include <map>
  17. #include <unordered_set>
  18. #include <strstream>
  19. #include <iomanip>
  20. #include "CompilationResult.h"
  21. #include "HLSLTestData.h"
  22. #include <Shlwapi.h>
  23. #include <atlcoll.h>
  24. #include <locale>
  25. #include <algorithm>
  26. #undef _read
  27. #include "WexTestClass.h"
  28. #include "HlslTestUtils.h"
  29. #include "DxcTestUtils.h"
  30. #include "dxc/Support/Global.h"
  31. #include "dxc/Support/WinIncludes.h"
  32. #include "dxc/Support/FileIOHelper.h"
  33. #include "dxc/Support/Unicode.h"
  34. //
  35. // d3d12.h and dxgi1_4.h are included in the Windows 10 SDK
  36. // https://msdn.microsoft.com/en-us/library/windows/desktop/dn899120(v=vs.85).aspx
  37. // https://developer.microsoft.com/en-US/windows/downloads/windows-10-sdk
  38. //
  39. #include <d3d12.h>
  40. #include <dxgi1_4.h>
  41. #include <DXGIDebug.h>
  42. #include <D3dx12.h>
  43. #include <DirectXMath.h>
  44. #include <strsafe.h>
  45. #include <d3dcompiler.h>
  46. #include <wincodec.h>
  47. #include "ShaderOpTest.h"
  48. #pragma comment(lib, "d3dcompiler.lib")
  49. #pragma comment(lib, "windowscodecs.lib")
  50. #pragma comment(lib, "dxguid.lib")
  51. #pragma comment(lib, "version.lib")
  52. // A more recent Windows SDK than currently required is needed for these.
  53. typedef HRESULT(WINAPI *D3D12EnableExperimentalFeaturesFn)(
  54. UINT NumFeatures,
  55. __in_ecount(NumFeatures) const IID* pIIDs,
  56. __in_ecount_opt(NumFeatures) void* pConfigurationStructs,
  57. __in_ecount_opt(NumFeatures) UINT* pConfigurationStructSizes);
  58. static const GUID D3D12ExperimentalShaderModelsID = { /* 76f5573e-f13a-40f5-b297-81ce9e18933f */
  59. 0x76f5573e,
  60. 0xf13a,
  61. 0x40f5,
  62. { 0xb2, 0x97, 0x81, 0xce, 0x9e, 0x18, 0x93, 0x3f }
  63. };
  64. using namespace DirectX;
  65. using namespace hlsl_test;
  66. template <typename TSequence, typename T>
  67. static bool contains(TSequence s, const T &val) {
  68. return std::cend(s) != std::find(std::cbegin(s), std::cend(s), val);
  69. }
  70. template <typename InputIterator, typename T>
  71. static bool contains(InputIterator b, InputIterator e, const T &val) {
  72. return e != std::find(b, e, val);
  73. }
  74. static HRESULT EnableExperimentalShaderModels() {
  75. HMODULE hRuntime = LoadLibraryW(L"d3d12.dll");
  76. if (hRuntime == NULL) {
  77. return HRESULT_FROM_WIN32(GetLastError());
  78. }
  79. D3D12EnableExperimentalFeaturesFn pD3D12EnableExperimentalFeatures =
  80. (D3D12EnableExperimentalFeaturesFn)GetProcAddress(hRuntime, "D3D12EnableExperimentalFeatures");
  81. if (pD3D12EnableExperimentalFeatures == nullptr) {
  82. FreeLibrary(hRuntime);
  83. return HRESULT_FROM_WIN32(GetLastError());
  84. }
  85. HRESULT hr = pD3D12EnableExperimentalFeatures(1, &D3D12ExperimentalShaderModelsID, nullptr, nullptr);
  86. FreeLibrary(hRuntime);
  87. return hr;
  88. }
  89. static HRESULT ReportLiveObjects() {
  90. CComPtr<IDXGIDebug1> pDebug;
  91. IFR(DXGIGetDebugInterface1(0, IID_PPV_ARGS(&pDebug)));
  92. IFR(pDebug->ReportLiveObjects(DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL));
  93. return S_OK;
  94. }
  95. static void WriteInfoQueueMessages(void *pStrCtx, st::OutputStringFn pOutputStrFn, ID3D12InfoQueue *pInfoQueue) {
  96. bool allMessagesOK = true;
  97. UINT64 count = pInfoQueue->GetNumStoredMessages();
  98. CAtlArray<BYTE> message;
  99. for (UINT64 i = 0; i < count; ++i) {
  100. // 'GetMessageA' rather than 'GetMessage' is an artifact of user32 headers.
  101. SIZE_T msgLen = 0;
  102. if (FAILED(pInfoQueue->GetMessageA(i, nullptr, &msgLen))) {
  103. allMessagesOK = false;
  104. continue;
  105. }
  106. if (message.GetCount() < msgLen) {
  107. if (!message.SetCount(msgLen)) {
  108. allMessagesOK = false;
  109. continue;
  110. }
  111. }
  112. D3D12_MESSAGE *pMessage = (D3D12_MESSAGE *)message.GetData();
  113. if (FAILED(pInfoQueue->GetMessageA(i, pMessage, &msgLen))) {
  114. allMessagesOK = false;
  115. continue;
  116. }
  117. CA2W msgW(pMessage->pDescription, CP_ACP);
  118. pOutputStrFn(pStrCtx, msgW.m_psz);
  119. pOutputStrFn(pStrCtx, L"\r\n");
  120. }
  121. if (!allMessagesOK) {
  122. pOutputStrFn(pStrCtx, L"Failed to retrieve some messages.\r\n");
  123. }
  124. }
  125. class CComContext {
  126. private:
  127. bool m_init;
  128. public:
  129. CComContext() : m_init(false) {}
  130. ~CComContext() { Dispose(); }
  131. void Dispose() { if (!m_init) return; m_init = false; CoUninitialize(); }
  132. HRESULT Init() { HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED); if (SUCCEEDED(hr)) { m_init = true; } return hr; }
  133. };
  134. static void SavePixelsToFile(LPCVOID pPixels, DXGI_FORMAT format, UINT32 m_width, UINT32 m_height, LPCWSTR pFileName) {
  135. CComContext ctx;
  136. CComPtr<IWICImagingFactory> pFactory;
  137. CComPtr<IWICBitmap> pBitmap;
  138. CComPtr<IWICBitmapEncoder> pEncoder;
  139. CComPtr<IWICBitmapFrameEncode> pFrameEncode;
  140. CComPtr<hlsl::AbstractMemoryStream> pStream;
  141. CComPtr<IMalloc> pMalloc;
  142. struct PF {
  143. DXGI_FORMAT Format;
  144. GUID PixelFormat;
  145. UINT32 PixelSize;
  146. bool operator==(DXGI_FORMAT F) const {
  147. return F == Format;
  148. }
  149. } Vals[] = {
  150. // Add more pixel format mappings as needed.
  151. { DXGI_FORMAT_R8G8B8A8_UNORM, GUID_WICPixelFormat32bppRGBA, 4 }
  152. };
  153. PF *pFormat = std::find(Vals, Vals + _countof(Vals), format);
  154. VERIFY_SUCCEEDED(ctx.Init());
  155. VERIFY_SUCCEEDED(CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&pFactory));
  156. VERIFY_SUCCEEDED(CoGetMalloc(1, &pMalloc));
  157. VERIFY_SUCCEEDED(hlsl::CreateMemoryStream(pMalloc, &pStream));
  158. VERIFY_ARE_NOT_EQUAL(pFormat, Vals + _countof(Vals));
  159. VERIFY_SUCCEEDED(pFactory->CreateBitmapFromMemory(m_width, m_height, pFormat->PixelFormat, m_width * pFormat->PixelSize, m_width * m_height * pFormat->PixelSize, (BYTE *)pPixels, &pBitmap));
  160. VERIFY_SUCCEEDED(pFactory->CreateEncoder(GUID_ContainerFormatBmp, nullptr, &pEncoder));
  161. VERIFY_SUCCEEDED(pEncoder->Initialize(pStream, WICBitmapEncoderNoCache));
  162. VERIFY_SUCCEEDED(pEncoder->CreateNewFrame(&pFrameEncode, nullptr));
  163. VERIFY_SUCCEEDED(pFrameEncode->Initialize(nullptr));
  164. VERIFY_SUCCEEDED(pFrameEncode->WriteSource(pBitmap, nullptr));
  165. VERIFY_SUCCEEDED(pFrameEncode->Commit());
  166. VERIFY_SUCCEEDED(pEncoder->Commit());
  167. hlsl::WriteBinaryFile(pFileName, pStream->GetPtr(), pStream->GetPtrSize());
  168. }
  169. // Setup for wave intrinsics tests
  170. enum class ShaderOpKind {
  171. WaveSum,
  172. WaveProduct,
  173. WaveActiveMax,
  174. WaveActiveMin,
  175. WaveCountBits,
  176. WaveActiveAllEqual,
  177. WaveActiveAnyTrue,
  178. WaveActiveAllTrue,
  179. WaveActiveBitOr,
  180. WaveActiveBitAnd,
  181. WaveActiveBitXor,
  182. ShaderOpInvalid
  183. };
  184. struct ShaderOpKindPair {
  185. LPCWSTR name;
  186. ShaderOpKind kind;
  187. };
  188. static ShaderOpKindPair ShaderOpKindTable[] = {
  189. { L"WaveActiveSum", ShaderOpKind::WaveSum },
  190. { L"WaveActiveUSum", ShaderOpKind::WaveSum },
  191. { L"WaveActiveProduct", ShaderOpKind::WaveProduct },
  192. { L"WaveActiveUProduct", ShaderOpKind::WaveProduct },
  193. { L"WaveActiveMax", ShaderOpKind::WaveActiveMax },
  194. { L"WaveActiveUMax", ShaderOpKind::WaveActiveMax },
  195. { L"WaveActiveMin", ShaderOpKind::WaveActiveMin },
  196. { L"WaveActiveUMin", ShaderOpKind::WaveActiveMin },
  197. { L"WaveActiveCountBits", ShaderOpKind::WaveCountBits },
  198. { L"WaveActiveAllEqual", ShaderOpKind::WaveActiveAllEqual },
  199. { L"WaveActiveAnyTrue", ShaderOpKind::WaveActiveAnyTrue },
  200. { L"WaveActiveAllTrue", ShaderOpKind::WaveActiveAllTrue },
  201. { L"WaveActiveBitOr", ShaderOpKind::WaveActiveBitOr },
  202. { L"WaveActiveBitAnd", ShaderOpKind::WaveActiveBitAnd },
  203. { L"WaveActiveBitXor", ShaderOpKind::WaveActiveBitXor },
  204. { L"WavePrefixSum", ShaderOpKind::WaveSum },
  205. { L"WavePrefixUSum", ShaderOpKind::WaveSum },
  206. { L"WavePrefixProduct", ShaderOpKind::WaveProduct },
  207. { L"WavePrefixUProduct", ShaderOpKind::WaveProduct },
  208. { L"WavePrefixMax", ShaderOpKind::WaveActiveMax },
  209. { L"WavePrefixUMax", ShaderOpKind::WaveActiveMax },
  210. { L"WavePrefixMin", ShaderOpKind::WaveActiveMin },
  211. { L"WavePrefixUMin", ShaderOpKind::WaveActiveMin },
  212. { L"WavePrefixCountBits", ShaderOpKind::WaveCountBits }
  213. };
  214. ShaderOpKind GetShaderOpKind(LPCWSTR str) {
  215. for (size_t i = 0; i < sizeof(ShaderOpKindTable)/sizeof(ShaderOpKindPair); ++i) {
  216. if (_wcsicmp(ShaderOpKindTable[i].name, str) == 0) {
  217. return ShaderOpKindTable[i].kind;
  218. }
  219. }
  220. DXASSERT(false, "Invalid ShaderOp name: %s", str);
  221. return ShaderOpKind::ShaderOpInvalid;
  222. }
  223. // Virtual class to compute the expected result given a set of inputs
  224. struct TableParameter;
  225. template <typename InType, typename OutType, ShaderOpKind kind>
  226. struct computeExpected {
  227. OutType operator()(const std::vector<InType> &inputs,
  228. const std::vector<int> &masks, int maskValue,
  229. unsigned int index) {
  230. return 0;
  231. }
  232. };
  233. template <typename InType, typename OutType>
  234. struct computeExpected<InType, OutType, ShaderOpKind::WaveSum> {
  235. OutType operator()(const std::vector<InType> &inputs,
  236. const std::vector<int> &masks, int maskValue,
  237. unsigned int index) {
  238. OutType sum = 0;
  239. for (size_t i = 0; i < index; ++i) {
  240. if (masks.at(i) == maskValue) {
  241. sum += inputs.at(i);
  242. }
  243. }
  244. return sum;
  245. }
  246. };
  247. template <typename InType, typename OutType>
  248. struct computeExpected<InType, OutType, ShaderOpKind::WaveProduct> {
  249. OutType operator()(const std::vector<InType> &inputs,
  250. const std::vector<int> &masks, int maskValue,
  251. unsigned int index) {
  252. OutType prod = 1;
  253. for (size_t i = 0; i < index; ++i) {
  254. if (masks.at(i) == maskValue) {
  255. prod *= inputs.at(i);
  256. }
  257. }
  258. return prod;
  259. }
  260. };
  261. template <typename InType, typename OutType>
  262. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveMax> {
  263. OutType operator()(const std::vector<InType> &inputs,
  264. const std::vector<int> &masks, int maskValue,
  265. unsigned int index) {
  266. OutType maximum = std::numeric_limits<OutType>::min();
  267. for (size_t i = 0; i < index; ++i) {
  268. if (masks.at(i) == maskValue && inputs.at(i) > maximum)
  269. maximum = inputs.at(i);
  270. }
  271. return maximum;
  272. }
  273. };
  274. template <typename InType, typename OutType>
  275. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveMin> {
  276. OutType operator()(const std::vector<InType> &inputs,
  277. const std::vector<int> &masks, int maskValue,
  278. unsigned int index) {
  279. OutType minimum = std::numeric_limits<OutType>::max();
  280. for (size_t i = 0; i < index; ++i) {
  281. if (masks.at(i) == maskValue && inputs.at(i) < minimum)
  282. minimum = inputs.at(i);
  283. }
  284. return minimum;
  285. }
  286. };
  287. template <typename InType, typename OutType>
  288. struct computeExpected<InType, OutType, ShaderOpKind::WaveCountBits> {
  289. OutType operator()(const std::vector<InType> &inputs,
  290. const std::vector<int> &masks, int maskValue,
  291. unsigned int index) {
  292. OutType count = 0;
  293. for (size_t i = 0; i < index; ++i) {
  294. if (masks.at(i) == maskValue && inputs.at(i) > 3) {
  295. count++;
  296. }
  297. }
  298. return count;
  299. }
  300. };
  301. // In HLSL, boolean is represented in a 4 byte (uint32) format,
  302. // So we cannot use c++ bool type to represent bool in HLSL
  303. // HLSL returns 0 for false and 1 for true
  304. template <typename InType, typename OutType>
  305. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveAnyTrue> {
  306. OutType operator()(const std::vector<InType> &inputs,
  307. const std::vector<int> &masks, int maskValue,
  308. unsigned int index) {
  309. for (size_t i = 0; i < index; ++i) {
  310. if (masks.at(i) == maskValue && inputs.at(i) != 0) {
  311. return 1;
  312. }
  313. }
  314. return 0;
  315. }
  316. };
  317. template <typename InType, typename OutType>
  318. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveAllTrue> {
  319. OutType operator()(const std::vector<InType> &inputs,
  320. const std::vector<int> &masks, int maskValue,
  321. unsigned int index) {
  322. for (size_t i = 0; i < index; ++i) {
  323. if (masks.at(i) == maskValue && inputs.at(i) == 0) {
  324. return 0;
  325. }
  326. }
  327. return 1;
  328. }
  329. };
  330. template <typename InType, typename OutType>
  331. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveAllEqual> {
  332. OutType operator()(const std::vector<InType> &inputs,
  333. const std::vector<int> &masks, int maskValue,
  334. unsigned int index) {
  335. const InType *val = nullptr;
  336. for (size_t i = 0; i < index; ++i) {
  337. if (masks.at(i) == maskValue) {
  338. if (val && *val != inputs.at(i)) {
  339. return 0;
  340. }
  341. val = &inputs.at(i);
  342. }
  343. }
  344. return 1;
  345. }
  346. };
  347. template <typename InType, typename OutType>
  348. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitOr> {
  349. OutType operator()(const std::vector<InType> &inputs,
  350. const std::vector<int> &masks, int maskValue,
  351. unsigned int index) {
  352. OutType bits = 0x00000000;
  353. for (size_t i = 0; i < index; ++i) {
  354. if (masks.at(i) == maskValue) {
  355. bits |= inputs.at(i);
  356. }
  357. }
  358. return bits;
  359. }
  360. };
  361. template <typename InType, typename OutType>
  362. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitAnd> {
  363. OutType operator()(const std::vector<InType> &inputs,
  364. const std::vector<int> &masks, int maskValue,
  365. unsigned int index) {
  366. OutType bits = 0xffffffff;
  367. for (size_t i = 0; i < index; ++i) {
  368. if (masks.at(i) == maskValue) {
  369. bits &= inputs.at(i);
  370. }
  371. }
  372. return bits;
  373. }
  374. };
  375. template <typename InType, typename OutType>
  376. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitXor> {
  377. OutType operator()(const std::vector<InType> &inputs,
  378. const std::vector<int> &masks, int maskValue,
  379. unsigned int index) {
  380. OutType bits = 0x00000000;
  381. for (size_t i = 0; i < index; ++i) {
  382. if (masks.at(i) == maskValue) {
  383. bits ^= inputs.at(i);
  384. }
  385. }
  386. return bits;
  387. }
  388. };
  389. // Mask functions used to control active lanes
  390. static int MaskAll(int i) {
  391. return 1;
  392. }
  393. static int MaskEveryOther(int i) {
  394. return i % 2 == 0 ? 1 : 0;
  395. }
  396. static int MaskEveryThird(int i) {
  397. return i % 3 == 0 ? 1 : 0;
  398. }
  399. // TODO: It seems there is an issue with WARP with controlling active lanes
  400. // Add more masks when this is resolved
  401. typedef int(*MaskFunction)(int);
  402. static MaskFunction MaskFunctionTable[] = {
  403. MaskAll, MaskEveryOther, MaskEveryThird
  404. };
  405. template <typename InType, typename OutType>
  406. static OutType computeExpectedWithShaderOp(const std::vector<InType> &inputs,
  407. const std::vector<int> &masks,
  408. int maskValue, unsigned int index,
  409. LPCWSTR str) {
  410. ShaderOpKind kind = GetShaderOpKind(str);
  411. switch (kind) {
  412. case ShaderOpKind::WaveSum:
  413. return computeExpected<InType, OutType, ShaderOpKind::WaveSum>()(inputs, masks, maskValue, index);
  414. case ShaderOpKind::WaveProduct:
  415. return computeExpected<InType, OutType, ShaderOpKind::WaveProduct>()(inputs, masks, maskValue, index);
  416. case ShaderOpKind::WaveActiveMax:
  417. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveMax>()(inputs, masks, maskValue, index);
  418. case ShaderOpKind::WaveActiveMin:
  419. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveMin>()(inputs, masks, maskValue, index);
  420. case ShaderOpKind::WaveCountBits:
  421. return computeExpected<InType, OutType, ShaderOpKind::WaveCountBits>()(inputs, masks, maskValue, index);
  422. case ShaderOpKind::WaveActiveBitOr:
  423. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitOr>()(inputs, masks, maskValue, index);
  424. case ShaderOpKind::WaveActiveBitAnd:
  425. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitAnd>()(inputs, masks, maskValue, index);
  426. case ShaderOpKind::WaveActiveBitXor:
  427. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitXor>()(inputs, masks, maskValue, index);
  428. case ShaderOpKind::WaveActiveAnyTrue:
  429. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveAnyTrue>()(inputs, masks, maskValue, index);
  430. case ShaderOpKind::WaveActiveAllTrue:
  431. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveAllTrue>()(inputs, masks, maskValue, index);
  432. case ShaderOpKind::WaveActiveAllEqual:
  433. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveAllEqual>()(inputs, masks, maskValue, index);
  434. default:
  435. DXASSERT(false, "Invalid ShaderOp Name: %s", str);
  436. return (OutType) 0;
  437. }
  438. };
  439. // Checks if the given warp version supports the given operation.
  440. bool IsValidWarpDllVersion(unsigned int minBuildNumber) {
  441. HMODULE pLibrary = LoadLibrary("D3D10Warp.dll");
  442. if (pLibrary) {
  443. char path[MAX_PATH];
  444. DWORD length = GetModuleFileName(pLibrary, path, MAX_PATH);
  445. if (length) {
  446. DWORD dwVerHnd = 0;
  447. DWORD dwVersionInfoSize = GetFileVersionInfoSize(path, &dwVerHnd);
  448. std::unique_ptr<int[]> VffInfo(new int[dwVersionInfoSize]);
  449. if (GetFileVersionInfo(path, NULL, dwVersionInfoSize, VffInfo.get())) {
  450. LPVOID versionInfo;
  451. UINT size;
  452. if (VerQueryValue(VffInfo.get(), "\\", &versionInfo, &size)) {
  453. if (size) {
  454. VS_FIXEDFILEINFO *verInfo = (VS_FIXEDFILEINFO *)versionInfo;
  455. unsigned int warpBuildNumber = verInfo->dwFileVersionLS >> 16 & 0xffff;
  456. if (verInfo->dwSignature == 0xFEEF04BD && warpBuildNumber >= minBuildNumber) {
  457. return true;
  458. }
  459. }
  460. }
  461. }
  462. }
  463. FreeLibrary(pLibrary);
  464. }
  465. return false;
  466. }
  467. class ExecutionTest {
  468. public:
  469. // By default, ignore these tests, which require a recent build to run properly.
  470. BEGIN_TEST_CLASS(ExecutionTest)
  471. TEST_CLASS_PROPERTY(L"Parallel", L"true")
  472. TEST_CLASS_PROPERTY(L"Ignore", L"true")
  473. TEST_METHOD_PROPERTY(L"Priority", L"0")
  474. END_TEST_CLASS()
  475. TEST_CLASS_SETUP(ExecutionTestClassSetup)
  476. TEST_METHOD(BasicComputeTest);
  477. TEST_METHOD(BasicTriangleTest);
  478. TEST_METHOD(BasicTriangleOpTest);
  479. TEST_METHOD(OutOfBoundsTest);
  480. TEST_METHOD(SaturateTest);
  481. TEST_METHOD(SignTest);
  482. TEST_METHOD(Int64Test);
  483. TEST_METHOD(WaveIntrinsicsTest);
  484. TEST_METHOD(WaveIntrinsicsInPSTest);
  485. TEST_METHOD(PartialDerivTest);
  486. // TODO: Change the priority to 0 once there is a driver that fixes the issue with WaveActive operations
  487. BEGIN_TEST_METHOD(WaveIntrinsicsActiveIntTest)
  488. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#WaveIntrinsicsActiveIntTable")
  489. END_TEST_METHOD()
  490. BEGIN_TEST_METHOD(WaveIntrinsicsActiveUintTest)
  491. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#WaveIntrinsicsActiveUintTable")
  492. END_TEST_METHOD()
  493. BEGIN_TEST_METHOD(WaveIntrinsicsPrefixIntTest)
  494. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#WaveIntrinsicsPrefixIntTable")
  495. END_TEST_METHOD()
  496. BEGIN_TEST_METHOD(WaveIntrinsicsPrefixUintTest)
  497. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#WaveIntrinsicsPrefixUintTable")
  498. END_TEST_METHOD()
  499. // TAEF data-driven tests.
  500. BEGIN_TEST_METHOD(UnaryFloatOpTest)
  501. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#UnaryFloatOpTable")
  502. END_TEST_METHOD()
  503. BEGIN_TEST_METHOD(BinaryFloatOpTest)
  504. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#BinaryFloatOpTable")
  505. END_TEST_METHOD()
  506. BEGIN_TEST_METHOD(TertiaryFloatOpTest)
  507. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#TertiaryFloatOpTable")
  508. END_TEST_METHOD()
  509. BEGIN_TEST_METHOD(UnaryIntOpTest)
  510. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#UnaryIntOpTable")
  511. END_TEST_METHOD()
  512. BEGIN_TEST_METHOD(BinaryIntOpTest)
  513. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#BinaryIntOpTable")
  514. END_TEST_METHOD()
  515. BEGIN_TEST_METHOD(TertiaryIntOpTest)
  516. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#TertiaryIntOpTable")
  517. END_TEST_METHOD()
  518. BEGIN_TEST_METHOD(UnaryUintOpTest)
  519. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#UnaryUintOpTable")
  520. END_TEST_METHOD()
  521. BEGIN_TEST_METHOD(BinaryUintOpTest)
  522. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#BinaryUintOpTable")
  523. END_TEST_METHOD()
  524. BEGIN_TEST_METHOD(TertiaryUintOpTest)
  525. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#TertiaryUintOpTable")
  526. END_TEST_METHOD()
  527. BEGIN_TEST_METHOD(DotTest)
  528. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#DotOpTable")
  529. END_TEST_METHOD()
  530. BEGIN_TEST_METHOD(Msad4Test)
  531. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#Msad4Table")
  532. END_TEST_METHOD()
  533. dxc::DxcDllSupport m_support;
  534. bool m_ExperimentalModeEnabled = false;
  535. static const float ClearColor[4];
  536. template <class T1, class T2>
  537. void WaveIntrinsicsActivePrefixTest(
  538. TableParameter *pParameterList, size_t numParameter, bool isPrefix);
  539. bool UseDxbc() {
  540. return GetTestParamBool(L"DXBC");
  541. }
  542. bool UseDebugIfaces() {
  543. return true;
  544. }
  545. bool SaveImages() {
  546. return GetTestParamBool(L"SaveImages");
  547. }
  548. void CompileFromText(LPCSTR pText, LPCWSTR pEntryPoint, LPCWSTR pTargetProfile, ID3DBlob **ppBlob) {
  549. VERIFY_SUCCEEDED(m_support.Initialize());
  550. CComPtr<IDxcCompiler> pCompiler;
  551. CComPtr<IDxcLibrary> pLibrary;
  552. CComPtr<IDxcBlobEncoding> pTextBlob;
  553. CComPtr<IDxcOperationResult> pResult;
  554. HRESULT resultCode;
  555. VERIFY_SUCCEEDED(m_support.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  556. VERIFY_SUCCEEDED(m_support.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  557. VERIFY_SUCCEEDED(pLibrary->CreateBlobWithEncodingFromPinned((LPBYTE)pText, strlen(pText), CP_UTF8, &pTextBlob));
  558. VERIFY_SUCCEEDED(pCompiler->Compile(pTextBlob, L"hlsl.hlsl", pEntryPoint, pTargetProfile, nullptr, 0, nullptr, 0, nullptr, &pResult));
  559. VERIFY_SUCCEEDED(pResult->GetStatus(&resultCode));
  560. if (FAILED(resultCode)) {
  561. CComPtr<IDxcBlobEncoding> errors;
  562. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&errors));
  563. LogCommentFmt(L"Failed to compile shader: %s", BlobToUtf16(errors).data());
  564. }
  565. VERIFY_SUCCEEDED(resultCode);
  566. VERIFY_SUCCEEDED(pResult->GetResult((IDxcBlob **)ppBlob));
  567. }
  568. void CreateComputeCommandQueue(ID3D12Device *pDevice, LPCWSTR pName, ID3D12CommandQueue **ppCommandQueue) {
  569. D3D12_COMMAND_QUEUE_DESC queueDesc = {};
  570. queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  571. queueDesc.Type = D3D12_COMMAND_LIST_TYPE_COMPUTE;
  572. VERIFY_SUCCEEDED(pDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(ppCommandQueue)));
  573. VERIFY_SUCCEEDED((*ppCommandQueue)->SetName(pName));
  574. }
  575. void CreateComputePSO(ID3D12Device *pDevice, ID3D12RootSignature *pRootSignature, LPCSTR pShader, ID3D12PipelineState **ppComputeState) {
  576. CComPtr<ID3DBlob> pComputeShader;
  577. // Load and compile shaders.
  578. if (UseDxbc()) {
  579. DXBCFromText(pShader, L"main", L"cs_6_0", &pComputeShader);
  580. }
  581. else {
  582. CompileFromText(pShader, L"main", L"cs_6_0", &pComputeShader);
  583. }
  584. // Describe and create the compute pipeline state object (PSO).
  585. D3D12_COMPUTE_PIPELINE_STATE_DESC computePsoDesc = {};
  586. computePsoDesc.pRootSignature = pRootSignature;
  587. computePsoDesc.CS = CD3DX12_SHADER_BYTECODE(pComputeShader);
  588. VERIFY_SUCCEEDED(pDevice->CreateComputePipelineState(&computePsoDesc, IID_PPV_ARGS(ppComputeState)));
  589. }
  590. bool CreateDevice(_COM_Outptr_ ID3D12Device **ppDevice) {
  591. const D3D_FEATURE_LEVEL FeatureLevelRequired = D3D_FEATURE_LEVEL_11_0;
  592. CComPtr<IDXGIFactory4> factory;
  593. CComPtr<ID3D12Device> pDevice;
  594. *ppDevice = nullptr;
  595. VERIFY_SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)));
  596. if (GetTestParamUseWARP(true)) {
  597. CComPtr<IDXGIAdapter> warpAdapter;
  598. VERIFY_SUCCEEDED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warpAdapter)));
  599. HRESULT createHR = D3D12CreateDevice(warpAdapter, FeatureLevelRequired,
  600. IID_PPV_ARGS(&pDevice));
  601. if (FAILED(createHR)) {
  602. LogCommentFmt(L"The available version of WARP does not support d3d12.");
  603. WEX::Logging::Log::Result(WEX::Logging::TestResults::Blocked);
  604. return false;
  605. }
  606. } else {
  607. CComPtr<IDXGIAdapter1> hardwareAdapter;
  608. WEX::Common::String AdapterValue;
  609. IFT(WEX::TestExecution::RuntimeParameters::TryGetValue(L"Adapter",
  610. AdapterValue));
  611. GetHardwareAdapter(factory, AdapterValue, &hardwareAdapter);
  612. if (hardwareAdapter == nullptr) {
  613. WEX::Logging::Log::Error(
  614. L"Unable to find hardware adapter with D3D12 support.");
  615. return false;
  616. }
  617. VERIFY_SUCCEEDED(D3D12CreateDevice(hardwareAdapter, FeatureLevelRequired,
  618. IID_PPV_ARGS(&pDevice)));
  619. DXGI_ADAPTER_DESC1 AdapterDesc;
  620. VERIFY_SUCCEEDED(hardwareAdapter->GetDesc1(&AdapterDesc));
  621. LogCommentFmt(L"Using Adapter: %s", AdapterDesc.Description);
  622. }
  623. if (pDevice == nullptr)
  624. return false;
  625. if (!UseDxbc()) {
  626. // Check for DXIL support.
  627. // This is defined in d3d.h for Windows 10 Anniversary Edition SDK, but we only
  628. // require the Windows 10 SDK.
  629. typedef enum D3D_SHADER_MODEL {
  630. D3D_SHADER_MODEL_5_1 = 0x51,
  631. D3D_SHADER_MODEL_6_0 = 0x60
  632. } D3D_SHADER_MODEL;
  633. typedef struct D3D12_FEATURE_DATA_SHADER_MODEL {
  634. _Inout_ D3D_SHADER_MODEL HighestShaderModel;
  635. } D3D12_FEATURE_DATA_SHADER_MODEL;
  636. const UINT D3D12_FEATURE_SHADER_MODEL = 7;
  637. D3D12_FEATURE_DATA_SHADER_MODEL SMData;
  638. SMData.HighestShaderModel = D3D_SHADER_MODEL_6_0;
  639. VERIFY_SUCCEEDED(pDevice->CheckFeatureSupport(
  640. (D3D12_FEATURE)D3D12_FEATURE_SHADER_MODEL, &SMData, sizeof(SMData)));
  641. if (SMData.HighestShaderModel != D3D_SHADER_MODEL_6_0) {
  642. LogCommentFmt(L"The selected device does not support "
  643. L"shader model 6 (required for DXIL).");
  644. WEX::Logging::Log::Result(WEX::Logging::TestResults::Blocked);
  645. return false;
  646. }
  647. }
  648. if (UseDebugIfaces()) {
  649. CComPtr<ID3D12InfoQueue> pInfoQueue;
  650. if (SUCCEEDED(pDevice->QueryInterface(&pInfoQueue))) {
  651. pInfoQueue->SetMuteDebugOutput(FALSE);
  652. }
  653. }
  654. *ppDevice = pDevice.Detach();
  655. return true;
  656. }
  657. void CreateGraphicsCommandQueue(ID3D12Device *pDevice, ID3D12CommandQueue **ppCommandQueue) {
  658. D3D12_COMMAND_QUEUE_DESC queueDesc = {};
  659. queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  660. queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;;
  661. VERIFY_SUCCEEDED(pDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(ppCommandQueue)));
  662. }
  663. void CreateGraphicsCommandQueueAndList(
  664. ID3D12Device *pDevice, ID3D12CommandQueue **ppCommandQueue,
  665. ID3D12CommandAllocator **ppAllocator,
  666. ID3D12GraphicsCommandList **ppCommandList, ID3D12PipelineState *pPSO) {
  667. CreateGraphicsCommandQueue(pDevice, ppCommandQueue);
  668. VERIFY_SUCCEEDED(pDevice->CreateCommandAllocator(
  669. D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(ppAllocator)));
  670. VERIFY_SUCCEEDED(pDevice->CreateCommandList(
  671. 0, D3D12_COMMAND_LIST_TYPE_DIRECT, *ppAllocator, pPSO,
  672. IID_PPV_ARGS(ppCommandList)));
  673. }
  674. void CreateGraphicsPSO(ID3D12Device *pDevice,
  675. D3D12_INPUT_LAYOUT_DESC *pInputLayout,
  676. ID3D12RootSignature *pRootSignature, LPCSTR pShaders,
  677. ID3D12PipelineState **ppPSO) {
  678. CComPtr<ID3DBlob> vertexShader;
  679. CComPtr<ID3DBlob> pixelShader;
  680. if (UseDxbc()) {
  681. DXBCFromText(pShaders, L"VSMain", L"vs_6_0", &vertexShader);
  682. DXBCFromText(pShaders, L"PSMain", L"ps_6_0", &pixelShader);
  683. } else {
  684. CompileFromText(pShaders, L"VSMain", L"vs_6_0", &vertexShader);
  685. CompileFromText(pShaders, L"PSMain", L"ps_6_0", &pixelShader);
  686. }
  687. // Describe and create the graphics pipeline state object (PSO).
  688. D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
  689. psoDesc.InputLayout = *pInputLayout;
  690. psoDesc.pRootSignature = pRootSignature;
  691. psoDesc.VS = CD3DX12_SHADER_BYTECODE(vertexShader);
  692. psoDesc.PS = CD3DX12_SHADER_BYTECODE(pixelShader);
  693. psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
  694. psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
  695. psoDesc.DepthStencilState.DepthEnable = FALSE;
  696. psoDesc.DepthStencilState.StencilEnable = FALSE;
  697. psoDesc.SampleMask = UINT_MAX;
  698. psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
  699. psoDesc.NumRenderTargets = 1;
  700. psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
  701. psoDesc.SampleDesc.Count = 1;
  702. VERIFY_SUCCEEDED(
  703. pDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(ppPSO)));
  704. }
  705. void CreateRenderTargetAndReadback(ID3D12Device *pDevice,
  706. ID3D12DescriptorHeap *pHeap, UINT width,
  707. UINT height,
  708. ID3D12Resource **ppRenderTarget,
  709. ID3D12Resource **ppBuffer) {
  710. const DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
  711. const size_t formatElementSize = 4;
  712. CComPtr<ID3D12Resource> pRenderTarget;
  713. CComPtr<ID3D12Resource> pBuffer;
  714. CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(
  715. pHeap->GetCPUDescriptorHandleForHeapStart());
  716. CD3DX12_HEAP_PROPERTIES rtHeap(D3D12_HEAP_TYPE_DEFAULT);
  717. CD3DX12_RESOURCE_DESC rtDesc(
  718. CD3DX12_RESOURCE_DESC::Tex2D(format, width, height));
  719. CD3DX12_CLEAR_VALUE rtClearVal(format, ClearColor);
  720. rtDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
  721. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  722. &rtHeap, D3D12_HEAP_FLAG_NONE, &rtDesc, D3D12_RESOURCE_STATE_COPY_DEST,
  723. &rtClearVal, IID_PPV_ARGS(&pRenderTarget)));
  724. pDevice->CreateRenderTargetView(pRenderTarget, nullptr, rtvHandle);
  725. // rtvHandle.Offset(1, rtvDescriptorSize); // Not needed for a single
  726. // resource.
  727. CD3DX12_HEAP_PROPERTIES readHeap(D3D12_HEAP_TYPE_READBACK);
  728. CD3DX12_RESOURCE_DESC readDesc(
  729. CD3DX12_RESOURCE_DESC::Buffer(width * height * formatElementSize));
  730. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  731. &readHeap, D3D12_HEAP_FLAG_NONE, &readDesc,
  732. D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&pBuffer)));
  733. *ppRenderTarget = pRenderTarget.Detach();
  734. *ppBuffer = pBuffer.Detach();
  735. }
  736. void CreateRootSignatureFromDesc(ID3D12Device *pDevice,
  737. const D3D12_ROOT_SIGNATURE_DESC *pDesc,
  738. ID3D12RootSignature **pRootSig) {
  739. CComPtr<ID3DBlob> signature;
  740. CComPtr<ID3DBlob> error;
  741. VERIFY_SUCCEEDED(D3D12SerializeRootSignature(pDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
  742. VERIFY_SUCCEEDED(pDevice->CreateRootSignature(
  743. 0, signature->GetBufferPointer(), signature->GetBufferSize(),
  744. IID_PPV_ARGS(pRootSig)));
  745. }
  746. void CreateRtvDescriptorHeap(ID3D12Device *pDevice, UINT numDescriptors,
  747. ID3D12DescriptorHeap **pRtvHeap, UINT *rtvDescriptorSize) {
  748. D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
  749. rtvHeapDesc.NumDescriptors = numDescriptors;
  750. rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
  751. rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
  752. VERIFY_SUCCEEDED(
  753. pDevice->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(pRtvHeap)));
  754. if (rtvDescriptorSize != nullptr) {
  755. *rtvDescriptorSize = pDevice->GetDescriptorHandleIncrementSize(
  756. D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  757. }
  758. }
  759. void CreateTestUavs(ID3D12Device *pDevice,
  760. ID3D12GraphicsCommandList *pCommandList, LPCVOID values,
  761. UINT32 valueSizeInBytes, ID3D12Resource **ppUavResource,
  762. ID3D12Resource **ppReadBuffer,
  763. ID3D12Resource **ppUploadResource) {
  764. CComPtr<ID3D12Resource> pUavResource;
  765. CComPtr<ID3D12Resource> pReadBuffer;
  766. CComPtr<ID3D12Resource> pUploadResource;
  767. D3D12_SUBRESOURCE_DATA transferData;
  768. D3D12_HEAP_PROPERTIES defaultHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
  769. D3D12_HEAP_PROPERTIES uploadHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
  770. D3D12_RESOURCE_DESC bufferDesc = CD3DX12_RESOURCE_DESC::Buffer(valueSizeInBytes, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
  771. D3D12_RESOURCE_DESC uploadBufferDesc = CD3DX12_RESOURCE_DESC::Buffer(valueSizeInBytes);
  772. CD3DX12_HEAP_PROPERTIES readHeap(D3D12_HEAP_TYPE_READBACK);
  773. CD3DX12_RESOURCE_DESC readDesc(CD3DX12_RESOURCE_DESC::Buffer(valueSizeInBytes));
  774. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  775. &defaultHeapProperties,
  776. D3D12_HEAP_FLAG_NONE,
  777. &bufferDesc,
  778. D3D12_RESOURCE_STATE_COPY_DEST,
  779. nullptr,
  780. IID_PPV_ARGS(&pUavResource)));
  781. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  782. &uploadHeapProperties,
  783. D3D12_HEAP_FLAG_NONE,
  784. &uploadBufferDesc,
  785. D3D12_RESOURCE_STATE_GENERIC_READ,
  786. nullptr,
  787. IID_PPV_ARGS(&pUploadResource)));
  788. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  789. &readHeap, D3D12_HEAP_FLAG_NONE, &readDesc,
  790. D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&pReadBuffer)));
  791. transferData.pData = values;
  792. transferData.RowPitch = valueSizeInBytes;
  793. transferData.SlicePitch = transferData.RowPitch;
  794. UpdateSubresources<1>(pCommandList, pUavResource.p, pUploadResource.p, 0, 0, 1, &transferData);
  795. RecordTransitionBarrier(pCommandList, pUavResource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
  796. *ppUavResource = pUavResource.Detach();
  797. *ppReadBuffer = pReadBuffer.Detach();
  798. *ppUploadResource = pUploadResource.Detach();
  799. }
  800. template <typename TVertex, int len>
  801. void CreateVertexBuffer(ID3D12Device *pDevice, TVertex(&vertices)[len],
  802. ID3D12Resource **ppVertexBuffer,
  803. D3D12_VERTEX_BUFFER_VIEW *pVertexBufferView) {
  804. size_t vertexBufferSize = sizeof(vertices);
  805. CComPtr<ID3D12Resource> pVertexBuffer;
  806. CD3DX12_HEAP_PROPERTIES heapProps(D3D12_HEAP_TYPE_UPLOAD);
  807. CD3DX12_RESOURCE_DESC bufferDesc(
  808. CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize));
  809. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  810. &heapProps, D3D12_HEAP_FLAG_NONE, &bufferDesc,
  811. D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
  812. IID_PPV_ARGS(&pVertexBuffer)));
  813. UINT8 *pVertexDataBegin;
  814. CD3DX12_RANGE readRange(0, 0);
  815. VERIFY_SUCCEEDED(pVertexBuffer->Map(
  816. 0, &readRange, reinterpret_cast<void **>(&pVertexDataBegin)));
  817. memcpy(pVertexDataBegin, vertices, vertexBufferSize);
  818. pVertexBuffer->Unmap(0, nullptr);
  819. // Initialize the vertex buffer view.
  820. pVertexBufferView->BufferLocation = pVertexBuffer->GetGPUVirtualAddress();
  821. pVertexBufferView->StrideInBytes = sizeof(TVertex);
  822. pVertexBufferView->SizeInBytes = vertexBufferSize;
  823. *ppVertexBuffer = pVertexBuffer.Detach();
  824. }
  825. // Requires Anniversary Edition headers, so simplifying things for current setup.
  826. const UINT D3D12_FEATURE_D3D12_OPTIONS1 = 8;
  827. struct D3D12_FEATURE_DATA_D3D12_OPTIONS1 {
  828. BOOL WaveOps;
  829. UINT WaveLaneCountMin;
  830. UINT WaveLaneCountMax;
  831. UINT TotalLaneCount;
  832. BOOL ExpandedComputeResourceStates;
  833. BOOL Int64ShaderOps;
  834. };
  835. bool DoesDeviceSupportInt64(ID3D12Device *pDevice) {
  836. D3D12_FEATURE_DATA_D3D12_OPTIONS1 O;
  837. if (FAILED(pDevice->CheckFeatureSupport((D3D12_FEATURE)D3D12_FEATURE_D3D12_OPTIONS1, &O, sizeof(O))))
  838. return false;
  839. return O.Int64ShaderOps != FALSE;
  840. }
  841. bool DoesDeviceSupportWaveOps(ID3D12Device *pDevice) {
  842. D3D12_FEATURE_DATA_D3D12_OPTIONS1 O;
  843. if (FAILED(pDevice->CheckFeatureSupport((D3D12_FEATURE)D3D12_FEATURE_D3D12_OPTIONS1, &O, sizeof(O))))
  844. return false;
  845. return O.WaveOps != FALSE;
  846. }
  847. void DXBCFromText(LPCSTR pText, LPCWSTR pEntryPoint, LPCWSTR pTargetProfile, ID3DBlob **ppBlob) {
  848. CW2A pEntryPointA(pEntryPoint, CP_UTF8);
  849. CW2A pTargetProfileA(pTargetProfile, CP_UTF8);
  850. CComPtr<ID3DBlob> pErrors;
  851. D3D_SHADER_MACRO d3dMacro[2];
  852. ZeroMemory(d3dMacro, sizeof(d3dMacro));
  853. d3dMacro[0].Definition = "1";
  854. d3dMacro[0].Name = "USING_DXBC";
  855. HRESULT hr = D3DCompile(pText, strlen(pText), "hlsl.hlsl", d3dMacro, nullptr, pEntryPointA, pTargetProfileA, 0, 0, ppBlob, &pErrors);
  856. if (pErrors != nullptr) {
  857. CA2W errors((char *)pErrors->GetBufferPointer(), CP_ACP);
  858. LogCommentFmt(L"Compilation failure: %s", errors.m_szBuffer);
  859. }
  860. VERIFY_SUCCEEDED(hr);
  861. }
  862. HRESULT EnableDebugLayer() {
  863. // The debug layer does net yet validate DXIL programs that require rewriting,
  864. // but basic logging should work properly.
  865. HRESULT hr = S_FALSE;
  866. if (UseDebugIfaces()) {
  867. CComPtr<ID3D12Debug> debugController;
  868. hr = D3D12GetDebugInterface(IID_PPV_ARGS(&debugController));
  869. if (SUCCEEDED(hr)) {
  870. debugController->EnableDebugLayer();
  871. hr = S_OK;
  872. }
  873. }
  874. return hr;
  875. }
  876. HRESULT EnableExperimentalMode() {
  877. if (m_ExperimentalModeEnabled) {
  878. return S_OK;
  879. }
  880. if (!GetTestParamBool(L"ExperimentalShaders")) {
  881. return S_FALSE;
  882. }
  883. HRESULT hr = EnableExperimentalShaderModels();
  884. if (SUCCEEDED(hr)) {
  885. m_ExperimentalModeEnabled = true;
  886. }
  887. return hr;
  888. }
  889. struct FenceObj {
  890. HANDLE m_fenceEvent = NULL;
  891. CComPtr<ID3D12Fence> m_fence;
  892. UINT64 m_fenceValue;
  893. ~FenceObj() {
  894. if (m_fenceEvent) CloseHandle(m_fenceEvent);
  895. }
  896. };
  897. void InitFenceObj(ID3D12Device *pDevice, FenceObj *pObj) {
  898. pObj->m_fenceValue = 1;
  899. VERIFY_SUCCEEDED(pDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE,
  900. IID_PPV_ARGS(&pObj->m_fence)));
  901. // Create an event handle to use for frame synchronization.
  902. pObj->m_fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
  903. if (pObj->m_fenceEvent == nullptr) {
  904. VERIFY_SUCCEEDED(HRESULT_FROM_WIN32(GetLastError()));
  905. }
  906. }
  907. void ReadHlslDataIntoNewStream(LPCWSTR relativePath, IStream **ppStream) {
  908. VERIFY_SUCCEEDED(m_support.Initialize());
  909. CComPtr<IDxcLibrary> pLibrary;
  910. CComPtr<IDxcBlobEncoding> pBlob;
  911. CComPtr<IStream> pStream;
  912. std::wstring path = GetPathToHlslDataFile(relativePath);
  913. VERIFY_SUCCEEDED(m_support.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  914. VERIFY_SUCCEEDED(pLibrary->CreateBlobFromFile(path.c_str(), nullptr, &pBlob));
  915. VERIFY_SUCCEEDED(pLibrary->CreateStreamFromBlobReadOnly(pBlob, &pStream));
  916. *ppStream = pStream.Detach();
  917. }
  918. void RecordRenderAndReadback(ID3D12GraphicsCommandList *pList,
  919. ID3D12DescriptorHeap *pRtvHeap,
  920. UINT rtvDescriptorSize,
  921. UINT instanceCount,
  922. D3D12_VERTEX_BUFFER_VIEW *pVertexBufferView,
  923. ID3D12RootSignature *pRootSig,
  924. ID3D12Resource *pRenderTarget,
  925. ID3D12Resource *pReadBuffer) {
  926. D3D12_RESOURCE_DESC rtDesc = pRenderTarget->GetDesc();
  927. D3D12_VIEWPORT viewport;
  928. D3D12_RECT scissorRect;
  929. memset(&viewport, 0, sizeof(viewport));
  930. viewport.Height = rtDesc.Height;
  931. viewport.Width = rtDesc.Width;
  932. viewport.MaxDepth = 1.0f;
  933. memset(&scissorRect, 0, sizeof(scissorRect));
  934. scissorRect.right = rtDesc.Width;
  935. scissorRect.bottom = rtDesc.Height;
  936. if (pRootSig != nullptr) {
  937. pList->SetGraphicsRootSignature(pRootSig);
  938. }
  939. pList->RSSetViewports(1, &viewport);
  940. pList->RSSetScissorRects(1, &scissorRect);
  941. // Indicate that the buffer will be used as a render target.
  942. RecordTransitionBarrier(pList, pRenderTarget, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_RENDER_TARGET);
  943. CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(pRtvHeap->GetCPUDescriptorHandleForHeapStart(), 0, rtvDescriptorSize);
  944. pList->OMSetRenderTargets(1, &rtvHandle, FALSE, nullptr);
  945. pList->ClearRenderTargetView(rtvHandle, ClearColor, 0, nullptr);
  946. pList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  947. pList->IASetVertexBuffers(0, 1, pVertexBufferView);
  948. pList->DrawInstanced(3, instanceCount, 0, 0);
  949. // Transition to copy source and copy into read-back buffer.
  950. RecordTransitionBarrier(pList, pRenderTarget, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
  951. // Copy into read-back buffer.
  952. UINT rowPitch = rtDesc.Width * 4;
  953. if (rowPitch % D3D12_TEXTURE_DATA_PITCH_ALIGNMENT)
  954. rowPitch += D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - (rowPitch % D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
  955. D3D12_PLACED_SUBRESOURCE_FOOTPRINT Footprint;
  956. Footprint.Offset = 0;
  957. Footprint.Footprint = CD3DX12_SUBRESOURCE_FOOTPRINT(DXGI_FORMAT_R8G8B8A8_UNORM, rtDesc.Width, rtDesc.Height, 1, rowPitch);
  958. CD3DX12_TEXTURE_COPY_LOCATION DstLoc(pReadBuffer, Footprint);
  959. CD3DX12_TEXTURE_COPY_LOCATION SrcLoc(pRenderTarget, 0);
  960. pList->CopyTextureRegion(&DstLoc, 0, 0, 0, &SrcLoc, nullptr);
  961. }
  962. void RunRWByteBufferComputeTest(ID3D12Device *pDevice, LPCSTR shader, std::vector<uint32_t> &values);
  963. void SetDescriptorHeap(ID3D12GraphicsCommandList *pCommandList, ID3D12DescriptorHeap *pHeap) {
  964. ID3D12DescriptorHeap *const pHeaps[1] = { pHeap };
  965. pCommandList->SetDescriptorHeaps(1, pHeaps);
  966. }
  967. void WaitForSignal(ID3D12CommandQueue *pCQ, FenceObj &FO) {
  968. ::WaitForSignal(pCQ, FO.m_fence, FO.m_fenceEvent, FO.m_fenceValue++);
  969. }
  970. };
  971. const float ExecutionTest::ClearColor[4] = { 0.0f, 0.2f, 0.4f, 1.0f };
  972. #define WAVE_INTRINSIC_DXBC_GUARD \
  973. "#ifdef USING_DXBC\r\n" \
  974. "uint WaveGetLaneIndex() { return 1; }\r\n" \
  975. "uint WaveReadLaneFirst(uint u) { return u; }\r\n" \
  976. "bool WaveIsFirstLane() { return true; }\r\n" \
  977. "uint WaveGetLaneCount() { return 1; }\r\n" \
  978. "uint WaveReadLaneAt(uint n, uint u) { return u; }\r\n" \
  979. "bool WaveActiveAnyTrue(bool b) { return b; }\r\n" \
  980. "bool WaveActiveAllTrue(bool b) { return false; }\r\n" \
  981. "uint WaveActiveAllEqual(uint u) { return u; }\r\n" \
  982. "uint4 WaveActiveBallot(bool b) { return 1; }\r\n" \
  983. "uint WaveActiveCountBits(uint u) { return 1; }\r\n" \
  984. "uint WaveActiveSum(uint u) { return 1; }\r\n" \
  985. "uint WaveActiveProduct(uint u) { return 1; }\r\n" \
  986. "uint WaveActiveBitAnd(uint u) { return 1; }\r\n" \
  987. "uint WaveActiveBitOr(uint u) { return 1; }\r\n" \
  988. "uint WaveActiveBitXor(uint u) { return 1; }\r\n" \
  989. "uint WaveActiveMin(uint u) { return 1; }\r\n" \
  990. "uint WaveActiveMax(uint u) { return 1; }\r\n" \
  991. "uint WavePrefixCountBits(uint u) { return 1; }\r\n" \
  992. "uint WavePrefixSum(uint u) { return 1; }\r\n" \
  993. "uint WavePrefixProduct(uint u) { return 1; }\r\n" \
  994. "uint QuadReadLaneAt(uint a, uint u) { return 1; }\r\n" \
  995. "uint QuadReadAcrossX(uint u) { return 1; }\r\n" \
  996. "uint QuadReadAcrossY(uint u) { return 1; }\r\n" \
  997. "uint QuadReadAcrossDiagonal(uint u) { return 1; }\r\n" \
  998. "#endif\r\n"
  999. static void SetupComputeValuePattern(std::vector<uint32_t> &values, size_t count) {
  1000. values.resize(count); // one element per dispatch group, in bytes
  1001. for (size_t i = 0; i < count; ++i) {
  1002. values[i] = i;
  1003. }
  1004. }
  1005. bool ExecutionTest::ExecutionTestClassSetup() {
  1006. HRESULT hr = EnableExperimentalMode();
  1007. if (FAILED(hr)) {
  1008. LogCommentFmt(L"Unable to enable shader experimental mode - 0x%08x.", hr);
  1009. }
  1010. else if (hr == S_FALSE) {
  1011. LogCommentFmt(L"Experimental mode not enabled.");
  1012. }
  1013. else {
  1014. LogCommentFmt(L"Experimental mode enabled.");
  1015. }
  1016. hr = EnableDebugLayer();
  1017. if (FAILED(hr)) {
  1018. LogCommentFmt(L"Unable to enable debug layer - 0x%08x.", hr);
  1019. }
  1020. else {
  1021. LogCommentFmt(L"Debug layer enabled.");
  1022. }
  1023. return true;
  1024. }
  1025. void ExecutionTest::RunRWByteBufferComputeTest(ID3D12Device *pDevice, LPCSTR pShader, std::vector<uint32_t> &values) {
  1026. static const int DispatchGroupX = 1;
  1027. static const int DispatchGroupY = 1;
  1028. static const int DispatchGroupZ = 1;
  1029. CComPtr<ID3D12GraphicsCommandList> pCommandList;
  1030. CComPtr<ID3D12CommandQueue> pCommandQueue;
  1031. CComPtr<ID3D12DescriptorHeap> pUavHeap;
  1032. CComPtr<ID3D12CommandAllocator> pCommandAllocator;
  1033. UINT uavDescriptorSize;
  1034. FenceObj FO;
  1035. const size_t valueSizeInBytes = values.size() * sizeof(uint32_t);
  1036. CreateComputeCommandQueue(pDevice, L"RunRWByteBufferComputeTest Command Queue", &pCommandQueue);
  1037. InitFenceObj(pDevice, &FO);
  1038. // Describe and create a UAV descriptor heap.
  1039. D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
  1040. heapDesc.NumDescriptors = 1;
  1041. heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  1042. heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  1043. VERIFY_SUCCEEDED(pDevice->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&pUavHeap)));
  1044. uavDescriptorSize = pDevice->GetDescriptorHandleIncrementSize(heapDesc.Type);
  1045. // Create root signature.
  1046. CComPtr<ID3D12RootSignature> pRootSignature;
  1047. {
  1048. CD3DX12_DESCRIPTOR_RANGE ranges[1];
  1049. ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 0, 0, 0);
  1050. CD3DX12_ROOT_PARAMETER rootParameters[1];
  1051. rootParameters[0].InitAsDescriptorTable(1, &ranges[0], D3D12_SHADER_VISIBILITY_ALL);
  1052. CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
  1053. rootSignatureDesc.Init(_countof(rootParameters), rootParameters, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE);
  1054. CreateRootSignatureFromDesc(pDevice, &rootSignatureDesc, &pRootSignature);
  1055. }
  1056. // Create pipeline state object.
  1057. CComPtr<ID3D12PipelineState> pComputeState;
  1058. CreateComputePSO(pDevice, pRootSignature, pShader, &pComputeState);
  1059. // Create a command allocator and list for compute.
  1060. VERIFY_SUCCEEDED(pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE, IID_PPV_ARGS(&pCommandAllocator)));
  1061. VERIFY_SUCCEEDED(pDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, pCommandAllocator, pComputeState, IID_PPV_ARGS(&pCommandList)));
  1062. pCommandList->SetName(L"ExecutionTest::RunRWByteButterComputeTest Command List");
  1063. // Set up UAV resource.
  1064. CComPtr<ID3D12Resource> pUavResource;
  1065. CComPtr<ID3D12Resource> pReadBuffer;
  1066. CComPtr<ID3D12Resource> pUploadResource;
  1067. CreateTestUavs(pDevice, pCommandList, values.data(), valueSizeInBytes, &pUavResource, &pReadBuffer, &pUploadResource);
  1068. VERIFY_SUCCEEDED(pUavResource->SetName(L"RunRWByteBufferComputeText UAV"));
  1069. VERIFY_SUCCEEDED(pReadBuffer->SetName(L"RunRWByteBufferComputeText UAV Read Buffer"));
  1070. VERIFY_SUCCEEDED(pUploadResource->SetName(L"RunRWByteBufferComputeText UAV Upload Buffer"));
  1071. // Close the command list and execute it to perform the GPU setup.
  1072. pCommandList->Close();
  1073. ExecuteCommandList(pCommandQueue, pCommandList);
  1074. WaitForSignal(pCommandQueue, FO);
  1075. VERIFY_SUCCEEDED(pCommandAllocator->Reset());
  1076. VERIFY_SUCCEEDED(pCommandList->Reset(pCommandAllocator, pComputeState));
  1077. // Run the compute shader and copy the results back to readable memory.
  1078. {
  1079. D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
  1080. uavDesc.Format = DXGI_FORMAT_R32_TYPELESS;
  1081. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
  1082. uavDesc.Buffer.FirstElement = 0;
  1083. uavDesc.Buffer.NumElements = values.size();
  1084. uavDesc.Buffer.StructureByteStride = 0;
  1085. uavDesc.Buffer.CounterOffsetInBytes = 0;
  1086. uavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW;
  1087. CD3DX12_CPU_DESCRIPTOR_HANDLE uavHandle(pUavHeap->GetCPUDescriptorHandleForHeapStart());
  1088. CD3DX12_GPU_DESCRIPTOR_HANDLE uavHandleGpu(pUavHeap->GetGPUDescriptorHandleForHeapStart());
  1089. pDevice->CreateUnorderedAccessView(pUavResource, nullptr, &uavDesc, uavHandle);
  1090. SetDescriptorHeap(pCommandList, pUavHeap);
  1091. pCommandList->SetComputeRootSignature(pRootSignature);
  1092. pCommandList->SetComputeRootDescriptorTable(0, uavHandleGpu);
  1093. }
  1094. pCommandList->Dispatch(DispatchGroupX, DispatchGroupY, DispatchGroupZ);
  1095. RecordTransitionBarrier(pCommandList, pUavResource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
  1096. pCommandList->CopyResource(pReadBuffer, pUavResource);
  1097. pCommandList->Close();
  1098. ExecuteCommandList(pCommandQueue, pCommandList);
  1099. WaitForSignal(pCommandQueue, FO);
  1100. {
  1101. MappedData mappedData(pReadBuffer, valueSizeInBytes);
  1102. uint32_t *pData = (uint32_t *)mappedData.data();
  1103. memcpy(values.data(), pData, valueSizeInBytes);
  1104. }
  1105. WaitForSignal(pCommandQueue, FO);
  1106. }
  1107. TEST_F(ExecutionTest, BasicComputeTest) {
  1108. //
  1109. // BasicComputeTest is a simple compute shader that can be used as the basis
  1110. // for more interesting compute execution tests.
  1111. // The HLSL is compatible with shader models <=5.1 to allow using the DXBC
  1112. // rendering code paths for comparison.
  1113. //
  1114. static const char pShader[] =
  1115. "RWByteAddressBuffer g_bab : register(u0);\r\n"
  1116. "[numthreads(8,8,1)]\r\n"
  1117. "void main(uint GI : SV_GroupIndex) {"
  1118. " uint addr = GI * 4;\r\n"
  1119. " uint val = g_bab.Load(addr);\r\n"
  1120. " DeviceMemoryBarrierWithGroupSync();\r\n"
  1121. " g_bab.Store(addr, val + 1);\r\n"
  1122. "}";
  1123. static const int NumThreadsX = 8;
  1124. static const int NumThreadsY = 8;
  1125. static const int NumThreadsZ = 1;
  1126. static const int ThreadsPerGroup = NumThreadsX * NumThreadsY * NumThreadsZ;
  1127. static const int DispatchGroupCount = 1;
  1128. CComPtr<ID3D12Device> pDevice;
  1129. if (!CreateDevice(&pDevice))
  1130. return;
  1131. std::vector<uint32_t> values;
  1132. SetupComputeValuePattern(values, ThreadsPerGroup * DispatchGroupCount);
  1133. VERIFY_ARE_EQUAL(values[0], 0);
  1134. RunRWByteBufferComputeTest(pDevice, pShader, values);
  1135. VERIFY_ARE_EQUAL(values[0], 1);
  1136. }
  1137. TEST_F(ExecutionTest, BasicTriangleTest) {
  1138. static const UINT FrameCount = 2;
  1139. static const UINT m_width = 320;
  1140. static const UINT m_height = 200;
  1141. static const float m_aspectRatio = static_cast<float>(m_width) / static_cast<float>(m_height);
  1142. struct Vertex {
  1143. XMFLOAT3 position;
  1144. XMFLOAT4 color;
  1145. };
  1146. // Pipeline objects.
  1147. CComPtr<ID3D12Device> pDevice;
  1148. CComPtr<ID3D12Resource> pRenderTarget;
  1149. CComPtr<ID3D12CommandAllocator> pCommandAllocator;
  1150. CComPtr<ID3D12CommandQueue> pCommandQueue;
  1151. CComPtr<ID3D12RootSignature> pRootSig;
  1152. CComPtr<ID3D12DescriptorHeap> pRtvHeap;
  1153. CComPtr<ID3D12PipelineState> pPipelineState;
  1154. CComPtr<ID3D12GraphicsCommandList> pCommandList;
  1155. CComPtr<ID3D12Resource> pReadBuffer;
  1156. UINT rtvDescriptorSize;
  1157. CComPtr<ID3D12Resource> pVertexBuffer;
  1158. D3D12_VERTEX_BUFFER_VIEW vertexBufferView;
  1159. // Synchronization objects.
  1160. FenceObj FO;
  1161. // Shaders.
  1162. static const char pShaders[] =
  1163. "struct PSInput {\r\n"
  1164. " float4 position : SV_POSITION;\r\n"
  1165. " float4 color : COLOR;\r\n"
  1166. "};\r\n\r\n"
  1167. "PSInput VSMain(float4 position : POSITION, float4 color : COLOR) {\r\n"
  1168. " PSInput result;\r\n"
  1169. "\r\n"
  1170. " result.position = position;\r\n"
  1171. " result.color = color;\r\n"
  1172. " return result;\r\n"
  1173. "}\r\n\r\n"
  1174. "float4 PSMain(PSInput input) : SV_TARGET {\r\n"
  1175. " return 1; //input.color;\r\n"
  1176. "};\r\n";
  1177. if (!CreateDevice(&pDevice))
  1178. return;
  1179. struct BasicTestChecker {
  1180. CComPtr<ID3D12Device> m_pDevice;
  1181. CComPtr<ID3D12InfoQueue> m_pInfoQueue;
  1182. bool m_OK = false;
  1183. void SetOK(bool value) { m_OK = value; }
  1184. BasicTestChecker(ID3D12Device *pDevice) : m_pDevice(pDevice) {
  1185. if (FAILED(m_pDevice.QueryInterface(&m_pInfoQueue)))
  1186. return;
  1187. m_pInfoQueue->PushEmptyStorageFilter();
  1188. m_pInfoQueue->PushEmptyRetrievalFilter();
  1189. }
  1190. ~BasicTestChecker() {
  1191. if (!m_OK && m_pInfoQueue != nullptr) {
  1192. UINT64 count = m_pInfoQueue->GetNumStoredMessages();
  1193. bool invalidBytecodeFound = false;
  1194. CAtlArray<BYTE> m_pBytes;
  1195. for (UINT64 i = 0; i < count; ++i) {
  1196. SIZE_T len = 0;
  1197. if (FAILED(m_pInfoQueue->GetMessageA(i, nullptr, &len)))
  1198. continue;
  1199. if (m_pBytes.GetCount() < len && !m_pBytes.SetCount(len))
  1200. continue;
  1201. D3D12_MESSAGE *pMsg = (D3D12_MESSAGE *)m_pBytes.GetData();
  1202. if (FAILED(m_pInfoQueue->GetMessageA(i, pMsg, &len)))
  1203. continue;
  1204. if (pMsg->ID == D3D12_MESSAGE_ID_CREATEVERTEXSHADER_INVALIDSHADERBYTECODE ||
  1205. pMsg->ID == D3D12_MESSAGE_ID_CREATEPIXELSHADER_INVALIDSHADERBYTECODE) {
  1206. invalidBytecodeFound = true;
  1207. break;
  1208. }
  1209. }
  1210. if (invalidBytecodeFound) {
  1211. LogCommentFmt(L"%s", L"Found an invalid bytecode message. This "
  1212. L"typically indicates that experimental mode "
  1213. L"is not set up properly.");
  1214. if (!GetTestParamBool(L"ExperimentalShaders")) {
  1215. LogCommentFmt(L"Note that the ExperimentalShaders test parameter isn't set.");
  1216. }
  1217. }
  1218. else {
  1219. LogCommentFmt(L"Did not find corrupt pixel or vertex shaders in "
  1220. L"queue - dumping complete queue.");
  1221. WriteInfoQueueMessages(nullptr, OutputFn, m_pInfoQueue);
  1222. }
  1223. }
  1224. }
  1225. static void __stdcall OutputFn(void *pCtx, const wchar_t *pMsg) {
  1226. LogCommentFmt(L"%s", pMsg);
  1227. }
  1228. };
  1229. BasicTestChecker BTC(pDevice);
  1230. {
  1231. InitFenceObj(pDevice, &FO);
  1232. CreateRtvDescriptorHeap(pDevice, FrameCount, &pRtvHeap, &rtvDescriptorSize);
  1233. CreateRenderTargetAndReadback(pDevice, pRtvHeap, m_width, m_height, &pRenderTarget, &pReadBuffer);
  1234. // Create an empty root signature.
  1235. CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
  1236. rootSignatureDesc.Init(
  1237. 0, nullptr, 0, nullptr,
  1238. D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
  1239. CreateRootSignatureFromDesc(pDevice, &rootSignatureDesc, &pRootSig);
  1240. // Create the pipeline state, which includes compiling and loading shaders.
  1241. // Define the vertex input layout.
  1242. D3D12_INPUT_ELEMENT_DESC inputElementDescs[] = {
  1243. {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,
  1244. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
  1245. {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12,
  1246. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}};
  1247. D3D12_INPUT_LAYOUT_DESC InputLayout = { inputElementDescs, _countof(inputElementDescs) };
  1248. CreateGraphicsPSO(pDevice, &InputLayout, pRootSig, pShaders, &pPipelineState);
  1249. CreateGraphicsCommandQueueAndList(pDevice, &pCommandQueue,
  1250. &pCommandAllocator, &pCommandList,
  1251. pPipelineState);
  1252. // Define the geometry for a triangle.
  1253. Vertex triangleVertices[] = {
  1254. { { 0.0f, 0.25f * m_aspectRatio, 0.0f },{ 1.0f, 0.0f, 0.0f, 1.0f } },
  1255. { { 0.25f, -0.25f * m_aspectRatio, 0.0f },{ 0.0f, 1.0f, 0.0f, 1.0f } },
  1256. { { -0.25f, -0.25f * m_aspectRatio, 0.0f },{ 0.0f, 0.0f, 1.0f, 1.0f } } };
  1257. CreateVertexBuffer(pDevice, triangleVertices, &pVertexBuffer, &vertexBufferView);
  1258. WaitForSignal(pCommandQueue, FO);
  1259. }
  1260. // Render and execute the command list.
  1261. RecordRenderAndReadback(pCommandList, pRtvHeap, rtvDescriptorSize, 1,
  1262. &vertexBufferView, pRootSig, pRenderTarget,
  1263. pReadBuffer);
  1264. VERIFY_SUCCEEDED(pCommandList->Close());
  1265. ExecuteCommandList(pCommandQueue, pCommandList);
  1266. // Wait for previous frame.
  1267. WaitForSignal(pCommandQueue, FO);
  1268. // At this point, we've verified that execution succeeded with DXIL.
  1269. BTC.SetOK(true);
  1270. // Read back to CPU and examine contents.
  1271. {
  1272. MappedData data(pReadBuffer, m_width * m_height * 4);
  1273. const uint32_t *pPixels = (uint32_t *)data.data();
  1274. if (SaveImages()) {
  1275. SavePixelsToFile(pPixels, DXGI_FORMAT_R8G8B8A8_UNORM, m_width, m_height, L"basic.bmp");
  1276. }
  1277. uint32_t top = pPixels[m_width / 2]; // Top center.
  1278. uint32_t mid = pPixels[m_width / 2 + m_width * (m_height / 2)]; // Middle center.
  1279. VERIFY_ARE_EQUAL(0xff663300, top); // clear color
  1280. VERIFY_ARE_EQUAL(0xffffffff, mid); // white
  1281. }
  1282. }
  1283. TEST_F(ExecutionTest, Int64Test) {
  1284. static const char pShader[] =
  1285. "RWByteAddressBuffer g_bab : register(u0);\r\n"
  1286. "[numthreads(8,8,1)]\r\n"
  1287. "void main(uint GI : SV_GroupIndex) {"
  1288. " uint addr = GI * 4;\r\n"
  1289. " uint val = g_bab.Load(addr);\r\n"
  1290. " uint64_t u64 = val;\r\n"
  1291. " u64 *= val;\r\n"
  1292. " g_bab.Store(addr, (uint)(u64 >> 32));\r\n"
  1293. "}";
  1294. static const int NumThreadsX = 8;
  1295. static const int NumThreadsY = 8;
  1296. static const int NumThreadsZ = 1;
  1297. static const int ThreadsPerGroup = NumThreadsX * NumThreadsY * NumThreadsZ;
  1298. static const int DispatchGroupCount = 1;
  1299. CComPtr<ID3D12Device> pDevice;
  1300. if (!CreateDevice(&pDevice))
  1301. return;
  1302. if (!DoesDeviceSupportInt64(pDevice)) {
  1303. // Optional feature, so it's correct to not support it if declared as such.
  1304. WEX::Logging::Log::Comment(L"Device does not support int64 operations.");
  1305. return;
  1306. }
  1307. std::vector<uint32_t> values;
  1308. SetupComputeValuePattern(values, ThreadsPerGroup * DispatchGroupCount);
  1309. VERIFY_ARE_EQUAL(values[0], 0);
  1310. RunRWByteBufferComputeTest(pDevice, pShader, values);
  1311. VERIFY_ARE_EQUAL(values[0], 0);
  1312. }
  1313. TEST_F(ExecutionTest, SignTest) {
  1314. static const char pShader[] =
  1315. "RWByteAddressBuffer g_bab : register(u0);\r\n"
  1316. "[numthreads(8,1,1)]\r\n"
  1317. "void main(uint GI : SV_GroupIndex) {"
  1318. " uint addr = GI * 4;\r\n"
  1319. " int val = g_bab.Load(addr);\r\n"
  1320. " g_bab.Store(addr, (uint)(sign(val)));\r\n"
  1321. "}";
  1322. static const int NumThreadsX = 8;
  1323. static const int NumThreadsY = 1;
  1324. static const int NumThreadsZ = 1;
  1325. static const int ThreadsPerGroup = NumThreadsX * NumThreadsY * NumThreadsZ;
  1326. static const int DispatchGroupCount = 1;
  1327. CComPtr<ID3D12Device> pDevice;
  1328. if (!CreateDevice(&pDevice))
  1329. return;
  1330. std::vector<uint32_t> values = { (uint32_t)-3, (uint32_t)-2, (uint32_t)-1, 0, 1, 2, 3, 4};
  1331. RunRWByteBufferComputeTest(pDevice, pShader, values);
  1332. VERIFY_ARE_EQUAL(values[0], -1);
  1333. VERIFY_ARE_EQUAL(values[1], -1);
  1334. VERIFY_ARE_EQUAL(values[2], -1);
  1335. VERIFY_ARE_EQUAL(values[3], 0);
  1336. VERIFY_ARE_EQUAL(values[4], 1);
  1337. VERIFY_ARE_EQUAL(values[5], 1);
  1338. VERIFY_ARE_EQUAL(values[6], 1);
  1339. VERIFY_ARE_EQUAL(values[7], 1);
  1340. }
  1341. TEST_F(ExecutionTest, WaveIntrinsicsTest) {
  1342. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  1343. struct PerThreadData {
  1344. uint32_t id, flags, laneIndex, laneCount, firstLaneId, preds, firstlaneX, lane1X;
  1345. uint32_t allBC, allSum, allProd, allAND, allOR, allXOR, allMin, allMax;
  1346. uint32_t pfBC, pfSum, pfProd;
  1347. uint32_t ballot[4];
  1348. uint32_t diver; // divergent value, used in calculation
  1349. int32_t i_diver; // divergent value, used in calculation
  1350. int32_t i_allMax, i_allMin, i_allSum, i_allProd;
  1351. int32_t i_pfSum, i_pfProd;
  1352. };
  1353. static const char pShader[] =
  1354. WAVE_INTRINSIC_DXBC_GUARD
  1355. "struct PerThreadData {\r\n"
  1356. " uint id, flags, laneIndex, laneCount, firstLaneId, preds, firstlaneX, lane1X;\r\n"
  1357. " uint allBC, allSum, allProd, allAND, allOR, allXOR, allMin, allMax;\r\n"
  1358. " uint pfBC, pfSum, pfProd;\r\n"
  1359. " uint4 ballot;\r\n"
  1360. " uint diver;\r\n"
  1361. " int i_diver;\r\n"
  1362. " int i_allMax, i_allMin, i_allSum, i_allProd;\r\n"
  1363. " int i_pfSum, i_pfProd;\r\n"
  1364. "};\r\n"
  1365. "RWStructuredBuffer<PerThreadData> g_sb : register(u0);\r\n"
  1366. "[numthreads(8,8,1)]\r\n"
  1367. "void main(uint GI : SV_GroupIndex, uint3 GTID : SV_GroupThreadID) {"
  1368. " PerThreadData pts = g_sb[GI];\r\n"
  1369. " uint diver = GTID.x + 2;\r\n"
  1370. " pts.diver = diver;\r\n"
  1371. " pts.flags = 0;\r\n"
  1372. " pts.preds = 0;\r\n"
  1373. " if (WaveIsFirstLane()) pts.flags |= 1;\r\n"
  1374. " pts.laneIndex = WaveGetLaneIndex();\r\n"
  1375. " pts.laneCount = WaveGetLaneCount();\r\n"
  1376. " pts.firstLaneId = WaveReadLaneFirst(pts.id);\r\n"
  1377. " pts.preds |= ((WaveActiveAnyTrue(diver == 1) ? 1 : 0) << 0);\r\n"
  1378. " pts.preds |= ((WaveActiveAllTrue(diver == 1) ? 1 : 0) << 1);\r\n"
  1379. " pts.preds |= ((WaveActiveAllEqual(diver) ? 1 : 0) << 2);\r\n"
  1380. " pts.preds |= ((WaveActiveAllEqual(GTID.z) ? 1 : 0) << 3);\r\n"
  1381. " pts.preds |= ((WaveActiveAllEqual(WaveReadLaneFirst(diver)) ? 1 : 0) << 4);\r\n"
  1382. " pts.ballot = WaveActiveBallot(diver > 3);\r\n"
  1383. " pts.firstlaneX = WaveReadLaneFirst(GTID.x);\r\n"
  1384. " pts.lane1X = WaveReadLaneAt(GTID.x, 1);\r\n"
  1385. "\r\n"
  1386. " pts.allBC = WaveActiveCountBits(diver > 3);\r\n"
  1387. " pts.allSum = WaveActiveSum(diver);\r\n"
  1388. " pts.allProd = WaveActiveProduct(diver);\r\n"
  1389. " pts.allAND = WaveActiveBitAnd(diver);\r\n"
  1390. " pts.allOR = WaveActiveBitOr(diver);\r\n"
  1391. " pts.allXOR = WaveActiveBitXor(diver);\r\n"
  1392. " pts.allMin = WaveActiveMin(diver);\r\n"
  1393. " pts.allMax = WaveActiveMax(diver);\r\n"
  1394. "\r\n"
  1395. " pts.pfBC = WavePrefixCountBits(diver > 3);\r\n"
  1396. " pts.pfSum = WavePrefixSum(diver);\r\n"
  1397. " pts.pfProd = WavePrefixProduct(diver);\r\n"
  1398. "\r\n"
  1399. " int i_diver = pts.i_diver;\r\n"
  1400. " pts.i_allMax = WaveActiveMax(i_diver);\r\n"
  1401. " pts.i_allMin = WaveActiveMin(i_diver);\r\n"
  1402. " pts.i_allSum = WaveActiveSum(i_diver);\r\n"
  1403. " pts.i_allProd = WaveActiveProduct(i_diver);\r\n"
  1404. " pts.i_pfSum = WavePrefixSum(i_diver);\r\n"
  1405. " pts.i_pfProd = WavePrefixProduct(i_diver);\r\n"
  1406. "\r\n"
  1407. " g_sb[GI] = pts;\r\n"
  1408. "}";
  1409. static const int NumtheadsX = 8;
  1410. static const int NumtheadsY = 8;
  1411. static const int NumtheadsZ = 1;
  1412. static const int ThreadsPerGroup = NumtheadsX * NumtheadsY * NumtheadsZ;
  1413. static const int DispatchGroupCount = 1;
  1414. CComPtr<ID3D12Device> pDevice;
  1415. if (!CreateDevice(&pDevice))
  1416. return;
  1417. if (!DoesDeviceSupportWaveOps(pDevice)) {
  1418. // Optional feature, so it's correct to not support it if declared as such.
  1419. WEX::Logging::Log::Comment(L"Device does not support wave operations.");
  1420. return;
  1421. }
  1422. std::vector<PerThreadData> values;
  1423. values.resize(ThreadsPerGroup * DispatchGroupCount);
  1424. for (size_t i = 0; i < values.size(); ++i) {
  1425. memset(&values[i], 0, sizeof(PerThreadData));
  1426. values[i].id = i;
  1427. values[i].i_diver = (int)i;
  1428. values[i].i_diver *= (i % 2) ? 1 : -1;
  1429. }
  1430. static const int DispatchGroupX = 1;
  1431. static const int DispatchGroupY = 1;
  1432. static const int DispatchGroupZ = 1;
  1433. CComPtr<ID3D12GraphicsCommandList> pCommandList;
  1434. CComPtr<ID3D12CommandQueue> pCommandQueue;
  1435. CComPtr<ID3D12DescriptorHeap> pUavHeap;
  1436. CComPtr<ID3D12CommandAllocator> pCommandAllocator;
  1437. UINT uavDescriptorSize;
  1438. FenceObj FO;
  1439. bool dxbc = UseDxbc();
  1440. const size_t valueSizeInBytes = values.size() * sizeof(PerThreadData);
  1441. CreateComputeCommandQueue(pDevice, L"WaveIntrinsicsTest Command Queue", &pCommandQueue);
  1442. InitFenceObj(pDevice, &FO);
  1443. // Describe and create a UAV descriptor heap.
  1444. D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
  1445. heapDesc.NumDescriptors = 1;
  1446. heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  1447. heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  1448. VERIFY_SUCCEEDED(pDevice->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&pUavHeap)));
  1449. uavDescriptorSize = pDevice->GetDescriptorHandleIncrementSize(heapDesc.Type);
  1450. // Create root signature.
  1451. CComPtr<ID3D12RootSignature> pRootSignature;
  1452. {
  1453. CD3DX12_DESCRIPTOR_RANGE ranges[1];
  1454. ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 0, 0, 0);
  1455. CD3DX12_ROOT_PARAMETER rootParameters[1];
  1456. rootParameters[0].InitAsDescriptorTable(1, &ranges[0], D3D12_SHADER_VISIBILITY_ALL);
  1457. CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
  1458. rootSignatureDesc.Init(_countof(rootParameters), rootParameters, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE);
  1459. CComPtr<ID3DBlob> signature;
  1460. CComPtr<ID3DBlob> error;
  1461. VERIFY_SUCCEEDED(D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
  1462. VERIFY_SUCCEEDED(pDevice->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&pRootSignature)));
  1463. }
  1464. // Create pipeline state object.
  1465. CComPtr<ID3D12PipelineState> pComputeState;
  1466. CreateComputePSO(pDevice, pRootSignature, pShader, &pComputeState);
  1467. // Create a command allocator and list for compute.
  1468. VERIFY_SUCCEEDED(pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE, IID_PPV_ARGS(&pCommandAllocator)));
  1469. VERIFY_SUCCEEDED(pDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, pCommandAllocator, pComputeState, IID_PPV_ARGS(&pCommandList)));
  1470. // Set up UAV resource.
  1471. CComPtr<ID3D12Resource> pUavResource;
  1472. CComPtr<ID3D12Resource> pReadBuffer;
  1473. CComPtr<ID3D12Resource> pUploadResource;
  1474. CreateTestUavs(pDevice, pCommandList, values.data(), valueSizeInBytes, &pUavResource, &pReadBuffer, &pUploadResource);
  1475. // Close the command list and execute it to perform the GPU setup.
  1476. pCommandList->Close();
  1477. ExecuteCommandList(pCommandQueue, pCommandList);
  1478. WaitForSignal(pCommandQueue, FO);
  1479. VERIFY_SUCCEEDED(pCommandAllocator->Reset());
  1480. VERIFY_SUCCEEDED(pCommandList->Reset(pCommandAllocator, pComputeState));
  1481. // Run the compute shader and copy the results back to readable memory.
  1482. {
  1483. D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
  1484. uavDesc.Format = DXGI_FORMAT_UNKNOWN;
  1485. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
  1486. uavDesc.Buffer.FirstElement = 0;
  1487. uavDesc.Buffer.NumElements = values.size();
  1488. uavDesc.Buffer.StructureByteStride = sizeof(PerThreadData);
  1489. uavDesc.Buffer.CounterOffsetInBytes = 0;
  1490. uavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE;
  1491. CD3DX12_CPU_DESCRIPTOR_HANDLE uavHandle(pUavHeap->GetCPUDescriptorHandleForHeapStart());
  1492. CD3DX12_GPU_DESCRIPTOR_HANDLE uavHandleGpu(pUavHeap->GetGPUDescriptorHandleForHeapStart());
  1493. pDevice->CreateUnorderedAccessView(pUavResource, nullptr, &uavDesc, uavHandle);
  1494. SetDescriptorHeap(pCommandList, pUavHeap);
  1495. pCommandList->SetComputeRootSignature(pRootSignature);
  1496. pCommandList->SetComputeRootDescriptorTable(0, uavHandleGpu);
  1497. }
  1498. pCommandList->Dispatch(DispatchGroupX, DispatchGroupY, DispatchGroupZ);
  1499. RecordTransitionBarrier(pCommandList, pUavResource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
  1500. pCommandList->CopyResource(pReadBuffer, pUavResource);
  1501. pCommandList->Close();
  1502. ExecuteCommandList(pCommandQueue, pCommandList);
  1503. WaitForSignal(pCommandQueue, FO);
  1504. {
  1505. MappedData mappedData(pReadBuffer, valueSizeInBytes);
  1506. PerThreadData *pData = (PerThreadData *)mappedData.data();
  1507. memcpy(values.data(), pData, valueSizeInBytes);
  1508. // Gather some general data.
  1509. // The 'firstLaneId' captures a unique number per first-lane per wave.
  1510. // Counting the number distinct firstLaneIds gives us the number of waves.
  1511. std::vector<uint32_t> firstLaneIds;
  1512. for (size_t i = 0; i < values.size(); ++i) {
  1513. PerThreadData &pts = values[i];
  1514. uint32_t firstLaneId = pts.firstLaneId;
  1515. if (!contains(firstLaneIds, firstLaneId)) {
  1516. firstLaneIds.push_back(firstLaneId);
  1517. }
  1518. }
  1519. // Waves should cover 4 threads or more.
  1520. LogCommentFmt(L"Found %u distinct lane ids: %u", firstLaneIds.size());
  1521. if (!dxbc) {
  1522. VERIFY_IS_GREATER_THAN_OR_EQUAL(values.size() / 4, firstLaneIds.size());
  1523. }
  1524. // Now, group threads into waves.
  1525. std::map<uint32_t, std::unique_ptr<std::vector<PerThreadData *> > > waves;
  1526. for (size_t i = 0; i < firstLaneIds.size(); ++i) {
  1527. waves[firstLaneIds[i]] = std::make_unique<std::vector<PerThreadData *> >();
  1528. }
  1529. for (size_t i = 0; i < values.size(); ++i) {
  1530. PerThreadData &pts = values[i];
  1531. std::unique_ptr<std::vector<PerThreadData *> > &wave = waves[pts.firstLaneId];
  1532. wave->push_back(&pts);
  1533. }
  1534. // Verify that all the wave values are coherent across the wave.
  1535. for (size_t i = 0; i < values.size(); ++i) {
  1536. PerThreadData &pts = values[i];
  1537. std::unique_ptr<std::vector<PerThreadData *> > &wave = waves[pts.firstLaneId];
  1538. // Sort the lanes by increasing lane ID.
  1539. struct LaneIdOrderPred {
  1540. bool operator()(PerThreadData *a, PerThreadData *b) {
  1541. return a->laneIndex < b->laneIndex;
  1542. }
  1543. };
  1544. std::sort(wave.get()->begin(), wave.get()->end(), LaneIdOrderPred());
  1545. // Verify some interesting properties of the first lane.
  1546. uint32_t pfBC, pfSum, pfProd;
  1547. int32_t i_pfSum, i_pfProd;
  1548. int32_t i_allMax, i_allMin;
  1549. {
  1550. PerThreadData *ptdFirst = wave->front();
  1551. VERIFY_IS_TRUE(0 != (ptdFirst->flags & 1)); // FirstLane sets this bit.
  1552. VERIFY_IS_TRUE(0 == ptdFirst->pfBC);
  1553. VERIFY_IS_TRUE(0 == ptdFirst->pfSum);
  1554. VERIFY_IS_TRUE(1 == ptdFirst->pfProd);
  1555. VERIFY_IS_TRUE(0 == ptdFirst->i_pfSum);
  1556. VERIFY_IS_TRUE(1 == ptdFirst->i_pfProd);
  1557. pfBC = (ptdFirst->diver > 3) ? 1 : 0;
  1558. pfSum = ptdFirst->diver;
  1559. pfProd = ptdFirst->diver;
  1560. i_pfSum = ptdFirst->i_diver;
  1561. i_pfProd = ptdFirst->i_diver;
  1562. i_allMax = i_allMin = ptdFirst->i_diver;
  1563. }
  1564. // Calculate values which take into consideration all lanes.
  1565. uint32_t preds = 0;
  1566. preds |= 1 << 1; // AllTrue starts true, switches to false if needed.
  1567. preds |= 1 << 2; // AllEqual starts true, switches to false if needed.
  1568. preds |= 1 << 3; // WaveActiveAllEqual(GTID.z) is always true
  1569. preds |= 1 << 4; // (WaveActiveAllEqual(WaveReadLaneFirst(diver)) is always true
  1570. uint32_t ballot[4] = { 0, 0, 0, 0 };
  1571. int32_t i_allSum = 0, i_allProd = 1;
  1572. for (size_t n = 0; n < wave->size(); ++n) {
  1573. std::vector<PerThreadData *> &lanes = *wave.get();
  1574. // pts.preds |= ((WaveActiveAnyTrue(diver == 1) ? 1 : 0) << 0);
  1575. if (lanes[n]->diver == 1) preds |= (1 << 0);
  1576. // pts.preds |= ((WaveActiveAllTrue(diver == 1) ? 1 : 0) << 1);
  1577. if (lanes[n]->diver != 1) preds &= ~(1 << 1);
  1578. // pts.preds |= ((WaveActiveAllEqual(diver) ? 1 : 0) << 2);
  1579. if (lanes[0]->diver != lanes[n]->diver) preds &= ~(1 << 2);
  1580. // pts.ballot = WaveActiveBallot(diver > 3);\r\n"
  1581. if (lanes[n]->diver > 3) {
  1582. // This is the uint4 result layout:
  1583. // .x -> bits 0 .. 31
  1584. // .y -> bits 32 .. 63
  1585. // .z -> bits 64 .. 95
  1586. // .w -> bits 96 ..127
  1587. uint32_t component = lanes[n]->laneIndex / 32;
  1588. uint32_t bit = lanes[n]->laneIndex % 32;
  1589. ballot[component] |= 1 << bit;
  1590. }
  1591. i_allMax = std::max(lanes[n]->i_diver, i_allMax);
  1592. i_allMin = std::min(lanes[n]->i_diver, i_allMin);
  1593. i_allProd *= lanes[n]->i_diver;
  1594. i_allSum += lanes[n]->i_diver;
  1595. }
  1596. for (size_t n = 1; n < wave->size(); ++n) {
  1597. // 'All' operations are uniform across the wave.
  1598. std::vector<PerThreadData *> &lanes = *wave.get();
  1599. VERIFY_IS_TRUE(0 == (lanes[n]->flags & 1)); // non-firstlanes do not set this bit
  1600. VERIFY_ARE_EQUAL(lanes[0]->allBC, lanes[n]->allBC);
  1601. VERIFY_ARE_EQUAL(lanes[0]->allSum, lanes[n]->allSum);
  1602. VERIFY_ARE_EQUAL(lanes[0]->allProd, lanes[n]->allProd);
  1603. VERIFY_ARE_EQUAL(lanes[0]->allAND, lanes[n]->allAND);
  1604. VERIFY_ARE_EQUAL(lanes[0]->allOR, lanes[n]->allOR);
  1605. VERIFY_ARE_EQUAL(lanes[0]->allXOR, lanes[n]->allXOR);
  1606. VERIFY_ARE_EQUAL(lanes[0]->allMin, lanes[n]->allMin);
  1607. VERIFY_ARE_EQUAL(lanes[0]->allMax, lanes[n]->allMax);
  1608. VERIFY_ARE_EQUAL(i_allMax, lanes[n]->i_allMax);
  1609. VERIFY_ARE_EQUAL(i_allMin, lanes[n]->i_allMin);
  1610. VERIFY_ARE_EQUAL(i_allProd, lanes[n]->i_allProd);
  1611. VERIFY_ARE_EQUAL(i_allSum, lanes[n]->i_allSum);
  1612. // first-lane reads and uniform reads are uniform across the wave.
  1613. VERIFY_ARE_EQUAL(lanes[0]->firstlaneX, lanes[n]->firstlaneX);
  1614. VERIFY_ARE_EQUAL(lanes[0]->lane1X, lanes[n]->lane1X);
  1615. // the lane count is uniform across the wave.
  1616. VERIFY_ARE_EQUAL(lanes[0]->laneCount, lanes[n]->laneCount);
  1617. // The predicates are uniform across the wave.
  1618. VERIFY_ARE_EQUAL(lanes[n]->preds, preds);
  1619. // the lane index is distinct per thread.
  1620. for (size_t prior = 0; prior < n; ++prior) {
  1621. VERIFY_ARE_NOT_EQUAL(lanes[prior]->laneIndex, lanes[n]->laneIndex);
  1622. }
  1623. // Ballot results are uniform across the wave.
  1624. VERIFY_ARE_EQUAL(0, memcmp(ballot, lanes[n]->ballot, sizeof(ballot)));
  1625. // Keep running total of prefix calculation. Prefix values are exclusive to
  1626. // the executing lane.
  1627. VERIFY_ARE_EQUAL(pfBC, lanes[n]->pfBC);
  1628. VERIFY_ARE_EQUAL(pfSum, lanes[n]->pfSum);
  1629. VERIFY_ARE_EQUAL(pfProd, lanes[n]->pfProd);
  1630. VERIFY_ARE_EQUAL(i_pfSum, lanes[n]->i_pfSum);
  1631. VERIFY_ARE_EQUAL(i_pfProd, lanes[n]->i_pfProd);
  1632. pfBC += (lanes[n]->diver > 3) ? 1 : 0;
  1633. pfSum += lanes[n]->diver;
  1634. pfProd *= lanes[n]->diver;
  1635. i_pfSum += lanes[n]->i_diver;
  1636. i_pfProd *= lanes[n]->i_diver;
  1637. }
  1638. // TODO: add divergent branching and verify that the otherwise uniform values properly diverge
  1639. }
  1640. // Compare each value of each per-thread element.
  1641. for (size_t i = 0; i < values.size(); ++i) {
  1642. PerThreadData &pts = values[i];
  1643. VERIFY_ARE_EQUAL(i, pts.id); // ID is unchanged.
  1644. }
  1645. }
  1646. }
  1647. TEST_F(ExecutionTest, WaveIntrinsicsInPSTest) {
  1648. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  1649. struct Vertex {
  1650. XMFLOAT3 position;
  1651. };
  1652. struct PerPixelData {
  1653. XMFLOAT4 position;
  1654. uint32_t id, flags, laneIndex, laneCount, firstLaneId, sum1;
  1655. uint32_t id0, id1, id2, id3;
  1656. uint32_t acrossX, acrossY, acrossDiag, quadActiveCount;
  1657. };
  1658. const UINT RTWidth = 128;
  1659. const UINT RTHeight = 128;
  1660. // Shaders.
  1661. static const char pShaders[] =
  1662. WAVE_INTRINSIC_DXBC_GUARD
  1663. "struct PSInput {\r\n"
  1664. " float4 position : SV_POSITION;\r\n"
  1665. "};\r\n\r\n"
  1666. "PSInput VSMain(float4 position : POSITION) {\r\n"
  1667. " PSInput result;\r\n"
  1668. "\r\n"
  1669. " result.position = position;\r\n"
  1670. " return result;\r\n"
  1671. "}\r\n\r\n"
  1672. "typedef uint uint32_t;\r\n"
  1673. "uint pos_to_id(float4 pos) { return pos.x * 128 + pos.y; }\r\n"
  1674. "struct PerPixelData {\r\n"
  1675. " float4 position;\r\n"
  1676. " uint32_t id, flags, laneIndex, laneCount, firstLaneId, sum1;\r\n"
  1677. " uint32_t id0, id1, id2, id3;\r\n"
  1678. " uint32_t acrossX, acrossY, acrossDiag, quadActiveCount;\r\n"
  1679. "};\r\n"
  1680. "AppendStructuredBuffer<PerPixelData> g_sb : register(u1);\r\n"
  1681. "float4 PSMain(PSInput input) : SV_TARGET {\r\n"
  1682. " uint one = 1;\r\n"
  1683. " PerPixelData d;\r\n"
  1684. " d.position = input.position;\r\n"
  1685. " d.id = pos_to_id(input.position);\r\n"
  1686. " d.flags = 0;\r\n"
  1687. " if (WaveIsFirstLane()) d.flags |= 1;\r\n"
  1688. " d.laneIndex = WaveGetLaneIndex();\r\n"
  1689. " d.laneCount = WaveGetLaneCount();\r\n"
  1690. " d.firstLaneId = WaveReadLaneFirst(d.id);\r\n"
  1691. " d.sum1 = WaveActiveSum(one);\r\n"
  1692. " d.id0 = QuadReadLaneAt(d.id, 0);\r\n"
  1693. " d.id1 = QuadReadLaneAt(d.id, 1);\r\n"
  1694. " d.id2 = QuadReadLaneAt(d.id, 2);\r\n"
  1695. " d.id3 = QuadReadLaneAt(d.id, 3);\r\n"
  1696. " d.acrossX = QuadReadAcrossX(d.id);\r\n"
  1697. " d.acrossY = QuadReadAcrossY(d.id);\r\n"
  1698. " d.acrossDiag = QuadReadAcrossDiagonal(d.id);\r\n"
  1699. " d.quadActiveCount = one + QuadReadAcrossX(one) + QuadReadAcrossY(one) + QuadReadAcrossDiagonal(one);\r\n"
  1700. " g_sb.Append(d);\r\n"
  1701. " return 1;\r\n"
  1702. "};\r\n";
  1703. CComPtr<ID3D12Device> pDevice;
  1704. CComPtr<ID3D12CommandQueue> pCommandQueue;
  1705. CComPtr<ID3D12DescriptorHeap> pUavHeap, pRtvHeap;
  1706. CComPtr<ID3D12CommandAllocator> pCommandAllocator;
  1707. CComPtr<ID3D12GraphicsCommandList> pCommandList;
  1708. CComPtr<ID3D12PipelineState> pPSO;
  1709. CComPtr<ID3D12Resource> pRenderTarget, pReadBuffer;
  1710. UINT uavDescriptorSize, rtvDescriptorSize;
  1711. CComPtr<ID3D12Resource> pVertexBuffer;
  1712. D3D12_VERTEX_BUFFER_VIEW vertexBufferView;
  1713. if (!CreateDevice(&pDevice))
  1714. return;
  1715. if (!DoesDeviceSupportWaveOps(pDevice)) {
  1716. // Optional feature, so it's correct to not support it if declared as such.
  1717. WEX::Logging::Log::Comment(L"Device does not support wave operations.");
  1718. return;
  1719. }
  1720. FenceObj FO;
  1721. InitFenceObj(pDevice, &FO);
  1722. // Describe and create a UAV descriptor heap.
  1723. D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
  1724. heapDesc.NumDescriptors = 1;
  1725. heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  1726. heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  1727. VERIFY_SUCCEEDED(pDevice->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&pUavHeap)));
  1728. uavDescriptorSize = pDevice->GetDescriptorHandleIncrementSize(heapDesc.Type);
  1729. CreateRtvDescriptorHeap(pDevice, 1, &pRtvHeap, &rtvDescriptorSize);
  1730. CreateRenderTargetAndReadback(pDevice, pRtvHeap, RTHeight, RTWidth, &pRenderTarget, &pReadBuffer);
  1731. // Create root signature: one UAV.
  1732. CComPtr<ID3D12RootSignature> pRootSignature;
  1733. {
  1734. CD3DX12_DESCRIPTOR_RANGE ranges[1];
  1735. ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 1, 0, 0);
  1736. CD3DX12_ROOT_PARAMETER rootParameters[1];
  1737. rootParameters[0].InitAsDescriptorTable(1, &ranges[0], D3D12_SHADER_VISIBILITY_ALL);
  1738. CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
  1739. rootSignatureDesc.Init(_countof(rootParameters), rootParameters, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
  1740. CreateRootSignatureFromDesc(pDevice, &rootSignatureDesc, &pRootSignature);
  1741. }
  1742. D3D12_INPUT_ELEMENT_DESC elementDesc[] = {
  1743. {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,
  1744. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}};
  1745. D3D12_INPUT_LAYOUT_DESC InputLayout = {elementDesc, _countof(elementDesc)};
  1746. CreateGraphicsPSO(pDevice, &InputLayout, pRootSignature, pShaders, &pPSO);
  1747. CreateGraphicsCommandQueueAndList(pDevice, &pCommandQueue, &pCommandAllocator,
  1748. &pCommandList, pPSO);
  1749. // Single triangle covering half the target.
  1750. Vertex vertices[] = {
  1751. { { -1.0f, 1.0f, 0.0f } },
  1752. { { 1.0f, 1.0f, 0.0f } },
  1753. { { -1.0f, -1.0f, 0.0f } } };
  1754. const UINT TriangleCount = _countof(vertices) / 3;
  1755. CreateVertexBuffer(pDevice, vertices, &pVertexBuffer, &vertexBufferView);
  1756. bool dxbc = UseDxbc();
  1757. // Set up UAV resource.
  1758. std::vector<PerPixelData> values;
  1759. values.resize(RTWidth * RTHeight * 2);
  1760. UINT valueSizeInBytes = values.size() * sizeof(PerPixelData);
  1761. memset(values.data(), 0, valueSizeInBytes);
  1762. CComPtr<ID3D12Resource> pUavResource;
  1763. CComPtr<ID3D12Resource> pUavReadBuffer;
  1764. CComPtr<ID3D12Resource> pUploadResource;
  1765. CreateTestUavs(pDevice, pCommandList, values.data(), valueSizeInBytes, &pUavResource, &pUavReadBuffer, &pUploadResource);
  1766. // Set up the append counter resource.
  1767. CComPtr<ID3D12Resource> pUavCounterResource;
  1768. CComPtr<ID3D12Resource> pReadCounterBuffer;
  1769. CComPtr<ID3D12Resource> pUploadCounterResource;
  1770. BYTE zero[sizeof(UINT)] = { 0 };
  1771. CreateTestUavs(pDevice, pCommandList, zero, sizeof(zero), &pUavCounterResource, &pReadCounterBuffer, &pUploadCounterResource);
  1772. // Close the command list and execute it to perform the GPU setup.
  1773. pCommandList->Close();
  1774. ExecuteCommandList(pCommandQueue, pCommandList);
  1775. WaitForSignal(pCommandQueue, FO);
  1776. VERIFY_SUCCEEDED(pCommandAllocator->Reset());
  1777. VERIFY_SUCCEEDED(pCommandList->Reset(pCommandAllocator, pPSO));
  1778. pCommandList->SetGraphicsRootSignature(pRootSignature);
  1779. SetDescriptorHeap(pCommandList, pUavHeap);
  1780. {
  1781. D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
  1782. uavDesc.Format = DXGI_FORMAT_UNKNOWN;
  1783. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
  1784. uavDesc.Buffer.FirstElement = 0;
  1785. uavDesc.Buffer.NumElements = values.size();
  1786. uavDesc.Buffer.StructureByteStride = sizeof(PerPixelData);
  1787. uavDesc.Buffer.CounterOffsetInBytes = 0;
  1788. uavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE;
  1789. CD3DX12_CPU_DESCRIPTOR_HANDLE uavHandle(pUavHeap->GetCPUDescriptorHandleForHeapStart());
  1790. CD3DX12_GPU_DESCRIPTOR_HANDLE uavHandleGpu(pUavHeap->GetGPUDescriptorHandleForHeapStart());
  1791. pDevice->CreateUnorderedAccessView(pUavResource, pUavCounterResource, &uavDesc, uavHandle);
  1792. pCommandList->SetGraphicsRootDescriptorTable(0, uavHandleGpu);
  1793. }
  1794. RecordRenderAndReadback(pCommandList, pRtvHeap, rtvDescriptorSize, TriangleCount, &vertexBufferView, nullptr, pRenderTarget, pReadBuffer);
  1795. RecordTransitionBarrier(pCommandList, pUavResource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
  1796. RecordTransitionBarrier(pCommandList, pUavCounterResource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
  1797. pCommandList->CopyResource(pUavReadBuffer, pUavResource);
  1798. pCommandList->CopyResource(pReadCounterBuffer, pUavCounterResource);
  1799. VERIFY_SUCCEEDED(pCommandList->Close());
  1800. LogCommentFmt(L"Rendering to %u by %u", RTWidth, RTHeight);
  1801. ExecuteCommandList(pCommandQueue, pCommandList);
  1802. WaitForSignal(pCommandQueue, FO);
  1803. {
  1804. MappedData data(pReadBuffer, RTWidth * RTHeight * 4);
  1805. const uint32_t *pPixels = (uint32_t *)data.data();
  1806. if (SaveImages()) {
  1807. SavePixelsToFile(pPixels, DXGI_FORMAT_R8G8B8A8_UNORM, RTWidth, RTHeight, L"psintrin.bmp");
  1808. }
  1809. }
  1810. uint32_t appendCount;
  1811. {
  1812. MappedData mappedData(pReadCounterBuffer, sizeof(uint32_t));
  1813. appendCount = *((uint32_t *)mappedData.data());
  1814. LogCommentFmt(L"%u elements in append buffer", appendCount);
  1815. }
  1816. {
  1817. MappedData mappedData(pUavReadBuffer, values.size());
  1818. PerPixelData *pData = (PerPixelData *)mappedData.data();
  1819. memcpy(values.data(), pData, valueSizeInBytes);
  1820. // DXBC is handy to test pipeline setup, but interesting functions are
  1821. // stubbed out, so there is no point in further validation.
  1822. if (dxbc)
  1823. return;
  1824. uint32_t maxActiveLaneCount = 0;
  1825. uint32_t maxLaneCount = 0;
  1826. for (uint32_t i = 0; i < appendCount; ++i) {
  1827. maxActiveLaneCount = std::max(maxActiveLaneCount, values[i].sum1);
  1828. maxLaneCount = std::max(maxLaneCount, values[i].laneCount);
  1829. }
  1830. uint32_t peerOfHelperLanes = 0;
  1831. for (uint32_t i = 0; i < appendCount; ++i) {
  1832. if (values[i].sum1 != maxActiveLaneCount) {
  1833. ++peerOfHelperLanes;
  1834. }
  1835. }
  1836. LogCommentFmt(
  1837. L"Found: %u threads. Waves reported up to %u total lanes, up "
  1838. L"to %u active lanes, and %u threads had helper/inactive lanes.",
  1839. appendCount, maxLaneCount, maxActiveLaneCount, peerOfHelperLanes);
  1840. // Group threads into quad invocations.
  1841. uint32_t singlePixelCount = 0;
  1842. uint32_t multiPixelCount = 0;
  1843. std::unordered_set<uint32_t> ids;
  1844. std::multimap<uint32_t, PerPixelData *> idGroups;
  1845. std::multimap<uint32_t, PerPixelData *> firstIdGroups;
  1846. for (uint32_t i = 0; i < appendCount; ++i) {
  1847. ids.insert(values[i].id);
  1848. idGroups.insert(std::make_pair(values[i].id, &values[i]));
  1849. firstIdGroups.insert(std::make_pair(values[i].firstLaneId, &values[i]));
  1850. }
  1851. for (uint32_t id : ids) {
  1852. if (idGroups.count(id) == 1)
  1853. ++singlePixelCount;
  1854. else
  1855. ++multiPixelCount;
  1856. }
  1857. LogCommentFmt(L"%u pixels were processed by a single thread. %u invocations were for shared pixels.",
  1858. singlePixelCount, multiPixelCount);
  1859. // Multiple threads may have tried to shade the same pixel.
  1860. // Where every pixel is distinct, it's very straightforward to validate.
  1861. {
  1862. auto cur = firstIdGroups.begin(), end = firstIdGroups.end();
  1863. while (cur != end) {
  1864. bool simpleWave = true;
  1865. uint32_t firstId = (*cur).first;
  1866. auto groupEnd = cur;
  1867. while (groupEnd != end && (*groupEnd).first == firstId) {
  1868. if (idGroups.count((*groupEnd).second->id) > 1)
  1869. simpleWave = false;
  1870. ++groupEnd;
  1871. }
  1872. if (simpleWave) {
  1873. // Break the wave into quads.
  1874. struct QuadData {
  1875. unsigned count;
  1876. PerPixelData *data[4];
  1877. };
  1878. std::map<uint32_t, QuadData> quads;
  1879. for (auto i = cur; i != groupEnd; ++i) {
  1880. uint32_t quadId = (*i).second->id0;
  1881. auto match = quads.find(quadId);
  1882. if (match == quads.end()) {
  1883. QuadData qdata;
  1884. qdata.count = 1;
  1885. qdata.data[0] = (*i).second;
  1886. quads.insert(std::make_pair(quadId, qdata));
  1887. }
  1888. else {
  1889. VERIFY_IS_TRUE((*match).second.count < 4);
  1890. (*match).second.data[(*match).second.count++] = (*i).second;
  1891. }
  1892. }
  1893. for (auto quadPair : quads) {
  1894. unsigned count = quadPair.second.count;
  1895. if (count < 2) continue;
  1896. PerPixelData **data = quadPair.second.data;
  1897. bool isTop[4];
  1898. bool isLeft[4];
  1899. PerPixelData helperData;
  1900. memset(&helperData, sizeof(helperData), 0);
  1901. PerPixelData *layout[4]; // tl,tr,bl,br
  1902. memset(layout, sizeof(layout), 0);
  1903. auto fnToLayout = [&](bool top, bool left) -> PerPixelData ** {
  1904. int idx = top ? 0 : 2;
  1905. idx += left ? 0 : 1;
  1906. return &layout[idx];
  1907. };
  1908. auto fnToLayoutData = [&](bool top, bool left) -> PerPixelData * {
  1909. PerPixelData **pResult = fnToLayout(top, left);
  1910. if (*pResult == nullptr) return &helperData;
  1911. return *pResult;
  1912. };
  1913. VERIFY_IS_TRUE(count <= 4);
  1914. if (count == 2) {
  1915. isTop[0] = data[0]->position.y < data[1]->position.y;
  1916. isTop[1] = (data[0]->position.y == data[1]->position.y) ? isTop[0] : !isTop[0];
  1917. isLeft[0] = data[0]->position.x < data[1]->position.x;
  1918. isLeft[1] = (data[0]->position.x == data[1]->position.x) ? isLeft[0] : !isLeft[0];
  1919. }
  1920. else {
  1921. // with at least three samples, we have distinct x and y coordinates.
  1922. float left = std::min(data[0]->position.x, data[1]->position.x);
  1923. left = std::min(data[2]->position.x, left);
  1924. float top = std::min(data[0]->position.y, data[1]->position.y);
  1925. top = std::min(data[2]->position.y, top);
  1926. for (unsigned i = 0; i < count; ++i) {
  1927. isTop[i] = data[i]->position.y == top;
  1928. isLeft[i] = data[i]->position.x == left;
  1929. }
  1930. }
  1931. for (unsigned i = 0; i < count; ++i) {
  1932. *(fnToLayout(isTop[i], isLeft[i])) = data[i];
  1933. }
  1934. // Finally, we have a proper quad reconstructed. Validate.
  1935. for (unsigned i = 0; i < count; ++i) {
  1936. PerPixelData *d = data[i];
  1937. VERIFY_ARE_EQUAL(d->id0, fnToLayoutData(true, true)->id);
  1938. VERIFY_ARE_EQUAL(d->id1, fnToLayoutData(true, false)->id);
  1939. VERIFY_ARE_EQUAL(d->id2, fnToLayoutData(false, true)->id);
  1940. VERIFY_ARE_EQUAL(d->id3, fnToLayoutData(false, false)->id);
  1941. VERIFY_ARE_EQUAL(d->acrossX, fnToLayoutData(isTop[i], !isLeft[i])->id);
  1942. VERIFY_ARE_EQUAL(d->acrossY, fnToLayoutData(!isTop[i], isLeft[i])->id);
  1943. VERIFY_ARE_EQUAL(d->acrossDiag, fnToLayoutData(!isTop[i], !isLeft[i])->id);
  1944. VERIFY_ARE_EQUAL(d->quadActiveCount, count);
  1945. }
  1946. }
  1947. }
  1948. cur = groupEnd;
  1949. }
  1950. }
  1951. // TODO: provide validation for quads where the same pixel was shaded multiple times
  1952. //
  1953. // Consider: for pixels that were shaded multiple times, check whether
  1954. // some grouping of threads into quads satisfies all value requirements.
  1955. }
  1956. }
  1957. struct ShaderOpTestResult {
  1958. st::ShaderOp *ShaderOp;
  1959. std::shared_ptr<st::ShaderOpSet> ShaderOpSet;
  1960. std::shared_ptr<st::ShaderOpTest> Test;
  1961. };
  1962. struct SPrimitives {
  1963. float f_float;
  1964. float f_float2;
  1965. float f_float_o;
  1966. float f_float2_o;
  1967. };
  1968. std::shared_ptr<ShaderOpTestResult>
  1969. RunShaderOpTestAfterParse(ID3D12Device *pDevice, dxc::DxcDllSupport &support,
  1970. IStream *pStream, LPCSTR pName,
  1971. st::ShaderOpTest::TInitCallbackFn pInitCallback, std::shared_ptr<st::ShaderOpSet> ShaderOpSet) {
  1972. DXASSERT_NOMSG(pStream != nullptr);
  1973. st::ShaderOp *pShaderOp;
  1974. if (pName == nullptr) {
  1975. if (ShaderOpSet->ShaderOps.size() != 1) {
  1976. VERIFY_FAIL(L"Expected a single shader operation.");
  1977. }
  1978. pShaderOp = ShaderOpSet->ShaderOps[0].get();
  1979. }
  1980. else {
  1981. pShaderOp = ShaderOpSet->GetShaderOp(pName);
  1982. }
  1983. if (pShaderOp == nullptr) {
  1984. std::string msg = "Unable to find shader op ";
  1985. msg += pName;
  1986. msg += "; available ops";
  1987. const char sep = ':';
  1988. for (auto &pAvailOp : ShaderOpSet->ShaderOps) {
  1989. msg += sep;
  1990. msg += pAvailOp->Name ? pAvailOp->Name : "[n/a]";
  1991. }
  1992. CA2W msgWide(msg.c_str());
  1993. VERIFY_FAIL(msgWide.m_psz);
  1994. }
  1995. // This won't actually be used since we're supplying the device,
  1996. // but let's make it consistent.
  1997. pShaderOp->UseWarpDevice = GetTestParamUseWARP(true);
  1998. std::shared_ptr<st::ShaderOpTest> test = std::make_shared<st::ShaderOpTest>();
  1999. test->SetDxcSupport(&support);
  2000. test->SetInitCallback(pInitCallback);
  2001. test->SetDevice(pDevice);
  2002. test->RunShaderOp(pShaderOp);
  2003. std::shared_ptr<ShaderOpTestResult> result =
  2004. std::make_shared<ShaderOpTestResult>();
  2005. result->ShaderOpSet = ShaderOpSet;
  2006. result->Test = test;
  2007. result->ShaderOp = pShaderOp;
  2008. return result;
  2009. }
  2010. std::shared_ptr<ShaderOpTestResult>
  2011. RunShaderOpTest(ID3D12Device *pDevice, dxc::DxcDllSupport &support,
  2012. IStream *pStream, LPCSTR pName,
  2013. st::ShaderOpTest::TInitCallbackFn pInitCallback) {
  2014. DXASSERT_NOMSG(pStream != nullptr);
  2015. std::shared_ptr<st::ShaderOpSet> ShaderOpSet =
  2016. std::make_shared<st::ShaderOpSet>();
  2017. st::ParseShaderOpSetFromStream(pStream, ShaderOpSet.get());
  2018. return RunShaderOpTestAfterParse(pDevice, support, pStream, pName, pInitCallback, ShaderOpSet);
  2019. }
  2020. TEST_F(ExecutionTest, OutOfBoundsTest) {
  2021. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2022. CComPtr<IStream> pStream;
  2023. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2024. // Single operation test at the moment.
  2025. CComPtr<ID3D12Device> pDevice;
  2026. if (!CreateDevice(&pDevice))
  2027. return;
  2028. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(pDevice, m_support, pStream, "OOB", nullptr);
  2029. MappedData data;
  2030. // Read back to CPU and examine contents - should get pure red.
  2031. {
  2032. MappedData data;
  2033. test->Test->GetReadBackData("RTarget", &data);
  2034. const uint32_t *pPixels = (uint32_t *)data.data();
  2035. uint32_t first = *pPixels;
  2036. VERIFY_ARE_EQUAL(0xff0000ff, first); // pure red - only first component is read
  2037. }
  2038. }
  2039. TEST_F(ExecutionTest, SaturateTest) {
  2040. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2041. CComPtr<IStream> pStream;
  2042. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2043. // Single operation test at the moment.
  2044. CComPtr<ID3D12Device> pDevice;
  2045. if (!CreateDevice(&pDevice))
  2046. return;
  2047. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(pDevice, m_support, pStream, "Saturate", nullptr);
  2048. MappedData data;
  2049. test->Test->GetReadBackData("U0", &data);
  2050. const float *pValues = (float *)data.data();
  2051. // Everything is zero except for 1.5f and +Inf, which saturate to 1.0f
  2052. const float ExpectedCases[9] = {
  2053. 0.0f, 0.0f, 0.0f, 0.0f, // -inf, -1.5, -denorm, -0
  2054. 0.0f, 0.0f, 1.0f, 1.0f, // 0, denorm, 1.5f, inf
  2055. 0.0f // nan
  2056. };
  2057. for (size_t i = 0; i < _countof(ExpectedCases); ++i) {
  2058. VERIFY_ARE_EQUAL(*pValues, ExpectedCases[i]);
  2059. ++pValues;
  2060. }
  2061. }
  2062. TEST_F(ExecutionTest, BasicTriangleOpTest) {
  2063. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2064. CComPtr<IStream> pStream;
  2065. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2066. // Single operation test at the moment.
  2067. CComPtr<ID3D12Device> pDevice;
  2068. if (!CreateDevice(&pDevice))
  2069. return;
  2070. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(pDevice, m_support, pStream, "Triangle", nullptr);
  2071. MappedData data;
  2072. D3D12_RESOURCE_DESC &D = test->ShaderOp->GetResourceByName("RTarget")->Desc;
  2073. UINT width = (UINT64)D.Width;
  2074. UINT height = (UINT64)D.Height;
  2075. test->Test->GetReadBackData("RTarget", &data);
  2076. const uint32_t *pPixels = (uint32_t *)data.data();
  2077. if (SaveImages()) {
  2078. SavePixelsToFile(pPixels, DXGI_FORMAT_R8G8B8A8_UNORM, 320, 200, L"basic.bmp");
  2079. }
  2080. uint32_t top = pPixels[width / 2]; // Top center.
  2081. uint32_t mid = pPixels[width / 2 + width * (height / 2)]; // Middle center.
  2082. VERIFY_ARE_EQUAL(0xff663300, top); // clear color
  2083. VERIFY_ARE_EQUAL(0xffffffff, mid); // white
  2084. // This is the basic validation test for shader operations, so it's good to
  2085. // check this here at least for this one test case.
  2086. data.reset();
  2087. test.reset();
  2088. ReportLiveObjects();
  2089. }
  2090. // Rendering two right triangles forming a square and assigning a texture value
  2091. // for each pixel to calculate derivates.
  2092. TEST_F(ExecutionTest, PartialDerivTest) {
  2093. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2094. CComPtr<IStream> pStream;
  2095. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2096. CComPtr<ID3D12Device> pDevice;
  2097. if (!CreateDevice(&pDevice))
  2098. return;
  2099. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(pDevice, m_support, pStream, "DerivFine", nullptr);
  2100. MappedData data;
  2101. D3D12_RESOURCE_DESC &D = test->ShaderOp->GetResourceByName("RTarget")->Desc;
  2102. UINT width = (UINT64)D.Width;
  2103. UINT height = (UINT64)D.Height;
  2104. UINT pixelSize = GetByteSizeForFormat(D.Format) / 4;
  2105. test->Test->GetReadBackData("RTarget", &data);
  2106. const float *pPixels = (float *)data.data();
  2107. UINT centerIndex = (UINT64)width * height / 2 - width / 2;
  2108. // pixel at the center
  2109. UINT offsetCenter = centerIndex * pixelSize;
  2110. float CenterDDXFine = pPixels[offsetCenter];
  2111. float CenterDDYFine = pPixels[offsetCenter + 1];
  2112. float CenterDDXCoarse = pPixels[offsetCenter + 2];
  2113. float CenterDDYCoarse = pPixels[offsetCenter + 3];
  2114. LogCommentFmt(
  2115. L"center ddx_fine: %8f, ddy_fine: %8f, ddx_coarse: %8f, ddy_coarse: %8f",
  2116. CenterDDXFine, CenterDDYFine, CenterDDXCoarse, CenterDDYCoarse);
  2117. // The texture for the 9 pixels in the center should look like the following
  2118. // 256 32 64
  2119. // 2048 256 512
  2120. // 1 .125 .25
  2121. // In D3D12 there is no guarantee of how the adapter is grouping 2x2 pixels
  2122. // So for fine derivatives there can be up to two possible results for the center pixel,
  2123. // while for coarse derivatives there can be up to six possible results.
  2124. int ulpTolerance = 1;
  2125. // 512 - 256 or 2048 - 256
  2126. bool left = CompareFloatULP(CenterDDXFine, -1792.0f, ulpTolerance);
  2127. VERIFY_IS_TRUE(left || CompareFloatULP(CenterDDXFine, 256.0f, ulpTolerance));
  2128. // 256 - 32 or 256 - .125
  2129. bool top = CompareFloatULP(CenterDDYFine, 224.0f, ulpTolerance);
  2130. VERIFY_IS_TRUE(top || CompareFloatULP(CenterDDYFine, -255.875, ulpTolerance));
  2131. if (top && left) {
  2132. VERIFY_IS_TRUE((CompareFloatULP(CenterDDXCoarse, -224.0f, ulpTolerance) ||
  2133. CompareFloatULP(CenterDDXCoarse, -1792.0f, ulpTolerance)) &&
  2134. (CompareFloatULP(CenterDDYCoarse, 224.0f, ulpTolerance) ||
  2135. CompareFloatULP(CenterDDYCoarse, 1792.0f, ulpTolerance)));
  2136. }
  2137. else if (top) { // top right quad
  2138. VERIFY_IS_TRUE((CompareFloatULP(CenterDDXCoarse, 256.0f, ulpTolerance) ||
  2139. CompareFloatULP(CenterDDXCoarse, 32.0f, ulpTolerance)) &&
  2140. (CompareFloatULP(CenterDDYCoarse, 224.0f, ulpTolerance) ||
  2141. CompareFloatULP(CenterDDYCoarse, 448.0f, ulpTolerance)));
  2142. }
  2143. else if (left) { // bottom left quad
  2144. VERIFY_IS_TRUE((CompareFloatULP(CenterDDXCoarse, -1792.0f, ulpTolerance) ||
  2145. CompareFloatULP(CenterDDXCoarse, -.875f, ulpTolerance)) &&
  2146. (CompareFloatULP(CenterDDYCoarse, -2047.0f, ulpTolerance) ||
  2147. CompareFloatULP(CenterDDYCoarse, -255.875f, ulpTolerance)));
  2148. }
  2149. else { // bottom right
  2150. VERIFY_IS_TRUE((CompareFloatULP(CenterDDXCoarse, 256.0f, ulpTolerance) ||
  2151. CompareFloatULP(CenterDDXCoarse, .125f, ulpTolerance)) &&
  2152. (CompareFloatULP(CenterDDYCoarse, -255.875f, ulpTolerance) ||
  2153. CompareFloatULP(CenterDDYCoarse, -511.75f, ulpTolerance)));
  2154. }
  2155. }
  2156. // Resource structure for data-driven tests.
  2157. struct SUnaryFPOp {
  2158. float input;
  2159. float output;
  2160. };
  2161. struct SBinaryFPOp {
  2162. float input1;
  2163. float input2;
  2164. float output1;
  2165. float output2;
  2166. };
  2167. struct STertiaryFPOp {
  2168. float input1;
  2169. float input2;
  2170. float input3;
  2171. float output;
  2172. };
  2173. struct SUnaryIntOp {
  2174. int input;
  2175. int output;
  2176. };
  2177. struct SUnaryUintOp {
  2178. unsigned int input;
  2179. unsigned int output;
  2180. };
  2181. struct SBinaryIntOp {
  2182. int input1;
  2183. int input2;
  2184. int output1;
  2185. int output2;
  2186. };
  2187. struct STertiaryIntOp {
  2188. int input1;
  2189. int input2;
  2190. int input3;
  2191. int output;
  2192. };
  2193. struct SBinaryUintOp {
  2194. unsigned int input1;
  2195. unsigned int input2;
  2196. unsigned int output1;
  2197. unsigned int output2;
  2198. };
  2199. struct STertiaryUintOp {
  2200. unsigned int input1;
  2201. unsigned int input2;
  2202. unsigned int input3;
  2203. unsigned int output;
  2204. };
  2205. // representation for HLSL float vectors
  2206. struct SDotOp {
  2207. XMFLOAT4 input1;
  2208. XMFLOAT4 input2;
  2209. float o_dot2;
  2210. float o_dot3;
  2211. float o_dot4;
  2212. };
  2213. struct SMsad4 {
  2214. unsigned int ref;
  2215. XMUINT2 src;
  2216. XMUINT4 accum;
  2217. XMUINT4 result;
  2218. };
  2219. // Parameter representation for taef data-driven tests
  2220. struct TableParameter {
  2221. LPCWSTR m_name;
  2222. enum TableParameterType {
  2223. INT,
  2224. UINT,
  2225. DOUBLE,
  2226. STRING,
  2227. BOOL,
  2228. INT_TABLE,
  2229. DOUBLE_TABLE,
  2230. STRING_TABLE,
  2231. UINT_TABLE,
  2232. BOOL_TABLE
  2233. };
  2234. TableParameterType m_type;
  2235. bool m_required = true; // required parameter
  2236. int m_int = 0;
  2237. unsigned int m_uint = 0;
  2238. double m_double = 0;
  2239. bool m_bool = false;
  2240. WEX::Common::String m_str;
  2241. WEX::TestExecution::TestDataArray<int> m_intTable;
  2242. WEX::TestExecution::TestDataArray<unsigned int> m_uintTable;
  2243. WEX::TestExecution::TestDataArray<double> m_doubleTable;
  2244. WEX::TestExecution::TestDataArray<bool> m_boolTable;
  2245. WEX::TestExecution::TestDataArray<WEX::Common::String> m_StringTable;
  2246. };
  2247. class TableParameterHandler {
  2248. public:
  2249. TableParameter* m_table;
  2250. size_t m_tableSize;
  2251. TableParameterHandler(TableParameter *pTable, size_t size) : m_table(pTable), m_tableSize(size) {}
  2252. TableParameter* GetTableParamByName(LPCWSTR name) {
  2253. for (size_t i = 0; i < m_tableSize; ++i) {
  2254. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2255. return &m_table[i];
  2256. }
  2257. }
  2258. DXASSERT(false, "Invalid Table Parameter Name %s", name);
  2259. return nullptr;
  2260. }
  2261. void clearTableParameter() {
  2262. for (size_t i = 0; i < m_tableSize; ++i) {
  2263. m_table[i].m_int = 0;
  2264. m_table[i].m_uint = 0;
  2265. m_table[i].m_double = 0;
  2266. m_table[i].m_bool = false;
  2267. m_table[i].m_str = WEX::Common::String();
  2268. }
  2269. }
  2270. template <class T1>
  2271. WEX::TestExecution::TestDataArray<T1> *GetDataArray(LPCWSTR name) {
  2272. return nullptr;
  2273. }
  2274. template <>
  2275. WEX::TestExecution::TestDataArray<int> *GetDataArray(LPCWSTR name) {
  2276. for (size_t i = 0; i < m_tableSize; ++i) {
  2277. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2278. return &(m_table[i].m_intTable);
  2279. }
  2280. }
  2281. DXASSERT(false, "Invalid Table Parameter Name %s", name);
  2282. return nullptr;
  2283. }
  2284. template <>
  2285. WEX::TestExecution::TestDataArray<unsigned int> *GetDataArray(LPCWSTR name) {
  2286. for (size_t i = 0; i < m_tableSize; ++i) {
  2287. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2288. return &(m_table[i].m_uintTable);
  2289. }
  2290. }
  2291. DXASSERT(false, "Invalid Table Parameter Name %s", name);
  2292. return nullptr;
  2293. }
  2294. template <>
  2295. WEX::TestExecution::TestDataArray<double> *GetDataArray(LPCWSTR name) {
  2296. for (size_t i = 0; i < m_tableSize; ++i) {
  2297. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2298. return &(m_table[i].m_doubleTable);
  2299. }
  2300. }
  2301. DXASSERT(false, "Invalid Table Parameter Name %s", name);
  2302. return nullptr;
  2303. }
  2304. template <>
  2305. WEX::TestExecution::TestDataArray<bool> *GetDataArray(LPCWSTR name) {
  2306. for (size_t i = 0; i < m_tableSize; ++i) {
  2307. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2308. return &(m_table[i].m_boolTable);
  2309. }
  2310. }
  2311. DXASSERT(false, "Invalid Table Parameter Name %s", name);
  2312. return nullptr;
  2313. }
  2314. };
  2315. static TableParameter UnaryFPOpParameters[] = {
  2316. { L"ShaderOp.Name", TableParameter::STRING, true },
  2317. { L"ShaderOp.Target", TableParameter::STRING, true },
  2318. { L"ShaderOp.EntryPoint", TableParameter::STRING, true },
  2319. { L"ShaderOp.Text", TableParameter::STRING, true },
  2320. { L"Validation.Input", TableParameter::STRING_TABLE, true },
  2321. { L"Validation.Expected", TableParameter::STRING_TABLE, true },
  2322. { L"Validation.Type", TableParameter::STRING, true },
  2323. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2324. { L"Validation.NumInput", TableParameter::UINT, true },
  2325. { L"Warp.Version", TableParameter::UINT, false }
  2326. };
  2327. static TableParameter BinaryFPOpParameters[] = {
  2328. { L"ShaderOp.Name", TableParameter::STRING, true },
  2329. { L"ShaderOp.Target", TableParameter::STRING, true },
  2330. { L"ShaderOp.EntryPoint", TableParameter::STRING, true },
  2331. { L"ShaderOp.Text", TableParameter::STRING, true },
  2332. { L"Validation.Input1", TableParameter::STRING_TABLE, true },
  2333. { L"Validation.Input2", TableParameter::STRING_TABLE, true },
  2334. { L"Validation.Expected1", TableParameter::STRING_TABLE, true },
  2335. { L"Validation.Expected2", TableParameter::STRING_TABLE, true },
  2336. { L"Validation.Type", TableParameter::STRING, true },
  2337. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2338. { L"Validation.NumInput", TableParameter::UINT, true }
  2339. };
  2340. static TableParameter TertiaryFPOpParameters[] = {
  2341. { L"ShaderOp.Name", TableParameter::STRING, true },
  2342. { L"ShaderOp.Target", TableParameter::STRING, true },
  2343. { L"ShaderOp.EntryPoint", TableParameter::STRING, true },
  2344. { L"ShaderOp.Text", TableParameter::STRING, true },
  2345. { L"Validation.Input1", TableParameter::STRING_TABLE, true },
  2346. { L"Validation.Input2", TableParameter::STRING_TABLE, true },
  2347. { L"Validation.Input3", TableParameter::STRING_TABLE, true },
  2348. { L"Validation.Expected", TableParameter::STRING_TABLE, true },
  2349. { L"Validation.Type", TableParameter::STRING, true },
  2350. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2351. { L"Validation.NumInput", TableParameter::UINT, true }
  2352. };
  2353. static TableParameter UnaryIntOpParameters[] = {
  2354. { L"ShaderOp.Name", TableParameter::STRING, true },
  2355. { L"ShaderOp.Target", TableParameter::STRING, true },
  2356. { L"ShaderOp.EntryPoint", TableParameter::STRING, true },
  2357. { L"ShaderOp.Text", TableParameter::STRING, true },
  2358. { L"Validation.Input", TableParameter::INT_TABLE, true },
  2359. { L"Validation.Expected", TableParameter::INT_TABLE, true },
  2360. { L"Validation.Tolerance", TableParameter::INT, true },
  2361. { L"Validation.NumInput", TableParameter::UINT, true }
  2362. };
  2363. static TableParameter UnaryUintOpParameters[] = {
  2364. { L"ShaderOp.Name", TableParameter::STRING, true },
  2365. { L"ShaderOp.Target", TableParameter::STRING, true },
  2366. { L"ShaderOp.EntryPoint", TableParameter::STRING, true },
  2367. { L"ShaderOp.Text", TableParameter::STRING, true },
  2368. { L"Validation.Input", TableParameter::UINT_TABLE, true },
  2369. { L"Validation.Expected", TableParameter::UINT_TABLE, true },
  2370. { L"Validation.Tolerance", TableParameter::INT, true },
  2371. { L"Validation.NumInput", TableParameter::UINT, true }
  2372. };
  2373. static TableParameter BinaryIntOpParameters[] = {
  2374. { L"ShaderOp.Name", TableParameter::STRING, true },
  2375. { L"ShaderOp.Target", TableParameter::STRING, true },
  2376. { L"ShaderOp.EntryPoint", TableParameter::STRING, true },
  2377. { L"ShaderOp.Text", TableParameter::STRING, true },
  2378. { L"Validation.Input1", TableParameter::INT_TABLE, true },
  2379. { L"Validation.Input2", TableParameter::INT_TABLE, true },
  2380. { L"Validation.Expected1", TableParameter::INT_TABLE, true },
  2381. { L"Validation.Expected2", TableParameter::INT_TABLE, false },
  2382. { L"Validation.Tolerance", TableParameter::INT, true },
  2383. { L"Validation.NumInput", TableParameter::UINT, true },
  2384. { L"Validation.NumExpected", TableParameter::INT, true }
  2385. };
  2386. static TableParameter TertiaryIntOpParameters[] = {
  2387. { L"ShaderOp.Name", TableParameter::STRING, true },
  2388. { L"ShaderOp.Target", TableParameter::STRING, true },
  2389. { L"ShaderOp.EntryPoint", TableParameter::STRING, true },
  2390. { L"ShaderOp.Text", TableParameter::STRING, true },
  2391. { L"Validation.Input1", TableParameter::INT_TABLE, true },
  2392. { L"Validation.Input2", TableParameter::INT_TABLE, true },
  2393. { L"Validation.Input3", TableParameter::INT_TABLE, true },
  2394. { L"Validation.Expected", TableParameter::INT_TABLE, true },
  2395. { L"Validation.Tolerance", TableParameter::INT, true },
  2396. { L"Validation.NumInput", TableParameter::UINT, true }
  2397. };
  2398. static TableParameter BinaryUintOpParameters[] = {
  2399. { L"ShaderOp.Name", TableParameter::STRING, true },
  2400. { L"ShaderOp.Target", TableParameter::STRING, true },
  2401. { L"ShaderOp.EntryPoint", TableParameter::STRING, true },
  2402. { L"ShaderOp.Text", TableParameter::STRING, true },
  2403. { L"Validation.Input1", TableParameter::UINT_TABLE, true },
  2404. { L"Validation.Input2", TableParameter::UINT_TABLE, true },
  2405. { L"Validation.Expected1", TableParameter::UINT_TABLE, true },
  2406. { L"Validation.Expected2", TableParameter::UINT_TABLE, false },
  2407. { L"Validation.Tolerance", TableParameter::INT, true },
  2408. { L"Validation.NumInput", TableParameter::UINT, true },
  2409. { L"Validation.NumExpected", TableParameter::INT, true },
  2410. };
  2411. static TableParameter TertiaryUintOpParameters[] = {
  2412. { L"ShaderOp.Name", TableParameter::STRING, true },
  2413. { L"ShaderOp.Target", TableParameter::STRING, true },
  2414. { L"ShaderOp.EntryPoint", TableParameter::STRING, true },
  2415. { L"ShaderOp.Text", TableParameter::STRING, true },
  2416. { L"Validation.Input1", TableParameter::UINT_TABLE, true },
  2417. { L"Validation.Input2", TableParameter::UINT_TABLE, true },
  2418. { L"Validation.Input3", TableParameter::UINT_TABLE, true },
  2419. { L"Validation.Expected", TableParameter::UINT_TABLE, true },
  2420. { L"Validation.Tolerance", TableParameter::INT, true },
  2421. { L"Validation.NumInput", TableParameter::UINT, true }
  2422. };
  2423. static TableParameter DotOpParameters[] = {
  2424. { L"ShaderOp.Name", TableParameter::STRING, true },
  2425. { L"ShaderOp.Target", TableParameter::STRING, true },
  2426. { L"ShaderOp.EntryPoint", TableParameter::STRING, true },
  2427. { L"ShaderOp.Text", TableParameter::STRING, true },
  2428. { L"Validation.Input1", TableParameter::STRING_TABLE, true },
  2429. { L"Validation.Input2", TableParameter::STRING_TABLE, true },
  2430. { L"Validation.dot2", TableParameter::STRING_TABLE, true },
  2431. { L"Validation.dot3", TableParameter::STRING_TABLE, true },
  2432. { L"Validation.dot4", TableParameter::STRING_TABLE, true },
  2433. { L"Validation.Type", TableParameter::STRING, true },
  2434. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2435. { L"Validation.NumInput", TableParameter::UINT, true }
  2436. };
  2437. static TableParameter Msad4OpParameters[] = {
  2438. { L"ShaderOp.Text", TableParameter::STRING, true },
  2439. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2440. { L"Validation.NumInput", TableParameter::UINT, true },
  2441. { L"Validation.Reference", TableParameter::UINT_TABLE, true},
  2442. { L"Validation.Source", TableParameter::STRING_TABLE, true },
  2443. { L"Validation.Accum", TableParameter::STRING_TABLE, true },
  2444. { L"Validation.Expected", TableParameter::STRING_TABLE, true }
  2445. };
  2446. static TableParameter WaveIntrinsicsActiveIntParameters[] = {
  2447. { L"ShaderOp.Name", TableParameter::STRING, true },
  2448. { L"ShaderOp.Text", TableParameter::STRING, true },
  2449. { L"Validation.NumInputSet", TableParameter::UINT, true },
  2450. { L"Validation.InputSet1", TableParameter::INT_TABLE, true },
  2451. { L"Validation.InputSet2", TableParameter::INT_TABLE, false },
  2452. { L"Validation.InputSet3", TableParameter::INT_TABLE, false },
  2453. { L"Validation.InputSet4", TableParameter::INT_TABLE, false }
  2454. };
  2455. static TableParameter WaveIntrinsicsPrefixIntParameters[] = {
  2456. { L"ShaderOp.Name", TableParameter::STRING, true },
  2457. { L"ShaderOp.Text", TableParameter::STRING, true },
  2458. { L"Validation.NumInputSet", TableParameter::UINT, true },
  2459. { L"Validation.InputSet1", TableParameter::INT_TABLE, true },
  2460. { L"Validation.InputSet2", TableParameter::INT_TABLE, false },
  2461. { L"Validation.InputSet3", TableParameter::INT_TABLE, false },
  2462. { L"Validation.InputSet4", TableParameter::INT_TABLE, false }
  2463. };
  2464. static TableParameter WaveIntrinsicsActiveUintParameters[] = {
  2465. { L"ShaderOp.Name", TableParameter::STRING, true },
  2466. { L"ShaderOp.Text", TableParameter::STRING, true },
  2467. { L"Validation.NumInputSet", TableParameter::UINT, true },
  2468. { L"Validation.InputSet1", TableParameter::UINT_TABLE, true },
  2469. { L"Validation.InputSet2", TableParameter::UINT_TABLE, false },
  2470. { L"Validation.InputSet3", TableParameter::UINT_TABLE, false },
  2471. { L"Validation.InputSet4", TableParameter::UINT_TABLE, false }
  2472. };
  2473. static TableParameter WaveIntrinsicsPrefixUintParameters[] = {
  2474. { L"ShaderOp.Name", TableParameter::STRING, true },
  2475. { L"ShaderOp.Text", TableParameter::STRING, true },
  2476. { L"Validation.NumInputSet", TableParameter::UINT, true },
  2477. { L"Validation.InputSet1", TableParameter::UINT_TABLE, true },
  2478. { L"Validation.InputSet2", TableParameter::UINT_TABLE, false },
  2479. { L"Validation.InputSet3", TableParameter::UINT_TABLE, false },
  2480. { L"Validation.InputSet4", TableParameter::UINT_TABLE, false }
  2481. };
  2482. static TableParameter WaveIntrinsicsActiveBoolParameters[] = {
  2483. { L"ShaderOp.Name", TableParameter::STRING, true },
  2484. { L"ShaderOp.Text", TableParameter::STRING, true },
  2485. { L"Validation.NumInputSet", TableParameter::UINT, true },
  2486. { L"Validation.InputSet1", TableParameter::BOOL_TABLE, true },
  2487. { L"Validation.InputSet2", TableParameter::BOOL_TABLE, false },
  2488. { L"Validation.InputSet3", TableParameter::BOOL_TABLE, false },
  2489. };
  2490. static HRESULT ParseDataToFloat(PCWSTR str, float &value) {
  2491. std::wstring wString(str);
  2492. wString.erase(std::remove(wString.begin(), wString.end(), L' '), wString.end());
  2493. PCWSTR wstr = wString.data();
  2494. if (_wcsicmp(wstr, L"NaN") == 0) {
  2495. value = NAN;
  2496. } else if (_wcsicmp(wstr, L"-inf") == 0) {
  2497. value = -(INFINITY);
  2498. } else if (_wcsicmp(wstr, L"inf") == 0) {
  2499. value = INFINITY;
  2500. } else if (_wcsicmp(wstr, L"-denorm") == 0) {
  2501. value = -(FLT_MIN / 2);
  2502. } else if (_wcsicmp(wstr, L"denorm") == 0) {
  2503. value = FLT_MIN / 2;
  2504. } else if (_wcsicmp(wstr, L"-0.0f") == 0 || _wcsicmp(wstr, L"-0.0") == 0 ||
  2505. _wcsicmp(wstr, L"-0") == 0) {
  2506. value = -0.0f;
  2507. } else if (_wcsicmp(wstr, L"0.0f") == 0 || _wcsicmp(wstr, L"0.0") == 0 ||
  2508. _wcsicmp(wstr, L"0") == 0) {
  2509. value = 0.0f;
  2510. } else {
  2511. // evaluate the expression of wstring
  2512. double val = _wtof(wstr);
  2513. if (val == 0) {
  2514. LogErrorFmt(L"Failed to parse parameter %s to float", wstr);
  2515. return E_FAIL;
  2516. }
  2517. value = val;
  2518. }
  2519. return S_OK;
  2520. }
  2521. static HRESULT ParseDataToInt(PCWSTR str, int &value) {
  2522. std::wstring wString(str);
  2523. wString.erase(std::remove(wString.begin(), wString.end(), L' '), wString.end());
  2524. PCWSTR wstr = wString.data();
  2525. // evaluate the expression of string
  2526. if (_wcsicmp(wstr, L"0.0") == 0 || _wcsicmp(wstr, L"0") == 0) {
  2527. value = 0;
  2528. return S_OK;
  2529. }
  2530. int val = _wtoi(wstr);
  2531. if (val == 0) {
  2532. LogErrorFmt(L"Failed to parse parameter %s to int", wstr);
  2533. return E_FAIL;
  2534. }
  2535. value = val;
  2536. return S_OK;
  2537. }
  2538. static HRESULT ParseDataToUint(PCWSTR str, unsigned int &value) {
  2539. std::wstring wString(str);
  2540. wString.erase(std::remove(wString.begin(), wString.end(), L' '), wString.end());
  2541. PCWSTR wstr = wString.data();
  2542. // evaluate the expression of string
  2543. if (_wcsicmp(wstr, L"0") == 0 || _wcsicmp(wstr, L"0x00000000") == 0) {
  2544. value = 0;
  2545. return S_OK;
  2546. }
  2547. wchar_t *end;
  2548. unsigned int val = std::wcstoul(wstr, &end, 0);
  2549. if (val == 0) {
  2550. LogErrorFmt(L"Failed to parse parameter %s to int", wstr);
  2551. return E_FAIL;
  2552. }
  2553. value = val;
  2554. return S_OK;
  2555. }
  2556. static HRESULT ParseDataToVectorFloat(PCWSTR str, float *ptr, size_t count) {
  2557. std::wstring wstr(str);
  2558. size_t curPosition = 0;
  2559. // parse a string of dot product separated by commas
  2560. for (size_t i = 0; i < count; ++i) {
  2561. size_t nextPosition = wstr.find(L",", curPosition);
  2562. if (FAILED(ParseDataToFloat(
  2563. wstr.substr(curPosition, nextPosition - curPosition).data(),
  2564. *(ptr + i)))) {
  2565. return E_FAIL;
  2566. }
  2567. curPosition = nextPosition + 1;
  2568. }
  2569. return S_OK;
  2570. }
  2571. static HRESULT ParseDataToVectorUint(PCWSTR str, unsigned int *ptr, size_t count) {
  2572. std::wstring wstr(str);
  2573. size_t curPosition = 0;
  2574. // parse a string of dot product separated by commas
  2575. for (size_t i = 0; i < count; ++i) {
  2576. size_t nextPosition = wstr.find(L",", curPosition);
  2577. if (FAILED(ParseDataToUint(
  2578. wstr.substr(curPosition, nextPosition - curPosition).data(),
  2579. *(ptr + i)))) {
  2580. return E_FAIL;
  2581. }
  2582. curPosition = nextPosition + 1;
  2583. }
  2584. return S_OK;
  2585. }
  2586. static HRESULT ParseTableRow(TableParameter *table, unsigned int size) {
  2587. for (unsigned int i = 0; i < size; ++i) {
  2588. switch (table[i].m_type) {
  2589. case TableParameter::INT:
  2590. if (FAILED(WEX::TestExecution::TestData::TryGetValue(table[i].m_name,
  2591. table[i].m_int)) && table[i].m_required) {
  2592. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2593. return E_FAIL;
  2594. }
  2595. break;
  2596. case TableParameter::UINT:
  2597. if (FAILED(WEX::TestExecution::TestData::TryGetValue(table[i].m_name,
  2598. table[i].m_uint)) && table[i].m_required) {
  2599. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2600. return E_FAIL;
  2601. }
  2602. break;
  2603. case TableParameter::DOUBLE:
  2604. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2605. table[i].m_name, table[i].m_double)) && table[i].m_required) {
  2606. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2607. return E_FAIL;
  2608. }
  2609. break;
  2610. case TableParameter::STRING:
  2611. if (FAILED(WEX::TestExecution::TestData::TryGetValue(table[i].m_name,
  2612. table[i].m_str)) && table[i].m_required) {
  2613. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2614. return E_FAIL;
  2615. }
  2616. break;
  2617. case TableParameter::BOOL:
  2618. if (FAILED(WEX::TestExecution::TestData::TryGetValue(table[i].m_name,
  2619. table[i].m_str)) && table[i].m_bool) {
  2620. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2621. return E_FAIL;
  2622. }
  2623. break;
  2624. case TableParameter::INT_TABLE:
  2625. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2626. table[i].m_name, table[i].m_intTable)) && table[i].m_required) {
  2627. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2628. return E_FAIL;
  2629. }
  2630. break;
  2631. case TableParameter::UINT_TABLE:
  2632. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2633. table[i].m_name, table[i].m_uintTable)) && table[i].m_required) {
  2634. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2635. return E_FAIL;
  2636. }
  2637. break;
  2638. case TableParameter::DOUBLE_TABLE:
  2639. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2640. table[i].m_name, table[i].m_doubleTable)) && table[i].m_required) {
  2641. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2642. return E_FAIL;
  2643. }
  2644. break;
  2645. case TableParameter::BOOL_TABLE:
  2646. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2647. table[i].m_name, table[i].m_boolTable)) && table[i].m_required) {
  2648. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2649. return E_FAIL;
  2650. }
  2651. break;
  2652. case TableParameter::STRING_TABLE:
  2653. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2654. table[i].m_name, table[i].m_StringTable)) && table[i].m_required) {
  2655. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2656. return E_FAIL;
  2657. }
  2658. break;
  2659. default:
  2660. DXASSERT_NOMSG("Invalid Parameter Type");
  2661. }
  2662. }
  2663. return S_OK;
  2664. }
  2665. static void VerifyOutputWithExpectedValueInt(int output, int ref, int tolerance) {
  2666. VERIFY_IS_TRUE(output - ref <= tolerance && ref - output <= tolerance);
  2667. }
  2668. static void VerifyOutputWithExpectedValueFloat(float output, float ref, LPCWSTR type, double tolerance) {
  2669. if (_wcsicmp(type, L"Relative") == 0) {
  2670. VERIFY_IS_TRUE(CompareFloatRelativeEpsilon(output, ref, tolerance));
  2671. }
  2672. else if (_wcsicmp(type, L"Epsilon") == 0) {
  2673. VERIFY_IS_TRUE(CompareFloatEpsilon(output, ref, tolerance));
  2674. }
  2675. else if (_wcsicmp(type, L"ULP") == 0) {
  2676. VERIFY_IS_TRUE(CompareFloatULP(output, ref, (int)tolerance));
  2677. }
  2678. else {
  2679. LogErrorFmt(L"Failed to read comparison type %S", type);
  2680. }
  2681. }
  2682. TEST_F(ExecutionTest, UnaryFloatOpTest) {
  2683. WEX::TestExecution::SetVerifyOutput verifySettings(
  2684. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2685. CComPtr<IStream> pStream;
  2686. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2687. CComPtr<ID3D12Device> pDevice;
  2688. if (!CreateDevice(&pDevice)) {
  2689. return;
  2690. }
  2691. // Read data from the table
  2692. int tableSize = sizeof(UnaryFPOpParameters) / sizeof(TableParameter);
  2693. TableParameterHandler handler(UnaryFPOpParameters, tableSize);
  2694. handler.clearTableParameter();
  2695. VERIFY_SUCCEEDED(ParseTableRow(UnaryFPOpParameters, tableSize));
  2696. st::ShaderOpShader shader;
  2697. CW2A Name(handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  2698. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  2699. CW2A EntryPoint(handler.GetTableParamByName(L"ShaderOp.EntryPoint")->m_str);
  2700. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  2701. shader.Name = Name.m_psz;
  2702. shader.Target = Target.m_psz;
  2703. shader.EntryPoint = EntryPoint.m_psz;
  2704. shader.Text = Text.m_psz;
  2705. unsigned int WarpVersion = handler.GetTableParamByName(L"Warp.Version")->m_uint;
  2706. if (GetTestParamUseWARP(true) && !IsValidWarpDllVersion(WarpVersion)) {
  2707. return;
  2708. }
  2709. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Input =
  2710. &(handler.GetTableParamByName(L"Validation.Input")->m_StringTable);
  2711. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Expected =
  2712. &(handler.GetTableParamByName(L"Validation.Expected")->m_StringTable);
  2713. LPCWSTR Validation_Type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  2714. double Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  2715. size_t count = handler.GetTableParamByName(L"Validation.NumInput")->m_uint;
  2716. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  2717. pDevice, m_support, pStream, "UnaryFPOp",
  2718. // this callbacked is called when the test
  2719. // is creating the resource to run the test
  2720. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  2721. VERIFY_IS_TRUE(0 == _stricmp(Name, "SUnaryFPOp"));
  2722. size_t size = sizeof(SUnaryFPOp) * count;
  2723. Data.resize(size);
  2724. SUnaryFPOp *pPrimitives = (SUnaryFPOp *)Data.data();
  2725. for (size_t i = 0; i < count; ++i) {
  2726. SUnaryFPOp *p = &pPrimitives[i];
  2727. PCWSTR str = (*Validation_Input)[i % Validation_Input->GetSize()];
  2728. float val;
  2729. VERIFY_SUCCEEDED(ParseDataToFloat(str, val));
  2730. p->input = val;
  2731. }
  2732. // use shader from data table
  2733. pShaderOp->Shaders.at(0).Target = shader.Target;
  2734. pShaderOp->Shaders.at(0).EntryPoint = shader.EntryPoint;
  2735. pShaderOp->Shaders.at(0).Text = shader.Text;
  2736. });
  2737. MappedData data;
  2738. test->Test->GetReadBackData("SUnaryFPOp", &data);
  2739. SUnaryFPOp *pPrimitives = (SUnaryFPOp*)data.data();
  2740. WEX::TestExecution::DisableVerifyExceptions dve;
  2741. for (unsigned i = 0; i < count; ++i) {
  2742. SUnaryFPOp *p = &pPrimitives[i];
  2743. LPCWSTR str = (*Validation_Expected)[i % Validation_Expected->GetSize()];
  2744. float val;
  2745. VERIFY_SUCCEEDED(ParseDataToFloat(str, val));
  2746. LogCommentFmt(
  2747. L"element #%u, input = %10f, output = %10f, expected = %10f", i,
  2748. p->input, p->output, val);
  2749. VerifyOutputWithExpectedValueFloat(p->output, val, Validation_Type, Validation_Tolerance);
  2750. }
  2751. }
  2752. TEST_F(ExecutionTest, BinaryFloatOpTest) {
  2753. WEX::TestExecution::SetVerifyOutput verifySettings(
  2754. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2755. CComPtr<IStream> pStream;
  2756. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2757. CComPtr<ID3D12Device> pDevice;
  2758. if (!CreateDevice(&pDevice)) {
  2759. return;
  2760. }
  2761. // Read data from the table
  2762. int tableSize = sizeof(BinaryFPOpParameters) / sizeof(TableParameter);
  2763. TableParameterHandler handler(BinaryFPOpParameters, tableSize);
  2764. handler.clearTableParameter();
  2765. VERIFY_SUCCEEDED(ParseTableRow(BinaryFPOpParameters, tableSize));
  2766. st::ShaderOpShader shader;
  2767. CW2A Name(handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  2768. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  2769. CW2A EntryPoint(handler.GetTableParamByName(L"ShaderOp.EntryPoint")->m_str);
  2770. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  2771. shader.Name = Name.m_psz;
  2772. shader.Target = Target.m_psz;
  2773. shader.EntryPoint = EntryPoint.m_psz;
  2774. shader.Text = Text.m_psz;
  2775. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Input1 =
  2776. &(handler.GetTableParamByName(L"Validation.Input1")->m_StringTable);
  2777. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Input2 =
  2778. &(handler.GetTableParamByName(L"Validation.Input2")->m_StringTable);
  2779. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Expected1 =
  2780. &(handler.GetTableParamByName(L"Validation.Expected1")->m_StringTable);
  2781. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Expected2 =
  2782. &(handler.GetTableParamByName(L"Validation.Expected2")->m_StringTable);
  2783. LPCWSTR Validation_Type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  2784. double Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  2785. size_t count = handler.GetTableParamByName(L"Validation.NumInput")->m_uint;
  2786. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  2787. pDevice, m_support, pStream, "BinaryFPOp",
  2788. // this callbacked is called when the test
  2789. // is creating the resource to run the test
  2790. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  2791. VERIFY_IS_TRUE(0 == _stricmp(Name, "SBinaryFPOp"));
  2792. size_t size = sizeof(SBinaryFPOp) * count;
  2793. Data.resize(size);
  2794. SBinaryFPOp *pPrimitives = (SBinaryFPOp *)Data.data();
  2795. for (size_t i = 0; i < count; ++i) {
  2796. SBinaryFPOp *p = &pPrimitives[i];
  2797. PCWSTR str1 = (*Validation_Input1)[i % Validation_Input1->GetSize()];
  2798. PCWSTR str2 = (*Validation_Input2)[i % Validation_Input2->GetSize()];
  2799. float val1, val2;
  2800. VERIFY_SUCCEEDED(ParseDataToFloat(str1, val1));
  2801. VERIFY_SUCCEEDED(ParseDataToFloat(str2, val2));
  2802. p->input1 = val1;
  2803. p->input2 = val2;
  2804. }
  2805. // use shader from data table
  2806. pShaderOp->Shaders.at(0).Target = shader.Target;
  2807. pShaderOp->Shaders.at(0).EntryPoint = shader.EntryPoint;
  2808. pShaderOp->Shaders.at(0).Text = shader.Text;
  2809. });
  2810. MappedData data;
  2811. test->Test->GetReadBackData("SBinaryFPOp", &data);
  2812. SBinaryFPOp *pPrimitives = (SBinaryFPOp *)data.data();
  2813. WEX::TestExecution::DisableVerifyExceptions dve;
  2814. for (unsigned i = 0; i < count; ++i) {
  2815. SBinaryFPOp *p = &pPrimitives[i];
  2816. LPCWSTR str1 = (*Validation_Expected1)[i % Validation_Expected1->GetSize()];
  2817. LPCWSTR str2 = (*Validation_Expected2)[i % Validation_Expected2->GetSize()];
  2818. float val1, val2;
  2819. VERIFY_SUCCEEDED(ParseDataToFloat(str1, val1));
  2820. VERIFY_SUCCEEDED(ParseDataToFloat(str2, val2));
  2821. LogCommentFmt(L"element #%u, input1 = %10f, input2 = %10f, output1 = "
  2822. L"%10f, expected1 = %10f, output2 = %10f, expected2 = %10f",
  2823. i, p->input1, p->input2, p->output1, val1, p->output2,
  2824. val2);
  2825. VerifyOutputWithExpectedValueFloat(p->output1, val1, Validation_Type,
  2826. Validation_Tolerance);
  2827. VerifyOutputWithExpectedValueFloat(p->output2, val2, Validation_Type,
  2828. Validation_Tolerance);
  2829. }
  2830. }
  2831. TEST_F(ExecutionTest, TertiaryFloatOpTest) {
  2832. WEX::TestExecution::SetVerifyOutput verifySettings(
  2833. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2834. CComPtr<IStream> pStream;
  2835. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2836. CComPtr<ID3D12Device> pDevice;
  2837. if (!CreateDevice(&pDevice)) {
  2838. return;
  2839. }
  2840. // Read data from the table
  2841. int tableSize = sizeof(TertiaryFPOpParameters) / sizeof(TableParameter);
  2842. TableParameterHandler handler(TertiaryFPOpParameters, tableSize);
  2843. handler.clearTableParameter();
  2844. VERIFY_SUCCEEDED(ParseTableRow(TertiaryFPOpParameters, tableSize));
  2845. st::ShaderOpShader shader;
  2846. CW2A Name(handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  2847. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  2848. CW2A EntryPoint(handler.GetTableParamByName(L"ShaderOp.EntryPoint")->m_str);
  2849. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  2850. shader.Name = Name.m_psz;
  2851. shader.Target = Target.m_psz;
  2852. shader.EntryPoint = EntryPoint.m_psz;
  2853. shader.Text = Text.m_psz;
  2854. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Input1 =
  2855. &(handler.GetTableParamByName(L"Validation.Input1")->m_StringTable);
  2856. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Input2 =
  2857. &(handler.GetTableParamByName(L"Validation.Input2")->m_StringTable);
  2858. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Input3 =
  2859. &(handler.GetTableParamByName(L"Validation.Input3")->m_StringTable);
  2860. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Expected =
  2861. &(handler.GetTableParamByName(L"Validation.Expected")->m_StringTable);
  2862. LPCWSTR Validation_Type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  2863. double Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  2864. size_t count = handler.GetTableParamByName(L"Validation.NumInput")->m_uint;
  2865. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  2866. pDevice, m_support, pStream, "TertiaryFPOp",
  2867. // this callbacked is called when the test
  2868. // is creating the resource to run the test
  2869. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  2870. VERIFY_IS_TRUE(0 == _stricmp(Name, "STertiaryFPOp"));
  2871. size_t size = sizeof(STertiaryFPOp) * count;
  2872. Data.resize(size);
  2873. STertiaryFPOp *pPrimitives = (STertiaryFPOp *)Data.data();
  2874. for (size_t i = 0; i < count; ++i) {
  2875. STertiaryFPOp *p = &pPrimitives[i];
  2876. PCWSTR str1 = (*Validation_Input1)[i % Validation_Input1->GetSize()];
  2877. PCWSTR str2 = (*Validation_Input2)[i % Validation_Input2->GetSize()];
  2878. PCWSTR str3 = (*Validation_Input3)[i % Validation_Input3->GetSize()];
  2879. float val1, val2, val3;
  2880. VERIFY_SUCCEEDED(ParseDataToFloat(str1, val1));
  2881. VERIFY_SUCCEEDED(ParseDataToFloat(str2, val2));
  2882. VERIFY_SUCCEEDED(ParseDataToFloat(str3, val3));
  2883. p->input1 = val1;
  2884. p->input2 = val2;
  2885. p->input3 = val3;
  2886. }
  2887. // use shader from data table
  2888. pShaderOp->Shaders.at(0).Target = shader.Target;
  2889. pShaderOp->Shaders.at(0).EntryPoint = shader.EntryPoint;
  2890. pShaderOp->Shaders.at(0).Text = shader.Text;
  2891. });
  2892. MappedData data;
  2893. test->Test->GetReadBackData("STertiaryFPOp", &data);
  2894. STertiaryFPOp *pPrimitives = (STertiaryFPOp *)data.data();
  2895. WEX::TestExecution::DisableVerifyExceptions dve;
  2896. for (unsigned i = 0; i < count; ++i) {
  2897. STertiaryFPOp *p = &pPrimitives[i];
  2898. LPCWSTR str = (*Validation_Expected)[i % Validation_Expected->GetSize()];
  2899. float val;
  2900. VERIFY_SUCCEEDED(ParseDataToFloat(str, val));
  2901. LogCommentFmt(L"element #%u, input1 = %10f, input2 = %10f, input3 = %10f, output1 = "
  2902. L"%10f, expected = %10f",
  2903. i, p->input1, p->input2, p->input3, p->output, val);
  2904. VerifyOutputWithExpectedValueFloat(p->output, val, Validation_Type,
  2905. Validation_Tolerance);
  2906. }
  2907. }
  2908. TEST_F(ExecutionTest, UnaryIntOpTest) {
  2909. WEX::TestExecution::SetVerifyOutput verifySettings(
  2910. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2911. CComPtr<IStream> pStream;
  2912. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2913. CComPtr<ID3D12Device> pDevice;
  2914. if (!CreateDevice(&pDevice)) {
  2915. return;
  2916. }
  2917. // Read data from the table
  2918. int tableSize = sizeof(UnaryIntOpParameters) / sizeof(TableParameter);
  2919. TableParameterHandler handler(UnaryIntOpParameters, tableSize);
  2920. handler.clearTableParameter();
  2921. VERIFY_SUCCEEDED(ParseTableRow(UnaryIntOpParameters, tableSize));
  2922. st::ShaderOpShader shader;
  2923. CW2A Name(handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  2924. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  2925. CW2A EntryPoint(handler.GetTableParamByName(L"ShaderOp.EntryPoint")->m_str);
  2926. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  2927. shader.Name = Name.m_psz;
  2928. shader.Target = Target.m_psz;
  2929. shader.EntryPoint = EntryPoint.m_psz;
  2930. shader.Text = Text.m_psz;
  2931. WEX::TestExecution::TestDataArray<int> *Validation_Input =
  2932. &handler.GetTableParamByName(L"Validation.Input")->m_intTable;
  2933. WEX::TestExecution::TestDataArray<int> *Validation_Expected =
  2934. &handler.GetTableParamByName(L"Validation.Expected")->m_intTable;
  2935. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int;
  2936. size_t count = handler.GetTableParamByName(L"Validation.NumInput")->m_uint;
  2937. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  2938. pDevice, m_support, pStream, "UnaryIntOp",
  2939. // this callbacked is called when the test
  2940. // is creating the resource to run the test
  2941. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  2942. VERIFY_IS_TRUE(0 == _stricmp(Name, "SUnaryIntOp"));
  2943. size_t size = sizeof(SUnaryIntOp) * count;
  2944. Data.resize(size);
  2945. SUnaryIntOp *pPrimitives = (SUnaryIntOp *)Data.data();
  2946. for (size_t i = 0; i < count; ++i) {
  2947. SUnaryIntOp *p = &pPrimitives[i];
  2948. int val = (*Validation_Input)[i % Validation_Input->GetSize()];
  2949. p->input = val;
  2950. }
  2951. // use shader data table
  2952. pShaderOp->Shaders.at(0).Target = shader.Target;
  2953. pShaderOp->Shaders.at(0).EntryPoint = shader.EntryPoint;
  2954. pShaderOp->Shaders.at(0).Text = shader.Text;
  2955. });
  2956. MappedData data;
  2957. test->Test->GetReadBackData("SUnaryIntOp", &data);
  2958. SUnaryIntOp *pPrimitives = (SUnaryIntOp *)data.data();
  2959. WEX::TestExecution::DisableVerifyExceptions dve;
  2960. for (unsigned i = 0; i < count; ++i) {
  2961. SUnaryIntOp *p = &pPrimitives[i];
  2962. int val = (*Validation_Expected)[i % Validation_Expected->GetSize()];
  2963. LogCommentFmt(L"element #%u, input = %11i(0x%08x), output = %11i(0x%08x), "
  2964. L"expected = %11i(0x%08x)",
  2965. i, p->input, p->input, p->output, p->output, val, val);
  2966. VerifyOutputWithExpectedValueInt(p->output, val, Validation_Tolerance);
  2967. }
  2968. }
  2969. TEST_F(ExecutionTest, UnaryUintOpTest) {
  2970. WEX::TestExecution::SetVerifyOutput verifySettings(
  2971. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2972. CComPtr<IStream> pStream;
  2973. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2974. CComPtr<ID3D12Device> pDevice;
  2975. if (!CreateDevice(&pDevice)) {
  2976. return;
  2977. }
  2978. // Read data from the table
  2979. int tableSize = sizeof(UnaryUintOpParameters) / sizeof(TableParameter);
  2980. TableParameterHandler handler(UnaryUintOpParameters, tableSize);
  2981. handler.clearTableParameter();
  2982. VERIFY_SUCCEEDED(ParseTableRow(UnaryUintOpParameters, tableSize));
  2983. st::ShaderOpShader shader;
  2984. CW2A Name(handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  2985. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  2986. CW2A EntryPoint(handler.GetTableParamByName(L"ShaderOp.EntryPoint")->m_str);
  2987. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  2988. shader.Name = Name.m_psz;
  2989. shader.Target = Target.m_psz;
  2990. shader.EntryPoint = EntryPoint.m_psz;
  2991. shader.Text = Text.m_psz;
  2992. WEX::TestExecution::TestDataArray<unsigned int> *Validation_Input =
  2993. &handler.GetTableParamByName(L"Validation.Input")->m_uintTable;
  2994. WEX::TestExecution::TestDataArray<unsigned int> *Validation_Expected =
  2995. &handler.GetTableParamByName(L"Validation.Expected")->m_uintTable;
  2996. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int;
  2997. size_t count = handler.GetTableParamByName(L"Validation.NumInput")->m_uint;
  2998. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  2999. pDevice, m_support, pStream, "UnaryUintOp",
  3000. // this callbacked is called when the test
  3001. // is creating the resource to run the test
  3002. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3003. VERIFY_IS_TRUE(0 == _stricmp(Name, "SUnaryUintOp"));
  3004. size_t size = sizeof(SUnaryUintOp) * count;
  3005. Data.resize(size);
  3006. SUnaryUintOp *pPrimitives = (SUnaryUintOp *)Data.data();
  3007. for (size_t i = 0; i < count; ++i) {
  3008. SUnaryUintOp *p = &pPrimitives[i];
  3009. unsigned int val = (*Validation_Input)[i % Validation_Input->GetSize()];
  3010. p->input = val;
  3011. }
  3012. // use shader data table
  3013. pShaderOp->Shaders.at(0).Target = shader.Target;
  3014. pShaderOp->Shaders.at(0).EntryPoint = shader.EntryPoint;
  3015. pShaderOp->Shaders.at(0).Text = shader.Text;
  3016. });
  3017. MappedData data;
  3018. test->Test->GetReadBackData("SUnaryUintOp", &data);
  3019. SUnaryUintOp *pPrimitives = (SUnaryUintOp *)data.data();
  3020. WEX::TestExecution::DisableVerifyExceptions dve;
  3021. for (unsigned i = 0; i < count; ++i) {
  3022. SUnaryUintOp *p = &pPrimitives[i];
  3023. unsigned int val = (*Validation_Expected)[i % Validation_Expected->GetSize()];
  3024. LogCommentFmt(L"element #%u, input = %11u(0x%08x), output = %11u(0x%08x), "
  3025. L"expected = %11u(0x%08x)",
  3026. i, p->input, p->input, p->output, p->output, val, val);
  3027. VerifyOutputWithExpectedValueInt(p->output, val, Validation_Tolerance);
  3028. }
  3029. }
  3030. TEST_F(ExecutionTest, BinaryIntOpTest) {
  3031. WEX::TestExecution::SetVerifyOutput verifySettings(
  3032. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3033. CComPtr<IStream> pStream;
  3034. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3035. CComPtr<ID3D12Device> pDevice;
  3036. if (!CreateDevice(&pDevice)) {
  3037. return;
  3038. }
  3039. // Read data from the table
  3040. size_t tableSize = sizeof(BinaryIntOpParameters) / sizeof(TableParameter);
  3041. TableParameterHandler handler(BinaryIntOpParameters, tableSize);
  3042. handler.clearTableParameter();
  3043. VERIFY_SUCCEEDED(ParseTableRow(BinaryIntOpParameters,tableSize));
  3044. st::ShaderOpShader shader;
  3045. CW2A Name(handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  3046. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3047. CW2A EntryPoint(handler.GetTableParamByName(L"ShaderOp.EntryPoint")->m_str);
  3048. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3049. shader.Name = Name.m_psz;
  3050. shader.Target = Target.m_psz;
  3051. shader.EntryPoint = EntryPoint.m_psz;
  3052. shader.Text = Text.m_psz;
  3053. int numExpected = handler.GetTableParamByName(L"Validation.NumExpected")->m_int;
  3054. WEX::TestExecution::TestDataArray<int> *Validation_Input1 =
  3055. &handler.GetTableParamByName(L"Validation.Input1")->m_intTable;
  3056. WEX::TestExecution::TestDataArray<int> *Validation_Input2 =
  3057. &handler.GetTableParamByName(L"Validation.Input2")->m_intTable;
  3058. WEX::TestExecution::TestDataArray<int> *Validation_Expected1 =
  3059. &handler.GetTableParamByName(L"Validation.Expected1")->m_intTable;
  3060. WEX::TestExecution::TestDataArray<int> *Validation_Expected2 =
  3061. &handler.GetTableParamByName(L"Validation.Expected2")->m_intTable;
  3062. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int;
  3063. size_t count = handler.GetTableParamByName(L"Validation.NumInput")->m_uint;
  3064. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3065. pDevice, m_support, pStream, "BinaryIntOp",
  3066. // this callbacked is called when the test
  3067. // is creating the resource to run the test
  3068. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3069. VERIFY_IS_TRUE(0 == _stricmp(Name, "SBinaryIntOp"));
  3070. size_t size = sizeof(SBinaryIntOp) * count;
  3071. Data.resize(size);
  3072. SBinaryIntOp *pPrimitives = (SBinaryIntOp *)Data.data();
  3073. for (size_t i = 0; i < count; ++i) {
  3074. SBinaryIntOp *p = &pPrimitives[i];
  3075. int val1 = (*Validation_Input1)[i % Validation_Input1->GetSize()];
  3076. int val2 = (*Validation_Input2)[i % Validation_Input2->GetSize()];
  3077. p->input1 = val1;
  3078. p->input2 = val2;
  3079. }
  3080. // use shader from data table
  3081. pShaderOp->Shaders.at(0).Target = shader.Target;
  3082. pShaderOp->Shaders.at(0).EntryPoint = shader.EntryPoint;
  3083. pShaderOp->Shaders.at(0).Text = shader.Text;
  3084. });
  3085. MappedData data;
  3086. test->Test->GetReadBackData("SBinaryIntOp", &data);
  3087. SBinaryIntOp *pPrimitives = (SBinaryIntOp *)data.data();
  3088. WEX::TestExecution::DisableVerifyExceptions dve;
  3089. if (numExpected == 2) {
  3090. for (unsigned i = 0; i < count; ++i) {
  3091. SBinaryIntOp *p = &pPrimitives[i];
  3092. int val1 = (*Validation_Expected1)[i % Validation_Expected1->GetSize()];
  3093. int val2 = (*Validation_Expected2)[i % Validation_Expected2->GetSize()];
  3094. LogCommentFmt(L"element #%u, input1 = %11i(0x%08x), input2 = "
  3095. L"%11i(0x%08x), output1 = "
  3096. L"%11i(0x%08x), expected1 = %11i(0x%08x), output2 = "
  3097. L"%11i(0x%08x), expected2 = %11i(0x%08x)",
  3098. i, p->input1, p->input1, p->input2, p->input2, p->output1,
  3099. p->output1, val1, val1, p->output2, p->output2, val2,
  3100. val2);
  3101. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  3102. VerifyOutputWithExpectedValueInt(p->output2, val2, Validation_Tolerance);
  3103. }
  3104. }
  3105. else if (numExpected == 1) {
  3106. for (unsigned i = 0; i < count; ++i) {
  3107. SBinaryIntOp *p = &pPrimitives[i];
  3108. int val1 = (*Validation_Expected1)[i % Validation_Expected1->GetSize()];
  3109. LogCommentFmt(L"element #%u, input1 = %11i(0x%08x), input2 = "
  3110. L"%11i(0x%08x), output = "
  3111. L"%11i(0x%08x), expected = %11i(0x%08x)", i,
  3112. p->input1, p->input1, p->input2, p->input2,
  3113. p->output1, p->output1, val1, val1);
  3114. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  3115. }
  3116. }
  3117. else {
  3118. LogErrorFmt(L"Unexpected number of expected values for operation %i", numExpected);
  3119. }
  3120. }
  3121. TEST_F(ExecutionTest, TertiaryIntOpTest) {
  3122. WEX::TestExecution::SetVerifyOutput verifySettings(
  3123. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3124. CComPtr<IStream> pStream;
  3125. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3126. CComPtr<ID3D12Device> pDevice;
  3127. if (!CreateDevice(&pDevice)) {
  3128. return;
  3129. }
  3130. // Read data from the table
  3131. size_t tableSize = sizeof(TertiaryIntOpParameters) / sizeof(TableParameter);
  3132. TableParameterHandler handler(TertiaryIntOpParameters, tableSize);
  3133. handler.clearTableParameter();
  3134. VERIFY_SUCCEEDED(ParseTableRow(TertiaryIntOpParameters, tableSize));
  3135. st::ShaderOpShader shader;
  3136. CW2A Name(handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  3137. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3138. CW2A EntryPoint(handler.GetTableParamByName(L"ShaderOp.EntryPoint")->m_str);
  3139. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3140. shader.Name = Name.m_psz;
  3141. shader.Target = Target.m_psz;
  3142. shader.EntryPoint = EntryPoint.m_psz;
  3143. shader.Text = Text.m_psz;
  3144. WEX::TestExecution::TestDataArray<int> *Validation_Input1 =
  3145. &handler.GetTableParamByName(L"Validation.Input1")->m_intTable;
  3146. WEX::TestExecution::TestDataArray<int> *Validation_Input2 =
  3147. &handler.GetTableParamByName(L"Validation.Input2")->m_intTable;
  3148. WEX::TestExecution::TestDataArray<int> *Validation_Input3 =
  3149. &handler.GetTableParamByName(L"Validation.Input3")->m_intTable;
  3150. WEX::TestExecution::TestDataArray<int> *Validation_Expected =
  3151. &handler.GetTableParamByName(L"Validation.Expected")->m_intTable;
  3152. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int;
  3153. size_t count = handler.GetTableParamByName(L"Validation.NumInput")->m_uint;
  3154. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3155. pDevice, m_support, pStream, "TertiaryIntOp",
  3156. // this callbacked is called when the test
  3157. // is creating the resource to run the test
  3158. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3159. VERIFY_IS_TRUE(0 == _stricmp(Name, "STertiaryIntOp"));
  3160. size_t size = sizeof(STertiaryIntOp) * count;
  3161. Data.resize(size);
  3162. STertiaryIntOp *pPrimitives = (STertiaryIntOp *)Data.data();
  3163. for (size_t i = 0; i < count; ++i) {
  3164. STertiaryIntOp *p = &pPrimitives[i];
  3165. int val1 = (*Validation_Input1)[i % Validation_Input1->GetSize()];
  3166. int val2 = (*Validation_Input2)[i % Validation_Input2->GetSize()];
  3167. int val3 = (*Validation_Input3)[i % Validation_Input3->GetSize()];
  3168. p->input1 = val1;
  3169. p->input2 = val2;
  3170. p->input3 = val3;
  3171. }
  3172. // use shader from data table
  3173. pShaderOp->Shaders.at(0).Target = shader.Target;
  3174. pShaderOp->Shaders.at(0).EntryPoint = shader.EntryPoint;
  3175. pShaderOp->Shaders.at(0).Text = shader.Text;
  3176. });
  3177. MappedData data;
  3178. test->Test->GetReadBackData("STertiaryIntOp", &data);
  3179. STertiaryIntOp *pPrimitives = (STertiaryIntOp *)data.data();
  3180. WEX::TestExecution::DisableVerifyExceptions dve;
  3181. for (unsigned i = 0; i < count; ++i) {
  3182. STertiaryIntOp *p = &pPrimitives[i];
  3183. int val1 = (*Validation_Expected)[i % Validation_Expected->GetSize()];
  3184. LogCommentFmt(L"element #%u, input1 = %11i(0x%08x), input2 = "
  3185. L"%11i(0x%08x), input3= %11i(0x%08x), output = "
  3186. L"%11i(0x%08x), expected = %11i(0x%08x)",
  3187. i, p->input1, p->input1, p->input2, p->input2,
  3188. p->input3, p->input3, p->output, p->output, val1,
  3189. val1);
  3190. VerifyOutputWithExpectedValueInt(p->output, val1, Validation_Tolerance);
  3191. }
  3192. }
  3193. TEST_F(ExecutionTest, BinaryUintOpTest) {
  3194. WEX::TestExecution::SetVerifyOutput verifySettings(
  3195. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3196. CComPtr<IStream> pStream;
  3197. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3198. CComPtr<ID3D12Device> pDevice;
  3199. if (!CreateDevice(&pDevice)) {
  3200. return;
  3201. }
  3202. // Read data from the table
  3203. size_t tableSize = sizeof(BinaryUintOpParameters) / sizeof(TableParameter);
  3204. TableParameterHandler handler(BinaryUintOpParameters, tableSize);
  3205. handler.clearTableParameter();
  3206. VERIFY_SUCCEEDED(ParseTableRow(BinaryUintOpParameters, tableSize));
  3207. st::ShaderOpShader shader;
  3208. CW2A Name(handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  3209. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3210. CW2A EntryPoint(handler.GetTableParamByName(L"ShaderOp.EntryPoint")->m_str);
  3211. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3212. shader.Name = Name.m_psz;
  3213. shader.Target = Target.m_psz;
  3214. shader.EntryPoint = EntryPoint.m_psz;
  3215. shader.Text = Text.m_psz;
  3216. int numExpected = handler.GetTableParamByName(L"Validation.NumExpected")->m_int;
  3217. WEX::TestExecution::TestDataArray<unsigned int> *Validation_Input1 =
  3218. &handler.GetTableParamByName(L"Validation.Input1")->m_uintTable;
  3219. WEX::TestExecution::TestDataArray<unsigned int> *Validation_Input2 =
  3220. &handler.GetTableParamByName(L"Validation.Input2")->m_uintTable;
  3221. WEX::TestExecution::TestDataArray<unsigned int> *Validation_Expected1 =
  3222. &handler.GetTableParamByName(L"Validation.Expected1")->m_uintTable;
  3223. WEX::TestExecution::TestDataArray<unsigned int> *Validation_Expected2 =
  3224. &handler.GetTableParamByName(L"Validation.Expected2")->m_uintTable;
  3225. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int;
  3226. size_t count = handler.GetTableParamByName(L"Validation.NumInput")->m_uint;
  3227. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3228. pDevice, m_support, pStream, "BinaryUintOp",
  3229. // this callbacked is called when the test
  3230. // is creating the resource to run the test
  3231. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3232. VERIFY_IS_TRUE(0 == _stricmp(Name, "SBinaryUintOp"));
  3233. size_t size = sizeof(SBinaryUintOp) * count;
  3234. Data.resize(size);
  3235. SBinaryUintOp *pPrimitives = (SBinaryUintOp *)Data.data();
  3236. for (size_t i = 0; i < count; ++i) {
  3237. SBinaryUintOp *p = &pPrimitives[i];
  3238. unsigned int val1 = (*Validation_Input1)[i % Validation_Input1->GetSize()];
  3239. unsigned int val2 = (*Validation_Input2)[i % Validation_Input2->GetSize()];
  3240. p->input1 = val1;
  3241. p->input2 = val2;
  3242. }
  3243. // use shader from data table
  3244. pShaderOp->Shaders.at(0).Target = shader.Target;
  3245. pShaderOp->Shaders.at(0).EntryPoint = shader.EntryPoint;
  3246. pShaderOp->Shaders.at(0).Text = shader.Text;
  3247. });
  3248. MappedData data;
  3249. test->Test->GetReadBackData("SBinaryUintOp", &data);
  3250. SBinaryUintOp *pPrimitives = (SBinaryUintOp *)data.data();
  3251. WEX::TestExecution::DisableVerifyExceptions dve;
  3252. if (numExpected == 2) {
  3253. for (unsigned i = 0; i < count; ++i) {
  3254. SBinaryUintOp *p = &pPrimitives[i];
  3255. unsigned int val1 = (*Validation_Expected1)[i % Validation_Expected1->GetSize()];
  3256. unsigned int val2 = (*Validation_Expected2)[i % Validation_Expected2->GetSize()];
  3257. LogCommentFmt(L"element #%u, input1 = %11u(0x%08x), input2 = "
  3258. L"%11u(0x%08x), output1 = "
  3259. L"%11u(0x%08x), expected1 = %11u(0x%08x), output2 = "
  3260. L"%11u(0x%08x), expected2 = %11u(0x%08x)",
  3261. i, p->input1, p->input1, p->input2, p->input2, p->output1,
  3262. p->output1, val1, val1, p->output2, p->output2, val2,
  3263. val2);
  3264. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  3265. VerifyOutputWithExpectedValueInt(p->output2, val2, Validation_Tolerance);
  3266. }
  3267. }
  3268. else if (numExpected == 1) {
  3269. for (unsigned i = 0; i < count; ++i) {
  3270. SBinaryUintOp *p = &pPrimitives[i];
  3271. unsigned int val1 = (*Validation_Expected1)[i % Validation_Expected1->GetSize()];
  3272. LogCommentFmt(L"element #%u, input1 = %11u(0x%08x), input2 = "
  3273. L"%11u(0x%08x), output = "
  3274. L"%11u(0x%08x), expected = %11u(0x%08x)", i,
  3275. p->input1, p->input1, p->input2, p->input2,
  3276. p->output1, p->output1, val1, val1);
  3277. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  3278. }
  3279. }
  3280. else {
  3281. LogErrorFmt(L"Unexpected number of expected values for operation %i", numExpected);
  3282. }
  3283. }
  3284. TEST_F(ExecutionTest, TertiaryUintOpTest) {
  3285. WEX::TestExecution::SetVerifyOutput verifySettings(
  3286. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3287. CComPtr<IStream> pStream;
  3288. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3289. CComPtr<ID3D12Device> pDevice;
  3290. if (!CreateDevice(&pDevice)) {
  3291. return;
  3292. }
  3293. // Read data from the table
  3294. size_t tableSize = sizeof(TertiaryUintOpParameters) / sizeof(TableParameter);
  3295. TableParameterHandler handler(TertiaryUintOpParameters, tableSize);
  3296. handler.clearTableParameter();
  3297. VERIFY_SUCCEEDED(ParseTableRow(TertiaryUintOpParameters, tableSize));
  3298. st::ShaderOpShader shader;
  3299. CW2A Name(handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  3300. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3301. CW2A EntryPoint(handler.GetTableParamByName(L"ShaderOp.EntryPoint")->m_str);
  3302. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3303. shader.Name = Name.m_psz;
  3304. shader.Target = Target.m_psz;
  3305. shader.EntryPoint = EntryPoint.m_psz;
  3306. shader.Text = Text.m_psz;
  3307. WEX::TestExecution::TestDataArray<unsigned int> *Validation_Input1 =
  3308. &handler.GetTableParamByName(L"Validation.Input1")->m_uintTable;
  3309. WEX::TestExecution::TestDataArray<unsigned int> *Validation_Input2 =
  3310. &handler.GetTableParamByName(L"Validation.Input2")->m_uintTable;
  3311. WEX::TestExecution::TestDataArray<unsigned int> *Validation_Input3 =
  3312. &handler.GetTableParamByName(L"Validation.Input3")->m_uintTable;
  3313. WEX::TestExecution::TestDataArray<unsigned int> *Validation_Expected =
  3314. &handler.GetTableParamByName(L"Validation.Expected")->m_uintTable;
  3315. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int;
  3316. size_t count = handler.GetTableParamByName(L"Validation.NumInput")->m_uint;
  3317. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3318. pDevice, m_support, pStream, "TertiaryUintOp",
  3319. // this callbacked is called when the test
  3320. // is creating the resource to run the test
  3321. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3322. VERIFY_IS_TRUE(0 == _stricmp(Name, "STertiaryUintOp"));
  3323. size_t size = sizeof(STertiaryUintOp) * count;
  3324. Data.resize(size);
  3325. STertiaryUintOp *pPrimitives = (STertiaryUintOp *)Data.data();
  3326. for (size_t i = 0; i < count; ++i) {
  3327. STertiaryUintOp *p = &pPrimitives[i];
  3328. unsigned int val1 = (*Validation_Input1)[i % Validation_Input1->GetSize()];
  3329. unsigned int val2 = (*Validation_Input2)[i % Validation_Input2->GetSize()];
  3330. unsigned int val3 = (*Validation_Input3)[i % Validation_Input3->GetSize()];
  3331. p->input1 = val1;
  3332. p->input2 = val2;
  3333. p->input3 = val3;
  3334. }
  3335. // use shader from data table
  3336. pShaderOp->Shaders.at(0).Target = shader.Target;
  3337. pShaderOp->Shaders.at(0).EntryPoint = shader.EntryPoint;
  3338. pShaderOp->Shaders.at(0).Text = shader.Text;
  3339. });
  3340. MappedData data;
  3341. test->Test->GetReadBackData("STertiaryUintOp", &data);
  3342. STertiaryUintOp *pPrimitives = (STertiaryUintOp *)data.data();
  3343. WEX::TestExecution::DisableVerifyExceptions dve;
  3344. for (unsigned i = 0; i < count; ++i) {
  3345. STertiaryUintOp *p = &pPrimitives[i];
  3346. unsigned int val1 = (*Validation_Expected)[i % Validation_Expected->GetSize()];
  3347. LogCommentFmt(L"element #%u, input1 = %11u(0x%08x), input2 = "
  3348. L"%11u(0x%08x), input3 = %11u(0x%08x), output = "
  3349. L"%11u(0x%08x), expected = %11u(0x%08x)", i,
  3350. p->input1, p->input1, p->input2, p->input2, p->input3, p->input3,
  3351. p->output, p->output, val1, val1);
  3352. VerifyOutputWithExpectedValueInt(p->output, val1, Validation_Tolerance);
  3353. }
  3354. }
  3355. TEST_F(ExecutionTest, DotTest) {
  3356. WEX::TestExecution::SetVerifyOutput verifySettings(
  3357. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3358. CComPtr<IStream> pStream;
  3359. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3360. CComPtr<ID3D12Device> pDevice;
  3361. if (!CreateDevice(&pDevice)) {
  3362. return;
  3363. }
  3364. int tableSize = sizeof(DotOpParameters) / sizeof(TableParameter);
  3365. TableParameterHandler handler(DotOpParameters, tableSize);
  3366. handler.clearTableParameter();
  3367. VERIFY_SUCCEEDED(ParseTableRow(DotOpParameters, tableSize));
  3368. st::ShaderOpShader shader;
  3369. CW2A Name(handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  3370. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3371. CW2A EntryPoint(handler.GetTableParamByName(L"ShaderOp.EntryPoint")->m_str);
  3372. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3373. shader.Name = Name.m_psz;
  3374. shader.Target = Target.m_psz;
  3375. shader.EntryPoint = EntryPoint.m_psz;
  3376. shader.Text = Text.m_psz;
  3377. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Input1 =
  3378. &handler.GetTableParamByName(L"Validation.Input1")->m_StringTable;
  3379. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Input2 =
  3380. &handler.GetTableParamByName(L"Validation.Input2")->m_StringTable;
  3381. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_dot2 =
  3382. &handler.GetTableParamByName(L"Validation.dot2")->m_StringTable;
  3383. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_dot3 =
  3384. &handler.GetTableParamByName(L"Validation.dot3")->m_StringTable;
  3385. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_dot4 =
  3386. &handler.GetTableParamByName(L"Validation.dot4")->m_StringTable;
  3387. PCWSTR Validation_type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  3388. double tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  3389. unsigned int count = handler.GetTableParamByName(L"Validation.NumInput")->m_uint;
  3390. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3391. pDevice, m_support, pStream, "DotOp",
  3392. // this callbacked is called when the test
  3393. // is creating the resource to run the test
  3394. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3395. VERIFY_IS_TRUE(0 == _stricmp(Name, "SDotOp"));
  3396. size_t size = sizeof(SDotOp) * count;
  3397. Data.resize(size);
  3398. SDotOp *pPrimitives = (SDotOp*)Data.data();
  3399. for (size_t i = 0; i < count; ++i) {
  3400. SDotOp *p = &pPrimitives[i];
  3401. XMFLOAT4 val1,val2;
  3402. VERIFY_SUCCEEDED(ParseDataToVectorFloat((*Validation_Input1)[i],
  3403. (float *)&val1, 4));
  3404. VERIFY_SUCCEEDED(ParseDataToVectorFloat((*Validation_Input2)[i],
  3405. (float *)&val2, 4));
  3406. p->input1 = val1;
  3407. p->input2 = val2;
  3408. }
  3409. // use shader from data table
  3410. pShaderOp->Shaders.at(0).Target = shader.Target;
  3411. pShaderOp->Shaders.at(0).EntryPoint = shader.EntryPoint;
  3412. pShaderOp->Shaders.at(0).Text = shader.Text;
  3413. });
  3414. MappedData data;
  3415. test->Test->GetReadBackData("SDotOp", &data);
  3416. SDotOp *pPrimitives = (SDotOp*)data.data();
  3417. WEX::TestExecution::DisableVerifyExceptions dve;
  3418. for (size_t i = 0; i < count; ++i) {
  3419. SDotOp *p = &pPrimitives[i];
  3420. float dot2, dot3, dot4;
  3421. VERIFY_SUCCEEDED(ParseDataToFloat((*Validation_dot2)[i], dot2));
  3422. VERIFY_SUCCEEDED(ParseDataToFloat((*Validation_dot3)[i], dot3));
  3423. VERIFY_SUCCEEDED(ParseDataToFloat((*Validation_dot4)[i], dot4));
  3424. LogCommentFmt(
  3425. L"element #%u, input1 = (%f, %f, %f, %f), input2 = (%f, %f, "
  3426. L"%f, %f), \n dot2 = %f, dot2_expected = %f, dot3 = %f, "
  3427. L"dot3_expected = %f, dot4 = %f, dot4_expected = %f",
  3428. i, p->input1.x, p->input1.y, p->input1.z, p->input1.w, p->input2.x,
  3429. p->input2.y, p->input2.z, p->input2.w, p->o_dot2, dot2, p->o_dot3, dot3,
  3430. p->o_dot4, dot4);
  3431. VerifyOutputWithExpectedValueFloat(p->o_dot2, dot2, Validation_type,
  3432. tolerance);
  3433. VerifyOutputWithExpectedValueFloat(p->o_dot3, dot3, Validation_type,
  3434. tolerance);
  3435. VerifyOutputWithExpectedValueFloat(p->o_dot4, dot4, Validation_type,
  3436. tolerance);
  3437. }
  3438. }
  3439. TEST_F(ExecutionTest, Msad4Test) {
  3440. WEX::TestExecution::SetVerifyOutput verifySettings(
  3441. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3442. CComPtr<IStream> pStream;
  3443. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3444. CComPtr<ID3D12Device> pDevice;
  3445. if (!CreateDevice(&pDevice)) {
  3446. return;
  3447. }
  3448. size_t tableSize = sizeof(Msad4OpParameters) / sizeof(TableParameter);
  3449. TableParameterHandler handler(Msad4OpParameters, tableSize);
  3450. handler.clearTableParameter();
  3451. VERIFY_SUCCEEDED(ParseTableRow(Msad4OpParameters, tableSize));
  3452. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3453. double tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  3454. unsigned int count = handler.GetTableParamByName(L"Validation.NumInput")->m_uint;
  3455. WEX::TestExecution::TestDataArray<unsigned int> *Validation_Reference =
  3456. &handler.GetTableParamByName(L"Validation.Reference")->m_uintTable;
  3457. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Source =
  3458. &handler.GetTableParamByName(L"Validation.Source")->m_StringTable;
  3459. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Accum =
  3460. &handler.GetTableParamByName(L"Validation.Accum")->m_StringTable;
  3461. WEX::TestExecution::TestDataArray<WEX::Common::String> *Validation_Expected =
  3462. &handler.GetTableParamByName(L"Validation.Expected")->m_StringTable;
  3463. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3464. pDevice, m_support, pStream, "Msad4",
  3465. // this callbacked is called when the test
  3466. // is creating the resource to run the test
  3467. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3468. VERIFY_IS_TRUE(0 == _stricmp(Name, "SMsad4"));
  3469. size_t size = sizeof(SMsad4) * count;
  3470. Data.resize(size);
  3471. SMsad4 *pPrimitives = (SMsad4*)Data.data();
  3472. for (size_t i = 0; i < count; ++i) {
  3473. SMsad4 *p = &pPrimitives[i];
  3474. XMUINT2 src;
  3475. XMUINT4 accum;
  3476. VERIFY_SUCCEEDED(ParseDataToVectorUint((*Validation_Source)[i], (unsigned int*)&src, 2));
  3477. VERIFY_SUCCEEDED(ParseDataToVectorUint((*Validation_Accum)[i], (unsigned int*)&accum, 4));
  3478. p->ref = (*Validation_Reference)[i];
  3479. p->src = src;
  3480. p->accum = accum;
  3481. }
  3482. // use shader from data table
  3483. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3484. });
  3485. MappedData data;
  3486. test->Test->GetReadBackData("SMsad4", &data);
  3487. SMsad4 *pPrimitives = (SMsad4*)data.data();
  3488. WEX::TestExecution::DisableVerifyExceptions dve;
  3489. for (size_t i = 0; i < count; ++i) {
  3490. SMsad4 *p = &pPrimitives[i];
  3491. XMUINT4 result;
  3492. VERIFY_SUCCEEDED(ParseDataToVectorUint((*Validation_Expected)[i],
  3493. (unsigned int *)&result, 4));
  3494. LogCommentFmt(
  3495. L"element #%u, ref = %u(0x%08x), src = %u(0x%08x), %u(0x%08x), "
  3496. L"accum = %u(0x%08x), %u(0x%08x), %u(0x%08x), %u(0x%08x),\n"
  3497. L"result = %u(0x%08x), %u(0x%08x), %u(0x%08x), %u(0x%08x),\n"
  3498. L"expected = %u(0x%08x), %u(0x%08x), %u(0x%08x), %u(0x%08x)", i,
  3499. p->ref, p->ref, p->src.x, p->src.x, p->src.y, p->src.y, p->accum.x,
  3500. p->accum.x, p->accum.y, p->accum.y, p->accum.z, p->accum.z,
  3501. p->accum.w, p->accum.w, p->result.x, p->result.x, p->result.y,
  3502. p->result.y, p->result.z, p->result.z, p->result.w, p->result.w,
  3503. result.x, result.x, result.y, result.y, result.z, result.z,
  3504. result.w, result.w);
  3505. VerifyOutputWithExpectedValueInt(p->result.x, result.x, tolerance);
  3506. VerifyOutputWithExpectedValueInt(p->result.y, result.y, tolerance);
  3507. VerifyOutputWithExpectedValueInt(p->result.z, result.z, tolerance);
  3508. VerifyOutputWithExpectedValueInt(p->result.w, result.w, tolerance);
  3509. }
  3510. }
  3511. template <class T1, class T2>
  3512. void ExecutionTest::WaveIntrinsicsActivePrefixTest(
  3513. TableParameter *pParameterList, size_t numParameter, bool isPrefix) {
  3514. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3515. // Resource representation for compute shader
  3516. // firstLaneId is used to group different waves
  3517. struct PerThreadData {
  3518. int firstLaneId;
  3519. int mask;
  3520. T1 input;
  3521. T2 output;
  3522. };
  3523. unsigned int NumThreadsX = 8;
  3524. unsigned int NumThreadsY = 12;
  3525. unsigned int NumThreadsZ = 1;
  3526. static const unsigned int ThreadsPerGroup = NumThreadsX * NumThreadsY * NumThreadsZ;
  3527. static const unsigned int DispatchGroupCount = 1;
  3528. static const unsigned int ThreadCount = ThreadsPerGroup * DispatchGroupCount;
  3529. CComPtr<IStream> pStream;
  3530. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3531. CComPtr<ID3D12Device> pDevice;
  3532. if (!CreateDevice(&pDevice)) {
  3533. return;
  3534. }
  3535. if (!DoesDeviceSupportWaveOps(pDevice)) {
  3536. // Optional feature, so it's correct to not support it if declared as such.
  3537. WEX::Logging::Log::Comment(L"Device does not support wave operations.");
  3538. return;
  3539. }
  3540. TableParameterHandler handler(pParameterList, numParameter);
  3541. handler.clearTableParameter();
  3542. VERIFY_SUCCEEDED(ParseTableRow(pParameterList, numParameter));
  3543. unsigned int numInputSet = handler.GetTableParamByName(L"Validation.NumInputSet")->m_uint;
  3544. // Obtain the list of input lists
  3545. typedef WEX::TestExecution::TestDataArray<T1> DataArray;
  3546. std::vector<DataArray*> InputList;
  3547. for (unsigned int i = 0;
  3548. i < numInputSet; ++i) {
  3549. std::wstring inputName = L"Validation.InputSet";
  3550. inputName.append(std::to_wstring(i + 1));
  3551. InputList.push_back(handler.GetDataArray<T1>(inputName.data()));
  3552. }
  3553. CW2A Text(handler.GetTableParamByName(L"ShaderOp.text")->m_str);
  3554. std::shared_ptr<st::ShaderOpSet> ShaderOpSet = std::make_shared<st::ShaderOpSet>();
  3555. st::ParseShaderOpSetFromStream(pStream, ShaderOpSet.get());
  3556. // Running compute shader for each input set with different masks
  3557. for (size_t setIndex = 0; setIndex < numInputSet; ++setIndex) {
  3558. for (size_t maskIndex = 0; maskIndex < sizeof(MaskFunctionTable) / sizeof(MaskFunction); ++maskIndex) {
  3559. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTestAfterParse(
  3560. pDevice, m_support, pStream, "WaveIntrinsicsOp",
  3561. // this callbacked is called when the test
  3562. // is creating the resource to run the test
  3563. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3564. VERIFY_IS_TRUE(0 == _stricmp(Name, "SWaveIntrinsicsOp"));
  3565. size_t size = sizeof(PerThreadData) * ThreadCount;
  3566. Data.resize(size);
  3567. PerThreadData *pPrimitives = (PerThreadData*)Data.data();
  3568. // 4 different inputs for each operation test
  3569. size_t index = 0;
  3570. while (index < ThreadCount) {
  3571. PerThreadData *p = &pPrimitives[index];
  3572. DataArray *IntList = InputList[setIndex];
  3573. p->mask = MaskFunctionTable[maskIndex](index);
  3574. p->input = (*IntList)[index % IntList->GetSize()];
  3575. p->output = 0xFFFFBFFF;
  3576. index++;
  3577. }
  3578. // use shader from data table
  3579. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3580. }, ShaderOpSet);
  3581. // Check the value
  3582. MappedData data;
  3583. test->Test->GetReadBackData("SWaveIntrinsicsOp", &data);
  3584. PerThreadData *pPrimitives = (PerThreadData*)data.data();
  3585. WEX::TestExecution::DisableVerifyExceptions dve;
  3586. // Grouping data by waves
  3587. std::vector<int> firstLaneIds;
  3588. for (size_t i = 0; i < ThreadCount; ++i) {
  3589. PerThreadData *p = &pPrimitives[i];
  3590. int firstLaneId = p->firstLaneId;
  3591. if (!contains(firstLaneIds, firstLaneId)) {
  3592. firstLaneIds.push_back(firstLaneId);
  3593. }
  3594. }
  3595. std::map<int, std::unique_ptr<std::vector<PerThreadData *>>> waves;
  3596. for (size_t i = 0; i < firstLaneIds.size(); ++i) {
  3597. waves[firstLaneIds.at(i)] = std::make_unique<std::vector<PerThreadData*>>(std::vector<PerThreadData*>());
  3598. }
  3599. for (size_t i = 0; i < ThreadCount; ++i) {
  3600. PerThreadData *p = &pPrimitives[i];
  3601. waves[p->firstLaneId].get()->push_back(p);
  3602. }
  3603. // validate for each wave
  3604. for (size_t i = 0; i < firstLaneIds.size(); ++i) {
  3605. // collect inputs and masks for a given wave
  3606. std::vector<PerThreadData *> *waveData = waves[firstLaneIds.at(i)].get();
  3607. std::vector<T1> inputList(waveData->size());
  3608. std::vector<int> maskList(waveData->size());
  3609. std::wstring inputStr = L"Wave Inputs: ";
  3610. std::wstring maskStr = L"Wave Mask: ";
  3611. for (size_t j = 0; j < waveData->size(); ++j) {
  3612. inputList.at(j) = (waveData->at(j)->input);
  3613. maskList.at(j) = (waveData->at(j)->mask);
  3614. inputStr.append(std::to_wstring(waveData->at(j)->input));
  3615. inputStr.append(L" ");
  3616. maskStr.append(std::to_wstring(waveData->at(j)->mask));
  3617. maskStr.append(L" ");
  3618. }
  3619. LogCommentFmt(inputStr.data());
  3620. LogCommentFmt(maskStr.data());
  3621. // Compute expected output for a given inputs, masks, and index
  3622. for (size_t laneIndex = 0; laneIndex < waveData->size(); ++laneIndex) {
  3623. T2 expected;
  3624. // WaveActive is equivalent to WavePrefix lane # lane count
  3625. unsigned int index = isPrefix ? laneIndex : waveData->size();
  3626. if (waveData->at(laneIndex)->mask == 1) {
  3627. expected = computeExpectedWithShaderOp<T1, T2>(
  3628. inputList, maskList, 1, index,
  3629. handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  3630. }
  3631. else {
  3632. expected = computeExpectedWithShaderOp<T1, T2>(
  3633. inputList, maskList, 0, index,
  3634. handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  3635. }
  3636. // TODO: use different comparison for floating point inputs
  3637. bool equal = waveData->at(laneIndex)->output == expected;
  3638. if (!equal) {
  3639. LogCommentFmt(L"lane%d: %4d, Expected : %4d", laneIndex, waveData->at(laneIndex)->output, expected);
  3640. }
  3641. VERIFY_IS_TRUE(equal);
  3642. }
  3643. }
  3644. }
  3645. }
  3646. }
  3647. static const unsigned int MinWarpVersionForWaveIntrinsics = 16202;
  3648. TEST_F(ExecutionTest, WaveIntrinsicsActiveIntTest) {
  3649. if (GetTestParamUseWARP(true) &&
  3650. !IsValidWarpDllVersion(MinWarpVersionForWaveIntrinsics)) {
  3651. return;
  3652. }
  3653. WaveIntrinsicsActivePrefixTest<int, int>(
  3654. WaveIntrinsicsActiveIntParameters,
  3655. sizeof(WaveIntrinsicsActiveIntParameters) / sizeof(TableParameter),
  3656. /*isPrefix*/ false);
  3657. }
  3658. TEST_F(ExecutionTest, WaveIntrinsicsActiveUintTest) {
  3659. if (GetTestParamUseWARP(true) &&
  3660. !IsValidWarpDllVersion(MinWarpVersionForWaveIntrinsics)) {
  3661. return;
  3662. }
  3663. WaveIntrinsicsActivePrefixTest<unsigned int, unsigned int>(
  3664. WaveIntrinsicsActiveUintParameters,
  3665. sizeof(WaveIntrinsicsActiveUintParameters) / sizeof(TableParameter),
  3666. /*isPrefix*/ false);
  3667. }
  3668. TEST_F(ExecutionTest, WaveIntrinsicsPrefixIntTest) {
  3669. if (GetTestParamUseWARP(true) &&
  3670. !IsValidWarpDllVersion(MinWarpVersionForWaveIntrinsics)) {
  3671. return;
  3672. }
  3673. WaveIntrinsicsActivePrefixTest<int, int>(
  3674. WaveIntrinsicsPrefixIntParameters,
  3675. sizeof(WaveIntrinsicsPrefixIntParameters) / sizeof(TableParameter),
  3676. /*isPrefix*/ true);
  3677. }
  3678. TEST_F(ExecutionTest, WaveIntrinsicsPrefixUintTest) {
  3679. if (GetTestParamUseWARP(true) &&
  3680. !IsValidWarpDllVersion(MinWarpVersionForWaveIntrinsics)) {
  3681. return;
  3682. }
  3683. WaveIntrinsicsActivePrefixTest<unsigned int, unsigned int>(
  3684. WaveIntrinsicsPrefixUintParameters,
  3685. sizeof(WaveIntrinsicsPrefixUintParameters) / sizeof(TableParameter),
  3686. /*isPrefix*/ true);
  3687. }
  3688. static void WriteReadBackDump(st::ShaderOp *pShaderOp, st::ShaderOpTest *pTest,
  3689. char **pReadBackDump) {
  3690. std::stringstream str;
  3691. unsigned count = 0;
  3692. for (auto &R : pShaderOp->Resources) {
  3693. if (!R.ReadBack)
  3694. continue;
  3695. ++count;
  3696. str << "Resource: " << R.Name << "\r\n";
  3697. // Find a descriptor that can tell us how to dump this resource.
  3698. bool found = false;
  3699. for (auto &Heaps : pShaderOp->DescriptorHeaps) {
  3700. for (auto &D : Heaps.Descriptors) {
  3701. if (_stricmp(D.ResName, R.Name) != 0) {
  3702. continue;
  3703. }
  3704. found = true;
  3705. if (_stricmp(D.Kind, "UAV") != 0) {
  3706. str << "Resource dump for kind " << D.Kind << " not implemented yet.\r\n";
  3707. break;
  3708. }
  3709. if (D.UavDesc.ViewDimension != D3D12_UAV_DIMENSION_BUFFER) {
  3710. str << "Resource dump for this kind of view dimension not implemented yet.\r\n";
  3711. break;
  3712. }
  3713. // We can map back to the structure if a structured buffer via the shader, but
  3714. // we'll keep this simple and simply dump out 32-bit uint/float representations.
  3715. MappedData data;
  3716. pTest->GetReadBackData(R.Name, &data);
  3717. uint32_t *pData = (uint32_t *)data.data();
  3718. size_t u32_count = R.Desc.Width / sizeof(uint32_t);
  3719. for (size_t i = 0; i < u32_count; ++i) {
  3720. float f = *(float *)pData;
  3721. str << i << ": 0n" << *pData << " 0x" << std::hex << *pData
  3722. << std::dec << " " << f << "\r\n";
  3723. ++pData;
  3724. }
  3725. break;
  3726. }
  3727. if (found) break;
  3728. }
  3729. if (!found) {
  3730. str << "Unable to find a view for the resource.\r\n";
  3731. }
  3732. }
  3733. str << "Resources read back: " << count << "\r\n";
  3734. std::string s(str.str());
  3735. CComHeapPtr<char> pDump;
  3736. if (!pDump.Allocate(s.size() + 1))
  3737. throw std::bad_alloc();
  3738. memcpy(pDump.m_pData, s.data(), s.size());
  3739. pDump.m_pData[s.size()] = '\0';
  3740. *pReadBackDump = pDump.Detach();
  3741. }
  3742. // This is the exported interface by use from HLSLHost.exe.
  3743. // It's exclusive with the use of the DLL as a TAEF target.
  3744. extern "C" {
  3745. __declspec(dllexport) HRESULT WINAPI InitializeOpTests(void *pStrCtx, st::OutputStringFn pOutputStrFn) {
  3746. HRESULT hr = EnableExperimentalShaderModels();
  3747. if (FAILED(hr)) {
  3748. pOutputStrFn(pStrCtx, L"Unable to enable experimental shader models.\r\n.");
  3749. }
  3750. return S_OK;
  3751. }
  3752. __declspec(dllexport) HRESULT WINAPI
  3753. RunOpTest(void *pStrCtx, st::OutputStringFn pOutputStrFn, LPCSTR pText,
  3754. ID3D12Device *pDevice, ID3D12CommandQueue *pCommandQueue,
  3755. ID3D12Resource *pRenderTarget, char **pReadBackDump) {
  3756. HRESULT hr;
  3757. if (pReadBackDump) *pReadBackDump = nullptr;
  3758. st::SetOutputFn(pStrCtx, pOutputStrFn);
  3759. CComPtr<ID3D12InfoQueue> pInfoQueue;
  3760. CComHeapPtr<char> pDump;
  3761. bool FilterCreation = false;
  3762. if (SUCCEEDED(pDevice->QueryInterface(&pInfoQueue))) {
  3763. // Creation is largely driven by inputs, so don't log create/destroy messages.
  3764. pInfoQueue->PushEmptyStorageFilter();
  3765. pInfoQueue->PushEmptyRetrievalFilter();
  3766. if (FilterCreation) {
  3767. D3D12_INFO_QUEUE_FILTER filter;
  3768. D3D12_MESSAGE_CATEGORY denyCategories[] = { D3D12_MESSAGE_CATEGORY_STATE_CREATION };
  3769. ZeroMemory(&filter, sizeof(filter));
  3770. filter.DenyList.NumCategories = _countof(denyCategories);
  3771. filter.DenyList.pCategoryList = denyCategories;
  3772. pInfoQueue->PushStorageFilter(&filter);
  3773. }
  3774. }
  3775. else {
  3776. pOutputStrFn(pStrCtx, L"Unable to enable info queue for D3D.\r\n.");
  3777. }
  3778. try {
  3779. dxc::DxcDllSupport m_support;
  3780. m_support.Initialize();
  3781. const char *pName = nullptr;
  3782. CComPtr<IStream> pStream = SHCreateMemStream((BYTE *)pText, strlen(pText));
  3783. std::shared_ptr<st::ShaderOpSet> ShaderOpSet =
  3784. std::make_shared<st::ShaderOpSet>();
  3785. st::ParseShaderOpSetFromStream(pStream, ShaderOpSet.get());
  3786. st::ShaderOp *pShaderOp;
  3787. if (pName == nullptr) {
  3788. if (ShaderOpSet->ShaderOps.size() != 1) {
  3789. pOutputStrFn(pStrCtx, L"Expected a single shader operation.\r\n");
  3790. return E_FAIL;
  3791. }
  3792. pShaderOp = ShaderOpSet->ShaderOps[0].get();
  3793. }
  3794. else {
  3795. pShaderOp = ShaderOpSet->GetShaderOp(pName);
  3796. }
  3797. if (pShaderOp == nullptr) {
  3798. std::string msg = "Unable to find shader op ";
  3799. msg += pName;
  3800. msg += "; available ops";
  3801. const char sep = ':';
  3802. for (auto &pAvailOp : ShaderOpSet->ShaderOps) {
  3803. msg += sep;
  3804. msg += pAvailOp->Name ? pAvailOp->Name : "[n/a]";
  3805. }
  3806. CA2W msgWide(msg.c_str());
  3807. pOutputStrFn(pStrCtx, msgWide);
  3808. return E_FAIL;
  3809. }
  3810. std::shared_ptr<st::ShaderOpTest> test = std::make_shared<st::ShaderOpTest>();
  3811. test->SetupRenderTarget(pShaderOp, pDevice, pCommandQueue, pRenderTarget);
  3812. test->SetDxcSupport(&m_support);
  3813. test->RunShaderOp(pShaderOp);
  3814. test->PresentRenderTarget(pShaderOp, pCommandQueue, pRenderTarget);
  3815. pOutputStrFn(pStrCtx, L"Rendering complete.\r\n");
  3816. if (!pShaderOp->IsCompute()) {
  3817. D3D12_QUERY_DATA_PIPELINE_STATISTICS stats;
  3818. test->GetPipelineStats(&stats);
  3819. wchar_t statsText[400];
  3820. StringCchPrintfW(statsText, _countof(statsText),
  3821. L"Vertices/primitives read by input assembler: %I64u/%I64u\r\n"
  3822. L"Vertex shader invocations: %I64u\r\n"
  3823. L"Geometry shader invocations/output primitive: %I64u/%I64u\r\n"
  3824. L"Primitives sent to rasterizer/rendered: %I64u/%I64u\r\n"
  3825. L"PS/HS/DS/CS invocations: %I64u/%I64u/%I64u/%I64u\r\n",
  3826. stats.IAVertices, stats.IAPrimitives, stats.VSInvocations,
  3827. stats.GSInvocations, stats.GSPrimitives, stats.CInvocations,
  3828. stats.CPrimitives, stats.PSInvocations, stats.HSInvocations,
  3829. stats.DSInvocations, stats.CSInvocations);
  3830. pOutputStrFn(pStrCtx, statsText);
  3831. }
  3832. if (pReadBackDump) {
  3833. WriteReadBackDump(pShaderOp, test.get(), &pDump);
  3834. }
  3835. hr = S_OK;
  3836. }
  3837. catch (const CAtlException &E)
  3838. {
  3839. hr = E.m_hr;
  3840. }
  3841. catch (const std::bad_alloc &)
  3842. {
  3843. hr = E_OUTOFMEMORY;
  3844. }
  3845. catch (const std::exception &)
  3846. {
  3847. hr = E_FAIL;
  3848. }
  3849. // Drain the device message queue if available.
  3850. if (pInfoQueue != nullptr) {
  3851. wchar_t buf[200];
  3852. StringCchPrintfW(buf, _countof(buf),
  3853. L"NumStoredMessages=%u limit/discarded by limit=%u/%u "
  3854. L"allowed/denied by storage filter=%u/%u "
  3855. L"NumStoredMessagesAllowedByRetrievalFilter=%u\r\n",
  3856. (unsigned)pInfoQueue->GetNumStoredMessages(),
  3857. (unsigned)pInfoQueue->GetMessageCountLimit(),
  3858. (unsigned)pInfoQueue->GetNumMessagesDiscardedByMessageCountLimit(),
  3859. (unsigned)pInfoQueue->GetNumMessagesAllowedByStorageFilter(),
  3860. (unsigned)pInfoQueue->GetNumMessagesDeniedByStorageFilter(),
  3861. (unsigned)pInfoQueue->GetNumStoredMessagesAllowedByRetrievalFilter());
  3862. pOutputStrFn(pStrCtx, buf);
  3863. WriteInfoQueueMessages(pStrCtx, pOutputStrFn, pInfoQueue);
  3864. pInfoQueue->ClearStoredMessages();
  3865. pInfoQueue->PopRetrievalFilter();
  3866. pInfoQueue->PopStorageFilter();
  3867. if (FilterCreation) {
  3868. pInfoQueue->PopStorageFilter();
  3869. }
  3870. }
  3871. if (pReadBackDump) *pReadBackDump = pDump.Detach();
  3872. return hr;
  3873. }
  3874. }