private void SurfaceUpdated(IntPtr surfaceHandle) { SharpDX.ComObject sharedObject = new SharpDX.ComObject(surfaceHandle); SharpDX.DXGI.Resource sharedResource = sharedObject.QueryInterface <SharpDX.DXGI.Resource>(); SharpDX.Direct3D11.Texture2D nativeRexture = dX11GraphicsContext.DXDevice.OpenSharedResource <SharpDX.Direct3D11.Texture2D>(sharedResource.SharedHandle); var texture = DX11Texture.FromDirectXTexture(dX11GraphicsContext, nativeRexture); var rTDepthTargetDescription = new TextureDescription() { Type = TextureType.Texture2D, Format = PixelFormat.D24_UNorm_S8_UInt, Width = texture.Description.Width, Height = texture.Description.Height, Depth = 1, ArraySize = 1, Faces = 1, Flags = TextureFlags.DepthStencil, CpuAccess = ResourceCpuAccess.None, MipLevels = 1, Usage = ResourceUsage.Default, SampleCount = TextureSampleCount.None, }; var rTDepthTarget = this.dX11GraphicsContext.Factory.CreateTexture(ref rTDepthTargetDescription, "SwapChain_Depth"); var frameBuffer = this.dX11GraphicsContext.Factory.CreateFrameBuffer(new FrameBufferAttachment(rTDepthTarget, 0, 1), new[] { new FrameBufferAttachment(texture, 0, 1) }); frameBuffer.SwapchainAssociated = true; display.FrameBuffer?.Dispose(); display.UpdateFrameBuffer(frameBuffer); }
public Framebuffer FrameBuffer(IntPtr surface, bool isNewSurface) { if (isNewSurface) { if (_framebuffer != null) { _framebuffer.Dispose(); _framebuffer = null; } } if (_framebuffer == null) { SharpDX.Direct3D11.Device _deviceD11 = device; if (_deviceD11 == null) { return(null); } using (var _ComObject = new SharpDX.ComObject(surface)) { Texture2D _texture2D = null; Texture _texture = null; try { var _dxgiResource = _ComObject.QueryInterface <SharpDX.DXGI.Resource>(); if (_dxgiResource == null) { return(null); } _texture2D = _deviceD11.OpenSharedResource <Texture2D>(_dxgiResource.SharedHandle); if (_texture2D == null) { return(null); } _texture = graphicsDevice.ResourceFactory.CreateTexture((ulong)_texture2D.NativePointer, getTexture2DDescription(_texture2D)); if (_texture == null) { return(null); } _framebuffer = s_graphicsDevice.ResourceFactory.CreateFramebuffer(new FramebufferDescription(null, _texture)); } finally { _texture?.Dispose(); _texture2D?.Dispose(); } } } return(_framebuffer); }
public D2DContext GetContext(GraphicsDevice graphicsDevice) { SharpDX.ComObject obj = GetRenderTargetResource(graphicsDevice); D2DContext context = GetOrCreateContext(obj); if (context == null) { return(null); } AlphaMode alphaMode = AlphaMode.Ignore; if (context.DxgiSurface == null || context.DxgiSurface.IsDisposed) { if (obj is SharpDX.DXGI.SwapChain) { var swapChain = (SharpDX.DXGI.SwapChain)obj; context.DxgiSurface = SharpDX.DXGI.Surface.FromSwapChain(swapChain, 0); alphaMode = AlphaMode.Ignore; } else if (obj is SharpDX.Direct3D11.Resource) { context.DxgiSurface = obj.QueryInterface <SharpDX.DXGI.Surface>(); alphaMode = AlphaMode.Premultiplied; } else { return(null); } } if (context.D2DRenderTarget == null || context.D2DRenderTarget.IsDisposed) { var rtProp = new RenderTargetProperties(new PixelFormat(SharpDX.DXGI.Format.Unknown, alphaMode)); var d2drt = new RenderTarget(this.factory2D, context.DxgiSurface, rtProp); d2drt.TextRenderingParams = new RenderingParams(factoryDWrite, 1f, 0f, 0f, PixelGeometry.Flat, RenderingMode.CleartypeGdiClassic); d2drt.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale; context.D2DRenderTarget = d2drt; context.DxgiSurface.Disposing += (o, e) => d2drt.Dispose(); } return(context); }
private void OnRender(IntPtr handle, bool isNewSurface) { if (isNewSurface) { if (brush != null) { brush.Dispose(); brush = null; } if (renderTarget != null) { renderTarget.Dispose(); renderTarget = null; } SharpDX.ComObject comObject = new SharpDX.ComObject(handle); SharpDX.DXGI.Resource resource = comObject.QueryInterface <SharpDX.DXGI.Resource>(); SharpDX.Direct3D10.Texture2D texture = resource.QueryInterface <SharpDX.Direct3D10.Texture2D>(); using (var surface = texture.QueryInterface <SharpDX.DXGI.Surface>()) { var properties = new RenderTargetProperties(); properties.DpiX = 96; properties.DpiY = 96; properties.MinLevel = FeatureLevel.Level_DEFAULT; properties.PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, AlphaMode.Premultiplied); properties.Type = RenderTargetType.Default; properties.Usage = RenderTargetUsage.None; renderTarget = new RenderTarget(new Factory(), surface, properties); } } if (brush == null) { brush = new SharpDX.Direct2D1.SolidColorBrush(renderTarget, new SharpDX.Color4(0.2f, 0.2f, 0.2f, 0.5f)); } renderTarget.BeginDraw(); renderTarget.DrawTextLayout(new SharpDX.Vector2(50, 50), textLayout, brush); renderTarget.EndDraw(); }
public HResult Invoke(IMFAsyncResult pResult) { object pUnkObject; IMFSample pSample = null; IMFMediaBuffer pBuffer = null; IMFDXGIBuffer pDXGIBuffer = null; // Get the IUnknown out of the IMFAsyncResult if there is one HResult hr = pResult.GetObject(out pUnkObject); if (Succeeded(hr)) { pSample = pUnkObject as IMFSample; } if (pSample != null) { // Based on your implementation, there should only be one // buffer attached to one sample, so we can always grab the // first buffer. You could add some error checking here to make // sure the sample has a buffer count that is 1. hr = pSample.GetBufferByIndex(0, out pBuffer); } if (Succeeded(hr)) { // Query the IMFMediaBuffer to see if it implements IMFDXGIBuffer pDXGIBuffer = pBuffer as IMFDXGIBuffer; } if (pDXGIBuffer != null) { // Got an IMFDXGIBuffer, so we can extract the internal // ID3D11Texture2D and make a new SharpDX.Texture2D wrapper. hr = pDXGIBuffer.GetResource(s_IID_ID3D11Texture2D, out pUnkObject); } if (Succeeded(hr)) { // If we got here, pUnkObject is the native D3D11 Texture2D as // a System.Object, but it's unlikely you have an interface // definition for ID3D11Texture2D handy, so we can't just cast // the object to the proper interface. // Happily, SharpDX supports wrapping System.Object within // SharpDX.ComObject which makes things pretty easy. SharpDX.ComObject comWrapper = new SharpDX.ComObject(pUnkObject); // If this doesn't work, or you're using something like SlimDX // which doesn't support object wrapping the same way, the below // code is an alternative way. /* * IntPtr pD3DTexture2D = Marshal.GetIUnknownForObject(pUnkObject); * // Create your wrapper object here, like this for SharpDX * SharpDX.ComObject comWrapper = new SharpDX.ComObject(pD3DTexture2D); * // or like this for SlimDX * SlimDX.Direct3D11.Texture2D.FromPointer(pD3DTexture2D); * Marshal.Release(pD3DTexture2D); */ // You might need to query comWrapper for a SharpDX.DXGI.Resource // first, then query that for the SharpDX.Direct3D11.Texture2D. SharpDX.Direct3D11.Texture2D texture = comWrapper.QueryInterface <SharpDX.Direct3D11.Texture2D>(); if (texture != null) { // Now you can add "texture" back to the allocator's free list ReturnFreeTexture(texture); } } }