// ReSharper restore UnusedMember.Local private DepthFormat ChooseDepthStencilFormat(GraphicsAdapter adapter, DeviceType deviceType, SurfaceFormat adapterFormat) { if (adapter.CheckDeviceFormat( deviceType, adapterFormat, TextureUsage.None, QueryUsages.None, ResourceType.DepthStencilBuffer, PreferredDepthStencilFormat)) { return(PreferredDepthStencilFormat); } DepthFormat depthFormat; if (Array.IndexOf(_depthFormatsWithStencil, PreferredDepthStencilFormat) >= 0) { depthFormat = ChooseDepthStencilFormatFromList(_depthFormatsWithStencil, adapter, deviceType, adapterFormat); if (depthFormat != DepthFormat.Unknown) { return(depthFormat); } } depthFormat = ChooseDepthStencilFormatFromList(_depthFormatsWithoutStencil, adapter, deviceType, adapterFormat); if (depthFormat != DepthFormat.Unknown) { return(depthFormat); } return(DepthFormat.Depth24); }
static SurfaceFormat GetSupportedTextureFormat(Type type) { SurfaceFormat surface = SurfaceFormat.Unknown; if (textureFormatDict.TryGetValue(type, out surface)) { return(surface); } //find a supported format to use. GraphicsAdapter adapter = GraphicsAdapter.DefaultAdapter; SurfaceFormat[] formats = (SurfaceFormat[])Enum.GetValues(typeof(SurfaceFormat)); ResourceType resType = ResourceType.Texture2D; if (type == typeof(Texture3D)) { resType = ResourceType.Texture3D; } if (type == typeof(TextureCube)) { resType = ResourceType.TextureCube; } surface = SurfaceFormat.Color; foreach (SurfaceFormat format in formats) { if (format != SurfaceFormat.Unknown) { //find a format that works for PS and VS if (adapter.CheckDeviceFormat(deviceType, SurfaceFormat.Bgr32, TextureUsage.None, QueryUsages.None, resType, format) && adapter.CheckDeviceFormat(deviceType, SurfaceFormat.Bgr32, TextureUsage.None, QueryUsages.VertexTexture, resType, format)) { surface = format; break; } } } //ohh well. hope nothing breaks. textureFormatDict.Add(type, surface); return(surface); }
private DepthFormat ChooseDepthStencilFormatFromList(DepthFormat[] availableFormats, GraphicsAdapter adapter, DeviceType deviceType, SurfaceFormat adapterFormat) { for (int i = 0; i < availableFormats.Length; i++) { if ((availableFormats[i] != this.PreferredDepthStencilFormat) && adapter.CheckDeviceFormat(deviceType, adapterFormat, TextureUsage.None, QueryUsages.None, ResourceType.DepthStencilBuffer, availableFormats[i])) { return(availableFormats[i]); } } return(DepthFormat.Unknown); }
private void RankDevicesPlatform(List <GraphicsDeviceInformation> foundDevices) { int index = 0; while (index < foundDevices.Count) { DeviceType deviceType = foundDevices[index].DeviceType; GraphicsAdapter adapter = foundDevices[index].Adapter; PresentationParameters presentationParameters = foundDevices[index].PresentationParameters; if (!adapter.CheckDeviceFormat(deviceType, adapter.CurrentDisplayMode.Format, TextureUsage.None, QueryUsages.PostPixelShaderBlending, ResourceType.Texture2D, presentationParameters.BackBufferFormat)) { foundDevices.RemoveAt(index); } else { index++; } } foundDevices.Sort(new GraphicsDeviceInformationComparer(this)); }
private DepthFormat ChooseDepthStencilFormat(GraphicsAdapter adapter, DeviceType deviceType, SurfaceFormat adapterFormat) { if (adapter.CheckDeviceFormat(deviceType, adapterFormat, TextureUsage.None, QueryUsages.None, ResourceType.DepthStencilBuffer, this.PreferredDepthStencilFormat)) { return(this.PreferredDepthStencilFormat); } if (Array.IndexOf <DepthFormat>(depthFormatsWithStencil, this.PreferredDepthStencilFormat) >= 0) { DepthFormat format = this.ChooseDepthStencilFormatFromList(depthFormatsWithStencil, adapter, deviceType, adapterFormat); if (format != DepthFormat.Unknown) { return(format); } } DepthFormat format2 = this.ChooseDepthStencilFormatFromList(depthFormatsWithoutStencil, adapter, deviceType, adapterFormat); if (format2 != DepthFormat.Unknown) { return(format2); } return(DepthFormat.Depth24); }
private DepthFormat ChooseDepthStencilFormatFromList( DepthFormat[] availableFormats, GraphicsAdapter adapter, DeviceType deviceType, SurfaceFormat adapterFormat) { foreach (var depthFormat in availableFormats) { if (depthFormat != PreferredDepthStencilFormat && adapter.CheckDeviceFormat( deviceType, adapterFormat, TextureUsage.None, QueryUsages.None, ResourceType.DepthStencilBuffer, depthFormat)) { return(depthFormat); } } return(DepthFormat.Unknown); }
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.Default: 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.Default)) { 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, ResourceUsage.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); } } }
void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e) { int quality = 0; GraphicsAdapter adapter = e.GraphicsDeviceInformation.Adapter; SurfaceFormat format = adapter.CurrentDisplayMode.Format; DisplayMode currentmode = adapter.CurrentDisplayMode; PresentationParameters pp = e.GraphicsDeviceInformation.PresentationParameters; #if XBOX pp.MultiSampleQuality = 0; pp.MultiSampleType = MultiSampleType.FourSamples; pp.BackBufferWidth = 1280; pp.BackBufferHeight = 720; pp.BackBufferFormat = SurfaceFormat.Bgr32; pp.AutoDepthStencilFormat = DepthFormat.Depth24Stencil8Single; pp.EnableAutoDepthStencil = true; return; #endif // Set a window size compatible with the current screen if (currentmode.Width < 800) { pp.BackBufferWidth = 640; pp.BackBufferHeight = 480; } else if (currentmode.Width < 1024) { pp.BackBufferWidth = 800; pp.BackBufferHeight = 600; } else if (currentmode.Width < 1280) { pp.BackBufferWidth = 1024; pp.BackBufferHeight = 768; } else // Xbox, or a PC with a big screen { pp.BackBufferWidth = 1280; pp.BackBufferHeight = 720; } // Xbox 360 and most PCs support FourSamples/0 (4x) // and TwoSamples/0 (2x) antialiasing. // Check for 4xAA if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format, false, MultiSampleType.FourSamples, out quality)) { // even if a greater quality is returned, we only want quality 0 pp.MultiSampleQuality = 0; pp.MultiSampleType = MultiSampleType.FourSamples; } // Check for 2xAA else if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format, false, MultiSampleType.TwoSamples, out quality)) { // even if a greater quality is returned, we only want quality 0 pp.MultiSampleQuality = 0; pp.MultiSampleType = MultiSampleType.TwoSamples; } // Does this video card support Depth24Stencil8Single // for the back buffer? if (adapter.CheckDeviceFormat(DeviceType.Hardware, adapter.CurrentDisplayMode.Format, TextureUsage.None, QueryUsages.None, ResourceType.RenderTarget, DepthFormat.Depth24Stencil8Single)) { // if so, let's use that pp.AutoDepthStencilFormat = DepthFormat.Depth24Stencil8Single; pp.EnableAutoDepthStencil = true; } return; }
/// <summary> /// Creates render targets and DS buffers needed for rendering /// </summary> private void CreateRenderTargets() { Vector2 gBufferMultiplier = GBufferSizes[(int)gBufferSize]; int gBufferWidth = (int)(gBufferMultiplier.X * ScreenWidth); int gBufferHeight = (int)(gBufferMultiplier.Y * ScreenHeight); gBufferDimensions = new Vector2(gBufferWidth, gBufferHeight); if (depthIDBuffer != null) { depthIDBuffer.Dispose(); } depthIDBuffer = new RenderTarget2D(GraphicsDevice, gBufferWidth, gBufferHeight, 1, SurfaceFormat.HalfVector2, RenderTargetUsage.PreserveContents); // We'll prefer Rgba1010102 for normals + specular exponent, since we only need 3 components // and it's nice to have the extra precision for normals. If you needed another component, // you could use Color and only use 8-bits for the normals. Or if you wanted to check to see if // the GPU supports MRT with independed bit-depths, you could go with HalfVector4 and get // even more precision. SurfaceFormat normalSpecularFormat = SurfaceFormat.Color; GraphicsAdapter adapter = GraphicsAdapter.DefaultAdapter; SurfaceFormat adapterFormat = adapter.CurrentDisplayMode.Format; if (adapter.CheckDeviceFormat(DeviceType.Hardware, adapterFormat, TextureUsage.None, QueryUsages.None, ResourceType.RenderTarget, SurfaceFormat.Rgba1010102)) { normalSpecularFormat = SurfaceFormat.Rgba1010102; } if (normalSpecularBuffer != null) { normalSpecularBuffer.Dispose(); } normalSpecularBuffer = new RenderTarget2D(GraphicsDevice, gBufferWidth, gBufferHeight, 1, normalSpecularFormat, RenderTargetUsage.PreserveContents); if (lightBuffer != null) { lightBuffer.Dispose(); } lightBuffer = new RenderTarget2D(GraphicsDevice, gBufferWidth, gBufferHeight, 1, SurfaceFormat.HalfVector4, RenderTargetUsage.PreserveContents); if (colorBuffer != null) { colorBuffer.Dispose(); } if (enableMSAA) { colorBuffer = new RenderTarget2D(GraphicsDevice, ScreenWidth, ScreenHeight, 1, SurfaceFormat.Color, MultiSampleType.FourSamples, 0, RenderTargetUsage.PreserveContents); } else { colorBuffer = new RenderTarget2D(GraphicsDevice, ScreenWidth, ScreenHeight, 1, SurfaceFormat.Color, RenderTargetUsage.PreserveContents); } nonMSAADS = GraphicsDevice.DepthStencilBuffer; if (msaaDS != null && msaaDS != nonMSAADS) { msaaDS.Dispose(); } if (enableMSAA) { msaaDS = new DepthStencilBuffer(GraphicsDevice, ScreenWidth, ScreenHeight, DepthFormat.Depth24Stencil8, MultiSampleType.FourSamples, 0); } else { msaaDS = nonMSAADS; } if (shadowMap != null) { shadowMap.Dispose(); } shadowMap = new RenderTarget2D(GraphicsDevice, (int)shadowMapSize, (int)shadowMapSize, 1, SurfaceFormat.Single, RenderTargetUsage.PreserveContents); if (shadowMapDS != null) { shadowMapDS.Dispose(); } shadowMapDS = new DepthStencilBuffer(GraphicsDevice, (int)shadowMapSize, (int)shadowMapSize, DepthFormat.Depth24Stencil8); }