public override List<GraphicsDeviceInformation> FindBestDevices(GameGraphicsParameters prefferedParameters)
        {
            var graphicsDeviceInfos = base.FindBestDevices(prefferedParameters);

            // Special case where the default FindBestDevices is not working
            if (graphicsDeviceInfos.Count == 0)
            {
                var graphicsAdapter = GraphicsAdapter.Adapters[0];

                // Iterate on each preferred graphics profile
                foreach (var featureLevel in prefferedParameters.PreferredGraphicsProfile)
                {
                    // Check if this profile is supported.
                    if (graphicsAdapter.IsProfileSupported(featureLevel))
                    {
                        var deviceInfo = new GraphicsDeviceInformation
                                             {
                                                 Adapter = graphicsAdapter,
                                                 GraphicsProfile = featureLevel,
                                                 PresentationParameters =
                                                     {
                                                         MultiSampleCount = MSAALevel.None,
                                                         IsFullScreen = prefferedParameters.IsFullScreen,
                                                         PresentationInterval = prefferedParameters.SynchronizeWithVerticalRetrace ? PresentInterval.One : PresentInterval.Immediate,
                                                         DeviceWindowHandle = MainWindow.NativeWindow,
                                                         RenderTargetUsage = Usage.BackBuffer | Usage.RenderTargetOutput
                                                     }
                                             };

                        // Hardcoded format and refresh rate...
                        // This is a workaround to allow this code to work inside the emulator
                        // but this is not really robust
                        // TODO: Check how to handle this case properly
                        var displayMode = new DisplayMode(DXGI.Format.B8G8R8A8_UNorm, gameWindow.ClientBounds.Width, gameWindow.ClientBounds.Height, new Rational(60, 1));
                        AddDevice(graphicsAdapter, displayMode, deviceInfo, prefferedParameters, graphicsDeviceInfos);

                        // If the profile is supported, we are just using the first best one
                        break;
                    }
                }
            }

            return graphicsDeviceInfos;
        }
        /// <summary>
        /// Find the display mode that most closely matches the requested display mode.
        /// </summary>
        /// <param name="targetProfiles">The target profile, as available formats are different depending on the feature level..</param>
        /// <param name="mode">The mode.</param>
        /// <returns>Returns the closes display mode.</returns>
        /// <unmanaged>HRESULT IDXGIOutput::FindClosestMatchingMode([In] const DXGI_MODE_DESC* pModeToMatch,[Out] DXGI_MODE_DESC* pClosestMatch,[In, Optional] IUnknown* pConcernedDevice)</unmanaged>
        /// <remarks>Direct3D devices require UNORM formats. This method finds the closest matching available display mode to the mode specified in pModeToMatch. Similarly ranked fields (i.e. all specified, or all unspecified, etc) are resolved in the following order.  ScanlineOrdering Scaling Format Resolution RefreshRate  When determining the closest value for a particular field, previously matched fields are used to filter the display mode list choices, and  other fields are ignored. For example, when matching Resolution, the display mode list will have already been filtered by a certain ScanlineOrdering,  Scaling, and Format, while RefreshRate is ignored. This ordering doesn't define the absolute ordering for every usage scenario of FindClosestMatchingMode, because  the application can choose some values initially, effectively changing the order that fields are chosen. Fields of the display mode are matched one at a time, generally in a specified order. If a field is unspecified, FindClosestMatchingMode gravitates toward the values for the desktop related to this output.  If this output is not part of the desktop, then the default desktop output is used to find values. If an application uses a fully unspecified  display mode, FindClosestMatchingMode will typically return a display mode that matches the desktop settings for this output.   Unspecified fields are lower priority than specified fields and will be resolved later than specified fields.</remarks>
        public DisplayMode FindClosestMatchingDisplayMode(FeatureLevel[] targetProfiles,  DisplayMode mode)
        {
            ModeDescription closestDescription;
            SharpDX.Direct3D11.Device deviceTemp = null;
            try
            {
                deviceTemp = new SharpDX.Direct3D11.Device(adapter, DeviceCreationFlags.None, targetProfiles);
            }
            catch(Exception ex) {}

            var descriprtion = new ModeDescription()
                                   {
                                       Width = mode.Width,
                                       Height = mode.Height,
                                       RefreshRate = mode.RefreshRate,
                                       Format = mode.Format,
                                       Scaling = DisplayModeScaling.Unspecified,
                                       ScanlineOrdering = DisplayModeScanlineOrder.Unspecified
                                   };
            using (var device = deviceTemp)
                output.GetClosestMatchingMode(device, descriprtion, out closestDescription);

            return DisplayMode.FromDescription(closestDescription);
        }
