Exemplo 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);
     }
 }
Exemplo n.º 2
0
        protected void EnumerateDevices(GraphicsAdapterInfo adapterInfo, ICollection <Format> adapterFormatList)
        {
            DeviceType[] devTypeArray = new DeviceType[] { DeviceType.Hardware, DeviceType.Software, DeviceType.Reference };

            foreach (DeviceType devType in devTypeArray)
            {
                GraphicsDeviceInfo deviceInfo = new GraphicsDeviceInfo
                {
                    AdapterOrdinal = adapterInfo.AdapterOrdinal,
                    DevType        = devType
                };
                try
                {
                    deviceInfo.Caps = MPDirect3D.Direct3D.GetDeviceCaps(adapterInfo.AdapterOrdinal, devType);
                }
                catch
                {
                    continue;
                }

                // Get info for each devicecombo on this device
                EnumerateDeviceCombos(deviceInfo, adapterFormatList);

                // If at least one devicecombo for this device is found,
                // add the deviceInfo to the list
                if (deviceInfo.DeviceCombos.Count == 0)
                {
                    continue;
                }
                adapterInfo.DeviceInfos.Add(deviceInfo);
            }
        }
Exemplo n.º 3
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);
                }
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates the DirectX device.
        /// </summary>
        public DeviceEx CreateDevice(D3DConfiguration configuration)
        {
            GraphicsAdapterInfo adapterInfo = configuration.AdapterInfo;
            GraphicsDeviceInfo  deviceInfo  = configuration.DeviceInfo;

            // Set up the presentation parameters
            _presentParams = BuildPresentParamsFromSettings(_currentGraphicsConfiguration = configuration);

            if ((deviceInfo.Caps.PrimitiveMiscCaps & PrimitiveMiscCaps.NullReference) != 0)
            {
                // Warn user about null ref device that can't render anything
                HandleException(new NullReferenceDeviceException(), ApplicationMessage.None);
            }

            CreateFlags createFlags;

            if (configuration.DeviceCombo.VertexProcessingTypes.Contains(VertexProcessingType.PureHardware))
            {
                createFlags = CreateFlags.HardwareVertexProcessing; // | CreateFlags.PureDevice;
            }
            else if (configuration.DeviceCombo.VertexProcessingTypes.Contains(VertexProcessingType.Hardware))
            {
                createFlags = CreateFlags.HardwareVertexProcessing;
            }
            else if (configuration.DeviceCombo.VertexProcessingTypes.Contains(VertexProcessingType.Mixed))
            {
                createFlags = CreateFlags.MixedVertexProcessing;
            }
            else if (configuration.DeviceCombo.VertexProcessingTypes.Contains(VertexProcessingType.Software))
            {
                createFlags = CreateFlags.SoftwareVertexProcessing;
            }
            else
            {
                throw new ApplicationException();
            }

            ServiceRegistration.Get <ILogger>().Info("DirectX: Using adapter: {0} {1} {2}",
                                                     configuration.AdapterInfo.AdapterOrdinal,
                                                     MPDirect3D.Direct3D.Adapters[configuration.AdapterInfo.AdapterOrdinal].Details.Description,
                                                     configuration.DeviceInfo.DevType);

            // Create the device
            DeviceEx result = new DeviceEx(MPDirect3D.Direct3D,
                                           configuration.AdapterInfo.AdapterOrdinal,
                                           configuration.DeviceInfo.DevType,
                                           _renderTarget.Handle,
                                           createFlags | CreateFlags.Multithreaded | CreateFlags.EnablePresentStatistics,
                                           _presentParams);

            // When moving from fullscreen to windowed mode, it is important to
            // adjust the window size after recreating the device rather than
            // beforehand to ensure that you get the window size you want.  For
            // example, when switching from 640x480 fullscreen to windowed with
            // a 1000x600 window on a 1024x768 desktop, it is impossible to set
            // the window size to 1000x600 until after the display mode has
            // changed to 1024x768, because windows cannot be larger than the
            // desktop.

            StringBuilder sb = new StringBuilder();

            // Store device description
            if (deviceInfo.DevType == DeviceType.Reference)
            {
                sb.Append("REF");
            }
            else if (deviceInfo.DevType == DeviceType.Hardware)
            {
                sb.Append("HAL");
            }
            else if (deviceInfo.DevType == DeviceType.Software)
            {
                sb.Append("SW");
            }

            if (deviceInfo.DevType == DeviceType.Hardware)
            {
                sb.Append(": ");
                sb.Append(adapterInfo.AdapterDetails.Description);
            }

            ServiceRegistration.Get <ILogger>().Info("DirectX: {0}", sb.ToString());
            return(result);
        }
Exemplo n.º 7
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);
                    }
                }
            }
        }