-
Notifications
You must be signed in to change notification settings - Fork 522
/
fb_d3d9.cpp
3892 lines (3494 loc) · 94.7 KB
/
fb_d3d9.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
/*
** fb_d3d9.cpp
** Code to let ZDoom use Direct3D 9 as a simple framebuffer
**
**---------------------------------------------------------------------------
** Copyright 1998-2011 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
** This file does _not_ implement hardware-acclerated 3D rendering. It is
** just a means of getting the pixel data to the screen in a more reliable
** method on modern hardware by copying the entire frame to a texture,
** drawing that to the screen, and presenting.
**
** That said, it does implement hardware-accelerated 2D rendering.
*/
// HEADER FILES ------------------------------------------------------------
#ifdef _DEBUG
#define D3D_DEBUG_INFO
#endif
#define DIRECT3D_VERSION 0x0900
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <d3d9.h>
#include <stdio.h>
#define USE_WINDOWS_DWORD
#include "doomtype.h"
#include "c_dispatch.h"
#include "templates.h"
#include "i_system.h"
#include "i_video.h"
#include "i_input.h"
#include "v_video.h"
#include "v_pfx.h"
#include "stats.h"
#include "doomerrors.h"
#include "r_main.h"
#include "r_data/r_translate.h"
#include "f_wipe.h"
#include "sbar.h"
#include "win32iface.h"
#include "doomstat.h"
#include "v_palette.h"
#include "w_wad.h"
#include "r_data/colormaps.h"
#include "SkylineBinPack.h"
// MACROS ------------------------------------------------------------------
// The number of points for the vertex buffer.
#define NUM_VERTS 10240
// The number of indices for the index buffer.
#define NUM_INDEXES ((NUM_VERTS * 6) / 4)
// The number of quads we can batch together.
#define MAX_QUAD_BATCH (NUM_INDEXES / 6)
// The default size for a texture atlas.
#define DEF_ATLAS_WIDTH 512
#define DEF_ATLAS_HEIGHT 512
// TYPES -------------------------------------------------------------------
IMPLEMENT_CLASS(D3DFB, false, false)
struct D3DFB::PackedTexture
{
D3DFB::Atlas *Owner;
PackedTexture **Prev, *Next;
// Pixels this image covers
RECT Area;
// Texture coordinates for this image
float Left, Top, Right, Bottom;
// Texture has extra space on the border?
bool Padded;
};
struct D3DFB::Atlas
{
Atlas(D3DFB *fb, int width, int height, D3DFORMAT format);
~Atlas();
PackedTexture *AllocateImage(const Rect &rect, bool padded);
void FreeBox(PackedTexture *box);
SkylineBinPack Packer;
Atlas *Next;
IDirect3DTexture9 *Tex;
D3DFORMAT Format;
PackedTexture *UsedList; // Boxes that contain images
int Width, Height;
bool OneUse;
};
class D3DTex : public FNativeTexture
{
public:
D3DTex(FTexture *tex, D3DFB *fb, bool wrapping);
~D3DTex();
FTexture *GameTex;
D3DFB::PackedTexture *Box;
D3DTex **Prev;
D3DTex *Next;
bool IsGray;
bool Create(D3DFB *fb, bool wrapping);
bool Update();
bool CheckWrapping(bool wrapping);
D3DFORMAT GetTexFormat();
FTextureFormat ToTexFmt(D3DFORMAT fmt);
};
class D3DPal : public FNativePalette
{
public:
D3DPal(FRemapTable *remap, D3DFB *fb);
~D3DPal();
D3DPal **Prev;
D3DPal *Next;
IDirect3DTexture9 *Tex;
D3DCOLOR BorderColor;
bool DoColorSkip;
bool Update();
FRemapTable *Remap;
int RoundedPaletteSize;
};
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
void DoBlending (const PalEntry *from, PalEntry *to, int count, int r, int g, int b, int a);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
extern HWND Window;
extern IVideo *Video;
extern BOOL AppActive;
extern int SessionState;
extern bool VidResizing;
EXTERN_CVAR (Bool, fullscreen)
EXTERN_CVAR (Float, Gamma)
EXTERN_CVAR (Bool, vid_vsync)
EXTERN_CVAR (Float, transsouls)
EXTERN_CVAR (Int, vid_refreshrate)
extern IDirect3D9 *D3D;
extern cycle_t BlitCycles;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
const char *const D3DFB::ShaderNames[D3DFB::NUM_SHADERS] =
{
"NormalColor.pso",
"NormalColorPal.pso",
"NormalColorInv.pso",
"NormalColorPalInv.pso",
"RedToAlpha.pso",
"RedToAlphaInv.pso",
"VertexColor.pso",
"SpecialColormap.pso",
"SpecialColorMapPal.pso",
"InGameColormap.pso",
"InGameColormapDesat.pso",
"InGameColormapInv.pso",
"InGameColormapInvDesat.pso",
"InGameColormapPal.pso",
"InGameColormapPalDesat.pso",
"InGameColormapPalInv.pso",
"InGameColormapPalInvDesat.pso",
"BurnWipe.pso",
"GammaCorrection.pso",
};
// PUBLIC DATA DEFINITIONS -------------------------------------------------
CUSTOM_CVAR(Bool, vid_hw2d, true, CVAR_NOINITCALL)
{
V_SetBorderNeedRefresh();
ST_SetNeedRefresh();
}
CVAR(Bool, d3d_antilag, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
CVAR(Int, d3d_showpacks, 0, 0)
CVAR(Bool, vid_hwaalines, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
// CODE --------------------------------------------------------------------
//==========================================================================
//
// D3DFB - Constructor
//
//==========================================================================
D3DFB::D3DFB (UINT adapter, int width, int height, bool fullscreen)
: BaseWinFB (width, height)
{
D3DPRESENT_PARAMETERS d3dpp;
LastHR = 0;
Adapter = adapter;
D3DDevice = NULL;
VertexBuffer = NULL;
IndexBuffer = NULL;
FBTexture = NULL;
TempRenderTexture = NULL;
RenderTexture[0] = NULL;
RenderTexture[1] = NULL;
InitialWipeScreen = NULL;
ScreenshotTexture = NULL;
ScreenshotSurface = NULL;
FinalWipeScreen = NULL;
PaletteTexture = NULL;
GammaTexture = NULL;
FrontCopySurface = NULL;
for (int i = 0; i < NUM_SHADERS; ++i)
{
Shaders[i] = NULL;
}
GammaShader = NULL;
BlockSurface[0] = NULL;
BlockSurface[1] = NULL;
VSync = vid_vsync;
BlendingRect.left = 0;
BlendingRect.top = 0;
BlendingRect.right = FBWidth;
BlendingRect.bottom = FBHeight;
In2D = 0;
Palettes = NULL;
Textures = NULL;
Accel2D = true;
GatheringWipeScreen = false;
ScreenWipe = NULL;
InScene = false;
QuadExtra = new BufferedTris[MAX_QUAD_BATCH];
Atlases = NULL;
PixelDoubling = 0;
SkipAt = -1;
CurrRenderTexture = 0;
RenderTextureToggle = 0;
Gamma = 1.0;
FlashColor0 = 0;
FlashColor1 = 0xFFFFFFFF;
FlashColor = 0;
FlashAmount = 0;
NeedGammaUpdate = false;
NeedPalUpdate = false;
if (MemBuffer == NULL)
{
return;
}
memcpy(SourcePalette, GPalette.BaseColors, sizeof(PalEntry)*256);
Windowed = !(static_cast<Win32Video *>(Video)->GoFullscreen(fullscreen));
TrueHeight = height;
if (fullscreen)
{
for (Win32Video::ModeInfo *mode = static_cast<Win32Video *>(Video)->m_Modes; mode != NULL; mode = mode->next)
{
if (mode->width == Width && mode->height == Height)
{
TrueHeight = mode->realheight;
PixelDoubling = mode->doubling;
break;
}
}
}
// Offset from top of screen to top of letterboxed screen
LBOffsetI = (TrueHeight - Height) / 2;
LBOffset = float(LBOffsetI);
FillPresentParameters(&d3dpp, fullscreen, VSync);
HRESULT hr;
LOG("CreateDevice attempt 1 hwvp\n");
if (FAILED(hr = D3D->CreateDevice(Adapter, D3DDEVTYPE_HAL, Window,
D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &d3dpp, &D3DDevice)) &&
(hr != D3DERR_DEVICELOST || D3DDevice == NULL))
{
LOG2("CreateDevice returned hr %08x dev %p; attempt 2 swvp\n", hr, D3DDevice);
if (FAILED(D3D->CreateDevice(Adapter, D3DDEVTYPE_HAL, Window,
D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &d3dpp, &D3DDevice)) &&
(hr != D3DERR_DEVICELOST || D3DDevice == NULL))
{
if (d3dpp.FullScreen_RefreshRateInHz != 0)
{
d3dpp.FullScreen_RefreshRateInHz = 0;
LOG2("CreateDevice returned hr %08x dev %p; attempt 3 (hwvp, default Hz)\n", hr, D3DDevice);
if (FAILED(hr = D3D->CreateDevice(Adapter, D3DDEVTYPE_HAL, Window,
D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &d3dpp, &D3DDevice)) &&
(hr != D3DERR_DEVICELOST || D3DDevice == NULL))
{
LOG2("CreateDevice returned hr %08x dev %p; attempt 4 (swvp, default Hz)\n", hr, D3DDevice);
if (FAILED(D3D->CreateDevice(Adapter, D3DDEVTYPE_HAL, Window,
D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &d3dpp, &D3DDevice)) &&
hr != D3DERR_DEVICELOST)
{
D3DDevice = NULL;
}
}
}
}
}
LOG2("Final CreateDevice returned HR %08x and device %p\n", hr, D3DDevice);
LastHR = hr;
if (D3DDevice != NULL)
{
D3DADAPTER_IDENTIFIER9 adapter_id;
D3DDEVICE_CREATION_PARAMETERS create_params;
if (FAILED(hr = D3DDevice->GetDeviceCaps(&DeviceCaps)))
{
memset(&DeviceCaps, 0, sizeof(DeviceCaps));
}
if (SUCCEEDED(hr = D3DDevice->GetCreationParameters(&create_params)) &&
SUCCEEDED(hr = D3D->GetAdapterIdentifier(create_params.AdapterOrdinal, 0, &adapter_id)))
{
// NVidia's drivers lie, claiming they don't support
// antialiased lines when, really, they do.
if (adapter_id.VendorId == 0x10de)
{
DeviceCaps.LineCaps |= D3DLINECAPS_ANTIALIAS;
}
// ATI's drivers apparently also lie, so screw this caps bit.
}
CreateResources();
SetInitialState();
}
}
//==========================================================================
//
// D3DFB - Destructor
//
//==========================================================================
D3DFB::~D3DFB ()
{
ReleaseResources();
SAFE_RELEASE( D3DDevice );
delete[] QuadExtra;
}
//==========================================================================
//
// D3DFB :: SetInitialState
//
// Called after initial device creation and reset, when everything is set
// to D3D's defaults.
//
//==========================================================================
void D3DFB::SetInitialState()
{
AlphaBlendEnabled = FALSE;
AlphaBlendOp = D3DBLENDOP_ADD;
AlphaSrcBlend = D3DBLEND(0);
AlphaDestBlend = D3DBLEND(0);
CurPixelShader = NULL;
memset(Constant, 0, sizeof(Constant));
for (unsigned i = 0; i < countof(Texture); ++i)
{
Texture[i] = NULL;
D3DDevice->SetSamplerState(i, D3DSAMP_ADDRESSU, (i == 1 && SM14) ? D3DTADDRESS_BORDER : D3DTADDRESS_CLAMP);
D3DDevice->SetSamplerState(i, D3DSAMP_ADDRESSV, (i == 1 && SM14) ? D3DTADDRESS_BORDER : D3DTADDRESS_CLAMP);
if (i > 1)
{
// Set linear filtering for the SM14 gamma texture.
D3DDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
}
}
NeedGammaUpdate = true;
NeedPalUpdate = true;
OldRenderTarget = NULL;
if (!Windowed && SM14)
{
// Fix for Radeon 9000, possibly other R200s: When the device is
// reset, it resets the gamma ramp, but the driver apparently keeps a
// cached copy of the ramp that it doesn't update, so when
// SetGammaRamp is called later to handle the NeedGammaUpdate flag,
// it doesn't do anything, because the gamma ramp is the same as the
// one passed in the last call, even though the visible gamma ramp
// actually has changed.
//
// So here we force the gamma ramp to something absolutely horrible and
// trust that we will be able to properly set the gamma later when
// NeedGammaUpdate is handled.
D3DGAMMARAMP ramp;
memset(&ramp, 0, sizeof(ramp));
D3DDevice->SetGammaRamp(0, 0, &ramp);
}
// This constant is used for grayscaling weights (.xyz) and color inversion (.w)
float weights[4] = { 77/256.f, 143/256.f, 37/256.f, 1 };
D3DDevice->SetPixelShaderConstantF(PSCONST_Weights, weights, 1);
// D3DRS_ALPHATESTENABLE defaults to FALSE
// D3DRS_ALPHAREF defaults to 0
D3DDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_NOTEQUAL);
AlphaTestEnabled = FALSE;
CurBorderColor = 0;
// Clear to black, just in case it wasn't done already.
D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 0, 0);
}
//==========================================================================
//
// D3DFB :: FillPresentParameters
//
//==========================================================================
void D3DFB::FillPresentParameters (D3DPRESENT_PARAMETERS *pp, bool fullscreen, bool vsync)
{
memset (pp, 0, sizeof(*pp));
pp->Windowed = !fullscreen;
pp->SwapEffect = D3DSWAPEFFECT_DISCARD;
pp->BackBufferWidth = Width << PixelDoubling;
pp->BackBufferHeight = TrueHeight << PixelDoubling;
pp->BackBufferFormat = fullscreen ? D3DFMT_A8R8G8B8 : D3DFMT_UNKNOWN;
pp->BackBufferCount = 1;
pp->hDeviceWindow = Window;
pp->PresentationInterval = vsync ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE;
if (fullscreen)
{
pp->FullScreen_RefreshRateInHz = vid_refreshrate;
}
}
//==========================================================================
//
// D3DFB :: CreateResources
//
//==========================================================================
bool D3DFB::CreateResources()
{
Atlases = NULL;
if (!Windowed)
{
// Remove the window border in fullscreen mode
SetWindowLong (Window, GWL_STYLE, WS_POPUP|WS_VISIBLE|WS_SYSMENU);
}
else
{
// Resize the window to match desired dimensions
RECT rect = { 0, 0, Width, Height };
AdjustWindowRectEx(&rect, WS_VISIBLE|WS_OVERLAPPEDWINDOW, FALSE, WS_EX_APPWINDOW);
int sizew = rect.right - rect.left;
int sizeh = rect.bottom - rect.top;
LOG2 ("Resize window to %dx%d\n", sizew, sizeh);
VidResizing = true;
// Make sure the window has a border in windowed mode
SetWindowLong(Window, GWL_STYLE, WS_VISIBLE|WS_OVERLAPPEDWINDOW);
if (GetWindowLong(Window, GWL_EXSTYLE) & WS_EX_TOPMOST)
{
// Direct3D 9 will apparently add WS_EX_TOPMOST to fullscreen windows,
// and removing it is a little tricky. Using SetWindowLongPtr to clear it
// will not do the trick, but sending the window behind everything will.
SetWindowPos(Window, HWND_BOTTOM, 0, 0, sizew, sizeh,
SWP_DRAWFRAME | SWP_NOCOPYBITS | SWP_NOMOVE);
SetWindowPos(Window, HWND_TOP, 0, 0, 0, 0, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE);
}
else
{
SetWindowPos(Window, NULL, 0, 0, sizew, sizeh,
SWP_DRAWFRAME | SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOZORDER);
}
I_RestoreWindowedPos();
VidResizing = false;
}
if (!LoadShaders())
{
return false;
}
if (!CreateFBTexture() ||
!CreatePaletteTexture())
{
return false;
}
if (!CreateVertexes())
{
return false;
}
CreateGammaTexture();
CreateBlockSurfaces();
return true;
}
//==========================================================================
//
// D3DFB :: LoadShaders
//
// Returns true if all required shaders were loaded. (Gamma and burn wipe
// are the only ones not considered "required".)
//
//==========================================================================
bool D3DFB::LoadShaders()
{
static const char models[][4] = { "30/", "20/", "14/" };
FString shaderdir, shaderpath;
unsigned model, i;
int lump;
// We determine the best available model simply by trying them all in
// order of decreasing preference.
for (model = 0; model < countof(models); ++model)
{
shaderdir = "shaders/d3d/sm";
shaderdir += models[model];
for (i = 0; i < NUM_SHADERS; ++i)
{
shaderpath = shaderdir;
shaderpath += ShaderNames[i];
lump = Wads.CheckNumForFullName(shaderpath);
if (lump >= 0)
{
FMemLump data = Wads.ReadLump(lump);
if (FAILED(D3DDevice->CreatePixelShader((DWORD *)data.GetMem(), &Shaders[i])) &&
i < SHADER_BurnWipe)
{
break;
}
}
}
if (i == NUM_SHADERS)
{ // Success!
SM14 = (model == countof(models) - 1);
return true;
}
// Failure. Release whatever managed to load (which is probably nothing.)
for (i = 0; i < NUM_SHADERS; ++i)
{
SAFE_RELEASE( Shaders[i] );
}
}
return false;
}
//==========================================================================
//
// D3DFB :: ReleaseResources
//
//==========================================================================
void D3DFB::ReleaseResources ()
{
I_SaveWindowedPos ();
KillNativeTexs();
KillNativePals();
ReleaseDefaultPoolItems();
SAFE_RELEASE( ScreenshotSurface );
SAFE_RELEASE( ScreenshotTexture );
SAFE_RELEASE( PaletteTexture );
for (int i = 0; i < NUM_SHADERS; ++i)
{
SAFE_RELEASE( Shaders[i] );
}
GammaShader = NULL;
if (ScreenWipe != NULL)
{
delete ScreenWipe;
ScreenWipe = NULL;
}
Atlas *pack, *next;
for (pack = Atlases; pack != NULL; pack = next)
{
next = pack->Next;
delete pack;
}
GatheringWipeScreen = false;
}
//==========================================================================
//
// D3DFB :: ReleaseDefaultPoolItems
//
// Free resources created with D3DPOOL_DEFAULT.
//
//==========================================================================
void D3DFB::ReleaseDefaultPoolItems()
{
SAFE_RELEASE( FBTexture );
SAFE_RELEASE( FinalWipeScreen );
SAFE_RELEASE( RenderTexture[0] );
SAFE_RELEASE( RenderTexture[1] );
SAFE_RELEASE( InitialWipeScreen );
SAFE_RELEASE( VertexBuffer );
SAFE_RELEASE( IndexBuffer );
SAFE_RELEASE( BlockSurface[0] );
SAFE_RELEASE( BlockSurface[1] );
SAFE_RELEASE( FrontCopySurface );
}
//==========================================================================
//
// D3DFB :: Reset
//
//==========================================================================
bool D3DFB::Reset ()
{
D3DPRESENT_PARAMETERS d3dpp;
ReleaseDefaultPoolItems();
FillPresentParameters (&d3dpp, !Windowed, VSync);
if (!SUCCEEDED(D3DDevice->Reset (&d3dpp)))
{
if (d3dpp.FullScreen_RefreshRateInHz != 0)
{
d3dpp.FullScreen_RefreshRateInHz = 0;
if (!SUCCEEDED(D3DDevice->Reset (&d3dpp)))
{
return false;
}
}
else
{
return false;
}
}
LOG("Device was reset\n");
if (!CreateFBTexture() || !CreateVertexes())
{
return false;
}
CreateBlockSurfaces();
SetInitialState();
return true;
}
//==========================================================================
//
// D3DFB :: CreateBlockSurfaces
//
// Create blocking surfaces for antilag. It's okay if these can't be
// created; antilag just won't work.
//
//==========================================================================
void D3DFB::CreateBlockSurfaces()
{
BlockNum = 0;
if (SUCCEEDED(D3DDevice->CreateOffscreenPlainSurface(16, 16, D3DFMT_A8R8G8B8,
D3DPOOL_DEFAULT, &BlockSurface[0], 0)))
{
if (FAILED(D3DDevice->CreateOffscreenPlainSurface(16, 16, D3DFMT_A8R8G8B8,
D3DPOOL_DEFAULT, &BlockSurface[1], 0)))
{
BlockSurface[0]->Release();
BlockSurface[0] = NULL;
}
}
}
//==========================================================================
//
// D3DFB :: KillNativePals
//
// Frees all native palettes.
//
//==========================================================================
void D3DFB::KillNativePals()
{
while (Palettes != NULL)
{
delete Palettes;
}
}
//==========================================================================
//
// D3DFB :: KillNativeTexs
//
// Frees all native textures.
//
//==========================================================================
void D3DFB::KillNativeTexs()
{
while (Textures != NULL)
{
delete Textures;
}
}
//==========================================================================
//
// D3DFB :: CreateFBTexture
//
// Creates the "Framebuffer" texture. With the advent of hardware-assisted
// 2D, this is something of a misnomer now. The FBTexture is only used for
// uploading the software 3D image to video memory so that it can be drawn
// to the real frame buffer.
//
// It also creates the TempRenderTexture, since this seemed like a
// convenient place to do so.
//
//==========================================================================
bool D3DFB::CreateFBTexture ()
{
if (FAILED(D3DDevice->CreateTexture(Width, Height, 1, D3DUSAGE_DYNAMIC, D3DFMT_L8, D3DPOOL_DEFAULT, &FBTexture, NULL)))
{
int pow2width, pow2height, i;
for (i = 1; i < Width; i <<= 1) {} pow2width = i;
for (i = 1; i < Height; i <<= 1) {} pow2height = i;
if (FAILED(D3DDevice->CreateTexture(pow2width, pow2height, 1, D3DUSAGE_DYNAMIC, D3DFMT_L8, D3DPOOL_DEFAULT, &FBTexture, NULL)))
{
return false;
}
else
{
FBWidth = pow2width;
FBHeight = pow2height;
}
}
else
{
FBWidth = Width;
FBHeight = Height;
}
RenderTextureToggle = 0;
RenderTexture[0] = NULL;
RenderTexture[1] = NULL;
if (FAILED(D3DDevice->CreateTexture(FBWidth, FBHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &RenderTexture[0], NULL)))
{
return false;
}
if (Windowed || PixelDoubling)
{
// Windowed or pixel doubling: Create another render texture so we can flip between them.
RenderTextureToggle = 1;
if (FAILED(D3DDevice->CreateTexture(FBWidth, FBHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &RenderTexture[1], NULL)))
{
return false;
}
}
else
{
// Fullscreen and not pixel doubling: Create a render target to have the back buffer copied to.
if (FAILED(D3DDevice->CreateRenderTarget(Width, Height, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &FrontCopySurface, NULL)))
{
return false;
}
}
// Initialize the TempRenderTextures to black.
for (int i = 0; i <= RenderTextureToggle; ++i)
{
IDirect3DSurface9 *surf;
if (SUCCEEDED(RenderTexture[i]->GetSurfaceLevel(0, &surf)))
{
D3DDevice->ColorFill(surf, NULL, D3DCOLOR_XRGB(0,0,0));
surf->Release();
}
}
TempRenderTexture = RenderTexture[0];
CurrRenderTexture = 0;
return true;
}
//==========================================================================
//
// D3DFB :: CreatePaletteTexture
//
//==========================================================================
bool D3DFB::CreatePaletteTexture ()
{
if (FAILED(D3DDevice->CreateTexture (256, 1, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &PaletteTexture, NULL)))
{
return false;
}
return true;
}
//==========================================================================
//
// D3DFB :: CreateGammaTexture
//
//==========================================================================
bool D3DFB::CreateGammaTexture ()
{
// If this fails, you just won't get gamma correction in a window
// on SM14 cards.
assert(GammaTexture == NULL);
if (SM14)
{
return SUCCEEDED(D3DDevice->CreateTexture(256, 1, 1, 0, D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED, &GammaTexture, NULL));
}
return false;
}
//==========================================================================
//
// D3DFB :: CreateVertexes
//
//==========================================================================
bool D3DFB::CreateVertexes ()
{
VertexPos = -1;
IndexPos = -1;
QuadBatchPos = -1;
BatchType = BATCH_None;
if (FAILED(D3DDevice->CreateVertexBuffer(sizeof(FBVERTEX)*NUM_VERTS,
D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_FBVERTEX, D3DPOOL_DEFAULT, &VertexBuffer, NULL)))
{
return false;
}
if (FAILED(D3DDevice->CreateIndexBuffer(sizeof(WORD)*NUM_INDEXES,
D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &IndexBuffer, NULL)))
{
return false;
}
return true;
}
//==========================================================================
//
// D3DFB :: CalcFullscreenCoords
//
//==========================================================================
void D3DFB::CalcFullscreenCoords (FBVERTEX verts[4], bool viewarea_only, bool can_double, D3DCOLOR color0, D3DCOLOR color1) const
{
float offset = OldRenderTarget != NULL ? 0 : LBOffset;
float top = offset - 0.5f;
float texright = float(Width) / float(FBWidth);
float texbot = float(Height) / float(FBHeight);
float mxl, mxr, myt, myb, tmxl, tmxr, tmyt, tmyb;
if (viewarea_only)
{ // Just calculate vertices for the viewarea/BlendingRect
mxl = float(BlendingRect.left) - 0.5f;
mxr = float(BlendingRect.right) - 0.5f;
myt = float(BlendingRect.top) + top;
myb = float(BlendingRect.bottom) + top;
tmxl = float(BlendingRect.left) / float(Width) * texright;
tmxr = float(BlendingRect.right) / float(Width) * texright;
tmyt = float(BlendingRect.top) / float(Height) * texbot;
tmyb = float(BlendingRect.bottom) / float(Height) * texbot;
}
else
{ // Calculate vertices for the whole screen
mxl = -0.5f;
mxr = float(Width << (can_double ? PixelDoubling : 0)) - 0.5f;
myt = top;
myb = float(Height << (can_double ? PixelDoubling : 0)) + top;
tmxl = 0;
tmxr = texright;
tmyt = 0;
tmyb = texbot;
}
//{ mxl, myt, 0, 1, 0, 0xFFFFFFFF, tmxl, tmyt },
//{ mxr, myt, 0, 1, 0, 0xFFFFFFFF, tmxr, tmyt },
//{ mxr, myb, 0, 1, 0, 0xFFFFFFFF, tmxr, tmyb },
//{ mxl, myb, 0, 1, 0, 0xFFFFFFFF, tmxl, tmyb },
verts[0].x = mxl;
verts[0].y = myt;
verts[0].z = 0;
verts[0].rhw = 1;
verts[0].color0 = color0;
verts[0].color1 = color1;
verts[0].tu = tmxl;
verts[0].tv = tmyt;
verts[1].x = mxr;
verts[1].y = myt;
verts[1].z = 0;
verts[1].rhw = 1;
verts[1].color0 = color0;
verts[1].color1 = color1;
verts[1].tu = tmxr;
verts[1].tv = tmyt;
verts[2].x = mxr;
verts[2].y = myb;
verts[2].z = 0;
verts[2].rhw = 1;
verts[2].color0 = color0;
verts[2].color1 = color1;
verts[2].tu = tmxr;
verts[2].tv = tmyb;
verts[3].x = mxl;
verts[3].y = myb;
verts[3].z = 0;
verts[3].rhw = 1;
verts[3].color0 = color0;
verts[3].color1 = color1;
verts[3].tu = tmxl;
verts[3].tv = tmyb;
}
//==========================================================================
//
// D3DFB :: GetPageCount
//
//==========================================================================
int D3DFB::GetPageCount ()
{
return 1;
}
//==========================================================================
//
// D3DFB :: PaletteChanged
//
//==========================================================================
void D3DFB::PaletteChanged ()
{
}
//==========================================================================
//
// D3DFB :: QueryNewPalette
//
//==========================================================================
int D3DFB::QueryNewPalette ()
{
return 0;
}
//==========================================================================
//