-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCDXScreen.cpp
847 lines (641 loc) · 18.9 KB
/
CDXScreen.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
// CDXScreen.cpp
//
// CDXScreen class
// Copyright (c) 2015 by Kronosaur Productions, LLC. All Rights Reserved.
#include <windows.h>
#include <dsound.h>
#include "Alchemy.h"
#include "DirectXUtil.h"
#include "DX3DImpl.h"
#pragma comment(lib, "d3d9.lib")
CDXScreen::CDXScreen (void) :
m_hWnd(NULL),
m_pD3D(NULL),
m_pD3DDevice(NULL),
m_bDeviceLost(false)
// CDXScreen constructor
{
}
bool CDXScreen::BeginScene (void)
// BeginScene
//
// Start the scene. Must call EndScene when we're done.
{
ASSERT(!m_bDeviceLost);
HRESULT hr = m_pD3DDevice->BeginScene();
if (hr != D3D_OK)
{
// If the device is lost, then reset it and try again.
if (hr == D3DERR_DEVICELOST)
{
::kernelDebugLogPattern("[DX] Device lost at BeginScene.");
m_bDeviceLost = true;
return false;
}
// Otherwise, it's some error we can't handle
else
{
RenderError(CONSTLIT("BeginScene unknown error."));
return false;
}
}
// Success!
m_bEndSceneNeeded = true;
return true;
}
void CDXScreen::BltToSurface (const CG32bitImage &Src, IDirect3DSurface9 *pDest)
// BltToSurface
//
// Blts from the source to destination
{
// Get some metrics
D3DSURFACE_DESC DestDesc;
if (FAILED(pDest->GetDesc(&DestDesc)))
return;
int cxWidth = Min(Src.GetWidth(), (int)DestDesc.Width);
int cyHeight = Min(Src.GetHeight(), (int)DestDesc.Height);
// Blt based on the format.
switch (DestDesc.Format)
{
// For D3DFMT_X8R8G8B8 and D3DFMT_A8R8G8B8 we just copy the bitmap
// (which is in the same format).
case D3DFMT_X8R8G8B8:
case D3DFMT_A8R8G8B8:
{
// Lock the surfaces
D3DLOCKED_RECT DestLocked;
if (FAILED(pDest->LockRect(&DestLocked, NULL, 0)))
return;
// Blt
CG32bitPixel *pSrcRow = Src.GetPixelPos(0, 0);
CG32bitPixel *pSrcRowEnd = Src.GetPixelPos(0, cyHeight);
BYTE *pDestRow = (BYTE *)DestLocked.pBits;
while (pSrcRow < pSrcRowEnd)
{
DWORD *pSrc = (DWORD *)pSrcRow;
DWORD *pSrcEnd = (DWORD *)pSrcRow + cxWidth;
DWORD *pDest = (DWORD *)pDestRow;
while (pSrc < pSrcEnd)
*pDest++ = *pSrc++;
// Next
pSrcRow = Src.NextRow(pSrcRow);
pDestRow += DestLocked.Pitch;
}
// Done
pDest->UnlockRect();
break;
}
// For other cases, we just get the DC and use BitBlt
default:
{
HDC hDC;
if (FAILED(pDest->GetDC(&hDC)))
return;
Src.BltToDC(hDC, 0, 0);
pDest->ReleaseDC(hDC);
break;
}
}
}
void CDXScreen::CleanUp (void)
// CleanUp
//
// Clean up the class
{
m_Layers.DeleteAll();
m_PaintOrder.DeleteAll();
if (m_pD3DDevice != NULL)
{
m_pD3DDevice->Release();
m_pD3DDevice = NULL;
}
if (m_pD3D != NULL)
{
m_pD3D->Release();
m_pD3D = NULL;
}
}
bool CDXScreen::CreateLayer (const SDXLayerCreate &Create, int *retiLayerID, CString *retsError)
// CreateLayer
//
// Creates a paint layer, which will be composited with all the other layers.
{
// Check some parameters
if (Create.cxWidth <= 0 || Create.cyHeight <= 0)
{
ASSERT(false);
if (retsError) *retsError = CONSTLIT("Invalid layer size.");
return false;
}
// Optimistically insert a new layer. We always add layers to the end of
// our array, do we know the ID.
int iID = m_Layers.GetCount();
SLayer *pLayer = m_Layers.Insert();
pLayer->cxWidth = Create.cxWidth;
pLayer->cyHeight = Create.cyHeight;
pLayer->xPos = Create.xPos;
pLayer->yPos = Create.yPos;
pLayer->zPos = Create.zPos;
// If we're using textures, we need a vertex buffer and three textures.
if (m_bUseTextures)
{
if (!CreateLayerResources(*pLayer, retsError))
{
m_Layers.Delete(iID);
return false;
}
}
// Otherwise, if we're not using textures then we just need to create some
// Off-screen bitmaps
else
{
pLayer->FrontBuffer.Create(Create.cxWidth, Create.cyHeight);
pLayer->BackBuffer.Create(Create.cxWidth, Create.cyHeight);
}
// Now that we've succeeded, add it to the paint order
m_PaintOrder.Insert(pLayer->zPos, iID);
// Done
if (retiLayerID)
*retiLayerID = iID;
return true;
}
bool CDXScreen::CreateLayerResources (SLayer &Layer, CString *retsError)
// CreateLayerResources
//
// Creates all the resources for a layer
{
// Create the vertices
if (FAILED(m_pD3DDevice->CreateVertexBuffer(4 * sizeof(SVertexFormatStd),
D3DUSAGE_WRITEONLY,
D3DFVF_VERTEXFORMAT_STD,
D3DPOOL_MANAGED,
&Layer.pVertices,
NULL)))
{
if (retsError) *retsError = CONSTLIT("Unable to create vertex buffer.");
return false;
}
// Initialize vertices
SVertexFormatStd *pVertexList;
if (FAILED(Layer.pVertices->Lock(0, 4 * sizeof(SVertexFormatStd), (void **)&pVertexList, 0)))
{
if (retsError) *retsError = CONSTLIT("Unable to lock vertex buffer.");
return false;
}
// Set all the colors to white
pVertexList[0].color = pVertexList[1].color = pVertexList[2].color = pVertexList[3].color = 0xffffffff;
// Set positions and texture coordinates
//
// NOTE: We offset by 0.5 to make the textures align on pixel boundaries.
pVertexList[0].x = pVertexList[3].x = -(Layer.cxWidth / 2.0f) - 0.5f;
pVertexList[1].x = pVertexList[2].x = (Layer.cxWidth / 2.0f) - 0.5f;
pVertexList[0].y = pVertexList[1].y = (Layer.cyHeight / 2.0f) + 0.5f;
pVertexList[2].y = pVertexList[3].y = -(Layer.cyHeight / 2.0f) + 0.5f;
pVertexList[0].z = pVertexList[1].z = pVertexList[2].z = pVertexList[3].z = 1.0f;
// This positions and scales the texture across the target rectangle
pVertexList[1].u = pVertexList[2].u = 1.0f;
pVertexList[0].u = pVertexList[3].u = 0.0f;
pVertexList[0].v = pVertexList[1].v = 0.0f;
pVertexList[2].v = pVertexList[3].v = 1.0f;
Layer.pVertices->Unlock();
// Create a couple of textures
HRESULT hr;
if ((hr = m_pD3DDevice->CreateTexture(Layer.cxWidth,
Layer.cyHeight,
1,
0,
D3DFMT_A8R8G8B8,
D3DPOOL_DEFAULT,
&Layer.pTexture,
NULL)) != D3D_OK)
{
if (retsError) *retsError = strPatternSubst(CONSTLIT("Unable to create device texture: %d."), hr);
return false;
}
if ((hr = m_pD3DDevice->CreateTexture(Layer.cxWidth,
Layer.cyHeight,
1,
(CanUseDynamicTextures() ? D3DUSAGE_DYNAMIC : 0),
D3DFMT_A8R8G8B8,
D3DPOOL_SYSTEMMEM,
&Layer.pFrontBuffer,
NULL)) != D3D_OK)
{
if (retsError) *retsError = strPatternSubst(CONSTLIT("Unable to create front buffer texture: %d."), hr);
return false;
}
if ((hr = m_pD3DDevice->CreateTexture(Layer.cxWidth,
Layer.cyHeight,
1,
(CanUseDynamicTextures() ? D3DUSAGE_DYNAMIC : 0),
D3DFMT_A8R8G8B8,
D3DPOOL_SYSTEMMEM,
&Layer.pBackBuffer,
NULL)) != D3D_OK)
{
if (retsError) *retsError = strPatternSubst(CONSTLIT("Unable to create back buffer texture: %d."), hr);
return false;
}
// Create an image object that paints to the backbuffer texture. We leave
// the backbuffer locked until we swap.
D3DLOCKED_RECT Locked;
if (FAILED(Layer.pBackBuffer->LockRect(0, &Locked, NULL, (CanUseDynamicTextures() ? D3DLOCK_DISCARD : 0))))
{
if (retsError) *retsError = CONSTLIT("Unable to lock texture.");
return false;
}
// We create a CG32bitImage object which points to the DX surface. We can do this
// because we keep the surface locked and because we specified the proper
// pixel depth (D3DFMT_A8R8G8B8) which happens to match CG32bitPixel.
Layer.BackBuffer.CreateFromExternalBuffer(Locked.pBits, Layer.cxWidth, Layer.cyHeight, Locked.Pitch);
// Success
return true;
}
void CDXScreen::DebugOutputStats (void)
// DebugOutputStats
//
// Output stats about the DX stack
{
::kernelDebugLogPattern("DeviceType: 0x%x", m_DeviceCaps.DeviceType);
::kernelDebugLogPattern("Caps: 0x%08x", m_DeviceCaps.Caps);
::kernelDebugLogPattern("Caps2: 0x%08x", m_DeviceCaps.Caps2);
::kernelDebugLogPattern("Caps3: 0x%08x", m_DeviceCaps.Caps3);
::kernelDebugLogPattern("DevCaps: 0x%08x", m_DeviceCaps.DevCaps);
::kernelDebugLogPattern("DevCaps2: 0x%08x", m_DeviceCaps.DevCaps2);
// Get the backbuffer so we can output some stats
if (m_pD3DDevice != NULL)
{
IDirect3DSurface9 *pBackBuffer;
if (FAILED(m_pD3DDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer)))
{
::kernelDebugLogPattern("[DX] Unable to get back buffer surface.");
return;
}
D3DSURFACE_DESC Desc;
if (FAILED(pBackBuffer->GetDesc(&Desc)))
{
pBackBuffer->Release();
::kernelDebugLogPattern("[DX] Unable to back buffer descriptor.");
return;
}
::kernelDebugLogPattern("[DX] Buffer Format: %d", Desc.Format);
pBackBuffer->Release();
}
}
bool CDXScreen::Init (HWND hWnd, int cxWidth, int cyHeight, DWORD dwFlags, CString *retsError)
// Init
//
// Initialize
{
// Initialize our metrics
m_hWnd = hWnd;
m_cxSource = cxWidth;
m_cySource = cyHeight;
RECT rcClient;
::GetClientRect(hWnd, &rcClient);
m_cxTarget = RectWidth(rcClient);
m_cyTarget = RectHeight(rcClient);
// Options
m_bUseGDI = ((dwFlags & FLAG_FORCE_GDI) ? true : false);
m_bNoGPUAcceleration = m_bUseGDI || ((dwFlags & FLAG_NO_TEXTURES) ? true : false);
m_bEndSceneNeeded = false;
m_bErrorReported = false;
// Create the D3D object, which is needed to create the D3DDevice.
if ((m_pD3D = ::Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
if (retsError) *retsError = CONSTLIT("Unable to create D3D object.");
return false;
}
// Set up the structure used to create the D3DDevice. Most parameters are
// zeroed out. We set Windowed to TRUE, since we want to do D3D in a
// window, and then set the SwapEffect to "discard", which is the most
// efficient method of presenting the back buffer to the display. And
// we request a back buffer format that matches the current desktop display
// format.
ZeroMemory( &m_Present, sizeof( m_Present ) );
m_Present.Windowed = TRUE;
m_Present.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_Present.BackBufferFormat = D3DFMT_UNKNOWN;
m_Present.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
// Create the Direct3D device. Here we are using the default adapter (most
// systems only have one, unless they have multiple graphics hardware cards
// installed) and requesting the HAL (which is saying we want the hardware
// device rather than a software one). Software vertex processing is
// specified since we know it will work on all cards. On cards that support
// hardware vertex processing, though, we would see a big performance gain
// by specifying hardware vertex processing.
if (FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&m_Present,
&m_pD3DDevice)))
{
if (retsError) *retsError = CONSTLIT("Unable to create D3D device.");
return false;
}
// Initialize
if (!InitDevice(retsError))
return false;
// Done
return true;
}
bool CDXScreen::InitDevice (CString *retsError)
// InitDevice
//
// Initialize the device after it has been created or reset.
{
// Get device caps
if (FAILED(m_pD3DDevice->GetDeviceCaps(&m_DeviceCaps)))
{
if (retsError) *retsError = CONSTLIT("Unable to get device caps.");
return false;
}
// Set up some options
m_bUseTextures = (!m_bNoGPUAcceleration
&& CanUseDynamicTextures()
&& ((m_DeviceCaps.Caps3 & D3DCAPS3_COPY_TO_VIDMEM) == D3DCAPS3_COPY_TO_VIDMEM));
// Set up our scene
CDXMatrix Identity;
Identity.SetIdentity();
CDXMatrix Ortho2D;
Ortho2D.SetOrthoLH((FLOAT)m_cxSource, (FLOAT)m_cySource, 0.0, 1.0);
m_pD3DDevice->SetTransform(D3DTS_PROJECTION, Ortho2D);
m_pD3DDevice->SetTransform(D3DTS_WORLD, Identity);
m_pD3DDevice->SetTransform(D3DTS_VIEW, Identity);
// No lighting needed
m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
// Done
return true;
}
bool CDXScreen::InitLayerResources (void)
// InitLayerResources
//
// Initializes layer resources after a reset
{
int i;
if (m_bUseTextures)
{
for (i = 0; i < m_Layers.GetCount(); i++)
{
SLayer &Layer = m_Layers[i];
if (!CreateLayerResources(Layer))
{
::kernelDebugLogPattern("[DX] CreateLayerResources failed.");
return false;
}
}
}
return true;
}
bool CDXScreen::Present (void)
// Present
//
// Presents the scene.
{
ASSERT(!m_bDeviceLost);
// End the scene
if (m_bEndSceneNeeded)
{
m_pD3DDevice->EndScene();
m_bEndSceneNeeded = false;
}
// Otherwise, present
HRESULT hr;
if (FAILED(hr = m_pD3DDevice->Present(NULL, NULL, NULL, NULL)))
{
if (hr == D3DERR_DEVICELOST)
{
::kernelDebugLogPattern("[DX] Device lost at Present.");
m_bDeviceLost = true;
return false;
}
else
{
RenderError(CONSTLIT("Present unknown error."));
return false;
}
}
// Done
return true;
}
void CDXScreen::Render (void)
// Present
//
// Draw
{
int i;
// If we're using GDI, then just blt. This DOES NOT use the DirectX stack,
// so things like the Steam Overlay will not work.
if (m_bUseGDI)
{
if (m_PaintOrder.GetCount() >= 1)
{
SLayer &Layer = m_Layers[m_PaintOrder[0]];
HDC hDC = ::GetDC(m_hWnd);
Layer.FrontBuffer.BltToDC(hDC, 0, 0);
::ReleaseDC(m_hWnd, hDC);
}
}
// The remaining methods all use DX, so if we've lost the device, we can't
// do anything.
else if (m_bDeviceLost || m_pD3DDevice == NULL)
return;
// If we're using textures, then we paint each layer as a texture
else if (m_bUseTextures)
{
// Before we start the scene, update all the textures.
for (i = 0; i < m_PaintOrder.GetCount(); i++)
{
SLayer &Layer = m_Layers[m_PaintOrder[i]];
if (Layer.pTexture)
m_pD3DDevice->UpdateTexture(Layer.pFrontBuffer, Layer.pTexture);
}
// Begin the scene
if (!BeginScene())
{
RenderError(CONSTLIT("Unable to begin scene."));
return;
}
// Render all layers in order (back to front)
for (i = 0; i < m_PaintOrder.GetCount(); i++)
{
SLayer &Layer = m_Layers[m_PaintOrder[i]];
// Copy the image bits to the texture
if (Layer.pTexture)
{
m_pD3DDevice->SetTexture(0, Layer.pTexture);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
// No looping texture
m_pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
m_pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
// If necessary, we need to antialias the texture
m_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, (Layer.cxWidth < m_cxTarget ? D3DTEXF_LINEAR : D3DTEXF_POINT));
// Blend with layer below us
m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, (i == 0 ? FALSE : TRUE));
m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
}
m_pD3DDevice->SetFVF(D3DFVF_VERTEXFORMAT_STD);
m_pD3DDevice->SetStreamSource(0, Layer.pVertices, 0, sizeof(SVertexFormatStd));
m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
}
// Present. NOTE: This will also end the scene (we don't need to call
// it explicitly).
if (!Present())
return;
}
// Otherwise, we just blt directly to the back buffer. In this case, however, we
// can only handle a single layer
else
{
if (m_PaintOrder.GetCount() >= 1)
{
IDirect3DSurface9 *pBackBuffer;
if (FAILED(m_pD3DDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer)))
return;
SLayer &Layer = m_Layers[m_PaintOrder[0]];
BltToSurface(Layer.FrontBuffer, pBackBuffer);
pBackBuffer->Release();
}
// Done
if (!Present())
return;
}
}
void CDXScreen::RenderError (const CString &sError)
// RenderError
//
// Report an error during rendering. We use a flag so we don't keep on
// reporting an error on every render frame.
{
if (!m_bErrorReported)
{
::kernelDebugLogPattern("[DX] %s", sError);
m_bErrorReported = true;
}
}
bool CDXScreen::ResetDevice (void)
// ResetDevice
//
// Reset the device after it has been lost
{
int i;
// Free all layer resources
if (m_bUseTextures)
{
for (i = 0; i < m_Layers.GetCount(); i++)
{
SLayer &Layer = m_Layers[i];
if (Layer.pVertices)
{
Layer.pVertices->Release();
Layer.pVertices = NULL;
}
if (Layer.pTexture)
{
Layer.pTexture->Release();
Layer.pTexture = NULL;
}
if (Layer.pFrontBuffer)
{
Layer.pFrontBuffer->Release();
Layer.pFrontBuffer = NULL;
}
if (Layer.pBackBuffer)
{
Layer.pBackBuffer->UnlockRect(0);
Layer.pBackBuffer->Release();
Layer.pBackBuffer = NULL;
}
}
}
// Reset the device
HRESULT hr = m_pD3DDevice->Reset(&m_Present);
if (hr != D3D_OK)
{
// If the device is lost, don't worry, we'll keep on resetting
if (hr == D3DERR_DEVICELOST)
{
::kernelDebugLogPattern("[DX] Device lost after reset.");
return false;
}
// Otherwise, this is a real error.
::kernelDebugLogPattern("[DX] Device reset failed: %x", hr);
return false;
}
return true;
}
void CDXScreen::SwapBuffers (void)
// SwapBuffers
//
// Swap back buffer to primary, in preparation for rendering.
{
int i;
// This function is guaranteed to be called when no one else is touching
// the buffers (since we're swapping), so this is a safe place to reset the
// device if necessary.
if (m_bDeviceLost)
{
// See if we need to reset
HRESULT hr = m_pD3DDevice->TestCooperativeLevel();
switch (hr)
{
case D3D_OK:
{
// Reinitialize the render state
if (!InitDevice())
{
::kernelDebugLogPattern("[DX] InitDevice failed.");
return;
}
// Create all layer resources`
if (!InitLayerResources())
{
::kernelDebugLogPattern("[DX] CreateLayerResources failed.");
return;
}
// Success
m_bDeviceLost = false;
m_bErrorReported = false;
::kernelDebugLogPattern("[DX] Device reset successfully.");
break;
}
case D3DERR_DEVICELOST:
// If we're still lost, keep waiting
Sleep(10);
break;
case D3DERR_DEVICENOTRESET:
ResetDevice();
break;
default:
::kernelDebugLogPattern("[DX] TestCooperativeLevel failed.");
return;
}
}
// Swap
else if (m_bUseTextures)
{
for (i = 0; i < m_Layers.GetCount(); i++)
{
SLayer &Layer = m_Layers[i];
// Unlock the back buffer
Layer.pBackBuffer->UnlockRect(0);
// Swap textures
Swap(Layer.pBackBuffer, Layer.pFrontBuffer);
// Lock again
D3DLOCKED_RECT Locked;
if (FAILED(Layer.pBackBuffer->LockRect(0, &Locked, NULL, (CanUseDynamicTextures() ? D3DLOCK_DISCARD : 0))))
continue;
Layer.BackBuffer.CreateFromExternalBuffer(Locked.pBits, Layer.cxWidth, Layer.cyHeight, Locked.Pitch);
}
}
else
{
for (i = 0; i < m_Layers.GetCount(); i++)
{
SLayer &Layer = m_Layers[i];
Layer.BackBuffer.SwapBuffers(Layer.FrontBuffer);
}
}
}