2 * Copyright (c) 2007-2009 SlimDX Group
\r
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
5 * of this software and associated documentation files (the "Software"), to deal
\r
6 * in the Software without restriction, including without limitation the rights
\r
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
8 * copies of the Software, and to permit persons to whom the Software is
\r
9 * furnished to do so, subject to the following conditions:
\r
11 * The above copyright notice and this permission notice shall be included in
\r
12 * all copies or substantial portions of the Software.
\r
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
23 using SlimDX.Direct3D9;
\r
24 namespace SampleFramework
\r
27 /// Contains settings for creating a 3D device.
\r
29 public class DeviceSettings : ICloneable
\r
32 /// Gets or sets the adapter ordinal.
\r
34 /// <value>The adapter ordinal.</value>
\r
35 public int AdapterOrdinal
\r
42 /// Gets or sets the type of the device.
\r
44 /// <value>The type of the device.</value>
\r
45 public DeviceType DeviceType
\r
52 /// Gets or sets the refresh rate.
\r
54 /// <value>The refresh rate.</value>
\r
55 public int RefreshRate
\r
62 /// Gets or sets the width of the back buffer.
\r
64 /// <value>The width of the back buffer.</value>
\r
65 public int BackBufferWidth
\r
72 /// Gets or sets the height of the back buffer.
\r
74 /// <value>The height of the back buffer.</value>
\r
75 public int BackBufferHeight
\r
82 /// Gets or sets the back buffer format.
\r
84 /// <value>The back buffer format.</value>
\r
85 public Format BackBufferFormat
\r
92 /// Gets or sets the back buffer count.
\r
94 /// <value>The back buffer count.</value>
\r
95 public int BackBufferCount
\r
102 /// Gets or sets a value indicating whether the device is windowed.
\r
104 /// <value><c>true</c> if windowed; otherwise, <c>false</c>.</value>
\r
105 public bool Windowed
\r
112 /// Gets or sets a value indicating whether VSync is enabled.
\r
114 /// <value><c>true</c> if VSync is enabled; otherwise, <c>false</c>.</value>
\r
115 public bool EnableVSync
\r
122 /// Gets or sets a value indicating whether this <see cref="DeviceSettings"/> is multithreaded.
\r
124 /// <value><c>true</c> if multithreaded; otherwise, <c>false</c>.</value>
\r
125 /// <remarks>This only has an effect for Direct3D9 devices.</remarks>
\r
126 public bool Multithreaded
\r
133 /// Gets or sets the multisample type.
\r
135 /// <value>The multisample type.</value>
\r
136 public MultisampleType MultisampleType
\r
143 /// Gets or sets the multisample quality.
\r
145 /// <value>The multisample quality.</value>
\r
146 public int MultisampleQuality
\r
153 /// Gets or sets the depth stencil format.
\r
155 /// <value>The depth stencil format.</value>
\r
156 public Format DepthStencilFormat
\r
163 /// Gets or sets the Direct3D9 specific settings.
\r
165 /// <value>The Direct3D9 specific settings.</value>
\r
166 internal Direct3D9Settings Direct3D9
\r
173 /// Initializes a new instance of the <see cref="DeviceSettings"/> class.
\r
175 public DeviceSettings()
\r
177 // set sane defaults
\r
178 DeviceType = DeviceType.Hardware;
\r
179 BackBufferFormat = Format.Unknown;
\r
180 BackBufferCount = 1;
\r
181 MultisampleType = MultisampleType.None;
\r
182 DepthStencilFormat = Format.Unknown;
\r
184 EnableVSync = true;
\r
188 /// Creates a new object that is a copy of the current instance.
\r
191 /// A new object that is a copy of this instance.
\r
193 public DeviceSettings Clone()
\r
195 DeviceSettings result = new DeviceSettings();
\r
196 result.DeviceType = DeviceType;
\r
197 result.RefreshRate = RefreshRate;
\r
198 result.BackBufferCount = BackBufferCount;
\r
199 result.BackBufferFormat = BackBufferFormat;
\r
200 result.BackBufferHeight = BackBufferHeight;
\r
201 result.BackBufferWidth = BackBufferWidth;
\r
202 result.DepthStencilFormat = DepthStencilFormat;
\r
203 result.MultisampleQuality = MultisampleQuality;
\r
204 result.MultisampleType = MultisampleType;
\r
205 result.Windowed = Windowed;
\r
206 result.EnableVSync = EnableVSync;
\r
207 result.AdapterOrdinal = AdapterOrdinal;
\r
208 result.Multithreaded = Multithreaded;
\r
210 if (Direct3D9 != null)
\r
211 result.Direct3D9 = Direct3D9.Clone();
\r
217 /// Creates a new object that is a copy of the current instance.
\r
220 /// A new object that is a copy of this instance.
\r
222 object ICloneable.Clone()
\r
228 /// Finds valid device settings based upon the desired settings.
\r
230 /// <param name="settings">The desired settings.</param>
\r
231 /// <returns>The best valid device settings matching the input settings.</returns>
\r
232 public static DeviceSettings FindValidSettings( DeviceSettings settings )
\r
236 GraphicsDeviceManager.EnsureD3D9();
\r
238 catch( Exception e )
\r
240 throw new NoCompatibleDevicesException( "Could not initialize Direct3D9.", e );
\r
243 if( !Enumeration9.HasEnumerated )
\r
244 Enumeration9.Enumerate();
\r
246 DeviceSettings newSettings = settings.Clone();
\r
247 Direct3D9Settings d3d9 = FindValidD3D9Settings( settings );
\r
248 newSettings.Direct3D9 = d3d9;
\r
249 return newSettings;
\r
252 static Direct3D9Settings FindValidD3D9Settings(DeviceSettings settings)
\r
254 Direct3D9Settings optimal = Direct3D9Settings.BuildOptimalSettings(settings);
\r
256 SettingsCombo9 bestCombo = null;
\r
257 float bestRanking = -1.0f;
\r
259 foreach (AdapterInfo9 adapterInfo in Enumeration9.Adapters)
\r
261 DisplayMode desktopMode = GraphicsDeviceManager.Direct3D9Object.GetAdapterDisplayMode(adapterInfo.AdapterOrdinal);
\r
262 foreach (DeviceInfo9 deviceInfo in adapterInfo.Devices)
\r
264 foreach (SettingsCombo9 combo in deviceInfo.DeviceSettings)
\r
266 if (combo.Windowed && combo.AdapterFormat != desktopMode.Format)
\r
269 float ranking = Direct3D9Settings.RankSettingsCombo(combo, optimal, desktopMode);
\r
270 if (ranking > bestRanking)
\r
273 bestRanking = ranking;
\r
279 if (bestCombo == null)
\r
280 throw new NoCompatibleDevicesException("No compatible Direct3D9 devices found.");
\r
282 return Direct3D9Settings.BuildValidSettings(bestCombo, optimal);
\r