Exemple #3
0
        /// <summary>
        /// Returns a collection of supported display modes for a particular Format.
        /// </summary>
        /// <returns>a read-only collection of display modes</returns>
        private DisplayMode[] GetSupportedDisplayModes()
        {
            var output = outputs[0];
            var modeAvailable = new List<DisplayMode>();
            var modeMap = new Dictionary<string, DisplayMode>();

#if DIRECTX11_1
            var output1 = output.QueryInterface<Output1>();
#endif

            try
            {
                foreach (var format in Enum.GetValues(typeof(DXGI.Format)))
                {
#if DIRECTX11_1
                    var modes = output1.GetDisplayModeList1(
                        (Format)format,
                        DisplayModeEnumerationFlags.Interlaced |
                        DisplayModeEnumerationFlags.Scaling);
#else
                var modes = output.GetDisplayModeList((Format)format,
                                                      DisplayModeEnumerationFlags.Interlaced |
                                                      DisplayModeEnumerationFlags.Scaling);
#endif


                    foreach (var mode in modes)
                    {
                        if (mode.Scaling == DisplayModeScaling.Unspecified)
                        {
                            string key = format + ";" + mode.Width + ";" + mode.Height + ";" + mode.RefreshRate.Numerator + ";" + mode.RefreshRate.Denominator;

                            DisplayMode oldMode;
                            if (!modeMap.TryGetValue(key, out oldMode))
                            {
                                var displayMode = new DisplayMode(mode.Format, mode.Width, mode.Height, mode.RefreshRate);

                                modeMap.Add(key, displayMode);
                                modeAvailable.Add(displayMode);
                            }
                        }
                    }
                }
            }
            catch (SharpDXException dxgiException)
            {
                if (dxgiException.ResultCode != DXGI.ResultCode.NotCurrentlyAvailable)
                {
                    throw;
                }
            }
            return modeAvailable.ToArray();
        }
Exemple #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GraphicsAdapter" /> class.
        /// </summary>
        /// <param name="adapterOrdinal">The adapter ordinal.</param>
        private GraphicsAdapter(int adapterOrdinal)
        {
            this.adapterOrdinal = adapterOrdinal;
            adapter = ToDispose(Factory.GetAdapter1(adapterOrdinal));
            Description = adapter.Description1;
            outputs = adapter.Outputs;
            SupportedDisplayModes = new DisplayMode[0];

            // Enumerate outputs if any.
            if (outputs != null && outputs.Length > 0)
            {
                outputDescription = outputs[0].Description;
                SupportedDisplayModes = GetSupportedDisplayModes();
                foreach (var output in outputs)
                {
                    ToDispose(output);
                }

                foreach (var supportedDisplayMode in SupportedDisplayModes)
                {
                    if (supportedDisplayMode.Width == outputDescription.DesktopBounds.Width
                        && supportedDisplayMode.Height == outputDescription.DesktopBounds.Height
                        && supportedDisplayMode.Format == Format.R8G8B8A8_UNorm)
                    {
                        // Stupid DXGI, there is no way to get the DXGI.Format, nor the refresh rate.
                        CurrentDisplayMode = new DisplayMode(Format.R8G8B8A8_UNorm, outputDescription.DesktopBounds.Width, outputDescription.DesktopBounds.Height, supportedDisplayMode.RefreshRate);
                        break;
                    }
                }

                if (CurrentDisplayMode == null)
                {
                    foreach (var supportedDisplayMode in SupportedDisplayModes)
                    {
                        if (supportedDisplayMode.Width == outputDescription.DesktopBounds.Width
                            && supportedDisplayMode.Height == outputDescription.DesktopBounds.Height
                            && supportedDisplayMode.Format == Format.B8G8R8A8_UNorm)
                        {
                            // Stupid DXGI, there is no way to get the DXGI.Format, nor the refresh rate.
                            CurrentDisplayMode = new DisplayMode(Format.B8G8R8A8_UNorm, outputDescription.DesktopBounds.Width, outputDescription.DesktopBounds.Height, supportedDisplayMode.RefreshRate);
                            break;
                        }
                    }
                }
            }
        }
Exemple #5
0
        protected void AddDevice(GraphicsAdapter graphicsAdapter, DisplayMode mode,  GraphicsDeviceInformation deviceBaseInfo, GameGraphicsParameters prefferedParameters, List<GraphicsDeviceInformation> graphicsDeviceInfos)
        {
            var deviceInfo = deviceBaseInfo.Clone();

            deviceInfo.PresentationParameters.RefreshRate = mode.RefreshRate;

            if (prefferedParameters.IsFullScreen)
            {
                deviceInfo.PresentationParameters.BackBufferWidth = mode.Width;
                deviceInfo.PresentationParameters.BackBufferHeight = mode.Height;
            }
            else
            {
                deviceInfo.PresentationParameters.BackBufferWidth = prefferedParameters.PreferredBackBufferWidth;
                deviceInfo.PresentationParameters.BackBufferHeight = prefferedParameters.PreferredBackBufferHeight;
            }

            // TODO: Handle BackBufferFormat / multisampling / depthstencil format
            deviceInfo.PresentationParameters.BackBufferFormat = prefferedParameters.PreferredBackBufferFormat;
            deviceInfo.PresentationParameters.DepthStencilFormat = prefferedParameters.PreferredDepthStencilFormat;
            deviceInfo.PresentationParameters.MultiSampleCount = MSAALevel.None;

            if (!graphicsDeviceInfos.Contains(deviceInfo))
            {
                graphicsDeviceInfos.Add(deviceInfo);
            }
        }
