77
88#include " StateManager/dx10SamplerStateCache.h"
99#include " StateManager/dx10StateCache.h"
10+ #include " dx10TextureUtils.h"
1011
1112CHW HW;
1213
@@ -76,27 +77,84 @@ void CHW::CreateDevice(SDL_Window* m_sdlWnd)
7677 Caps.id_vendor = Desc.VendorId ;
7778 Caps.id_device = Desc.DeviceId ;
7879
79- // Select back-buffer & depth-stencil format
80- D3DFORMAT& fTarget = Caps.fTarget ;
81- D3DFORMAT& fDepth = Caps.fDepth ;
80+ #if 0 // def DEBUG
81+ UINT createDeviceFlags = D3D_CREATE_DEVICE_DEBUG;
82+ #else
83+ UINT createDeviceFlags = 0 ;
84+ #endif
85+
86+ HRESULT R;
87+
88+ #ifdef USE_DX11
89+ D3D_FEATURE_LEVEL featureLevels[] =
90+ {
91+ D3D_FEATURE_LEVEL_11_1,
92+ D3D_FEATURE_LEVEL_11_0,
93+ D3D_FEATURE_LEVEL_10_1,
94+ D3D_FEATURE_LEVEL_10_0,
95+ D3D_FEATURE_LEVEL_9_3,
96+ D3D_FEATURE_LEVEL_9_2,
97+ D3D_FEATURE_LEVEL_9_1,
98+ };
99+
100+ constexpr auto count = sizeof (featureLevels) / sizeof (featureLevels[0 ]);
101+
102+ const auto createDevice = [&](const D3D_FEATURE_LEVEL* level, const u32 levels)
103+ {
104+ return D3D11CreateDevice (m_pAdapter, D3D_DRIVER_TYPE_UNKNOWN,
105+ nullptr , createDeviceFlags, level, levels,
106+ D3D11_SDK_VERSION, &pDevice, &FeatureLevel, &pContext);
107+ };
108+
109+ R = createDevice (featureLevels, count);
110+ if (FAILED (R))
111+ R = createDevice (&featureLevels[1 ], count - 1 );
112+ #else
113+ R = D3D10CreateDevice (m_pAdapter, m_DriverType, NULL , createDeviceFlags, D3D10_SDK_VERSION, &pDevice);
114+
115+ pContext = pDevice;
116+ FeatureLevel = D3D_FEATURE_LEVEL_10_0;
117+ if (!FAILED (R))
118+ {
119+ D3DX10GetFeatureLevel1 (pDevice, &pDevice1);
120+ FeatureLevel = D3D_FEATURE_LEVEL_10_1;
121+ }
122+ pContext1 = pDevice1;
123+ #endif
82124
83- // HACK: DX10: Embed hard target format.
84- fTarget = D3DFMT_X8R8G8B8; // No match in DX10. D3DFMT_A8B8G8R8->DXGI_FORMAT_R8G8B8A8_UNORM
85- fDepth = selectDepthStencil (fTarget );
125+ if (FAILED (R))
126+ {
127+ // Fatal error! Cannot create rendering device AT STARTUP !!!
128+ Msg (" Failed to initialize graphics hardware.\n "
129+ " Please try to restart the game.\n "
130+ " CreateDevice returned 0x%08x" , R);
131+ FlushLog ();
132+ MessageBox (nullptr , " Failed to initialize graphics hardware.\n Please try to restart the game." , " Error!" ,
133+ MB_OK | MB_ICONERROR);
134+ TerminateProcess (GetCurrentProcess (), 0 );
135+ };
86136
87137 // Set up the presentation parameters
88138 DXGI_SWAP_CHAIN_DESC& sd = m_ChainDesc;
89139 ZeroMemory (&sd, sizeof (sd));
90140
141+ // Back buffer
91142 sd.BufferDesc .Width = Device.dwWidth ;
92143 sd.BufferDesc .Height = Device.dwHeight ;
93144
94- // Back buffer
95- // . P.BackBufferWidth = dwWidth;
96- // . P.BackBufferHeight = dwHeight;
97145 // TODO: DX10: implement dynamic format selection
98- // sd.BufferDesc.Format = fTarget;
99- sd.BufferDesc .Format = DXGI_FORMAT_R8G8B8A8_UNORM;
146+ constexpr DXGI_FORMAT formats[] =
147+ {
148+ // DXGI_FORMAT_R16G16B16A16_FLOAT,
149+ // DXGI_FORMAT_R10G10B10A2_UNORM,
150+ // DXGI_FORMAT_B8G8R8X8_UNORM,
151+ DXGI_FORMAT_R8G8B8A8_UNORM,
152+ };
153+
154+ // Select back-buffer format
155+ sd.BufferDesc .Format = SelectFormat (D3D_FORMAT_SUPPORT_DISPLAY, formats, std::size (formats));
156+ Caps.fTarget = dx10TextureUtils::ConvertTextureFormat (sd.BufferDesc .Format );
157+
100158 sd.BufferCount = 1 ;
101159
102160 // Multisample
@@ -129,69 +187,13 @@ void CHW::CreateDevice(SDL_Window* m_sdlWnd)
129187
130188 sd.Windowed = bWindowed;
131189
132- // Depth/stencil (DX10 don't need this?)
133- // P.EnableAutoDepthStencil = TRUE;
134- // P.AutoDepthStencilFormat = fDepth;
135- // P.Flags = 0; //. D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;
136-
137190 // Additional set up
138191 sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
139192
140- UINT createDeviceFlags = 0 ;
141- #ifdef DEBUG
142- // createDeviceFlags |= D3Dxx_CREATE_DEVICE_DEBUG;
143- #endif
144- HRESULT R;
145- #ifdef USE_DX11
146- D3D_FEATURE_LEVEL featureLevels[] =
147- {
148- D3D_FEATURE_LEVEL_11_1,
149- D3D_FEATURE_LEVEL_11_0,
150- D3D_FEATURE_LEVEL_10_1,
151- D3D_FEATURE_LEVEL_10_0,
152- D3D_FEATURE_LEVEL_9_3,
153- D3D_FEATURE_LEVEL_9_2,
154- D3D_FEATURE_LEVEL_9_1,
155- };
156-
157- R = D3D11CreateDevice (m_pAdapter,
158- D3D_DRIVER_TYPE_UNKNOWN, // Если мы выбираем конкретный адаптер, то мы обязаны использовать D3D_DRIVER_TYPE_UNKNOWN.
159- nullptr , createDeviceFlags, featureLevels, sizeof (featureLevels) / sizeof (featureLevels[0 ]),
160- D3D11_SDK_VERSION, &pDevice, &FeatureLevel, &pContext);
161-
162- if (FAILED (R))
163- R = D3D11CreateDevice (m_pAdapter,
164- D3D_DRIVER_TYPE_UNKNOWN, // Если мы выбираем конкретный адаптер, то мы обязаны использовать D3D_DRIVER_TYPE_UNKNOWN.
165- nullptr , createDeviceFlags, &featureLevels[1 ], sizeof (featureLevels) / sizeof (featureLevels[0 ]) - 1 ,
166- D3D11_SDK_VERSION, &pDevice, &FeatureLevel, &pContext);
167- #else
168- R = D3D10CreateDevice (m_pAdapter, m_DriverType, NULL , createDeviceFlags, D3D10_SDK_VERSION, &pDevice);
193+ _SHOW_REF (" * CREATE: DeviceREF:" , HW.pDevice );
169194
170- pContext = pDevice;
171- FeatureLevel = D3D_FEATURE_LEVEL_10_0;
172- if (!FAILED (R))
173- {
174- D3DX10GetFeatureLevel1 (pDevice, &pDevice1);
175- FeatureLevel = D3D_FEATURE_LEVEL_10_1;
176- }
177- pContext1 = pDevice1;
178- #endif
179195 R_CHK (m_pFactory->CreateSwapChain (pDevice, &sd, &m_pSwapChain));
180196
181- if (FAILED (R))
182- {
183- // Fatal error! Cannot create rendering device AT STARTUP !!!
184- Msg (" Failed to initialize graphics hardware.\n "
185- " Please try to restart the game.\n "
186- " CreateDevice returned 0x%08x" , R);
187- FlushLog ();
188- MessageBox (nullptr , " Failed to initialize graphics hardware.\n Please try to restart the game." , " Error!" ,
189- MB_OK | MB_ICONERROR);
190- TerminateProcess (GetCurrentProcess (), 0 );
191- };
192-
193- _SHOW_REF (" * CREATE: DeviceREF:" , HW.pDevice );
194-
195197 // Create render target and depth-stencil views here
196198 UpdateViews ();
197199
@@ -258,6 +260,23 @@ void CHW::Reset()
258260 UpdateViews ();
259261}
260262
263+ bool CHW::CheckFormatSupport (const DXGI_FORMAT format, const D3D_FORMAT_SUPPORT feature) const
264+ {
265+ UINT feature_bit = feature;
266+ if (SUCCEEDED (pDevice->CheckFormatSupport (format, &feature_bit)))
267+ return true ;
268+ return false ;
269+ }
270+
271+ DXGI_FORMAT CHW::SelectFormat (D3D_FORMAT_SUPPORT feature, const DXGI_FORMAT formats[], size_t count) const
272+ {
273+ for (u32 i = 0 ; i < count; ++i)
274+ if (CheckFormatSupport (formats[i], feature))
275+ return formats[i];
276+
277+ return DXGI_FORMAT_UNKNOWN;
278+ }
279+
261280D3DFORMAT CHW::selectDepthStencil (D3DFORMAT /* fTarget*/ )
262281{
263282// R3 hack
@@ -278,7 +297,6 @@ void CHW::UpdateViews()
278297 HRESULT R;
279298
280299 // Create a render target view
281- // R_CHK (pDevice->GetRenderTarget (0,&pBaseRT));
282300 ID3DTexture2D* pBuffer;
283301 R = m_pSwapChain->GetBuffer (0 , __uuidof (ID3DTexture2D), (LPVOID*)&pBuffer);
284302 R_CHK (R);
@@ -288,17 +306,27 @@ void CHW::UpdateViews()
288306 R_CHK (R);
289307
290308 // Create Depth/stencil buffer
291- // HACK: DX10: hard depth buffer format
292- // R_CHK (pDevice->GetDepthStencilSurface (&pBaseZB));
293309 ID3DTexture2D* pDepthStencil = NULL ;
294310 D3D_TEXTURE2D_DESC descDepth;
295311 descDepth.Width = sd.BufferDesc .Width ;
296312 descDepth.Height = sd.BufferDesc .Height ;
297313 descDepth.MipLevels = 1 ;
298314 descDepth.ArraySize = 1 ;
299- descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
300- descDepth.SampleDesc .Count = 1 ;
301- descDepth.SampleDesc .Quality = 0 ;
315+
316+ // Select depth-stencil format
317+ // TODO: DX10: test and support other formats
318+ constexpr DXGI_FORMAT formats[] =
319+ {
320+ // DXGI_FORMAT_D32_FLOAT_S8X24_UINT,
321+ DXGI_FORMAT_D24_UNORM_S8_UINT,
322+ // DXGI_FORMAT_D32_FLOAT,
323+ // DXGI_FORMAT_D16_UNORM
324+ };
325+ descDepth.Format = SelectFormat (D3D_FORMAT_SUPPORT_DEPTH_STENCIL, formats, std::size (formats));
326+ Caps.fDepth = dx10TextureUtils::ConvertTextureFormat (descDepth.Format );
327+
328+ descDepth.SampleDesc .Count = sd.SampleDesc .Count ;
329+ descDepth.SampleDesc .Quality = sd.SampleDesc .Quality ;
302330 descDepth.Usage = D3D_USAGE_DEFAULT;
303331 descDepth.BindFlags = D3D_BIND_DEPTH_STENCIL;
304332 descDepth.CPUAccessFlags = 0 ;
@@ -308,8 +336,18 @@ void CHW::UpdateViews()
308336 &pDepthStencil); // [out] Texture
309337 R_CHK (R);
310338
339+ D3D_DEPTH_STENCIL_VIEW_DESC descDSV;
340+ ZeroMemory (&descDSV, sizeof (descDSV));
341+
342+ descDSV.Format = descDepth.Format ;
343+ if (descDepth.SampleDesc .Count > 1 )
344+ descDSV.ViewDimension = D3D_DSV_DIMENSION_TEXTURE2DMS;
345+ else
346+ descDSV.ViewDimension = D3D_DSV_DIMENSION_TEXTURE2D;
347+ descDSV.Texture2D .MipSlice = 0 ;
348+
311349 // Create Depth/stencil view
312- R = pDevice->CreateDepthStencilView (pDepthStencil, NULL , &pBaseZB);
350+ R = pDevice->CreateDepthStencilView (pDepthStencil, &descDSV , &pBaseZB);
313351 R_CHK (R);
314352
315353 _RELEASE (pDepthStencil);
0 commit comments