forked from ForsakenX/forsaken
/
render_d3d.cpp
1647 lines (1358 loc) · 39.3 KB
/
render_d3d.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
#ifndef OPENGL
#define DIRECT3D_VERSION 0x0900
#include "main.h"
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <string.h>
#include <search.h>
#include <d3d9.h>
#include <d3dx9.h>
extern "C" {
#include "render.h"
#include "tload.h"
#include <assert.h>
#include "util.h"
#include <dxerr.h>
#include "file.h"
#include "texture.h"
#define OPAQUE_BLACK 0xFF000000
#define D3DFVF_TLVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)
#define D3DFVF_LVERTEX D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1
#define STATE( K, V ) \
lpD3DDevice->SetRenderState( K, V );
#define TSTATE( N, K, V ) \
lpD3DDevice->SetTextureStageState( N, K, V );
#define SSTATE( N, K, V ) \
lpD3DDevice->SetSamplerState( N, K, V );
// prototypes
static BOOL init_render_states( render_info_t * info );
uchar_t gamma_table[256];
void build_gamma_table( double gamma )
{
double k;
int i;
#ifndef DEBUG_ON
if (gamma <= 0)
gamma = 1.0;
#endif
k = 255.0/pow(255.0, 1.0/gamma);
for (i = 0; i <= 255; i++)
{
gamma_table[i] = (uchar_t)(k*(pow((double)i, 1.0/gamma)));
if( i && !gamma_table[i] )
gamma_table[i] = 1;
}
}
LPDIRECT3D9 lpD3D; /* D3D interface object */
LPDIRECT3DDEVICE9 lpD3DDevice; /* D3D device */
static int bpp = 16;
// TODO - shouldn't sdl handle the window and resolution enumeration ?
BOOL render_init( render_info_t * info )
{
HRESULT LastError;
render_viewport_t viewport;
// should get this from d3d caps
bSquareOnly = FALSE;
// default gamma table
build_gamma_table(1.0);
// Set up Direct3D interface object
lpD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (!lpD3D)
{
DebugPrintf("couldnt create d3d9\n");
return FALSE;
}
// create d3d device
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory (&d3dpp, sizeof(d3dpp));
//
d3dpp.Windowed = !info->fullscreen;
//
// presentation settings
//
d3dpp.hDeviceWindow = GetActiveWindow(); // the window handle
d3dpp.BackBufferCount = 1; // we only have one swap chain
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // does not protect the contents of the backbuffer after flipping (faster)
// shouldn't we specify D3DSWAPEFFECT_FLIP ?
// wouldn't D3DSWAPEFFECT_OVERLAY be fastest ?
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; // display refresh
d3dpp.EnableAutoDepthStencil = TRUE; // let d3d manage the z-buffer
if(info->vsync)
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // enable vsync
else
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // disable vsync
// 16 bit zbuffer
//d3dpp.AutoDepthStencilFormat = D3DFMT_D15S1; // 16 bit depth buffer with 1 bit stencil
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // 16 bit depth buffer
// 32 bit back buffer
// Also supports 32 bit zbuffer
if( SUCCEEDED(lpD3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, d3dpp.Windowed)))
{
bpp = 32;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
//d3dpp.AutoDepthStencilFormat = D3DFMT_D32; // 32 bit depth buffer
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; // 24 bit depth buffer with 8 bit stencil buffer
DebugPrintf("picked 24 bit D3DFMT_X8R8G8B8 back buffer\n");
}
// 16 bit
else if(SUCCEEDED(lpD3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X1R5G5B5, D3DFMT_X1R5G5B5, d3dpp.Windowed)))
{
d3dpp.BackBufferFormat = D3DFMT_X1R5G5B5;
DebugPrintf("picked 16 bit D3DFMT_X1R5G5B5 back buffer\n");
}
// 16 bit
else if(SUCCEEDED(lpD3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_R5G6B5, D3DFMT_R5G6B5, d3dpp.Windowed)))
{
d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
DebugPrintf("picked 16 bit D3DFMT_R5G6B5 back buffer\n");
}
// failed
else
{
CloseWindow(d3dpp.hDeviceWindow);
Msg("Failed to find a suitable back buffer format");
exit(1);
}
//
// Enumerates display modes
// picking info->default_mode if it exists
// or picking the biggest mode possible :]
//
#if 0 // done by sdl now
{
int mode = 0;
int desired_mode = -1;
int best_mode = 0; // default to the first mode
int i;
int x = 0;
int count = (int) lpD3D->GetAdapterModeCount( D3DADAPTER_DEFAULT, d3dpp.BackBufferFormat );
info->Mode = (render_display_mode_t *) malloc( count * sizeof(render_display_mode_t) );
D3DDISPLAYMODE * modes = (D3DDISPLAYMODE *) malloc( count * sizeof(D3DDISPLAYMODE) );
for ( i = 0; i < count; i++ )
{
// get the mode description
lpD3D->EnumAdapterModes( D3DADAPTER_DEFAULT, d3dpp.BackBufferFormat, i, &modes[i] );
DebugPrintf("Enumerated mode: %dx%d @ %dhz , format=%d\n",
modes[i].Width,modes[i].Height,modes[i].RefreshRate,modes[i].Format);
// ignore modes under 640x480
if(modes[i].Width < 640 || modes[i].Height < 480)
{
DebugPrintf("Skipping mode because it's to small for anyone to want...\n");
continue;
}
// save the mode
info->Mode[x].h = modes[i].Height;
info->Mode[x].w = modes[i].Width;
info->Mode[x].bpp = bpp;
// if this is the mode the user wanted pick it
if( info->Mode[x].w == info->default_mode.w &&
info->Mode[x].h == info->default_mode.h )
{
desired_mode = x;
}
// smallest mode as default
if( info->Mode[x].w < info->Mode[best_mode].w &&
info->Mode[x].h < info->Mode[best_mode].h )
{
best_mode = x;
}
// biggest mode by width as default
//if(info->Mode[x].w > info->Mode[best_mode].w )
// best_mode = x;
// 800x600 @ 60 as default
//if( info->Mode[x].w == 800 && info->Mode[x].h == 600 )
// desired_mode = x;
// go to next storage location
x++;
}
info->NumModes = x;
if( desired_mode >= 0 )
{
mode = desired_mode;
}
else
{
mode = best_mode;
}
info->CurrMode = mode;
info->ThisMode = info->Mode[ info->CurrMode ];
info->WindowsDisplay = info->Mode[ info->CurrMode ];
info->window_size.cx = info->ThisMode.w;
info->window_size.cy = info->ThisMode.h;
info->WindowsDisplay.w = info->ThisMode.w;
info->WindowsDisplay.h = info->ThisMode.h;
free(modes);
}
#endif // done by sdl now
d3dpp.BackBufferWidth = info->ThisMode.w;
d3dpp.BackBufferHeight = info->ThisMode.h;
info->aspect_ratio = (float) info->ThisMode.w / (float) info->ThisMode.h;
DebugPrintf("Using display mode: %dx%d\n",
info->ThisMode.w,info->ThisMode.h);
// try to create a device falling back to less capable versions
//
// TODO
// the docs say that pure devices don't support Get* calls
// which explains why people are crashing on FSGet* functions for no reason
// to support pure device we'd have to track our own states
//
/*
LastError = lpD3D->CreateDevice(
D3DADAPTER_DEFAULT, // the default video card
D3DDEVTYPE_HAL, // device type - hal = Hardware rasterization
d3dpp.hDeviceWindow, // the window handle
// these define how the device is created
D3DCREATE_HARDWARE_VERTEXPROCESSING | // do vertex processing in hardware
D3DCREATE_PUREDEVICE, // D3D will not support Get* calls for anything that can be stored in state blocks.
// and not to provide any emulation services for vertex processing.
// Thus if the device does not support vertex processing,
// then the application can use only post-transformed vertices.
&d3dpp, // presentation parameters defined above
&lpD3DDevice // pointer that will contain the returned device
);
if (SUCCEEDED(LastError))
{
Msg("pure");
}
if (FAILED(LastError))
*/
{
LastError = lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dpp.hDeviceWindow,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &lpD3DDevice);
if (SUCCEEDED(LastError))
{
DebugPrintf("d3d device created: hardware\n");
}
else
{
const char * error = render_error_description(LastError);
DebugPrintf("d3d device create failed with hardware: %s\n",error);
}
}
if (FAILED(LastError))
{
LastError = lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dpp.hDeviceWindow,
D3DCREATE_MIXED_VERTEXPROCESSING, // do vertex processing in both hardware and software
&d3dpp, &lpD3DDevice);
if (SUCCEEDED(LastError))
{
DebugPrintf("d3d device created: mixed\n");
}
else
{
const char * error = render_error_description(LastError);
DebugPrintf("d3d device create failed with mixed: %s\n",error);
}
}
if (FAILED(LastError))
{
LastError = lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dpp.hDeviceWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, // do vertex processing in software only
&d3dpp, &lpD3DDevice);
if (SUCCEEDED(LastError))
{
DebugPrintf("d3d device created: software\n");
}
else
{
const char * error = render_error_description(LastError);
DebugPrintf("d3d device create failed with software: %s\n",error);
}
}
if (FAILED(LastError))
{
CloseWindow(d3dpp.hDeviceWindow);
Msg("Failed to create a suitable d3d device:\n%s",
render_error_description(LastError));
exit(1);
}
info->ok_to_render = TRUE;
viewport.X = 0;
viewport.Y = 0;
/* do "after device created" stuff */
ZeroMemory( &viewport, sizeof(viewport) );
{
WINDOWPLACEMENT placement;
placement.length = sizeof(WINDOWPLACEMENT);
if(GetWindowPlacement( d3dpp.hDeviceWindow, &placement ))
{
viewport.X = placement.rcNormalPosition.left;
viewport.Y = placement.rcNormalPosition.top;
}
}
viewport.Width = d3dpp.BackBufferWidth;
viewport.Height = d3dpp.BackBufferHeight;
viewport.MinZ = 0.0f;
viewport.MaxZ = 1.0f;
LastError = FSSetViewPort(&viewport);
if (!LastError)
{
DebugPrintf("couldn't set viewport\n");
}
if(!init_render_states( info ))
return FALSE;
return TRUE;
}
void render_cleanup( render_info_t * info )
{
info->ok_to_render = FALSE;
RELEASE(lpD3DDevice);
RELEASE(lpD3D);
}
extern BOOL sdl_init_video( void );
BOOL render_mode_select( render_info_t * info )
{
render_cleanup( info );
if(!sdl_init_video())
return FALSE;
//if(!render_init( info ))
// return FALSE;
return TRUE;
}
// If the device can be restored,
// the application prepares the device by destroying all video-memory
// resources and any swap chains.
// Then, the application calls the IDirect3DDevice9::Reset method.
// Reset is the only method that has an effect when a device is lost,
// and is the only method by which an application can change the device
// from a lost to an operational state.
// Reset will fail unless the application releases all resources that
// are allocated in D3DPOOL_DEFAULT, including those created by the
// The application can determine what to do on encountering a lost device by querying the return
// value of the IDirect3DDevice9::TestCooperativeLevel method.
// All video memory must be released before a device can be reset from a lost state to an operational state.
// This means that the application should release any resources placed in the D3DPOOL_DEFAULT memory class.
// TODO - right now we just call render_reset which creates a new device
// * does the things we didn't release get released when the device gets destroyed?
// * fullscreen to window mode is completely broken
static BOOL needs_reset = FALSE;
BOOL render_reset( render_info_t * info )
{
if(!needs_reset)
return FALSE;
if(!render_mode_select( info ))
return FALSE;
needs_reset = FALSE;
return TRUE;
}
// TODO - check caps
// This functionality is available if the D3DPMISCCAPS_COLORWRITEENABLE
// capabilities bit is set in the PrimitiveMiscCaps member of the >D3DCAPS8
// structure for the device.
void render_set_filter( BOOL red, BOOL green, BOOL blue )
{
DWORD flags = (0 | D3DCOLORWRITEENABLE_ALPHA);
if(blue)
flags |= D3DCOLORWRITEENABLE_BLUE;
if(green)
flags |= D3DCOLORWRITEENABLE_GREEN;
if(red)
flags |= D3DCOLORWRITEENABLE_RED;
STATE( D3DRS_COLORWRITEENABLE, flags );
}
BOOL render_flip( render_info_t * info )
{
HRESULT hr;
if (!info->ok_to_render)
{
DebugPrintf("Cannot call render_flip() while ok_to_render is FALSE.\n");
return FALSE;
}
hr = lpD3DDevice->Present(NULL, NULL, NULL, NULL);
if(FAILED(hr))
{
DebugPrintf("render_flip Error: %s\n",render_error_description(hr));
switch(hr)
{
case D3DERR_DEVICELOST:
needs_reset = TRUE;
break;
case D3DERR_DEVICENOTRESET:
DebugPrintf("render_flip Stub: Device needs to be reset.\n");
break;
}
}
return TRUE;
}
// c helper
void render_state( D3DRENDERSTATETYPE type, int val )
{
lpD3DDevice->SetRenderState( type, val );
}
void set_trans_state_3( void )
{
STATE(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
STATE(D3DRS_DESTBLEND, D3DBLEND_SRCALPHA);
}
void set_trans_state_2( void )
{
STATE(D3DRS_SRCBLEND, D3DBLEND_ONE);
STATE(D3DRS_DESTBLEND, D3DBLEND_ONE);
}
void set_trans_state_9( void )
{
STATE(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
STATE(D3DRS_DESTBLEND, D3DBLEND_ONE);
}
void set_trans_state_5( void )
{
STATE(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
STATE(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
}
// transparency tester
void render_state_trans( void )
{
static int state = 0;
static int count = 0;
/*
D3DBLEND_ZERO = 1,
D3DBLEND_ONE = 2,
D3DBLEND_SRCCOLOR = 3,
D3DBLEND_INVSRCCOLOR = 4,
D3DBLEND_SRCALPHA = 5,
D3DBLEND_INVSRCALPHA = 6,
D3DBLEND_DESTALPHA = 7,
D3DBLEND_INVDESTALPHA = 8,
D3DBLEND_DESTCOLOR = 9,
D3DBLEND_INVDESTCOLOR = 10,
D3DBLEND_SRCALPHASAT = 11,
D3DBLEND_BOTHSRCALPHA = 12,
D3DBLEND_BOTHINVSRCALPHA = 13,
*/
// set the state
switch( state )
{
case 0:
STATE(D3DRS_SRCBLEND, D3DBLEND_ZERO);
STATE(D3DRS_DESTBLEND, D3DBLEND_ONE);
break;
case 1:
STATE(D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR);
STATE(D3DRS_DESTBLEND, D3DBLEND_INVDESTCOLOR);
break;
case 2:
STATE(D3DRS_SRCBLEND, D3DBLEND_ONE);
STATE(D3DRS_DESTBLEND, D3DBLEND_ONE);
break;
case 3:
STATE(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
STATE(D3DRS_DESTBLEND, D3DBLEND_SRCALPHA);
break;
case 4:
STATE(D3DRS_SRCBLEND, D3DBLEND_INVSRCALPHA);
STATE(D3DRS_DESTBLEND, D3DBLEND_SRCALPHA);
break;
case 5: // normal trans
STATE(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
STATE(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
break;
// new modes
case 6:
STATE(D3DRS_SRCBLEND, D3DBLEND_ONE);
STATE(D3DRS_DESTBLEND, D3DBLEND_ZERO);
break;
case 7: // color trans
STATE(D3DRS_SRCBLEND, D3DBLEND_ZERO);
STATE(D3DRS_DESTBLEND, D3DBLEND_SRCCOLOR);
break;
case 8: // inv color trans
STATE(D3DRS_SRCBLEND, D3DBLEND_ZERO);
STATE(D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR);
break;
case 9: // glowing
STATE(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
STATE(D3DRS_DESTBLEND, D3DBLEND_ONE);
break;
case 10: // darkening color
STATE(D3DRS_SRCBLEND, D3DBLEND_INVDESTCOLOR);
STATE(D3DRS_DESTBLEND, D3DBLEND_ZERO);
break;
}
DebugPrintf("trans state = %d ...... count %d\n",state,count);
//render_flip();
// try the next state
count++;
if( count > 100 )
{
count = 0;
state++;
if( state > 10 )
state = 0;
}
}
// set transparency off
void reset_trans( void )
{
STATE( D3DRS_ALPHABLENDENABLE, FALSE );
STATE( D3DRS_SRCBLEND, D3DBLEND_ONE);
STATE( D3DRS_DESTBLEND, D3DBLEND_ZERO);
}
void reset_zbuff( void )
{
STATE( D3DRS_ZENABLE, D3DZB_TRUE);
STATE( D3DRS_ZWRITEENABLE, TRUE);
STATE( D3DRS_ZFUNC, D3DCMP_LESS);
}
void disable_zbuff_write( void )
{
STATE( D3DRS_ZWRITEENABLE, FALSE);
}
void disable_zbuff( void )
{
STATE( D3DRS_ZENABLE, D3DZB_FALSE);
}
void linear_filtering( void )
{
SSTATE(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
SSTATE(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
}
void anisotropic_filtering( void )
{
// Compensates for distortion caused by the difference
// in angle between the texture polygon and the plane
// of the screen.
SSTATE(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC );
SSTATE(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC );
}
void disable_filtering( void )
{
SSTATE(0, D3DSAMP_MAGFILTER, D3DTEXF_NONE );
SSTATE(0, D3DSAMP_MINFILTER, D3DTEXF_NONE );
}
// d3d default filtering settings
void default_filtering( void ){
SSTATE(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
SSTATE(0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
}
// reset to our default filtering modes
void reset_filtering( void )
{
anisotropic_filtering();
// D3DSAMP_MIPFILTER
// Mipmap filter to use during minification.
// When used with D3DSAMP_MIPFILTER,
// enables mipmapping and trilinear filtering;
// it specifies that the rasterizer interpolates
// between the two nearest mip levels.
SSTATE(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
}
void cull_none( void )
{
STATE( D3DRS_CULLMODE, D3DCULL_NONE);
}
void cull_cw( void )
{
STATE( D3DRS_CULLMODE, D3DCULL_CW);
}
void reset_cull( void )
{
STATE( D3DRS_CULLMODE, D3DCULL_CCW);
}
void set_alpha_ignore( void )
{
STATE( D3DRS_ALPHATESTENABLE, TRUE);
STATE( D3DRS_ALPHAREF, (DWORD)100 );
STATE( D3DRS_ALPHAFUNC, D3DCMP_GREATER);
}
void unset_alpha_ignore( void )
{
STATE( D3DRS_ALPHATESTENABLE, FALSE);
}
// old lpD3DNormCmdBuf
void set_normal_states( void )
{
reset_zbuff();
reset_trans();
// need to find equivalants
//STATE( D3DRS_WRAPU, FALSE );
//STATE( D3DRS_WRAPV, FALSE );
//STATE( D3DRS_STIPPLEDALPHA, FALSE );
//STATE( D3DRS_TEXTUREMAPBLEND, D3DTBLEND_MODULATE );
}
// old lpD3DTransCmdBuf
void set_alpha_states( void )
{
disable_zbuff_write();
STATE( D3DRS_ALPHABLENDENABLE, TRUE );
set_trans_state_9();
}
// old lpD3DSpcFxTransCmdBuf
void set_whiteout_state( void )
{
disable_zbuff_write();
STATE( D3DRS_ALPHABLENDENABLE, TRUE );
set_trans_state_9();
}
/* bjd - CHECK
// game attempted to use these modes in order
// first one that was fully supported was used
mode D3DPTBLENDCAPS_MODULATE = D3DTBLEND_MODULATE;
src D3DPBLENDCAPS_ONE = D3DBLEND_ONE;
dest D3DPBLENDCAPS_ONE = D3DBLEND_ONE;
mode D3DPTBLENDCAPS_MODULATEALPHA = D3DTBLEND_MODULATEALPHA;
src D3DPBLENDCAPS_SRCALPHA = D3DBLEND_SRCALPHA;
dest D3DPBLENDCAPS_SRCALPHA = D3DBLEND_SRCALPHA;
mode D3DPTBLENDCAPS_ADD = D3DTBLEND_ADD;
src D3DPBLENDCAPS_INVSRCALPHA = D3DBLEND_INVSRCALPHA;
dest D3DPBLENDCAPS_SRCALPHA = D3DBLEND_SRCALPHA;
mode D3DPTBLENDCAPS_MODULATEALPHA = D3DTBLEND_MODULATEALPHA;
src D3DPBLENDCAPS_SRCALPHA = D3DBLEND_SRCALPHA;
dest D3DPBLENDCAPS_INVSRCALPHA = D3DBLEND_INVSRCALPHA;
*/
static BOOL init_render_states( render_info_t * info )
{
//STATE( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
STATE( D3DRS_SHADEMODE, D3DSHADE_GOURAUD );
STATE( D3DRS_LIGHTING, FALSE);
if( bpp < 32 )
STATE( D3DRS_DITHERENABLE, TRUE );
reset_cull();
reset_trans();
reset_filtering();
set_normal_states();
return TRUE;
}
char buf[100];
int LastError;
// clears color/zbuff same time to opaque black
BOOL FSClear(XYRECT * rect)
{
return lpD3DDevice->Clear( ((rect) ? 1 : 0), (D3DRECT*)rect, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, OPAQUE_BLACK, 1.0f, 0 ) == D3D_OK;
}
BOOL FSClearDepth(XYRECT * rect)
{
return lpD3DDevice->Clear( ((rect) ? 1 : 0), (D3DRECT*)rect, D3DCLEAR_ZBUFFER, 0, 1.0f, 0 ) == D3D_OK;
}
BOOL FSClearBlack(void)
{
// TODO - does this function actually clear the zbuff/stencil ?
// this specifies values for zbuff/stencil but then states D3DCLEAR_TARGET
return lpD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, OPAQUE_BLACK, 1.0f, 0 ) == D3D_OK;
}
BOOL FSGetViewPort(render_viewport_t *returnViewPort)
{
return lpD3DDevice->GetViewport( (D3DVIEWPORT9*) returnViewPort ) == D3D_OK;
}
BOOL FSSetViewPort(render_viewport_t *newViewPort)
{
return lpD3DDevice->SetViewport( (D3DVIEWPORT9*) newViewPort ) == D3D_OK;
}
BOOL FSSetWorld( RENDERMATRIX *matrix )
{
return lpD3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)matrix) == D3D_OK;
}
BOOL FSSetProjection( RENDERMATRIX *matrix )
{
return lpD3DDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)matrix) == D3D_OK;
}
BOOL FSSetView( RENDERMATRIX *matrix )
{
return lpD3DDevice->SetTransform(D3DTS_VIEW, (D3DMATRIX*)matrix) == D3D_OK;
}
BOOL FSGetWorld(RENDERMATRIX *matrix)
{
return lpD3DDevice->GetTransform(D3DTS_WORLD, (D3DMATRIX*)matrix) == D3D_OK;
}
BOOL FSBeginScene( void )
{
return lpD3DDevice->BeginScene() == D3D_OK;
}
BOOL FSEndScene( void )
{
return lpD3DDevice->EndScene() == D3D_OK;
}
void save_texture( char * path, LPTEXTURE texture )
{
#ifndef __WINE__
D3DXSaveTextureToFile(path, D3DXIFF_PNG, (LPDIRECT3DTEXTURE9)texture, 0);
#endif
}
const char * d3d_image_file_formats[] =
{
"D3DXIFF_BMP",
"D3DXIFF_JPG",
"D3DXIFF_TGA",
"D3DXIFF_PNG",
"D3DXIFF_DDS",
"D3DXIFF_PPM",
"D3DXIFF_DIB",
"D3DXIFF_HDR",
"D3DXIFF_PFM",
};
const char * d3d_format( D3DFORMAT format )
{
switch(format)
{
case D3DFMT_UNKNOWN:
return "D3DFMT_UNKNOWN";
break;
case D3DFMT_R8G8B8:
return "D3DFMT_R8G8B8";
break;
case D3DFMT_A8R8G8B8:
return "D3DFMT_A8R8G8B8";
break;
case D3DFMT_X8R8G8B8:
return "D3DFMT_X8R8G8B8";
break;
case D3DFMT_R5G6B5:
return "D3DFMT_R5G6B5";
break;
case D3DFMT_X1R5G5B5:
return "D3DFMT_X1R5G5B5";
break;
case D3DFMT_A1R5G5B5:
return "D3DFMT_A1R5G5B5";
break;
case D3DFMT_A4R4G4B4:
return "D3DFMT_A4R4G4B4";
break;
case D3DFMT_R3G3B2:
return "D3DFMT_R3G3B2";
break;
case D3DFMT_A8:
return "D3DFMT_A8";
break;
case D3DFMT_A8R3G3B2:
return "D3DFMT_A8R3G3B2";
break;
case D3DFMT_X4R4G4B4:
return "D3DFMT_X4R4G4B4";
break;
case D3DFMT_A2B10G10R10:
return "D3DFMT_A2B10G10R10";
break;
case D3DFMT_A8B8G8R8:
return "D3DFMT_A8B8G8R8";
break;
case D3DFMT_X8B8G8R8:
return "D3DFMT_X8B8G8R8";
break;
case D3DFMT_G16R16:
return "D3DFMT_G16R16";
break;
case D3DFMT_A2R10G10B10:
return "D3DFMT_A2R10G10B10";
break;
case D3DFMT_A16B16G16R16:
return "D3DFMT_A16B16G16R16";
break;
case D3DFMT_A8P8:
return "D3DFMT_A8P8";
break;
case D3DFMT_P8:
return "D3DFMT_P8";
break;
case D3DFMT_L8:
return "D3DFMT_L8";
break;
case D3DFMT_A8L8:
return "D3DFMT_A8L8";
break;
case D3DFMT_A4L4:
return "D3DFMT_A4L4";
break;
case D3DFMT_V8U8:
return "D3DFMT_V8U8";
break;
case D3DFMT_L6V5U5:
return "D3DFMT_L6V5U5";
break;
case D3DFMT_X8L8V8U8:
return "D3DFMT_X8L8V8U8";
break;
case D3DFMT_Q8W8V8U8:
return "D3DFMT_Q8W8V8U8";
break;
case D3DFMT_V16U16:
return "D3DFMT_V16U16";
break;
case D3DFMT_A2W10V10U10:
return "D3DFMT_A2W10V10U10";
break;
case D3DFMT_UYVY:
return "D3DFMT_UYVY";
break;
case D3DFMT_R8G8_B8G8:
return "D3DFMT_R8G8_B8G8";
break;
case D3DFMT_YUY2:
return "D3DFMT_YUY2";
break;
case D3DFMT_G8R8_G8B8:
return "D3DFMT_G8R8_G8B8";
break;
case D3DFMT_DXT1:
return "D3DFMT_DXT1";
break;
case D3DFMT_DXT2:
return "D3DFMT_DXT2";
break;
case D3DFMT_DXT3:
return "D3DFMT_DXT3";
break;
case D3DFMT_DXT4:
return "D3DFMT_DXT4";
break;
case D3DFMT_DXT5:
return "D3DFMT_DXT5";
break;
case D3DFMT_D16_LOCKABLE:
return "D3DFMT_D16_LOCKABLE";
break;
case D3DFMT_D32:
return "D3DFMT_D32";
break;
case D3DFMT_D15S1:
return "D3DFMT_D15S1";
break;
case D3DFMT_D24S8:
return "D3DFMT_D24S8";
break;
case D3DFMT_D24X8:
return "D3DFMT_D24X8";
break;
case D3DFMT_D24X4S4:
return "D3DFMT_D24X4S4";
break;
case D3DFMT_D16:
return "D3DFMT_D16";
break;
case D3DFMT_D32F_LOCKABLE:
return "D3DFMT_D32F_LOCKABLE";
break;