annotate DirectX11.cpp @ 2402:8cee51ce4382

included directx 11
author Ritor1
date Mon, 14 Jul 2014 10:03:44 +0600
parents
children 2d3c3d1dff0c
rev   line source
2402
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
1 #define WIN32_LEAN_AND_MEAN
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
2
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
3 #include "DirectX11.h"
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
4
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
5 HRESULT D3D11::InitDevice()
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
6 {
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
7 HRESULT hr = S_OK;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
8 RECT rc;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
9 GetClientRect( g_hWnd, &rc );
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
10 UINT width = rc.right - rc.left; // получаем ширину
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
11 UINT height = rc.bottom - rc.top; // и высоту окна
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
12 UINT createDeviceFlags = 0;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
13 D3D_DRIVER_TYPE driverTypes[] =
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
14 {
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
15 D3D_DRIVER_TYPE_HARDWARE,
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
16 D3D_DRIVER_TYPE_WARP,
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
17 D3D_DRIVER_TYPE_REFERENCE,
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
18 };
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
19 UINT numDriverTypes = ARRAYSIZE( driverTypes );
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
20
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
21 // Тут мы создаем список поддерживаемых версий DirectX
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
22 D3D_FEATURE_LEVEL featureLevels[] =
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
23 {
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
24 D3D_FEATURE_LEVEL_11_0,
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
25 D3D_FEATURE_LEVEL_10_1,
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
26 D3D_FEATURE_LEVEL_10_0,
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
27 };
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
28 UINT numFeatureLevels = ARRAYSIZE( featureLevels );
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
29
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
30 // Сейчас мы создадим устройства DirectX. Для начала заполним структуру,
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
31 // которая описывает свойства переднего буфера и привязывает его к нашему окну.
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
32 DXGI_SWAP_CHAIN_DESC sd; // Структура, описывающая цепь связи (Swap Chain)
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
33 ZeroMemory( &sd, sizeof( sd ) ); // очищаем ее
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
34 sd.BufferCount = 1; // у нас один задний буфер
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
35 sd.BufferDesc.Width = width; // ширина буфера
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
36 sd.BufferDesc.Height = height; // высота буфера
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
37 sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // формат пикселя в буфере
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
38 sd.BufferDesc.RefreshRate.Numerator = 75; // частота обновления экрана
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
39 sd.BufferDesc.RefreshRate.Denominator = 1;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
40 sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // назначение буфера - задний буфер
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
41 sd.OutputWindow = g_hWnd; // привязываем к нашему окну
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
42 sd.SampleDesc.Count = 1;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
43 sd.SampleDesc.Quality = 0;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
44 sd.Windowed = TRUE; // не полноэкранный режим
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
45
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
46 for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ )
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
47 {
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
48 g_driverType = driverTypes[driverTypeIndex];
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
49 hr = D3D11CreateDeviceAndSwapChain ( NULL, g_driverType, NULL, createDeviceFlags, featureLevels, numFeatureLevels, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext );
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
50 if (SUCCEEDED(hr)) // Если устройства созданы успешно, то выходим из цикла
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
51 break;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
52 }
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
53 if (FAILED(hr)) return hr;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
54
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
55 // Теперь создаем задний буфер. Обратите внимание, в SDK
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
56 // RenderTargetOutput - это передний буфер, а RenderTargetView - задний.
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
57 ID3D11Texture2D* pBackBuffer = NULL;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
58 hr = g_pSwapChain->GetBuffer( 0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer );
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
59 if (FAILED(hr)) return hr;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
60
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
61 // интерфейс g_pd3dDevice будет использоваться для создания остальных объектов
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
62 hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
63 pBackBuffer->Release();
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
64 if (FAILED(hr)) return hr;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
65
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
66 // Подключаем объект заднего буфера к контексту устройства
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
67 g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL );
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
68
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
69 // Настройка вьюпорта
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
70 D3D11_VIEWPORT vp;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
71 vp.Width = (FLOAT)width;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
72 vp.Height = (FLOAT)height;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
73 vp.MinDepth = 0.0f;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
74 vp.MaxDepth = 1.0f;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
75 vp.TopLeftX = 0;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
76 vp.TopLeftY = 0;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
77
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
78 // Подключаем вьюпорт к контексту устройства
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
79 g_pImmediateContext->RSSetViewports (1, &vp);
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
80
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
81 return S_OK;
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
82 }
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
83
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
84 //--------------------------------------------------------------------------------------
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
85 // Удалить все созданные объекты
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
86 //--------------------------------------------------------------------------------------
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
87 void D3D11::CleanupDevice()
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
88 {
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
89 // Сначала отключим контекст устройства, потом отпустим объекты.
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
90 if( g_pImmediateContext ) g_pImmediateContext->ClearState();
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
91
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
92 // удалеям объекты в порядке, обратном тому, в котором создавали.
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
93 if( g_pRenderTargetView ) g_pRenderTargetView->Release();
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
94
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
95 if( g_pSwapChain ) g_pSwapChain->Release();
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
96
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
97 if( g_pImmediateContext ) g_pImmediateContext->Release();
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
98
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
99 if( g_pd3dDevice ) g_pd3dDevice->Release();
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
100 }
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
101
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
102 void Render()
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
103 {
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
104
8cee51ce4382 included directx 11
Ritor1
parents:
diff changeset
105 }