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
23 using SharpDX.Direct3D9;
24 namespace SampleFramework
27 /// Contains settings for creating a 3D device.
29 public class DeviceSettings : ICloneable
32 /// Gets or sets the adapter ordinal.
34 /// <value>The adapter ordinal.</value>
35 public int AdapterOrdinal
42 /// Gets or sets the type of the device.
44 /// <value>The type of the device.</value>
45 public DeviceType DeviceType
52 /// Gets or sets the refresh rate.
54 /// <value>The refresh rate.</value>
55 public int RefreshRate
62 /// Gets or sets the width of the back buffer.
64 /// <value>The width of the back buffer.</value>
65 public int BackBufferWidth
72 /// Gets or sets the height of the back buffer.
74 /// <value>The height of the back buffer.</value>
75 public int BackBufferHeight
82 /// Gets or sets the back buffer format.
84 /// <value>The back buffer format.</value>
85 public Format BackBufferFormat
92 /// Gets or sets the back buffer count.
94 /// <value>The back buffer count.</value>
95 public int BackBufferCount
102 /// Gets or sets a value indicating whether the device is windowed.
104 /// <value><c>true</c> if windowed; otherwise, <c>false</c>.</value>
112 /// Gets or sets a value indicating whether VSync is enabled.
114 /// <value><c>true</c> if VSync is enabled; otherwise, <c>false</c>.</value>
115 public bool EnableVSync
122 /// Gets or sets a value indicating whether this <see cref="DeviceSettings"/> is multithreaded.
124 /// <value><c>true</c> if multithreaded; otherwise, <c>false</c>.</value>
125 /// <remarks>This only has an effect for Direct3D9 devices.</remarks>
126 public bool Multithreaded
133 /// Gets or sets the multisample type.
135 /// <value>The multisample type.</value>
136 public MultisampleType MultisampleType
143 /// Gets or sets the multisample quality.
145 /// <value>The multisample quality.</value>
146 public int MultisampleQuality
153 /// Gets or sets the depth stencil format.
155 /// <value>The depth stencil format.</value>
156 public Format DepthStencilFormat
163 /// Gets or sets the Direct3D9 specific settings.
165 /// <value>The Direct3D9 specific settings.</value>
166 internal Direct3D9Settings Direct3D9
173 /// Initializes a new instance of the <see cref="DeviceSettings"/> class.
175 public DeviceSettings()
178 DeviceType = DeviceType.Hardware;
179 BackBufferFormat = Format.Unknown;
181 MultisampleType = MultisampleType.None;
182 DepthStencilFormat = Format.Unknown;
188 /// Creates a new object that is a copy of the current instance.
191 /// A new object that is a copy of this instance.
193 public DeviceSettings Clone()
195 DeviceSettings result = new DeviceSettings();
196 result.DeviceType = DeviceType;
197 result.RefreshRate = RefreshRate;
198 result.BackBufferCount = BackBufferCount;
199 result.BackBufferFormat = BackBufferFormat;
200 result.BackBufferHeight = BackBufferHeight;
201 result.BackBufferWidth = BackBufferWidth;
202 result.DepthStencilFormat = DepthStencilFormat;
203 result.MultisampleQuality = MultisampleQuality;
204 result.MultisampleType = MultisampleType;
205 result.Windowed = Windowed;
206 result.EnableVSync = EnableVSync;
207 result.AdapterOrdinal = AdapterOrdinal;
208 result.Multithreaded = Multithreaded;
210 if (Direct3D9 != null)
211 result.Direct3D9 = Direct3D9.Clone();
217 /// Creates a new object that is a copy of the current instance.
220 /// A new object that is a copy of this instance.
222 object ICloneable.Clone()
228 /// Finds valid device settings based upon the desired settings.
230 /// <param name="settings">The desired settings.</param>
231 /// <returns>The best valid device settings matching the input settings.</returns>
232 public static DeviceSettings FindValidSettings( DeviceSettings settings )
236 GraphicsDeviceManager.EnsureD3D9();
240 throw new NoCompatibleDevicesException( "Could not initialize Direct3D9.", e );
243 if( !Enumeration9.HasEnumerated )
244 Enumeration9.Enumerate();
246 DeviceSettings newSettings = settings.Clone();
247 Direct3D9Settings d3d9 = FindValidD3D9Settings( settings );
248 newSettings.Direct3D9 = d3d9;
252 static Direct3D9Settings FindValidD3D9Settings(DeviceSettings settings)
254 Direct3D9Settings optimal = Direct3D9Settings.BuildOptimalSettings(settings);
256 SettingsCombo9 bestCombo = null;
257 float bestRanking = -1.0f;
259 foreach (AdapterInfo9 adapterInfo in Enumeration9.Adapters)
261 DisplayMode desktopMode = GraphicsDeviceManager.Direct3D9Object.GetAdapterDisplayMode(adapterInfo.AdapterOrdinal);
262 foreach (DeviceInfo9 deviceInfo in adapterInfo.Devices)
264 foreach (SettingsCombo9 combo in deviceInfo.DeviceSettings)
266 if (combo.Windowed && combo.AdapterFormat != desktopMode.Format)
269 float ranking = Direct3D9Settings.RankSettingsCombo(combo, optimal, desktopMode);
270 if (ranking > bestRanking)
273 bestRanking = ranking;
279 if (bestCombo == null)
280 throw new NoCompatibleDevicesException("No compatible Direct3D9 devices found.");
282 return Direct3D9Settings.BuildValidSettings(bestCombo, optimal);