Exemple #6
0
 protected void AddDeviceWithDefaultDisplayMode(GameGraphicsParameters prefferedParameters, GraphicsAdapter graphicsAdapter, GraphicsDeviceInformation deviceInfo, List<GraphicsDeviceInformation> graphicsDeviceInfos)
 {
     var displayMode = new DisplayMode(DXGI.Format.B8G8R8A8_UNorm, gameWindow.ClientBounds.Width, gameWindow.ClientBounds.Height, new Rational(60, 1));
     AddDevice(displayMode, deviceInfo, prefferedParameters, graphicsDeviceInfos);
 }
 /// <summary>
 /// Initializes <see cref="CurrentDisplayMode"/> with the most appropiate mode from <see cref="SupportedDisplayModes"/>.
 /// </summary>
 /// <remarks>It checks first for a mode with <see cref="Format.R8G8B8A8_UNorm"/>,
 /// if it is not found - it checks for <see cref="Format.B8G8R8A8_UNorm"/>.</remarks>
 private void InitializeCurrentDisplayMode()
 {
     currentDisplayMode = TryFindMatchingDisplayMode(Format.R8G8B8A8_UNorm)
                          ?? TryFindMatchingDisplayMode(Format.B8G8R8A8_UNorm);
 }
        private void TryAddDeviceFromOutput(GameGraphicsParameters prefferedParameters,
                                            GraphicsOutput output,
                                            GraphicsDeviceInformation deviceInfo,
                                            List<GraphicsDeviceInformation> graphicsDeviceInfos)
        {
            var preferredMode = new DisplayMode(prefferedParameters.PreferredBackBufferFormat,
                prefferedParameters.PreferredBackBufferWidth,
                prefferedParameters.PreferredBackBufferHeight,
                prefferedParameters.PreferredRefreshRate);

            if (prefferedParameters.IsFullScreen)
            {
                var displayMode = output.FindClosestMatchingDisplayMode(prefferedParameters.PreferredGraphicsProfile, preferredMode);
                AddDevice(displayMode, deviceInfo, prefferedParameters, graphicsDeviceInfos);
            }
            else
            {
                AddDevice(preferredMode, deviceInfo, prefferedParameters, graphicsDeviceInfos);
            }
        }
        /// <summary>
        /// Returns a collection of supported display modes for a particular Format.
        /// </summary>
        /// <returns>a read-only collection of display modes</returns>
        private DisplayMode[] GetSupportedDisplayModes()
        {
            var output = outputs[0];
            var modeAvailable = new List<DisplayMode>();
            var modeMap = new Dictionary<string, DisplayMode>();

            foreach (var format in Enum.GetValues(typeof(DXGI.Format)))
            {
                var modes = output.GetDisplayModeList((Format)format,
                                                      DisplayModeEnumerationFlags.Interlaced |
                                                      DisplayModeEnumerationFlags.Scaling);

                foreach (var mode in modes)
                {
                    if (mode.Scaling == DisplayModeScaling.Unspecified)
                    {
                        string key = format + ";" + mode.Width + ";" + mode.Height + ";" + mode.RefreshRate.Numerator + ";" + mode.RefreshRate.Denominator;

                        DisplayMode oldMode;
                        if (!modeMap.TryGetValue(key, out oldMode))
                        {
                            var displayMode = new DisplayMode(mode);

                            modeMap.Add(key, displayMode);
                            modeAvailable.Add(displayMode);
                        }
                    }
                }
            }
            return modeAvailable.ToArray();
        }
