public GraphicsDeviceInformation Clone()
 {
     GraphicsDeviceInformation gdi = new GraphicsDeviceInformation();
     gdi.adapter = Adapter;
     gdi.deviceType = DeviceType;
     gdi.presentationParameters = PresentationParameters;
     return gdi;
 }
        protected override void RankDevices(List <GraphicsDeviceInformation> foundDevices)
        {
            List <GraphicsDeviceInformation> removals = new List <GraphicsDeviceInformation>();

            for (int i = 0; i < foundDevices.Count; i++)
            {
                GraphicsDeviceInformation gdi = foundDevices[i];
                if (!gdi.Adapter.DeviceName.Contains("DISPLAY" + DisplayID))
                {
                    removals.Add(foundDevices[i]);
                }
            }
            foreach (GraphicsDeviceInformation info in removals)
            {
                foundDevices.Remove(info);
            }
            base.RankDevices(foundDevices);
        }
Exemple #3
0
        /// <summary>
        /// This populates a GraphicsDeviceInformation instance and invokes PreparingDeviceSettings to
        /// allow users to change the settings. Then returns that GraphicsDeviceInformation.
        /// Throws NullReferenceException if users set GraphicsDeviceInformation.PresentationParameters to null.
        /// </summary>
        private GraphicsDeviceInformation DoPreparingDeviceSettings()
        {
            var gdi = new GraphicsDeviceInformation();

            PrepareGraphicsDeviceInformation(gdi);
            var preparingDeviceSettingsHandler = PreparingDeviceSettings;

            if (preparingDeviceSettingsHandler != null)
            {
                // this allows users to overwrite settings through the argument
                var args = new PreparingDeviceSettingsEventArgs(gdi);
                preparingDeviceSettingsHandler(this, args);

                if (gdi.PresentationParameters == null || gdi.Adapter == null)
                {
                    throw new NullReferenceException("Members should not be set to null in PreparingDeviceSettingsEventArgs");
                }
            }

            return(gdi);
        }
 private void CreateDevice(GraphicsDeviceInformation newInfo)
 {
     if (this.device != null)
     {
         this.device.Dispose();
         this.device = null;
     }
     this.OnPreparingDeviceSettings(this, new PreparingDeviceSettingsEventArgs(newInfo));
     this.MassagePresentParameters(newInfo.PresentationParameters);
     try
     {
         this.ValidateGraphicsDeviceInformation(newInfo);
         GraphicsDevice device = new GraphicsDevice(newInfo.Adapter, newInfo.DeviceType, new System.Windows.Interop.WindowInteropHelper(this.game.Window).Handle, newInfo.PresentationParameters);
         this.device = device;
         this.device.DeviceResetting += new EventHandler(this.HandleDeviceResetting);
         this.device.DeviceReset     += new EventHandler(this.HandleDeviceReset);
         this.device.DeviceLost      += new EventHandler(this.HandleDeviceLost);
         this.device.Disposing       += new EventHandler(this.HandleDisposing);
     }
     catch (DeviceNotSupportedException exception)
     {
         throw this.CreateNoSuitableGraphicsDeviceException("Direct3D not avaible", exception);
     }
     catch (DriverInternalErrorException exception2)
     {
         throw this.CreateNoSuitableGraphicsDeviceException("Direct3D internal error", exception2);
     }
     catch (ArgumentException exception3)
     {
         throw this.CreateNoSuitableGraphicsDeviceException("Invalid create parameter", exception3);
     }
     catch (Exception exception4)
     {
         throw this.CreateNoSuitableGraphicsDeviceException("Create 3D error", exception4);
     }
     this.OnDeviceCreated(this, EventArgs.Empty);
 }
        public GraphicsDeviceService()
        {
            System.Windows.Forms.Control control = new System.Windows.Forms.Panel();

            deviceInformation = new GraphicsDeviceInformation()
            {
                GraphicsProfile        = FeatureLevel.Level_11_0,
                DeviceCreationFlags    = SharpDX.Direct3D11.DeviceCreationFlags.Debug,
                PresentationParameters = new PresentationParameters()
                {
                    BackBufferFormat          = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                    DepthStencilFormat        = DepthFormat.Depth32,
                    DepthBufferShaderResource = true,
                    BackBufferWidth           = 1920,
                    BackBufferHeight          = 1080,
                    IsFullScreen       = false,
                    RenderTargetUsage  = SharpDX.DXGI.Usage.RenderTargetOutput,
                    MultiSampleCount   = MSAALevel.None,
                    DeviceWindowHandle = control
                }
            };

            FindAdapter();

            graphicsDevice = GraphicsDevice.New(deviceInformation.Adapter, deviceInformation.DeviceCreationFlags);

            renderTarget             = RenderTarget2D.New(graphicsDevice, 1920, 1080, PixelFormat.B8G8R8A8.UNorm);
            graphicsDevice.Presenter = new RenderTargetGraphicsPresenter(graphicsDevice, renderTarget, DepthFormat.Depth32, false);

            dxDevice  = (Device)typeof(GraphicsDevice).GetField("Device", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(GraphicsDevice);
            dxContext = (DeviceContext)typeof(GraphicsDevice).GetField("Context", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(GraphicsDevice);

            if (DeviceCreated != null)
            {
                DeviceCreated(this, EventArgs.Empty);
            }
        }
        public GraphicsDeviceService(System.Windows.Forms.Form form)
        {
            var ad = GraphicsAdapter.Default;

            deviceInformation = new GraphicsDeviceInformation()
            {
                GraphicsProfile        = FeatureLevel.Level_11_0,
                DeviceCreationFlags    = SharpDX.Direct3D11.DeviceCreationFlags.Debug,
                PresentationParameters = new PresentationParameters()
                {
                    BackBufferFormat          = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                    DepthStencilFormat        = DepthFormat.Depth32,
                    DepthBufferShaderResource = true,
                    BackBufferWidth           = form.Width,
                    BackBufferHeight          = form.Height,
                    IsFullScreen       = false,
                    RenderTargetUsage  = SharpDX.DXGI.Usage.RenderTargetOutput,
                    MultiSampleCount   = MSAALevel.None,
                    DeviceWindowHandle = form
                }
            };

            FindAdapter();

            graphicsDevice           = GraphicsDevice.New(deviceInformation.Adapter, deviceInformation.DeviceCreationFlags);
            graphicsDevice.Presenter = new SwapChainGraphicsPresenter(graphicsDevice, deviceInformation.PresentationParameters);

            dxDevice  = (Device)typeof(GraphicsDevice).GetField("Device", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(GraphicsDevice);
            dxContext = (DeviceContext)typeof(GraphicsDevice).GetField("Context", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(GraphicsDevice);

            this.Form = form;

            if (DeviceCreated != null)
            {
                DeviceCreated(this, EventArgs.Empty);
            }
        }
        void IGraphicsDeviceManager.CreateDevice()
        {
            // Set the default device information
            GraphicsDeviceInformation gdi = new GraphicsDeviceInformation();
            gdi.Adapter = GraphicsAdapter.DefaultAdapter;
            gdi.GraphicsProfile = GraphicsProfile;
            gdi.PresentationParameters = new PresentationParameters();
            gdi.PresentationParameters.DeviceWindowHandle = game.Window.Handle;
            gdi.PresentationParameters.DepthStencilFormat = DepthFormat.Depth24;
            gdi.PresentationParameters.IsFullScreen = false;

            // Give the user a chance to change the initial settings
            OnPreparingDeviceSettings(
                this,
                new PreparingDeviceSettingsEventArgs(gdi)
            );

            // Apply these settings to this GraphicsDeviceManager
            GraphicsProfile = gdi.GraphicsProfile;
            PreferredBackBufferFormat = gdi.PresentationParameters.BackBufferFormat;
            PreferredDepthStencilFormat = gdi.PresentationParameters.DepthStencilFormat;

            // Create the GraphicsDevice, apply the initial settings.
            graphicsDevice = new GraphicsDevice(
                gdi.Adapter,
                gdi.GraphicsProfile,
                gdi.PresentationParameters
            );
            ApplyChanges();

            /* Set the new display orientation on the touch panel.
             *
             * TODO: In XNA this seems to be done as part of the
             * GraphicsDevice.DeviceReset event... we need to get
             * those working.
             */
            TouchPanel.DisplayOrientation =
                graphicsDevice.PresentationParameters.DisplayOrientation;

            // Call the DeviceCreated Event
            OnDeviceCreated(this, EventArgs.Empty);
        }
        public void AreEqualTest()
        {
            GraphicsDeviceInformation gdi2 = new GraphicsDeviceInformation();

            Assert.AreEqual(gdi, gdi2, "Objects were expected to be equal");
        }
 public void Setup()
 {
     gdi = new GraphicsDeviceInformation();
 }
 /// <summary>
 /// Start game in a window at render target resolution;
 /// </summary>
 private void StartWindowed(GraphicsDeviceInformation graphicsDeviceInformation)
 {
     // Set back buffer size to match render target size
     graphicsDeviceInformation.PresentationParameters.BackBufferWidth  = displayMode.Width;
     graphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height;
 }
		void IGraphicsDeviceManager.CreateDevice()
		{
			// Set the default device information
			GraphicsDeviceInformation gdi = new GraphicsDeviceInformation();
			gdi.Adapter = GraphicsAdapter.DefaultAdapter;
			gdi.GraphicsProfile = GraphicsProfile;
			gdi.PresentationParameters = new PresentationParameters();
			gdi.PresentationParameters.DeviceWindowHandle = game.Window.Handle;
			gdi.PresentationParameters.DepthStencilFormat = DepthFormat.Depth24;
			gdi.PresentationParameters.IsFullScreen = false;

			// Give the user a chance to change the initial settings
			OnPreparingDeviceSettings(
				this,
				new PreparingDeviceSettingsEventArgs(gdi)
			);

			// Apply these settings to this GraphicsDeviceManager
			GraphicsProfile = gdi.GraphicsProfile;
			PreferredBackBufferFormat = gdi.PresentationParameters.BackBufferFormat;
			PreferredDepthStencilFormat = gdi.PresentationParameters.DepthStencilFormat;

			// Create the GraphicsDevice, apply the initial settings.
			graphicsDevice = new GraphicsDevice(
				gdi.Adapter,
				gdi.GraphicsProfile,
				gdi.PresentationParameters
			);
			ApplyChanges();

			// Call the DeviceCreated Event
			OnDeviceCreated(this, EventArgs.Empty);
		}
Exemple #12
0
 public SpriteSystem(GraphicsDeviceInformation graphicsDevice)
     : base(Aspect.All(typeof(SpriteComponent), typeof(TransformComponent)))
 {
 }
        private void ChangeDevice(bool forceCreate)
        {
            if (this.m_objGame == null)
            {
                throw new InvalidOperationException(Resources.GraphicsComponentNotAttachedToGame);
            }
            this.m_bInDeviceTransition = true;
            string screenDeviceName = string.Empty;
            int    width            = 0;
            int    height           = 0;

            if (this.m_objGame.Canvas != null && this.m_objGame.Canvas.Window != null)
            {
                screenDeviceName = this.m_objGame.Canvas.Window.Title;
                width            = (int)this.m_objGame.Canvas.Window.ActualWidth;
                height           = (int)this.m_objGame.Canvas.Window.ActualHeight;
            }
            bool flag = false;

            try
            {
                GraphicsDeviceInformation graphicsDeviceInformation = this.FindBestDevice(forceCreate);
                //this.game.Window.BeginScreenDeviceChange(graphicsDeviceInformation.PresentationParameters.IsFullScreen);
                flag = true;
                bool flag2 = true;
                if (!forceCreate && (this.m_objDevice != null))
                {
                    this.OnPreparingDeviceSettings(this, new PreparingDeviceSettingsEventArgs(graphicsDeviceInformation));
                    if (this.CanResetDevice(graphicsDeviceInformation))
                    {
                        try
                        {
                            GraphicsDeviceInformation information2 = graphicsDeviceInformation.Clone();
                            MassagePresentParameters(graphicsDeviceInformation.PresentationParameters);
                            ValidateGraphicsDeviceInformation(graphicsDeviceInformation);
                            m_objDevice.Reset(information2.PresentationParameters, information2.Adapter);
                            flag2 = false;
                        }
                        catch
                        {
                        }
                    }
                }
                if (flag2)
                {
                    this.CreateDevice(graphicsDeviceInformation);
                }
                PresentationParameters presentationParameters = this.m_objDevice.PresentationParameters;
                screenDeviceName           = this.m_objDevice.Adapter.DeviceName;
                this.m_bIsReallyFullScreen = presentationParameters.IsFullScreen;
                if (presentationParameters.BackBufferWidth != 0)
                {
                    width = presentationParameters.BackBufferWidth;
                }
                if (presentationParameters.BackBufferHeight != 0)
                {
                    height = presentationParameters.BackBufferHeight;
                }
                this.m_bIsDeviceDirty = false;
            }
            finally
            {
                if (flag)
                {
                    //this.game.Window.EndScreenDeviceChange(screenDeviceName, width, height);
                }
                this.m_bInDeviceTransition = false;
            }
        }
        private void ChangeDevice(bool forceCreate)
        {
            if (this.game == null)
            {
                throw new InvalidOperationException("Graphics component not attached to game");
            }
            this.CheckForAvailableSupportedHardware();
            this.inDeviceTransition = true;
            string screenDeviceName = this.game.Window.Title;
            int    width            = (int)this.game.Window.ActualWidth;
            int    height           = (int)this.game.Window.ActualHeight;
            bool   flag             = false;

            try
            {
                GraphicsDeviceInformation graphicsDeviceInformation = this.FindBestDevice(forceCreate);
                //this.game.Window.BeginScreenDeviceChange(graphicsDeviceInformation.PresentationParameters.IsFullScreen);
                flag = true;
                bool flag2 = true;
                if (!forceCreate && (this.device != null))
                {
                    this.OnPreparingDeviceSettings(this, new PreparingDeviceSettingsEventArgs(graphicsDeviceInformation));
                    if (this.CanResetDevice(graphicsDeviceInformation))
                    {
                        try
                        {
                            GraphicsDeviceInformation information2 = graphicsDeviceInformation.Clone();
                            this.MassagePresentParameters(graphicsDeviceInformation.PresentationParameters);
                            this.ValidateGraphicsDeviceInformation(graphicsDeviceInformation);

                            this.device.Reset(information2.PresentationParameters, information2.Adapter);
                            flag2 = false;
                        }
                        catch
                        {
                        }
                    }
                }
                if (flag2)
                {
                    this.CreateDevice(graphicsDeviceInformation);
                }
                PresentationParameters presentationParameters = this.device.PresentationParameters;
                screenDeviceName        = this.device.CreationParameters.Adapter.DeviceName;
                this.isReallyFullScreen = presentationParameters.IsFullScreen;
                if (presentationParameters.BackBufferWidth != 0)
                {
                    width = presentationParameters.BackBufferWidth;
                }
                if (presentationParameters.BackBufferHeight != 0)
                {
                    height = presentationParameters.BackBufferHeight;
                }
                this.isDeviceDirty = false;
            }
            finally
            {
                if (flag)
                {
                    //this.game.Window.EndScreenDeviceChange(screenDeviceName, width, height);
                }
                this.inDeviceTransition = false;
            }
        }
 protected virtual bool CanResetDevice(GraphicsDeviceInformation newDeviceInfo)
 {
     if (this.device.CreationParameters.DeviceType != newDeviceInfo.DeviceType)
     {
         return false;
     }
     return true;
 }
 private void AddDevices(GraphicsAdapter adapter, DeviceType deviceType, DisplayMode mode, GraphicsDeviceInformation baseDeviceInfo, List <GraphicsDeviceInformation> foundDevices)
 {
     for (int i = 0; i < ValidBackBufferFormats.Length; i++)
     {
         SurfaceFormat backBufferFormat = ValidBackBufferFormats[i];
         if (adapter.CheckDeviceType(deviceType, mode.Format, backBufferFormat, this.IsFullScreen))
         {
             GraphicsDeviceInformation item = baseDeviceInfo.Clone();
             if (this.IsFullScreen)
             {
                 item.PresentationParameters.BackBufferWidth           = mode.Width;
                 item.PresentationParameters.BackBufferHeight          = mode.Height;
                 item.PresentationParameters.FullScreenRefreshRateInHz = mode.RefreshRate;
             }
             else if (this.useResizedBackBuffer)
             {
                 item.PresentationParameters.BackBufferWidth  = this.resizedBackBufferWidth;
                 item.PresentationParameters.BackBufferHeight = this.resizedBackBufferHeight;
             }
             else
             {
                 item.PresentationParameters.BackBufferWidth  = this.PreferredBackBufferWidth;
                 item.PresentationParameters.BackBufferHeight = this.PreferredBackBufferHeight;
             }
             item.PresentationParameters.BackBufferFormat       = backBufferFormat;
             item.PresentationParameters.AutoDepthStencilFormat = this.ChooseDepthStencilFormat(adapter, deviceType, mode.Format);
             if (this.PreferMultiSampling)
             {
                 for (int j = 0; j < multiSampleTypes.Length; j++)
                 {
                     int             qualityLevels = 0;
                     MultiSampleType sampleType    = multiSampleTypes[j];
                     if (adapter.CheckDeviceMultiSampleType(deviceType, backBufferFormat, this.IsFullScreen, sampleType, out qualityLevels))
                     {
                         GraphicsDeviceInformation information2 = item.Clone();
                         information2.PresentationParameters.MultiSampleType = sampleType;
                         if (!foundDevices.Contains(information2))
                         {
                             foundDevices.Add(information2);
                         }
                         break;
                     }
                 }
             }
             else if (!foundDevices.Contains(item))
             {
                 foundDevices.Add(item);
             }
         }
     }
 }
        protected virtual GraphicsDeviceInformation FindBestDevice(bool anySuitableDevice)
        {
			List<GraphicsDeviceInformation> graphicDeviceInfoList = new List<GraphicsDeviceInformation>();
			
			int flags = Sdl.SDL_OPENGL;
			if (IsFullScreen)
				flags |= Sdl.SDL_FULLSCREEN;

#warning Finding available display modes along with their formats isn't supported by the current release of Tao.Sdl.
#warning The current pixel format is used for querying available modes
			Sdl.SDL_Rect[] modes = Sdl.SDL_ListModes(IntPtr.Zero, flags);
			if (modes.Length > 0)
			{	            
	            foreach (Sdl.SDL_Rect mode in modes)
	            {
	                if (mode.w == PreferredBackBufferWidth &&
	                    mode.h == PreferredBackBufferHeight)
	                    continue;
	
					GraphicsDeviceInformation info = new GraphicsDeviceInformation();
					info.Adapter = GraphicsAdapter.DefaultAdapter;
					info.DeviceType = DeviceType.Hardware;
					info.PresentationParameters.BackBufferWidth = PreferredBackBufferWidth;
					info.PresentationParameters.BackBufferHeight = PreferredBackBufferHeight;
					info.PresentationParameters.BackBufferFormat = PreferredBackBufferFormat;
					info.PresentationParameters.IsFullScreen = IsFullScreen;
	                graphicDeviceInfoList.Add(info);
				}
			}
			else 
			{
				GraphicsDeviceInformation info = new GraphicsDeviceInformation();
				info.Adapter = GraphicsAdapter.DefaultAdapter;
				info.DeviceType = DeviceType.Hardware;
				info.PresentationParameters.BackBufferWidth = PreferredBackBufferWidth;
				info.PresentationParameters.BackBufferHeight = PreferredBackBufferHeight;
				info.PresentationParameters.BackBufferFormat = PreferredBackBufferFormat;
				info.PresentationParameters.IsFullScreen = IsFullScreen;
	            graphicDeviceInfoList.Add(info);
			}
			
            RankDevices(graphicDeviceInfoList);

            if (graphicDeviceInfoList.Count == 0)
                throw new NoSuitableGraphicsDeviceException("The process of ranking devices removed all compatible devices.");  // LOCALIZE

			
			return graphicDeviceInfoList[0];
        }
 protected virtual bool CanResetDevice(GraphicsDeviceInformation newDeviceInfo)
 {
     throw new NotImplementedException();
 }
        private static bool HookCanResetDevice(orig_CanResetDevice orig, GraphicsDeviceManager self, GraphicsDeviceInformation newDeviceInfo)
        {
            LogGraphicsDevice();

            var changes = new StringBuilder();

            void AddParam <T>(string name, T t1, T t2)
            {
                changes.Append(", ").Append(name).Append(": ").Append(t1);
                if (!EqualityComparer <T> .Default.Equals(t1, t2))
                {
                    changes.Append(" -> ").Append(t2);
                }
            }

            AddParam("Profile", self.GraphicsDevice.GraphicsProfile, newDeviceInfo.GraphicsProfile);
            AddParam("Width", self.GraphicsDevice.PresentationParameters.BackBufferWidth, newDeviceInfo.PresentationParameters.BackBufferWidth);
            AddParam("Height", self.GraphicsDevice.PresentationParameters.BackBufferHeight, newDeviceInfo.PresentationParameters.BackBufferHeight);
            AddParam("Fullscreen", self.GraphicsDevice.PresentationParameters.IsFullScreen, newDeviceInfo.PresentationParameters.IsFullScreen);
            AddParam("Display", self.GraphicsDevice.Adapter.DeviceName, newDeviceInfo.Adapter.DeviceName);

            Logging.Terraria.Debug("Device Reset" + changes);

            // we can force a device recreation, to 'simulate' the bug as it's not very reproducible
            //return false;

            return(orig(self, newDeviceInfo));
        }
        private void ValidateGraphicsDeviceInformation(GraphicsDeviceInformation devInfo)
        {
            SurfaceFormat format;
            GraphicsAdapter adapter = devInfo.Adapter;
            DeviceType deviceType = devInfo.DeviceType;
            bool enableAutoDepthStencil = devInfo.PresentationParameters.EnableAutoDepthStencil;
            DepthFormat autoDepthStencilFormat = devInfo.PresentationParameters.AutoDepthStencilFormat;
            SurfaceFormat backBufferFormat = devInfo.PresentationParameters.BackBufferFormat;
            int backBufferWidth = devInfo.PresentationParameters.BackBufferWidth;
            int backBufferHeight = devInfo.PresentationParameters.BackBufferHeight;
            PresentationParameters presentationParameters = devInfo.PresentationParameters;
            SurfaceFormat format4 = presentationParameters.BackBufferFormat;
            if (!presentationParameters.IsFullScreen)
            {
                format = adapter.CurrentDisplayMode.Format;
                if (SurfaceFormat.Unknown == presentationParameters.BackBufferFormat)
                {
                    format4 = format;
                }
            }
            else
            {
                SurfaceFormat format5 = presentationParameters.BackBufferFormat;
                if (format5 != SurfaceFormat.Color)
                {
                    if (format5 != SurfaceFormat.Bgra5551)
                    {
                        format = presentationParameters.BackBufferFormat;
                    }
                    else
                    {
                        format = SurfaceFormat.Bgr555;
                    }
                }
                else
                {
                    format = SurfaceFormat.Bgr32;
                }
            }
            if (-1 == Array.IndexOf<SurfaceFormat>(ValidBackBufferFormats, format4))
            {
                throw new ArgumentException(Resources.ValidateBackBufferFormatIsInvalid);
            }
            if (!adapter.CheckDeviceType(deviceType, format, presentationParameters.BackBufferFormat, presentationParameters.IsFullScreen))
            {
                throw new ArgumentException(Resources.ValidateDeviceType);
            }
            if ((presentationParameters.BackBufferCount < 0) || (presentationParameters.BackBufferCount > 3))
            {
                throw new ArgumentException(Resources.ValidateBackBufferCount);
            }
            if ((presentationParameters.BackBufferCount > 1) && (presentationParameters.SwapEffect == SwapEffect.Copy))
            {
                throw new ArgumentException(Resources.ValidateBackBufferCountSwapCopy);
            }
            switch (presentationParameters.SwapEffect)
            {
                case SwapEffect.Discard:
                case SwapEffect.Flip:
                case SwapEffect.Copy:
                    {
                        int num3;
                        if (!adapter.CheckDeviceMultiSampleType(deviceType, format4, presentationParameters.IsFullScreen, presentationParameters.MultiSampleType, out num3))
                        {
                            throw new ArgumentException(Resources.ValidateMultiSampleTypeInvalid);
                        }
                        if (presentationParameters.MultiSampleQuality >= num3)
                        {
                            throw new ArgumentException(Resources.ValidateMultiSampleQualityInvalid);
                        }
                        if ((presentationParameters.MultiSampleType != MultiSampleType.None) && (presentationParameters.SwapEffect != SwapEffect.Discard))
                        {
                            throw new ArgumentException(Resources.ValidateMultiSampleSwapEffect);
                        }
                        if (((presentationParameters.PresentOptions & PresentOptions.DiscardDepthStencil) != PresentOptions.None) && !presentationParameters.EnableAutoDepthStencil)
                        {
                            throw new ArgumentException(Resources.ValidateAutoDepthStencilMismatch);
                        }
                        if (presentationParameters.EnableAutoDepthStencil)
                        {
                            if (!adapter.CheckDeviceFormat(deviceType, format, TextureUsage.None, QueryUsages.None, ResourceType.DepthStencilBuffer, presentationParameters.AutoDepthStencilFormat))
                            {
                                throw new ArgumentException(Resources.ValidateAutoDepthStencilFormatInvalid);
                            }
                            if (!adapter.CheckDepthStencilMatch(deviceType, format, format4, presentationParameters.AutoDepthStencilFormat))
                            {
                                throw new ArgumentException(Resources.ValidateAutoDepthStencilFormatIncompatible);
                            }
                        }
                        if (!presentationParameters.IsFullScreen)
                        {
                            if (presentationParameters.FullScreenRefreshRateInHz != 0)
                            {
                                throw new ArgumentException(Resources.ValidateRefreshRateInWindow);
                            }
                            switch (presentationParameters.PresentationInterval)
                            {
                                case PresentInterval.Default:
                                case PresentInterval.One:
                                case PresentInterval.Immediate:
                                    return;
                            }
                            throw new ArgumentException(Resources.ValidatePresentationIntervalInWindow);
                        }
                        if (presentationParameters.FullScreenRefreshRateInHz == 0)
                        {
                            throw new ArgumentException(Resources.ValidateRefreshRateInFullScreen);
                        }
                        GraphicsDeviceCapabilities capabilities = adapter.GetCapabilities(deviceType);
                        switch (presentationParameters.PresentationInterval)
                        {
                            case PresentInterval.Default:
                            case PresentInterval.One:
                            case PresentInterval.Immediate:
                                goto Label_02E5;

                            case PresentInterval.Two:
                            case PresentInterval.Three:
                            case PresentInterval.Four:
                                if ((capabilities.PresentInterval & presentationParameters.PresentationInterval) == PresentInterval.Default)
                                {
                                    throw new ArgumentException(Resources.ValidatePresentationIntervalIncompatibleInFullScreen);
                                }
                                goto Label_02E5;
                        }
                        break;
                    }
                default:
                    throw new ArgumentException(Resources.ValidateSwapEffectInvalid);
            }
            throw new ArgumentException(Resources.ValidatePresentationIntervalInFullScreen);
            Label_02E5:
            if (presentationParameters.IsFullScreen)
            {
                if ((presentationParameters.BackBufferWidth == 0) || (presentationParameters.BackBufferHeight == 0))
                {
                    throw new ArgumentException(Resources.ValidateBackBufferDimsFullScreen);
                }
                bool flag2 = true;
                bool flag3 = false;
                DisplayMode currentDisplayMode = adapter.CurrentDisplayMode;
                if (((currentDisplayMode.Format != format) && (currentDisplayMode.Width != presentationParameters.BackBufferHeight)) && ((currentDisplayMode.Height != presentationParameters.BackBufferHeight) && (currentDisplayMode.RefreshRate != presentationParameters.FullScreenRefreshRateInHz)))
                {
                    flag2 = false;
                    foreach (DisplayMode mode2 in adapter.SupportedDisplayModes[format])
                    {
                        if ((mode2.Width == presentationParameters.BackBufferWidth) && (mode2.Height == presentationParameters.BackBufferHeight))
                        {
                            flag3 = true;
                            if (mode2.RefreshRate == presentationParameters.FullScreenRefreshRateInHz)
                            {
                                flag2 = true;
                                break;
                            }
                        }
                    }
                }
                if (!flag2 && flag3)
                {
                    throw new ArgumentException(Resources.ValidateBackBufferDimsModeFullScreen);
                }
                if (!flag2)
                {
                    throw new ArgumentException(Resources.ValidateBackBufferHzModeFullScreen);
                }
            }
            if (presentationParameters.EnableAutoDepthStencil != enableAutoDepthStencil)
            {
                throw new ArgumentException(Resources.ValidateAutoDepthStencilAdapterGroup);
            }
            if (presentationParameters.EnableAutoDepthStencil)
            {
                if (presentationParameters.AutoDepthStencilFormat != autoDepthStencilFormat)
                {
                    throw new ArgumentException(Resources.ValidateAutoDepthStencilAdapterGroup);
                }
                if (presentationParameters.BackBufferFormat != backBufferFormat)
                {
                    throw new ArgumentException(Resources.ValidateAutoDepthStencilAdapterGroup);
                }
                if (presentationParameters.BackBufferWidth != backBufferWidth)
                {
                    throw new ArgumentException(Resources.ValidateAutoDepthStencilAdapterGroup);
                }
                if (presentationParameters.BackBufferHeight != backBufferHeight)
                {
                    throw new ArgumentException(Resources.ValidateAutoDepthStencilAdapterGroup);
                }
            }
        }
 private void AddDevices(GraphicsAdapter adapter, DeviceType deviceType, DisplayMode mode, GraphicsDeviceInformation baseDeviceInfo, List<GraphicsDeviceInformation> foundDevices)
 {
     for (int i = 0; i < ValidBackBufferFormats.Length; i++)
     {
         SurfaceFormat backBufferFormat = ValidBackBufferFormats[i];
         if (adapter.CheckDeviceType(deviceType, mode.Format, backBufferFormat, this.IsFullScreen))
         {
             GraphicsDeviceInformation item = baseDeviceInfo.Clone();
             if (this.IsFullScreen)
             {
                 item.PresentationParameters.BackBufferWidth = mode.Width;
                 item.PresentationParameters.BackBufferHeight = mode.Height;
                 item.PresentationParameters.FullScreenRefreshRateInHz = mode.RefreshRate;
             }
             else if (this.useResizedBackBuffer)
             {
                 item.PresentationParameters.BackBufferWidth = this.resizedBackBufferWidth;
                 item.PresentationParameters.BackBufferHeight = this.resizedBackBufferHeight;
             }
             else
             {
                 item.PresentationParameters.BackBufferWidth = this.PreferredBackBufferWidth;
                 item.PresentationParameters.BackBufferHeight = this.PreferredBackBufferHeight;
             }
             item.PresentationParameters.BackBufferFormat = backBufferFormat;
             item.PresentationParameters.AutoDepthStencilFormat = this.ChooseDepthStencilFormat(adapter, deviceType, mode.Format);
             if (this.PreferMultiSampling)
             {
                 for (int j = 0; j < multiSampleTypes.Length; j++)
                 {
                     int qualityLevels = 0;
                     MultiSampleType sampleType = multiSampleTypes[j];
                     if (adapter.CheckDeviceMultiSampleType(deviceType, backBufferFormat, this.IsFullScreen, sampleType, out qualityLevels))
                     {
                         GraphicsDeviceInformation information2 = item.Clone();
                         information2.PresentationParameters.MultiSampleType = sampleType;
                         if (!foundDevices.Contains(information2))
                         {
                             foundDevices.Add(information2);
                         }
                         break;
                     }
                 }
             }
             else if (!foundDevices.Contains(item))
             {
                 foundDevices.Add(item);
             }
         }
     }
 }
 protected virtual bool CanResetDevice(GraphicsDeviceInformation newDeviceInfo)
 {
     return(true);
 }
 private void AddDevices(bool anySuitableDevice, List<GraphicsDeviceInformation> foundDevices)
 {
     IntPtr handle =  new System.Windows.Interop.WindowInteropHelper(this.game.Window).Handle;
     foreach (GraphicsAdapter adapter in GraphicsAdapter.Adapters)
     {
         //if (anySuitableDevice)// || this.IsWindowOnAdapter(handle, adapter))
         {
             foreach (DeviceType type in ValidDeviceTypes)
             {
                 try
                 {
                     if (adapter.IsDeviceTypeAvailable(type))
                     {
                         GraphicsDeviceCapabilities capabilities = adapter.GetCapabilities(type);
                         if ((capabilities.DeviceCapabilities.IsDirect3D9Driver && IsValidShaderProfile(capabilities.MaxPixelShaderProfile, this.MinimumPixelShaderProfile)) && IsValidShaderProfile(capabilities.MaxVertexShaderProfile, this.MinimumVertexShaderProfile))
                         {
                             GraphicsDeviceInformation baseDeviceInfo = new GraphicsDeviceInformation();
                             baseDeviceInfo.Adapter = adapter;
                             baseDeviceInfo.DeviceType = type;
                             baseDeviceInfo.PresentationParameters.DeviceWindowHandle = IntPtr.Zero;
                             baseDeviceInfo.PresentationParameters.EnableAutoDepthStencil = true;
                             baseDeviceInfo.PresentationParameters.BackBufferCount = 1;
                             baseDeviceInfo.PresentationParameters.PresentOptions = PresentOptions.None;
                             baseDeviceInfo.PresentationParameters.SwapEffect = SwapEffect.Discard;
                             baseDeviceInfo.PresentationParameters.FullScreenRefreshRateInHz = 0;
                             baseDeviceInfo.PresentationParameters.MultiSampleQuality = 0;
                             baseDeviceInfo.PresentationParameters.MultiSampleType = MultiSampleType.None;
                             baseDeviceInfo.PresentationParameters.IsFullScreen = this.IsFullScreen;
                             baseDeviceInfo.PresentationParameters.PresentationInterval = this.SynchronizeWithVerticalRetrace ? PresentInterval.One : PresentInterval.Immediate;
                             for (int i = 0; i < ValidAdapterFormats.Length; i++)
                             {
                                 this.AddDevices(adapter, type, adapter.CurrentDisplayMode, baseDeviceInfo, foundDevices);
                                 if (this.isFullScreen)
                                 {
                                     foreach (DisplayMode mode in adapter.SupportedDisplayModes[ValidAdapterFormats[i]])
                                     {
                                         if ((mode.Width >= 640) && (mode.Height >= 480))
                                         {
                                             this.AddDevices(adapter, type, mode, baseDeviceInfo, foundDevices);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 catch (DeviceNotSupportedException)
                 {
                 }
             }
         }
     }
 }
        private void ValidateGraphicsDeviceInformation(GraphicsDeviceInformation devInfo)
        {
            SurfaceFormat          format;
            GraphicsAdapter        adapter                = devInfo.Adapter;
            DeviceType             deviceType             = devInfo.DeviceType;
            bool                   enableAutoDepthStencil = devInfo.PresentationParameters.EnableAutoDepthStencil;
            DepthFormat            autoDepthStencilFormat = devInfo.PresentationParameters.AutoDepthStencilFormat;
            SurfaceFormat          backBufferFormat       = devInfo.PresentationParameters.BackBufferFormat;
            int                    backBufferWidth        = devInfo.PresentationParameters.BackBufferWidth;
            int                    backBufferHeight       = devInfo.PresentationParameters.BackBufferHeight;
            PresentationParameters presentationParameters = devInfo.PresentationParameters;
            SurfaceFormat          format4                = presentationParameters.BackBufferFormat;

            if (!presentationParameters.IsFullScreen)
            {
                format = adapter.CurrentDisplayMode.Format;
                if (SurfaceFormat.Unknown == presentationParameters.BackBufferFormat)
                {
                    format4 = format;
                }
            }
            else
            {
                SurfaceFormat format5 = presentationParameters.BackBufferFormat;
                if (format5 != SurfaceFormat.Color)
                {
                    if (format5 != SurfaceFormat.Bgra5551)
                    {
                        format = presentationParameters.BackBufferFormat;
                    }
                    else
                    {
                        format = SurfaceFormat.Bgr555;
                    }
                }
                else
                {
                    format = SurfaceFormat.Bgr32;
                }
            }
            if (-1 == Array.IndexOf <SurfaceFormat>(ValidBackBufferFormats, format4))
            {
                throw new ArgumentException("Backbuffer format is invalid");
            }
            if (!adapter.CheckDeviceType(deviceType, format, presentationParameters.BackBufferFormat, presentationParameters.IsFullScreen))
            {
                throw new ArgumentException("Device Type is invalid");
            }
            if ((presentationParameters.BackBufferCount < 0) || (presentationParameters.BackBufferCount > 3))
            {
                throw new ArgumentException("Backbuffer count is invalid");
            }
            if ((presentationParameters.BackBufferCount > 1) && (presentationParameters.SwapEffect == SwapEffect.Copy))
            {
                throw new ArgumentException("Backbuffer swap copies count is invalid");
            }
            switch (presentationParameters.SwapEffect)
            {
            case SwapEffect.Discard:
            case SwapEffect.Flip:
            case SwapEffect.Copy:
            {
                int num3;
                if (!adapter.CheckDeviceMultiSampleType(deviceType, format4, presentationParameters.IsFullScreen, presentationParameters.MultiSampleType, out num3))
                {
                    throw new ArgumentException("Multi sample is invalid");
                }
                if (presentationParameters.MultiSampleQuality >= num3)
                {
                    throw new ArgumentException("Multi sample quality is invalid");
                }
                if ((presentationParameters.MultiSampleType != MultiSampleType.None) && (presentationParameters.SwapEffect != SwapEffect.Discard))
                {
                    throw new ArgumentException("Multi sample swap is invalid");
                }
                if (((presentationParameters.PresentOptions & PresentOptions.DiscardDepthStencil) != PresentOptions.None) && !presentationParameters.EnableAutoDepthStencil)
                {
                    throw new ArgumentException("Auto depth is invalid");
                }
                if (presentationParameters.EnableAutoDepthStencil)
                {
                    if (!adapter.CheckDeviceFormat(deviceType, format, TextureUsage.None, QueryUsages.None, ResourceType.DepthStencilBuffer, presentationParameters.AutoDepthStencilFormat))
                    {
                        throw new ArgumentException("Auto depth format is invalid");
                    }
                    if (!adapter.CheckDepthStencilMatch(deviceType, format, format4, presentationParameters.AutoDepthStencilFormat))
                    {
                        throw new ArgumentException("Auto depth is not compatible");
                    }
                }
                if (!presentationParameters.IsFullScreen)
                {
                    if (presentationParameters.FullScreenRefreshRateInHz != 0)
                    {
                        throw new ArgumentException("Refresh rate is invalid");
                    }
                    switch (presentationParameters.PresentationInterval)
                    {
                    case PresentInterval.Default:
                    case PresentInterval.One:
                    case PresentInterval.Immediate:
                        return;
                    }
                    throw new ArgumentException("Refresh rate is invalid");
                }
                if (presentationParameters.FullScreenRefreshRateInHz == 0)
                {
                    throw new ArgumentException("Refresh rate is invalid");
                }
                GraphicsDeviceCapabilities capabilities = adapter.GetCapabilities(deviceType);
                switch (presentationParameters.PresentationInterval)
                {
                case PresentInterval.Default:
                case PresentInterval.One:
                case PresentInterval.Immediate:
                    goto Label_02E5;

                case PresentInterval.Two:
                case PresentInterval.Three:
                case PresentInterval.Four:
                    if ((capabilities.PresentInterval & presentationParameters.PresentationInterval) == PresentInterval.Default)
                    {
                        throw new ArgumentException("Refresh rate is invalid");
                    }
                    goto Label_02E5;
                }
                break;
            }

            default:
                throw new ArgumentException("Refresh rate is invalid");
            }
            throw new ArgumentException("Refresh rate is invalid");
Label_02E5:
            if (presentationParameters.IsFullScreen)
            {
                if ((presentationParameters.BackBufferWidth == 0) || (presentationParameters.BackBufferHeight == 0))
                {
                    throw new ArgumentException("Refresh rate is invalid");
                }
                bool        flag2 = true;
                bool        flag3 = false;
                DisplayMode currentDisplayMode = adapter.CurrentDisplayMode;
                if (((currentDisplayMode.Format != format) && (currentDisplayMode.Width != presentationParameters.BackBufferHeight)) && ((currentDisplayMode.Height != presentationParameters.BackBufferHeight) && (currentDisplayMode.RefreshRate != presentationParameters.FullScreenRefreshRateInHz)))
                {
                    flag2 = false;
                    foreach (DisplayMode mode2 in adapter.SupportedDisplayModes[format])
                    {
                        if ((mode2.Width == presentationParameters.BackBufferWidth) && (mode2.Height == presentationParameters.BackBufferHeight))
                        {
                            flag3 = true;
                            if (mode2.RefreshRate == presentationParameters.FullScreenRefreshRateInHz)
                            {
                                flag2 = true;
                                break;
                            }
                        }
                    }
                }
                if (!flag2 && flag3)
                {
                    throw new ArgumentException("Refresh rate is invalid");
                }
                if (!flag2)
                {
                    throw new ArgumentException("Refresh rate is invalid");
                }
            }
            if (presentationParameters.EnableAutoDepthStencil != enableAutoDepthStencil)
            {
                throw new ArgumentException("Refresh rate is invalid");
            }
            if (presentationParameters.EnableAutoDepthStencil)
            {
                if (presentationParameters.AutoDepthStencilFormat != autoDepthStencilFormat)
                {
                    throw new ArgumentException("Refresh rate is invalid");
                }
                if (presentationParameters.BackBufferFormat != backBufferFormat)
                {
                    throw new ArgumentException("Refresh rate is invalid");
                }
                if (presentationParameters.BackBufferWidth != backBufferWidth)
                {
                    throw new ArgumentException("Refresh rate is invalid");
                }
                if (presentationParameters.BackBufferHeight != backBufferHeight)
                {
                    throw new ArgumentException("Refresh rate is invalid");
                }
            }
        }
 private void CreateDevice(GraphicsDeviceInformation newInfo)
 {
     if (this.device != null)
     {
         this.device.Dispose();
         this.device = null;
     }
     this.OnPreparingDeviceSettings(this, new PreparingDeviceSettingsEventArgs(newInfo));
     this.MassagePresentParameters(newInfo.PresentationParameters);
     try
     {
         this.ValidateGraphicsDeviceInformation(newInfo);
         GraphicsDevice device = new GraphicsDevice(newInfo.Adapter, newInfo.DeviceType, new System.Windows.Interop.WindowInteropHelper(this.game.Window).Handle, newInfo.PresentationParameters);
         this.device = device;
         this.device.DeviceResetting += new EventHandler(this.HandleDeviceResetting);
         this.device.DeviceReset += new EventHandler(this.HandleDeviceReset);
         this.device.DeviceLost += new EventHandler(this.HandleDeviceLost);
         this.device.Disposing += new EventHandler(this.HandleDisposing);
     }
     catch (DeviceNotSupportedException exception)
     {
         throw this.CreateNoSuitableGraphicsDeviceException(Resources.Direct3DNotAvailable, exception);
     }
     catch (DriverInternalErrorException exception2)
     {
         throw this.CreateNoSuitableGraphicsDeviceException(Resources.Direct3DInternalDriverError, exception2);
     }
     catch (ArgumentException exception3)
     {
         throw this.CreateNoSuitableGraphicsDeviceException(Resources.Direct3DInvalidCreateParameters, exception3);
     }
     catch (Exception exception4)
     {
         throw this.CreateNoSuitableGraphicsDeviceException(Resources.Direct3DCreateError, exception4);
     }
     this.OnDeviceCreated(this, EventArgs.Empty);
 }
		public void ApplyChanges()
		{
			// Calling ApplyChanges() before CreateDevice() should have no effect.
			if (graphicsDevice == null)
			{
				return;
			}

			// Recreate device information before resetting
			GraphicsDeviceInformation gdi = new GraphicsDeviceInformation();
			gdi.Adapter = GraphicsDevice.Adapter;
			gdi.GraphicsProfile = GraphicsDevice.GraphicsProfile;
			gdi.PresentationParameters = GraphicsDevice.PresentationParameters.Clone();

			/* Apply the GraphicsDevice changes to the new Parameters.
			 * Note that PreparingDeviceSettings can override any of these!
			 * -flibit
			 */
			gdi.PresentationParameters.BackBufferFormat =
				PreferredBackBufferFormat;
			gdi.PresentationParameters.BackBufferWidth =
				PreferredBackBufferWidth;
			gdi.PresentationParameters.BackBufferHeight =
				PreferredBackBufferHeight;
			gdi.PresentationParameters.DepthStencilFormat =
				PreferredDepthStencilFormat;
			gdi.PresentationParameters.IsFullScreen =
				IsFullScreen;
			if (!PreferMultiSampling)
			{
				gdi.PresentationParameters.MultiSampleCount = 0;
			}
			else if (gdi.PresentationParameters.MultiSampleCount == 0)
			{
				/* XNA4 seems to have an upper limit of 8, but I'm willing to
				 * limit this only in GraphicsDeviceManager's default setting.
				 * If you want even higher values, Reset() with a custom value.
				 * -flibit
				 */
				gdi.PresentationParameters.MultiSampleCount = Math.Min(
					GraphicsDevice.GLDevice.MaxMultiSampleCount,
					8
				);
			}

			// Give the user a chance to override the above settings.
			OnPreparingDeviceSettings(
				this,
				new PreparingDeviceSettingsEventArgs(gdi)
			);

			// We're about to reset a device, notify the application.
			OnDeviceResetting(this, EventArgs.Empty);

			// Make the Platform device changes.
			game.Window.BeginScreenDeviceChange(
				gdi.PresentationParameters.IsFullScreen
			);
			game.Window.EndScreenDeviceChange(
				gdi.Adapter.Description, // FIXME: Should be Name! -flibit
				gdi.PresentationParameters.BackBufferWidth,
				gdi.PresentationParameters.BackBufferHeight
			);

			// Apply the PresentInterval.
			FNAPlatform.SetPresentationInterval(
				SynchronizeWithVerticalRetrace ?
					gdi.PresentationParameters.PresentationInterval :
					PresentInterval.Immediate
			);

			// Reset!
			GraphicsDevice.Reset(gdi.PresentationParameters, gdi.Adapter);

			// We just reset a device, notify the application.
			OnDeviceReset(this, EventArgs.Empty);
		}
 public PreparingDeviceSettingsEventArgs(GraphicsDeviceInformation information)
 {
     GraphicsDeviceInformation = information;
 }
 protected virtual bool CanResetDevice(GraphicsDeviceInformation newDeviceInfo)
 {
     return true;//TODO
 }