-
Notifications
You must be signed in to change notification settings - Fork 4
/
xxGraphicD3D6.cpp
1130 lines (1020 loc) · 42.3 KB
/
xxGraphicD3D6.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
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
//==============================================================================
// xxGraphic : Direct3D 6.0 Source
//
// Copyright (c) 2019-2024 TAiGA
// https://github.com/metarutaiga/xxGraphic
//==============================================================================
#define DIRECTDRAW_VERSION 0x600
#define DIRECT3D_VERSION 0x600
#define D3D_OVERLOADS
#include "xxSystem.h"
#include "dxsdk/ddraw.h"
#include "dxsdk/d3d.h"
#include "internal/xxGraphicInternalD3D.h"
#include "xxGraphicD3D6.h"
typedef HRESULT (WINAPI *PFN_DIRECT_DRAW_CREATE)(GUID*, LPDIRECTDRAW*, IUnknown*);
#define D3DRTYPE_CONSTANTBUFFER 0
#define D3DRTYPE_INDEXBUFFER 1
#define D3DRTYPE_VERTEXBUFFER 2
static void* g_ddrawLibrary = nullptr;
static LPDIRECTDRAW4 g_ddraw = nullptr;
static LPDIRECTDRAWSURFACE4 g_primarySurface = nullptr;
#if USE_VB
static LPDIRECT3DVERTEXBUFFER g_vertexBuffer = nullptr;
#else
static D3DVERTEXBUFFER3* g_vertexBuffer = nullptr;
#endif
//==============================================================================
// Instance
//==============================================================================
uint64_t xxCreateInstanceD3D6()
{
if (g_ddrawLibrary == nullptr)
g_ddrawLibrary = xxLoadLibrary("ddraw.dll");
if (g_ddrawLibrary == nullptr)
return 0;
PFN_DIRECT_DRAW_CREATE DirectDrawCreate;
(void*&)DirectDrawCreate = xxGetProcAddress(g_ddrawLibrary, "DirectDrawCreate");
if (DirectDrawCreate == nullptr)
return 0;
LPDIRECTDRAW ddraw = nullptr;
if (ddraw == nullptr)
{
HRESULT result = DirectDrawCreate(nullptr, &ddraw, nullptr);
if (result != S_OK)
return 0;
ddraw->SetCooperativeLevel(nullptr, DDSCL_FPUSETUP | DDSCL_NORMAL);
}
LPDIRECTDRAW4 ddraw4 = nullptr;
if (ddraw4 == nullptr)
{
HRESULT result = ddraw->QueryInterface(IID_PPV_ARGS(&ddraw4));
if (result != S_OK)
return 0;
ddraw->Release();
}
g_ddraw = ddraw4;
LPDIRECT3D3 d3d = nullptr;
HRESULT result = ddraw4->QueryInterface(IID_PPV_ARGS(&d3d));
if (result != S_OK)
return 0;
PatchD3DIM("d3dim.dll");
xxRegisterFunction(D3D6);
return reinterpret_cast<uint64_t>(d3d);
}
//------------------------------------------------------------------------------
void xxDestroyInstanceD3D6(uint64_t instance)
{
LPDIRECT3D3 d3d = reinterpret_cast<LPDIRECT3D3>(instance);
SafeRelease(d3d);
SafeRelease(g_ddraw);
if (g_ddrawLibrary)
{
xxFreeLibrary(g_ddrawLibrary);
g_ddrawLibrary = nullptr;
}
xxUnregisterFunction();
}
//==============================================================================
// Device
//==============================================================================
uint64_t xxCreateDeviceD3D6(uint64_t instance)
{
LPDIRECT3D3 d3d = reinterpret_cast<LPDIRECT3D3>(instance);
if (d3d == nullptr)
return 0;
LPDIRECTDRAWSURFACE4 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;
}
LPDIRECTDRAWSURFACE4 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;
}
LPDIRECT3DDEVICE3 d3dDevice = nullptr;
d3d->CreateDevice(__uuidof(IDirect3DHALDevice), backSurface, &d3dDevice, nullptr);
if (d3dDevice == nullptr)
{
backSurface->Release();
return 0;
}
backSurface->Release();
LPDIRECT3DVIEWPORT3 viewport = nullptr;
d3d->CreateViewport(&viewport, nullptr);
d3dDevice->AddViewport(viewport);
d3dDevice->SetCurrentViewport(viewport);
viewport->Release();
IUnknown* unknown = nullptr;
xxLocalBreak()
{
if (d3dDevice->QueryInterface(__uuidof(IDirect3DDevice3), (void**)&unknown) == S_OK)
{
xxLog("xxGraphic", "%s %s (%s)", "Direct3D", "6.0", xxGetInstanceName());
break;
}
}
SafeRelease(unknown);
return reinterpret_cast<uint64_t>(d3dDevice);
}
//------------------------------------------------------------------------------
void xxDestroyDeviceD3D6(uint64_t device)
{
LPDIRECT3DDEVICE3 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE3>(device);
SafeRelease(d3dDevice);
SafeRelease(g_primarySurface);
}
//------------------------------------------------------------------------------
bool xxResetDeviceD3D6(uint64_t device)
{
return false;
}
//------------------------------------------------------------------------------
bool xxTestDeviceD3D6(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 xxCreateSwapchainD3D6(uint64_t device, uint64_t renderPass, void* view, int width, int height, uint64_t oldSwapchain)
{
LPDIRECT3DDEVICE3 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE3>(device);
if (d3dDevice == nullptr)
return 0;
D3DSWAPCHAIN3* d3dOldSwapchain = reinterpret_cast<D3DSWAPCHAIN3*>(oldSwapchain);
if (d3dOldSwapchain && d3dOldSwapchain->hWnd == view && d3dOldSwapchain->width == width && d3dOldSwapchain->height == height)
return oldSwapchain;
D3DSWAPCHAIN3* swapchain = xxAlloc(D3DSWAPCHAIN3);
if (swapchain == nullptr)
return 0;
xxDestroySwapchainD3D6(oldSwapchain);
HWND hWnd = (HWND)view;
LPDIRECTDRAWSURFACE4 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;
}
LPDIRECTDRAWSURFACE4 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 xxDestroySwapchainD3D6(uint64_t swapchain)
{
D3DSWAPCHAIN3* d3dSwapchain = reinterpret_cast<D3DSWAPCHAIN3*>(swapchain);
if (d3dSwapchain == nullptr)
return;
SafeRelease(d3dSwapchain->backSurface);
SafeRelease(d3dSwapchain->depthSurface);
SafeRelease(d3dSwapchain->clipper);
xxFree(d3dSwapchain);
}
//------------------------------------------------------------------------------
void xxPresentSwapchainD3D6(uint64_t swapchain)
{
D3DSWAPCHAIN3* d3dSwapchain = reinterpret_cast<D3DSWAPCHAIN3*>(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 xxGetCommandBufferD3D6(uint64_t device, uint64_t swapchain)
{
LPDIRECT3DDEVICE3 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE3>(device);
if (d3dDevice == nullptr)
return 0;
D3DSWAPCHAIN3* d3dSwapchain = reinterpret_cast<D3DSWAPCHAIN3*>(swapchain);
if (d3dSwapchain == nullptr)
return 0;
HRESULT hResult = d3dDevice->SetRenderTarget(d3dSwapchain->backSurface, 0);
if (hResult != S_OK)
return 0;
return device;
}
//------------------------------------------------------------------------------
uint64_t xxGetFramebufferD3D6(uint64_t device, uint64_t swapchain, float* scale)
{
if (scale)
{
(*scale) = 1.0f;
}
return swapchain;
}
//==============================================================================
// Command Buffer
//==============================================================================
bool xxBeginCommandBufferD3D6(uint64_t commandBuffer)
{
LPDIRECT3DDEVICE3 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE3>(commandBuffer);
if (d3dDevice == nullptr)
return false;
HRESULT hResult = d3dDevice->BeginScene();
return (hResult == S_OK);
}
//------------------------------------------------------------------------------
void xxEndCommandBufferD3D6(uint64_t commandBuffer)
{
LPDIRECT3DDEVICE3 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE3>(commandBuffer);
if (d3dDevice == nullptr)
return;
HRESULT hResult = d3dDevice->EndScene();
}
//------------------------------------------------------------------------------
void xxSubmitCommandBufferD3D6(uint64_t commandBuffer, uint64_t swapchain)
{
}
//==============================================================================
// Render Pass
//==============================================================================
uint64_t xxCreateRenderPassD3D6(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 xxDestroyRenderPassD3D6(uint64_t renderPass)
{
}
//------------------------------------------------------------------------------
uint64_t xxBeginRenderPassD3D6(uint64_t commandBuffer, uint64_t framebuffer, uint64_t renderPass, int width, int height, float color[4], float depth, unsigned char stencil)
{
LPDIRECT3DDEVICE3 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE3>(commandBuffer);
if (d3dDevice == nullptr)
return 0;
D3DFRAMEBUFFER3* d3dFramebuffer = reinterpret_cast<D3DFRAMEBUFFER3*>(framebuffer);
if (d3dFramebuffer == nullptr)
return 0;
DWORD d3dFlags = static_cast<DWORD>(renderPass);
if (d3dFlags)
{
LPDIRECT3DVIEWPORT3 viewport = nullptr;
d3dDevice->GetCurrentViewport(&viewport);
viewport->Release();
D3DVIEWPORT2 vp = {};
vp.dwSize = sizeof(D3DVIEWPORT2);
vp.dwX = 0;
vp.dwY = 0;
vp.dwWidth = width;
vp.dwHeight = height;
vp.dvClipX = -1.0f;
vp.dvClipY = 1.0f;
vp.dvClipWidth = 2.0f;
vp.dvClipHeight = 2.0f;
vp.dvMinZ = 0.0f;
vp.dvMaxZ = 1.0f;
viewport->SetViewport2(&vp);
D3DRECT rect = {};
rect.lX2 = width;
rect.lY2 = height;
viewport->Clear2(1, &rect, d3dFlags, D3DRGBA(color[0], color[1], color[2], color[3]), depth, stencil);
}
return commandBuffer;
}
//------------------------------------------------------------------------------
void xxEndRenderPassD3D6(uint64_t commandEncoder, uint64_t framebuffer, uint64_t renderPass)
{
}
//==============================================================================
// Vertex Attribute
//==============================================================================
uint64_t xxCreateVertexAttributeD3D6(uint64_t device, int count, int* attribute)
{
D3DVERTEXATTRIBUTE3 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;
stride += size;
if (element == 'POS3' && size == sizeof(float) * 3)
{
d3dVertexAttribute.fvf |= D3DFVF_XYZ;
d3dVertexAttribute.offset |= 1 << (offset / sizeof(float));
}
if (element == 'NOR3' && size == sizeof(float) * 3)
{
d3dVertexAttribute.fvf |= D3DFVF_NORMAL;
d3dVertexAttribute.offset |= 1 << (offset / sizeof(float));
}
if (element == 'COL4' && size == sizeof(char) * 4)
{
d3dVertexAttribute.fvf |= D3DFVF_DIFFUSE;
d3dVertexAttribute.offset |= 1 << (offset / sizeof(float));
}
if (element == 'TEX2' && size == sizeof(float) * 2)
{
d3dVertexAttribute.fvf += D3DFVF_TEX1;
d3dVertexAttribute.offset |= 1 << (offset / sizeof(float));
}
}
d3dVertexAttribute.stride = stride;
return static_cast<uint64_t>(d3dVertexAttribute.value);
}
//------------------------------------------------------------------------------
void xxDestroyVertexAttributeD3D6(uint64_t vertexAttribute)
{
}
//==============================================================================
// Buffer
//==============================================================================
uint64_t xxCreateConstantBufferD3D6(uint64_t device, int size)
{
char* d3dBuffer = xxAlloc(char, size);
return reinterpret_cast<uint64_t>(d3dBuffer) | D3DRTYPE_CONSTANTBUFFER;
}
//------------------------------------------------------------------------------
uint64_t xxCreateIndexBufferD3D6(uint64_t device, int size)
{
char* d3dBuffer = xxAlloc(char, size);
return reinterpret_cast<uint64_t>(d3dBuffer) | D3DRTYPE_INDEXBUFFER;
}
//------------------------------------------------------------------------------
uint64_t xxCreateVertexBufferD3D6(uint64_t device, int size, uint64_t vertexAttribute)
{
#if USE_VB
LPDIRECT3DDEVICE3 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE3>(device);
if (d3dDevice == nullptr)
return 0;
LPDIRECT3D3 d3d = nullptr;
if (d3d == nullptr)
{
HRESULT result = d3dDevice->GetDirect3D(&d3d);
if (result != S_OK)
return 0;
d3d->Release();
}
D3DVERTEXATTRIBUTE3 d3dVertexAttribute = { vertexAttribute };
D3DVERTEXBUFFERDESC desc = {};
desc.dwSize = sizeof(D3DVERTEXBUFFERDESC);
desc.dwCaps = D3DVBCAPS_WRITEONLY;
desc.dwFVF = d3dVertexAttribute.fvf;
desc.dwNumVertices = (size / d3dVertexAttribute.stride);
LPDIRECT3DVERTEXBUFFER buffer = nullptr;
HRESULT result = d3d->CreateVertexBuffer(&desc, &buffer, 0, nullptr);
if (result != S_OK)
return 0;
return reinterpret_cast<uint64_t>(buffer) | D3DRTYPE_VERTEXBUFFER;
#else
D3DVERTEXATTRIBUTE2 d3dVertexAttribute = { vertexAttribute };
if (d3dVertexAttribute.stride == 0)
return 0;
D3DVERTEXBUFFER3* d3dVertexBuffer = xxAlloc(D3DVERTEXBUFFER3);
if (d3dVertexBuffer == nullptr)
return 0;
D3DDRAWPRIMITIVESTRIDEDDATA* stridedData = xxAlloc(D3DDRAWPRIMITIVESTRIDEDDATA);
if (stridedData == nullptr)
{
xxFree(d3dVertexBuffer);
return 0;
}
d3dVertexBuffer->buffer = xxAlloc(char, size);
d3dVertexBuffer->size = size;
d3dVertexBuffer->count = size / d3dVertexAttribute.stride;
d3dVertexBuffer->fvf = d3dVertexAttribute.fvf;
d3dVertexBuffer->address = stridedData;
int offsetBits = d3dVertexAttribute.offset;
if (d3dVertexAttribute.fvf & D3DFVF_XYZ)
{
int offset = xxCountTrailingZeros(offsetBits);
offsetBits &= ~(1 << offset);
stridedData->position.lpvData = (char*)d3dVertexBuffer->buffer + offset * 4;
stridedData->position.dwStride = d3dVertexAttribute.stride;
}
if (d3dVertexAttribute.fvf & D3DFVF_NORMAL)
{
int offset = xxCountTrailingZeros(offsetBits);
offsetBits &= ~(1 << offset);
stridedData->normal.lpvData = (char*)d3dVertexBuffer->buffer + offset * 4;
stridedData->normal.dwStride = d3dVertexAttribute.stride;
}
if (d3dVertexAttribute.fvf & D3DFVF_DIFFUSE)
{
int offset = xxCountTrailingZeros(offsetBits);
offsetBits &= ~(1 << offset);
stridedData->diffuse.lpvData = (char*)d3dVertexBuffer->buffer + offset * 4;
stridedData->diffuse.dwStride = d3dVertexAttribute.stride;
}
if (d3dVertexAttribute.fvf & D3DFVF_TEX1)
{
int offset = xxCountTrailingZeros(offsetBits);
offsetBits &= ~(1 << offset);
stridedData->textureCoords[0].lpvData = (char*)d3dVertexBuffer->buffer + offset * 4;
stridedData->textureCoords[0].dwStride = d3dVertexAttribute.stride;
}
return reinterpret_cast<uint64_t>(d3dVertexBuffer) | D3DRTYPE_VERTEXBUFFER;
#endif
}
//------------------------------------------------------------------------------
void xxDestroyBufferD3D6(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:
{
#if USE_VB
LPDIRECT3DVERTEXBUFFER d3dVertexBuffer = reinterpret_cast<LPDIRECT3DVERTEXBUFFER>(getResourceData(buffer));
if (d3dVertexBuffer == nullptr)
return;
d3dVertexBuffer->Release();
#else
D3DVERTEXBUFFER3* d3dVertexBuffer = reinterpret_cast<D3DVERTEXBUFFER3*>(getResourceData(buffer));
if (d3dVertexBuffer == nullptr)
break;
xxFree(d3dVertexBuffer->buffer);
xxFree(d3dVertexBuffer->address);
xxFree(d3dVertexBuffer);
#endif
break;
}
default:
break;
}
}
//------------------------------------------------------------------------------
void* xxMapBufferD3D6(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:
{
#if USE_VB
LPDIRECT3DVERTEXBUFFER d3dVertexBuffer = reinterpret_cast<LPDIRECT3DVERTEXBUFFER>(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;
#else
D3DVERTEXBUFFER3* d3dVertexBuffer = reinterpret_cast<D3DVERTEXBUFFER3*>(getResourceData(buffer));
if (d3dVertexBuffer == nullptr)
break;
return d3dVertexBuffer->buffer;
#endif
}
default:
break;
}
return nullptr;
}
//------------------------------------------------------------------------------
void xxUnmapBufferD3D6(uint64_t device, uint64_t buffer)
{
switch (getResourceType(buffer))
{
case D3DRTYPE_CONSTANTBUFFER:
case D3DRTYPE_INDEXBUFFER:
{
return;
}
case D3DRTYPE_VERTEXBUFFER:
{
#if USE_VB
LPDIRECT3DVERTEXBUFFER d3dVertexBuffer = reinterpret_cast<LPDIRECT3DVERTEXBUFFER>(getResourceData(buffer));
if (d3dVertexBuffer == nullptr)
return;
d3dVertexBuffer->Unlock();
#endif
return;
}
default:
break;
}
}
//==============================================================================
// Texture
//==============================================================================
uint64_t xxCreateTextureD3D6(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);
LPDIRECTDRAWSURFACE4 surface = nullptr;
HRESULT result = g_ddraw->CreateSurface(&desc, &surface, nullptr);
if (result != S_OK)
return 0;
LPDIRECT3DTEXTURE2 texture = nullptr;
surface->QueryInterface(IID_PPV_ARGS(&texture));
surface->Release();
return reinterpret_cast<uint64_t>(texture);
}
//------------------------------------------------------------------------------
void xxDestroyTextureD3D6(uint64_t texture)
{
LPDIRECT3DTEXTURE2 d3dTexture = reinterpret_cast<LPDIRECT3DTEXTURE2>(texture);
SafeRelease(d3dTexture);
}
//------------------------------------------------------------------------------
void* xxMapTextureD3D6(uint64_t device, uint64_t texture, int* stride, int level, int array)
{
LPDIRECT3DTEXTURE2 d3dTexture = reinterpret_cast<LPDIRECT3DTEXTURE2>(texture);
if (d3dTexture == nullptr)
return nullptr;
LPDIRECTDRAWSURFACE4 surface = nullptr;
d3dTexture->QueryInterface(IID_PPV_ARGS(&surface));
if (surface == nullptr)
return nullptr;
surface->Release();
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 xxUnmapTextureD3D6(uint64_t device, uint64_t texture, int level, int array)
{
LPDIRECT3DTEXTURE2 d3dTexture = reinterpret_cast<LPDIRECT3DTEXTURE2>(texture);
if (d3dTexture == nullptr)
return;
LPDIRECTDRAWSURFACE4 surface = nullptr;
d3dTexture->QueryInterface(IID_PPV_ARGS(&surface));
if (surface == nullptr)
return;
surface->Release();
surface->Unlock(nullptr);
}
//==============================================================================
// Sampler
//==============================================================================
uint64_t xxCreateSamplerD3D6(uint64_t device, bool clampU, bool clampV, bool clampW, bool linearMag, bool linearMin, bool linearMip, int anisotropy)
{
D3DSAMPLER3 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 xxDestroySamplerD3D6(uint64_t sampler)
{
}
//==============================================================================
// Shader
//==============================================================================
uint64_t xxCreateVertexShaderD3D6(uint64_t device, char const* shader, uint64_t vertexAttribute)
{
return 0;
}
//------------------------------------------------------------------------------
uint64_t xxCreateFragmentShaderD3D6(uint64_t device, char const* shader)
{
return 0;
}
//------------------------------------------------------------------------------
void xxDestroyShaderD3D6(uint64_t device, uint64_t shader)
{
}
//==============================================================================
// Pipeline
//==============================================================================
uint64_t xxCreateBlendStateD3D6(uint64_t device, char const* sourceColor, char const* operationColor, char const* destinationColor, char const* sourceAlpha, char const* operationAlpha, char const* destinationAlpha)
{
D3DRENDERSTATE3 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 xxCreateDepthStencilStateD3D6(uint64_t device, char const* depthTest, bool depthWrite)
{
D3DRENDERSTATE3 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 xxCreateRasterizerStateD3D6(uint64_t device, bool cull, bool scissor)
{
D3DRENDERSTATE3 d3dRenderState = {};
d3dRenderState.cull = cull;
d3dRenderState.scissor = scissor;
return static_cast<uint64_t>(d3dRenderState.value);
}
//------------------------------------------------------------------------------
uint64_t xxCreatePipelineD3D6(uint64_t device, uint64_t renderPass, uint64_t blendState, uint64_t depthStencilState, uint64_t rasterizerState, uint64_t vertexAttribute, uint64_t vertexShader, uint64_t fragmentShader)
{
D3DPIPELINE3* d3dPipeline = xxAlloc(D3DPIPELINE3);
if (d3dPipeline == nullptr)
return 0;
DWORD d3dVertexShader = static_cast<DWORD>(vertexShader);
DWORD d3dPixelShader = static_cast<DWORD>(fragmentShader);
D3DRENDERSTATE3 d3dBlendState = { blendState };
D3DRENDERSTATE3 d3dDepthStencilState = { depthStencilState };
D3DRENDERSTATE3 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 xxDestroyBlendStateD3D6(uint64_t blendState)
{
}
//------------------------------------------------------------------------------
void xxDestroyDepthStencilStateD3D6(uint64_t depthStencilState)
{
}
//------------------------------------------------------------------------------
void xxDestroyRasterizerStateD3D6(uint64_t rasterizerState)
{
}
//------------------------------------------------------------------------------
void xxDestroyPipelineD3D6(uint64_t pipeline)
{
D3DPIPELINE3* d3dPipeline = reinterpret_cast<D3DPIPELINE3*>(pipeline);
xxFree(d3dPipeline);
}
//==============================================================================
// Command
//==============================================================================
void xxSetViewportD3D6(uint64_t commandEncoder, int x, int y, int width, int height, float minZ, float maxZ)
{
LPDIRECT3DDEVICE3 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE3>(commandEncoder);
LPDIRECT3DVIEWPORT3 viewport = nullptr;
d3dDevice->GetCurrentViewport(&viewport);
viewport->Release();
D3DVIEWPORT2 vp = {};
vp.dwSize = sizeof(D3DVIEWPORT2);
vp.dwX = x;
vp.dwY = y;
vp.dwWidth = width;
vp.dwHeight = height;
vp.dvClipX = -1.0f;
vp.dvClipY = 1.0f;
vp.dvClipWidth = 2.0f;
vp.dvClipHeight = 2.0f;
vp.dvMinZ = minZ;
vp.dvMaxZ = maxZ;
viewport->SetViewport2(&vp);
}
//------------------------------------------------------------------------------
void xxSetScissorD3D6(uint64_t commandEncoder, int x, int y, int width, int height)
{
LPDIRECT3DDEVICE3 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE3>(commandEncoder);
LPDIRECT3DVIEWPORT3 viewport = nullptr;
d3dDevice->GetCurrentViewport(&viewport);
viewport->Release();
D3DVIEWPORT2 vp;
vp.dwSize = sizeof(D3DVIEWPORT2);
viewport->GetViewport2(&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;
viewport->SetViewport2(&vp);
}
//------------------------------------------------------------------------------
void xxSetPipelineD3D6(uint64_t commandEncoder, uint64_t pipeline)
{
LPDIRECT3DDEVICE3 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE3>(commandEncoder);
D3DPIPELINE3* d3dPipeline = reinterpret_cast<D3DPIPELINE3*>(pipeline);
d3dDevice->SetRenderState(D3DRENDERSTATE_SHADEMODE, D3DSHADE_GOURAUD);
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 xxSetVertexBuffersD3D6(uint64_t commandEncoder, int count, const uint64_t* buffers, uint64_t vertexAttribute)
{
#if USB_VB
g_vertexBuffer = reinterpret_cast<LPDIRECT3DVERTEXBUFFER>(getResourceData(buffers[0]));
#else
g_vertexBuffer = reinterpret_cast<D3DVERTEXBUFFER3*>(getResourceData(buffers[0]));
#endif
}
//------------------------------------------------------------------------------
void xxSetVertexTexturesD3D6(uint64_t commandEncoder, int count, const uint64_t* textures)
{
}
//------------------------------------------------------------------------------
void xxSetFragmentTexturesD3D6(uint64_t commandEncoder, int count, const uint64_t* textures)
{
LPDIRECT3DDEVICE3 d3dDevice = reinterpret_cast<LPDIRECT3DDEVICE3>(commandEncoder);
for (int i = 0; i < count; ++i)
{
LPDIRECT3DTEXTURE2 texture = reinterpret_cast<LPDIRECT3DTEXTURE2>(textures[i]);
d3dDevice->SetTexture(i, texture);
}
}
//------------------------------------------------------------------------------
void xxSetVertexSamplersD3D6(uint64_t commandEncoder, int count, const uint64_t* samplers)
{
}