Esempio n. 1
0
 /// <summary>
 /// Adds all vertex processing types that are compatible with the device and app to
 /// the given deviceCombo
 /// </summary>
 public void BuildVertexProcessingTypeList(GraphicsDeviceInfo deviceInfo, DeviceCombo deviceCombo)
 {
     if ((deviceInfo.Caps.DeviceCaps & DeviceCaps.HWTransformAndLight) != 0)
     {
         if ((deviceInfo.Caps.DeviceCaps & DeviceCaps.PureDevice) != 0)
         {
             if (ConfirmDeviceCallback == null ||
                 ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.PureHardware,
                                       deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat))
             {
                 deviceCombo.VertexProcessingTypes.Add(VertexProcessingType.PureHardware);
             }
         }
         if (ConfirmDeviceCallback == null ||
             ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Hardware,
                                   deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat))
         {
             deviceCombo.VertexProcessingTypes.Add(VertexProcessingType.Hardware);
         }
         if (AppUsesMixedVP && (ConfirmDeviceCallback == null ||
                                ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Mixed,
                                                      deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat)))
         {
             deviceCombo.VertexProcessingTypes.Add(VertexProcessingType.Mixed);
         }
     }
     if (ConfirmDeviceCallback == null ||
         ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Software,
                               deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat))
     {
         deviceCombo.VertexProcessingTypes.Add(VertexProcessingType.Software);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Adds all depth/stencil formats that are compatible with the device and app to
        /// the given deviceCombo
        /// </summary>
        public void BuildDepthStencilFormatList(DeviceCombo deviceCombo)
        {
            Format[] depthStencilFormatArray =
            {
                Format.D16,
                Format.D15S1,
                Format.D24X8,
                Format.D24S8,
                Format.D24X4S4,
                Format.D32,
            };

            foreach (Format depthStencilFmt in depthStencilFormatArray)
            {
                if (D3DUtil.GetDepthBits(depthStencilFmt) < AppMinDepthBits)
                {
                    continue;
                }
                if (D3DUtil.GetStencilBits(depthStencilFmt) < AppMinStencilBits)
                {
                    continue;
                }
                if (MPDirect3D.Direct3D.CheckDeviceFormat(deviceCombo.AdapterOrdinal, deviceCombo.DevType, deviceCombo.AdapterFormat,
                                                          Usage.DepthStencil, ResourceType.Surface, depthStencilFmt))
                {
                    if (MPDirect3D.Direct3D.CheckDepthStencilMatch(deviceCombo.AdapterOrdinal, deviceCombo.DevType,
                                                                   deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat, depthStencilFmt))
                    {
                        deviceCombo.DepthStencilFormats.Add(depthStencilFmt);
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Build presentation parameters from the given settings.
        /// </summary>
        /// <param name="configuration">Graphics configuration to use.</param>
        public PresentParameters BuildPresentParamsFromSettings(D3DConfiguration configuration)
        {
            int backBufferWidth;
            int backBufferHeight;

            if (configuration.DeviceCombo.IsWindowed)
            {
                backBufferWidth  = _renderTarget.ClientRectangle.Width;
                backBufferHeight = _renderTarget.ClientRectangle.Height;
            }
            else
            {
                backBufferWidth  = configuration.DisplayMode.Width;
                backBufferHeight = configuration.DisplayMode.Height;
            }

            ServiceRegistration.Get <ILogger>().Debug("BuildPresentParamsFromSettings: Windowed = {0},  {1} x {2}",
                                                      configuration.DeviceCombo.IsWindowed, backBufferWidth, backBufferHeight);

            PresentParameters result = new PresentParameters();

            AppSettings settings = ServiceRegistration.Get <ISettingsManager>().Load <AppSettings>();

            DeviceCombo     dc  = configuration.DeviceCombo;
            MultisampleType mst = settings.MultisampleType;

            mst = dc.MultisampleTypes.ContainsKey(mst) ? mst : MultisampleType.None;
            result.Multisample            = mst;
            result.MultisampleQuality     = 0;
            result.EnableAutoDepthStencil = false;
            result.AutoDepthStencilFormat = dc.DepthStencilFormats.FirstOrDefault(dsf =>
                                                                                  !dc.DepthStencilMultiSampleConflicts.Contains(new DepthStencilMultiSampleConflict {
                DepthStencilFormat = dsf, MultisampleType = mst
            }));
            // Note that PresentFlags.Video makes NVidia graphics drivers switch off multisampling antialiasing
            result.PresentFlags       = PresentFlags.None;
            result.DeviceWindowHandle = _renderTarget.Handle;
            result.Windowed           = configuration.DeviceCombo.IsWindowed;
            result.BackBufferFormat   = configuration.DeviceCombo.BackBufferFormat;
#if PROFILE_PERFORMANCE
            result.BackBufferCount      = 20; // Such high backbuffer count is only useful for benchmarking so that rendering is not limited by backbuffer count
            result.PresentationInterval = PresentInterval.One;
#else
            result.BackBufferCount      = 4; // 2 to 4 are recommended for FlipEx swap mode
            result.PresentationInterval = PresentInterval.One;
#endif
            result.FullScreenRefreshRateInHertz = result.Windowed ? 0 : configuration.DisplayMode.RefreshRate;

            // From http://msdn.microsoft.com/en-us/library/windows/desktop/bb173422%28v=vs.85%29.aspx :
            // To use multisampling, the SwapEffect member of D3DPRESENT_PARAMETER must be set to D3DSWAPEFFECT_DISCARD.
            // SwapEffect must be set to SwapEffect.FlipEx to support the Present property to be Present.ForceImmediate
            // (see http://msdn.microsoft.com/en-us/library/windows/desktop/bb174343%28v=vs.85%29.aspx )
            result.SwapEffect = mst == MultisampleType.None ? SwapEffect.FlipEx : SwapEffect.Discard;

            result.BackBufferWidth  = backBufferWidth;
            result.BackBufferHeight = backBufferHeight;

            return(result);
        }
Esempio n. 4
0
 /// <summary>
 /// Adds all multisample types that are compatible with the device and app to
 /// the given deviceCombo
 /// </summary>
 public void BuildMultisampleTypeList(DeviceCombo deviceCombo)
 {
     foreach (MultisampleType msType in Enum.GetValues(typeof(MultisampleType)))
     {
         Result result;
         int    qualityLevels;
         if (MPDirect3D.Direct3D.CheckDeviceMultisampleType(deviceCombo.AdapterOrdinal, deviceCombo.DevType,
                                                            deviceCombo.BackBufferFormat, deviceCombo.IsWindowed, msType, out qualityLevels, out result))
         {
             deviceCombo.MultisampleTypes.Add(msType, qualityLevels);
         }
     }
 }
Esempio n. 5
0
 /// <summary>
 /// Finds any depthstencil formats that are incompatible with multisample types and
 /// builds a list of them.
 /// </summary>
 public void BuildDepthStencilMultiSampleConflictList(DeviceCombo deviceCombo)
 {
     foreach (Format dsFmt in deviceCombo.DepthStencilFormats)
     {
         foreach (MultisampleType mst in deviceCombo.MultisampleTypes.Keys)
         {
             if (!MPDirect3D.Direct3D.CheckDeviceMultisampleType(deviceCombo.AdapterOrdinal,
                                                                 deviceCombo.DevType, dsFmt, deviceCombo.IsWindowed, mst))
             {
                 deviceCombo.DepthStencilMultiSampleConflicts.Add(
                     new DepthStencilMultiSampleConflict {
                     DepthStencilFormat = dsFmt, MultisampleType = mst
                 });
             }
         }
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Adds all present intervals that are compatible with the device and app to
        /// the given deviceCombo
        /// </summary>
        public void BuildPresentIntervalList(GraphicsDeviceInfo deviceInfo, DeviceCombo deviceCombo)
        {
            foreach (PresentInterval pi in Enum.GetValues(typeof(PresentInterval)))
            {
                if (deviceCombo.IsWindowed)
                {
                    if (pi == PresentInterval.Two ||
                        pi == PresentInterval.Three ||
                        pi == PresentInterval.Four)
                    {
                        // These intervals are not supported in windowed mode.
                        continue;
                    }
                }

                // Note that PresentInterval.Default is zero, so you
                // can't do a caps check for it -- it is always available.
                if (pi == PresentInterval.Default || (deviceInfo.Caps.PresentationIntervals & pi) != 0)
                {
                    deviceCombo.PresentIntervals.Add(pi);
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Returns a settings object with best available fullscreen mode, according to
        /// the <paramref name="doesRequireHardware"/> and <paramref name="doesRequireReference"/> constraints.
        /// </summary>
        /// <param name="doesRequireHardware">The device requires hardware support.</param>
        /// <param name="doesRequireReference">The device requires the ref device.</param>
        /// <returns><c>true</c> if a mode is found, <c>false</c> otherwise.</returns>
        public bool FindBestFullscreenMode(bool doesRequireHardware, bool doesRequireReference)
        {
            D3DConfiguration result = new D3DConfiguration();

            // For fullscreen, default to first HAL DeviceCombo that supports the current desktop
            // display mode, or any display mode if HAL is not compatible with the desktop mode, or
            // non-HAL if no HAL is available
            DisplayMode bestAdapterDesktopDisplayMode = new DisplayMode();

            GraphicsAdapterInfo bestAdapterInfo = null;
            GraphicsDeviceInfo  bestDeviceInfo  = null;
            DeviceCombo         bestDeviceCombo = null;

            foreach (GraphicsAdapterInfo adapterInfo in _enumerationSettings.AdapterInfoList)
            {
                //if (GUIGraphicsContext._useScreenSelector)
                //  adapterInfo = FindAdapterForScreen(GUI.Library.GUIGraphicsContext.currentScreen);

                DisplayMode adapterDesktopDisplayMode = MPDirect3D.Direct3D.Adapters[adapterInfo.AdapterOrdinal].CurrentDisplayMode;
                foreach (GraphicsDeviceInfo deviceInfo in adapterInfo.DeviceInfos)
                {
                    if (doesRequireHardware && deviceInfo.DevType != DeviceType.Hardware)
                    {
                        continue;
                    }
                    if (doesRequireReference && deviceInfo.DevType != DeviceType.Reference)
                    {
                        continue;
                    }

                    foreach (DeviceCombo deviceCombo in deviceInfo.DeviceCombos)
                    {
                        if (deviceCombo.IsWindowed)
                        {
                            continue;
                        }

                        bool adapterMatchesBackBuffer = (deviceCombo.BackBufferFormat == deviceCombo.AdapterFormat);
                        bool adapterMatchesDesktop    = (deviceCombo.AdapterFormat == adapterDesktopDisplayMode.Format);

                        // If we haven't found a compatible set yet, or if this set
                        // is better (because it's a HAL, and/or because formats match better),
                        // save it
                        if (bestDeviceCombo == null ||
                            bestDeviceCombo.DevType != DeviceType.Hardware && deviceInfo.DevType == DeviceType.Hardware ||
                            bestDeviceCombo.DevType == DeviceType.Hardware &&
                            bestDeviceCombo.AdapterFormat != adapterDesktopDisplayMode.Format && adapterMatchesDesktop ||
                            bestDeviceCombo.DevType == DeviceType.Hardware && adapterMatchesDesktop && adapterMatchesBackBuffer)
                        {
                            bestAdapterDesktopDisplayMode = adapterDesktopDisplayMode;
                            bestAdapterInfo = adapterInfo;
                            bestDeviceInfo  = deviceInfo;
                            bestDeviceCombo = deviceCombo;
                            if (deviceInfo.DevType == DeviceType.Hardware && adapterMatchesDesktop && adapterMatchesBackBuffer)
                            {
                                // This fullscreen device combo looks great -- take it
                                goto EndFullscreenDeviceComboSearch;
                            }
                            // Otherwise keep looking for a better fullscreen device combo
                        }
                    }
                }
                //if (GUIGraphicsContext._useScreenSelector)
                //  break;// no need to loop again.. result would be the same
            }

EndFullscreenDeviceComboSearch:
            if (bestDeviceCombo == null)
            {
                return(false);
            }

            // Need to find a display mode on the best adapter that uses pBestDeviceCombo->AdapterFormat
            // and is as close to bestAdapterDesktopDisplayMode's res as possible
            foreach (DisplayMode displayMode in bestAdapterInfo.DisplayModes)
            {
                if (displayMode.Format != bestDeviceCombo.AdapterFormat)
                {
                    continue;
                }
                if (displayMode.Width == bestAdapterDesktopDisplayMode.Width &&
                    displayMode.Height == bestAdapterDesktopDisplayMode.Height &&
                    displayMode.RefreshRate == bestAdapterDesktopDisplayMode.RefreshRate)
                {
                    _desktopDisplayMode = displayMode;
                }
            }

            result.DisplayMode = bestAdapterDesktopDisplayMode;
            result.AdapterInfo = bestAdapterInfo;
            result.DeviceInfo  = bestDeviceInfo;
            result.DeviceCombo = bestDeviceCombo;

            return(true);
        }
Esempio n. 8
0
        /// <summary>
        /// Returns a settings object with best available windowed mode, according to
        /// the <paramref name="doesRequireHardware"/> and <paramref name="doesRequireReference"/> constraints.
        /// </summary>
        /// <param name="doesRequireHardware">The device requires hardware support.</param>
        /// <param name="doesRequireReference">The device requires the ref device.</param>
        /// <returns><c>true</c> if a mode is found, <c>false</c> otherwise.</returns>
        public D3DConfiguration FindBestWindowedMode(bool doesRequireHardware, bool doesRequireReference)
        {
            D3DConfiguration result = new D3DConfiguration();

            // Get display mode of primary adapter (which is assumed to be where the window will appear)
            DisplayMode         primaryDesktopDisplayMode = MPDirect3D.Direct3D.Adapters[0].CurrentDisplayMode;
            GraphicsAdapterInfo bestAdapterInfo           = null;
            GraphicsDeviceInfo  bestDeviceInfo            = null;
            DeviceCombo         bestDeviceCombo           = null;

            foreach (GraphicsAdapterInfo adapterInfo in _enumerationSettings.AdapterInfoList)
            {
                /*
                 * if (GUIGraphicsContext._useScreenSelector)
                 * {
                 * adapterInfo = FindAdapterForScreen(GUI.Library.GUIGraphicsContext.currentScreen);
                 * primaryDesktopDisplayMode = Direct3D.Adapters[adapterInfo.AdapterOrdinal].CurrentDisplayMode;
                 * }*/
                foreach (GraphicsDeviceInfo deviceInfo in adapterInfo.DeviceInfos)
                {
                    if (doesRequireHardware && deviceInfo.DevType != DeviceType.Hardware)
                    {
                        continue;
                    }
                    if (doesRequireReference && deviceInfo.DevType != DeviceType.Reference)
                    {
                        continue;
                    }

                    foreach (DeviceCombo deviceCombo in deviceInfo.DeviceCombos)
                    {
                        if (!deviceCombo.IsWindowed)
                        {
                            continue;
                        }
                        if (deviceCombo.AdapterFormat != primaryDesktopDisplayMode.Format)
                        {
                            continue;
                        }

                        bool adapterMatchesBackBuffer = (deviceCombo.BackBufferFormat == deviceCombo.AdapterFormat);

                        // If we haven't found a compatible DeviceCombo yet, or if this set
                        // is better (because it's a HAL, and/or because formats match better),
                        // save it
                        if (bestDeviceCombo == null ||
                            bestDeviceCombo.DevType != DeviceType.Hardware && deviceInfo.DevType == DeviceType.Hardware ||
                            deviceCombo.DevType == DeviceType.Hardware && adapterMatchesBackBuffer)
                        {
                            bestAdapterInfo = adapterInfo;
                            bestDeviceInfo  = deviceInfo;
                            bestDeviceCombo = deviceCombo;
                            if (deviceInfo.DevType == DeviceType.Hardware && adapterMatchesBackBuffer)
                            {
                                // This windowed device combo looks great -- take it
                                goto EndWindowedDeviceComboSearch;
                            }
                            // Otherwise keep looking for a better windowed device combo
                        }
                    }
                }
                //if (GUIGraphicsContext._useScreenSelector)
                //  break;// no need to loop again.. result would be the same
            }

EndWindowedDeviceComboSearch:
            if (bestDeviceCombo == null)
            {
                return(null);
            }

            result.AdapterInfo = bestAdapterInfo;
            result.DeviceInfo  = bestDeviceInfo;
            result.DeviceCombo = bestDeviceCombo;
            result.DisplayMode = primaryDesktopDisplayMode;

            return(result);
        }
Esempio n. 9
0
        /// <summary>
        /// Enumerates DeviceCombos for a particular device
        /// </summary>
        protected void EnumerateDeviceCombos(GraphicsDeviceInfo deviceInfo, ICollection <Format> adapterFormatList)
        {
            Format[] backBufferFormats = new Format[]
            {
                Format.A8R8G8B8, Format.X8R8G8B8, Format.A2R10G10B10,
                Format.R5G6B5, Format.A1R5G5B5, Format.X1R5G5B5,
            };
            bool[] bools = new bool[] { false, true };

            // See which adapter formats are supported by this device
            foreach (Format adapterFormat in adapterFormatList)
            {
                foreach (Format backBufferFormat in backBufferFormats)
                {
                    if (D3DUtil.GetAlphaChannelBits(backBufferFormat) < AppMinAlphaChannelBits)
                    {
                        continue;
                    }
                    foreach (bool isWindowed in bools)
                    {
                        if (!MPDirect3D.Direct3D.CheckDeviceType(deviceInfo.AdapterOrdinal, deviceInfo.DevType,
                                                                 adapterFormat, backBufferFormat, isWindowed))
                        {
                            continue;
                        }

                        // At this point, we have an adapter/device/adapterformat/backbufferformat/iswindowed
                        // DeviceCombo that is supported by the system.  We still need to confirm that it's
                        // compatible with the app, and find one or more suitable depth/stencil buffer format,
                        // multisample type, vertex processing type, and present interval.
                        DeviceCombo deviceCombo = new DeviceCombo
                        {
                            AdapterOrdinal   = deviceInfo.AdapterOrdinal,
                            DevType          = deviceInfo.DevType,
                            AdapterFormat    = adapterFormat,
                            BackBufferFormat = backBufferFormat,
                            IsWindowed       = isWindowed
                        };
                        if (AppUsesDepthBuffer)
                        {
                            BuildDepthStencilFormatList(deviceCombo);
                            if (deviceCombo.DepthStencilFormats.Count == 0)
                            {
                                continue;
                            }
                        }
                        BuildMultisampleTypeList(deviceCombo);
                        if (deviceCombo.MultisampleTypes.Count == 0)
                        {
                            continue;
                        }
                        BuildDepthStencilMultiSampleConflictList(deviceCombo);
                        BuildVertexProcessingTypeList(deviceInfo, deviceCombo);
                        if (deviceCombo.VertexProcessingTypes.Count == 0)
                        {
                            continue;
                        }
                        BuildPresentIntervalList(deviceInfo, deviceCombo);
                        if (deviceCombo.PresentIntervals.Count == 0)
                        {
                            continue;
                        }

                        deviceInfo.DeviceCombos.Add(deviceCombo);
                    }
                }
            }
        }