SDL_render_d3d11.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
  20. #define COBJMACROS
  21. #include "../../core/windows/SDL_windows.h"
  22. #include "SDL_hints.h"
  23. #include "SDL_loadso.h"
  24. #include "SDL_syswm.h"
  25. #include "../SDL_sysrender.h"
  26. #include "../SDL_d3dmath.h"
  27. #include <d3d11_1.h>
  28. #include "SDL_shaders_d3d11.h"
  29. #ifdef __WINRT__
  30. #if NTDDI_VERSION > NTDDI_WIN8
  31. #include <DXGI1_3.h>
  32. #endif
  33. #include "SDL_render_winrt.h"
  34. #if WINAPI_FAMILY == WINAPI_FAMILY_APP
  35. #include <windows.ui.xaml.media.dxinterop.h>
  36. /* TODO, WinRT, XAML: get the ISwapChainBackgroundPanelNative from something other than a global var */
  37. extern ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative;
  38. #endif /* WINAPI_FAMILY == WINAPI_FAMILY_APP */
  39. #endif /* __WINRT__ */
  40. #define SDL_COMPOSE_ERROR(str) SDL_STRINGIFY_ARG(__FUNCTION__) ", " str
  41. #define SAFE_RELEASE(X) if ((X)) { IUnknown_Release(SDL_static_cast(IUnknown*, X)); X = NULL; }
  42. /* !!! FIXME: vertex buffer bandwidth could be significantly lower; move color to a uniform, only use UV coords
  43. !!! FIXME: when textures are needed, and don't ever pass Z, since it's always zero. */
  44. /* Vertex shader, common values */
  45. typedef struct
  46. {
  47. Float4X4 model;
  48. Float4X4 projectionAndView;
  49. } VertexShaderConstants;
  50. /* Per-vertex data */
  51. typedef struct
  52. {
  53. Float3 pos;
  54. Float2 tex;
  55. Float4 color;
  56. } VertexPositionColor;
  57. /* Per-texture data */
  58. typedef struct
  59. {
  60. ID3D11Texture2D *mainTexture;
  61. ID3D11ShaderResourceView *mainTextureResourceView;
  62. ID3D11RenderTargetView *mainTextureRenderTargetView;
  63. ID3D11Texture2D *stagingTexture;
  64. int lockedTexturePositionX;
  65. int lockedTexturePositionY;
  66. D3D11_FILTER scaleMode;
  67. /* YV12 texture support */
  68. SDL_bool yuv;
  69. ID3D11Texture2D *mainTextureU;
  70. ID3D11ShaderResourceView *mainTextureResourceViewU;
  71. ID3D11Texture2D *mainTextureV;
  72. ID3D11ShaderResourceView *mainTextureResourceViewV;
  73. /* NV12 texture support */
  74. SDL_bool nv12;
  75. ID3D11Texture2D *mainTextureNV;
  76. ID3D11ShaderResourceView *mainTextureResourceViewNV;
  77. Uint8 *pixels;
  78. int pitch;
  79. SDL_Rect locked_rect;
  80. } D3D11_TextureData;
  81. /* Blend mode data */
  82. typedef struct
  83. {
  84. SDL_BlendMode blendMode;
  85. ID3D11BlendState *blendState;
  86. } D3D11_BlendMode;
  87. /* Private renderer data */
  88. typedef struct
  89. {
  90. void *hDXGIMod;
  91. void *hD3D11Mod;
  92. IDXGIFactory2 *dxgiFactory;
  93. IDXGIAdapter *dxgiAdapter;
  94. ID3D11Device1 *d3dDevice;
  95. ID3D11DeviceContext1 *d3dContext;
  96. IDXGISwapChain1 *swapChain;
  97. DXGI_SWAP_EFFECT swapEffect;
  98. ID3D11RenderTargetView *mainRenderTargetView;
  99. ID3D11RenderTargetView *currentOffscreenRenderTargetView;
  100. ID3D11InputLayout *inputLayout;
  101. ID3D11Buffer *vertexBuffers[8];
  102. size_t vertexBufferSizes[8];
  103. ID3D11VertexShader *vertexShader;
  104. ID3D11PixelShader *pixelShaders[NUM_SHADERS];
  105. int blendModesCount;
  106. D3D11_BlendMode *blendModes;
  107. ID3D11SamplerState *nearestPixelSampler;
  108. ID3D11SamplerState *linearSampler;
  109. D3D_FEATURE_LEVEL featureLevel;
  110. /* Rasterizers */
  111. ID3D11RasterizerState *mainRasterizer;
  112. ID3D11RasterizerState *clippedRasterizer;
  113. /* Vertex buffer constants */
  114. VertexShaderConstants vertexShaderConstantsData;
  115. ID3D11Buffer *vertexShaderConstants;
  116. /* Cached renderer properties */
  117. DXGI_MODE_ROTATION rotation;
  118. ID3D11RenderTargetView *currentRenderTargetView;
  119. ID3D11RasterizerState *currentRasterizerState;
  120. ID3D11BlendState *currentBlendState;
  121. ID3D11PixelShader *currentShader;
  122. ID3D11ShaderResourceView *currentShaderResource;
  123. ID3D11SamplerState *currentSampler;
  124. SDL_bool cliprectDirty;
  125. SDL_bool currentCliprectEnabled;
  126. SDL_Rect currentCliprect;
  127. SDL_Rect currentViewport;
  128. int currentViewportRotation;
  129. SDL_bool viewportDirty;
  130. Float4X4 identity;
  131. int currentVertexBuffer;
  132. } D3D11_RenderData;
  133. /* Define D3D GUIDs here so we don't have to include uuid.lib.
  134. *
  135. * Fix for SDL bug https://bugzilla.libsdl.org/show_bug.cgi?id=3437:
  136. * The extra 'SDL_' was added to the start of each IID's name, in order
  137. * to prevent build errors on both MinGW-w64 and WinRT/UWP.
  138. * (SDL bug https://bugzilla.libsdl.org/show_bug.cgi?id=3336 led to
  139. * linker errors in WinRT/UWP builds.)
  140. */
  141. #ifdef __GNUC__
  142. #pragma GCC diagnostic push
  143. #pragma GCC diagnostic ignored "-Wunused-const-variable"
  144. #endif
  145. static const GUID SDL_IID_IDXGIFactory2 = { 0x50c83a1c, 0xe072, 0x4c48, { 0x87, 0xb0, 0x36, 0x30, 0xfa, 0x36, 0xa6, 0xd0 } };
  146. static const GUID SDL_IID_IDXGIDevice1 = { 0x77db970f, 0x6276, 0x48ba, { 0xba, 0x28, 0x07, 0x01, 0x43, 0xb4, 0x39, 0x2c } };
  147. static const GUID SDL_IID_IDXGIDevice3 = { 0x6007896c, 0x3244, 0x4afd, { 0xbf, 0x18, 0xa6, 0xd3, 0xbe, 0xda, 0x50, 0x23 } };
  148. static const GUID SDL_IID_ID3D11Texture2D = { 0x6f15aaf2, 0xd208, 0x4e89, { 0x9a, 0xb4, 0x48, 0x95, 0x35, 0xd3, 0x4f, 0x9c } };
  149. static const GUID SDL_IID_ID3D11Device1 = { 0xa04bfb29, 0x08ef, 0x43d6, { 0xa4, 0x9c, 0xa9, 0xbd, 0xbd, 0xcb, 0xe6, 0x86 } };
  150. static const GUID SDL_IID_ID3D11DeviceContext1 = { 0xbb2c6faa, 0xb5fb, 0x4082, { 0x8e, 0x6b, 0x38, 0x8b, 0x8c, 0xfa, 0x90, 0xe1 } };
  151. static const GUID SDL_IID_ID3D11Debug = { 0x79cf2233, 0x7536, 0x4948, { 0x9d, 0x36, 0x1e, 0x46, 0x92, 0xdc, 0x57, 0x60 } };
  152. #ifdef __GNUC__
  153. #pragma GCC diagnostic pop
  154. #endif
  155. Uint32
  156. D3D11_DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat)
  157. {
  158. switch (dxgiFormat) {
  159. case DXGI_FORMAT_B8G8R8A8_UNORM:
  160. return SDL_PIXELFORMAT_ARGB8888;
  161. case DXGI_FORMAT_B8G8R8X8_UNORM:
  162. return SDL_PIXELFORMAT_RGB888;
  163. default:
  164. return SDL_PIXELFORMAT_UNKNOWN;
  165. }
  166. }
  167. static DXGI_FORMAT
  168. SDLPixelFormatToDXGIFormat(Uint32 sdlFormat)
  169. {
  170. switch (sdlFormat) {
  171. case SDL_PIXELFORMAT_ARGB8888:
  172. return DXGI_FORMAT_B8G8R8A8_UNORM;
  173. case SDL_PIXELFORMAT_RGB888:
  174. return DXGI_FORMAT_B8G8R8X8_UNORM;
  175. case SDL_PIXELFORMAT_YV12:
  176. case SDL_PIXELFORMAT_IYUV:
  177. case SDL_PIXELFORMAT_NV12: /* For the Y texture */
  178. case SDL_PIXELFORMAT_NV21: /* For the Y texture */
  179. return DXGI_FORMAT_R8_UNORM;
  180. default:
  181. return DXGI_FORMAT_UNKNOWN;
  182. }
  183. }
  184. static void D3D11_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  185. static void
  186. D3D11_ReleaseAll(SDL_Renderer * renderer)
  187. {
  188. D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  189. SDL_Texture *texture = NULL;
  190. /* Release all textures */
  191. for (texture = renderer->textures; texture; texture = texture->next) {
  192. D3D11_DestroyTexture(renderer, texture);
  193. }
  194. /* Release/reset everything else */
  195. if (data) {
  196. int i;
  197. SAFE_RELEASE(data->dxgiFactory);
  198. SAFE_RELEASE(data->dxgiAdapter);
  199. SAFE_RELEASE(data->d3dDevice);
  200. SAFE_RELEASE(data->d3dContext);
  201. SAFE_RELEASE(data->swapChain);
  202. SAFE_RELEASE(data->mainRenderTargetView);
  203. SAFE_RELEASE(data->currentOffscreenRenderTargetView);
  204. SAFE_RELEASE(data->inputLayout);
  205. for (i = 0; i < SDL_arraysize(data->vertexBuffers); ++i) {
  206. SAFE_RELEASE(data->vertexBuffers[i]);
  207. }
  208. SAFE_RELEASE(data->vertexShader);
  209. for (i = 0; i < SDL_arraysize(data->pixelShaders); ++i) {
  210. SAFE_RELEASE(data->pixelShaders[i]);
  211. }
  212. if (data->blendModesCount > 0) {
  213. for (i = 0; i < data->blendModesCount; ++i) {
  214. SAFE_RELEASE(data->blendModes[i].blendState);
  215. }
  216. SDL_free(data->blendModes);
  217. data->blendModesCount = 0;
  218. }
  219. SAFE_RELEASE(data->nearestPixelSampler);
  220. SAFE_RELEASE(data->linearSampler);
  221. SAFE_RELEASE(data->mainRasterizer);
  222. SAFE_RELEASE(data->clippedRasterizer);
  223. SAFE_RELEASE(data->vertexShaderConstants);
  224. data->swapEffect = (DXGI_SWAP_EFFECT) 0;
  225. data->rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
  226. data->currentRenderTargetView = NULL;
  227. data->currentRasterizerState = NULL;
  228. data->currentBlendState = NULL;
  229. data->currentShader = NULL;
  230. data->currentShaderResource = NULL;
  231. data->currentSampler = NULL;
  232. /* Unload the D3D libraries. This should be done last, in order
  233. * to prevent IUnknown::Release() calls from crashing.
  234. */
  235. if (data->hD3D11Mod) {
  236. SDL_UnloadObject(data->hD3D11Mod);
  237. data->hD3D11Mod = NULL;
  238. }
  239. if (data->hDXGIMod) {
  240. SDL_UnloadObject(data->hDXGIMod);
  241. data->hDXGIMod = NULL;
  242. }
  243. }
  244. }
  245. static void
  246. D3D11_DestroyRenderer(SDL_Renderer * renderer)
  247. {
  248. D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  249. D3D11_ReleaseAll(renderer);
  250. if (data) {
  251. SDL_free(data);
  252. }
  253. SDL_free(renderer);
  254. }
  255. static D3D11_BLEND GetBlendFunc(SDL_BlendFactor factor)
  256. {
  257. switch (factor) {
  258. case SDL_BLENDFACTOR_ZERO:
  259. return D3D11_BLEND_ZERO;
  260. case SDL_BLENDFACTOR_ONE:
  261. return D3D11_BLEND_ONE;
  262. case SDL_BLENDFACTOR_SRC_COLOR:
  263. return D3D11_BLEND_SRC_COLOR;
  264. case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR:
  265. return D3D11_BLEND_INV_SRC_COLOR;
  266. case SDL_BLENDFACTOR_SRC_ALPHA:
  267. return D3D11_BLEND_SRC_ALPHA;
  268. case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA:
  269. return D3D11_BLEND_INV_SRC_ALPHA;
  270. case SDL_BLENDFACTOR_DST_COLOR:
  271. return D3D11_BLEND_DEST_COLOR;
  272. case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR:
  273. return D3D11_BLEND_INV_DEST_COLOR;
  274. case SDL_BLENDFACTOR_DST_ALPHA:
  275. return D3D11_BLEND_DEST_ALPHA;
  276. case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA:
  277. return D3D11_BLEND_INV_DEST_ALPHA;
  278. default:
  279. return (D3D11_BLEND)0;
  280. }
  281. }
  282. static D3D11_BLEND_OP GetBlendEquation(SDL_BlendOperation operation)
  283. {
  284. switch (operation) {
  285. case SDL_BLENDOPERATION_ADD:
  286. return D3D11_BLEND_OP_ADD;
  287. case SDL_BLENDOPERATION_SUBTRACT:
  288. return D3D11_BLEND_OP_SUBTRACT;
  289. case SDL_BLENDOPERATION_REV_SUBTRACT:
  290. return D3D11_BLEND_OP_REV_SUBTRACT;
  291. case SDL_BLENDOPERATION_MINIMUM:
  292. return D3D11_BLEND_OP_MIN;
  293. case SDL_BLENDOPERATION_MAXIMUM:
  294. return D3D11_BLEND_OP_MAX;
  295. default:
  296. return (D3D11_BLEND_OP)0;
  297. }
  298. }
  299. static ID3D11BlendState *
  300. D3D11_CreateBlendState(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  301. {
  302. D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  303. SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
  304. SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
  305. SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
  306. SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
  307. SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
  308. SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
  309. ID3D11BlendState *blendState = NULL;
  310. D3D11_BlendMode *blendModes;
  311. HRESULT result = S_OK;
  312. D3D11_BLEND_DESC blendDesc;
  313. SDL_zero(blendDesc);
  314. blendDesc.AlphaToCoverageEnable = FALSE;
  315. blendDesc.IndependentBlendEnable = FALSE;
  316. blendDesc.RenderTarget[0].BlendEnable = TRUE;
  317. blendDesc.RenderTarget[0].SrcBlend = GetBlendFunc(srcColorFactor);
  318. blendDesc.RenderTarget[0].DestBlend = GetBlendFunc(dstColorFactor);
  319. blendDesc.RenderTarget[0].BlendOp = GetBlendEquation(colorOperation);
  320. blendDesc.RenderTarget[0].SrcBlendAlpha = GetBlendFunc(srcAlphaFactor);
  321. blendDesc.RenderTarget[0].DestBlendAlpha = GetBlendFunc(dstAlphaFactor);
  322. blendDesc.RenderTarget[0].BlendOpAlpha = GetBlendEquation(alphaOperation);
  323. blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  324. result = ID3D11Device_CreateBlendState(data->d3dDevice, &blendDesc, &blendState);
  325. if (FAILED(result)) {
  326. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateBlendState"), result);
  327. return NULL;
  328. }
  329. blendModes = (D3D11_BlendMode *)SDL_realloc(data->blendModes, (data->blendModesCount + 1) * sizeof(*blendModes));
  330. if (!blendModes) {
  331. SAFE_RELEASE(blendState);
  332. SDL_OutOfMemory();
  333. return NULL;
  334. }
  335. blendModes[data->blendModesCount].blendMode = blendMode;
  336. blendModes[data->blendModesCount].blendState = blendState;
  337. data->blendModes = blendModes;
  338. ++data->blendModesCount;
  339. return blendState;
  340. }
  341. /* Create resources that depend on the device. */
  342. static HRESULT
  343. D3D11_CreateDeviceResources(SDL_Renderer * renderer)
  344. {
  345. typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY)(REFIID riid, void **ppFactory);
  346. PFN_CREATE_DXGI_FACTORY CreateDXGIFactoryFunc;
  347. D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  348. PFN_D3D11_CREATE_DEVICE D3D11CreateDeviceFunc;
  349. ID3D11Device *d3dDevice = NULL;
  350. ID3D11DeviceContext *d3dContext = NULL;
  351. IDXGIDevice1 *dxgiDevice = NULL;
  352. HRESULT result = S_OK;
  353. UINT creationFlags;
  354. int i;
  355. /* This array defines the set of DirectX hardware feature levels this app will support.
  356. * Note the ordering should be preserved.
  357. * Don't forget to declare your application's minimum required feature level in its
  358. * description. All applications are assumed to support 9.1 unless otherwise stated.
  359. */
  360. D3D_FEATURE_LEVEL featureLevels[] =
  361. {
  362. D3D_FEATURE_LEVEL_11_1,
  363. D3D_FEATURE_LEVEL_11_0,
  364. D3D_FEATURE_LEVEL_10_1,
  365. D3D_FEATURE_LEVEL_10_0,
  366. D3D_FEATURE_LEVEL_9_3,
  367. D3D_FEATURE_LEVEL_9_2,
  368. D3D_FEATURE_LEVEL_9_1
  369. };
  370. D3D11_BUFFER_DESC constantBufferDesc;
  371. D3D11_SAMPLER_DESC samplerDesc;
  372. D3D11_RASTERIZER_DESC rasterDesc;
  373. #ifdef __WINRT__
  374. CreateDXGIFactoryFunc = CreateDXGIFactory1;
  375. D3D11CreateDeviceFunc = D3D11CreateDevice;
  376. #else
  377. data->hDXGIMod = SDL_LoadObject("dxgi.dll");
  378. if (!data->hDXGIMod) {
  379. result = E_FAIL;
  380. goto done;
  381. }
  382. CreateDXGIFactoryFunc = (PFN_CREATE_DXGI_FACTORY)SDL_LoadFunction(data->hDXGIMod, "CreateDXGIFactory");
  383. if (!CreateDXGIFactoryFunc) {
  384. result = E_FAIL;
  385. goto done;
  386. }
  387. data->hD3D11Mod = SDL_LoadObject("d3d11.dll");
  388. if (!data->hD3D11Mod) {
  389. result = E_FAIL;
  390. goto done;
  391. }
  392. D3D11CreateDeviceFunc = (PFN_D3D11_CREATE_DEVICE)SDL_LoadFunction(data->hD3D11Mod, "D3D11CreateDevice");
  393. if (!D3D11CreateDeviceFunc) {
  394. result = E_FAIL;
  395. goto done;
  396. }
  397. #endif /* __WINRT__ */
  398. result = CreateDXGIFactoryFunc(&SDL_IID_IDXGIFactory2, (void **)&data->dxgiFactory);
  399. if (FAILED(result)) {
  400. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("CreateDXGIFactory"), result);
  401. goto done;
  402. }
  403. /* FIXME: Should we use the default adapter? */
  404. result = IDXGIFactory2_EnumAdapters(data->dxgiFactory, 0, &data->dxgiAdapter);
  405. if (FAILED(result)) {
  406. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("D3D11CreateDevice"), result);
  407. goto done;
  408. }
  409. /* This flag adds support for surfaces with a different color channel ordering
  410. * than the API default. It is required for compatibility with Direct2D.
  411. */
  412. creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
  413. /* Make sure Direct3D's debugging feature gets used, if the app requests it. */
  414. if (SDL_GetHintBoolean(SDL_HINT_RENDER_DIRECT3D11_DEBUG, SDL_FALSE)) {
  415. creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
  416. }
  417. /* Create the Direct3D 11 API device object and a corresponding context. */
  418. result = D3D11CreateDeviceFunc(
  419. data->dxgiAdapter,
  420. D3D_DRIVER_TYPE_UNKNOWN,
  421. NULL,
  422. creationFlags, /* Set set debug and Direct2D compatibility flags. */
  423. featureLevels, /* List of feature levels this app can support. */
  424. SDL_arraysize(featureLevels),
  425. D3D11_SDK_VERSION, /* Always set this to D3D11_SDK_VERSION for Windows Store apps. */
  426. &d3dDevice, /* Returns the Direct3D device created. */
  427. &data->featureLevel, /* Returns feature level of device created. */
  428. &d3dContext /* Returns the device immediate context. */
  429. );
  430. if (FAILED(result)) {
  431. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("D3D11CreateDevice"), result);
  432. goto done;
  433. }
  434. result = ID3D11Device_QueryInterface(d3dDevice, &SDL_IID_ID3D11Device1, (void **)&data->d3dDevice);
  435. if (FAILED(result)) {
  436. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device to ID3D11Device1"), result);
  437. goto done;
  438. }
  439. result = ID3D11DeviceContext_QueryInterface(d3dContext, &SDL_IID_ID3D11DeviceContext1, (void **)&data->d3dContext);
  440. if (FAILED(result)) {
  441. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11DeviceContext to ID3D11DeviceContext1"), result);
  442. goto done;
  443. }
  444. result = ID3D11Device_QueryInterface(d3dDevice, &SDL_IID_IDXGIDevice1, (void **)&dxgiDevice);
  445. if (FAILED(result)) {
  446. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device to IDXGIDevice1"), result);
  447. goto done;
  448. }
  449. /* Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
  450. * ensures that the application will only render after each VSync, minimizing power consumption.
  451. */
  452. result = IDXGIDevice1_SetMaximumFrameLatency(dxgiDevice, 1);
  453. if (FAILED(result)) {
  454. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGIDevice1::SetMaximumFrameLatency"), result);
  455. goto done;
  456. }
  457. /* Make note of the maximum texture size
  458. * Max texture sizes are documented on MSDN, at:
  459. * http://msdn.microsoft.com/en-us/library/windows/apps/ff476876.aspx
  460. */
  461. switch (data->featureLevel) {
  462. case D3D_FEATURE_LEVEL_11_1:
  463. case D3D_FEATURE_LEVEL_11_0:
  464. renderer->info.max_texture_width = renderer->info.max_texture_height = 16384;
  465. break;
  466. case D3D_FEATURE_LEVEL_10_1:
  467. case D3D_FEATURE_LEVEL_10_0:
  468. renderer->info.max_texture_width = renderer->info.max_texture_height = 8192;
  469. break;
  470. case D3D_FEATURE_LEVEL_9_3:
  471. renderer->info.max_texture_width = renderer->info.max_texture_height = 4096;
  472. break;
  473. case D3D_FEATURE_LEVEL_9_2:
  474. case D3D_FEATURE_LEVEL_9_1:
  475. renderer->info.max_texture_width = renderer->info.max_texture_height = 2048;
  476. break;
  477. default:
  478. SDL_SetError("%s, Unexpected feature level: %d", __FUNCTION__, data->featureLevel);
  479. result = E_FAIL;
  480. goto done;
  481. }
  482. if (D3D11_CreateVertexShader(data->d3dDevice, &data->vertexShader, &data->inputLayout) < 0) {
  483. goto done;
  484. }
  485. for (i = 0; i < SDL_arraysize(data->pixelShaders); ++i) {
  486. if (D3D11_CreatePixelShader(data->d3dDevice, (D3D11_Shader)i, &data->pixelShaders[i]) < 0) {
  487. goto done;
  488. }
  489. }
  490. /* Setup space to hold vertex shader constants: */
  491. SDL_zero(constantBufferDesc);
  492. constantBufferDesc.ByteWidth = sizeof(VertexShaderConstants);
  493. constantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  494. constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  495. result = ID3D11Device_CreateBuffer(data->d3dDevice,
  496. &constantBufferDesc,
  497. NULL,
  498. &data->vertexShaderConstants
  499. );
  500. if (FAILED(result)) {
  501. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateBuffer [vertex shader constants]"), result);
  502. goto done;
  503. }
  504. /* Create samplers to use when drawing textures: */
  505. SDL_zero(samplerDesc);
  506. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
  507. samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
  508. samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
  509. samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
  510. samplerDesc.MipLODBias = 0.0f;
  511. samplerDesc.MaxAnisotropy = 1;
  512. samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  513. samplerDesc.MinLOD = 0.0f;
  514. samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
  515. result = ID3D11Device_CreateSamplerState(data->d3dDevice,
  516. &samplerDesc,
  517. &data->nearestPixelSampler
  518. );
  519. if (FAILED(result)) {
  520. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateSamplerState [nearest-pixel filter]"), result);
  521. goto done;
  522. }
  523. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  524. result = ID3D11Device_CreateSamplerState(data->d3dDevice,
  525. &samplerDesc,
  526. &data->linearSampler
  527. );
  528. if (FAILED(result)) {
  529. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateSamplerState [linear filter]"), result);
  530. goto done;
  531. }
  532. /* Setup Direct3D rasterizer states */
  533. SDL_zero(rasterDesc);
  534. rasterDesc.AntialiasedLineEnable = FALSE;
  535. rasterDesc.CullMode = D3D11_CULL_NONE;
  536. rasterDesc.DepthBias = 0;
  537. rasterDesc.DepthBiasClamp = 0.0f;
  538. rasterDesc.DepthClipEnable = TRUE;
  539. rasterDesc.FillMode = D3D11_FILL_SOLID;
  540. rasterDesc.FrontCounterClockwise = FALSE;
  541. rasterDesc.MultisampleEnable = FALSE;
  542. rasterDesc.ScissorEnable = FALSE;
  543. rasterDesc.SlopeScaledDepthBias = 0.0f;
  544. result = ID3D11Device_CreateRasterizerState(data->d3dDevice, &rasterDesc, &data->mainRasterizer);
  545. if (FAILED(result)) {
  546. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateRasterizerState [main rasterizer]"), result);
  547. goto done;
  548. }
  549. rasterDesc.ScissorEnable = TRUE;
  550. result = ID3D11Device_CreateRasterizerState(data->d3dDevice, &rasterDesc, &data->clippedRasterizer);
  551. if (FAILED(result)) {
  552. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateRasterizerState [clipped rasterizer]"), result);
  553. goto done;
  554. }
  555. /* Create blending states: */
  556. if (!D3D11_CreateBlendState(renderer, SDL_BLENDMODE_BLEND) ||
  557. !D3D11_CreateBlendState(renderer, SDL_BLENDMODE_ADD) ||
  558. !D3D11_CreateBlendState(renderer, SDL_BLENDMODE_MOD) ||
  559. !D3D11_CreateBlendState(renderer, SDL_BLENDMODE_MUL)) {
  560. /* D3D11_CreateBlendMode will set the SDL error, if it fails */
  561. goto done;
  562. }
  563. /* Setup render state that doesn't change */
  564. ID3D11DeviceContext_IASetInputLayout(data->d3dContext, data->inputLayout);
  565. ID3D11DeviceContext_VSSetShader(data->d3dContext, data->vertexShader, NULL, 0);
  566. ID3D11DeviceContext_VSSetConstantBuffers(data->d3dContext, 0, 1, &data->vertexShaderConstants);
  567. done:
  568. SAFE_RELEASE(d3dDevice);
  569. SAFE_RELEASE(d3dContext);
  570. SAFE_RELEASE(dxgiDevice);
  571. return result;
  572. }
  573. #ifdef __WIN32__
  574. static DXGI_MODE_ROTATION
  575. D3D11_GetCurrentRotation()
  576. {
  577. /* FIXME */
  578. return DXGI_MODE_ROTATION_IDENTITY;
  579. }
  580. #endif /* __WIN32__ */
  581. static BOOL
  582. D3D11_IsDisplayRotated90Degrees(DXGI_MODE_ROTATION rotation)
  583. {
  584. switch (rotation) {
  585. case DXGI_MODE_ROTATION_ROTATE90:
  586. case DXGI_MODE_ROTATION_ROTATE270:
  587. return TRUE;
  588. default:
  589. return FALSE;
  590. }
  591. }
  592. static int
  593. D3D11_GetRotationForCurrentRenderTarget(SDL_Renderer * renderer)
  594. {
  595. D3D11_RenderData *data = (D3D11_RenderData *)renderer->driverdata;
  596. if (data->currentOffscreenRenderTargetView) {
  597. return DXGI_MODE_ROTATION_IDENTITY;
  598. } else {
  599. return data->rotation;
  600. }
  601. }
  602. static int
  603. D3D11_GetViewportAlignedD3DRect(SDL_Renderer * renderer, const SDL_Rect * sdlRect, D3D11_RECT * outRect, BOOL includeViewportOffset)
  604. {
  605. const int rotation = D3D11_GetRotationForCurrentRenderTarget(renderer);
  606. switch (rotation) {
  607. case DXGI_MODE_ROTATION_IDENTITY:
  608. outRect->left = sdlRect->x;
  609. outRect->right = sdlRect->x + sdlRect->w;
  610. outRect->top = sdlRect->y;
  611. outRect->bottom = sdlRect->y + sdlRect->h;
  612. if (includeViewportOffset) {
  613. outRect->left += renderer->viewport.x;
  614. outRect->right += renderer->viewport.x;
  615. outRect->top += renderer->viewport.y;
  616. outRect->bottom += renderer->viewport.y;
  617. }
  618. break;
  619. case DXGI_MODE_ROTATION_ROTATE270:
  620. outRect->left = sdlRect->y;
  621. outRect->right = sdlRect->y + sdlRect->h;
  622. outRect->top = renderer->viewport.w - sdlRect->x - sdlRect->w;
  623. outRect->bottom = renderer->viewport.w - sdlRect->x;
  624. break;
  625. case DXGI_MODE_ROTATION_ROTATE180:
  626. outRect->left = renderer->viewport.w - sdlRect->x - sdlRect->w;
  627. outRect->right = renderer->viewport.w - sdlRect->x;
  628. outRect->top = renderer->viewport.h - sdlRect->y - sdlRect->h;
  629. outRect->bottom = renderer->viewport.h - sdlRect->y;
  630. break;
  631. case DXGI_MODE_ROTATION_ROTATE90:
  632. outRect->left = renderer->viewport.h - sdlRect->y - sdlRect->h;
  633. outRect->right = renderer->viewport.h - sdlRect->y;
  634. outRect->top = sdlRect->x;
  635. outRect->bottom = sdlRect->x + sdlRect->h;
  636. break;
  637. default:
  638. return SDL_SetError("The physical display is in an unknown or unsupported rotation");
  639. }
  640. return 0;
  641. }
  642. static HRESULT
  643. D3D11_CreateSwapChain(SDL_Renderer * renderer, int w, int h)
  644. {
  645. D3D11_RenderData *data = (D3D11_RenderData *)renderer->driverdata;
  646. #ifdef __WINRT__
  647. IUnknown *coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
  648. const BOOL usingXAML = (coreWindow == NULL);
  649. #else
  650. IUnknown *coreWindow = NULL;
  651. const BOOL usingXAML = FALSE;
  652. #endif
  653. HRESULT result = S_OK;
  654. /* Create a swap chain using the same adapter as the existing Direct3D device. */
  655. DXGI_SWAP_CHAIN_DESC1 swapChainDesc;
  656. SDL_zero(swapChainDesc);
  657. swapChainDesc.Width = w;
  658. swapChainDesc.Height = h;
  659. swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; /* This is the most common swap chain format. */
  660. swapChainDesc.Stereo = FALSE;
  661. swapChainDesc.SampleDesc.Count = 1; /* Don't use multi-sampling. */
  662. swapChainDesc.SampleDesc.Quality = 0;
  663. swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  664. swapChainDesc.BufferCount = 2; /* Use double-buffering to minimize latency. */
  665. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  666. swapChainDesc.Scaling = DXGI_SCALING_STRETCH; /* On phone, only stretch and aspect-ratio stretch scaling are allowed. */
  667. swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; /* On phone, no swap effects are supported. */
  668. /* TODO, WinRT: see if Win 8.x DXGI_SWAP_CHAIN_DESC1 settings are available on Windows Phone 8.1, and if there's any advantage to having them on */
  669. #else
  670. if (usingXAML) {
  671. swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
  672. } else {
  673. swapChainDesc.Scaling = DXGI_SCALING_NONE;
  674. }
  675. swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; /* All Windows Store apps must use this SwapEffect. */
  676. #endif
  677. swapChainDesc.Flags = 0;
  678. if (coreWindow) {
  679. result = IDXGIFactory2_CreateSwapChainForCoreWindow(data->dxgiFactory,
  680. (IUnknown *)data->d3dDevice,
  681. coreWindow,
  682. &swapChainDesc,
  683. NULL, /* Allow on all displays. */
  684. &data->swapChain
  685. );
  686. if (FAILED(result)) {
  687. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGIFactory2::CreateSwapChainForCoreWindow"), result);
  688. goto done;
  689. }
  690. } else if (usingXAML) {
  691. result = IDXGIFactory2_CreateSwapChainForComposition(data->dxgiFactory,
  692. (IUnknown *)data->d3dDevice,
  693. &swapChainDesc,
  694. NULL,
  695. &data->swapChain);
  696. if (FAILED(result)) {
  697. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGIFactory2::CreateSwapChainForComposition"), result);
  698. goto done;
  699. }
  700. #if WINAPI_FAMILY == WINAPI_FAMILY_APP
  701. result = ISwapChainBackgroundPanelNative_SetSwapChain(WINRT_GlobalSwapChainBackgroundPanelNative, (IDXGISwapChain *) data->swapChain);
  702. if (FAILED(result)) {
  703. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ISwapChainBackgroundPanelNative::SetSwapChain"), result);
  704. goto done;
  705. }
  706. #else
  707. SDL_SetError(SDL_COMPOSE_ERROR("XAML support is not yet available for Windows Phone"));
  708. result = E_FAIL;
  709. goto done;
  710. #endif
  711. } else {
  712. #ifdef __WIN32__
  713. SDL_SysWMinfo windowinfo;
  714. SDL_VERSION(&windowinfo.version);
  715. SDL_GetWindowWMInfo(renderer->window, &windowinfo);
  716. result = IDXGIFactory2_CreateSwapChainForHwnd(data->dxgiFactory,
  717. (IUnknown *)data->d3dDevice,
  718. windowinfo.info.win.window,
  719. &swapChainDesc,
  720. NULL,
  721. NULL, /* Allow on all displays. */
  722. &data->swapChain
  723. );
  724. if (FAILED(result)) {
  725. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGIFactory2::CreateSwapChainForHwnd"), result);
  726. goto done;
  727. }
  728. IDXGIFactory_MakeWindowAssociation(data->dxgiFactory, windowinfo.info.win.window, DXGI_MWA_NO_WINDOW_CHANGES);
  729. #else
  730. SDL_SetError(__FUNCTION__", Unable to find something to attach a swap chain to");
  731. goto done;
  732. #endif /* ifdef __WIN32__ / else */
  733. }
  734. data->swapEffect = swapChainDesc.SwapEffect;
  735. done:
  736. SAFE_RELEASE(coreWindow);
  737. return result;
  738. }
  739. static void
  740. D3D11_ReleaseMainRenderTargetView(SDL_Renderer * renderer)
  741. {
  742. D3D11_RenderData *data = (D3D11_RenderData *)renderer->driverdata;
  743. ID3D11DeviceContext_OMSetRenderTargets(data->d3dContext, 0, NULL, NULL);
  744. SAFE_RELEASE(data->mainRenderTargetView);
  745. }
  746. static HRESULT D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer);
  747. HRESULT
  748. D3D11_HandleDeviceLost(SDL_Renderer * renderer)
  749. {
  750. HRESULT result = S_OK;
  751. D3D11_ReleaseAll(renderer);
  752. result = D3D11_CreateDeviceResources(renderer);
  753. if (FAILED(result)) {
  754. /* D3D11_CreateDeviceResources will set the SDL error */
  755. return result;
  756. }
  757. result = D3D11_UpdateForWindowSizeChange(renderer);
  758. if (FAILED(result)) {
  759. /* D3D11_UpdateForWindowSizeChange will set the SDL error */
  760. return result;
  761. }
  762. /* Let the application know that the device has been reset */
  763. {
  764. SDL_Event event;
  765. event.type = SDL_RENDER_DEVICE_RESET;
  766. SDL_PushEvent(&event);
  767. }
  768. return S_OK;
  769. }
  770. /* Initialize all resources that change when the window's size changes. */
  771. static HRESULT
  772. D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
  773. {
  774. D3D11_RenderData *data = (D3D11_RenderData *)renderer->driverdata;
  775. ID3D11Texture2D *backBuffer = NULL;
  776. HRESULT result = S_OK;
  777. int w, h;
  778. /* Release the previous render target view */
  779. D3D11_ReleaseMainRenderTargetView(renderer);
  780. /* The width and height of the swap chain must be based on the display's
  781. * non-rotated size.
  782. */
  783. SDL_GetWindowSize(renderer->window, &w, &h);
  784. data->rotation = D3D11_GetCurrentRotation();
  785. /* SDL_Log("%s: windowSize={%d,%d}, orientation=%d\n", __FUNCTION__, w, h, (int)data->rotation); */
  786. if (D3D11_IsDisplayRotated90Degrees(data->rotation)) {
  787. int tmp = w;
  788. w = h;
  789. h = tmp;
  790. }
  791. if (data->swapChain) {
  792. /* IDXGISwapChain::ResizeBuffers is not available on Windows Phone 8. */
  793. #if !defined(__WINRT__) || (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP)
  794. /* If the swap chain already exists, resize it. */
  795. result = IDXGISwapChain_ResizeBuffers(data->swapChain,
  796. 0,
  797. w, h,
  798. DXGI_FORMAT_UNKNOWN,
  799. 0
  800. );
  801. if (result == DXGI_ERROR_DEVICE_REMOVED) {
  802. /* If the device was removed for any reason, a new device and swap chain will need to be created. */
  803. D3D11_HandleDeviceLost(renderer);
  804. /* Everything is set up now. Do not continue execution of this method. HandleDeviceLost will reenter this method
  805. * and correctly set up the new device.
  806. */
  807. goto done;
  808. } else if (FAILED(result)) {
  809. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::ResizeBuffers"), result);
  810. goto done;
  811. }
  812. #endif
  813. } else {
  814. result = D3D11_CreateSwapChain(renderer, w, h);
  815. if (FAILED(result)) {
  816. goto done;
  817. }
  818. }
  819. #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
  820. /* Set the proper rotation for the swap chain.
  821. *
  822. * To note, the call for this, IDXGISwapChain1::SetRotation, is not necessary
  823. * on Windows Phone 8.0, nor is it supported there.
  824. *
  825. * IDXGISwapChain1::SetRotation does seem to be available on Windows Phone 8.1,
  826. * however I've yet to find a way to make it work. It might have something to
  827. * do with IDXGISwapChain::ResizeBuffers appearing to not being available on
  828. * Windows Phone 8.1 (it wasn't on Windows Phone 8.0), but I'm not 100% sure of this.
  829. * The call doesn't appear to be entirely necessary though, and is a performance-related
  830. * call, at least according to the following page on MSDN:
  831. * http://code.msdn.microsoft.com/windowsapps/DXGI-swap-chain-rotation-21d13d71
  832. * -- David L.
  833. *
  834. * TODO, WinRT: reexamine the docs for IDXGISwapChain1::SetRotation, see if might be available, usable, and prudent-to-call on WinPhone 8.1
  835. */
  836. if (data->swapEffect == DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL) {
  837. result = IDXGISwapChain1_SetRotation(data->swapChain, data->rotation);
  838. if (FAILED(result)) {
  839. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain1::SetRotation"), result);
  840. goto done;
  841. }
  842. }
  843. #endif
  844. result = IDXGISwapChain_GetBuffer(data->swapChain,
  845. 0,
  846. &SDL_IID_ID3D11Texture2D,
  847. (void **)&backBuffer
  848. );
  849. if (FAILED(result)) {
  850. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::GetBuffer [back-buffer]"), result);
  851. goto done;
  852. }
  853. /* Create a render target view of the swap chain back buffer. */
  854. result = ID3D11Device_CreateRenderTargetView(data->d3dDevice,
  855. (ID3D11Resource *)backBuffer,
  856. NULL,
  857. &data->mainRenderTargetView
  858. );
  859. if (FAILED(result)) {
  860. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device::CreateRenderTargetView"), result);
  861. goto done;
  862. }
  863. data->viewportDirty = SDL_TRUE;
  864. done:
  865. SAFE_RELEASE(backBuffer);
  866. return result;
  867. }
  868. /* This method is called when the window's size changes. */
  869. static HRESULT
  870. D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer)
  871. {
  872. return D3D11_CreateWindowSizeDependentResources(renderer);
  873. }
  874. void
  875. D3D11_Trim(SDL_Renderer * renderer)
  876. {
  877. #ifdef __WINRT__
  878. #if NTDDI_VERSION > NTDDI_WIN8
  879. D3D11_RenderData *data = (D3D11_RenderData *)renderer->driverdata;
  880. HRESULT result = S_OK;
  881. IDXGIDevice3 *dxgiDevice = NULL;
  882. result = ID3D11Device_QueryInterface(data->d3dDevice, &SDL_IID_IDXGIDevice3, &dxgiDevice);
  883. if (FAILED(result)) {
  884. //WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device to IDXGIDevice3", result);
  885. return;
  886. }
  887. IDXGIDevice3_Trim(dxgiDevice);
  888. SAFE_RELEASE(dxgiDevice);
  889. #endif
  890. #endif
  891. }
  892. static void
  893. D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
  894. {
  895. if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
  896. D3D11_UpdateForWindowSizeChange(renderer);
  897. }
  898. }
  899. static SDL_bool
  900. D3D11_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  901. {
  902. SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
  903. SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
  904. SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
  905. SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
  906. SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
  907. SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
  908. if (!GetBlendFunc(srcColorFactor) || !GetBlendFunc(srcAlphaFactor) ||
  909. !GetBlendEquation(colorOperation) ||
  910. !GetBlendFunc(dstColorFactor) || !GetBlendFunc(dstAlphaFactor) ||
  911. !GetBlendEquation(alphaOperation)) {
  912. return SDL_FALSE;
  913. }
  914. return SDL_TRUE;
  915. }
  916. static int
  917. D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  918. {
  919. D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
  920. D3D11_TextureData *textureData;
  921. HRESULT result;
  922. DXGI_FORMAT textureFormat = SDLPixelFormatToDXGIFormat(texture->format);
  923. D3D11_TEXTURE2D_DESC textureDesc;
  924. D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
  925. if (textureFormat == DXGI_FORMAT_UNKNOWN) {
  926. return SDL_SetError("%s, An unsupported SDL pixel format (0x%x) was specified",
  927. __FUNCTION__, texture->format);
  928. }
  929. textureData = (D3D11_TextureData*) SDL_calloc(1, sizeof(*textureData));
  930. if (!textureData) {
  931. SDL_OutOfMemory();
  932. return -1;
  933. }
  934. textureData->scaleMode = (texture->scaleMode == SDL_ScaleModeNearest) ? D3D11_FILTER_MIN_MAG_MIP_POINT : D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  935. texture->driverdata = textureData;
  936. SDL_zero(textureDesc);
  937. textureDesc.Width = texture->w;
  938. textureDesc.Height = texture->h;
  939. textureDesc.MipLevels = 1;
  940. textureDesc.ArraySize = 1;
  941. textureDesc.Format = textureFormat;
  942. textureDesc.SampleDesc.Count = 1;
  943. textureDesc.SampleDesc.Quality = 0;
  944. textureDesc.MiscFlags = 0;
  945. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  946. textureDesc.Usage = D3D11_USAGE_DYNAMIC;
  947. textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  948. } else {
  949. textureDesc.Usage = D3D11_USAGE_DEFAULT;
  950. textureDesc.CPUAccessFlags = 0;
  951. }
  952. if (texture->access == SDL_TEXTUREACCESS_TARGET) {
  953. textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
  954. } else {
  955. textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  956. }
  957. result = ID3D11Device_CreateTexture2D(rendererData->d3dDevice,
  958. &textureDesc,
  959. NULL,
  960. &textureData->mainTexture
  961. );
  962. if (FAILED(result)) {
  963. D3D11_DestroyTexture(renderer, texture);
  964. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateTexture2D"), result);
  965. return -1;
  966. }
  967. if (texture->format == SDL_PIXELFORMAT_YV12 ||
  968. texture->format == SDL_PIXELFORMAT_IYUV) {
  969. textureData->yuv = SDL_TRUE;
  970. textureDesc.Width = (textureDesc.Width + 1) / 2;
  971. textureDesc.Height = (textureDesc.Height + 1) / 2;
  972. result = ID3D11Device_CreateTexture2D(rendererData->d3dDevice,
  973. &textureDesc,
  974. NULL,
  975. &textureData->mainTextureU
  976. );
  977. if (FAILED(result)) {
  978. D3D11_DestroyTexture(renderer, texture);
  979. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateTexture2D"), result);
  980. return -1;
  981. }
  982. result = ID3D11Device_CreateTexture2D(rendererData->d3dDevice,
  983. &textureDesc,
  984. NULL,
  985. &textureData->mainTextureV
  986. );
  987. if (FAILED(result)) {
  988. D3D11_DestroyTexture(renderer, texture);
  989. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateTexture2D"), result);
  990. return -1;
  991. }
  992. }
  993. if (texture->format == SDL_PIXELFORMAT_NV12 ||
  994. texture->format == SDL_PIXELFORMAT_NV21) {
  995. D3D11_TEXTURE2D_DESC nvTextureDesc = textureDesc;
  996. textureData->nv12 = SDL_TRUE;
  997. nvTextureDesc.Format = DXGI_FORMAT_R8G8_UNORM;
  998. nvTextureDesc.Width = (textureDesc.Width + 1) / 2;
  999. nvTextureDesc.Height = (textureDesc.Height + 1) / 2;
  1000. result = ID3D11Device_CreateTexture2D(rendererData->d3dDevice,
  1001. &nvTextureDesc,
  1002. NULL,
  1003. &textureData->mainTextureNV
  1004. );
  1005. if (FAILED(result)) {
  1006. D3D11_DestroyTexture(renderer, texture);
  1007. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateTexture2D"), result);
  1008. return -1;
  1009. }
  1010. }
  1011. resourceViewDesc.Format = textureDesc.Format;
  1012. resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  1013. resourceViewDesc.Texture2D.MostDetailedMip = 0;
  1014. resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
  1015. result = ID3D11Device_CreateShaderResourceView(rendererData->d3dDevice,
  1016. (ID3D11Resource *)textureData->mainTexture,
  1017. &resourceViewDesc,
  1018. &textureData->mainTextureResourceView
  1019. );
  1020. if (FAILED(result)) {
  1021. D3D11_DestroyTexture(renderer, texture);
  1022. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateShaderResourceView"), result);
  1023. return -1;
  1024. }
  1025. if (textureData->yuv) {
  1026. result = ID3D11Device_CreateShaderResourceView(rendererData->d3dDevice,
  1027. (ID3D11Resource *)textureData->mainTextureU,
  1028. &resourceViewDesc,
  1029. &textureData->mainTextureResourceViewU
  1030. );
  1031. if (FAILED(result)) {
  1032. D3D11_DestroyTexture(renderer, texture);
  1033. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateShaderResourceView"), result);
  1034. return -1;
  1035. }
  1036. result = ID3D11Device_CreateShaderResourceView(rendererData->d3dDevice,
  1037. (ID3D11Resource *)textureData->mainTextureV,
  1038. &resourceViewDesc,
  1039. &textureData->mainTextureResourceViewV
  1040. );
  1041. if (FAILED(result)) {
  1042. D3D11_DestroyTexture(renderer, texture);
  1043. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateShaderResourceView"), result);
  1044. return -1;
  1045. }
  1046. }
  1047. if (textureData->nv12) {
  1048. D3D11_SHADER_RESOURCE_VIEW_DESC nvResourceViewDesc = resourceViewDesc;
  1049. nvResourceViewDesc.Format = DXGI_FORMAT_R8G8_UNORM;
  1050. result = ID3D11Device_CreateShaderResourceView(rendererData->d3dDevice,
  1051. (ID3D11Resource *)textureData->mainTextureNV,
  1052. &nvResourceViewDesc,
  1053. &textureData->mainTextureResourceViewNV
  1054. );
  1055. if (FAILED(result)) {
  1056. D3D11_DestroyTexture(renderer, texture);
  1057. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateShaderResourceView"), result);
  1058. return -1;
  1059. }
  1060. }
  1061. if (texture->access & SDL_TEXTUREACCESS_TARGET) {
  1062. D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
  1063. renderTargetViewDesc.Format = textureDesc.Format;
  1064. renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  1065. renderTargetViewDesc.Texture2D.MipSlice = 0;
  1066. result = ID3D11Device_CreateRenderTargetView(rendererData->d3dDevice,
  1067. (ID3D11Resource *)textureData->mainTexture,
  1068. &renderTargetViewDesc,
  1069. &textureData->mainTextureRenderTargetView);
  1070. if (FAILED(result)) {
  1071. D3D11_DestroyTexture(renderer, texture);
  1072. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateRenderTargetView"), result);
  1073. return -1;
  1074. }
  1075. }
  1076. return 0;
  1077. }
  1078. static void
  1079. D3D11_DestroyTexture(SDL_Renderer * renderer,
  1080. SDL_Texture * texture)
  1081. {
  1082. D3D11_TextureData *data = (D3D11_TextureData *)texture->driverdata;
  1083. if (!data) {
  1084. return;
  1085. }
  1086. SAFE_RELEASE(data->mainTexture);
  1087. SAFE_RELEASE(data->mainTextureResourceView);
  1088. SAFE_RELEASE(data->mainTextureRenderTargetView);
  1089. SAFE_RELEASE(data->stagingTexture);
  1090. SAFE_RELEASE(data->mainTextureU);
  1091. SAFE_RELEASE(data->mainTextureResourceViewU);
  1092. SAFE_RELEASE(data->mainTextureV);
  1093. SAFE_RELEASE(data->mainTextureResourceViewV);
  1094. SDL_free(data->pixels);
  1095. SDL_free(data);
  1096. texture->driverdata = NULL;
  1097. }
  1098. static int
  1099. D3D11_UpdateTextureInternal(D3D11_RenderData *rendererData, ID3D11Texture2D *texture, int bpp, int x, int y, int w, int h, const void *pixels, int pitch)
  1100. {
  1101. ID3D11Texture2D *stagingTexture;
  1102. const Uint8 *src;
  1103. Uint8 *dst;
  1104. int row;
  1105. UINT length;
  1106. HRESULT result;
  1107. D3D11_TEXTURE2D_DESC stagingTextureDesc;
  1108. D3D11_MAPPED_SUBRESOURCE textureMemory;
  1109. /* Create a 'staging' texture, which will be used to write to a portion of the main texture. */
  1110. ID3D11Texture2D_GetDesc(texture, &stagingTextureDesc);
  1111. stagingTextureDesc.Width = w;
  1112. stagingTextureDesc.Height = h;
  1113. stagingTextureDesc.BindFlags = 0;
  1114. stagingTextureDesc.MiscFlags = 0;
  1115. stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  1116. stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
  1117. result = ID3D11Device_CreateTexture2D(rendererData->d3dDevice,
  1118. &stagingTextureDesc,
  1119. NULL,
  1120. &stagingTexture);
  1121. if (FAILED(result)) {
  1122. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateTexture2D [create staging texture]"), result);
  1123. return -1;
  1124. }
  1125. /* Get a write-only pointer to data in the staging texture: */
  1126. result = ID3D11DeviceContext_Map(rendererData->d3dContext,
  1127. (ID3D11Resource *)stagingTexture,
  1128. 0,
  1129. D3D11_MAP_WRITE,
  1130. 0,
  1131. &textureMemory
  1132. );
  1133. if (FAILED(result)) {
  1134. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11DeviceContext1::Map [map staging texture]"), result);
  1135. SAFE_RELEASE(stagingTexture);
  1136. return -1;
  1137. }
  1138. src = (const Uint8 *)pixels;
  1139. dst = textureMemory.pData;
  1140. length = w * bpp;
  1141. if (length == pitch && length == textureMemory.RowPitch) {
  1142. SDL_memcpy(dst, src, length*h);
  1143. } else {
  1144. if (length > (UINT)pitch) {
  1145. length = pitch;
  1146. }
  1147. if (length > textureMemory.RowPitch) {
  1148. length = textureMemory.RowPitch;
  1149. }
  1150. for (row = 0; row < h; ++row) {
  1151. SDL_memcpy(dst, src, length);
  1152. src += pitch;
  1153. dst += textureMemory.RowPitch;
  1154. }
  1155. }
  1156. /* Commit the pixel buffer's changes back to the staging texture: */
  1157. ID3D11DeviceContext_Unmap(rendererData->d3dContext,
  1158. (ID3D11Resource *)stagingTexture,
  1159. 0);
  1160. /* Copy the staging texture's contents back to the texture: */
  1161. ID3D11DeviceContext_CopySubresourceRegion(rendererData->d3dContext,
  1162. (ID3D11Resource *)texture,
  1163. 0,
  1164. x,
  1165. y,
  1166. 0,
  1167. (ID3D11Resource *)stagingTexture,
  1168. 0,
  1169. NULL);
  1170. SAFE_RELEASE(stagingTexture);
  1171. return 0;
  1172. }
  1173. static int
  1174. D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  1175. const SDL_Rect * rect, const void * srcPixels,
  1176. int srcPitch)
  1177. {
  1178. D3D11_RenderData *rendererData = (D3D11_RenderData *)renderer->driverdata;
  1179. D3D11_TextureData *textureData = (D3D11_TextureData *)texture->driverdata;
  1180. if (!textureData) {
  1181. SDL_SetError("Texture is not currently available");
  1182. return -1;
  1183. }
  1184. if (D3D11_UpdateTextureInternal(rendererData, textureData->mainTexture, SDL_BYTESPERPIXEL(texture->format), rect->x, rect->y, rect->w, rect->h, srcPixels, srcPitch) < 0) {
  1185. return -1;
  1186. }
  1187. if (textureData->yuv) {
  1188. /* Skip to the correct offset into the next texture */
  1189. srcPixels = (const void*)((const Uint8*)srcPixels + rect->h * srcPitch);
  1190. if (D3D11_UpdateTextureInternal(rendererData, texture->format == SDL_PIXELFORMAT_YV12 ? textureData->mainTextureV : textureData->mainTextureU, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, srcPixels, (srcPitch + 1) / 2) < 0) {
  1191. return -1;
  1192. }
  1193. /* Skip to the correct offset into the next texture */
  1194. srcPixels = (const void*)((const Uint8*)srcPixels + ((rect->h + 1) / 2) * ((srcPitch + 1) / 2));
  1195. if (D3D11_UpdateTextureInternal(rendererData, texture->format == SDL_PIXELFORMAT_YV12 ? textureData->mainTextureU : textureData->mainTextureV, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, srcPixels, (srcPitch + 1) / 2) < 0) {
  1196. return -1;
  1197. }
  1198. }
  1199. if (textureData->nv12) {
  1200. /* Skip to the correct offset into the next texture */
  1201. srcPixels = (const void*)((const Uint8*)srcPixels + rect->h * srcPitch);
  1202. if (D3D11_UpdateTextureInternal(rendererData, textureData->mainTextureNV, 2, rect->x / 2, rect->y / 2, ((rect->w + 1) / 2), (rect->h + 1) / 2, srcPixels, 2*((srcPitch + 1) / 2)) < 0) {
  1203. return -1;
  1204. }
  1205. }
  1206. return 0;
  1207. }
  1208. static int
  1209. D3D11_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
  1210. const SDL_Rect * rect,
  1211. const Uint8 *Yplane, int Ypitch,
  1212. const Uint8 *Uplane, int Upitch,
  1213. const Uint8 *Vplane, int Vpitch)
  1214. {
  1215. D3D11_RenderData *rendererData = (D3D11_RenderData *)renderer->driverdata;
  1216. D3D11_TextureData *textureData = (D3D11_TextureData *)texture->driverdata;
  1217. if (!textureData) {
  1218. SDL_SetError("Texture is not currently available");
  1219. return -1;
  1220. }
  1221. if (D3D11_UpdateTextureInternal(rendererData, textureData->mainTexture, SDL_BYTESPERPIXEL(texture->format), rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch) < 0) {
  1222. return -1;
  1223. }
  1224. if (D3D11_UpdateTextureInternal(rendererData, textureData->mainTextureU, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Uplane, Upitch) < 0) {
  1225. return -1;
  1226. }
  1227. if (D3D11_UpdateTextureInternal(rendererData, textureData->mainTextureV, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Vplane, Vpitch) < 0) {
  1228. return -1;
  1229. }
  1230. return 0;
  1231. }
  1232. static int
  1233. D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  1234. const SDL_Rect * rect, void **pixels, int *pitch)
  1235. {
  1236. D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
  1237. D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
  1238. HRESULT result = S_OK;
  1239. D3D11_TEXTURE2D_DESC stagingTextureDesc;
  1240. D3D11_MAPPED_SUBRESOURCE textureMemory;
  1241. if (!textureData) {
  1242. SDL_SetError("Texture is not currently available");
  1243. return -1;
  1244. }
  1245. if (textureData->yuv || textureData->nv12) {
  1246. /* It's more efficient to upload directly... */
  1247. if (!textureData->pixels) {
  1248. textureData->pitch = texture->w;
  1249. textureData->pixels = (Uint8 *)SDL_malloc((texture->h * textureData->pitch * 3) / 2);
  1250. if (!textureData->pixels) {
  1251. return SDL_OutOfMemory();
  1252. }
  1253. }
  1254. textureData->locked_rect = *rect;
  1255. *pixels =
  1256. (void *)((Uint8 *)textureData->pixels + rect->y * textureData->pitch +
  1257. rect->x * SDL_BYTESPERPIXEL(texture->format));
  1258. *pitch = textureData->pitch;
  1259. return 0;
  1260. }
  1261. if (textureData->stagingTexture) {
  1262. return SDL_SetError("texture is already locked");
  1263. }
  1264. /* Create a 'staging' texture, which will be used to write to a portion
  1265. * of the main texture. This is necessary, as Direct3D 11.1 does not
  1266. * have the ability to write a CPU-bound pixel buffer to a rectangular
  1267. * subrect of a texture. Direct3D 11.1 can, however, write a pixel
  1268. * buffer to an entire texture, hence the use of a staging texture.
  1269. *
  1270. * TODO, WinRT: consider avoiding the use of a staging texture in D3D11_LockTexture if/when the entire texture is being updated
  1271. */
  1272. ID3D11Texture2D_GetDesc(textureData->mainTexture, &stagingTextureDesc);
  1273. stagingTextureDesc.Width = rect->w;
  1274. stagingTextureDesc.Height = rect->h;
  1275. stagingTextureDesc.BindFlags = 0;
  1276. stagingTextureDesc.MiscFlags = 0;
  1277. stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  1278. stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
  1279. result = ID3D11Device_CreateTexture2D(rendererData->d3dDevice,
  1280. &stagingTextureDesc,
  1281. NULL,
  1282. &textureData->stagingTexture);
  1283. if (FAILED(result)) {
  1284. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateTexture2D [create staging texture]"), result);
  1285. return -1;
  1286. }
  1287. /* Get a write-only pointer to data in the staging texture: */
  1288. result = ID3D11DeviceContext_Map(rendererData->d3dContext,
  1289. (ID3D11Resource *)textureData->stagingTexture,
  1290. 0,
  1291. D3D11_MAP_WRITE,
  1292. 0,
  1293. &textureMemory
  1294. );
  1295. if (FAILED(result)) {
  1296. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11DeviceContext1::Map [map staging texture]"), result);
  1297. SAFE_RELEASE(textureData->stagingTexture);
  1298. return -1;
  1299. }
  1300. /* Make note of where the staging texture will be written to
  1301. * (on a call to SDL_UnlockTexture):
  1302. */
  1303. textureData->lockedTexturePositionX = rect->x;
  1304. textureData->lockedTexturePositionY = rect->y;
  1305. /* Make sure the caller has information on the texture's pixel buffer,
  1306. * then return:
  1307. */
  1308. *pixels = textureMemory.pData;
  1309. *pitch = textureMemory.RowPitch;
  1310. return 0;
  1311. }
  1312. static void
  1313. D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  1314. {
  1315. D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
  1316. D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
  1317. if (!textureData) {
  1318. return;
  1319. }
  1320. if (textureData->yuv || textureData->nv12) {
  1321. const SDL_Rect *rect = &textureData->locked_rect;
  1322. void *pixels =
  1323. (void *) ((Uint8 *) textureData->pixels + rect->y * textureData->pitch +
  1324. rect->x * SDL_BYTESPERPIXEL(texture->format));
  1325. D3D11_UpdateTexture(renderer, texture, rect, pixels, textureData->pitch);
  1326. return;
  1327. }
  1328. /* Commit the pixel buffer's changes back to the staging texture: */
  1329. ID3D11DeviceContext_Unmap(rendererData->d3dContext,
  1330. (ID3D11Resource *)textureData->stagingTexture,
  1331. 0);
  1332. /* Copy the staging texture's contents back to the main texture: */
  1333. ID3D11DeviceContext_CopySubresourceRegion(rendererData->d3dContext,
  1334. (ID3D11Resource *)textureData->mainTexture,
  1335. 0,
  1336. textureData->lockedTexturePositionX,
  1337. textureData->lockedTexturePositionY,
  1338. 0,
  1339. (ID3D11Resource *)textureData->stagingTexture,
  1340. 0,
  1341. NULL);
  1342. SAFE_RELEASE(textureData->stagingTexture);
  1343. }
  1344. static void
  1345. D3D11_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture, SDL_ScaleMode scaleMode)
  1346. {
  1347. D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
  1348. if (!textureData) {
  1349. return;
  1350. }
  1351. textureData->scaleMode = (scaleMode == SDL_ScaleModeNearest) ? D3D11_FILTER_MIN_MAG_MIP_POINT : D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  1352. }
  1353. static int
  1354. D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
  1355. {
  1356. D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
  1357. D3D11_TextureData *textureData = NULL;
  1358. if (texture == NULL) {
  1359. rendererData->currentOffscreenRenderTargetView = NULL;
  1360. return 0;
  1361. }
  1362. textureData = (D3D11_TextureData *) texture->driverdata;
  1363. if (!textureData->mainTextureRenderTargetView) {
  1364. return SDL_SetError("specified texture is not a render target");
  1365. }
  1366. rendererData->currentOffscreenRenderTargetView = textureData->mainTextureRenderTargetView;
  1367. return 0;
  1368. }
  1369. static int
  1370. D3D11_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
  1371. {
  1372. return 0; /* nothing to do in this backend. */
  1373. }
  1374. static int
  1375. D3D11_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
  1376. {
  1377. VertexPositionColor *verts = (VertexPositionColor *) SDL_AllocateRenderVertices(renderer, count * sizeof (VertexPositionColor), 0, &cmd->data.draw.first);
  1378. const float r = (float)(cmd->data.draw.r / 255.0f);
  1379. const float g = (float)(cmd->data.draw.g / 255.0f);
  1380. const float b = (float)(cmd->data.draw.b / 255.0f);
  1381. const float a = (float)(cmd->data.draw.a / 255.0f);
  1382. int i;
  1383. if (!verts) {
  1384. return -1;
  1385. }
  1386. cmd->data.draw.count = count;
  1387. for (i = 0; i < count; i++) {
  1388. verts->pos.x = points[i].x + 0.5f;
  1389. verts->pos.y = points[i].y + 0.5f;
  1390. verts->pos.z = 0.0f;
  1391. verts->tex.x = 0.0f;
  1392. verts->tex.y = 0.0f;
  1393. verts->color.x = r;
  1394. verts->color.y = g;
  1395. verts->color.z = b;
  1396. verts->color.w = a;
  1397. verts++;
  1398. }
  1399. return 0;
  1400. }
  1401. static int
  1402. D3D11_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
  1403. {
  1404. VertexPositionColor *verts = (VertexPositionColor *) SDL_AllocateRenderVertices(renderer, count * 4 * sizeof (VertexPositionColor), 0, &cmd->data.draw.first);
  1405. const float r = (float)(cmd->data.draw.r / 255.0f);
  1406. const float g = (float)(cmd->data.draw.g / 255.0f);
  1407. const float b = (float)(cmd->data.draw.b / 255.0f);
  1408. const float a = (float)(cmd->data.draw.a / 255.0f);
  1409. int i;
  1410. if (!verts) {
  1411. return -1;
  1412. }
  1413. cmd->data.draw.count = count;
  1414. for (i = 0; i < count; i++) {
  1415. verts->pos.x = rects[i].x;
  1416. verts->pos.y = rects[i].y;
  1417. verts->pos.z = 0.0f;
  1418. verts->tex.x = 0.0f;
  1419. verts->tex.y = 0.0f;
  1420. verts->color.x = r;
  1421. verts->color.y = g;
  1422. verts->color.z = b;
  1423. verts->color.w = a;
  1424. verts++;
  1425. verts->pos.x = rects[i].x;
  1426. verts->pos.y = rects[i].y + rects[i].h;
  1427. verts->pos.z = 0.0f;
  1428. verts->tex.x = 0.0f;
  1429. verts->tex.y = 0.0f;
  1430. verts->color.x = r;
  1431. verts->color.y = g;
  1432. verts->color.z = b;
  1433. verts->color.w = a;
  1434. verts++;
  1435. verts->pos.x = rects[i].x + rects[i].w;
  1436. verts->pos.y = rects[i].y;
  1437. verts->pos.z = 0.0f;
  1438. verts->tex.x = 0.0f;
  1439. verts->tex.y = 0.0f;
  1440. verts->color.x = r;
  1441. verts->color.y = g;
  1442. verts->color.z = b;
  1443. verts->color.w = a;
  1444. verts++;
  1445. verts->pos.x = rects[i].x + rects[i].w;
  1446. verts->pos.y = rects[i].y + rects[i].h;
  1447. verts->pos.z = 0.0f;
  1448. verts->tex.x = 0.0f;
  1449. verts->tex.y = 0.0f;
  1450. verts->color.x = r;
  1451. verts->color.y = g;
  1452. verts->color.z = b;
  1453. verts->color.w = a;
  1454. verts++;
  1455. }
  1456. return 0;
  1457. }
  1458. static int
  1459. D3D11_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
  1460. const SDL_Rect * srcrect, const SDL_FRect * dstrect)
  1461. {
  1462. VertexPositionColor *verts = (VertexPositionColor *) SDL_AllocateRenderVertices(renderer, 4 * sizeof (VertexPositionColor), 0, &cmd->data.draw.first);
  1463. const float r = (float)(cmd->data.draw.r / 255.0f);
  1464. const float g = (float)(cmd->data.draw.g / 255.0f);
  1465. const float b = (float)(cmd->data.draw.b / 255.0f);
  1466. const float a = (float)(cmd->data.draw.a / 255.0f);
  1467. const float minu = (float) srcrect->x / texture->w;
  1468. const float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
  1469. const float minv = (float) srcrect->y / texture->h;
  1470. const float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
  1471. if (!verts) {
  1472. return -1;
  1473. }
  1474. cmd->data.draw.count = 1;
  1475. verts->pos.x = dstrect->x;
  1476. verts->pos.y = dstrect->y;
  1477. verts->pos.z = 0.0f;
  1478. verts->tex.x = minu;
  1479. verts->tex.y = minv;
  1480. verts->color.x = r;
  1481. verts->color.y = g;
  1482. verts->color.z = b;
  1483. verts->color.w = a;
  1484. verts++;
  1485. verts->pos.x = dstrect->x;
  1486. verts->pos.y = dstrect->y + dstrect->h;
  1487. verts->pos.z = 0.0f;
  1488. verts->tex.x = minu;
  1489. verts->tex.y = maxv;
  1490. verts->color.x = r;
  1491. verts->color.y = g;
  1492. verts->color.z = b;
  1493. verts->color.w = a;
  1494. verts++;
  1495. verts->pos.x = dstrect->x + dstrect->w;
  1496. verts->pos.y = dstrect->y;
  1497. verts->pos.z = 0.0f;
  1498. verts->tex.x = maxu;
  1499. verts->tex.y = minv;
  1500. verts->color.x = r;
  1501. verts->color.y = g;
  1502. verts->color.z = b;
  1503. verts->color.w = a;
  1504. verts++;
  1505. verts->pos.x = dstrect->x + dstrect->w;
  1506. verts->pos.y = dstrect->y + dstrect->h;
  1507. verts->pos.z = 0.0f;
  1508. verts->tex.x = maxu;
  1509. verts->tex.y = maxv;
  1510. verts->color.x = r;
  1511. verts->color.y = g;
  1512. verts->color.z = b;
  1513. verts->color.w = a;
  1514. verts++;
  1515. return 0;
  1516. }
  1517. static int
  1518. D3D11_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
  1519. const SDL_Rect * srcrect, const SDL_FRect * dstrect,
  1520. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
  1521. {
  1522. VertexPositionColor *verts = (VertexPositionColor *) SDL_AllocateRenderVertices(renderer, 5 * sizeof (VertexPositionColor), 0, &cmd->data.draw.first);
  1523. const float r = (float)(cmd->data.draw.r / 255.0f);
  1524. const float g = (float)(cmd->data.draw.g / 255.0f);
  1525. const float b = (float)(cmd->data.draw.b / 255.0f);
  1526. const float a = (float)(cmd->data.draw.a / 255.0f);
  1527. float minx, miny, maxx, maxy;
  1528. float minu, maxu, minv, maxv;
  1529. if (!verts) {
  1530. return -1;
  1531. }
  1532. cmd->data.draw.count = 1;
  1533. minx = -center->x;
  1534. maxx = dstrect->w - center->x;
  1535. miny = -center->y;
  1536. maxy = dstrect->h - center->y;
  1537. if (flip & SDL_FLIP_HORIZONTAL) {
  1538. minu = (float) (srcrect->x + srcrect->w) / texture->w;
  1539. maxu = (float) srcrect->x / texture->w;
  1540. } else {
  1541. minu = (float) srcrect->x / texture->w;
  1542. maxu = (float) (srcrect->x + srcrect->w) / texture->w;
  1543. }
  1544. if (flip & SDL_FLIP_VERTICAL) {
  1545. minv = (float) (srcrect->y + srcrect->h) / texture->h;
  1546. maxv = (float) srcrect->y / texture->h;
  1547. } else {
  1548. minv = (float) srcrect->y / texture->h;
  1549. maxv = (float) (srcrect->y + srcrect->h) / texture->h;
  1550. }
  1551. verts->pos.x = minx;
  1552. verts->pos.y = miny;
  1553. verts->pos.z = 0.0f;
  1554. verts->color.x = r;
  1555. verts->color.y = g;
  1556. verts->color.z = b;
  1557. verts->color.w = a;
  1558. verts->tex.x = minu;
  1559. verts->tex.y = minv;
  1560. verts++;
  1561. verts->pos.x = minx;
  1562. verts->pos.y = maxy;
  1563. verts->pos.z = 0.0f;
  1564. verts->color.x = r;
  1565. verts->color.y = g;
  1566. verts->color.z = b;
  1567. verts->color.w = a;
  1568. verts->tex.x = minu;
  1569. verts->tex.y = maxv;
  1570. verts++;
  1571. verts->pos.x = maxx;
  1572. verts->pos.y = miny;
  1573. verts->pos.z = 0.0f;
  1574. verts->color.x = r;
  1575. verts->color.y = g;
  1576. verts->color.z = b;
  1577. verts->color.w = a;
  1578. verts->tex.x = maxu;
  1579. verts->tex.y = minv;
  1580. verts++;
  1581. verts->pos.x = maxx;
  1582. verts->pos.y = maxy;
  1583. verts->pos.z = 0.0f;
  1584. verts->color.x = r;
  1585. verts->color.y = g;
  1586. verts->color.z = b;
  1587. verts->color.w = a;
  1588. verts->tex.x = maxu;
  1589. verts->tex.y = maxv;
  1590. verts++;
  1591. verts->pos.x = dstrect->x + center->x; /* X translation */
  1592. verts->pos.y = dstrect->y + center->y; /* Y translation */
  1593. verts->pos.z = (float)(M_PI * (float) angle / 180.0f); /* rotation */
  1594. verts->color.x = 0;
  1595. verts->color.y = 0;
  1596. verts->color.z = 0;
  1597. verts->color.w = 0;
  1598. verts->tex.x = 0.0f;
  1599. verts->tex.y = 0.0f;
  1600. verts++;
  1601. return 0;
  1602. }
  1603. static int
  1604. D3D11_UpdateVertexBuffer(SDL_Renderer *renderer,
  1605. const void * vertexData, size_t dataSizeInBytes)
  1606. {
  1607. D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
  1608. HRESULT result = S_OK;
  1609. const int vbidx = rendererData->currentVertexBuffer;
  1610. const UINT stride = sizeof(VertexPositionColor);
  1611. const UINT offset = 0;
  1612. if (dataSizeInBytes == 0) {
  1613. return 0; /* nothing to do. */
  1614. }
  1615. if (rendererData->vertexBuffers[vbidx] && rendererData->vertexBufferSizes[vbidx] >= dataSizeInBytes) {
  1616. D3D11_MAPPED_SUBRESOURCE mappedResource;
  1617. result = ID3D11DeviceContext_Map(rendererData->d3dContext,
  1618. (ID3D11Resource *)rendererData->vertexBuffers[vbidx],
  1619. 0,
  1620. D3D11_MAP_WRITE_DISCARD,
  1621. 0,
  1622. &mappedResource
  1623. );
  1624. if (FAILED(result)) {
  1625. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11DeviceContext1::Map [vertex buffer]"), result);
  1626. return -1;
  1627. }
  1628. SDL_memcpy(mappedResource.pData, vertexData, dataSizeInBytes);
  1629. ID3D11DeviceContext_Unmap(rendererData->d3dContext, (ID3D11Resource *)rendererData->vertexBuffers[vbidx], 0);
  1630. } else {
  1631. D3D11_BUFFER_DESC vertexBufferDesc;
  1632. D3D11_SUBRESOURCE_DATA vertexBufferData;
  1633. SAFE_RELEASE(rendererData->vertexBuffers[vbidx]);
  1634. SDL_zero(vertexBufferDesc);
  1635. vertexBufferDesc.ByteWidth = (UINT) dataSizeInBytes;
  1636. vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
  1637. vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  1638. vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  1639. SDL_zero(vertexBufferData);
  1640. vertexBufferData.pSysMem = vertexData;
  1641. vertexBufferData.SysMemPitch = 0;
  1642. vertexBufferData.SysMemSlicePitch = 0;
  1643. result = ID3D11Device_CreateBuffer(rendererData->d3dDevice,
  1644. &vertexBufferDesc,
  1645. &vertexBufferData,
  1646. &rendererData->vertexBuffers[vbidx]
  1647. );
  1648. if (FAILED(result)) {
  1649. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateBuffer [vertex buffer]"), result);
  1650. return -1;
  1651. }
  1652. rendererData->vertexBufferSizes[vbidx] = dataSizeInBytes;
  1653. }
  1654. ID3D11DeviceContext_IASetVertexBuffers(rendererData->d3dContext,
  1655. 0,
  1656. 1,
  1657. &rendererData->vertexBuffers[vbidx],
  1658. &stride,
  1659. &offset
  1660. );
  1661. rendererData->currentVertexBuffer++;
  1662. if (rendererData->currentVertexBuffer >= SDL_arraysize(rendererData->vertexBuffers)) {
  1663. rendererData->currentVertexBuffer = 0;
  1664. }
  1665. return 0;
  1666. }
  1667. static int
  1668. D3D11_UpdateViewport(SDL_Renderer * renderer)
  1669. {
  1670. D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  1671. const SDL_Rect *viewport = &data->currentViewport;
  1672. Float4X4 projection;
  1673. Float4X4 view;
  1674. SDL_FRect orientationAlignedViewport;
  1675. BOOL swapDimensions;
  1676. D3D11_VIEWPORT d3dviewport;
  1677. const int rotation = D3D11_GetRotationForCurrentRenderTarget(renderer);
  1678. if (viewport->w == 0 || viewport->h == 0) {
  1679. /* If the viewport is empty, assume that it is because
  1680. * SDL_CreateRenderer is calling it, and will call it again later
  1681. * with a non-empty viewport.
  1682. */
  1683. /* SDL_Log("%s, no viewport was set!\n", __FUNCTION__); */
  1684. return -1;
  1685. }
  1686. /* Make sure the SDL viewport gets rotated to that of the physical display's rotation.
  1687. * Keep in mind here that the Y-axis will be been inverted (from Direct3D's
  1688. * default coordinate system) so rotations will be done in the opposite
  1689. * direction of the DXGI_MODE_ROTATION enumeration.
  1690. */
  1691. switch (rotation) {
  1692. case DXGI_MODE_ROTATION_IDENTITY:
  1693. projection = MatrixIdentity();
  1694. break;
  1695. case DXGI_MODE_ROTATION_ROTATE270:
  1696. projection = MatrixRotationZ(SDL_static_cast(float, M_PI * 0.5f));
  1697. break;
  1698. case DXGI_MODE_ROTATION_ROTATE180:
  1699. projection = MatrixRotationZ(SDL_static_cast(float, M_PI));
  1700. break;
  1701. case DXGI_MODE_ROTATION_ROTATE90:
  1702. projection = MatrixRotationZ(SDL_static_cast(float, -M_PI * 0.5f));
  1703. break;
  1704. default:
  1705. return SDL_SetError("An unknown DisplayOrientation is being used");
  1706. }
  1707. /* Update the view matrix */
  1708. SDL_zero(view);
  1709. view.m[0][0] = 2.0f / viewport->w;
  1710. view.m[1][1] = -2.0f / viewport->h;
  1711. view.m[2][2] = 1.0f;
  1712. view.m[3][0] = -1.0f;
  1713. view.m[3][1] = 1.0f;
  1714. view.m[3][3] = 1.0f;
  1715. /* Combine the projection + view matrix together now, as both only get
  1716. * set here (as of this writing, on Dec 26, 2013). When done, store it
  1717. * for eventual transfer to the GPU.
  1718. */
  1719. data->vertexShaderConstantsData.projectionAndView = MatrixMultiply(
  1720. view,
  1721. projection);
  1722. /* Update the Direct3D viewport, which seems to be aligned to the
  1723. * swap buffer's coordinate space, which is always in either
  1724. * a landscape mode, for all Windows 8/RT devices, or a portrait mode,
  1725. * for Windows Phone devices.
  1726. */
  1727. swapDimensions = D3D11_IsDisplayRotated90Degrees(rotation);
  1728. if (swapDimensions) {
  1729. orientationAlignedViewport.x = (float) viewport->y;
  1730. orientationAlignedViewport.y = (float) viewport->x;
  1731. orientationAlignedViewport.w = (float) viewport->h;
  1732. orientationAlignedViewport.h = (float) viewport->w;
  1733. } else {
  1734. orientationAlignedViewport.x = (float) viewport->x;
  1735. orientationAlignedViewport.y = (float) viewport->y;
  1736. orientationAlignedViewport.w = (float) viewport->w;
  1737. orientationAlignedViewport.h = (float) viewport->h;
  1738. }
  1739. /* TODO, WinRT: get custom viewports working with non-Landscape modes (Portrait, PortraitFlipped, and LandscapeFlipped) */
  1740. d3dviewport.TopLeftX = orientationAlignedViewport.x;
  1741. d3dviewport.TopLeftY = orientationAlignedViewport.y;
  1742. d3dviewport.Width = orientationAlignedViewport.w;
  1743. d3dviewport.Height = orientationAlignedViewport.h;
  1744. d3dviewport.MinDepth = 0.0f;
  1745. d3dviewport.MaxDepth = 1.0f;
  1746. /* SDL_Log("%s: D3D viewport = {%f,%f,%f,%f}\n", __FUNCTION__, d3dviewport.TopLeftX, d3dviewport.TopLeftY, d3dviewport.Width, d3dviewport.Height); */
  1747. ID3D11DeviceContext_RSSetViewports(data->d3dContext, 1, &d3dviewport);
  1748. data->viewportDirty = SDL_FALSE;
  1749. return 0;
  1750. }
  1751. static ID3D11RenderTargetView *
  1752. D3D11_GetCurrentRenderTargetView(SDL_Renderer * renderer)
  1753. {
  1754. D3D11_RenderData *data = (D3D11_RenderData *)renderer->driverdata;
  1755. if (data->currentOffscreenRenderTargetView) {
  1756. return data->currentOffscreenRenderTargetView;
  1757. }
  1758. else {
  1759. return data->mainRenderTargetView;
  1760. }
  1761. }
  1762. static int
  1763. D3D11_SetDrawState(SDL_Renderer * renderer, const SDL_RenderCommand *cmd, ID3D11PixelShader * shader,
  1764. const int numShaderResources, ID3D11ShaderResourceView ** shaderResources,
  1765. ID3D11SamplerState * sampler, const Float4X4 *matrix)
  1766. {
  1767. D3D11_RenderData *rendererData = (D3D11_RenderData *)renderer->driverdata;
  1768. const Float4X4 *newmatrix = matrix ? matrix : &rendererData->identity;
  1769. ID3D11RasterizerState *rasterizerState;
  1770. ID3D11RenderTargetView *renderTargetView = D3D11_GetCurrentRenderTargetView(renderer);
  1771. ID3D11ShaderResourceView *shaderResource;
  1772. const SDL_BlendMode blendMode = cmd->data.draw.blend;
  1773. ID3D11BlendState *blendState = NULL;
  1774. SDL_bool updateSubresource = SDL_FALSE;
  1775. if (renderTargetView != rendererData->currentRenderTargetView) {
  1776. ID3D11DeviceContext_OMSetRenderTargets(rendererData->d3dContext,
  1777. 1,
  1778. &renderTargetView,
  1779. NULL
  1780. );
  1781. rendererData->currentRenderTargetView = renderTargetView;
  1782. }
  1783. if (rendererData->viewportDirty) {
  1784. if (D3D11_UpdateViewport(renderer) == 0) {
  1785. /* vertexShaderConstantsData.projectionAndView has changed */
  1786. updateSubresource = SDL_TRUE;
  1787. }
  1788. }
  1789. if (rendererData->cliprectDirty) {
  1790. if (!rendererData->currentCliprectEnabled) {
  1791. ID3D11DeviceContext_RSSetScissorRects(rendererData->d3dContext, 0, NULL);
  1792. } else {
  1793. D3D11_RECT scissorRect;
  1794. if (D3D11_GetViewportAlignedD3DRect(renderer, &rendererData->currentCliprect, &scissorRect, TRUE) != 0) {
  1795. /* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
  1796. return -1;
  1797. }
  1798. ID3D11DeviceContext_RSSetScissorRects(rendererData->d3dContext, 1, &scissorRect);
  1799. }
  1800. rendererData->cliprectDirty = SDL_FALSE;
  1801. }
  1802. if (!rendererData->currentCliprectEnabled) {
  1803. rasterizerState = rendererData->mainRasterizer;
  1804. } else {
  1805. rasterizerState = rendererData->clippedRasterizer;
  1806. }
  1807. if (rasterizerState != rendererData->currentRasterizerState) {
  1808. ID3D11DeviceContext_RSSetState(rendererData->d3dContext, rasterizerState);
  1809. rendererData->currentRasterizerState = rasterizerState;
  1810. }
  1811. if (blendMode != SDL_BLENDMODE_NONE) {
  1812. int i;
  1813. for (i = 0; i < rendererData->blendModesCount; ++i) {
  1814. if (blendMode == rendererData->blendModes[i].blendMode) {
  1815. blendState = rendererData->blendModes[i].blendState;
  1816. break;
  1817. }
  1818. }
  1819. if (!blendState) {
  1820. blendState = D3D11_CreateBlendState(renderer, blendMode);
  1821. if (!blendState) {
  1822. return -1;
  1823. }
  1824. }
  1825. }
  1826. if (blendState != rendererData->currentBlendState) {
  1827. ID3D11DeviceContext_OMSetBlendState(rendererData->d3dContext, blendState, 0, 0xFFFFFFFF);
  1828. rendererData->currentBlendState = blendState;
  1829. }
  1830. if (shader != rendererData->currentShader) {
  1831. ID3D11DeviceContext_PSSetShader(rendererData->d3dContext, shader, NULL, 0);
  1832. rendererData->currentShader = shader;
  1833. }
  1834. if (numShaderResources > 0) {
  1835. shaderResource = shaderResources[0];
  1836. } else {
  1837. shaderResource = NULL;
  1838. }
  1839. if (shaderResource != rendererData->currentShaderResource) {
  1840. ID3D11DeviceContext_PSSetShaderResources(rendererData->d3dContext, 0, numShaderResources, shaderResources);
  1841. rendererData->currentShaderResource = shaderResource;
  1842. }
  1843. if (sampler != rendererData->currentSampler) {
  1844. ID3D11DeviceContext_PSSetSamplers(rendererData->d3dContext, 0, 1, &sampler);
  1845. rendererData->currentSampler = sampler;
  1846. }
  1847. if (updateSubresource == SDL_TRUE || SDL_memcmp(&rendererData->vertexShaderConstantsData.model, newmatrix, sizeof (*newmatrix)) != 0) {
  1848. SDL_memcpy(&rendererData->vertexShaderConstantsData.model, newmatrix, sizeof (*newmatrix));
  1849. ID3D11DeviceContext_UpdateSubresource(rendererData->d3dContext,
  1850. (ID3D11Resource *)rendererData->vertexShaderConstants,
  1851. 0,
  1852. NULL,
  1853. &rendererData->vertexShaderConstantsData,
  1854. 0,
  1855. 0
  1856. );
  1857. }
  1858. return 0;
  1859. }
  1860. static int
  1861. D3D11_SetCopyState(SDL_Renderer * renderer, const SDL_RenderCommand *cmd, const Float4X4 *matrix)
  1862. {
  1863. SDL_Texture *texture = cmd->data.draw.texture;
  1864. D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
  1865. D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
  1866. ID3D11SamplerState *textureSampler;
  1867. switch (textureData->scaleMode) {
  1868. case D3D11_FILTER_MIN_MAG_MIP_POINT:
  1869. textureSampler = rendererData->nearestPixelSampler;
  1870. break;
  1871. case D3D11_FILTER_MIN_MAG_MIP_LINEAR:
  1872. textureSampler = rendererData->linearSampler;
  1873. break;
  1874. default:
  1875. return SDL_SetError("Unknown scale mode: %d\n", textureData->scaleMode);
  1876. }
  1877. if (textureData->yuv) {
  1878. ID3D11ShaderResourceView *shaderResources[] = {
  1879. textureData->mainTextureResourceView,
  1880. textureData->mainTextureResourceViewU,
  1881. textureData->mainTextureResourceViewV
  1882. };
  1883. D3D11_Shader shader;
  1884. switch (SDL_GetYUVConversionModeForResolution(texture->w, texture->h)) {
  1885. case SDL_YUV_CONVERSION_JPEG:
  1886. shader = SHADER_YUV_JPEG;
  1887. break;
  1888. case SDL_YUV_CONVERSION_BT601:
  1889. shader = SHADER_YUV_BT601;
  1890. break;
  1891. case SDL_YUV_CONVERSION_BT709:
  1892. shader = SHADER_YUV_BT709;
  1893. break;
  1894. default:
  1895. return SDL_SetError("Unsupported YUV conversion mode");
  1896. }
  1897. return D3D11_SetDrawState(renderer, cmd, rendererData->pixelShaders[shader],
  1898. SDL_arraysize(shaderResources), shaderResources, textureSampler, matrix);
  1899. } else if (textureData->nv12) {
  1900. ID3D11ShaderResourceView *shaderResources[] = {
  1901. textureData->mainTextureResourceView,
  1902. textureData->mainTextureResourceViewNV,
  1903. };
  1904. D3D11_Shader shader;
  1905. switch (SDL_GetYUVConversionModeForResolution(texture->w, texture->h)) {
  1906. case SDL_YUV_CONVERSION_JPEG:
  1907. shader = texture->format == SDL_PIXELFORMAT_NV12 ? SHADER_NV12_JPEG : SHADER_NV21_JPEG;
  1908. break;
  1909. case SDL_YUV_CONVERSION_BT601:
  1910. shader = texture->format == SDL_PIXELFORMAT_NV12 ? SHADER_NV12_BT601 : SHADER_NV21_BT601;
  1911. break;
  1912. case SDL_YUV_CONVERSION_BT709:
  1913. shader = texture->format == SDL_PIXELFORMAT_NV12 ? SHADER_NV12_BT709 : SHADER_NV21_BT709;
  1914. break;
  1915. default:
  1916. return SDL_SetError("Unsupported YUV conversion mode");
  1917. }
  1918. return D3D11_SetDrawState(renderer, cmd, rendererData->pixelShaders[shader],
  1919. SDL_arraysize(shaderResources), shaderResources, textureSampler, matrix);
  1920. }
  1921. return D3D11_SetDrawState(renderer, cmd, rendererData->pixelShaders[SHADER_RGB],
  1922. 1, &textureData->mainTextureResourceView, textureSampler, matrix);
  1923. }
  1924. static void
  1925. D3D11_DrawPrimitives(SDL_Renderer * renderer, D3D11_PRIMITIVE_TOPOLOGY primitiveTopology, const size_t vertexStart, const size_t vertexCount)
  1926. {
  1927. D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
  1928. ID3D11DeviceContext_IASetPrimitiveTopology(rendererData->d3dContext, primitiveTopology);
  1929. ID3D11DeviceContext_Draw(rendererData->d3dContext, (UINT) vertexCount, (UINT) vertexStart);
  1930. }
  1931. static int
  1932. D3D11_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
  1933. {
  1934. D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
  1935. const int viewportRotation = D3D11_GetRotationForCurrentRenderTarget(renderer);
  1936. size_t i;
  1937. if (rendererData->currentViewportRotation != viewportRotation) {
  1938. rendererData->currentViewportRotation = viewportRotation;
  1939. rendererData->viewportDirty = SDL_TRUE;
  1940. }
  1941. if (D3D11_UpdateVertexBuffer(renderer, vertices, vertsize) < 0) {
  1942. return -1;
  1943. }
  1944. while (cmd) {
  1945. switch (cmd->command) {
  1946. case SDL_RENDERCMD_SETDRAWCOLOR: {
  1947. break; /* this isn't currently used in this render backend. */
  1948. }
  1949. case SDL_RENDERCMD_SETVIEWPORT: {
  1950. SDL_Rect *viewport = &rendererData->currentViewport;
  1951. if (SDL_memcmp(viewport, &cmd->data.viewport.rect, sizeof (SDL_Rect)) != 0) {
  1952. SDL_memcpy(viewport, &cmd->data.viewport.rect, sizeof (SDL_Rect));
  1953. rendererData->viewportDirty = SDL_TRUE;
  1954. }
  1955. break;
  1956. }
  1957. case SDL_RENDERCMD_SETCLIPRECT: {
  1958. const SDL_Rect *rect = &cmd->data.cliprect.rect;
  1959. if (rendererData->currentCliprectEnabled != cmd->data.cliprect.enabled) {
  1960. rendererData->currentCliprectEnabled = cmd->data.cliprect.enabled;
  1961. rendererData->cliprectDirty = SDL_TRUE;
  1962. }
  1963. if (SDL_memcmp(&rendererData->currentCliprect, rect, sizeof (SDL_Rect)) != 0) {
  1964. SDL_memcpy(&rendererData->currentCliprect, rect, sizeof (SDL_Rect));
  1965. rendererData->cliprectDirty = SDL_TRUE;
  1966. }
  1967. break;
  1968. }
  1969. case SDL_RENDERCMD_CLEAR: {
  1970. const float colorRGBA[] = {
  1971. (cmd->data.color.r / 255.0f),
  1972. (cmd->data.color.g / 255.0f),
  1973. (cmd->data.color.b / 255.0f),
  1974. (cmd->data.color.a / 255.0f)
  1975. };
  1976. ID3D11DeviceContext_ClearRenderTargetView(rendererData->d3dContext, D3D11_GetCurrentRenderTargetView(renderer), colorRGBA);
  1977. break;
  1978. }
  1979. case SDL_RENDERCMD_DRAW_POINTS: {
  1980. const size_t count = cmd->data.draw.count;
  1981. const size_t first = cmd->data.draw.first;
  1982. const size_t start = first / sizeof (VertexPositionColor);
  1983. D3D11_SetDrawState(renderer, cmd, rendererData->pixelShaders[SHADER_SOLID], 0, NULL, NULL, NULL);
  1984. D3D11_DrawPrimitives(renderer, D3D11_PRIMITIVE_TOPOLOGY_POINTLIST, start, count);
  1985. break;
  1986. }
  1987. case SDL_RENDERCMD_DRAW_LINES: {
  1988. const size_t count = cmd->data.draw.count;
  1989. const size_t first = cmd->data.draw.first;
  1990. const size_t start = first / sizeof (VertexPositionColor);
  1991. const VertexPositionColor *verts = (VertexPositionColor *) (((Uint8 *) vertices) + first);
  1992. D3D11_SetDrawState(renderer, cmd, rendererData->pixelShaders[SHADER_SOLID], 0, NULL, NULL, NULL);
  1993. D3D11_DrawPrimitives(renderer, D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, start, count);
  1994. if (verts[0].pos.x != verts[count - 1].pos.x || verts[0].pos.y != verts[count - 1].pos.y) {
  1995. D3D11_DrawPrimitives(renderer, D3D11_PRIMITIVE_TOPOLOGY_POINTLIST, start + (count-1), 1);
  1996. }
  1997. break;
  1998. }
  1999. case SDL_RENDERCMD_FILL_RECTS: {
  2000. const size_t count = cmd->data.draw.count;
  2001. const size_t first = cmd->data.draw.first;
  2002. const size_t start = first / sizeof (VertexPositionColor);
  2003. size_t offset = 0;
  2004. D3D11_SetDrawState(renderer, cmd, rendererData->pixelShaders[SHADER_SOLID], 0, NULL, NULL, NULL);
  2005. for (i = 0; i < count; i++, offset += 4) {
  2006. D3D11_DrawPrimitives(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, start + offset, 4);
  2007. }
  2008. break;
  2009. }
  2010. case SDL_RENDERCMD_COPY: {
  2011. const size_t first = cmd->data.draw.first;
  2012. const size_t start = first / sizeof (VertexPositionColor);
  2013. D3D11_SetCopyState(renderer, cmd, NULL);
  2014. D3D11_DrawPrimitives(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, start, 4);
  2015. break;
  2016. }
  2017. case SDL_RENDERCMD_COPY_EX: {
  2018. const size_t first = cmd->data.draw.first;
  2019. const size_t start = first / sizeof (VertexPositionColor);
  2020. const VertexPositionColor *verts = (VertexPositionColor *) (((Uint8 *) vertices) + first);
  2021. const VertexPositionColor *transvert = verts + 4;
  2022. const float translatex = transvert->pos.x;
  2023. const float translatey = transvert->pos.y;
  2024. const float rotation = transvert->pos.z;
  2025. const Float4X4 matrix = MatrixMultiply(MatrixRotationZ(rotation), MatrixTranslation(translatex, translatey, 0));
  2026. D3D11_SetCopyState(renderer, cmd, &matrix);
  2027. D3D11_DrawPrimitives(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, start, 4);
  2028. break;
  2029. }
  2030. case SDL_RENDERCMD_NO_OP:
  2031. break;
  2032. }
  2033. cmd = cmd->next;
  2034. }
  2035. return 0;
  2036. }
  2037. static int
  2038. D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  2039. Uint32 format, void * pixels, int pitch)
  2040. {
  2041. D3D11_RenderData * data = (D3D11_RenderData *) renderer->driverdata;
  2042. ID3D11RenderTargetView *renderTargetView = NULL;
  2043. ID3D11Texture2D *backBuffer = NULL;
  2044. ID3D11Texture2D *stagingTexture = NULL;
  2045. HRESULT result;
  2046. int status = -1;
  2047. D3D11_TEXTURE2D_DESC stagingTextureDesc;
  2048. D3D11_RECT srcRect = {0, 0, 0, 0};
  2049. D3D11_BOX srcBox;
  2050. D3D11_MAPPED_SUBRESOURCE textureMemory;
  2051. ID3D11DeviceContext_OMGetRenderTargets(data->d3dContext, 1, &renderTargetView, NULL);
  2052. if (renderTargetView == NULL) {
  2053. SDL_SetError("%s, ID3D11DeviceContext::OMGetRenderTargets failed", __FUNCTION__);
  2054. goto done;
  2055. }
  2056. ID3D11View_GetResource(renderTargetView, (ID3D11Resource**)&backBuffer);
  2057. if (backBuffer == NULL) {
  2058. SDL_SetError("%s, ID3D11View::GetResource failed", __FUNCTION__);
  2059. goto done;
  2060. }
  2061. /* Create a staging texture to copy the screen's data to: */
  2062. ID3D11Texture2D_GetDesc(backBuffer, &stagingTextureDesc);
  2063. stagingTextureDesc.Width = rect->w;
  2064. stagingTextureDesc.Height = rect->h;
  2065. stagingTextureDesc.BindFlags = 0;
  2066. stagingTextureDesc.MiscFlags = 0;
  2067. stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  2068. stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
  2069. result = ID3D11Device_CreateTexture2D(data->d3dDevice,
  2070. &stagingTextureDesc,
  2071. NULL,
  2072. &stagingTexture);
  2073. if (FAILED(result)) {
  2074. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateTexture2D [create staging texture]"), result);
  2075. goto done;
  2076. }
  2077. /* Copy the desired portion of the back buffer to the staging texture: */
  2078. if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &srcRect, FALSE) != 0) {
  2079. /* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
  2080. goto done;
  2081. }
  2082. srcBox.left = srcRect.left;
  2083. srcBox.right = srcRect.right;
  2084. srcBox.top = srcRect.top;
  2085. srcBox.bottom = srcRect.bottom;
  2086. srcBox.front = 0;
  2087. srcBox.back = 1;
  2088. ID3D11DeviceContext_CopySubresourceRegion(data->d3dContext,
  2089. (ID3D11Resource *)stagingTexture,
  2090. 0,
  2091. 0, 0, 0,
  2092. (ID3D11Resource *)backBuffer,
  2093. 0,
  2094. &srcBox);
  2095. /* Map the staging texture's data to CPU-accessible memory: */
  2096. result = ID3D11DeviceContext_Map(data->d3dContext,
  2097. (ID3D11Resource *)stagingTexture,
  2098. 0,
  2099. D3D11_MAP_READ,
  2100. 0,
  2101. &textureMemory);
  2102. if (FAILED(result)) {
  2103. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11DeviceContext1::Map [map staging texture]"), result);
  2104. goto done;
  2105. }
  2106. /* Copy the data into the desired buffer, converting pixels to the
  2107. * desired format at the same time:
  2108. */
  2109. if (SDL_ConvertPixels(
  2110. rect->w, rect->h,
  2111. D3D11_DXGIFormatToSDLPixelFormat(stagingTextureDesc.Format),
  2112. textureMemory.pData,
  2113. textureMemory.RowPitch,
  2114. format,
  2115. pixels,
  2116. pitch) != 0) {
  2117. /* When SDL_ConvertPixels fails, it'll have already set the format.
  2118. * Get the error message, and attach some extra data to it.
  2119. */
  2120. char errorMessage[1024];
  2121. SDL_snprintf(errorMessage, sizeof(errorMessage), "%s, Convert Pixels failed: %s", __FUNCTION__, SDL_GetError());
  2122. SDL_SetError("%s", errorMessage);
  2123. goto done;
  2124. }
  2125. /* Unmap the texture: */
  2126. ID3D11DeviceContext_Unmap(data->d3dContext,
  2127. (ID3D11Resource *)stagingTexture,
  2128. 0);
  2129. status = 0;
  2130. done:
  2131. SAFE_RELEASE(backBuffer);
  2132. SAFE_RELEASE(stagingTexture);
  2133. return status;
  2134. }
  2135. static void
  2136. D3D11_RenderPresent(SDL_Renderer * renderer)
  2137. {
  2138. D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
  2139. UINT syncInterval;
  2140. UINT presentFlags;
  2141. HRESULT result;
  2142. DXGI_PRESENT_PARAMETERS parameters;
  2143. SDL_zero(parameters);
  2144. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  2145. syncInterval = 1;
  2146. presentFlags = 0;
  2147. result = IDXGISwapChain_Present(data->swapChain, syncInterval, presentFlags);
  2148. #else
  2149. if (renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
  2150. syncInterval = 1;
  2151. presentFlags = 0;
  2152. } else {
  2153. syncInterval = 0;
  2154. presentFlags = DXGI_PRESENT_DO_NOT_WAIT;
  2155. }
  2156. /* The application may optionally specify "dirty" or "scroll"
  2157. * rects to improve efficiency in certain scenarios.
  2158. * This option is not available on Windows Phone 8, to note.
  2159. */
  2160. result = IDXGISwapChain1_Present1(data->swapChain, syncInterval, presentFlags, &parameters);
  2161. #endif
  2162. /* Discard the contents of the render target.
  2163. * This is a valid operation only when the existing contents will be entirely
  2164. * overwritten. If dirty or scroll rects are used, this call should be removed.
  2165. */
  2166. ID3D11DeviceContext1_DiscardView(data->d3dContext, (ID3D11View*)data->mainRenderTargetView);
  2167. /* When the present flips, it unbinds the current view, so bind it again on the next draw call */
  2168. data->currentRenderTargetView = NULL;
  2169. if (FAILED(result) && result != DXGI_ERROR_WAS_STILL_DRAWING) {
  2170. /* If the device was removed either by a disconnect or a driver upgrade, we
  2171. * must recreate all device resources.
  2172. *
  2173. * TODO, WinRT: consider throwing an exception if D3D11_RenderPresent fails, especially if there is a way to salvage debug info from users' machines
  2174. */
  2175. if ( result == DXGI_ERROR_DEVICE_REMOVED ) {
  2176. D3D11_HandleDeviceLost(renderer);
  2177. } else if (result == DXGI_ERROR_INVALID_CALL) {
  2178. /* We probably went through a fullscreen <-> windowed transition */
  2179. D3D11_CreateWindowSizeDependentResources(renderer);
  2180. } else {
  2181. WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::Present"), result);
  2182. }
  2183. }
  2184. }
  2185. SDL_Renderer *
  2186. D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
  2187. {
  2188. SDL_Renderer *renderer;
  2189. D3D11_RenderData *data;
  2190. renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
  2191. if (!renderer) {
  2192. SDL_OutOfMemory();
  2193. return NULL;
  2194. }
  2195. data = (D3D11_RenderData *) SDL_calloc(1, sizeof(*data));
  2196. if (!data) {
  2197. SDL_OutOfMemory();
  2198. return NULL;
  2199. }
  2200. data->identity = MatrixIdentity();
  2201. renderer->WindowEvent = D3D11_WindowEvent;
  2202. renderer->SupportsBlendMode = D3D11_SupportsBlendMode;
  2203. renderer->CreateTexture = D3D11_CreateTexture;
  2204. renderer->UpdateTexture = D3D11_UpdateTexture;
  2205. renderer->UpdateTextureYUV = D3D11_UpdateTextureYUV;
  2206. renderer->LockTexture = D3D11_LockTexture;
  2207. renderer->UnlockTexture = D3D11_UnlockTexture;
  2208. renderer->SetTextureScaleMode = D3D11_SetTextureScaleMode;
  2209. renderer->SetRenderTarget = D3D11_SetRenderTarget;
  2210. renderer->QueueSetViewport = D3D11_QueueSetViewport;
  2211. renderer->QueueSetDrawColor = D3D11_QueueSetViewport; /* SetViewport and SetDrawColor are (currently) no-ops. */
  2212. renderer->QueueDrawPoints = D3D11_QueueDrawPoints;
  2213. renderer->QueueDrawLines = D3D11_QueueDrawPoints; /* lines and points queue vertices the same way. */
  2214. renderer->QueueFillRects = D3D11_QueueFillRects;
  2215. renderer->QueueCopy = D3D11_QueueCopy;
  2216. renderer->QueueCopyEx = D3D11_QueueCopyEx;
  2217. renderer->RunCommandQueue = D3D11_RunCommandQueue;
  2218. renderer->RenderReadPixels = D3D11_RenderReadPixels;
  2219. renderer->RenderPresent = D3D11_RenderPresent;
  2220. renderer->DestroyTexture = D3D11_DestroyTexture;
  2221. renderer->DestroyRenderer = D3D11_DestroyRenderer;
  2222. renderer->info = D3D11_RenderDriver.info;
  2223. renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
  2224. renderer->driverdata = data;
  2225. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  2226. /* VSync is required in Windows Phone, at least for Win Phone 8.0 and 8.1.
  2227. * Failure to use it seems to either result in:
  2228. *
  2229. * - with the D3D11 debug runtime turned OFF, vsync seemingly gets turned
  2230. * off (framerate doesn't get capped), but nothing appears on-screen
  2231. *
  2232. * - with the D3D11 debug runtime turned ON, vsync gets automatically
  2233. * turned back on, and the following gets output to the debug console:
  2234. *
  2235. * DXGI ERROR: IDXGISwapChain::Present: Interval 0 is not supported, changed to Interval 1. [ UNKNOWN ERROR #1024: ]
  2236. */
  2237. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  2238. #else
  2239. if ((flags & SDL_RENDERER_PRESENTVSYNC)) {
  2240. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  2241. }
  2242. #endif
  2243. /* HACK: make sure the SDL_Renderer references the SDL_Window data now, in
  2244. * order to give init functions access to the underlying window handle:
  2245. */
  2246. renderer->window = window;
  2247. /* Initialize Direct3D resources */
  2248. if (FAILED(D3D11_CreateDeviceResources(renderer))) {
  2249. D3D11_DestroyRenderer(renderer);
  2250. return NULL;
  2251. }
  2252. if (FAILED(D3D11_CreateWindowSizeDependentResources(renderer))) {
  2253. D3D11_DestroyRenderer(renderer);
  2254. return NULL;
  2255. }
  2256. return renderer;
  2257. }
  2258. SDL_RenderDriver D3D11_RenderDriver = {
  2259. D3D11_CreateRenderer,
  2260. {
  2261. "direct3d11",
  2262. (
  2263. SDL_RENDERER_ACCELERATED |
  2264. SDL_RENDERER_PRESENTVSYNC |
  2265. SDL_RENDERER_TARGETTEXTURE
  2266. ), /* flags. see SDL_RendererFlags */
  2267. 6, /* num_texture_formats */
  2268. { /* texture_formats */
  2269. SDL_PIXELFORMAT_ARGB8888,
  2270. SDL_PIXELFORMAT_RGB888,
  2271. SDL_PIXELFORMAT_YV12,
  2272. SDL_PIXELFORMAT_IYUV,
  2273. SDL_PIXELFORMAT_NV12,
  2274. SDL_PIXELFORMAT_NV21
  2275. },
  2276. 0, /* max_texture_width: will be filled in later */
  2277. 0 /* max_texture_height: will be filled in later */
  2278. }
  2279. };
  2280. #endif /* SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED */
  2281. /* vi: set ts=4 sw=4 expandtab: */