public DrawGeometry ( SharpDX.Direct2D1.Geometry geometry, Brush brush ) : void | ||
geometry | SharpDX.Direct2D1.Geometry | The geometry to draw. |
brush | Brush | The brush used to paint the geometry's stroke. |
Résultat | void |
public void RenderScatterGeometry(RenderTarget renderTarget) { double[] x = curve.X; double[] y = curve.Y; int length = x.Length; double xScale, xOffset, yScale, yOffset; xScale = graphToCanvas.Matrix.M11; xOffset = graphToCanvas.Matrix.OffsetX - this.xOffsetMarker; yScale = graphToCanvas.Matrix.M22; yOffset = graphToCanvas.Matrix.OffsetY - this.yOffsetMarker; bool[] include = curve.includeMarker; StrokeStyleProperties properties = new StrokeStyleProperties(); properties.LineJoin = LineJoin.MiterOrBevel; StrokeStyle strokeStyle = new StrokeStyle(renderTarget.Factory, properties); for (int i = 0; i < length; ++i) { if (include[i]) { renderTarget.Transform = (Matrix3x2)Matrix.Translation((float)(x[i] * xScale + xOffset), (float)(y[i] * yScale + yOffset), 0); renderTarget.FillGeometry(Geometry, FillBrush); renderTarget.DrawGeometry(Geometry, Brush, (float)StrokeThickness, strokeStyle); } } renderTarget.Transform = Matrix3x2.Identity; }
public static void DrawPolygon(this SharpDX.Direct2D1.RenderTarget target, ID2DPen pen, params PointF[] points) { if (points == null) { throw new ArgumentNullException(nameof(points)); } if (points.Length == 0) { return; } using (var path = new PathGeometry(target.Factory)) { using (var sink = path.Open()) { sink.BeginFigure(new RawVector2(points[0].X, points[0].Y), FigureBegin.Filled); var len = points.Length; for (var i = 1; i < len; ++i) { var pt = points[i]; sink.AddLine(new RawVector2(pt.X, pt.Y)); } sink.EndFigure(FigureEnd.Closed); sink.Close(); } target.DrawGeometry(path, pen.Brush.NativeBrush, pen.StrokeWidth, pen.StrokeStyle); } }
/// <summary> /// Draws a geometry. /// </summary> /// <param name="brush">The fill brush.</param> /// <param name="pen">The stroke pen.</param> /// <param name="geometry">The geometry.</param> public void DrawGeometry(IBrush brush, Pen pen, Avalonia.Media.Geometry geometry) { if (brush != null) { using (var d2dBrush = CreateBrush(brush, geometry.Bounds.Size)) { if (d2dBrush.PlatformBrush != null) { var impl = (GeometryImpl)geometry.PlatformImpl; _renderTarget.FillGeometry(impl.Geometry, d2dBrush.PlatformBrush); } } } if (pen != null) { using (var d2dBrush = CreateBrush(pen.Brush, geometry.GetRenderBounds(pen.Thickness).Size)) using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget)) { if (d2dBrush.PlatformBrush != null) { var impl = (GeometryImpl)geometry.PlatformImpl; _renderTarget.DrawGeometry(impl.Geometry, d2dBrush.PlatformBrush, (float)pen.Thickness, d2dStroke); } } } }
internal static async Task Render(CompositionEngine compositionEngine, SharpDX.Direct2D1.RenderTarget renderTarget, FrameworkElement rootElement, Jupiter.Shapes.Path path) { var rect = path.GetBoundingRect(rootElement).ToSharpDX(); var fill = await path.Fill.ToSharpDX(renderTarget, rect); var stroke = await path.Stroke.ToSharpDX(renderTarget, rect); //var layer = new D2D.Layer(renderTarget); //var layerParameters = new D2D.LayerParameters(); //layerParameters.ContentBounds = rect; var oldTransform = renderTarget.Transform; renderTarget.Transform = new Matrix3x2( 1, 0, 0, 1, rect.Left, rect.Top); //renderTarget.PushLayer(ref layerParameters, layer); var d2dGeometry = path.Data.ToSharpDX(compositionEngine.D2DFactory, rect); if (fill != null) { renderTarget.FillGeometry(d2dGeometry, fill, null); } if (stroke != null && path.StrokeThickness > 0) { renderTarget.DrawGeometry( d2dGeometry, stroke, (float)path.StrokeThickness, path.GetStrokeStyle(compositionEngine.D2DFactory)); } //if (path.StrokeThickness > 0 && // stroke != null) //{ // var halfThickness = (float)(path.StrokeThickness * 0.5); // roundedRect.Rect = rect.Eroded(halfThickness); // if (fill != null) // { // renderTarget.FillRoundedRectangle(roundedRect, fill); // } // renderTarget.DrawRoundedRectangle( // roundedRect, // stroke, // (float)path.StrokeThickness, // path.GetStrokeStyle(compositionEngine.D2DFactory)); //} //else //{ // renderTarget.FillRoundedRectangle(roundedRect, fill); //} //renderTarget.PopLayer(); renderTarget.Transform = oldTransform; }
public void DrawPath(Seal.Geometries.IPath path, Seal.Media.IBrush brush) { var p = path as Path; //Здесь стоит проверка, не решит ли пользователь смешать два движка сразу. //Тогда теоретически может быть так, что p будет null, так что лучше проверить. var pBrush = brush as Media.Brush; if (p != null && pBrush != null) { _renderTarget.DrawGeometry(p.D2DPath, pBrush.D2DBrush, 1); } }
//--------------------------------------------------------------------------------------------------------- /// <summary> /// Рисование арки /// </summary> //--------------------------------------------------------------------------------------------------------- public override void Draw() { Direct2D.RenderTarget render_target = XDirect2DManager.D2DRenderTarget; if (mIsFilled) { render_target.FillGeometry(mD2DGeometry, mFill.D2DBrush); } if (mIsStroked) { render_target.DrawGeometry(mD2DGeometry, mStroke.Brush.D2DBrush, mStroke.Thickness, mStroke.mD2DStrokeStyle); } }
public override Result DrawGlyphRun(object clientDrawingContext, float baselineOriginX, float baselineOriginY, D2D1.MeasuringMode measuringMode, DW.GlyphRun glyphRun, DW.GlyphRunDescription glyphRunDescription, ComObject clientDrawingEffect) { using (var pathGeometry = new D2D1.PathGeometry(_renderTarget.Factory)) { using (var geometrySink = pathGeometry.Open()) using (var fontFace = glyphRun.FontFace) { if (glyphRun.Indices.Length > 0) { fontFace.GetGlyphRunOutline( glyphRun.FontSize, glyphRun.Indices, glyphRun.Advances, glyphRun.Offsets, glyphRun.Indices.Length, glyphRun.IsSideways, glyphRun.BidiLevel % 2 != 0, geometrySink ); } geometrySink.Close(); } var matrix = new Matrix3x2() { M11 = 1, M12 = 0, M21 = 0, M22 = 1, M31 = baselineOriginX, M32 = baselineOriginY }; var sw = _renderTarget.StrokeWidth; using (var transformedGeometry = new D2D1.TransformedGeometry(_renderTarget.Factory, pathGeometry, matrix)) { _renderTarget.StrokeWidth = _strokeWidth; _renderTarget.DrawGeometry(transformedGeometry, _strokeBrush); _renderTarget.FillGeometry(transformedGeometry, _fillBrush); } _renderTarget.StrokeWidth = sw; } return(SharpDX.Result.Ok); }
/// <summary> /// Draws the given geometry. /// </summary> public void DrawGeometry(Geometry2DResourceBase geometry, BrushResource brush, float strokeWidth = 1f) { if (m_renderTarget == null) { return; } geometry.EnsureNotNullOrDisposed(nameof(geometry)); brush.EnsureNotNull(nameof(brush)); strokeWidth.EnsurePositive(nameof(strokeWidth)); m_renderTarget.DrawGeometry( geometry.GetGeometry(), brush.GetBrush(m_device), strokeWidth); }
public static void DrawQuadraticBezier(this SharpDX.Direct2D1.RenderTarget target, ID2DPen pen, float x1, float y1, float cx, float cy, float x2, float y2) { using (var path = new PathGeometry(target.Factory)) { using (var sink = path.Open()) { var bezier = new QuadraticBezierSegment { Point1 = new RawVector2(cx, cy), Point2 = new RawVector2(x2, y2) }; sink.BeginFigure(new RawVector2(x1, y1), FigureBegin.Filled); sink.AddQuadraticBezier(bezier); sink.EndFigure(FigureEnd.Open); sink.Close(); } target.DrawGeometry(path, pen.Brush.NativeBrush, pen.StrokeWidth, pen.StrokeStyle); } }
public static void DrawPolygon(D2D1.RenderTarget renderTarget, D2D1.Factory factory, IPolygon pol, D2D1.Brush brush, D2D1.Brush pen, float penWidth, D2D1.StrokeStyle penStrokeStyle, bool clip, Map map) { if (pol.ExteriorRing == null) { return; } Vector2[] points; var startPoint = TransformToImage(pol.ExteriorRing, map, out points); if (points.Length > 1) { using (var geom = new D2D1.PathGeometry(factory)) { using (var gs = geom.Open()) { gs.SetFillMode(D2D1.FillMode.Alternate); gs.BeginFigure(startPoint, D2D1.FigureBegin.Filled); gs.AddLines(points); gs.EndFigure(D2D1.FigureEnd.Closed); for (var i = 0; i < pol.NumInteriorRings; i++) { startPoint = TransformToImage(pol.GetInteriorRingN(i), map, out points); if (points.Length > 1) { gs.BeginFigure(startPoint, D2D1.FigureBegin.Filled); gs.AddLines(points); gs.EndFigure(D2D1.FigureEnd.Closed); } } gs.Close(); } if (brush != null) { renderTarget.FillGeometry(geom, brush); } if (pen != null) { renderTarget.DrawGeometry(geom, pen, penWidth, penStrokeStyle); } } } }
/// <summary> /// Renders a LineString to the map. /// </summary> /// <param name="renderTarget">The render target</param> /// <param name="factory">The factory to create shapes</param> /// <param name="line">LineString to render</param> /// <param name="pen">Pen style used for rendering</param> /// <param name="map">Map reference</param> /// <param name="offset">Offset by which line will be moved to right</param> public static void DrawLineString(D2D1.RenderTarget renderTarget, D2D1.Factory factory, ILineString line, D2D1.Brush pen, float penWidth, D2D1.StrokeStyle penStrokeStyle, Map map, float offset) { var points = TransformToImage(line, map); if (points.Length > 1) { using (var geom = new D2D1.PathGeometry(factory)) { using (var gs = geom.Open()) { gs.BeginFigure(points[0], D2D1.FigureBegin.Filled); gs.AddLines(points); gs.EndFigure(D2D1.FigureEnd.Open); gs.Close(); } renderTarget.DrawGeometry(geom, pen, penWidth, penStrokeStyle); } } }
public void Render2D(RenderTarget renderTarget, Brush brush) { #region Update Color Brush // if the brush is dirty renew it if (m_isLineColorBrushDirty && !m_useDefaultColor) { // clean the color brush if (m_lineColorBrush != null) m_lineColorBrush.Dispose(); // allocate a new one m_lineColorBrush = new SolidColorBrush(renderTarget, m_lineColor); // clean the flag m_isLineColorBrushDirty = false; } #endregion if(isGeometryDirty) { if (m_sharpGeometry != null) m_sharpGeometry.Dispose(); m_sharpGeometry = new PathGeometry(renderTarget.Factory); using(GeometrySink Geo_Sink = m_sharpGeometry.Open()) { // draw the rectangle Geo_Sink.BeginFigure(m_start.GetVector2(), FigureBegin.Filled); Geo_Sink.AddLine(new SharpDX.Vector2(m_start.x, m_end.y)); Geo_Sink.AddLine(m_end.GetVector2()); Geo_Sink.AddLine(new SharpDX.Vector2(m_end.x, m_start.y)); Geo_Sink.EndFigure(FigureEnd.Closed); Geo_Sink.Close(); } isGeometryDirty = false; } // check if we use other color if (m_useDefaultColor || m_lineColorBrush == null) { renderTarget.DrawGeometry(m_sharpGeometry, brush, m_lineWidth); } else { renderTarget.DrawGeometry(m_sharpGeometry, m_lineColorBrush, m_lineWidth); } }
/// <summary> /// render the circle to specific render target of Direct2D1 /// </summary> /// <param name="renderTarget"></param> public void Render2D(RenderTarget renderTarget, SharpDX.Direct2D1.Brush brush) { // check if the geometry is dirty if (isGeometryDirty) { // dispose the old geometry if (TriangleGeometry != null) { TriangleGeometry.Dispose(); } // create a new one TriangleGeometry = new PathGeometry(renderTarget.Factory); // fill the geometry struct using (GeometrySink Geo_Sink = TriangleGeometry.Open()) { // create the figure Geo_Sink.BeginFigure(mp_P1, FigureBegin.Filled); Geo_Sink.AddLine(mp_P2); Geo_Sink.AddLine(mp_P3); Geo_Sink.EndFigure(FigureEnd.Closed); // close the mesh Geo_Sink.Close(); } // set the geometry that is the final isGeometryDirty = false; } // draw the wireframe of the triangle renderTarget.DrawGeometry(TriangleGeometry, brush, _LineWidth); if (fillTheTriangle) { // check if we must renew the brush if (fillColorDirty) { // dispose the old brush if (fillColorBrush != null) fillColorBrush.Dispose(); // create the new one fillColorBrush = new SolidColorBrush(renderTarget, fillColor); // set that the color is the correct fillColorDirty = false; } // fill the triangle renderTarget.FillGeometry(TriangleGeometry, fillColorBrush); } }
public void DrawPath(IEnumerable <PathOp> ops, Pen pen = null, Brush brush = null) { var bb = new BoundingBoxBuilder(); var s = new D2D1.PathGeometry(factories.D2DFactory); var figureDepth = 0; using (var sink = s.Open()) { foreach (var op in ops) { if (op is MoveTo) { while (figureDepth > 0) { sink.EndFigure(D2D1.FigureEnd.Open); figureDepth--; } var mt = ((MoveTo)op); sink.BeginFigure(Conversions.ToVector2(mt.Point), D2D1.FigureBegin.Filled); figureDepth++; bb.Add(mt.Point); } else if (op is LineTo) { var lt = ((LineTo)op); sink.AddLine(Conversions.ToVector2(lt.Point)); bb.Add(lt.Point); } else if (op is ArcTo) { var ar = ((ArcTo)op); // TODO: Direct2D Arcs //sink.AddArc (new D2D1.ArcSegment { // Size = Conversions.ToSize2F (ar.Radius), // Point = Conversions.ToVector2 (ar.Point), // SweepDirection = ar.SweepClockwise ? D2D1.SweepDirection.Clockwise : D2D1.SweepDirection.CounterClockwise, //}); sink.AddLine(Conversions.ToVector2(ar.Point)); bb.Add(ar.Point); } else if (op is CurveTo) { var ct = ((CurveTo)op); sink.AddBezier(new D2D1.BezierSegment { Point1 = Conversions.ToVector2(ct.Control1), Point2 = Conversions.ToVector2(ct.Control2), Point3 = Conversions.ToVector2(ct.Point), }); bb.Add(ct.Point); bb.Add(ct.Control1); bb.Add(ct.Control2); } else if (op is ClosePath) { sink.EndFigure(D2D1.FigureEnd.Closed); figureDepth--; } else { // TODO: More path operations } } while (figureDepth > 0) { sink.EndFigure(D2D1.FigureEnd.Open); figureDepth--; } sink.Close(); } var p = GetBrush(pen); var b = GetBrush(bb.BoundingBox, brush); if (b != null) { renderTarget.FillGeometry(s, b); } if (p != null) { renderTarget.DrawGeometry(s, p, (float)pen.Width, GetStrokeStyle(pen)); } }
public void Render2D(RenderTarget renderTarget, Brush brush) { #region Update Color Brush // if the brush is dirty renew it if (m_isLineColorBrushDirty && !m_useDefaultColor) { // clean the color brush if (m_lineColorBrush != null) m_lineColorBrush.Dispose(); // allocate a new one m_lineColorBrush = new SolidColorBrush(renderTarget, m_lineColor); // clean the flag m_isLineColorBrushDirty = false; } #endregion if (m_path.Count > 0) { // Update geometry if (isGeometryDirty) { if (m_sharpGeometry != null) m_sharpGeometry.Dispose(); m_sharpGeometry = new PathGeometry(renderTarget.Factory); using (GeometrySink Geo_Sink = m_sharpGeometry.Open()) { int count = m_path.Count; // create the path Geo_Sink.BeginFigure(m_path[0].GetVector2(), FigureBegin.Filled); for (int i = 1; i < count; i++) { Geo_Sink.AddLine(m_path[i].GetVector2()); } Geo_Sink.EndFigure(FigureEnd.Open); Geo_Sink.Close(); } isGeometryDirty = false; } // check if we use other color if (m_useDefaultColor || m_lineColorBrush == null) { renderTarget.DrawGeometry(m_sharpGeometry, brush, m_lineWidth); } else { renderTarget.DrawGeometry(m_sharpGeometry, m_lineColorBrush, m_lineWidth); } } }
public static void DrawPath(this SharpDX.Direct2D1.RenderTarget target, ID2DPen pen, D2DPathData path) { target.DrawGeometry(path.NativeGeometry, pen.Brush.NativeBrush, pen.StrokeWidth, pen.StrokeStyle); }
public override void Render(RenderTarget renderTarget) { if (!IsVisible) return; CurrentBorderBrush.Resource.Opacity = this.Opacity; CurrentBackgroundBrush.Resource.Opacity = this.Opacity; renderTarget.Transform = this.Transform; //TODO: Check if we need a second rect for this... renderTarget.PushAxisAlignedClip(this.ClippingRectangle, AntialiasMode.Aliased); try { if (DrawBackground) { renderTarget.FillGeometry(_backgroundGeometry, CurrentBackgroundBrush.Resource); //if (CurrentBitmap != null) // renderTarget.DrawBitmap(CurrentBitmap.Resource, Opacity, BitmapInterpolationMode.Linear); } if (DrawBorder) renderTarget.DrawGeometry(_backgroundGeometry, CurrentBorderBrush.Resource); if (DrawText && !String.IsNullOrEmpty(_text)) renderTarget.DrawTextLayout(new DrawingPointF(TextIndent, 0f), RenderedText, CurrentFontBrush.Resource); base.Render(renderTarget); } finally { renderTarget.PopAxisAlignedClip(); } }
public void Run() { var form = new RenderForm("2d and 3d combined...it's like magic"); form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) form.Close(); }; // DirectX DXGI 1.1 factory var factory1 = new Factory1(); // The 1st graphics adapter var adapter1 = factory1.GetAdapter1(0); // --------------------------------------------------------------------------------------------- // Setup direct 3d version 11. It's context will be used to combine the two elements // --------------------------------------------------------------------------------------------- var description = new SwapChainDescription { BufferCount = 1, ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, Flags = SwapChainFlags.AllowModeSwitch }; Device11 device11; SwapChain swapChain; Device11.CreateWithSwapChain(adapter1, DeviceCreationFlags.None, description, out device11, out swapChain); // create a view of our render target, which is the backbuffer of the swap chain we just created RenderTargetView renderTargetView; using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0)) renderTargetView = new RenderTargetView(device11, resource); // setting a viewport is required if you want to actually see anything var context = device11.ImmediateContext; var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height); context.OutputMerger.SetTargets(renderTargetView); context.Rasterizer.SetViewports(viewport); // // Create the DirectX11 texture2D. This texture will be shared with the DirectX10 device. // // The DirectX10 device will be used to render text onto this texture. // DirectX11 will then draw this texture (blended) onto the screen. // The KeyedMutex flag is required in order to share this resource between the two devices. var textureD3D11 = new Texture2D(device11, new Texture2DDescription { Width = form.ClientSize.Width, Height = form.ClientSize.Height, MipLevels = 1, ArraySize = 1, Format = Format.B8G8R8A8_UNorm, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.SharedKeyedmutex }); // --------------------------------------------------------------------------------------------- // Setup a direct 3d version 10.1 adapter // --------------------------------------------------------------------------------------------- var device10 = new Device10(adapter1, SharpDX.Direct3D10.DeviceCreationFlags.BgraSupport, FeatureLevel.Level_10_0); // --------------------------------------------------------------------------------------------- // Setup Direct 2d // --------------------------------------------------------------------------------------------- // Direct2D Factory var factory2D = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded, DebugLevel.Information); // Here we bind the texture we've created on our direct3d11 device through the direct3d10 // to the direct 2d render target.... var sharedResource = textureD3D11.QueryInterface<SharpDX.DXGI.Resource>(); var textureD3D10 = device10.OpenSharedResource<SharpDX.Direct3D10.Texture2D>(sharedResource.SharedHandle); var surface = textureD3D10.AsSurface(); var rtp = new RenderTargetProperties { MinLevel = SharpDX.Direct2D1.FeatureLevel.Level_10, Type = RenderTargetType.Hardware, PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied) }; var renderTarget2D = new RenderTarget(factory2D, surface, rtp); var solidColorBrush = new SolidColorBrush(renderTarget2D, Colors.Red); // --------------------------------------------------------------------------------------------------- // Setup the rendering data // --------------------------------------------------------------------------------------------------- // Load Effect. This includes both the vertex and pixel shaders. // Also can include more than one technique. ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile( "effectDx11.fx", "fx_5_0", ShaderFlags.EnableStrictness); var effect = new Effect(device11, shaderByteCode); // create triangle vertex data, making sure to rewind the stream afterward var verticesTriangle = new DataStream(VertexPositionColor.SizeInBytes * 3, true, true); verticesTriangle.Write(new VertexPositionColor(new Vector3(0.0f, 0.5f, 0.5f),new Color4(1.0f, 0.0f, 0.0f, 1.0f))); verticesTriangle.Write(new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.5f),new Color4(0.0f, 1.0f, 0.0f, 1.0f))); verticesTriangle.Write(new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.5f),new Color4(0.0f, 0.0f, 1.0f, 1.0f))); verticesTriangle.Position = 0; // create the triangle vertex layout and buffer var layoutColor = new InputLayout(device11, effect.GetTechniqueByName("Color").GetPassByIndex(0).Description.Signature, VertexPositionColor.inputElements); var vertexBufferColor = new Buffer(device11, verticesTriangle, (int)verticesTriangle.Length, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0); verticesTriangle.Close(); // create overlay vertex data, making sure to rewind the stream afterward // Top Left of screen is -1, +1 // Bottom Right of screen is +1, -1 var verticesText = new DataStream(VertexPositionTexture.SizeInBytes * 4, true, true); verticesText.Write(new VertexPositionTexture(new Vector3(-1, 1, 0),new Vector2(0, 0f))); verticesText.Write(new VertexPositionTexture(new Vector3(1, 1, 0),new Vector2(1, 0))); verticesText.Write(new VertexPositionTexture(new Vector3(-1, -1, 0),new Vector2(0, 1))); verticesText.Write(new VertexPositionTexture(new Vector3(1, -1, 0),new Vector2(1, 1))); verticesText.Position = 0; // create the overlay vertex layout and buffer var layoutOverlay = new InputLayout(device11, effect.GetTechniqueByName("Overlay").GetPassByIndex(0).Description.Signature, VertexPositionTexture.inputElements); var vertexBufferOverlay = new Buffer(device11, verticesText, (int)verticesText.Length, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0); verticesText.Close(); // Think of the shared textureD3D10 as an overlay. // The overlay needs to show the 2d content but let the underlying triangle (or whatever) // show thru, which is accomplished by blending. var bsd = new BlendStateDescription(); bsd.RenderTarget[0].IsBlendEnabled = true; bsd.RenderTarget[0].SourceBlend = BlendOption.SourceColor; bsd.RenderTarget[0].DestinationBlend = BlendOption.BlendFactor; bsd.RenderTarget[0].BlendOperation = BlendOperation.Add; bsd.RenderTarget[0].SourceAlphaBlend = BlendOption.One; bsd.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero; bsd.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add; bsd.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; var blendStateTransparent = new BlendState(device11, bsd); // --------------------------------------------------------------------------------------------------- // Create and tesselate an ellipse // --------------------------------------------------------------------------------------------------- var center = new DrawingPointF(form.ClientSize.Width/2.0f, form.ClientSize.Height/2.0f); var ellipse = new EllipseGeometry(factory2D, new Ellipse(center, form.ClientSize.Width / 2.0f, form.ClientSize.Height / 2.0f)); // Populate a PathGeometry from Ellipse tessellation var tesselatedGeometry = new PathGeometry(factory2D); _geometrySink = tesselatedGeometry.Open(); // Force RoundLineJoin otherwise the tesselated looks buggy at line joins _geometrySink.SetSegmentFlags(PathSegment.ForceRoundLineJoin); // Tesselate the ellipse to our TessellationSink ellipse.Tessellate(1, this); _geometrySink.Close(); // --------------------------------------------------------------------------------------------------- // Acquire the mutexes. These are needed to assure the device in use has exclusive access to the surface // --------------------------------------------------------------------------------------------------- var device10Mutex = textureD3D10.QueryInterface<KeyedMutex>(); var device11Mutex = textureD3D11.QueryInterface<KeyedMutex>(); // --------------------------------------------------------------------------------------------------- // Main rendering loop // --------------------------------------------------------------------------------------------------- bool first = true; RenderLoop .Run(form, () => { if(first) { form.Activate(); first = false; } // clear the render target to black context.ClearRenderTargetView(renderTargetView, Colors.DarkSlateGray); // Draw the triangle context.InputAssembler.InputLayout = layoutColor; context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBufferColor, VertexPositionColor.SizeInBytes, 0)); context.OutputMerger.BlendState = null; var currentTechnique = effect.GetTechniqueByName("Color"); for (var pass = 0; pass < currentTechnique.Description.PassCount; ++pass) { using (var effectPass = currentTechnique.GetPassByIndex(pass)) { System.Diagnostics.Debug.Assert(effectPass.IsValid, "Invalid EffectPass"); effectPass.Apply(context); } context.Draw(3, 0); }; // Draw Ellipse on the shared Texture2D device10Mutex.Acquire(0, 100); renderTarget2D.BeginDraw(); renderTarget2D.Clear(Colors.Black); renderTarget2D.DrawGeometry(tesselatedGeometry, solidColorBrush); renderTarget2D.DrawEllipse(new Ellipse(center, 200, 200), solidColorBrush, 20, null); renderTarget2D.EndDraw(); device10Mutex.Release(0); // Draw the shared texture2D onto the screen, blending the 2d content in device11Mutex.Acquire(0, 100); var srv = new ShaderResourceView(device11, textureD3D11); effect.GetVariableByName("g_Overlay").AsShaderResource().SetResource(srv); context.InputAssembler.InputLayout = layoutOverlay; context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip; context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBufferOverlay, VertexPositionTexture.SizeInBytes, 0)); context.OutputMerger.BlendState = blendStateTransparent; currentTechnique = effect.GetTechniqueByName("Overlay"); for (var pass = 0; pass < currentTechnique.Description.PassCount; ++pass) { using (var effectPass = currentTechnique.GetPassByIndex(pass)) { System.Diagnostics.Debug.Assert(effectPass.IsValid, "Invalid EffectPass"); effectPass.Apply(context); } context.Draw(4, 0); } srv.Dispose(); device11Mutex.Release(0); swapChain.Present(0, PresentFlags.None); }); // dispose everything vertexBufferColor.Dispose(); vertexBufferOverlay.Dispose(); layoutColor.Dispose(); layoutOverlay.Dispose(); effect.Dispose(); shaderByteCode.Dispose(); renderTarget2D.Dispose(); swapChain.Dispose(); device11.Dispose(); device10.Dispose(); textureD3D10.Dispose(); textureD3D11.Dispose(); factory1.Dispose(); adapter1.Dispose(); sharedResource.Dispose(); factory2D.Dispose(); surface.Dispose(); solidColorBrush.Dispose(); blendStateTransparent.Dispose(); device10Mutex.Dispose(); device11Mutex.Dispose(); }