2 * Copyright (c) 2007-2009 SlimDX Group
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 using System.Collections.Generic;
23 using System.Globalization;
24 using SharpDX.Direct3D9;
26 namespace SampleFramework
30 public int AdapterOrdinal
36 public AdapterDetails Details
42 public string Description
48 public List<DisplayMode> DisplayModes
54 public List<DeviceInfo9> Devices
63 DisplayModes = new List<DisplayMode>();
64 Devices = new List<DeviceInfo9>();
70 public DeviceType DeviceType
76 public Capabilities Capabilities
82 public List<SettingsCombo9> DeviceSettings
90 DeviceSettings = new List<SettingsCombo9>();
96 public int AdapterOrdinal
102 public DeviceType DeviceType
108 public Format AdapterFormat
114 public Format BackBufferFormat
126 public List<Format> DepthStencilFormats
132 public List<MultisampleType> MultisampleTypes
138 public List<int> MultisampleQualities
144 public List<PresentInterval> PresentIntervals
150 public AdapterInfo9 AdapterInfo
156 public DeviceInfo9 DeviceInfo
162 public SettingsCombo9()
164 DepthStencilFormats = new List<Format>();
165 MultisampleQualities = new List<int>();
166 MultisampleTypes = new List<MultisampleType>();
167 PresentIntervals = new List<PresentInterval>();
171 class DisplayModeComparer9 : IComparer<DisplayMode>
173 static DisplayModeComparer9 comparer = new DisplayModeComparer9();
175 public static DisplayModeComparer9 Comparer
177 get { return comparer; }
180 public DisplayModeComparer9()
184 public int Compare(DisplayMode x, DisplayMode y)
186 if (x.Width > y.Width)
188 if (x.Width < y.Width)
190 if (x.Height > y.Height)
192 if (x.Height < y.Height)
194 if (x.Format > y.Format)
196 if (x.Format < y.Format)
198 if (x.RefreshRate > y.RefreshRate)
200 if (x.RefreshRate < y.RefreshRate)
207 static class Enumeration9
209 public static DeviceSettings MinimumSettings
215 public static List<AdapterInfo9> Adapters
221 public static bool HasEnumerated
227 public static void Enumerate()
229 HasEnumerated = true;
230 Adapters = new List<AdapterInfo9>();
231 List<Format> adapterFormats = new List<Format>();
232 Format[] allowedAdapterFormats = { Format.X8R8G8B8, Format.X1R5G5B5, Format.R5G6B5,
233 Format.A2R10G10B10 };
235 foreach (AdapterInformation adapter in GraphicsDeviceManager.Direct3D9Object.Adapters) //
237 AdapterInfo9 info = new AdapterInfo9();
238 info.AdapterOrdinal = adapter.Adapter;
239 info.Details = adapter.Details;
241 adapterFormats.Clear();
242 foreach (Format adapterFormat in allowedAdapterFormats)
244 foreach (DisplayMode displayMode in adapter.GetDisplayModes(adapterFormat))
246 if (MinimumSettings != null)
248 if (displayMode.Width < MinimumSettings.BackBufferWidth ||
249 displayMode.Height < MinimumSettings.BackBufferHeight ||
250 displayMode.RefreshRate < MinimumSettings.RefreshRate)
254 info.DisplayModes.Add(displayMode);
256 if (!adapterFormats.Contains(displayMode.Format))
257 adapterFormats.Add(displayMode.Format);
261 if (!adapterFormats.Contains(adapter.CurrentDisplayMode.Format))
262 adapterFormats.Add(adapter.CurrentDisplayMode.Format);
264 info.DisplayModes.Sort(DisplayModeComparer9.Comparer);
266 EnumerateDevices(info, adapterFormats);
268 if (info.Devices.Count > 0)
273 foreach (AdapterInfo9 adapter1 in Adapters)
275 foreach (AdapterInfo9 adapter2 in Adapters)
277 if (adapter1 == adapter2)
279 if (adapter1.Details.Description == adapter2.Details.Description)
290 foreach (AdapterInfo9 info in Adapters)
292 info.Description = info.Details.Description;
294 info.Description += " " + info.AdapterOrdinal.ToString(CultureInfo.CurrentCulture);
298 static void EnumerateDevices(AdapterInfo9 info, List<Format> adapterFormats)
300 DeviceType[] deviceTypes = { DeviceType.Hardware, DeviceType.Reference };
302 foreach (DeviceType deviceType in deviceTypes)
304 if (MinimumSettings != null && MinimumSettings.DeviceType != deviceType)
307 DeviceInfo9 deviceInfo = new DeviceInfo9();
308 deviceInfo.DeviceType = deviceType;
311 deviceInfo.Capabilities = GraphicsDeviceManager.Direct3D9Object.GetDeviceCaps(info.AdapterOrdinal, deviceInfo.DeviceType);
313 EnumerateSettingsCombos(info, deviceInfo, adapterFormats);
315 if (deviceInfo.DeviceSettings.Count > 0)
316 info.Devices.Add(deviceInfo);
320 // #23681 2010.11.17 yyagi: GetDeviceCaps()で例外が発生するモニタに対しては、enumerateをスキップする。
325 static void EnumerateSettingsCombos(AdapterInfo9 adapterInfo, DeviceInfo9 deviceInfo, List<Format> adapterFormats)
327 Format[] backBufferFormats = { Format.A8R8G8B8, Format.X8R8G8B8, Format.A2R10G10B10,
328 Format.R5G6B5, Format.A1R5G5B5, Format.X1R5G5B5 };
330 foreach (Format adapterFormat in adapterFormats)
332 foreach (Format backBufferFormat in backBufferFormats)
334 for (int windowed = 0; windowed < 2; windowed++)
336 if (windowed == 0 && adapterInfo.DisplayModes.Count == 0)
339 if (!GraphicsDeviceManager.Direct3D9Object.CheckDeviceType(adapterInfo.AdapterOrdinal, deviceInfo.DeviceType,
340 adapterFormat, backBufferFormat, (windowed == 1)))
343 if (!GraphicsDeviceManager.Direct3D9Object.CheckDeviceFormat(adapterInfo.AdapterOrdinal,
344 deviceInfo.DeviceType, adapterFormat, Usage.QueryPostPixelShaderBlending,
345 ResourceType.Texture, backBufferFormat))
348 SettingsCombo9 combo = new SettingsCombo9();
349 combo.AdapterOrdinal = adapterInfo.AdapterOrdinal;
350 combo.DeviceType = deviceInfo.DeviceType;
351 combo.AdapterFormat = adapterFormat;
352 combo.BackBufferFormat = backBufferFormat;
353 combo.Windowed = (windowed == 1);
354 combo.AdapterInfo = adapterInfo;
355 combo.DeviceInfo = deviceInfo;
357 BuildDepthStencilFormatList(combo);
358 BuildMultisampleTypeList(combo);
360 if (combo.MultisampleTypes.Count == 0)
363 BuildPresentIntervalList(combo);
365 if (MinimumSettings != null)
367 if (MinimumSettings.BackBufferFormat != Format.Unknown &&
368 MinimumSettings.BackBufferFormat != backBufferFormat)
371 if (MinimumSettings.DepthStencilFormat != Format.Unknown &&
372 !combo.DepthStencilFormats.Contains(MinimumSettings.DepthStencilFormat))
375 if (!combo.MultisampleTypes.Contains(MinimumSettings.MultisampleType))
379 deviceInfo.DeviceSettings.Add(combo);
385 static void BuildDepthStencilFormatList(SettingsCombo9 combo)
387 List<Format> possibleDepthStencilFormats = new List<Format> {
388 Format.D16, Format.D15S1, Format.D24X8,
389 Format.D24S8, Format.D24X4S4, Format.D32 };
391 foreach (Format format in possibleDepthStencilFormats)
393 if (GraphicsDeviceManager.Direct3D9Object.CheckDeviceFormat(combo.AdapterOrdinal, combo.DeviceType, combo.AdapterFormat,
394 Usage.DepthStencil, ResourceType.Surface, format) &&
395 GraphicsDeviceManager.Direct3D9Object.CheckDepthStencilMatch(combo.AdapterOrdinal, combo.DeviceType,
396 combo.AdapterFormat, combo.BackBufferFormat, format))
397 combo.DepthStencilFormats.Add(format);
401 static void BuildMultisampleTypeList(SettingsCombo9 combo)
403 List<MultisampleType> possibleMultisampleTypes = new List<MultisampleType>() {
404 MultisampleType.None, MultisampleType.NonMaskable,
405 MultisampleType.TwoSamples, MultisampleType.ThreeSamples,
406 MultisampleType.FourSamples, MultisampleType.FiveSamples,
407 MultisampleType.SixSamples, MultisampleType.SevenSamples,
408 MultisampleType.EightSamples, MultisampleType.NineSamples,
409 MultisampleType.TenSamples, MultisampleType.ElevenSamples,
410 MultisampleType.TwelveSamples, MultisampleType.ThirteenSamples,
411 MultisampleType.FourteenSamples, MultisampleType.FifteenSamples,
412 MultisampleType.SixteenSamples
416 foreach (MultisampleType type in possibleMultisampleTypes)
418 if (GraphicsDeviceManager.Direct3D9Object.CheckDeviceMultisampleType(combo.AdapterOrdinal, combo.DeviceType,
419 combo.AdapterFormat, combo.Windowed, type, out quality))
421 combo.MultisampleTypes.Add(type);
422 combo.MultisampleQualities.Add(quality);
427 static void BuildPresentIntervalList(SettingsCombo9 combo)
429 List<PresentInterval> possiblePresentIntervals = new List<PresentInterval>() {
430 PresentInterval.Immediate, PresentInterval.Default,
431 PresentInterval.One, PresentInterval.Two,
432 PresentInterval.Three, PresentInterval.Four
435 foreach (PresentInterval interval in possiblePresentIntervals)
437 if (combo.Windowed && (interval == PresentInterval.Two ||
438 interval == PresentInterval.Three || interval == PresentInterval.Four))
441 if (interval == PresentInterval.Default ||
442 (combo.DeviceInfo.Capabilities.PresentationIntervals & interval) != 0)
443 combo.PresentIntervals.Add(interval);