-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD3DEnumeration.cs
494 lines (417 loc) · 14.7 KB
/
D3DEnumeration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//-----------------------------------------------------------------------------
// File: D3DEnumeration.cs
//
// Desc: Enumerates D3D adapters, devices, modes, etc.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Collections;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
/// <summary>
/// Enumeration of all possible D3D vertex processing types
/// </summary>
public enum VertexProcessingType
{
Software,
Mixed,
Hardware,
PureHardware
}
/// <summary>
/// Info about a display adapter
/// </summary>
public class GraphicsAdapterInfo
{
public int AdapterOrdinal;
public AdapterDetails AdapterDetails;
public ArrayList DisplayModeList = new ArrayList(); // List of D3DDISPLAYMODEs
public ArrayList DeviceInfoList = new ArrayList(); // List of D3DDeviceInfos
public override string ToString() { return AdapterDetails.Description; }
}
/// <summary>
/// Info about a D3D device, including a list of DeviceCombos (see below)
/// that work with the device
/// </summary>
public class GraphicsDeviceInfo
{
public int AdapterOrdinal;
public DeviceType DevType;
public Caps Caps;
public ArrayList DeviceComboList = new ArrayList(); // List of D3DDeviceCombos
public override string ToString() { return DevType.ToString(); }
}
/// <summary>
/// Info about a depth/stencil buffer format that is incompatible with a
/// multisample type
/// </summary>
public class DepthStencilMultiSampleConflict
{
public DepthFormat DepthStencilFormat;
public MultiSampleType MultiSampleType;
}
/// <summary>
/// A combination of adapter format, back buffer format, and windowed/fullscreen
/// that is compatible with a particular D3D device (and the app)
/// </summary>
public class DeviceCombo
{
public int AdapterOrdinal;
public DeviceType DevType;
public Format AdapterFormat;
public Format BackBufferFormat;
public bool IsWindowed;
public ArrayList DepthStencilFormatList = new ArrayList(); // List of D3DFORMATs
public ArrayList MultiSampleTypeList = new ArrayList(); // List of D3DMULTISAMPLE_TYPEs
public ArrayList MultiSampleQualityList = new ArrayList(); // List of ints (maxQuality per multisample type)
public ArrayList DepthStencilMultiSampleConflictList = new ArrayList(); // List of DepthStencilMultiSampleConflicts
public ArrayList VertexProcessingTypeList = new ArrayList(); // List of VertexProcessingTypes
public ArrayList PresentIntervalList = new ArrayList(); // List of D3DPRESENT_INTERVALs
}
/// <summary>
/// Used to sort Displaymodes
/// </summary>
class DisplayModeComparer : System.Collections.IComparer
{
/// <summary>
/// Compare two display modes
/// </summary>
public int Compare(object x, object y)
{
DisplayMode dx = (DisplayMode)x;
DisplayMode dy = (DisplayMode)y;
if (dx.Width > dy.Width)
return 1;
if (dx.Width < dy.Width)
return -1;
if (dx.Height > dy.Height)
return 1;
if (dx.Height < dy.Height)
return -1;
if (dx.Format > dy.Format)
return 1;
if (dx.Format < dy.Format)
return -1;
if (dx.RefreshRate > dy.RefreshRate)
return 1;
if (dx.RefreshRate < dy.RefreshRate)
return -1;
return 0;
}
}
/// <summary>
/// Enumerates available adapters, devices, modes, etc.
/// </summary>
public class D3DEnumeration
{
/// <summary>
/// The confirm device delegate which is used to determine if a device
/// meets the needs of the simulation
/// </summary>
public delegate bool ConfirmDeviceCallbackType(Caps caps,
VertexProcessingType vertexProcessingType, Format adapterFormat,
Format backBufferFormat);
public ConfirmDeviceCallbackType ConfirmDeviceCallback;
public ArrayList AdapterInfoList = new ArrayList(); // List of D3DAdapterInfos
// The following variables can be used to limit what modes, formats,
// etc. are enumerated. Set them to the values you want before calling
// Enumerate().
public int AppMinFullscreenWidth = 640;
public int AppMinFullscreenHeight = 480;
public int AppMinColorChannelBits = 5; // min color bits per channel in adapter format
public int AppMinAlphaChannelBits = 0; // min alpha bits per pixel in back buffer format
public int AppMinDepthBits = 15;
public int AppMinStencilBits = 0;
public bool AppUsesDepthBuffer = true;
public bool AppUsesMixedVP = false; // whether app can take advantage of mixed vp mode
public bool AppRequiresWindowed = false;
public bool AppRequiresFullscreen = false;
/// <summary>
/// Enumerates available D3D adapters, devices, modes, etc.
/// </summary>
public void Enumerate()
{
foreach (AdapterInformation ai in Manager.Adapters)
{
ArrayList adapterFormatList = new ArrayList();
GraphicsAdapterInfo adapterInfo = new GraphicsAdapterInfo();
adapterInfo.AdapterOrdinal = ai.Adapter;
adapterInfo.AdapterDetails = ai.Information;
// Get list of all display modes on this adapter.
// Also build a temporary list of all display adapter formats.
foreach (DisplayMode displayMode in ai.SupportedDisplayModes)
{
if (displayMode.Width < AppMinFullscreenWidth)
continue;
if (displayMode.Height < AppMinFullscreenHeight)
continue;
if (GraphicsUtility.GetColorChannelBits(displayMode.Format) < AppMinColorChannelBits)
continue;
adapterInfo.DisplayModeList.Add(displayMode);
if (!adapterFormatList.Contains(displayMode.Format))
adapterFormatList.Add(displayMode.Format);
}
// Sort displaymode list
DisplayModeComparer dmc = new DisplayModeComparer();
adapterInfo.DisplayModeList.Sort(dmc);
// Get info for each device on this adapter
EnumerateDevices(adapterInfo, adapterFormatList);
// If at least one device on this adapter is available and compatible
// with the app, add the adapterInfo to the list
if (adapterInfo.DeviceInfoList.Count == 0)
continue;
AdapterInfoList.Add(adapterInfo);
}
}
/// <summary>
/// Enumerates D3D devices for a particular adapter
/// </summary>
protected void EnumerateDevices(GraphicsAdapterInfo adapterInfo, ArrayList adapterFormatList)
{
DeviceType[] devTypeArray = new DeviceType[]
{ DeviceType.Hardware, DeviceType.Software, DeviceType.Reference };
foreach (DeviceType devType in devTypeArray)
{
GraphicsDeviceInfo deviceInfo = new GraphicsDeviceInfo();
deviceInfo.AdapterOrdinal = adapterInfo.AdapterOrdinal;
deviceInfo.DevType = devType;
try
{
deviceInfo.Caps = Manager.GetDeviceCaps(adapterInfo.AdapterOrdinal, devType);
}
catch (DirectXException)
{
continue;
}
// Get info for each devicecombo on this device
EnumerateDeviceCombos(deviceInfo, adapterFormatList);
// If at least one devicecombo for this device is found,
// add the deviceInfo to the list
if (deviceInfo.DeviceComboList.Count == 0)
continue;
adapterInfo.DeviceInfoList.Add(deviceInfo);
}
}
/// <summary>
/// Enumerates DeviceCombos for a particular device
/// </summary>
protected void EnumerateDeviceCombos(GraphicsDeviceInfo deviceInfo, ArrayList adapterFormatList)
{
Format[] backBufferFormatArray = new Format[]
{ Format.A8R8G8B8, Format.X8R8G8B8, Format.A2R10G10B10,
Format.R5G6B5, Format.A1R5G5B5, Format.X1R5G5B5,
};
bool[] isWindowedArray = new bool[] { false, true };
// See which adapter formats are supported by this device
foreach (Format adapterFormat in adapterFormatList)
{
foreach (Format backBufferFormat in backBufferFormatArray)
{
if (GraphicsUtility.GetAlphaChannelBits(backBufferFormat) < AppMinAlphaChannelBits)
continue;
foreach (bool isWindowed in isWindowedArray)
{
if (!isWindowed && AppRequiresWindowed)
continue;
if (isWindowed && AppRequiresFullscreen)
continue;
if (!Manager.CheckDeviceType(deviceInfo.AdapterOrdinal, deviceInfo.DevType, adapterFormat, backBufferFormat, isWindowed))
{
continue;
}
// At this point, we have an adapter/device/adapterformat/backbufferformat/iswindowed
// DeviceCombo that is supported by the system. We still need to confirm that it's
// compatible with the app, and find one or more suitable depth/stencil buffer format,
// multisample type, vertex processing type, and present interval.
DeviceCombo deviceCombo = new DeviceCombo();
deviceCombo.AdapterOrdinal = deviceInfo.AdapterOrdinal;
deviceCombo.DevType = deviceInfo.DevType;
deviceCombo.AdapterFormat = adapterFormat;
deviceCombo.BackBufferFormat = backBufferFormat;
deviceCombo.IsWindowed = isWindowed;
if (AppUsesDepthBuffer)
{
BuildDepthStencilFormatList(deviceCombo);
if (deviceCombo.DepthStencilFormatList.Count == 0)
continue;
}
BuildMultiSampleTypeList(deviceCombo);
if (deviceCombo.MultiSampleTypeList.Count == 0)
continue;
BuildDepthStencilMultiSampleConflictList(deviceCombo);
BuildVertexProcessingTypeList(deviceInfo, deviceCombo);
if (deviceCombo.VertexProcessingTypeList.Count == 0)
continue;
BuildPresentIntervalList(deviceInfo, deviceCombo);
if (deviceCombo.PresentIntervalList.Count == 0)
continue;
deviceInfo.DeviceComboList.Add(deviceCombo);
}
}
}
}
/// <summary>
/// Adds all depth/stencil formats that are compatible with the device and app to
/// the given deviceCombo
/// </summary>
public void BuildDepthStencilFormatList(DeviceCombo deviceCombo)
{
DepthFormat[] depthStencilFormatArray =
{
DepthFormat.D16,
DepthFormat.D15S1,
DepthFormat.D24X8,
DepthFormat.D24S8,
DepthFormat.D24X4S4,
DepthFormat.D32,
};
foreach (DepthFormat depthStencilFmt in depthStencilFormatArray)
{
if (GraphicsUtility.GetDepthBits(depthStencilFmt) < AppMinDepthBits)
continue;
if (GraphicsUtility.GetStencilBits(depthStencilFmt) < AppMinStencilBits)
continue;
if (Manager.CheckDeviceFormat(deviceCombo.AdapterOrdinal, deviceCombo.DevType, deviceCombo.AdapterFormat,
Usage.DepthStencil, ResourceType.Surface, depthStencilFmt))
{
if (Manager.CheckDepthStencilMatch(deviceCombo.AdapterOrdinal, deviceCombo.DevType,
deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat, depthStencilFmt))
{
deviceCombo.DepthStencilFormatList.Add(depthStencilFmt);
}
}
}
}
/// <summary>
/// Adds all multisample types that are compatible with the device and app to
/// the given deviceCombo
/// </summary>
public void BuildMultiSampleTypeList(DeviceCombo deviceCombo)
{
MultiSampleType[] msTypeArray = {
MultiSampleType.None,
MultiSampleType.NonMaskable,
MultiSampleType.TwoSamples,
MultiSampleType.ThreeSamples,
MultiSampleType.FourSamples,
MultiSampleType.FiveSamples,
MultiSampleType.SixSamples,
MultiSampleType.SevenSamples,
MultiSampleType.EightSamples,
MultiSampleType.NineSamples,
MultiSampleType.TenSamples,
MultiSampleType.ElevenSamples,
MultiSampleType.TwelveSamples,
MultiSampleType.ThirteenSamples,
MultiSampleType.FourteenSamples,
MultiSampleType.FifteenSamples,
MultiSampleType.SixteenSamples,
};
foreach (MultiSampleType msType in msTypeArray)
{
int result;
int qualityLevels = 0;
if (Manager.CheckDeviceMultiSampleType(deviceCombo.AdapterOrdinal, deviceCombo.DevType,
deviceCombo.BackBufferFormat, deviceCombo.IsWindowed, msType, out result, ref qualityLevels))
{
deviceCombo.MultiSampleTypeList.Add(msType);
deviceCombo.MultiSampleQualityList.Add(qualityLevels);
}
}
}
/// <summary>
/// Finds any depthstencil formats that are incompatible with multisample types and
/// builds a list of them.
/// </summary>
public void BuildDepthStencilMultiSampleConflictList(DeviceCombo deviceCombo)
{
DepthStencilMultiSampleConflict DSMSConflict;
foreach (DepthFormat dsFmt in deviceCombo.DepthStencilFormatList)
{
foreach (MultiSampleType msType in deviceCombo.MultiSampleTypeList)
{
if (!Manager.CheckDeviceMultiSampleType(deviceCombo.AdapterOrdinal,
deviceCombo.DevType, (Format)dsFmt, deviceCombo.IsWindowed, msType))
{
DSMSConflict = new DepthStencilMultiSampleConflict();
DSMSConflict.DepthStencilFormat = dsFmt;
DSMSConflict.MultiSampleType = msType;
deviceCombo.DepthStencilMultiSampleConflictList.Add(DSMSConflict);
}
}
}
}
/// <summary>
/// Adds all vertex processing types that are compatible with the device and app to
/// the given deviceCombo
/// </summary>
public void BuildVertexProcessingTypeList(GraphicsDeviceInfo deviceInfo, DeviceCombo deviceCombo)
{
if (deviceInfo.Caps.DeviceCaps.SupportsHardwareTransformAndLight)
{
if (deviceInfo.Caps.DeviceCaps.SupportsPureDevice)
{
if (ConfirmDeviceCallback == null ||
ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.PureHardware,
deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat))
{
deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.PureHardware);
}
}
if (ConfirmDeviceCallback == null ||
ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Hardware,
deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat))
{
deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.Hardware);
}
if (AppUsesMixedVP && (ConfirmDeviceCallback == null ||
ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Mixed,
deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat)))
{
deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.Mixed);
}
}
if (ConfirmDeviceCallback == null ||
ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Software,
deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat))
{
deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.Software);
}
}
/// <summary>
/// Adds all present intervals that are compatible with the device and app to
/// the given deviceCombo
/// </summary>
public void BuildPresentIntervalList(GraphicsDeviceInfo deviceInfo, DeviceCombo deviceCombo)
{
PresentInterval[] piArray = {
PresentInterval.Immediate,
PresentInterval.Default,
PresentInterval.One,
PresentInterval.Two,
PresentInterval.Three,
PresentInterval.Four,
};
foreach (PresentInterval pi in piArray)
{
if (deviceCombo.IsWindowed)
{
if (pi == PresentInterval.Two ||
pi == PresentInterval.Three ||
pi == PresentInterval.Four)
{
// These intervals are not supported in windowed mode.
continue;
}
}
// Note that PresentInterval.Default is zero, so you
// can't do a caps check for it -- it is always available.
if (pi == PresentInterval.Default ||
(deviceInfo.Caps.PresentationIntervals & pi) != (PresentInterval)0)
{
deviceCombo.PresentIntervalList.Add(pi);
}
}
}
}