-
Notifications
You must be signed in to change notification settings - Fork 4
/
xxGraphicD3D7.cpp
940 lines (843 loc) · 35.8 KB
/
xxGraphicD3D7.cpp
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
//==============================================================================
// xxGraphic : Direct3D 7.0 Source
//
// Copyright (c) 2019-2024 TAiGA
// https://github.com/metarutaiga/xxGraphic
//==============================================================================
#define DIRECTDRAW_VERSION 0x700
#define DIRECT3D_VERSION 0x700
#define D3D_OVERLOADS
#include "xxSystem.h"
#include "dxsdk/ddraw.h"
#include "dxsdk/d3d.h"
#include "internal/xxGraphicInternalD3D.h"
#include "xxGraphicD3D7.h"
typedef HRESULT (WINAPI *PFN_DIRECT_DRAW_CREATE_EX)(GUID*, LPVOID*, REFIID, IUnknown*);
#define D3DRTYPE_CONSTANTBUFFER 0
#define D3DRTYPE_INDEXBUFFER 1
#define D3DRTYPE_VERTEXBUFFER 2
static void* g_ddrawLibrary = nullptr;
static LPDIRECTDRAW7 g_ddraw = nullptr;
static LPDIRECTDRAWSURFACE7 g_primarySurface = nullptr;
static LPDIRECT3DVERTEXBUFFER7 g_vertexBuffer = nullptr;
//==============================================================================
// Instance
//==============================================================================
uint64_t xxCreateInstanceD3D7()
{
if (g_ddrawLibrary == nullptr)
g_ddrawLibrary = xxLoadLibrary("ddraw.dll");
if (g_ddrawLibrary == nullptr)
return 0;
PFN_DIRECT_DRAW_CREATE_EX DirectDrawCreateEx;
(void*&)DirectDrawCreateEx = xxGetProcAddress(g_ddrawLibrary, "DirectDrawCreateEx");
if (DirectDrawCreateEx == nullptr)
return 0;
LPDIRECTDRAW7 ddraw = nullptr;
if (ddraw == nullptr)
{
HRESULT result = DirectDrawCreateEx(nullptr, (void**)&ddraw, __uuidof(IDirectDraw7), nullptr);
if (result != S_OK)
return 0;
ddraw->SetCooperativeLevel(nullptr, DDSCL_FPUSETUP | DDSCL_NORMAL);
}
g_ddraw = ddraw;
LPDIRECT3D7 d3d = nullptr;
HRESULT result = ddraw->QueryInterface(IID_PPV_ARGS(&d3d));
if (result != S_OK)
return 0;
PatchD3DIM("d3dim700.dll");
xxRegisterFunction(D3D7);
return reinterpret_cast<uint64_t>(d3d);
}
//------------------------------------------------------------------------------
void xxDestroyInstanceD3D7(uint64_t instance)
{
LPDIRECT3D7 d3d = reinterpret_cast<LPDIRECT3D7>(instance);
SafeRelease(d3d);
SafeRelease(g_ddraw);
if (g_ddrawLibrary)
{
xxFreeLibrary(g_ddrawLibrary);
g_ddrawLibrary = nullptr;
}
xxUnregisterFunction();
}
//==============================================================================
// Device
//==============================================================================
uint64_t xxCreateDeviceD3D7(uint64_t instance)
{
LPDIRECT3D7 d3d = reinterpret_cast<LPDIRECT3D7>(instance);
if (d3d == nullptr)
return 0;
LPDIRECTDRAWSURFACE7 primarySurface = nullptr;
if (primarySurface == nullptr)
{
DDSURFACEDESC2 desc = {};
desc.dwSize = sizeof(DDSURFACEDESC2);
desc.dwFlags = DDSD_CAPS;
desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
HRESULT result = g_ddraw->CreateSurface(&desc, &primarySurface, nullptr);
if (result != S_OK)
return 0;
g_primarySurface = primarySurface;
}
LPDIRECTDRAWSURFACE7 backSurface = nullptr;
if (backSurface == nullptr)
{
DDSURFACEDESC2 desc = {};
desc.dwSize = sizeof(DDSURFACEDESC2);
desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
desc.dwWidth = 1;
desc.dwHeight = 1;
HRESULT result = g_ddraw->CreateSurface(&desc, &backSurface, nullptr);
if (result != S_OK)
return 0;
}
const IID* iid = nullptr;
LPDIRECT3DDEVICE7 d3dDevice = nullptr;
if (d3dDevice == nullptr)
{
iid = &__uuidof(IDirect3DTnLHalDevice);
d3d->CreateDevice(__uuidof(IDirect3DTnLHalDevice), backSurface, &d3dDevice);
}
if (d3dDevice == nullptr)
{
iid = &__uuidof(IDirect3DHALDevice);
d3d->CreateDevice(__uuidof(IDirect3DHALDevice), backSurface, &d3dDevice);
}
if (d3dDevice == nullptr)
{
backSurface->Release();
return 0;
}
backSurface->Release();
IUnknown* unknown = nullptr;
xxLocalBreak()
{
if ((*iid) == __uuidof(IDirect3DTnLHalDevice))
{
xxLog("xxGraphic", "%s %s (%s)", "Direct3D", "7.0 Hardware Transform and Lighting", xxGetInstanceName());
break;
}
if (d3dDevice->QueryInterface(__uuidof(IDirect3DDevice7), (void**)&unknown) == S_OK)
{
xxLog("xxGraphic", "%s %s (%s)", "Direct3D", "7.0", xxGetInstanceName());
break;
}
}
SafeRelease(unknown);
return reinterpret_cast<uint64_t>(d3dDevice);
}
//------------------------------------------------------------------------------
void xxDestroyDeviceD3D7(uint64_t device)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(device);
SafeRelease(d3dDevice);
SafeRelease(g_primarySurface);
g_vertexBuffer = nullptr;
}
//------------------------------------------------------------------------------
bool xxResetDeviceD3D7(uint64_t device)
{
return false;
}
//------------------------------------------------------------------------------
bool xxTestDeviceD3D7(uint64_t device)
{
HRESULT hResult = g_ddraw->TestCooperativeLevel();
while (hResult == DDERR_NOEXCLUSIVEMODE || hResult == DDERR_EXCLUSIVEMODEALREADYSET)
{
Sleep(100);
hResult = g_ddraw->TestCooperativeLevel();
}
return hResult == DD_OK;
}
//==============================================================================
// Swapchain
//==============================================================================
uint64_t xxCreateSwapchainD3D7(uint64_t device, uint64_t renderPass, void* view, int width, int height, uint64_t oldSwapchain)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(device);
if (d3dDevice == nullptr)
return 0;
D3DSWAPCHAIN7* d3dOldSwapchain = reinterpret_cast<D3DSWAPCHAIN7*>(oldSwapchain);
if (d3dOldSwapchain && d3dOldSwapchain->hWnd == view && d3dOldSwapchain->width == width && d3dOldSwapchain->height == height)
return oldSwapchain;
D3DSWAPCHAIN7* swapchain = xxAlloc(D3DSWAPCHAIN7);
if (swapchain == nullptr)
return 0;
xxDestroySwapchainD3D7(oldSwapchain);
HWND hWnd = (HWND)view;
LPDIRECTDRAWSURFACE7 backSurface = nullptr;
if (backSurface == nullptr)
{
DDSURFACEDESC2 desc = {};
desc.dwSize = sizeof(DDSURFACEDESC2);
desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
desc.ddsCaps.dwCaps = DDSCAPS_VIDEOMEMORY | DDSCAPS_3DDEVICE;
desc.dwWidth = width;
desc.dwHeight = height;
HRESULT result = g_ddraw->CreateSurface(&desc, &backSurface, nullptr);
if (result != S_OK)
return 0;
}
LPDIRECTDRAWSURFACE7 depthSurface = nullptr;
if (depthSurface == nullptr)
{
DDSURFACEDESC2 desc = {};
desc.dwSize = sizeof(DDSURFACEDESC2);
desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY;
desc.dwWidth = width;
desc.dwHeight = height;
desc.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
desc.ddpfPixelFormat.dwFlags = DDPF_ZBUFFER | DDPF_STENCILBUFFER;
desc.ddpfPixelFormat.dwZBufferBitDepth = 32;
desc.ddpfPixelFormat.dwStencilBitDepth = 8;
desc.ddpfPixelFormat.dwZBitMask = 0x00FFFFFF;
desc.ddpfPixelFormat.dwStencilBitMask = 0xFF000000;
HRESULT result = g_ddraw->CreateSurface(&desc, &depthSurface, nullptr);
if (result != S_OK)
{
backSurface->Release();
return 0;
}
backSurface->AddAttachedSurface(depthSurface);
}
LPDIRECTDRAWCLIPPER clipper = nullptr;
if (clipper == nullptr)
{
HRESULT result = g_ddraw->CreateClipper(0, &clipper, nullptr);
if (result != S_OK)
{
backSurface->Release();
depthSurface->Release();
return 0;
}
clipper->SetHWnd(0, hWnd);
}
swapchain->backSurface = backSurface;
swapchain->depthSurface = depthSurface;
swapchain->clipper = clipper;
swapchain->hWnd = hWnd;
swapchain->width = width;
swapchain->height = height;
return reinterpret_cast<uint64_t>(swapchain);
}
//------------------------------------------------------------------------------
void xxDestroySwapchainD3D7(uint64_t swapchain)
{
D3DSWAPCHAIN7* d3dSwapchain = reinterpret_cast<D3DSWAPCHAIN7*>(swapchain);
if (d3dSwapchain == nullptr)
return;
SafeRelease(d3dSwapchain->backSurface);
SafeRelease(d3dSwapchain->depthSurface);
SafeRelease(d3dSwapchain->clipper);
xxFree(d3dSwapchain);
}
//------------------------------------------------------------------------------
void xxPresentSwapchainD3D7(uint64_t swapchain)
{
D3DSWAPCHAIN7* d3dSwapchain = reinterpret_cast<D3DSWAPCHAIN7*>(swapchain);
if (d3dSwapchain == nullptr)
return;
RECT rect = {};
GetClientRect(d3dSwapchain->hWnd, &rect);
ClientToScreen(d3dSwapchain->hWnd, (POINT*)&rect.left);
ClientToScreen(d3dSwapchain->hWnd, (POINT*)&rect.right);
g_primarySurface->SetClipper(d3dSwapchain->clipper);
g_primarySurface->Blt(&rect, d3dSwapchain->backSurface, nullptr, DDBLT_WAIT, nullptr);
}
//------------------------------------------------------------------------------
uint64_t xxGetCommandBufferD3D7(uint64_t device, uint64_t swapchain)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(device);
if (d3dDevice == nullptr)
return 0;
D3DSWAPCHAIN7* d3dSwapchain = reinterpret_cast<D3DSWAPCHAIN7*>(swapchain);
if (d3dSwapchain == nullptr)
return 0;
HRESULT hResult = d3dDevice->SetRenderTarget(d3dSwapchain->backSurface, 0);
if (hResult != S_OK)
return 0;
return device;
}
//------------------------------------------------------------------------------
uint64_t xxGetFramebufferD3D7(uint64_t device, uint64_t swapchain, float* scale)
{
if (scale)
{
(*scale) = 1.0f;
}
return swapchain;
}
//==============================================================================
// Command Buffer
//==============================================================================
bool xxBeginCommandBufferD3D7(uint64_t commandBuffer)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(commandBuffer);
if (d3dDevice == nullptr)
return false;
HRESULT hResult = d3dDevice->BeginScene();
return (hResult == S_OK);
}
//------------------------------------------------------------------------------
void xxEndCommandBufferD3D7(uint64_t commandBuffer)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(commandBuffer);
if (d3dDevice == nullptr)
return;
HRESULT hResult = d3dDevice->EndScene();
}
//------------------------------------------------------------------------------
void xxSubmitCommandBufferD3D7(uint64_t commandBuffer, uint64_t swapchain)
{
}
//==============================================================================
// Render Pass
//==============================================================================
uint64_t xxCreateRenderPassD3D7(uint64_t device, bool clearColor, bool clearDepth, bool clearStencil, bool storeColor, bool storeDepth, bool storeStencil)
{
DWORD flags = 0;
if (clearColor)
flags |= D3DCLEAR_TARGET;
if (clearDepth)
flags |= D3DCLEAR_ZBUFFER;
if (clearStencil)
flags |= D3DCLEAR_STENCIL;
return flags;
}
//------------------------------------------------------------------------------
void xxDestroyRenderPassD3D7(uint64_t renderPass)
{
}
//------------------------------------------------------------------------------
uint64_t xxBeginRenderPassD3D7(uint64_t commandBuffer, uint64_t framebuffer, uint64_t renderPass, int width, int height, float color[4], float depth, unsigned char stencil)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(commandBuffer);
if (d3dDevice == nullptr)
return 0;
D3DFRAMEBUFFER7* d3dFramebuffer = reinterpret_cast<D3DFRAMEBUFFER7*>(framebuffer);
if (d3dFramebuffer == nullptr)
return 0;
DWORD d3dFlags = static_cast<DWORD>(renderPass);
if (d3dFlags)
{
D3DVIEWPORT7 vp;
vp.dwX = 0;
vp.dwY = 0;
vp.dwWidth = width;
vp.dwHeight = height;
vp.dvMinZ = 0.0f;
vp.dvMaxZ = 1.0f;
d3dDevice->SetViewport(&vp);
d3dDevice->Clear(0, nullptr, d3dFlags, D3DRGBA(color[0], color[1], color[2], color[3]), depth, stencil);
}
return commandBuffer;
}
//------------------------------------------------------------------------------
void xxEndRenderPassD3D7(uint64_t commandEncoder, uint64_t framebuffer, uint64_t renderPass)
{
}
//==============================================================================
// Vertex Attribute
//==============================================================================
uint64_t xxCreateVertexAttributeD3D7(uint64_t device, int count, int* attribute)
{
D3DVERTEXATTRIBUTE7 d3dVertexAttribute = {};
int stride = 0;
for (int i = 0; i < count; ++i)
{
int stream = (*attribute++);
int offset = (*attribute++);
int element = (*attribute++);
int size = (*attribute++);
(void)stream;
(void)offset;
stride += size;
if (element == 'POS3' && size == sizeof(float) * 3)
d3dVertexAttribute.fvf = D3DFVF_XYZ | (d3dVertexAttribute.fvf & ~D3DFVF_POSITION_MASK);
if (element == 'BON3' && size == sizeof(float) * 3)
d3dVertexAttribute.fvf = D3DFVF_XYZB3 | (d3dVertexAttribute.fvf & ~D3DFVF_POSITION_MASK);
if (element == 'BON4' && size == sizeof(char) * 4)
d3dVertexAttribute.fvf |= D3DFVF_RESERVED1;
if (element == 'NOR3' && size == sizeof(float) * 3)
d3dVertexAttribute.fvf |= D3DFVF_NORMAL;
if (element == 'COL4' && size == sizeof(char) * 4)
d3dVertexAttribute.fvf |= D3DFVF_DIFFUSE;
if (element == 'TEX2' && size == sizeof(float) * 2)
d3dVertexAttribute.fvf += D3DFVF_TEX1;
}
d3dVertexAttribute.stride = stride;
return static_cast<uint64_t>(d3dVertexAttribute.value);
}
//------------------------------------------------------------------------------
void xxDestroyVertexAttributeD3D7(uint64_t vertexAttribute)
{
}
//==============================================================================
// Buffer
//==============================================================================
uint64_t xxCreateConstantBufferD3D7(uint64_t device, int size)
{
char* d3dBuffer = xxAlloc(char, size);
return reinterpret_cast<uint64_t>(d3dBuffer) | D3DRTYPE_CONSTANTBUFFER;
}
//------------------------------------------------------------------------------
uint64_t xxCreateIndexBufferD3D7(uint64_t device, int size)
{
char* d3dBuffer = xxAlloc(char, size);
return reinterpret_cast<uint64_t>(d3dBuffer) | D3DRTYPE_INDEXBUFFER;
}
//------------------------------------------------------------------------------
uint64_t xxCreateVertexBufferD3D7(uint64_t device, int size, uint64_t vertexAttribute)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(device);
if (d3dDevice == nullptr)
return 0;
LPDIRECT3D7 d3d = nullptr;
if (d3d == nullptr)
{
HRESULT result = d3dDevice->GetDirect3D(&d3d);
if (result != S_OK)
return 0;
d3d->Release();
}
D3DVERTEXATTRIBUTE7 d3dVertexAttribute = { vertexAttribute };
D3DVERTEXBUFFERDESC desc = {};
desc.dwSize = sizeof(D3DVERTEXBUFFERDESC);
desc.dwCaps = D3DVBCAPS_WRITEONLY;
desc.dwFVF = d3dVertexAttribute.fvf;
desc.dwNumVertices = (size / d3dVertexAttribute.stride);
LPDIRECT3DVERTEXBUFFER7 buffer = nullptr;
HRESULT result = d3d->CreateVertexBuffer(&desc, &buffer, 0);
if (result != S_OK)
return 0;
return reinterpret_cast<uint64_t>(buffer) | D3DRTYPE_VERTEXBUFFER;
}
//------------------------------------------------------------------------------
void xxDestroyBufferD3D7(uint64_t device, uint64_t buffer)
{
switch (getResourceType(buffer))
{
case D3DRTYPE_CONSTANTBUFFER:
case D3DRTYPE_INDEXBUFFER:
{
char* d3dBuffer = reinterpret_cast<char*>(getResourceData(buffer));
xxFree(d3dBuffer);
break;
}
case D3DRTYPE_VERTEXBUFFER:
{
LPDIRECT3DVERTEXBUFFER7 d3dVertexBuffer = reinterpret_cast<LPDIRECT3DVERTEXBUFFER7>(getResourceData(buffer));
if (d3dVertexBuffer == nullptr)
return;
d3dVertexBuffer->Release();
break;
}
default:
break;
}
}
//------------------------------------------------------------------------------
void* xxMapBufferD3D7(uint64_t device, uint64_t buffer)
{
switch (getResourceType(buffer))
{
case D3DRTYPE_CONSTANTBUFFER:
case D3DRTYPE_INDEXBUFFER:
{
char* ptr = reinterpret_cast<char*>(getResourceData(buffer));
if (ptr == nullptr)
break;
return ptr;
}
case D3DRTYPE_VERTEXBUFFER:
{
LPDIRECT3DVERTEXBUFFER7 d3dVertexBuffer = reinterpret_cast<LPDIRECT3DVERTEXBUFFER7>(getResourceData(buffer));
if (d3dVertexBuffer == nullptr)
return nullptr;
void* ptr = nullptr;
HRESULT hResult = d3dVertexBuffer->Lock(DDLOCK_DISCARDCONTENTS, &ptr, nullptr);
if (hResult != S_OK)
break;
return ptr;
}
default:
break;
}
return nullptr;
}
//------------------------------------------------------------------------------
void xxUnmapBufferD3D7(uint64_t device, uint64_t buffer)
{
switch (getResourceType(buffer))
{
case D3DRTYPE_CONSTANTBUFFER:
case D3DRTYPE_INDEXBUFFER:
{
return;
}
case D3DRTYPE_VERTEXBUFFER:
{
LPDIRECT3DVERTEXBUFFER7 d3dVertexBuffer = reinterpret_cast<LPDIRECT3DVERTEXBUFFER7>(getResourceData(buffer));
if (d3dVertexBuffer == nullptr)
return;
d3dVertexBuffer->Unlock();
return;
}
default:
break;
}
}
//==============================================================================
// Texture
//==============================================================================
uint64_t xxCreateTextureD3D7(uint64_t device, uint64_t format, int width, int height, int depth, int mipmap, int array, void const* external)
{
DDPIXELFORMAT pixelFormat;
switch (format)
{
case "RGB565"_CC: pixelFormat = { 0, DDPF_RGB, 0, 16, 0xFF, 0xFF00, 0xFF0000 }; break;
case "BGR565"_CC: pixelFormat = { 0, DDPF_RGB, 0, 16, 0xFF0000, 0xFF00, 0xFF }; break;
case "ARGB1555"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 16, 0xF800, 0x7C0, 0x3E, 0x1 }; break;
case "ABGR1555"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 16, 0x3E, 0x7C0, 0xF800, 0x1 }; break;
case "ARGB4444"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 16, 0xF0, 0xF00, 0xF000, 0xF }; break;
case "ABGR4444"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 16, 0xF000, 0xF00, 0xF0, 0xF }; break;
case "RGBA4444"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 16, 0xF, 0xF0, 0xF00, 0xF000 }; break;
case "BGRA4444"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 16, 0xF00, 0xF0, 0xF, 0xF000 }; break;
case "RGBA5551"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 16, 0x1F, 0x3E0, 0x7C00, 0x8000 }; break;
case "BGRA5551"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 16, 0x7C00, 0x3E0, 0x1F, 0x8000 }; break;
case "ARGB8888"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 32, 0xFF00, 0xFF0000, 0xFF000000, 0xFF }; break;
case "RGBA8888"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000 }; break;
case "ABGR8888"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF }; break;
case "BGRA8888"_CC: pixelFormat = { 0, DDPF_RGB | DDPF_ALPHAPIXELS, 0, 32, 0xFF0000, 0xFF00, 0xFF, 0xFF000000 }; break;
case "DS24"_cc: pixelFormat = { 0, DDPF_ZBUFFER | DDPF_STENCILBUFFER, 0, 24, 8 }; break;
case "BC1"_cc:
case "DXT1"_cc: pixelFormat = { 0, DDPF_FOURCC, "DXT1"_cc }; break;
case "BC2"_cc:
case "DXT3"_cc: pixelFormat = { 0, DDPF_FOURCC, "DXT3"_cc }; break;
case "BC3"_cc:
case "DXT5"_cc: pixelFormat = { 0, DDPF_FOURCC, "DXT5"_cc }; break;
case "BC4S"_cc: pixelFormat = { 0, DDPF_FOURCC, "ATI1"_cc }; break;
case "BC4U"_cc:
case "ATI1"_cc: pixelFormat = { 0, DDPF_FOURCC, "ATI1"_cc }; break;
case "BC5S"_cc: pixelFormat = { 0, DDPF_FOURCC, "ATI2"_cc }; break;
case "BC5U"_cc:
case "ATI2"_cc: pixelFormat = { 0, DDPF_FOURCC, "ATI2"_cc }; break;
case "BC6H"_cc: pixelFormat = { 0, DDPF_FOURCC, "DXT5"_cc }; break;
case "BC7"_cc: pixelFormat = { 0, DDPF_FOURCC, "DXT5"_cc }; break;
default:
xxLog("xxGraphic", "Unknown format (%.8s)", &format);
return 0;
}
DDSURFACEDESC2 desc = {};
desc.dwSize = sizeof(DDSURFACEDESC2);
desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
desc.ddsCaps.dwCaps2 = DDSCAPS2_HINTDYNAMIC;
desc.dwWidth = width;
desc.dwHeight = height;
desc.ddpfPixelFormat = pixelFormat;
desc.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
LPDIRECTDRAWSURFACE7 surface = nullptr;
HRESULT result = g_ddraw->CreateSurface(&desc, &surface, nullptr);
if (result != S_OK)
return 0;
return reinterpret_cast<uint64_t>(surface);
}
//------------------------------------------------------------------------------
void xxDestroyTextureD3D7(uint64_t texture)
{
LPDIRECTDRAWSURFACE7 surface = reinterpret_cast<LPDIRECTDRAWSURFACE7>(texture);
SafeRelease(surface);
}
//------------------------------------------------------------------------------
void* xxMapTextureD3D7(uint64_t device, uint64_t texture, int* stride, int level, int array)
{
LPDIRECTDRAWSURFACE7 surface = reinterpret_cast<LPDIRECTDRAWSURFACE7>(texture);
if (surface == nullptr)
return nullptr;
DDSURFACEDESC2 desc = {};
if (desc.dwSize == 0)
{
desc.dwSize = sizeof(DDSURFACEDESC2);
HRESULT result = surface->GetSurfaceDesc(&desc);
if (result != S_OK)
return nullptr;
}
HRESULT result = surface->Lock(nullptr, &desc, DDLOCK_WAIT, nullptr);
if (result != S_OK)
return nullptr;
(*stride) = desc.lPitch;
return desc.lpSurface;
}
//------------------------------------------------------------------------------
void xxUnmapTextureD3D7(uint64_t device, uint64_t texture, int level, int array)
{
LPDIRECTDRAWSURFACE7 surface = reinterpret_cast<LPDIRECTDRAWSURFACE7>(texture);
if (surface == nullptr)
return;
surface->Unlock(nullptr);
}
//==============================================================================
// Sampler
//==============================================================================
uint64_t xxCreateSamplerD3D7(uint64_t device, bool clampU, bool clampV, bool clampW, bool linearMag, bool linearMin, bool linearMip, int anisotropy)
{
D3DSAMPLER7 d3dSampler = {};
d3dSampler.addressU = clampU ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP;
d3dSampler.addressV = clampV ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP;
d3dSampler.addressW = clampW ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP;
d3dSampler.magFilter = linearMag ? D3DTFG_LINEAR : D3DTFG_POINT;
d3dSampler.minFilter = linearMin ? D3DTFN_LINEAR : D3DTFN_POINT;
d3dSampler.mipFilter = linearMip ? D3DTFP_LINEAR : D3DTFP_POINT;
d3dSampler.anisotropy = anisotropy;
if (anisotropy > 1)
{
d3dSampler.magFilter = linearMag ? D3DTFG_ANISOTROPIC : D3DTFG_POINT;
d3dSampler.minFilter = linearMin ? D3DTFN_ANISOTROPIC : D3DTFN_POINT;
}
return static_cast<uint64_t>(d3dSampler.value);
}
//------------------------------------------------------------------------------
void xxDestroySamplerD3D7(uint64_t sampler)
{
}
//==============================================================================
// Shader
//==============================================================================
uint64_t xxCreateVertexShaderD3D7(uint64_t device, char const* shader, uint64_t vertexAttribute)
{
return 0;
}
//------------------------------------------------------------------------------
uint64_t xxCreateFragmentShaderD3D7(uint64_t device, char const* shader)
{
return 0;
}
//------------------------------------------------------------------------------
void xxDestroyShaderD3D7(uint64_t device, uint64_t shader)
{
}
//==============================================================================
// Pipeline
//==============================================================================
uint64_t xxCreateBlendStateD3D7(uint64_t device, char const* sourceColor, char const* operationColor, char const* destinationColor, char const* sourceAlpha, char const* operationAlpha, char const* destinationAlpha)
{
D3DRENDERSTATE7 d3dRenderState = {};
d3dRenderState.blendSourceColor = d3dBlendFactor(sourceColor);
d3dRenderState.blendDestinationColor = d3dBlendFactor(destinationColor);
d3dRenderState.blendEnable = (d3dRenderState.blendSourceColor != D3DBLEND_ONE ||
d3dRenderState.blendDestinationColor != D3DBLEND_ZERO) ? TRUE : FALSE;
return static_cast<uint64_t>(d3dRenderState.value);
}
//------------------------------------------------------------------------------
uint64_t xxCreateDepthStencilStateD3D7(uint64_t device, char const* depthTest, bool depthWrite)
{
D3DRENDERSTATE7 d3dRenderState = {};
d3dRenderState.depthTest = d3dCompareOp(depthTest);
d3dRenderState.depthWrite = depthWrite;
d3dRenderState.depthEnable = (d3dRenderState.depthTest != D3DCMP_ALWAYS ||
d3dRenderState.depthWrite != FALSE) ? TRUE : FALSE;
return static_cast<uint64_t>(d3dRenderState.value);
}
//------------------------------------------------------------------------------
uint64_t xxCreateRasterizerStateD3D7(uint64_t device, bool cull, bool scissor)
{
D3DRENDERSTATE7 d3dRenderState = {};
d3dRenderState.cull = cull;
d3dRenderState.scissor = scissor;
return static_cast<uint64_t>(d3dRenderState.value);
}
//------------------------------------------------------------------------------
uint64_t xxCreatePipelineD3D7(uint64_t device, uint64_t renderPass, uint64_t blendState, uint64_t depthStencilState, uint64_t rasterizerState, uint64_t vertexAttribute, uint64_t vertexShader, uint64_t fragmentShader)
{
D3DPIPELINE7* d3dPipeline = xxAlloc(D3DPIPELINE7);
if (d3dPipeline == nullptr)
return 0;
DWORD d3dVertexShader = static_cast<DWORD>(vertexShader);
DWORD d3dPixelShader = static_cast<DWORD>(fragmentShader);
D3DRENDERSTATE7 d3dBlendState = { blendState };
D3DRENDERSTATE7 d3dDepthStencilState = { depthStencilState };
D3DRENDERSTATE7 d3dRasterizerState = { rasterizerState };
d3dPipeline->renderState.blendEnable = d3dBlendState.blendEnable;
d3dPipeline->renderState.blendSourceColor = d3dBlendState.blendSourceColor;
d3dPipeline->renderState.blendDestinationColor = d3dBlendState.blendDestinationColor;
d3dPipeline->renderState.depthEnable = d3dDepthStencilState.depthEnable;
d3dPipeline->renderState.depthTest = d3dDepthStencilState.depthTest;
d3dPipeline->renderState.depthWrite = d3dDepthStencilState.depthWrite;
d3dPipeline->renderState.cull = d3dRasterizerState.cull;
d3dPipeline->renderState.scissor = d3dRasterizerState.scissor;
return reinterpret_cast<uint64_t>(d3dPipeline);
}
//------------------------------------------------------------------------------
void xxDestroyBlendStateD3D7(uint64_t blendState)
{
}
//------------------------------------------------------------------------------
void xxDestroyDepthStencilStateD3D7(uint64_t depthStencilState)
{
}
//------------------------------------------------------------------------------
void xxDestroyRasterizerStateD3D7(uint64_t rasterizerState)
{
}
//------------------------------------------------------------------------------
void xxDestroyPipelineD3D7(uint64_t pipeline)
{
D3DPIPELINE7* d3dPipeline = reinterpret_cast<D3DPIPELINE7*>(pipeline);
xxFree(d3dPipeline);
}
//==============================================================================
// Command
//==============================================================================
void xxSetViewportD3D7(uint64_t commandEncoder, int x, int y, int width, int height, float minZ, float maxZ)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(commandEncoder);
D3DVIEWPORT7 vp;
vp.dwX = x;
vp.dwY = y;
vp.dwWidth = width;
vp.dwHeight = height;
vp.dvMinZ = minZ;
vp.dvMaxZ = maxZ;
d3dDevice->SetViewport(&vp);
}
//------------------------------------------------------------------------------
void xxSetScissorD3D7(uint64_t commandEncoder, int x, int y, int width, int height)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(commandEncoder);
D3DVIEWPORT7 vp;
d3dDevice->GetViewport(&vp);
if (vp.dwX == x && vp.dwY == y && vp.dwWidth == width && vp.dwHeight == height)
return;
D3DMATRIX projection;
d3dDevice->GetTransform(D3DTRANSFORMSTATE_PROJECTION, &projection);
ViewportFromScissor(projection.m, vp.dwX, vp.dwY, vp.dwWidth, vp.dwHeight, x, y, width, height);
d3dDevice->SetTransform(D3DTRANSFORMSTATE_PROJECTION, &projection);
vp.dwX = x;
vp.dwY = y;
vp.dwWidth = width;
vp.dwHeight = height;
d3dDevice->SetViewport(&vp);
}
//------------------------------------------------------------------------------
void xxSetPipelineD3D7(uint64_t commandEncoder, uint64_t pipeline)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(commandEncoder);
D3DPIPELINE7* d3dPipeline = reinterpret_cast<D3DPIPELINE7*>(pipeline);
d3dDevice->SetRenderState(D3DRENDERSTATE_SHADEMODE, D3DSHADE_GOURAUD);
d3dDevice->SetRenderState(D3DRENDERSTATE_LIGHTING, FALSE);
d3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
d3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
d3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
d3dDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, FALSE);
d3dDevice->SetRenderState(D3DRENDERSTATE_SPECULARENABLE, FALSE);
d3dDevice->SetRenderState(D3DRENDERSTATE_CULLMODE, d3dPipeline->renderState.cull ? D3DCULL_CCW : D3DCULL_NONE);
d3dDevice->SetRenderState(D3DRENDERSTATE_ZENABLE, d3dPipeline->renderState.depthEnable);
if (d3dPipeline->renderState.depthEnable)
{
d3dDevice->SetRenderState(D3DRENDERSTATE_ZFUNC, d3dPipeline->renderState.depthTest);
d3dDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, d3dPipeline->renderState.depthWrite);
}
d3dDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, d3dPipeline->renderState.blendEnable);
if (d3dPipeline->renderState.blendEnable)
{
d3dDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, d3dPipeline->renderState.blendSourceColor);
d3dDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, d3dPipeline->renderState.blendDestinationColor);
}
}
//------------------------------------------------------------------------------
void xxSetVertexBuffersD3D7(uint64_t commandEncoder, int count, const uint64_t* buffers, uint64_t vertexAttribute)
{
g_vertexBuffer = reinterpret_cast<LPDIRECT3DVERTEXBUFFER7>(getResourceData(buffers[0]));
}
//------------------------------------------------------------------------------
void xxSetVertexTexturesD3D7(uint64_t commandEncoder, int count, const uint64_t* textures)
{
}
//------------------------------------------------------------------------------
void xxSetFragmentTexturesD3D7(uint64_t commandEncoder, int count, const uint64_t* textures)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(commandEncoder);
for (int i = 0; i < count; ++i)
{
LPDIRECTDRAWSURFACE7 ddSurface = reinterpret_cast<LPDIRECTDRAWSURFACE7>(textures[i]);
d3dDevice->SetTexture(i, ddSurface);
}
}
//------------------------------------------------------------------------------
void xxSetVertexSamplersD3D7(uint64_t commandEncoder, int count, const uint64_t* samplers)
{
}
//------------------------------------------------------------------------------
void xxSetFragmentSamplersD3D7(uint64_t commandEncoder, int count, const uint64_t* samplers)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(commandEncoder);
for (int i = 0; i < count; ++i)
{
D3DSAMPLER7 d3dSampler = { samplers[i] };
d3dDevice->SetTextureStageState(i, D3DTSS_ADDRESSU, d3dSampler.addressU);
d3dDevice->SetTextureStageState(i, D3DTSS_ADDRESSV, d3dSampler.addressV);
d3dDevice->SetTextureStageState(i, D3DTSS_MAGFILTER, d3dSampler.magFilter);
d3dDevice->SetTextureStageState(i, D3DTSS_MINFILTER, d3dSampler.minFilter);
d3dDevice->SetTextureStageState(i, D3DTSS_MIPFILTER, d3dSampler.mipFilter);
d3dDevice->SetTextureStageState(i, D3DTSS_MAXANISOTROPY, d3dSampler.anisotropy);
}
}
//------------------------------------------------------------------------------
void xxSetVertexConstantBufferD3D7(uint64_t commandEncoder, uint64_t buffer, int size)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(commandEncoder);
D3DMATRIX* d3dMatrix = reinterpret_cast<D3DMATRIX*>(getResourceData(buffer));
if (size >= sizeof(D3DMATRIX) * 1)
d3dDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &d3dMatrix[0]);
if (size >= sizeof(D3DMATRIX) * 2)
d3dDevice->SetTransform(D3DTRANSFORMSTATE_VIEW, &d3dMatrix[1]);
if (size >= sizeof(D3DMATRIX) * 3)
d3dDevice->SetTransform(D3DTRANSFORMSTATE_PROJECTION, &d3dMatrix[2]);
}
//------------------------------------------------------------------------------
void xxSetFragmentConstantBufferD3D7(uint64_t commandEncoder, uint64_t buffer, int size)
{
}
//------------------------------------------------------------------------------
void xxDrawD3D7(uint64_t commandEncoder, int vertexCount, int instanceCount, int firstVertex, int firstInstance)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(commandEncoder);
d3dDevice->DrawPrimitiveVB(D3DPT_TRIANGLELIST, g_vertexBuffer, firstVertex, vertexCount, 0);
}
//------------------------------------------------------------------------------
void xxDrawIndexedD3D7(uint64_t commandEncoder, uint64_t indexBuffer, int indexCount, int instanceCount, int firstIndex, int vertexOffset, int firstInstance)
{
LPDIRECT3DDEVICE7 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE7>(commandEncoder);
WORD* indexArray = nullptr;
if (INDEX_BUFFER_WIDTH == 2)
{
WORD* d3dIndexBuffer = reinterpret_cast<WORD*>(getResourceData(indexBuffer));
indexArray = d3dIndexBuffer + firstIndex;
}
else
{
static WORD wordIndexBuffer[65536];
DWORD* d3dIndexBuffer = reinterpret_cast<DWORD*>(getResourceData(indexBuffer));
DWORD* p = d3dIndexBuffer + firstIndex;
WORD* q = wordIndexBuffer;
#pragma loop(ivdep)
for (int i = 0; i < indexCount; ++i)
{
(*q++) = (WORD)(*p++);
}
indexArray = wordIndexBuffer;
}
d3dDevice->DrawIndexedPrimitiveVB(D3DPT_TRIANGLELIST, g_vertexBuffer, vertexOffset, 0, indexArray, indexCount, 0);
}
//==============================================================================