public static Texture GetSharedD3D9(this DeviceEx device, SharpDX.Direct3D10.Texture2D renderTarget) { if (renderTarget == null) { return(null); } if ((renderTarget.Description.OptionFlags & SharpDX.Direct3D10.ResourceOptionFlags.Shared) == 0) { throw new ArgumentException("Texture must be created with ResourceOptionFlags.Shared"); } Format format = ToD3D9(renderTarget.Description.Format); if (format == Format.Unknown) { throw new ArgumentException("Texture format is not compatible with OpenSharedResource"); } using (var resource = renderTarget.QueryInterface <SharpDX.DXGI.Resource>()) { IntPtr handle = resource.SharedHandle; if (handle == IntPtr.Zero) { throw new ArgumentNullException("Handle"); } return(new Texture(device, renderTarget.Description.Width, renderTarget.Description.Height, 1, Usage.RenderTarget, format, Pool.Default, ref handle)); } }
public unsafe static WriteableBitmap GetBitmap(this SharpDX.Direct3D10.Texture2D tex) { DataStream ds; DataRectangle db; using (var copy = tex.GetCopy()) using (var surface = copy.AsSurface()) db = surface.Map(DXGI.MapFlags.Read, out ds); int w = tex.Description.Width; int h = tex.Description.Height; var wb = new WriteableBitmap(w, h, 96.0, 96.0, PixelFormats.Bgra32, null); wb.Lock(); uint *wbb = (uint *)wb.BackBuffer; ds.Position = 0; for (int y = 0; y < h; y++) { ds.Position = y * db.Pitch; for (int x = 0; x < w; x++) { var c = ds.Read <FIXREAD>(); c.Fix(); wbb[y * w + x] = c.UINT; } } wb.AddDirtyRect(new Int32Rect(0, 0, w, h)); wb.Unlock(); ds.Dispose(); return(wb); }
private IntPtr GetSharedHandle(SharpDX.Direct3D10.Texture2D Texture) { SharpDX.DXGI.Resource resource = Texture.QueryInterface <SharpDX.DXGI.Resource>(); IntPtr result = resource.SharedHandle; resource.Dispose(); return(result); }
public void SetRenderTargetDX10(SharpDX.Direct3D10.Texture2D renderTarget) { if (_isDisposed) { throw new ObjectDisposedException(GetType().Name); } // System.Diagnostics.Debug.WriteLine("D3DImageSource.SetRenderTarget Name={0}, Id={1}, renderTarget={2}", Name, InstanceID, renderTarget); if (_renderTarget != null) { Disposer.RemoveAndDispose(ref _renderTarget); base.Lock(); base.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); base.Unlock(); } if (renderTarget != null) { if (!IsShareable(renderTarget)) { throw new ArgumentException("Texture must be created with ResourceOptionFlags.Shared"); } Format format = D3D10ImageSource.TranslateFormat(renderTarget); if (format == Format.Unknown) { throw new ArgumentException("Texture format is not compatible with OpenSharedResource"); } IntPtr handle = GetSharedHandle(renderTarget); if (handle == IntPtr.Zero) { throw new ArgumentNullException("Handle"); } _renderTarget = new Texture(D3D10ImageSource._d3DDevice, renderTarget.Description.Width, renderTarget.Description.Height, 1, Usage.RenderTarget, format, Pool.Default, ref handle); using (Surface surface = _renderTarget.GetSurfaceLevel(0)) { base.Lock(); base.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer); base.Unlock(); } } // System.Diagnostics.Debug.WriteLine("D3DImageSource.SetRenderTarget(end) Name={0}, Id={1}, renderTarget={2}, Size={3}x{4}", Name, InstanceID, renderTarget, this.PixelWidth, this.PixelHeight); }
private static Format TranslateFormat(SharpDX.Direct3D10.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); } }
public void SetRenderTargetDX10(SharpDX.Direct3D10.Texture2D renderTarget) { if (this.RenderTarget != null) { this.RenderTarget = 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"); } Format format = DX10ImageSource.TranslateFormat(renderTarget); if (format == Format.Unknown) { throw new ArgumentException("Texture format is not compatible with OpenSharedResource"); } IntPtr handle = GetSharedHandle(renderTarget); if (handle == IntPtr.Zero) { throw new ArgumentNullException("Handle"); } this.RenderTarget = new Texture(DX10ImageSource.D3DDevice, renderTarget.Description.Width, renderTarget.Description.Height, 1, Usage.RenderTarget, format, Pool.Default, ref handle); using (Surface surface = this.RenderTarget.GetSurfaceLevel(0)) { base.Lock(); base.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer); base.Unlock(); } }
internal static SharpDX.Direct3D10.Texture2D GetCopy(this SharpDX.Direct3D10.Texture2D tex) { var teximg = new SharpDX.Direct3D10.Texture2D(tex.Device, new SharpDX.Direct3D10.Texture2DDescription { Usage = SharpDX.Direct3D10.ResourceUsage.Staging, BindFlags = SharpDX.Direct3D10.BindFlags.None, CpuAccessFlags = SharpDX.Direct3D10.CpuAccessFlags.Read, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, OptionFlags = SharpDX.Direct3D10.ResourceOptionFlags.None, ArraySize = tex.Description.ArraySize, Height = tex.Description.Height, Width = tex.Description.Width, MipLevels = tex.Description.MipLevels, SampleDescription = tex.Description.SampleDescription, }); tex.Device.CopyResource(tex, teximg); return(teximg); }
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(); }
private static bool IsShareable(SharpDX.Direct3D10.Texture2D Texture) { return((Texture.Description.OptionFlags & SharpDX.Direct3D10.ResourceOptionFlags.Shared) != 0); }
/// <summary> /// /// </summary> /// <param name="texture"></param> public void SetBackBuffer(SharpDX.Direct3D10.Texture2D texture) { SetBackBuffer(s_d3d9.Device.GetSharedD3D9(texture)); }
internal static SharpDX.Direct3D10.Texture2D GetCopy(this SharpDX.Direct3D10.Texture2D tex) { var teximg = new SharpDX.Direct3D10.Texture2D(tex.Device, new SharpDX.Direct3D10.Texture2DDescription { Usage = SharpDX.Direct3D10.ResourceUsage.Staging, BindFlags = SharpDX.Direct3D10.BindFlags.None, CpuAccessFlags = SharpDX.Direct3D10.CpuAccessFlags.Read, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, OptionFlags = SharpDX.Direct3D10.ResourceOptionFlags.None, ArraySize = tex.Description.ArraySize, Height = tex.Description.Height, Width = tex.Description.Width, MipLevels = tex.Description.MipLevels, SampleDescription = tex.Description.SampleDescription, }); tex.Device.CopyResource(tex, teximg); return teximg; }
public static WriteableBitmap GetBitmap(this SharpDX.Direct3D10.Texture2D tex) { throw new NotImplementedException(); }