-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
d3d8.c
2189 lines (1852 loc) · 59.6 KB
/
d3d8.c
1
2
3
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
208
209
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
238
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
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
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
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
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
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
994
995
996
997
998
999
1000
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
* Copyright (C) 2012-2014 - OV2
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
/* Direct3D 8 driver.
*
* Minimum version : Direct3D 8.0 (2000)
* Minimum OS : Windows 95 (8.0a), Windows 98 and XP after 8.0a
* Recommended OS : Windows 98/ME and/or Windows XP
*/
#define CINTERFACE
#ifdef _XBOX
#include <xtl.h>
#include <xgraphics.h>
#endif
#include <formats/image.h>
#include <compat/strl.h>
#include <compat/posix_string.h>
#include <file/file_path.h>
#include <string/stdstring.h>
#include <retro_math.h>
#include <d3d8.h>
#include <defines/d3d_defines.h>
#include "../common/d3d8_defines.h"
#include "../common/d3d_common.h"
#include "../../configuration.h"
#include "../../retroarch.h"
#include "../../dynamic.h"
#include "../../frontend/frontend_driver.h"
#ifdef HAVE_THREADS
#include "../video_thread_wrapper.h"
#endif
#include "../common/win32_common.h"
#ifdef _XBOX
#define D3D8_PRESENTATIONINTERVAL D3DRS_PRESENTATIONINTERVAL
#endif
#ifdef HAVE_MENU
#include "../../menu/menu_driver.h"
#endif
#include "../font_driver.h"
#include "../../core.h"
#include "../../verbosity.h"
#ifdef __WINRT__
#error "UWP does not support D3D8"
#endif
#ifdef _XBOX
#define D3D8_RGB565_FORMAT D3DFMT_LIN_R5G6B5
#define D3D8_XRGB8888_FORMAT D3DFMT_LIN_X8R8G8B8
#define D3D8_ARGB8888_FORMAT D3DFMT_LIN_A8R8G8B8
#else
#define D3D8_RGB565_FORMAT D3DFMT_R5G6B5
#define D3D8_XRGB8888_FORMAT D3DFMT_X8R8G8B8
#define D3D8_ARGB8888_FORMAT D3DFMT_A8R8G8B8
#endif
typedef struct d3d8_renderchain
{
unsigned pixel_size;
LPDIRECT3DDEVICE8 dev;
const video_info_t *video_info;
LPDIRECT3DTEXTURE8 tex;
LPDIRECT3DVERTEXBUFFER8 vertex_buf;
unsigned last_width;
unsigned last_height;
void *vertex_decl;
unsigned tex_w;
unsigned tex_h;
uint64_t frame_count;
} d3d8_renderchain_t;
struct d3d8_texture_info
{
void *userdata;
void *data;
enum texture_filter_type type;
};
static const float d3d8_vertexes[8] = {
0, 0,
1, 0,
0, 1,
1, 1
};
static const float d3d8_tex_coords[8] = {
0, 1,
1, 1,
0, 0,
1, 0
};
static LPDIRECT3D8 g_pD3D8;
void *dinput;
/*
* D3D8 COMMON
*/
/* For Xbox we will just link statically
* to Direct3D libraries instead. */
#if !defined(_XBOX) && defined(HAVE_DYLIB)
#define HAVE_DYNAMIC_D3D
#endif
#ifdef HAVE_DYNAMIC_D3D
#include <dynamic/dylib.h>
#endif
/* TODO/FIXME - static globals */
#ifdef HAVE_DYNAMIC_D3D
static dylib_t g_d3d8_dll;
static bool dylib_initialized = false;
#endif
typedef IDirect3D8 *(__stdcall *D3DCreate_t)(UINT);
static D3DCreate_t D3DCreate;
#ifdef HAVE_DYNAMIC_D3D
static void d3d8_deinitialize_symbols(void)
{
if (g_d3d8_dll)
dylib_close(g_d3d8_dll);
g_d3d8_dll = NULL;
dylib_initialized = false;
}
#endif
static bool d3d8_initialize_symbols(enum gfx_ctx_api api)
{
#ifdef HAVE_DYNAMIC_D3D
if (dylib_initialized)
return true;
#if defined(DEBUG) || defined(_DEBUG)
if (!(g_d3d8_dll = dylib_load("d3d8d.dll")))
#endif
g_d3d8_dll = dylib_load("d3d8.dll");
if (!g_d3d8_dll)
return false;
D3DCreate = (D3DCreate_t)dylib_proc(g_d3d8_dll, "Direct3DCreate8");
#else
D3DCreate = Direct3DCreate8;
#endif
if (!D3DCreate)
goto error;
#ifdef HAVE_DYNAMIC_D3D
dylib_initialized = true;
#endif
return true;
error:
#ifdef HAVE_DYNAMIC_D3D
d3d8_deinitialize_symbols();
#endif
return false;
}
static INLINE void *
d3d8_vertex_buffer_lock(LPDIRECT3DVERTEXBUFFER8 vertbuf)
{
void *buf = NULL;
IDirect3DVertexBuffer8_Lock(vertbuf, 0, 0, (BYTE**)&buf, 0);
return buf;
}
bool d3d8_reset(void *data, void *d3dpp)
{
const char *err = NULL;
LPDIRECT3DDEVICE8 dev = (LPDIRECT3DDEVICE8)data;
if (dev && IDirect3DDevice8_Reset(dev, (D3DPRESENT_PARAMETERS*)d3dpp) ==
D3D_OK)
return true;
#ifndef _XBOX
RARCH_WARN("[D3D]: Attempting to recover from dead state...\n");
/* Try to recreate the device completely. */
switch (IDirect3DDevice8_TestCooperativeLevel(dev))
{
case D3DERR_DEVICELOST:
err = "DEVICELOST";
break;
case D3DERR_DEVICENOTRESET:
err = "DEVICENOTRESET";
break;
case D3DERR_DRIVERINTERNALERROR:
err = "DRIVERINTERNALERROR";
break;
default:
err = "Unknown";
}
RARCH_WARN("[D3D]: recovering from dead state: (%s).\n", err);
#endif
return false;
}
static bool d3d8_create_device_internal(
LPDIRECT3DDEVICE8 dev,
D3DPRESENT_PARAMETERS *d3dpp,
LPDIRECT3D8 d3d,
HWND focus_window,
unsigned cur_mon_id,
DWORD behavior_flags)
{
if (dev &&
SUCCEEDED(IDirect3D8_CreateDevice(d3d,
cur_mon_id,
D3DDEVTYPE_HAL,
focus_window,
behavior_flags,
d3dpp,
(IDirect3DDevice8**)dev)))
return true;
return false;
}
static bool d3d8_create_device(void *dev,
void *d3dpp,
LPDIRECT3D8 d3d,
HWND focus_window,
unsigned cur_mon_id)
{
if (!d3d8_create_device_internal(dev,
(D3DPRESENT_PARAMETERS*)d3dpp,
d3d,
focus_window,
cur_mon_id,
D3DCREATE_HARDWARE_VERTEXPROCESSING))
if (!d3d8_create_device_internal(
dev,
(D3DPRESENT_PARAMETERS*)d3dpp, d3d, focus_window,
cur_mon_id,
D3DCREATE_SOFTWARE_VERTEXPROCESSING))
return false;
return true;
}
static void *d3d8_texture_new(LPDIRECT3DDEVICE8 dev,
unsigned width, unsigned height,
unsigned miplevels, unsigned usage, INT32 format,
INT32 pool, unsigned filter, unsigned mipfilter,
INT32 color_key, void *src_info_data,
PALETTEENTRY *palette, bool want_mipmap)
{
void *buf = NULL;
if (SUCCEEDED(IDirect3DDevice8_CreateTexture(dev,
width, height, miplevels, usage,
(D3DFORMAT)format, (D3DPOOL)pool,
(struct IDirect3DTexture8**)&buf)))
return buf;
return NULL;
}
static void d3d8_set_mvp(void *data, const void *mat_data)
{
struct d3d_matrix matrix;
LPDIRECT3DDEVICE8 d3dr = (LPDIRECT3DDEVICE8)data;
d3d_matrix_identity(&matrix);
IDirect3DDevice8_SetTransform(d3dr,
D3DTS_PROJECTION, (D3DMATRIX*)&matrix);
IDirect3DDevice8_SetTransform(d3dr,
D3DTS_VIEW, (D3DMATRIX*)&matrix);
d3d_matrix_transpose(&matrix, mat_data);
IDirect3DDevice8_SetTransform(d3dr, D3DTS_WORLD, (D3DMATRIX*)&matrix);
}
static void d3d8_set_vertices(
d3d8_video_t *d3d,
d3d8_renderchain_t *chain,
unsigned pass,
unsigned vert_width, unsigned vert_height, uint64_t frame_count)
{
unsigned width, height;
video_driver_get_size(&width, &height);
if (chain->last_width != vert_width || chain->last_height != vert_height)
{
Vertex vert[4];
void *verts = NULL;
float tex_w = vert_width;
float tex_h = vert_height;
chain->last_width = vert_width;
chain->last_height = vert_height;
if (chain->vertex_buf)
{
LPDIRECT3DVERTEXBUFFER8 vbo;
vert[0].x = 0.0f;
vert[0].y = 1.0f;
vert[0].z = 1.0f;
vert[1].x = 1.0f;
vert[1].y = 1.0f;
vert[1].z = 1.0f;
vert[2].x = 0.0f;
vert[2].y = 0.0f;
vert[2].z = 1.0f;
vert[3].x = 1.0f;
vert[3].y = 0.0f;
vert[3].z = 1.0f;
vert[0].u = 0.0f;
vert[0].v = 0.0f;
vert[1].v = 0.0f;
vert[2].u = 0.0f;
vert[1].u = tex_w;
vert[2].v = tex_h;
vert[3].u = tex_w;
vert[3].v = tex_h;
#ifndef _XBOX
vert[1].u /= chain->tex_w;
vert[2].v /= chain->tex_h;
vert[3].u /= chain->tex_w;
vert[3].v /= chain->tex_h;
#endif
vert[0].color = 0xFFFFFFFF;
vert[1].color = 0xFFFFFFFF;
vert[2].color = 0xFFFFFFFF;
vert[3].color = 0xFFFFFFFF;
vbo = (LPDIRECT3DVERTEXBUFFER8)chain->vertex_buf;
verts = d3d8_vertex_buffer_lock(vbo);
memcpy(verts, vert, sizeof(vert));
IDirect3DVertexBuffer8_Unlock(vbo);
}
}
}
static void d3d8_blit_to_texture(
d3d8_renderchain_t *chain,
const void *frame,
unsigned width, unsigned height, unsigned pitch)
{
D3DLOCKED_RECT d3dlr;
D3DLOCKED_RECT *lr = &d3dlr;
LPDIRECT3DDEVICE8 d3dr = (LPDIRECT3DDEVICE8)chain->dev;
LPDIRECT3DTEXTURE8 tex = (LPDIRECT3DTEXTURE8)chain->tex;
#ifdef _XBOX
global_t *global = global_get_ptr();
D3DDevice_SetFlickerFilter(global->console.screen.flicker_filter_index);
D3DDevice_SetSoftDisplayFilter(global->console.softfilter_enable);
#endif
if (chain->last_width != width || chain->last_height != height)
{
if (IDirect3DTexture8_LockRect(tex, 0, lr,
NULL, D3DLOCK_NOSYSLOCK) == D3D_OK)
{
memset(lr->pBits, 0, chain->tex_h * lr->Pitch);
IDirect3DTexture8_UnlockRect((LPDIRECT3DTEXTURE8)tex, 0);
}
}
/* Set the texture to NULL so D3D doesn't complain about it being in use... */
IDirect3DDevice8_SetTexture(d3dr, 0, NULL);
if (IDirect3DTexture8_LockRect(tex, 0, &d3dlr, NULL, 0) == D3D_OK)
{
unsigned y;
unsigned pixel_size = chain->pixel_size;
for (y = 0; y < height; y++)
{
const uint8_t *in = (const uint8_t*)frame + y * pitch;
uint8_t *out = (uint8_t*)lr->pBits + y * lr->Pitch;
memcpy(out, in, width * pixel_size);
}
IDirect3DTexture8_UnlockRect(tex, 0);
}
}
static void d3d8_render(
d3d8_video_t *d3d,
LPDIRECT3DDEVICE8 d3dr,
d3d8_renderchain_t *chain,
const void *frame,
unsigned frame_width, unsigned frame_height,
unsigned pitch, unsigned rotation)
{
settings_t *settings = config_get_ptr();
bool video_smooth = settings->bools.video_smooth;
d3d8_blit_to_texture(chain, frame, frame_width, frame_height, pitch);
d3d8_set_vertices(d3d, chain, 1, frame_width, frame_height, chain->frame_count);
IDirect3DDevice8_SetTexture(d3dr, 0,
(IDirect3DBaseTexture8*)chain->tex);
IDirect3DDevice8_SetTextureStageState(d3dr, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_MAGFILTER,
video_smooth ?
D3DTEXF_LINEAR : D3DTEXF_POINT);
IDirect3DDevice8_SetTextureStageState(d3dr, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_MINFILTER,
video_smooth ?
D3DTEXF_LINEAR : D3DTEXF_POINT);
IDirect3DDevice8_SetViewport(chain->dev, (D3DVIEWPORT8*)&d3d->final_viewport);
IDirect3DDevice8_SetVertexShader(d3dr,
D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_DIFFUSE);
IDirect3DDevice8_SetStreamSource(d3dr,
0, chain->vertex_buf, sizeof(Vertex));
d3d8_set_mvp(d3d->dev, &d3d->mvp_rotate);
IDirect3DDevice8_BeginScene(d3dr);
IDirect3DDevice8_DrawPrimitive(d3dr, D3DPT_TRIANGLESTRIP, 0, 2);
IDirect3DDevice8_EndScene(d3dr);
chain->frame_count++;
}
static INLINE void *d3d8_vertex_buffer_new(
LPDIRECT3DDEVICE8 dev,
unsigned length, unsigned usage,
unsigned fvf, D3DPOOL pool)
{
void *buf = NULL;
if (FAILED(IDirect3DDevice8_CreateVertexBuffer(
dev, length, usage, fvf,
pool,
(struct IDirect3DVertexBuffer8**)&buf)))
return NULL;
return buf;
}
static bool d3d8_setup_init(void *data,
const video_info_t *video_info,
void *dev_data,
const struct LinkInfo *link_info,
bool rgb32
)
{
unsigned width, height;
d3d8_video_t *d3d = (d3d8_video_t*)data;
settings_t *settings = config_get_ptr();
LPDIRECT3DDEVICE8 d3dr = (LPDIRECT3DDEVICE8)d3d->dev;
d3d8_renderchain_t *chain = (d3d8_renderchain_t*)d3d->renderchain_data;
unsigned fmt = (rgb32) ? RETRO_PIXEL_FORMAT_XRGB8888 : RETRO_PIXEL_FORMAT_RGB565;
video_viewport_t *custom_vp = &settings->video_viewport_custom;
video_driver_get_size(&width, &height);
chain->dev = dev_data;
chain->pixel_size = (fmt == RETRO_PIXEL_FORMAT_RGB565)
? 2
: 4;
chain->tex_w = link_info->tex_w;
chain->tex_h = link_info->tex_h;
chain->vertex_buf = (LPDIRECT3DVERTEXBUFFER8)d3d8_vertex_buffer_new(d3dr, 4 * sizeof(Vertex),
D3DUSAGE_WRITEONLY,
D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_DIFFUSE,
D3DPOOL_MANAGED);
if (!chain->vertex_buf)
return false;
chain->tex = (LPDIRECT3DTEXTURE8)d3d8_texture_new(d3dr,
chain->tex_w, chain->tex_h, 1, 0,
video_info->rgb32
?
D3D8_XRGB8888_FORMAT : D3D8_RGB565_FORMAT,
D3DPOOL_MANAGED, 0, 0, 0, NULL, NULL,
false);
if (!chain->tex)
return false;
IDirect3DDevice8_SetTextureStageState(d3dr, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_ADDRESSU, D3DTADDRESS_BORDER);
IDirect3DDevice8_SetTextureStageState(d3dr, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_ADDRESSV, D3DTADDRESS_BORDER);
IDirect3DDevice8_SetRenderState(d3dr, D3DRS_LIGHTING, 0);
IDirect3DDevice8_SetRenderState(d3dr, D3DRS_CULLMODE, D3DCULL_NONE);
IDirect3DDevice8_SetRenderState(d3dr, D3DRS_ZENABLE, FALSE);
/* FIXME */
if (custom_vp->width == 0)
custom_vp->width = width;
if (custom_vp->height == 0)
custom_vp->height = height;
return true;
}
static void *d3d8_renderchain_new(void)
{
d3d8_renderchain_t *renderchain = (d3d8_renderchain_t*)calloc(1, sizeof(*renderchain));
if (!renderchain)
return NULL;
return renderchain;
}
/*
* DISPLAY DRIVER
*/
static const float *gfx_display_d3d8_get_default_vertices(void)
{
return &d3d8_vertexes[0];
}
static const float *gfx_display_d3d8_get_default_tex_coords(void)
{
return &d3d8_tex_coords[0];
}
static void *gfx_display_d3d8_get_default_mvp(void *data)
{
static float id[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
return &id;
}
static INT32 gfx_display_prim_to_d3d8_enum(
enum gfx_display_prim_type prim_type)
{
switch (prim_type)
{
case GFX_DISPLAY_PRIM_TRIANGLES:
case GFX_DISPLAY_PRIM_TRIANGLESTRIP:
return D3DPT_COMM_TRIANGLESTRIP;
case GFX_DISPLAY_PRIM_NONE:
default:
break;
}
/* TODO/FIXME - hack */
return 0;
}
static void gfx_display_d3d8_blend_begin(void *data)
{
d3d8_video_t *d3d = (d3d8_video_t*)data;
if (!d3d)
return;
IDirect3DDevice8_SetRenderState(d3d->dev, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
IDirect3DDevice8_SetRenderState(d3d->dev, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
IDirect3DDevice8_SetRenderState(d3d->dev, D3DRS_ALPHABLENDENABLE, true);
}
static void gfx_display_d3d8_blend_end(void *data)
{
d3d8_video_t *d3d = (d3d8_video_t*)data;
if (!d3d)
return;
IDirect3DDevice8_SetRenderState(d3d->dev, D3DRS_ALPHABLENDENABLE, false);
}
static void gfx_display_d3d8_draw(gfx_display_ctx_draw_t *draw,
void *data,
unsigned video_width,
unsigned video_height)
{
static float default_mvp[] ={ 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
unsigned i;
math_matrix_4x4 mop, m1, m2;
LPDIRECT3DVERTEXBUFFER8 vbo;
LPDIRECT3DDEVICE8 dev;
D3DPRIMITIVETYPE type;
unsigned start = 0;
unsigned count = 0;
d3d8_video_t *d3d = (d3d8_video_t*)data;
Vertex * pv = NULL;
const float *vertex = NULL;
const float *tex_coord = NULL;
const float *color = NULL;
if (!d3d || !draw || draw->pipeline_id)
return;
if ((d3d->menu_display.offset + draw->coords->vertices )
> (unsigned)d3d->menu_display.size)
return;
vbo = (LPDIRECT3DVERTEXBUFFER8)d3d->menu_display.buffer;
dev = d3d->dev;
pv = (Vertex*)d3d8_vertex_buffer_lock(vbo);
if (!pv)
return;
pv += d3d->menu_display.offset;
vertex = draw->coords->vertex;
tex_coord = draw->coords->tex_coord;
color = draw->coords->color;
if (!vertex)
vertex = &d3d8_vertexes[0];
if (!tex_coord)
tex_coord = &d3d8_tex_coords[0];
for (i = 0; i < draw->coords->vertices; i++)
{
int colors[4];
colors[0] = *color++ * 0xFF;
colors[1] = *color++ * 0xFF;
colors[2] = *color++ * 0xFF;
colors[3] = *color++ * 0xFF;
pv[i].x = *vertex++;
pv[i].y = *vertex++;
pv[i].z = 0.5f;
pv[i].u = *tex_coord++;
pv[i].v = *tex_coord++;
if ((void*)draw->texture)
{
D3DSURFACE_DESC desc;
LPDIRECT3DTEXTURE8 tex = (LPDIRECT3DTEXTURE8)draw->texture;
if (SUCCEEDED(IDirect3DTexture8_GetLevelDesc(tex,
0, (D3DSURFACE_DESC*)&desc)))
{
pv[i].u *= desc.Width;
pv[i].v *= desc.Height;
}
}
pv[i].color =
D3DCOLOR_ARGB(
colors[3], /* A */
colors[0], /* R */
colors[1], /* G */
colors[2] /* B */
);
}
IDirect3DVertexBuffer8_Unlock(vbo);
if (!draw->matrix_data)
draw->matrix_data = &default_mvp;
/* ugh */
matrix_4x4_scale(m1, 2.0, 2.0, 0);
matrix_4x4_translate(mop, -1.0, -1.0, 0);
matrix_4x4_multiply(m2, mop, m1);
matrix_4x4_multiply(m1,
*((math_matrix_4x4*)draw->matrix_data), m2);
matrix_4x4_scale(mop,
(draw->width / 2.0) / video_width,
(draw->height / 2.0) / video_height, 0);
matrix_4x4_multiply(m2, mop, m1);
matrix_4x4_translate(mop,
(draw->x + (draw->width / 2.0)) / video_width,
(draw->y + (draw->height / 2.0)) / video_height,
0);
matrix_4x4_multiply(m1, mop, m2);
matrix_4x4_multiply(m2, d3d->mvp_transposed, m1);
d3d_matrix_transpose(&m1, &m2);
d3d8_set_mvp(dev, &m1);
if (draw->texture)
{
IDirect3DDevice8_SetTexture(dev, 0,
(IDirect3DBaseTexture8*)draw->texture);
IDirect3DDevice8_SetTextureStageState(dev, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_ADDRESSU, D3DTADDRESS_COMM_CLAMP);
IDirect3DDevice8_SetTextureStageState(dev, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_ADDRESSV, D3DTADDRESS_COMM_CLAMP);
IDirect3DDevice8_SetTextureStageState(dev, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_MINFILTER, D3DTEXF_COMM_LINEAR);
IDirect3DDevice8_SetTextureStageState(dev, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_MAGFILTER, D3DTEXF_COMM_LINEAR);
}
type = gfx_display_prim_to_d3d8_enum(draw->prim_type);
start = d3d->menu_display.offset;
count = draw->coords->vertices -
((draw->prim_type == GFX_DISPLAY_PRIM_TRIANGLESTRIP)
? 2 : 0);
IDirect3DDevice8_BeginScene(dev);
IDirect3DDevice8_DrawPrimitive(dev, type, start, count);
IDirect3DDevice8_EndScene(dev);
d3d->menu_display.offset += draw->coords->vertices;
}
gfx_display_ctx_driver_t gfx_display_ctx_d3d8 = {
gfx_display_d3d8_draw,
NULL, /* draw_pipeline */
gfx_display_d3d8_blend_begin,
gfx_display_d3d8_blend_end,
gfx_display_d3d8_get_default_mvp,
gfx_display_d3d8_get_default_vertices,
gfx_display_d3d8_get_default_tex_coords,
FONT_DRIVER_RENDER_D3D8_API,
GFX_VIDEO_DRIVER_DIRECT3D8,
"d3d8",
false,
NULL,
NULL
};
/*
* VIDEO DRIVER
*/
static void d3d8_viewport_info(void *data, struct video_viewport *vp)
{
unsigned width, height;
d3d8_video_t *d3d = (d3d8_video_t*)data;
if (!d3d || !vp)
return;
video_driver_get_size(&width, &height);
vp->x = d3d->final_viewport.X;
vp->y = d3d->final_viewport.Y;
vp->width = d3d->final_viewport.Width;
vp->height = d3d->final_viewport.Height;
vp->full_width = width;
vp->full_height = height;
}
static void d3d8_overlay_render(d3d8_video_t *d3d,
unsigned width, unsigned height,
overlay_t *overlay, bool force_linear)
{
D3DVIEWPORT8 vp_full;
struct video_viewport vp;
unsigned i;
Vertex vert[4];
enum D3DTEXTUREFILTERTYPE filter_type = D3DTEXF_LINEAR;
if (!d3d || !overlay || !overlay->tex)
return;
if (!overlay->vert_buf)
{
overlay->vert_buf = d3d8_vertex_buffer_new(
d3d->dev, sizeof(vert), D3DUSAGE_WRITEONLY,
D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_DIFFUSE,
D3DPOOL_MANAGED);
if (!overlay->vert_buf)
return;
}
for (i = 0; i < 4; i++)
{
vert[i].z = 0.5f;
vert[i].color = (((uint32_t)(overlay->alpha_mod * 0xFF)) << 24) | 0xFFFFFF;
}
d3d8_viewport_info(d3d, &vp);
vert[0].x = overlay->vert_coords[0];
vert[1].x = overlay->vert_coords[0] + overlay->vert_coords[2];
vert[2].x = overlay->vert_coords[0];
vert[3].x = overlay->vert_coords[0] + overlay->vert_coords[2];
vert[0].y = overlay->vert_coords[1];
vert[1].y = overlay->vert_coords[1];
vert[2].y = overlay->vert_coords[1] + overlay->vert_coords[3];
vert[3].y = overlay->vert_coords[1] + overlay->vert_coords[3];
vert[0].u = overlay->tex_coords[0];
vert[1].u = overlay->tex_coords[0] + overlay->tex_coords[2];
vert[2].u = overlay->tex_coords[0];
vert[3].u = overlay->tex_coords[0] + overlay->tex_coords[2];
vert[0].v = overlay->tex_coords[1];
vert[1].v = overlay->tex_coords[1];
vert[2].v = overlay->tex_coords[1] + overlay->tex_coords[3];
vert[3].v = overlay->tex_coords[1] + overlay->tex_coords[3];
if (overlay->vert_buf)
{
LPDIRECT3DVERTEXBUFFER8 vbo = (LPDIRECT3DVERTEXBUFFER8)overlay->vert_buf;
void *verts = d3d8_vertex_buffer_lock(vbo);
memcpy(verts, vert, sizeof(vert));
IDirect3DVertexBuffer8_Unlock(vbo);
}
IDirect3DDevice8_SetRenderState(d3d->dev, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
IDirect3DDevice8_SetRenderState(d3d->dev, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
IDirect3DDevice8_SetRenderState(d3d->dev, D3DRS_ALPHABLENDENABLE, true);
IDirect3DDevice8_SetVertexShader(d3d->dev,
D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_DIFFUSE);
IDirect3DDevice8_SetStreamSource(d3d->dev,
0, overlay->vert_buf, sizeof(*vert));
if (overlay->fullscreen)
{
vp_full.X = 0;
vp_full.Y = 0;
vp_full.Width = width;
vp_full.Height = height;
vp_full.MinZ = 0.0f;
vp_full.MaxZ = 1.0f;
IDirect3DDevice8_SetViewport(d3d->dev, &vp_full);
}
if (!force_linear)
{
settings_t *settings = config_get_ptr();
bool menu_linear_filter = settings->bools.menu_linear_filter;
if (!menu_linear_filter)
filter_type = D3DTEXF_POINT;
}
/* Render overlay. */
IDirect3DDevice8_SetTexture(d3d->dev, 0,
(IDirect3DBaseTexture8*)overlay->tex);
IDirect3DDevice8_SetTextureStageState(d3d->dev, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_ADDRESSU, D3DTADDRESS_BORDER);
IDirect3DDevice8_SetTextureStageState(d3d->dev, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_ADDRESSV, D3DTADDRESS_BORDER);
IDirect3DDevice8_SetTextureStageState(d3d->dev, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_MAGFILTER, filter_type);
IDirect3DDevice8_SetTextureStageState(d3d->dev, 0,
(D3DTEXTURESTAGESTATETYPE)D3DTSS_MINFILTER, filter_type);
IDirect3DDevice8_BeginScene(d3d->dev);
IDirect3DDevice8_DrawPrimitive(d3d->dev, D3DPT_TRIANGLESTRIP, 0, 2);
IDirect3DDevice8_EndScene(d3d->dev);
/* Restore previous state. */
IDirect3DDevice8_SetRenderState(d3d->dev, D3DRS_ALPHABLENDENABLE, false);
IDirect3DDevice8_SetViewport(d3d->dev, (D3DVIEWPORT8*)&d3d->final_viewport);
}
static void d3d8_free_overlay(d3d8_video_t *d3d, overlay_t *overlay)
{
LPDIRECT3DTEXTURE8 tex = overlay->tex;
LPDIRECT3DVERTEXBUFFER8 vbo = (LPDIRECT3DVERTEXBUFFER8)overlay->vert_buf;
if (tex)
IDirect3DTexture8_Release(tex);
if (vbo)
IDirect3DVertexBuffer8_Release(vbo);
overlay->vert_buf = NULL;
}
static void d3d8_deinitialize(d3d8_video_t *d3d)
{
LPDIRECT3DVERTEXBUFFER8 _vbo;
d3d8_renderchain_t *chain = NULL;
if (!d3d)
return;
chain = (d3d8_renderchain_t*)d3d->renderchain_data;
font_driver_free_osd();
if (chain)
{
LPDIRECT3DTEXTURE8 tex = (LPDIRECT3DTEXTURE8)chain->tex;
LPDIRECT3DVERTEXBUFFER8 vbo = (LPDIRECT3DVERTEXBUFFER8)chain->vertex_buf;
if (tex)
IDirect3DTexture8_Release(tex);
if (vbo)
IDirect3DVertexBuffer8_Release(vbo);
chain->vertex_buf = NULL;
chain->tex = NULL;
free(chain);
}
d3d->renderchain_data = NULL;
_vbo = (LPDIRECT3DVERTEXBUFFER8)d3d->menu_display.buffer;
IDirect3DVertexBuffer8_Release(_vbo);
d3d->menu_display.buffer = NULL;
d3d->menu_display.decl = NULL;
}
#define FS_PRESENTINTERVAL(pp) ((pp)->FullScreen_PresentationInterval)
static INLINE bool d3d8_get_adapter_display_mode(
LPDIRECT3D8 d3d,
unsigned idx,
void *display_mode)
{
if (d3d &&
SUCCEEDED(IDirect3D8_GetAdapterDisplayMode(
d3d, idx, (D3DDISPLAYMODE*)display_mode)))
return true;
return false;
}
static D3DFORMAT d3d8_get_color_format_backbuffer(bool rgb32, bool windowed)
{
D3DFORMAT fmt = D3DFMT_X8R8G8B8;
#ifdef _XBOX
if (!rgb32)
fmt = D3D8_RGB565_FORMAT;
#else
if (windowed)
{
D3DDISPLAYMODE display_mode;
if (d3d8_get_adapter_display_mode(g_pD3D8, 0, &display_mode))
fmt = display_mode.Format;
}
#endif
return fmt;
}
static bool d3d8_is_windowed_enable(bool info_fullscreen)
{
#ifndef _XBOX
settings_t *settings = config_get_ptr();
if (!info_fullscreen)
return true;
if (settings)
return settings->bools.video_windowed_fullscreen;
#endif
return false;
}
#ifdef _XBOX
static void d3d8_get_video_size(d3d8_video_t *d3d,
unsigned *width, unsigned *height)
{
DWORD video_mode = XGetVideoFlags();
*width = 640;
*height = 480;
d3d->widescreen_mode = false;
/* Only valid in PAL mode, not valid for HDTV modes! */
if (XGetVideoStandard() == XC_VIDEO_STANDARD_PAL_I)
{
/* Check for 16:9 mode (PAL REGION) */
if (video_mode & XC_VIDEO_FLAGS_WIDESCREEN)
{
*width = 720;
/* 60 Hz, 720x480i */
if (video_mode & XC_VIDEO_FLAGS_PAL_60Hz)
*height = 480;
else /* 50 Hz, 720x576i */
*height = 576;
d3d->widescreen_mode = true;
}
}
else
{
/* Check for 16:9 mode (NTSC REGIONS) */
if (video_mode & XC_VIDEO_FLAGS_WIDESCREEN)
{
*width = 720;
*height = 480;
d3d->widescreen_mode = true;
}
}
if (XGetAVPack() == XC_AV_PACK_HDTV)
{
if (video_mode & XC_VIDEO_FLAGS_HDTV_480p)