Swap chain->Present() performance -
1 void CGraphView::OnInitialUpdate()
2 {
3 // this is my MFC View class OnInitialUpdate()
4
5 // Get the handle to the 1st render target.
6 m_hwndRenderWindow = GetDlgItem( IDC_RENDER_FRAME )->GetSafeHwnd();
7
8 // Get the handle to the 2nd render target.
9 m_hwndRenderWindow2 = GetDlgItem( IDC_RENDER_FRAME2 )->GetSafeHwnd();
10
11 // Create the device using the Example Framework class
12 // CD3DApplication
13 CD3DApplication::m_hWnd = m_hwndRenderWindow;
14
15 // Create the device passing it the HINSTANCE handle
16 // of our main app.
17 CD3DApplication::Create( AfxGetInstanceHandle() );
18
19 // The seperate D3DPRESENT_PARAMETERS for each swap chain.
20 ZeroMemory( &m_d3dppSwapChain, sizeof( D3DPRESENT_PARAMETERS ) );
21 ZeroMemory( &m_d3dppSwapChain2, sizeof( D3DPRESENT_PARAMETERS ) );
22
23 // Get the current display mode for the desktop.
24 D3DDISPLAYMODE mode;
25 m_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &mode );
26
27 // Setup the presentation paramaters for the first swapchain.
28 m_d3dppSwapChain.Windowed = TRUE;
29 m_d3dppSwapChain.SwapEffect = D3DSWAPEFFECT_COPY;
30 m_d3dppSwapChain.BackBufferFormat = mode.Format;
31 m_d3dppSwapChain.hDeviceWindow = m_hwndRenderWindow;
32
33 // Create the first swapchain
34 m_pd3dDevice->CreateAdditionalSwapChain( &m_d3dppSwapChain, &m_pSwapChain );
35
36 // Setup the presentation parameters for second swapchain.
37 m_d3dppSwapChain2.Windowed = TRUE;
38 m_d3dppSwapChain2.SwapEffect = D3DSWAPEFFECT_COPY;
39 m_d3dppSwapChain2.BackBufferFormat = mode.Format;
40 m_d3dppSwapChain.hDeviceWindow = m_hwndRenderWindow2;
41
42 // Create the 2nd swap chain
43 m_pd3dDevice->CreateAdditionalSwapChain( &m_d3dppSwapChain2, &m_pSwapChain2 );
44 }
45
46 // The main render function called from the main apps
47 // OnIdle() function.
48 HRESULT CGraphView::Render()
49 {
50 // Create a temporary surface to hold the returned
51 // suface from GetBackBuffer()
52 LPDIRECT3DSURFACE9 pSurface = NULL;
53
54 if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
55 {
56 // Render the 1st viewport
57 m_pSwapChain->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &pSurface );
58 m_pd3dDevice->SetRenderTarget( 0, pSurface );
59
60 m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR(0x000000), 1.0f, 0L );
61
62 // Reset the world matrix
63 D3DXMATRIXA16 matWorld;
64 D3DXMatrixIdentity( &matWorld );
65 m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
66
67 // Draw the grid
68 m_pd3dDevice->SetFVF( D3DFVF_GRIDVERTEX );
69 m_pd3dDevice->SetStreamSource( 0, m_pGridVB, 0, sizeof( GRIDVERTEX ) );
70 m_pd3dDevice->DrawPrimitive( D3DPT_LINELIST, 0, m_dwNumGridVerts / 2 );
71
72 pSurface->Release();
73
74 // Render the 2nd viewport, For the purposes of this
75 // example i am rendering the same scene as the other
76 m_pSwapChain2->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &pSurface );
77 m_pd3dDevice->SetRenderTarget( 0, pSurface );
78
79 m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR(0x000000), 1.0f, 0L );
80 // Reset the world matrix
81 D3DXMATRIXA16 matWorld;
82 D3DXMatrixIdentity( &matWorld );
83 m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
84
85 // Draw the grid
86 m_pd3dDevice->SetFVF( D3DFVF_GRIDVERTEX );
87 m_pd3dDevice->SetStreamSource( 0, m_pGridVB, 0, sizeof( GRIDVERTEX ) );
88 m_pd3dDevice->DrawPrimitive( D3DPT_LINELIST, 0, m_dwNumGridVerts / 2 );
89 pSurface->Release();
90
91 m_pd3dDevice->EndScene();
92 }
93
94 m_pSwapChain->Present( NULL, NULL, NULL, NULL, NULL );
95 m_pSwapChain2->Present( NULL, NULL, NULL, NULL, NULL );
96
97 return S_OK;
98 }