public D2DFont(Texture2D backBuffer) { SharpDX.Direct2D1.Factory d2dFactory = new SharpDX.Direct2D1.Factory(); Surface d2dSurface = backBuffer.QueryInterface <Surface>(); PixelFormat pixelFormat = new PixelFormat(D3DHandler.RENDER_FORMAT, D3DHandler.ALPHA_MODE); var renderTargetProp = new RenderTargetProperties(pixelFormat); renderTarget = new RenderTarget(d2dFactory, d2dSurface, renderTargetProp); d2dSurface.Dispose(); d2dFactory.Dispose(); }
public DUIWICRenderTarget(DUIBitmap image) { System.Threading.Monitor.Enter(lockObj); RenderTargetProperties renderTargetProperties = new RenderTargetProperties { Type = RenderTargetType.Default, DpiX = 96.0f, DpiY = 96.0f, PixelFormat = new PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Unknown), Usage = RenderTargetUsage.None, MinLevel = FeatureLevel.Level_DEFAULT }; this.wicRenderTarget = new SharpDX.Direct2D1.WicRenderTarget(d2dFactory, image, renderTargetProperties); }
public IDrawingTarget BeginDraw(Color?clearColor) { #if NETFX_CORE var surface = _texture.QueryInterface <Surface>(); #else var surface = _texture.AsSurface(); #endif var rtProperties = new RenderTargetProperties { DpiX = 96, DpiY = 96, Type = RenderTargetType.Default, PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied) }; var renderTarget = new RenderTarget(_factory, surface, rtProperties); renderTarget.BeginDraw(); // required to clear the render target // (not required on all machines, MacBook Air + Win7 seems to recycle old textures) renderTarget.Clear(clearColor != null ? clearColor.Value.import() : (Color4?)null); var state = new DrawingState(); var transform = new DrawingTransform(); var drawingTarget = new DrawingTarget(state, transform, renderTarget, _width, _height); var target = new DrawingTargetSplitter( _backend, state, transform, drawingTarget, drawingTarget, drawingTarget, drawingTarget, drawingTarget, () => { drawingTarget.Dispose(); renderTarget.EndDraw(); renderTarget.Dispose(); surface.Dispose(); }); var pixelAligner = PixelAligningDrawingTarget.Create(target, target.Dispose, state, transform); return(pixelAligner); }
private void CreateDeviceResource() { if (_renderTarget == null) { var width = _view_width; var height = _view_height; _cacheBmp = new Bitmap(width, height); _hBitmap = _cacheBmp.GetHbitmap(Color.FromArgb(0, 0, 0, 0)); _hOldBitmap = Gdi32.SelectObject(_memDC, _hBitmap); var renderProps = new RenderTargetProperties() { Type = RenderTargetType.Hardware, Usage = RenderTargetUsage.None, PixelFormat = new Vortice.DCommon.PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied), MinLevel = FeatureLevel.Default }; _renderTarget = _d2dFactory.CreateDCRenderTarget(renderProps); if (_cachedActiveBmp == null) { var bmp = LoadD2D1BitmapFromBitmap(_renderTarget, _decorator.ActiveBitmapTemplate); _cachedActiveBmp = _renderTarget.CreateSharedBitmap(bmp, new BitmapProperties { PixelFormat = bmp.PixelFormat }); } if (_cachedInactiveBmp == null) { var bmp = LoadD2D1BitmapFromBitmap(_renderTarget, _decorator.InactiveBitmapTemplate); _cachedInactiveBmp = _renderTarget.CreateSharedBitmap(bmp, new BitmapProperties { PixelFormat = bmp.PixelFormat }); } if (width > 0 && height > 0) { _renderTarget.BindDC(_memDC, new Vortice.RawRect(0, 0, width, height)); } } }
public Renderer(IntPtr hwnd, Bounds b) { this.hwnd = hwnd; this.currentBounds = b; d2dFactory = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded); fontFactory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared); var properties = new RenderTargetProperties(new PixelFormat(Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)); d2dTarget = new SharpDX.Direct2D1.WindowRenderTarget(d2dFactory, properties, new HwndRenderTargetProperties { Hwnd = hwnd, PixelSize = new Size2(currentBounds.Width, currentBounds.Height), PresentOptions = PresentOptions.Immediately }); }
private void InitDX() { _d2dFactory = new SharpDX.Direct2D1.Factory(FactoryType.MultiThreaded); SharpDX.Direct2D1.PixelFormat p = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied); HwndRenderTargetProperties h = new HwndRenderTargetProperties(); h.Hwnd = _picContainer.Handle; h.PixelSize = new Size2(_picContainer.Width, _picContainer.Height); h.PresentOptions = SharpDX.Direct2D1.PresentOptions.None; RenderTargetProperties r = new RenderTargetProperties(RenderTargetType.Hardware, p, 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); _renderTarget = new WindowRenderTarget(_d2dFactory, r, h); _imgFactory = new ImagingFactory(); }
public RenderTarget Create(Factory factory, Graphics g, Map map) { var hdc = g.GetHdc(); var rtp = new RenderTargetProperties( RenderTargetType.Default, new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); var rt = new DeviceContextRenderTarget(factory, rtp); rt.Tag = hdc; rt.BindDeviceContext(hdc, new SharpDX.Rectangle(0, 0, map.Size.Width, map.Size.Height)); rt.BeginDraw(); return(rt); }
private void InitRender(Control container) { // Direct2D var renderTargetProperties = new RenderTargetProperties() { PixelFormat = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Ignore) }; var hwndRenderTargetProperties = new HwndRenderTargetProperties() { Hwnd = container.Handle, PixelSize = new Size2(container.Width, container.Height), PresentOptions = PresentOptions.Immediately, }; var renderTarget = new WindowRenderTarget(factory, renderTargetProperties, hwndRenderTargetProperties); render[container.Handle] = renderTarget; }
public GPURenderer1(System.Windows.Forms.Control cltr) { Program.MainLog.Add(MessageType.MSG, "Creating D2D Renderer"); IsHardware = true; refTrack.Add(this); float scale = GetScaleFactor(); this.cltr = cltr; if (factory == null || factory.IsDisposed) { factory = new Factory(FactoryType.MultiThreaded, DebugLevel.Error); Program.MainLog.Add(MessageType.MSG, "Factory created: {0}", factory.NativePointer); factoryAccesCount = 1; } else { factoryAccesCount++; Program.MainLog.Add(MessageType.MSG, "Accessing Factory {0}, cnt = {1}", factory.NativePointer, factoryAccesCount); } HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(); RenderTargetProperties rtProps = new RenderTargetProperties(); hwndProps.Hwnd = cltr.Handle; hwndProps.PixelSize = new Size2(cltr.Width, cltr.Height); hwndProps.PresentOptions = PresentOptions.Immediately; rtProps.Type = RenderTargetType.Default; mainRenderTarget = new WindowRenderTarget(factory, rtProps, hwndProps); Program.MainLog.Add(MessageType.MSG, "Render Target created: {0}", mainRenderTarget.NativePointer); //RenderTarget = mainRenderTarget; Width = cltr.Width; Height = cltr.Height; mainRenderTarget.AntialiasMode = AntialiasMode.PerPrimitive; mainRenderTarget.DotsPerInch = new Size2F( mainRenderTarget.DotsPerInch.Width / scale, mainRenderTarget.DotsPerInch.Height / scale); writeFactory = new text.Factory(); Program.MainLog.Add(MessageType.MSG, "Direct Write Factory created: {0}", writeFactory.NativePointer); if (brush_gray == null) { brush_gray = new SolidColorBrush(mainRenderTarget, gdi.Color.Gray.ToColor4()); } renderTarget = mainRenderTarget; Program.MainLog.Add(MessageType.MSG, "Renderer initialized"); }
/// <summary> /// 使用指定的渲染目标控件创建渲染目标。 /// </summary> /// <param name="control">渲染的目标控件。</param> public void CreateRenderTarget(Control control) { if (SupportD3D) { // 创建 Direct3D SwapChain。 SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = 1, Usage = Usage.RenderTargetOutput, OutputHandle = control.Handle, IsWindowed = true, ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format), SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard }; this.swapChain = new SwapChain(dxgiDevice.GetParent <Adapter>().GetParent <DXGIFactory>(), d3DDevice, swapChainDesc); this.backBuffer = Surface.FromSwapChain(this.swapChain, 0); this.targetBitmap = new Bitmap1(this.d2DContext, backBuffer); this.renderTarget = new DeviceContext(this.d2DDevice, DeviceContextOptions.None); ((DeviceContext)this.renderTarget).Target = targetBitmap; } else { // 创建 Direct2D 工厂。 // 渲染参数。 RenderTargetProperties renderProps = new RenderTargetProperties { PixelFormat = D2PixelFormat, Usage = RenderTargetUsage.None, Type = RenderTargetType.Default }; // 渲染目标属性。 HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties() { Hwnd = control.Handle, PixelSize = new Size2(control.ClientSize.Width, control.ClientSize.Height), PresentOptions = PresentOptions.None }; // 渲染目标。 this.renderTarget = new WindowRenderTarget(d2DFactory, renderProps, hwndProps) { AntialiasMode = AntialiasMode.PerPrimitive }; } }
private void setupInstance(bool deleteOld = false) { if (deleteOld) { try { _brush.Dispose(); _font.Dispose(); _fontFactory.Dispose(); _factory.Dispose(); _device.Dispose(); } catch { } } _factory = new Factory(SharpDX.Direct2D1.FactoryType.MultiThreaded, DebugLevel.None); _fontFactory = new FontFactory(); RECT bounds = default(RECT); User32.GetWindowRect(TargetHandle, out bounds); _targetProperties = new HwndRenderTargetProperties() { Hwnd = TargetHandle, PixelSize = new Size2(Math.Abs(bounds.Right - bounds.Left), Math.Abs(bounds.Bottom - bounds.Top)), PresentOptions = VSync ? PresentOptions.None : PresentOptions.Immediately }; var renderTargetProperties = new RenderTargetProperties( RenderTargetType.Hardware, new PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT ); _device = new WindowRenderTarget(_factory, renderTargetProperties, _targetProperties); _device.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Aliased; _device.AntialiasMode = AntialiasMode.Aliased; _brush = new SolidColorBrush(_device, new RawColor4(0, 0, 0, 0)); }
public WicRenderTargetBitmapImpl( PixelSize size, Vector dpi, Platform.PixelFormat?pixelFormat = null) : base(size, dpi, pixelFormat) { var props = new RenderTargetProperties { DpiX = (float)dpi.X, DpiY = (float)dpi.Y, }; _renderTarget = new WicRenderTarget( Direct2D1Platform.Direct2D1Factory, WicImpl, props); }
/// <summary> /// This method creates the render target and all associated D2D and DWrite resources /// </summary> void CreateDeviceResources() { // Only calls if resources have not been initialize before if (renderTarget == null) { // The text format textFormat = dwriteFactory.CreateTextFormat("Bodoni MT", 24, DWrite.FontWeight.Normal, DWrite.FontStyle.Italic, DWrite.FontStretch.Normal); // Create the render target SizeU size = new SizeU((uint)host.ActualWidth, (uint)host.ActualHeight); RenderTargetProperties props = new RenderTargetProperties(); HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(host.Handle, size, PresentOptions.None); renderTarget = d2dFactory.CreateHwndRenderTarget(props, hwndProps); // A black brush to be used for drawing text ColorF cf = new ColorF(0, 0, 0, 1); blackBrush = renderTarget.CreateSolidColorBrush(cf); // Create a linear gradient. GradientStop[] stops = { new GradientStop(1, new ColorF(1f, 0f, 0f, 0.25f)), new GradientStop(0, new ColorF(0f, 0f, 1f, 1f)) }; GradientStopCollection pGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap); LinearGradientBrushProperties gradBrushProps = new LinearGradientBrushProperties(new Point2F(50, 25), new Point2F(25, 50)); linearGradientBrush = renderTarget.CreateLinearGradientBrush(gradBrushProps, pGradientStops); gridPatternBitmapBrush = CreateGridPatternBrush(renderTarget); solidBrush1 = renderTarget.CreateSolidColorBrush(new ColorF(0.3F, 0.5F, 0.65F, 0.25F)); solidBrush2 = renderTarget.CreateSolidColorBrush(new ColorF(0.0F, 0.0F, 0.65F, 0.5F)); solidBrush3 = renderTarget.CreateSolidColorBrush(new ColorF(0.9F, 0.5F, 0.3F, 0.75F)); // Create a linear gradient. stops[0] = new GradientStop(1, new ColorF(0f, 0f, 0f, 0.25f)); stops[1] = new GradientStop(0, new ColorF(1f, 1f, 0.2f, 1f)); GradientStopCollection radiantGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap); RadialGradientBrushProperties radialBrushProps = new RadialGradientBrushProperties(new Point2F(25, 25), new Point2F(0, 0), 10, 10); radialGradientBrush = renderTarget.CreateRadialGradientBrush(radialBrushProps, radiantGradientStops); } }
public RenderTargetBitmapImpl( ImagingFactory imagingFactory, Factory d2dFactory, int width, int height) : base(imagingFactory, width, height) { var props = new RenderTargetProperties { DpiX = 96, DpiY = 96, }; _target = new WicRenderTarget( d2dFactory, WicImpl, props); }
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); }
/// <summary> /// Creates an instance of D2dWicGraphics that can be used for rendering to off screen surfaces /// and can be copied to main memory</summary> /// <param name="width">Width of the off screen surface.</param> /// <param name="height">Height of the off screen surface.</param> /// <returns>D2dWicGraphics that can be used for rendering to off screen surfaces</returns> public static D2dWicGraphics CreateWicGraphics(int width, int height) { var wicBitmap = new SharpDX.WIC.Bitmap(s_wicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); var rtprops = new RenderTargetProperties { Type = RenderTargetType.Default, DpiX = 96.0f, DpiY = 96.0f, PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Unknown), Usage = RenderTargetUsage.None, MinLevel = FeatureLevel.Level_DEFAULT }; var rt = new WicRenderTarget(s_d2dFactory, wicBitmap, rtprops); return(new D2dWicGraphics(rt, wicBitmap)); }
/// <summary> /// 初始化 HwndRenderTarget。 /// </summary> private void InitHwndRenderTarget() { // 创建 Direct2D 单线程工厂。 using (DXFactory dxFactory = new DXFactory(FactoryType.SingleThreaded)) { // 渲染参数。 RenderTargetProperties renderProps = new RenderTargetProperties { PixelFormat = D2PixelFormat, Usage = RenderTargetUsage.None, Type = RenderTargetType.Default }; // 渲染目标属性。 HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties() { // 承载控件的句柄。 Hwnd = _ctrl.Handle, // 控件的尺寸。 PixelSize = RenderTargetClientSize, PresentOptions = PresentOptions.None, }; // 渲染目标。 hwndRenderTarget = new WindowRenderTarget(dxFactory, renderProps, hwndProps) { AntialiasMode = AntialiasMode.PerPrimitive, }; } // 初始化画刷资源。 //hwndBrush = new SolidColorBrush(hwndRenderTarget, Color.Black); //_textFormat = new SharpDX.DirectWrite.TextFormat(new SharpDX.DirectWrite.Factory(), _ctrl.Font.Name, 15); // 在控件大小被改变的时候必须改变渲染目标的大小, // 否则会导致绘制结果被拉伸,引起失真。 _ctrl.SizeChanged += (sender, e) => { lock (_lockObj) { hwndRenderTarget.Resize(RenderTargetClientSize); } }; _ctrl.HandleDestroyed += (sender, e) => _cancelTask.Cancel(true); }
public WicRenderTargetBitmapImpl( int width, int height, double dpiX, double dpiY, Platform.PixelFormat?pixelFormat = null) : base(width, height, pixelFormat) { var props = new RenderTargetProperties { DpiX = (float)dpiX, DpiY = (float)dpiY, }; _renderTarget = new WicRenderTarget( Direct2D1Platform.Direct2D1Factory, WicImpl, props); }
public void CreateRenderTarget(SharpDX.WIC.Bitmap wicBitmap) { if (renderTarget != null) { return; } var renderTargetProperties = new RenderTargetProperties( RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Unknown), 0, 0, RenderTargetUsage.GdiCompatible, FeatureLevel.Level_DEFAULT); renderTarget = new WicRenderTarget(d2dFactory, wicBitmap, renderTargetProperties); IntPtr gdirtPtr; renderTarget.QueryInterface(Guid.Parse("e0db51c3-6f77-4bae-b3d5-e47509b35838"), out gdirtPtr); gdiRenderTarget = new GdiInteropRenderTarget(gdirtPtr); }
public TextBlockRenderer(SpriteRenderer sprite, String fontName, SlimDX.DirectWrite.FontWeight fontWeight, SlimDX.DirectWrite.FontStyle fontStyle, FontStretch fontStretch, float fontSize) { Sprite = sprite; FontSize = fontSize; D3DDevice11 = ModelEx.DeviceManager.Instance.device; System.Threading.Monitor.Enter(D3DDevice11); rtp = new RenderTargetProperties() { HorizontalDpi = 96, VerticalDpi = 96, Type = RenderTargetType.Default, PixelFormat = new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), MinimumFeatureLevel = FeatureLevel.Direct3D10 }; Font = ModelEx.FontManager.Instance.WriteFactory.CreateTextFormat(fontName, fontWeight, fontStyle, fontStretch, fontSize, CultureInfo.CurrentCulture.Name); System.Threading.Monitor.Exit(D3DDevice11); CreateCharTable(0); }
private void CreateAndBindTargets() { if (d3DSurface == null) { return; } d3DSurface.SetRenderTarget(null); Disposer.SafeDispose(ref d2DRenderTarget); Disposer.SafeDispose(ref d2DFactory); Disposer.SafeDispose(ref renderTarget); var width = Math.Max((int)ActualWidth, 100); var height = Math.Max((int)ActualHeight, 100); var renderDesc = new Texture2DDescription { BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Format = Format.B8G8R8A8_UNorm, Width = width, Height = height, MipLevels = 1, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.Shared, CpuAccessFlags = CpuAccessFlags.None, ArraySize = 1 }; renderTarget = new Texture2D(device, renderDesc); var surface = renderTarget.QueryInterface <Surface>(); d2DFactory = new SharpDX.Direct2D1.Factory(); var rtp = new RenderTargetProperties(new PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)); d2DRenderTarget = new RenderTarget(d2DFactory, surface, rtp); resCache.RenderTarget = d2DRenderTarget; d3DSurface.SetRenderTarget(renderTarget); device.ImmediateContext.Rasterizer.SetViewport(0, 0, width, height, 0.0f, 1.0f); }
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 void InitializeRenderTarget() { if (dcRenderTarget == null) { var props = new RenderTargetProperties { PixelFormat = new PixelFormat( Microsoft.WindowsAPICodePack.DirectX.Graphics.Format.B8G8R8A8UNorm, AlphaMode.Ignore), Usage = RenderTargetUsages.GdiCompatible }; dcRenderTarget = d2DFactory.CreateDCRenderTarget(props); brush = dcRenderTarget.CreateSolidColorBrush( new ColorF( ForeColor.R / 256f, ForeColor.G / 256f, ForeColor.B / 256f, 1)); } }
private void InitializeRenderTarget() { if (dcRenderTarget == null) { var props = new RenderTargetProperties { PixelFormat = new PixelFormat( DXGI.Format.B8G8R8A8_UNORM, AlphaMode.Ignore), Usage = RenderTargetUsage.GdiCompatible }; dcRenderTarget = d2DFactory.CreateDCRenderTarget(props); brush = dcRenderTarget.CreateSolidColorBrush( new ColorF( ForeColor.R / 256f, ForeColor.G / 256f, ForeColor.B / 256f, 1)); } }
/// <summary> /// This method creates the render target and all associated D2D and DWrite resources /// </summary> void CreateDeviceResources() { // Only calls if resources have not been initialize before if (renderTarget == null) { // Create the render target SizeU size = new SizeU((uint)host.ActualWidth, (uint)host.ActualHeight); RenderTargetProperties props = new RenderTargetProperties(); HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(host.Handle, size, PresentOptions.None); renderTarget = d2dFactory.CreateHwndRenderTarget(props, hwndProps); // Create the black brush for text blackBrush = renderTarget.CreateSolidColorBrush(new ColorF(0, 0, 0, 1)); inlineImage = new ImageInlineObject(renderTarget, wicFactory, "TextInlineImage.img1.jpg"); TextRange textRange = new TextRange(14, 1); textLayout.SetInlineObject(inlineImage, textRange); } }
/// <summary> /// Initializes a new instance of the <see cref="Renderer"/> class. /// </summary> /// <param name="hwnd">The window handle.</param> /// <param name="width">The width of the window.</param> /// <param name="height">The height of the window.</param> public Renderer(IntPtr hwnd, double width, double height) { this.Direct2DFactory = Locator.Current.GetService <Factory>(); this.DirectWriteFactory = Locator.Current.GetService <DwFactory>(); RenderTargetProperties renderTargetProperties = new RenderTargetProperties { }; HwndRenderTargetProperties hwndProperties = new HwndRenderTargetProperties { Hwnd = hwnd, PixelSize = new Size2((int)width, (int)height), }; this.renderTarget = new WindowRenderTarget( this.Direct2DFactory, renderTargetProperties, hwndProperties); }
public Direct2DRenderer(IntPtr pWindowHandle) { Control control = Control.FromHandle(pWindowHandle); var renderProps = new RenderTargetProperties { PixelFormat = new PixelFormat( Microsoft.WindowsAPICodePack.DirectX.DXGI.Format.B8G8R8A8_UNORM, AlphaMode.Ignore), Usage = RenderTargetUsage.None, Type = RenderTargetType.Default // Software type is required to allow resource // sharing between hardware (HwndRenderTarget) // and software (WIC Bitmap render Target). }; _renderer = D2DFactory.CreateHwndRenderTarget( renderProps, new HwndRenderTargetProperties(pWindowHandle, new SizeU(Convert.ToUInt32(control.ClientSize.Width), Convert.ToUInt32(control.ClientSize.Height)), PresentOptions.Immediately)); }
public MainWindow() { InitializeComponent(); Loaded += (s, e) => { var factory = new Factory(); var pixelFormat = new Direct2D1.PixelFormat(DXGI.Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied); var hwndRenderTargetProperties = new HwndRenderTargetProperties(); hwndRenderTargetProperties.Hwnd = new WindowInteropHelper(this).Handle; hwndRenderTargetProperties.PixelSize = new Size2((int)ActualWidth, (int)ActualHeight); var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, pixelFormat, 96, 96, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); _renderTarget = new WindowRenderTarget(factory, renderTargetProperties, hwndRenderTargetProperties); CompositionTarget.Rendering += CompositionTarget_Rendering; }; }
private void InitializeResources(Surface surface) { m_renderTargetProperties = new RenderTargetProperties(); m_renderTargetProperties.HorizontalDpi = 96; m_renderTargetProperties.VerticalDpi = 96; m_renderTargetProperties.PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied); m_renderTargetProperties.Usage = RenderTargetUsage.None; m_renderTargetProperties.MinimumFeatureLevel = FeatureLevel.Direct3D10; InternalRenderTarget = RenderTarget.FromDXGI(m_deviceContext10.Direct2DFactory, surface, m_renderTargetProperties); InternalRenderTarget.AntialiasMode = AntialiasMode.PerPrimitive; var bitmapProperties = new BitmapProperties(); bitmapProperties.PixelFormat = new PixelFormat(m_format, AlphaMode.Premultiplied); InternalBitmap = new Bitmap(InternalRenderTarget, surface, bitmapProperties); }
/// <summary> /// Creates a new text renderer for a specific font. /// </summary> /// <param name="Sprite">The sprite renderer that is used for rendering</param> /// <param name="FontName">Name of font. The font has to be installed on the system. /// If no font can be found, a default one is used.</param> /// <param name="FontSize">Size in which to prerender the text. FontSize should be equal to render size for best results.</param> /// <param name="FontStretch">Font stretch parameter</param> /// <param name="FontStyle">Font style parameter</param> /// <param name="FontWeight">Font weight parameter</param> public TextBlockRenderer(SpriteRenderer Sprite, String FontName, global::SlimDX.DirectWrite.FontWeight FontWeight, global::SlimDX.DirectWrite.FontStyle FontStyle, FontStretch FontStretch, float FontSize) { AssertDevice(); ReferenceCount++; this.Sprite = Sprite; this._FontSize = FontSize; D3DDevice11 = Sprite.Device; System.Threading.Monitor.Enter(D3DDevice11); rtp = new RenderTargetProperties() { HorizontalDpi = D2DFactory.DesktopDpi.Width, VerticalDpi = D2DFactory.DesktopDpi.Height, Type = RenderTargetType.Default, PixelFormat = new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), MinimumFeatureLevel = FeatureLevel.Direct3D10 }; Font = WriteFactory.CreateTextFormat(FontName, FontWeight, FontStyle, FontStretch, FontSize, CultureInfo.CurrentCulture.Name); System.Threading.Monitor.Exit(D3DDevice11); CreateCharTable(0); }
/// <summary> /// Initializes a new instance of the <see cref="RenderTarget"/> class. /// </summary> /// <param name="hwnd">The window handle.</param> /// <param name="width">The width of the window.</param> /// <param name="height">The height of the window.</param> public RenderTarget(IntPtr hwnd, double width, double height) { Direct2DFactory = PerspexLocator.Current.GetService <Factory>(); DirectWriteFactory = PerspexLocator.Current.GetService <DwFactory>(); RenderTargetProperties renderTargetProperties = new RenderTargetProperties { }; HwndRenderTargetProperties hwndProperties = new HwndRenderTargetProperties { Hwnd = hwnd, PixelSize = new Size2((int)width, (int)height), PresentOptions = PresentOptions.Immediately, }; _renderTarget = new WindowRenderTarget( Direct2DFactory, renderTargetProperties, hwndProperties); }
public RenderTarget Create(Factory factory, Graphics g, Map map) { var wicBitmap = new WICBitmap(_wicFactory, map.Size.Width, map.Size.Height, SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnDemand); var rtp = new RenderTargetProperties(RenderTargetType.Default, new D2D1PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); var res = new WicRenderTarget(factory, wicBitmap, rtp) { Tag = wicBitmap }; res.BeginDraw(); res.Clear(SharpDX.Color.Transparent); return(res); }
public TextRenderTarget(RenderTargetTexture renderTargetTexture) { var surface = renderTargetTexture.AsSurface(); var factory2D = new SlimDX.Direct2D.Factory(SlimDX.Direct2D.FactoryType.SingleThreaded); var renderTargetProperties = new RenderTargetProperties { PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied), Usage = RenderTargetUsage.None, HorizontalDpi = 96, VerticalDpi = 96, MinimumFeatureLevel = FeatureLevel.Direct3D10, Type = RenderTargetType.Default }; mRenderTarget = RenderTarget.FromDXGI(factory2D, surface, renderTargetProperties); mBrush = new SolidColorBrush(mRenderTarget, new Color4(1, 1, 1)); CreateTextFormat(); }
private static bool RenderTargetSupportsGDI(RenderTarget rt) { var propertiesToSupport = new RenderTargetProperties( RenderTargetType.Default, new PixelFormat( Format.B8G8R8A8_UNORM, AlphaMode.Ignore), 96, 96, RenderTargetUsage.GdiCompatible, FeatureLevel.Default); if (rt.IsSupported(propertiesToSupport)) return true; propertiesToSupport = new RenderTargetProperties( RenderTargetType.Default, new PixelFormat( Format.B8G8R8A8_UNORM, AlphaMode.Premultiplied), 96, 96, RenderTargetUsage.GdiCompatible, FeatureLevel.Default); return rt.IsSupported(propertiesToSupport); }
private void SetRenderMode(RenderModes rm) { lock (renderSyncObject) { renderMode = rm; //if (!IsInitialized && !isInitializing) // return; //clean up objects that will be invalid after RenderTarget change if (dcRenderTarget != null) { dcRenderTarget.Dispose(); dcRenderTarget = null; } if (hwndRenderTarget != null) { hwndRenderTarget.Dispose(); hwndRenderTarget = null; } if (bitmapRenderTarget != null) { bitmapRenderTarget.Dispose(); bitmapRenderTarget = null; } //bitmap = null; //the bitmap created in dc render target can't be used in hwnd render target // Create the screen render target RECT cRect; User32.GetClientRect(fWindowHandle, out cRect); var size = new SizeU((uint)cRect.Width, (uint)cRect.Height); PixelFormat pFormat = new PixelFormat(Format.B8G8R8A8_UNORM, AlphaMode.Ignore); var props = new RenderTargetProperties {PixelFormat = pFormat, Usage = RenderTargetUsage.GdiCompatible}; //if (renderMode == RenderModes.DCRenderTarget || renderMode == RenderModes.BitmapRenderTargetOnPaint) //{ // dcRenderTarget = d2DFactory.CreateDCRenderTarget(props); // if (renderMode == RenderModes.BitmapRenderTargetOnPaint) // { // bitmapRenderTarget = // dcRenderTarget.CreateCompatibleRenderTarget( // CompatibleRenderTargetOptions.GdiCompatible, // new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); // } // render = null; //} //else { HwndRenderTargetProperties hProps = new HwndRenderTargetProperties(fWindowHandle, size, PresentOptions.RetainContents); hwndRenderTarget = d2DFactory.CreateHwndRenderTarget(props, hProps); //if (renderMode == RenderModes.BitmapRenderTargetRealTime) //{ // bitmapRenderTarget = // hwndRenderTarget.CreateCompatibleRenderTarget( // CompatibleRenderTargetOptions.GdiCompatible, // new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); //} //render = RenderSceneInBackground; } } }
private void SetRenderMode(RenderModes rm) { lock (renderSyncObject) { renderMode = rm; if (!IsInitialized && !isInitializing) return; //clean up objects that will be invalid after RenderTarget change if (dcRenderTarget != null) { dcRenderTarget.Dispose(); dcRenderTarget = null; } if (hwndRenderTarget != null) { hwndRenderTarget.Dispose(); hwndRenderTarget = null; } if (bitmapRenderTarget != null) { bitmapRenderTarget.Dispose(); bitmapRenderTarget = null; } peelings.Clear(); bitmap = null; //the bitmap created in dc render target can't be used in hwnd render target // Create the screen render target var size = new SizeU((uint)ClientSize.Width, (uint)ClientSize.Height); var props = new RenderTargetProperties { PixelFormat = new PixelFormat( Format.B8G8R8A8UNorm, AlphaMode.Ignore), Usage = RenderTargetUsages.GdiCompatible }; if (renderMode == RenderModes.DCRenderTarget || renderMode == RenderModes.BitmapRenderTargetOnPaint) { dcRenderTarget = d2DFactory.CreateDCRenderTarget(props); if (renderMode == RenderModes.BitmapRenderTargetOnPaint) { bitmapRenderTarget = dcRenderTarget.CreateCompatibleRenderTarget( CompatibleRenderTargetOptions.GdiCompatible, new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); } render = null; } else { hwndRenderTarget = d2DFactory.CreateHwndRenderTarget( props, new HwndRenderTargetProperties(Handle, size, Microsoft.WindowsAPICodePack.DirectX.Direct2D1.PresentOptions.RetainContents)); if (renderMode == RenderModes.BitmapRenderTargetRealTime) { bitmapRenderTarget = hwndRenderTarget.CreateCompatibleRenderTarget( CompatibleRenderTargetOptions.GdiCompatible, new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); } render = RenderSceneInBackground; } //move all shapes to new rendertarget and refresh foreach (var shape in drawingShapes) { shape.Bitmap = Bitmap; shape.RenderTarget = RenderTarget; } RefreshAll(); } }
/// <summary> /// This method creates the render target and all associated D2D and DWrite resources /// </summary> void CreateDeviceResources() { // Only calls if resources have not been initialize before if (renderTarget == null) { // Create the render target SizeU size = new SizeU((uint)host.ActualWidth, (uint)host.ActualHeight); RenderTargetProperties props = new RenderTargetProperties(); HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(host.Handle, size, PresentOptions.None); renderTarget = d2dFactory.CreateHwndRenderTarget(props, hwndProps); // Create the black brush for text blackBrush = renderTarget.CreateSolidColorBrush(new ColorF(0 ,0, 0, 1)); inlineImage = new ImageInlineObject(renderTarget, wicFactory, "TextInlineImage.img1.jpg"); TextRange textRange = new TextRange(14, 1); textLayout.SetInlineObject(inlineImage, textRange); } }
void CreateDeviceResources() { uint width = (uint) host.ActualWidth; uint height = (uint) host.ActualHeight; // If we don't have a device, need to create one now and all // accompanying D3D resources. CreateDevice(); DXGIFactory dxgiFactory = DXGIFactory.CreateFactory(); SwapChainDescription swapDesc = new SwapChainDescription(); swapDesc.BufferDescription.Width = width; swapDesc.BufferDescription.Height = height; swapDesc.BufferDescription.Format = Format.R8G8B8A8_UNORM; swapDesc.BufferDescription.RefreshRate.Numerator = 60; swapDesc.BufferDescription.RefreshRate.Denominator = 1; swapDesc.SampleDescription.Count = 1; swapDesc.SampleDescription.Quality = 0; swapDesc.BufferUsage = UsageOption.RenderTargetOutput; swapDesc.BufferCount = 1; swapDesc.OutputWindowHandle = host.Handle; swapDesc.Windowed = true; swapChain = dxgiFactory.CreateSwapChain( device, swapDesc); // Create rasterizer state object RasterizerDescription rsDesc = new RasterizerDescription(); rsDesc.AntialiasedLineEnable = false; rsDesc.CullMode = CullMode.None; rsDesc.DepthBias = 0; rsDesc.DepthBiasClamp = 0; rsDesc.DepthClipEnable = true; rsDesc.FillMode = D3D10.FillMode.Solid; rsDesc.FrontCounterClockwise = false; // Must be FALSE for 10on9 rsDesc.MultisampleEnable = false; rsDesc.ScissorEnable = false; rsDesc.SlopeScaledDepthBias = 0; rasterizerState = device.CreateRasterizerState( rsDesc); device.RS.SetState( rasterizerState ); // If we don't have a D2D render target, need to create all of the resources // required to render to one here. // Ensure that nobody is holding onto one of the old resources device.OM.SetRenderTargets(new RenderTargetView[] {null}); InitializeDepthStencil(width, height); // Create views on the RT buffers and set them on the device RenderTargetViewDescription renderDesc = new RenderTargetViewDescription(); renderDesc.Format = Format.R8G8B8A8_UNORM; renderDesc.ViewDimension = RenderTargetViewDimension.Texture2D; renderDesc.Texture2D.MipSlice = 0; using (D3DResource spBackBufferResource = swapChain.GetBuffer<D3DResource>(0)) { renderTargetView = device.CreateRenderTargetView( spBackBufferResource, renderDesc); } device.OM.SetRenderTargets(new RenderTargetView[] {renderTargetView}, depthStencilView); SetViewport(width, height); // Create a D2D render target which can draw into the surface in the swap chain RenderTargetProperties props = new RenderTargetProperties( RenderTargetType.Default, new PixelFormat(Format.Unknown, AlphaMode.Premultiplied), 96, 96, RenderTargetUsage.None, FeatureLevel.Default); // Allocate a offscreen D3D surface for D2D to render our 2D content into Texture2DDescription tex2DDescription; tex2DDescription.ArraySize = 1; tex2DDescription.BindFlags = BindFlag.RenderTarget | BindFlag.ShaderResource; tex2DDescription.CpuAccessFlags = CpuAccessFlag.Unspecified; tex2DDescription.Format = Format.R8G8B8A8_UNORM; tex2DDescription.Height = 4096; tex2DDescription.Width = 512; tex2DDescription.MipLevels = 1; tex2DDescription.MiscFlags = 0; tex2DDescription.SampleDescription.Count = 1; tex2DDescription.SampleDescription.Quality = 0; tex2DDescription.Usage = Usage.Default; offscreenTexture = device.CreateTexture2D(tex2DDescription); using (Surface dxgiSurface = offscreenTexture.GetDXGISurface()) { // Create a D2D render target which can draw into our offscreen D3D surface renderTarget = d2DFactory.CreateDxgiSurfaceRenderTarget( dxgiSurface, props); } PixelFormat alphaOnlyFormat = new PixelFormat(Format.A8_UNORM, AlphaMode.Premultiplied); opacityRenderTarget = renderTarget.CreateCompatibleRenderTarget(CompatibleRenderTargetOptions.None, alphaOnlyFormat); // Load pixel shader // Open precompiled vertex shader // This file was compiled using DirectX's SDK Shader compilation tool: // fxc.exe /T fx_4_0 /Fo SciFiText.fxo SciFiText.fx shader = LoadResourceShader(device, "SciFiTextDemo.SciFiText.fxo"); // Obtain the technique technique = shader.GetTechniqueByName("Render"); // Obtain the variables worldMatrixVariable = shader.GetVariableByName("World").AsMatrix(); viewMatrixVariable = shader.GetVariableByName("View").AsMatrix(); projectionMarixVariable = shader.GetVariableByName("Projection").AsMatrix(); diffuseVariable = shader.GetVariableByName("txDiffuse").AsShaderResource(); // Create the input layout PassDescription passDesc = new PassDescription(); passDesc = technique.GetPassByIndex(0).Description; vertexLayout = device.CreateInputLayout( inputLayoutDescriptions, passDesc.InputAssemblerInputSignature, passDesc.InputAssemblerInputSignatureSize ); // Set the input layout device.IA.SetInputLayout( vertexLayout ); IntPtr verticesDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(VertexArray.VerticesInstance)); Marshal.StructureToPtr(VertexArray.VerticesInstance, verticesDataPtr, true); BufferDescription bd = new BufferDescription(); bd.Usage = Usage.Default; bd.ByteWidth = (uint) Marshal.SizeOf(VertexArray.VerticesInstance); bd.BindFlags = BindFlag.VertexBuffer; bd.CpuAccessFlags = CpuAccessFlag.Unspecified; bd.MiscFlags = ResourceMiscFlag.Undefined; SubresourceData InitData = new SubresourceData() { SysMem = verticesDataPtr }; vertexBuffer = device.CreateBuffer( bd, InitData ); Marshal.FreeHGlobal(verticesDataPtr); // Set vertex buffer uint stride = (uint) Marshal.SizeOf(typeof (SimpleVertex)); uint offset = 0; device.IA.SetVertexBuffers( 0, new D3DBuffer[] {vertexBuffer}, new uint[] {stride}, new uint[] {offset} ); IntPtr indicesDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(VertexArray.IndicesInstance)); Marshal.StructureToPtr(VertexArray.IndicesInstance, indicesDataPtr, true); bd.Usage = Usage.Default; bd.ByteWidth = (uint) Marshal.SizeOf(VertexArray.IndicesInstance); bd.BindFlags = BindFlag.IndexBuffer; bd.CpuAccessFlags = 0; bd.MiscFlags = 0; InitData.SysMem = indicesDataPtr; facesIndexBuffer = device.CreateBuffer( bd, InitData ); Marshal.FreeHGlobal(indicesDataPtr); // Set primitive topology device.IA.SetPrimitiveTopology(PrimitiveTopology.TriangleList); // Convert the D2D texture into a Shader Resource View textureResourceView = device.CreateShaderResourceView( offscreenTexture); // Initialize the world matrices worldMatrix = Matrix4x4F.Identity; // Initialize the view matrix Vector3F Eye = new Vector3F(0.0f, 0.0f, 13.0f); Vector3F At = new Vector3F(0.0f, -3.5f, 45.0f); Vector3F Up = new Vector3F(0.0f, 1.0f, 0.0f); viewMatrix = Camera.MatrixLookAtLH(Eye, At, Up); // Initialize the projection matrix projectionMatrix = Camera.MatrixPerspectiveFovLH( (float) Math.PI*0.1f, width/(float) height, 0.1f, 100.0f); // Update Variables that never change viewMatrixVariable.Matrix = viewMatrix; projectionMarixVariable.Matrix = projectionMatrix; GradientStop[] gradientStops = { new GradientStop(0.0f, new ColorF(Colors.Yellow)), new GradientStop(1.0f, new ColorF(Colors.Black)) }; GradientStopCollection spGradientStopCollection = renderTarget.CreateGradientStopCollection( gradientStops, Gamma.Gamma_22, ExtendMode.Clamp); // Create a linear gradient brush for text textBrush = renderTarget.CreateLinearGradientBrush( new LinearGradientBrushProperties(new Point2F(0, 0), new Point2F(0, -2048)), spGradientStopCollection ); }