/// <summary> /// Creates a default SwapChain for the given target control. /// </summary> /// <param name="targetControl">Target control of the swap chain.</param> /// <param name="device">Graphics device.</param> /// <param name="gfxConfig">The current graphics configuration.</param> internal static IDXGISwapChain1 CreateSwapChainForWinForms(WinForms.Control targetControl, EngineDevice device, GraphicsViewConfiguration gfxConfig) { targetControl.EnsureNotNull(nameof(targetControl)); device.EnsureNotNull(nameof(device)); gfxConfig.EnsureNotNull(nameof(gfxConfig)); // Create the swap chain description var swapChainDesc = new SwapChainDescription1(); if (gfxConfig.AntialiasingEnabled && device.IsStandardAntialiasingPossible) { swapChainDesc.BufferCount = 2; swapChainDesc.SampleDescription = device.Internals.GetSampleDescription(gfxConfig.AntialiasingQuality); } else { swapChainDesc.BufferCount = 2; swapChainDesc.SampleDescription = new SampleDescription(1, 0); } // Set common parameters swapChainDesc.Width = targetControl.Width; swapChainDesc.Height = targetControl.Height; swapChainDesc.Format = GraphicsHelper.Internals.DEFAULT_TEXTURE_FORMAT; swapChainDesc.Scaling = Scaling.Stretch; swapChainDesc.SwapEffect = SwapEffect.Discard; swapChainDesc.Usage = Usage.RenderTargetOutput; // Create and return the swap chain and the render target return(device.Internals.FactoryDxgi.CreateSwapChainForHwnd( device.Internals.DeviceD3D11_1, targetControl.Handle, swapChainDesc)); }
/// <summary> /// Creates a depth buffer texture with given width and height. /// </summary> /// <param name="device">Graphics device.</param> /// <param name="width">Width of generated texture.</param> /// <param name="height">Height of generated texture.</param> /// <param name="gfxConfig">Current graphics configuration.</param> public static D3D11.ID3D11Texture2D CreateDepthBufferTexture(EngineDevice device, int width, int height, GraphicsViewConfiguration gfxConfig) { device.EnsureNotNull(nameof(device)); width.EnsurePositiveOrZero(nameof(width)); height.EnsurePositiveOrZero(nameof(height)); gfxConfig.EnsureNotNull(nameof(gfxConfig)); var textureDescription = new D3D11.Texture2DDescription(); if (gfxConfig.AntialiasingEnabled && device.IsStandardAntialiasingPossible) { textureDescription.Width = width; textureDescription.Height = height; textureDescription.MipLevels = 1; textureDescription.ArraySize = 1; textureDescription.Usage = D3D11.ResourceUsage.Default; textureDescription.SampleDescription = device.GetSampleDescription(gfxConfig.AntialiasingQuality); textureDescription.BindFlags = D3D11.BindFlags.DepthStencil; textureDescription.CpuAccessFlags = D3D11.CpuAccessFlags.None; textureDescription.OptionFlags = D3D11.ResourceOptionFlags.None; } else { textureDescription.Width = width; textureDescription.Height = height; textureDescription.MipLevels = 1; textureDescription.ArraySize = 1; textureDescription.Usage = D3D11.ResourceUsage.Default; textureDescription.SampleDescription = new SampleDescription(1, 0); textureDescription.BindFlags = D3D11.BindFlags.DepthStencil; textureDescription.CpuAccessFlags = D3D11.CpuAccessFlags.None; textureDescription.OptionFlags = D3D11.ResourceOptionFlags.None; } // Set buffer format textureDescription.Format = Format.D32_Float_S8X24_UInt; // Create the texture finally return(device.DeviceD3D11_1.CreateTexture2D(textureDescription)); }
/// <summary> /// Creates a render target texture with the given width and height. /// </summary> /// <param name="device">Graphics device.</param> /// <param name="width">Width of generated texture.</param> /// <param name="height">Height of generated texture.</param> /// <param name="gfxConfig">The GFX configuration.</param> public static D3D11.ID3D11Texture2D CreateRenderTargetTexture( EngineDevice device, int width, int height, GraphicsViewConfiguration gfxConfig) { device.EnsureNotNull(nameof(device)); width.EnsurePositiveOrZero(nameof(width)); height.EnsurePositiveOrZero(nameof(height)); gfxConfig.EnsureNotNull(nameof(gfxConfig)); var textureDescription = new D3D11.Texture2DDescription(); if (gfxConfig.AntialiasingEnabled && device.IsStandardAntialiasingPossible) { textureDescription.Width = width; textureDescription.Height = height; textureDescription.MipLevels = 1; textureDescription.ArraySize = 1; textureDescription.Format = DEFAULT_TEXTURE_FORMAT; textureDescription.Usage = D3D11.ResourceUsage.Default; textureDescription.SampleDescription = device.GetSampleDescription(gfxConfig.AntialiasingQuality); textureDescription.BindFlags = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget; textureDescription.CpuAccessFlags = D3D11.CpuAccessFlags.None; textureDescription.OptionFlags = D3D11.ResourceOptionFlags.None; } else { textureDescription.Width = width; textureDescription.Height = height; textureDescription.MipLevels = 1; textureDescription.ArraySize = 1; textureDescription.Format = DEFAULT_TEXTURE_FORMAT; textureDescription.Usage = D3D11.ResourceUsage.Default; textureDescription.SampleDescription = new SampleDescription(1, 0); textureDescription.BindFlags = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget; textureDescription.CpuAccessFlags = D3D11.CpuAccessFlags.None; textureDescription.OptionFlags = D3D11.ResourceOptionFlags.None; } return(device.DeviceD3D11_1.CreateTexture2D(textureDescription)); }
/// <summary> /// Creates the SwapChain object that is used on WinRT platforms. /// </summary> /// <param name="device">The device on which to create the SwapChain.</param> /// <param name="width">Width of the screen in pixels.</param> /// <param name="height">Height of the screen in pixels.</param> /// <param name="gfxConfig">Current graphics configuration.</param> internal static IDXGISwapChain1 CreateSwapChainForComposition(EngineDevice device, int width, int height, GraphicsViewConfiguration gfxConfig) { device.EnsureNotNull(nameof(device)); width.EnsurePositiveOrZero(nameof(width)); height.EnsurePositiveOrZero(nameof(height)); gfxConfig.EnsureNotNull(nameof(gfxConfig)); var desc = new SwapChainDescription1() { Width = width, Height = height, Format = GraphicsHelper.Internals.DEFAULT_TEXTURE_FORMAT, Stereo = false, SampleDescription = new SampleDescription(1, 0), Usage = Usage.Backbuffer | Usage.RenderTargetOutput, BufferCount = 2, Scaling = Scaling.Stretch, SwapEffect = SwapEffect.FlipSequential, AlphaMode = gfxConfig.AlphaEnabledSwapChain ? AlphaMode.Premultiplied : AlphaMode.Ignore }; return(device.Internals.FactoryDxgi.CreateSwapChainForComposition(device.Internals.DeviceD3D11_1, desc)); }