private static Interop.Direct3DTexture9 GetSharedSurface(Interop.Direct3DDevice9Ex device, D3D10.Texture2D texture) { // First get a shared handle to the D3D10 texture using (var surface = texture.QueryInterface<DirectX.Graphics.Resource>()) { IntPtr handle = surface.SharedHandle; // Then create a D3D9 texture using the D3D10 shared handle. // The D3D10 texture must be in the DXGI_FORMAT_B8G8R8A8_UNORM // format (direct 9 version is D3DFMT_A8R8G8B8). return device.CreateTexture( texture.Description.Width, texture.Description.Height, 1, 1, // D3DUSAGE_RENDERTARGET 21, // D3DFMT_A8R8G8B8 0, // D3DPOOL_DEFAULT ref handle); } }
/// <summary> /// Assigns a <see cref="D3D10.Texture2D"/> as the source of the back buffer. /// </summary> /// <param name="texture"> /// The <c>Texture2D</c> to assign as the back buffer. Value can be null. /// </param> /// <exception cref="InvalidOperationException"> /// The D3D10Image has not been locked by a call to the /// <see cref="D3DImage.Lock" /> or <see cref="D3DImage.TryLock" /> methods. /// </exception> public void SetBackBuffer(D3D10.Texture2D texture) { // Free the old surface if (this.surface != null) { this.surface.Dispose(); this.surface = null; } if (texture == null) { this.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); } else { using (var device = CreateDevice(Interop.NativeMethods.GetDesktopWindow())) using (var texture9 = GetSharedSurface(device, texture)) { this.surface = texture9.GetSurfaceLevel(0); } this.SetBackBuffer(D3DResourceType.IDirect3DSurface9, this.surface.NativeInterface); } }
private static D3D10.D3DDevice1 TryCreateDevice1(D3D10.DriverType type) { // We'll try to create the device that supports any of these feature levels DirectX.Direct3D.FeatureLevel[] levels = { DirectX.Direct3D.FeatureLevel.Ten, DirectX.Direct3D.FeatureLevel.NinePointThree, DirectX.Direct3D.FeatureLevel.NinePointTwo, DirectX.Direct3D.FeatureLevel.NinePointOne }; foreach (var level in levels) { try { return D3D10.D3DDevice1.CreateDevice1(null, type, null, D3D10.CreateDeviceOptions.SupportBgra, level); } catch (ArgumentException) // E_INVALIDARG { continue; // Try the next feature level } catch (OutOfMemoryException) // E_OUTOFMEMORY { continue; // Try the next feature level } catch (DirectX.DirectXException) // D3DERR_INVALIDCALL or E_FAIL { continue; // Try the next feature level } } return null; // We failed to create a device at any required feature level }