-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD3dcalls.c
968 lines (902 loc) · 33.7 KB
/
D3dcalls.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
/*
* The X Men, June 1996
* Copyright (c) 1996 Probe Entertainment Limited
* All Rights Reserved
*
* $Revision: 28 $
*
* $Header: /PcProjectX/D3dcalls.c 28 11/11/98 16:00 Philipy $
*
* $Log: /PcProjectX/D3dcalls.c $
*
* 28 11/11/98 16:00 Philipy
* various fixes for warnings / errors when compiling under VC6
*
* 27 5/11/98 3:34p Oliverc
* Made Z_TRICK work as it should (but left disabled for patch beta 4 as
* it messes up on translucent external views in certain levels)
*
* 26 3/11/98 16:06 Philipy
* z trick stuff
*
* 25 14/08/98 15:25 Philipy
* added trilinear option to menus
* fixed level name / shutdown packet in heartbeat
*
*
* 24 6/11/98 9:29a Phillipd
*
* 23 4/09/98 11:33a Phillipd
*
* 22 3/20/98 12:39p Phillipd
* removed the enumeration of alpha texture formats....
*
* 21 3/14/98 12:42p Phillipd
*
* 20 5/01/98 19:58 Oliverc
* Current screen mode, texture format and sound volume settings now saved
* to registry on exit and restored on startup
*
* 19 12/29/97 9:54a Phillipd
*
* 18 12/23/97 10:44a Phillipd
*
* 17 12/23/97 9:59a Phillipd
* Unwanted Tetxure Formats commented out...
* Unwanted ScreenModes out...
* Pickup Generation Sfx added
*
* 16 12/19/97 3:21p Phillipd
*
* 15 12/15/97 4:25p Phillipd
* Changed Is3Dfx2 so it does Trilinear if MipMap is requested...
*
* 14 11/10/97 4:11p Phillipd
*
* 13 7/22/97 4:31p Phillipd
*
* 12 5/20/97 2:19p Phillipd
* Joystick support added....PowerVr PolySorting done
*
* 11 5/16/97 10:07a Phillipd
*
* 10 4/15/97 4:43p Phillipd
*
* 9 3/11/97 9:16a Phillipd
* Texture animation added to models..not finished..
*
* 8 1/10/97 11:30a Phillipd
* movies are now doable
*
* 7 8/06/96 3:59p Phillipd
*
* 6 8/03/96 4:39p Phillipd
* all Models can be env mapped..
* Alpha texture formats are detected and reported...
* But cant load a system Alpha texture into a Video Alpha Texture!!
*
* 5 18/07/96 9:52 Collinsd
*
* 4 7/17/96 2:38p Phillipd
*
* 3 7/11/96 9:46a Phillipd
*
* 2 6/25/96 11:37a Phillipd
* First SS update
*
*/
/*
* Copyright (C) 1995, 1996 Microsoft Corporation. All Rights Reserved.
*
* File: d3dcalls.c
*
* Calls to Direct3D objects needed for rendering. Part of D3DApp.
*
* D3DApp is a collection of helper functions for Direct3D applications.
* D3DApp consists of the following files:
* d3dapp.h Main D3DApp header to be included by application
* d3dappi.h Internal header
* d3dapp.c D3DApp functions seen by application.
* ddcalls.c All calls to DirectDraw objects except textures
* d3dcalls.c All calls to Direct3D objects except textures
* texture.c Texture loading and managing texture list
* misc.c Miscellaneous calls
*/
//#define INITGUID
#include "typedefs.h"
#include "d3dappi.h"
extern BOOL MipMap;
extern BOOL Is3Dfx2;
BOOL TriLinear;
extern BOOL DontColourKey;
extern int TexturePalettized;
extern int TextureRedBPP;
extern int TextureGreenBPP;
extern int TextureBlueBPP;
extern int TextureAlphaBPP;
extern int TextureIndexBPP;
/***************************************************************************/
/* Creation of D3D */
/***************************************************************************/
//LPDIRECT3D lpD3D2;
BOOL
D3DAppICreateD3D(void)
{
// LastError = d3dappi.lpDD->lpVtbl->QueryInterface(d3dappi.lpDD,
// &IID_IDirect3D2, (LPVOID*)&lpD3D2);
LastError = d3dappi.lpDD->lpVtbl->QueryInterface(d3dappi.lpDD,
&IID_IDirect3D, (LPVOID*)&d3dappi.lpD3D);
if (LastError != DD_OK) {
D3DAppISetErrorString("Creation of IDirect3D failed.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
return TRUE;
exit_with_error:
return FALSE;
}
/***************************************************************************/
/* D3D Device Enumeration */
/***************************************************************************/
/*
* enumDeviceFunc
* Device enumeration callback. Record information about the D3D device
* reported by D3D.
*/
static HRESULT
WINAPI enumDeviceFunc(LPGUID lpGuid, LPSTR lpDeviceDescription,
LPSTR lpDeviceName, LPD3DDEVICEDESC lpHWDesc,
LPD3DDEVICEDESC lpHELDesc, LPVOID lpContext)
{
lpContext = lpContext;
/*
* Don't accept any hardware D3D devices if emulation only option is set
*/
if (lpHWDesc->dcmColorModel && d3dappi.bOnlyEmulation)
return D3DENUMRET_OK;
/*
* Record the D3D driver's inforamation
*/
memcpy(&d3dappi.Driver[d3dappi.NumDrivers].Guid, lpGuid, sizeof(GUID));
lstrcpy(d3dappi.Driver[d3dappi.NumDrivers].About, lpDeviceDescription);
lstrcpy(d3dappi.Driver[d3dappi.NumDrivers].Name, lpDeviceName);
/*
* Is this a hardware device or software emulation? Checking the color
* model for a valid model works.
*/
if (lpHWDesc->dcmColorModel) {
d3dappi.Driver[d3dappi.NumDrivers].bIsHardware = TRUE;
memcpy(&d3dappi.Driver[d3dappi.NumDrivers].Desc, lpHWDesc,
sizeof(D3DDEVICEDESC));
} else {
d3dappi.Driver[d3dappi.NumDrivers].bIsHardware = FALSE;
memcpy(&d3dappi.Driver[d3dappi.NumDrivers].Desc, lpHELDesc,
sizeof(D3DDEVICEDESC));
}
/*
* Does this driver do texture mapping?
*/
d3dappi.Driver[d3dappi.NumDrivers].bDoesTextures =
(d3dappi.Driver[d3dappi.NumDrivers].Desc.dpcTriCaps.dwTextureCaps &
D3DPTEXTURECAPS_PERSPECTIVE) ? TRUE : FALSE;
/*
* Does this driver only do Square Textures
*/
d3dappi.Driver[d3dappi.NumDrivers].bSquareOnly =
(d3dappi.Driver[d3dappi.NumDrivers].Desc.dpcTriCaps.dwTextureCaps &
D3DPTEXTURECAPS_SQUAREONLY) ? TRUE : FALSE;
/*
* Does this driver do Colour Key Transparency....
*/
d3dappi.Driver[d3dappi.NumDrivers].bTransparency =
(d3dappi.Driver[d3dappi.NumDrivers].Desc.dpcTriCaps.dwTextureCaps &
D3DPTEXTURECAPS_TRANSPARENCY) ? TRUE : FALSE;
/*
* Can this driver use a z-buffer?
*/
d3dappi.Driver[d3dappi.NumDrivers].bDoesZBuffer =
d3dappi.Driver[d3dappi.NumDrivers].Desc.dwDeviceZBufferBitDepth
? TRUE : FALSE;
/*
* Can this driver render to the Windows display depth
*/
d3dappi.Driver[d3dappi.NumDrivers].bCanDoWindow =
(d3dappi.Driver[d3dappi.NumDrivers].Desc.dwDeviceRenderBitDepth &
D3DAppIBPPToDDBD(d3dappi.WindowsDisplay.bpp)) ? TRUE : FALSE;
if (!d3dappi.bIsPrimary)
d3dappi.Driver[d3dappi.NumDrivers].bCanDoWindow = FALSE;
d3dappi.NumDrivers++;
if (d3dappi.NumDrivers == D3DAPP_MAXD3DDRIVERS)
return (D3DENUMRET_CANCEL);
return (D3DENUMRET_OK);
}
/*
* D3DAppIEnumDrivers
* Get the available drivers from Direct3D by enumeration.
*/
BOOL
D3DAppIEnumDrivers(void)
{
d3dappi.NumDrivers = 0;
LastError = d3dappi.lpD3D->lpVtbl->EnumDevices(d3dappi.lpD3D,
enumDeviceFunc, NULL);
if (LastError != DD_OK) {
D3DAppISetErrorString("Enumeration of drivers failed.\n%s",
D3DAppErrorToString(LastError));
return FALSE;
}
d3dappi.CurrDriver = 0;
return TRUE;
}
static BOOL TextureFormatMatch( D3DAppTextureFormat *t )
{
if ( t && TexturePalettized >= 0 )
{
if ( t->bPalettized && TexturePalettized
&& t->IndexBPP == TextureIndexBPP )
return TRUE;
if ( !t->bPalettized && !TexturePalettized
&& t->RedBPP == TextureRedBPP
&& t->GreenBPP == TextureGreenBPP
&& t->BlueBPP == TextureBlueBPP
&& t->AlphaBPP == TextureAlphaBPP )
return TRUE;
}
return FALSE;
}
int LowestTexFormat; // used to select the lowest bit depth for textures;
/***************************************************************************/
/* Enumeration of texure format */
/***************************************************************************/
/*
* EnumTextureFormatsCallback
* Record information about each texture format the current D3D driver can
* support. Choose one as the default format (paletted formats are prefered)
* and return it through lpContext.
*/
static HRESULT
CALLBACK EnumTextureFormatsCallback(LPDDSURFACEDESC lpDDSD, LPVOID lpContext)
{
unsigned long m;
int r, g, b, a;
int *lpStartFormat = (int *)lpContext;
D3DAppTextureFormat *tf_default, *tf_this;
if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS)
return DDENUMRET_OK;
/*
* Record the DDSURFACEDESC of this texture format
*/
memset(&d3dappi.TextureFormat[d3dappi.NumTextureFormats], 0,
sizeof(D3DAppTextureFormat));
memcpy(&d3dappi.TextureFormat[d3dappi.NumTextureFormats].ddsd, lpDDSD,
sizeof(DDSURFACEDESC));
/*
* Is this format palettized? How many bits? Otherwise, how many RGB
* bits?
*/
if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
// 8 bit palettized is fine if you dont have mipmaps....
if( MipMap )
return DDENUMRET_OK;
d3dappi.TextureFormat[d3dappi.NumTextureFormats].bPalettized = TRUE;
d3dappi.TextureFormat[d3dappi.NumTextureFormats].IndexBPP = 8;
} else if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED4) {
// We dont want 4 bit palettized!!!!!
return DDENUMRET_OK;
// d3dappi.TextureFormat[d3dappi.NumTextureFormats].bPalettized = TRUE;
// d3dappi.TextureFormat[d3dappi.NumTextureFormats].IndexBPP = 4;
}else{
d3dappi.TextureFormat[d3dappi.NumTextureFormats].bPalettized = FALSE;
d3dappi.TextureFormat[d3dappi.NumTextureFormats].IndexBPP = 0;
for (r = 0, m = lpDDSD->ddpfPixelFormat.dwRBitMask; !(m & 1);
r++, m >>= 1);
for (r = 0; m & 1; r++, m >>= 1);
for (g = 0, m = lpDDSD->ddpfPixelFormat.dwGBitMask; !(m & 1);
g++, m >>= 1);
for (g = 0; m & 1; g++, m >>= 1);
for (b = 0, m = lpDDSD->ddpfPixelFormat.dwBBitMask; !(m & 1);
b++, m >>= 1);
for (b = 0; m & 1; b++, m >>= 1);
a = 0;
if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS) {
for (a = 0, m = lpDDSD->ddpfPixelFormat.dwRGBAlphaBitMask; !(m & 1);
a++, m >>= 1);
for (a = 0; m & 1; a++, m >>= 1);
}
// we dont want less than 4 bits per gun...
if( r+g+b <= 8 )
return DDENUMRET_OK;
d3dappi.TextureFormat[d3dappi.NumTextureFormats].RedBPP = r;
d3dappi.TextureFormat[d3dappi.NumTextureFormats].GreenBPP = g;
d3dappi.TextureFormat[d3dappi.NumTextureFormats].BlueBPP = b;
d3dappi.TextureFormat[d3dappi.NumTextureFormats].AlphaBPP = a;
}
/*
* If lpStarFormat is -1, this is the first format. Select it.
*/
tf_default = ( *lpStartFormat >= 0 && *lpStartFormat < d3dappi.NumTextureFormats ) ? &d3dappi.TextureFormat[*lpStartFormat] : NULL;
if (*lpStartFormat == -1)
{
*lpStartFormat = d3dappi.NumTextureFormats;
LowestTexFormat = r+g+b ; // makes a note of the bit depths...
if( LowestTexFormat < 15 )
LowestTexFormat = 32;
}else if ( !TextureFormatMatch( tf_default ) ) {
tf_this = &d3dappi.TextureFormat[d3dappi.NumTextureFormats];
/*
* If this format is paletted and at least 8 bit, select it . but not if MipMap is enabled....
*/
if ( ( (d3dappi.TextureFormat[d3dappi.NumTextureFormats].bPalettized ) &&
(d3dappi.TextureFormat[d3dappi.NumTextureFormats].IndexBPP == 8 ) && !MipMap ) )
{
if ( TextureFormatMatch( tf_this ) )
{
*lpStartFormat = d3dappi.NumTextureFormats; // only select 8-bit palettized if chosen by user
}
}else{
if ( TextureFormatMatch( tf_this ) )
{
*lpStartFormat = d3dappi.NumTextureFormats;
LowestTexFormat = r+g+b ; // makes a note of the lowest bit depth so far...
}
else if( d3dappi.Driver[d3dappi.CurrDriver].bTransparency && !DontColourKey )
{
// Driver does do Colourkey Transparency...
if ( ( r+g+b < LowestTexFormat && r+g+b >= 15 && a == 0 ) ) // is this the lowest bit depth????...
{
*lpStartFormat = d3dappi.NumTextureFormats;
LowestTexFormat = r+g+b ; // makes a note of the lowest bit depth so far...
}
}else{
// Driver does not do Colour key so we need a texture format with some alpha control...
// or it messes up when we do colourkey...
if ( ( r+g+b <= LowestTexFormat && r+g+b >= 12 && a != 0 ) ) // is this the lowest bit depth????...
{
*lpStartFormat = d3dappi.NumTextureFormats;
LowestTexFormat = r+g+b ; // makes a note of the lowest bit depth so far...
}
}
}
}
d3dappi.NumTextureFormats++;
return DDENUMRET_OK;
}
/*
* D3DAppIEnumTextureFormats
* Get a list of available texture map formats from the Direct3D driver by
* enumeration. Choose a default format (paletted is prefered).
*/
BOOL
D3DAppIEnumTextureFormats(void)
{
int StartFormat;
/*
* Set the default format to -1 to let the callback know it's being
* called for the first time.
*/
StartFormat = -1;
d3dappi.NumTextureFormats = 0;
LastError =
d3dappi.lpD3DDevice->lpVtbl->EnumTextureFormats(d3dappi.lpD3DDevice,
EnumTextureFormatsCallback,
(LPVOID)&StartFormat);
if (LastError != DD_OK) {
D3DAppISetErrorString("Enumeration of texture formats failed.\n%s",
D3DAppErrorToString(LastError));
return FALSE;
}
memcpy(&d3dappi.ThisTextureFormat, &d3dappi.TextureFormat[StartFormat],
sizeof(D3DAppTextureFormat));
d3dappi.CurrTextureFormat = StartFormat;
TexturePalettized = d3dappi.ThisTextureFormat.bPalettized;
TextureRedBPP = d3dappi.ThisTextureFormat.RedBPP;
TextureGreenBPP = d3dappi.ThisTextureFormat.GreenBPP;
TextureBlueBPP = d3dappi.ThisTextureFormat.BlueBPP;
TextureAlphaBPP = d3dappi.ThisTextureFormat.AlphaBPP;
TextureIndexBPP = d3dappi.ThisTextureFormat.IndexBPP;
return TRUE;
}
/***************************************************************************/
/* Device creation */
/***************************************************************************/
/*
* D3DAppICreateDevice
* Create the D3D device and enumerate the texture formats
*/
BOOL
D3DAppICreateDevice(int driver)
{
RELEASE(d3dappi.lpD3DDevice);
if (d3dappi.Driver[driver].bIsHardware && !d3dappi.bBackBufferInVideo) {
D3DAppISetErrorString("Could not fit the rendering surfaces in video memory for this hardware device.\n");
goto exit_with_error;
}
d3dappi.CurrDriver = driver;
memcpy(&d3dappi.ThisDriver, &d3dappi.Driver[driver], sizeof(D3DAppD3DDriver));
LastError =
d3dappi.lpBackBuffer->lpVtbl->QueryInterface(d3dappi.lpBackBuffer,
&d3dappi.Driver[driver].Guid,
(LPVOID*)&d3dappi.lpD3DDevice);
if (LastError != DD_OK) {
D3DAppISetErrorString("Create D3D device failed.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
d3dappi.CurrDriver = driver;
d3dappi.NumTextureFormats = 0;
if (d3dappi.Driver[driver].bDoesTextures) {
if (!D3DAppIEnumTextureFormats())
goto exit_with_error;
}
return TRUE;
exit_with_error:
RELEASE(d3dappi.lpD3DDevice);
return FALSE;
}
/***************************************************************************/
/* Setting the render state */
/***************************************************************************/
/*
* D3DAppISetRenderState
* Create and execute an execute buffer which will set the render state and
* light state for the current viewport.
*/
static BOOL SetUpZBuf( DWORD type )
{
D3DEXECUTEBUFFERDESC debDesc;
D3DEXECUTEDATA d3dExData;
LPVOID lpBuffer, lpInsStart;
size_t size;
LPDIRECT3DEXECUTEBUFFER lpD3DExCmdBuf;
lpD3DExCmdBuf = NULL;
/*
* If there is no D3D Viewport, we must return true because it is not
* required.
*/
if (!d3dappi.lpD3DViewport)
return TRUE;
/*
* Create an execute buffer of the required size
*/
size = 0;
size += sizeof(D3DINSTRUCTION) * 3;
size += sizeof(D3DSTATE);
memset(&debDesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
debDesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
debDesc.dwFlags = D3DDEB_BUFSIZE;
debDesc.dwBufferSize = size;
LastError =
d3dappi.lpD3DDevice->lpVtbl->CreateExecuteBuffer(d3dappi.lpD3DDevice,
&debDesc, &lpD3DExCmdBuf, NULL);
if (LastError != D3D_OK) {
D3DAppISetErrorString("CreateExecuteBuffer failed in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
/*
* Lock the execute buffer so it can be filled
*/
LastError = lpD3DExCmdBuf->lpVtbl->Lock(lpD3DExCmdBuf, &debDesc);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Lock failed on execute buffer in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
memset(debDesc.lpData, 0, size);
lpInsStart = debDesc.lpData;
lpBuffer = lpInsStart;
/*
* Set render state
*/
OP_STATE_RENDER(1, lpBuffer);
STATE_DATA(D3DRENDERSTATE_ZFUNC, type, lpBuffer);
OP_EXIT(lpBuffer);
LastError = lpD3DExCmdBuf->lpVtbl->Unlock(lpD3DExCmdBuf);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Unlock failed on execute buffer in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
/*
* Set the execute data and exectue the buffer
*/
memset(&d3dExData, 0, sizeof(D3DEXECUTEDATA));
d3dExData.dwSize = sizeof(D3DEXECUTEDATA);
d3dExData.dwInstructionOffset = (ULONG) 0;
d3dExData.dwInstructionLength = (ULONG) ((char*)lpBuffer -
(char*)lpInsStart);
lpD3DExCmdBuf->lpVtbl->SetExecuteData(lpD3DExCmdBuf, &d3dExData);
LastError = d3dappi.lpD3DDevice->lpVtbl->Execute(d3dappi.lpD3DDevice,
lpD3DExCmdBuf,
d3dappi.lpD3DViewport,
D3DEXECUTE_UNCLIPPED);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Execute failed in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
RELEASE( lpD3DExCmdBuf );
return TRUE;
exit_with_error:
RELEASE( lpD3DExCmdBuf );
return FALSE;
}
extern BOOL ZClearsOn;
extern BOOL g_OddFrame;
BOOL SetZCompare( void )
{
if( !ZClearsOn && g_OddFrame )
{
if( !SetUpZBuf( D3DCMP_GREATEREQUAL ) )
return FALSE;
}else
{
if( !SetUpZBuf( D3DCMP_LESSEQUAL ) )
return FALSE;
}
return TRUE;
}
BOOL
D3DAppISetRenderState()
{
D3DEXECUTEBUFFERDESC debDesc;
D3DEXECUTEDATA d3dExData;
LPDIRECT3DEXECUTEBUFFER lpD3DExCmdBuf = NULL;
LPVOID lpBuffer, lpInsStart;
size_t size;
/*
* If there is no D3D Viewport, we must return true because it is not
* required.
*/
if (!d3dappi.lpD3DViewport)
return TRUE;
/*
* Create an execute buffer of the required size
*/
size = 0;
size += sizeof(D3DINSTRUCTION) * 3;
size += sizeof(D3DSTATE) * 20;
memset(&debDesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
debDesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
debDesc.dwFlags = D3DDEB_BUFSIZE;
debDesc.dwBufferSize = size;
LastError =
d3dappi.lpD3DDevice->lpVtbl->CreateExecuteBuffer(d3dappi.lpD3DDevice,
&debDesc, &lpD3DExCmdBuf, NULL);
if (LastError != D3D_OK) {
D3DAppISetErrorString("CreateExecuteBuffer failed in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
/*
* Lock the execute buffer so it can be filled
*/
LastError = lpD3DExCmdBuf->lpVtbl->Lock(lpD3DExCmdBuf, &debDesc);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Lock failed on execute buffer in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
memset(debDesc.lpData, 0, size);
lpInsStart = debDesc.lpData;
lpBuffer = lpInsStart;
/*
* Set render state
*/
OP_STATE_RENDER(17, lpBuffer);
STATE_DATA(D3DRENDERSTATE_SHADEMODE, d3dapprs.ShadeMode, lpBuffer);
STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, d3dapprs.bPerspCorrect,
lpBuffer);
STATE_DATA(D3DRENDERSTATE_ZENABLE, d3dapprs.bZBufferOn &&
d3dappi.ThisDriver.bDoesZBuffer, lpBuffer);
STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, d3dapprs.bZBufferOn, lpBuffer);
STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_LESS, lpBuffer);
#ifdef TRILINEAR_MENU_OPTION
STATE_DATA(D3DRENDERSTATE_TEXTUREMAG, d3dapprs.TextureFilter,lpBuffer);
STATE_DATA(D3DRENDERSTATE_TEXTUREMIN, d3dapprs.TextureFilter,lpBuffer);
#else
if( !MipMap )
{
STATE_DATA(D3DRENDERSTATE_TEXTUREMAG, d3dapprs.TextureFilter,lpBuffer);
STATE_DATA(D3DRENDERSTATE_TEXTUREMIN, d3dapprs.TextureFilter,lpBuffer);
}else if( !Is3Dfx2 && !TriLinear )
{
STATE_DATA(D3DRENDERSTATE_TEXTUREMAG, D3DFILTER_MIPLINEAR,lpBuffer);
STATE_DATA(D3DRENDERSTATE_TEXTUREMIN, D3DFILTER_MIPLINEAR,lpBuffer);
}else{
STATE_DATA(D3DRENDERSTATE_TEXTUREMAG, D3DFILTER_LINEARMIPLINEAR,lpBuffer);
STATE_DATA(D3DRENDERSTATE_TEXTUREMIN, D3DFILTER_LINEARMIPLINEAR,lpBuffer);
}
#endif
STATE_DATA(D3DRENDERSTATE_TEXTUREMAPBLEND, d3dapprs.TextureBlend,
lpBuffer);
STATE_DATA(D3DRENDERSTATE_FILLMODE, d3dapprs.FillMode, lpBuffer);
STATE_DATA(D3DRENDERSTATE_DITHERENABLE, d3dapprs.bDithering, lpBuffer);
STATE_DATA(D3DRENDERSTATE_SPECULARENABLE, d3dapprs.bSpecular,lpBuffer);
STATE_DATA(D3DRENDERSTATE_ANTIALIAS, d3dapprs.bAntialiasing, lpBuffer);
STATE_DATA(D3DRENDERSTATE_FOGENABLE, d3dapprs.bFogEnabled, lpBuffer);
STATE_DATA(D3DRENDERSTATE_FOGCOLOR, d3dapprs.FogColor, lpBuffer);
STATE_DATA(D3DRENDERSTATE_FOGTABLEMODE , D3DFOG_LINEAR , lpBuffer);
STATE_DATA(D3DRENDERSTATE_FOGTABLESTART , *(unsigned long*)&d3dapprs.FogStart , lpBuffer);
STATE_DATA(D3DRENDERSTATE_FOGTABLEEND , *(unsigned long*)&d3dapprs.FogEnd , lpBuffer);
#if 0
/*
* Set light state
*/
OP_STATE_LIGHT(3, lpBuffer);
STATE_DATA(D3DLIGHTSTATE_FOGMODE, d3dapprs.bFogEnabled ?
d3dapprs.FogMode : D3DFOG_NONE, lpBuffer);
STATE_DATA(D3DLIGHTSTATE_FOGSTART, *(unsigned long*)&d3dapprs.FogStart,
lpBuffer);
STATE_DATA(D3DLIGHTSTATE_FOGEND, *(unsigned long*)&d3dapprs.FogEnd,
lpBuffer);
#endif
OP_EXIT(lpBuffer);
LastError = lpD3DExCmdBuf->lpVtbl->Unlock(lpD3DExCmdBuf);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Unlock failed on execute buffer in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
/*
* Set the execute data and exectue the buffer
*/
memset(&d3dExData, 0, sizeof(D3DEXECUTEDATA));
d3dExData.dwSize = sizeof(D3DEXECUTEDATA);
d3dExData.dwInstructionOffset = (ULONG) 0;
d3dExData.dwInstructionLength = (ULONG) ((char*)lpBuffer -
(char*)lpInsStart);
lpD3DExCmdBuf->lpVtbl->SetExecuteData(lpD3DExCmdBuf, &d3dExData);
LastError = d3dappi.lpD3DDevice->lpVtbl->BeginScene(d3dappi.lpD3DDevice);
if (LastError != D3D_OK) {
D3DAppISetErrorString("BeginScene failed in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
LastError = d3dappi.lpD3DDevice->lpVtbl->Execute(d3dappi.lpD3DDevice,
lpD3DExCmdBuf,
d3dappi.lpD3DViewport,
D3DEXECUTE_UNCLIPPED);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Execute failed in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
LastError = d3dappi.lpD3DDevice->lpVtbl->EndScene(d3dappi.lpD3DDevice);
if (LastError != D3D_OK) {
D3DAppISetErrorString("EndScene failed in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
/*
* We are done with the execute buffer, so release it.
*/
lpD3DExCmdBuf->lpVtbl->Release(lpD3DExCmdBuf);
return TRUE;
exit_with_error:
RELEASE(lpD3DExCmdBuf);
return FALSE;
}
/***************************************************************************/
/* Setting the render state */
/***************************************************************************/
/*
* D3DAppISetRenderState
* Create and execute an execute buffer which will set the render state and
* light state for the current viewport.
*/
BOOL FogOn( float Start , float End )
{
D3DEXECUTEBUFFERDESC debDesc;
D3DEXECUTEDATA d3dExData;
LPDIRECT3DEXECUTEBUFFER lpD3DExCmdBuf = NULL;
LPVOID lpBuffer, lpInsStart;
size_t size;
long lStart;
long lEnd;
lStart = (long)Start;
lEnd = (long)End;
/*
* If there is no D3D Viewport, we must return true because it is not
* required.
*/
if (!d3dappi.lpD3DViewport)
return TRUE;
/*
* Create an execute buffer of the required size
*/
size = 0;
size += sizeof(D3DINSTRUCTION) * 3;
size += sizeof(D3DSTATE) * 20;
memset(&debDesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
debDesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
debDesc.dwFlags = D3DDEB_BUFSIZE;
debDesc.dwBufferSize = size;
LastError =
d3dappi.lpD3DDevice->lpVtbl->CreateExecuteBuffer(d3dappi.lpD3DDevice,
&debDesc, &lpD3DExCmdBuf, NULL);
if (LastError != D3D_OK) {
D3DAppISetErrorString("CreateExecuteBuffer failed in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
/*
* Lock the execute buffer so it can be filled
*/
LastError = lpD3DExCmdBuf->lpVtbl->Lock(lpD3DExCmdBuf, &debDesc);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Lock failed on execute buffer in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
memset(debDesc.lpData, 0, size);
lpInsStart = debDesc.lpData;
lpBuffer = lpInsStart;
/*
* Set render state
*/
OP_STATE_RENDER(2, lpBuffer);
STATE_DATA(D3DRENDERSTATE_FOGENABLE, 1, lpBuffer);
STATE_DATA(D3DRENDERSTATE_FOGCOLOR, d3dapprs.FogColor, lpBuffer);
// STATE_DATA(D3DRENDERSTATE_FOGTABLEMODE , D3DFOG_LINEAR , lpBuffer);
// STATE_DATA(D3DRENDERSTATE_FOGTABLESTART , *(unsigned long*)&lStart , lpBuffer);
// STATE_DATA(D3DRENDERSTATE_FOGTABLEEND , *(unsigned long*)&lEnd , lpBuffer);
#if 1
/*
* Set light state
*/
OP_STATE_LIGHT(3, lpBuffer);
STATE_DATA(D3DLIGHTSTATE_FOGMODE, D3DFOG_LINEAR , lpBuffer);
STATE_DATA(D3DLIGHTSTATE_FOGSTART, *(unsigned long*)&lStart,
lpBuffer);
STATE_DATA(D3DLIGHTSTATE_FOGEND, *(unsigned long*)&lEnd,
lpBuffer);
#endif
OP_EXIT(lpBuffer);
LastError = lpD3DExCmdBuf->lpVtbl->Unlock(lpD3DExCmdBuf);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Unlock failed on execute buffer in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
/*
* Set the execute data and exectue the buffer
*/
memset(&d3dExData, 0, sizeof(D3DEXECUTEDATA));
d3dExData.dwSize = sizeof(D3DEXECUTEDATA);
d3dExData.dwInstructionOffset = (ULONG) 0;
d3dExData.dwInstructionLength = (ULONG) ((char*)lpBuffer -
(char*)lpInsStart);
lpD3DExCmdBuf->lpVtbl->SetExecuteData(lpD3DExCmdBuf, &d3dExData);
// LastError = d3dappi.lpD3DDevice->lpVtbl->BeginScene(d3dappi.lpD3DDevice);
// if (LastError != D3D_OK) {
// D3DAppISetErrorString("BeginScene failed in SetRenderState.\n%s",
// D3DAppErrorToString(LastError));
// goto exit_with_error;
// }
LastError = d3dappi.lpD3DDevice->lpVtbl->Execute(d3dappi.lpD3DDevice,
lpD3DExCmdBuf,
d3dappi.lpD3DViewport,
D3DEXECUTE_UNCLIPPED);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Execute failed in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
// LastError = d3dappi.lpD3DDevice->lpVtbl->EndScene(d3dappi.lpD3DDevice);
// if (LastError != D3D_OK) {
// D3DAppISetErrorString("EndScene failed in SetRenderState.\n%s",
// D3DAppErrorToString(LastError));
// goto exit_with_error;
// }
/*
* We are done with the execute buffer, so release it.
*/
lpD3DExCmdBuf->lpVtbl->Release(lpD3DExCmdBuf);
return TRUE;
exit_with_error:
RELEASE(lpD3DExCmdBuf);
return FALSE;
}
/***************************************************************************/
/* Setting the render state */
/***************************************************************************/
/*
* D3DAppISetRenderState
* Create and execute an execute buffer which will set the render state and
* light state for the current viewport.
*/
BOOL FogOff( void )
{
D3DEXECUTEBUFFERDESC debDesc;
D3DEXECUTEDATA d3dExData;
LPDIRECT3DEXECUTEBUFFER lpD3DExCmdBuf = NULL;
LPVOID lpBuffer, lpInsStart;
size_t size;
/*
* If there is no D3D Viewport, we must return true because it is not
* required.
*/
if (!d3dappi.lpD3DViewport)
return TRUE;
/*
* Create an execute buffer of the required size
*/
size = 0;
size += sizeof(D3DINSTRUCTION) * 3;
size += sizeof(D3DSTATE) * 20;
memset(&debDesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
debDesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
debDesc.dwFlags = D3DDEB_BUFSIZE;
debDesc.dwBufferSize = size;
LastError =
d3dappi.lpD3DDevice->lpVtbl->CreateExecuteBuffer(d3dappi.lpD3DDevice,
&debDesc, &lpD3DExCmdBuf, NULL);
if (LastError != D3D_OK) {
D3DAppISetErrorString("CreateExecuteBuffer failed in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
/*
* Lock the execute buffer so it can be filled
*/
LastError = lpD3DExCmdBuf->lpVtbl->Lock(lpD3DExCmdBuf, &debDesc);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Lock failed on execute buffer in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
memset(debDesc.lpData, 0, size);
lpInsStart = debDesc.lpData;
lpBuffer = lpInsStart;
/*
* Set render state
*/
OP_STATE_RENDER(1, lpBuffer);
STATE_DATA(D3DRENDERSTATE_FOGENABLE, 0, lpBuffer);
OP_EXIT(lpBuffer);
LastError = lpD3DExCmdBuf->lpVtbl->Unlock(lpD3DExCmdBuf);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Unlock failed on execute buffer in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
/*
* Set the execute data and exectue the buffer
*/
memset(&d3dExData, 0, sizeof(D3DEXECUTEDATA));
d3dExData.dwSize = sizeof(D3DEXECUTEDATA);
d3dExData.dwInstructionOffset = (ULONG) 0;
d3dExData.dwInstructionLength = (ULONG) ((char*)lpBuffer -
(char*)lpInsStart);
lpD3DExCmdBuf->lpVtbl->SetExecuteData(lpD3DExCmdBuf, &d3dExData);
// LastError = d3dappi.lpD3DDevice->lpVtbl->BeginScene(d3dappi.lpD3DDevice);
// if (LastError != D3D_OK) {
// D3DAppISetErrorString("BeginScene failed in SetRenderState.\n%s",
// D3DAppErrorToString(LastError));
// goto exit_with_error;
// }
LastError = d3dappi.lpD3DDevice->lpVtbl->Execute(d3dappi.lpD3DDevice,
lpD3DExCmdBuf,
d3dappi.lpD3DViewport,
D3DEXECUTE_UNCLIPPED);
if (LastError != D3D_OK) {
D3DAppISetErrorString("Execute failed in SetRenderState.\n%s",
D3DAppErrorToString(LastError));
goto exit_with_error;
}
// LastError = d3dappi.lpD3DDevice->lpVtbl->EndScene(d3dappi.lpD3DDevice);
// if (LastError != D3D_OK) {
// D3DAppISetErrorString("EndScene failed in SetRenderState.\n%s",
// D3DAppErrorToString(LastError));
// goto exit_with_error;
// }
/*
* We are done with the execute buffer, so release it.
*/
lpD3DExCmdBuf->lpVtbl->Release(lpD3DExCmdBuf);
return TRUE;
exit_with_error:
RELEASE(lpD3DExCmdBuf);
return FALSE;
}