OSDN Git Service

DTXManiaソリューション、DTXManiaプロジェクト、DTXCreatorプロジェクト、FDKプロジェクトについて英語化。
[dtxmania/dtxmania.git] / FDK / コード / 01.フレームワーク / Enumeration / Enumeration9.cs
1 /*
2 * Copyright (c) 2007-2009 SlimDX Group
3
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:
10
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13
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
20 * THE SOFTWARE.
21 */
22 using System.Collections.Generic;
23 using System.Globalization;
24 using SharpDX.Direct3D9;
25
26 namespace SampleFramework
27 {
28     class AdapterInfo9
29     {
30         public int AdapterOrdinal
31         {
32             get;
33             set;
34         }
35
36         public AdapterDetails Details
37         {
38             get;
39             set;
40         }
41
42         public string Description
43         {
44             get;
45             set;
46         }
47
48         public List<DisplayMode> DisplayModes
49         {
50             get;
51             private set;
52         }
53
54         public List<DeviceInfo9> Devices
55         {
56             get;
57             private set;
58         }
59
60         public AdapterInfo9()
61         {
62             // create lists
63             DisplayModes = new List<DisplayMode>();
64             Devices = new List<DeviceInfo9>();
65         }
66     }
67
68     class DeviceInfo9
69     {
70         public DeviceType DeviceType
71         {
72             get;
73             set;
74         }
75
76         public Capabilities Capabilities
77         {
78             get;
79             set;
80         }
81
82         public List<SettingsCombo9> DeviceSettings
83         {
84             get;
85             private set;
86         }
87
88         public DeviceInfo9()
89         {
90             DeviceSettings = new List<SettingsCombo9>();
91         }
92     }
93
94     class SettingsCombo9
95     {
96         public int AdapterOrdinal
97         {
98             get;
99             set;
100         }
101
102         public DeviceType DeviceType
103         {
104             get;
105             set;
106         }
107
108         public Format AdapterFormat
109         {
110             get;
111             set;
112         }
113
114         public Format BackBufferFormat
115         {
116             get;
117             set;
118         }
119
120         public bool Windowed
121         {
122             get;
123             set;
124         }
125
126         public List<Format> DepthStencilFormats
127         {
128             get;
129             internal set;
130         }
131
132         public List<MultisampleType> MultisampleTypes
133         {
134             get;
135             private set;
136         }
137
138         public List<int> MultisampleQualities
139         {
140             get;
141             private set;
142         }
143
144         public List<PresentInterval> PresentIntervals
145         {
146             get;
147             private set;
148         }
149
150         public AdapterInfo9 AdapterInfo
151         {
152             get;
153             set;
154         }
155
156         public DeviceInfo9 DeviceInfo
157         {
158             get;
159             set;
160         }
161
162         public SettingsCombo9()
163         {
164             DepthStencilFormats = new List<Format>();
165             MultisampleQualities = new List<int>();
166             MultisampleTypes = new List<MultisampleType>();
167             PresentIntervals = new List<PresentInterval>();
168         }
169     }
170
171     class DisplayModeComparer9 : IComparer<DisplayMode>
172     {
173         static DisplayModeComparer9 comparer = new DisplayModeComparer9();
174
175         public static DisplayModeComparer9 Comparer
176         {
177             get { return comparer; }
178         }
179
180         public DisplayModeComparer9()
181         {
182         }
183
184         public int Compare(DisplayMode x, DisplayMode y)
185         {
186             if (x.Width > y.Width)
187                 return 1;
188             if (x.Width < y.Width)
189                 return -1;
190             if (x.Height > y.Height)
191                 return 1;
192             if (x.Height < y.Height)
193                 return -1;
194             if (x.Format > y.Format)
195                 return 1;
196             if (x.Format < y.Format)
197                 return -1;
198             if (x.RefreshRate > y.RefreshRate)
199                 return 1;
200             if (x.RefreshRate < y.RefreshRate)
201                 return -1;
202
203             return 0;
204         }
205     }
206
207     static class Enumeration9
208     {
209         public static DeviceSettings MinimumSettings
210         {
211             get;
212             set;
213         }
214
215         public static List<AdapterInfo9> Adapters
216         {
217             get;
218             private set;
219         }
220
221         public static bool HasEnumerated
222         {
223             get;
224             private set;
225         }
226
227         public static void Enumerate()
228         {
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 };
234
235                         foreach (AdapterInformation adapter in GraphicsDeviceManager.Direct3D9Object.Adapters)          //
236             {
237                 AdapterInfo9 info = new AdapterInfo9();
238                 info.AdapterOrdinal = adapter.Adapter;
239                 info.Details = adapter.Details;
240
241                 adapterFormats.Clear();
242                 foreach (Format adapterFormat in allowedAdapterFormats)
243                 {
244                     foreach (DisplayMode displayMode in adapter.GetDisplayModes(adapterFormat))
245                     {
246                         if (MinimumSettings != null)
247                         {
248                             if (displayMode.Width < MinimumSettings.BackBufferWidth ||
249                                 displayMode.Height < MinimumSettings.BackBufferHeight ||
250                                 displayMode.RefreshRate < MinimumSettings.RefreshRate)
251                                 continue;
252                         }
253
254                         info.DisplayModes.Add(displayMode);
255
256                         if (!adapterFormats.Contains(displayMode.Format))
257                             adapterFormats.Add(displayMode.Format);
258                     }
259                 }
260
261                 if (!adapterFormats.Contains(adapter.CurrentDisplayMode.Format))
262                     adapterFormats.Add(adapter.CurrentDisplayMode.Format);
263
264                 info.DisplayModes.Sort(DisplayModeComparer9.Comparer);
265
266                 EnumerateDevices(info, adapterFormats);
267
268                 if (info.Devices.Count > 0)
269                     Adapters.Add(info);
270             }
271
272             bool unique = true;
273             foreach (AdapterInfo9 adapter1 in Adapters)
274             {
275                 foreach (AdapterInfo9 adapter2 in Adapters)
276                 {
277                     if (adapter1 == adapter2)
278                         continue;
279                     if (adapter1.Details.Description == adapter2.Details.Description)
280                     {
281                         unique = false;
282                         break;
283                     }
284                 }
285
286                 if (!unique)
287                     break;
288             }
289
290             foreach (AdapterInfo9 info in Adapters)
291             {
292                 info.Description = info.Details.Description;
293                 if (!unique)
294                     info.Description += " " + info.AdapterOrdinal.ToString(CultureInfo.CurrentCulture);
295             }
296         }
297
298         static void EnumerateDevices(AdapterInfo9 info, List<Format> adapterFormats)
299         {
300             DeviceType[] deviceTypes = { DeviceType.Hardware, DeviceType.Reference };
301
302             foreach (DeviceType deviceType in deviceTypes)
303             {
304                 if (MinimumSettings != null && MinimumSettings.DeviceType != deviceType)
305                     continue;
306
307                 DeviceInfo9 deviceInfo = new DeviceInfo9();
308                 deviceInfo.DeviceType = deviceType;
309                                 try
310                                 {
311                                         deviceInfo.Capabilities = GraphicsDeviceManager.Direct3D9Object.GetDeviceCaps(info.AdapterOrdinal, deviceInfo.DeviceType);
312
313                                         EnumerateSettingsCombos(info, deviceInfo, adapterFormats);
314
315                                         if (deviceInfo.DeviceSettings.Count > 0)
316                                                 info.Devices.Add(deviceInfo);
317                                 }
318                                 catch
319                                 {
320                                         // #23681 2010.11.17 yyagi: GetDeviceCaps()で例外が発生するモニタに対しては、enumerateをスキップする。
321                                 }
322             }
323         }
324
325         static void EnumerateSettingsCombos(AdapterInfo9 adapterInfo, DeviceInfo9 deviceInfo, List<Format> adapterFormats)
326         {
327             Format[] backBufferFormats = { Format.A8R8G8B8, Format.X8R8G8B8, Format.A2R10G10B10,
328                 Format.R5G6B5, Format.A1R5G5B5, Format.X1R5G5B5 };
329
330             foreach (Format adapterFormat in adapterFormats)
331             {
332                 foreach (Format backBufferFormat in backBufferFormats)
333                 {
334                     for (int windowed = 0; windowed < 2; windowed++)
335                     {
336                         if (windowed == 0 && adapterInfo.DisplayModes.Count == 0)
337                             continue;
338
339                         if (!GraphicsDeviceManager.Direct3D9Object.CheckDeviceType(adapterInfo.AdapterOrdinal, deviceInfo.DeviceType,
340                             adapterFormat, backBufferFormat, (windowed == 1)))
341                             continue;
342
343                         if (!GraphicsDeviceManager.Direct3D9Object.CheckDeviceFormat(adapterInfo.AdapterOrdinal,
344                             deviceInfo.DeviceType, adapterFormat, Usage.QueryPostPixelShaderBlending,
345                             ResourceType.Texture, backBufferFormat))
346                             continue;
347
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;
356
357                         BuildDepthStencilFormatList(combo);
358                         BuildMultisampleTypeList(combo);
359
360                         if (combo.MultisampleTypes.Count == 0)
361                             continue;
362
363                         BuildPresentIntervalList(combo);
364
365                         if (MinimumSettings != null)
366                         {
367                             if (MinimumSettings.BackBufferFormat != Format.Unknown &&
368                                 MinimumSettings.BackBufferFormat != backBufferFormat)
369                                 continue;
370
371                             if (MinimumSettings.DepthStencilFormat != Format.Unknown &&
372                                 !combo.DepthStencilFormats.Contains(MinimumSettings.DepthStencilFormat))
373                                 continue;
374
375                             if (!combo.MultisampleTypes.Contains(MinimumSettings.MultisampleType))
376                                 continue;
377                         }
378
379                         deviceInfo.DeviceSettings.Add(combo);
380                     }
381                 }
382             }
383         }
384
385         static void BuildDepthStencilFormatList(SettingsCombo9 combo)
386         {
387             List<Format> possibleDepthStencilFormats = new List<Format> {
388                 Format.D16,     Format.D15S1,   Format.D24X8,
389                 Format.D24S8,   Format.D24X4S4, Format.D32 };
390
391             foreach (Format format in possibleDepthStencilFormats)
392             {
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);
398             }
399         }
400
401         static void BuildMultisampleTypeList(SettingsCombo9 combo)
402         {
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
413             };
414
415             int quality;
416             foreach (MultisampleType type in possibleMultisampleTypes)
417             {
418                 if (GraphicsDeviceManager.Direct3D9Object.CheckDeviceMultisampleType(combo.AdapterOrdinal, combo.DeviceType,
419                     combo.AdapterFormat, combo.Windowed, type, out quality))
420                 {
421                     combo.MultisampleTypes.Add(type);
422                     combo.MultisampleQualities.Add(quality);
423                 }
424             }
425         }
426
427         static void BuildPresentIntervalList(SettingsCombo9 combo)
428         {
429             List<PresentInterval> possiblePresentIntervals = new List<PresentInterval>() {
430                 PresentInterval.Immediate,  PresentInterval.Default,
431                 PresentInterval.One,        PresentInterval.Two,
432                 PresentInterval.Three,      PresentInterval.Four
433             };
434
435             foreach (PresentInterval interval in possiblePresentIntervals)
436             {
437                 if (combo.Windowed && (interval == PresentInterval.Two ||
438                     interval == PresentInterval.Three || interval == PresentInterval.Four))
439                     continue;
440
441                 if (interval == PresentInterval.Default ||
442                     (combo.DeviceInfo.Capabilities.PresentationIntervals & interval) != 0)
443                     combo.PresentIntervals.Add(interval);
444             }
445         }
446     }
447 }