annotate DirectX11.cpp @ 2411:4106678916e1

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