SDL_render_d3d.c 61 KB

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