SDL_render_d3d12.c 141 KB

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