/// <summary> /// Sets the render target of this D3DImage object. /// </summary> /// <param name="renderTarget">The render target to set.</param> public void SetRenderTarget(D3D11.Texture2D renderTarget) { if (this.m_d3dRenderTarget != null) { this.m_d3dRenderTarget = null; base.Lock(); base.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); base.Unlock(); } if (renderTarget == null) { return; } if (!IsShareable(renderTarget)) { throw new ArgumentException("Texture must be created with ResourceOptionFlags.Shared"); } D3D9.Format format = HigherD3DImageSource.TranslateFormat(renderTarget); if (format == D3D9.Format.Unknown) { throw new ArgumentException("Texture format is not compatible with OpenSharedResource"); } IntPtr handle = GetSharedHandle(renderTarget); if (handle == IntPtr.Zero) { throw new ArgumentNullException("Handle"); } //Map the texture to the D3DImage base class bool tDisposed = renderTarget.IsDisposed; float tWidth = renderTarget.Description.Width; float tHeight = renderTarget.Description.Height; this.m_d3dRenderTarget = new D3D9.Texture( m_d3dDevice, renderTarget.Description.Width, renderTarget.Description.Height, 1, D3D9.Usage.RenderTarget, format, D3D9.Pool.Default, ref handle); using (D3D9.Surface surface = this.m_d3dRenderTarget.GetSurfaceLevel(0)) { base.Lock(); base.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer); base.Unlock(); } }
/// <summary> /// Create all view resources. /// </summary> Tuple <D3D11.Texture2D, D3D11.RenderTargetView, D3D11.Texture2D, D3D11.DepthStencilView, SharpDX.Mathematics.Interop.RawViewportF, Size2, DpiScaling> IRenderLoopHost.OnRenderLoop_CreateViewResources(EngineDevice engineDevice) { // Calculate pixel with and high of this visual Size pixelSize = this.GetPixelSize(new Size(100.0, 100.0)); int width = (int)pixelSize.Width; int height = (int)pixelSize.Height; //Get references to current render device D3D11.Device renderDevice = engineDevice.DeviceD3D11_1; D3D11.DeviceContext renderDeviceContext = renderDevice.ImmediateContext; //Create the swap chain and the render target m_backBufferD3D11 = GraphicsHelper.CreateRenderTargetTexture(engineDevice, width, height, m_renderLoop.ViewConfiguration); m_backBufferForWpf = GraphicsHelper.CreateSharedTexture(engineDevice, width, height); m_renderTarget = new D3D11.RenderTargetView(renderDevice, m_backBufferD3D11); //Create the depth buffer m_depthBuffer = GraphicsHelper.CreateDepthBufferTexture(engineDevice, width, height, m_renderLoop.ViewConfiguration); m_renderTargetDepth = new D3D11.DepthStencilView(renderDevice, m_depthBuffer); //Apply render target size values m_renderTargetWidth = width; m_renderTargetHeight = height; //Define the viewport for rendering SharpDX.Mathematics.Interop.RawViewportF viewPort = GraphicsHelper.CreateDefaultViewport(width, height); //Apply new width and height values of the viewport m_viewportWidth = width; m_viewportHeight = height; //Create and apply the image source object m_d3dImageSource = new HigherD3DImageSource(engineDevice); m_d3dImageSource.SetRenderTarget(m_backBufferForWpf); if (this.Source == m_dummyBitmap) { this.Source = m_d3dImageSource; } m_lastRecreateWidth = width; m_lastRecreateHeight = height; //Return all generated objects return(Tuple.Create(m_backBufferD3D11, m_renderTarget, m_depthBuffer, m_renderTargetDepth, viewPort, new Size2(width, height), GetDpiScaling())); }
/// <summary> /// Disposes all loaded view resources. /// </summary> void IRenderLoopHost.OnRenderLoop_DisposeViewResources(EngineDevice engineDevice) { if (m_d3dImageSource != null) { this.Source = m_dummyBitmap; // Dispose the render target m_d3dImageSource.SetRenderTarget(null); m_d3dImageSource.Dispose(); m_d3dImageSource = null; } // Dispose all other resources m_renderTarget2DDxgi = GraphicsHelper.DisposeObject(m_renderTarget2DDxgi); m_renderTargetDepth = GraphicsHelper.DisposeObject(m_renderTargetDepth); m_depthBuffer = GraphicsHelper.DisposeObject(m_depthBuffer); m_renderTarget = GraphicsHelper.DisposeObject(m_renderTarget); m_backBufferForWpf = GraphicsHelper.DisposeObject(m_backBufferForWpf); m_backBufferD3D11 = GraphicsHelper.DisposeObject(m_backBufferD3D11); }