Skip to content

Latest commit

 

History

History
1845 lines (1598 loc) · 61.4 KB

SDL_render_d3d.c

File metadata and controls

1845 lines (1598 loc) · 61.4 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 5, 2019
Jan 5, 2019
3
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "SDL_render.h"
#include "SDL_system.h"
#if SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED
#include "../../core/windows/SDL_windows.h"
#include "SDL_hints.h"
#include "SDL_loadso.h"
#include "SDL_syswm.h"
Oct 1, 2018
Oct 1, 2018
33
34
#include "SDL_log.h"
#include "SDL_assert.h"
35
36
37
38
39
40
41
42
43
#include "../SDL_sysrender.h"
#include "../SDL_d3dmath.h"
#include "../../video/windows/SDL_windowsvideo.h"
#if SDL_VIDEO_RENDER_D3D
#define D3D_DEBUG_INFO
#include <d3d9.h>
#endif
Nov 13, 2017
Nov 13, 2017
44
#include "SDL_shaders_d3d.h"
Oct 1, 2018
Oct 1, 2018
46
47
48
49
50
51
52
53
54
55
56
57
58
typedef struct
{
SDL_Rect viewport;
SDL_bool viewport_dirty;
SDL_Texture *texture;
SDL_BlendMode blend;
SDL_bool cliprect_enabled;
SDL_bool cliprect_enabled_dirty;
SDL_Rect cliprect;
SDL_bool cliprect_dirty;
SDL_bool is_copy_ex;
LPDIRECT3DPIXELSHADER9 shader;
} D3D_DrawStateCache;
Oct 1, 2018
Oct 1, 2018
61
/* Direct3D renderer implementation */
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
typedef struct
{
void* d3dDLL;
IDirect3D9 *d3d;
IDirect3DDevice9 *device;
UINT adapter;
D3DPRESENT_PARAMETERS pparams;
SDL_bool updateSize;
SDL_bool beginScene;
SDL_bool enableSeparateAlphaBlend;
D3DTEXTUREFILTERTYPE scaleMode[8];
IDirect3DSurface9 *defaultRenderTarget;
IDirect3DSurface9 *currentRenderTarget;
void* d3dxDLL;
Nov 13, 2017
Nov 13, 2017
77
LPDIRECT3DPIXELSHADER9 shaders[NUM_SHADERS];
Oct 1, 2018
Oct 1, 2018
78
79
LPDIRECT3DVERTEXBUFFER9 vertexBuffers[8];
size_t vertexBufferSize[8];
Oct 1, 2018
Oct 1, 2018
80
81
82
int currentVertexBuffer;
SDL_bool reportedVboProblem;
D3D_DrawStateCache drawstate;
83
84
85
86
87
88
89
90
} D3D_RenderData;
typedef struct
{
SDL_bool dirty;
int w, h;
DWORD usage;
Uint32 format;
Nov 13, 2017
Nov 13, 2017
91
D3DFORMAT d3dfmt;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
IDirect3DTexture9 *texture;
IDirect3DTexture9 *staging;
} D3D_TextureRep;
typedef struct
{
D3D_TextureRep texture;
D3DTEXTUREFILTERTYPE scaleMode;
/* YV12 texture support */
SDL_bool yuv;
D3D_TextureRep utexture;
D3D_TextureRep vtexture;
Uint8 *pixels;
int pitch;
SDL_Rect locked_rect;
} D3D_TextureData;
typedef struct
{
float x, y, z;
DWORD color;
float u, v;
} Vertex;
static int
D3D_SetError(const char *prefix, HRESULT result)
{
const char *error;
switch (result) {
case D3DERR_WRONGTEXTUREFORMAT:
error = "WRONGTEXTUREFORMAT";
break;
case D3DERR_UNSUPPORTEDCOLOROPERATION:
error = "UNSUPPORTEDCOLOROPERATION";
break;
case D3DERR_UNSUPPORTEDCOLORARG:
error = "UNSUPPORTEDCOLORARG";
break;
case D3DERR_UNSUPPORTEDALPHAOPERATION:
error = "UNSUPPORTEDALPHAOPERATION";
break;
case D3DERR_UNSUPPORTEDALPHAARG:
error = "UNSUPPORTEDALPHAARG";
break;
case D3DERR_TOOMANYOPERATIONS:
error = "TOOMANYOPERATIONS";
break;
case D3DERR_CONFLICTINGTEXTUREFILTER:
error = "CONFLICTINGTEXTUREFILTER";
break;
case D3DERR_UNSUPPORTEDFACTORVALUE:
error = "UNSUPPORTEDFACTORVALUE";
break;
case D3DERR_CONFLICTINGRENDERSTATE:
error = "CONFLICTINGRENDERSTATE";
break;
case D3DERR_UNSUPPORTEDTEXTUREFILTER:
error = "UNSUPPORTEDTEXTUREFILTER";
break;
case D3DERR_CONFLICTINGTEXTUREPALETTE:
error = "CONFLICTINGTEXTUREPALETTE";
break;
case D3DERR_DRIVERINTERNALERROR:
error = "DRIVERINTERNALERROR";
break;
case D3DERR_NOTFOUND:
error = "NOTFOUND";
break;
case D3DERR_MOREDATA:
error = "MOREDATA";
break;
case D3DERR_DEVICELOST:
error = "DEVICELOST";
break;
case D3DERR_DEVICENOTRESET:
error = "DEVICENOTRESET";
break;
case D3DERR_NOTAVAILABLE:
error = "NOTAVAILABLE";
break;
case D3DERR_OUTOFVIDEOMEMORY:
error = "OUTOFVIDEOMEMORY";
break;
case D3DERR_INVALIDDEVICE:
error = "INVALIDDEVICE";
break;
case D3DERR_INVALIDCALL:
error = "INVALIDCALL";
break;
case D3DERR_DRIVERINVALIDCALL:
error = "DRIVERINVALIDCALL";
break;
case D3DERR_WASSTILLDRAWING:
error = "WASSTILLDRAWING";
break;
default:
error = "UNKNOWN";
break;
}
return SDL_SetError("%s: %s", prefix, error);
}
static D3DFORMAT
PixelFormatToD3DFMT(Uint32 format)
{
switch (format) {
case SDL_PIXELFORMAT_RGB565:
return D3DFMT_R5G6B5;
case SDL_PIXELFORMAT_RGB888:
return D3DFMT_X8R8G8B8;
case SDL_PIXELFORMAT_ARGB8888:
return D3DFMT_A8R8G8B8;
case SDL_PIXELFORMAT_YV12:
case SDL_PIXELFORMAT_IYUV:
Nov 13, 2017
Nov 13, 2017
208
209
case SDL_PIXELFORMAT_NV12:
case SDL_PIXELFORMAT_NV21:
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
return D3DFMT_L8;
default:
return D3DFMT_UNKNOWN;
}
}
static Uint32
D3DFMTToPixelFormat(D3DFORMAT format)
{
switch (format) {
case D3DFMT_R5G6B5:
return SDL_PIXELFORMAT_RGB565;
case D3DFMT_X8R8G8B8:
return SDL_PIXELFORMAT_RGB888;
case D3DFMT_A8R8G8B8:
return SDL_PIXELFORMAT_ARGB8888;
default:
return SDL_PIXELFORMAT_UNKNOWN;
}
}
static void
D3D_InitRenderState(D3D_RenderData *data)
{
D3DMATRIX matrix;
IDirect3DDevice9 *device = data->device;
IDirect3DDevice9_SetFVF(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
Oct 1, 2018
Oct 1, 2018
238
IDirect3DDevice9_SetVertexShader(device, NULL);
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
IDirect3DDevice9_SetRenderState(device, D3DRS_ZENABLE, D3DZB_FALSE);
IDirect3DDevice9_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE);
IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
/* Enable color modulation by diffuse color */
IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_COLOROP,
D3DTOP_MODULATE);
IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_COLORARG1,
D3DTA_TEXTURE);
IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_COLORARG2,
D3DTA_DIFFUSE);
/* Enable alpha modulation by diffuse alpha */
IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_ALPHAOP,
D3DTOP_MODULATE);
IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_ALPHAARG1,
D3DTA_TEXTURE);
IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_ALPHAARG2,
D3DTA_DIFFUSE);
/* Enable separate alpha blend function, if possible */
if (data->enableSeparateAlphaBlend) {
IDirect3DDevice9_SetRenderState(device, D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
}
/* Disable second texture stage, since we're done */
IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_COLOROP,
D3DTOP_DISABLE);
IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_ALPHAOP,
D3DTOP_DISABLE);
/* Set an identity world and view matrix */
Oct 1, 2018
Oct 1, 2018
271
SDL_zero(matrix);
272
273
274
275
276
277
278
279
280
281
282
283
284
285
matrix.m[0][0] = 1.0f;
matrix.m[1][1] = 1.0f;
matrix.m[2][2] = 1.0f;
matrix.m[3][3] = 1.0f;
IDirect3DDevice9_SetTransform(device, D3DTS_WORLD, &matrix);
IDirect3DDevice9_SetTransform(device, D3DTS_VIEW, &matrix);
/* Reset our current scale mode */
SDL_memset(data->scaleMode, 0xFF, sizeof(data->scaleMode));
/* Start the render with beginScene */
data->beginScene = SDL_TRUE;
}
Oct 1, 2018
Oct 1, 2018
286
static int D3D_Reset(SDL_Renderer * renderer);
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
static int
D3D_ActivateRenderer(SDL_Renderer * renderer)
{
D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
HRESULT result;
if (data->updateSize) {
SDL_Window *window = renderer->window;
int w, h;
Uint32 window_flags = SDL_GetWindowFlags(window);
SDL_GetWindowSize(window, &w, &h);
data->pparams.BackBufferWidth = w;
data->pparams.BackBufferHeight = h;
if (window_flags & SDL_WINDOW_FULLSCREEN && (window_flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != SDL_WINDOW_FULLSCREEN_DESKTOP) {
SDL_DisplayMode fullscreen_mode;
SDL_GetWindowDisplayMode(window, &fullscreen_mode);
data->pparams.Windowed = FALSE;
data->pparams.BackBufferFormat = PixelFormatToD3DFMT(fullscreen_mode.format);
data->pparams.FullScreen_RefreshRateInHz = fullscreen_mode.refresh_rate;
} else {
data->pparams.Windowed = TRUE;
data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
data->pparams.FullScreen_RefreshRateInHz = 0;
}
if (D3D_Reset(renderer) < 0) {
return -1;
}
data->updateSize = SDL_FALSE;
}
if (data->beginScene) {
result = IDirect3DDevice9_BeginScene(data->device);
if (result == D3DERR_DEVICELOST) {
if (D3D_Reset(renderer) < 0) {
return -1;
}
result = IDirect3DDevice9_BeginScene(data->device);
}
if (FAILED(result)) {
return D3D_SetError("BeginScene()", result);
}
data->beginScene = SDL_FALSE;
}
return 0;
}
static void
D3D_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
{
D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
data->updateSize = SDL_TRUE;
}
}
Aug 14, 2017
Aug 14, 2017
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
static D3DBLEND GetBlendFunc(SDL_BlendFactor factor)
{
switch (factor) {
case SDL_BLENDFACTOR_ZERO:
return D3DBLEND_ZERO;
case SDL_BLENDFACTOR_ONE:
return D3DBLEND_ONE;
case SDL_BLENDFACTOR_SRC_COLOR:
return D3DBLEND_SRCCOLOR;
case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR:
return D3DBLEND_INVSRCCOLOR;
case SDL_BLENDFACTOR_SRC_ALPHA:
return D3DBLEND_SRCALPHA;
case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA:
return D3DBLEND_INVSRCALPHA;
case SDL_BLENDFACTOR_DST_COLOR:
return D3DBLEND_DESTCOLOR;
case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR:
return D3DBLEND_INVDESTCOLOR;
case SDL_BLENDFACTOR_DST_ALPHA:
return D3DBLEND_DESTALPHA;
case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA:
return D3DBLEND_INVDESTALPHA;
default:
return (D3DBLEND)0;
}
}
static SDL_bool
D3D_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
{
D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
if (!GetBlendFunc(srcColorFactor) || !GetBlendFunc(srcAlphaFactor) ||
!GetBlendFunc(dstColorFactor) || !GetBlendFunc(dstAlphaFactor)) {
return SDL_FALSE;
}
if ((srcColorFactor != srcAlphaFactor || dstColorFactor != dstAlphaFactor) && !data->enableSeparateAlphaBlend) {
return SDL_FALSE;
}
if (colorOperation != SDL_BLENDOPERATION_ADD || alphaOperation != SDL_BLENDOPERATION_ADD) {
return SDL_FALSE;
}
return SDL_TRUE;
}
Nov 13, 2017
Nov 13, 2017
398
D3D_CreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, DWORD usage, Uint32 format, D3DFORMAT d3dfmt, int w, int h)
399
400
401
402
403
404
405
406
{
HRESULT result;
texture->dirty = SDL_FALSE;
texture->w = w;
texture->h = h;
texture->usage = usage;
texture->format = format;
Nov 13, 2017
Nov 13, 2017
407
texture->d3dfmt = d3dfmt;
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
result = IDirect3DDevice9_CreateTexture(device, w, h, 1, usage,
PixelFormatToD3DFMT(format),
D3DPOOL_DEFAULT, &texture->texture, NULL);
if (FAILED(result)) {
return D3D_SetError("CreateTexture(D3DPOOL_DEFAULT)", result);
}
return 0;
}
static int
D3D_CreateStagingTexture(IDirect3DDevice9 *device, D3D_TextureRep *texture)
{
HRESULT result;
if (texture->staging == NULL) {
result = IDirect3DDevice9_CreateTexture(device, texture->w, texture->h, 1, 0,
Nov 13, 2017
Nov 13, 2017
426
texture->d3dfmt, D3DPOOL_SYSTEMMEM, &texture->staging, NULL);
427
428
429
430
431
432
433
434
if (FAILED(result)) {
return D3D_SetError("CreateTexture(D3DPOOL_SYSTEMMEM)", result);
}
}
return 0;
}
static int
Nov 13, 2017
Nov 13, 2017
435
D3D_RecreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture)
436
437
438
439
440
441
442
443
444
445
446
447
448
{
if (texture->texture) {
IDirect3DTexture9_Release(texture->texture);
texture->texture = NULL;
}
if (texture->staging) {
IDirect3DTexture9_AddDirtyRect(texture->staging, NULL);
texture->dirty = SDL_TRUE;
}
return 0;
}
static int
Nov 13, 2017
Nov 13, 2017
449
D3D_UpdateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, int x, int y, int w, int h, const void *pixels, int pitch)
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
{
RECT d3drect;
D3DLOCKED_RECT locked;
const Uint8 *src;
Uint8 *dst;
int row, length;
HRESULT result;
if (D3D_CreateStagingTexture(device, texture) < 0) {
return -1;
}
d3drect.left = x;
d3drect.right = x + w;
d3drect.top = y;
d3drect.bottom = y + h;
result = IDirect3DTexture9_LockRect(texture->staging, 0, &locked, &d3drect, 0);
if (FAILED(result)) {
return D3D_SetError("LockRect()", result);
}
src = (const Uint8 *)pixels;
Nov 13, 2017
Nov 13, 2017
473
474
dst = (Uint8 *)locked.pBits;
length = w * SDL_BYTESPERPIXEL(texture->format);
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
if (length == pitch && length == locked.Pitch) {
SDL_memcpy(dst, src, length*h);
} else {
if (length > pitch) {
length = pitch;
}
if (length > locked.Pitch) {
length = locked.Pitch;
}
for (row = 0; row < h; ++row) {
SDL_memcpy(dst, src, length);
src += pitch;
dst += locked.Pitch;
}
}
result = IDirect3DTexture9_UnlockRect(texture->staging, 0);
if (FAILED(result)) {
return D3D_SetError("UnlockRect()", result);
}
texture->dirty = SDL_TRUE;
return 0;
}
static void
D3D_DestroyTextureRep(D3D_TextureRep *texture)
{
if (texture->texture) {
IDirect3DTexture9_Release(texture->texture);
texture->texture = NULL;
}
if (texture->staging) {
IDirect3DTexture9_Release(texture->staging);
texture->staging = NULL;
}
}
static int
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
D3D_TextureData *texturedata;
DWORD usage;
texturedata = (D3D_TextureData *) SDL_calloc(1, sizeof(*texturedata));
if (!texturedata) {
return SDL_OutOfMemory();
}
May 8, 2018
May 8, 2018
523
texturedata->scaleMode = (texture->scaleMode == SDL_ScaleModeNearest) ? D3DTEXF_POINT : D3DTEXF_LINEAR;
524
525
526
527
528
529
530
531
532
texture->driverdata = texturedata;
if (texture->access == SDL_TEXTUREACCESS_TARGET) {
usage = D3DUSAGE_RENDERTARGET;
} else {
usage = 0;
}
Nov 13, 2017
Nov 13, 2017
533
if (D3D_CreateTextureRep(data->device, &texturedata->texture, usage, texture->format, PixelFormatToD3DFMT(texture->format), texture->w, texture->h) < 0) {
534
535
536
537
538
539
540
return -1;
}
if (texture->format == SDL_PIXELFORMAT_YV12 ||
texture->format == SDL_PIXELFORMAT_IYUV) {
texturedata->yuv = SDL_TRUE;
Nov 13, 2017
Nov 13, 2017
541
if (D3D_CreateTextureRep(data->device, &texturedata->utexture, usage, texture->format, PixelFormatToD3DFMT(texture->format), (texture->w + 1) / 2, (texture->h + 1) / 2) < 0) {
542
543
544
return -1;
}
Nov 13, 2017
Nov 13, 2017
545
if (D3D_CreateTextureRep(data->device, &texturedata->vtexture, usage, texture->format, PixelFormatToD3DFMT(texture->format), (texture->w + 1) / 2, (texture->h + 1) / 2) < 0) {
546
547
548
549
550
551
552
553
554
555
556
557
return -1;
}
}
return 0;
}
static int
D3D_RecreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
Jun 25, 2016
Jun 25, 2016
558
559
560
561
if (!texturedata) {
return 0;
}
Nov 13, 2017
Nov 13, 2017
562
if (D3D_RecreateTextureRep(data->device, &texturedata->texture) < 0) {
563
564
565
566
return -1;
}
if (texturedata->yuv) {
Nov 13, 2017
Nov 13, 2017
567
if (D3D_RecreateTextureRep(data->device, &texturedata->utexture) < 0) {
568
569
570
return -1;
}
Nov 13, 2017
Nov 13, 2017
571
if (D3D_RecreateTextureRep(data->device, &texturedata->vtexture) < 0) {
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
return -1;
}
}
return 0;
}
static int
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
{
D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
if (!texturedata) {
SDL_SetError("Texture is not currently available");
return -1;
}
Nov 13, 2017
Nov 13, 2017
590
if (D3D_UpdateTextureRep(data->device, &texturedata->texture, rect->x, rect->y, rect->w, rect->h, pixels, pitch) < 0) {
591
592
593
594
595
596
597
return -1;
}
if (texturedata->yuv) {
/* Skip to the correct offset into the next texture */
pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
Nov 13, 2017
Nov 13, 2017
598
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) {
599
600
601
602
return -1;
}
/* Skip to the correct offset into the next texture */
Nov 13, 2017
Nov 13, 2017
603
604
pixels = (const void*)((const Uint8*)pixels + ((rect->h + 1) / 2) * ((pitch + 1) / 2));
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) {
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
return -1;
}
}
return 0;
}
static int
D3D_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect,
const Uint8 *Yplane, int Ypitch,
const Uint8 *Uplane, int Upitch,
const Uint8 *Vplane, int Vpitch)
{
D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
if (!texturedata) {
SDL_SetError("Texture is not currently available");
return -1;
}
Nov 13, 2017
Nov 13, 2017
626
if (D3D_UpdateTextureRep(data->device, &texturedata->texture, rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch) < 0) {
Nov 13, 2017
Nov 13, 2017
629
if (D3D_UpdateTextureRep(data->device, &texturedata->utexture, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Uplane, Upitch) < 0) {
Nov 13, 2017
Nov 13, 2017
632
if (D3D_UpdateTextureRep(data->device, &texturedata->vtexture, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Vplane, Vpitch) < 0) {
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
return -1;
}
return 0;
}
static int
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
{
D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
IDirect3DDevice9 *device = data->device;
if (!texturedata) {
SDL_SetError("Texture is not currently available");
return -1;
}
texturedata->locked_rect = *rect;
if (texturedata->yuv) {
/* It's more efficient to upload directly... */
if (!texturedata->pixels) {
texturedata->pitch = texture->w;
texturedata->pixels = (Uint8 *)SDL_malloc((texture->h * texturedata->pitch * 3) / 2);
if (!texturedata->pixels) {
return SDL_OutOfMemory();
}
}
*pixels =
(void *) ((Uint8 *) texturedata->pixels + rect->y * texturedata->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = texturedata->pitch;
} else {
RECT d3drect;
D3DLOCKED_RECT locked;
HRESULT result;
if (D3D_CreateStagingTexture(device, &texturedata->texture) < 0) {
return -1;
}
d3drect.left = rect->x;
d3drect.right = rect->x + rect->w;
d3drect.top = rect->y;
d3drect.bottom = rect->y + rect->h;
result = IDirect3DTexture9_LockRect(texturedata->texture.staging, 0, &locked, &d3drect, 0);
if (FAILED(result)) {
return D3D_SetError("LockRect()", result);
}
*pixels = locked.pBits;
*pitch = locked.Pitch;
}
return 0;
}
static void
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
Dec 3, 2018
Dec 3, 2018
693
D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
if (!texturedata) {
return;
}
if (texturedata->yuv) {
const SDL_Rect *rect = &texturedata->locked_rect;
void *pixels =
(void *) ((Uint8 *) texturedata->pixels + rect->y * texturedata->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
D3D_UpdateTexture(renderer, texture, rect, pixels, texturedata->pitch);
} else {
IDirect3DTexture9_UnlockRect(texturedata->texture.staging, 0);
texturedata->texture.dirty = SDL_TRUE;
Dec 3, 2018
Dec 3, 2018
709
710
711
if (data->drawstate.texture == texture) {
data->drawstate.texture = NULL;
}
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
}
}
static int
D3D_SetRenderTargetInternal(SDL_Renderer * renderer, SDL_Texture * texture)
{
D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
D3D_TextureData *texturedata;
D3D_TextureRep *texturerep;
HRESULT result;
IDirect3DDevice9 *device = data->device;
/* Release the previous render target if it wasn't the default one */
if (data->currentRenderTarget != NULL) {
IDirect3DSurface9_Release(data->currentRenderTarget);
data->currentRenderTarget = NULL;
}
if (texture == NULL) {
IDirect3DDevice9_SetRenderTarget(data->device, 0, data->defaultRenderTarget);
return 0;
}
texturedata = (D3D_TextureData *)texture->driverdata;
if (!texturedata) {
SDL_SetError("Texture is not currently available");
return -1;
}
/* Make sure the render target is updated if it was locked and written to */
texturerep = &texturedata->texture;
if (texturerep->dirty && texturerep->staging) {
if (!texturerep->texture) {
result = IDirect3DDevice9_CreateTexture(device, texturerep->w, texturerep->h, 1, texturerep->usage,
PixelFormatToD3DFMT(texturerep->format), D3DPOOL_DEFAULT, &texturerep->texture, NULL);
if (FAILED(result)) {
return D3D_SetError("CreateTexture(D3DPOOL_DEFAULT)", result);
}
}
result = IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)texturerep->staging, (IDirect3DBaseTexture9 *)texturerep->texture);
if (FAILED(result)) {
return D3D_SetError("UpdateTexture()", result);
}
texturerep->dirty = SDL_FALSE;
}
result = IDirect3DTexture9_GetSurfaceLevel(texturedata->texture.texture, 0, &data->currentRenderTarget);
if(FAILED(result)) {
return D3D_SetError("GetSurfaceLevel()", result);
}
result = IDirect3DDevice9_SetRenderTarget(data->device, 0, data->currentRenderTarget);
if(FAILED(result)) {
return D3D_SetError("SetRenderTarget()", result);
}
return 0;
}
static int
D3D_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
{
Dec 8, 2017
Dec 8, 2017
774
775
776
if (D3D_ActivateRenderer(renderer) < 0) {
return -1;
}
777
778
779
780
return D3D_SetRenderTargetInternal(renderer, texture);
}
Oct 1, 2018
Oct 1, 2018
781
Oct 1, 2018
Oct 1, 2018
783
D3D_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
Oct 1, 2018
Oct 1, 2018
785
return 0; /* nothing to do in this backend. */
Oct 1, 2018
Oct 1, 2018
789
D3D_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
Oct 1, 2018
Oct 1, 2018
791
792
793
const DWORD color = D3DCOLOR_ARGB(cmd->data.draw.a, cmd->data.draw.r, cmd->data.draw.g, cmd->data.draw.b);
const size_t vertslen = count * sizeof (Vertex);
Vertex *verts = (Vertex *) SDL_AllocateRenderVertices(renderer, vertslen, 0, &cmd->data.draw.first);
Mar 19, 2019
Mar 19, 2019
794
int i;
Oct 1, 2018
Oct 1, 2018
796
797
798
if (!verts) {
return -1;
}
Oct 1, 2018
Oct 1, 2018
800
801
SDL_memset(verts, '\0', vertslen);
cmd->data.draw.count = count;
Oct 1, 2018
Oct 1, 2018
803
804
805
806
for (i = 0; i < count; i++, verts++, points++) {
verts->x = points->x;
verts->y = points->y;
verts->color = color;
Oct 1, 2018
Oct 1, 2018
808
809
810
811
812
return 0;
}
static int
Oct 1, 2018
Oct 1, 2018
813
D3D_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
Oct 1, 2018
Oct 1, 2018
815
816
817
const DWORD color = D3DCOLOR_ARGB(cmd->data.draw.a, cmd->data.draw.r, cmd->data.draw.g, cmd->data.draw.b);
const size_t vertslen = count * sizeof (Vertex) * 4;
Vertex *verts = (Vertex *) SDL_AllocateRenderVertices(renderer, vertslen, 0, &cmd->data.draw.first);
Mar 19, 2019
Mar 19, 2019
818
int i;
Oct 1, 2018
Oct 1, 2018
820
if (!verts) {
821
822
823
return -1;
}
Oct 1, 2018
Oct 1, 2018
824
825
SDL_memset(verts, '\0', vertslen);
cmd->data.draw.count = count;
Oct 1, 2016
Oct 1, 2016
826
Oct 1, 2018
Oct 1, 2018
827
828
829
830
831
832
for (i = 0; i < count; i++) {
const SDL_FRect *rect = &rects[i];
const float minx = rect->x;
const float maxx = rect->x + rect->w;
const float miny = rect->y;
const float maxy = rect->y + rect->h;
Oct 1, 2018
Oct 1, 2018
834
835
836
837
verts->x = minx;
verts->y = miny;
verts->color = color;
verts++;
Oct 1, 2018
Oct 1, 2018
839
840
841
842
verts->x = maxx;
verts->y = miny;
verts->color = color;
verts++;
Oct 1, 2018
Oct 1, 2018
844
845
846
847
verts->x = maxx;
verts->y = maxy;
verts->color = color;
verts++;
Oct 1, 2018
Oct 1, 2018
849
850
851
852
verts->x = minx;
verts->y = maxy;
verts->color = color;
verts++;
Oct 1, 2016
Oct 1, 2016
853
854
}
855
856
857
858
return 0;
}
static int
Oct 1, 2018
Oct 1, 2018
859
860
D3D_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect)
Oct 1, 2018
Oct 1, 2018
862
863
864
865
866
const DWORD color = D3DCOLOR_ARGB(cmd->data.draw.a, cmd->data.draw.r, cmd->data.draw.g, cmd->data.draw.b);
float minx, miny, maxx, maxy;
float minu, maxu, minv, maxv;
const size_t vertslen = sizeof (Vertex) * 4;
Vertex *verts = (Vertex *) SDL_AllocateRenderVertices(renderer, vertslen, 0, &cmd->data.draw.first);
Oct 1, 2018
Oct 1, 2018
868
if (!verts) {
869
870
871
return -1;
}
Oct 1, 2018
Oct 1, 2018
872
cmd->data.draw.count = 1;
Oct 1, 2018
Oct 1, 2018
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
minx = dstrect->x - 0.5f;
miny = dstrect->y - 0.5f;
maxx = dstrect->x + dstrect->w - 0.5f;
maxy = dstrect->y + dstrect->h - 0.5f;
minu = (float) srcrect->x / texture->w;
maxu = (float) (srcrect->x + srcrect->w) / texture->w;
minv = (float) srcrect->y / texture->h;
maxv = (float) (srcrect->y + srcrect->h) / texture->h;
verts->x = minx;
verts->y = miny;
verts->z = 0.0f;
verts->color = color;
verts->u = minu;
verts->v = minv;
verts++;
verts->x = maxx;
verts->y = miny;
verts->z = 0.0f;
verts->color = color;
verts->u = maxu;
verts->v = minv;
verts++;
verts->x = maxx;
verts->y = maxy;
verts->z = 0.0f;
verts->color = color;
verts->u = maxu;
verts->v = maxv;
verts++;
verts->x = minx;
verts->y = maxy;
verts->z = 0.0f;
verts->color = color;
verts->u = minu;
verts->v = maxv;
verts++;
915
916
917
918
919
return 0;
}
static int
Oct 1, 2018
Oct 1, 2018
920
921
922
D3D_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
const SDL_Rect * srcquad, const SDL_FRect * dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
Oct 1, 2018
Oct 1, 2018
924
925
926
927
928
const DWORD color = D3DCOLOR_ARGB(cmd->data.draw.a, cmd->data.draw.r, cmd->data.draw.g, cmd->data.draw.b);
float minx, miny, maxx, maxy;
float minu, maxu, minv, maxv;
const size_t vertslen = sizeof (Vertex) * 5;
Vertex *verts = (Vertex *) SDL_AllocateRenderVertices(renderer, vertslen, 0, &cmd->data.draw.first);
Oct 1, 2018
Oct 1, 2018
930
if (!verts) {
931
932
933
return -1;
}
Oct 1, 2018
Oct 1, 2018
934
cmd->data.draw.count = 1;
Oct 1, 2018
Oct 1, 2018
936
937
938
939
minx = -center->x;
maxx = dstrect->w - center->x;
miny = -center->y;
maxy = dstrect->h - center->y;
Oct 1, 2018
Oct 1, 2018
941
942
943
944
945
946
if (flip & SDL_FLIP_HORIZONTAL) {
minu = (float) (srcquad->x + srcquad->w) / texture->w;
maxu = (float) srcquad->x / texture->w;
} else {
minu = (float) srcquad->x / texture->w;
maxu = (float) (srcquad->x + srcquad->w) / texture->w;
Oct 1, 2018
Oct 1, 2018
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
if (flip & SDL_FLIP_VERTICAL) {
minv = (float) (srcquad->y + srcquad->h) / texture->h;
maxv = (float) srcquad->y / texture->h;
} else {
minv = (float) srcquad->y / texture->h;
maxv = (float) (srcquad->y + srcquad->h) / texture->h;
}
verts->x = minx;
verts->y = miny;
verts->z = 0.0f;
verts->color = color;
verts->u = minu;
verts->v = minv;
verts++;
verts->x = maxx;
verts->y = miny;
verts->z = 0.0f;
verts->color = color;
verts->u = maxu;
verts->v = minv;
verts++;
verts->x = maxx;
verts->y = maxy;
verts->z = 0.0f;
verts->color = color;
verts->u = maxu;
verts->v = maxv;
verts++;
verts->x = minx;
verts->y = maxy;
verts->z = 0.0f;
verts->color = color;
verts->u = minu;
verts->v = maxv;
verts++;
verts->x = dstrect->x + center->x - 0.5f; /* X translation */
verts->y = dstrect->y + center->y - 0.5f; /* Y translation */
verts->z = (float)(M_PI * (float) angle / 180.0f); /* rotation */
verts->color = 0;
verts->u = 0.0f;
verts->v = 0.0f;
verts++;
996
997
998
999
1000
return 0;
}
static int