Exemple #10
0
        /// <summary>
        /// Find the display mode that most closely matches the requested display mode.
        /// </summary>
        /// <param name="targetProfiles">The target profile, as available formats are different depending on the feature level..</param>
        /// <param name="mode">The mode.</param>
        /// <returns>Returns the closes display mode.</returns>
        /// <unmanaged>HRESULT IDXGIOutput::FindClosestMatchingMode([In] const DXGI_MODE_DESC* pModeToMatch,[Out] DXGI_MODE_DESC* pClosestMatch,[In, Optional] IUnknown* pConcernedDevice)</unmanaged>
        /// <remarks>Direct3D devices require UNORM formats. This method finds the closest matching available display mode to the mode specified in pModeToMatch. Similarly ranked fields (i.e. all specified, or all unspecified, etc) are resolved in the following order.  ScanlineOrdering Scaling Format Resolution RefreshRate  When determining the closest value for a particular field, previously matched fields are used to filter the display mode list choices, and  other fields are ignored. For example, when matching Resolution, the display mode list will have already been filtered by a certain ScanlineOrdering,  Scaling, and Format, while RefreshRate is ignored. This ordering doesn't define the absolute ordering for every usage scenario of FindClosestMatchingMode, because  the application can choose some values initially, effectively changing the order that fields are chosen. Fields of the display mode are matched one at a time, generally in a specified order. If a field is unspecified, FindClosestMatchingMode gravitates toward the values for the desktop related to this output.  If this output is not part of the desktop, then the default desktop output is used to find values. If an application uses a fully unspecified  display mode, FindClosestMatchingMode will typically return a display mode that matches the desktop settings for this output.   Unspecified fields are lower priority than specified fields and will be resolved later than specified fields.</remarks>
        public DisplayMode FindClosestMatchingDisplayMode(FeatureLevel[] targetProfiles, DisplayMode mode)
        {
            ModeDescription closestDescription;

            SharpDX.Direct3D11.Device deviceTemp = null;
            try
            {
                deviceTemp = new SharpDX.Direct3D11.Device(adapter, DeviceCreationFlags.None, targetProfiles);
            }
            catch (Exception ex) {}

            var descriprtion = new ModeDescription()
            {
                Width            = mode.Width,
                Height           = mode.Height,
                RefreshRate      = mode.RefreshRate,
                Format           = mode.Format,
                Scaling          = DisplayModeScaling.Unspecified,
                ScanlineOrdering = DisplayModeScanlineOrder.Unspecified
            };

            using (var device = deviceTemp)
                output.GetClosestMatchingMode(device, descriprtion, out closestDescription);

            return(DisplayMode.FromDescription(closestDescription));
        }
Exemple #11
0
 /// <summary>
 /// Initializes <see cref="CurrentDisplayMode"/> with the most appropiate mode from <see cref="SupportedDisplayModes"/>.
 /// </summary>
 /// <remarks>It checks first for a mode with <see cref="Format.R8G8B8A8_UNorm"/>,
 /// if it is not found - it checks for <see cref="Format.B8G8R8A8_UNorm"/>.</remarks>
 private void InitializeCurrentDisplayMode()
 {
     currentDisplayMode = TryFindMatchingDisplayMode(Format.R8G8B8A8_UNorm)
                          ?? TryFindMatchingDisplayMode(Format.B8G8R8A8_UNorm);
 }
Exemple #12
0
        /// <summary>
        /// Enumerates all available display modes for this output and stores them in <see cref="SupportedDisplayModes"/>.
        /// </summary>
        private void InitializeSupportedDisplayModes()
        {
            var modesAvailable = new List<DisplayMode>();
            var modesMap = new Dictionary<string, DisplayMode>();

#if DIRECTX11_1
            var output1 = output.QueryInterface<Output1>();
#endif

            try
            {
                const DisplayModeEnumerationFlags displayModeEnumerationFlags = DisplayModeEnumerationFlags.Interlaced | DisplayModeEnumerationFlags.Scaling;

                foreach (var format in Enum.GetValues(typeof(DXGI.Format)))
                {
                    var dxgiFormat = (Format)format;
#if DIRECTX11_1
                    var modes = output1.GetDisplayModeList1(dxgiFormat, displayModeEnumerationFlags);
#else
                    var modes = output.GetDisplayModeList(dxgiFormat, displayModeEnumerationFlags);
#endif

                    foreach (var mode in modes)
                    {
                        if (mode.Scaling == DisplayModeScaling.Unspecified)
                        {
                            var key = format + ";" + mode.Width + ";" + mode.Height + ";" + mode.RefreshRate.Numerator + ";" + mode.RefreshRate.Denominator;

                            DisplayMode oldMode;
                            if (!modesMap.TryGetValue(key, out oldMode))
                            {
                                var displayMode = new DisplayMode(mode.Format, mode.Width, mode.Height, mode.RefreshRate);

                                modesMap.Add(key, displayMode);
                                modesAvailable.Add(displayMode);
                            }
                        }
                    }
                }
            }
            catch (SharpDXException dxgiException)
            {
                if (dxgiException.ResultCode != ResultCode.NotCurrentlyAvailable)
                    throw;
            }

#if DIRECTX11_1
            output1.Dispose();
#endif
            SupportedDisplayModes = modesAvailable.ToArray();
        }