ExecutionTest.cpp 242 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046
  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. // Checks if the given warp version supports the given operation.
  170. bool IsValidWarpDllVersion(unsigned int minBuildNumber) {
  171. HMODULE pLibrary = LoadLibrary("D3D10Warp.dll");
  172. if (pLibrary) {
  173. char path[MAX_PATH];
  174. DWORD length = GetModuleFileName(pLibrary, path, MAX_PATH);
  175. if (length) {
  176. DWORD dwVerHnd = 0;
  177. DWORD dwVersionInfoSize = GetFileVersionInfoSize(path, &dwVerHnd);
  178. std::unique_ptr<int[]> VffInfo(new int[dwVersionInfoSize]);
  179. if (GetFileVersionInfo(path, NULL, dwVersionInfoSize, VffInfo.get())) {
  180. LPVOID versionInfo;
  181. UINT size;
  182. if (VerQueryValue(VffInfo.get(), "\\", &versionInfo, &size)) {
  183. if (size) {
  184. VS_FIXEDFILEINFO *verInfo = (VS_FIXEDFILEINFO *)versionInfo;
  185. unsigned int warpBuildNumber = verInfo->dwFileVersionLS >> 16 & 0xffff;
  186. if (verInfo->dwSignature == 0xFEEF04BD && warpBuildNumber >= minBuildNumber) {
  187. return true;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. FreeLibrary(pLibrary);
  194. }
  195. return false;
  196. }
  197. #if WDK_NTDDI_VERSION <= NTDDI_WIN10_RS2
  198. #define D3D12_FEATURE_D3D12_OPTIONS3 ((D3D12_FEATURE)21)
  199. #define NTDDI_WIN10_RS3 0x0A000004 /* ABRACADABRA_WIN10_RS2 */
  200. typedef
  201. enum D3D12_COMMAND_LIST_SUPPORT_FLAGS
  202. {
  203. D3D12_COMMAND_LIST_SUPPORT_FLAG_NONE = 0,
  204. D3D12_COMMAND_LIST_SUPPORT_FLAG_DIRECT = (1 << D3D12_COMMAND_LIST_TYPE_DIRECT),
  205. D3D12_COMMAND_LIST_SUPPORT_FLAG_BUNDLE = (1 << D3D12_COMMAND_LIST_TYPE_BUNDLE),
  206. D3D12_COMMAND_LIST_SUPPORT_FLAG_COMPUTE = (1 << D3D12_COMMAND_LIST_TYPE_COMPUTE),
  207. D3D12_COMMAND_LIST_SUPPORT_FLAG_COPY = (1 << D3D12_COMMAND_LIST_TYPE_COPY),
  208. D3D12_COMMAND_LIST_SUPPORT_FLAG_VIDEO_DECODE = (1 << 4),
  209. D3D12_COMMAND_LIST_SUPPORT_FLAG_VIDEO_PROCESS = (1 << 5)
  210. } D3D12_COMMAND_LIST_SUPPORT_FLAGS;
  211. typedef
  212. enum D3D12_VIEW_INSTANCING_TIER
  213. {
  214. D3D12_VIEW_INSTANCING_TIER_NOT_SUPPORTED = 0,
  215. D3D12_VIEW_INSTANCING_TIER_1 = 1,
  216. D3D12_VIEW_INSTANCING_TIER_2 = 2,
  217. D3D12_VIEW_INSTANCING_TIER_3 = 3
  218. } D3D12_VIEW_INSTANCING_TIER;
  219. typedef struct D3D12_FEATURE_DATA_D3D12_OPTIONS3
  220. {
  221. _Out_ BOOL CopyQueueTimestampQueriesSupported;
  222. _Out_ BOOL CastingFullyTypedFormatSupported;
  223. _Out_ DWORD WriteBufferImmediateSupportFlags;
  224. _Out_ D3D12_VIEW_INSTANCING_TIER ViewInstancingTier;
  225. _Out_ BOOL BarycentricsSupported;
  226. } D3D12_FEATURE_DATA_D3D12_OPTIONS3;
  227. #endif
  228. #if WDK_NTDDI_VERSION <= NTDDI_WIN10_RS3
  229. #define D3D12_FEATURE_D3D12_OPTIONS4 ((D3D12_FEATURE)23)
  230. typedef enum D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER
  231. {
  232. D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER_0,
  233. D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER_1,
  234. } D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER;
  235. typedef struct D3D12_FEATURE_DATA_D3D12_OPTIONS4
  236. {
  237. _Out_ BOOL ReservedBufferPlacementSupported;
  238. _Out_ D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER SharedResourceCompatibilityTier;
  239. _Out_ BOOL Native16BitShaderOpsSupported;
  240. } D3D12_FEATURE_DATA_D3D12_OPTIONS4;
  241. #endif
  242. // Virtual class to compute the expected result given a set of inputs
  243. struct TableParameter;
  244. class ExecutionTest {
  245. public:
  246. // By default, ignore these tests, which require a recent build to run properly.
  247. BEGIN_TEST_CLASS(ExecutionTest)
  248. TEST_CLASS_PROPERTY(L"Parallel", L"true")
  249. TEST_CLASS_PROPERTY(L"Ignore", L"true")
  250. TEST_METHOD_PROPERTY(L"Priority", L"0")
  251. END_TEST_CLASS()
  252. TEST_CLASS_SETUP(ExecutionTestClassSetup)
  253. TEST_METHOD(BasicComputeTest);
  254. TEST_METHOD(BasicTriangleTest);
  255. TEST_METHOD(BasicTriangleOpTest);
  256. BEGIN_TEST_METHOD(BasicTriangleOpTestHalf)
  257. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  258. END_TEST_METHOD()
  259. TEST_METHOD(OutOfBoundsTest);
  260. TEST_METHOD(SaturateTest);
  261. TEST_METHOD(SignTest);
  262. TEST_METHOD(Int64Test);
  263. TEST_METHOD(WaveIntrinsicsTest);
  264. TEST_METHOD(WaveIntrinsicsDDITest);
  265. TEST_METHOD(WaveIntrinsicsInPSTest);
  266. TEST_METHOD(PartialDerivTest);
  267. BEGIN_TEST_METHOD(CBufferTestHalf)
  268. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  269. END_TEST_METHOD()
  270. TEST_METHOD(BasicShaderModel61);
  271. TEST_METHOD(BasicShaderModel63);
  272. BEGIN_TEST_METHOD(WaveIntrinsicsActiveIntTest)
  273. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#WaveIntrinsicsActiveIntTable")
  274. END_TEST_METHOD()
  275. BEGIN_TEST_METHOD(WaveIntrinsicsActiveUintTest)
  276. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#WaveIntrinsicsActiveUintTable")
  277. END_TEST_METHOD()
  278. BEGIN_TEST_METHOD(WaveIntrinsicsPrefixIntTest)
  279. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#WaveIntrinsicsPrefixIntTable")
  280. END_TEST_METHOD()
  281. BEGIN_TEST_METHOD(WaveIntrinsicsPrefixUintTest)
  282. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#WaveIntrinsicsPrefixUintTable")
  283. END_TEST_METHOD()
  284. // TAEF data-driven tests.
  285. BEGIN_TEST_METHOD(UnaryFloatOpTest)
  286. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#UnaryFloatOpTable")
  287. END_TEST_METHOD()
  288. BEGIN_TEST_METHOD(BinaryFloatOpTest)
  289. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#BinaryFloatOpTable")
  290. END_TEST_METHOD()
  291. BEGIN_TEST_METHOD(TertiaryFloatOpTest)
  292. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#TertiaryFloatOpTable")
  293. END_TEST_METHOD()
  294. BEGIN_TEST_METHOD(UnaryHalfOpTest)
  295. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#UnaryHalfOpTable")
  296. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  297. END_TEST_METHOD()
  298. BEGIN_TEST_METHOD(BinaryHalfOpTest)
  299. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#BinaryHalfOpTable")
  300. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  301. END_TEST_METHOD()
  302. BEGIN_TEST_METHOD(TertiaryHalfOpTest)
  303. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#TertiaryHalfOpTable")
  304. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  305. END_TEST_METHOD()
  306. BEGIN_TEST_METHOD(UnaryIntOpTest)
  307. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#UnaryIntOpTable")
  308. END_TEST_METHOD()
  309. BEGIN_TEST_METHOD(BinaryIntOpTest)
  310. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#BinaryIntOpTable")
  311. END_TEST_METHOD()
  312. BEGIN_TEST_METHOD(TertiaryIntOpTest)
  313. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#TertiaryIntOpTable")
  314. END_TEST_METHOD()
  315. BEGIN_TEST_METHOD(UnaryUintOpTest)
  316. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#UnaryUintOpTable")
  317. END_TEST_METHOD()
  318. BEGIN_TEST_METHOD(BinaryUintOpTest)
  319. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#BinaryUintOpTable")
  320. END_TEST_METHOD()
  321. BEGIN_TEST_METHOD(TertiaryUintOpTest)
  322. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#TertiaryUintOpTable")
  323. END_TEST_METHOD()
  324. BEGIN_TEST_METHOD(UnaryInt16OpTest)
  325. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#UnaryInt16OpTable")
  326. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  327. END_TEST_METHOD()
  328. BEGIN_TEST_METHOD(BinaryInt16OpTest)
  329. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#BinaryInt16OpTable")
  330. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  331. END_TEST_METHOD()
  332. BEGIN_TEST_METHOD(TertiaryInt16OpTest)
  333. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#TertiaryInt16OpTable")
  334. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  335. END_TEST_METHOD()
  336. BEGIN_TEST_METHOD(UnaryUint16OpTest)
  337. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#UnaryUint16OpTable")
  338. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  339. END_TEST_METHOD()
  340. BEGIN_TEST_METHOD(BinaryUint16OpTest)
  341. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#BinaryUint16OpTable")
  342. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  343. END_TEST_METHOD()
  344. BEGIN_TEST_METHOD(TertiaryUint16OpTest)
  345. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#TertiaryUint16OpTable")
  346. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  347. END_TEST_METHOD()
  348. BEGIN_TEST_METHOD(DotTest)
  349. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#DotOpTable")
  350. END_TEST_METHOD()
  351. BEGIN_TEST_METHOD(Msad4Test)
  352. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#Msad4Table")
  353. END_TEST_METHOD()
  354. BEGIN_TEST_METHOD(DenormBinaryFloatOpTest)
  355. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#DenormBinaryFloatOpTable")
  356. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  357. END_TEST_METHOD()
  358. BEGIN_TEST_METHOD(DenormTertiaryFloatOpTest)
  359. TEST_METHOD_PROPERTY(L"DataSource", L"Table:ShaderOpArithTable.xml#DenormTertiaryFloatOpTable")
  360. TEST_METHOD_PROPERTY(L"Priority", L"2") // Remove this line once warp supports this feature in Shader Model 6.2
  361. END_TEST_METHOD()
  362. BEGIN_TEST_METHOD(BarycentricsTest)
  363. TEST_METHOD_PROPERTY(L"Priority", L"2")
  364. END_TEST_METHOD()
  365. // This is defined in d3d.h for Windows 10 Anniversary Edition SDK, but we only
  366. // require the Windows 10 SDK.
  367. typedef enum D3D_SHADER_MODEL {
  368. D3D_SHADER_MODEL_5_1 = 0x51,
  369. D3D_SHADER_MODEL_6_0 = 0x60,
  370. D3D_SHADER_MODEL_6_1 = 0x61,
  371. D3D_SHADER_MODEL_6_2 = 0x62,
  372. D3D_SHADER_MODEL_6_3 = 0x63,
  373. } D3D_SHADER_MODEL;
  374. #if WDK_NTDDI_VERSION == NTDDI_WIN10_RS2
  375. static const D3D_SHADER_MODEL HIGHEST_SHADER_MODEL = D3D_SHADER_MODEL_6_0;
  376. #elif WDK_NTDDI_VERSION == NTDDI_WIN10_RS3
  377. static const D3D_SHADER_MODEL HIGHEST_SHADER_MODEL = D3D_SHADER_MODEL_6_1;
  378. #else
  379. static const D3D_SHADER_MODEL HIGHEST_SHADER_MODEL = D3D_SHADER_MODEL_6_2;
  380. #endif
  381. dxc::DxcDllSupport m_support;
  382. VersionSupportInfo m_ver;
  383. bool m_ExperimentalModeEnabled = false;
  384. const float ClearColor[4] = { 0.0f, 0.2f, 0.4f, 1.0f };
  385. // Do not remove the following line - it is used bqy TranslateExecutionTest.py
  386. // MARKER: ExecutionTest/DxilConf Shared Implementation Start
  387. bool UseDxbc() {
  388. #ifdef _HLK_CONF
  389. return false;
  390. #else
  391. return GetTestParamBool(L"DXBC");
  392. #endif
  393. }
  394. bool UseWarpByDefault() {
  395. #ifdef _HLK_CONF
  396. return false;
  397. #else
  398. return true;
  399. #endif
  400. }
  401. bool UseDebugIfaces() {
  402. return true;
  403. }
  404. bool SaveImages() {
  405. return GetTestParamBool(L"SaveImages");
  406. }
  407. template <class T1, class T2>
  408. void WaveIntrinsicsActivePrefixTest(TableParameter *pParameterList,
  409. size_t numParameter, bool isPrefix);
  410. void BasicTriangleTestSetup(LPCSTR OpName, LPCWSTR FileName, D3D_SHADER_MODEL testModel);
  411. void RunBasicShaderModelTest(D3D_SHADER_MODEL shaderModel);
  412. template <class Ty>
  413. void RunBasicShaderModelTest(CComPtr<ID3D12Device> pDevice, const char *pShaderModelStr, const char *pShader, Ty *pInputDataPairs, unsigned inputDataCount);
  414. template <class Ty>
  415. const wchar_t* BasicShaderModelTest_GetFormatString();
  416. void CompileFromText(LPCSTR pText, LPCWSTR pEntryPoint, LPCWSTR pTargetProfile, ID3DBlob **ppBlob) {
  417. VERIFY_SUCCEEDED(m_support.Initialize());
  418. CComPtr<IDxcCompiler> pCompiler;
  419. CComPtr<IDxcLibrary> pLibrary;
  420. CComPtr<IDxcBlobEncoding> pTextBlob;
  421. CComPtr<IDxcOperationResult> pResult;
  422. HRESULT resultCode;
  423. VERIFY_SUCCEEDED(m_support.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  424. VERIFY_SUCCEEDED(m_support.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  425. VERIFY_SUCCEEDED(pLibrary->CreateBlobWithEncodingFromPinned(pText, (UINT32)strlen(pText), CP_UTF8, &pTextBlob));
  426. VERIFY_SUCCEEDED(pCompiler->Compile(pTextBlob, L"hlsl.hlsl", pEntryPoint, pTargetProfile, nullptr, 0, nullptr, 0, nullptr, &pResult));
  427. VERIFY_SUCCEEDED(pResult->GetStatus(&resultCode));
  428. if (FAILED(resultCode)) {
  429. CComPtr<IDxcBlobEncoding> errors;
  430. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&errors));
  431. #ifndef _HLK_CONF
  432. LogCommentFmt(L"Failed to compile shader: %s", BlobToUtf16(errors).data());
  433. #endif
  434. }
  435. VERIFY_SUCCEEDED(resultCode);
  436. VERIFY_SUCCEEDED(pResult->GetResult((IDxcBlob **)ppBlob));
  437. }
  438. void CreateComputeCommandQueue(ID3D12Device *pDevice, LPCWSTR pName, ID3D12CommandQueue **ppCommandQueue) {
  439. D3D12_COMMAND_QUEUE_DESC queueDesc = {};
  440. queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  441. queueDesc.Type = D3D12_COMMAND_LIST_TYPE_COMPUTE;
  442. VERIFY_SUCCEEDED(pDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(ppCommandQueue)));
  443. VERIFY_SUCCEEDED((*ppCommandQueue)->SetName(pName));
  444. }
  445. void CreateComputePSO(ID3D12Device *pDevice, ID3D12RootSignature *pRootSignature, LPCSTR pShader, ID3D12PipelineState **ppComputeState) {
  446. CComPtr<ID3DBlob> pComputeShader;
  447. // Load and compile shaders.
  448. if (UseDxbc()) {
  449. #ifndef _HLK_CONF
  450. DXBCFromText(pShader, L"main", L"cs_6_0", &pComputeShader);
  451. #endif
  452. }
  453. else {
  454. CompileFromText(pShader, L"main", L"cs_6_0", &pComputeShader);
  455. }
  456. // Describe and create the compute pipeline state object (PSO).
  457. D3D12_COMPUTE_PIPELINE_STATE_DESC computePsoDesc = {};
  458. computePsoDesc.pRootSignature = pRootSignature;
  459. computePsoDesc.CS = CD3DX12_SHADER_BYTECODE(pComputeShader);
  460. VERIFY_SUCCEEDED(pDevice->CreateComputePipelineState(&computePsoDesc, IID_PPV_ARGS(ppComputeState)));
  461. }
  462. bool CreateDevice(_COM_Outptr_ ID3D12Device **ppDevice,
  463. D3D_SHADER_MODEL testModel = D3D_SHADER_MODEL_6_0) {
  464. if (testModel > HIGHEST_SHADER_MODEL) {
  465. UINT minor = (UINT)testModel & 0x0f;
  466. LogCommentFmt(L"Installed SDK does not support "
  467. L"shader model 6.%1u", minor);
  468. WEX::Logging::Log::Result(WEX::Logging::TestResults::Skipped);
  469. return false;
  470. }
  471. const D3D_FEATURE_LEVEL FeatureLevelRequired = D3D_FEATURE_LEVEL_11_0;
  472. CComPtr<IDXGIFactory4> factory;
  473. CComPtr<ID3D12Device> pDevice;
  474. *ppDevice = nullptr;
  475. VERIFY_SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)));
  476. if (GetTestParamUseWARP(UseWarpByDefault())) {
  477. CComPtr<IDXGIAdapter> warpAdapter;
  478. VERIFY_SUCCEEDED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warpAdapter)));
  479. HRESULT createHR = D3D12CreateDevice(warpAdapter, FeatureLevelRequired,
  480. IID_PPV_ARGS(&pDevice));
  481. if (FAILED(createHR)) {
  482. LogCommentFmt(L"The available version of WARP does not support d3d12.");
  483. WEX::Logging::Log::Result(WEX::Logging::TestResults::Skipped);
  484. return false;
  485. }
  486. } else {
  487. CComPtr<IDXGIAdapter1> hardwareAdapter;
  488. WEX::Common::String AdapterValue;
  489. HRESULT hr = WEX::TestExecution::RuntimeParameters::TryGetValue(L"Adapter",
  490. AdapterValue);
  491. if (SUCCEEDED(hr)) {
  492. GetHardwareAdapter(factory, AdapterValue, &hardwareAdapter);
  493. } else {
  494. WEX::Logging::Log::Comment(
  495. L"Using default hardware adapter with D3D12 support.");
  496. }
  497. VERIFY_SUCCEEDED(D3D12CreateDevice(hardwareAdapter, FeatureLevelRequired,
  498. IID_PPV_ARGS(&pDevice)));
  499. }
  500. // retrieve adapter information
  501. LUID adapterID = pDevice->GetAdapterLuid();
  502. CComPtr<IDXGIAdapter> adapter;
  503. factory->EnumAdapterByLuid(adapterID, IID_PPV_ARGS(&adapter));
  504. DXGI_ADAPTER_DESC AdapterDesc;
  505. VERIFY_SUCCEEDED(adapter->GetDesc(&AdapterDesc));
  506. LogCommentFmt(L"Using Adapter:%s", AdapterDesc.Description);
  507. if (pDevice == nullptr)
  508. return false;
  509. if (!UseDxbc()) {
  510. // Check for DXIL support.
  511. typedef struct D3D12_FEATURE_DATA_SHADER_MODEL {
  512. _Inout_ D3D_SHADER_MODEL HighestShaderModel;
  513. } D3D12_FEATURE_DATA_SHADER_MODEL;
  514. const UINT D3D12_FEATURE_SHADER_MODEL = 7;
  515. D3D12_FEATURE_DATA_SHADER_MODEL SMData;
  516. SMData.HighestShaderModel = testModel;
  517. VERIFY_SUCCEEDED(pDevice->CheckFeatureSupport(
  518. (D3D12_FEATURE)D3D12_FEATURE_SHADER_MODEL, &SMData, sizeof(SMData)));
  519. if (SMData.HighestShaderModel < testModel) {
  520. UINT minor = (UINT)testModel & 0x0f;
  521. LogCommentFmt(L"The selected device does not support "
  522. L"shader model 6.%1u", minor);
  523. WEX::Logging::Log::Result(WEX::Logging::TestResults::Skipped);
  524. return false;
  525. }
  526. }
  527. if (UseDebugIfaces()) {
  528. CComPtr<ID3D12InfoQueue> pInfoQueue;
  529. if (SUCCEEDED(pDevice->QueryInterface(&pInfoQueue))) {
  530. pInfoQueue->SetMuteDebugOutput(FALSE);
  531. }
  532. }
  533. *ppDevice = pDevice.Detach();
  534. return true;
  535. }
  536. void CreateGraphicsCommandQueue(ID3D12Device *pDevice, ID3D12CommandQueue **ppCommandQueue) {
  537. D3D12_COMMAND_QUEUE_DESC queueDesc = {};
  538. queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  539. queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;;
  540. VERIFY_SUCCEEDED(pDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(ppCommandQueue)));
  541. }
  542. void CreateGraphicsCommandQueueAndList(
  543. ID3D12Device *pDevice, ID3D12CommandQueue **ppCommandQueue,
  544. ID3D12CommandAllocator **ppAllocator,
  545. ID3D12GraphicsCommandList **ppCommandList, ID3D12PipelineState *pPSO) {
  546. CreateGraphicsCommandQueue(pDevice, ppCommandQueue);
  547. VERIFY_SUCCEEDED(pDevice->CreateCommandAllocator(
  548. D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(ppAllocator)));
  549. VERIFY_SUCCEEDED(pDevice->CreateCommandList(
  550. 0, D3D12_COMMAND_LIST_TYPE_DIRECT, *ppAllocator, pPSO,
  551. IID_PPV_ARGS(ppCommandList)));
  552. }
  553. void CreateGraphicsPSO(ID3D12Device *pDevice,
  554. D3D12_INPUT_LAYOUT_DESC *pInputLayout,
  555. ID3D12RootSignature *pRootSignature, LPCSTR pShaders,
  556. ID3D12PipelineState **ppPSO) {
  557. CComPtr<ID3DBlob> vertexShader;
  558. CComPtr<ID3DBlob> pixelShader;
  559. if (UseDxbc()) {
  560. #ifndef _HLK_CONF
  561. DXBCFromText(pShaders, L"VSMain", L"vs_6_0", &vertexShader);
  562. DXBCFromText(pShaders, L"PSMain", L"ps_6_0", &pixelShader);
  563. #endif
  564. } else {
  565. CompileFromText(pShaders, L"VSMain", L"vs_6_0", &vertexShader);
  566. CompileFromText(pShaders, L"PSMain", L"ps_6_0", &pixelShader);
  567. }
  568. // Describe and create the graphics pipeline state object (PSO).
  569. D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
  570. psoDesc.InputLayout = *pInputLayout;
  571. psoDesc.pRootSignature = pRootSignature;
  572. psoDesc.VS = CD3DX12_SHADER_BYTECODE(vertexShader);
  573. psoDesc.PS = CD3DX12_SHADER_BYTECODE(pixelShader);
  574. psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
  575. psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
  576. psoDesc.DepthStencilState.DepthEnable = FALSE;
  577. psoDesc.DepthStencilState.StencilEnable = FALSE;
  578. psoDesc.SampleMask = UINT_MAX;
  579. psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
  580. psoDesc.NumRenderTargets = 1;
  581. psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
  582. psoDesc.SampleDesc.Count = 1;
  583. VERIFY_SUCCEEDED(
  584. pDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(ppPSO)));
  585. }
  586. void CreateRenderTargetAndReadback(ID3D12Device *pDevice,
  587. ID3D12DescriptorHeap *pHeap, UINT width,
  588. UINT height,
  589. ID3D12Resource **ppRenderTarget,
  590. ID3D12Resource **ppBuffer) {
  591. const DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
  592. const size_t formatElementSize = 4;
  593. CComPtr<ID3D12Resource> pRenderTarget;
  594. CComPtr<ID3D12Resource> pBuffer;
  595. CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(
  596. pHeap->GetCPUDescriptorHandleForHeapStart());
  597. CD3DX12_HEAP_PROPERTIES rtHeap(D3D12_HEAP_TYPE_DEFAULT);
  598. CD3DX12_RESOURCE_DESC rtDesc(
  599. CD3DX12_RESOURCE_DESC::Tex2D(format, width, height));
  600. CD3DX12_CLEAR_VALUE rtClearVal(format, ClearColor);
  601. rtDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
  602. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  603. &rtHeap, D3D12_HEAP_FLAG_NONE, &rtDesc, D3D12_RESOURCE_STATE_COPY_DEST,
  604. &rtClearVal, IID_PPV_ARGS(&pRenderTarget)));
  605. pDevice->CreateRenderTargetView(pRenderTarget, nullptr, rtvHandle);
  606. // rtvHandle.Offset(1, rtvDescriptorSize); // Not needed for a single
  607. // resource.
  608. CD3DX12_HEAP_PROPERTIES readHeap(D3D12_HEAP_TYPE_READBACK);
  609. CD3DX12_RESOURCE_DESC readDesc(
  610. CD3DX12_RESOURCE_DESC::Buffer(width * height * formatElementSize));
  611. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  612. &readHeap, D3D12_HEAP_FLAG_NONE, &readDesc,
  613. D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&pBuffer)));
  614. *ppRenderTarget = pRenderTarget.Detach();
  615. *ppBuffer = pBuffer.Detach();
  616. }
  617. void CreateRootSignatureFromDesc(ID3D12Device *pDevice,
  618. const D3D12_ROOT_SIGNATURE_DESC *pDesc,
  619. ID3D12RootSignature **pRootSig) {
  620. CComPtr<ID3DBlob> signature;
  621. CComPtr<ID3DBlob> error;
  622. VERIFY_SUCCEEDED(D3D12SerializeRootSignature(pDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
  623. VERIFY_SUCCEEDED(pDevice->CreateRootSignature(
  624. 0, signature->GetBufferPointer(), signature->GetBufferSize(),
  625. IID_PPV_ARGS(pRootSig)));
  626. }
  627. void CreateRtvDescriptorHeap(ID3D12Device *pDevice, UINT numDescriptors,
  628. ID3D12DescriptorHeap **pRtvHeap, UINT *rtvDescriptorSize) {
  629. D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
  630. rtvHeapDesc.NumDescriptors = numDescriptors;
  631. rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
  632. rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
  633. VERIFY_SUCCEEDED(
  634. pDevice->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(pRtvHeap)));
  635. if (rtvDescriptorSize != nullptr) {
  636. *rtvDescriptorSize = pDevice->GetDescriptorHandleIncrementSize(
  637. D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  638. }
  639. }
  640. void CreateTestUavs(ID3D12Device *pDevice,
  641. ID3D12GraphicsCommandList *pCommandList, LPCVOID values,
  642. UINT32 valueSizeInBytes, ID3D12Resource **ppUavResource,
  643. ID3D12Resource **ppReadBuffer,
  644. ID3D12Resource **ppUploadResource) {
  645. CComPtr<ID3D12Resource> pUavResource;
  646. CComPtr<ID3D12Resource> pReadBuffer;
  647. CComPtr<ID3D12Resource> pUploadResource;
  648. D3D12_SUBRESOURCE_DATA transferData;
  649. D3D12_HEAP_PROPERTIES defaultHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
  650. D3D12_HEAP_PROPERTIES uploadHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
  651. D3D12_RESOURCE_DESC bufferDesc = CD3DX12_RESOURCE_DESC::Buffer(valueSizeInBytes, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
  652. D3D12_RESOURCE_DESC uploadBufferDesc = CD3DX12_RESOURCE_DESC::Buffer(valueSizeInBytes);
  653. CD3DX12_HEAP_PROPERTIES readHeap(D3D12_HEAP_TYPE_READBACK);
  654. CD3DX12_RESOURCE_DESC readDesc(CD3DX12_RESOURCE_DESC::Buffer(valueSizeInBytes));
  655. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  656. &defaultHeapProperties,
  657. D3D12_HEAP_FLAG_NONE,
  658. &bufferDesc,
  659. D3D12_RESOURCE_STATE_COPY_DEST,
  660. nullptr,
  661. IID_PPV_ARGS(&pUavResource)));
  662. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  663. &uploadHeapProperties,
  664. D3D12_HEAP_FLAG_NONE,
  665. &uploadBufferDesc,
  666. D3D12_RESOURCE_STATE_GENERIC_READ,
  667. nullptr,
  668. IID_PPV_ARGS(&pUploadResource)));
  669. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  670. &readHeap, D3D12_HEAP_FLAG_NONE, &readDesc,
  671. D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&pReadBuffer)));
  672. transferData.pData = values;
  673. transferData.RowPitch = valueSizeInBytes;
  674. transferData.SlicePitch = transferData.RowPitch;
  675. UpdateSubresources<1>(pCommandList, pUavResource.p, pUploadResource.p, 0, 0, 1, &transferData);
  676. RecordTransitionBarrier(pCommandList, pUavResource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
  677. *ppUavResource = pUavResource.Detach();
  678. *ppReadBuffer = pReadBuffer.Detach();
  679. *ppUploadResource = pUploadResource.Detach();
  680. }
  681. template <typename TVertex, int len>
  682. void CreateVertexBuffer(ID3D12Device *pDevice, TVertex(&vertices)[len],
  683. ID3D12Resource **ppVertexBuffer,
  684. D3D12_VERTEX_BUFFER_VIEW *pVertexBufferView) {
  685. size_t vertexBufferSize = sizeof(vertices);
  686. CComPtr<ID3D12Resource> pVertexBuffer;
  687. CD3DX12_HEAP_PROPERTIES heapProps(D3D12_HEAP_TYPE_UPLOAD);
  688. CD3DX12_RESOURCE_DESC bufferDesc(
  689. CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize));
  690. VERIFY_SUCCEEDED(pDevice->CreateCommittedResource(
  691. &heapProps, D3D12_HEAP_FLAG_NONE, &bufferDesc,
  692. D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
  693. IID_PPV_ARGS(&pVertexBuffer)));
  694. UINT8 *pVertexDataBegin;
  695. CD3DX12_RANGE readRange(0, 0);
  696. VERIFY_SUCCEEDED(pVertexBuffer->Map(
  697. 0, &readRange, reinterpret_cast<void **>(&pVertexDataBegin)));
  698. memcpy(pVertexDataBegin, vertices, vertexBufferSize);
  699. pVertexBuffer->Unmap(0, nullptr);
  700. // Initialize the vertex buffer view.
  701. pVertexBufferView->BufferLocation = pVertexBuffer->GetGPUVirtualAddress();
  702. pVertexBufferView->StrideInBytes = sizeof(TVertex);
  703. pVertexBufferView->SizeInBytes = (UINT)vertexBufferSize;
  704. *ppVertexBuffer = pVertexBuffer.Detach();
  705. }
  706. // Requires Anniversary Edition headers, so simplifying things for current setup.
  707. const UINT D3D12_FEATURE_D3D12_OPTIONS1 = 8;
  708. struct D3D12_FEATURE_DATA_D3D12_OPTIONS1 {
  709. BOOL WaveOps;
  710. UINT WaveLaneCountMin;
  711. UINT WaveLaneCountMax;
  712. UINT TotalLaneCount;
  713. BOOL ExpandedComputeResourceStates;
  714. BOOL Int64ShaderOps;
  715. };
  716. bool DoesDeviceSupportInt64(ID3D12Device *pDevice) {
  717. D3D12_FEATURE_DATA_D3D12_OPTIONS1 O;
  718. if (FAILED(pDevice->CheckFeatureSupport((D3D12_FEATURE)D3D12_FEATURE_D3D12_OPTIONS1, &O, sizeof(O))))
  719. return false;
  720. return O.Int64ShaderOps != FALSE;
  721. }
  722. bool DoesDeviceSupportDouble(ID3D12Device *pDevice) {
  723. D3D12_FEATURE_DATA_D3D12_OPTIONS O;
  724. if (FAILED(pDevice->CheckFeatureSupport((D3D12_FEATURE)D3D12_FEATURE_D3D12_OPTIONS, &O, sizeof(O))))
  725. return false;
  726. return O.DoublePrecisionFloatShaderOps != FALSE;
  727. }
  728. bool DoesDeviceSupportWaveOps(ID3D12Device *pDevice) {
  729. D3D12_FEATURE_DATA_D3D12_OPTIONS1 O;
  730. if (FAILED(pDevice->CheckFeatureSupport((D3D12_FEATURE)D3D12_FEATURE_D3D12_OPTIONS1, &O, sizeof(O))))
  731. return false;
  732. return O.WaveOps != FALSE;
  733. }
  734. #ifndef _HLK_CONF
  735. void DXBCFromText(LPCSTR pText, LPCWSTR pEntryPoint, LPCWSTR pTargetProfile, ID3DBlob **ppBlob) {
  736. CW2A pEntryPointA(pEntryPoint, CP_UTF8);
  737. CW2A pTargetProfileA(pTargetProfile, CP_UTF8);
  738. CComPtr<ID3DBlob> pErrors;
  739. D3D_SHADER_MACRO d3dMacro[2];
  740. ZeroMemory(d3dMacro, sizeof(d3dMacro));
  741. d3dMacro[0].Definition = "1";
  742. d3dMacro[0].Name = "USING_DXBC";
  743. HRESULT hr = D3DCompile(pText, strlen(pText), "hlsl.hlsl", d3dMacro, nullptr, pEntryPointA, pTargetProfileA, 0, 0, ppBlob, &pErrors);
  744. if (pErrors != nullptr) {
  745. CA2W errors((char *)pErrors->GetBufferPointer(), CP_ACP);
  746. LogCommentFmt(L"Compilation failure: %s", errors.m_szBuffer);
  747. }
  748. VERIFY_SUCCEEDED(hr);
  749. }
  750. #endif
  751. HRESULT EnableDebugLayer() {
  752. // The debug layer does net yet validate DXIL programs that require rewriting,
  753. // but basic logging should work properly.
  754. HRESULT hr = S_FALSE;
  755. if (UseDebugIfaces()) {
  756. CComPtr<ID3D12Debug> debugController;
  757. hr = D3D12GetDebugInterface(IID_PPV_ARGS(&debugController));
  758. if (SUCCEEDED(hr)) {
  759. debugController->EnableDebugLayer();
  760. hr = S_OK;
  761. }
  762. }
  763. return hr;
  764. }
  765. #ifndef _HLK_CONF
  766. HRESULT EnableExperimentalMode() {
  767. if (m_ExperimentalModeEnabled) {
  768. return S_OK;
  769. }
  770. if (!GetTestParamBool(L"ExperimentalShaders")) {
  771. return S_FALSE;
  772. }
  773. HRESULT hr = EnableExperimentalShaderModels();
  774. if (SUCCEEDED(hr)) {
  775. m_ExperimentalModeEnabled = true;
  776. }
  777. return hr;
  778. }
  779. #endif
  780. struct FenceObj {
  781. HANDLE m_fenceEvent = NULL;
  782. CComPtr<ID3D12Fence> m_fence;
  783. UINT64 m_fenceValue;
  784. ~FenceObj() {
  785. if (m_fenceEvent) CloseHandle(m_fenceEvent);
  786. }
  787. };
  788. void InitFenceObj(ID3D12Device *pDevice, FenceObj *pObj) {
  789. pObj->m_fenceValue = 1;
  790. VERIFY_SUCCEEDED(pDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE,
  791. IID_PPV_ARGS(&pObj->m_fence)));
  792. // Create an event handle to use for frame synchronization.
  793. pObj->m_fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
  794. if (pObj->m_fenceEvent == nullptr) {
  795. VERIFY_SUCCEEDED(HRESULT_FROM_WIN32(GetLastError()));
  796. }
  797. }
  798. void ReadHlslDataIntoNewStream(LPCWSTR relativePath, IStream **ppStream) {
  799. VERIFY_SUCCEEDED(m_support.Initialize());
  800. CComPtr<IDxcLibrary> pLibrary;
  801. CComPtr<IDxcBlobEncoding> pBlob;
  802. CComPtr<IStream> pStream;
  803. std::wstring path = GetPathToHlslDataFile(relativePath);
  804. VERIFY_SUCCEEDED(m_support.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  805. VERIFY_SUCCEEDED(pLibrary->CreateBlobFromFile(path.c_str(), nullptr, &pBlob));
  806. VERIFY_SUCCEEDED(pLibrary->CreateStreamFromBlobReadOnly(pBlob, &pStream));
  807. *ppStream = pStream.Detach();
  808. }
  809. void RecordRenderAndReadback(ID3D12GraphicsCommandList *pList,
  810. ID3D12DescriptorHeap *pRtvHeap,
  811. UINT rtvDescriptorSize,
  812. UINT instanceCount,
  813. D3D12_VERTEX_BUFFER_VIEW *pVertexBufferView,
  814. ID3D12RootSignature *pRootSig,
  815. ID3D12Resource *pRenderTarget,
  816. ID3D12Resource *pReadBuffer) {
  817. D3D12_RESOURCE_DESC rtDesc = pRenderTarget->GetDesc();
  818. D3D12_VIEWPORT viewport;
  819. D3D12_RECT scissorRect;
  820. memset(&viewport, 0, sizeof(viewport));
  821. viewport.Height = (float)rtDesc.Height;
  822. viewport.Width = (float)rtDesc.Width;
  823. viewport.MaxDepth = 1.0f;
  824. memset(&scissorRect, 0, sizeof(scissorRect));
  825. scissorRect.right = (long)rtDesc.Width;
  826. scissorRect.bottom = rtDesc.Height;
  827. if (pRootSig != nullptr) {
  828. pList->SetGraphicsRootSignature(pRootSig);
  829. }
  830. pList->RSSetViewports(1, &viewport);
  831. pList->RSSetScissorRects(1, &scissorRect);
  832. // Indicate that the buffer will be used as a render target.
  833. RecordTransitionBarrier(pList, pRenderTarget, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_RENDER_TARGET);
  834. CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(pRtvHeap->GetCPUDescriptorHandleForHeapStart(), 0, rtvDescriptorSize);
  835. pList->OMSetRenderTargets(1, &rtvHandle, FALSE, nullptr);
  836. pList->ClearRenderTargetView(rtvHandle, ClearColor, 0, nullptr);
  837. pList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  838. pList->IASetVertexBuffers(0, 1, pVertexBufferView);
  839. pList->DrawInstanced(3, instanceCount, 0, 0);
  840. // Transition to copy source and copy into read-back buffer.
  841. RecordTransitionBarrier(pList, pRenderTarget, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
  842. // Copy into read-back buffer.
  843. UINT64 rowPitch = rtDesc.Width * 4;
  844. if (rowPitch % D3D12_TEXTURE_DATA_PITCH_ALIGNMENT)
  845. rowPitch += D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - (rowPitch % D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
  846. D3D12_PLACED_SUBRESOURCE_FOOTPRINT Footprint;
  847. Footprint.Offset = 0;
  848. Footprint.Footprint = CD3DX12_SUBRESOURCE_FOOTPRINT(DXGI_FORMAT_R8G8B8A8_UNORM, (UINT)rtDesc.Width, rtDesc.Height, 1, (UINT)rowPitch);
  849. CD3DX12_TEXTURE_COPY_LOCATION DstLoc(pReadBuffer, Footprint);
  850. CD3DX12_TEXTURE_COPY_LOCATION SrcLoc(pRenderTarget, 0);
  851. pList->CopyTextureRegion(&DstLoc, 0, 0, 0, &SrcLoc, nullptr);
  852. }
  853. void RunRWByteBufferComputeTest(ID3D12Device *pDevice, LPCSTR shader, std::vector<uint32_t> &values);
  854. void SetDescriptorHeap(ID3D12GraphicsCommandList *pCommandList, ID3D12DescriptorHeap *pHeap) {
  855. ID3D12DescriptorHeap *const pHeaps[1] = { pHeap };
  856. pCommandList->SetDescriptorHeaps(1, pHeaps);
  857. }
  858. void WaitForSignal(ID3D12CommandQueue *pCQ, FenceObj &FO) {
  859. ::WaitForSignal(pCQ, FO.m_fence, FO.m_fenceEvent, FO.m_fenceValue++);
  860. }
  861. };
  862. #define WAVE_INTRINSIC_DXBC_GUARD \
  863. "#ifdef USING_DXBC\r\n" \
  864. "uint WaveGetLaneIndex() { return 1; }\r\n" \
  865. "uint WaveReadLaneFirst(uint u) { return u; }\r\n" \
  866. "bool WaveIsFirstLane() { return true; }\r\n" \
  867. "uint WaveGetLaneCount() { return 1; }\r\n" \
  868. "uint WaveReadLaneAt(uint n, uint u) { return u; }\r\n" \
  869. "bool WaveActiveAnyTrue(bool b) { return b; }\r\n" \
  870. "bool WaveActiveAllTrue(bool b) { return false; }\r\n" \
  871. "uint WaveActiveAllEqual(uint u) { return u; }\r\n" \
  872. "uint4 WaveActiveBallot(bool b) { return 1; }\r\n" \
  873. "uint WaveActiveCountBits(uint u) { return 1; }\r\n" \
  874. "uint WaveActiveSum(uint u) { return 1; }\r\n" \
  875. "uint WaveActiveProduct(uint u) { return 1; }\r\n" \
  876. "uint WaveActiveBitAnd(uint u) { return 1; }\r\n" \
  877. "uint WaveActiveBitOr(uint u) { return 1; }\r\n" \
  878. "uint WaveActiveBitXor(uint u) { return 1; }\r\n" \
  879. "uint WaveActiveMin(uint u) { return 1; }\r\n" \
  880. "uint WaveActiveMax(uint u) { return 1; }\r\n" \
  881. "uint WavePrefixCountBits(uint u) { return 1; }\r\n" \
  882. "uint WavePrefixSum(uint u) { return 1; }\r\n" \
  883. "uint WavePrefixProduct(uint u) { return 1; }\r\n" \
  884. "uint QuadReadLaneAt(uint a, uint u) { return 1; }\r\n" \
  885. "uint QuadReadAcrossX(uint u) { return 1; }\r\n" \
  886. "uint QuadReadAcrossY(uint u) { return 1; }\r\n" \
  887. "uint QuadReadAcrossDiagonal(uint u) { return 1; }\r\n" \
  888. "#endif\r\n"
  889. static void SetupComputeValuePattern(std::vector<uint32_t> &values,
  890. size_t count) {
  891. values.resize(count); // one element per dispatch group, in bytes
  892. for (size_t i = 0; i < count; ++i) {
  893. values[i] = (uint32_t)i;
  894. }
  895. }
  896. bool ExecutionTest::ExecutionTestClassSetup() {
  897. #ifdef _HLK_CONF
  898. // TODO: Enabling the D3D driver verifier. Check out the logic in the D3DConf_12_Core test.
  899. VERIFY_SUCCEEDED(m_support.Initialize());
  900. m_UseWarp = hlsl_test::GetTestParamUseWARP(false);
  901. m_EnableDebugLayer = hlsl_test::GetTestParamBool(L"DebugLayer");
  902. if (m_EnableDebugLayer) {
  903. EnableDebugLayer();
  904. }
  905. return true;
  906. #else
  907. HRESULT hr = EnableExperimentalMode();
  908. if (FAILED(hr)) {
  909. LogCommentFmt(L"Unable to enable shader experimental mode - 0x%08x.", hr);
  910. }
  911. else if (hr == S_FALSE) {
  912. LogCommentFmt(L"Experimental mode not enabled.");
  913. }
  914. else {
  915. LogCommentFmt(L"Experimental mode enabled.");
  916. }
  917. hr = EnableDebugLayer();
  918. if (FAILED(hr)) {
  919. LogCommentFmt(L"Unable to enable debug layer - 0x%08x.", hr);
  920. }
  921. else {
  922. LogCommentFmt(L"Debug layer enabled.");
  923. }
  924. return true;
  925. #endif
  926. }
  927. void ExecutionTest::RunRWByteBufferComputeTest(ID3D12Device *pDevice, LPCSTR pShader, std::vector<uint32_t> &values) {
  928. static const int DispatchGroupX = 1;
  929. static const int DispatchGroupY = 1;
  930. static const int DispatchGroupZ = 1;
  931. CComPtr<ID3D12GraphicsCommandList> pCommandList;
  932. CComPtr<ID3D12CommandQueue> pCommandQueue;
  933. CComPtr<ID3D12DescriptorHeap> pUavHeap;
  934. CComPtr<ID3D12CommandAllocator> pCommandAllocator;
  935. UINT uavDescriptorSize;
  936. FenceObj FO;
  937. const UINT valueSizeInBytes = (UINT)values.size() * sizeof(uint32_t);
  938. CreateComputeCommandQueue(pDevice, L"RunRWByteBufferComputeTest Command Queue", &pCommandQueue);
  939. InitFenceObj(pDevice, &FO);
  940. // Describe and create a UAV descriptor heap.
  941. D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
  942. heapDesc.NumDescriptors = 1;
  943. heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  944. heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  945. VERIFY_SUCCEEDED(pDevice->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&pUavHeap)));
  946. uavDescriptorSize = pDevice->GetDescriptorHandleIncrementSize(heapDesc.Type);
  947. // Create root signature.
  948. CComPtr<ID3D12RootSignature> pRootSignature;
  949. {
  950. CD3DX12_DESCRIPTOR_RANGE ranges[1];
  951. ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 0, 0, 0);
  952. CD3DX12_ROOT_PARAMETER rootParameters[1];
  953. rootParameters[0].InitAsDescriptorTable(1, &ranges[0], D3D12_SHADER_VISIBILITY_ALL);
  954. CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
  955. rootSignatureDesc.Init(_countof(rootParameters), rootParameters, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE);
  956. CreateRootSignatureFromDesc(pDevice, &rootSignatureDesc, &pRootSignature);
  957. }
  958. // Create pipeline state object.
  959. CComPtr<ID3D12PipelineState> pComputeState;
  960. CreateComputePSO(pDevice, pRootSignature, pShader, &pComputeState);
  961. // Create a command allocator and list for compute.
  962. VERIFY_SUCCEEDED(pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE, IID_PPV_ARGS(&pCommandAllocator)));
  963. VERIFY_SUCCEEDED(pDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, pCommandAllocator, pComputeState, IID_PPV_ARGS(&pCommandList)));
  964. pCommandList->SetName(L"ExecutionTest::RunRWByteButterComputeTest Command List");
  965. // Set up UAV resource.
  966. CComPtr<ID3D12Resource> pUavResource;
  967. CComPtr<ID3D12Resource> pReadBuffer;
  968. CComPtr<ID3D12Resource> pUploadResource;
  969. CreateTestUavs(pDevice, pCommandList, values.data(), valueSizeInBytes, &pUavResource, &pReadBuffer, &pUploadResource);
  970. VERIFY_SUCCEEDED(pUavResource->SetName(L"RunRWByteBufferComputeText UAV"));
  971. VERIFY_SUCCEEDED(pReadBuffer->SetName(L"RunRWByteBufferComputeText UAV Read Buffer"));
  972. VERIFY_SUCCEEDED(pUploadResource->SetName(L"RunRWByteBufferComputeText UAV Upload Buffer"));
  973. // Close the command list and execute it to perform the GPU setup.
  974. pCommandList->Close();
  975. ExecuteCommandList(pCommandQueue, pCommandList);
  976. WaitForSignal(pCommandQueue, FO);
  977. VERIFY_SUCCEEDED(pCommandAllocator->Reset());
  978. VERIFY_SUCCEEDED(pCommandList->Reset(pCommandAllocator, pComputeState));
  979. // Run the compute shader and copy the results back to readable memory.
  980. {
  981. D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
  982. uavDesc.Format = DXGI_FORMAT_R32_TYPELESS;
  983. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
  984. uavDesc.Buffer.FirstElement = 0;
  985. uavDesc.Buffer.NumElements = (UINT)values.size();
  986. uavDesc.Buffer.StructureByteStride = 0;
  987. uavDesc.Buffer.CounterOffsetInBytes = 0;
  988. uavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW;
  989. CD3DX12_CPU_DESCRIPTOR_HANDLE uavHandle(pUavHeap->GetCPUDescriptorHandleForHeapStart());
  990. CD3DX12_GPU_DESCRIPTOR_HANDLE uavHandleGpu(pUavHeap->GetGPUDescriptorHandleForHeapStart());
  991. pDevice->CreateUnorderedAccessView(pUavResource, nullptr, &uavDesc, uavHandle);
  992. SetDescriptorHeap(pCommandList, pUavHeap);
  993. pCommandList->SetComputeRootSignature(pRootSignature);
  994. pCommandList->SetComputeRootDescriptorTable(0, uavHandleGpu);
  995. }
  996. pCommandList->Dispatch(DispatchGroupX, DispatchGroupY, DispatchGroupZ);
  997. RecordTransitionBarrier(pCommandList, pUavResource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
  998. pCommandList->CopyResource(pReadBuffer, pUavResource);
  999. pCommandList->Close();
  1000. ExecuteCommandList(pCommandQueue, pCommandList);
  1001. WaitForSignal(pCommandQueue, FO);
  1002. {
  1003. MappedData mappedData(pReadBuffer, valueSizeInBytes);
  1004. uint32_t *pData = (uint32_t *)mappedData.data();
  1005. memcpy(values.data(), pData, (size_t)valueSizeInBytes);
  1006. }
  1007. WaitForSignal(pCommandQueue, FO);
  1008. }
  1009. TEST_F(ExecutionTest, BasicComputeTest) {
  1010. #ifndef _HLK_CONF
  1011. //
  1012. // BasicComputeTest is a simple compute shader that can be used as the basis
  1013. // for more interesting compute execution tests.
  1014. // The HLSL is compatible with shader models <=5.1 to allow using the DXBC
  1015. // rendering code paths for comparison.
  1016. //
  1017. static const char pShader[] =
  1018. "RWByteAddressBuffer g_bab : register(u0);\r\n"
  1019. "[numthreads(8,8,1)]\r\n"
  1020. "void main(uint GI : SV_GroupIndex) {"
  1021. " uint addr = GI * 4;\r\n"
  1022. " uint val = g_bab.Load(addr);\r\n"
  1023. " DeviceMemoryBarrierWithGroupSync();\r\n"
  1024. " g_bab.Store(addr, val + 1);\r\n"
  1025. "}";
  1026. static const int NumThreadsX = 8;
  1027. static const int NumThreadsY = 8;
  1028. static const int NumThreadsZ = 1;
  1029. static const int ThreadsPerGroup = NumThreadsX * NumThreadsY * NumThreadsZ;
  1030. static const int DispatchGroupCount = 1;
  1031. CComPtr<ID3D12Device> pDevice;
  1032. if (!CreateDevice(&pDevice))
  1033. return;
  1034. std::vector<uint32_t> values;
  1035. SetupComputeValuePattern(values, ThreadsPerGroup * DispatchGroupCount);
  1036. VERIFY_ARE_EQUAL(values[0], (uint32_t)0);
  1037. RunRWByteBufferComputeTest(pDevice, pShader, values);
  1038. VERIFY_ARE_EQUAL(values[0], (uint32_t)1);
  1039. #endif
  1040. }
  1041. TEST_F(ExecutionTest, BasicTriangleTest) {
  1042. #ifndef _HLK_CONF
  1043. static const UINT FrameCount = 2;
  1044. static const UINT m_width = 320;
  1045. static const UINT m_height = 200;
  1046. static const float m_aspectRatio = static_cast<float>(m_width) / static_cast<float>(m_height);
  1047. struct Vertex {
  1048. XMFLOAT3 position;
  1049. XMFLOAT4 color;
  1050. };
  1051. // Pipeline objects.
  1052. CComPtr<ID3D12Device> pDevice;
  1053. CComPtr<ID3D12Resource> pRenderTarget;
  1054. CComPtr<ID3D12CommandAllocator> pCommandAllocator;
  1055. CComPtr<ID3D12CommandQueue> pCommandQueue;
  1056. CComPtr<ID3D12RootSignature> pRootSig;
  1057. CComPtr<ID3D12DescriptorHeap> pRtvHeap;
  1058. CComPtr<ID3D12PipelineState> pPipelineState;
  1059. CComPtr<ID3D12GraphicsCommandList> pCommandList;
  1060. CComPtr<ID3D12Resource> pReadBuffer;
  1061. UINT rtvDescriptorSize;
  1062. CComPtr<ID3D12Resource> pVertexBuffer;
  1063. D3D12_VERTEX_BUFFER_VIEW vertexBufferView;
  1064. // Synchronization objects.
  1065. FenceObj FO;
  1066. // Shaders.
  1067. static const char pShaders[] =
  1068. "struct PSInput {\r\n"
  1069. " float4 position : SV_POSITION;\r\n"
  1070. " float4 color : COLOR;\r\n"
  1071. "};\r\n\r\n"
  1072. "PSInput VSMain(float4 position : POSITION, float4 color : COLOR) {\r\n"
  1073. " PSInput result;\r\n"
  1074. "\r\n"
  1075. " result.position = position;\r\n"
  1076. " result.color = color;\r\n"
  1077. " return result;\r\n"
  1078. "}\r\n\r\n"
  1079. "float4 PSMain(PSInput input) : SV_TARGET {\r\n"
  1080. " return 1; //input.color;\r\n"
  1081. "};\r\n";
  1082. if (!CreateDevice(&pDevice))
  1083. return;
  1084. struct BasicTestChecker {
  1085. CComPtr<ID3D12Device> m_pDevice;
  1086. CComPtr<ID3D12InfoQueue> m_pInfoQueue;
  1087. bool m_OK = false;
  1088. void SetOK(bool value) { m_OK = value; }
  1089. BasicTestChecker(ID3D12Device *pDevice) : m_pDevice(pDevice) {
  1090. if (FAILED(m_pDevice.QueryInterface(&m_pInfoQueue)))
  1091. return;
  1092. m_pInfoQueue->PushEmptyStorageFilter();
  1093. m_pInfoQueue->PushEmptyRetrievalFilter();
  1094. }
  1095. ~BasicTestChecker() {
  1096. if (!m_OK && m_pInfoQueue != nullptr) {
  1097. UINT64 count = m_pInfoQueue->GetNumStoredMessages();
  1098. bool invalidBytecodeFound = false;
  1099. CAtlArray<BYTE> m_pBytes;
  1100. for (UINT64 i = 0; i < count; ++i) {
  1101. SIZE_T len = 0;
  1102. if (FAILED(m_pInfoQueue->GetMessageA(i, nullptr, &len)))
  1103. continue;
  1104. if (m_pBytes.GetCount() < len && !m_pBytes.SetCount(len))
  1105. continue;
  1106. D3D12_MESSAGE *pMsg = (D3D12_MESSAGE *)m_pBytes.GetData();
  1107. if (FAILED(m_pInfoQueue->GetMessageA(i, pMsg, &len)))
  1108. continue;
  1109. if (pMsg->ID == D3D12_MESSAGE_ID_CREATEVERTEXSHADER_INVALIDSHADERBYTECODE ||
  1110. pMsg->ID == D3D12_MESSAGE_ID_CREATEPIXELSHADER_INVALIDSHADERBYTECODE) {
  1111. invalidBytecodeFound = true;
  1112. break;
  1113. }
  1114. }
  1115. if (invalidBytecodeFound) {
  1116. LogCommentFmt(L"%s", L"Found an invalid bytecode message. This "
  1117. L"typically indicates that experimental mode "
  1118. L"is not set up properly.");
  1119. if (!GetTestParamBool(L"ExperimentalShaders")) {
  1120. LogCommentFmt(L"Note that the ExperimentalShaders test parameter isn't set.");
  1121. }
  1122. }
  1123. else {
  1124. LogCommentFmt(L"Did not find corrupt pixel or vertex shaders in "
  1125. L"queue - dumping complete queue.");
  1126. WriteInfoQueueMessages(nullptr, OutputFn, m_pInfoQueue);
  1127. }
  1128. }
  1129. }
  1130. static void __stdcall OutputFn(void *pCtx, const wchar_t *pMsg) {
  1131. LogCommentFmt(L"%s", pMsg);
  1132. }
  1133. };
  1134. BasicTestChecker BTC(pDevice);
  1135. {
  1136. InitFenceObj(pDevice, &FO);
  1137. CreateRtvDescriptorHeap(pDevice, FrameCount, &pRtvHeap, &rtvDescriptorSize);
  1138. CreateRenderTargetAndReadback(pDevice, pRtvHeap, m_width, m_height, &pRenderTarget, &pReadBuffer);
  1139. // Create an empty root signature.
  1140. CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
  1141. rootSignatureDesc.Init(
  1142. 0, nullptr, 0, nullptr,
  1143. D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
  1144. CreateRootSignatureFromDesc(pDevice, &rootSignatureDesc, &pRootSig);
  1145. // Create the pipeline state, which includes compiling and loading shaders.
  1146. // Define the vertex input layout.
  1147. D3D12_INPUT_ELEMENT_DESC inputElementDescs[] = {
  1148. {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,
  1149. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
  1150. {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12,
  1151. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}};
  1152. D3D12_INPUT_LAYOUT_DESC InputLayout = { inputElementDescs, _countof(inputElementDescs) };
  1153. CreateGraphicsPSO(pDevice, &InputLayout, pRootSig, pShaders, &pPipelineState);
  1154. CreateGraphicsCommandQueueAndList(pDevice, &pCommandQueue,
  1155. &pCommandAllocator, &pCommandList,
  1156. pPipelineState);
  1157. // Define the geometry for a triangle.
  1158. Vertex triangleVertices[] = {
  1159. { { 0.0f, 0.25f * m_aspectRatio, 0.0f },{ 1.0f, 0.0f, 0.0f, 1.0f } },
  1160. { { 0.25f, -0.25f * m_aspectRatio, 0.0f },{ 0.0f, 1.0f, 0.0f, 1.0f } },
  1161. { { -0.25f, -0.25f * m_aspectRatio, 0.0f },{ 0.0f, 0.0f, 1.0f, 1.0f } } };
  1162. CreateVertexBuffer(pDevice, triangleVertices, &pVertexBuffer, &vertexBufferView);
  1163. WaitForSignal(pCommandQueue, FO);
  1164. }
  1165. // Render and execute the command list.
  1166. RecordRenderAndReadback(pCommandList, pRtvHeap, rtvDescriptorSize, 1,
  1167. &vertexBufferView, pRootSig, pRenderTarget,
  1168. pReadBuffer);
  1169. VERIFY_SUCCEEDED(pCommandList->Close());
  1170. ExecuteCommandList(pCommandQueue, pCommandList);
  1171. // Wait for previous frame.
  1172. WaitForSignal(pCommandQueue, FO);
  1173. // At this point, we've verified that execution succeeded with DXIL.
  1174. BTC.SetOK(true);
  1175. // Read back to CPU and examine contents.
  1176. {
  1177. MappedData data(pReadBuffer, m_width * m_height * 4);
  1178. const uint32_t *pPixels = (uint32_t *)data.data();
  1179. if (SaveImages()) {
  1180. SavePixelsToFile(pPixels, DXGI_FORMAT_R8G8B8A8_UNORM, m_width, m_height, L"basic.bmp");
  1181. }
  1182. uint32_t top = pPixels[m_width / 2]; // Top center.
  1183. uint32_t mid = pPixels[m_width / 2 + m_width * (m_height / 2)]; // Middle center.
  1184. VERIFY_ARE_EQUAL(0xff663300, top); // clear color
  1185. VERIFY_ARE_EQUAL(0xffffffff, mid); // white
  1186. }
  1187. #endif
  1188. }
  1189. TEST_F(ExecutionTest, Int64Test) {
  1190. static const char pShader[] =
  1191. "RWByteAddressBuffer g_bab : register(u0);\r\n"
  1192. "[numthreads(8,8,1)]\r\n"
  1193. "void main(uint GI : SV_GroupIndex) {"
  1194. " uint addr = GI * 4;\r\n"
  1195. " uint val = g_bab.Load(addr);\r\n"
  1196. " uint64_t u64 = val;\r\n"
  1197. " u64 *= val;\r\n"
  1198. " g_bab.Store(addr, (uint)(u64 >> 32));\r\n"
  1199. "}";
  1200. static const int NumThreadsX = 8;
  1201. static const int NumThreadsY = 8;
  1202. static const int NumThreadsZ = 1;
  1203. static const int ThreadsPerGroup = NumThreadsX * NumThreadsY * NumThreadsZ;
  1204. static const int DispatchGroupCount = 1;
  1205. CComPtr<ID3D12Device> pDevice;
  1206. if (!CreateDevice(&pDevice))
  1207. return;
  1208. if (!DoesDeviceSupportInt64(pDevice)) {
  1209. // Optional feature, so it's correct to not support it if declared as such.
  1210. WEX::Logging::Log::Comment(L"Device does not support int64 operations.");
  1211. return;
  1212. }
  1213. std::vector<uint32_t> values;
  1214. SetupComputeValuePattern(values, ThreadsPerGroup * DispatchGroupCount);
  1215. VERIFY_ARE_EQUAL(values[0], (uint32_t)0);
  1216. RunRWByteBufferComputeTest(pDevice, pShader, values);
  1217. VERIFY_ARE_EQUAL(values[0], (uint32_t)0);
  1218. }
  1219. TEST_F(ExecutionTest, SignTest) {
  1220. static const char pShader[] =
  1221. "RWByteAddressBuffer g_bab : register(u0);\r\n"
  1222. "[numthreads(8,1,1)]\r\n"
  1223. "void main(uint GI : SV_GroupIndex) {"
  1224. " uint addr = GI * 4;\r\n"
  1225. " int val = g_bab.Load(addr);\r\n"
  1226. " g_bab.Store(addr, (uint)(sign(val)));\r\n"
  1227. "}";
  1228. static const int NumThreadsX = 8;
  1229. static const int NumThreadsY = 1;
  1230. static const int NumThreadsZ = 1;
  1231. static const int ThreadsPerGroup = NumThreadsX * NumThreadsY * NumThreadsZ;
  1232. static const int DispatchGroupCount = 1;
  1233. CComPtr<ID3D12Device> pDevice;
  1234. if (!CreateDevice(&pDevice))
  1235. return;
  1236. const uint32_t neg1 = (uint32_t)-1;
  1237. uint32_t origValues[] = { (uint32_t)-3, (uint32_t)-2, neg1, 0, 1, 2, 3, 4 };
  1238. std::vector<uint32_t> values(origValues, origValues + _countof(origValues));
  1239. RunRWByteBufferComputeTest(pDevice, pShader, values);
  1240. VERIFY_ARE_EQUAL(values[0], neg1);
  1241. VERIFY_ARE_EQUAL(values[1], neg1);
  1242. VERIFY_ARE_EQUAL(values[2], neg1);
  1243. VERIFY_ARE_EQUAL(values[3], (uint32_t)0);
  1244. VERIFY_ARE_EQUAL(values[4], (uint32_t)1);
  1245. VERIFY_ARE_EQUAL(values[5], (uint32_t)1);
  1246. VERIFY_ARE_EQUAL(values[6], (uint32_t)1);
  1247. VERIFY_ARE_EQUAL(values[7], (uint32_t)1);
  1248. }
  1249. TEST_F(ExecutionTest, WaveIntrinsicsDDITest) {
  1250. #ifndef _HLK_CONF
  1251. CComPtr<ID3D12Device> pDevice;
  1252. if (!CreateDevice(&pDevice))
  1253. return;
  1254. D3D12_FEATURE_DATA_D3D12_OPTIONS1 O;
  1255. if (FAILED(pDevice->CheckFeatureSupport((D3D12_FEATURE)D3D12_FEATURE_D3D12_OPTIONS1, &O, sizeof(O))))
  1256. return;
  1257. bool waveSupported = O.WaveOps;
  1258. UINT laneCountMin = O.WaveLaneCountMin;
  1259. UINT laneCountMax = O.WaveLaneCountMax;
  1260. LogCommentFmt(L"WaveOps %i, WaveLaneCountMin %u, WaveLaneCountMax %u", waveSupported, laneCountMin, laneCountMax);
  1261. VERIFY_IS_TRUE(laneCountMin <= laneCountMax);
  1262. if (waveSupported) {
  1263. VERIFY_IS_TRUE(laneCountMin > 0 && laneCountMax > 0);
  1264. }
  1265. else {
  1266. VERIFY_IS_TRUE(laneCountMin == 0 && laneCountMax == 0);
  1267. }
  1268. #endif
  1269. }
  1270. TEST_F(ExecutionTest, WaveIntrinsicsTest) {
  1271. #ifndef _HLK_CONF
  1272. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  1273. struct PerThreadData {
  1274. uint32_t id, flags, laneIndex, laneCount, firstLaneId, preds, firstlaneX, lane1X;
  1275. uint32_t allBC, allSum, allProd, allAND, allOR, allXOR, allMin, allMax;
  1276. uint32_t pfBC, pfSum, pfProd;
  1277. uint32_t ballot[4];
  1278. uint32_t diver; // divergent value, used in calculation
  1279. int32_t i_diver; // divergent value, used in calculation
  1280. int32_t i_allMax, i_allMin, i_allSum, i_allProd;
  1281. int32_t i_pfSum, i_pfProd;
  1282. };
  1283. static const char pShader[] =
  1284. WAVE_INTRINSIC_DXBC_GUARD
  1285. "struct PerThreadData {\r\n"
  1286. " uint id, flags, laneIndex, laneCount, firstLaneId, preds, firstlaneX, lane1X;\r\n"
  1287. " uint allBC, allSum, allProd, allAND, allOR, allXOR, allMin, allMax;\r\n"
  1288. " uint pfBC, pfSum, pfProd;\r\n"
  1289. " uint4 ballot;\r\n"
  1290. " uint diver;\r\n"
  1291. " int i_diver;\r\n"
  1292. " int i_allMax, i_allMin, i_allSum, i_allProd;\r\n"
  1293. " int i_pfSum, i_pfProd;\r\n"
  1294. "};\r\n"
  1295. "RWStructuredBuffer<PerThreadData> g_sb : register(u0);\r\n"
  1296. "[numthreads(8,8,1)]\r\n"
  1297. "void main(uint GI : SV_GroupIndex, uint3 GTID : SV_GroupThreadID) {"
  1298. " PerThreadData pts = g_sb[GI];\r\n"
  1299. " uint diver = GTID.x + 2;\r\n"
  1300. " pts.diver = diver;\r\n"
  1301. " pts.flags = 0;\r\n"
  1302. " pts.preds = 0;\r\n"
  1303. " if (WaveIsFirstLane()) pts.flags |= 1;\r\n"
  1304. " pts.laneIndex = WaveGetLaneIndex();\r\n"
  1305. " pts.laneCount = WaveGetLaneCount();\r\n"
  1306. " pts.firstLaneId = WaveReadLaneFirst(pts.id);\r\n"
  1307. " pts.preds |= ((WaveActiveAnyTrue(diver == 1) ? 1 : 0) << 0);\r\n"
  1308. " pts.preds |= ((WaveActiveAllTrue(diver == 1) ? 1 : 0) << 1);\r\n"
  1309. " pts.preds |= ((WaveActiveAllEqual(diver) ? 1 : 0) << 2);\r\n"
  1310. " pts.preds |= ((WaveActiveAllEqual(GTID.z) ? 1 : 0) << 3);\r\n"
  1311. " pts.preds |= ((WaveActiveAllEqual(WaveReadLaneFirst(diver)) ? 1 : 0) << 4);\r\n"
  1312. " pts.ballot = WaveActiveBallot(diver > 3);\r\n"
  1313. " pts.firstlaneX = WaveReadLaneFirst(GTID.x);\r\n"
  1314. " pts.lane1X = WaveReadLaneAt(GTID.x, 1);\r\n"
  1315. "\r\n"
  1316. " pts.allBC = WaveActiveCountBits(diver > 3);\r\n"
  1317. " pts.allSum = WaveActiveSum(diver);\r\n"
  1318. " pts.allProd = WaveActiveProduct(diver);\r\n"
  1319. " pts.allAND = WaveActiveBitAnd(diver);\r\n"
  1320. " pts.allOR = WaveActiveBitOr(diver);\r\n"
  1321. " pts.allXOR = WaveActiveBitXor(diver);\r\n"
  1322. " pts.allMin = WaveActiveMin(diver);\r\n"
  1323. " pts.allMax = WaveActiveMax(diver);\r\n"
  1324. "\r\n"
  1325. " pts.pfBC = WavePrefixCountBits(diver > 3);\r\n"
  1326. " pts.pfSum = WavePrefixSum(diver);\r\n"
  1327. " pts.pfProd = WavePrefixProduct(diver);\r\n"
  1328. "\r\n"
  1329. " int i_diver = pts.i_diver;\r\n"
  1330. " pts.i_allMax = WaveActiveMax(i_diver);\r\n"
  1331. " pts.i_allMin = WaveActiveMin(i_diver);\r\n"
  1332. " pts.i_allSum = WaveActiveSum(i_diver);\r\n"
  1333. " pts.i_allProd = WaveActiveProduct(i_diver);\r\n"
  1334. " pts.i_pfSum = WavePrefixSum(i_diver);\r\n"
  1335. " pts.i_pfProd = WavePrefixProduct(i_diver);\r\n"
  1336. "\r\n"
  1337. " g_sb[GI] = pts;\r\n"
  1338. "}";
  1339. static const int NumtheadsX = 8;
  1340. static const int NumtheadsY = 8;
  1341. static const int NumtheadsZ = 1;
  1342. static const int ThreadsPerGroup = NumtheadsX * NumtheadsY * NumtheadsZ;
  1343. static const int DispatchGroupCount = 1;
  1344. CComPtr<ID3D12Device> pDevice;
  1345. if (!CreateDevice(&pDevice))
  1346. return;
  1347. if (!DoesDeviceSupportWaveOps(pDevice)) {
  1348. // Optional feature, so it's correct to not support it if declared as such.
  1349. WEX::Logging::Log::Comment(L"Device does not support wave operations.");
  1350. return;
  1351. }
  1352. std::vector<PerThreadData> values;
  1353. values.resize(ThreadsPerGroup * DispatchGroupCount);
  1354. for (size_t i = 0; i < values.size(); ++i) {
  1355. memset(&values[i], 0, sizeof(PerThreadData));
  1356. values[i].id = i;
  1357. values[i].i_diver = (int)i;
  1358. values[i].i_diver *= (i % 2) ? 1 : -1;
  1359. }
  1360. static const int DispatchGroupX = 1;
  1361. static const int DispatchGroupY = 1;
  1362. static const int DispatchGroupZ = 1;
  1363. CComPtr<ID3D12GraphicsCommandList> pCommandList;
  1364. CComPtr<ID3D12CommandQueue> pCommandQueue;
  1365. CComPtr<ID3D12DescriptorHeap> pUavHeap;
  1366. CComPtr<ID3D12CommandAllocator> pCommandAllocator;
  1367. UINT uavDescriptorSize;
  1368. FenceObj FO;
  1369. bool dxbc = UseDxbc();
  1370. const size_t valueSizeInBytes = values.size() * sizeof(PerThreadData);
  1371. CreateComputeCommandQueue(pDevice, L"WaveIntrinsicsTest Command Queue", &pCommandQueue);
  1372. InitFenceObj(pDevice, &FO);
  1373. // Describe and create a UAV descriptor heap.
  1374. D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
  1375. heapDesc.NumDescriptors = 1;
  1376. heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  1377. heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  1378. VERIFY_SUCCEEDED(pDevice->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&pUavHeap)));
  1379. uavDescriptorSize = pDevice->GetDescriptorHandleIncrementSize(heapDesc.Type);
  1380. // Create root signature.
  1381. CComPtr<ID3D12RootSignature> pRootSignature;
  1382. {
  1383. CD3DX12_DESCRIPTOR_RANGE ranges[1];
  1384. ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 0, 0, 0);
  1385. CD3DX12_ROOT_PARAMETER rootParameters[1];
  1386. rootParameters[0].InitAsDescriptorTable(1, &ranges[0], D3D12_SHADER_VISIBILITY_ALL);
  1387. CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
  1388. rootSignatureDesc.Init(_countof(rootParameters), rootParameters, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE);
  1389. CComPtr<ID3DBlob> signature;
  1390. CComPtr<ID3DBlob> error;
  1391. VERIFY_SUCCEEDED(D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
  1392. VERIFY_SUCCEEDED(pDevice->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&pRootSignature)));
  1393. }
  1394. // Create pipeline state object.
  1395. CComPtr<ID3D12PipelineState> pComputeState;
  1396. CreateComputePSO(pDevice, pRootSignature, pShader, &pComputeState);
  1397. // Create a command allocator and list for compute.
  1398. VERIFY_SUCCEEDED(pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE, IID_PPV_ARGS(&pCommandAllocator)));
  1399. VERIFY_SUCCEEDED(pDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, pCommandAllocator, pComputeState, IID_PPV_ARGS(&pCommandList)));
  1400. // Set up UAV resource.
  1401. CComPtr<ID3D12Resource> pUavResource;
  1402. CComPtr<ID3D12Resource> pReadBuffer;
  1403. CComPtr<ID3D12Resource> pUploadResource;
  1404. CreateTestUavs(pDevice, pCommandList, values.data(), valueSizeInBytes, &pUavResource, &pReadBuffer, &pUploadResource);
  1405. // Close the command list and execute it to perform the GPU setup.
  1406. pCommandList->Close();
  1407. ExecuteCommandList(pCommandQueue, pCommandList);
  1408. WaitForSignal(pCommandQueue, FO);
  1409. VERIFY_SUCCEEDED(pCommandAllocator->Reset());
  1410. VERIFY_SUCCEEDED(pCommandList->Reset(pCommandAllocator, pComputeState));
  1411. // Run the compute shader and copy the results back to readable memory.
  1412. {
  1413. D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
  1414. uavDesc.Format = DXGI_FORMAT_UNKNOWN;
  1415. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
  1416. uavDesc.Buffer.FirstElement = 0;
  1417. uavDesc.Buffer.NumElements = values.size();
  1418. uavDesc.Buffer.StructureByteStride = sizeof(PerThreadData);
  1419. uavDesc.Buffer.CounterOffsetInBytes = 0;
  1420. uavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE;
  1421. CD3DX12_CPU_DESCRIPTOR_HANDLE uavHandle(pUavHeap->GetCPUDescriptorHandleForHeapStart());
  1422. CD3DX12_GPU_DESCRIPTOR_HANDLE uavHandleGpu(pUavHeap->GetGPUDescriptorHandleForHeapStart());
  1423. pDevice->CreateUnorderedAccessView(pUavResource, nullptr, &uavDesc, uavHandle);
  1424. SetDescriptorHeap(pCommandList, pUavHeap);
  1425. pCommandList->SetComputeRootSignature(pRootSignature);
  1426. pCommandList->SetComputeRootDescriptorTable(0, uavHandleGpu);
  1427. }
  1428. pCommandList->Dispatch(DispatchGroupX, DispatchGroupY, DispatchGroupZ);
  1429. RecordTransitionBarrier(pCommandList, pUavResource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
  1430. pCommandList->CopyResource(pReadBuffer, pUavResource);
  1431. pCommandList->Close();
  1432. ExecuteCommandList(pCommandQueue, pCommandList);
  1433. WaitForSignal(pCommandQueue, FO);
  1434. {
  1435. MappedData mappedData(pReadBuffer, valueSizeInBytes);
  1436. PerThreadData *pData = (PerThreadData *)mappedData.data();
  1437. memcpy(values.data(), pData, valueSizeInBytes);
  1438. // Gather some general data.
  1439. // The 'firstLaneId' captures a unique number per first-lane per wave.
  1440. // Counting the number distinct firstLaneIds gives us the number of waves.
  1441. std::vector<uint32_t> firstLaneIds;
  1442. for (size_t i = 0; i < values.size(); ++i) {
  1443. PerThreadData &pts = values[i];
  1444. uint32_t firstLaneId = pts.firstLaneId;
  1445. if (!contains(firstLaneIds, firstLaneId)) {
  1446. firstLaneIds.push_back(firstLaneId);
  1447. }
  1448. }
  1449. // Waves should cover 4 threads or more.
  1450. LogCommentFmt(L"Found %u distinct lane ids: %u", firstLaneIds.size());
  1451. if (!dxbc) {
  1452. VERIFY_IS_GREATER_THAN_OR_EQUAL(values.size() / 4, firstLaneIds.size());
  1453. }
  1454. // Now, group threads into waves.
  1455. std::map<uint32_t, std::unique_ptr<std::vector<PerThreadData *> > > waves;
  1456. for (size_t i = 0; i < firstLaneIds.size(); ++i) {
  1457. waves[firstLaneIds[i]] = std::make_unique<std::vector<PerThreadData *> >();
  1458. }
  1459. for (size_t i = 0; i < values.size(); ++i) {
  1460. PerThreadData &pts = values[i];
  1461. std::unique_ptr<std::vector<PerThreadData *> > &wave = waves[pts.firstLaneId];
  1462. wave->push_back(&pts);
  1463. }
  1464. // Verify that all the wave values are coherent across the wave.
  1465. for (size_t i = 0; i < values.size(); ++i) {
  1466. PerThreadData &pts = values[i];
  1467. std::unique_ptr<std::vector<PerThreadData *> > &wave = waves[pts.firstLaneId];
  1468. // Sort the lanes by increasing lane ID.
  1469. struct LaneIdOrderPred {
  1470. bool operator()(PerThreadData *a, PerThreadData *b) {
  1471. return a->laneIndex < b->laneIndex;
  1472. }
  1473. };
  1474. std::sort(wave.get()->begin(), wave.get()->end(), LaneIdOrderPred());
  1475. // Verify some interesting properties of the first lane.
  1476. uint32_t pfBC, pfSum, pfProd;
  1477. int32_t i_pfSum, i_pfProd;
  1478. int32_t i_allMax, i_allMin;
  1479. {
  1480. PerThreadData *ptdFirst = wave->front();
  1481. VERIFY_IS_TRUE(0 != (ptdFirst->flags & 1)); // FirstLane sets this bit.
  1482. VERIFY_IS_TRUE(0 == ptdFirst->pfBC);
  1483. VERIFY_IS_TRUE(0 == ptdFirst->pfSum);
  1484. VERIFY_IS_TRUE(1 == ptdFirst->pfProd);
  1485. VERIFY_IS_TRUE(0 == ptdFirst->i_pfSum);
  1486. VERIFY_IS_TRUE(1 == ptdFirst->i_pfProd);
  1487. pfBC = (ptdFirst->diver > 3) ? 1 : 0;
  1488. pfSum = ptdFirst->diver;
  1489. pfProd = ptdFirst->diver;
  1490. i_pfSum = ptdFirst->i_diver;
  1491. i_pfProd = ptdFirst->i_diver;
  1492. i_allMax = i_allMin = ptdFirst->i_diver;
  1493. }
  1494. // Calculate values which take into consideration all lanes.
  1495. uint32_t preds = 0;
  1496. preds |= 1 << 1; // AllTrue starts true, switches to false if needed.
  1497. preds |= 1 << 2; // AllEqual starts true, switches to false if needed.
  1498. preds |= 1 << 3; // WaveActiveAllEqual(GTID.z) is always true
  1499. preds |= 1 << 4; // (WaveActiveAllEqual(WaveReadLaneFirst(diver)) is always true
  1500. uint32_t ballot[4] = { 0, 0, 0, 0 };
  1501. int32_t i_allSum = 0, i_allProd = 1;
  1502. for (size_t n = 0; n < wave->size(); ++n) {
  1503. std::vector<PerThreadData *> &lanes = *wave.get();
  1504. // pts.preds |= ((WaveActiveAnyTrue(diver == 1) ? 1 : 0) << 0);
  1505. if (lanes[n]->diver == 1) preds |= (1 << 0);
  1506. // pts.preds |= ((WaveActiveAllTrue(diver == 1) ? 1 : 0) << 1);
  1507. if (lanes[n]->diver != 1) preds &= ~(1 << 1);
  1508. // pts.preds |= ((WaveActiveAllEqual(diver) ? 1 : 0) << 2);
  1509. if (lanes[0]->diver != lanes[n]->diver) preds &= ~(1 << 2);
  1510. // pts.ballot = WaveActiveBallot(diver > 3);\r\n"
  1511. if (lanes[n]->diver > 3) {
  1512. // This is the uint4 result layout:
  1513. // .x -> bits 0 .. 31
  1514. // .y -> bits 32 .. 63
  1515. // .z -> bits 64 .. 95
  1516. // .w -> bits 96 ..127
  1517. uint32_t component = lanes[n]->laneIndex / 32;
  1518. uint32_t bit = lanes[n]->laneIndex % 32;
  1519. ballot[component] |= 1 << bit;
  1520. }
  1521. i_allMax = std::max(lanes[n]->i_diver, i_allMax);
  1522. i_allMin = std::min(lanes[n]->i_diver, i_allMin);
  1523. i_allProd *= lanes[n]->i_diver;
  1524. i_allSum += lanes[n]->i_diver;
  1525. }
  1526. for (size_t n = 1; n < wave->size(); ++n) {
  1527. // 'All' operations are uniform across the wave.
  1528. std::vector<PerThreadData *> &lanes = *wave.get();
  1529. VERIFY_IS_TRUE(0 == (lanes[n]->flags & 1)); // non-firstlanes do not set this bit
  1530. VERIFY_ARE_EQUAL(lanes[0]->allBC, lanes[n]->allBC);
  1531. VERIFY_ARE_EQUAL(lanes[0]->allSum, lanes[n]->allSum);
  1532. VERIFY_ARE_EQUAL(lanes[0]->allProd, lanes[n]->allProd);
  1533. VERIFY_ARE_EQUAL(lanes[0]->allAND, lanes[n]->allAND);
  1534. VERIFY_ARE_EQUAL(lanes[0]->allOR, lanes[n]->allOR);
  1535. VERIFY_ARE_EQUAL(lanes[0]->allXOR, lanes[n]->allXOR);
  1536. VERIFY_ARE_EQUAL(lanes[0]->allMin, lanes[n]->allMin);
  1537. VERIFY_ARE_EQUAL(lanes[0]->allMax, lanes[n]->allMax);
  1538. VERIFY_ARE_EQUAL(i_allMax, lanes[n]->i_allMax);
  1539. VERIFY_ARE_EQUAL(i_allMin, lanes[n]->i_allMin);
  1540. VERIFY_ARE_EQUAL(i_allProd, lanes[n]->i_allProd);
  1541. VERIFY_ARE_EQUAL(i_allSum, lanes[n]->i_allSum);
  1542. // first-lane reads and uniform reads are uniform across the wave.
  1543. VERIFY_ARE_EQUAL(lanes[0]->firstlaneX, lanes[n]->firstlaneX);
  1544. VERIFY_ARE_EQUAL(lanes[0]->lane1X, lanes[n]->lane1X);
  1545. // the lane count is uniform across the wave.
  1546. VERIFY_ARE_EQUAL(lanes[0]->laneCount, lanes[n]->laneCount);
  1547. // The predicates are uniform across the wave.
  1548. VERIFY_ARE_EQUAL(lanes[n]->preds, preds);
  1549. // the lane index is distinct per thread.
  1550. for (size_t prior = 0; prior < n; ++prior) {
  1551. VERIFY_ARE_NOT_EQUAL(lanes[prior]->laneIndex, lanes[n]->laneIndex);
  1552. }
  1553. // Ballot results are uniform across the wave.
  1554. VERIFY_ARE_EQUAL(0, memcmp(ballot, lanes[n]->ballot, sizeof(ballot)));
  1555. // Keep running total of prefix calculation. Prefix values are exclusive to
  1556. // the executing lane.
  1557. VERIFY_ARE_EQUAL(pfBC, lanes[n]->pfBC);
  1558. VERIFY_ARE_EQUAL(pfSum, lanes[n]->pfSum);
  1559. VERIFY_ARE_EQUAL(pfProd, lanes[n]->pfProd);
  1560. VERIFY_ARE_EQUAL(i_pfSum, lanes[n]->i_pfSum);
  1561. VERIFY_ARE_EQUAL(i_pfProd, lanes[n]->i_pfProd);
  1562. pfBC += (lanes[n]->diver > 3) ? 1 : 0;
  1563. pfSum += lanes[n]->diver;
  1564. pfProd *= lanes[n]->diver;
  1565. i_pfSum += lanes[n]->i_diver;
  1566. i_pfProd *= lanes[n]->i_diver;
  1567. }
  1568. // TODO: add divergent branching and verify that the otherwise uniform values properly diverge
  1569. }
  1570. // Compare each value of each per-thread element.
  1571. for (size_t i = 0; i < values.size(); ++i) {
  1572. PerThreadData &pts = values[i];
  1573. VERIFY_ARE_EQUAL(i, pts.id); // ID is unchanged.
  1574. }
  1575. }
  1576. #endif
  1577. }
  1578. // This test is assuming that the adapter implements WaveReadLaneFirst correctly
  1579. TEST_F(ExecutionTest, WaveIntrinsicsInPSTest) {
  1580. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  1581. struct Vertex {
  1582. XMFLOAT3 position;
  1583. };
  1584. struct PerPixelData {
  1585. XMFLOAT4 position;
  1586. uint32_t id, flags, laneIndex, laneCount, firstLaneId, sum1;
  1587. uint32_t id0, id1, id2, id3;
  1588. uint32_t acrossX, acrossY, acrossDiag, quadActiveCount;
  1589. };
  1590. const UINT RTWidth = 128;
  1591. const UINT RTHeight = 128;
  1592. // Shaders.
  1593. static const char pShaders[] =
  1594. WAVE_INTRINSIC_DXBC_GUARD
  1595. "struct PSInput {\r\n"
  1596. " float4 position : SV_POSITION;\r\n"
  1597. "};\r\n\r\n"
  1598. "PSInput VSMain(float4 position : POSITION) {\r\n"
  1599. " PSInput result;\r\n"
  1600. "\r\n"
  1601. " result.position = position;\r\n"
  1602. " return result;\r\n"
  1603. "}\r\n\r\n"
  1604. "uint pos_to_id(float4 pos) { return pos.x * 128 + pos.y; }\r\n"
  1605. "struct PerPixelData {\r\n"
  1606. " float4 position;\r\n"
  1607. " uint id, flags, laneIndex, laneCount, firstLaneId, sum1;\r\n"
  1608. " uint id0, id1, id2, id3;\r\n"
  1609. " uint acrossX, acrossY, acrossDiag, quadActiveCount;\r\n"
  1610. "};\r\n"
  1611. "AppendStructuredBuffer<PerPixelData> g_sb : register(u1);\r\n"
  1612. "float4 PSMain(PSInput input) : SV_TARGET {\r\n"
  1613. " uint one = 1;\r\n"
  1614. " PerPixelData d;\r\n"
  1615. " d.position = input.position;\r\n"
  1616. " d.id = pos_to_id(input.position);\r\n"
  1617. " d.flags = 0;\r\n"
  1618. " if (WaveIsFirstLane()) d.flags |= 1;\r\n"
  1619. " d.laneIndex = WaveGetLaneIndex();\r\n"
  1620. " d.laneCount = WaveGetLaneCount();\r\n"
  1621. " d.firstLaneId = WaveReadLaneFirst(d.id);\r\n"
  1622. " d.sum1 = WaveActiveSum(one);\r\n"
  1623. " d.id0 = QuadReadLaneAt(d.id, 0);\r\n"
  1624. " d.id1 = QuadReadLaneAt(d.id, 1);\r\n"
  1625. " d.id2 = QuadReadLaneAt(d.id, 2);\r\n"
  1626. " d.id3 = QuadReadLaneAt(d.id, 3);\r\n"
  1627. " d.acrossX = QuadReadAcrossX(d.id);\r\n"
  1628. " d.acrossY = QuadReadAcrossY(d.id);\r\n"
  1629. " d.acrossDiag = QuadReadAcrossDiagonal(d.id);\r\n"
  1630. " d.quadActiveCount = one + QuadReadAcrossX(one) + QuadReadAcrossY(one) + QuadReadAcrossDiagonal(one);\r\n"
  1631. " g_sb.Append(d);\r\n"
  1632. " return 1;\r\n"
  1633. "};\r\n";
  1634. CComPtr<ID3D12Device> pDevice;
  1635. CComPtr<ID3D12CommandQueue> pCommandQueue;
  1636. CComPtr<ID3D12DescriptorHeap> pUavHeap, pRtvHeap;
  1637. CComPtr<ID3D12CommandAllocator> pCommandAllocator;
  1638. CComPtr<ID3D12GraphicsCommandList> pCommandList;
  1639. CComPtr<ID3D12PipelineState> pPSO;
  1640. CComPtr<ID3D12Resource> pRenderTarget, pReadBuffer;
  1641. UINT uavDescriptorSize, rtvDescriptorSize;
  1642. CComPtr<ID3D12Resource> pVertexBuffer;
  1643. D3D12_VERTEX_BUFFER_VIEW vertexBufferView;
  1644. if (!CreateDevice(&pDevice))
  1645. return;
  1646. if (!DoesDeviceSupportWaveOps(pDevice)) {
  1647. // Optional feature, so it's correct to not support it if declared as such.
  1648. WEX::Logging::Log::Comment(L"Device does not support wave operations.");
  1649. return;
  1650. }
  1651. FenceObj FO;
  1652. InitFenceObj(pDevice, &FO);
  1653. // Describe and create a UAV descriptor heap.
  1654. D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
  1655. heapDesc.NumDescriptors = 1;
  1656. heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  1657. heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  1658. VERIFY_SUCCEEDED(pDevice->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&pUavHeap)));
  1659. uavDescriptorSize = pDevice->GetDescriptorHandleIncrementSize(heapDesc.Type);
  1660. CreateRtvDescriptorHeap(pDevice, 1, &pRtvHeap, &rtvDescriptorSize);
  1661. CreateRenderTargetAndReadback(pDevice, pRtvHeap, RTHeight, RTWidth, &pRenderTarget, &pReadBuffer);
  1662. // Create root signature: one UAV.
  1663. CComPtr<ID3D12RootSignature> pRootSignature;
  1664. {
  1665. CD3DX12_DESCRIPTOR_RANGE ranges[1];
  1666. ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 1, 0, 0);
  1667. CD3DX12_ROOT_PARAMETER rootParameters[1];
  1668. rootParameters[0].InitAsDescriptorTable(1, &ranges[0], D3D12_SHADER_VISIBILITY_ALL);
  1669. CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
  1670. rootSignatureDesc.Init(_countof(rootParameters), rootParameters, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
  1671. CreateRootSignatureFromDesc(pDevice, &rootSignatureDesc, &pRootSignature);
  1672. }
  1673. D3D12_INPUT_ELEMENT_DESC elementDesc[] = {
  1674. {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,
  1675. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}};
  1676. D3D12_INPUT_LAYOUT_DESC InputLayout = {elementDesc, _countof(elementDesc)};
  1677. CreateGraphicsPSO(pDevice, &InputLayout, pRootSignature, pShaders, &pPSO);
  1678. CreateGraphicsCommandQueueAndList(pDevice, &pCommandQueue, &pCommandAllocator,
  1679. &pCommandList, pPSO);
  1680. // Single triangle covering half the target.
  1681. Vertex vertices[] = {
  1682. { { -1.0f, 1.0f, 0.0f } },
  1683. { { 1.0f, 1.0f, 0.0f } },
  1684. { { -1.0f, -1.0f, 0.0f } } };
  1685. const UINT TriangleCount = _countof(vertices) / 3;
  1686. CreateVertexBuffer(pDevice, vertices, &pVertexBuffer, &vertexBufferView);
  1687. bool dxbc = UseDxbc();
  1688. // Set up UAV resource.
  1689. std::vector<PerPixelData> values;
  1690. values.resize(RTWidth * RTHeight * 2);
  1691. UINT valueSizeInBytes = (UINT)values.size() * sizeof(PerPixelData);
  1692. memset(values.data(), 0, valueSizeInBytes);
  1693. CComPtr<ID3D12Resource> pUavResource;
  1694. CComPtr<ID3D12Resource> pUavReadBuffer;
  1695. CComPtr<ID3D12Resource> pUploadResource;
  1696. CreateTestUavs(pDevice, pCommandList, values.data(), valueSizeInBytes, &pUavResource, &pUavReadBuffer, &pUploadResource);
  1697. // Set up the append counter resource.
  1698. CComPtr<ID3D12Resource> pUavCounterResource;
  1699. CComPtr<ID3D12Resource> pReadCounterBuffer;
  1700. CComPtr<ID3D12Resource> pUploadCounterResource;
  1701. BYTE zero[sizeof(UINT)] = { 0 };
  1702. CreateTestUavs(pDevice, pCommandList, zero, sizeof(zero), &pUavCounterResource, &pReadCounterBuffer, &pUploadCounterResource);
  1703. // Close the command list and execute it to perform the GPU setup.
  1704. pCommandList->Close();
  1705. ExecuteCommandList(pCommandQueue, pCommandList);
  1706. WaitForSignal(pCommandQueue, FO);
  1707. VERIFY_SUCCEEDED(pCommandAllocator->Reset());
  1708. VERIFY_SUCCEEDED(pCommandList->Reset(pCommandAllocator, pPSO));
  1709. pCommandList->SetGraphicsRootSignature(pRootSignature);
  1710. SetDescriptorHeap(pCommandList, pUavHeap);
  1711. {
  1712. D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
  1713. uavDesc.Format = DXGI_FORMAT_UNKNOWN;
  1714. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
  1715. uavDesc.Buffer.FirstElement = 0;
  1716. uavDesc.Buffer.NumElements = (UINT)values.size();
  1717. uavDesc.Buffer.StructureByteStride = sizeof(PerPixelData);
  1718. uavDesc.Buffer.CounterOffsetInBytes = 0;
  1719. uavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE;
  1720. CD3DX12_CPU_DESCRIPTOR_HANDLE uavHandle(pUavHeap->GetCPUDescriptorHandleForHeapStart());
  1721. CD3DX12_GPU_DESCRIPTOR_HANDLE uavHandleGpu(pUavHeap->GetGPUDescriptorHandleForHeapStart());
  1722. pDevice->CreateUnorderedAccessView(pUavResource, pUavCounterResource, &uavDesc, uavHandle);
  1723. pCommandList->SetGraphicsRootDescriptorTable(0, uavHandleGpu);
  1724. }
  1725. RecordRenderAndReadback(pCommandList, pRtvHeap, rtvDescriptorSize, TriangleCount, &vertexBufferView, nullptr, pRenderTarget, pReadBuffer);
  1726. RecordTransitionBarrier(pCommandList, pUavResource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
  1727. RecordTransitionBarrier(pCommandList, pUavCounterResource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
  1728. pCommandList->CopyResource(pUavReadBuffer, pUavResource);
  1729. pCommandList->CopyResource(pReadCounterBuffer, pUavCounterResource);
  1730. VERIFY_SUCCEEDED(pCommandList->Close());
  1731. LogCommentFmt(L"Rendering to %u by %u", RTWidth, RTHeight);
  1732. ExecuteCommandList(pCommandQueue, pCommandList);
  1733. WaitForSignal(pCommandQueue, FO);
  1734. {
  1735. MappedData data(pReadBuffer, RTWidth * RTHeight * 4);
  1736. const uint32_t *pPixels = (uint32_t *)data.data();
  1737. if (SaveImages()) {
  1738. SavePixelsToFile(pPixels, DXGI_FORMAT_R8G8B8A8_UNORM, RTWidth, RTHeight, L"psintrin.bmp");
  1739. }
  1740. }
  1741. uint32_t appendCount;
  1742. {
  1743. MappedData mappedData(pReadCounterBuffer, sizeof(uint32_t));
  1744. appendCount = *((uint32_t *)mappedData.data());
  1745. LogCommentFmt(L"%u elements in append buffer", appendCount);
  1746. }
  1747. {
  1748. MappedData mappedData(pUavReadBuffer, (UINT32)values.size());
  1749. PerPixelData *pData = (PerPixelData *)mappedData.data();
  1750. memcpy(values.data(), pData, valueSizeInBytes);
  1751. // DXBC is handy to test pipeline setup, but interesting functions are
  1752. // stubbed out, so there is no point in further validation.
  1753. if (dxbc)
  1754. return;
  1755. uint32_t maxActiveLaneCount = 0;
  1756. uint32_t maxLaneCount = 0;
  1757. for (uint32_t i = 0; i < appendCount; ++i) {
  1758. maxActiveLaneCount = std::max(maxActiveLaneCount, values[i].sum1);
  1759. maxLaneCount = std::max(maxLaneCount, values[i].laneCount);
  1760. }
  1761. uint32_t peerOfHelperLanes = 0;
  1762. for (uint32_t i = 0; i < appendCount; ++i) {
  1763. if (values[i].sum1 != maxActiveLaneCount) {
  1764. ++peerOfHelperLanes;
  1765. }
  1766. }
  1767. LogCommentFmt(
  1768. L"Found: %u threads. Waves reported up to %u total lanes, up "
  1769. L"to %u active lanes, and %u threads had helper/inactive lanes.",
  1770. appendCount, maxLaneCount, maxActiveLaneCount, peerOfHelperLanes);
  1771. // Group threads into quad invocations.
  1772. uint32_t singlePixelCount = 0;
  1773. uint32_t multiPixelCount = 0;
  1774. std::unordered_set<uint32_t> ids;
  1775. std::multimap<uint32_t, PerPixelData *> idGroups;
  1776. std::multimap<uint32_t, PerPixelData *> firstIdGroups;
  1777. for (uint32_t i = 0; i < appendCount; ++i) {
  1778. ids.insert(values[i].id);
  1779. idGroups.insert(std::make_pair(values[i].id, &values[i]));
  1780. firstIdGroups.insert(std::make_pair(values[i].firstLaneId, &values[i]));
  1781. }
  1782. for (uint32_t id : ids) {
  1783. if (idGroups.count(id) == 1)
  1784. ++singlePixelCount;
  1785. else
  1786. ++multiPixelCount;
  1787. }
  1788. LogCommentFmt(L"%u pixels were processed by a single thread. %u invocations were for shared pixels.",
  1789. singlePixelCount, multiPixelCount);
  1790. // Multiple threads may have tried to shade the same pixel. (Is this true even if we have only one triangle?)
  1791. // Where every pixel is distinct, it's very straightforward to validate.
  1792. {
  1793. auto cur = firstIdGroups.begin(), end = firstIdGroups.end();
  1794. while (cur != end) {
  1795. bool simpleWave = true;
  1796. uint32_t firstId = (*cur).first;
  1797. auto groupEnd = cur;
  1798. while (groupEnd != end && (*groupEnd).first == firstId) {
  1799. if (idGroups.count((*groupEnd).second->id) > 1)
  1800. simpleWave = false;
  1801. ++groupEnd;
  1802. }
  1803. if (simpleWave) {
  1804. // Break the wave into quads.
  1805. struct QuadData {
  1806. unsigned count;
  1807. PerPixelData *data[4];
  1808. };
  1809. std::map<uint32_t, QuadData> quads;
  1810. for (auto i = cur; i != groupEnd; ++i) {
  1811. // assuming that it is a simple wave, idGroups has a unique id for each entry.
  1812. uint32_t laneId = (*i).second->id;
  1813. uint32_t laneIds[4] = {(*i).second->id0, (*i).second->id1,
  1814. (*i).second->id2, (*i).second->id3};
  1815. // Since this is a simple wave, each lane has an unique id and
  1816. // therefore should not have any ids in there.
  1817. VERIFY_IS_TRUE(quads.find(laneId) == quads.end());
  1818. // check if QuadReadLaneAt is returning same values in a single quad.
  1819. bool newQuad = true;
  1820. for (unsigned quadIndex = 0; quadIndex < 4; ++quadIndex) {
  1821. auto match = quads.find(laneIds[quadIndex]);
  1822. if (match != quads.end()) {
  1823. (*match).second.data[(*match).second.count++] = (*i).second;
  1824. newQuad = false;
  1825. break;
  1826. }
  1827. auto quadMemberData = idGroups.find(laneIds[quadIndex]);
  1828. if (quadMemberData != idGroups.end()) {
  1829. VERIFY_IS_TRUE((*quadMemberData).second->id0 == laneIds[0]);
  1830. VERIFY_IS_TRUE((*quadMemberData).second->id1 == laneIds[1]);
  1831. VERIFY_IS_TRUE((*quadMemberData).second->id2 == laneIds[2]);
  1832. VERIFY_IS_TRUE((*quadMemberData).second->id3 == laneIds[3]);
  1833. }
  1834. }
  1835. if (newQuad) {
  1836. QuadData qdata;
  1837. qdata.count = 1;
  1838. qdata.data[0] = (*i).second;
  1839. quads.insert(std::make_pair(laneId, qdata));
  1840. }
  1841. }
  1842. for (auto quadPair : quads) {
  1843. unsigned count = quadPair.second.count;
  1844. // There could be only one pixel data on the edge of the triangle
  1845. if (count < 2) continue;
  1846. PerPixelData **data = quadPair.second.data;
  1847. bool isTop[4];
  1848. bool isLeft[4];
  1849. PerPixelData helperData;
  1850. memset(&helperData, sizeof(helperData), 0);
  1851. PerPixelData *layout[4]; // tl,tr,bl,br
  1852. memset(layout, sizeof(layout), 0);
  1853. auto fnToLayout = [&](bool top, bool left) -> PerPixelData ** {
  1854. int idx = top ? 0 : 2;
  1855. idx += left ? 0 : 1;
  1856. return &layout[idx];
  1857. };
  1858. auto fnToLayoutData = [&](bool top, bool left) -> PerPixelData * {
  1859. PerPixelData **pResult = fnToLayout(top, left);
  1860. if (*pResult == nullptr) return &helperData;
  1861. return *pResult;
  1862. };
  1863. VERIFY_IS_TRUE(count <= 4);
  1864. if (count == 2) {
  1865. isTop[0] = data[0]->position.y < data[1]->position.y;
  1866. isTop[1] = (data[0]->position.y == data[1]->position.y) ? isTop[0] : !isTop[0];
  1867. isLeft[0] = data[0]->position.x < data[1]->position.x;
  1868. isLeft[1] = (data[0]->position.x == data[1]->position.x) ? isLeft[0] : !isLeft[0];
  1869. }
  1870. else {
  1871. // with at least three samples, we have distinct x and y coordinates.
  1872. float left = std::min(data[0]->position.x, data[1]->position.x);
  1873. left = std::min(data[2]->position.x, left);
  1874. float top = std::min(data[0]->position.y, data[1]->position.y);
  1875. top = std::min(data[2]->position.y, top);
  1876. for (unsigned i = 0; i < count; ++i) {
  1877. isTop[i] = data[i]->position.y == top;
  1878. isLeft[i] = data[i]->position.x == left;
  1879. }
  1880. }
  1881. for (unsigned i = 0; i < count; ++i) {
  1882. *(fnToLayout(isTop[i], isLeft[i])) = data[i];
  1883. }
  1884. // Finally, we have a proper quad reconstructed. Validate.
  1885. for (unsigned i = 0; i < count; ++i) {
  1886. PerPixelData *d = data[i];
  1887. VERIFY_ARE_EQUAL(d->id0, fnToLayoutData(true, true)->id);
  1888. VERIFY_ARE_EQUAL(d->id1, fnToLayoutData(true, false)->id);
  1889. VERIFY_ARE_EQUAL(d->id2, fnToLayoutData(false, true)->id);
  1890. VERIFY_ARE_EQUAL(d->id3, fnToLayoutData(false, false)->id);
  1891. VERIFY_ARE_EQUAL(d->acrossX, fnToLayoutData(isTop[i], !isLeft[i])->id);
  1892. VERIFY_ARE_EQUAL(d->acrossY, fnToLayoutData(!isTop[i], isLeft[i])->id);
  1893. VERIFY_ARE_EQUAL(d->acrossDiag, fnToLayoutData(!isTop[i], !isLeft[i])->id);
  1894. VERIFY_ARE_EQUAL(d->quadActiveCount, count);
  1895. }
  1896. }
  1897. }
  1898. cur = groupEnd;
  1899. }
  1900. }
  1901. // TODO: provide validation for quads where the same pixel was shaded multiple times
  1902. //
  1903. // Consider: for pixels that were shaded multiple times, check whether
  1904. // some grouping of threads into quads satisfies all value requirements.
  1905. }
  1906. }
  1907. struct ShaderOpTestResult {
  1908. st::ShaderOp *ShaderOp;
  1909. std::shared_ptr<st::ShaderOpSet> ShaderOpSet;
  1910. std::shared_ptr<st::ShaderOpTest> Test;
  1911. };
  1912. struct SPrimitives {
  1913. float f_float;
  1914. float f_float2;
  1915. float f_float_o;
  1916. float f_float2_o;
  1917. };
  1918. std::shared_ptr<ShaderOpTestResult>
  1919. RunShaderOpTestAfterParse(ID3D12Device *pDevice, dxc::DxcDllSupport &support,
  1920. LPCSTR pName,
  1921. st::ShaderOpTest::TInitCallbackFn pInitCallback,
  1922. std::shared_ptr<st::ShaderOpSet> ShaderOpSet) {
  1923. st::ShaderOp *pShaderOp;
  1924. if (pName == nullptr) {
  1925. if (ShaderOpSet->ShaderOps.size() != 1) {
  1926. VERIFY_FAIL(L"Expected a single shader operation.");
  1927. }
  1928. pShaderOp = ShaderOpSet->ShaderOps[0].get();
  1929. }
  1930. else {
  1931. pShaderOp = ShaderOpSet->GetShaderOp(pName);
  1932. }
  1933. if (pShaderOp == nullptr) {
  1934. std::string msg = "Unable to find shader op ";
  1935. msg += pName;
  1936. msg += "; available ops";
  1937. const char sep = ':';
  1938. for (auto &pAvailOp : ShaderOpSet->ShaderOps) {
  1939. msg += sep;
  1940. msg += pAvailOp->Name ? pAvailOp->Name : "[n/a]";
  1941. }
  1942. CA2W msgWide(msg.c_str());
  1943. VERIFY_FAIL(msgWide.m_psz);
  1944. }
  1945. // This won't actually be used since we're supplying the device,
  1946. // but let's make it consistent.
  1947. pShaderOp->UseWarpDevice = GetTestParamUseWARP(true);
  1948. std::shared_ptr<st::ShaderOpTest> test = std::make_shared<st::ShaderOpTest>();
  1949. test->SetDxcSupport(&support);
  1950. test->SetInitCallback(pInitCallback);
  1951. test->SetDevice(pDevice);
  1952. test->RunShaderOp(pShaderOp);
  1953. std::shared_ptr<ShaderOpTestResult> result =
  1954. std::make_shared<ShaderOpTestResult>();
  1955. result->ShaderOpSet = ShaderOpSet;
  1956. result->Test = test;
  1957. result->ShaderOp = pShaderOp;
  1958. return result;
  1959. }
  1960. std::shared_ptr<ShaderOpTestResult>
  1961. RunShaderOpTest(ID3D12Device *pDevice, dxc::DxcDllSupport &support,
  1962. IStream *pStream, LPCSTR pName,
  1963. st::ShaderOpTest::TInitCallbackFn pInitCallback) {
  1964. DXASSERT_NOMSG(pStream != nullptr);
  1965. std::shared_ptr<st::ShaderOpSet> ShaderOpSet =
  1966. std::make_shared<st::ShaderOpSet>();
  1967. st::ParseShaderOpSetFromStream(pStream, ShaderOpSet.get());
  1968. return RunShaderOpTestAfterParse(pDevice, support, pName, pInitCallback, ShaderOpSet);
  1969. }
  1970. TEST_F(ExecutionTest, OutOfBoundsTest) {
  1971. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  1972. CComPtr<IStream> pStream;
  1973. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  1974. // Single operation test at the moment.
  1975. CComPtr<ID3D12Device> pDevice;
  1976. if (!CreateDevice(&pDevice))
  1977. return;
  1978. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(pDevice, m_support, pStream, "OOB", nullptr);
  1979. MappedData data;
  1980. // Read back to CPU and examine contents - should get pure red.
  1981. {
  1982. MappedData data;
  1983. test->Test->GetReadBackData("RTarget", &data);
  1984. const uint32_t *pPixels = (uint32_t *)data.data();
  1985. uint32_t first = *pPixels;
  1986. VERIFY_ARE_EQUAL(0xff0000ff, first); // pure red - only first component is read
  1987. }
  1988. }
  1989. TEST_F(ExecutionTest, SaturateTest) {
  1990. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  1991. CComPtr<IStream> pStream;
  1992. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  1993. // Single operation test at the moment.
  1994. CComPtr<ID3D12Device> pDevice;
  1995. if (!CreateDevice(&pDevice))
  1996. return;
  1997. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(pDevice, m_support, pStream, "Saturate", nullptr);
  1998. MappedData data;
  1999. test->Test->GetReadBackData("U0", &data);
  2000. const float *pValues = (float *)data.data();
  2001. // Everything is zero except for 1.5f and +Inf, which saturate to 1.0f
  2002. const float ExpectedCases[9] = {
  2003. 0.0f, 0.0f, 0.0f, 0.0f, // -inf, -1.5, -denorm, -0
  2004. 0.0f, 0.0f, 1.0f, 1.0f, // 0, denorm, 1.5f, inf
  2005. 0.0f // nan
  2006. };
  2007. for (size_t i = 0; i < _countof(ExpectedCases); ++i) {
  2008. VERIFY_IS_TRUE(ifdenorm_flushf_eq(*pValues, ExpectedCases[i]));
  2009. ++pValues;
  2010. }
  2011. }
  2012. void ExecutionTest::BasicTriangleTestSetup(LPCSTR ShaderOpName, LPCWSTR FileName, D3D_SHADER_MODEL testModel) {
  2013. #ifdef _HLK_CONF
  2014. UNREFERENCED_PARAMETER(ShaderOpName);
  2015. UNREFERENCED_PARAMETER(FileName);
  2016. UNREFERENCED_PARAMETER(testModel);
  2017. #else
  2018. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2019. CComPtr<IStream> pStream;
  2020. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2021. // Single operation test at the moment.
  2022. CComPtr<ID3D12Device> pDevice;
  2023. if (!CreateDevice(&pDevice, testModel))
  2024. return;
  2025. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(pDevice, m_support, pStream, ShaderOpName, nullptr);
  2026. MappedData data;
  2027. D3D12_RESOURCE_DESC &D = test->ShaderOp->GetResourceByName("RTarget")->Desc;
  2028. UINT width = (UINT64)D.Width;
  2029. UINT height = (UINT64)D.Height;
  2030. test->Test->GetReadBackData("RTarget", &data);
  2031. const uint32_t *pPixels = (uint32_t *)data.data();
  2032. if (SaveImages()) {
  2033. SavePixelsToFile(pPixels, DXGI_FORMAT_R8G8B8A8_UNORM, 320, 200, FileName);
  2034. }
  2035. uint32_t top = pPixels[width / 2]; // Top center.
  2036. uint32_t mid = pPixels[width / 2 + width * (height / 2)]; // Middle center.
  2037. VERIFY_ARE_EQUAL(0xff663300, top); // clear color
  2038. VERIFY_ARE_EQUAL(0xffffffff, mid); // white
  2039. // This is the basic validation test for shader operations, so it's good to
  2040. // check this here at least for this one test case.
  2041. data.reset();
  2042. test.reset();
  2043. ReportLiveObjects();
  2044. #endif
  2045. }
  2046. TEST_F(ExecutionTest, BasicTriangleOpTest) {
  2047. BasicTriangleTestSetup("Triangle", L"basic-triangle.bmp", D3D_SHADER_MODEL_6_0);
  2048. }
  2049. TEST_F(ExecutionTest, BasicTriangleOpTestHalf) {
  2050. BasicTriangleTestSetup("TriangleHalf", L"basic-triangle-half.bmp", D3D_SHADER_MODEL_6_2);
  2051. }
  2052. // Rendering two right triangles forming a square and assigning a texture value
  2053. // for each pixel to calculate derivates.
  2054. TEST_F(ExecutionTest, PartialDerivTest) {
  2055. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2056. CComPtr<IStream> pStream;
  2057. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2058. CComPtr<ID3D12Device> pDevice;
  2059. if (!CreateDevice(&pDevice))
  2060. return;
  2061. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(pDevice, m_support, pStream, "DerivFine", nullptr);
  2062. MappedData data;
  2063. D3D12_RESOURCE_DESC &D = test->ShaderOp->GetResourceByName("RTarget")->Desc;
  2064. UINT width = (UINT)D.Width;
  2065. UINT height = D.Height;
  2066. UINT pixelSize = GetByteSizeForFormat(D.Format) / 4;
  2067. test->Test->GetReadBackData("RTarget", &data);
  2068. const float *pPixels = (float *)data.data();
  2069. UINT centerIndex = (UINT64)width * height / 2 - width / 2;
  2070. // pixel at the center
  2071. UINT offsetCenter = centerIndex * pixelSize;
  2072. float CenterDDXFine = pPixels[offsetCenter];
  2073. float CenterDDYFine = pPixels[offsetCenter + 1];
  2074. float CenterDDXCoarse = pPixels[offsetCenter + 2];
  2075. float CenterDDYCoarse = pPixels[offsetCenter + 3];
  2076. LogCommentFmt(
  2077. L"center ddx_fine: %8f, ddy_fine: %8f, ddx_coarse: %8f, ddy_coarse: %8f",
  2078. CenterDDXFine, CenterDDYFine, CenterDDXCoarse, CenterDDYCoarse);
  2079. // The texture for the 9 pixels in the center should look like the following
  2080. // 256 32 64
  2081. // 2048 256 512
  2082. // 1 .125 .25
  2083. // In D3D12 there is no guarantee of how the adapter is grouping 2x2 pixels
  2084. // So for fine derivatives there can be up to two possible results for the center pixel,
  2085. // while for coarse derivatives there can be up to six possible results.
  2086. int ulpTolerance = 1;
  2087. // 512 - 256 or 2048 - 256
  2088. bool left = CompareFloatULP(CenterDDXFine, -1792.0f, ulpTolerance);
  2089. VERIFY_IS_TRUE(left || CompareFloatULP(CenterDDXFine, 256.0f, ulpTolerance));
  2090. // 256 - 32 or 256 - .125
  2091. bool top = CompareFloatULP(CenterDDYFine, 224.0f, ulpTolerance);
  2092. VERIFY_IS_TRUE(top || CompareFloatULP(CenterDDYFine, -255.875, ulpTolerance));
  2093. if (top && left) {
  2094. VERIFY_IS_TRUE((CompareFloatULP(CenterDDXCoarse, -224.0f, ulpTolerance) ||
  2095. CompareFloatULP(CenterDDXCoarse, -1792.0f, ulpTolerance)) &&
  2096. (CompareFloatULP(CenterDDYCoarse, 224.0f, ulpTolerance) ||
  2097. CompareFloatULP(CenterDDYCoarse, 1792.0f, ulpTolerance)));
  2098. }
  2099. else if (top) { // top right quad
  2100. VERIFY_IS_TRUE((CompareFloatULP(CenterDDXCoarse, 256.0f, ulpTolerance) ||
  2101. CompareFloatULP(CenterDDXCoarse, 32.0f, ulpTolerance)) &&
  2102. (CompareFloatULP(CenterDDYCoarse, 224.0f, ulpTolerance) ||
  2103. CompareFloatULP(CenterDDYCoarse, 448.0f, ulpTolerance)));
  2104. }
  2105. else if (left) { // bottom left quad
  2106. VERIFY_IS_TRUE((CompareFloatULP(CenterDDXCoarse, -1792.0f, ulpTolerance) ||
  2107. CompareFloatULP(CenterDDXCoarse, -.875f, ulpTolerance)) &&
  2108. (CompareFloatULP(CenterDDYCoarse, -2047.0f, ulpTolerance) ||
  2109. CompareFloatULP(CenterDDYCoarse, -255.875f, ulpTolerance)));
  2110. }
  2111. else { // bottom right
  2112. VERIFY_IS_TRUE((CompareFloatULP(CenterDDXCoarse, 256.0f, ulpTolerance) ||
  2113. CompareFloatULP(CenterDDXCoarse, .125f, ulpTolerance)) &&
  2114. (CompareFloatULP(CenterDDYCoarse, -255.875f, ulpTolerance) ||
  2115. CompareFloatULP(CenterDDYCoarse, -511.75f, ulpTolerance)));
  2116. }
  2117. }
  2118. // Executing a simple binop to verify shadel model 6.1 support; runs with
  2119. // ShaderModel61.CoreRequirement
  2120. TEST_F(ExecutionTest, BasicShaderModel61) {
  2121. RunBasicShaderModelTest(D3D_SHADER_MODEL_6_1);
  2122. }
  2123. // Executing a simple binop to verify shadel model 6.3 support; runs with
  2124. // ShaderModel63.CoreRequirement
  2125. TEST_F(ExecutionTest, BasicShaderModel63) {
  2126. RunBasicShaderModelTest(D3D_SHADER_MODEL_6_3);
  2127. }
  2128. void ExecutionTest::RunBasicShaderModelTest(D3D_SHADER_MODEL shaderModel) {
  2129. WEX::TestExecution::SetVerifyOutput verifySettings(
  2130. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  2131. CComPtr<ID3D12Device> pDevice;
  2132. if (!CreateDevice(&pDevice, shaderModel)) {
  2133. return;
  2134. }
  2135. char *pShaderModelStr;
  2136. if (shaderModel == D3D_SHADER_MODEL_6_1) {
  2137. pShaderModelStr = "cs_6_1";
  2138. } else if (shaderModel == D3D_SHADER_MODEL_6_3) {
  2139. pShaderModelStr = "cs_6_3";
  2140. } else {
  2141. DXASSERT_NOMSG("Invalid Shader Model Parameter");
  2142. pShaderModelStr = nullptr;
  2143. }
  2144. const char shaderTemplate[] =
  2145. "struct SBinaryOp { %s input1; %s input2; %s output; };"
  2146. "RWStructuredBuffer<SBinaryOp> g_buf : register(u0);"
  2147. "[numthreads(8,8,1)]"
  2148. "void main(uint GI : SV_GroupIndex) {"
  2149. " SBinaryOp l = g_buf[GI];"
  2150. " l.output = l.input1 + l.input2;"
  2151. " g_buf[GI] = l;"
  2152. "}";
  2153. char shader[sizeof(shaderTemplate) + 50];
  2154. // Run simple shader with float data types
  2155. char* sTy = "float";
  2156. float inputFloatPairs[] = { 1.5f, -2.8f, 3.23e-5f, 6.0f, 181.621f, 14.978f };
  2157. VERIFY_IS_TRUE(sprintf(shader, shaderTemplate, sTy, sTy, sTy) > 0);
  2158. WEX::Logging::Log::Comment(L"BasicShaderModel float");
  2159. RunBasicShaderModelTest<float>(pDevice, pShaderModelStr, shader, inputFloatPairs, sizeof(inputFloatPairs) / (2 * sizeof(float)));
  2160. // Run simple shader with double data types
  2161. if (DoesDeviceSupportDouble(pDevice)) {
  2162. sTy = "double";
  2163. double inputDoublePairs[] = { 1.5891020, -2.8, 3.23e-5, 1 / 3, 181.91621, 14.654978 };
  2164. VERIFY_IS_TRUE(sprintf(shader, shaderTemplate, sTy, sTy, sTy) > 0);
  2165. WEX::Logging::Log::Comment(L"BasicShaderModel double");
  2166. RunBasicShaderModelTest<double>(pDevice, pShaderModelStr, shader, inputDoublePairs, sizeof(inputDoublePairs) / (2 * sizeof(double)));
  2167. }
  2168. else {
  2169. // Optional feature, so it's correct to not support it if declared as such.
  2170. WEX::Logging::Log::Comment(L"Device does not support double operations.");
  2171. }
  2172. // Run simple shader with int64 types
  2173. if (DoesDeviceSupportInt64(pDevice)) {
  2174. sTy = "int64_t";
  2175. int64_t inputInt64Pairs[] = { 1, -100, 6814684, -9814810, 654, 1021248900 };
  2176. VERIFY_IS_TRUE(sprintf(shader, shaderTemplate, sTy, sTy, sTy) > 0);
  2177. WEX::Logging::Log::Comment(L"BasicShaderModel int64_t");
  2178. RunBasicShaderModelTest<int64_t>(pDevice, pShaderModelStr, shader, inputInt64Pairs, sizeof(inputInt64Pairs) / (2 * sizeof(int64_t)));
  2179. }
  2180. else {
  2181. // Optional feature, so it's correct to not support it if declared as such.
  2182. WEX::Logging::Log::Comment(L"Device does not support int64 operations.");
  2183. }
  2184. }
  2185. template <class Ty>
  2186. const wchar_t* ExecutionTest::BasicShaderModelTest_GetFormatString() {
  2187. DXASSERT_NOMSG("Unsupported type");
  2188. return "";
  2189. }
  2190. template <>
  2191. const wchar_t* ExecutionTest::BasicShaderModelTest_GetFormatString<float>() {
  2192. return L"element #%u: input1 = %6.8f, input1 = %6.8f, output = %6.8f, expected = %6.8f";
  2193. }
  2194. template <>
  2195. const wchar_t* ExecutionTest::BasicShaderModelTest_GetFormatString<double>() {
  2196. return BasicShaderModelTest_GetFormatString<float>();
  2197. }
  2198. template <>
  2199. const wchar_t* ExecutionTest::BasicShaderModelTest_GetFormatString<int64_t>() {
  2200. return L"element #%u: input1 = %ld, input1 = %ld, output = %ld, expected = %ld";
  2201. }
  2202. template <class Ty>
  2203. void ExecutionTest::RunBasicShaderModelTest(CComPtr<ID3D12Device> pDevice, const char *pShaderModelStr, const char *pShader,
  2204. Ty *pInputDataPairs, unsigned inputDataCount) {
  2205. struct SBinaryOp {
  2206. Ty input1;
  2207. Ty input2;
  2208. Ty output;
  2209. };
  2210. CComPtr<IStream> pStream;
  2211. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  2212. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  2213. pDevice, m_support, pStream, "BinaryFPOp",
  2214. // this callbacked is called when the test is creating the resource to run the test
  2215. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  2216. UNREFERENCED_PARAMETER(Name);
  2217. pShaderOp->Shaders.at(0).Target = pShaderModelStr;
  2218. pShaderOp->Shaders.at(0).Text = pShader;
  2219. size_t size = sizeof(SBinaryOp) * inputDataCount;
  2220. Data.resize(size);
  2221. SBinaryOp *pPrimitives = (SBinaryOp*)Data.data();
  2222. Ty *pIn = pInputDataPairs;
  2223. for (size_t i = 0; i < inputDataCount; i++, pIn += 2) {
  2224. SBinaryOp *p = &pPrimitives[i];
  2225. p->input1 = pIn[0];
  2226. p->input2 = pIn[1];
  2227. }
  2228. });
  2229. VERIFY_SUCCEEDED(S_OK);
  2230. MappedData data;
  2231. test->Test->GetReadBackData("SBinaryFPOp", &data);
  2232. SBinaryOp *pPrimitives = (SBinaryOp*)data.data();
  2233. const wchar_t* formatStr = BasicShaderModelTest_GetFormatString<Ty>();
  2234. Ty *pIn = pInputDataPairs;
  2235. for (unsigned i = 0; i < inputDataCount; i++, pIn += 2) {
  2236. Ty expValue = pIn[0] + pIn[1];
  2237. SBinaryOp *p = &pPrimitives[i];
  2238. LogCommentFmt(formatStr, i, pIn[0], pIn[1], p->output, expValue);
  2239. VERIFY_ARE_EQUAL(p->output, expValue);
  2240. }
  2241. }
  2242. // Resource structure for data-driven tests.
  2243. struct SUnaryFPOp {
  2244. float input;
  2245. float output;
  2246. };
  2247. struct SBinaryFPOp {
  2248. float input1;
  2249. float input2;
  2250. float output1;
  2251. float output2;
  2252. };
  2253. struct STertiaryFPOp {
  2254. float input1;
  2255. float input2;
  2256. float input3;
  2257. float output;
  2258. };
  2259. struct SUnaryHalfOp {
  2260. uint16_t input;
  2261. uint16_t output;
  2262. };
  2263. struct SBinaryHalfOp {
  2264. uint16_t input1;
  2265. uint16_t input2;
  2266. uint16_t output1;
  2267. uint16_t output2;
  2268. };
  2269. struct STertiaryHalfOp {
  2270. uint16_t input1;
  2271. uint16_t input2;
  2272. uint16_t input3;
  2273. uint16_t output;
  2274. };
  2275. struct SUnaryIntOp {
  2276. int input;
  2277. int output;
  2278. };
  2279. struct SUnaryUintOp {
  2280. unsigned int input;
  2281. unsigned int output;
  2282. };
  2283. struct SBinaryIntOp {
  2284. int input1;
  2285. int input2;
  2286. int output1;
  2287. int output2;
  2288. };
  2289. struct STertiaryIntOp {
  2290. int input1;
  2291. int input2;
  2292. int input3;
  2293. int output;
  2294. };
  2295. struct SBinaryUintOp {
  2296. unsigned int input1;
  2297. unsigned int input2;
  2298. unsigned int output1;
  2299. unsigned int output2;
  2300. };
  2301. struct STertiaryUintOp {
  2302. unsigned int input1;
  2303. unsigned int input2;
  2304. unsigned int input3;
  2305. unsigned int output;
  2306. };
  2307. struct SUnaryInt16Op {
  2308. short input;
  2309. short output;
  2310. };
  2311. struct SUnaryUint16Op {
  2312. unsigned short input;
  2313. unsigned short output;
  2314. };
  2315. struct SBinaryInt16Op {
  2316. short input1;
  2317. short input2;
  2318. short output1;
  2319. short output2;
  2320. };
  2321. struct STertiaryInt16Op {
  2322. short input1;
  2323. short input2;
  2324. short input3;
  2325. short output;
  2326. };
  2327. struct SBinaryUint16Op {
  2328. unsigned short input1;
  2329. unsigned short input2;
  2330. unsigned short output1;
  2331. unsigned short output2;
  2332. };
  2333. struct STertiaryUint16Op {
  2334. unsigned short input1;
  2335. unsigned short input2;
  2336. unsigned short input3;
  2337. unsigned short output;
  2338. };
  2339. // representation for HLSL float vectors
  2340. struct SDotOp {
  2341. XMFLOAT4 input1;
  2342. XMFLOAT4 input2;
  2343. float o_dot2;
  2344. float o_dot3;
  2345. float o_dot4;
  2346. };
  2347. struct SMsad4 {
  2348. unsigned int ref;
  2349. XMUINT2 src;
  2350. XMUINT4 accum;
  2351. XMUINT4 result;
  2352. };
  2353. // Parameter representation for taef data-driven tests
  2354. struct TableParameter {
  2355. LPCWSTR m_name;
  2356. enum TableParameterType {
  2357. INT8,
  2358. INT16,
  2359. INT32,
  2360. UINT,
  2361. FLOAT,
  2362. HALF,
  2363. DOUBLE,
  2364. STRING,
  2365. BOOL,
  2366. INT8_TABLE,
  2367. INT16_TABLE,
  2368. INT32_TABLE,
  2369. FLOAT_TABLE,
  2370. HALF_TABLE,
  2371. DOUBLE_TABLE,
  2372. STRING_TABLE,
  2373. UINT8_TABLE,
  2374. UINT16_TABLE,
  2375. UINT32_TABLE,
  2376. BOOL_TABLE
  2377. };
  2378. TableParameterType m_type;
  2379. bool m_required; // required parameter
  2380. int8_t m_int8;
  2381. int16_t m_int16;
  2382. int m_int32;
  2383. unsigned int m_uint;
  2384. float m_float;
  2385. uint16_t m_half; // no such thing as half type in c++. Use int16 instead
  2386. double m_double;
  2387. bool m_bool;
  2388. WEX::Common::String m_str;
  2389. std::vector<int8_t> m_int8Table;
  2390. std::vector<int16_t> m_int16Table;
  2391. std::vector<int> m_int32Table;
  2392. std::vector<uint8_t> m_uint8Table;
  2393. std::vector<uint16_t> m_uint16Table;
  2394. std::vector<unsigned int> m_uint32Table;
  2395. std::vector<float> m_floatTable;
  2396. std::vector<uint16_t> m_halfTable; // no such thing as half type in c++
  2397. std::vector<double> m_doubleTable;
  2398. std::vector<bool> m_boolTable;
  2399. std::vector<WEX::Common::String> m_StringTable;
  2400. };
  2401. class TableParameterHandler {
  2402. private:
  2403. HRESULT ParseTableRow();
  2404. public:
  2405. TableParameter* m_table;
  2406. size_t m_tableSize;
  2407. TableParameterHandler(TableParameter *pTable, size_t size) : m_table(pTable), m_tableSize(size) {
  2408. clearTableParameter();
  2409. VERIFY_SUCCEEDED(ParseTableRow());
  2410. }
  2411. TableParameter* GetTableParamByName(LPCWSTR name) {
  2412. for (size_t i = 0; i < m_tableSize; ++i) {
  2413. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2414. return &m_table[i];
  2415. }
  2416. }
  2417. DXASSERT_ARGS(false, "Invalid Table Parameter Name %s", name);
  2418. return nullptr;
  2419. }
  2420. void clearTableParameter() {
  2421. for (size_t i = 0; i < m_tableSize; ++i) {
  2422. m_table[i].m_int32 = 0;
  2423. m_table[i].m_uint = 0;
  2424. m_table[i].m_double = 0;
  2425. m_table[i].m_bool = false;
  2426. m_table[i].m_str = WEX::Common::String();
  2427. }
  2428. }
  2429. template <class T1>
  2430. std::vector<T1> *GetDataArray(LPCWSTR name) {
  2431. return nullptr;
  2432. }
  2433. template <>
  2434. std::vector<int> *GetDataArray(LPCWSTR name) {
  2435. for (size_t i = 0; i < m_tableSize; ++i) {
  2436. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2437. return &(m_table[i].m_int32Table);
  2438. }
  2439. }
  2440. DXASSERT_ARGS(false, "Invalid Table Parameter Name %s", name);
  2441. return nullptr;
  2442. }
  2443. template <>
  2444. std::vector<int8_t> *GetDataArray(LPCWSTR name) {
  2445. for (size_t i = 0; i < m_tableSize; ++i) {
  2446. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2447. return &(m_table[i].m_int8Table);
  2448. }
  2449. }
  2450. DXASSERT_ARGS(false, "Invalid Table Parameter Name %s", name);
  2451. return nullptr;
  2452. }
  2453. template <>
  2454. std::vector<int16_t> *GetDataArray(LPCWSTR name) {
  2455. for (size_t i = 0; i < m_tableSize; ++i) {
  2456. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2457. return &(m_table[i].m_int16Table);
  2458. }
  2459. }
  2460. DXASSERT_ARGS(false, "Invalid Table Parameter Name %s", name);
  2461. return nullptr;
  2462. }
  2463. template <>
  2464. std::vector<unsigned int> *GetDataArray(LPCWSTR name) {
  2465. for (size_t i = 0; i < m_tableSize; ++i) {
  2466. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2467. return &(m_table[i].m_uint32Table);
  2468. }
  2469. }
  2470. DXASSERT_ARGS(false, "Invalid Table Parameter Name %s", name);
  2471. return nullptr;
  2472. }
  2473. template <>
  2474. std::vector<float> *GetDataArray(LPCWSTR name) {
  2475. for (size_t i = 0; i < m_tableSize; ++i) {
  2476. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2477. return &(m_table[i].m_floatTable);
  2478. }
  2479. }
  2480. DXASSERT_ARGS(false, "Invalid Table Parameter Name %s", name);
  2481. return nullptr;
  2482. }
  2483. // TODO: uin16_t may be used to represent two different types when we introduce uint16
  2484. template <>
  2485. std::vector<uint16_t> *GetDataArray(LPCWSTR name) {
  2486. for (size_t i = 0; i < m_tableSize; ++i) {
  2487. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2488. return &(m_table[i].m_halfTable);
  2489. }
  2490. }
  2491. DXASSERT_ARGS(false, "Invalid Table Parameter Name %s", name);
  2492. return nullptr;
  2493. }
  2494. template <>
  2495. std::vector<double> *GetDataArray(LPCWSTR name) {
  2496. for (size_t i = 0; i < m_tableSize; ++i) {
  2497. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2498. return &(m_table[i].m_doubleTable);
  2499. }
  2500. }
  2501. DXASSERT_ARGS(false, "Invalid Table Parameter Name %s", name);
  2502. return nullptr;
  2503. }
  2504. template <>
  2505. std::vector<bool> *GetDataArray(LPCWSTR name) {
  2506. for (size_t i = 0; i < m_tableSize; ++i) {
  2507. if (_wcsicmp(name, m_table[i].m_name) == 0) {
  2508. return &(m_table[i].m_boolTable);
  2509. }
  2510. }
  2511. DXASSERT_ARGS(false, "Invalid Table Parameter Name %s", name);
  2512. return nullptr;
  2513. }
  2514. };
  2515. static TableParameter UnaryFPOpParameters[] = {
  2516. { L"ShaderOp.Target", TableParameter::STRING, true },
  2517. { L"ShaderOp.Text", TableParameter::STRING, true },
  2518. { L"Validation.Input1", TableParameter::FLOAT_TABLE, true },
  2519. { L"Validation.Expected1", TableParameter::FLOAT_TABLE, true },
  2520. { L"Validation.Type", TableParameter::STRING, true },
  2521. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2522. { L"Warp.Version", TableParameter::UINT, false }
  2523. };
  2524. static TableParameter BinaryFPOpParameters[] = {
  2525. { L"ShaderOp.Target", TableParameter::STRING, true },
  2526. { L"ShaderOp.Text", TableParameter::STRING, true },
  2527. { L"Validation.Input1", TableParameter::FLOAT_TABLE, true },
  2528. { L"Validation.Input2", TableParameter::FLOAT_TABLE, true },
  2529. { L"Validation.Expected1", TableParameter::FLOAT_TABLE, true },
  2530. { L"Validation.Expected2", TableParameter::FLOAT_TABLE, false },
  2531. { L"Validation.Type", TableParameter::STRING, true },
  2532. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2533. };
  2534. static TableParameter TertiaryFPOpParameters[] = {
  2535. { L"ShaderOp.Target", TableParameter::STRING, true },
  2536. { L"ShaderOp.Text", TableParameter::STRING, true },
  2537. { L"Validation.Input1", TableParameter::FLOAT_TABLE, true },
  2538. { L"Validation.Input2", TableParameter::FLOAT_TABLE, true },
  2539. { L"Validation.Input3", TableParameter::FLOAT_TABLE, true },
  2540. { L"Validation.Expected1", TableParameter::FLOAT_TABLE, true },
  2541. { L"Validation.Type", TableParameter::STRING, true },
  2542. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2543. };
  2544. static TableParameter UnaryHalfOpParameters[] = {
  2545. { L"ShaderOp.Target", TableParameter::STRING, true },
  2546. { L"ShaderOp.Text", TableParameter::STRING, true },
  2547. { L"ShaderOp.Arguments", TableParameter::STRING, true },
  2548. { L"Validation.Input1", TableParameter::HALF_TABLE, true },
  2549. { L"Validation.Expected1", TableParameter::HALF_TABLE, true },
  2550. { L"Validation.Type", TableParameter::STRING, true },
  2551. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2552. { L"Warp.Version", TableParameter::UINT, false }
  2553. };
  2554. static TableParameter BinaryHalfOpParameters[] = {
  2555. { L"ShaderOp.Target", TableParameter::STRING, true },
  2556. { L"ShaderOp.Text", TableParameter::STRING, true },
  2557. { L"ShaderOp.Arguments", TableParameter::STRING, true },
  2558. { L"Validation.Input1", TableParameter::HALF_TABLE, true },
  2559. { L"Validation.Input2", TableParameter::HALF_TABLE, true },
  2560. { L"Validation.Expected1", TableParameter::HALF_TABLE, true },
  2561. { L"Validation.Expected2", TableParameter::HALF_TABLE, false },
  2562. { L"Validation.Type", TableParameter::STRING, true },
  2563. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2564. };
  2565. static TableParameter TertiaryHalfOpParameters[] = {
  2566. { L"ShaderOp.Target", TableParameter::STRING, true },
  2567. { L"ShaderOp.Text", TableParameter::STRING, true },
  2568. { L"ShaderOp.Arguments", TableParameter::STRING, true },
  2569. { L"Validation.Input1", TableParameter::HALF_TABLE, true },
  2570. { L"Validation.Input2", TableParameter::HALF_TABLE, true },
  2571. { L"Validation.Input3", TableParameter::HALF_TABLE, true },
  2572. { L"Validation.Expected1", TableParameter::HALF_TABLE, true },
  2573. { L"Validation.Type", TableParameter::STRING, true },
  2574. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2575. };
  2576. static TableParameter UnaryIntOpParameters[] = {
  2577. { L"ShaderOp.Target", TableParameter::STRING, true },
  2578. { L"ShaderOp.Text", TableParameter::STRING, true },
  2579. { L"Validation.Input1", TableParameter::INT32_TABLE, true },
  2580. { L"Validation.Expected1", TableParameter::INT32_TABLE, true },
  2581. { L"Validation.Tolerance", TableParameter::INT32, true },
  2582. };
  2583. static TableParameter UnaryUintOpParameters[] = {
  2584. { L"ShaderOp.Target", TableParameter::STRING, true },
  2585. { L"ShaderOp.Text", TableParameter::STRING, true },
  2586. { L"Validation.Input1", TableParameter::UINT32_TABLE, true },
  2587. { L"Validation.Expected1", TableParameter::UINT32_TABLE, true },
  2588. { L"Validation.Tolerance", TableParameter::INT32, true },
  2589. };
  2590. static TableParameter BinaryIntOpParameters[] = {
  2591. { L"ShaderOp.Target", TableParameter::STRING, true },
  2592. { L"ShaderOp.Text", TableParameter::STRING, true },
  2593. { L"Validation.Input1", TableParameter::INT32_TABLE, true },
  2594. { L"Validation.Input2", TableParameter::INT32_TABLE, true },
  2595. { L"Validation.Expected1", TableParameter::INT32_TABLE, true },
  2596. { L"Validation.Expected2", TableParameter::INT32_TABLE, false },
  2597. { L"Validation.Tolerance", TableParameter::INT32, true },
  2598. };
  2599. static TableParameter TertiaryIntOpParameters[] = {
  2600. { L"ShaderOp.Target", TableParameter::STRING, true },
  2601. { L"ShaderOp.Text", TableParameter::STRING, true },
  2602. { L"Validation.Input1", TableParameter::INT32_TABLE, true },
  2603. { L"Validation.Input2", TableParameter::INT32_TABLE, true },
  2604. { L"Validation.Input3", TableParameter::INT32_TABLE, true },
  2605. { L"Validation.Expected1", TableParameter::INT32_TABLE, true },
  2606. { L"Validation.Tolerance", TableParameter::INT32, true },
  2607. };
  2608. static TableParameter BinaryUintOpParameters[] = {
  2609. { L"ShaderOp.Target", TableParameter::STRING, true },
  2610. { L"ShaderOp.Text", TableParameter::STRING, true },
  2611. { L"Validation.Input1", TableParameter::UINT32_TABLE, true },
  2612. { L"Validation.Input2", TableParameter::UINT32_TABLE, true },
  2613. { L"Validation.Expected1", TableParameter::UINT32_TABLE, true },
  2614. { L"Validation.Expected2", TableParameter::UINT32_TABLE, false },
  2615. { L"Validation.Tolerance", TableParameter::INT32, true },
  2616. };
  2617. static TableParameter TertiaryUintOpParameters[] = {
  2618. { L"ShaderOp.Target", TableParameter::STRING, true },
  2619. { L"ShaderOp.Text", TableParameter::STRING, true },
  2620. { L"Validation.Input1", TableParameter::UINT32_TABLE, true },
  2621. { L"Validation.Input2", TableParameter::UINT32_TABLE, true },
  2622. { L"Validation.Input3", TableParameter::UINT32_TABLE, true },
  2623. { L"Validation.Expected1", TableParameter::UINT32_TABLE, true },
  2624. { L"Validation.Tolerance", TableParameter::INT32, true },
  2625. };
  2626. static TableParameter UnaryInt16OpParameters[] = {
  2627. { L"ShaderOp.Target", TableParameter::STRING, true },
  2628. { L"ShaderOp.Text", TableParameter::STRING, true },
  2629. { L"ShaderOp.Arguments", TableParameter::STRING, true },
  2630. { L"Validation.Input1", TableParameter::INT16_TABLE, true },
  2631. { L"Validation.Expected1", TableParameter::INT16_TABLE, true },
  2632. { L"Validation.Tolerance", TableParameter::INT32, true },
  2633. };
  2634. static TableParameter UnaryUint16OpParameters[] = {
  2635. { L"ShaderOp.Target", TableParameter::STRING, true },
  2636. { L"ShaderOp.Text", TableParameter::STRING, true },
  2637. { L"ShaderOp.Arguments", TableParameter::STRING, true },
  2638. { L"Validation.Input1", TableParameter::UINT16_TABLE, true },
  2639. { L"Validation.Expected1", TableParameter::UINT16_TABLE, true },
  2640. { L"Validation.Tolerance", TableParameter::INT32, true },
  2641. };
  2642. static TableParameter BinaryInt16OpParameters[] = {
  2643. { L"ShaderOp.Target", TableParameter::STRING, true },
  2644. { L"ShaderOp.Text", TableParameter::STRING, true },
  2645. { L"ShaderOp.Arguments", TableParameter::STRING, true },
  2646. { L"Validation.Input1", TableParameter::INT16_TABLE, true },
  2647. { L"Validation.Input2", TableParameter::INT16_TABLE, true },
  2648. { L"Validation.Expected1", TableParameter::INT16_TABLE, true },
  2649. { L"Validation.Expected2", TableParameter::INT16_TABLE, false },
  2650. { L"Validation.Tolerance", TableParameter::INT32, true },
  2651. };
  2652. static TableParameter TertiaryInt16OpParameters[] = {
  2653. { L"ShaderOp.Target", TableParameter::STRING, true },
  2654. { L"ShaderOp.Text", TableParameter::STRING, true },
  2655. { L"ShaderOp.Arguments", TableParameter::STRING, true },
  2656. { L"Validation.Input1", TableParameter::INT16_TABLE, true },
  2657. { L"Validation.Input2", TableParameter::INT16_TABLE, true },
  2658. { L"Validation.Input3", TableParameter::INT16_TABLE, true },
  2659. { L"Validation.Expected1", TableParameter::INT16_TABLE, true },
  2660. { L"Validation.Tolerance", TableParameter::INT32, true },
  2661. };
  2662. static TableParameter BinaryUint16OpParameters[] = {
  2663. { L"ShaderOp.Target", TableParameter::STRING, true },
  2664. { L"ShaderOp.Text", TableParameter::STRING, true },
  2665. { L"ShaderOp.Arguments", TableParameter::STRING, true },
  2666. { L"Validation.Input1", TableParameter::UINT16_TABLE, true },
  2667. { L"Validation.Input2", TableParameter::UINT16_TABLE, true },
  2668. { L"Validation.Expected1", TableParameter::UINT16_TABLE, true },
  2669. { L"Validation.Expected2", TableParameter::UINT16_TABLE, false },
  2670. { L"Validation.Tolerance", TableParameter::INT32, true },
  2671. };
  2672. static TableParameter TertiaryUint16OpParameters[] = {
  2673. { L"ShaderOp.Target", TableParameter::STRING, true },
  2674. { L"ShaderOp.Text", TableParameter::STRING, true },
  2675. { L"ShaderOp.Arguments", TableParameter::STRING, true },
  2676. { L"Validation.Input1", TableParameter::UINT16_TABLE, true },
  2677. { L"Validation.Input2", TableParameter::UINT16_TABLE, true },
  2678. { L"Validation.Input3", TableParameter::UINT16_TABLE, true },
  2679. { L"Validation.Expected1", TableParameter::UINT16_TABLE, true },
  2680. { L"Validation.Tolerance", TableParameter::INT32, true },
  2681. };
  2682. static TableParameter DotOpParameters[] = {
  2683. { L"ShaderOp.Target", TableParameter::STRING, true },
  2684. { L"ShaderOp.Text", TableParameter::STRING, true },
  2685. { L"Validation.Input1", TableParameter::STRING_TABLE, true },
  2686. { L"Validation.Input2", TableParameter::STRING_TABLE, true },
  2687. { L"Validation.Expected1", TableParameter::STRING_TABLE, true },
  2688. { L"Validation.Expected2", TableParameter::STRING_TABLE, true },
  2689. { L"Validation.Expected3", TableParameter::STRING_TABLE, true },
  2690. { L"Validation.Type", TableParameter::STRING, true },
  2691. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2692. };
  2693. static TableParameter Msad4OpParameters[] = {
  2694. { L"ShaderOp.Text", TableParameter::STRING, true },
  2695. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2696. { L"Validation.Input1", TableParameter::UINT32_TABLE, true},
  2697. { L"Validation.Input2", TableParameter::STRING_TABLE, true },
  2698. { L"Validation.Input3", TableParameter::STRING_TABLE, true },
  2699. { L"Validation.Expected1", TableParameter::STRING_TABLE, true }
  2700. };
  2701. static TableParameter WaveIntrinsicsActiveIntParameters[] = {
  2702. { L"ShaderOp.Name", TableParameter::STRING, true },
  2703. { L"ShaderOp.Text", TableParameter::STRING, true },
  2704. { L"Validation.NumInputSet", TableParameter::UINT, true },
  2705. { L"Validation.InputSet1", TableParameter::INT32_TABLE, true },
  2706. { L"Validation.InputSet2", TableParameter::INT32_TABLE, false },
  2707. { L"Validation.InputSet3", TableParameter::INT32_TABLE, false },
  2708. { L"Validation.InputSet4", TableParameter::INT32_TABLE, false }
  2709. };
  2710. static TableParameter WaveIntrinsicsPrefixIntParameters[] = {
  2711. { L"ShaderOp.Name", TableParameter::STRING, true },
  2712. { L"ShaderOp.Text", TableParameter::STRING, true },
  2713. { L"Validation.NumInputSet", TableParameter::UINT, true },
  2714. { L"Validation.InputSet1", TableParameter::INT32_TABLE, true },
  2715. { L"Validation.InputSet2", TableParameter::INT32_TABLE, false },
  2716. { L"Validation.InputSet3", TableParameter::INT32_TABLE, false },
  2717. { L"Validation.InputSet4", TableParameter::INT32_TABLE, false }
  2718. };
  2719. static TableParameter WaveIntrinsicsActiveUintParameters[] = {
  2720. { L"ShaderOp.Name", TableParameter::STRING, true },
  2721. { L"ShaderOp.Text", TableParameter::STRING, true },
  2722. { L"Validation.NumInputSet", TableParameter::UINT, true },
  2723. { L"Validation.InputSet1", TableParameter::UINT32_TABLE, true },
  2724. { L"Validation.InputSet2", TableParameter::UINT32_TABLE, false },
  2725. { L"Validation.InputSet3", TableParameter::UINT32_TABLE, false },
  2726. { L"Validation.InputSet4", TableParameter::UINT32_TABLE, false }
  2727. };
  2728. static TableParameter WaveIntrinsicsPrefixUintParameters[] = {
  2729. { L"ShaderOp.Name", TableParameter::STRING, true },
  2730. { L"ShaderOp.Text", TableParameter::STRING, true },
  2731. { L"Validation.NumInputSet", TableParameter::UINT, true },
  2732. { L"Validation.InputSet1", TableParameter::UINT32_TABLE, true },
  2733. { L"Validation.InputSet2", TableParameter::UINT32_TABLE, false },
  2734. { L"Validation.InputSet3", TableParameter::UINT32_TABLE, false },
  2735. { L"Validation.InputSet4", TableParameter::UINT32_TABLE, false }
  2736. };
  2737. static TableParameter WaveIntrinsicsActiveBoolParameters[] = {
  2738. { L"ShaderOp.Name", TableParameter::STRING, true },
  2739. { L"ShaderOp.Text", TableParameter::STRING, true },
  2740. { L"Validation.NumInputSet", TableParameter::UINT, true },
  2741. { L"Validation.InputSet1", TableParameter::BOOL_TABLE, true },
  2742. { L"Validation.InputSet2", TableParameter::BOOL_TABLE, false },
  2743. { L"Validation.InputSet3", TableParameter::BOOL_TABLE, false },
  2744. };
  2745. static TableParameter CBufferTestHalfParameters[] = {
  2746. { L"Validation.InputSet", TableParameter::HALF_TABLE, true },
  2747. };
  2748. static TableParameter DenormBinaryFPOpParameters[] = {
  2749. { L"ShaderOp.Target", TableParameter::STRING, true },
  2750. { L"ShaderOp.Text", TableParameter::STRING, true },
  2751. { L"ShaderOp.Arguments", TableParameter::STRING, true },
  2752. { L"Validation.Input1", TableParameter::STRING_TABLE, true },
  2753. { L"Validation.Input2", TableParameter::STRING_TABLE, true },
  2754. { L"Validation.Expected1", TableParameter::STRING_TABLE, true },
  2755. { L"Validation.Expected2", TableParameter::STRING_TABLE, false },
  2756. { L"Validation.Type", TableParameter::STRING, true },
  2757. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2758. };
  2759. static TableParameter DenormTertiaryFPOpParameters[] = {
  2760. { L"ShaderOp.Target", TableParameter::STRING, true },
  2761. { L"ShaderOp.Text", TableParameter::STRING, true },
  2762. { L"ShaderOp.Arguments", TableParameter::STRING, true },
  2763. { L"Validation.Input1", TableParameter::STRING_TABLE, true },
  2764. { L"Validation.Input2", TableParameter::STRING_TABLE, true },
  2765. { L"Validation.Input3", TableParameter::STRING_TABLE, true },
  2766. { L"Validation.Expected1", TableParameter::STRING_TABLE, true },
  2767. { L"Validation.Expected2", TableParameter::STRING_TABLE, false },
  2768. { L"Validation.Type", TableParameter::STRING, true },
  2769. { L"Validation.Tolerance", TableParameter::DOUBLE, true },
  2770. };
  2771. static bool IsHexString(PCWSTR str, uint16_t *value) {
  2772. std::wstring wString(str);
  2773. wString.erase(std::remove(wString.begin(), wString.end(), L' '), wString.end());
  2774. LPCWSTR wstr = wString.c_str();
  2775. if (wcsncmp(wstr, L"0x", 2) == 0 || wcsncmp(wstr, L"0b", 2) == 0) {
  2776. *value = (uint16_t)wcstol(wstr, NULL, 0);
  2777. return true;
  2778. }
  2779. return false;
  2780. }
  2781. static HRESULT ParseDataToFloat(PCWSTR str, float &value) {
  2782. std::wstring wString(str);
  2783. wString.erase(std::remove(wString.begin(), wString.end(), L' '), wString.end());
  2784. PCWSTR wstr = wString.data();
  2785. if (_wcsicmp(wstr, L"NaN") == 0) {
  2786. value = NAN;
  2787. } else if (_wcsicmp(wstr, L"-inf") == 0) {
  2788. value = -(INFINITY);
  2789. } else if (_wcsicmp(wstr, L"inf") == 0) {
  2790. value = INFINITY;
  2791. } else if (_wcsicmp(wstr, L"-denorm") == 0) {
  2792. value = -(FLT_MIN / 2);
  2793. } else if (_wcsicmp(wstr, L"denorm") == 0) {
  2794. value = FLT_MIN / 2;
  2795. } else if (_wcsicmp(wstr, L"-0.0f") == 0 || _wcsicmp(wstr, L"-0.0") == 0 ||
  2796. _wcsicmp(wstr, L"-0") == 0) {
  2797. value = -0.0f;
  2798. } else if (_wcsicmp(wstr, L"0.0f") == 0 || _wcsicmp(wstr, L"0.0") == 0 ||
  2799. _wcsicmp(wstr, L"0") == 0) {
  2800. value = 0.0f;
  2801. } else if (_wcsnicmp(wstr, L"0x", 2) == 0) { // For hex values, take values literally
  2802. unsigned temp_i = std::stoul(wstr, nullptr, 16);
  2803. value = (float&)temp_i;
  2804. }
  2805. else {
  2806. // evaluate the expression of wstring
  2807. double val = _wtof(wstr);
  2808. if (val == 0) {
  2809. LogErrorFmt(L"Failed to parse parameter %s to float", wstr);
  2810. return E_FAIL;
  2811. }
  2812. value = (float)val;
  2813. }
  2814. return S_OK;
  2815. }
  2816. static HRESULT ParseDataToInt(PCWSTR str, int &value) {
  2817. std::wstring wString(str);
  2818. wString.erase(std::remove(wString.begin(), wString.end(), L' '), wString.end());
  2819. PCWSTR wstr = wString.data();
  2820. // evaluate the expression of string
  2821. if (_wcsicmp(wstr, L"0.0") == 0 || _wcsicmp(wstr, L"0") == 0) {
  2822. value = 0;
  2823. return S_OK;
  2824. }
  2825. int val = _wtoi(wstr);
  2826. if (val == 0) {
  2827. LogErrorFmt(L"Failed to parse parameter %s to int", wstr);
  2828. return E_FAIL;
  2829. }
  2830. value = val;
  2831. return S_OK;
  2832. }
  2833. static HRESULT ParseDataToUint(PCWSTR str, unsigned int &value) {
  2834. std::wstring wString(str);
  2835. wString.erase(std::remove(wString.begin(), wString.end(), L' '), wString.end());
  2836. PCWSTR wstr = wString.data();
  2837. // evaluate the expression of string
  2838. if (_wcsicmp(wstr, L"0") == 0 || _wcsicmp(wstr, L"0x00000000") == 0) {
  2839. value = 0;
  2840. return S_OK;
  2841. }
  2842. wchar_t *end;
  2843. unsigned int val = std::wcstoul(wstr, &end, 0);
  2844. if (val == 0) {
  2845. LogErrorFmt(L"Failed to parse parameter %s to int", wstr);
  2846. return E_FAIL;
  2847. }
  2848. value = val;
  2849. return S_OK;
  2850. }
  2851. static HRESULT ParseDataToVectorFloat(PCWSTR str, float *ptr, size_t count) {
  2852. std::wstring wstr(str);
  2853. size_t curPosition = 0;
  2854. // parse a string of dot product separated by commas
  2855. for (size_t i = 0; i < count; ++i) {
  2856. size_t nextPosition = wstr.find(L",", curPosition);
  2857. if (FAILED(ParseDataToFloat(
  2858. wstr.substr(curPosition, nextPosition - curPosition).data(),
  2859. *(ptr + i)))) {
  2860. return E_FAIL;
  2861. }
  2862. curPosition = nextPosition + 1;
  2863. }
  2864. return S_OK;
  2865. }
  2866. static HRESULT ParseDataToVectorUint(PCWSTR str, unsigned int *ptr, size_t count) {
  2867. std::wstring wstr(str);
  2868. size_t curPosition = 0;
  2869. // parse a string of dot product separated by commas
  2870. for (size_t i = 0; i < count; ++i) {
  2871. size_t nextPosition = wstr.find(L",", curPosition);
  2872. if (FAILED(ParseDataToUint(
  2873. wstr.substr(curPosition, nextPosition - curPosition).data(),
  2874. *(ptr + i)))) {
  2875. return E_FAIL;
  2876. }
  2877. curPosition = nextPosition + 1;
  2878. }
  2879. return S_OK;
  2880. }
  2881. HRESULT TableParameterHandler::ParseTableRow() {
  2882. TableParameter *table = m_table;
  2883. for (unsigned int i = 0; i < m_tableSize; ++i) {
  2884. switch (table[i].m_type) {
  2885. case TableParameter::INT8:
  2886. if (FAILED(WEX::TestExecution::TestData::TryGetValue(table[i].m_name,
  2887. table[i].m_int32)) && table[i].m_required) {
  2888. // TryGetValue does not suppport reading from int16
  2889. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2890. return E_FAIL;
  2891. }
  2892. table[i].m_int8 = (int8_t)(table[i].m_int32);
  2893. break;
  2894. case TableParameter::INT16:
  2895. if (FAILED(WEX::TestExecution::TestData::TryGetValue(table[i].m_name,
  2896. table[i].m_int32)) && table[i].m_required) {
  2897. // TryGetValue does not suppport reading from int16
  2898. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2899. return E_FAIL;
  2900. }
  2901. table[i].m_int16 = (short)(table[i].m_int32);
  2902. break;
  2903. case TableParameter::INT32:
  2904. if (FAILED(WEX::TestExecution::TestData::TryGetValue(table[i].m_name,
  2905. table[i].m_int32)) && table[i].m_required) {
  2906. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2907. return E_FAIL;
  2908. }
  2909. break;
  2910. case TableParameter::UINT:
  2911. if (FAILED(WEX::TestExecution::TestData::TryGetValue(table[i].m_name,
  2912. table[i].m_uint)) && table[i].m_required) {
  2913. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2914. return E_FAIL;
  2915. }
  2916. break;
  2917. case TableParameter::DOUBLE:
  2918. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2919. table[i].m_name, table[i].m_double)) && table[i].m_required) {
  2920. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2921. return E_FAIL;
  2922. }
  2923. break;
  2924. case TableParameter::STRING:
  2925. if (FAILED(WEX::TestExecution::TestData::TryGetValue(table[i].m_name,
  2926. table[i].m_str)) && table[i].m_required) {
  2927. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2928. return E_FAIL;
  2929. }
  2930. break;
  2931. case TableParameter::BOOL:
  2932. if (FAILED(WEX::TestExecution::TestData::TryGetValue(table[i].m_name,
  2933. table[i].m_str)) && table[i].m_bool) {
  2934. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2935. return E_FAIL;
  2936. }
  2937. break;
  2938. case TableParameter::INT8_TABLE: {
  2939. WEX::TestExecution::TestDataArray<int> tempTable;
  2940. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2941. table[i].m_name, tempTable)) && table[i].m_required) {
  2942. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2943. return E_FAIL;
  2944. }
  2945. // TryGetValue does not suppport reading from int8
  2946. table[i].m_int8Table.resize(tempTable.GetSize());
  2947. for (size_t j = 0, end = tempTable.GetSize(); j != end; ++j) {
  2948. table[i].m_int8Table[j] = (int8_t)tempTable[j];
  2949. }
  2950. break;
  2951. }
  2952. case TableParameter::INT16_TABLE: {
  2953. WEX::TestExecution::TestDataArray<int> tempTable;
  2954. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2955. table[i].m_name, tempTable)) && table[i].m_required) {
  2956. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2957. return E_FAIL;
  2958. }
  2959. // TryGetValue does not suppport reading from int8
  2960. table[i].m_int16Table.resize(tempTable.GetSize());
  2961. for (size_t j = 0, end = tempTable.GetSize(); j != end; ++j) {
  2962. table[i].m_int16Table[j] = (int16_t)tempTable[j];
  2963. }
  2964. break;
  2965. }case TableParameter::INT32_TABLE: {
  2966. WEX::TestExecution::TestDataArray<int> tempTable;
  2967. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2968. table[i].m_name, tempTable)) && table[i].m_required) {
  2969. // TryGetValue does not suppport reading from int8
  2970. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2971. return E_FAIL;
  2972. }
  2973. table[i].m_int32Table.resize(tempTable.GetSize());
  2974. for (size_t j = 0, end = tempTable.GetSize(); j != end; ++j) {
  2975. table[i].m_int32Table[j] = tempTable[j];
  2976. }
  2977. break;
  2978. }
  2979. case TableParameter::UINT8_TABLE: {
  2980. WEX::TestExecution::TestDataArray<int> tempTable;
  2981. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2982. table[i].m_name, tempTable)) && table[i].m_required) {
  2983. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2984. return E_FAIL;
  2985. }
  2986. // TryGetValue does not suppport reading from int8
  2987. table[i].m_int8Table.resize(tempTable.GetSize());
  2988. for (size_t j = 0, end = tempTable.GetSize(); j != end; ++j) {
  2989. table[i].m_int8Table[j] = (uint8_t)tempTable[j];
  2990. }
  2991. break;
  2992. }
  2993. case TableParameter::UINT16_TABLE: {
  2994. WEX::TestExecution::TestDataArray<int> tempTable;
  2995. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  2996. table[i].m_name, tempTable)) && table[i].m_required) {
  2997. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  2998. return E_FAIL;
  2999. }
  3000. // TryGetValue does not suppport reading from int8
  3001. table[i].m_uint16Table.resize(tempTable.GetSize());
  3002. for (size_t j = 0, end = tempTable.GetSize(); j != end; ++j) {
  3003. table[i].m_uint16Table[j] = (uint16_t)tempTable[j];
  3004. }
  3005. break;
  3006. }
  3007. case TableParameter::UINT32_TABLE: {
  3008. WEX::TestExecution::TestDataArray<unsigned int> tempTable;
  3009. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  3010. table[i].m_name, tempTable)) && table[i].m_required) {
  3011. // TryGetValue does not suppport reading from int8
  3012. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  3013. return E_FAIL;
  3014. }
  3015. table[i].m_uint32Table.resize(tempTable.GetSize());
  3016. for (size_t j = 0, end = tempTable.GetSize(); j != end; ++j) {
  3017. table[i].m_uint32Table[j] = tempTable[j];
  3018. }
  3019. break;
  3020. }
  3021. case TableParameter::FLOAT_TABLE: {
  3022. WEX::TestExecution::TestDataArray<WEX::Common::String> tempTable;
  3023. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  3024. table[i].m_name, tempTable)) && table[i].m_required) {
  3025. // TryGetValue does not suppport reading from int8
  3026. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  3027. return E_FAIL;
  3028. }
  3029. table[i].m_floatTable.resize(tempTable.GetSize());
  3030. for (size_t j = 0, end = tempTable.GetSize(); j != end; ++j) {
  3031. ParseDataToFloat(tempTable[j], table[i].m_floatTable[j]);
  3032. }
  3033. break;
  3034. }
  3035. case TableParameter::HALF_TABLE: {
  3036. WEX::TestExecution::TestDataArray<WEX::Common::String> tempTable;
  3037. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  3038. table[i].m_name, tempTable)) && table[i].m_required) {
  3039. // TryGetValue does not suppport reading from int8
  3040. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  3041. return E_FAIL;
  3042. }
  3043. table[i].m_halfTable.resize(tempTable.GetSize());
  3044. for (size_t j = 0, end = tempTable.GetSize(); j != end; ++j) {
  3045. uint16_t value = 0;
  3046. if (IsHexString(tempTable[j], &value)) {
  3047. table[i].m_halfTable[j] = value;
  3048. }
  3049. else {
  3050. float val;
  3051. ParseDataToFloat(tempTable[j], val);
  3052. if (isdenorm(val))
  3053. table[i].m_halfTable[j] = signbit(val) ? Float16NegDenorm : Float16PosDenorm;
  3054. else
  3055. table[i].m_halfTable[j] = ConvertFloat32ToFloat16(val);
  3056. }
  3057. }
  3058. break;
  3059. }
  3060. case TableParameter::DOUBLE_TABLE: {
  3061. WEX::TestExecution::TestDataArray<double> tempTable;
  3062. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  3063. table[i].m_name, tempTable)) && table[i].m_required) {
  3064. // TryGetValue does not suppport reading from int8
  3065. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  3066. return E_FAIL;
  3067. }
  3068. table[i].m_doubleTable.resize(tempTable.GetSize());
  3069. for (size_t j = 0, end = tempTable.GetSize(); j != end; ++j) {
  3070. table[i].m_doubleTable[j] = tempTable[j];
  3071. }
  3072. break;
  3073. }
  3074. case TableParameter::BOOL_TABLE: {
  3075. WEX::TestExecution::TestDataArray<bool> tempTable;
  3076. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  3077. table[i].m_name, tempTable)) && table[i].m_required) {
  3078. // TryGetValue does not suppport reading from int8
  3079. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  3080. return E_FAIL;
  3081. }
  3082. table[i].m_boolTable.resize(tempTable.GetSize());
  3083. for (size_t j = 0, end = tempTable.GetSize(); j != end; ++j) {
  3084. table[i].m_boolTable[j] = tempTable[j];
  3085. }
  3086. break;
  3087. }
  3088. case TableParameter::STRING_TABLE: {
  3089. WEX::TestExecution::TestDataArray<WEX::Common::String> tempTable;
  3090. if (FAILED(WEX::TestExecution::TestData::TryGetValue(
  3091. table[i].m_name, tempTable)) && table[i].m_required) {
  3092. // TryGetValue does not suppport reading from int8
  3093. LogErrorFmt(L"Failed to get %s", table[i].m_name);
  3094. return E_FAIL;
  3095. }
  3096. table[i].m_StringTable.resize(tempTable.GetSize());
  3097. for (size_t j = 0, end = tempTable.GetSize(); j != end; ++j) {
  3098. table[i].m_StringTable[j] = tempTable[j];
  3099. }
  3100. break;
  3101. }
  3102. default:
  3103. DXASSERT_NOMSG("Invalid Parameter Type");
  3104. }
  3105. if (errno == ERANGE) {
  3106. LogErrorFmt(L"got out of range value for table %s", table[i].m_name);
  3107. return E_FAIL;
  3108. }
  3109. }
  3110. return S_OK;
  3111. }
  3112. static void VerifyOutputWithExpectedValueInt(int output, int ref, int tolerance) {
  3113. VERIFY_IS_TRUE(output - ref <= tolerance && ref - output <= tolerance);
  3114. }
  3115. static void VerifyOutputWithExpectedValueFloat(
  3116. float output, float ref, LPCWSTR type, double tolerance,
  3117. hlsl::DXIL::Float32DenormMode mode = hlsl::DXIL::Float32DenormMode::Any) {
  3118. if (_wcsicmp(type, L"Relative") == 0) {
  3119. VERIFY_IS_TRUE(CompareFloatRelativeEpsilon(output, ref, (int)tolerance, mode));
  3120. } else if (_wcsicmp(type, L"Epsilon") == 0) {
  3121. VERIFY_IS_TRUE(CompareFloatEpsilon(output, ref, (float)tolerance, mode));
  3122. } else if (_wcsicmp(type, L"ULP") == 0) {
  3123. VERIFY_IS_TRUE(CompareFloatULP(output, ref, (int)tolerance, mode));
  3124. } else {
  3125. LogErrorFmt(L"Failed to read comparison type %S", type);
  3126. }
  3127. }
  3128. static bool CompareOutputWithExpectedValueFloat(
  3129. float output, float ref, LPCWSTR type, double tolerance,
  3130. hlsl::DXIL::Float32DenormMode mode = hlsl::DXIL::Float32DenormMode::Any) {
  3131. if (_wcsicmp(type, L"Relative") == 0) {
  3132. return CompareFloatRelativeEpsilon(output, ref, (int)tolerance, mode);
  3133. } else if (_wcsicmp(type, L"Epsilon") == 0) {
  3134. return CompareFloatEpsilon(output, ref, (float)tolerance, mode);
  3135. } else if (_wcsicmp(type, L"ULP") == 0) {
  3136. return CompareFloatULP(output, ref, (int)tolerance, mode);
  3137. } else {
  3138. LogErrorFmt(L"Failed to read comparison type %S", type);
  3139. return false;
  3140. }
  3141. }
  3142. static void VerifyOutputWithExpectedValueHalf(
  3143. uint16_t output, uint16_t ref, LPCWSTR type, double tolerance) {
  3144. if (_wcsicmp(type, L"Relative") == 0) {
  3145. VERIFY_IS_TRUE(CompareHalfRelativeEpsilon(output, ref, (int)tolerance));
  3146. }
  3147. else if (_wcsicmp(type, L"Epsilon") == 0) {
  3148. VERIFY_IS_TRUE(CompareHalfEpsilon(output, ref, (float)tolerance));
  3149. }
  3150. else if (_wcsicmp(type, L"ULP") == 0) {
  3151. VERIFY_IS_TRUE(CompareHalfULP(output, ref, (float)tolerance));
  3152. }
  3153. else {
  3154. LogErrorFmt(L"Failed to read comparison type %S", type);
  3155. }
  3156. }
  3157. TEST_F(ExecutionTest, UnaryFloatOpTest) {
  3158. WEX::TestExecution::SetVerifyOutput verifySettings(
  3159. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3160. CComPtr<IStream> pStream;
  3161. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3162. CComPtr<ID3D12Device> pDevice;
  3163. if (!CreateDevice(&pDevice)) {
  3164. return;
  3165. }
  3166. // Read data from the table
  3167. int tableSize = sizeof(UnaryFPOpParameters) / sizeof(TableParameter);
  3168. TableParameterHandler handler(UnaryFPOpParameters, tableSize);
  3169. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3170. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3171. unsigned int WarpVersion = handler.GetTableParamByName(L"Warp.Version")->m_uint;
  3172. if (GetTestParamUseWARP(true) && !IsValidWarpDllVersion(WarpVersion)) {
  3173. return;
  3174. }
  3175. std::vector<float> *Validation_Input =
  3176. &(handler.GetTableParamByName(L"Validation.Input1")->m_floatTable);
  3177. std::vector<float> *Validation_Expected =
  3178. &(handler.GetTableParamByName(L"Validation.Expected1")->m_floatTable);
  3179. LPCWSTR Validation_Type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  3180. double Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  3181. size_t count = Validation_Input->size();
  3182. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3183. pDevice, m_support, pStream, "UnaryFPOp",
  3184. // this callbacked is called when the test
  3185. // is creating the resource to run the test
  3186. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3187. VERIFY_IS_TRUE(0 == _stricmp(Name, "SUnaryFPOp"));
  3188. size_t size = sizeof(SUnaryFPOp) * count;
  3189. Data.resize(size);
  3190. SUnaryFPOp *pPrimitives = (SUnaryFPOp *)Data.data();
  3191. for (size_t i = 0; i < count; ++i) {
  3192. SUnaryFPOp *p = &pPrimitives[i];
  3193. p->input = (*Validation_Input)[i % Validation_Input->size()];
  3194. }
  3195. // use shader from data table
  3196. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3197. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3198. });
  3199. MappedData data;
  3200. test->Test->GetReadBackData("SUnaryFPOp", &data);
  3201. SUnaryFPOp *pPrimitives = (SUnaryFPOp*)data.data();
  3202. WEX::TestExecution::DisableVerifyExceptions dve;
  3203. for (unsigned i = 0; i < count; ++i) {
  3204. SUnaryFPOp *p = &pPrimitives[i];
  3205. float val = (*Validation_Expected)[i % Validation_Expected->size()];
  3206. LogCommentFmt(
  3207. L"element #%u, input = %6.8f, output = %6.8f, expected = %6.8f", i,
  3208. p->input, p->output, val);
  3209. VerifyOutputWithExpectedValueFloat(p->output, val, Validation_Type, Validation_Tolerance);
  3210. }
  3211. }
  3212. TEST_F(ExecutionTest, BinaryFloatOpTest) {
  3213. WEX::TestExecution::SetVerifyOutput verifySettings(
  3214. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3215. CComPtr<IStream> pStream;
  3216. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3217. CComPtr<ID3D12Device> pDevice;
  3218. if (!CreateDevice(&pDevice)) {
  3219. return;
  3220. }
  3221. // Read data from the table
  3222. int tableSize = sizeof(BinaryFPOpParameters) / sizeof(TableParameter);
  3223. TableParameterHandler handler(BinaryFPOpParameters, tableSize);
  3224. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3225. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3226. std::vector<float> *Validation_Input1 =
  3227. &(handler.GetTableParamByName(L"Validation.Input1")->m_floatTable);
  3228. std::vector<float> *Validation_Input2 =
  3229. &(handler.GetTableParamByName(L"Validation.Input2")->m_floatTable);
  3230. std::vector<float> *Validation_Expected1 =
  3231. &(handler.GetTableParamByName(L"Validation.Expected1")->m_floatTable);
  3232. std::vector<float> *Validation_Expected2 =
  3233. &(handler.GetTableParamByName(L"Validation.Expected2")->m_floatTable);
  3234. LPCWSTR Validation_Type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  3235. double Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  3236. size_t count = Validation_Input1->size();
  3237. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3238. pDevice, m_support, pStream, "BinaryFPOp",
  3239. // this callbacked is called when the test
  3240. // is creating the resource to run the test
  3241. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3242. VERIFY_IS_TRUE(0 == _stricmp(Name, "SBinaryFPOp"));
  3243. size_t size = sizeof(SBinaryFPOp) * count;
  3244. Data.resize(size);
  3245. SBinaryFPOp *pPrimitives = (SBinaryFPOp *)Data.data();
  3246. for (size_t i = 0; i < count; ++i) {
  3247. SBinaryFPOp *p = &pPrimitives[i];
  3248. p->input1 = (*Validation_Input1)[i % Validation_Input1->size()];
  3249. p->input2 = (*Validation_Input2)[i % Validation_Input2->size()];
  3250. }
  3251. // use shader from data table
  3252. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3253. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3254. });
  3255. MappedData data;
  3256. test->Test->GetReadBackData("SBinaryFPOp", &data);
  3257. SBinaryFPOp *pPrimitives = (SBinaryFPOp *)data.data();
  3258. WEX::TestExecution::DisableVerifyExceptions dve;
  3259. unsigned numExpected = Validation_Expected2->size() == 0 ? 1 : 2;
  3260. if (numExpected == 2) {
  3261. for (unsigned i = 0; i < count; ++i) {
  3262. SBinaryFPOp *p = &pPrimitives[i];
  3263. float val1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  3264. float val2 = (*Validation_Expected2)[i % Validation_Expected2->size()];
  3265. LogCommentFmt(L"element #%u, input1 = %6.8f, input2 = %6.8f, output1 = "
  3266. L"%6.8f, expected1 = %6.8f, output2 = %6.8f, expected2 = %6.8f",
  3267. i, p->input1, p->input2, p->output1, val1, p->output2,
  3268. val2);
  3269. VerifyOutputWithExpectedValueFloat(p->output1, val1, Validation_Type,
  3270. Validation_Tolerance);
  3271. VerifyOutputWithExpectedValueFloat(p->output2, val2, Validation_Type,
  3272. Validation_Tolerance);
  3273. }
  3274. }
  3275. else if (numExpected == 1) {
  3276. for (unsigned i = 0; i < count; ++i) {
  3277. SBinaryFPOp *p = &pPrimitives[i];
  3278. float val1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  3279. LogCommentFmt(L"element #%u, input1 = %6.8f, input2 = %6.8f, output1 = "
  3280. L"%6.8f, expected1 = %6.8f",
  3281. i, p->input1, p->input2, p->output1, val1);
  3282. VerifyOutputWithExpectedValueFloat(p->output1, val1, Validation_Type,
  3283. Validation_Tolerance);
  3284. }
  3285. }
  3286. else {
  3287. LogErrorFmt(L"Unexpected number of expected values for operation %i", numExpected);
  3288. }
  3289. }
  3290. TEST_F(ExecutionTest, TertiaryFloatOpTest) {
  3291. WEX::TestExecution::SetVerifyOutput verifySettings(
  3292. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3293. CComPtr<IStream> pStream;
  3294. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3295. CComPtr<ID3D12Device> pDevice;
  3296. if (!CreateDevice(&pDevice)) {
  3297. return;
  3298. }
  3299. // Read data from the table
  3300. int tableSize = sizeof(TertiaryFPOpParameters) / sizeof(TableParameter);
  3301. TableParameterHandler handler(TertiaryFPOpParameters, tableSize);
  3302. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3303. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3304. std::vector<float> *Validation_Input1 =
  3305. &(handler.GetTableParamByName(L"Validation.Input1")->m_floatTable);
  3306. std::vector<float> *Validation_Input2 =
  3307. &(handler.GetTableParamByName(L"Validation.Input2")->m_floatTable);
  3308. std::vector<float> *Validation_Input3 =
  3309. &(handler.GetTableParamByName(L"Validation.Input3")->m_floatTable);
  3310. std::vector<float> *Validation_Expected =
  3311. &(handler.GetTableParamByName(L"Validation.Expected1")->m_floatTable);
  3312. LPCWSTR Validation_Type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  3313. double Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  3314. size_t count = Validation_Input1->size();
  3315. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3316. pDevice, m_support, pStream, "TertiaryFPOp",
  3317. // this callbacked is called when the test
  3318. // is creating the resource to run the test
  3319. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3320. VERIFY_IS_TRUE(0 == _stricmp(Name, "STertiaryFPOp"));
  3321. size_t size = sizeof(STertiaryFPOp) * count;
  3322. Data.resize(size);
  3323. STertiaryFPOp *pPrimitives = (STertiaryFPOp *)Data.data();
  3324. for (size_t i = 0; i < count; ++i) {
  3325. STertiaryFPOp *p = &pPrimitives[i];
  3326. p->input1 = (*Validation_Input1)[i % Validation_Input1->size()];
  3327. p->input2 = (*Validation_Input2)[i % Validation_Input2->size()];
  3328. p->input3 = (*Validation_Input3)[i % Validation_Input3->size()];
  3329. }
  3330. // use shader from data table
  3331. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3332. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3333. });
  3334. MappedData data;
  3335. test->Test->GetReadBackData("STertiaryFPOp", &data);
  3336. STertiaryFPOp *pPrimitives = (STertiaryFPOp *)data.data();
  3337. WEX::TestExecution::DisableVerifyExceptions dve;
  3338. for (unsigned i = 0; i < count; ++i) {
  3339. STertiaryFPOp *p = &pPrimitives[i];
  3340. float val = (*Validation_Expected)[i % Validation_Expected->size()];
  3341. LogCommentFmt(L"element #%u, input1 = %6.8f, input2 = %6.8f, input3 = %6.8f, output1 = "
  3342. L"%6.8f, expected = %6.8f",
  3343. i, p->input1, p->input2, p->input3, p->output, val);
  3344. VerifyOutputWithExpectedValueFloat(p->output, val, Validation_Type,
  3345. Validation_Tolerance);
  3346. }
  3347. }
  3348. TEST_F(ExecutionTest, UnaryHalfOpTest) {
  3349. WEX::TestExecution::SetVerifyOutput verifySettings(
  3350. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3351. CComPtr<IStream> pStream;
  3352. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3353. CComPtr<ID3D12Device> pDevice;
  3354. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL::D3D_SHADER_MODEL_6_2)) {
  3355. return;
  3356. }
  3357. // Read data from the table
  3358. int tableSize = sizeof(UnaryHalfOpParameters) / sizeof(TableParameter);
  3359. TableParameterHandler handler(UnaryHalfOpParameters, tableSize);
  3360. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3361. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3362. CW2A Arguments(handler.GetTableParamByName(L"ShaderOp.Arguments")->m_str);
  3363. unsigned int WarpVersion = handler.GetTableParamByName(L"Warp.Version")->m_uint;
  3364. if (GetTestParamUseWARP(true) && !IsValidWarpDllVersion(WarpVersion)) {
  3365. return;
  3366. }
  3367. std::vector<uint16_t> *Validation_Input =
  3368. &(handler.GetTableParamByName(L"Validation.Input1")->m_halfTable);
  3369. std::vector<uint16_t> *Validation_Expected =
  3370. &(handler.GetTableParamByName(L"Validation.Expected1")->m_halfTable);
  3371. LPCWSTR Validation_Type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  3372. double Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  3373. size_t count = Validation_Input->size();
  3374. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3375. pDevice, m_support, pStream, "UnaryFPOp",
  3376. // this callbacked is called when the test
  3377. // is creating the resource to run the test
  3378. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3379. VERIFY_IS_TRUE(0 == _stricmp(Name, "SUnaryFPOp"));
  3380. size_t size = sizeof(SUnaryHalfOp) * count;
  3381. Data.resize(size);
  3382. SUnaryHalfOp *pPrimitives = (SUnaryHalfOp *)Data.data();
  3383. for (size_t i = 0; i < count; ++i) {
  3384. SUnaryHalfOp *p = &pPrimitives[i];
  3385. p->input = (*Validation_Input)[i % Validation_Input->size()];
  3386. }
  3387. // use shader from data table
  3388. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3389. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3390. pShaderOp->Shaders.at(0).Arguments = Arguments.m_psz;
  3391. });
  3392. MappedData data;
  3393. test->Test->GetReadBackData("SUnaryFPOp", &data);
  3394. SUnaryHalfOp *pPrimitives = (SUnaryHalfOp*)data.data();
  3395. WEX::TestExecution::DisableVerifyExceptions dve;
  3396. for (unsigned i = 0; i < count; ++i) {
  3397. SUnaryHalfOp *p = &pPrimitives[i];
  3398. uint16_t expected = (*Validation_Expected)[i % Validation_Input->size()];
  3399. LogCommentFmt(L"element #%u, input = %6.8f(0x%04x), output = "
  3400. L"%6.8f(0x%04x), expected = %6.8f(0x%04x)",
  3401. i, ConvertFloat16ToFloat32(p->input), p->input,
  3402. ConvertFloat16ToFloat32(p->output), p->output,
  3403. ConvertFloat16ToFloat32(expected), expected);
  3404. VerifyOutputWithExpectedValueHalf(p->output, expected, Validation_Type, Validation_Tolerance);
  3405. }
  3406. }
  3407. TEST_F(ExecutionTest, BinaryHalfOpTest) {
  3408. WEX::TestExecution::SetVerifyOutput verifySettings(
  3409. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3410. CComPtr<IStream> pStream;
  3411. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3412. CComPtr<ID3D12Device> pDevice;
  3413. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL::D3D_SHADER_MODEL_6_2)) {
  3414. return;
  3415. }
  3416. // Read data from the table
  3417. int tableSize = sizeof(BinaryHalfOpParameters) / sizeof(TableParameter);
  3418. TableParameterHandler handler(BinaryHalfOpParameters, tableSize);
  3419. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3420. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3421. CW2A Arguments(handler.GetTableParamByName(L"ShaderOp.Arguments")->m_str);
  3422. std::vector<uint16_t> *Validation_Input1 =
  3423. &(handler.GetTableParamByName(L"Validation.Input1")->m_halfTable);
  3424. std::vector<uint16_t> *Validation_Input2 =
  3425. &(handler.GetTableParamByName(L"Validation.Input2")->m_halfTable);
  3426. std::vector<uint16_t> *Validation_Expected1 =
  3427. &(handler.GetTableParamByName(L"Validation.Expected1")->m_halfTable);
  3428. std::vector<uint16_t> *Validation_Expected2 =
  3429. &(handler.GetTableParamByName(L"Validation.Expected2")->m_halfTable);
  3430. LPCWSTR Validation_Type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  3431. double Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  3432. size_t count = Validation_Input1->size();
  3433. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3434. pDevice, m_support, pStream, "BinaryFPOp",
  3435. // this callbacked is called when the test
  3436. // is creating the resource to run the test
  3437. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3438. VERIFY_IS_TRUE(0 == _stricmp(Name, "SBinaryFPOp"));
  3439. size_t size = sizeof(SBinaryHalfOp) * count;
  3440. Data.resize(size);
  3441. SBinaryHalfOp *pPrimitives = (SBinaryHalfOp *)Data.data();
  3442. for (size_t i = 0; i < count; ++i) {
  3443. SBinaryHalfOp *p = &pPrimitives[i];
  3444. p->input1 = (*Validation_Input1)[i % Validation_Input1->size()];
  3445. p->input2 = (*Validation_Input2)[i % Validation_Input2->size()];
  3446. }
  3447. // use shader from data table
  3448. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3449. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3450. pShaderOp->Shaders.at(0).Arguments = Arguments.m_psz;
  3451. });
  3452. MappedData data;
  3453. test->Test->GetReadBackData("SBinaryFPOp", &data);
  3454. SBinaryHalfOp *pPrimitives = (SBinaryHalfOp *)data.data();
  3455. WEX::TestExecution::DisableVerifyExceptions dve;
  3456. unsigned numExpected = Validation_Expected2->size() == 0 ? 1 : 2;
  3457. if (numExpected == 2) {
  3458. for (unsigned i = 0; i < count; ++i) {
  3459. SBinaryHalfOp *p = &pPrimitives[i];
  3460. uint16_t expected1 = (*Validation_Expected1)[i % Validation_Input1->size()];
  3461. uint16_t expected2 = (*Validation_Expected2)[i % Validation_Input2->size()];
  3462. LogCommentFmt(L"element #%u, input1 = %6.8f(0x%04x), input2 = %6.8f(0x%04x), output1 = "
  3463. L"%6.8f(0x%04x), expected1 = %6.8f(0x%04x), output2 = %6.8f(0x%04x), expected2 = %6.8f(0x%04x)",
  3464. i, ConvertFloat16ToFloat32(p->input1), p->input1,
  3465. ConvertFloat16ToFloat32(p->input2), p->input2,
  3466. ConvertFloat16ToFloat32(p->output1), p->output1,
  3467. ConvertFloat16ToFloat32(p->output2), p->output2,
  3468. ConvertFloat16ToFloat32(expected1), expected1,
  3469. ConvertFloat16ToFloat32(expected2), expected2);
  3470. VerifyOutputWithExpectedValueHalf(p->output1, expected1, Validation_Type, Validation_Tolerance);
  3471. VerifyOutputWithExpectedValueHalf(p->output2, expected2, Validation_Type, Validation_Tolerance);
  3472. }
  3473. }
  3474. else if (numExpected == 1) {
  3475. for (unsigned i = 0; i < count; ++i) {
  3476. uint16_t expected = (*Validation_Expected1)[i % Validation_Input1->size()];
  3477. SBinaryHalfOp *p = &pPrimitives[i];
  3478. LogCommentFmt(L"element #%u, input = %6.8f(0x%04x), output = "
  3479. L"%6.8f(0x%04x), expected = %6.8f(0x%04x)",
  3480. i, ConvertFloat16ToFloat32(p->input1), p->input1,
  3481. ConvertFloat16ToFloat32(p->output1), p->output1,
  3482. ConvertFloat16ToFloat32(expected), expected);
  3483. VerifyOutputWithExpectedValueHalf(p->output1, expected, Validation_Type, Validation_Tolerance);
  3484. }
  3485. }
  3486. else {
  3487. LogErrorFmt(L"Unexpected number of expected values for operation %i", numExpected);
  3488. }
  3489. }
  3490. TEST_F(ExecutionTest, TertiaryHalfOpTest) {
  3491. WEX::TestExecution::SetVerifyOutput verifySettings(
  3492. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3493. CComPtr<IStream> pStream;
  3494. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3495. CComPtr<ID3D12Device> pDevice;
  3496. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL::D3D_SHADER_MODEL_6_2)) {
  3497. return;
  3498. }
  3499. // Read data from the table
  3500. int tableSize = sizeof(TertiaryHalfOpParameters) / sizeof(TableParameter);
  3501. TableParameterHandler handler(TertiaryHalfOpParameters, tableSize);
  3502. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3503. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3504. CW2A Arguments(handler.GetTableParamByName(L"ShaderOp.Arguments")->m_str);
  3505. std::vector<uint16_t> *Validation_Input1 =
  3506. &(handler.GetTableParamByName(L"Validation.Input1")->m_halfTable);
  3507. std::vector<uint16_t> *Validation_Input2 =
  3508. &(handler.GetTableParamByName(L"Validation.Input2")->m_halfTable);
  3509. std::vector<uint16_t> *Validation_Input3 =
  3510. &(handler.GetTableParamByName(L"Validation.Input3")->m_halfTable);
  3511. std::vector<uint16_t> *Validation_Expected =
  3512. &(handler.GetTableParamByName(L"Validation.Expected1")->m_halfTable);
  3513. LPCWSTR Validation_Type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  3514. double Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  3515. size_t count = Validation_Input1->size();
  3516. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3517. pDevice, m_support, pStream, "TertiaryFPOp",
  3518. // this callbacked is called when the test
  3519. // is creating the resource to run the test
  3520. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3521. VERIFY_IS_TRUE(0 == _stricmp(Name, "STertiaryFPOp"));
  3522. size_t size = sizeof(STertiaryHalfOp) * count;
  3523. Data.resize(size);
  3524. STertiaryHalfOp *pPrimitives = (STertiaryHalfOp *)Data.data();
  3525. for (size_t i = 0; i < count; ++i) {
  3526. STertiaryHalfOp *p = &pPrimitives[i];
  3527. p->input1 = (*Validation_Input1)[i % Validation_Input1->size()];
  3528. p->input2 = (*Validation_Input2)[i % Validation_Input2->size()];
  3529. p->input3 = (*Validation_Input3)[i % Validation_Input3->size()];
  3530. }
  3531. // use shader from data table
  3532. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3533. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3534. pShaderOp->Shaders.at(0).Arguments = Arguments.m_psz;
  3535. });
  3536. MappedData data;
  3537. test->Test->GetReadBackData("STertiaryFPOp", &data);
  3538. STertiaryHalfOp *pPrimitives = (STertiaryHalfOp *)data.data();
  3539. WEX::TestExecution::DisableVerifyExceptions dve;
  3540. for (unsigned i = 0; i < count; ++i) {
  3541. STertiaryHalfOp *p = &pPrimitives[i];
  3542. uint16_t expected = (*Validation_Expected)[i % Validation_Expected->size()];
  3543. LogCommentFmt(L"element #%u, input1 = %6.8f(0x%04x), input2 = %6.8f(0x%04x), input3 = %6.8f(0x%04x), output = "
  3544. L"%6.8f(0x%04x), expected = %6.8f(0x%04x)",
  3545. i, ConvertFloat16ToFloat32(p->input1), p->input1,
  3546. ConvertFloat16ToFloat32(p->input2), p->input2,
  3547. ConvertFloat16ToFloat32(p->input3), p->input3,
  3548. ConvertFloat16ToFloat32(p->output), p->output,
  3549. ConvertFloat16ToFloat32(expected), expected);
  3550. VerifyOutputWithExpectedValueHalf(p->output, expected, Validation_Type, Validation_Tolerance);
  3551. }
  3552. }
  3553. TEST_F(ExecutionTest, UnaryIntOpTest) {
  3554. WEX::TestExecution::SetVerifyOutput verifySettings(
  3555. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3556. CComPtr<IStream> pStream;
  3557. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3558. CComPtr<ID3D12Device> pDevice;
  3559. if (!CreateDevice(&pDevice)) {
  3560. return;
  3561. }
  3562. // Read data from the table
  3563. int tableSize = sizeof(UnaryIntOpParameters) / sizeof(TableParameter);
  3564. TableParameterHandler handler(UnaryIntOpParameters, tableSize);
  3565. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3566. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3567. std::vector<int> *Validation_Input =
  3568. &handler.GetTableParamByName(L"Validation.Input1")->m_int32Table;
  3569. std::vector<int> *Validation_Expected =
  3570. &handler.GetTableParamByName(L"Validation.Expected1")->m_int32Table;
  3571. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  3572. size_t count = Validation_Input->size();
  3573. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3574. pDevice, m_support, pStream, "UnaryIntOp",
  3575. // this callbacked is called when the test
  3576. // is creating the resource to run the test
  3577. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3578. VERIFY_IS_TRUE(0 == _stricmp(Name, "SUnaryIntOp"));
  3579. size_t size = sizeof(SUnaryIntOp) * count;
  3580. Data.resize(size);
  3581. SUnaryIntOp *pPrimitives = (SUnaryIntOp *)Data.data();
  3582. for (size_t i = 0; i < count; ++i) {
  3583. SUnaryIntOp *p = &pPrimitives[i];
  3584. int val = (*Validation_Input)[i % Validation_Input->size()];
  3585. p->input = val;
  3586. }
  3587. // use shader data table
  3588. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3589. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3590. });
  3591. MappedData data;
  3592. test->Test->GetReadBackData("SUnaryIntOp", &data);
  3593. SUnaryIntOp *pPrimitives = (SUnaryIntOp *)data.data();
  3594. WEX::TestExecution::DisableVerifyExceptions dve;
  3595. for (unsigned i = 0; i < count; ++i) {
  3596. SUnaryIntOp *p = &pPrimitives[i];
  3597. int val = (*Validation_Expected)[i % Validation_Expected->size()];
  3598. LogCommentFmt(L"element #%u, input = %11i(0x%08x), output = %11i(0x%08x), "
  3599. L"expected = %11i(0x%08x)",
  3600. i, p->input, p->input, p->output, p->output, val, val);
  3601. VerifyOutputWithExpectedValueInt(p->output, val, Validation_Tolerance);
  3602. }
  3603. }
  3604. TEST_F(ExecutionTest, UnaryUintOpTest) {
  3605. WEX::TestExecution::SetVerifyOutput verifySettings(
  3606. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3607. CComPtr<IStream> pStream;
  3608. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3609. CComPtr<ID3D12Device> pDevice;
  3610. if (!CreateDevice(&pDevice)) {
  3611. return;
  3612. }
  3613. // Read data from the table
  3614. int tableSize = sizeof(UnaryUintOpParameters) / sizeof(TableParameter);
  3615. TableParameterHandler handler(UnaryUintOpParameters, tableSize);
  3616. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3617. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3618. std::vector<unsigned int> *Validation_Input =
  3619. &handler.GetTableParamByName(L"Validation.Input1")->m_uint32Table;
  3620. std::vector<unsigned int> *Validation_Expected =
  3621. &handler.GetTableParamByName(L"Validation.Expected1")->m_uint32Table;
  3622. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  3623. size_t count = Validation_Input->size();
  3624. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3625. pDevice, m_support, pStream, "UnaryUintOp",
  3626. // this callbacked is called when the test
  3627. // is creating the resource to run the test
  3628. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3629. VERIFY_IS_TRUE(0 == _stricmp(Name, "SUnaryUintOp"));
  3630. size_t size = sizeof(SUnaryUintOp) * count;
  3631. Data.resize(size);
  3632. SUnaryUintOp *pPrimitives = (SUnaryUintOp *)Data.data();
  3633. for (size_t i = 0; i < count; ++i) {
  3634. SUnaryUintOp *p = &pPrimitives[i];
  3635. unsigned int val = (*Validation_Input)[i % Validation_Input->size()];
  3636. p->input = val;
  3637. }
  3638. // use shader data table
  3639. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3640. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3641. });
  3642. MappedData data;
  3643. test->Test->GetReadBackData("SUnaryUintOp", &data);
  3644. SUnaryUintOp *pPrimitives = (SUnaryUintOp *)data.data();
  3645. WEX::TestExecution::DisableVerifyExceptions dve;
  3646. for (unsigned i = 0; i < count; ++i) {
  3647. SUnaryUintOp *p = &pPrimitives[i];
  3648. unsigned int val = (*Validation_Expected)[i % Validation_Expected->size()];
  3649. LogCommentFmt(L"element #%u, input = %11u(0x%08x), output = %11u(0x%08x), "
  3650. L"expected = %11u(0x%08x)",
  3651. i, p->input, p->input, p->output, p->output, val, val);
  3652. VerifyOutputWithExpectedValueInt(p->output, val, Validation_Tolerance);
  3653. }
  3654. }
  3655. TEST_F(ExecutionTest, BinaryIntOpTest) {
  3656. WEX::TestExecution::SetVerifyOutput verifySettings(
  3657. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3658. CComPtr<IStream> pStream;
  3659. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3660. CComPtr<ID3D12Device> pDevice;
  3661. if (!CreateDevice(&pDevice)) {
  3662. return;
  3663. }
  3664. // Read data from the table
  3665. size_t tableSize = sizeof(BinaryIntOpParameters) / sizeof(TableParameter);
  3666. TableParameterHandler handler(BinaryIntOpParameters, tableSize);
  3667. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3668. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3669. std::vector<int> *Validation_Input1 =
  3670. &handler.GetTableParamByName(L"Validation.Input1")->m_int32Table;
  3671. std::vector<int> *Validation_Input2 =
  3672. &handler.GetTableParamByName(L"Validation.Input2")->m_int32Table;
  3673. std::vector<int> *Validation_Expected1 =
  3674. &handler.GetTableParamByName(L"Validation.Expected1")->m_int32Table;
  3675. std::vector<int> *Validation_Expected2 =
  3676. &handler.GetTableParamByName(L"Validation.Expected2")->m_int32Table;
  3677. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  3678. size_t count = Validation_Input1->size();
  3679. size_t numExpected = Validation_Expected2->size() == 0 ? 1 : 2;
  3680. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3681. pDevice, m_support, pStream, "BinaryIntOp",
  3682. // this callbacked is called when the test
  3683. // is creating the resource to run the test
  3684. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3685. VERIFY_IS_TRUE(0 == _stricmp(Name, "SBinaryIntOp"));
  3686. size_t size = sizeof(SBinaryIntOp) * count;
  3687. Data.resize(size);
  3688. SBinaryIntOp *pPrimitives = (SBinaryIntOp *)Data.data();
  3689. for (size_t i = 0; i < count; ++i) {
  3690. SBinaryIntOp *p = &pPrimitives[i];
  3691. int val1 = (*Validation_Input1)[i % Validation_Input1->size()];
  3692. int val2 = (*Validation_Input2)[i % Validation_Input2->size()];
  3693. p->input1 = val1;
  3694. p->input2 = val2;
  3695. }
  3696. // use shader from data table
  3697. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3698. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3699. });
  3700. MappedData data;
  3701. test->Test->GetReadBackData("SBinaryIntOp", &data);
  3702. SBinaryIntOp *pPrimitives = (SBinaryIntOp *)data.data();
  3703. WEX::TestExecution::DisableVerifyExceptions dve;
  3704. if (numExpected == 2) {
  3705. for (unsigned i = 0; i < count; ++i) {
  3706. SBinaryIntOp *p = &pPrimitives[i];
  3707. int val1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  3708. int val2 = (*Validation_Expected2)[i % Validation_Expected2->size()];
  3709. LogCommentFmt(L"element #%u, input1 = %11i(0x%08x), input2 = "
  3710. L"%11i(0x%08x), output1 = "
  3711. L"%11i(0x%08x), expected1 = %11i(0x%08x), output2 = "
  3712. L"%11i(0x%08x), expected2 = %11i(0x%08x)",
  3713. i, p->input1, p->input1, p->input2, p->input2, p->output1,
  3714. p->output1, val1, val1, p->output2, p->output2, val2,
  3715. val2);
  3716. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  3717. VerifyOutputWithExpectedValueInt(p->output2, val2, Validation_Tolerance);
  3718. }
  3719. }
  3720. else if (numExpected == 1) {
  3721. for (unsigned i = 0; i < count; ++i) {
  3722. SBinaryIntOp *p = &pPrimitives[i];
  3723. int val1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  3724. LogCommentFmt(L"element #%u, input1 = %11i(0x%08x), input2 = "
  3725. L"%11i(0x%08x), output = "
  3726. L"%11i(0x%08x), expected = %11i(0x%08x)", i,
  3727. p->input1, p->input1, p->input2, p->input2,
  3728. p->output1, p->output1, val1, val1);
  3729. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  3730. }
  3731. }
  3732. else {
  3733. LogErrorFmt(L"Unexpected number of expected values for operation %i", numExpected);
  3734. }
  3735. }
  3736. TEST_F(ExecutionTest, TertiaryIntOpTest) {
  3737. WEX::TestExecution::SetVerifyOutput verifySettings(
  3738. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3739. CComPtr<IStream> pStream;
  3740. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3741. CComPtr<ID3D12Device> pDevice;
  3742. if (!CreateDevice(&pDevice)) {
  3743. return;
  3744. }
  3745. // Read data from the table
  3746. size_t tableSize = sizeof(TertiaryIntOpParameters) / sizeof(TableParameter);
  3747. TableParameterHandler handler(TertiaryIntOpParameters, tableSize);
  3748. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3749. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3750. std::vector<int> *Validation_Input1 =
  3751. &handler.GetTableParamByName(L"Validation.Input1")->m_int32Table;
  3752. std::vector<int> *Validation_Input2 =
  3753. &handler.GetTableParamByName(L"Validation.Input2")->m_int32Table;
  3754. std::vector<int> *Validation_Input3 =
  3755. &handler.GetTableParamByName(L"Validation.Input3")->m_int32Table;
  3756. std::vector<int> *Validation_Expected =
  3757. &handler.GetTableParamByName(L"Validation.Expected1")->m_int32Table;
  3758. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  3759. size_t count = Validation_Input1->size();
  3760. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3761. pDevice, m_support, pStream, "TertiaryIntOp",
  3762. // this callbacked is called when the test
  3763. // is creating the resource to run the test
  3764. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3765. VERIFY_IS_TRUE(0 == _stricmp(Name, "STertiaryIntOp"));
  3766. size_t size = sizeof(STertiaryIntOp) * count;
  3767. Data.resize(size);
  3768. STertiaryIntOp *pPrimitives = (STertiaryIntOp *)Data.data();
  3769. for (size_t i = 0; i < count; ++i) {
  3770. STertiaryIntOp *p = &pPrimitives[i];
  3771. int val1 = (*Validation_Input1)[i % Validation_Input1->size()];
  3772. int val2 = (*Validation_Input2)[i % Validation_Input2->size()];
  3773. int val3 = (*Validation_Input3)[i % Validation_Input3->size()];
  3774. p->input1 = val1;
  3775. p->input2 = val2;
  3776. p->input3 = val3;
  3777. }
  3778. // use shader from data table
  3779. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3780. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3781. });
  3782. MappedData data;
  3783. test->Test->GetReadBackData("STertiaryIntOp", &data);
  3784. STertiaryIntOp *pPrimitives = (STertiaryIntOp *)data.data();
  3785. WEX::TestExecution::DisableVerifyExceptions dve;
  3786. for (unsigned i = 0; i < count; ++i) {
  3787. STertiaryIntOp *p = &pPrimitives[i];
  3788. int val1 = (*Validation_Expected)[i % Validation_Expected->size()];
  3789. LogCommentFmt(L"element #%u, input1 = %11i(0x%08x), input2 = "
  3790. L"%11i(0x%08x), input3= %11i(0x%08x), output = "
  3791. L"%11i(0x%08x), expected = %11i(0x%08x)",
  3792. i, p->input1, p->input1, p->input2, p->input2,
  3793. p->input3, p->input3, p->output, p->output, val1,
  3794. val1);
  3795. VerifyOutputWithExpectedValueInt(p->output, val1, Validation_Tolerance);
  3796. }
  3797. }
  3798. TEST_F(ExecutionTest, BinaryUintOpTest) {
  3799. WEX::TestExecution::SetVerifyOutput verifySettings(
  3800. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3801. CComPtr<IStream> pStream;
  3802. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3803. CComPtr<ID3D12Device> pDevice;
  3804. if (!CreateDevice(&pDevice)) {
  3805. return;
  3806. }
  3807. // Read data from the table
  3808. size_t tableSize = sizeof(BinaryUintOpParameters) / sizeof(TableParameter);
  3809. TableParameterHandler handler(BinaryUintOpParameters, tableSize);
  3810. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3811. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3812. std::vector<unsigned int> *Validation_Input1 =
  3813. &handler.GetTableParamByName(L"Validation.Input1")->m_uint32Table;
  3814. std::vector<unsigned int> *Validation_Input2 =
  3815. &handler.GetTableParamByName(L"Validation.Input2")->m_uint32Table;
  3816. std::vector<unsigned int> *Validation_Expected1 =
  3817. &handler.GetTableParamByName(L"Validation.Expected1")->m_uint32Table;
  3818. std::vector<unsigned int> *Validation_Expected2 =
  3819. &handler.GetTableParamByName(L"Validation.Expected2")->m_uint32Table;
  3820. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  3821. size_t count = Validation_Input1->size();
  3822. int numExpected = Validation_Expected2->size() == 0 ? 1 : 2;
  3823. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3824. pDevice, m_support, pStream, "BinaryUintOp",
  3825. // this callbacked is called when the test
  3826. // is creating the resource to run the test
  3827. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3828. VERIFY_IS_TRUE(0 == _stricmp(Name, "SBinaryUintOp"));
  3829. size_t size = sizeof(SBinaryUintOp) * count;
  3830. Data.resize(size);
  3831. SBinaryUintOp *pPrimitives = (SBinaryUintOp *)Data.data();
  3832. for (size_t i = 0; i < count; ++i) {
  3833. SBinaryUintOp *p = &pPrimitives[i];
  3834. unsigned int val1 = (*Validation_Input1)[i % Validation_Input1->size()];
  3835. unsigned int val2 = (*Validation_Input2)[i % Validation_Input2->size()];
  3836. p->input1 = val1;
  3837. p->input2 = val2;
  3838. }
  3839. // use shader from data table
  3840. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3841. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3842. });
  3843. MappedData data;
  3844. test->Test->GetReadBackData("SBinaryUintOp", &data);
  3845. SBinaryUintOp *pPrimitives = (SBinaryUintOp *)data.data();
  3846. WEX::TestExecution::DisableVerifyExceptions dve;
  3847. if (numExpected == 2) {
  3848. for (unsigned i = 0; i < count; ++i) {
  3849. SBinaryUintOp *p = &pPrimitives[i];
  3850. unsigned int val1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  3851. unsigned int val2 = (*Validation_Expected2)[i % Validation_Expected2->size()];
  3852. LogCommentFmt(L"element #%u, input1 = %11u(0x%08x), input2 = "
  3853. L"%11u(0x%08x), output1 = "
  3854. L"%11u(0x%08x), expected1 = %11u(0x%08x), output2 = "
  3855. L"%11u(0x%08x), expected2 = %11u(0x%08x)",
  3856. i, p->input1, p->input1, p->input2, p->input2, p->output1,
  3857. p->output1, val1, val1, p->output2, p->output2, val2,
  3858. val2);
  3859. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  3860. VerifyOutputWithExpectedValueInt(p->output2, val2, Validation_Tolerance);
  3861. }
  3862. }
  3863. else if (numExpected == 1) {
  3864. for (unsigned i = 0; i < count; ++i) {
  3865. SBinaryUintOp *p = &pPrimitives[i];
  3866. unsigned int val1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  3867. LogCommentFmt(L"element #%u, input1 = %11u(0x%08x), input2 = "
  3868. L"%11u(0x%08x), output = "
  3869. L"%11u(0x%08x), expected = %11u(0x%08x)", i,
  3870. p->input1, p->input1, p->input2, p->input2,
  3871. p->output1, p->output1, val1, val1);
  3872. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  3873. }
  3874. }
  3875. else {
  3876. LogErrorFmt(L"Unexpected number of expected values for operation %i", numExpected);
  3877. }
  3878. }
  3879. TEST_F(ExecutionTest, TertiaryUintOpTest) {
  3880. WEX::TestExecution::SetVerifyOutput verifySettings(
  3881. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3882. CComPtr<IStream> pStream;
  3883. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3884. CComPtr<ID3D12Device> pDevice;
  3885. if (!CreateDevice(&pDevice)) {
  3886. return;
  3887. }
  3888. // Read data from the table
  3889. size_t tableSize = sizeof(TertiaryUintOpParameters) / sizeof(TableParameter);
  3890. TableParameterHandler handler(TertiaryUintOpParameters, tableSize);
  3891. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3892. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3893. std::vector<unsigned int> *Validation_Input1 =
  3894. &handler.GetTableParamByName(L"Validation.Input1")->m_uint32Table;
  3895. std::vector<unsigned int> *Validation_Input2 =
  3896. &handler.GetTableParamByName(L"Validation.Input2")->m_uint32Table;
  3897. std::vector<unsigned int> *Validation_Input3 =
  3898. &handler.GetTableParamByName(L"Validation.Input3")->m_uint32Table;
  3899. std::vector<unsigned int> *Validation_Expected =
  3900. &handler.GetTableParamByName(L"Validation.Expected1")->m_uint32Table;
  3901. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  3902. size_t count = Validation_Input1->size();
  3903. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3904. pDevice, m_support, pStream, "TertiaryUintOp",
  3905. // this callbacked is called when the test
  3906. // is creating the resource to run the test
  3907. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3908. VERIFY_IS_TRUE(0 == _stricmp(Name, "STertiaryUintOp"));
  3909. size_t size = sizeof(STertiaryUintOp) * count;
  3910. Data.resize(size);
  3911. STertiaryUintOp *pPrimitives = (STertiaryUintOp *)Data.data();
  3912. for (size_t i = 0; i < count; ++i) {
  3913. STertiaryUintOp *p = &pPrimitives[i];
  3914. unsigned int val1 = (*Validation_Input1)[i % Validation_Input1->size()];
  3915. unsigned int val2 = (*Validation_Input2)[i % Validation_Input2->size()];
  3916. unsigned int val3 = (*Validation_Input3)[i % Validation_Input3->size()];
  3917. p->input1 = val1;
  3918. p->input2 = val2;
  3919. p->input3 = val3;
  3920. }
  3921. // use shader from data table
  3922. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3923. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3924. });
  3925. MappedData data;
  3926. test->Test->GetReadBackData("STertiaryUintOp", &data);
  3927. STertiaryUintOp *pPrimitives = (STertiaryUintOp *)data.data();
  3928. WEX::TestExecution::DisableVerifyExceptions dve;
  3929. for (unsigned i = 0; i < count; ++i) {
  3930. STertiaryUintOp *p = &pPrimitives[i];
  3931. unsigned int val1 = (*Validation_Expected)[i % Validation_Expected->size()];
  3932. LogCommentFmt(L"element #%u, input1 = %11u(0x%08x), input2 = "
  3933. L"%11u(0x%08x), input3 = %11u(0x%08x), output = "
  3934. L"%11u(0x%08x), expected = %11u(0x%08x)", i,
  3935. p->input1, p->input1, p->input2, p->input2, p->input3, p->input3,
  3936. p->output, p->output, val1, val1);
  3937. VerifyOutputWithExpectedValueInt(p->output, val1, Validation_Tolerance);
  3938. }
  3939. }
  3940. // 16 bit integer type tests
  3941. TEST_F(ExecutionTest, UnaryInt16OpTest) {
  3942. WEX::TestExecution::SetVerifyOutput verifySettings(
  3943. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3944. CComPtr<IStream> pStream;
  3945. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3946. CComPtr<ID3D12Device> pDevice;
  3947. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL::D3D_SHADER_MODEL_6_2)) {
  3948. return;
  3949. }
  3950. // Read data from the table
  3951. int tableSize = sizeof(UnaryInt16OpParameters) / sizeof(TableParameter);
  3952. TableParameterHandler handler(UnaryInt16OpParameters, tableSize);
  3953. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  3954. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  3955. CW2A Arguments(handler.GetTableParamByName(L"ShaderOp.Arguments")->m_str);
  3956. std::vector<short> *Validation_Input =
  3957. &handler.GetTableParamByName(L"Validation.Input1")->m_int16Table;
  3958. std::vector<short> *Validation_Expected =
  3959. &handler.GetTableParamByName(L"Validation.Expected1")->m_int16Table;
  3960. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  3961. size_t count = Validation_Input->size();
  3962. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  3963. pDevice, m_support, pStream, "UnaryIntOp",
  3964. // this callbacked is called when the test
  3965. // is creating the resource to run the test
  3966. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  3967. VERIFY_IS_TRUE(0 == _stricmp(Name, "SUnaryIntOp"));
  3968. size_t size = sizeof(SUnaryInt16Op) * count;
  3969. Data.resize(size);
  3970. SUnaryInt16Op *pPrimitives = (SUnaryInt16Op *)Data.data();
  3971. for (size_t i = 0; i < count; ++i) {
  3972. SUnaryInt16Op *p = &pPrimitives[i];
  3973. p->input = (*Validation_Input)[i % Validation_Input->size()];
  3974. }
  3975. // use shader data table
  3976. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  3977. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  3978. pShaderOp->Shaders.at(0).Arguments = Arguments.m_psz;
  3979. });
  3980. MappedData data;
  3981. test->Test->GetReadBackData("SUnaryIntOp", &data);
  3982. SUnaryInt16Op *pPrimitives = (SUnaryInt16Op *)data.data();
  3983. WEX::TestExecution::DisableVerifyExceptions dve;
  3984. for (unsigned i = 0; i < count; ++i) {
  3985. SUnaryInt16Op *p = &pPrimitives[i];
  3986. short val = (*Validation_Expected)[i % Validation_Expected->size()];
  3987. LogCommentFmt(L"element #%u, input = %5hi(0x%08x), output = %5hi(0x%08x), "
  3988. L"expected = %5hi(0x%08x)",
  3989. i, p->input, p->input, p->output, p->output, val, val);
  3990. VerifyOutputWithExpectedValueInt(p->output, val, Validation_Tolerance);
  3991. }
  3992. }
  3993. TEST_F(ExecutionTest, UnaryUint16OpTest) {
  3994. WEX::TestExecution::SetVerifyOutput verifySettings(
  3995. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  3996. CComPtr<IStream> pStream;
  3997. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  3998. CComPtr<ID3D12Device> pDevice;
  3999. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL::D3D_SHADER_MODEL_6_2)) {
  4000. return;
  4001. }
  4002. // Read data from the table
  4003. int tableSize = sizeof(UnaryUint16OpParameters) / sizeof(TableParameter);
  4004. TableParameterHandler handler(UnaryUint16OpParameters, tableSize);
  4005. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  4006. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  4007. CW2A Arguments(handler.GetTableParamByName(L"ShaderOp.Arguments")->m_str);
  4008. std::vector<unsigned short> *Validation_Input =
  4009. &handler.GetTableParamByName(L"Validation.Input1")->m_uint16Table;
  4010. std::vector<unsigned short> *Validation_Expected =
  4011. &handler.GetTableParamByName(L"Validation.Expected1")->m_uint16Table;
  4012. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  4013. size_t count = Validation_Input->size();
  4014. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  4015. pDevice, m_support, pStream, "UnaryUintOp",
  4016. // this callbacked is called when the test
  4017. // is creating the resource to run the test
  4018. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  4019. VERIFY_IS_TRUE(0 == _stricmp(Name, "SUnaryUintOp"));
  4020. size_t size = sizeof(SUnaryUint16Op) * count;
  4021. Data.resize(size);
  4022. SUnaryUint16Op *pPrimitives = (SUnaryUint16Op *)Data.data();
  4023. for (size_t i = 0; i < count; ++i) {
  4024. SUnaryUint16Op *p = &pPrimitives[i];
  4025. p->input = (*Validation_Input)[i % Validation_Input->size()];
  4026. }
  4027. // use shader data table
  4028. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  4029. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  4030. pShaderOp->Shaders.at(0).Arguments = Arguments.m_psz;
  4031. });
  4032. MappedData data;
  4033. test->Test->GetReadBackData("SUnaryUintOp", &data);
  4034. SUnaryUint16Op *pPrimitives = (SUnaryUint16Op *)data.data();
  4035. WEX::TestExecution::DisableVerifyExceptions dve;
  4036. for (unsigned i = 0; i < count; ++i) {
  4037. SUnaryUint16Op *p = &pPrimitives[i];
  4038. unsigned short val = (*Validation_Expected)[i % Validation_Expected->size()];
  4039. LogCommentFmt(L"element #%u, input = %5hu(0x%08x), output = %5hu(0x%08x), "
  4040. L"expected = %5hu(0x%08x)",
  4041. i, p->input, p->input, p->output, p->output, val, val);
  4042. VerifyOutputWithExpectedValueInt(p->output, val, Validation_Tolerance);
  4043. }
  4044. }
  4045. TEST_F(ExecutionTest, BinaryInt16OpTest) {
  4046. WEX::TestExecution::SetVerifyOutput verifySettings(
  4047. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  4048. CComPtr<IStream> pStream;
  4049. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  4050. CComPtr<ID3D12Device> pDevice;
  4051. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL::D3D_SHADER_MODEL_6_2)) {
  4052. return;
  4053. }
  4054. // Read data from the table
  4055. size_t tableSize = sizeof(BinaryInt16OpParameters) / sizeof(TableParameter);
  4056. TableParameterHandler handler(BinaryInt16OpParameters, tableSize);
  4057. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  4058. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  4059. CW2A Arguments(handler.GetTableParamByName(L"ShaderOp.Arguments")->m_str);
  4060. std::vector<short> *Validation_Input1 =
  4061. &handler.GetTableParamByName(L"Validation.Input1")->m_int16Table;
  4062. std::vector<short> *Validation_Input2 =
  4063. &handler.GetTableParamByName(L"Validation.Input2")->m_int16Table;
  4064. std::vector<short> *Validation_Expected1 =
  4065. &handler.GetTableParamByName(L"Validation.Expected1")->m_int16Table;
  4066. std::vector<short> *Validation_Expected2 =
  4067. &handler.GetTableParamByName(L"Validation.Expected2")->m_int16Table;
  4068. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  4069. size_t count = Validation_Input1->size();
  4070. size_t numExpected = Validation_Expected2->size() == 0 ? 1 : 2;
  4071. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  4072. pDevice, m_support, pStream, "BinaryIntOp",
  4073. // this callbacked is called when the test
  4074. // is creating the resource to run the test
  4075. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  4076. VERIFY_IS_TRUE(0 == _stricmp(Name, "SBinaryIntOp"));
  4077. size_t size = sizeof(SBinaryInt16Op) * count;
  4078. Data.resize(size);
  4079. SBinaryInt16Op *pPrimitives = (SBinaryInt16Op *)Data.data();
  4080. for (size_t i = 0; i < count; ++i) {
  4081. SBinaryInt16Op *p = &pPrimitives[i];
  4082. p->input1 = (*Validation_Input1)[i % Validation_Input1->size()];
  4083. p->input2 = (*Validation_Input2)[i % Validation_Input2->size()];
  4084. }
  4085. // use shader from data table
  4086. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  4087. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  4088. pShaderOp->Shaders.at(0).Arguments = Arguments.m_psz;
  4089. });
  4090. MappedData data;
  4091. test->Test->GetReadBackData("SBinaryIntOp", &data);
  4092. SBinaryInt16Op *pPrimitives = (SBinaryInt16Op *)data.data();
  4093. WEX::TestExecution::DisableVerifyExceptions dve;
  4094. if (numExpected == 2) {
  4095. for (unsigned i = 0; i < count; ++i) {
  4096. SBinaryInt16Op *p = &pPrimitives[i];
  4097. short val1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  4098. short val2 = (*Validation_Expected2)[i % Validation_Expected2->size()];
  4099. LogCommentFmt(L"element #%u, input1 = %5hi(0x%08x), input2 = "
  4100. L"%5hi(0x%08x), output1 = "
  4101. L"%5hi(0x%08x), expected1 = %5hi(0x%08x), output2 = "
  4102. L"%5hi(0x%08x), expected2 = %5hi(0x%08x)",
  4103. i, p->input1, p->input1, p->input2, p->input2, p->output1,
  4104. p->output1, val1, val1, p->output2, p->output2, val2,
  4105. val2);
  4106. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  4107. VerifyOutputWithExpectedValueInt(p->output2, val2, Validation_Tolerance);
  4108. }
  4109. }
  4110. else if (numExpected == 1) {
  4111. for (unsigned i = 0; i < count; ++i) {
  4112. SBinaryInt16Op *p = &pPrimitives[i];
  4113. short val1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  4114. LogCommentFmt(L"element #%u, input1 = %5hi(0x%08x), input2 = "
  4115. L"%5hi(0x%08x), output = "
  4116. L"%5hi(0x%08x), expected = %5hi(0x%08x)", i,
  4117. p->input1, p->input1, p->input2, p->input2,
  4118. p->output1, p->output1, val1, val1);
  4119. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  4120. }
  4121. }
  4122. else {
  4123. LogErrorFmt(L"Unexpected number of expected values for operation %i", numExpected);
  4124. }
  4125. }
  4126. TEST_F(ExecutionTest, TertiaryInt16OpTest) {
  4127. WEX::TestExecution::SetVerifyOutput verifySettings(
  4128. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  4129. CComPtr<IStream> pStream;
  4130. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  4131. CComPtr<ID3D12Device> pDevice;
  4132. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL::D3D_SHADER_MODEL_6_2)) {
  4133. return;
  4134. }
  4135. // Read data from the table
  4136. size_t tableSize = sizeof(TertiaryInt16OpParameters) / sizeof(TableParameter);
  4137. TableParameterHandler handler(TertiaryInt16OpParameters, tableSize);
  4138. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  4139. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  4140. CW2A Arguments(handler.GetTableParamByName(L"ShaderOp.Arguments")->m_str);
  4141. std::vector<short> *Validation_Input1 =
  4142. &handler.GetTableParamByName(L"Validation.Input1")->m_int16Table;
  4143. std::vector<short> *Validation_Input2 =
  4144. &handler.GetTableParamByName(L"Validation.Input2")->m_int16Table;
  4145. std::vector<short> *Validation_Input3 =
  4146. &handler.GetTableParamByName(L"Validation.Input3")->m_int16Table;
  4147. std::vector<short> *Validation_Expected =
  4148. &handler.GetTableParamByName(L"Validation.Expected1")->m_int16Table;
  4149. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  4150. size_t count = Validation_Input1->size();
  4151. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  4152. pDevice, m_support, pStream, "TertiaryIntOp",
  4153. // this callbacked is called when the test
  4154. // is creating the resource to run the test
  4155. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  4156. VERIFY_IS_TRUE(0 == _stricmp(Name, "STertiaryIntOp"));
  4157. size_t size = sizeof(STertiaryInt16Op) * count;
  4158. Data.resize(size);
  4159. STertiaryInt16Op *pPrimitives = (STertiaryInt16Op *)Data.data();
  4160. for (size_t i = 0; i < count; ++i) {
  4161. STertiaryInt16Op *p = &pPrimitives[i];
  4162. p->input1 = (*Validation_Input1)[i % Validation_Input1->size()];
  4163. p->input2 = (*Validation_Input2)[i % Validation_Input2->size()];
  4164. p->input3 = (*Validation_Input3)[i % Validation_Input3->size()];
  4165. }
  4166. // use shader from data table
  4167. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  4168. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  4169. pShaderOp->Shaders.at(0).Arguments = Arguments.m_psz;
  4170. });
  4171. MappedData data;
  4172. test->Test->GetReadBackData("STertiaryIntOp", &data);
  4173. STertiaryInt16Op *pPrimitives = (STertiaryInt16Op *)data.data();
  4174. WEX::TestExecution::DisableVerifyExceptions dve;
  4175. for (unsigned i = 0; i < count; ++i) {
  4176. STertiaryInt16Op *p = &pPrimitives[i];
  4177. short val1 = (*Validation_Expected)[i % Validation_Expected->size()];
  4178. LogCommentFmt(L"element #%u, input1 = %11i(0x%08x), input2 = "
  4179. L"%11i(0x%08x), input3= %11i(0x%08x), output = "
  4180. L"%11i(0x%08x), expected = %11i(0x%08x)",
  4181. i, p->input1, p->input1, p->input2, p->input2,
  4182. p->input3, p->input3, p->output, p->output, val1,
  4183. val1);
  4184. VerifyOutputWithExpectedValueInt(p->output, val1, Validation_Tolerance);
  4185. }
  4186. }
  4187. TEST_F(ExecutionTest, BinaryUint16OpTest) {
  4188. WEX::TestExecution::SetVerifyOutput verifySettings(
  4189. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  4190. CComPtr<IStream> pStream;
  4191. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  4192. CComPtr<ID3D12Device> pDevice;
  4193. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL::D3D_SHADER_MODEL_6_2)) {
  4194. return;
  4195. }
  4196. // Read data from the table
  4197. size_t tableSize = sizeof(BinaryUint16OpParameters) / sizeof(TableParameter);
  4198. TableParameterHandler handler(BinaryUint16OpParameters, tableSize);
  4199. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  4200. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  4201. CW2A Arguments(handler.GetTableParamByName(L"ShaderOp.Arguments")->m_str);
  4202. std::vector<unsigned short> *Validation_Input1 =
  4203. &handler.GetTableParamByName(L"Validation.Input1")->m_uint16Table;
  4204. std::vector<unsigned short> *Validation_Input2 =
  4205. &handler.GetTableParamByName(L"Validation.Input2")->m_uint16Table;
  4206. std::vector<unsigned short> *Validation_Expected1 =
  4207. &handler.GetTableParamByName(L"Validation.Expected1")->m_uint16Table;
  4208. std::vector<unsigned short> *Validation_Expected2 =
  4209. &handler.GetTableParamByName(L"Validation.Expected2")->m_uint16Table;
  4210. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  4211. size_t count = Validation_Input1->size();
  4212. int numExpected = Validation_Expected2->size() == 0 ? 1 : 2;
  4213. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  4214. pDevice, m_support, pStream, "BinaryUintOp",
  4215. // this callbacked is called when the test
  4216. // is creating the resource to run the test
  4217. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  4218. VERIFY_IS_TRUE(0 == _stricmp(Name, "SBinaryUintOp"));
  4219. size_t size = sizeof(SBinaryUint16Op) * count;
  4220. Data.resize(size);
  4221. SBinaryUint16Op *pPrimitives = (SBinaryUint16Op *)Data.data();
  4222. for (size_t i = 0; i < count; ++i) {
  4223. SBinaryUint16Op *p = &pPrimitives[i];
  4224. p->input1 = (*Validation_Input1)[i % Validation_Input1->size()];
  4225. p->input2 = (*Validation_Input2)[i % Validation_Input2->size()];
  4226. }
  4227. // use shader from data table
  4228. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  4229. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  4230. pShaderOp->Shaders.at(0).Arguments = Arguments.m_psz;
  4231. });
  4232. MappedData data;
  4233. test->Test->GetReadBackData("SBinaryUintOp", &data);
  4234. SBinaryUint16Op *pPrimitives = (SBinaryUint16Op *)data.data();
  4235. WEX::TestExecution::DisableVerifyExceptions dve;
  4236. if (numExpected == 2) {
  4237. for (unsigned i = 0; i < count; ++i) {
  4238. SBinaryUint16Op *p = &pPrimitives[i];
  4239. unsigned short val1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  4240. unsigned short val2 = (*Validation_Expected2)[i % Validation_Expected2->size()];
  4241. LogCommentFmt(L"element #%u, input1 = %5hu(0x%08x), input2 = "
  4242. L"%5hu(0x%08x), output1 = "
  4243. L"%5hu(0x%08x), expected1 = %5hu(0x%08x), output2 = "
  4244. L"%5hu(0x%08x), expected2 = %5hu(0x%08x)",
  4245. i, p->input1, p->input1, p->input2, p->input2, p->output1,
  4246. p->output1, val1, val1, p->output2, p->output2, val2,
  4247. val2);
  4248. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  4249. VerifyOutputWithExpectedValueInt(p->output2, val2, Validation_Tolerance);
  4250. }
  4251. }
  4252. else if (numExpected == 1) {
  4253. for (unsigned i = 0; i < count; ++i) {
  4254. SBinaryUint16Op *p = &pPrimitives[i];
  4255. unsigned short val1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  4256. LogCommentFmt(L"element #%u, input1 = %5hu(0x%08x), input2 = "
  4257. L"%5hu(0x%08x), output = "
  4258. L"%5hu(0x%08x), expected = %5hu(0x%08x)", i,
  4259. p->input1, p->input1, p->input2, p->input2,
  4260. p->output1, p->output1, val1, val1);
  4261. VerifyOutputWithExpectedValueInt(p->output1, val1, Validation_Tolerance);
  4262. }
  4263. }
  4264. else {
  4265. LogErrorFmt(L"Unexpected number of expected values for operation %i", numExpected);
  4266. }
  4267. }
  4268. TEST_F(ExecutionTest, TertiaryUint16OpTest) {
  4269. WEX::TestExecution::SetVerifyOutput verifySettings(
  4270. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  4271. CComPtr<IStream> pStream;
  4272. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  4273. CComPtr<ID3D12Device> pDevice;
  4274. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL::D3D_SHADER_MODEL_6_2)) {
  4275. return;
  4276. }
  4277. // Read data from the table
  4278. size_t tableSize = sizeof(TertiaryUint16OpParameters) / sizeof(TableParameter);
  4279. TableParameterHandler handler(TertiaryUint16OpParameters, tableSize);
  4280. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  4281. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  4282. CW2A Arguments(handler.GetTableParamByName(L"ShaderOp.Arguments")->m_str);
  4283. std::vector<unsigned short> *Validation_Input1 =
  4284. &handler.GetTableParamByName(L"Validation.Input1")->m_uint16Table;
  4285. std::vector<unsigned short> *Validation_Input2 =
  4286. &handler.GetTableParamByName(L"Validation.Input2")->m_uint16Table;
  4287. std::vector<unsigned short> *Validation_Input3 =
  4288. &handler.GetTableParamByName(L"Validation.Input3")->m_uint16Table;
  4289. std::vector<unsigned short> *Validation_Expected =
  4290. &handler.GetTableParamByName(L"Validation.Expected1")->m_uint16Table;
  4291. int Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_int32;
  4292. size_t count = Validation_Input1->size();
  4293. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  4294. pDevice, m_support, pStream, "TertiaryUintOp",
  4295. // this callbacked is called when the test
  4296. // is creating the resource to run the test
  4297. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  4298. VERIFY_IS_TRUE(0 == _stricmp(Name, "STertiaryUintOp"));
  4299. size_t size = sizeof(STertiaryUint16Op) * count;
  4300. Data.resize(size);
  4301. STertiaryUint16Op *pPrimitives = (STertiaryUint16Op *)Data.data();
  4302. for (size_t i = 0; i < count; ++i) {
  4303. STertiaryUint16Op *p = &pPrimitives[i];
  4304. p->input1 = (*Validation_Input1)[i % Validation_Input1->size()];
  4305. p->input2 = (*Validation_Input2)[i % Validation_Input2->size()];
  4306. p->input3 = (*Validation_Input3)[i % Validation_Input3->size()];
  4307. }
  4308. // use shader from data table
  4309. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  4310. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  4311. pShaderOp->Shaders.at(0).Arguments = Arguments.m_psz;
  4312. });
  4313. MappedData data;
  4314. test->Test->GetReadBackData("STertiaryUintOp", &data);
  4315. STertiaryUint16Op *pPrimitives = (STertiaryUint16Op *)data.data();
  4316. WEX::TestExecution::DisableVerifyExceptions dve;
  4317. for (unsigned i = 0; i < count; ++i) {
  4318. STertiaryUint16Op *p = &pPrimitives[i];
  4319. unsigned short val1 = (*Validation_Expected)[i % Validation_Expected->size()];
  4320. LogCommentFmt(L"element #%u, input1 = %5hu(0x%08x), input2 = "
  4321. L"%5hu(0x%08x), input3 = %5hu(0x%08x), output = "
  4322. L"%5hu(0x%08x), expected = %5hu(0x%08x)", i,
  4323. p->input1, p->input1, p->input2, p->input2, p->input3, p->input3,
  4324. p->output, p->output, val1, val1);
  4325. VerifyOutputWithExpectedValueInt(p->output, val1, Validation_Tolerance);
  4326. }
  4327. }
  4328. TEST_F(ExecutionTest, DotTest) {
  4329. WEX::TestExecution::SetVerifyOutput verifySettings(
  4330. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  4331. CComPtr<IStream> pStream;
  4332. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  4333. CComPtr<ID3D12Device> pDevice;
  4334. if (!CreateDevice(&pDevice)) {
  4335. return;
  4336. }
  4337. int tableSize = sizeof(DotOpParameters) / sizeof(TableParameter);
  4338. TableParameterHandler handler(DotOpParameters, tableSize);
  4339. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  4340. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  4341. std::vector<WEX::Common::String> *Validation_Input1 =
  4342. &handler.GetTableParamByName(L"Validation.Input1")->m_StringTable;
  4343. std::vector<WEX::Common::String> *Validation_Input2 =
  4344. &handler.GetTableParamByName(L"Validation.Input2")->m_StringTable;
  4345. std::vector<WEX::Common::String> *Validation_dot2 =
  4346. &handler.GetTableParamByName(L"Validation.Expected1")->m_StringTable;
  4347. std::vector<WEX::Common::String> *Validation_dot3 =
  4348. &handler.GetTableParamByName(L"Validation.Expected2")->m_StringTable;
  4349. std::vector<WEX::Common::String> *Validation_dot4 =
  4350. &handler.GetTableParamByName(L"Validation.Expected3")->m_StringTable;
  4351. PCWSTR Validation_type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  4352. double tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  4353. size_t count = Validation_Input1->size();
  4354. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  4355. pDevice, m_support, pStream, "DotOp",
  4356. // this callbacked is called when the test
  4357. // is creating the resource to run the test
  4358. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  4359. VERIFY_IS_TRUE(0 == _stricmp(Name, "SDotOp"));
  4360. size_t size = sizeof(SDotOp) * count;
  4361. Data.resize(size);
  4362. SDotOp *pPrimitives = (SDotOp*)Data.data();
  4363. for (size_t i = 0; i < count; ++i) {
  4364. SDotOp *p = &pPrimitives[i];
  4365. XMFLOAT4 val1,val2;
  4366. VERIFY_SUCCEEDED(ParseDataToVectorFloat((*Validation_Input1)[i],
  4367. (float *)&val1, 4));
  4368. VERIFY_SUCCEEDED(ParseDataToVectorFloat((*Validation_Input2)[i],
  4369. (float *)&val2, 4));
  4370. p->input1 = val1;
  4371. p->input2 = val2;
  4372. }
  4373. // use shader from data table
  4374. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  4375. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  4376. });
  4377. MappedData data;
  4378. test->Test->GetReadBackData("SDotOp", &data);
  4379. SDotOp *pPrimitives = (SDotOp*)data.data();
  4380. WEX::TestExecution::DisableVerifyExceptions dve;
  4381. for (size_t i = 0; i < count; ++i) {
  4382. SDotOp *p = &pPrimitives[i];
  4383. float dot2, dot3, dot4;
  4384. VERIFY_SUCCEEDED(ParseDataToFloat((*Validation_dot2)[i], dot2));
  4385. VERIFY_SUCCEEDED(ParseDataToFloat((*Validation_dot3)[i], dot3));
  4386. VERIFY_SUCCEEDED(ParseDataToFloat((*Validation_dot4)[i], dot4));
  4387. LogCommentFmt(
  4388. L"element #%u, input1 = (%f, %f, %f, %f), input2 = (%f, %f, "
  4389. L"%f, %f), \n dot2 = %f, dot2_expected = %f, dot3 = %f, "
  4390. L"dot3_expected = %f, dot4 = %f, dot4_expected = %f",
  4391. i, p->input1.x, p->input1.y, p->input1.z, p->input1.w, p->input2.x,
  4392. p->input2.y, p->input2.z, p->input2.w, p->o_dot2, dot2, p->o_dot3, dot3,
  4393. p->o_dot4, dot4);
  4394. VerifyOutputWithExpectedValueFloat(p->o_dot2, dot2, Validation_type,
  4395. tolerance);
  4396. VerifyOutputWithExpectedValueFloat(p->o_dot3, dot3, Validation_type,
  4397. tolerance);
  4398. VerifyOutputWithExpectedValueFloat(p->o_dot4, dot4, Validation_type,
  4399. tolerance);
  4400. }
  4401. }
  4402. TEST_F(ExecutionTest, Msad4Test) {
  4403. WEX::TestExecution::SetVerifyOutput verifySettings(
  4404. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  4405. CComPtr<IStream> pStream;
  4406. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  4407. CComPtr<ID3D12Device> pDevice;
  4408. if (!CreateDevice(&pDevice)) {
  4409. return;
  4410. }
  4411. size_t tableSize = sizeof(Msad4OpParameters) / sizeof(TableParameter);
  4412. TableParameterHandler handler(Msad4OpParameters, tableSize);
  4413. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  4414. double tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  4415. std::vector<unsigned int> *Validation_Reference =
  4416. &handler.GetTableParamByName(L"Validation.Input1")->m_uint32Table;
  4417. std::vector<WEX::Common::String> *Validation_Source =
  4418. &handler.GetTableParamByName(L"Validation.Input2")->m_StringTable;
  4419. std::vector<WEX::Common::String> *Validation_Accum =
  4420. &handler.GetTableParamByName(L"Validation.Input3")->m_StringTable;
  4421. std::vector<WEX::Common::String> *Validation_Expected =
  4422. &handler.GetTableParamByName(L"Validation.Expected1")->m_StringTable;
  4423. size_t count = Validation_Expected->size();
  4424. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  4425. pDevice, m_support, pStream, "Msad4",
  4426. // this callbacked is called when the test
  4427. // is creating the resource to run the test
  4428. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  4429. VERIFY_IS_TRUE(0 == _stricmp(Name, "SMsad4"));
  4430. size_t size = sizeof(SMsad4) * count;
  4431. Data.resize(size);
  4432. SMsad4 *pPrimitives = (SMsad4*)Data.data();
  4433. for (size_t i = 0; i < count; ++i) {
  4434. SMsad4 *p = &pPrimitives[i];
  4435. XMUINT2 src;
  4436. XMUINT4 accum;
  4437. VERIFY_SUCCEEDED(ParseDataToVectorUint((*Validation_Source)[i], (unsigned int*)&src, 2));
  4438. VERIFY_SUCCEEDED(ParseDataToVectorUint((*Validation_Accum)[i], (unsigned int*)&accum, 4));
  4439. p->ref = (*Validation_Reference)[i];
  4440. p->src = src;
  4441. p->accum = accum;
  4442. }
  4443. // use shader from data table
  4444. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  4445. });
  4446. MappedData data;
  4447. test->Test->GetReadBackData("SMsad4", &data);
  4448. SMsad4 *pPrimitives = (SMsad4*)data.data();
  4449. WEX::TestExecution::DisableVerifyExceptions dve;
  4450. for (size_t i = 0; i < count; ++i) {
  4451. SMsad4 *p = &pPrimitives[i];
  4452. XMUINT4 result;
  4453. VERIFY_SUCCEEDED(ParseDataToVectorUint((*Validation_Expected)[i],
  4454. (unsigned int *)&result, 4));
  4455. LogCommentFmt(
  4456. L"element #%u, ref = %u(0x%08x), src = %u(0x%08x), %u(0x%08x), "
  4457. L"accum = %u(0x%08x), %u(0x%08x), %u(0x%08x), %u(0x%08x),\n"
  4458. L"result = %u(0x%08x), %u(0x%08x), %u(0x%08x), %u(0x%08x),\n"
  4459. L"expected = %u(0x%08x), %u(0x%08x), %u(0x%08x), %u(0x%08x)", i,
  4460. p->ref, p->ref, p->src.x, p->src.x, p->src.y, p->src.y, p->accum.x,
  4461. p->accum.x, p->accum.y, p->accum.y, p->accum.z, p->accum.z,
  4462. p->accum.w, p->accum.w, p->result.x, p->result.x, p->result.y,
  4463. p->result.y, p->result.z, p->result.z, p->result.w, p->result.w,
  4464. result.x, result.x, result.y, result.y, result.z, result.z,
  4465. result.w, result.w);
  4466. int toleranceInt = (int)tolerance;
  4467. VerifyOutputWithExpectedValueInt(p->result.x, result.x, toleranceInt);
  4468. VerifyOutputWithExpectedValueInt(p->result.y, result.y, toleranceInt);
  4469. VerifyOutputWithExpectedValueInt(p->result.z, result.z, toleranceInt);
  4470. VerifyOutputWithExpectedValueInt(p->result.w, result.w, toleranceInt);
  4471. }
  4472. }
  4473. TEST_F(ExecutionTest, DenormBinaryFloatOpTest) {
  4474. WEX::TestExecution::SetVerifyOutput verifySettings(
  4475. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  4476. CComPtr<IStream> pStream;
  4477. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  4478. CComPtr<ID3D12Device> pDevice;
  4479. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL::D3D_SHADER_MODEL_6_2)) {
  4480. return;
  4481. }
  4482. // Read data from the table
  4483. int tableSize = sizeof(DenormBinaryFPOpParameters) / sizeof(TableParameter);
  4484. TableParameterHandler handler(DenormBinaryFPOpParameters, tableSize);
  4485. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  4486. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  4487. CW2A Arguments(handler.GetTableParamByName(L"ShaderOp.Arguments")->m_str);
  4488. std::vector<WEX::Common::String> *Validation_Input1 =
  4489. &(handler.GetTableParamByName(L"Validation.Input1")->m_StringTable);
  4490. std::vector<WEX::Common::String> *Validation_Input2 =
  4491. &(handler.GetTableParamByName(L"Validation.Input2")->m_StringTable);
  4492. std::vector<WEX::Common::String> *Validation_Expected1 =
  4493. &(handler.GetTableParamByName(L"Validation.Expected1")->m_StringTable);
  4494. // two expected outputs for any mode
  4495. std::vector<WEX::Common::String> *Validation_Expected2 =
  4496. &(handler.GetTableParamByName(L"Validation.Expected2")->m_StringTable);
  4497. LPCWSTR Validation_Type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  4498. double Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  4499. size_t count = Validation_Input1->size();
  4500. using namespace hlsl::DXIL;
  4501. Float32DenormMode mode = Float32DenormMode::Any;
  4502. if (strcmp(Arguments.m_psz, "-denorm preserve") == 0) {
  4503. mode = Float32DenormMode::Preserve;
  4504. }
  4505. else if (strcmp(Arguments.m_psz, "-denorm ftz") == 0) {
  4506. mode = Float32DenormMode::FTZ;
  4507. }
  4508. if (mode == Float32DenormMode::Any) {
  4509. DXASSERT(Validation_Expected2->size() == Validation_Expected1->size(),
  4510. "must have same number of expected values");
  4511. }
  4512. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  4513. pDevice, m_support, pStream, "BinaryFPOp",
  4514. // this callbacked is called when the test
  4515. // is creating the resource to run the test
  4516. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  4517. VERIFY_IS_TRUE(0 == _stricmp(Name, "SBinaryFPOp"));
  4518. size_t size = sizeof(SBinaryFPOp) * count;
  4519. Data.resize(size);
  4520. SBinaryFPOp *pPrimitives = (SBinaryFPOp *)Data.data();
  4521. for (size_t i = 0; i < count; ++i) {
  4522. SBinaryFPOp *p = &pPrimitives[i];
  4523. PCWSTR str1 = (*Validation_Input1)[i % Validation_Input1->size()];
  4524. PCWSTR str2 = (*Validation_Input2)[i % Validation_Input2->size()];
  4525. float val1, val2;
  4526. VERIFY_SUCCEEDED(ParseDataToFloat(str1, val1));
  4527. VERIFY_SUCCEEDED(ParseDataToFloat(str2, val2));
  4528. p->input1 = val1;
  4529. p->input2 = val2;
  4530. }
  4531. // use shader from data table
  4532. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  4533. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  4534. pShaderOp->Shaders.at(0).Arguments = Arguments.m_psz;
  4535. });
  4536. MappedData data;
  4537. test->Test->GetReadBackData("SBinaryFPOp", &data);
  4538. SBinaryFPOp *pPrimitives = (SBinaryFPOp *)data.data();
  4539. WEX::TestExecution::DisableVerifyExceptions dve;
  4540. for (unsigned i = 0; i < count; ++i) {
  4541. SBinaryFPOp *p = &pPrimitives[i];
  4542. if (mode == Float32DenormMode::Any) {
  4543. LPCWSTR str1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  4544. LPCWSTR str2 = (*Validation_Expected2)[i % Validation_Expected2->size()];
  4545. float val1;
  4546. float val2;
  4547. VERIFY_SUCCEEDED(ParseDataToFloat(str1, val1));
  4548. VERIFY_SUCCEEDED(ParseDataToFloat(str2, val2));
  4549. LogCommentFmt(L"element #%u, input1 = %6.8f, input2 = %6.8f, output = "
  4550. L"%6.8f, expected = %6.8f(%x) or %6.8f(%x)",
  4551. i, p->input1, p->input2, p->output1, val1, *(int *)&val1, val2, *(int *)&val2);
  4552. VERIFY_IS_TRUE(
  4553. CompareOutputWithExpectedValueFloat(
  4554. p->output1, val1, Validation_Type, Validation_Tolerance, mode) ||
  4555. CompareOutputWithExpectedValueFloat(
  4556. p->output1, val2, Validation_Type, Validation_Tolerance, mode));
  4557. }
  4558. else {
  4559. LPCWSTR str1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  4560. float val1;
  4561. VERIFY_SUCCEEDED(ParseDataToFloat(str1, val1));
  4562. LogCommentFmt(L"element #%u, input1 = %6.8f, input2 = %6.8f, output = "
  4563. L"%6.8f, expected = %6.8f(%a)",
  4564. i, p->input1, p->input2, p->output1, val1, *(int *)&val1);
  4565. VerifyOutputWithExpectedValueFloat(p->output1, val1, Validation_Type,
  4566. Validation_Tolerance, mode);
  4567. }
  4568. }
  4569. }
  4570. TEST_F(ExecutionTest, DenormTertiaryFloatOpTest) {
  4571. WEX::TestExecution::SetVerifyOutput verifySettings(
  4572. WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  4573. CComPtr<IStream> pStream;
  4574. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  4575. CComPtr<ID3D12Device> pDevice;
  4576. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL::D3D_SHADER_MODEL_6_2)) {
  4577. return;
  4578. }
  4579. // Read data from the table
  4580. int tableSize = sizeof(DenormTertiaryFPOpParameters) / sizeof(TableParameter);
  4581. TableParameterHandler handler(DenormTertiaryFPOpParameters, tableSize);
  4582. CW2A Target(handler.GetTableParamByName(L"ShaderOp.Target")->m_str);
  4583. CW2A Text(handler.GetTableParamByName(L"ShaderOp.Text")->m_str);
  4584. CW2A Arguments(handler.GetTableParamByName(L"ShaderOp.Arguments")->m_str);
  4585. std::vector<WEX::Common::String> *Validation_Input1 =
  4586. &(handler.GetTableParamByName(L"Validation.Input1")->m_StringTable);
  4587. std::vector<WEX::Common::String> *Validation_Input2 =
  4588. &(handler.GetTableParamByName(L"Validation.Input2")->m_StringTable);
  4589. std::vector<WEX::Common::String> *Validation_Input3 =
  4590. &(handler.GetTableParamByName(L"Validation.Input3")->m_StringTable);
  4591. std::vector<WEX::Common::String> *Validation_Expected1 =
  4592. &(handler.GetTableParamByName(L"Validation.Expected1")->m_StringTable);
  4593. // two expected outputs for any mode
  4594. std::vector<WEX::Common::String> *Validation_Expected2 =
  4595. &(handler.GetTableParamByName(L"Validation.Expected2")->m_StringTable);
  4596. LPCWSTR Validation_Type = handler.GetTableParamByName(L"Validation.Type")->m_str;
  4597. double Validation_Tolerance = handler.GetTableParamByName(L"Validation.Tolerance")->m_double;
  4598. size_t count = Validation_Input1->size();
  4599. using namespace hlsl::DXIL;
  4600. Float32DenormMode mode = Float32DenormMode::Any;
  4601. if (strcmp(Arguments.m_psz, "-denorm preserve") == 0) {
  4602. mode = Float32DenormMode::Preserve;
  4603. }
  4604. else if (strcmp(Arguments.m_psz, "-denorm ftz") == 0) {
  4605. mode = Float32DenormMode::FTZ;
  4606. }
  4607. if (mode == Float32DenormMode::Any) {
  4608. DXASSERT(Validation_Expected2->size() == Validation_Expected1->size(),
  4609. "must have same number of expected values");
  4610. }
  4611. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(
  4612. pDevice, m_support, pStream, "TertiaryFPOp",
  4613. // this callbacked is called when the test
  4614. // is creating the resource to run the test
  4615. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  4616. VERIFY_IS_TRUE(0 == _stricmp(Name, "STertiaryFPOp"));
  4617. size_t size = sizeof(STertiaryFPOp) * count;
  4618. Data.resize(size);
  4619. STertiaryFPOp *pPrimitives = (STertiaryFPOp *)Data.data();
  4620. for (size_t i = 0; i < count; ++i) {
  4621. STertiaryFPOp *p = &pPrimitives[i];
  4622. PCWSTR str1 = (*Validation_Input1)[i % Validation_Input1->size()];
  4623. PCWSTR str2 = (*Validation_Input2)[i % Validation_Input2->size()];
  4624. PCWSTR str3 = (*Validation_Input3)[i % Validation_Input3->size()];
  4625. float val1, val2, val3;
  4626. VERIFY_SUCCEEDED(ParseDataToFloat(str1, val1));
  4627. VERIFY_SUCCEEDED(ParseDataToFloat(str2, val2));
  4628. VERIFY_SUCCEEDED(ParseDataToFloat(str3, val3));
  4629. p->input1 = val1;
  4630. p->input2 = val2;
  4631. p->input3 = val3;
  4632. }
  4633. // use shader from data table
  4634. pShaderOp->Shaders.at(0).Target = Target.m_psz;
  4635. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  4636. pShaderOp->Shaders.at(0).Arguments = Arguments.m_psz;
  4637. });
  4638. MappedData data;
  4639. test->Test->GetReadBackData("STertiaryFPOp", &data);
  4640. STertiaryFPOp *pPrimitives = (STertiaryFPOp *)data.data();
  4641. WEX::TestExecution::DisableVerifyExceptions dve;
  4642. for (unsigned i = 0; i < count; ++i) {
  4643. STertiaryFPOp *p = &pPrimitives[i];
  4644. if (mode == Float32DenormMode::Any) {
  4645. LPCWSTR str1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  4646. LPCWSTR str2 = (*Validation_Expected2)[i % Validation_Expected2->size()];
  4647. float val1;
  4648. float val2;
  4649. VERIFY_SUCCEEDED(ParseDataToFloat(str1, val1));
  4650. VERIFY_SUCCEEDED(ParseDataToFloat(str2, val2));
  4651. LogCommentFmt(L"element #%u, input1 = %6.8f, input2 = %6.8f, input3 = %6.8f, output = "
  4652. L"%6.8f, expected = %6.8f(%x) or %6.8f(%x)",
  4653. i, p->input1, p->input2, p->input3, p->output, val1, *(int *)&val1, val2, *(int *)&val2);
  4654. VERIFY_IS_TRUE(
  4655. CompareOutputWithExpectedValueFloat(
  4656. p->output, val1, Validation_Type, Validation_Tolerance, mode) ||
  4657. CompareOutputWithExpectedValueFloat(
  4658. p->output, val2, Validation_Type, Validation_Tolerance, mode));
  4659. }
  4660. else {
  4661. LPCWSTR str1 = (*Validation_Expected1)[i % Validation_Expected1->size()];
  4662. float val1;
  4663. VERIFY_SUCCEEDED(ParseDataToFloat(str1, val1));
  4664. LogCommentFmt(L"element #%u, input1 = %6.8f, input2 = %6.8f, input3 = %6.8f, output = "
  4665. L"%6.8f, expected = %6.8f(%a)",
  4666. i, p->input1, p->input2, p->input3, p->output, val1, *(int *)&val1);
  4667. VerifyOutputWithExpectedValueFloat(p->output, val1, Validation_Type,
  4668. Validation_Tolerance, mode);
  4669. }
  4670. }
  4671. }
  4672. // Setup for wave intrinsics tests
  4673. enum class ShaderOpKind {
  4674. WaveSum,
  4675. WaveProduct,
  4676. WaveActiveMax,
  4677. WaveActiveMin,
  4678. WaveCountBits,
  4679. WaveActiveAllEqual,
  4680. WaveActiveAnyTrue,
  4681. WaveActiveAllTrue,
  4682. WaveActiveBitOr,
  4683. WaveActiveBitAnd,
  4684. WaveActiveBitXor,
  4685. ShaderOpInvalid
  4686. };
  4687. struct ShaderOpKindPair {
  4688. LPCWSTR name;
  4689. ShaderOpKind kind;
  4690. };
  4691. static ShaderOpKindPair ShaderOpKindTable[] = {
  4692. { L"WaveActiveSum", ShaderOpKind::WaveSum },
  4693. { L"WaveActiveUSum", ShaderOpKind::WaveSum },
  4694. { L"WaveActiveProduct", ShaderOpKind::WaveProduct },
  4695. { L"WaveActiveUProduct", ShaderOpKind::WaveProduct },
  4696. { L"WaveActiveMax", ShaderOpKind::WaveActiveMax },
  4697. { L"WaveActiveUMax", ShaderOpKind::WaveActiveMax },
  4698. { L"WaveActiveMin", ShaderOpKind::WaveActiveMin },
  4699. { L"WaveActiveUMin", ShaderOpKind::WaveActiveMin },
  4700. { L"WaveActiveCountBits", ShaderOpKind::WaveCountBits },
  4701. { L"WaveActiveAllEqual", ShaderOpKind::WaveActiveAllEqual },
  4702. { L"WaveActiveAnyTrue", ShaderOpKind::WaveActiveAnyTrue },
  4703. { L"WaveActiveAllTrue", ShaderOpKind::WaveActiveAllTrue },
  4704. { L"WaveActiveBitOr", ShaderOpKind::WaveActiveBitOr },
  4705. { L"WaveActiveBitAnd", ShaderOpKind::WaveActiveBitAnd },
  4706. { L"WaveActiveBitXor", ShaderOpKind::WaveActiveBitXor },
  4707. { L"WavePrefixSum", ShaderOpKind::WaveSum },
  4708. { L"WavePrefixUSum", ShaderOpKind::WaveSum },
  4709. { L"WavePrefixProduct", ShaderOpKind::WaveProduct },
  4710. { L"WavePrefixUProduct", ShaderOpKind::WaveProduct },
  4711. { L"WavePrefixMax", ShaderOpKind::WaveActiveMax },
  4712. { L"WavePrefixUMax", ShaderOpKind::WaveActiveMax },
  4713. { L"WavePrefixMin", ShaderOpKind::WaveActiveMin },
  4714. { L"WavePrefixUMin", ShaderOpKind::WaveActiveMin },
  4715. { L"WavePrefixCountBits", ShaderOpKind::WaveCountBits }
  4716. };
  4717. ShaderOpKind GetShaderOpKind(LPCWSTR str) {
  4718. for (size_t i = 0; i < sizeof(ShaderOpKindTable)/sizeof(ShaderOpKindPair); ++i) {
  4719. if (_wcsicmp(ShaderOpKindTable[i].name, str) == 0) {
  4720. return ShaderOpKindTable[i].kind;
  4721. }
  4722. }
  4723. DXASSERT_ARGS(false, "Invalid ShaderOp name: %s", str);
  4724. return ShaderOpKind::ShaderOpInvalid;
  4725. }
  4726. template <typename InType, typename OutType, ShaderOpKind kind>
  4727. struct computeExpected {
  4728. OutType operator()(const std::vector<InType> &inputs,
  4729. const std::vector<int> &masks, int maskValue,
  4730. unsigned int index) {
  4731. return 0;
  4732. }
  4733. };
  4734. template <typename InType, typename OutType>
  4735. struct computeExpected<InType, OutType, ShaderOpKind::WaveSum> {
  4736. OutType operator()(const std::vector<InType> &inputs,
  4737. const std::vector<int> &masks, int maskValue,
  4738. unsigned int index) {
  4739. OutType sum = 0;
  4740. for (size_t i = 0; i < index; ++i) {
  4741. if (masks.at(i) == maskValue) {
  4742. sum += inputs.at(i);
  4743. }
  4744. }
  4745. return sum;
  4746. }
  4747. };
  4748. template <typename InType, typename OutType>
  4749. struct computeExpected<InType, OutType, ShaderOpKind::WaveProduct> {
  4750. OutType operator()(const std::vector<InType> &inputs,
  4751. const std::vector<int> &masks, int maskValue,
  4752. unsigned int index) {
  4753. OutType prod = 1;
  4754. for (size_t i = 0; i < index; ++i) {
  4755. if (masks.at(i) == maskValue) {
  4756. prod *= inputs.at(i);
  4757. }
  4758. }
  4759. return prod;
  4760. }
  4761. };
  4762. template <typename InType, typename OutType>
  4763. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveMax> {
  4764. OutType operator()(const std::vector<InType> &inputs,
  4765. const std::vector<int> &masks, int maskValue,
  4766. unsigned int index) {
  4767. OutType maximum = std::numeric_limits<OutType>::min();
  4768. for (size_t i = 0; i < index; ++i) {
  4769. if (masks.at(i) == maskValue && inputs.at(i) > maximum)
  4770. maximum = inputs.at(i);
  4771. }
  4772. return maximum;
  4773. }
  4774. };
  4775. template <typename InType, typename OutType>
  4776. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveMin> {
  4777. OutType operator()(const std::vector<InType> &inputs,
  4778. const std::vector<int> &masks, int maskValue,
  4779. unsigned int index) {
  4780. OutType minimum = std::numeric_limits<OutType>::max();
  4781. for (size_t i = 0; i < index; ++i) {
  4782. if (masks.at(i) == maskValue && inputs.at(i) < minimum)
  4783. minimum = inputs.at(i);
  4784. }
  4785. return minimum;
  4786. }
  4787. };
  4788. template <typename InType, typename OutType>
  4789. struct computeExpected<InType, OutType, ShaderOpKind::WaveCountBits> {
  4790. OutType operator()(const std::vector<InType> &inputs,
  4791. const std::vector<int> &masks, int maskValue,
  4792. unsigned int index) {
  4793. OutType count = 0;
  4794. for (size_t i = 0; i < index; ++i) {
  4795. if (masks.at(i) == maskValue && inputs.at(i) > 3) {
  4796. count++;
  4797. }
  4798. }
  4799. return count;
  4800. }
  4801. };
  4802. // In HLSL, boolean is represented in a 4 byte (uint32) format,
  4803. // So we cannot use c++ bool type to represent bool in HLSL
  4804. // HLSL returns 0 for false and 1 for true
  4805. template <typename InType, typename OutType>
  4806. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveAnyTrue> {
  4807. OutType operator()(const std::vector<InType> &inputs,
  4808. const std::vector<int> &masks, int maskValue,
  4809. unsigned int index) {
  4810. for (size_t i = 0; i < index; ++i) {
  4811. if (masks.at(i) == maskValue && inputs.at(i) != 0) {
  4812. return 1;
  4813. }
  4814. }
  4815. return 0;
  4816. }
  4817. };
  4818. template <typename InType, typename OutType>
  4819. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveAllTrue> {
  4820. OutType operator()(const std::vector<InType> &inputs,
  4821. const std::vector<int> &masks, int maskValue,
  4822. unsigned int index) {
  4823. for (size_t i = 0; i < index; ++i) {
  4824. if (masks.at(i) == maskValue && inputs.at(i) == 0) {
  4825. return 0;
  4826. }
  4827. }
  4828. return 1;
  4829. }
  4830. };
  4831. template <typename InType, typename OutType>
  4832. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveAllEqual> {
  4833. OutType operator()(const std::vector<InType> &inputs,
  4834. const std::vector<int> &masks, int maskValue,
  4835. unsigned int index) {
  4836. const InType *val = nullptr;
  4837. for (size_t i = 0; i < index; ++i) {
  4838. if (masks.at(i) == maskValue) {
  4839. if (val && *val != inputs.at(i)) {
  4840. return 0;
  4841. }
  4842. val = &inputs.at(i);
  4843. }
  4844. }
  4845. return 1;
  4846. }
  4847. };
  4848. template <typename InType, typename OutType>
  4849. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitOr> {
  4850. OutType operator()(const std::vector<InType> &inputs,
  4851. const std::vector<int> &masks, int maskValue,
  4852. unsigned int index) {
  4853. OutType bits = 0x00000000;
  4854. for (size_t i = 0; i < index; ++i) {
  4855. if (masks.at(i) == maskValue) {
  4856. bits |= inputs.at(i);
  4857. }
  4858. }
  4859. return bits;
  4860. }
  4861. };
  4862. template <typename InType, typename OutType>
  4863. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitAnd> {
  4864. OutType operator()(const std::vector<InType> &inputs,
  4865. const std::vector<int> &masks, int maskValue,
  4866. unsigned int index) {
  4867. OutType bits = 0xffffffff;
  4868. for (size_t i = 0; i < index; ++i) {
  4869. if (masks.at(i) == maskValue) {
  4870. bits &= inputs.at(i);
  4871. }
  4872. }
  4873. return bits;
  4874. }
  4875. };
  4876. template <typename InType, typename OutType>
  4877. struct computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitXor> {
  4878. OutType operator()(const std::vector<InType> &inputs,
  4879. const std::vector<int> &masks, int maskValue,
  4880. unsigned int index) {
  4881. OutType bits = 0x00000000;
  4882. for (size_t i = 0; i < index; ++i) {
  4883. if (masks.at(i) == maskValue) {
  4884. bits ^= inputs.at(i);
  4885. }
  4886. }
  4887. return bits;
  4888. }
  4889. };
  4890. // Mask functions used to control active lanes
  4891. static int MaskAll(int i) {
  4892. UNREFERENCED_PARAMETER(i);
  4893. return 1;
  4894. }
  4895. static int MaskEveryOther(int i) {
  4896. return i % 2 == 0 ? 1 : 0;
  4897. }
  4898. static int MaskEveryThird(int i) {
  4899. return i % 3 == 0 ? 1 : 0;
  4900. }
  4901. typedef int(*MaskFunction)(int);
  4902. static MaskFunction MaskFunctionTable[] = {
  4903. MaskAll, MaskEveryOther, MaskEveryThird
  4904. };
  4905. template <typename InType, typename OutType>
  4906. static OutType computeExpectedWithShaderOp(const std::vector<InType> &inputs,
  4907. const std::vector<int> &masks,
  4908. int maskValue, unsigned int index,
  4909. LPCWSTR str) {
  4910. ShaderOpKind kind = GetShaderOpKind(str);
  4911. switch (kind) {
  4912. case ShaderOpKind::WaveSum:
  4913. return computeExpected<InType, OutType, ShaderOpKind::WaveSum>()(inputs, masks, maskValue, index);
  4914. case ShaderOpKind::WaveProduct:
  4915. return computeExpected<InType, OutType, ShaderOpKind::WaveProduct>()(inputs, masks, maskValue, index);
  4916. case ShaderOpKind::WaveActiveMax:
  4917. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveMax>()(inputs, masks, maskValue, index);
  4918. case ShaderOpKind::WaveActiveMin:
  4919. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveMin>()(inputs, masks, maskValue, index);
  4920. case ShaderOpKind::WaveCountBits:
  4921. return computeExpected<InType, OutType, ShaderOpKind::WaveCountBits>()(inputs, masks, maskValue, index);
  4922. case ShaderOpKind::WaveActiveBitOr:
  4923. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitOr>()(inputs, masks, maskValue, index);
  4924. case ShaderOpKind::WaveActiveBitAnd:
  4925. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitAnd>()(inputs, masks, maskValue, index);
  4926. case ShaderOpKind::WaveActiveBitXor:
  4927. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveBitXor>()(inputs, masks, maskValue, index);
  4928. case ShaderOpKind::WaveActiveAnyTrue:
  4929. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveAnyTrue>()(inputs, masks, maskValue, index);
  4930. case ShaderOpKind::WaveActiveAllTrue:
  4931. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveAllTrue>()(inputs, masks, maskValue, index);
  4932. case ShaderOpKind::WaveActiveAllEqual:
  4933. return computeExpected<InType, OutType, ShaderOpKind::WaveActiveAllEqual>()(inputs, masks, maskValue, index);
  4934. default:
  4935. DXASSERT_ARGS(false, "Invalid ShaderOp Name: %s", str);
  4936. return (OutType) 0;
  4937. }
  4938. };
  4939. // A framework for testing individual wave intrinsics tests.
  4940. // This test case is assuming that functions 1) WaveIsFirstLane and 2) WaveGetLaneIndex are correct for all lanes.
  4941. template <class T1, class T2>
  4942. void ExecutionTest::WaveIntrinsicsActivePrefixTest(
  4943. TableParameter *pParameterList, size_t numParameter, bool isPrefix) {
  4944. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  4945. // Resource representation for compute shader
  4946. // firstLaneId is used to group different waves
  4947. // laneIndex is used to identify lane within the wave.
  4948. // Lane ids are not necessarily in same order as thread ids.
  4949. struct PerThreadData {
  4950. unsigned firstLaneId;
  4951. unsigned laneIndex;
  4952. int mask;
  4953. T1 input;
  4954. T2 output;
  4955. };
  4956. unsigned int NumThreadsX = 8;
  4957. unsigned int NumThreadsY = 12;
  4958. unsigned int NumThreadsZ = 1;
  4959. static const unsigned int ThreadsPerGroup = NumThreadsX * NumThreadsY * NumThreadsZ;
  4960. static const unsigned int DispatchGroupCount = 1;
  4961. static const unsigned int ThreadCount = ThreadsPerGroup * DispatchGroupCount;
  4962. CComPtr<IStream> pStream;
  4963. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  4964. CComPtr<ID3D12Device> pDevice;
  4965. if (!CreateDevice(&pDevice)) {
  4966. return;
  4967. }
  4968. if (!DoesDeviceSupportWaveOps(pDevice)) {
  4969. // Optional feature, so it's correct to not support it if declared as such.
  4970. WEX::Logging::Log::Comment(L"Device does not support wave operations.");
  4971. return;
  4972. }
  4973. TableParameterHandler handler(pParameterList, numParameter);
  4974. unsigned int numInputSet = handler.GetTableParamByName(L"Validation.NumInputSet")->m_uint;
  4975. // Obtain the list of input lists
  4976. std::vector<std::vector<T1>*> InputDataList;
  4977. for (unsigned int i = 0;
  4978. i < numInputSet; ++i) {
  4979. std::wstring inputName = L"Validation.InputSet";
  4980. inputName.append(std::to_wstring(i + 1));
  4981. InputDataList.push_back(handler.GetDataArray<T1>(inputName.data()));
  4982. }
  4983. CW2A Text(handler.GetTableParamByName(L"ShaderOp.text")->m_str);
  4984. std::shared_ptr<st::ShaderOpSet> ShaderOpSet = std::make_shared<st::ShaderOpSet>();
  4985. st::ParseShaderOpSetFromStream(pStream, ShaderOpSet.get());
  4986. // Running compute shader for each input set with different masks
  4987. for (size_t setIndex = 0; setIndex < numInputSet; ++setIndex) {
  4988. for (size_t maskIndex = 0; maskIndex < sizeof(MaskFunctionTable) / sizeof(MaskFunction); ++maskIndex) {
  4989. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTestAfterParse(
  4990. pDevice, m_support, "WaveIntrinsicsOp",
  4991. // this callbacked is called when the test
  4992. // is creating the resource to run the test
  4993. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  4994. VERIFY_IS_TRUE(0 == _stricmp(Name, "SWaveIntrinsicsOp"));
  4995. size_t size = sizeof(PerThreadData) * ThreadCount;
  4996. Data.resize(size);
  4997. PerThreadData *pPrimitives = (PerThreadData*)Data.data();
  4998. // 4 different inputs for each operation test
  4999. size_t index = 0;
  5000. std::vector<T1> *IntList = InputDataList[setIndex];
  5001. while (index < ThreadCount) {
  5002. PerThreadData *p = &pPrimitives[index];
  5003. p->firstLaneId = 0xFFFFBFFF;
  5004. p->laneIndex = 0xFFFFBFFF;
  5005. p->mask = MaskFunctionTable[maskIndex]((int)index);
  5006. p->input = (*IntList)[index % IntList->size()];
  5007. p->output = 0xFFFFBFFF;
  5008. index++;
  5009. }
  5010. // use shader from data table
  5011. pShaderOp->Shaders.at(0).Text = Text.m_psz;
  5012. }, ShaderOpSet);
  5013. // Check the value
  5014. MappedData data;
  5015. test->Test->GetReadBackData("SWaveIntrinsicsOp", &data);
  5016. PerThreadData *pPrimitives = (PerThreadData*)data.data();
  5017. WEX::TestExecution::DisableVerifyExceptions dve;
  5018. // Grouping data by waves
  5019. std::vector<int> firstLaneIds;
  5020. for (size_t i = 0; i < ThreadCount; ++i) {
  5021. PerThreadData *p = &pPrimitives[i];
  5022. int firstLaneId = p->firstLaneId;
  5023. if (!contains(firstLaneIds, firstLaneId)) {
  5024. firstLaneIds.push_back(firstLaneId);
  5025. }
  5026. }
  5027. std::map<int, std::unique_ptr<std::vector<PerThreadData *>>> waves;
  5028. for (size_t i = 0; i < firstLaneIds.size(); ++i) {
  5029. waves[firstLaneIds.at(i)] = std::make_unique<std::vector<PerThreadData*>>();
  5030. }
  5031. for (size_t i = 0; i < ThreadCount; ++i) {
  5032. PerThreadData *p = &pPrimitives[i];
  5033. waves[p->firstLaneId].get()->push_back(p);
  5034. }
  5035. // validate for each wave
  5036. for (size_t i = 0; i < firstLaneIds.size(); ++i) {
  5037. // collect inputs and masks for a given wave
  5038. std::vector<PerThreadData *> *waveData = waves[firstLaneIds.at(i)].get();
  5039. std::vector<T1> inputList(waveData->size());
  5040. std::vector<int> maskList(waveData->size(), -1);
  5041. std::vector<T2> outputList(waveData->size());
  5042. // sort inputList and masklist by lane id. input for each lane can be computed for its group index
  5043. for (size_t j = 0, end = waveData->size(); j < end; ++j) {
  5044. unsigned laneID = waveData->at(j)->laneIndex;
  5045. // ensure that each lane ID is unique and within the range
  5046. VERIFY_IS_TRUE(0 <= laneID && laneID < waveData->size());
  5047. VERIFY_IS_TRUE(maskList.at(laneID) == -1);
  5048. maskList.at(laneID) = waveData->at(j)->mask;
  5049. inputList.at(laneID) = waveData->at(j)->input;
  5050. outputList.at(laneID) = waveData->at(j)->output;
  5051. }
  5052. std::wstring inputStr = L"Wave Inputs: ";
  5053. std::wstring maskStr = L"Wave Masks: ";
  5054. std::wstring outputStr = L"Wave Outputs: ";
  5055. // append input string and mask string in lane id order
  5056. for (size_t j = 0, end = waveData->size(); j < end; ++j) {
  5057. maskStr.append(std::to_wstring(maskList.at(j)));
  5058. maskStr.append(L" ");
  5059. inputStr.append(std::to_wstring(inputList.at(j)));
  5060. inputStr.append(L" ");
  5061. outputStr.append(std::to_wstring(outputList.at(j)));
  5062. outputStr.append(L" ");
  5063. }
  5064. LogCommentFmt(inputStr.data());
  5065. LogCommentFmt(maskStr.data());
  5066. LogCommentFmt(outputStr.data());
  5067. LogCommentFmt(L"\n");
  5068. // Compute expected output for a given inputs, masks, and index
  5069. for (size_t laneIndex = 0, laneEnd = inputList.size(); laneIndex < laneEnd; ++laneIndex) {
  5070. T2 expected;
  5071. // WaveActive is equivalent to WavePrefix lane # lane count
  5072. unsigned index = isPrefix ? (unsigned)laneIndex : (unsigned)inputList.size();
  5073. if (maskList.at(laneIndex) == 1) {
  5074. expected = computeExpectedWithShaderOp<T1, T2>(
  5075. inputList, maskList, 1, index,
  5076. handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  5077. }
  5078. else {
  5079. expected = computeExpectedWithShaderOp<T1, T2>(
  5080. inputList, maskList, 0, index,
  5081. handler.GetTableParamByName(L"ShaderOp.Name")->m_str);
  5082. }
  5083. // TODO: use different comparison for floating point inputs
  5084. bool equal = outputList.at(laneIndex) == expected;
  5085. if (!equal) {
  5086. LogCommentFmt(L"lane%d: %4d, Expected : %4d", laneIndex, outputList.at(laneIndex), expected);
  5087. }
  5088. VERIFY_IS_TRUE(equal);
  5089. }
  5090. }
  5091. }
  5092. }
  5093. }
  5094. static const unsigned int MinWarpVersionForWaveIntrinsics = 16202;
  5095. TEST_F(ExecutionTest, WaveIntrinsicsActiveIntTest) {
  5096. if (GetTestParamUseWARP(true) &&
  5097. !IsValidWarpDllVersion(MinWarpVersionForWaveIntrinsics)) {
  5098. return;
  5099. }
  5100. WaveIntrinsicsActivePrefixTest<int, int>(
  5101. WaveIntrinsicsActiveIntParameters,
  5102. sizeof(WaveIntrinsicsActiveIntParameters) / sizeof(TableParameter),
  5103. /*isPrefix*/ false);
  5104. }
  5105. TEST_F(ExecutionTest, WaveIntrinsicsActiveUintTest) {
  5106. if (GetTestParamUseWARP(true) &&
  5107. !IsValidWarpDllVersion(MinWarpVersionForWaveIntrinsics)) {
  5108. return;
  5109. }
  5110. WaveIntrinsicsActivePrefixTest<unsigned int, unsigned int>(
  5111. WaveIntrinsicsActiveUintParameters,
  5112. sizeof(WaveIntrinsicsActiveUintParameters) / sizeof(TableParameter),
  5113. /*isPrefix*/ false);
  5114. }
  5115. TEST_F(ExecutionTest, WaveIntrinsicsPrefixIntTest) {
  5116. if (GetTestParamUseWARP(true) &&
  5117. !IsValidWarpDllVersion(MinWarpVersionForWaveIntrinsics)) {
  5118. return;
  5119. }
  5120. WaveIntrinsicsActivePrefixTest<int, int>(
  5121. WaveIntrinsicsPrefixIntParameters,
  5122. sizeof(WaveIntrinsicsPrefixIntParameters) / sizeof(TableParameter),
  5123. /*isPrefix*/ true);
  5124. }
  5125. TEST_F(ExecutionTest, WaveIntrinsicsPrefixUintTest) {
  5126. if (GetTestParamUseWARP(true) &&
  5127. !IsValidWarpDllVersion(MinWarpVersionForWaveIntrinsics)) {
  5128. return;
  5129. }
  5130. WaveIntrinsicsActivePrefixTest<unsigned int, unsigned int>(
  5131. WaveIntrinsicsPrefixUintParameters,
  5132. sizeof(WaveIntrinsicsPrefixUintParameters) / sizeof(TableParameter),
  5133. /*isPrefix*/ true);
  5134. }
  5135. TEST_F(ExecutionTest, CBufferTestHalf) {
  5136. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  5137. CComPtr<IStream> pStream;
  5138. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  5139. // Single operation test at the moment.
  5140. CComPtr<ID3D12Device> pDevice;
  5141. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL_6_2))
  5142. return;
  5143. uint16_t InputData[] = { 0x3F80, 0x3F00, 0x3D80, 0x7BFF };
  5144. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(pDevice, m_support, pStream, "CBufferTestHalf",
  5145. [&](LPCSTR Name, std::vector<BYTE> &Data, st::ShaderOp *pShaderOp) {
  5146. UNREFERENCED_PARAMETER(pShaderOp);
  5147. VERIFY_IS_TRUE(0 == _stricmp(Name, "CB0"));
  5148. // use shader from data table.
  5149. Data.resize(sizeof(InputData));
  5150. uint16_t *pData = (uint16_t *)Data.data();
  5151. for (size_t i = 0; i < 4; ++i, ++pData) {
  5152. *pData = InputData[i];
  5153. }
  5154. });
  5155. {
  5156. MappedData data;
  5157. test->Test->GetReadBackData("RTarget", &data);
  5158. const uint16_t *pPixels = (uint16_t *)data.data();
  5159. for (int i = 0; i < 4; ++i) {
  5160. uint16_t output = *(pPixels + i);
  5161. float outputFloat = ConvertFloat16ToFloat32(output);
  5162. float inputFloat = ConvertFloat16ToFloat32(InputData[i]);
  5163. LogCommentFmt(L"element #%u: input = %6.8f(0x%04x), output = %6.8f(0x%04x)",
  5164. i, inputFloat, InputData[i], outputFloat, output);
  5165. VERIFY_ARE_EQUAL(inputFloat, outputFloat);
  5166. }
  5167. }
  5168. }
  5169. TEST_F(ExecutionTest, BarycentricsTest) {
  5170. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  5171. CComPtr<IStream> pStream;
  5172. ReadHlslDataIntoNewStream(L"ShaderOpArith.xml", &pStream);
  5173. CComPtr<ID3D12Device> pDevice;
  5174. if (!CreateDevice(&pDevice, D3D_SHADER_MODEL_6_1))
  5175. return;
  5176. // TODO: check feature support for different tier. This test does not assume vertex ordering and only tests SV_Barycentrics yet
  5177. // Split this test into 2 for different tiers.
  5178. // Tier 1: Index 0-2 for GetAttributeAtVertex returns values for three different vertices
  5179. // Tier 2: Match the provoking vertex (index 0 is a provoking vertex)
  5180. std::shared_ptr<ShaderOpTestResult> test = RunShaderOpTest(pDevice, m_support, pStream, "Barycentrics", nullptr);
  5181. MappedData data;
  5182. D3D12_RESOURCE_DESC &D = test->ShaderOp->GetResourceByName("RTarget")->Desc;
  5183. UINT width = (UINT64)D.Width;
  5184. UINT height = (UINT64)D.Height;
  5185. UINT pixelSize = GetByteSizeForFormat(D.Format);
  5186. test->Test->GetReadBackData("RTarget", &data);
  5187. //const uint8_t *pPixels = (uint8_t *)data.data();
  5188. const float *pPixels = (float *)data.data();
  5189. // Get the vertex of barycentric coordinate using VBuffer
  5190. MappedData triangleData;
  5191. test->Test->GetReadBackData("VBuffer", &triangleData);
  5192. const float *pTriangleData = (float*)triangleData.data();
  5193. // get the size of the input data
  5194. unsigned triangleVertexSizeInFloat = 0;
  5195. for (auto element : test->ShaderOp->InputElements)
  5196. triangleVertexSizeInFloat += GetByteSizeForFormat(element.Format) / 4;
  5197. XMFLOAT2 p0(pTriangleData[0], pTriangleData[1]);
  5198. XMFLOAT2 p1(pTriangleData[triangleVertexSizeInFloat], pTriangleData[triangleVertexSizeInFloat + 1]);
  5199. XMFLOAT2 p2(pTriangleData[triangleVertexSizeInFloat * 2], pTriangleData[triangleVertexSizeInFloat * 2 + 1]);
  5200. XMFLOAT3 barycentricWeights[4] = {
  5201. XMFLOAT3(0.3333f, 0.3333f, 0.3333f),
  5202. XMFLOAT3(0.5f, 0.25f, 0.25f),
  5203. XMFLOAT3(0.25f, 0.5f, 0.25f),
  5204. XMFLOAT3(0.25f, 0.25f, 0.50f)
  5205. };
  5206. float tolerance = 0.001f;
  5207. for (unsigned i = 0; i < sizeof(barycentricWeights) / sizeof(XMFLOAT3); ++i) {
  5208. float w0 = barycentricWeights[i].x;
  5209. float w1 = barycentricWeights[i].y;
  5210. float w2 = barycentricWeights[i].z;
  5211. float x1 = w0 * p0.x + w1 * p1.x + w2 * p2.x;
  5212. float y1 = w0 * p0.y + w1 * p1.y + w2 * p2.y;
  5213. // map from x1 y1 to rtv pixels
  5214. int pixelX = (x1 + 1) * (width - 1) / 2;
  5215. int pixelY = (1 - y1) * (height - 1) / 2;
  5216. int offset = pixelSize * (pixelX + pixelY * width) / sizeof(pPixels[0]);
  5217. LogCommentFmt(L"location %u %u, value %f, %f, %f", pixelX, pixelY, pPixels[offset], pPixels[offset + 1], pPixels[offset + 2]);
  5218. VERIFY_IS_TRUE(CompareFloatEpsilon(pPixels[offset], w0, tolerance));
  5219. VERIFY_IS_TRUE(CompareFloatEpsilon(pPixels[offset + 1], w1, tolerance));
  5220. VERIFY_IS_TRUE(CompareFloatEpsilon(pPixels[offset + 2], w2, tolerance));
  5221. }
  5222. //SavePixelsToFile(pPixels, DXGI_FORMAT_R32G32B32A32_FLOAT, width, height, L"barycentric.bmp");
  5223. }
  5224. #ifndef _HLK_CONF
  5225. static void WriteReadBackDump(st::ShaderOp *pShaderOp, st::ShaderOpTest *pTest,
  5226. char **pReadBackDump) {
  5227. std::stringstream str;
  5228. unsigned count = 0;
  5229. for (auto &R : pShaderOp->Resources) {
  5230. if (!R.ReadBack)
  5231. continue;
  5232. ++count;
  5233. str << "Resource: " << R.Name << "\r\n";
  5234. // Find a descriptor that can tell us how to dump this resource.
  5235. bool found = false;
  5236. for (auto &Heaps : pShaderOp->DescriptorHeaps) {
  5237. for (auto &D : Heaps.Descriptors) {
  5238. if (_stricmp(D.ResName, R.Name) != 0) {
  5239. continue;
  5240. }
  5241. found = true;
  5242. if (_stricmp(D.Kind, "UAV") != 0) {
  5243. str << "Resource dump for kind " << D.Kind << " not implemented yet.\r\n";
  5244. break;
  5245. }
  5246. if (D.UavDesc.ViewDimension != D3D12_UAV_DIMENSION_BUFFER) {
  5247. str << "Resource dump for this kind of view dimension not implemented yet.\r\n";
  5248. break;
  5249. }
  5250. // We can map back to the structure if a structured buffer via the shader, but
  5251. // we'll keep this simple and simply dump out 32-bit uint/float representations.
  5252. MappedData data;
  5253. pTest->GetReadBackData(R.Name, &data);
  5254. uint32_t *pData = (uint32_t *)data.data();
  5255. size_t u32_count = R.Desc.Width / sizeof(uint32_t);
  5256. for (size_t i = 0; i < u32_count; ++i) {
  5257. float f = *(float *)pData;
  5258. str << i << ": 0n" << *pData << " 0x" << std::hex << *pData
  5259. << std::dec << " " << f << "\r\n";
  5260. ++pData;
  5261. }
  5262. break;
  5263. }
  5264. if (found) break;
  5265. }
  5266. if (!found) {
  5267. str << "Unable to find a view for the resource.\r\n";
  5268. }
  5269. }
  5270. str << "Resources read back: " << count << "\r\n";
  5271. std::string s(str.str());
  5272. CComHeapPtr<char> pDump;
  5273. if (!pDump.Allocate(s.size() + 1))
  5274. throw std::bad_alloc();
  5275. memcpy(pDump.m_pData, s.data(), s.size());
  5276. pDump.m_pData[s.size()] = '\0';
  5277. *pReadBackDump = pDump.Detach();
  5278. }
  5279. // This is the exported interface by use from HLSLHost.exe.
  5280. // It's exclusive with the use of the DLL as a TAEF target.
  5281. extern "C" {
  5282. __declspec(dllexport) HRESULT WINAPI InitializeOpTests(void *pStrCtx, st::OutputStringFn pOutputStrFn) {
  5283. HRESULT hr = EnableExperimentalShaderModels();
  5284. if (FAILED(hr)) {
  5285. pOutputStrFn(pStrCtx, L"Unable to enable experimental shader models.\r\n.");
  5286. }
  5287. return S_OK;
  5288. }
  5289. __declspec(dllexport) HRESULT WINAPI
  5290. RunOpTest(void *pStrCtx, st::OutputStringFn pOutputStrFn, LPCSTR pText,
  5291. ID3D12Device *pDevice, ID3D12CommandQueue *pCommandQueue,
  5292. ID3D12Resource *pRenderTarget, char **pReadBackDump) {
  5293. HRESULT hr;
  5294. if (pReadBackDump) *pReadBackDump = nullptr;
  5295. st::SetOutputFn(pStrCtx, pOutputStrFn);
  5296. CComPtr<ID3D12InfoQueue> pInfoQueue;
  5297. CComHeapPtr<char> pDump;
  5298. bool FilterCreation = false;
  5299. if (SUCCEEDED(pDevice->QueryInterface(&pInfoQueue))) {
  5300. // Creation is largely driven by inputs, so don't log create/destroy messages.
  5301. pInfoQueue->PushEmptyStorageFilter();
  5302. pInfoQueue->PushEmptyRetrievalFilter();
  5303. if (FilterCreation) {
  5304. D3D12_INFO_QUEUE_FILTER filter;
  5305. D3D12_MESSAGE_CATEGORY denyCategories[] = { D3D12_MESSAGE_CATEGORY_STATE_CREATION };
  5306. ZeroMemory(&filter, sizeof(filter));
  5307. filter.DenyList.NumCategories = _countof(denyCategories);
  5308. filter.DenyList.pCategoryList = denyCategories;
  5309. pInfoQueue->PushStorageFilter(&filter);
  5310. }
  5311. }
  5312. else {
  5313. pOutputStrFn(pStrCtx, L"Unable to enable info queue for D3D.\r\n.");
  5314. }
  5315. try {
  5316. dxc::DxcDllSupport m_support;
  5317. m_support.Initialize();
  5318. const char *pName = nullptr;
  5319. CComPtr<IStream> pStream = SHCreateMemStream((BYTE *)pText, strlen(pText));
  5320. std::shared_ptr<st::ShaderOpSet> ShaderOpSet =
  5321. std::make_shared<st::ShaderOpSet>();
  5322. st::ParseShaderOpSetFromStream(pStream, ShaderOpSet.get());
  5323. st::ShaderOp *pShaderOp;
  5324. if (pName == nullptr) {
  5325. if (ShaderOpSet->ShaderOps.size() != 1) {
  5326. pOutputStrFn(pStrCtx, L"Expected a single shader operation.\r\n");
  5327. return E_FAIL;
  5328. }
  5329. pShaderOp = ShaderOpSet->ShaderOps[0].get();
  5330. }
  5331. else {
  5332. pShaderOp = ShaderOpSet->GetShaderOp(pName);
  5333. }
  5334. if (pShaderOp == nullptr) {
  5335. std::string msg = "Unable to find shader op ";
  5336. msg += pName;
  5337. msg += "; available ops";
  5338. const char sep = ':';
  5339. for (auto &pAvailOp : ShaderOpSet->ShaderOps) {
  5340. msg += sep;
  5341. msg += pAvailOp->Name ? pAvailOp->Name : "[n/a]";
  5342. }
  5343. CA2W msgWide(msg.c_str());
  5344. pOutputStrFn(pStrCtx, msgWide);
  5345. return E_FAIL;
  5346. }
  5347. std::shared_ptr<st::ShaderOpTest> test = std::make_shared<st::ShaderOpTest>();
  5348. test->SetupRenderTarget(pShaderOp, pDevice, pCommandQueue, pRenderTarget);
  5349. test->SetDxcSupport(&m_support);
  5350. test->RunShaderOp(pShaderOp);
  5351. test->PresentRenderTarget(pShaderOp, pCommandQueue, pRenderTarget);
  5352. pOutputStrFn(pStrCtx, L"Rendering complete.\r\n");
  5353. if (!pShaderOp->IsCompute()) {
  5354. D3D12_QUERY_DATA_PIPELINE_STATISTICS stats;
  5355. test->GetPipelineStats(&stats);
  5356. wchar_t statsText[400];
  5357. StringCchPrintfW(statsText, _countof(statsText),
  5358. L"Vertices/primitives read by input assembler: %I64u/%I64u\r\n"
  5359. L"Vertex shader invocations: %I64u\r\n"
  5360. L"Geometry shader invocations/output primitive: %I64u/%I64u\r\n"
  5361. L"Primitives sent to rasterizer/rendered: %I64u/%I64u\r\n"
  5362. L"PS/HS/DS/CS invocations: %I64u/%I64u/%I64u/%I64u\r\n",
  5363. stats.IAVertices, stats.IAPrimitives, stats.VSInvocations,
  5364. stats.GSInvocations, stats.GSPrimitives, stats.CInvocations,
  5365. stats.CPrimitives, stats.PSInvocations, stats.HSInvocations,
  5366. stats.DSInvocations, stats.CSInvocations);
  5367. pOutputStrFn(pStrCtx, statsText);
  5368. }
  5369. if (pReadBackDump) {
  5370. WriteReadBackDump(pShaderOp, test.get(), &pDump);
  5371. }
  5372. hr = S_OK;
  5373. }
  5374. catch (const CAtlException &E)
  5375. {
  5376. hr = E.m_hr;
  5377. }
  5378. catch (const std::bad_alloc &)
  5379. {
  5380. hr = E_OUTOFMEMORY;
  5381. }
  5382. catch (const std::exception &)
  5383. {
  5384. hr = E_FAIL;
  5385. }
  5386. // Drain the device message queue if available.
  5387. if (pInfoQueue != nullptr) {
  5388. wchar_t buf[200];
  5389. StringCchPrintfW(buf, _countof(buf),
  5390. L"NumStoredMessages=%u limit/discarded by limit=%u/%u "
  5391. L"allowed/denied by storage filter=%u/%u "
  5392. L"NumStoredMessagesAllowedByRetrievalFilter=%u\r\n",
  5393. (unsigned)pInfoQueue->GetNumStoredMessages(),
  5394. (unsigned)pInfoQueue->GetMessageCountLimit(),
  5395. (unsigned)pInfoQueue->GetNumMessagesDiscardedByMessageCountLimit(),
  5396. (unsigned)pInfoQueue->GetNumMessagesAllowedByStorageFilter(),
  5397. (unsigned)pInfoQueue->GetNumMessagesDeniedByStorageFilter(),
  5398. (unsigned)pInfoQueue->GetNumStoredMessagesAllowedByRetrievalFilter());
  5399. pOutputStrFn(pStrCtx, buf);
  5400. WriteInfoQueueMessages(pStrCtx, pOutputStrFn, pInfoQueue);
  5401. pInfoQueue->ClearStoredMessages();
  5402. pInfoQueue->PopRetrievalFilter();
  5403. pInfoQueue->PopStorageFilter();
  5404. if (FilterCreation) {
  5405. pInfoQueue->PopStorageFilter();
  5406. }
  5407. }
  5408. if (pReadBackDump) *pReadBackDump = pDump.Detach();
  5409. return hr;
  5410. }
  5411. }
  5412. #endif
  5413. // MARKER: ExecutionTest/DxilConf Shared Implementation End
  5414. // Do not remove the line above - it is used by TranslateExecutionTest.py