SDL_render_d3d.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if SDL_VIDEO_RENDER_D3D
  20. #include "../../core/windows/SDL_windows.h"
  21. #include "../SDL_sysrender.h"
  22. #include "../SDL_d3dmath.h"
  23. #include "../../video/windows/SDL_windowsvideo.h"
  24. #include "../../video/SDL_pixels_c.h"
  25. #if SDL_VIDEO_RENDER_D3D
  26. #define D3D_DEBUG_INFO
  27. #include <d3d9.h>
  28. #endif
  29. #include "SDL_shaders_d3d.h"
  30. typedef struct
  31. {
  32. SDL_Rect viewport;
  33. SDL_bool viewport_dirty;
  34. SDL_Texture *texture;
  35. SDL_BlendMode blend;
  36. SDL_bool cliprect_enabled;
  37. SDL_bool cliprect_enabled_dirty;
  38. SDL_Rect cliprect;
  39. SDL_bool cliprect_dirty;
  40. D3D9_Shader shader;
  41. const float *shader_params;
  42. } D3D_DrawStateCache;
  43. /* Direct3D renderer implementation */
  44. typedef struct
  45. {
  46. void *d3dDLL;
  47. IDirect3D9 *d3d;
  48. IDirect3DDevice9 *device;
  49. UINT adapter;
  50. D3DPRESENT_PARAMETERS pparams;
  51. SDL_bool updateSize;
  52. SDL_bool beginScene;
  53. SDL_bool enableSeparateAlphaBlend;
  54. D3DTEXTUREFILTERTYPE scaleMode[8];
  55. IDirect3DSurface9 *defaultRenderTarget;
  56. IDirect3DSurface9 *currentRenderTarget;
  57. void *d3dxDLL;
  58. #if SDL_HAVE_YUV
  59. LPDIRECT3DPIXELSHADER9 shaders[NUM_SHADERS];
  60. #endif
  61. LPDIRECT3DVERTEXBUFFER9 vertexBuffers[8];
  62. size_t vertexBufferSize[8];
  63. int currentVertexBuffer;
  64. SDL_bool reportedVboProblem;
  65. D3D_DrawStateCache drawstate;
  66. } D3D_RenderData;
  67. typedef struct
  68. {
  69. SDL_bool dirty;
  70. int w, h;
  71. DWORD usage;
  72. Uint32 format;
  73. D3DFORMAT d3dfmt;
  74. IDirect3DTexture9 *texture;
  75. IDirect3DTexture9 *staging;
  76. } D3D_TextureRep;
  77. typedef struct
  78. {
  79. D3D_TextureRep texture;
  80. D3DTEXTUREFILTERTYPE scaleMode;
  81. D3D9_Shader shader;
  82. const float *shader_params;
  83. #if SDL_HAVE_YUV
  84. /* YV12 texture support */
  85. SDL_bool yuv;
  86. D3D_TextureRep utexture;
  87. D3D_TextureRep vtexture;
  88. Uint8 *pixels;
  89. int pitch;
  90. SDL_Rect locked_rect;
  91. #endif
  92. } D3D_TextureData;
  93. typedef struct
  94. {
  95. float x, y, z;
  96. DWORD color;
  97. float u, v;
  98. } Vertex;
  99. static int D3D_SetError(const char *prefix, HRESULT result)
  100. {
  101. const char *error;
  102. switch (result) {
  103. case D3DERR_WRONGTEXTUREFORMAT:
  104. error = "WRONGTEXTUREFORMAT";
  105. break;
  106. case D3DERR_UNSUPPORTEDCOLOROPERATION:
  107. error = "UNSUPPORTEDCOLOROPERATION";
  108. break;
  109. case D3DERR_UNSUPPORTEDCOLORARG:
  110. error = "UNSUPPORTEDCOLORARG";
  111. break;
  112. case D3DERR_UNSUPPORTEDALPHAOPERATION:
  113. error = "UNSUPPORTEDALPHAOPERATION";
  114. break;
  115. case D3DERR_UNSUPPORTEDALPHAARG:
  116. error = "UNSUPPORTEDALPHAARG";
  117. break;
  118. case D3DERR_TOOMANYOPERATIONS:
  119. error = "TOOMANYOPERATIONS";
  120. break;
  121. case D3DERR_CONFLICTINGTEXTUREFILTER:
  122. error = "CONFLICTINGTEXTUREFILTER";
  123. break;
  124. case D3DERR_UNSUPPORTEDFACTORVALUE:
  125. error = "UNSUPPORTEDFACTORVALUE";
  126. break;
  127. case D3DERR_CONFLICTINGRENDERSTATE:
  128. error = "CONFLICTINGRENDERSTATE";
  129. break;
  130. case D3DERR_UNSUPPORTEDTEXTUREFILTER:
  131. error = "UNSUPPORTEDTEXTUREFILTER";
  132. break;
  133. case D3DERR_CONFLICTINGTEXTUREPALETTE:
  134. error = "CONFLICTINGTEXTUREPALETTE";
  135. break;
  136. case D3DERR_DRIVERINTERNALERROR:
  137. error = "DRIVERINTERNALERROR";
  138. break;
  139. case D3DERR_NOTFOUND:
  140. error = "NOTFOUND";
  141. break;
  142. case D3DERR_MOREDATA:
  143. error = "MOREDATA";
  144. break;
  145. case D3DERR_DEVICELOST:
  146. error = "DEVICELOST";
  147. break;
  148. case D3DERR_DEVICENOTRESET:
  149. error = "DEVICENOTRESET";
  150. break;
  151. case D3DERR_NOTAVAILABLE:
  152. error = "NOTAVAILABLE";
  153. break;
  154. case D3DERR_OUTOFVIDEOMEMORY:
  155. error = "OUTOFVIDEOMEMORY";
  156. break;
  157. case D3DERR_INVALIDDEVICE:
  158. error = "INVALIDDEVICE";
  159. break;
  160. case D3DERR_INVALIDCALL:
  161. error = "INVALIDCALL";
  162. break;
  163. case D3DERR_DRIVERINVALIDCALL:
  164. error = "DRIVERINVALIDCALL";
  165. break;
  166. case D3DERR_WASSTILLDRAWING:
  167. error = "WASSTILLDRAWING";
  168. break;
  169. default:
  170. error = "UNKNOWN";
  171. break;
  172. }
  173. return SDL_SetError("%s: %s", prefix, error);
  174. }
  175. static D3DFORMAT PixelFormatToD3DFMT(Uint32 format)
  176. {
  177. switch (format) {
  178. case SDL_PIXELFORMAT_RGB565:
  179. return D3DFMT_R5G6B5;
  180. case SDL_PIXELFORMAT_XRGB8888:
  181. return D3DFMT_X8R8G8B8;
  182. case SDL_PIXELFORMAT_ARGB8888:
  183. return D3DFMT_A8R8G8B8;
  184. case SDL_PIXELFORMAT_YV12:
  185. case SDL_PIXELFORMAT_IYUV:
  186. case SDL_PIXELFORMAT_NV12:
  187. case SDL_PIXELFORMAT_NV21:
  188. return D3DFMT_L8;
  189. default:
  190. return D3DFMT_UNKNOWN;
  191. }
  192. }
  193. static Uint32 D3DFMTToPixelFormat(D3DFORMAT format)
  194. {
  195. switch (format) {
  196. case D3DFMT_R5G6B5:
  197. return SDL_PIXELFORMAT_RGB565;
  198. case D3DFMT_X8R8G8B8:
  199. return SDL_PIXELFORMAT_XRGB8888;
  200. case D3DFMT_A8R8G8B8:
  201. return SDL_PIXELFORMAT_ARGB8888;
  202. default:
  203. return SDL_PIXELFORMAT_UNKNOWN;
  204. }
  205. }
  206. static void D3D_InitRenderState(D3D_RenderData *data)
  207. {
  208. D3DMATRIX matrix;
  209. IDirect3DDevice9 *device = data->device;
  210. IDirect3DDevice9_SetPixelShader(device, NULL);
  211. IDirect3DDevice9_SetTexture(device, 0, NULL);
  212. IDirect3DDevice9_SetTexture(device, 1, NULL);
  213. IDirect3DDevice9_SetTexture(device, 2, NULL);
  214. IDirect3DDevice9_SetFVF(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
  215. IDirect3DDevice9_SetVertexShader(device, NULL);
  216. IDirect3DDevice9_SetRenderState(device, D3DRS_ZENABLE, D3DZB_FALSE);
  217. IDirect3DDevice9_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE);
  218. IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
  219. /* Enable color modulation by diffuse color */
  220. IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_COLOROP,
  221. D3DTOP_MODULATE);
  222. IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_COLORARG1,
  223. D3DTA_TEXTURE);
  224. IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_COLORARG2,
  225. D3DTA_DIFFUSE);
  226. /* Enable alpha modulation by diffuse alpha */
  227. IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_ALPHAOP,
  228. D3DTOP_MODULATE);
  229. IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_ALPHAARG1,
  230. D3DTA_TEXTURE);
  231. IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_ALPHAARG2,
  232. D3DTA_DIFFUSE);
  233. /* Enable separate alpha blend function, if possible */
  234. if (data->enableSeparateAlphaBlend) {
  235. IDirect3DDevice9_SetRenderState(device, D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
  236. }
  237. /* Disable second texture stage, since we're done */
  238. IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_COLOROP,
  239. D3DTOP_DISABLE);
  240. IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_ALPHAOP,
  241. D3DTOP_DISABLE);
  242. /* Set an identity world and view matrix */
  243. SDL_zero(matrix);
  244. matrix.m[0][0] = 1.0f;
  245. matrix.m[1][1] = 1.0f;
  246. matrix.m[2][2] = 1.0f;
  247. matrix.m[3][3] = 1.0f;
  248. IDirect3DDevice9_SetTransform(device, D3DTS_WORLD, &matrix);
  249. IDirect3DDevice9_SetTransform(device, D3DTS_VIEW, &matrix);
  250. /* Reset our current scale mode */
  251. SDL_memset(data->scaleMode, 0xFF, sizeof(data->scaleMode));
  252. /* Start the render with beginScene */
  253. data->beginScene = SDL_TRUE;
  254. }
  255. static int D3D_Reset(SDL_Renderer *renderer);
  256. static int D3D_ActivateRenderer(SDL_Renderer *renderer)
  257. {
  258. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  259. HRESULT result;
  260. if (data->updateSize) {
  261. SDL_Window *window = renderer->window;
  262. int w, h;
  263. const SDL_DisplayMode *fullscreen_mode = NULL;
  264. SDL_GetWindowSizeInPixels(window, &w, &h);
  265. data->pparams.BackBufferWidth = w;
  266. data->pparams.BackBufferHeight = h;
  267. if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
  268. fullscreen_mode = SDL_GetWindowFullscreenMode(window);
  269. }
  270. if (fullscreen_mode) {
  271. data->pparams.Windowed = FALSE;
  272. data->pparams.BackBufferFormat = PixelFormatToD3DFMT(fullscreen_mode->format);
  273. data->pparams.FullScreen_RefreshRateInHz = (UINT)SDL_ceilf(fullscreen_mode->refresh_rate);
  274. } else {
  275. data->pparams.Windowed = TRUE;
  276. data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
  277. data->pparams.FullScreen_RefreshRateInHz = 0;
  278. }
  279. if (D3D_Reset(renderer) < 0) {
  280. return -1;
  281. }
  282. data->updateSize = SDL_FALSE;
  283. }
  284. if (data->beginScene) {
  285. result = IDirect3DDevice9_BeginScene(data->device);
  286. if (result == D3DERR_DEVICELOST) {
  287. if (D3D_Reset(renderer) < 0) {
  288. return -1;
  289. }
  290. result = IDirect3DDevice9_BeginScene(data->device);
  291. }
  292. if (FAILED(result)) {
  293. return D3D_SetError("BeginScene()", result);
  294. }
  295. data->beginScene = SDL_FALSE;
  296. }
  297. return 0;
  298. }
  299. static void D3D_WindowEvent(SDL_Renderer *renderer, const SDL_WindowEvent *event)
  300. {
  301. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  302. if (event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) {
  303. data->updateSize = SDL_TRUE;
  304. }
  305. }
  306. static D3DBLEND GetBlendFunc(SDL_BlendFactor factor)
  307. {
  308. switch (factor) {
  309. case SDL_BLENDFACTOR_ZERO:
  310. return D3DBLEND_ZERO;
  311. case SDL_BLENDFACTOR_ONE:
  312. return D3DBLEND_ONE;
  313. case SDL_BLENDFACTOR_SRC_COLOR:
  314. return D3DBLEND_SRCCOLOR;
  315. case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR:
  316. return D3DBLEND_INVSRCCOLOR;
  317. case SDL_BLENDFACTOR_SRC_ALPHA:
  318. return D3DBLEND_SRCALPHA;
  319. case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA:
  320. return D3DBLEND_INVSRCALPHA;
  321. case SDL_BLENDFACTOR_DST_COLOR:
  322. return D3DBLEND_DESTCOLOR;
  323. case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR:
  324. return D3DBLEND_INVDESTCOLOR;
  325. case SDL_BLENDFACTOR_DST_ALPHA:
  326. return D3DBLEND_DESTALPHA;
  327. case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA:
  328. return D3DBLEND_INVDESTALPHA;
  329. default:
  330. break;
  331. }
  332. return (D3DBLEND)0;
  333. }
  334. static D3DBLENDOP GetBlendEquation(SDL_BlendOperation operation)
  335. {
  336. switch (operation) {
  337. case SDL_BLENDOPERATION_ADD:
  338. return D3DBLENDOP_ADD;
  339. case SDL_BLENDOPERATION_SUBTRACT:
  340. return D3DBLENDOP_SUBTRACT;
  341. case SDL_BLENDOPERATION_REV_SUBTRACT:
  342. return D3DBLENDOP_REVSUBTRACT;
  343. case SDL_BLENDOPERATION_MINIMUM:
  344. return D3DBLENDOP_MIN;
  345. case SDL_BLENDOPERATION_MAXIMUM:
  346. return D3DBLENDOP_MAX;
  347. default:
  348. break;
  349. }
  350. return (D3DBLENDOP)0;
  351. }
  352. static SDL_bool D3D_SupportsBlendMode(SDL_Renderer *renderer, SDL_BlendMode blendMode)
  353. {
  354. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  355. SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
  356. SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
  357. SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
  358. SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
  359. SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
  360. SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
  361. if (!GetBlendFunc(srcColorFactor) || !GetBlendFunc(srcAlphaFactor) ||
  362. !GetBlendEquation(colorOperation) ||
  363. !GetBlendFunc(dstColorFactor) || !GetBlendFunc(dstAlphaFactor) ||
  364. !GetBlendEquation(alphaOperation)) {
  365. return SDL_FALSE;
  366. }
  367. if (!data->enableSeparateAlphaBlend) {
  368. if ((srcColorFactor != srcAlphaFactor) || (dstColorFactor != dstAlphaFactor) || (colorOperation != alphaOperation)) {
  369. return SDL_FALSE;
  370. }
  371. }
  372. return SDL_TRUE;
  373. }
  374. static int D3D_CreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, DWORD usage, Uint32 format, D3DFORMAT d3dfmt, int w, int h)
  375. {
  376. HRESULT result;
  377. texture->dirty = SDL_FALSE;
  378. texture->w = w;
  379. texture->h = h;
  380. texture->usage = usage;
  381. texture->format = format;
  382. texture->d3dfmt = d3dfmt;
  383. result = IDirect3DDevice9_CreateTexture(device, w, h, 1, usage,
  384. PixelFormatToD3DFMT(format),
  385. D3DPOOL_DEFAULT, &texture->texture, NULL);
  386. if (FAILED(result)) {
  387. return D3D_SetError("CreateTexture(D3DPOOL_DEFAULT)", result);
  388. }
  389. return 0;
  390. }
  391. static int D3D_CreateStagingTexture(IDirect3DDevice9 *device, D3D_TextureRep *texture)
  392. {
  393. HRESULT result;
  394. if (!texture->staging) {
  395. result = IDirect3DDevice9_CreateTexture(device, texture->w, texture->h, 1, 0,
  396. texture->d3dfmt, D3DPOOL_SYSTEMMEM, &texture->staging, NULL);
  397. if (FAILED(result)) {
  398. return D3D_SetError("CreateTexture(D3DPOOL_SYSTEMMEM)", result);
  399. }
  400. }
  401. return 0;
  402. }
  403. static int D3D_RecreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture)
  404. {
  405. if (texture->texture) {
  406. IDirect3DTexture9_Release(texture->texture);
  407. texture->texture = NULL;
  408. }
  409. if (texture->staging) {
  410. IDirect3DTexture9_AddDirtyRect(texture->staging, NULL);
  411. texture->dirty = SDL_TRUE;
  412. }
  413. return 0;
  414. }
  415. static int D3D_UpdateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, int x, int y, int w, int h, const void *pixels, int pitch)
  416. {
  417. RECT d3drect;
  418. D3DLOCKED_RECT locked;
  419. const Uint8 *src;
  420. Uint8 *dst;
  421. int row, length;
  422. HRESULT result;
  423. if (D3D_CreateStagingTexture(device, texture) < 0) {
  424. return -1;
  425. }
  426. d3drect.left = x;
  427. d3drect.right = (LONG)x + w;
  428. d3drect.top = y;
  429. d3drect.bottom = (LONG)y + h;
  430. result = IDirect3DTexture9_LockRect(texture->staging, 0, &locked, &d3drect, 0);
  431. if (FAILED(result)) {
  432. return D3D_SetError("LockRect()", result);
  433. }
  434. src = (const Uint8 *)pixels;
  435. dst = (Uint8 *)locked.pBits;
  436. length = w * SDL_BYTESPERPIXEL(texture->format);
  437. if (length == pitch && length == locked.Pitch) {
  438. SDL_memcpy(dst, src, (size_t)length * h);
  439. } else {
  440. if (length > pitch) {
  441. length = pitch;
  442. }
  443. if (length > locked.Pitch) {
  444. length = locked.Pitch;
  445. }
  446. for (row = 0; row < h; ++row) {
  447. SDL_memcpy(dst, src, length);
  448. src += pitch;
  449. dst += locked.Pitch;
  450. }
  451. }
  452. result = IDirect3DTexture9_UnlockRect(texture->staging, 0);
  453. if (FAILED(result)) {
  454. return D3D_SetError("UnlockRect()", result);
  455. }
  456. texture->dirty = SDL_TRUE;
  457. return 0;
  458. }
  459. static void D3D_DestroyTextureRep(D3D_TextureRep *texture)
  460. {
  461. if (texture->texture) {
  462. IDirect3DTexture9_Release(texture->texture);
  463. texture->texture = NULL;
  464. }
  465. if (texture->staging) {
  466. IDirect3DTexture9_Release(texture->staging);
  467. texture->staging = NULL;
  468. }
  469. }
  470. static int D3D_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_PropertiesID create_props)
  471. {
  472. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  473. D3D_TextureData *texturedata;
  474. DWORD usage;
  475. texturedata = (D3D_TextureData *)SDL_calloc(1, sizeof(*texturedata));
  476. if (!texturedata) {
  477. return -1;
  478. }
  479. texturedata->scaleMode = (texture->scaleMode == SDL_SCALEMODE_NEAREST) ? D3DTEXF_POINT : D3DTEXF_LINEAR;
  480. texture->driverdata = texturedata;
  481. if (texture->access == SDL_TEXTUREACCESS_TARGET) {
  482. usage = D3DUSAGE_RENDERTARGET;
  483. } else {
  484. usage = 0;
  485. }
  486. if (D3D_CreateTextureRep(data->device, &texturedata->texture, usage, texture->format, PixelFormatToD3DFMT(texture->format), texture->w, texture->h) < 0) {
  487. return -1;
  488. }
  489. #if SDL_HAVE_YUV
  490. if (texture->format == SDL_PIXELFORMAT_YV12 ||
  491. texture->format == SDL_PIXELFORMAT_IYUV) {
  492. texturedata->yuv = SDL_TRUE;
  493. if (D3D_CreateTextureRep(data->device, &texturedata->utexture, usage, texture->format, PixelFormatToD3DFMT(texture->format), (texture->w + 1) / 2, (texture->h + 1) / 2) < 0) {
  494. return -1;
  495. }
  496. if (D3D_CreateTextureRep(data->device, &texturedata->vtexture, usage, texture->format, PixelFormatToD3DFMT(texture->format), (texture->w + 1) / 2, (texture->h + 1) / 2) < 0) {
  497. return -1;
  498. }
  499. texturedata->shader = SHADER_YUV;
  500. texturedata->shader_params = SDL_GetYCbCRtoRGBConversionMatrix(texture->colorspace, texture->w, texture->h, 8);
  501. if (texturedata->shader_params == NULL) {
  502. return SDL_SetError("Unsupported YUV colorspace");
  503. }
  504. }
  505. #endif
  506. return 0;
  507. }
  508. static int D3D_RecreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
  509. {
  510. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  511. D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
  512. if (!texturedata) {
  513. return 0;
  514. }
  515. if (D3D_RecreateTextureRep(data->device, &texturedata->texture) < 0) {
  516. return -1;
  517. }
  518. #if SDL_HAVE_YUV
  519. if (texturedata->yuv) {
  520. if (D3D_RecreateTextureRep(data->device, &texturedata->utexture) < 0) {
  521. return -1;
  522. }
  523. if (D3D_RecreateTextureRep(data->device, &texturedata->vtexture) < 0) {
  524. return -1;
  525. }
  526. }
  527. #endif
  528. return 0;
  529. }
  530. static int D3D_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
  531. const SDL_Rect *rect, const void *pixels, int pitch)
  532. {
  533. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  534. D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
  535. if (!texturedata) {
  536. return SDL_SetError("Texture is not currently available");
  537. }
  538. if (D3D_UpdateTextureRep(data->device, &texturedata->texture, rect->x, rect->y, rect->w, rect->h, pixels, pitch) < 0) {
  539. return -1;
  540. }
  541. #if SDL_HAVE_YUV
  542. if (texturedata->yuv) {
  543. /* Skip to the correct offset into the next texture */
  544. pixels = (const void *)((const Uint8 *)pixels + rect->h * pitch);
  545. if (D3D_UpdateTextureRep(data->device, texture->format == SDL_PIXELFORMAT_YV12 ? &texturedata->vtexture : &texturedata->utexture, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, pixels, (pitch + 1) / 2) < 0) {
  546. return -1;
  547. }
  548. /* Skip to the correct offset into the next texture */
  549. pixels = (const void *)((const Uint8 *)pixels + ((rect->h + 1) / 2) * ((pitch + 1) / 2));
  550. if (D3D_UpdateTextureRep(data->device, texture->format == SDL_PIXELFORMAT_YV12 ? &texturedata->utexture : &texturedata->vtexture, rect->x / 2, (rect->y + 1) / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, pixels, (pitch + 1) / 2) < 0) {
  551. return -1;
  552. }
  553. }
  554. #endif
  555. return 0;
  556. }
  557. #if SDL_HAVE_YUV
  558. static int D3D_UpdateTextureYUV(SDL_Renderer *renderer, SDL_Texture *texture,
  559. const SDL_Rect *rect,
  560. const Uint8 *Yplane, int Ypitch,
  561. const Uint8 *Uplane, int Upitch,
  562. const Uint8 *Vplane, int Vpitch)
  563. {
  564. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  565. D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
  566. if (!texturedata) {
  567. return SDL_SetError("Texture is not currently available");
  568. }
  569. if (D3D_UpdateTextureRep(data->device, &texturedata->texture, rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch) < 0) {
  570. return -1;
  571. }
  572. if (D3D_UpdateTextureRep(data->device, &texturedata->utexture, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Uplane, Upitch) < 0) {
  573. return -1;
  574. }
  575. if (D3D_UpdateTextureRep(data->device, &texturedata->vtexture, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Vplane, Vpitch) < 0) {
  576. return -1;
  577. }
  578. return 0;
  579. }
  580. #endif
  581. static int D3D_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
  582. const SDL_Rect *rect, void **pixels, int *pitch)
  583. {
  584. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  585. D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
  586. IDirect3DDevice9 *device = data->device;
  587. if (!texturedata) {
  588. return SDL_SetError("Texture is not currently available");
  589. }
  590. #if SDL_HAVE_YUV
  591. texturedata->locked_rect = *rect;
  592. if (texturedata->yuv) {
  593. /* It's more efficient to upload directly... */
  594. if (!texturedata->pixels) {
  595. texturedata->pitch = texture->w;
  596. texturedata->pixels = (Uint8 *)SDL_malloc((texture->h * texturedata->pitch * 3) / 2);
  597. if (!texturedata->pixels) {
  598. return -1;
  599. }
  600. }
  601. *pixels =
  602. (void *)(texturedata->pixels + rect->y * texturedata->pitch +
  603. rect->x * SDL_BYTESPERPIXEL(texture->format));
  604. *pitch = texturedata->pitch;
  605. } else
  606. #endif
  607. {
  608. RECT d3drect;
  609. D3DLOCKED_RECT locked;
  610. HRESULT result;
  611. if (D3D_CreateStagingTexture(device, &texturedata->texture) < 0) {
  612. return -1;
  613. }
  614. d3drect.left = rect->x;
  615. d3drect.right = (LONG)rect->x + rect->w;
  616. d3drect.top = rect->y;
  617. d3drect.bottom = (LONG)rect->y + rect->h;
  618. result = IDirect3DTexture9_LockRect(texturedata->texture.staging, 0, &locked, &d3drect, 0);
  619. if (FAILED(result)) {
  620. return D3D_SetError("LockRect()", result);
  621. }
  622. *pixels = locked.pBits;
  623. *pitch = locked.Pitch;
  624. }
  625. return 0;
  626. }
  627. static void D3D_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
  628. {
  629. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  630. D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
  631. if (!texturedata) {
  632. return;
  633. }
  634. #if SDL_HAVE_YUV
  635. if (texturedata->yuv) {
  636. const SDL_Rect *rect = &texturedata->locked_rect;
  637. void *pixels =
  638. (void *)(texturedata->pixels + rect->y * texturedata->pitch +
  639. rect->x * SDL_BYTESPERPIXEL(texture->format));
  640. D3D_UpdateTexture(renderer, texture, rect, pixels, texturedata->pitch);
  641. } else
  642. #endif
  643. {
  644. IDirect3DTexture9_UnlockRect(texturedata->texture.staging, 0);
  645. texturedata->texture.dirty = SDL_TRUE;
  646. if (data->drawstate.texture == texture) {
  647. data->drawstate.texture = NULL;
  648. data->drawstate.shader = SHADER_NONE;
  649. data->drawstate.shader_params = NULL;
  650. IDirect3DDevice9_SetPixelShader(data->device, NULL);
  651. IDirect3DDevice9_SetTexture(data->device, 0, NULL);
  652. }
  653. }
  654. }
  655. static void D3D_SetTextureScaleMode(SDL_Renderer *renderer, SDL_Texture *texture, SDL_ScaleMode scaleMode)
  656. {
  657. D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
  658. if (!texturedata) {
  659. return;
  660. }
  661. texturedata->scaleMode = (scaleMode == SDL_SCALEMODE_NEAREST) ? D3DTEXF_POINT : D3DTEXF_LINEAR;
  662. }
  663. static int D3D_SetRenderTargetInternal(SDL_Renderer *renderer, SDL_Texture *texture)
  664. {
  665. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  666. D3D_TextureData *texturedata;
  667. D3D_TextureRep *texturerep;
  668. HRESULT result;
  669. IDirect3DDevice9 *device = data->device;
  670. /* Release the previous render target if it wasn't the default one */
  671. if (data->currentRenderTarget) {
  672. IDirect3DSurface9_Release(data->currentRenderTarget);
  673. data->currentRenderTarget = NULL;
  674. }
  675. if (!texture) {
  676. IDirect3DDevice9_SetRenderTarget(data->device, 0, data->defaultRenderTarget);
  677. return 0;
  678. }
  679. texturedata = (D3D_TextureData *)texture->driverdata;
  680. if (!texturedata) {
  681. return SDL_SetError("Texture is not currently available");
  682. }
  683. /* Make sure the render target is updated if it was locked and written to */
  684. texturerep = &texturedata->texture;
  685. if (texturerep->dirty && texturerep->staging) {
  686. if (!texturerep->texture) {
  687. result = IDirect3DDevice9_CreateTexture(device, texturerep->w, texturerep->h, 1, texturerep->usage,
  688. PixelFormatToD3DFMT(texturerep->format), D3DPOOL_DEFAULT, &texturerep->texture, NULL);
  689. if (FAILED(result)) {
  690. return D3D_SetError("CreateTexture(D3DPOOL_DEFAULT)", result);
  691. }
  692. }
  693. result = IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)texturerep->staging, (IDirect3DBaseTexture9 *)texturerep->texture);
  694. if (FAILED(result)) {
  695. return D3D_SetError("UpdateTexture()", result);
  696. }
  697. texturerep->dirty = SDL_FALSE;
  698. }
  699. result = IDirect3DTexture9_GetSurfaceLevel(texturedata->texture.texture, 0, &data->currentRenderTarget);
  700. if (FAILED(result)) {
  701. return D3D_SetError("GetSurfaceLevel()", result);
  702. }
  703. result = IDirect3DDevice9_SetRenderTarget(data->device, 0, data->currentRenderTarget);
  704. if (FAILED(result)) {
  705. return D3D_SetError("SetRenderTarget()", result);
  706. }
  707. return 0;
  708. }
  709. static int D3D_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
  710. {
  711. if (D3D_ActivateRenderer(renderer) < 0) {
  712. return -1;
  713. }
  714. return D3D_SetRenderTargetInternal(renderer, texture);
  715. }
  716. static int D3D_QueueNoOp(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
  717. {
  718. return 0; /* nothing to do in this backend. */
  719. }
  720. static int D3D_QueueDrawPoints(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
  721. {
  722. const DWORD color = D3DCOLOR_COLORVALUE(cmd->data.draw.color.r * cmd->data.draw.color_scale,
  723. cmd->data.draw.color.g * cmd->data.draw.color_scale,
  724. cmd->data.draw.color.b * cmd->data.draw.color_scale,
  725. cmd->data.draw.color.a);
  726. const size_t vertslen = count * sizeof(Vertex);
  727. Vertex *verts = (Vertex *)SDL_AllocateRenderVertices(renderer, vertslen, 0, &cmd->data.draw.first);
  728. int i;
  729. if (!verts) {
  730. return -1;
  731. }
  732. SDL_memset(verts, '\0', vertslen);
  733. cmd->data.draw.count = count;
  734. for (i = 0; i < count; i++, verts++, points++) {
  735. verts->x = points->x;
  736. verts->y = points->y;
  737. verts->color = color;
  738. }
  739. return 0;
  740. }
  741. static int D3D_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
  742. const float *xy, int xy_stride, const SDL_FColor *color, int color_stride, const float *uv, int uv_stride,
  743. int num_vertices, const void *indices, int num_indices, int size_indices,
  744. float scale_x, float scale_y)
  745. {
  746. int i;
  747. int count = indices ? num_indices : num_vertices;
  748. Vertex *verts = (Vertex *)SDL_AllocateRenderVertices(renderer, count * sizeof(Vertex), 0, &cmd->data.draw.first);
  749. const float color_scale = cmd->data.draw.color_scale;
  750. if (!verts) {
  751. return -1;
  752. }
  753. cmd->data.draw.count = count;
  754. size_indices = indices ? size_indices : 0;
  755. for (i = 0; i < count; i++) {
  756. int j;
  757. float *xy_;
  758. SDL_FColor *col_;
  759. if (size_indices == 4) {
  760. j = ((const Uint32 *)indices)[i];
  761. } else if (size_indices == 2) {
  762. j = ((const Uint16 *)indices)[i];
  763. } else if (size_indices == 1) {
  764. j = ((const Uint8 *)indices)[i];
  765. } else {
  766. j = i;
  767. }
  768. xy_ = (float *)((char *)xy + j * xy_stride);
  769. col_ = (SDL_FColor *)((char *)color + j * color_stride);
  770. verts->x = xy_[0] * scale_x - 0.5f;
  771. verts->y = xy_[1] * scale_y - 0.5f;
  772. verts->z = 0.0f;
  773. verts->color = D3DCOLOR_COLORVALUE(col_->r * color_scale, col_->g * color_scale, col_->b * color_scale, col_->a);
  774. if (texture) {
  775. float *uv_ = (float *)((char *)uv + j * uv_stride);
  776. verts->u = uv_[0];
  777. verts->v = uv_[1];
  778. } else {
  779. verts->u = 0.0f;
  780. verts->v = 0.0f;
  781. }
  782. verts += 1;
  783. }
  784. return 0;
  785. }
  786. static int UpdateDirtyTexture(IDirect3DDevice9 *device, D3D_TextureRep *texture)
  787. {
  788. if (texture->dirty && texture->staging) {
  789. HRESULT result;
  790. if (!texture->texture) {
  791. result = IDirect3DDevice9_CreateTexture(device, texture->w, texture->h, 1, texture->usage,
  792. PixelFormatToD3DFMT(texture->format), D3DPOOL_DEFAULT, &texture->texture, NULL);
  793. if (FAILED(result)) {
  794. return D3D_SetError("CreateTexture(D3DPOOL_DEFAULT)", result);
  795. }
  796. }
  797. result = IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)texture->staging, (IDirect3DBaseTexture9 *)texture->texture);
  798. if (FAILED(result)) {
  799. return D3D_SetError("UpdateTexture()", result);
  800. }
  801. texture->dirty = SDL_FALSE;
  802. }
  803. return 0;
  804. }
  805. static int BindTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, DWORD sampler)
  806. {
  807. HRESULT result;
  808. UpdateDirtyTexture(device, texture);
  809. result = IDirect3DDevice9_SetTexture(device, sampler, (IDirect3DBaseTexture9 *)texture->texture);
  810. if (FAILED(result)) {
  811. return D3D_SetError("SetTexture()", result);
  812. }
  813. return 0;
  814. }
  815. static void UpdateTextureScaleMode(D3D_RenderData *data, D3D_TextureData *texturedata, unsigned index)
  816. {
  817. if (texturedata->scaleMode != data->scaleMode[index]) {
  818. IDirect3DDevice9_SetSamplerState(data->device, index, D3DSAMP_MINFILTER,
  819. texturedata->scaleMode);
  820. IDirect3DDevice9_SetSamplerState(data->device, index, D3DSAMP_MAGFILTER,
  821. texturedata->scaleMode);
  822. IDirect3DDevice9_SetSamplerState(data->device, index, D3DSAMP_ADDRESSU,
  823. D3DTADDRESS_CLAMP);
  824. IDirect3DDevice9_SetSamplerState(data->device, index, D3DSAMP_ADDRESSV,
  825. D3DTADDRESS_CLAMP);
  826. data->scaleMode[index] = texturedata->scaleMode;
  827. }
  828. }
  829. static int SetupTextureState(D3D_RenderData *data, SDL_Texture *texture, D3D9_Shader *shader, const float **shader_params)
  830. {
  831. D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
  832. if (!texturedata) {
  833. return SDL_SetError("Texture is not currently available");
  834. }
  835. UpdateTextureScaleMode(data, texturedata, 0);
  836. *shader = texturedata->shader;
  837. *shader_params = texturedata->shader_params;
  838. if (BindTextureRep(data->device, &texturedata->texture, 0) < 0) {
  839. return -1;
  840. }
  841. #if SDL_HAVE_YUV
  842. if (texturedata->yuv) {
  843. UpdateTextureScaleMode(data, texturedata, 1);
  844. UpdateTextureScaleMode(data, texturedata, 2);
  845. if (BindTextureRep(data->device, &texturedata->utexture, 1) < 0) {
  846. return -1;
  847. }
  848. if (BindTextureRep(data->device, &texturedata->vtexture, 2) < 0) {
  849. return -1;
  850. }
  851. }
  852. #endif
  853. return 0;
  854. }
  855. static int SetDrawState(D3D_RenderData *data, const SDL_RenderCommand *cmd)
  856. {
  857. SDL_Texture *texture = cmd->data.draw.texture;
  858. const SDL_BlendMode blend = cmd->data.draw.blend;
  859. if (texture != data->drawstate.texture) {
  860. #if SDL_HAVE_YUV
  861. D3D_TextureData *oldtexturedata = data->drawstate.texture ? (D3D_TextureData *)data->drawstate.texture->driverdata : NULL;
  862. D3D_TextureData *newtexturedata = texture ? (D3D_TextureData *)texture->driverdata : NULL;
  863. #endif
  864. D3D9_Shader shader = SHADER_NONE;
  865. const float *shader_params = NULL;
  866. /* disable any enabled textures we aren't going to use, let SetupTextureState() do the rest. */
  867. if (!texture) {
  868. IDirect3DDevice9_SetTexture(data->device, 0, NULL);
  869. }
  870. #if SDL_HAVE_YUV
  871. if ((!newtexturedata || !newtexturedata->yuv) && (oldtexturedata && oldtexturedata->yuv)) {
  872. IDirect3DDevice9_SetTexture(data->device, 1, NULL);
  873. IDirect3DDevice9_SetTexture(data->device, 2, NULL);
  874. }
  875. #endif
  876. if (texture && SetupTextureState(data, texture, &shader, &shader_params) < 0) {
  877. return -1;
  878. }
  879. #if SDL_HAVE_YUV
  880. if (shader != data->drawstate.shader) {
  881. const HRESULT result = IDirect3DDevice9_SetPixelShader(data->device, data->shaders[shader]);
  882. if (FAILED(result)) {
  883. return D3D_SetError("IDirect3DDevice9_SetPixelShader()", result);
  884. }
  885. data->drawstate.shader = shader;
  886. }
  887. if (shader_params != data->drawstate.shader_params) {
  888. if (shader_params) {
  889. const UINT shader_params_length = 4; /* The YUV shader takes 4 float4 parameters */
  890. const HRESULT result = IDirect3DDevice9_SetPixelShaderConstantF(data->device, 0, shader_params, shader_params_length);
  891. if (FAILED(result)) {
  892. return D3D_SetError("IDirect3DDevice9_SetPixelShaderConstantF()", result);
  893. }
  894. }
  895. data->drawstate.shader_params = shader_params;
  896. }
  897. #endif /* SDL_HAVE_YUV */
  898. data->drawstate.texture = texture;
  899. } else if (texture) {
  900. D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
  901. UpdateDirtyTexture(data->device, &texturedata->texture);
  902. #if SDL_HAVE_YUV
  903. if (texturedata->yuv) {
  904. UpdateDirtyTexture(data->device, &texturedata->utexture);
  905. UpdateDirtyTexture(data->device, &texturedata->vtexture);
  906. }
  907. #endif
  908. }
  909. if (blend != data->drawstate.blend) {
  910. if (blend == SDL_BLENDMODE_NONE) {
  911. IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE, FALSE);
  912. } else {
  913. IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE, TRUE);
  914. IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
  915. GetBlendFunc(SDL_GetBlendModeSrcColorFactor(blend)));
  916. IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
  917. GetBlendFunc(SDL_GetBlendModeDstColorFactor(blend)));
  918. IDirect3DDevice9_SetRenderState(data->device, D3DRS_BLENDOP,
  919. GetBlendEquation(SDL_GetBlendModeColorOperation(blend)));
  920. if (data->enableSeparateAlphaBlend) {
  921. IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLENDALPHA,
  922. GetBlendFunc(SDL_GetBlendModeSrcAlphaFactor(blend)));
  923. IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLENDALPHA,
  924. GetBlendFunc(SDL_GetBlendModeDstAlphaFactor(blend)));
  925. IDirect3DDevice9_SetRenderState(data->device, D3DRS_BLENDOPALPHA,
  926. GetBlendEquation(SDL_GetBlendModeAlphaOperation(blend)));
  927. }
  928. }
  929. data->drawstate.blend = blend;
  930. }
  931. if (data->drawstate.viewport_dirty) {
  932. const SDL_Rect *viewport = &data->drawstate.viewport;
  933. D3DVIEWPORT9 d3dviewport;
  934. d3dviewport.X = viewport->x;
  935. d3dviewport.Y = viewport->y;
  936. d3dviewport.Width = viewport->w;
  937. d3dviewport.Height = viewport->h;
  938. d3dviewport.MinZ = 0.0f;
  939. d3dviewport.MaxZ = 1.0f;
  940. IDirect3DDevice9_SetViewport(data->device, &d3dviewport);
  941. /* Set an orthographic projection matrix */
  942. if (viewport->w && viewport->h) {
  943. D3DMATRIX d3dmatrix;
  944. SDL_zero(d3dmatrix);
  945. d3dmatrix.m[0][0] = 2.0f / viewport->w;
  946. d3dmatrix.m[1][1] = -2.0f / viewport->h;
  947. d3dmatrix.m[2][2] = 1.0f;
  948. d3dmatrix.m[3][0] = -1.0f;
  949. d3dmatrix.m[3][1] = 1.0f;
  950. d3dmatrix.m[3][3] = 1.0f;
  951. IDirect3DDevice9_SetTransform(data->device, D3DTS_PROJECTION, &d3dmatrix);
  952. }
  953. data->drawstate.viewport_dirty = SDL_FALSE;
  954. }
  955. if (data->drawstate.cliprect_enabled_dirty) {
  956. IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, data->drawstate.cliprect_enabled ? TRUE : FALSE);
  957. data->drawstate.cliprect_enabled_dirty = SDL_FALSE;
  958. }
  959. if (data->drawstate.cliprect_dirty) {
  960. const SDL_Rect *viewport = &data->drawstate.viewport;
  961. const SDL_Rect *rect = &data->drawstate.cliprect;
  962. RECT d3drect;
  963. d3drect.left = (LONG)viewport->x + rect->x;
  964. d3drect.top = (LONG)viewport->y + rect->y;
  965. d3drect.right = (LONG)viewport->x + rect->x + rect->w;
  966. d3drect.bottom = (LONG)viewport->y + rect->y + rect->h;
  967. IDirect3DDevice9_SetScissorRect(data->device, &d3drect);
  968. data->drawstate.cliprect_dirty = SDL_FALSE;
  969. }
  970. return 0;
  971. }
  972. static void D3D_InvalidateCachedState(SDL_Renderer *renderer)
  973. {
  974. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  975. data->drawstate.viewport_dirty = SDL_TRUE;
  976. data->drawstate.cliprect_enabled_dirty = SDL_TRUE;
  977. data->drawstate.cliprect_dirty = SDL_TRUE;
  978. data->drawstate.blend = SDL_BLENDMODE_INVALID;
  979. data->drawstate.texture = NULL;
  980. data->drawstate.shader = SHADER_NONE;
  981. data->drawstate.shader_params = NULL;
  982. }
  983. static int D3D_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
  984. {
  985. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  986. const int vboidx = data->currentVertexBuffer;
  987. IDirect3DVertexBuffer9 *vbo = NULL;
  988. const SDL_bool istarget = renderer->target != NULL;
  989. if (D3D_ActivateRenderer(renderer) < 0) {
  990. return -1;
  991. }
  992. if (vertsize > 0) {
  993. /* upload the new VBO data for this set of commands. */
  994. vbo = data->vertexBuffers[vboidx];
  995. if (data->vertexBufferSize[vboidx] < vertsize) {
  996. const DWORD usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
  997. const DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1;
  998. if (vbo) {
  999. IDirect3DVertexBuffer9_Release(vbo);
  1000. }
  1001. if (FAILED(IDirect3DDevice9_CreateVertexBuffer(data->device, (UINT)vertsize, usage, fvf, D3DPOOL_DEFAULT, &vbo, NULL))) {
  1002. vbo = NULL;
  1003. }
  1004. data->vertexBuffers[vboidx] = vbo;
  1005. data->vertexBufferSize[vboidx] = vbo ? vertsize : 0;
  1006. }
  1007. if (vbo) {
  1008. void *ptr;
  1009. if (FAILED(IDirect3DVertexBuffer9_Lock(vbo, 0, (UINT)vertsize, &ptr, D3DLOCK_DISCARD))) {
  1010. vbo = NULL; /* oh well, we'll do immediate mode drawing. :( */
  1011. } else {
  1012. SDL_memcpy(ptr, vertices, vertsize);
  1013. if (FAILED(IDirect3DVertexBuffer9_Unlock(vbo))) {
  1014. vbo = NULL; /* oh well, we'll do immediate mode drawing. :( */
  1015. }
  1016. }
  1017. }
  1018. /* cycle through a few VBOs so D3D has some time with the data before we replace it. */
  1019. if (vbo) {
  1020. data->currentVertexBuffer++;
  1021. if (data->currentVertexBuffer >= SDL_arraysize(data->vertexBuffers)) {
  1022. data->currentVertexBuffer = 0;
  1023. }
  1024. } else if (!data->reportedVboProblem) {
  1025. SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL failed to get a vertex buffer for this Direct3D 9 rendering batch!");
  1026. SDL_LogError(SDL_LOG_CATEGORY_RENDER, "Dropping back to a slower method.");
  1027. SDL_LogError(SDL_LOG_CATEGORY_RENDER, "This might be a brief hiccup, but if performance is bad, this is probably why.");
  1028. SDL_LogError(SDL_LOG_CATEGORY_RENDER, "This error will not be logged again for this renderer.");
  1029. data->reportedVboProblem = SDL_TRUE;
  1030. }
  1031. }
  1032. IDirect3DDevice9_SetStreamSource(data->device, 0, vbo, 0, sizeof(Vertex));
  1033. while (cmd) {
  1034. switch (cmd->command) {
  1035. case SDL_RENDERCMD_SETDRAWCOLOR:
  1036. {
  1037. /* currently this is sent with each vertex, but if we move to
  1038. shaders, we can put this in a uniform here and reduce vertex
  1039. buffer bandwidth */
  1040. break;
  1041. }
  1042. case SDL_RENDERCMD_SETCOLORSCALE:
  1043. {
  1044. break;
  1045. }
  1046. case SDL_RENDERCMD_SETVIEWPORT:
  1047. {
  1048. SDL_Rect *viewport = &data->drawstate.viewport;
  1049. if (SDL_memcmp(viewport, &cmd->data.viewport.rect, sizeof(cmd->data.viewport.rect)) != 0) {
  1050. SDL_copyp(viewport, &cmd->data.viewport.rect);
  1051. data->drawstate.viewport_dirty = SDL_TRUE;
  1052. }
  1053. break;
  1054. }
  1055. case SDL_RENDERCMD_SETCLIPRECT:
  1056. {
  1057. const SDL_Rect *rect = &cmd->data.cliprect.rect;
  1058. if (data->drawstate.cliprect_enabled != cmd->data.cliprect.enabled) {
  1059. data->drawstate.cliprect_enabled = cmd->data.cliprect.enabled;
  1060. data->drawstate.cliprect_enabled_dirty = SDL_TRUE;
  1061. }
  1062. if (SDL_memcmp(&data->drawstate.cliprect, rect, sizeof(*rect)) != 0) {
  1063. SDL_copyp(&data->drawstate.cliprect, rect);
  1064. data->drawstate.cliprect_dirty = SDL_TRUE;
  1065. }
  1066. break;
  1067. }
  1068. case SDL_RENDERCMD_CLEAR:
  1069. {
  1070. const DWORD color = D3DCOLOR_COLORVALUE(cmd->data.color.color.r * cmd->data.color.color_scale,
  1071. cmd->data.color.color.g * cmd->data.color.color_scale,
  1072. cmd->data.color.color.b * cmd->data.color.color_scale,
  1073. cmd->data.color.color.a);
  1074. const SDL_Rect *viewport = &data->drawstate.viewport;
  1075. const int backw = istarget ? renderer->target->w : data->pparams.BackBufferWidth;
  1076. const int backh = istarget ? renderer->target->h : data->pparams.BackBufferHeight;
  1077. const SDL_bool viewport_equal = ((viewport->x == 0) && (viewport->y == 0) && (viewport->w == backw) && (viewport->h == backh));
  1078. if (data->drawstate.cliprect_enabled || data->drawstate.cliprect_enabled_dirty) {
  1079. IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, FALSE);
  1080. data->drawstate.cliprect_enabled_dirty = data->drawstate.cliprect_enabled;
  1081. }
  1082. /* Don't reset the viewport if we don't have to! */
  1083. if (!data->drawstate.viewport_dirty && viewport_equal) {
  1084. IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
  1085. } else {
  1086. /* Clear is defined to clear the entire render target */
  1087. D3DVIEWPORT9 wholeviewport = { 0, 0, 0, 0, 0.0f, 1.0f };
  1088. wholeviewport.Width = backw;
  1089. wholeviewport.Height = backh;
  1090. IDirect3DDevice9_SetViewport(data->device, &wholeviewport);
  1091. data->drawstate.viewport_dirty = SDL_TRUE; /* we still need to (re)set orthographic projection, so always mark it dirty. */
  1092. IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
  1093. }
  1094. break;
  1095. }
  1096. case SDL_RENDERCMD_DRAW_POINTS:
  1097. {
  1098. const size_t count = cmd->data.draw.count;
  1099. const size_t first = cmd->data.draw.first;
  1100. SetDrawState(data, cmd);
  1101. if (vbo) {
  1102. IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_POINTLIST, (UINT)(first / sizeof(Vertex)), (UINT)count);
  1103. } else {
  1104. const Vertex *verts = (Vertex *)(((Uint8 *)vertices) + first);
  1105. IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, (UINT)count, verts, sizeof(Vertex));
  1106. }
  1107. break;
  1108. }
  1109. case SDL_RENDERCMD_DRAW_LINES:
  1110. {
  1111. const size_t count = cmd->data.draw.count;
  1112. const size_t first = cmd->data.draw.first;
  1113. const Vertex *verts = (Vertex *)(((Uint8 *)vertices) + first);
  1114. /* DirectX 9 has the same line rasterization semantics as GDI,
  1115. so we need to close the endpoint of the line with a second draw call.
  1116. NOLINTNEXTLINE(clang-analyzer-core.NullDereference): FIXME: Can verts truly not be NULL ? */
  1117. const SDL_bool close_endpoint = ((count == 2) || (verts[0].x != verts[count - 1].x) || (verts[0].y != verts[count - 1].y));
  1118. SetDrawState(data, cmd);
  1119. if (vbo) {
  1120. IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_LINESTRIP, (UINT)(first / sizeof(Vertex)), (UINT)(count - 1));
  1121. if (close_endpoint) {
  1122. IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_POINTLIST, (UINT)((first / sizeof(Vertex)) + (count - 1)), 1);
  1123. }
  1124. } else {
  1125. IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, (UINT)(count - 1), verts, sizeof(Vertex));
  1126. if (close_endpoint) {
  1127. IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1, &verts[count - 1], sizeof(Vertex));
  1128. }
  1129. }
  1130. break;
  1131. }
  1132. case SDL_RENDERCMD_FILL_RECTS: /* unused */
  1133. break;
  1134. case SDL_RENDERCMD_COPY: /* unused */
  1135. break;
  1136. case SDL_RENDERCMD_COPY_EX: /* unused */
  1137. break;
  1138. case SDL_RENDERCMD_GEOMETRY:
  1139. {
  1140. const size_t count = cmd->data.draw.count;
  1141. const size_t first = cmd->data.draw.first;
  1142. SetDrawState(data, cmd);
  1143. if (vbo) {
  1144. IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_TRIANGLELIST, (UINT)(first / sizeof(Vertex)), (UINT)count / 3);
  1145. } else {
  1146. const Vertex *verts = (Vertex *)(((Uint8 *)vertices) + first);
  1147. IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLELIST, (UINT)count / 3, verts, sizeof(Vertex));
  1148. }
  1149. break;
  1150. }
  1151. case SDL_RENDERCMD_NO_OP:
  1152. break;
  1153. }
  1154. cmd = cmd->next;
  1155. }
  1156. return 0;
  1157. }
  1158. static SDL_Surface *D3D_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect)
  1159. {
  1160. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  1161. D3DSURFACE_DESC desc;
  1162. LPDIRECT3DSURFACE9 backBuffer;
  1163. LPDIRECT3DSURFACE9 surface;
  1164. RECT d3drect;
  1165. D3DLOCKED_RECT locked;
  1166. HRESULT result;
  1167. SDL_Surface *output;
  1168. if (data->currentRenderTarget) {
  1169. backBuffer = data->currentRenderTarget;
  1170. } else {
  1171. backBuffer = data->defaultRenderTarget;
  1172. }
  1173. result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
  1174. if (FAILED(result)) {
  1175. D3D_SetError("GetDesc()", result);
  1176. return NULL;
  1177. }
  1178. result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
  1179. if (FAILED(result)) {
  1180. D3D_SetError("CreateOffscreenPlainSurface()", result);
  1181. return NULL;
  1182. }
  1183. result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
  1184. if (FAILED(result)) {
  1185. IDirect3DSurface9_Release(surface);
  1186. D3D_SetError("GetRenderTargetData()", result);
  1187. return NULL;
  1188. }
  1189. d3drect.left = rect->x;
  1190. d3drect.right = (LONG)rect->x + rect->w;
  1191. d3drect.top = rect->y;
  1192. d3drect.bottom = (LONG)rect->y + rect->h;
  1193. result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
  1194. if (FAILED(result)) {
  1195. IDirect3DSurface9_Release(surface);
  1196. D3D_SetError("LockRect()", result);
  1197. return NULL;
  1198. }
  1199. output = SDL_DuplicatePixels(rect->w, rect->h, D3DFMTToPixelFormat(desc.Format), SDL_COLORSPACE_SRGB, locked.pBits, locked.Pitch);
  1200. IDirect3DSurface9_UnlockRect(surface);
  1201. IDirect3DSurface9_Release(surface);
  1202. return output;
  1203. }
  1204. static int D3D_RenderPresent(SDL_Renderer *renderer)
  1205. {
  1206. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  1207. HRESULT result;
  1208. if (!data->beginScene) {
  1209. IDirect3DDevice9_EndScene(data->device);
  1210. data->beginScene = SDL_TRUE;
  1211. }
  1212. result = IDirect3DDevice9_TestCooperativeLevel(data->device);
  1213. if (result == D3DERR_DEVICELOST) {
  1214. /* We'll reset later */
  1215. return -1;
  1216. }
  1217. if (result == D3DERR_DEVICENOTRESET) {
  1218. D3D_Reset(renderer);
  1219. }
  1220. result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
  1221. if (FAILED(result)) {
  1222. return D3D_SetError("Present()", result);
  1223. }
  1224. return 0;
  1225. }
  1226. static void D3D_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
  1227. {
  1228. D3D_RenderData *renderdata = (D3D_RenderData *)renderer->driverdata;
  1229. D3D_TextureData *data = (D3D_TextureData *)texture->driverdata;
  1230. if (renderdata->drawstate.texture == texture) {
  1231. renderdata->drawstate.texture = NULL;
  1232. renderdata->drawstate.shader = SHADER_NONE;
  1233. renderdata->drawstate.shader_params = NULL;
  1234. IDirect3DDevice9_SetPixelShader(renderdata->device, NULL);
  1235. IDirect3DDevice9_SetTexture(renderdata->device, 0, NULL);
  1236. #if SDL_HAVE_YUV
  1237. if (data->yuv) {
  1238. IDirect3DDevice9_SetTexture(renderdata->device, 1, NULL);
  1239. IDirect3DDevice9_SetTexture(renderdata->device, 2, NULL);
  1240. }
  1241. #endif
  1242. }
  1243. if (!data) {
  1244. return;
  1245. }
  1246. D3D_DestroyTextureRep(&data->texture);
  1247. #if SDL_HAVE_YUV
  1248. D3D_DestroyTextureRep(&data->utexture);
  1249. D3D_DestroyTextureRep(&data->vtexture);
  1250. SDL_free(data->pixels);
  1251. #endif
  1252. SDL_free(data);
  1253. texture->driverdata = NULL;
  1254. }
  1255. static void D3D_DestroyRenderer(SDL_Renderer *renderer)
  1256. {
  1257. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  1258. if (data) {
  1259. int i;
  1260. /* Release the render target */
  1261. if (data->defaultRenderTarget) {
  1262. IDirect3DSurface9_Release(data->defaultRenderTarget);
  1263. data->defaultRenderTarget = NULL;
  1264. }
  1265. if (data->currentRenderTarget) {
  1266. IDirect3DSurface9_Release(data->currentRenderTarget);
  1267. data->currentRenderTarget = NULL;
  1268. }
  1269. #if SDL_HAVE_YUV
  1270. for (i = 0; i < SDL_arraysize(data->shaders); ++i) {
  1271. if (data->shaders[i]) {
  1272. IDirect3DPixelShader9_Release(data->shaders[i]);
  1273. data->shaders[i] = NULL;
  1274. }
  1275. }
  1276. #endif
  1277. /* Release all vertex buffers */
  1278. for (i = 0; i < SDL_arraysize(data->vertexBuffers); ++i) {
  1279. if (data->vertexBuffers[i]) {
  1280. IDirect3DVertexBuffer9_Release(data->vertexBuffers[i]);
  1281. }
  1282. data->vertexBuffers[i] = NULL;
  1283. }
  1284. if (data->device) {
  1285. IDirect3DDevice9_Release(data->device);
  1286. data->device = NULL;
  1287. }
  1288. if (data->d3d) {
  1289. IDirect3D9_Release(data->d3d);
  1290. SDL_UnloadObject(data->d3dDLL);
  1291. }
  1292. SDL_free(data);
  1293. }
  1294. SDL_free(renderer);
  1295. }
  1296. static int D3D_Reset(SDL_Renderer *renderer)
  1297. {
  1298. D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
  1299. const Float4X4 d3dmatrix = MatrixIdentity();
  1300. HRESULT result;
  1301. SDL_Texture *texture;
  1302. int i;
  1303. /* Cancel any scene that we've started */
  1304. if (!data->beginScene) {
  1305. IDirect3DDevice9_EndScene(data->device);
  1306. data->beginScene = SDL_TRUE;
  1307. }
  1308. /* Release the default render target before reset */
  1309. if (data->defaultRenderTarget) {
  1310. IDirect3DSurface9_Release(data->defaultRenderTarget);
  1311. data->defaultRenderTarget = NULL;
  1312. }
  1313. if (data->currentRenderTarget) {
  1314. IDirect3DSurface9_Release(data->currentRenderTarget);
  1315. data->currentRenderTarget = NULL;
  1316. }
  1317. /* Release application render targets */
  1318. for (texture = renderer->textures; texture; texture = texture->next) {
  1319. if (texture->access == SDL_TEXTUREACCESS_TARGET) {
  1320. D3D_DestroyTexture(renderer, texture);
  1321. } else {
  1322. D3D_RecreateTexture(renderer, texture);
  1323. }
  1324. }
  1325. /* Release all vertex buffers */
  1326. for (i = 0; i < SDL_arraysize(data->vertexBuffers); ++i) {
  1327. if (data->vertexBuffers[i]) {
  1328. IDirect3DVertexBuffer9_Release(data->vertexBuffers[i]);
  1329. }
  1330. data->vertexBuffers[i] = NULL;
  1331. data->vertexBufferSize[i] = 0;
  1332. }
  1333. result = IDirect3DDevice9_Reset(data->device, &data->pparams);
  1334. if (FAILED(result)) {
  1335. if (result == D3DERR_DEVICELOST) {
  1336. /* Don't worry about it, we'll reset later... */
  1337. return 0;
  1338. } else {
  1339. return D3D_SetError("Reset()", result);
  1340. }
  1341. }
  1342. /* Allocate application render targets */
  1343. for (texture = renderer->textures; texture; texture = texture->next) {
  1344. if (texture->access == SDL_TEXTUREACCESS_TARGET) {
  1345. D3D_CreateTexture(renderer, texture, 0);
  1346. }
  1347. }
  1348. IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget);
  1349. D3D_InitRenderState(data);
  1350. D3D_SetRenderTargetInternal(renderer, renderer->target);
  1351. D3D_InvalidateCachedState(renderer);
  1352. IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, (D3DMATRIX *)&d3dmatrix);
  1353. /* Let the application know that render targets were reset */
  1354. {
  1355. SDL_Event event;
  1356. event.type = SDL_EVENT_RENDER_TARGETS_RESET;
  1357. event.common.timestamp = 0;
  1358. SDL_PushEvent(&event);
  1359. }
  1360. return 0;
  1361. }
  1362. static int D3D_SetVSync(SDL_Renderer *renderer, const int vsync)
  1363. {
  1364. D3D_RenderData *data = renderer->driverdata;
  1365. if (vsync) {
  1366. data->pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  1367. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  1368. } else {
  1369. data->pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  1370. renderer->info.flags &= ~SDL_RENDERER_PRESENTVSYNC;
  1371. }
  1372. if (D3D_Reset(renderer) < 0) {
  1373. /* D3D_Reset will call SDL_SetError() */
  1374. return -1;
  1375. }
  1376. return 0;
  1377. }
  1378. SDL_Renderer *D3D_CreateRenderer(SDL_Window *window, SDL_PropertiesID create_props)
  1379. {
  1380. SDL_Renderer *renderer;
  1381. D3D_RenderData *data;
  1382. HRESULT result;
  1383. D3DPRESENT_PARAMETERS pparams;
  1384. IDirect3DSwapChain9 *chain;
  1385. D3DCAPS9 caps;
  1386. DWORD device_flags;
  1387. int w, h;
  1388. SDL_DisplayID displayID;
  1389. const SDL_DisplayMode *fullscreen_mode = NULL;
  1390. renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
  1391. if (!renderer) {
  1392. return NULL;
  1393. }
  1394. renderer->magic = &SDL_renderer_magic;
  1395. SDL_SetupRendererColorspace(renderer, create_props);
  1396. if (renderer->output_colorspace != SDL_COLORSPACE_SRGB) {
  1397. SDL_SetError("Unsupported output colorspace");
  1398. SDL_free(renderer);
  1399. return NULL;
  1400. }
  1401. data = (D3D_RenderData *)SDL_calloc(1, sizeof(*data));
  1402. if (!data) {
  1403. SDL_free(renderer);
  1404. return NULL;
  1405. }
  1406. if (!D3D_LoadDLL(&data->d3dDLL, &data->d3d)) {
  1407. SDL_SetError("Unable to create Direct3D interface");
  1408. SDL_free(renderer);
  1409. SDL_free(data);
  1410. return NULL;
  1411. }
  1412. renderer->WindowEvent = D3D_WindowEvent;
  1413. renderer->SupportsBlendMode = D3D_SupportsBlendMode;
  1414. renderer->CreateTexture = D3D_CreateTexture;
  1415. renderer->UpdateTexture = D3D_UpdateTexture;
  1416. #if SDL_HAVE_YUV
  1417. renderer->UpdateTextureYUV = D3D_UpdateTextureYUV;
  1418. #endif
  1419. renderer->LockTexture = D3D_LockTexture;
  1420. renderer->UnlockTexture = D3D_UnlockTexture;
  1421. renderer->SetTextureScaleMode = D3D_SetTextureScaleMode;
  1422. renderer->SetRenderTarget = D3D_SetRenderTarget;
  1423. renderer->QueueSetViewport = D3D_QueueNoOp;
  1424. renderer->QueueSetDrawColor = D3D_QueueNoOp;
  1425. renderer->QueueSetColorScale = D3D_QueueNoOp;
  1426. renderer->QueueDrawPoints = D3D_QueueDrawPoints;
  1427. renderer->QueueDrawLines = D3D_QueueDrawPoints; /* lines and points queue vertices the same way. */
  1428. renderer->QueueGeometry = D3D_QueueGeometry;
  1429. renderer->InvalidateCachedState = D3D_InvalidateCachedState;
  1430. renderer->RunCommandQueue = D3D_RunCommandQueue;
  1431. renderer->RenderReadPixels = D3D_RenderReadPixels;
  1432. renderer->RenderPresent = D3D_RenderPresent;
  1433. renderer->DestroyTexture = D3D_DestroyTexture;
  1434. renderer->DestroyRenderer = D3D_DestroyRenderer;
  1435. renderer->SetVSync = D3D_SetVSync;
  1436. renderer->info = D3D_RenderDriver.info;
  1437. renderer->info.flags = SDL_RENDERER_ACCELERATED;
  1438. renderer->driverdata = data;
  1439. D3D_InvalidateCachedState(renderer);
  1440. SDL_GetWindowSizeInPixels(window, &w, &h);
  1441. if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
  1442. fullscreen_mode = SDL_GetWindowFullscreenMode(window);
  1443. }
  1444. SDL_zero(pparams);
  1445. pparams.hDeviceWindow = (HWND)SDL_GetProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
  1446. pparams.BackBufferWidth = w;
  1447. pparams.BackBufferHeight = h;
  1448. pparams.BackBufferCount = 1;
  1449. pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
  1450. if (fullscreen_mode) {
  1451. pparams.Windowed = FALSE;
  1452. pparams.BackBufferFormat = PixelFormatToD3DFMT(fullscreen_mode->format);
  1453. pparams.FullScreen_RefreshRateInHz = (UINT)SDL_ceilf(fullscreen_mode->refresh_rate);
  1454. } else {
  1455. pparams.Windowed = TRUE;
  1456. pparams.BackBufferFormat = D3DFMT_UNKNOWN;
  1457. pparams.FullScreen_RefreshRateInHz = 0;
  1458. }
  1459. if (SDL_GetBooleanProperty(create_props, SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_BOOLEAN, SDL_FALSE)) {
  1460. pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  1461. } else {
  1462. pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  1463. }
  1464. /* Get the adapter for the display that the window is on */
  1465. displayID = SDL_GetDisplayForWindow(window);
  1466. data->adapter = SDL_Direct3D9GetAdapterIndex(displayID);
  1467. IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
  1468. device_flags = D3DCREATE_FPU_PRESERVE;
  1469. if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) {
  1470. device_flags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
  1471. } else {
  1472. device_flags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  1473. }
  1474. if (SDL_GetHintBoolean(SDL_HINT_RENDER_DIRECT3D_THREADSAFE, SDL_FALSE)) {
  1475. device_flags |= D3DCREATE_MULTITHREADED;
  1476. }
  1477. result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
  1478. D3DDEVTYPE_HAL,
  1479. pparams.hDeviceWindow,
  1480. device_flags,
  1481. &pparams, &data->device);
  1482. if (FAILED(result)) {
  1483. D3D_DestroyRenderer(renderer);
  1484. D3D_SetError("CreateDevice()", result);
  1485. return NULL;
  1486. }
  1487. /* Get presentation parameters to fill info */
  1488. result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
  1489. if (FAILED(result)) {
  1490. D3D_DestroyRenderer(renderer);
  1491. D3D_SetError("GetSwapChain()", result);
  1492. return NULL;
  1493. }
  1494. result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
  1495. if (FAILED(result)) {
  1496. IDirect3DSwapChain9_Release(chain);
  1497. D3D_DestroyRenderer(renderer);
  1498. D3D_SetError("GetPresentParameters()", result);
  1499. return NULL;
  1500. }
  1501. IDirect3DSwapChain9_Release(chain);
  1502. if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
  1503. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  1504. }
  1505. data->pparams = pparams;
  1506. IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
  1507. renderer->info.max_texture_width = caps.MaxTextureWidth;
  1508. renderer->info.max_texture_height = caps.MaxTextureHeight;
  1509. if (caps.PrimitiveMiscCaps & D3DPMISCCAPS_SEPARATEALPHABLEND) {
  1510. data->enableSeparateAlphaBlend = SDL_TRUE;
  1511. }
  1512. /* Store the default render target */
  1513. IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget);
  1514. data->currentRenderTarget = NULL;
  1515. /* Set up parameters for rendering */
  1516. D3D_InitRenderState(data);
  1517. #if SDL_HAVE_YUV
  1518. if (caps.MaxSimultaneousTextures >= 3) {
  1519. int i;
  1520. for (i = SHADER_NONE + 1; i < SDL_arraysize(data->shaders); ++i) {
  1521. result = D3D9_CreatePixelShader(data->device, (D3D9_Shader)i, &data->shaders[i]);
  1522. if (FAILED(result)) {
  1523. D3D_SetError("CreatePixelShader()", result);
  1524. }
  1525. }
  1526. if (data->shaders[SHADER_YUV]) {
  1527. renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
  1528. renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
  1529. }
  1530. }
  1531. #endif
  1532. SDL_SetProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_D3D9_DEVICE_POINTER, data->device);
  1533. return renderer;
  1534. }
  1535. SDL_RenderDriver D3D_RenderDriver = {
  1536. D3D_CreateRenderer,
  1537. { "direct3d",
  1538. (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
  1539. 1,
  1540. { SDL_PIXELFORMAT_ARGB8888 },
  1541. 0,
  1542. 0 }
  1543. };
  1544. #endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */