Skip to content

Commit f3b5bd6

Browse files
committed
Layers/xrRenderDX10/dx10HW.cpp: HW refactor
Implementation for format support check Moved device creation code to be earlier In future we can support more formats.
1 parent fc3f219 commit f3b5bd6

File tree

5 files changed

+142
-75
lines changed

5 files changed

+142
-75
lines changed

src/Layers/xrRender/HW.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class CHW
126126
void UpdateViews();
127127
#endif
128128
#if defined(USE_DX10) || defined(USE_DX11)
129+
bool CheckFormatSupport(DXGI_FORMAT format, D3D_FORMAT_SUPPORT feature) const;
130+
DXGI_FORMAT SelectFormat(D3D_FORMAT_SUPPORT feature, const DXGI_FORMAT formats[], size_t count) const;
129131
virtual void OnAppActivate();
130132
virtual void OnAppDeactivate();
131133
#endif // USE_DX10

src/Layers/xrRenderDX10/DXCommonTypes.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ typedef ID3D11DeviceContext ID3DDeviceContext;
8888
#define D3D_CULL_FRONT D3D11_CULL_FRONT
8989
#define D3D_CULL_BACK D3D11_CULL_BACK
9090

91+
#define D3D_CREATE_DEVICE_DEBUG D3D11_CREATE_DEVICE_DEBUG
92+
9193
#define D3D_COMPARISON_NEVER D3D11_COMPARISON_NEVER
9294
#define D3D_COMPARISON_LESS D3D11_COMPARISON_LESS
9395
#define D3D_COMPARISON_EQUAL D3D11_COMPARISON_EQUAL
@@ -147,6 +149,10 @@ typedef ID3D11DeviceContext ID3DDeviceContext;
147149
#define D3D_FILL_SOLID D3D11_FILL_SOLID
148150
#define D3D_FILL_WIREFRAME D3D11_FILL_WIREFRAME
149151

152+
#define D3D_FORMAT_SUPPORT D3D11_FORMAT_SUPPORT
153+
#define D3D_FORMAT_SUPPORT_DEPTH_STENCIL D3D11_FORMAT_SUPPORT_DEPTH_STENCIL
154+
#define D3D_FORMAT_SUPPORT_DISPLAY D3D11_FORMAT_SUPPORT_DISPLAY
155+
150156
#define D3D_INPUT_PER_VERTEX_DATA D3D11_INPUT_PER_VERTEX_DATA
151157

152158
#define D3D_BIND_INDEX_BUFFER D3D11_BIND_INDEX_BUFFER
@@ -342,6 +348,8 @@ typedef ID3D10Resource ID3DResource;
342348
#define D3D_CULL_FRONT D3D10_CULL_FRONT
343349
#define D3D_CULL_BACK D3D10_CULL_BACK
344350

351+
#define D3D_CREATE_DEVICE_DEBUG D3D10_CREATE_DEVICE_DEBUG
352+
345353
#define D3D_COMPARISON_NEVER D3D10_COMPARISON_NEVER
346354
#define D3D_COMPARISON_LESS D3D10_COMPARISON_LESS
347355
#define D3D_COMPARISON_EQUAL D3D10_COMPARISON_EQUAL
@@ -401,6 +409,10 @@ typedef ID3D10Resource ID3DResource;
401409
#define D3D_FILL_SOLID D3D10_FILL_SOLID
402410
#define D3D_FILL_WIREFRAME D3D10_FILL_WIREFRAME
403411

412+
#define D3D_FORMAT_SUPPORT D3D10_FORMAT_SUPPORT
413+
#define D3D_FORMAT_SUPPORT_DEPTH_STENCIL D3D10_FORMAT_SUPPORT_DEPTH_STENCIL
414+
#define D3D_FORMAT_SUPPORT_DISPLAY D3D10_FORMAT_SUPPORT_DISPLAY
415+
404416
#define D3D_INPUT_PER_VERTEX_DATA D3D10_INPUT_PER_VERTEX_DATA
405417

406418
#define D3D_BIND_INDEX_BUFFER D3D10_BIND_INDEX_BUFFER

src/Layers/xrRenderDX10/dx10HW.cpp

Lines changed: 113 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "StateManager/dx10SamplerStateCache.h"
99
#include "StateManager/dx10StateCache.h"
10+
#include "dx10TextureUtils.h"
1011

1112
CHW 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.\nPlease 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.\nPlease 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+
261280
D3DFORMAT 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);

src/Layers/xrRenderDX10/dx10TextureUtils.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ TextureFormatPairs TextureFormatList[] = {
6565
// D3DFMT_D32 Not available
6666
// D3DFMT_D15S1 Not available
6767
// D3DFMT_D24S8 Not available
68+
{D3DFMT_D24S8, DXGI_FORMAT_D24_UNORM_S8_UINT}, // XXX: documentation says that D3DFMT_D24S8 is unavailable
6869
{D3DFMT_D24X8, DXGI_FORMAT_R24G8_TYPELESS}, // DXGI_FORMAT_D24_UNORM_S8_UINT}, // Not available
6970
// D3DFMT_D24X4S4 Not available
7071
// D3DFMT_D16 DXGI_FORMAT_D16_UNORM
@@ -123,4 +124,17 @@ DXGI_FORMAT ConvertTextureFormat(D3DFORMAT dx9FMT)
123124
VERIFY(!"ConvertTextureFormat didn't find appropriate dx10 texture format!");
124125
return DXGI_FORMAT_UNKNOWN;
125126
}
127+
128+
D3DFORMAT ConvertTextureFormat(DXGI_FORMAT dx10FMT)
129+
{
130+
int arrayLength = sizeof(TextureFormatList) / sizeof(TextureFormatList[0]);
131+
for (int i = 0; i < arrayLength; ++i)
132+
{
133+
if (TextureFormatList[i].m_dx10FMT == dx10FMT)
134+
return TextureFormatList[i].m_dx9FMT;
135+
}
136+
137+
VERIFY(!"ConvertTextureFormat didn't find appropriate dx9 texture format!");
138+
return D3DFMT_UNKNOWN;
139+
}
126140
}

src/Layers/xrRenderDX10/dx10TextureUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace dx10TextureUtils
66
{
77
DXGI_FORMAT ConvertTextureFormat(D3DFORMAT dx9FMT);
8+
D3DFORMAT ConvertTextureFormat(DXGI_FORMAT dx10FMT);
89
}
910

1011
#endif // dx10TextureUtils_included

0 commit comments

Comments
 (0)