SDL_render_d3d12.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #include "SDL_render.h"
  20. #include "SDL_system.h"
  21. #if SDL_VIDEO_RENDER_D3D12 && !SDL_RENDER_DISABLED
  22. #define SDL_D3D12_NUM_BUFFERS 2
  23. #define SDL_D3D12_NUM_VERTEX_BUFFERS 256
  24. #define SDL_D3D12_MAX_NUM_TEXTURES 16384
  25. #define SDL_D3D12_NUM_UPLOAD_BUFFERS 32
  26. #include "../../core/windows/SDL_windows.h"
  27. #include "../../video/windows/SDL_windowswindow.h"
  28. #include "SDL_hints.h"
  29. #include "SDL_loadso.h"
  30. #include "../SDL_sysrender.h"
  31. #include "../SDL_d3dmath.h"
  32. #define SDL_ENABLE_SYSWM_WINDOWS
  33. #include "SDL_syswm.h"
  34. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  35. #include "SDL_render_d3d12_xbox.h"
  36. #ifndef D3D12_TEXTURE_DATA_PITCH_ALIGNMENT
  37. #define D3D12_TEXTURE_DATA_PITCH_ALIGNMENT 256
  38. #endif
  39. #else
  40. #include <d3d12.h>
  41. #include <dxgi1_6.h>
  42. #include <dxgidebug.h>
  43. #include <d3d12sdklayers.h>
  44. #endif
  45. #include "SDL_shaders_d3d12.h"
  46. #if defined(_MSC_VER) && !defined(__clang__)
  47. #define SDL_COMPOSE_ERROR(str) __FUNCTION__ ", " str
  48. #else
  49. #define SDL_COMPOSE_ERROR(str) SDL_STRINGIFY_ARG(__FUNCTION__) ", " str
  50. #endif
  51. #ifdef __cplusplus
  52. #define SAFE_RELEASE(X) if (X) { (X)->Release(); X = NULL; }
  53. #define D3D_CALL(THIS, FUNC, ...) (THIS)->FUNC(__VA_ARGS__)
  54. #define D3D_CALL_RET(THIS, FUNC, RETVAL, ...) *(RETVAL) = (THIS)->FUNC(__VA_ARGS__)
  55. #define D3D_GUID(X) (X)
  56. #else
  57. #define SAFE_RELEASE(X) if (X) { (X)->lpVtbl->Release(X); X = NULL; }
  58. #define D3D_CALL(THIS, FUNC, ...) (THIS)->lpVtbl->FUNC((THIS), ##__VA_ARGS__)
  59. #define D3D_CALL_RET(THIS, FUNC, ...) (THIS)->lpVtbl->FUNC((THIS), ##__VA_ARGS__)
  60. #define D3D_GUID(X) &(X)
  61. #endif
  62. /* Set up for C function definitions, even when using C++ */
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. /* !!! FIXME: vertex buffer bandwidth could be lower; only use UV coords when
  67. !!! FIXME: textures are needed. */
  68. /* Vertex shader, common values */
  69. typedef struct
  70. {
  71. Float4X4 model;
  72. Float4X4 projectionAndView;
  73. } VertexShaderConstants;
  74. /* Per-vertex data */
  75. typedef struct
  76. {
  77. Float2 pos;
  78. Float2 tex;
  79. SDL_Color color;
  80. } VertexPositionColor;
  81. /* Per-texture data */
  82. typedef struct
  83. {
  84. ID3D12Resource *mainTexture;
  85. D3D12_CPU_DESCRIPTOR_HANDLE mainTextureResourceView;
  86. D3D12_RESOURCE_STATES mainResourceState;
  87. SIZE_T mainSRVIndex;
  88. D3D12_CPU_DESCRIPTOR_HANDLE mainTextureRenderTargetView;
  89. DXGI_FORMAT mainTextureFormat;
  90. ID3D12Resource *stagingBuffer;
  91. D3D12_RESOURCE_STATES stagingResourceState;
  92. D3D12_FILTER scaleMode;
  93. #if SDL_HAVE_YUV
  94. /* YV12 texture support */
  95. SDL_bool yuv;
  96. ID3D12Resource *mainTextureU;
  97. D3D12_CPU_DESCRIPTOR_HANDLE mainTextureResourceViewU;
  98. D3D12_RESOURCE_STATES mainResourceStateU;
  99. SIZE_T mainSRVIndexU;
  100. ID3D12Resource *mainTextureV;
  101. D3D12_CPU_DESCRIPTOR_HANDLE mainTextureResourceViewV;
  102. D3D12_RESOURCE_STATES mainResourceStateV;
  103. SIZE_T mainSRVIndexV;
  104. /* NV12 texture support */
  105. SDL_bool nv12;
  106. ID3D12Resource *mainTextureNV;
  107. D3D12_CPU_DESCRIPTOR_HANDLE mainTextureResourceViewNV;
  108. D3D12_RESOURCE_STATES mainResourceStateNV;
  109. SIZE_T mainSRVIndexNV;
  110. Uint8 *pixels;
  111. int pitch;
  112. #endif
  113. SDL_Rect lockedRect;
  114. } D3D12_TextureData;
  115. /* Pipeline State Object data */
  116. typedef struct
  117. {
  118. D3D12_Shader shader;
  119. SDL_BlendMode blendMode;
  120. D3D12_PRIMITIVE_TOPOLOGY_TYPE topology;
  121. DXGI_FORMAT rtvFormat;
  122. ID3D12PipelineState *pipelineState;
  123. } D3D12_PipelineState;
  124. /* Vertex Buffer */
  125. typedef struct
  126. {
  127. ID3D12Resource *resource;
  128. D3D12_VERTEX_BUFFER_VIEW view;
  129. size_t size;
  130. } D3D12_VertexBuffer;
  131. /* For SRV pool allocator */
  132. typedef struct
  133. {
  134. SIZE_T index;
  135. void *next;
  136. } D3D12_SRVPoolNode;
  137. /* Private renderer data */
  138. typedef struct
  139. {
  140. void *hDXGIMod;
  141. void *hD3D12Mod;
  142. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  143. UINT64 frameToken;
  144. #else
  145. IDXGIFactory6 *dxgiFactory;
  146. IDXGIAdapter4 *dxgiAdapter;
  147. IDXGIDebug *dxgiDebug;
  148. IDXGISwapChain4 *swapChain;
  149. #endif
  150. ID3D12Device1 *d3dDevice;
  151. ID3D12Debug *debugInterface;
  152. ID3D12CommandQueue *commandQueue;
  153. ID3D12GraphicsCommandList2 *commandList;
  154. DXGI_SWAP_EFFECT swapEffect;
  155. UINT swapFlags;
  156. /* Descriptor heaps */
  157. ID3D12DescriptorHeap* rtvDescriptorHeap;
  158. UINT rtvDescriptorSize;
  159. ID3D12DescriptorHeap* textureRTVDescriptorHeap;
  160. ID3D12DescriptorHeap* srvDescriptorHeap;
  161. UINT srvDescriptorSize;
  162. ID3D12DescriptorHeap* samplerDescriptorHeap;
  163. UINT samplerDescriptorSize;
  164. /* Data needed per backbuffer */
  165. ID3D12CommandAllocator *commandAllocators[SDL_D3D12_NUM_BUFFERS];
  166. ID3D12Resource *renderTargets[SDL_D3D12_NUM_BUFFERS];
  167. UINT64 fenceValue;
  168. int currentBackBufferIndex;
  169. /* Fences */
  170. ID3D12Fence *fence;
  171. HANDLE fenceEvent;
  172. /* Root signature and pipeline state data */
  173. ID3D12RootSignature *rootSignatures[NUM_ROOTSIGS];
  174. int pipelineStateCount;
  175. D3D12_PipelineState *pipelineStates;
  176. D3D12_PipelineState *currentPipelineState;
  177. D3D12_VertexBuffer vertexBuffers[SDL_D3D12_NUM_VERTEX_BUFFERS];
  178. D3D12_CPU_DESCRIPTOR_HANDLE nearestPixelSampler;
  179. D3D12_CPU_DESCRIPTOR_HANDLE linearSampler;
  180. /* Data for staging/allocating textures */
  181. ID3D12Resource *uploadBuffers[SDL_D3D12_NUM_UPLOAD_BUFFERS];
  182. int currentUploadBuffer;
  183. /* Pool allocator to handle reusing SRV heap indices */
  184. D3D12_SRVPoolNode *srvPoolHead;
  185. D3D12_SRVPoolNode srvPoolNodes[SDL_D3D12_MAX_NUM_TEXTURES];
  186. /* Vertex buffer constants */
  187. VertexShaderConstants vertexShaderConstantsData;
  188. /* Cached renderer properties */
  189. DXGI_MODE_ROTATION rotation;
  190. D3D12_TextureData* textureRenderTarget;
  191. D3D12_CPU_DESCRIPTOR_HANDLE currentRenderTargetView;
  192. D3D12_CPU_DESCRIPTOR_HANDLE currentShaderResource;
  193. D3D12_CPU_DESCRIPTOR_HANDLE currentSampler;
  194. SDL_bool cliprectDirty;
  195. SDL_bool currentCliprectEnabled;
  196. SDL_Rect currentCliprect;
  197. SDL_Rect currentViewport;
  198. int currentViewportRotation;
  199. SDL_bool viewportDirty;
  200. Float4X4 identity;
  201. int currentVertexBuffer;
  202. SDL_bool issueBatch;
  203. } D3D12_RenderData;
  204. /* Define D3D GUIDs here so we don't have to include uuid.lib. */
  205. #if defined(__GNUC__) || defined(__clang__)
  206. #pragma GCC diagnostic push
  207. #pragma GCC diagnostic ignored "-Wunused-const-variable"
  208. #endif
  209. static const GUID SDL_IID_IDXGIFactory6 = { 0xc1b6694f, 0xff09, 0x44a9, { 0xb0, 0x3c, 0x77, 0x90, 0x0a, 0x0a, 0x1d, 0x17 } };
  210. static const GUID SDL_IID_IDXGIAdapter4 = { 0x3c8d99d1, 0x4fbf, 0x4181, { 0xa8, 0x2c, 0xaf, 0x66, 0xbf, 0x7b, 0xd2, 0x4e } };
  211. static const GUID SDL_IID_IDXGIDevice1 = { 0x77db970f, 0x6276, 0x48ba, { 0xba, 0x28, 0x07, 0x01, 0x43, 0xb4, 0x39, 0x2c } };
  212. static const GUID SDL_IID_ID3D12Device1 = { 0x77acce80, 0x638e, 0x4e65, { 0x88, 0x95, 0xc1, 0xf2, 0x33, 0x86, 0x86, 0x3e } };
  213. static const GUID SDL_IID_IDXGISwapChain4 = { 0x3D585D5A, 0xBD4A, 0x489E, { 0xB1, 0xF4, 0x3D, 0xBC, 0xB6, 0x45, 0x2F, 0xFB } };
  214. static const GUID SDL_IID_IDXGIDebug1 = { 0xc5a05f0c, 0x16f2, 0x4adf, { 0x9f, 0x4d, 0xa8, 0xc4, 0xd5, 0x8a, 0xc5, 0x50 } };
  215. static const GUID SDL_IID_IDXGIInfoQueue = { 0xD67441C7,0x672A,0x476f, { 0x9E,0x82,0xCD,0x55,0xB4,0x49,0x49,0xCE } };
  216. static const GUID SDL_IID_ID3D12Debug = { 0x344488b7, 0x6846, 0x474b, { 0xb9, 0x89, 0xf0, 0x27, 0x44, 0x82, 0x45, 0xe0 } };
  217. static const GUID SDL_DXGI_DEBUG_ALL = { 0xe48ae283, 0xda80, 0x490b, { 0x87, 0xe6, 0x43, 0xe9, 0xa9, 0xcf, 0xda, 0x8 } };
  218. static const GUID SDL_IID_ID3D12CommandQueue = { 0x0ec870a6, 0x5d7e, 0x4c22, { 0x8c, 0xfc, 0x5b, 0xaa, 0xe0, 0x76, 0x16, 0xed } };
  219. static const GUID SDL_IID_ID3D12DescriptorHeap = { 0x8efb471d, 0x616c, 0x4f49, { 0x90, 0xf7, 0x12, 0x7b, 0xb7, 0x63, 0xfa, 0x51 } };
  220. static const GUID SDL_IID_ID3D12CommandAllocator = { 0x6102dee4, 0xaf59, 0x4b09, { 0xb9, 0x99, 0xb4, 0x4d, 0x73, 0xf0, 0x9b, 0x24 } };
  221. static const GUID SDL_IID_ID3D12GraphicsCommandList2 = { 0x38C3E585, 0xFF17, 0x412C, { 0x91, 0x50, 0x4F, 0xC6, 0xF9, 0xD7, 0x2A, 0x28 } };
  222. static const GUID SDL_IID_ID3D12Fence = { 0x0a753dcf, 0xc4d8, 0x4b91, { 0xad, 0xf6, 0xbe, 0x5a, 0x60, 0xd9, 0x5a, 0x76 } };
  223. static const GUID SDL_IID_ID3D12Resource = { 0x696442be, 0xa72e, 0x4059, { 0xbc, 0x79, 0x5b, 0x5c, 0x98, 0x04, 0x0f, 0xad } };
  224. static const GUID SDL_IID_ID3D12RootSignature = { 0xc54a6b66, 0x72df, 0x4ee8, { 0x8b, 0xe5, 0xa9, 0x46, 0xa1, 0x42, 0x92, 0x14 } };
  225. static const GUID SDL_IID_ID3D12PipelineState = { 0x765a30f3, 0xf624, 0x4c6f, { 0xa8, 0x28, 0xac, 0xe9, 0x48, 0x62, 0x24, 0x45 } };
  226. static const GUID SDL_IID_ID3D12Heap = { 0x6b3b2502, 0x6e51, 0x45b3, { 0x90, 0xee, 0x98, 0x84, 0x26, 0x5e, 0x8d, 0xf3 } };
  227. static const GUID SDL_IID_ID3D12InfoQueue = { 0x0742a90b, 0xc387, 0x483f, { 0xb9, 0x46, 0x30, 0xa7, 0xe4, 0xe6, 0x14, 0x58 } };
  228. #if defined(__GNUC__) || defined(__clang__)
  229. #pragma GCC diagnostic pop
  230. #endif
  231. UINT
  232. D3D12_Align(UINT location, UINT alignment)
  233. {
  234. return ((location + (alignment - 1)) & ~(alignment - 1));
  235. }
  236. Uint32
  237. D3D12_DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat)
  238. {
  239. switch (dxgiFormat) {
  240. case DXGI_FORMAT_B8G8R8A8_UNORM:
  241. return SDL_PIXELFORMAT_ARGB8888;
  242. case DXGI_FORMAT_B8G8R8X8_UNORM:
  243. return SDL_PIXELFORMAT_RGB888;
  244. default:
  245. return SDL_PIXELFORMAT_UNKNOWN;
  246. }
  247. }
  248. static DXGI_FORMAT
  249. SDLPixelFormatToDXGIFormat(Uint32 sdlFormat)
  250. {
  251. switch (sdlFormat) {
  252. case SDL_PIXELFORMAT_ARGB8888:
  253. return DXGI_FORMAT_B8G8R8A8_UNORM;
  254. case SDL_PIXELFORMAT_RGB888:
  255. return DXGI_FORMAT_B8G8R8X8_UNORM;
  256. case SDL_PIXELFORMAT_YV12:
  257. case SDL_PIXELFORMAT_IYUV:
  258. case SDL_PIXELFORMAT_NV12: /* For the Y texture */
  259. case SDL_PIXELFORMAT_NV21: /* For the Y texture */
  260. return DXGI_FORMAT_R8_UNORM;
  261. default:
  262. return DXGI_FORMAT_UNKNOWN;
  263. }
  264. }
  265. static void D3D12_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  266. static void
  267. D3D12_ReleaseAll(SDL_Renderer * renderer)
  268. {
  269. D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
  270. SDL_Texture *texture = NULL;
  271. /* Release all textures */
  272. for (texture = renderer->textures; texture; texture = texture->next) {
  273. D3D12_DestroyTexture(renderer, texture);
  274. }
  275. /* Release/reset everything else */
  276. if (data) {
  277. int i;
  278. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  279. SAFE_RELEASE(data->dxgiFactory);
  280. SAFE_RELEASE(data->dxgiAdapter);
  281. SAFE_RELEASE(data->swapChain);
  282. #endif
  283. SAFE_RELEASE(data->d3dDevice);
  284. SAFE_RELEASE(data->debugInterface);
  285. SAFE_RELEASE(data->commandQueue);
  286. SAFE_RELEASE(data->commandList);
  287. SAFE_RELEASE(data->rtvDescriptorHeap);
  288. SAFE_RELEASE(data->textureRTVDescriptorHeap);
  289. SAFE_RELEASE(data->srvDescriptorHeap);
  290. SAFE_RELEASE(data->samplerDescriptorHeap);
  291. SAFE_RELEASE(data->fence);
  292. for (i = 0; i < SDL_D3D12_NUM_BUFFERS; ++i) {
  293. SAFE_RELEASE(data->commandAllocators[i]);
  294. SAFE_RELEASE(data->renderTargets[i]);
  295. }
  296. if (data->pipelineStateCount > 0) {
  297. for (i = 0; i < data->pipelineStateCount; ++i) {
  298. SAFE_RELEASE(data->pipelineStates[i].pipelineState);
  299. }
  300. SDL_free(data->pipelineStates);
  301. data->pipelineStateCount = 0;
  302. }
  303. for (i = 0; i < NUM_ROOTSIGS; ++i) {
  304. SAFE_RELEASE(data->rootSignatures[i]);
  305. }
  306. for (i = 0; i < SDL_D3D12_NUM_VERTEX_BUFFERS; ++i) {
  307. SAFE_RELEASE(data->vertexBuffers[i].resource);
  308. data->vertexBuffers[i].size = 0;
  309. }
  310. data->swapEffect = (DXGI_SWAP_EFFECT) 0;
  311. data->swapFlags = 0;
  312. data->currentRenderTargetView.ptr = 0;
  313. data->currentSampler.ptr = 0;
  314. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  315. /* Check for any leaks if in debug mode */
  316. if (data->dxgiDebug) {
  317. DXGI_DEBUG_RLO_FLAGS rloFlags = (DXGI_DEBUG_RLO_FLAGS)(DXGI_DEBUG_RLO_SUMMARY | DXGI_DEBUG_RLO_IGNORE_INTERNAL);
  318. D3D_CALL(data->dxgiDebug, ReportLiveObjects, SDL_DXGI_DEBUG_ALL, rloFlags);
  319. SAFE_RELEASE(data->dxgiDebug);
  320. }
  321. #endif
  322. /* Unload the D3D libraries. This should be done last, in order
  323. * to prevent IUnknown::Release() calls from crashing.
  324. */
  325. if (data->hD3D12Mod) {
  326. SDL_UnloadObject(data->hD3D12Mod);
  327. data->hD3D12Mod = NULL;
  328. }
  329. if (data->hDXGIMod) {
  330. SDL_UnloadObject(data->hDXGIMod);
  331. data->hDXGIMod = NULL;
  332. }
  333. }
  334. }
  335. static D3D12_GPU_DESCRIPTOR_HANDLE
  336. D3D12_CPUtoGPUHandle(ID3D12DescriptorHeap * heap, D3D12_CPU_DESCRIPTOR_HANDLE CPUHandle)
  337. {
  338. D3D12_CPU_DESCRIPTOR_HANDLE CPUHeapStart;
  339. D3D12_GPU_DESCRIPTOR_HANDLE GPUHandle;
  340. SIZE_T offset;
  341. /* Calculate the correct offset into the heap */
  342. D3D_CALL_RET(heap, GetCPUDescriptorHandleForHeapStart, &CPUHeapStart);
  343. offset = CPUHandle.ptr - CPUHeapStart.ptr;
  344. D3D_CALL_RET(heap, GetGPUDescriptorHandleForHeapStart, &GPUHandle);
  345. GPUHandle.ptr += offset;
  346. return GPUHandle;
  347. }
  348. static void
  349. D3D12_WaitForGPU(D3D12_RenderData * data)
  350. {
  351. if (data->commandQueue && data->fence && data->fenceEvent)
  352. {
  353. D3D_CALL(data->commandQueue, Signal, data->fence, data->fenceValue);
  354. if (D3D_CALL(data->fence, GetCompletedValue) < data->fenceValue) {
  355. D3D_CALL(data->fence, SetEventOnCompletion,
  356. data->fenceValue,
  357. data->fenceEvent
  358. );
  359. WaitForSingleObjectEx(data->fenceEvent, INFINITE, FALSE);
  360. }
  361. data->fenceValue++;
  362. }
  363. }
  364. static D3D12_CPU_DESCRIPTOR_HANDLE
  365. D3D12_GetCurrentRenderTargetView(SDL_Renderer * renderer)
  366. {
  367. D3D12_RenderData* data = (D3D12_RenderData*)renderer->driverdata;
  368. D3D12_CPU_DESCRIPTOR_HANDLE rtvDescriptor;
  369. if (data->textureRenderTarget) {
  370. return data->textureRenderTarget->mainTextureRenderTargetView;
  371. }
  372. SDL_zero(rtvDescriptor);
  373. D3D_CALL_RET(data->rtvDescriptorHeap, GetCPUDescriptorHandleForHeapStart, &rtvDescriptor);
  374. rtvDescriptor.ptr += data->currentBackBufferIndex * data->rtvDescriptorSize;
  375. return rtvDescriptor;
  376. }
  377. static void
  378. D3D12_TransitionResource(D3D12_RenderData * data,
  379. ID3D12Resource * resource,
  380. D3D12_RESOURCE_STATES beforeState,
  381. D3D12_RESOURCE_STATES afterState
  382. )
  383. {
  384. D3D12_RESOURCE_BARRIER barrier;
  385. if (beforeState != afterState) {
  386. SDL_zero(barrier);
  387. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  388. barrier.Transition.pResource = resource;
  389. barrier.Transition.StateBefore = beforeState;
  390. barrier.Transition.StateAfter = afterState;
  391. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  392. D3D_CALL(data->commandList, ResourceBarrier, 1, &barrier);
  393. }
  394. }
  395. static void
  396. D3D12_ResetCommandList(D3D12_RenderData * data)
  397. {
  398. int i;
  399. ID3D12DescriptorHeap *rootDescriptorHeaps[] = { data->srvDescriptorHeap, data->samplerDescriptorHeap };
  400. ID3D12CommandAllocator *commandAllocator = data->commandAllocators[data->currentBackBufferIndex];
  401. D3D_CALL(commandAllocator, Reset);
  402. D3D_CALL(data->commandList, Reset, commandAllocator, NULL);
  403. data->currentPipelineState = NULL;
  404. data->currentVertexBuffer = 0;
  405. data->issueBatch = SDL_FALSE;
  406. data->cliprectDirty = SDL_TRUE;
  407. data->viewportDirty = SDL_TRUE;
  408. data->currentRenderTargetView.ptr = 0;
  409. /* Release any upload buffers that were inflight */
  410. for (i = 0; i < data->currentUploadBuffer; ++i) {
  411. SAFE_RELEASE(data->uploadBuffers[i]);
  412. }
  413. data->currentUploadBuffer = 0;
  414. D3D_CALL(data->commandList, SetDescriptorHeaps, 2, rootDescriptorHeaps);
  415. }
  416. static int
  417. D3D12_IssueBatch(D3D12_RenderData * data)
  418. {
  419. HRESULT result = S_OK;
  420. /* Issue the command list */
  421. result = D3D_CALL(data->commandList, Close);
  422. if (FAILED(result)) {
  423. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("D3D12_IssueBatch"), result);
  424. return result;
  425. }
  426. D3D_CALL(data->commandQueue, ExecuteCommandLists, 1, (ID3D12CommandList* const*)&data->commandList);
  427. D3D12_WaitForGPU(data);
  428. D3D12_ResetCommandList(data);
  429. return result;
  430. }
  431. static void
  432. D3D12_DestroyRenderer(SDL_Renderer * renderer)
  433. {
  434. D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
  435. D3D12_WaitForGPU(data);
  436. D3D12_ReleaseAll(renderer);
  437. if (data) {
  438. SDL_free(data);
  439. }
  440. SDL_free(renderer);
  441. }
  442. static int
  443. D3D12_GetOutputSize(SDL_Renderer *renderer, int *w, int *h)
  444. {
  445. SDL_GetWindowSizeInPixels(renderer->window, w, h);
  446. return 0;
  447. }
  448. static D3D12_BLEND
  449. GetBlendFunc(SDL_BlendFactor factor)
  450. {
  451. switch (factor) {
  452. case SDL_BLENDFACTOR_ZERO:
  453. return D3D12_BLEND_ZERO;
  454. case SDL_BLENDFACTOR_ONE:
  455. return D3D12_BLEND_ONE;
  456. case SDL_BLENDFACTOR_SRC_COLOR:
  457. return D3D12_BLEND_SRC_COLOR;
  458. case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR:
  459. return D3D12_BLEND_INV_SRC_COLOR;
  460. case SDL_BLENDFACTOR_SRC_ALPHA:
  461. return D3D12_BLEND_SRC_ALPHA;
  462. case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA:
  463. return D3D12_BLEND_INV_SRC_ALPHA;
  464. case SDL_BLENDFACTOR_DST_COLOR:
  465. return D3D12_BLEND_DEST_COLOR;
  466. case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR:
  467. return D3D12_BLEND_INV_DEST_COLOR;
  468. case SDL_BLENDFACTOR_DST_ALPHA:
  469. return D3D12_BLEND_DEST_ALPHA;
  470. case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA:
  471. return D3D12_BLEND_INV_DEST_ALPHA;
  472. default:
  473. return (D3D12_BLEND)0;
  474. }
  475. }
  476. static D3D12_BLEND_OP
  477. GetBlendEquation(SDL_BlendOperation operation)
  478. {
  479. switch (operation) {
  480. case SDL_BLENDOPERATION_ADD:
  481. return D3D12_BLEND_OP_ADD;
  482. case SDL_BLENDOPERATION_SUBTRACT:
  483. return D3D12_BLEND_OP_SUBTRACT;
  484. case SDL_BLENDOPERATION_REV_SUBTRACT:
  485. return D3D12_BLEND_OP_REV_SUBTRACT;
  486. case SDL_BLENDOPERATION_MINIMUM:
  487. return D3D12_BLEND_OP_MIN;
  488. case SDL_BLENDOPERATION_MAXIMUM:
  489. return D3D12_BLEND_OP_MAX;
  490. default:
  491. return (D3D12_BLEND_OP)0;
  492. }
  493. }
  494. static void
  495. D3D12_CreateBlendState(SDL_Renderer * renderer, SDL_BlendMode blendMode, D3D12_BLEND_DESC * outBlendDesc)
  496. {
  497. SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
  498. SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
  499. SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
  500. SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
  501. SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
  502. SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
  503. SDL_zerop(outBlendDesc);
  504. outBlendDesc->AlphaToCoverageEnable = FALSE;
  505. outBlendDesc->IndependentBlendEnable = FALSE;
  506. outBlendDesc->RenderTarget[0].BlendEnable = TRUE;
  507. outBlendDesc->RenderTarget[0].SrcBlend = GetBlendFunc(srcColorFactor);
  508. outBlendDesc->RenderTarget[0].DestBlend = GetBlendFunc(dstColorFactor);
  509. outBlendDesc->RenderTarget[0].BlendOp = GetBlendEquation(colorOperation);
  510. outBlendDesc->RenderTarget[0].SrcBlendAlpha = GetBlendFunc(srcAlphaFactor);
  511. outBlendDesc->RenderTarget[0].DestBlendAlpha = GetBlendFunc(dstAlphaFactor);
  512. outBlendDesc->RenderTarget[0].BlendOpAlpha = GetBlendEquation(alphaOperation);
  513. outBlendDesc->RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
  514. }
  515. static D3D12_PipelineState *
  516. D3D12_CreatePipelineState(SDL_Renderer * renderer,
  517. D3D12_Shader shader,
  518. SDL_BlendMode blendMode,
  519. D3D12_PRIMITIVE_TOPOLOGY_TYPE topology,
  520. DXGI_FORMAT rtvFormat
  521. )
  522. {
  523. const D3D12_INPUT_ELEMENT_DESC vertexDesc[] = {
  524. { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
  525. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
  526. { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 16, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
  527. };
  528. D3D12_RenderData* data = (D3D12_RenderData*)renderer->driverdata;
  529. D3D12_GRAPHICS_PIPELINE_STATE_DESC pipelineDesc;
  530. ID3D12PipelineState* pipelineState = NULL;
  531. D3D12_PipelineState* pipelineStates;
  532. HRESULT result = S_OK;
  533. SDL_zero(pipelineDesc);
  534. pipelineDesc.pRootSignature = data->rootSignatures[D3D12_GetRootSignatureType(shader)];
  535. D3D12_GetVertexShader(shader, &pipelineDesc.VS);
  536. D3D12_GetPixelShader(shader, &pipelineDesc.PS);
  537. D3D12_CreateBlendState(renderer, blendMode, &pipelineDesc.BlendState);
  538. pipelineDesc.SampleMask = 0xffffffff;
  539. pipelineDesc.RasterizerState.AntialiasedLineEnable = FALSE;
  540. pipelineDesc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
  541. pipelineDesc.RasterizerState.DepthBias = 0;
  542. pipelineDesc.RasterizerState.DepthBiasClamp = 0.0f;
  543. pipelineDesc.RasterizerState.DepthClipEnable = TRUE;
  544. pipelineDesc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID;
  545. pipelineDesc.RasterizerState.FrontCounterClockwise = FALSE;
  546. pipelineDesc.RasterizerState.MultisampleEnable = FALSE;
  547. pipelineDesc.RasterizerState.SlopeScaledDepthBias = 0.0f;
  548. pipelineDesc.InputLayout.pInputElementDescs = vertexDesc;
  549. pipelineDesc.InputLayout.NumElements = 3;
  550. pipelineDesc.PrimitiveTopologyType = topology;
  551. pipelineDesc.NumRenderTargets = 1;
  552. pipelineDesc.RTVFormats[0] = rtvFormat;
  553. pipelineDesc.SampleDesc.Count = 1;
  554. pipelineDesc.SampleDesc.Quality = 0;
  555. result = D3D_CALL(data->d3dDevice, CreateGraphicsPipelineState,
  556. &pipelineDesc,
  557. D3D_GUID(SDL_IID_ID3D12PipelineState),
  558. (void **)&pipelineState
  559. );
  560. if (FAILED(result)) {
  561. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateGraphicsPipelineState"), result);
  562. return NULL;
  563. }
  564. pipelineStates = (D3D12_PipelineState*)SDL_realloc(data->pipelineStates, (data->pipelineStateCount + 1) * sizeof(*pipelineStates));
  565. if (!pipelineStates) {
  566. SAFE_RELEASE(pipelineState);
  567. SDL_OutOfMemory();
  568. return NULL;
  569. }
  570. pipelineStates[data->pipelineStateCount].shader = shader;
  571. pipelineStates[data->pipelineStateCount].blendMode = blendMode;
  572. pipelineStates[data->pipelineStateCount].topology = topology;
  573. pipelineStates[data->pipelineStateCount].rtvFormat = rtvFormat;
  574. pipelineStates[data->pipelineStateCount].pipelineState = pipelineState;
  575. data->pipelineStates = pipelineStates;
  576. ++data->pipelineStateCount;
  577. return &pipelineStates[data->pipelineStateCount - 1];
  578. }
  579. static HRESULT
  580. D3D12_CreateVertexBuffer(D3D12_RenderData *data, size_t vbidx, size_t size)
  581. {
  582. D3D12_HEAP_PROPERTIES vbufferHeapProps;
  583. D3D12_RESOURCE_DESC vbufferDesc;
  584. HRESULT result;
  585. SAFE_RELEASE(data->vertexBuffers[vbidx].resource);
  586. SDL_zero(vbufferHeapProps);
  587. vbufferHeapProps.Type = D3D12_HEAP_TYPE_UPLOAD;
  588. vbufferHeapProps.CreationNodeMask = 1;
  589. vbufferHeapProps.VisibleNodeMask = 1;
  590. SDL_zero(vbufferDesc);
  591. vbufferDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  592. vbufferDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
  593. vbufferDesc.Width = size;
  594. vbufferDesc.Height = 1;
  595. vbufferDesc.DepthOrArraySize = 1;
  596. vbufferDesc.MipLevels = 1;
  597. vbufferDesc.Format = DXGI_FORMAT_UNKNOWN;
  598. vbufferDesc.SampleDesc.Count = 1;
  599. vbufferDesc.SampleDesc.Quality = 0;
  600. vbufferDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  601. vbufferDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
  602. result = D3D_CALL(data->d3dDevice, CreateCommittedResource,
  603. &vbufferHeapProps,
  604. D3D12_HEAP_FLAG_NONE,
  605. &vbufferDesc,
  606. D3D12_RESOURCE_STATE_GENERIC_READ,
  607. NULL,
  608. D3D_GUID(SDL_IID_ID3D12Resource),
  609. (void **) &data->vertexBuffers[vbidx].resource
  610. );
  611. if (FAILED(result)) {
  612. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreatePlacedResource [vertex buffer]"), result);
  613. return result;
  614. }
  615. data->vertexBuffers[vbidx].view.BufferLocation = D3D_CALL(data->vertexBuffers[vbidx].resource, GetGPUVirtualAddress);
  616. data->vertexBuffers[vbidx].view.StrideInBytes = sizeof(VertexPositionColor);
  617. data->vertexBuffers[vbidx].size = size;
  618. return result;
  619. }
  620. /* Create resources that depend on the device. */
  621. static HRESULT
  622. D3D12_CreateDeviceResources(SDL_Renderer* renderer)
  623. {
  624. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  625. typedef HRESULT(WINAPI* PFN_CREATE_DXGI_FACTORY)(UINT flags, REFIID riid, void** ppFactory);
  626. PFN_CREATE_DXGI_FACTORY CreateDXGIFactoryFunc;
  627. PFN_D3D12_CREATE_DEVICE D3D12CreateDeviceFunc;
  628. #endif
  629. typedef HANDLE(WINAPI* PFN_CREATE_EVENT_EX)(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCWSTR lpName, DWORD dwFlags, DWORD dwDesiredAccess);
  630. PFN_CREATE_EVENT_EX CreateEventExFunc;
  631. D3D12_RenderData* data = (D3D12_RenderData*)renderer->driverdata;
  632. ID3D12Device* d3dDevice = NULL;
  633. HRESULT result = S_OK;
  634. UINT creationFlags = 0;
  635. int i, j, k, l;
  636. SDL_bool createDebug = SDL_FALSE;
  637. D3D12_COMMAND_QUEUE_DESC queueDesc;
  638. D3D12_DESCRIPTOR_HEAP_DESC descriptorHeapDesc;
  639. D3D12_SAMPLER_DESC samplerDesc;
  640. ID3D12DescriptorHeap *rootDescriptorHeaps[2];
  641. const SDL_BlendMode defaultBlendModes[] = {
  642. SDL_BLENDMODE_NONE,
  643. SDL_BLENDMODE_BLEND,
  644. SDL_BLENDMODE_ADD,
  645. SDL_BLENDMODE_MOD,
  646. SDL_BLENDMODE_MUL
  647. };
  648. const DXGI_FORMAT defaultRTVFormats[] = {
  649. DXGI_FORMAT_B8G8R8A8_UNORM,
  650. DXGI_FORMAT_B8G8R8X8_UNORM,
  651. DXGI_FORMAT_R8_UNORM
  652. };
  653. /* See if we need debug interfaces */
  654. createDebug = SDL_GetHintBoolean(SDL_HINT_RENDER_DIRECT3D11_DEBUG, SDL_FALSE);
  655. #ifdef __GDK__
  656. CreateEventExFunc = CreateEventExW;
  657. #else
  658. /* CreateEventEx() arrived in Vista, so we need to load it with GetProcAddress for XP. */
  659. {
  660. HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  661. CreateEventExFunc = NULL;
  662. if (kernel32) {
  663. CreateEventExFunc = (PFN_CREATE_EVENT_EX) GetProcAddress(kernel32, "CreateEventExW");
  664. }
  665. }
  666. #endif
  667. if (!CreateEventExFunc) {
  668. result = E_FAIL;
  669. goto done;
  670. }
  671. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  672. data->hDXGIMod = SDL_LoadObject("dxgi.dll");
  673. if (!data->hDXGIMod) {
  674. result = E_FAIL;
  675. goto done;
  676. }
  677. CreateDXGIFactoryFunc = (PFN_CREATE_DXGI_FACTORY)SDL_LoadFunction(data->hDXGIMod, "CreateDXGIFactory2");
  678. if (!CreateDXGIFactoryFunc) {
  679. result = E_FAIL;
  680. goto done;
  681. }
  682. data->hD3D12Mod = SDL_LoadObject("D3D12.dll");
  683. if (!data->hD3D12Mod) {
  684. result = E_FAIL;
  685. goto done;
  686. }
  687. D3D12CreateDeviceFunc = (PFN_D3D12_CREATE_DEVICE)SDL_LoadFunction(data->hD3D12Mod, "D3D12CreateDevice");
  688. if (!D3D12CreateDeviceFunc) {
  689. result = E_FAIL;
  690. goto done;
  691. }
  692. if (createDebug) {
  693. PFN_D3D12_GET_DEBUG_INTERFACE D3D12GetDebugInterfaceFunc;
  694. D3D12GetDebugInterfaceFunc = (PFN_D3D12_GET_DEBUG_INTERFACE)SDL_LoadFunction(data->hD3D12Mod, "D3D12GetDebugInterface");
  695. if (!D3D12GetDebugInterfaceFunc) {
  696. result = E_FAIL;
  697. goto done;
  698. }
  699. D3D12GetDebugInterfaceFunc(D3D_GUID(SDL_IID_ID3D12Debug), (void**)&data->debugInterface);
  700. D3D_CALL(data->debugInterface, EnableDebugLayer);
  701. }
  702. #endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  703. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  704. result = D3D12_XBOX_CreateDevice(&d3dDevice, createDebug);
  705. if (FAILED(result)) {
  706. /* SDL Error is set by D3D12_XBOX_CreateDevice */
  707. goto done;
  708. }
  709. #else
  710. if (createDebug) {
  711. #ifdef __IDXGIInfoQueue_INTERFACE_DEFINED__
  712. IDXGIInfoQueue *dxgiInfoQueue = NULL;
  713. PFN_CREATE_DXGI_FACTORY DXGIGetDebugInterfaceFunc;
  714. /* If the debug hint is set, also create the DXGI factory in debug mode */
  715. DXGIGetDebugInterfaceFunc = (PFN_CREATE_DXGI_FACTORY)SDL_LoadFunction(data->hDXGIMod, "DXGIGetDebugInterface1");
  716. if (!DXGIGetDebugInterfaceFunc) {
  717. result = E_FAIL;
  718. goto done;
  719. }
  720. result = DXGIGetDebugInterfaceFunc(0, D3D_GUID(SDL_IID_IDXGIDebug1), (void **)&data->dxgiDebug);
  721. if (FAILED(result)) {
  722. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("DXGIGetDebugInterface1"), result);
  723. goto done;
  724. }
  725. result = DXGIGetDebugInterfaceFunc(0, D3D_GUID(SDL_IID_IDXGIInfoQueue), (void **)&dxgiInfoQueue);
  726. if (FAILED(result)) {
  727. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("DXGIGetDebugInterface1"), result);
  728. goto done;
  729. }
  730. D3D_CALL(dxgiInfoQueue, SetBreakOnSeverity, SDL_DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_ERROR, TRUE);
  731. D3D_CALL(dxgiInfoQueue, SetBreakOnSeverity, SDL_DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_CORRUPTION, TRUE);
  732. SAFE_RELEASE(dxgiInfoQueue);
  733. #endif /* __IDXGIInfoQueue_INTERFACE_DEFINED__ */
  734. creationFlags = DXGI_CREATE_FACTORY_DEBUG;
  735. }
  736. result = CreateDXGIFactoryFunc(creationFlags, D3D_GUID(SDL_IID_IDXGIFactory6), (void**)&data->dxgiFactory);
  737. if (FAILED(result)) {
  738. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("CreateDXGIFactory"), result);
  739. goto done;
  740. }
  741. /* Prefer a high performance adapter if there are multiple choices */
  742. result = D3D_CALL(data->dxgiFactory, EnumAdapterByGpuPreference,
  743. 0,
  744. DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE,
  745. D3D_GUID(SDL_IID_IDXGIAdapter4),
  746. (void **)&data->dxgiAdapter
  747. );
  748. if (FAILED(result)) {
  749. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("D3D12CreateDevice"), result);
  750. goto done;
  751. }
  752. result = D3D12CreateDeviceFunc((IUnknown *)data->dxgiAdapter,
  753. D3D_FEATURE_LEVEL_11_0, /* Request minimum feature level 11.0 for maximum compatibility */
  754. D3D_GUID(SDL_IID_ID3D12Device1),
  755. (void **)&d3dDevice
  756. );
  757. if (FAILED(result)) {
  758. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("D3D12CreateDevice"), result);
  759. goto done;
  760. }
  761. /* Setup the info queue if in debug mode */
  762. if (createDebug) {
  763. ID3D12InfoQueue *infoQueue = NULL;
  764. D3D12_MESSAGE_SEVERITY severities[] = { D3D12_MESSAGE_SEVERITY_INFO };
  765. D3D12_INFO_QUEUE_FILTER filter;
  766. result = D3D_CALL(d3dDevice, QueryInterface, D3D_GUID(SDL_IID_ID3D12InfoQueue), (void **)&infoQueue);
  767. if (FAILED(result)) {
  768. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device to ID3D12InfoQueue"), result);
  769. goto done;
  770. }
  771. SDL_zero(filter);
  772. filter.DenyList.NumSeverities = 1;
  773. filter.DenyList.pSeverityList = severities;
  774. D3D_CALL(infoQueue, PushStorageFilter, &filter);
  775. D3D_CALL(infoQueue, SetBreakOnSeverity, D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
  776. D3D_CALL(infoQueue, SetBreakOnSeverity, D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);
  777. SAFE_RELEASE(infoQueue);
  778. }
  779. #endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  780. result = D3D_CALL(d3dDevice, QueryInterface, D3D_GUID(SDL_IID_ID3D12Device1), (void **)&data->d3dDevice);
  781. if (FAILED(result)) {
  782. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device to ID3D12Device1"), result);
  783. goto done;
  784. }
  785. /* Create a command queue */
  786. SDL_zero(queueDesc);
  787. queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  788. queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
  789. result = D3D_CALL(data->d3dDevice, CreateCommandQueue,
  790. &queueDesc,
  791. D3D_GUID(SDL_IID_ID3D12CommandQueue),
  792. (void **)&data->commandQueue
  793. );
  794. if (FAILED(result)) {
  795. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommandQueue"), result);
  796. goto done;
  797. }
  798. /* Create the descriptor heaps for the render target view, texture SRVs, and samplers */
  799. SDL_zero(descriptorHeapDesc);
  800. descriptorHeapDesc.NumDescriptors = SDL_D3D12_NUM_BUFFERS;
  801. descriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
  802. result = D3D_CALL(data->d3dDevice, CreateDescriptorHeap,
  803. &descriptorHeapDesc,
  804. D3D_GUID(SDL_IID_ID3D12DescriptorHeap),
  805. (void **)&data->rtvDescriptorHeap
  806. );
  807. if (FAILED(result)) {
  808. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateDescriptorHeap [rtv]"), result);
  809. goto done;
  810. }
  811. data->rtvDescriptorSize = D3D_CALL(d3dDevice, GetDescriptorHandleIncrementSize, D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  812. descriptorHeapDesc.NumDescriptors = SDL_D3D12_MAX_NUM_TEXTURES;
  813. result = D3D_CALL(data->d3dDevice, CreateDescriptorHeap,
  814. &descriptorHeapDesc,
  815. D3D_GUID(SDL_IID_ID3D12DescriptorHeap),
  816. (void **)&data->textureRTVDescriptorHeap
  817. );
  818. if (FAILED(result)) {
  819. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateDescriptorHeap [texture rtv]"), result);
  820. goto done;
  821. }
  822. SDL_zero(descriptorHeapDesc);
  823. descriptorHeapDesc.NumDescriptors = SDL_D3D12_MAX_NUM_TEXTURES;
  824. descriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  825. descriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  826. result = D3D_CALL(data->d3dDevice, CreateDescriptorHeap,
  827. &descriptorHeapDesc,
  828. D3D_GUID(SDL_IID_ID3D12DescriptorHeap),
  829. (void **)&data->srvDescriptorHeap
  830. );
  831. if (FAILED(result)) {
  832. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateDescriptorHeap [srv]"), result);
  833. goto done;
  834. }
  835. rootDescriptorHeaps[0] = data->srvDescriptorHeap;
  836. data->srvDescriptorSize = D3D_CALL(d3dDevice, GetDescriptorHandleIncrementSize, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
  837. SDL_zero(descriptorHeapDesc);
  838. descriptorHeapDesc.NumDescriptors = 2;
  839. descriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;
  840. descriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  841. result = D3D_CALL(data->d3dDevice, CreateDescriptorHeap,
  842. &descriptorHeapDesc,
  843. D3D_GUID(SDL_IID_ID3D12DescriptorHeap),
  844. (void **)&data->samplerDescriptorHeap
  845. );
  846. if (FAILED(result)) {
  847. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateDescriptorHeap [sampler]"), result);
  848. goto done;
  849. }
  850. rootDescriptorHeaps[1] = data->samplerDescriptorHeap;
  851. data->samplerDescriptorSize = D3D_CALL(d3dDevice, GetDescriptorHandleIncrementSize, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
  852. /* Create a command allocator for each back buffer */
  853. for (i = 0; i < SDL_D3D12_NUM_BUFFERS; ++i) {
  854. result = D3D_CALL(data->d3dDevice, CreateCommandAllocator,
  855. D3D12_COMMAND_LIST_TYPE_DIRECT,
  856. D3D_GUID(SDL_IID_ID3D12CommandAllocator),
  857. (void **)&data->commandAllocators[i]
  858. );
  859. if (FAILED(result)) {
  860. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommandAllocator"), result);
  861. goto done;
  862. }
  863. }
  864. /* Create the command list */
  865. result = D3D_CALL(data->d3dDevice, CreateCommandList,
  866. 0,
  867. D3D12_COMMAND_LIST_TYPE_DIRECT,
  868. data->commandAllocators[0],
  869. NULL,
  870. D3D_GUID(SDL_IID_ID3D12GraphicsCommandList2),
  871. (void **)&data->commandList
  872. );
  873. if (FAILED(result)) {
  874. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommandList"), result);
  875. goto done;
  876. }
  877. /* Set the descriptor heaps to the correct initial value */
  878. D3D_CALL(data->commandList, SetDescriptorHeaps, 2, rootDescriptorHeaps);
  879. /* Create the fence and fence event */
  880. result = D3D_CALL(data->d3dDevice, CreateFence,
  881. data->fenceValue,
  882. D3D12_FENCE_FLAG_NONE,
  883. D3D_GUID(SDL_IID_ID3D12Fence),
  884. (void **)&data->fence
  885. );
  886. if (FAILED(result)) {
  887. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateFence"), result);
  888. goto done;
  889. }
  890. data->fenceValue++;
  891. data->fenceEvent = CreateEventExFunc(NULL, NULL, 0, EVENT_MODIFY_STATE | SYNCHRONIZE);
  892. if (!data->fenceEvent) {
  893. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("CreateEventEx"), result);
  894. goto done;
  895. }
  896. /* Create all the root signatures */
  897. for (i = 0; i < NUM_ROOTSIGS; ++i) {
  898. D3D12_SHADER_BYTECODE rootSigData;
  899. D3D12_GetRootSignatureData((D3D12_RootSignature) i, &rootSigData);
  900. result = D3D_CALL(data->d3dDevice, CreateRootSignature,
  901. 0,
  902. rootSigData.pShaderBytecode,
  903. rootSigData.BytecodeLength,
  904. D3D_GUID(SDL_IID_ID3D12RootSignature),
  905. (void **)&data->rootSignatures[i]
  906. );
  907. if (FAILED(result)) {
  908. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateRootSignature"), result);
  909. goto done;
  910. }
  911. }
  912. /* Create all the default pipeline state objects
  913. (will add everything except custom blend states) */
  914. for (i = 0; i < NUM_SHADERS; ++i) {
  915. for (j = 0; j < SDL_arraysize(defaultBlendModes); ++j) {
  916. for (k = D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT; k < D3D12_PRIMITIVE_TOPOLOGY_TYPE_PATCH; ++k) {
  917. for (l = 0; l < SDL_arraysize(defaultRTVFormats); ++l) {
  918. if (!D3D12_CreatePipelineState(renderer, (D3D12_Shader) i, defaultBlendModes[j], (D3D12_PRIMITIVE_TOPOLOGY_TYPE) k, defaultRTVFormats[l])) {
  919. /* D3D12_CreatePipelineState will set the SDL error, if it fails */
  920. goto done;
  921. }
  922. }
  923. }
  924. }
  925. }
  926. /* Create default vertex buffers */
  927. for (i = 0; i < SDL_D3D12_NUM_VERTEX_BUFFERS; ++i) {
  928. D3D12_CreateVertexBuffer(data, i, D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT);
  929. }
  930. /* Create samplers to use when drawing textures: */
  931. SDL_zero(samplerDesc);
  932. samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
  933. samplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  934. samplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  935. samplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  936. samplerDesc.MipLODBias = 0.0f;
  937. samplerDesc.MaxAnisotropy = 1;
  938. samplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
  939. samplerDesc.MinLOD = 0.0f;
  940. samplerDesc.MaxLOD = D3D12_FLOAT32_MAX;
  941. D3D_CALL_RET(data->samplerDescriptorHeap, GetCPUDescriptorHandleForHeapStart, &data->nearestPixelSampler);
  942. D3D_CALL(data->d3dDevice, CreateSampler, &samplerDesc, data->nearestPixelSampler);
  943. samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
  944. data->linearSampler.ptr = data->nearestPixelSampler.ptr + data->samplerDescriptorSize;
  945. D3D_CALL(data->d3dDevice, CreateSampler, &samplerDesc, data->linearSampler);
  946. /* Initialize the pool allocator for SRVs */
  947. for (i = 0; i < SDL_D3D12_MAX_NUM_TEXTURES; ++i) {
  948. data->srvPoolNodes[i].index = (SIZE_T)i;
  949. if (i != SDL_D3D12_MAX_NUM_TEXTURES - 1) {
  950. data->srvPoolNodes[i].next = &data->srvPoolNodes[i + 1];
  951. }
  952. }
  953. data->srvPoolHead = &data->srvPoolNodes[0];
  954. done:
  955. SAFE_RELEASE(d3dDevice);
  956. return result;
  957. }
  958. static DXGI_MODE_ROTATION
  959. D3D12_GetCurrentRotation()
  960. {
  961. /* FIXME */
  962. return DXGI_MODE_ROTATION_IDENTITY;
  963. }
  964. static BOOL
  965. D3D12_IsDisplayRotated90Degrees(DXGI_MODE_ROTATION rotation)
  966. {
  967. switch (rotation) {
  968. case DXGI_MODE_ROTATION_ROTATE90:
  969. case DXGI_MODE_ROTATION_ROTATE270:
  970. return TRUE;
  971. default:
  972. return FALSE;
  973. }
  974. }
  975. static int
  976. D3D12_GetRotationForCurrentRenderTarget(SDL_Renderer * renderer)
  977. {
  978. D3D12_RenderData *data = (D3D12_RenderData *)renderer->driverdata;
  979. if (data->textureRenderTarget) {
  980. return DXGI_MODE_ROTATION_IDENTITY;
  981. } else {
  982. return data->rotation;
  983. }
  984. }
  985. static int
  986. D3D12_GetViewportAlignedD3DRect(SDL_Renderer * renderer, const SDL_Rect * sdlRect, D3D12_RECT * outRect, BOOL includeViewportOffset)
  987. {
  988. D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
  989. const int rotation = D3D12_GetRotationForCurrentRenderTarget(renderer);
  990. const SDL_Rect *viewport = &data->currentViewport;
  991. switch (rotation) {
  992. case DXGI_MODE_ROTATION_IDENTITY:
  993. outRect->left = sdlRect->x;
  994. outRect->right = sdlRect->x + sdlRect->w;
  995. outRect->top = sdlRect->y;
  996. outRect->bottom = sdlRect->y + sdlRect->h;
  997. if (includeViewportOffset) {
  998. outRect->left += viewport->x;
  999. outRect->right += viewport->x;
  1000. outRect->top += viewport->y;
  1001. outRect->bottom += viewport->y;
  1002. }
  1003. break;
  1004. case DXGI_MODE_ROTATION_ROTATE270:
  1005. outRect->left = sdlRect->y;
  1006. outRect->right = sdlRect->y + sdlRect->h;
  1007. outRect->top = viewport->w - sdlRect->x - sdlRect->w;
  1008. outRect->bottom = viewport->w - sdlRect->x;
  1009. break;
  1010. case DXGI_MODE_ROTATION_ROTATE180:
  1011. outRect->left = viewport->w - sdlRect->x - sdlRect->w;
  1012. outRect->right = viewport->w - sdlRect->x;
  1013. outRect->top = viewport->h - sdlRect->y - sdlRect->h;
  1014. outRect->bottom = viewport->h - sdlRect->y;
  1015. break;
  1016. case DXGI_MODE_ROTATION_ROTATE90:
  1017. outRect->left = viewport->h - sdlRect->y - sdlRect->h;
  1018. outRect->right = viewport->h - sdlRect->y;
  1019. outRect->top = sdlRect->x;
  1020. outRect->bottom = sdlRect->x + sdlRect->h;
  1021. break;
  1022. default:
  1023. return SDL_SetError("The physical display is in an unknown or unsupported rotation");
  1024. }
  1025. return 0;
  1026. }
  1027. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  1028. static HRESULT
  1029. D3D12_CreateSwapChain(SDL_Renderer * renderer, int w, int h)
  1030. {
  1031. D3D12_RenderData *data = (D3D12_RenderData *)renderer->driverdata;
  1032. IDXGISwapChain1* swapChain = NULL;
  1033. HRESULT result = S_OK;
  1034. SDL_SysWMinfo windowinfo;
  1035. /* Create a swap chain using the same adapter as the existing Direct3D device. */
  1036. DXGI_SWAP_CHAIN_DESC1 swapChainDesc;
  1037. SDL_zero(swapChainDesc);
  1038. swapChainDesc.Width = w;
  1039. swapChainDesc.Height = h;
  1040. swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; /* This is the most common swap chain format. */
  1041. swapChainDesc.Stereo = FALSE;
  1042. swapChainDesc.SampleDesc.Count = 1; /* Don't use multi-sampling. */
  1043. swapChainDesc.SampleDesc.Quality = 0;
  1044. swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  1045. swapChainDesc.BufferCount = 2; /* Use double-buffering to minimize latency. */
  1046. if (WIN_IsWindows8OrGreater()) {
  1047. swapChainDesc.Scaling = DXGI_SCALING_NONE;
  1048. } else {
  1049. swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
  1050. }
  1051. swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; /* All Windows Store apps must use this SwapEffect. */
  1052. swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT | /* To support SetMaximumFrameLatency */
  1053. DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING; /* To support presenting with allow tearing on */
  1054. if (SDL_GetWindowWMInfo(renderer->window, &windowinfo, SDL_SYSWM_CURRENT_VERSION) < 0 ||
  1055. windowinfo.subsystem != SDL_SYSWM_WINDOWS) {
  1056. SDL_SetError("Couldn't get window handle");
  1057. result = E_FAIL;
  1058. goto done;
  1059. }
  1060. result = D3D_CALL(data->dxgiFactory, CreateSwapChainForHwnd,
  1061. (IUnknown *)data->commandQueue,
  1062. windowinfo.info.win.window,
  1063. &swapChainDesc,
  1064. NULL,
  1065. NULL, /* Allow on all displays. */
  1066. &swapChain
  1067. );
  1068. if (FAILED(result)) {
  1069. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGIFactory2::CreateSwapChainForHwnd"), result);
  1070. goto done;
  1071. }
  1072. D3D_CALL(data->dxgiFactory, MakeWindowAssociation, windowinfo.info.win.window, DXGI_MWA_NO_WINDOW_CHANGES);
  1073. result = D3D_CALL(swapChain, QueryInterface, D3D_GUID(SDL_IID_IDXGISwapChain4), (void **)&data->swapChain);
  1074. if (FAILED(result)) {
  1075. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain1::QueryInterface"), result);
  1076. goto done;
  1077. }
  1078. /* Ensure that the swapchain does not queue more than one frame at a time. This both reduces latency
  1079. * and ensures that the application will only render after each VSync, minimizing power consumption.
  1080. */
  1081. result = D3D_CALL(data->swapChain, SetMaximumFrameLatency, 1);
  1082. if (FAILED(result)) {
  1083. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain4::SetMaximumFrameLatency"), result);
  1084. goto done;
  1085. }
  1086. data->swapEffect = swapChainDesc.SwapEffect;
  1087. data->swapFlags = swapChainDesc.Flags;
  1088. done:
  1089. SAFE_RELEASE(swapChain);
  1090. return result;
  1091. }
  1092. #endif
  1093. static HRESULT D3D12_UpdateForWindowSizeChange(SDL_Renderer * renderer);
  1094. HRESULT
  1095. D3D12_HandleDeviceLost(SDL_Renderer * renderer)
  1096. {
  1097. HRESULT result = S_OK;
  1098. D3D12_ReleaseAll(renderer);
  1099. result = D3D12_CreateDeviceResources(renderer);
  1100. if (FAILED(result)) {
  1101. /* D3D12_CreateDeviceResources will set the SDL error */
  1102. return result;
  1103. }
  1104. result = D3D12_UpdateForWindowSizeChange(renderer);
  1105. if (FAILED(result)) {
  1106. /* D3D12_UpdateForWindowSizeChange will set the SDL error */
  1107. return result;
  1108. }
  1109. /* Let the application know that the device has been reset */
  1110. {
  1111. SDL_Event event;
  1112. event.type = SDL_RENDER_DEVICE_RESET;
  1113. SDL_PushEvent(&event);
  1114. }
  1115. return S_OK;
  1116. }
  1117. /* Initialize all resources that change when the window's size changes. */
  1118. static HRESULT
  1119. D3D12_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
  1120. {
  1121. D3D12_RenderData *data = (D3D12_RenderData *)renderer->driverdata;
  1122. HRESULT result = S_OK;
  1123. int i, w, h;
  1124. D3D12_RENDER_TARGET_VIEW_DESC rtvDesc;
  1125. D3D12_CPU_DESCRIPTOR_HANDLE rtvDescriptor;
  1126. /* Release resources in the current command list */
  1127. D3D12_IssueBatch(data);
  1128. D3D_CALL(data->commandList, OMSetRenderTargets, 0, NULL, FALSE, NULL);
  1129. /* Release render targets */
  1130. for (i = 0; i < SDL_D3D12_NUM_BUFFERS; ++i) {
  1131. SAFE_RELEASE(data->renderTargets[i]);
  1132. }
  1133. /* The width and height of the swap chain must be based on the display's
  1134. * non-rotated size.
  1135. */
  1136. SDL_GetWindowSizeInPixels(renderer->window, &w, &h);
  1137. data->rotation = D3D12_GetCurrentRotation();
  1138. if (D3D12_IsDisplayRotated90Degrees(data->rotation)) {
  1139. int tmp = w;
  1140. w = h;
  1141. h = tmp;
  1142. }
  1143. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  1144. if (data->swapChain) {
  1145. /* If the swap chain already exists, resize it. */
  1146. result = D3D_CALL(data->swapChain, ResizeBuffers,
  1147. 0,
  1148. w, h,
  1149. DXGI_FORMAT_UNKNOWN,
  1150. data->swapFlags
  1151. );
  1152. if (result == DXGI_ERROR_DEVICE_REMOVED) {
  1153. /* If the device was removed for any reason, a new device and swap chain will need to be created. */
  1154. D3D12_HandleDeviceLost(renderer);
  1155. /* Everything is set up now. Do not continue execution of this method. HandleDeviceLost will reenter this method
  1156. * and correctly set up the new device.
  1157. */
  1158. goto done;
  1159. } else if (FAILED(result)) {
  1160. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::ResizeBuffers"), result);
  1161. goto done;
  1162. }
  1163. } else {
  1164. result = D3D12_CreateSwapChain(renderer, w, h);
  1165. if (FAILED(result)) {
  1166. goto done;
  1167. }
  1168. }
  1169. /* Set the proper rotation for the swap chain. */
  1170. if (WIN_IsWindows8OrGreater()) {
  1171. if (data->swapEffect == DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL) {
  1172. result = D3D_CALL(data->swapChain, SetRotation, data->rotation);
  1173. if (FAILED(result)) {
  1174. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain4::SetRotation"), result);
  1175. goto done;
  1176. }
  1177. }
  1178. }
  1179. #endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  1180. /* Get each back buffer render target and create render target views */
  1181. for (i = 0; i < SDL_D3D12_NUM_BUFFERS; ++i) {
  1182. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  1183. result = D3D12_XBOX_CreateBackBufferTarget(data->d3dDevice, renderer->window->w, renderer->window->h, (void **) &data->renderTargets[i]);
  1184. if (FAILED(result)) {
  1185. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("D3D12_XBOX_CreateBackBufferTarget"), result);
  1186. goto done;
  1187. }
  1188. #else
  1189. result = D3D_CALL(data->swapChain, GetBuffer,
  1190. i,
  1191. D3D_GUID(SDL_IID_ID3D12Resource),
  1192. (void **) &data->renderTargets[i]
  1193. );
  1194. if (FAILED(result)) {
  1195. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain4::GetBuffer"), result);
  1196. goto done;
  1197. }
  1198. #endif
  1199. SDL_zero(rtvDesc);
  1200. rtvDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  1201. rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
  1202. SDL_zero(rtvDescriptor);
  1203. D3D_CALL_RET(data->rtvDescriptorHeap, GetCPUDescriptorHandleForHeapStart, &rtvDescriptor);
  1204. rtvDescriptor.ptr += i * data->rtvDescriptorSize;
  1205. D3D_CALL(data->d3dDevice, CreateRenderTargetView, data->renderTargets[i], &rtvDesc, rtvDescriptor);
  1206. }
  1207. /* Set back buffer index to current buffer */
  1208. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  1209. data->currentBackBufferIndex = 0;
  1210. #else
  1211. data->currentBackBufferIndex = D3D_CALL(data->swapChain, GetCurrentBackBufferIndex);
  1212. #endif
  1213. /* Set the swap chain target immediately, so that a target is always set
  1214. * even before we get to SetDrawState. Without this it's possible to hit
  1215. * null references in places like ReadPixels!
  1216. */
  1217. data->currentRenderTargetView = D3D12_GetCurrentRenderTargetView(renderer);
  1218. D3D_CALL(data->commandList, OMSetRenderTargets, 1, &data->currentRenderTargetView, FALSE, NULL);
  1219. D3D12_TransitionResource(data,
  1220. data->renderTargets[data->currentBackBufferIndex],
  1221. D3D12_RESOURCE_STATE_PRESENT,
  1222. D3D12_RESOURCE_STATE_RENDER_TARGET
  1223. );
  1224. data->viewportDirty = SDL_TRUE;
  1225. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  1226. D3D12_XBOX_StartFrame(data->d3dDevice, &data->frameToken);
  1227. #endif
  1228. done:
  1229. return result;
  1230. }
  1231. /* This method is called when the window's size changes. */
  1232. static HRESULT
  1233. D3D12_UpdateForWindowSizeChange(SDL_Renderer * renderer)
  1234. {
  1235. D3D12_RenderData* data = (D3D12_RenderData*)renderer->driverdata;
  1236. /* If the GPU has previous work, wait for it to be done first */
  1237. D3D12_WaitForGPU(data);
  1238. return D3D12_CreateWindowSizeDependentResources(renderer);
  1239. }
  1240. static void
  1241. D3D12_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
  1242. {
  1243. if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
  1244. D3D12_UpdateForWindowSizeChange(renderer);
  1245. }
  1246. }
  1247. static SDL_bool
  1248. D3D12_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  1249. {
  1250. SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
  1251. SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
  1252. SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
  1253. SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
  1254. SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
  1255. SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
  1256. if (!GetBlendFunc(srcColorFactor) || !GetBlendFunc(srcAlphaFactor) ||
  1257. !GetBlendEquation(colorOperation) ||
  1258. !GetBlendFunc(dstColorFactor) || !GetBlendFunc(dstAlphaFactor) ||
  1259. !GetBlendEquation(alphaOperation)) {
  1260. return SDL_FALSE;
  1261. }
  1262. return SDL_TRUE;
  1263. }
  1264. static SIZE_T
  1265. D3D12_GetAvailableSRVIndex(SDL_Renderer * renderer)
  1266. {
  1267. D3D12_RenderData* rendererData = (D3D12_RenderData*)renderer->driverdata;
  1268. if (rendererData->srvPoolHead) {
  1269. SIZE_T index = rendererData->srvPoolHead->index;
  1270. rendererData->srvPoolHead = (D3D12_SRVPoolNode*)(rendererData->srvPoolHead->next);
  1271. return index;
  1272. } else {
  1273. SDL_SetError("[d3d12] Cannot allocate more than %d textures!", SDL_D3D12_MAX_NUM_TEXTURES);
  1274. return SDL_D3D12_MAX_NUM_TEXTURES + 1;
  1275. }
  1276. }
  1277. static void
  1278. D3D12_FreeSRVIndex(SDL_Renderer * renderer, SIZE_T index)
  1279. {
  1280. D3D12_RenderData* rendererData = (D3D12_RenderData*)renderer->driverdata;
  1281. rendererData->srvPoolNodes[index].next = rendererData->srvPoolHead;
  1282. rendererData->srvPoolHead = &rendererData->srvPoolNodes[index];
  1283. }
  1284. static int
  1285. D3D12_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  1286. {
  1287. D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
  1288. D3D12_TextureData *textureData;
  1289. HRESULT result;
  1290. DXGI_FORMAT textureFormat = SDLPixelFormatToDXGIFormat(texture->format);
  1291. D3D12_RESOURCE_DESC textureDesc;
  1292. D3D12_HEAP_PROPERTIES heapProps;
  1293. D3D12_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
  1294. if (textureFormat == DXGI_FORMAT_UNKNOWN) {
  1295. return SDL_SetError("%s, An unsupported SDL pixel format (0x%x) was specified",
  1296. __FUNCTION__, texture->format);
  1297. }
  1298. textureData = (D3D12_TextureData*) SDL_calloc(1, sizeof(*textureData));
  1299. if (!textureData) {
  1300. SDL_OutOfMemory();
  1301. return -1;
  1302. }
  1303. textureData->scaleMode = (texture->scaleMode == SDL_ScaleModeNearest) ? D3D12_FILTER_MIN_MAG_MIP_POINT : D3D12_FILTER_MIN_MAG_MIP_LINEAR;
  1304. texture->driverdata = textureData;
  1305. textureData->mainTextureFormat = textureFormat;
  1306. SDL_zero(textureDesc);
  1307. textureDesc.Width = texture->w;
  1308. textureDesc.Height = texture->h;
  1309. textureDesc.MipLevels = 1;
  1310. textureDesc.DepthOrArraySize = 1;
  1311. textureDesc.Format = textureFormat;
  1312. textureDesc.SampleDesc.Count = 1;
  1313. textureDesc.SampleDesc.Quality = 0;
  1314. textureDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  1315. textureDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
  1316. if (texture->access == SDL_TEXTUREACCESS_TARGET) {
  1317. textureDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
  1318. }
  1319. SDL_zero(heapProps);
  1320. heapProps.Type = D3D12_HEAP_TYPE_DEFAULT;
  1321. heapProps.CreationNodeMask = 1;
  1322. heapProps.VisibleNodeMask = 1;
  1323. result = D3D_CALL(rendererData->d3dDevice, CreateCommittedResource,
  1324. &heapProps,
  1325. D3D12_HEAP_FLAG_NONE,
  1326. &textureDesc,
  1327. D3D12_RESOURCE_STATE_COPY_DEST,
  1328. NULL,
  1329. D3D_GUID(SDL_IID_ID3D12Resource),
  1330. (void **)&textureData->mainTexture
  1331. );
  1332. textureData->mainResourceState = D3D12_RESOURCE_STATE_COPY_DEST;
  1333. if (FAILED(result)) {
  1334. D3D12_DestroyTexture(renderer, texture);
  1335. return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommittedResource [texture]"), result);
  1336. }
  1337. #if SDL_HAVE_YUV
  1338. if (texture->format == SDL_PIXELFORMAT_YV12 ||
  1339. texture->format == SDL_PIXELFORMAT_IYUV) {
  1340. textureData->yuv = SDL_TRUE;
  1341. textureDesc.Width = (textureDesc.Width + 1) / 2;
  1342. textureDesc.Height = (textureDesc.Height + 1) / 2;
  1343. result = D3D_CALL(rendererData->d3dDevice, CreateCommittedResource,
  1344. &heapProps,
  1345. D3D12_HEAP_FLAG_NONE,
  1346. &textureDesc,
  1347. D3D12_RESOURCE_STATE_COPY_DEST,
  1348. NULL,
  1349. D3D_GUID(SDL_IID_ID3D12Resource),
  1350. (void **)&textureData->mainTextureU
  1351. );
  1352. textureData->mainResourceStateU = D3D12_RESOURCE_STATE_COPY_DEST;
  1353. if (FAILED(result)) {
  1354. D3D12_DestroyTexture(renderer, texture);
  1355. return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommittedResource [texture]"), result);
  1356. }
  1357. result = D3D_CALL(rendererData->d3dDevice, CreateCommittedResource,
  1358. &heapProps,
  1359. D3D12_HEAP_FLAG_NONE,
  1360. &textureDesc,
  1361. D3D12_RESOURCE_STATE_COPY_DEST,
  1362. NULL,
  1363. D3D_GUID(SDL_IID_ID3D12Resource),
  1364. (void **)&textureData->mainTextureV
  1365. );
  1366. textureData->mainResourceStateV = D3D12_RESOURCE_STATE_COPY_DEST;
  1367. if (FAILED(result)) {
  1368. D3D12_DestroyTexture(renderer, texture);
  1369. return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommittedResource [texture]"), result);
  1370. }
  1371. }
  1372. if (texture->format == SDL_PIXELFORMAT_NV12 ||
  1373. texture->format == SDL_PIXELFORMAT_NV21) {
  1374. D3D12_RESOURCE_DESC nvTextureDesc = textureDesc;
  1375. textureData->nv12 = SDL_TRUE;
  1376. nvTextureDesc.Format = DXGI_FORMAT_R8G8_UNORM;
  1377. nvTextureDesc.Width = (textureDesc.Width + 1) / 2;
  1378. nvTextureDesc.Height = (textureDesc.Height + 1) / 2;
  1379. result = D3D_CALL(rendererData->d3dDevice, CreateCommittedResource,
  1380. &heapProps,
  1381. D3D12_HEAP_FLAG_NONE,
  1382. &nvTextureDesc,
  1383. D3D12_RESOURCE_STATE_COPY_DEST,
  1384. NULL,
  1385. D3D_GUID(SDL_IID_ID3D12Resource),
  1386. (void **)&textureData->mainTextureNV
  1387. );
  1388. textureData->mainResourceStateNV = D3D12_RESOURCE_STATE_COPY_DEST;
  1389. if (FAILED(result)) {
  1390. D3D12_DestroyTexture(renderer, texture);
  1391. return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateTexture2D"), result);
  1392. }
  1393. }
  1394. #endif /* SDL_HAVE_YUV */
  1395. SDL_zero(resourceViewDesc);
  1396. resourceViewDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  1397. resourceViewDesc.Format = textureDesc.Format;
  1398. resourceViewDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  1399. resourceViewDesc.Texture2D.MostDetailedMip = 0;
  1400. resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
  1401. textureData->mainSRVIndex = D3D12_GetAvailableSRVIndex(renderer);
  1402. D3D_CALL_RET(rendererData->srvDescriptorHeap, GetCPUDescriptorHandleForHeapStart, &textureData->mainTextureResourceView);
  1403. textureData->mainTextureResourceView.ptr += textureData->mainSRVIndex * rendererData->srvDescriptorSize;
  1404. D3D_CALL(rendererData->d3dDevice, CreateShaderResourceView,
  1405. textureData->mainTexture,
  1406. &resourceViewDesc,
  1407. textureData->mainTextureResourceView
  1408. );
  1409. #if SDL_HAVE_YUV
  1410. if (textureData->yuv) {
  1411. D3D_CALL_RET(rendererData->srvDescriptorHeap, GetCPUDescriptorHandleForHeapStart, &textureData->mainTextureResourceViewU);
  1412. textureData->mainSRVIndexU = D3D12_GetAvailableSRVIndex(renderer);
  1413. textureData->mainTextureResourceViewU.ptr += textureData->mainSRVIndexU * rendererData->srvDescriptorSize;
  1414. D3D_CALL(rendererData->d3dDevice, CreateShaderResourceView,
  1415. textureData->mainTextureU,
  1416. &resourceViewDesc,
  1417. textureData->mainTextureResourceViewU
  1418. );
  1419. D3D_CALL_RET(rendererData->srvDescriptorHeap, GetCPUDescriptorHandleForHeapStart, &textureData->mainTextureResourceViewV);
  1420. textureData->mainSRVIndexV = D3D12_GetAvailableSRVIndex(renderer);
  1421. textureData->mainTextureResourceViewV.ptr += textureData->mainSRVIndexV * rendererData->srvDescriptorSize;
  1422. D3D_CALL(rendererData->d3dDevice, CreateShaderResourceView,
  1423. textureData->mainTextureV,
  1424. &resourceViewDesc,
  1425. textureData->mainTextureResourceViewV
  1426. );
  1427. }
  1428. if (textureData->nv12) {
  1429. D3D12_SHADER_RESOURCE_VIEW_DESC nvResourceViewDesc = resourceViewDesc;
  1430. nvResourceViewDesc.Format = DXGI_FORMAT_R8G8_UNORM;
  1431. D3D_CALL_RET(rendererData->srvDescriptorHeap, GetCPUDescriptorHandleForHeapStart, &textureData->mainTextureResourceViewNV);
  1432. textureData->mainSRVIndexNV = D3D12_GetAvailableSRVIndex(renderer);
  1433. textureData->mainTextureResourceViewNV.ptr += textureData->mainSRVIndexNV * rendererData->srvDescriptorSize;
  1434. D3D_CALL(rendererData->d3dDevice, CreateShaderResourceView,
  1435. textureData->mainTextureNV,
  1436. &nvResourceViewDesc,
  1437. textureData->mainTextureResourceViewNV
  1438. );
  1439. }
  1440. #endif /* SDL_HAVE_YUV */
  1441. if (texture->access & SDL_TEXTUREACCESS_TARGET) {
  1442. D3D12_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
  1443. SDL_zero(renderTargetViewDesc);
  1444. renderTargetViewDesc.Format = textureDesc.Format;
  1445. renderTargetViewDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
  1446. renderTargetViewDesc.Texture2D.MipSlice = 0;
  1447. D3D_CALL_RET(rendererData->textureRTVDescriptorHeap, GetCPUDescriptorHandleForHeapStart, &textureData->mainTextureRenderTargetView);
  1448. textureData->mainTextureRenderTargetView.ptr += textureData->mainSRVIndex * rendererData->rtvDescriptorSize;
  1449. D3D_CALL(rendererData->d3dDevice, CreateRenderTargetView,
  1450. (ID3D12Resource*)textureData->mainTexture,
  1451. &renderTargetViewDesc,
  1452. textureData->mainTextureRenderTargetView);
  1453. }
  1454. return 0;
  1455. }
  1456. static void
  1457. D3D12_DestroyTexture(SDL_Renderer * renderer,
  1458. SDL_Texture * texture)
  1459. {
  1460. D3D12_RenderData *rendererData = (D3D12_RenderData*)renderer->driverdata;
  1461. D3D12_TextureData *textureData = (D3D12_TextureData *)texture->driverdata;
  1462. if (!textureData) {
  1463. return;
  1464. }
  1465. /* Because SDL_DestroyTexture might be called while the data is in-flight, we need to issue the batch first
  1466. Unfortunately, this means that deleting a lot of textures mid-frame will have poor performance. */
  1467. D3D12_IssueBatch(rendererData);
  1468. SAFE_RELEASE(textureData->mainTexture);
  1469. SAFE_RELEASE(textureData->stagingBuffer);
  1470. D3D12_FreeSRVIndex(renderer, textureData->mainSRVIndex);
  1471. #if SDL_HAVE_YUV
  1472. SAFE_RELEASE(textureData->mainTextureU);
  1473. SAFE_RELEASE(textureData->mainTextureV);
  1474. if (textureData->yuv) {
  1475. D3D12_FreeSRVIndex(renderer, textureData->mainSRVIndexU);
  1476. D3D12_FreeSRVIndex(renderer, textureData->mainSRVIndexV);
  1477. }
  1478. SAFE_RELEASE(textureData->mainTextureNV);
  1479. if (textureData->yuv) {
  1480. D3D12_FreeSRVIndex(renderer, textureData->mainSRVIndexNV);
  1481. }
  1482. SDL_free(textureData->pixels);
  1483. #endif
  1484. SDL_free(textureData);
  1485. texture->driverdata = NULL;
  1486. }
  1487. static int
  1488. D3D12_UpdateTextureInternal(D3D12_RenderData * rendererData, ID3D12Resource * texture, int bpp, int x, int y, int w, int h, const void *pixels, int pitch, D3D12_RESOURCE_STATES *resourceState)
  1489. {
  1490. const Uint8 *src;
  1491. Uint8 *dst;
  1492. int row;
  1493. UINT length;
  1494. HRESULT result;
  1495. D3D12_RESOURCE_DESC textureDesc;
  1496. D3D12_RESOURCE_DESC uploadDesc;
  1497. D3D12_HEAP_PROPERTIES heapProps;
  1498. D3D12_SUBRESOURCE_FOOTPRINT pitchedDesc;
  1499. D3D12_PLACED_SUBRESOURCE_FOOTPRINT placedTextureDesc;
  1500. D3D12_TEXTURE_COPY_LOCATION srcLocation;
  1501. D3D12_TEXTURE_COPY_LOCATION dstLocation;
  1502. BYTE *textureMemory;
  1503. ID3D12Resource *uploadBuffer;
  1504. /* Create an upload buffer, which will be used to write to the main texture. */
  1505. SDL_zero(textureDesc);
  1506. D3D_CALL_RET(texture, GetDesc, &textureDesc);
  1507. textureDesc.Width = w;
  1508. textureDesc.Height = h;
  1509. SDL_zero(uploadDesc);
  1510. uploadDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  1511. uploadDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
  1512. uploadDesc.Height = 1;
  1513. uploadDesc.DepthOrArraySize = 1;
  1514. uploadDesc.MipLevels = 1;
  1515. uploadDesc.Format = DXGI_FORMAT_UNKNOWN;
  1516. uploadDesc.SampleDesc.Count = 1;
  1517. uploadDesc.SampleDesc.Quality = 0;
  1518. uploadDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  1519. uploadDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
  1520. /* Figure out how much we need to allocate for the upload buffer */
  1521. D3D_CALL(rendererData->d3dDevice, GetCopyableFootprints,
  1522. &textureDesc,
  1523. 0,
  1524. 1,
  1525. 0,
  1526. NULL,
  1527. NULL,
  1528. NULL,
  1529. &uploadDesc.Width
  1530. );
  1531. SDL_zero(heapProps);
  1532. heapProps.Type = D3D12_HEAP_TYPE_UPLOAD;
  1533. heapProps.CreationNodeMask = 1;
  1534. heapProps.VisibleNodeMask = 1;
  1535. /* Create the upload buffer */
  1536. result = D3D_CALL(rendererData->d3dDevice, CreateCommittedResource,
  1537. &heapProps,
  1538. D3D12_HEAP_FLAG_NONE,
  1539. &uploadDesc,
  1540. D3D12_RESOURCE_STATE_GENERIC_READ,
  1541. NULL,
  1542. D3D_GUID(SDL_IID_ID3D12Resource),
  1543. (void **)&rendererData->uploadBuffers[rendererData->currentUploadBuffer]
  1544. );
  1545. if (FAILED(result)) {
  1546. return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommittedResource [create upload buffer]"), result);
  1547. }
  1548. /* Get a write-only pointer to data in the upload buffer: */
  1549. uploadBuffer = rendererData->uploadBuffers[rendererData->currentUploadBuffer];
  1550. result = D3D_CALL(uploadBuffer, Map,
  1551. 0,
  1552. NULL,
  1553. (void **)&textureMemory
  1554. );
  1555. if (FAILED(result)) {
  1556. SAFE_RELEASE(rendererData->uploadBuffers[rendererData->currentUploadBuffer]);
  1557. return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Resource::Map [map staging texture]"), result);
  1558. }
  1559. SDL_zero(pitchedDesc);
  1560. pitchedDesc.Format = textureDesc.Format;
  1561. pitchedDesc.Width = w;
  1562. pitchedDesc.Height = h;
  1563. pitchedDesc.Depth = 1;
  1564. pitchedDesc.RowPitch = D3D12_Align(w * bpp, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
  1565. SDL_zero(placedTextureDesc);
  1566. placedTextureDesc.Offset = 0;
  1567. placedTextureDesc.Footprint = pitchedDesc;
  1568. src = (const Uint8 *)pixels;
  1569. dst = textureMemory;
  1570. length = w * bpp;
  1571. if (length == pitch && length == pitchedDesc.RowPitch) {
  1572. SDL_memcpy(dst, src, length*h);
  1573. } else {
  1574. if (length > (UINT)pitch) {
  1575. length = pitch;
  1576. }
  1577. if (length > pitchedDesc.RowPitch) {
  1578. length = pitchedDesc.RowPitch;
  1579. }
  1580. for (row = 0; row < h; ++row) {
  1581. SDL_memcpy(dst, src, length);
  1582. src += pitch;
  1583. dst += pitchedDesc.RowPitch;
  1584. }
  1585. }
  1586. /* Commit the changes back to the upload buffer: */
  1587. D3D_CALL(uploadBuffer, Unmap, 0, NULL);
  1588. /* Make sure the destination is in the correct resource state */
  1589. D3D12_TransitionResource(rendererData, texture, *resourceState, D3D12_RESOURCE_STATE_COPY_DEST);
  1590. *resourceState = D3D12_RESOURCE_STATE_COPY_DEST;
  1591. SDL_zero(dstLocation);
  1592. dstLocation.pResource = texture;
  1593. dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  1594. dstLocation.SubresourceIndex = 0;
  1595. SDL_zero(srcLocation);
  1596. srcLocation.pResource = rendererData->uploadBuffers[rendererData->currentUploadBuffer];
  1597. srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
  1598. srcLocation.PlacedFootprint = placedTextureDesc;
  1599. D3D_CALL(rendererData->commandList, CopyTextureRegion,
  1600. &dstLocation,
  1601. x,
  1602. y,
  1603. 0,
  1604. &srcLocation,
  1605. NULL
  1606. );
  1607. /* Transition the texture to be shader accessible */
  1608. D3D12_TransitionResource(rendererData, texture, *resourceState, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
  1609. *resourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  1610. rendererData->currentUploadBuffer++;
  1611. /* If we've used up all the upload buffers, we need to issue the batch */
  1612. if (rendererData->currentUploadBuffer == SDL_D3D12_NUM_UPLOAD_BUFFERS) {
  1613. D3D12_IssueBatch(rendererData);
  1614. }
  1615. return 0;
  1616. }
  1617. static int
  1618. D3D12_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  1619. const SDL_Rect * rect, const void * srcPixels,
  1620. int srcPitch)
  1621. {
  1622. D3D12_RenderData *rendererData = (D3D12_RenderData *)renderer->driverdata;
  1623. D3D12_TextureData *textureData = (D3D12_TextureData *)texture->driverdata;
  1624. if (!textureData) {
  1625. return SDL_SetError("Texture is not currently available");
  1626. }
  1627. if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, SDL_BYTESPERPIXEL(texture->format), rect->x, rect->y, rect->w, rect->h, srcPixels, srcPitch, &textureData->mainResourceState) < 0) {
  1628. return -1;
  1629. }
  1630. #if SDL_HAVE_YUV
  1631. if (textureData->yuv) {
  1632. /* Skip to the correct offset into the next texture */
  1633. srcPixels = (const void*)((const Uint8*)srcPixels + rect->h * srcPitch);
  1634. if (D3D12_UpdateTextureInternal(rendererData, texture->format == SDL_PIXELFORMAT_YV12 ? textureData->mainTextureV : textureData->mainTextureU, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, srcPixels, (srcPitch + 1) / 2, texture->format == SDL_PIXELFORMAT_YV12 ? &textureData->mainResourceStateV : &textureData->mainResourceStateU) < 0) {
  1635. return -1;
  1636. }
  1637. /* Skip to the correct offset into the next texture */
  1638. srcPixels = (const void*)((const Uint8*)srcPixels + ((rect->h + 1) / 2) * ((srcPitch + 1) / 2));
  1639. if (D3D12_UpdateTextureInternal(rendererData, texture->format == SDL_PIXELFORMAT_YV12 ? textureData->mainTextureU : textureData->mainTextureV, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, srcPixels, (srcPitch + 1) / 2, texture->format == SDL_PIXELFORMAT_YV12 ? &textureData->mainResourceStateU : &textureData->mainResourceStateV) < 0) {
  1640. return -1;
  1641. }
  1642. }
  1643. if (textureData->nv12) {
  1644. /* Skip to the correct offset into the next texture */
  1645. srcPixels = (const void*)((const Uint8*)srcPixels + rect->h * srcPitch);
  1646. if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureNV, 2, rect->x / 2, rect->y / 2, ((rect->w + 1) / 2), (rect->h + 1) / 2, srcPixels, 2*((srcPitch + 1) / 2), &textureData->mainResourceStateNV) < 0) {
  1647. return -1;
  1648. }
  1649. }
  1650. #endif /* SDL_HAVE_YUV */
  1651. return 0;
  1652. }
  1653. #if SDL_HAVE_YUV
  1654. static int
  1655. D3D12_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
  1656. const SDL_Rect * rect,
  1657. const Uint8 *Yplane, int Ypitch,
  1658. const Uint8 *Uplane, int Upitch,
  1659. const Uint8 *Vplane, int Vpitch)
  1660. {
  1661. D3D12_RenderData *rendererData = (D3D12_RenderData *)renderer->driverdata;
  1662. D3D12_TextureData *textureData = (D3D12_TextureData *)texture->driverdata;
  1663. if (!textureData) {
  1664. return SDL_SetError("Texture is not currently available");
  1665. }
  1666. if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, SDL_BYTESPERPIXEL(texture->format), rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch, &textureData->mainResourceState) < 0) {
  1667. return -1;
  1668. }
  1669. if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureU, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Uplane, Upitch, &textureData->mainResourceStateU) < 0) {
  1670. return -1;
  1671. }
  1672. if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureV, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Vplane, Vpitch, &textureData->mainResourceStateV) < 0) {
  1673. return -1;
  1674. }
  1675. return 0;
  1676. }
  1677. static int
  1678. D3D12_UpdateTextureNV(SDL_Renderer * renderer, SDL_Texture * texture,
  1679. const SDL_Rect * rect,
  1680. const Uint8 *Yplane, int Ypitch,
  1681. const Uint8 *UVplane, int UVpitch)
  1682. {
  1683. D3D12_RenderData *rendererData = (D3D12_RenderData *)renderer->driverdata;
  1684. D3D12_TextureData *textureData = (D3D12_TextureData *)texture->driverdata;
  1685. if (!textureData) {
  1686. return SDL_SetError("Texture is not currently available");
  1687. }
  1688. if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, SDL_BYTESPERPIXEL(texture->format), rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch, &textureData->mainResourceState) < 0) {
  1689. return -1;
  1690. }
  1691. if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureNV, 2, rect->x / 2, rect->y / 2, ((rect->w + 1) / 2), (rect->h + 1) / 2, UVplane, UVpitch, &textureData->mainResourceStateNV) < 0) {
  1692. return -1;
  1693. }
  1694. return 0;
  1695. }
  1696. #endif
  1697. static int
  1698. D3D12_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  1699. const SDL_Rect * rect, void **pixels, int *pitch)
  1700. {
  1701. D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
  1702. D3D12_TextureData *textureData = (D3D12_TextureData *) texture->driverdata;
  1703. HRESULT result = S_OK;
  1704. D3D12_RESOURCE_DESC textureDesc;
  1705. D3D12_RESOURCE_DESC uploadDesc;
  1706. D3D12_HEAP_PROPERTIES heapProps;
  1707. D3D12_SUBRESOURCE_FOOTPRINT pitchedDesc;
  1708. BYTE *textureMemory;
  1709. int bpp;
  1710. if (!textureData) {
  1711. return SDL_SetError("Texture is not currently available");
  1712. }
  1713. #if SDL_HAVE_YUV
  1714. if (textureData->yuv || textureData->nv12) {
  1715. /* It's more efficient to upload directly... */
  1716. if (!textureData->pixels) {
  1717. textureData->pitch = texture->w;
  1718. textureData->pixels = (Uint8 *)SDL_malloc((texture->h * textureData->pitch * 3) / 2);
  1719. if (!textureData->pixels) {
  1720. return SDL_OutOfMemory();
  1721. }
  1722. }
  1723. textureData->lockedRect = *rect;
  1724. *pixels =
  1725. (void *)((Uint8 *)textureData->pixels + rect->y * textureData->pitch +
  1726. rect->x * SDL_BYTESPERPIXEL(texture->format));
  1727. *pitch = textureData->pitch;
  1728. return 0;
  1729. }
  1730. #endif
  1731. if (textureData->stagingBuffer) {
  1732. return SDL_SetError("texture is already locked");
  1733. }
  1734. /* Create an upload buffer, which will be used to write to the main texture. */
  1735. SDL_zero(textureDesc);
  1736. D3D_CALL_RET(textureData->mainTexture, GetDesc, &textureDesc);
  1737. textureDesc.Width = rect->w;
  1738. textureDesc.Height = rect->h;
  1739. SDL_zero(uploadDesc);
  1740. uploadDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  1741. uploadDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
  1742. uploadDesc.Height = 1;
  1743. uploadDesc.DepthOrArraySize = 1;
  1744. uploadDesc.MipLevels = 1;
  1745. uploadDesc.Format = DXGI_FORMAT_UNKNOWN;
  1746. uploadDesc.SampleDesc.Count = 1;
  1747. uploadDesc.SampleDesc.Quality = 0;
  1748. uploadDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  1749. uploadDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
  1750. /* Figure out how much we need to allocate for the upload buffer */
  1751. D3D_CALL(rendererData->d3dDevice, GetCopyableFootprints,
  1752. &textureDesc,
  1753. 0,
  1754. 1,
  1755. 0,
  1756. NULL,
  1757. NULL,
  1758. NULL,
  1759. &uploadDesc.Width
  1760. );
  1761. SDL_zero(heapProps);
  1762. heapProps.Type = D3D12_HEAP_TYPE_UPLOAD;
  1763. heapProps.CreationNodeMask = 1;
  1764. heapProps.VisibleNodeMask = 1;
  1765. /* Create the upload buffer */
  1766. result = D3D_CALL(rendererData->d3dDevice, CreateCommittedResource,
  1767. &heapProps,
  1768. D3D12_HEAP_FLAG_NONE,
  1769. &uploadDesc,
  1770. D3D12_RESOURCE_STATE_GENERIC_READ,
  1771. NULL,
  1772. D3D_GUID(SDL_IID_ID3D12Resource),
  1773. (void **)&textureData->stagingBuffer
  1774. );
  1775. if (FAILED(result)) {
  1776. return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommittedResource [create upload buffer]"), result);
  1777. }
  1778. /* Get a write-only pointer to data in the upload buffer: */
  1779. result = D3D_CALL(textureData->stagingBuffer, Map,
  1780. 0,
  1781. NULL,
  1782. (void **)&textureMemory
  1783. );
  1784. if (FAILED(result)) {
  1785. SAFE_RELEASE(rendererData->uploadBuffers[rendererData->currentUploadBuffer]);
  1786. return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Resource::Map [map staging texture]"), result);
  1787. }
  1788. SDL_zero(pitchedDesc);
  1789. pitchedDesc.Format = textureDesc.Format;
  1790. pitchedDesc.Width = rect->w;
  1791. pitchedDesc.Height = rect->h;
  1792. pitchedDesc.Depth = 1;
  1793. if (pitchedDesc.Format == DXGI_FORMAT_R8_UNORM) {
  1794. bpp = 1;
  1795. }
  1796. else {
  1797. bpp = 4;
  1798. }
  1799. pitchedDesc.RowPitch = D3D12_Align(rect->w * bpp, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
  1800. /* Make note of where the staging texture will be written to
  1801. * (on a call to SDL_UnlockTexture):
  1802. */
  1803. textureData->lockedRect = *rect;
  1804. /* Make sure the caller has information on the texture's pixel buffer,
  1805. * then return:
  1806. */
  1807. *pixels = textureMemory;
  1808. *pitch = pitchedDesc.RowPitch;
  1809. return 0;
  1810. }
  1811. static void
  1812. D3D12_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  1813. {
  1814. D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
  1815. D3D12_TextureData *textureData = (D3D12_TextureData *) texture->driverdata;
  1816. D3D12_RESOURCE_DESC textureDesc;
  1817. D3D12_SUBRESOURCE_FOOTPRINT pitchedDesc;
  1818. D3D12_PLACED_SUBRESOURCE_FOOTPRINT placedTextureDesc;
  1819. D3D12_TEXTURE_COPY_LOCATION srcLocation;
  1820. D3D12_TEXTURE_COPY_LOCATION dstLocation;
  1821. int bpp;
  1822. if (!textureData) {
  1823. return;
  1824. }
  1825. #if SDL_HAVE_YUV
  1826. if (textureData->yuv || textureData->nv12) {
  1827. const SDL_Rect *rect = &textureData->lockedRect;
  1828. void *pixels =
  1829. (void *) ((Uint8 *) textureData->pixels + rect->y * textureData->pitch +
  1830. rect->x * SDL_BYTESPERPIXEL(texture->format));
  1831. D3D12_UpdateTexture(renderer, texture, rect, pixels, textureData->pitch);
  1832. return;
  1833. }
  1834. #endif
  1835. /* Commit the pixel buffer's changes back to the staging texture: */
  1836. D3D_CALL(textureData->stagingBuffer, Unmap, 0, NULL);
  1837. SDL_zero(textureDesc);
  1838. D3D_CALL_RET(textureData->mainTexture, GetDesc, &textureDesc);
  1839. textureDesc.Width = textureData->lockedRect.w;
  1840. textureDesc.Height = textureData->lockedRect.h;
  1841. SDL_zero(pitchedDesc);
  1842. pitchedDesc.Format = textureDesc.Format;
  1843. pitchedDesc.Width = (UINT)textureDesc.Width;
  1844. pitchedDesc.Height = textureDesc.Height;
  1845. pitchedDesc.Depth = 1;
  1846. if (pitchedDesc.Format == DXGI_FORMAT_R8_UNORM) {
  1847. bpp = 1;
  1848. }
  1849. else {
  1850. bpp = 4;
  1851. }
  1852. pitchedDesc.RowPitch = D3D12_Align(textureData->lockedRect.w * bpp, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
  1853. SDL_zero(placedTextureDesc);
  1854. placedTextureDesc.Offset = 0;
  1855. placedTextureDesc.Footprint = pitchedDesc;
  1856. D3D12_TransitionResource(rendererData, textureData->mainTexture, textureData->mainResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
  1857. textureData->mainResourceState = D3D12_RESOURCE_STATE_COPY_DEST;
  1858. SDL_zero(dstLocation);
  1859. dstLocation.pResource = textureData->mainTexture;
  1860. dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  1861. dstLocation.SubresourceIndex = 0;
  1862. SDL_zero(srcLocation);
  1863. srcLocation.pResource = textureData->stagingBuffer;
  1864. srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
  1865. srcLocation.PlacedFootprint = placedTextureDesc;
  1866. D3D_CALL(rendererData->commandList, CopyTextureRegion,
  1867. &dstLocation,
  1868. textureData->lockedRect.x,
  1869. textureData->lockedRect.y,
  1870. 0,
  1871. &srcLocation,
  1872. NULL
  1873. );
  1874. /* Transition the texture to be shader accessible */
  1875. D3D12_TransitionResource(rendererData, textureData->mainTexture, textureData->mainResourceState, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
  1876. textureData->mainResourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  1877. /* Execute the command list before releasing the staging buffer */
  1878. D3D12_IssueBatch(rendererData);
  1879. SAFE_RELEASE(textureData->stagingBuffer);
  1880. }
  1881. static void
  1882. D3D12_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture, SDL_ScaleMode scaleMode)
  1883. {
  1884. D3D12_TextureData *textureData = (D3D12_TextureData *) texture->driverdata;
  1885. if (!textureData) {
  1886. return;
  1887. }
  1888. textureData->scaleMode = (scaleMode == SDL_ScaleModeNearest) ? D3D12_FILTER_MIN_MAG_MIP_POINT : D3D12_FILTER_MIN_MAG_MIP_LINEAR;
  1889. }
  1890. static int
  1891. D3D12_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
  1892. {
  1893. D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
  1894. D3D12_TextureData *textureData = NULL;
  1895. if (texture == NULL) {
  1896. if (rendererData->textureRenderTarget) {
  1897. D3D12_TransitionResource(rendererData,
  1898. rendererData->textureRenderTarget->mainTexture,
  1899. rendererData->textureRenderTarget->mainResourceState,
  1900. D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
  1901. );
  1902. rendererData->textureRenderTarget->mainResourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  1903. }
  1904. rendererData->textureRenderTarget = NULL;
  1905. return 0;
  1906. }
  1907. textureData = (D3D12_TextureData *) texture->driverdata;
  1908. if (!textureData->mainTextureRenderTargetView.ptr) {
  1909. return SDL_SetError("specified texture is not a render target");
  1910. }
  1911. rendererData->textureRenderTarget = textureData;
  1912. D3D12_TransitionResource(rendererData,
  1913. rendererData->textureRenderTarget->mainTexture,
  1914. rendererData->textureRenderTarget->mainResourceState,
  1915. D3D12_RESOURCE_STATE_RENDER_TARGET
  1916. );
  1917. rendererData->textureRenderTarget->mainResourceState = D3D12_RESOURCE_STATE_RENDER_TARGET;
  1918. return 0;
  1919. }
  1920. static int
  1921. D3D12_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
  1922. {
  1923. return 0; /* nothing to do in this backend. */
  1924. }
  1925. static int
  1926. D3D12_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
  1927. {
  1928. VertexPositionColor *verts = (VertexPositionColor *) SDL_AllocateRenderVertices(renderer, count * sizeof (VertexPositionColor), 0, &cmd->data.draw.first);
  1929. int i;
  1930. SDL_Color color;
  1931. color.r = cmd->data.draw.r;
  1932. color.g = cmd->data.draw.g;
  1933. color.b = cmd->data.draw.b;
  1934. color.a = cmd->data.draw.a;
  1935. if (!verts) {
  1936. return -1;
  1937. }
  1938. cmd->data.draw.count = count;
  1939. for (i = 0; i < count; i++) {
  1940. verts->pos.x = points[i].x + 0.5f;
  1941. verts->pos.y = points[i].y + 0.5f;
  1942. verts->tex.x = 0.0f;
  1943. verts->tex.y = 0.0f;
  1944. verts->color = color;
  1945. verts++;
  1946. }
  1947. return 0;
  1948. }
  1949. static int
  1950. D3D12_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
  1951. const float *xy, int xy_stride, const SDL_Color *color, int color_stride, const float *uv, int uv_stride,
  1952. int num_vertices, const void *indices, int num_indices, int size_indices,
  1953. float scale_x, float scale_y)
  1954. {
  1955. int i;
  1956. int count = indices ? num_indices : num_vertices;
  1957. VertexPositionColor *verts = (VertexPositionColor *) SDL_AllocateRenderVertices(renderer, count * sizeof (VertexPositionColor), 0, &cmd->data.draw.first);
  1958. if (!verts) {
  1959. return -1;
  1960. }
  1961. cmd->data.draw.count = count;
  1962. size_indices = indices ? size_indices : 0;
  1963. for (i = 0; i < count; i++) {
  1964. int j;
  1965. float *xy_;
  1966. if (size_indices == 4) {
  1967. j = ((const Uint32 *)indices)[i];
  1968. } else if (size_indices == 2) {
  1969. j = ((const Uint16 *)indices)[i];
  1970. } else if (size_indices == 1) {
  1971. j = ((const Uint8 *)indices)[i];
  1972. } else {
  1973. j = i;
  1974. }
  1975. xy_ = (float *)((char*)xy + j * xy_stride);
  1976. verts->pos.x = xy_[0] * scale_x;
  1977. verts->pos.y = xy_[1] * scale_y;
  1978. verts->color = *(SDL_Color*)((char*)color + j * color_stride);
  1979. if (texture) {
  1980. float *uv_ = (float *)((char*)uv + j * uv_stride);
  1981. verts->tex.x = uv_[0];
  1982. verts->tex.y = uv_[1];
  1983. } else {
  1984. verts->tex.x = 0.0f;
  1985. verts->tex.y = 0.0f;
  1986. }
  1987. verts += 1;
  1988. }
  1989. return 0;
  1990. }
  1991. static int
  1992. D3D12_UpdateVertexBuffer(SDL_Renderer *renderer,
  1993. const void * vertexData, size_t dataSizeInBytes)
  1994. {
  1995. D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
  1996. HRESULT result = S_OK;
  1997. const int vbidx = rendererData->currentVertexBuffer;
  1998. UINT8* vertexBufferData = NULL;
  1999. D3D12_RANGE range;
  2000. ID3D12Resource *vertexBuffer;
  2001. range.Begin = 0;
  2002. range.End = 0;
  2003. if (dataSizeInBytes == 0) {
  2004. return 0; /* nothing to do. */
  2005. }
  2006. if (rendererData->issueBatch) {
  2007. if (FAILED(D3D12_IssueBatch(rendererData))) {
  2008. SDL_SetError("Failed to issue intermediate batch");
  2009. return E_FAIL;
  2010. }
  2011. }
  2012. /* If the existing vertex buffer isn't big enough, we need to recreate a big enough one */
  2013. if (dataSizeInBytes > rendererData->vertexBuffers[vbidx].size) {
  2014. D3D12_CreateVertexBuffer(rendererData, vbidx, dataSizeInBytes);
  2015. }
  2016. vertexBuffer = rendererData->vertexBuffers[vbidx].resource;
  2017. result = D3D_CALL(vertexBuffer, Map, 0, &range, (void **)&vertexBufferData);
  2018. if (FAILED(result)) {
  2019. return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Resource::Map [vertex buffer]"), result);
  2020. }
  2021. SDL_memcpy(vertexBufferData, vertexData, dataSizeInBytes);
  2022. D3D_CALL(vertexBuffer, Unmap, 0, NULL);
  2023. rendererData->vertexBuffers[vbidx].view.SizeInBytes = (UINT)dataSizeInBytes;
  2024. D3D_CALL(rendererData->commandList, IASetVertexBuffers, 0, 1, &rendererData->vertexBuffers[vbidx].view);
  2025. rendererData->currentVertexBuffer++;
  2026. if (rendererData->currentVertexBuffer >= SDL_D3D12_NUM_VERTEX_BUFFERS) {
  2027. rendererData->currentVertexBuffer = 0;
  2028. rendererData->issueBatch = SDL_TRUE;
  2029. }
  2030. return S_OK;
  2031. }
  2032. static int
  2033. D3D12_UpdateViewport(SDL_Renderer * renderer)
  2034. {
  2035. D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
  2036. const SDL_Rect *viewport = &data->currentViewport;
  2037. Float4X4 projection;
  2038. Float4X4 view;
  2039. SDL_FRect orientationAlignedViewport;
  2040. BOOL swapDimensions;
  2041. D3D12_VIEWPORT d3dviewport;
  2042. const int rotation = D3D12_GetRotationForCurrentRenderTarget(renderer);
  2043. if (viewport->w == 0 || viewport->h == 0) {
  2044. /* If the viewport is empty, assume that it is because
  2045. * SDL_CreateRenderer is calling it, and will call it again later
  2046. * with a non-empty viewport.
  2047. */
  2048. /* SDL_Log("%s, no viewport was set!\n", __FUNCTION__); */
  2049. return -1;
  2050. }
  2051. /* Make sure the SDL viewport gets rotated to that of the physical display's rotation.
  2052. * Keep in mind here that the Y-axis will be been inverted (from Direct3D's
  2053. * default coordinate system) so rotations will be done in the opposite
  2054. * direction of the DXGI_MODE_ROTATION enumeration.
  2055. */
  2056. switch (rotation) {
  2057. case DXGI_MODE_ROTATION_IDENTITY:
  2058. projection = MatrixIdentity();
  2059. break;
  2060. case DXGI_MODE_ROTATION_ROTATE270:
  2061. projection = MatrixRotationZ(SDL_static_cast(float, M_PI * 0.5f));
  2062. break;
  2063. case DXGI_MODE_ROTATION_ROTATE180:
  2064. projection = MatrixRotationZ(SDL_static_cast(float, M_PI));
  2065. break;
  2066. case DXGI_MODE_ROTATION_ROTATE90:
  2067. projection = MatrixRotationZ(SDL_static_cast(float, -M_PI * 0.5f));
  2068. break;
  2069. default:
  2070. return SDL_SetError("An unknown DisplayOrientation is being used");
  2071. }
  2072. /* Update the view matrix */
  2073. SDL_zero(view);
  2074. view.m[0][0] = 2.0f / viewport->w;
  2075. view.m[1][1] = -2.0f / viewport->h;
  2076. view.m[2][2] = 1.0f;
  2077. view.m[3][0] = -1.0f;
  2078. view.m[3][1] = 1.0f;
  2079. view.m[3][3] = 1.0f;
  2080. /* Combine the projection + view matrix together now, as both only get
  2081. * set here (as of this writing, on Dec 26, 2013). When done, store it
  2082. * for eventual transfer to the GPU.
  2083. */
  2084. data->vertexShaderConstantsData.projectionAndView = MatrixMultiply(
  2085. view,
  2086. projection);
  2087. /* Update the Direct3D viewport, which seems to be aligned to the
  2088. * swap buffer's coordinate space, which is always in either
  2089. * a landscape mode, for all Windows 8/RT devices, or a portrait mode,
  2090. * for Windows Phone devices.
  2091. */
  2092. swapDimensions = D3D12_IsDisplayRotated90Degrees((DXGI_MODE_ROTATION) rotation);
  2093. if (swapDimensions) {
  2094. orientationAlignedViewport.x = (float) viewport->y;
  2095. orientationAlignedViewport.y = (float) viewport->x;
  2096. orientationAlignedViewport.w = (float) viewport->h;
  2097. orientationAlignedViewport.h = (float) viewport->w;
  2098. } else {
  2099. orientationAlignedViewport.x = (float) viewport->x;
  2100. orientationAlignedViewport.y = (float) viewport->y;
  2101. orientationAlignedViewport.w = (float) viewport->w;
  2102. orientationAlignedViewport.h = (float) viewport->h;
  2103. }
  2104. d3dviewport.TopLeftX = orientationAlignedViewport.x;
  2105. d3dviewport.TopLeftY = orientationAlignedViewport.y;
  2106. d3dviewport.Width = orientationAlignedViewport.w;
  2107. d3dviewport.Height = orientationAlignedViewport.h;
  2108. d3dviewport.MinDepth = 0.0f;
  2109. d3dviewport.MaxDepth = 1.0f;
  2110. /* SDL_Log("%s: D3D viewport = {%f,%f,%f,%f}\n", __FUNCTION__, d3dviewport.TopLeftX, d3dviewport.TopLeftY, d3dviewport.Width, d3dviewport.Height); */
  2111. D3D_CALL(data->commandList, RSSetViewports, 1, &d3dviewport);
  2112. data->viewportDirty = SDL_FALSE;
  2113. return 0;
  2114. }
  2115. static int
  2116. D3D12_SetDrawState(SDL_Renderer * renderer, const SDL_RenderCommand *cmd, D3D12_Shader shader,
  2117. D3D12_PRIMITIVE_TOPOLOGY_TYPE topology,
  2118. const int numShaderResources, D3D12_CPU_DESCRIPTOR_HANDLE * shaderResources,
  2119. D3D12_CPU_DESCRIPTOR_HANDLE * sampler, const Float4X4 *matrix)
  2120. {
  2121. D3D12_RenderData *rendererData = (D3D12_RenderData *)renderer->driverdata;
  2122. const Float4X4 *newmatrix = matrix ? matrix : &rendererData->identity;
  2123. D3D12_CPU_DESCRIPTOR_HANDLE renderTargetView = D3D12_GetCurrentRenderTargetView(renderer);
  2124. const SDL_BlendMode blendMode = cmd->data.draw.blend;
  2125. SDL_bool updateSubresource = SDL_FALSE;
  2126. int i;
  2127. D3D12_CPU_DESCRIPTOR_HANDLE firstShaderResource;
  2128. DXGI_FORMAT rtvFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
  2129. if (rendererData->textureRenderTarget) {
  2130. rtvFormat = rendererData->textureRenderTarget->mainTextureFormat;
  2131. }
  2132. /* See if we need to change the pipeline state */
  2133. if (!rendererData->currentPipelineState ||
  2134. rendererData->currentPipelineState->shader != shader ||
  2135. rendererData->currentPipelineState->blendMode != blendMode ||
  2136. rendererData->currentPipelineState->topology != topology ||
  2137. rendererData->currentPipelineState->rtvFormat != rtvFormat) {
  2138. /* Find the matching pipeline.
  2139. NOTE: Although it may seem inefficient to linearly search through ~450 pipelines
  2140. to find the correct one, in profiling this doesn't come up at all.
  2141. It's unlikely that using a hash table would affect performance a measurable amount unless
  2142. it's a degenerate case that's chaning the pipline state dozens of times per frame.
  2143. */
  2144. rendererData->currentPipelineState = NULL;
  2145. for (i = 0; i < rendererData->pipelineStateCount; ++i) {
  2146. D3D12_PipelineState* candidatePiplineState = &rendererData->pipelineStates[i];
  2147. if (candidatePiplineState->shader == shader &&
  2148. candidatePiplineState->blendMode == blendMode &&
  2149. candidatePiplineState->topology == topology &&
  2150. candidatePiplineState->rtvFormat == rtvFormat) {
  2151. rendererData->currentPipelineState = candidatePiplineState;
  2152. break;
  2153. }
  2154. }
  2155. /* If we didn't find a match, create a new one -- it must mean the blend mode is non-standard */
  2156. if (!rendererData->currentPipelineState) {
  2157. rendererData->currentPipelineState = D3D12_CreatePipelineState(renderer, shader, blendMode, topology, rtvFormat);
  2158. }
  2159. if (!rendererData->currentPipelineState) {
  2160. return SDL_SetError("[direct3d12] Unable to create required pipeline state");
  2161. }
  2162. D3D_CALL(rendererData->commandList, SetPipelineState, rendererData->currentPipelineState->pipelineState);
  2163. D3D_CALL(rendererData->commandList, SetGraphicsRootSignature,
  2164. rendererData->rootSignatures[D3D12_GetRootSignatureType(rendererData->currentPipelineState->shader)]);
  2165. /* When we change these we will need to re-upload the constant buffer and reset any descriptors */
  2166. updateSubresource = SDL_TRUE;
  2167. rendererData->currentSampler.ptr = 0;
  2168. rendererData->currentShaderResource.ptr = 0;
  2169. }
  2170. if (renderTargetView.ptr != rendererData->currentRenderTargetView.ptr) {
  2171. D3D_CALL(rendererData->commandList, OMSetRenderTargets, 1, &renderTargetView, FALSE, NULL);
  2172. rendererData->currentRenderTargetView = renderTargetView;
  2173. }
  2174. if (rendererData->viewportDirty) {
  2175. if (D3D12_UpdateViewport(renderer) == 0) {
  2176. /* vertexShaderConstantsData.projectionAndView has changed */
  2177. updateSubresource = SDL_TRUE;
  2178. }
  2179. }
  2180. if (rendererData->cliprectDirty) {
  2181. D3D12_RECT scissorRect;
  2182. if (D3D12_GetViewportAlignedD3DRect(renderer, &rendererData->currentCliprect, &scissorRect, TRUE) != 0) {
  2183. /* D3D12_GetViewportAlignedD3DRect will have set the SDL error */
  2184. return -1;
  2185. }
  2186. D3D_CALL(rendererData->commandList, RSSetScissorRects, 1, &scissorRect);
  2187. rendererData->cliprectDirty = SDL_FALSE;
  2188. }
  2189. if (numShaderResources > 0) {
  2190. firstShaderResource = shaderResources[0];
  2191. } else {
  2192. firstShaderResource.ptr = 0;
  2193. }
  2194. if (firstShaderResource.ptr != rendererData->currentShaderResource.ptr) {
  2195. for (i = 0; i < numShaderResources; ++i) {
  2196. D3D12_GPU_DESCRIPTOR_HANDLE GPUHandle = D3D12_CPUtoGPUHandle(rendererData->srvDescriptorHeap, shaderResources[i]);
  2197. D3D_CALL(rendererData->commandList, SetGraphicsRootDescriptorTable, i + 1, GPUHandle);
  2198. }
  2199. rendererData->currentShaderResource.ptr = firstShaderResource.ptr;
  2200. }
  2201. if (sampler && sampler->ptr != rendererData->currentSampler.ptr) {
  2202. D3D12_GPU_DESCRIPTOR_HANDLE GPUHandle = D3D12_CPUtoGPUHandle(rendererData->samplerDescriptorHeap, *sampler);
  2203. UINT tableIndex = 0;
  2204. /* Figure out the correct sampler descriptor table index based on the type of shader */
  2205. switch (shader) {
  2206. case SHADER_RGB:
  2207. tableIndex = 2;
  2208. break;
  2209. #if SDL_HAVE_YUV
  2210. case SHADER_YUV_JPEG:
  2211. case SHADER_YUV_BT601:
  2212. case SHADER_YUV_BT709:
  2213. tableIndex = 4;
  2214. break;
  2215. case SHADER_NV12_JPEG:
  2216. case SHADER_NV12_BT601:
  2217. case SHADER_NV12_BT709:
  2218. case SHADER_NV21_JPEG:
  2219. case SHADER_NV21_BT601:
  2220. case SHADER_NV21_BT709:
  2221. tableIndex = 3;
  2222. break;
  2223. #endif
  2224. default:
  2225. return SDL_SetError("[direct3d12] Trying to set a sampler for a shader which doesn't have one");
  2226. break;
  2227. }
  2228. D3D_CALL(rendererData->commandList, SetGraphicsRootDescriptorTable, tableIndex, GPUHandle);
  2229. rendererData->currentSampler = *sampler;
  2230. }
  2231. if (updateSubresource == SDL_TRUE || SDL_memcmp(&rendererData->vertexShaderConstantsData.model, newmatrix, sizeof (*newmatrix)) != 0) {
  2232. SDL_memcpy(&rendererData->vertexShaderConstantsData.model, newmatrix, sizeof (*newmatrix));
  2233. D3D_CALL(rendererData->commandList, SetGraphicsRoot32BitConstants,
  2234. 0,
  2235. 32,
  2236. &rendererData->vertexShaderConstantsData,
  2237. 0
  2238. );
  2239. }
  2240. return 0;
  2241. }
  2242. static int
  2243. D3D12_SetCopyState(SDL_Renderer * renderer, const SDL_RenderCommand *cmd, const Float4X4 *matrix)
  2244. {
  2245. SDL_Texture *texture = cmd->data.draw.texture;
  2246. D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
  2247. D3D12_TextureData *textureData = (D3D12_TextureData *) texture->driverdata;
  2248. D3D12_CPU_DESCRIPTOR_HANDLE *textureSampler;
  2249. switch (textureData->scaleMode) {
  2250. case D3D12_FILTER_MIN_MAG_MIP_POINT:
  2251. textureSampler = &rendererData->nearestPixelSampler;
  2252. break;
  2253. case D3D12_FILTER_MIN_MAG_MIP_LINEAR:
  2254. textureSampler = &rendererData->linearSampler;
  2255. break;
  2256. default:
  2257. return SDL_SetError("Unknown scale mode: %d\n", textureData->scaleMode);
  2258. }
  2259. #if SDL_HAVE_YUV
  2260. if (textureData->yuv) {
  2261. D3D12_CPU_DESCRIPTOR_HANDLE shaderResources[] = {
  2262. textureData->mainTextureResourceView,
  2263. textureData->mainTextureResourceViewU,
  2264. textureData->mainTextureResourceViewV
  2265. };
  2266. D3D12_Shader shader;
  2267. switch (SDL_GetYUVConversionModeForResolution(texture->w, texture->h)) {
  2268. case SDL_YUV_CONVERSION_JPEG:
  2269. shader = SHADER_YUV_JPEG;
  2270. break;
  2271. case SDL_YUV_CONVERSION_BT601:
  2272. shader = SHADER_YUV_BT601;
  2273. break;
  2274. case SDL_YUV_CONVERSION_BT709:
  2275. shader = SHADER_YUV_BT709;
  2276. break;
  2277. default:
  2278. return SDL_SetError("Unsupported YUV conversion mode");
  2279. }
  2280. /* Make sure each texture is in the correct state to be accessed by the pixel shader. */
  2281. D3D12_TransitionResource(rendererData, textureData->mainTexture, textureData->mainResourceState, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
  2282. textureData->mainResourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  2283. D3D12_TransitionResource(rendererData, textureData->mainTextureU, textureData->mainResourceStateU, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
  2284. textureData->mainResourceStateU = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  2285. D3D12_TransitionResource(rendererData, textureData->mainTextureV, textureData->mainResourceStateV, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
  2286. textureData->mainResourceStateV = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  2287. return D3D12_SetDrawState(renderer, cmd, shader, D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
  2288. SDL_arraysize(shaderResources), shaderResources, textureSampler, matrix);
  2289. } else if (textureData->nv12) {
  2290. D3D12_CPU_DESCRIPTOR_HANDLE shaderResources[] = {
  2291. textureData->mainTextureResourceView,
  2292. textureData->mainTextureResourceViewNV,
  2293. };
  2294. D3D12_Shader shader;
  2295. switch (SDL_GetYUVConversionModeForResolution(texture->w, texture->h)) {
  2296. case SDL_YUV_CONVERSION_JPEG:
  2297. shader = texture->format == SDL_PIXELFORMAT_NV12 ? SHADER_NV12_JPEG : SHADER_NV21_JPEG;
  2298. break;
  2299. case SDL_YUV_CONVERSION_BT601:
  2300. shader = texture->format == SDL_PIXELFORMAT_NV12 ? SHADER_NV12_BT601 : SHADER_NV21_BT601;
  2301. break;
  2302. case SDL_YUV_CONVERSION_BT709:
  2303. shader = texture->format == SDL_PIXELFORMAT_NV12 ? SHADER_NV12_BT709 : SHADER_NV21_BT709;
  2304. break;
  2305. default:
  2306. return SDL_SetError("Unsupported YUV conversion mode");
  2307. }
  2308. /* Make sure each texture is in the correct state to be accessed by the pixel shader. */
  2309. D3D12_TransitionResource(rendererData, textureData->mainTexture, textureData->mainResourceState, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
  2310. textureData->mainResourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  2311. D3D12_TransitionResource(rendererData, textureData->mainTextureNV, textureData->mainResourceStateNV, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
  2312. textureData->mainResourceStateNV = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  2313. return D3D12_SetDrawState(renderer, cmd, shader, D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
  2314. SDL_arraysize(shaderResources), shaderResources, textureSampler, matrix);
  2315. }
  2316. #endif /* SDL_HAVE_YUV */
  2317. D3D12_TransitionResource(rendererData, textureData->mainTexture, textureData->mainResourceState, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
  2318. textureData->mainResourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  2319. return D3D12_SetDrawState(renderer, cmd, SHADER_RGB, D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
  2320. 1, &textureData->mainTextureResourceView, textureSampler, matrix);
  2321. }
  2322. static void
  2323. D3D12_DrawPrimitives(SDL_Renderer * renderer, D3D12_PRIMITIVE_TOPOLOGY primitiveTopology, const size_t vertexStart, const size_t vertexCount)
  2324. {
  2325. D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
  2326. D3D_CALL(rendererData->commandList, IASetPrimitiveTopology, primitiveTopology);
  2327. D3D_CALL(rendererData->commandList, DrawInstanced, (UINT)vertexCount, 1, (UINT)vertexStart, 0);
  2328. }
  2329. static int
  2330. D3D12_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
  2331. {
  2332. D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
  2333. const int viewportRotation = D3D12_GetRotationForCurrentRenderTarget(renderer);
  2334. if (rendererData->currentViewportRotation != viewportRotation) {
  2335. rendererData->currentViewportRotation = viewportRotation;
  2336. rendererData->viewportDirty = SDL_TRUE;
  2337. }
  2338. if (D3D12_UpdateVertexBuffer(renderer, vertices, vertsize) < 0) {
  2339. return -1;
  2340. }
  2341. while (cmd) {
  2342. switch (cmd->command) {
  2343. case SDL_RENDERCMD_SETDRAWCOLOR: {
  2344. break; /* this isn't currently used in this render backend. */
  2345. }
  2346. case SDL_RENDERCMD_SETVIEWPORT: {
  2347. SDL_Rect *viewport = &rendererData->currentViewport;
  2348. if (SDL_memcmp(viewport, &cmd->data.viewport.rect, sizeof(cmd->data.viewport.rect)) != 0) {
  2349. SDL_copyp(viewport, &cmd->data.viewport.rect);
  2350. rendererData->viewportDirty = SDL_TRUE;
  2351. }
  2352. break;
  2353. }
  2354. case SDL_RENDERCMD_SETCLIPRECT: {
  2355. const SDL_Rect *rect = &cmd->data.cliprect.rect;
  2356. if (rendererData->currentCliprectEnabled != cmd->data.cliprect.enabled) {
  2357. rendererData->currentCliprectEnabled = cmd->data.cliprect.enabled;
  2358. rendererData->cliprectDirty = SDL_TRUE;
  2359. }
  2360. if (!rendererData->currentCliprectEnabled) {
  2361. /* If the clip rect is disabled, then the scissor rect should be the whole viewport,
  2362. since direct3d12 doesn't allow disabling the scissor rectangle */
  2363. rect = &rendererData->currentViewport;
  2364. }
  2365. if (SDL_memcmp(&rendererData->currentCliprect, rect, sizeof(*rect)) != 0) {
  2366. SDL_copyp(&rendererData->currentCliprect, rect);
  2367. rendererData->cliprectDirty = SDL_TRUE;
  2368. }
  2369. break;
  2370. }
  2371. case SDL_RENDERCMD_CLEAR: {
  2372. const float colorRGBA[] = {
  2373. (cmd->data.color.r / 255.0f),
  2374. (cmd->data.color.g / 255.0f),
  2375. (cmd->data.color.b / 255.0f),
  2376. (cmd->data.color.a / 255.0f)
  2377. };
  2378. D3D12_CPU_DESCRIPTOR_HANDLE rtvDescriptor = D3D12_GetCurrentRenderTargetView(renderer);
  2379. D3D_CALL(rendererData->commandList, ClearRenderTargetView, rtvDescriptor, colorRGBA, 0, NULL);
  2380. break;
  2381. }
  2382. case SDL_RENDERCMD_DRAW_POINTS: {
  2383. const size_t count = cmd->data.draw.count;
  2384. const size_t first = cmd->data.draw.first;
  2385. const size_t start = first / sizeof (VertexPositionColor);
  2386. D3D12_SetDrawState(renderer, cmd, SHADER_SOLID, D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT, 0, NULL, NULL, NULL);
  2387. D3D12_DrawPrimitives(renderer, D3D_PRIMITIVE_TOPOLOGY_POINTLIST, start, count);
  2388. break;
  2389. }
  2390. case SDL_RENDERCMD_DRAW_LINES: {
  2391. const size_t count = cmd->data.draw.count;
  2392. const size_t first = cmd->data.draw.first;
  2393. const size_t start = first / sizeof (VertexPositionColor);
  2394. const VertexPositionColor *verts = (VertexPositionColor *) (((Uint8 *) vertices) + first);
  2395. D3D12_SetDrawState(renderer, cmd, SHADER_SOLID, D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE, 0, NULL, NULL, NULL);
  2396. D3D12_DrawPrimitives(renderer, D3D_PRIMITIVE_TOPOLOGY_LINESTRIP, start, count);
  2397. if (verts[0].pos.x != verts[count - 1].pos.x || verts[0].pos.y != verts[count - 1].pos.y) {
  2398. D3D12_DrawPrimitives(renderer, D3D_PRIMITIVE_TOPOLOGY_POINTLIST, start + (count-1), 1);
  2399. }
  2400. break;
  2401. }
  2402. case SDL_RENDERCMD_FILL_RECTS: /* unused */
  2403. break;
  2404. case SDL_RENDERCMD_COPY: /* unused */
  2405. break;
  2406. case SDL_RENDERCMD_COPY_EX: /* unused */
  2407. break;
  2408. case SDL_RENDERCMD_GEOMETRY: {
  2409. SDL_Texture *texture = cmd->data.draw.texture;
  2410. const size_t count = cmd->data.draw.count;
  2411. const size_t first = cmd->data.draw.first;
  2412. const size_t start = first / sizeof (VertexPositionColor);
  2413. if (texture) {
  2414. D3D12_SetCopyState(renderer, cmd, NULL);
  2415. } else {
  2416. D3D12_SetDrawState(renderer, cmd, SHADER_SOLID, D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, 0, NULL, NULL, NULL);
  2417. }
  2418. D3D12_DrawPrimitives(renderer, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST, start, count);
  2419. break;
  2420. }
  2421. case SDL_RENDERCMD_NO_OP:
  2422. break;
  2423. }
  2424. cmd = cmd->next;
  2425. }
  2426. return 0;
  2427. }
  2428. static int
  2429. D3D12_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  2430. Uint32 format, void * pixels, int pitch)
  2431. {
  2432. D3D12_RenderData * data = (D3D12_RenderData *) renderer->driverdata;
  2433. ID3D12Resource *backBuffer = NULL;
  2434. ID3D12Resource *readbackBuffer = NULL;
  2435. HRESULT result;
  2436. int status = -1;
  2437. D3D12_RESOURCE_DESC textureDesc;
  2438. D3D12_RESOURCE_DESC readbackDesc;
  2439. D3D12_HEAP_PROPERTIES heapProps;
  2440. D3D12_RECT srcRect = {0, 0, 0, 0};
  2441. D3D12_BOX srcBox;
  2442. D3D12_TEXTURE_COPY_LOCATION dstLocation;
  2443. D3D12_TEXTURE_COPY_LOCATION srcLocation;
  2444. D3D12_PLACED_SUBRESOURCE_FOOTPRINT placedTextureDesc;
  2445. D3D12_SUBRESOURCE_FOOTPRINT pitchedDesc;
  2446. BYTE *textureMemory;
  2447. int bpp;
  2448. if (data->textureRenderTarget) {
  2449. backBuffer = data->textureRenderTarget->mainTexture;
  2450. } else {
  2451. backBuffer = data->renderTargets[data->currentBackBufferIndex];
  2452. }
  2453. /* Create a staging texture to copy the screen's data to: */
  2454. SDL_zero(textureDesc);
  2455. D3D_CALL_RET(backBuffer, GetDesc, &textureDesc);
  2456. textureDesc.Width = rect->w;
  2457. textureDesc.Height = rect->h;
  2458. SDL_zero(readbackDesc);
  2459. readbackDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  2460. readbackDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
  2461. readbackDesc.Height = 1;
  2462. readbackDesc.DepthOrArraySize = 1;
  2463. readbackDesc.MipLevels = 1;
  2464. readbackDesc.Format = DXGI_FORMAT_UNKNOWN;
  2465. readbackDesc.SampleDesc.Count = 1;
  2466. readbackDesc.SampleDesc.Quality = 0;
  2467. readbackDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  2468. readbackDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
  2469. /* Figure out how much we need to allocate for the upload buffer */
  2470. D3D_CALL(data->d3dDevice, GetCopyableFootprints,
  2471. &textureDesc,
  2472. 0,
  2473. 1,
  2474. 0,
  2475. NULL,
  2476. NULL,
  2477. NULL,
  2478. &readbackDesc.Width
  2479. );
  2480. SDL_zero(heapProps);
  2481. heapProps.Type = D3D12_HEAP_TYPE_READBACK;
  2482. heapProps.CreationNodeMask = 1;
  2483. heapProps.VisibleNodeMask = 1;
  2484. result = D3D_CALL(data->d3dDevice, CreateCommittedResource,
  2485. &heapProps,
  2486. D3D12_HEAP_FLAG_NONE,
  2487. &readbackDesc,
  2488. D3D12_RESOURCE_STATE_COPY_DEST,
  2489. NULL,
  2490. D3D_GUID(SDL_IID_ID3D12Resource),
  2491. (void **)&readbackBuffer
  2492. );
  2493. if (FAILED(result)) {
  2494. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateTexture2D [create staging texture]"), result);
  2495. goto done;
  2496. }
  2497. /* Transition the render target to be copyable from */
  2498. D3D12_TransitionResource(data, backBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
  2499. /* Copy the desired portion of the back buffer to the staging texture: */
  2500. if (D3D12_GetViewportAlignedD3DRect(renderer, rect, &srcRect, FALSE) != 0) {
  2501. /* D3D12_GetViewportAlignedD3DRect will have set the SDL error */
  2502. goto done;
  2503. }
  2504. srcBox.left = srcRect.left;
  2505. srcBox.right = srcRect.right;
  2506. srcBox.top = srcRect.top;
  2507. srcBox.bottom = srcRect.bottom;
  2508. srcBox.front = 0;
  2509. srcBox.back = 1;
  2510. /* Issue the copy texture region */
  2511. SDL_zero(pitchedDesc);
  2512. pitchedDesc.Format = textureDesc.Format;
  2513. pitchedDesc.Width = (UINT)textureDesc.Width;
  2514. pitchedDesc.Height = textureDesc.Height;
  2515. pitchedDesc.Depth = 1;
  2516. if (pitchedDesc.Format == DXGI_FORMAT_R8_UNORM) {
  2517. bpp = 1;
  2518. } else {
  2519. bpp = 4;
  2520. }
  2521. pitchedDesc.RowPitch = D3D12_Align(pitchedDesc.Width * bpp, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
  2522. SDL_zero(placedTextureDesc);
  2523. placedTextureDesc.Offset = 0;
  2524. placedTextureDesc.Footprint = pitchedDesc;
  2525. SDL_zero(dstLocation);
  2526. dstLocation.pResource = readbackBuffer;
  2527. dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
  2528. dstLocation.PlacedFootprint = placedTextureDesc;
  2529. SDL_zero(srcLocation);
  2530. srcLocation.pResource = backBuffer;
  2531. srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  2532. srcLocation.SubresourceIndex = 0;
  2533. D3D_CALL(data->commandList, CopyTextureRegion,
  2534. &dstLocation,
  2535. 0, 0, 0,
  2536. &srcLocation,
  2537. &srcBox
  2538. );
  2539. /* We need to issue the command list for the copy to finish */
  2540. D3D12_IssueBatch(data);
  2541. /* Transition the render target back to a render target */
  2542. D3D12_TransitionResource(data, backBuffer, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET );
  2543. /* Map the staging texture's data to CPU-accessible memory: */
  2544. result = D3D_CALL(readbackBuffer, Map,
  2545. 0,
  2546. NULL,
  2547. (void **)&textureMemory
  2548. );
  2549. if (FAILED(result)) {
  2550. SAFE_RELEASE(readbackBuffer);
  2551. return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Resource::Map [map staging texture]"), result);
  2552. }
  2553. /* Copy the data into the desired buffer, converting pixels to the
  2554. * desired format at the same time:
  2555. */
  2556. status = SDL_ConvertPixels(
  2557. rect->w, rect->h,
  2558. D3D12_DXGIFormatToSDLPixelFormat(textureDesc.Format),
  2559. textureMemory,
  2560. pitchedDesc.RowPitch,
  2561. format,
  2562. pixels,
  2563. pitch);
  2564. /* Unmap the texture: */
  2565. D3D_CALL(readbackBuffer, Unmap, 0, NULL);
  2566. done:
  2567. SAFE_RELEASE(readbackBuffer);
  2568. return status;
  2569. }
  2570. static int
  2571. D3D12_RenderPresent(SDL_Renderer * renderer)
  2572. {
  2573. D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
  2574. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  2575. UINT syncInterval;
  2576. UINT presentFlags;
  2577. #endif
  2578. HRESULT result;
  2579. /* Transition the render target to present state */
  2580. D3D12_TransitionResource(data,
  2581. data->renderTargets[data->currentBackBufferIndex],
  2582. D3D12_RESOURCE_STATE_RENDER_TARGET,
  2583. D3D12_RESOURCE_STATE_PRESENT
  2584. );
  2585. /* Issue the command list */
  2586. result = D3D_CALL(data->commandList, Close);
  2587. D3D_CALL(data->commandQueue, ExecuteCommandLists, 1, (ID3D12CommandList * const *)&data->commandList);
  2588. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  2589. result = D3D12_XBOX_PresentFrame(data->commandQueue, data->frameToken, data->renderTargets[data->currentBackBufferIndex]);
  2590. #else
  2591. if (renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
  2592. syncInterval = 1;
  2593. presentFlags = 0;
  2594. } else {
  2595. syncInterval = 0;
  2596. presentFlags = DXGI_PRESENT_ALLOW_TEARING;
  2597. }
  2598. /* The application may optionally specify "dirty" or "scroll"
  2599. * rects to improve efficiency in certain scenarios.
  2600. */
  2601. result = D3D_CALL(data->swapChain, Present, syncInterval, presentFlags);
  2602. #endif
  2603. if (FAILED(result) && result != DXGI_ERROR_WAS_STILL_DRAWING) {
  2604. /* If the device was removed either by a disconnect or a driver upgrade, we
  2605. * must recreate all device resources.
  2606. */
  2607. if ( result == DXGI_ERROR_DEVICE_REMOVED ) {
  2608. D3D12_HandleDeviceLost(renderer);
  2609. } else if (result == DXGI_ERROR_INVALID_CALL) {
  2610. /* We probably went through a fullscreen <-> windowed transition */
  2611. D3D12_CreateWindowSizeDependentResources(renderer);
  2612. } else {
  2613. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::Present"), result);
  2614. }
  2615. return -1;
  2616. } else {
  2617. /* Wait for the GPU and move to the next frame */
  2618. result = D3D_CALL(data->commandQueue, Signal, data->fence, data->fenceValue);
  2619. if (D3D_CALL(data->fence, GetCompletedValue) < data->fenceValue) {
  2620. result = D3D_CALL(data->fence, SetEventOnCompletion,
  2621. data->fenceValue,
  2622. data->fenceEvent
  2623. );
  2624. WaitForSingleObjectEx(data->fenceEvent, INFINITE, FALSE);
  2625. }
  2626. data->fenceValue++;
  2627. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  2628. data->currentBackBufferIndex++;
  2629. data->currentBackBufferIndex %= SDL_D3D12_NUM_BUFFERS;
  2630. #else
  2631. data->currentBackBufferIndex = D3D_CALL(data->swapChain, GetCurrentBackBufferIndex);
  2632. #endif
  2633. /* Reset the command allocator and command list, and transition back to render target */
  2634. D3D12_ResetCommandList(data);
  2635. D3D12_TransitionResource(data,
  2636. data->renderTargets[data->currentBackBufferIndex],
  2637. D3D12_RESOURCE_STATE_PRESENT,
  2638. D3D12_RESOURCE_STATE_RENDER_TARGET
  2639. );
  2640. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  2641. D3D12_XBOX_StartFrame(data->d3dDevice, &data->frameToken);
  2642. #endif
  2643. return 0;
  2644. }
  2645. }
  2646. static int
  2647. D3D12_SetVSync(SDL_Renderer * renderer, const int vsync)
  2648. {
  2649. if (vsync) {
  2650. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  2651. } else {
  2652. renderer->info.flags &= ~SDL_RENDERER_PRESENTVSYNC;
  2653. }
  2654. return 0;
  2655. }
  2656. SDL_Renderer *
  2657. D3D12_CreateRenderer(SDL_Window * window, Uint32 flags)
  2658. {
  2659. SDL_Renderer *renderer;
  2660. D3D12_RenderData *data;
  2661. renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
  2662. if (!renderer) {
  2663. SDL_OutOfMemory();
  2664. return NULL;
  2665. }
  2666. data = (D3D12_RenderData *) SDL_calloc(1, sizeof(*data));
  2667. if (!data) {
  2668. SDL_free(renderer);
  2669. SDL_OutOfMemory();
  2670. return NULL;
  2671. }
  2672. data->identity = MatrixIdentity();
  2673. renderer->WindowEvent = D3D12_WindowEvent;
  2674. renderer->GetOutputSize = D3D12_GetOutputSize;
  2675. renderer->SupportsBlendMode = D3D12_SupportsBlendMode;
  2676. renderer->CreateTexture = D3D12_CreateTexture;
  2677. renderer->UpdateTexture = D3D12_UpdateTexture;
  2678. #if SDL_HAVE_YUV
  2679. renderer->UpdateTextureYUV = D3D12_UpdateTextureYUV;
  2680. renderer->UpdateTextureNV = D3D12_UpdateTextureNV;
  2681. #endif
  2682. renderer->LockTexture = D3D12_LockTexture;
  2683. renderer->UnlockTexture = D3D12_UnlockTexture;
  2684. renderer->SetTextureScaleMode = D3D12_SetTextureScaleMode;
  2685. renderer->SetRenderTarget = D3D12_SetRenderTarget;
  2686. renderer->QueueSetViewport = D3D12_QueueSetViewport;
  2687. renderer->QueueSetDrawColor = D3D12_QueueSetViewport; /* SetViewport and SetDrawColor are (currently) no-ops. */
  2688. renderer->QueueDrawPoints = D3D12_QueueDrawPoints;
  2689. renderer->QueueDrawLines = D3D12_QueueDrawPoints; /* lines and points queue vertices the same way. */
  2690. renderer->QueueGeometry = D3D12_QueueGeometry;
  2691. renderer->RunCommandQueue = D3D12_RunCommandQueue;
  2692. renderer->RenderReadPixels = D3D12_RenderReadPixels;
  2693. renderer->RenderPresent = D3D12_RenderPresent;
  2694. renderer->DestroyTexture = D3D12_DestroyTexture;
  2695. renderer->DestroyRenderer = D3D12_DestroyRenderer;
  2696. renderer->info = D3D12_RenderDriver.info;
  2697. renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
  2698. renderer->driverdata = data;
  2699. if ((flags & SDL_RENDERER_PRESENTVSYNC)) {
  2700. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  2701. }
  2702. renderer->SetVSync = D3D12_SetVSync;
  2703. /* HACK: make sure the SDL_Renderer references the SDL_Window data now, in
  2704. * order to give init functions access to the underlying window handle:
  2705. */
  2706. renderer->window = window;
  2707. /* Initialize Direct3D resources */
  2708. if (FAILED(D3D12_CreateDeviceResources(renderer))) {
  2709. D3D12_DestroyRenderer(renderer);
  2710. return NULL;
  2711. }
  2712. if (FAILED(D3D12_CreateWindowSizeDependentResources(renderer))) {
  2713. D3D12_DestroyRenderer(renderer);
  2714. return NULL;
  2715. }
  2716. return renderer;
  2717. }
  2718. SDL_RenderDriver D3D12_RenderDriver = {
  2719. D3D12_CreateRenderer,
  2720. {
  2721. "direct3d12",
  2722. (
  2723. SDL_RENDERER_ACCELERATED |
  2724. SDL_RENDERER_PRESENTVSYNC |
  2725. SDL_RENDERER_TARGETTEXTURE
  2726. ), /* flags. see SDL_RendererFlags */
  2727. 6, /* num_texture_formats */
  2728. { /* texture_formats */
  2729. SDL_PIXELFORMAT_ARGB8888,
  2730. SDL_PIXELFORMAT_RGB888,
  2731. SDL_PIXELFORMAT_YV12,
  2732. SDL_PIXELFORMAT_IYUV,
  2733. SDL_PIXELFORMAT_NV12,
  2734. SDL_PIXELFORMAT_NV21
  2735. },
  2736. 16384, /* max_texture_width */
  2737. 16384 /* max_texture_height */
  2738. }
  2739. };
  2740. /* Ends C function definitions when using C++ */
  2741. #ifdef __cplusplus
  2742. }
  2743. #endif
  2744. #endif /* SDL_VIDEO_RENDER_D3D12 && !SDL_RENDER_DISABLED */
  2745. #if defined(__WIN32__) || defined(__GDK__)
  2746. #ifdef __cplusplus
  2747. extern "C"
  2748. #endif
  2749. /* This function needs to always exist on Windows, for the Dynamic API. */
  2750. ID3D12Device *
  2751. SDL_RenderGetD3D12Device(SDL_Renderer * renderer)
  2752. {
  2753. ID3D12Device *device = NULL;
  2754. #if SDL_VIDEO_RENDER_D3D12 && !SDL_RENDER_DISABLED
  2755. D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
  2756. /* Make sure that this is a D3D renderer */
  2757. if (renderer->DestroyRenderer != D3D12_DestroyRenderer) {
  2758. SDL_SetError("Renderer is not a D3D12 renderer");
  2759. return NULL;
  2760. }
  2761. device = (ID3D12Device *)data->d3dDevice;
  2762. if (device) {
  2763. D3D_CALL(device, AddRef);
  2764. }
  2765. #endif /* SDL_VIDEO_RENDER_D3D12 && !SDL_RENDER_DISABLED */
  2766. return device;
  2767. }
  2768. #endif /* defined(__WIN32__) || defined(__GDK__) */
  2769. /* vi: set ts=4 sw=4 expandtab: */