/// <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) { PresentInterval[] piArray = { PresentInterval.Immediate, PresentInterval.Default, PresentInterval.One, PresentInterval.Two, PresentInterval.Three, PresentInterval.Four, }; foreach (PresentInterval pi in piArray) { 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) != (PresentInterval)0) { deviceCombo.PresentIntervalList.Add(pi); } } }
/// <summary> /// Respond to a change of selected device by resetting the /// fullscreen/windowed radio buttons. Updating these buttons /// will trigger updates of the rest of the dialog. /// </summary> private void DeviceChanged(object sender, System.EventArgs e) { GraphicsAdapterInfo adapterInfo = (GraphicsAdapterInfo)adapterComboBox.SelectedItem; GraphicsDeviceInfo deviceInfo = (GraphicsDeviceInfo)deviceComboBox.SelectedItem; settings.DeviceInfo = deviceInfo; // Update fullscreen/windowed radio buttons bool HasWindowedDeviceCombo = false; bool HasFullscreenDeviceCombo = false; foreach (DeviceCombo deviceCombo in deviceInfo.DeviceComboList) { if (deviceCombo.IsWindowed) { HasWindowedDeviceCombo = true; } else { HasFullscreenDeviceCombo = true; } } windowedRadioButton.Enabled = HasWindowedDeviceCombo; fullscreenRadioButton.Enabled = HasFullscreenDeviceCombo; if (settings.IsWindowed && HasWindowedDeviceCombo) { windowedRadioButton.Checked = true; } else { fullscreenRadioButton.Checked = true; } WindowedFullscreenChanged(null, null); }
/// <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.SupportsHardwareTransformAndLight) { if (deviceInfo.Caps.DeviceCaps.SupportsPureDevice) { if (ConfirmDeviceCallback == null || ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.PureHardware, deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat)) { deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.PureHardware); } } if (ConfirmDeviceCallback == null || ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Hardware, deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat)) { deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.Hardware); } if (AppUsesMixedVP && (ConfirmDeviceCallback == null || ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Mixed, deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat))) { deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.Mixed); } } if (ConfirmDeviceCallback == null || ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Software, deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat)) { deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.Software); } }
/// <summary> /// Enumerates D3D devices for a particular adapter /// </summary> protected void EnumerateDevices(GraphicsAdapterInfo adapterInfo, ArrayList adapterFormatList) { DeviceType[] devTypeArray = new DeviceType[] { DeviceType.Hardware, DeviceType.Software, DeviceType.Reference }; foreach (DeviceType devType in devTypeArray) { GraphicsDeviceInfo deviceInfo = new GraphicsDeviceInfo(); deviceInfo.AdapterOrdinal = adapterInfo.AdapterOrdinal; deviceInfo.DevType = devType; try { deviceInfo.Caps = Manager.GetDeviceCaps(adapterInfo.AdapterOrdinal, devType); } catch (DirectXException) { 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.DeviceComboList.Count == 0) { continue; } adapterInfo.DeviceInfoList.Add(deviceInfo); } }
/// <summary> /// Respond to a change of selected adapter format by rebuilding the /// resolution list and back buffer format list. Updating the selected /// resolution and back buffer format will trigger updates of the rest /// of the dialog. /// </summary> private void AdapterFormatChanged(object sender, System.EventArgs e) { if (!windowedRadioButton.Checked) { GraphicsAdapterInfo adapterInfo = (GraphicsAdapterInfo)adapterComboBox.SelectedItem; Format adapterFormat = (Format)adapterFormatComboBox.SelectedItem; settings.FullscreenDisplayMode.Format = adapterFormat; System.Text.StringBuilder sb = new System.Text.StringBuilder(20); resolutionComboBox.Items.Clear(); foreach (DisplayMode displayMode in adapterInfo.DisplayModeList) { if (displayMode.Format == adapterFormat) { string resolutionString = FormatResolution(displayMode.Width, displayMode.Height); if (!resolutionComboBox.Items.Contains(resolutionString)) { resolutionComboBox.Items.Add(resolutionString); if (settings.FullscreenDisplayMode.Width == displayMode.Width && settings.FullscreenDisplayMode.Height == displayMode.Height) { resolutionComboBox.SelectedItem = resolutionString; } } } } if (resolutionComboBox.SelectedItem == null && resolutionComboBox.Items.Count > 0) { resolutionComboBox.SelectedIndex = 0; } } // Update backbuffer format combo box GraphicsDeviceInfo deviceInfo = (GraphicsDeviceInfo)deviceComboBox.SelectedItem; backBufferFormatComboBox.Items.Clear(); foreach (DeviceCombo deviceCombo in deviceInfo.DeviceComboList) { if (deviceCombo.IsWindowed == settings.IsWindowed && deviceCombo.AdapterFormat == settings.DisplayMode.Format) { if (!backBufferFormatComboBox.Items.Contains(deviceCombo.BackBufferFormat)) { backBufferFormatComboBox.Items.Add(deviceCombo.BackBufferFormat); if (deviceCombo.BackBufferFormat == settings.BackBufferFormat) { backBufferFormatComboBox.SelectedItem = deviceCombo.BackBufferFormat; } } } } if (backBufferFormatComboBox.SelectedItem == null && backBufferFormatComboBox.Items.Count > 0) { backBufferFormatComboBox.SelectedIndex = 0; } }
/// <summary> /// Enumerates DeviceCombos for a particular device /// </summary> protected void EnumerateDeviceCombos(GraphicsDeviceInfo deviceInfo, ArrayList adapterFormatList) { Format[] backBufferFormatArray = new Format[] { Format.A8R8G8B8, Format.X8R8G8B8, Format.A2R10G10B10, Format.R5G6B5, Format.A1R5G5B5, Format.X1R5G5B5, }; bool[] isWindowedArray = new bool[] { false, true }; // See which adapter formats are supported by this device foreach (Format adapterFormat in adapterFormatList) { foreach (Format backBufferFormat in backBufferFormatArray) { if (GraphicsUtility.GetAlphaChannelBits(backBufferFormat) < AppMinAlphaChannelBits) { continue; } foreach (bool isWindowed in isWindowedArray) { if (!isWindowed && AppRequiresWindowed) { continue; } if (isWindowed && AppRequiresFullscreen) { continue; } if (!Manager.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(); deviceCombo.AdapterOrdinal = deviceInfo.AdapterOrdinal; deviceCombo.DevType = deviceInfo.DevType; deviceCombo.AdapterFormat = adapterFormat; deviceCombo.BackBufferFormat = backBufferFormat; deviceCombo.IsWindowed = isWindowed; if (AppUsesDepthBuffer) { BuildDepthStencilFormatList(deviceCombo); if (deviceCombo.DepthStencilFormatList.Count == 0) { continue; } } BuildMultiSampleTypeList(deviceCombo); if (deviceCombo.MultiSampleTypeList.Count == 0) { continue; } BuildDepthStencilMultiSampleConflictList(deviceCombo); BuildVertexProcessingTypeList(deviceInfo, deviceCombo); if (deviceCombo.VertexProcessingTypeList.Count == 0) { continue; } BuildPresentIntervalList(deviceInfo, deviceCombo); if (deviceCombo.PresentIntervalList.Count == 0) { continue; } deviceInfo.DeviceComboList.Add(deviceCombo); } } } }
/// <summary> /// Respond to a change of selected back buffer format by rebuilding /// the depth/stencil format list, multisample type list, and vertex /// processing type list. /// </summary> private void BackBufferFormatChanged(object sender, System.EventArgs e) { GraphicsDeviceInfo deviceInfo = (GraphicsDeviceInfo)deviceComboBox.SelectedItem; Format adapterFormat = (Format)adapterFormatComboBox.SelectedItem; Format backBufferFormat = (Format)backBufferFormatComboBox.SelectedItem; foreach (DeviceCombo deviceCombo in deviceInfo.DeviceComboList) { if (deviceCombo.IsWindowed == settings.IsWindowed && deviceCombo.AdapterFormat == settings.DisplayMode.Format && deviceCombo.BackBufferFormat == settings.BackBufferFormat) { deviceCombo.BackBufferFormat = backBufferFormat; settings.DeviceCombo = deviceCombo; depthStencilBufferComboBox.Items.Clear(); if (enumeration.AppUsesDepthBuffer) { foreach (DepthFormat format in deviceCombo.DepthStencilFormatList) { depthStencilBufferComboBox.Items.Add(format); if (format == settings.DepthStencilBufferFormat) { depthStencilBufferComboBox.SelectedItem = format; } } if (depthStencilBufferComboBox.SelectedItem == null && depthStencilBufferComboBox.Items.Count > 0) { depthStencilBufferComboBox.SelectedIndex = 0; } } else { depthStencilBufferComboBox.Enabled = false; depthStencilBufferComboBox.Items.Add("(not used)"); depthStencilBufferComboBox.SelectedIndex = 0; } vertexProcComboBox.Items.Clear(); foreach (VertexProcessingType vpt in deviceCombo.VertexProcessingTypeList) { vertexProcComboBox.Items.Add(vpt); if (vpt == settings.VertexProcessingType) { vertexProcComboBox.SelectedItem = vpt; } } if (vertexProcComboBox.SelectedItem == null && vertexProcComboBox.Items.Count > 0) { vertexProcComboBox.SelectedIndex = 0; } presentIntervalComboBox.Items.Clear(); foreach (PresentInterval pi in deviceCombo.PresentIntervalList) { presentIntervalComboBox.Items.Add(pi); if (pi == settings.PresentInterval) { presentIntervalComboBox.SelectedItem = pi; } } if (presentIntervalComboBox.SelectedItem == null && presentIntervalComboBox.Items.Count > 0) { presentIntervalComboBox.SelectedIndex = 0; } break; } } }
/// <summary> /// Enumerates D3D devices for a particular adapter /// </summary> protected void EnumerateDevices(GraphicsAdapterInfo adapterInfo, ArrayList adapterFormatList) { DeviceType[] devTypeArray = new DeviceType[] { DeviceType.Hardware, DeviceType.Software, DeviceType.Reference }; foreach (DeviceType devType in devTypeArray) { GraphicsDeviceInfo deviceInfo = new GraphicsDeviceInfo(); deviceInfo.AdapterOrdinal = adapterInfo.AdapterOrdinal; deviceInfo.DevType = devType; try { deviceInfo.Caps = Manager.GetDeviceCaps(adapterInfo.AdapterOrdinal, devType); } catch (DirectXException) { 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.DeviceComboList.Count == 0) continue; adapterInfo.DeviceInfoList.Add(deviceInfo); } }
/// <summary> /// Respond to a change of windowed/fullscreen state by rebuilding the /// adapter format list, resolution list, and refresh rate list. /// Updating the selected adapter format will trigger updates of the /// rest of the dialog. /// </summary> private void WindowedFullscreenChanged(object sender, System.EventArgs e) { GraphicsAdapterInfo adapterInfo = (GraphicsAdapterInfo)adapterComboBox.SelectedItem; GraphicsDeviceInfo deviceInfo = (GraphicsDeviceInfo)deviceComboBox.SelectedItem; if (windowedRadioButton.Checked) { settings.IsWindowed = true; settings.WindowedAdapterInfo = adapterInfo; settings.WindowedDeviceInfo = deviceInfo; // Update adapter format combo box adapterFormatComboBox.Items.Clear(); adapterFormatComboBox.Items.Add(settings.WindowedDisplayMode.Format); adapterFormatComboBox.SelectedIndex = 0; adapterFormatComboBox.Enabled = false; // Update resolution combo box resolutionComboBox.Items.Clear(); resolutionComboBox.Items.Add(FormatResolution(settings.WindowedDisplayMode.Width, settings.WindowedDisplayMode.Height)); resolutionComboBox.SelectedIndex = 0; resolutionComboBox.Enabled = false; // Update refresh rate combo box refreshRateComboBox.Items.Clear(); refreshRateComboBox.Items.Add(FormatRefreshRate(settings.WindowedDisplayMode.RefreshRate)); refreshRateComboBox.SelectedIndex = 0; refreshRateComboBox.Enabled = false; } else { settings.IsWindowed = false; settings.FullscreenAdapterInfo = adapterInfo; settings.FullscreenDeviceInfo = deviceInfo; // Update adapter format combo box adapterFormatComboBox.Items.Clear(); foreach (DeviceCombo deviceCombo in deviceInfo.DeviceComboList) { if (!adapterFormatComboBox.Items.Contains(deviceCombo.AdapterFormat)) { adapterFormatComboBox.Items.Add(deviceCombo.AdapterFormat); if (deviceCombo.AdapterFormat == (settings.IsWindowed ? settings.WindowedDisplayMode.Format : settings.FullscreenDisplayMode.Format)) { adapterFormatComboBox.SelectedItem = deviceCombo.AdapterFormat; } } } if (adapterFormatComboBox.SelectedItem == null && adapterFormatComboBox.Items.Count > 0) { adapterFormatComboBox.SelectedIndex = 0; } adapterFormatComboBox.Enabled = true; // Update resolution combo box resolutionComboBox.Enabled = true; // Update refresh rate combo box refreshRateComboBox.Enabled = true; } }
/// <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.SupportsHardwareTransformAndLight) { if (deviceInfo.Caps.DeviceCaps.SupportsPureDevice) { if (ConfirmDeviceCallback == null || ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.PureHardware, deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat)) { deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.PureHardware); } } if (ConfirmDeviceCallback == null || ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Hardware, deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat)) { deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.Hardware); } if (AppUsesMixedVP && (ConfirmDeviceCallback == null || ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Mixed, deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat))) { deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.Mixed); } } if (ConfirmDeviceCallback == null || ConfirmDeviceCallback(deviceInfo.Caps, VertexProcessingType.Software, deviceCombo.AdapterFormat, deviceCombo.BackBufferFormat)) { deviceCombo.VertexProcessingTypeList.Add(VertexProcessingType.Software); } }
/// <summary> /// Enumerates DeviceCombos for a particular device /// </summary> protected void EnumerateDeviceCombos(GraphicsDeviceInfo deviceInfo, ArrayList adapterFormatList) { Format[] backBufferFormatArray = new Format[] { Format.A8R8G8B8, Format.X8R8G8B8, Format.A2R10G10B10, Format.R5G6B5, Format.A1R5G5B5, Format.X1R5G5B5, }; bool[] isWindowedArray = new bool[] { false, true }; // See which adapter formats are supported by this device foreach (Format adapterFormat in adapterFormatList) { foreach (Format backBufferFormat in backBufferFormatArray) { if (GraphicsUtility.GetAlphaChannelBits(backBufferFormat) < AppMinAlphaChannelBits) continue; foreach (bool isWindowed in isWindowedArray) { if (!isWindowed && AppRequiresWindowed) continue; if (isWindowed && AppRequiresFullscreen) continue; if (!Manager.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(); deviceCombo.AdapterOrdinal = deviceInfo.AdapterOrdinal; deviceCombo.DevType = deviceInfo.DevType; deviceCombo.AdapterFormat = adapterFormat; deviceCombo.BackBufferFormat = backBufferFormat; deviceCombo.IsWindowed = isWindowed; if (AppUsesDepthBuffer) { BuildDepthStencilFormatList(deviceCombo); if (deviceCombo.DepthStencilFormatList.Count == 0) continue; } BuildMultiSampleTypeList(deviceCombo); if (deviceCombo.MultiSampleTypeList.Count == 0) continue; BuildDepthStencilMultiSampleConflictList(deviceCombo); BuildVertexProcessingTypeList(deviceInfo, deviceCombo); if (deviceCombo.VertexProcessingTypeList.Count == 0) continue; BuildPresentIntervalList(deviceInfo, deviceCombo); if (deviceCombo.PresentIntervalList.Count == 0) continue; deviceInfo.DeviceComboList.Add(deviceCombo); } } } }
/// <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) { PresentInterval[] piArray = { PresentInterval.Immediate, PresentInterval.Default, PresentInterval.One, PresentInterval.Two, PresentInterval.Three, PresentInterval.Four, }; foreach (PresentInterval pi in piArray) { 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) != (PresentInterval)0) { deviceCombo.PresentIntervalList.Add(pi); } } }
public void InitializeEnvironment() { GraphicsAdapterInfo adapterInfo = graphicsSettings.AdapterInfo; GraphicsDeviceInfo deviceInfo = graphicsSettings.DeviceInfo; windowed = graphicsSettings.IsWindowed; // Set up the presentation parameters BuildPresentParamsFromSettings(); //if (deviceInfo.Caps.PrimitiveMiscCaps.IsNullReference) //{ // // Warn user about null ref device that can't render anything // HandleSampleException(new NullReferenceDeviceException(), ApplicationMessage.None); //} CreateFlags createFlags = new CreateFlags(); switch (graphicsSettings.VertexProcessingType) { case VertexProcessingType.Software: { createFlags = CreateFlags.SoftwareVertexProcessing; break; } case VertexProcessingType.Mixed: { createFlags = CreateFlags.MixedVertexProcessing; break; } case VertexProcessingType.Hardware: { createFlags = CreateFlags.HardwareVertexProcessing; break; } case VertexProcessingType.PureHardware: { createFlags = CreateFlags.HardwareVertexProcessing | CreateFlags.PureDevice; break; } default: throw new ApplicationException(); } createFlags |= CreateFlags.MultiThreaded; try { // Create the device device = new Device(graphicsSettings.AdapterOrdinal, graphicsSettings.DevType, presentParams.DeviceWindow, createFlags, presentParams); // Cache our local objects //renderState = device.RenderState; //sampleState = device.SamplerState; //textureStates = device.TextureState; // 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. if (windowed) { // Make sure main window isn't topmost, so error message is visible System.Drawing.Size currentClientSize = presentParams.DeviceWindow.ClientSize; presentParams.DeviceWindow.Size = presentParams.DeviceWindow.ClientSize; presentParams.DeviceWindow.ClientSize = currentClientSize; presentParams.DeviceWindow.SendToBack(); presentParams.DeviceWindow.BringToFront(); } device.Transform.World = Matrix.Identity; // Store device Caps graphicsCaps = device.DeviceCaps; behavior = createFlags; // Store device description if (deviceInfo.DevType == DeviceType.Reference) { deviceStats = "REF"; } else if (deviceInfo.DevType == DeviceType.Hardware) { deviceStats = "HAL"; } else if (deviceInfo.DevType == DeviceType.Software) { deviceStats = "SW"; } BehaviorFlags behaviorFlags = new BehaviorFlags(createFlags); if ((behaviorFlags.HardwareVertexProcessing) && (behaviorFlags.PureDevice)) { if (deviceInfo.DevType == DeviceType.Hardware) { deviceStats += " (pure hw vp)"; } else { deviceStats += " (simulated pure hw vp)"; } } else if ((behaviorFlags.HardwareVertexProcessing)) { if (deviceInfo.DevType == DeviceType.Hardware) { deviceStats += " (hw vp)"; } else { deviceStats += " (simulated hw vp)"; } } else if (behaviorFlags.MixedVertexProcessing) { if (deviceInfo.DevType == DeviceType.Hardware) { deviceStats += " (mixed vp)"; } else { deviceStats += " (simulated mixed vp)"; } } else if (behaviorFlags.SoftwareVertexProcessing) { deviceStats += " (sw vp)"; } if (deviceInfo.DevType == DeviceType.Hardware) { deviceStats += ": "; deviceStats += adapterInfo.AdapterDetails.Description; } // Set up the fullscreen cursor if (showCursorWhenFullscreen && !windowed) { System.Windows.Forms.Cursor ourCursor = presentParams.DeviceWindow.Cursor; device.SetCursor(ourCursor, true); device.ShowCursor(true); } // Confine cursor to fullscreen window if (clipCursorWhenFullscreen) { if (!windowed) { System.Drawing.Rectangle rcWindow = presentParams.DeviceWindow.ClientRectangle; } } // Setup the event handlers for our device device.DeviceLost += new System.EventHandler(this.OnInvalidateDeviceObjects); device.DeviceReset += new System.EventHandler(this.OnRestoreDeviceObjects); device.Disposing += new System.EventHandler(this.OnDeleteDeviceObjects); device.DeviceResizing += new System.ComponentModel.CancelEventHandler(this.EnvironmentResized); // Initialize the app's device-dependent objects try { if (InitializeDeviceObjects != null) { InitializeDeviceObjects(); } OnRestoreDeviceObjects(null, null); active = true; return; } catch { // Cleanup before we try again OnInvalidateDeviceObjects(null, null); OnDeleteDeviceObjects(null, null); device.Dispose(); device = null; if (presentParams.DeviceWindow.Disposing) { return; } } } catch { // If that failed, fall back to the reference rasterizer if (deviceInfo.DevType == DeviceType.Hardware) { if (FindBestWindowedMode(false, true)) { windowed = true; // Make sure main window isn't topmost, so error message is visible System.Drawing.Size currentClientSize = presentParams.DeviceWindow.ClientSize; presentParams.DeviceWindow.Size = presentParams.DeviceWindow.ClientSize; presentParams.DeviceWindow.ClientSize = currentClientSize; presentParams.DeviceWindow.SendToBack(); presentParams.DeviceWindow.BringToFront(); //// Let the user know we are switching from HAL to the reference rasterizer //HandleSampleException(null, ApplicationMessage.WarnSwitchToRef); InitializeEnvironment(); } } } }
public bool FindBestWindowedMode(bool doesRequireHardware, bool doesRequireReference) { // Get display mode of primary adapter (which is assumed to be where the window will appear) DisplayMode primaryDesktopDisplayMode = Manager.Adapters[0].CurrentDisplayMode; GraphicsAdapterInfo bestAdapterInfo = null; GraphicsDeviceInfo bestDeviceInfo = null; DeviceCombo bestDeviceCombo = null; foreach (GraphicsAdapterInfo adapterInfo in enumerationSettings.AdapterInfoList) { foreach (GraphicsDeviceInfo deviceInfo in adapterInfo.DeviceInfoList) { if (doesRequireHardware && deviceInfo.DevType != DeviceType.Hardware) { continue; } if (doesRequireReference && deviceInfo.DevType != DeviceType.Reference) { continue; } foreach (DeviceCombo deviceCombo in deviceInfo.DeviceComboList) { bool adapterMatchesBackBuffer = (deviceCombo.BackBufferFormat == deviceCombo.AdapterFormat); if (!deviceCombo.IsWindowed) { continue; } if (deviceCombo.AdapterFormat != primaryDesktopDisplayMode.Format) { continue; } // 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 } } } } EndWindowedDeviceComboSearch: if (bestDeviceCombo == null) { return(false); } graphicsSettings.WindowedAdapterInfo = bestAdapterInfo; graphicsSettings.WindowedDeviceInfo = bestDeviceInfo; graphicsSettings.WindowedDeviceCombo = bestDeviceCombo; graphicsSettings.IsWindowed = true; graphicsSettings.WindowedDisplayMode = primaryDesktopDisplayMode; graphicsSettings.WindowedWidth = ourRenderTarget.ClientRectangle.Right - ourRenderTarget.ClientRectangle.Left; graphicsSettings.WindowedHeight = ourRenderTarget.ClientRectangle.Bottom - ourRenderTarget.ClientRectangle.Top; if (enumerationSettings.AppUsesDepthBuffer) { graphicsSettings.WindowedDepthStencilBufferFormat = (DepthFormat)bestDeviceCombo.DepthStencilFormatList[0]; } graphicsSettings.WindowedMultisampleType = (MultiSampleType)bestDeviceCombo.MultiSampleTypeList[0]; graphicsSettings.WindowedMultisampleQuality = 0; graphicsSettings.WindowedVertexProcessingType = (VertexProcessingType)bestDeviceCombo.VertexProcessingTypeList[0]; graphicsSettings.WindowedPresentInterval = (PresentInterval)bestDeviceCombo.PresentIntervalList[0]; return(true); }
public bool FindBestFullscreenMode(bool doesRequireHardware, bool doesRequireReference) { DisplayMode adapterDesktopDisplayMode = new DisplayMode(); DisplayMode bestAdapterDesktopDisplayMode = new DisplayMode(); DisplayMode bestDisplayMode = new DisplayMode(); bestAdapterDesktopDisplayMode.Width = 0; bestAdapterDesktopDisplayMode.Height = 0; bestAdapterDesktopDisplayMode.Format = 0; bestAdapterDesktopDisplayMode.RefreshRate = 0; GraphicsAdapterInfo bestAdapterInfo = null; GraphicsDeviceInfo bestDeviceInfo = null; DeviceCombo bestDeviceCombo = null; foreach (GraphicsAdapterInfo adapterInfo in enumerationSettings.AdapterInfoList) { adapterDesktopDisplayMode = Manager.Adapters[adapterInfo.AdapterOrdinal].CurrentDisplayMode; foreach (GraphicsDeviceInfo deviceInfo in adapterInfo.DeviceInfoList) { if (doesRequireHardware && deviceInfo.DevType != DeviceType.Hardware) { continue; } if (doesRequireReference && deviceInfo.DevType != DeviceType.Reference) { continue; } foreach (DeviceCombo deviceCombo in deviceInfo.DeviceComboList) { bool adapterMatchesBackBuffer = (deviceCombo.BackBufferFormat == deviceCombo.AdapterFormat); bool adapterMatchesDesktop = (deviceCombo.AdapterFormat == adapterDesktopDisplayMode.Format); if (deviceCombo.IsWindowed) { continue; } // 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 } } } } 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 bestDisplayMode.Width = 0; bestDisplayMode.Height = 0; bestDisplayMode.Format = 0; bestDisplayMode.RefreshRate = 0; foreach (DisplayMode displayMode in bestAdapterInfo.DisplayModeList) { if (displayMode.Format != bestDeviceCombo.AdapterFormat) { continue; } if (displayMode.Width == bestAdapterDesktopDisplayMode.Width && displayMode.Height == bestAdapterDesktopDisplayMode.Height && displayMode.RefreshRate == bestAdapterDesktopDisplayMode.RefreshRate) { // found a perfect match, so stop bestDisplayMode = displayMode; break; } else if (displayMode.Width == bestAdapterDesktopDisplayMode.Width && displayMode.Height == bestAdapterDesktopDisplayMode.Height && displayMode.RefreshRate > bestDisplayMode.RefreshRate) { // refresh rate doesn't match, but width/height match, so keep this // and keep looking bestDisplayMode = displayMode; } else if (bestDisplayMode.Width == bestAdapterDesktopDisplayMode.Width) { // width matches, so keep this and keep looking bestDisplayMode = displayMode; } else if (bestDisplayMode.Width == 0) { // we don't have anything better yet, so keep this and keep looking bestDisplayMode = displayMode; } } graphicsSettings.FullscreenAdapterInfo = bestAdapterInfo; graphicsSettings.FullscreenDeviceInfo = bestDeviceInfo; graphicsSettings.FullscreenDeviceCombo = bestDeviceCombo; graphicsSettings.IsWindowed = false; graphicsSettings.FullscreenDisplayMode = bestDisplayMode; if (enumerationSettings.AppUsesDepthBuffer) { graphicsSettings.FullscreenDepthStencilBufferFormat = (DepthFormat)bestDeviceCombo.DepthStencilFormatList[0]; } graphicsSettings.FullscreenMultisampleType = (MultiSampleType)bestDeviceCombo.MultiSampleTypeList[0]; graphicsSettings.FullscreenMultisampleQuality = 0; graphicsSettings.FullscreenVertexProcessingType = (VertexProcessingType)bestDeviceCombo.VertexProcessingTypeList[0]; graphicsSettings.FullscreenPresentInterval = (PresentInterval)bestDeviceCombo.PresentIntervalList[0]; return(true); }
/// Initializes the DirectX environment. private void InitializeEnvironment() { GraphicsDeviceInfo deviceInfo = this.settings.DeviceInfo; this.windowed = this.settings.IsWindowed; // Set up the presentation parameters BuildPresentParametersFromSettings(); if (deviceInfo.Caps.PrimitiveMiscCapabilities.IsNullReference) { // Warn user about null ref device that can't render anything //TODO: implement } CreateFlags createFlags = new CreateFlags(); if (this.settings.VertexProcessingType == VertexProcessingType.Software) { createFlags = CreateFlags.SoftwareVertexProcessing; } else if (this.settings.VertexProcessingType == VertexProcessingType.Mixed) { createFlags = CreateFlags.MixedVertexProcessing; } else if (this.settings.VertexProcessingType == VertexProcessingType.Hardware) { createFlags = CreateFlags.HardwareVertexProcessing; } else if (this.settings.VertexProcessingType == VertexProcessingType.PureHardware) { createFlags = CreateFlags.HardwareVertexProcessing | CreateFlags.PureDevice; } try { // Create the device this.localDevice = new D3D.Device(this.settings.AdapterOrdinal, this.settings.DevType, this.owner.Handle, createFlags, this.parameters); // Cache our local objects //this.renderState = this.localDevice.RenderState; //this.sampleState = this.localDevice.SamplerState; //this.textureStates = this.localDevice.TextureState; // 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. if (this.windowed) { // Make sure main window isn't topmost, so error message is visible System.Drawing.Size currentClientSize = this.owner.ClientSize; this.owner.Size = this.owner.ClientSize; this.owner.ClientSize = currentClientSize; this.owner.SendToBack(); this.owner.BringToFront(); } BehaviorFlags behaviorFlags = new BehaviorFlags(createFlags); this.localDevice.DeviceReset += new System.EventHandler(this.RestoreDeviceObjects); // Initialize the app's device-dependent objects try { //InitializeDeviceObjects(); RestoreDeviceObjects(null, null); this.active = true; return; } catch { this.localDevice.Dispose(); this.localDevice = null; return; } } catch { // If that failed, fall back to the reference rasterizer if (deviceInfo.DevType == D3D.DeviceType.Hardware) { if (FindBestWindowedMode(false, true)) { this.windowed = true; // Make sure main window isn't topmost, so error message is visible System.Drawing.Size currentClientSize = this.owner.ClientSize; this.owner.Size = this.owner.ClientSize; this.owner.ClientSize = currentClientSize; this.owner.SendToBack(); this.owner.BringToFront(); // Let the user know we are switching from HAL to the reference rasterizer //HandleSampleException( null, ApplicationMessage.WarnSwitchToRef); InitializeEnvironment(); } } } }