Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
SwapChain.cpp
Ir a la documentación de este archivo.
1
6#include "SwapChain.h"
7#include "Device.h"
8#include "DeviceContext.h"
9#include "Texture.h"
10#include "Window.h"
11
12HRESULT
14 DeviceContext& deviceContext,
15 Texture& backBuffer,
16 Window window) {
17 // Check if Window is valid
18 if (!window.m_hWnd) {
19 ERROR("SwapChain", "init", "Invalid window handle. (m_hWnd is nullptr)");
20 return E_POINTER;
21 }
22
23 HRESULT hr = S_OK;
24
25 // Create the swap chain device and context
26 unsigned int createDeviceFlags = 0;
27#ifdef _DEBUG
28 createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
29#endif
30
31 D3D_DRIVER_TYPE driverTypes[] = {
32 D3D_DRIVER_TYPE_HARDWARE,
33 D3D_DRIVER_TYPE_WARP,
34 D3D_DRIVER_TYPE_REFERENCE,
35 };
36 unsigned int numDriverTypes = ARRAYSIZE(driverTypes);
37
38 D3D_FEATURE_LEVEL featureLevels[] = {
39 D3D_FEATURE_LEVEL_11_0,
40 D3D_FEATURE_LEVEL_10_1,
41 D3D_FEATURE_LEVEL_10_0,
42 };
43 unsigned int numFeatureLevels = ARRAYSIZE(featureLevels);
44
45 // Create the device
46 for (unsigned int driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++) {
47 D3D_DRIVER_TYPE driverType = driverTypes[driverTypeIndex];
48 hr = D3D11CreateDevice(nullptr,
49 driverType,
50 nullptr,
51 createDeviceFlags,
52 featureLevels,
53 numFeatureLevels,
54 D3D11_SDK_VERSION,
55 &device.m_device,
57 &deviceContext.m_deviceContext);
58
59 if (SUCCEEDED(hr)) {
60 MESSAGE("SwapChain", "init", "Device created successfully.");
61 break;
62 }
63 }
64
65 if (FAILED(hr)) {
66 ERROR("SwapChain", "init",
67 ("Failed to create D3D11 device. HRESULT: " + std::to_string(hr)).c_str());
68 return hr;
69 }
70
71 // Config the MSAA settings
72 m_sampleCount = 4;
73 hr = device.m_device->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM,
76 if (FAILED(hr) || m_qualityLevels == 0) {
77 ERROR("SwapChain", "init",
78 ("MSAA not supported or invalid quality level. HRESULT: " + std::to_string(hr)).c_str());
79 return hr;
80 }
81
82 // Config the swap chain description
83 DXGI_SWAP_CHAIN_DESC sd;
84 memset(&sd, 0, sizeof(sd));
85 sd.BufferCount = 1;
86 sd.BufferDesc.Width = window.m_width;
87 sd.BufferDesc.Height = window.m_height;
88 sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
89 sd.BufferDesc.RefreshRate.Numerator = 60;
90 sd.BufferDesc.RefreshRate.Denominator = 1;
91 sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
92 sd.OutputWindow = window.m_hWnd;
93 sd.Windowed = TRUE;
94 sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
95 sd.SampleDesc.Count = m_sampleCount;
96 sd.SampleDesc.Quality = m_qualityLevels - 1;
97
98 // Get the DXGI factory
99 hr = device.m_device->QueryInterface(__uuidof(IDXGIDevice), (void**)&m_dxgiDevice);
100 if (FAILED(hr)) {
101 ERROR("SwapChain", "init",
102 ("Failed to query IDXGIDevice. HRESULT: " + std::to_string(hr)).c_str());
103 return hr;
104 }
105
106 hr = m_dxgiDevice->GetAdapter(&m_dxgiAdapter);
107 if (FAILED(hr)) {
108 ERROR("SwapChain", "init",
109 ("Failed to get IDXGIAdapter. HRESULT: " + std::to_string(hr)).c_str());
110 return hr;
111 }
112
113 hr = m_dxgiAdapter->GetParent(__uuidof(IDXGIFactory),
114 reinterpret_cast<void**>(&m_dxgiFactory));
115 if (FAILED(hr)) {
116 ERROR("SwapChain", "init",
117 ("Failed to get IDXGIFactory. HRESULT: " + std::to_string(hr)).c_str());
118 return hr;
119 }
120
121 // Create the swap chain
122 hr = m_dxgiFactory->CreateSwapChain(device.m_device, &sd, &m_swapChain);
123
124 if (FAILED(hr)) {
125 ERROR("SwapChain", "init",
126 ("Failed to create swap chain. HRESULT: " + std::to_string(hr)).c_str());
127 return hr;
128 }
129
130 // Get the backbuffer
131 hr = m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D),
132 reinterpret_cast<void**>(&backBuffer));
133 if (FAILED(hr)) {
134 ERROR("SwapChain", "init",
135 ("Failed to get back buffer. HRESULT: " + std::to_string(hr)).c_str());
136 return hr;
137 }
138
139 return S_OK;
140}
141
142void
144 if (m_swapChain) {
146 }
147 if (m_dxgiDevice) {
149 }
150 if (m_dxgiAdapter) {
152 }
153 if (m_dxgiFactory) {
155 }
156}
157
158void
160 if (m_swapChain) {
161 HRESULT hr = m_swapChain->Present(0, 0);
162 if (FAILED(hr)) {
163 ERROR("SwapChain", "present",
164 ("Failed to present swap chain. HRESULT: " + std::to_string(hr)).c_str());
165 }
166 } else {
167 ERROR("SwapChain", "present", "Swap chain is not initialized.");
168 }
169}
170
171HRESULT
172SwapChain::resizeBuffers(unsigned int width, unsigned int height) {
173 if (!m_swapChain) {
174 ERROR("SwapChain", "resizeBuffers", "Swap chain is not initialized.");
175 return E_POINTER;
176 }
177
178 // 0,0 y DXGI_FORMAT_UNKNOWN = mantener cantidad de buffers y formato actual
179 HRESULT hr = m_swapChain->ResizeBuffers(
180 0,
181 width,
182 height,
183 DXGI_FORMAT_UNKNOWN,
184 0
185 );
186
187 if (FAILED(hr)) {
188 ERROR("SwapChain", "resizeBuffers",
189 ("ResizeBuffers failed. HRESULT: " + std::to_string(hr)).c_str());
190 return hr;
191 }
192
193 return S_OK;
194}
195
197{
198 if (!m_swapChain) {
199 ERROR("SwapChain", "getBackBuffer", "Swap chain is not initialized.");
200 return E_POINTER;
201 }
202
203 // IMPORTANTE: backBuffer debe ser un ID3D11Texture2D* internamente
204 HRESULT hr = m_swapChain->GetBuffer(
205 0, __uuidof(ID3D11Texture2D),
206 reinterpret_cast<void**>(&backBuffer)
207 );
208
209 if (FAILED(hr)) {
210 ERROR("SwapChain", "getBackBuffer",
211 ("Failed to get back buffer. HRESULT: " + std::to_string(hr)).c_str());
212 return hr;
213 }
214
215 return S_OK;
216}
217
218
Declara la API de DeviceContext dentro del subsistema Core.
Declara la API de Device dentro del subsistema Core.
#define SAFE_RELEASE(x)
#define MESSAGE(classObj, method, state)
#define ERROR(classObj, method, errorMSG)
Declara la API de SwapChain dentro del subsistema Core.
Declara la API de Texture dentro del subsistema Core.
Declara la API de Window dentro del subsistema Core.
ID3D11DeviceContext * m_deviceContext
Puntero al contexto inmediato de Direct3D 11.
Encapsula un ID3D11Device y facilita la creación de recursos gráficos en Direct3D 11.
Definition Device.h:21
ID3D11Device * m_device
Puntero al dispositivo Direct3D 11.
Definition Device.h:146
HRESULT init(Device &device, DeviceContext &deviceContext, Texture &backBuffer, Window window)
Inicializa el Swap Chain y obtiene el back buffer.
Definition SwapChain.cpp:13
void present()
Presenta el back buffer en pantalla.
IDXGISwapChain * m_swapChain
Objeto principal del Swap Chain en Direct3D 11.
Definition SwapChain.h:111
void destroy()
Libera todos los recursos asociados al Swap Chain.
IDXGIFactory * m_dxgiFactory
Interfaz DXGI para la fábrica (creación de swap chains).
Definition SwapChain.h:149
unsigned int m_qualityLevels
Niveles de calidad soportados para la configuración de MSAA.
Definition SwapChain.h:134
HRESULT resizeBuffers(unsigned int width, unsigned int height)
IDXGIDevice * m_dxgiDevice
Interfaz DXGI para el dispositivo.
Definition SwapChain.h:139
HRESULT getBackBuffer(Texture &backBuffer)
IDXGIAdapter * m_dxgiAdapter
Interfaz DXGI para el adaptador (GPU).
Definition SwapChain.h:144
D3D_FEATURE_LEVEL m_featureLevel
Nivel de características de Direct3D soportado por el dispositivo.
Definition SwapChain.h:122
unsigned int m_sampleCount
Número de muestras para MSAA.
Definition SwapChain.h:129
Encapsula una textura 2D en Direct3D 11, incluyendo su recurso y vista como Shader Resource.
Definition Texture.h:24
Encapsula la creacion y administracion de la ventana principal de Win32.
Definition Window.h:20
unsigned int m_width
Ancho actual del area cliente.
Definition Window.h:47
unsigned int m_height
Alto actual del area cliente.
Definition Window.h:48
HWND m_hWnd
Handle de la ventana nativa.
Definition Window.h:46