public static D2D.Brush ToD2DBrush(this Media.Brush brush, global::SharpDX.Direct2D1.RenderTarget target) { if (brush is Media.SolidColorBrush solid) { return(new global::SharpDX.Direct2D1.SolidColorBrush(target, solid.Color.ToColor4())); } else if (brush is Media.LinearGradientBrush linear) { return(new D2D.LinearGradientBrush(target, new D2D.LinearGradientBrushProperties() { StartPoint = linear.StartPoint.ToVector2(), EndPoint = linear.EndPoint.ToVector2() }, new D2D.GradientStopCollection ( target, linear.GradientStops.Select(x => new D2D.GradientStop() { Color = x.Color.ToColor4(), Position = (float)x.Offset }).ToArray(), linear.ColorInterpolationMode.ToD2DColorInterpolationMode(), linear.SpreadMethod.ToD2DExtendMode() ) )); } #if NETFX_CORE || WINUI #else else if (brush is Media.RadialGradientBrush radial) { return(new D2D.RadialGradientBrush(target, new D2D.RadialGradientBrushProperties() { Center = radial.Center.ToVector2(), GradientOriginOffset = radial.GradientOrigin.ToVector2(), RadiusX = (float)radial.RadiusX, RadiusY = (float)radial.RadiusY }, new D2D.GradientStopCollection ( target, radial.GradientStops.Select(x => new D2D.GradientStop() { Color = x.Color.ToColor4(), Position = (float)x.Offset }).ToArray(), radial.ColorInterpolationMode.ToD2DColorInterpolationMode(), radial.SpreadMethod.ToD2DExtendMode() ))); } #endif else { throw new NotImplementedException("Brush does not support yet."); } }
/// <summary> /// Free used resources. /// </summary> private void CleanResources() { this.oxyRenderContext.ResetRenderTarget(null); this.d3d11Device?.ImmediateContext.ClearState(); this.d3d11Device?.ImmediateContext.Flush(); this.imageSource?.Dispose(); this.d3d9Device?.Dispose(); this.renderTarget?.Dispose(); this.d3d11Device?.Dispose(); this.imageSource = null; this.d3d9Device = null; this.renderTarget = null; this.d3d11Device = null; }
/// <summary> /// Initializes DirectX rendering resources. /// </summary> private void InitRendering() { double dpiScale = 1.0; // default value for 96 dpi var hwndTarget = PresentationSource.FromVisual(this).CompositionTarget as HwndTarget; if (hwndTarget != null) { dpiScale = hwndTarget.TransformToDevice.M11; } int surfWidth = (int)(this.viewport.Width < 0 ? 0 : Math.Ceiling(this.viewport.Width * dpiScale)); int surfHeight = (int)(this.viewport.Height < 0 ? 0 : Math.Ceiling(this.viewport.Height * dpiScale)); var windowHandle = (new System.Windows.Interop.WindowInteropHelper(System.Windows.Window.GetWindow(this))).Handle; this.d3d11Device = new D3D11Device( DriverType.Hardware, DeviceCreationFlags.BgraSupport, FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0, FeatureLevel.Level_9_3, FeatureLevel.Level_9_2, FeatureLevel.Level_9_1); var backBuffer = new Texture2D( this.d3d11Device, new Texture2DDescription { Height = surfHeight, Width = surfWidth, ArraySize = 1, MipLevels = 1, Format = Format.B8G8R8A8_UNorm, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.Shared, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, CpuAccessFlags = 0 }); var surface = backBuffer.QueryInterface<Surface>(); this.renderTarget = new RenderTarget( this.oxyRenderContext.D2DFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); this.d3d9Device = new D3D9Device( new Direct3D(), 0, D3D9DeviceType.Hardware, windowHandle, D3D9CreateFlags.HardwareVertexProcessing | D3D9CreateFlags.Multithreaded | D3D9CreateFlags.FpuPreserve, new D3D9PresentParameters(surfWidth, surfHeight) { PresentationInterval = D3D9PresentInterval.Immediate, DeviceWindowHandle = windowHandle, SwapEffect = D3D9SwapEffect.Discard, Windowed = true }); this.imageSource = new D3D11Image(this.d3d9Device, backBuffer); this.oxyRenderContext.ResetRenderTarget(this.renderTarget); }
/// <summary> /// Initializes DirectX rendering resources. /// </summary> private void InitRendering() { double dpiScale = 1.0; // default value for 96 dpi var hwndTarget = PresentationSource.FromVisual(this).CompositionTarget as HwndTarget; if (hwndTarget != null) { dpiScale = hwndTarget.TransformToDevice.M11; } int surfWidth = (int)(this.viewport.Width < 0 ? 0 : Math.Ceiling(this.viewport.Width * dpiScale)); int surfHeight = (int)(this.viewport.Height < 0 ? 0 : Math.Ceiling(this.viewport.Height * dpiScale)); var windowHandle = (new System.Windows.Interop.WindowInteropHelper(System.Windows.Window.GetWindow(this))).Handle; this.d3d11Device = new D3D11Device( DriverType.Hardware, DeviceCreationFlags.BgraSupport, FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0, FeatureLevel.Level_9_3, FeatureLevel.Level_9_2, FeatureLevel.Level_9_1); var backBuffer = new Texture2D( this.d3d11Device, new Texture2DDescription { Height = surfHeight, Width = surfWidth, ArraySize = 1, MipLevels = 1, Format = Format.B8G8R8A8_UNorm, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.Shared, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, CpuAccessFlags = 0 }); var surface = backBuffer.QueryInterface <Surface>(); this.renderTarget = new RenderTarget( this.oxyRenderContext.D2DFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); this.d3d9Device = new D3D9Device( new Direct3D(), 0, D3D9DeviceType.Hardware, windowHandle, D3D9CreateFlags.HardwareVertexProcessing | D3D9CreateFlags.Multithreaded | D3D9CreateFlags.FpuPreserve, new D3D9PresentParameters(surfWidth, surfHeight) { PresentationInterval = D3D9PresentInterval.Immediate, DeviceWindowHandle = windowHandle, SwapEffect = D3D9SwapEffect.Discard, Windowed = true }); this.imageSource = new D3D11Image(this.d3d9Device, backBuffer); this.oxyRenderContext.ResetRenderTarget(this.renderTarget); }