Skip to content

Latest commit

 

History

History
1823 lines (1577 loc) · 60.8 KB

SDL_render_d3d.c

File metadata and controls

1823 lines (1577 loc) · 60.8 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 3, 2018
Jan 3, 2018
3
Copyright (C) 1997-2018 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
typedef struct
{
void* d3dDLL;
IDirect3D9 *d3d;
IDirect3DDevice9 *device;
UINT adapter;
D3DPRESENT_PARAMETERS pparams;
SDL_bool updateSize;
SDL_bool beginScene;
SDL_bool enableSeparateAlphaBlend;
Oct 1, 2018
Oct 1, 2018
73
SDL_bool supportsStreamOffset;
74
75
76
77
D3DTEXTUREFILTERTYPE scaleMode[8];
IDirect3DSurface9 *defaultRenderTarget;
IDirect3DSurface9 *currentRenderTarget;
void* d3dxDLL;
Nov 13, 2017
Nov 13, 2017
78
LPDIRECT3DPIXELSHADER9 shaders[NUM_SHADERS];
Oct 1, 2018
Oct 1, 2018
79
80
LPDIRECT3DVERTEXBUFFER9 vertexBuffers[8];
size_t vertexBufferSize[8];
Oct 1, 2018
Oct 1, 2018
81
82
83
int currentVertexBuffer;
SDL_bool reportedVboProblem;
D3D_DrawStateCache drawstate;
84
85
86
87
88
89
90
91
} D3D_RenderData;
typedef struct
{
SDL_bool dirty;
int w, h;
DWORD usage;
Uint32 format;
Nov 13, 2017
Nov 13, 2017
92
D3DFORMAT d3dfmt;
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
208
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
209
210
case SDL_PIXELFORMAT_NV12:
case SDL_PIXELFORMAT_NV21:
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
238
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
239
IDirect3DDevice9_SetVertexShader(device, NULL);
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
271
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
272
SDL_zero(matrix);
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
287
static int D3D_Reset(SDL_Renderer * renderer);
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
345
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
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
397
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
399
D3D_CreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, DWORD usage, Uint32 format, D3DFORMAT d3dfmt, int w, int h)
400
401
402
403
404
405
406
407
{
HRESULT result;
texture->dirty = SDL_FALSE;
texture->w = w;
texture->h = h;
texture->usage = usage;
texture->format = format;
Nov 13, 2017
Nov 13, 2017
408
texture->d3dfmt = d3dfmt;
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
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
427
texture->d3dfmt, D3DPOOL_SYSTEMMEM, &texture->staging, NULL);
428
429
430
431
432
433
434
435
if (FAILED(result)) {
return D3D_SetError("CreateTexture(D3DPOOL_SYSTEMMEM)", result);
}
}
return 0;
}
static int
Nov 13, 2017
Nov 13, 2017
436
D3D_RecreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture)
437
438
439
440
441
442
443
444
445
446
447
448
449
{
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
450
D3D_UpdateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, int x, int y, int w, int h, const void *pixels, int pitch)
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
{
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
474
475
dst = (Uint8 *)locked.pBits;
length = w * SDL_BYTESPERPIXEL(texture->format);
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
523
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
524
texturedata->scaleMode = (texture->scaleMode == SDL_ScaleModeNearest) ? D3DTEXF_POINT : D3DTEXF_LINEAR;
525
526
527
528
529
530
531
532
533
texture->driverdata = texturedata;
if (texture->access == SDL_TEXTUREACCESS_TARGET) {
usage = D3DUSAGE_RENDERTARGET;
} else {
usage = 0;
}
Nov 13, 2017
Nov 13, 2017
534
if (D3D_CreateTextureRep(data->device, &texturedata->texture, usage, texture->format, PixelFormatToD3DFMT(texture->format), texture->w, texture->h) < 0) {
535
536
537
538
539
540
541
return -1;
}
if (texture->format == SDL_PIXELFORMAT_YV12 ||
texture->format == SDL_PIXELFORMAT_IYUV) {
texturedata->yuv = SDL_TRUE;
Nov 13, 2017
Nov 13, 2017
542
if (D3D_CreateTextureRep(data->device, &texturedata->utexture, usage, texture->format, PixelFormatToD3DFMT(texture->format), (texture->w + 1) / 2, (texture->h + 1) / 2) < 0) {
543
544
545
return -1;
}
Nov 13, 2017
Nov 13, 2017
546
if (D3D_CreateTextureRep(data->device, &texturedata->vtexture, usage, texture->format, PixelFormatToD3DFMT(texture->format), (texture->w + 1) / 2, (texture->h + 1) / 2) < 0) {
547
548
549
550
551
552
553
554
555
556
557
558
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
559
560
561
562
if (!texturedata) {
return 0;
}
Nov 13, 2017
Nov 13, 2017
563
if (D3D_RecreateTextureRep(data->device, &texturedata->texture) < 0) {
564
565
566
567
return -1;
}
if (texturedata->yuv) {
Nov 13, 2017
Nov 13, 2017
568
if (D3D_RecreateTextureRep(data->device, &texturedata->utexture) < 0) {
569
570
571
return -1;
}
Nov 13, 2017
Nov 13, 2017
572
if (D3D_RecreateTextureRep(data->device, &texturedata->vtexture) < 0) {
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
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
591
if (D3D_UpdateTextureRep(data->device, &texturedata->texture, rect->x, rect->y, rect->w, rect->h, pixels, pitch) < 0) {
592
593
594
595
596
597
598
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
599
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) {
600
601
602
603
return -1;
}
/* Skip to the correct offset into the next texture */
Nov 13, 2017
Nov 13, 2017
604
605
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) {
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
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
627
if (D3D_UpdateTextureRep(data->device, &texturedata->texture, rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch) < 0) {
Nov 13, 2017
Nov 13, 2017
630
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
633
if (D3D_UpdateTextureRep(data->device, &texturedata->vtexture, rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, Vplane, Vpitch) < 0) {
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
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
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)
{
/*D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;*/
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;
}
}
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
772
773
774
if (D3D_ActivateRenderer(renderer) < 0) {
return -1;
}
775
776
777
778
return D3D_SetRenderTargetInternal(renderer, texture);
}
Oct 1, 2018
Oct 1, 2018
779
Oct 1, 2018
Oct 1, 2018
781
D3D_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
Oct 1, 2018
Oct 1, 2018
783
return 0; /* nothing to do in this backend. */
Oct 1, 2018
Oct 1, 2018
787
D3D_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
Oct 1, 2018
Oct 1, 2018
789
790
791
792
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);
size_t i;
Oct 1, 2018
Oct 1, 2018
794
795
796
if (!verts) {
return -1;
}
Oct 1, 2018
Oct 1, 2018
798
799
SDL_memset(verts, '\0', vertslen);
cmd->data.draw.count = count;
Oct 1, 2018
Oct 1, 2018
801
802
803
804
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
806
807
808
809
810
return 0;
}
static int
Oct 1, 2018
Oct 1, 2018
811
D3D_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
Oct 1, 2018
Oct 1, 2018
813
814
815
816
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);
size_t i;
Oct 1, 2018
Oct 1, 2018
818
if (!verts) {
819
820
821
return -1;
}
Oct 1, 2018
Oct 1, 2018
822
823
SDL_memset(verts, '\0', vertslen);
cmd->data.draw.count = count;
Oct 1, 2016
Oct 1, 2016
824
Oct 1, 2018
Oct 1, 2018
825
826
827
828
829
830
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
832
833
834
835
verts->x = minx;
verts->y = miny;
verts->color = color;
verts++;
Oct 1, 2018
Oct 1, 2018
837
838
839
840
verts->x = maxx;
verts->y = miny;
verts->color = color;
verts++;
Oct 1, 2018
Oct 1, 2018
842
843
844
845
verts->x = maxx;
verts->y = maxy;
verts->color = color;
verts++;
Oct 1, 2018
Oct 1, 2018
847
848
849
850
verts->x = minx;
verts->y = maxy;
verts->color = color;
verts++;
Oct 1, 2016
Oct 1, 2016
851
852
}
853
854
855
856
return 0;
}
static int
Oct 1, 2018
Oct 1, 2018
857
858
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
860
861
862
863
864
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
866
if (!verts) {
867
868
869
return -1;
}
Oct 1, 2018
Oct 1, 2018
870
cmd->data.draw.count = 1;
Oct 1, 2018
Oct 1, 2018
872
873
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
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++;
913
914
915
916
917
return 0;
}
static int
Oct 1, 2018
Oct 1, 2018
918
919
920
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
922
923
924
925
926
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
928
if (!verts) {
929
930
931
return -1;
}
Oct 1, 2018
Oct 1, 2018
932
cmd->data.draw.count = 1;
Oct 1, 2018
Oct 1, 2018
934
935
936
937
minx = -center->x;
maxx = dstrect->w - center->x;
miny = -center->y;
maxy = dstrect->h - center->y;
Oct 1, 2018
Oct 1, 2018
939
940
941
942
943
944
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
947
948
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
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++;
994
995
996
997
998
return 0;
}
static int
Oct 1, 2018
Oct 1, 2018
999
BindTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, DWORD sampler)