+++ /dev/null
-/*\r
-* Copyright (c) 2007-2009 SlimDX Group\r
-* \r
-* Permission is hereby granted, free of charge, to any person obtaining a copy\r
-* of this software and associated documentation files (the "Software"), to deal\r
-* in the Software without restriction, including without limitation the rights\r
-* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
-* copies of the Software, and to permit persons to whom the Software is\r
-* furnished to do so, subject to the following conditions:\r
-* \r
-* The above copyright notice and this permission notice shall be included in\r
-* all copies or substantial portions of the Software.\r
-* \r
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
-* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
-* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
-* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
-* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
-* THE SOFTWARE.\r
-*/\r
-using System;\r
-using System.Collections.Generic;\r
-using SharpDX.Direct3D9;\r
-\r
-namespace SampleFramework\r
-{\r
- class Direct3D9Settings : ICloneable\r
- {\r
- public int AdapterOrdinal\r
- {\r
- get;\r
- set;\r
- }\r
-\r
- public DeviceType DeviceType\r
- {\r
- get;\r
- set;\r
- }\r
-\r
- public Format AdapterFormat\r
- {\r
- get;\r
- set;\r
- }\r
-\r
- public CreateFlags CreationFlags\r
- {\r
- get;\r
- set;\r
- }\r
-\r
- public PresentParameters PresentParameters\r
- {\r
- get;\r
- set;\r
- }\r
-\r
- public Direct3D9Settings()\r
- {\r
- PresentParameters = new PresentParameters();\r
- DeviceType = DeviceType.Hardware;\r
- AdapterFormat = Format.Unknown;\r
- CreationFlags = CreateFlags.HardwareVertexProcessing;\r
-\r
- var pp = this.PresentParameters;\r
- pp.Windowed = true;\r
- pp.BackBufferFormat = Format.Unknown;\r
- pp.BackBufferCount = 1;\r
- pp.MultiSampleType = MultisampleType.None;\r
- pp.SwapEffect = SwapEffect.Discard;\r
- pp.EnableAutoDepthStencil = true;\r
- pp.AutoDepthStencilFormat = Format.Unknown;\r
- pp.PresentFlags = PresentFlags.DiscardDepthStencil;\r
- pp.PresentationInterval = PresentInterval.Default;\r
- this.PresentParameters = pp;\r
- }\r
-\r
- public Direct3D9Settings Clone()\r
- {\r
- Direct3D9Settings clone = new Direct3D9Settings();\r
- clone.AdapterFormat = AdapterFormat;\r
- clone.AdapterOrdinal = AdapterOrdinal;\r
- clone.CreationFlags = CreationFlags;\r
- clone.DeviceType = DeviceType;\r
- clone.PresentParameters = PresentParameters;\r
-\r
- return clone;\r
- }\r
-\r
- object ICloneable.Clone()\r
- {\r
- return Clone();\r
- }\r
-\r
- public static Direct3D9Settings BuildOptimalSettings(DeviceSettings settings)\r
- {\r
- DisplayMode desktopMode = GraphicsDeviceManager.Direct3D9Object.GetAdapterDisplayMode(0);\r
- Direct3D9Settings optimal = new Direct3D9Settings();\r
- var pp = optimal.PresentParameters;\r
-\r
- optimal.AdapterOrdinal = settings.AdapterOrdinal;\r
- optimal.DeviceType = settings.DeviceType;\r
- pp.Windowed = settings.Windowed;\r
- pp.BackBufferCount = settings.BackBufferCount;\r
- pp.MultiSampleType = settings.MultisampleType;\r
- pp.MultiSampleQuality = settings.MultisampleQuality;\r
- pp.FullScreenRefreshRateInHz = settings.RefreshRate;\r
-\r
- if(settings.Multithreaded)\r
- optimal.CreationFlags |= CreateFlags.Multithreaded;\r
-\r
- if(optimal.PresentParameters.Windowed || ConversionMethods.GetColorBits(desktopMode.Format) >= 8)\r
- optimal.AdapterFormat = desktopMode.Format;\r
- else\r
- optimal.AdapterFormat = Format.X8R8G8B8;\r
-\r
- if(settings.BackBufferWidth == 0 || settings.BackBufferHeight == 0)\r
- {\r
- if(optimal.PresentParameters.Windowed)\r
- {\r
- pp.BackBufferWidth = 640;\r
- pp.BackBufferHeight = 480;\r
- }\r
- else\r
- {\r
- pp.BackBufferWidth = desktopMode.Width;\r
- pp.BackBufferHeight = desktopMode.Height;\r
- }\r
- }\r
- else\r
- {\r
- pp.BackBufferWidth = settings.BackBufferWidth;\r
- pp.BackBufferHeight = settings.BackBufferHeight;\r
- }\r
-\r
- if(settings.BackBufferFormat == Format.Unknown)\r
- pp.BackBufferFormat = optimal.AdapterFormat;\r
- else\r
- pp.BackBufferFormat = settings.BackBufferFormat;\r
-\r
- if(settings.DepthStencilFormat == Format.Unknown)\r
- {\r
- if(ConversionMethods.GetColorBits(optimal.PresentParameters.BackBufferFormat) >= 8)\r
- pp.AutoDepthStencilFormat = Format.D32;\r
- else\r
- pp.AutoDepthStencilFormat = Format.D16;\r
- }\r
- else\r
- pp.AutoDepthStencilFormat = settings.DepthStencilFormat;\r
-\r
- if(!settings.EnableVSync)\r
- pp.PresentationInterval = PresentInterval.Immediate;\r
-\r
- optimal.PresentParameters = pp;\r
- return optimal;\r
- }\r
-\r
- public static float RankSettingsCombo(SettingsCombo9 combo, Direct3D9Settings optimal, DisplayMode desktopMode)\r
- {\r
- float ranking = 0.0f;\r
-\r
- if(combo.AdapterOrdinal == optimal.AdapterOrdinal)\r
- ranking += 1000.0f;\r
-\r
- if(combo.DeviceType == optimal.DeviceType)\r
- ranking += 100.0f;\r
-\r
- if(combo.DeviceType == DeviceType.Hardware)\r
- ranking += 0.1f;\r
-\r
- if(combo.Windowed == optimal.PresentParameters.Windowed)\r
- ranking += 10.0f;\r
-\r
- if(combo.AdapterFormat == optimal.AdapterFormat)\r
- ranking += 1.0f;\r
- else\r
- {\r
- int bitDepthDelta = Math.Abs(ConversionMethods.GetColorBits(combo.AdapterFormat) -\r
- ConversionMethods.GetColorBits(optimal.AdapterFormat));\r
- float scale = Math.Max(0.9f - bitDepthDelta * 0.2f, 0.0f);\r
- ranking += scale;\r
- }\r
-\r
- if(!combo.Windowed)\r
- {\r
- bool match;\r
- if(ConversionMethods.GetColorBits(desktopMode.Format) >= 8)\r
- match = (combo.AdapterFormat == desktopMode.Format);\r
- else\r
- match = (combo.AdapterFormat == Format.X8R8G8B8);\r
-\r
- if(match)\r
- ranking += 0.1f;\r
- }\r
-\r
- if((optimal.CreationFlags & CreateFlags.HardwareVertexProcessing) != 0 &&\r
- (optimal.CreationFlags & CreateFlags.MixedVertexProcessing) != 0)\r
- {\r
- if((combo.DeviceInfo.Capabilities.DeviceCaps & DeviceCaps.HWTransformAndLight) != 0)\r
- ranking += 1.0f;\r
- }\r
-\r
- if((combo.DeviceInfo.Capabilities.DeviceCaps & DeviceCaps.HWTransformAndLight) != 0)\r
- ranking += 0.1f;\r
-\r
- foreach(DisplayMode displayMode in combo.AdapterInfo.DisplayModes)\r
- {\r
- if(displayMode.Format == combo.AdapterFormat &&\r
- displayMode.Width == optimal.PresentParameters.BackBufferWidth &&\r
- displayMode.Height == optimal.PresentParameters.BackBufferHeight)\r
- {\r
- ranking += 1.0f;\r
- break;\r
- }\r
- }\r
-\r
- if(combo.BackBufferFormat == optimal.PresentParameters.BackBufferFormat)\r
- ranking += 1.0f;\r
- else\r
- {\r
- int bitDepthDelta = Math.Abs(ConversionMethods.GetColorBits(combo.BackBufferFormat) -\r
- ConversionMethods.GetColorBits(optimal.PresentParameters.BackBufferFormat));\r
- float scale = Math.Max(0.9f - bitDepthDelta * 0.2f, 0.0f);\r
- ranking += scale;\r
- }\r
-\r
- if(combo.BackBufferFormat == combo.AdapterFormat)\r
- ranking += 0.1f;\r
-\r
- for(int i = 0; i < combo.MultisampleTypes.Count; i++)\r
- {\r
- MultisampleType type = combo.MultisampleTypes[i];\r
- int quality = combo.MultisampleQualities[i];\r
-\r
- if(type == optimal.PresentParameters.MultiSampleType && quality == optimal.PresentParameters.MultiSampleQuality)\r
- {\r
- ranking += 1.0f;\r
- break;\r
- }\r
- }\r
-\r
- if(combo.DepthStencilFormats.Contains(optimal.PresentParameters.AutoDepthStencilFormat))\r
- ranking += 1.0f;\r
-\r
- foreach(DisplayMode displayMode in combo.AdapterInfo.DisplayModes)\r
- {\r
- if(displayMode.Format == combo.AdapterFormat &&\r
- displayMode.RefreshRate == optimal.PresentParameters.FullScreenRefreshRateInHz)\r
- {\r
- ranking += 1.0f;\r
- break;\r
- }\r
- }\r
-\r
- if(combo.PresentIntervals.Contains(optimal.PresentParameters.PresentationInterval))\r
- ranking += 1.0f;\r
-\r
- return ranking;\r
- }\r
-\r
- public static Direct3D9Settings BuildValidSettings(SettingsCombo9 combo, Direct3D9Settings input)\r
- {\r
- Direct3D9Settings settings = new Direct3D9Settings();\r
- var pp = settings.PresentParameters;\r
-\r
- settings.AdapterOrdinal = combo.AdapterOrdinal;\r
- settings.DeviceType = combo.DeviceType;\r
- settings.AdapterFormat = combo.AdapterFormat;\r
- pp.Windowed = combo.Windowed;\r
- pp.BackBufferFormat = combo.BackBufferFormat;\r
- pp.SwapEffect = input.PresentParameters.SwapEffect;\r
- pp.PresentFlags = input.PresentParameters.PresentFlags | PresentFlags.DiscardDepthStencil;\r
-\r
- settings.CreationFlags = input.CreationFlags;\r
- if((combo.DeviceInfo.Capabilities.DeviceCaps & DeviceCaps.HWTransformAndLight) == 0 &&\r
- ((settings.CreationFlags & CreateFlags.HardwareVertexProcessing) != 0 ||\r
- (settings.CreationFlags & CreateFlags.MixedVertexProcessing) != 0))\r
- {\r
- settings.CreationFlags &= ~CreateFlags.HardwareVertexProcessing;\r
- settings.CreationFlags &= ~CreateFlags.MixedVertexProcessing;\r
- settings.CreationFlags |= CreateFlags.SoftwareVertexProcessing;\r
- }\r
-\r
- if((settings.CreationFlags & CreateFlags.HardwareVertexProcessing) == 0 &&\r
- (settings.CreationFlags & CreateFlags.MixedVertexProcessing) == 0 &&\r
- (settings.CreationFlags & CreateFlags.SoftwareVertexProcessing) == 0)\r
- {\r
- if((combo.DeviceInfo.Capabilities.DeviceCaps & DeviceCaps.HWTransformAndLight) != 0)\r
- settings.CreationFlags |= CreateFlags.HardwareVertexProcessing;\r
- else\r
- settings.CreationFlags |= CreateFlags.SoftwareVertexProcessing;\r
- }\r
-\r
- DisplayMode bestDisplayMode = FindValidResolution(combo, input);\r
- pp.BackBufferWidth = bestDisplayMode.Width;\r
- pp.BackBufferHeight = bestDisplayMode.Height;\r
-\r
- pp.BackBufferCount = input.PresentParameters.BackBufferCount;\r
- if(pp.BackBufferCount > 3)\r
- pp.BackBufferCount = 3;\r
- if(pp.BackBufferCount < 1)\r
- pp.BackBufferCount = 1;\r
-\r
- if(input.PresentParameters.SwapEffect != SwapEffect.Discard)\r
- {\r
- pp.MultiSampleType = MultisampleType.None;\r
- pp.MultiSampleQuality = 0;\r
- }\r
- else\r
- {\r
- MultisampleType bestType = MultisampleType.None;\r
- int bestQuality = 0;\r
-\r
- for(int i = 0; i < combo.MultisampleTypes.Count; i++)\r
- {\r
- MultisampleType type = combo.MultisampleTypes[i];\r
- int quality = combo.MultisampleQualities[0];\r
-\r
- if(Math.Abs(type - input.PresentParameters.MultiSampleType) < Math.Abs(bestType - input.PresentParameters.MultiSampleType))\r
- {\r
- bestType = type;\r
- bestQuality = Math.Min(quality - 1, input.PresentParameters.MultiSampleQuality);\r
- }\r
- }\r
-\r
- pp.MultiSampleType = bestType;\r
- pp.MultiSampleQuality = bestQuality;\r
- }\r
-\r
- List<int> rankings = new List<int>();\r
- int inputDepthBitDepth = ConversionMethods.GetDepthBits(input.PresentParameters.AutoDepthStencilFormat);\r
- int inputStencilBitDepth = ConversionMethods.GetStencilBits(input.PresentParameters.AutoDepthStencilFormat);\r
-\r
- foreach(Format format in combo.DepthStencilFormats)\r
- {\r
- int currentBitDepth = ConversionMethods.GetDepthBits(format);\r
- int currentStencilDepth = ConversionMethods.GetStencilBits(format);\r
-\r
- int ranking = Math.Abs(currentBitDepth - inputDepthBitDepth);\r
- ranking += Math.Abs(currentStencilDepth - inputStencilBitDepth);\r
- rankings.Add(ranking);\r
- }\r
-\r
- int bestRanking = int.MaxValue;\r
- foreach(int ranking in rankings)\r
- {\r
- if(ranking < bestRanking)\r
- bestRanking = ranking;\r
- }\r
- int bestIndex = rankings.IndexOf(bestRanking);\r
-\r
- if(bestIndex >= 0)\r
- {\r
- pp.AutoDepthStencilFormat = combo.DepthStencilFormats[bestIndex];\r
- pp.EnableAutoDepthStencil = true;\r
- }\r
- else\r
- {\r
- pp.AutoDepthStencilFormat = Format.Unknown;\r
- pp.EnableAutoDepthStencil = false;\r
- }\r
-\r
- if(combo.Windowed)\r
- pp.FullScreenRefreshRateInHz = 0;\r
- else\r
- {\r
- int match = input.PresentParameters.FullScreenRefreshRateInHz;\r
- bestDisplayMode.RefreshRate = 0;\r
- if(match != 0)\r
- {\r
- bestRanking = 100000;\r
- foreach(DisplayMode displayMode in combo.AdapterInfo.DisplayModes)\r
- {\r
- if(displayMode.Format != combo.AdapterFormat ||\r
- displayMode.Width != bestDisplayMode.Width ||\r
- displayMode.Height != bestDisplayMode.Height)\r
- continue;\r
-\r
- int ranking = Math.Abs(displayMode.RefreshRate - match);\r
-\r
- if(ranking < bestRanking)\r
- {\r
- bestDisplayMode.RefreshRate = displayMode.RefreshRate;\r
- bestRanking = ranking;\r
-\r
- if(bestRanking == 0)\r
- break;\r
- }\r
- }\r
- }\r
-\r
- pp.FullScreenRefreshRateInHz = bestDisplayMode.RefreshRate;\r
- }\r
-\r
- if(combo.PresentIntervals.Contains(input.PresentParameters.PresentationInterval))\r
- pp.PresentationInterval = input.PresentParameters.PresentationInterval;\r
- else\r
- pp.PresentationInterval = PresentInterval.Default;\r
-\r
- settings.PresentParameters = pp;\r
- return settings;\r
- }\r
-\r
- static DisplayMode FindValidResolution(SettingsCombo9 combo, Direct3D9Settings input)\r
- {\r
- DisplayMode bestMode = new DisplayMode();\r
-\r
- if(combo.Windowed)\r
- {\r
- bestMode.Width = input.PresentParameters.BackBufferWidth;\r
- bestMode.Height = input.PresentParameters.BackBufferHeight;\r
- return bestMode;\r
- }\r
-\r
- int bestRanking = 100000;\r
- int ranking;\r
- foreach(DisplayMode mode in combo.AdapterInfo.DisplayModes)\r
- {\r
- if(mode.Format != combo.AdapterFormat)\r
- continue;\r
-\r
- ranking = Math.Abs(mode.Width - input.PresentParameters.BackBufferWidth) +\r
- Math.Abs(mode.Height - input.PresentParameters.BackBufferHeight);\r
-\r
- if(ranking < bestRanking)\r
- {\r
- bestMode = mode;\r
- bestRanking = ranking;\r
-\r
- if(bestRanking == 0)\r
- break;\r
- }\r
- }\r
-\r
- if(bestMode.Width == 0)\r
- {\r
- bestMode.Width = input.PresentParameters.BackBufferWidth;\r
- bestMode.Height = input.PresentParameters.BackBufferHeight;\r
- }\r
-\r
- return bestMode;\r
- }\r
- }\r
-}\r