/// <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> public D3D11.Texture2D CreateRenderTargetTexture(D3D11.Device device, int width, int height) { var textureDescription = new D3D11.Texture2DDescription(); if (TryEnableAntialiasing) { var maxCount = 1; for (var i = 0; i < 32; i++) { if (device.CheckMultisampleQualityLevels(DXGI.Format.B8G8R8A8_UNorm, i) > 0) maxCount = i; } textureDescription.Width = width; textureDescription.Height = height; textureDescription.MipLevels = 1; textureDescription.ArraySize = 1; textureDescription.Format = DXGI.Format.B8G8R8A8_UNorm; textureDescription.Usage = D3D11.ResourceUsage.Default; textureDescription.SampleDescription = new DXGI.SampleDescription(maxCount, 0); 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 = DXGI.Format.B8G8R8A8_UNorm; textureDescription.Usage = D3D11.ResourceUsage.Default; textureDescription.SampleDescription = new DXGI.SampleDescription(1, 0); textureDescription.BindFlags = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget; textureDescription.CpuAccessFlags = D3D11.CpuAccessFlags.None; textureDescription.OptionFlags = D3D11.ResourceOptionFlags.None; } return new D3D11.Texture2D(device, textureDescription); }
public void SetBackBufferDX11(D3D11.Texture2D texture) { if (m_sharedTexture != null) { m_sharedTexture.Dispose(); m_sharedTexture = null; } if (texture == null) { Lock(); SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); Unlock(); } else { if (IsShareable(texture) == false) throw new ArgumentException("Texture must be created with ResourceOptionFlags.Shared"); D3D9.Format format = TranslateFormat(texture); if (format == D3D9.Format.Unknown) throw new ArgumentException("Texture format is not compatible with OpenSharedResource"); IntPtr handle = GetSharedHandle(texture); if (handle == IntPtr.Zero) throw new ArgumentNullException("Handle"); m_sharedTexture = new D3D9.Texture(s_device, texture.Description.Width, texture.Description.Height, 1, D3D9.Usage.RenderTarget, format, D3D9.Pool.Default, ref handle); using (D3D9.Surface surface = m_sharedTexture.GetSurfaceLevel(0)) { Lock(); SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer); Unlock(); } } }
public void CheckTexture(ref D3D11.Texture2D tx, GDI.Size size) { if (tx == null) return; if (tx.Description.Width != size.Width || tx.Description.Height != size.Height) { tx.Dispose(); tx = null; } }
/// <summary> /// Creates a staging texture which enables copying data from gpu to cpu memory. /// </summary> /// <param name="device">Graphics device.</param> /// <param name="size">The size of generated texture.</param> public D3D11.Texture2D CreateStagingTexture(D3D11.Device device, GDI.Size size) { CheckTexture(ref _copyHelperTextureStaging, size); if (_copyHelperTextureStaging != null) return _copyHelperTextureStaging; //For handling of staging resource see // http://msdn.microsoft.com/en-us/library/windows/desktop/ff476259(v=vs.85).aspx D3D11.Texture2DDescription textureDescription = new D3D11.Texture2DDescription(); textureDescription.Width = size.Width; textureDescription.Height = size.Height; textureDescription.MipLevels = 1; textureDescription.ArraySize = 1; textureDescription.Format = DXGI.Format.B8G8R8A8_UNorm; textureDescription.Usage = D3D11.ResourceUsage.Staging; textureDescription.SampleDescription = new DXGI.SampleDescription(1, 0); textureDescription.BindFlags = D3D11.BindFlags.None; textureDescription.CpuAccessFlags = D3D11.CpuAccessFlags.Read; textureDescription.OptionFlags = D3D11.ResourceOptionFlags.None; return new D3D11.Texture2D(device, textureDescription); }
static IntPtr GetSharedHandle(D3D11.Texture2D texture) { var resource = texture.QueryInterface<SharpDX.DXGI.Resource>(); IntPtr result = resource.SharedHandle; resource.Dispose(); return result; }
static D3D9.Format TranslateFormat(D3D11.Texture2D texture) { switch (texture.Description.Format) { case SharpDX.DXGI.Format.R10G10B10A2_UNorm: return SharpDX.Direct3D9.Format.A2B10G10R10; case SharpDX.DXGI.Format.R16G16B16A16_Float: return SharpDX.Direct3D9.Format.A16B16G16R16F; case SharpDX.DXGI.Format.B8G8R8A8_UNorm: return SharpDX.Direct3D9.Format.A8R8G8B8; default: return SharpDX.Direct3D9.Format.Unknown; } }
static bool IsShareable(D3D11.Texture2D texture) { return (texture.Description.OptionFlags & D3D11.ResourceOptionFlags.Shared) != 0; }