Beispiel #1
0
        void DrawSwapChain(CanvasSwapChain swapChain)
        {
            using (var ds = swapChain.CreateDrawingSession(Colors.Transparent))
            {
                var size   = swapChain.Size.ToVector2();
                var radius = (Math.Min(size.X, size.Y) / 2.0f) - 4.0f;

                var center = size / 2;

                float fillRadius = radius;

                // yuck
                lock (PassthroughEffect.GetBadLock())
                {
                    if (PlayerService.Current.ReferencePropertySet != null &&
                        PlayerService.Current.ReferencePropertySet.ContainsKey("InputDataRaw"))
                    {
                        fillRadius *= (float)PlayerService.Current.ReferencePropertySet["InputDataRaw"];
                    }
                }

                ds.FillCircle(center, fillRadius, Colors.LightGoldenrodYellow);
                ds.DrawCircle(center, radius, Colors.LightGray);
            }

            swapChain.Present();
        }
        private void DrawSwapChain(CanvasSwapChain swapChain, bool isPaused)
        {
            ++DrawCount;

            using (var ds = swapChain.CreateDrawingSession(Colors.Transparent))
            {
                var size   = swapChain.Size.ToVector2();
                var radius = (Math.Min(size.X, size.Y) / 2f) - 4f;

                var center = size / 2;

                ds.FillCircle(center, radius, Colors.LightGoldenrodYellow);
                ds.DrawCircle(center, radius, Colors.LightGray);

                double mu = (-DrawCount / 50f);

                for (int i = 0; i < 16; i++)
                {
                    double a = mu + (i / 16f) * Math.PI * 2;
                    var    x = (float)Math.Sin(a);
                    var    y = (float)Math.Cos(a);
                    ds.DrawLine(center, center + new Vector2(x, y) * radius, Colors.Black, 5);
                }

                var rectLenght = Math.Sqrt(radius * radius * 2);

                ds.FillCircle(center, (float)rectLenght / 2, Colors.LightGoldenrodYellow);

                var rect = new Rect(center.X - rectLenght / 2, center.Y - rectLenght / 2, rectLenght, rectLenght);
            }
            SwapChain.Present();
        }
Beispiel #3
0
        // Draw to the CanvasSwapChain.
        void DrawToSwapChain(Size size)
        {
            if (size.Width <= 0 || size.Height <= 0)
            {
                swapChain = null;
                swapChainPanel.SwapChain = null;
                return;
            }
            else if (swapChain == null)
            {
                swapChain = new CanvasSwapChain(canvasControl, size);
                swapChainPanel.SwapChain = swapChain;
            }
            else
            {
                swapChain.ResizeBuffers((float)size.Width, (float)size.Height, canvasControl.Dpi);
            }

            using (var ds = swapChain.CreateDrawingSession(Colors.Transparent))
            {
                Draw(ds, "Canvas\nSwap\nChain", size);
            }

            swapChain.Present();
        }
Beispiel #4
0
        void DrawToSwapChain()
        {
            using (var ds = swapChain.CreateDrawingSession(Colors.Transparent))
            {
                DrawToOutput(mainDeviceResources, ds);
            }

            swapChain.Present();
        }
Beispiel #5
0
        void DrawSwapChain(CanvasSwapChain swapChain, bool isPaused)
        {
            ++drawCount;

            using (var ds = swapChain.CreateDrawingSession(Colors.Transparent))
            {
                var size   = swapChain.Size.ToVector2();
                var radius = (Math.Min(size.X, size.Y) / 2.0f) - 4.0f;

                var center = size / 2;

                ds.FillCircle(center, radius, Colors.LightGoldenrodYellow);
                ds.DrawCircle(center, radius, Colors.LightGray);

                double mu = (-drawCount / 50.0f);

                for (int i = 0; i < 16; ++i)
                {
                    double a = mu + (i / 16.0) * Math.PI * 2;
                    var    x = (float)Math.Sin(a);
                    var    y = (float)Math.Cos(a);
                    ds.DrawLine(center, center + new Vector2(x, y) * radius, Colors.Black, 5);
                }

                var rectLength = Math.Sqrt(radius * radius * 2);

                ds.FillCircle(center, (float)rectLength / 2, Colors.LightGoldenrodYellow);

                var rect = new Rect(center.X - rectLength / 2, center.Y - rectLength / 2, rectLength, rectLength);

                ds.DrawText("This is a swap chain",
                            rect,
                            Colors.Black,
                            new CanvasTextFormat()
                {
                    FontFamily          = "Comic Sans MS",
                    FontSize            = 24,
                    VerticalAlignment   = CanvasVerticalAlignment.Center,
                    HorizontalAlignment = CanvasHorizontalAlignment.Center,
                    WordWrapping        = CanvasWordWrapping.WholeWord,
                });

                var label = string.Format("Draws: {0}\nDevices: {1}\nTap to {2}", drawCount, deviceCount, isPaused ? "unpause" : "pause");

                ds.DrawText(label, rect, Colors.Black, new CanvasTextFormat()
                {
                    FontSize            = 10,
                    VerticalAlignment   = CanvasVerticalAlignment.Bottom,
                    HorizontalAlignment = CanvasHorizontalAlignment.Center
                });
            }

            swapChain.Present();
        }
Beispiel #6
0
        public void Render(Action <ICanvas> gameDraw)
        {
            _fps.Frame();


            using (CanvasDrawingSession ds = _swapChain.CreateDrawingSession(Colors.Black))
            {
                ds.DrawText("FPS: " + _fps.FPS, new Vector2(10, 10), Colors.Red, new CanvasTextFormat
                {
                    FontSize = 50
                });

                gameDraw(new DrawingSessionCanvas(ds));
            }

            _swapChain.Present();
        }
Beispiel #7
0
        // Draw to the CanvasSwapChain.
        void DrawToSwapChain(Size size)
        {
            if (swapChain == null)
            {
                swapChain = new CanvasSwapChain(canvasControl, (float)size.Width, (float)size.Height);
                swapChainPanel.SwapChain = swapChain;
            }
            else
            {
                swapChain.ResizeBuffers((float)size.Width, (float)size.Height);
            }

            using (var ds = swapChain.CreateDrawingSession(Colors.Yellow))
            {
                DrawTextLabel(ds, "Canvas\nSwap\nChain", size);
            }

            swapChain.Present();
        }
Beispiel #8
0
        private void ElementCompositionVisual_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            rootVisual.SetSize(hostElement);
            swapChainVisual.SetSize(hostElement);

            if (canvasDevice != null)
            {
                Dispose();
                canvasDevice = null;
            }

            if (canvasDevice == null)
            {
                canvasDevice = CanvasDevice.GetSharedDevice();
                SetDevice(canvasDevice);
            }

            DrawSwapChain(swapChain);
            swapChain.Present();
        }
Beispiel #9
0
        private void StartRendering(Size sizeSwapchain)
        {
            float ticks = 0;

            BurningTextExample fire = new BurningTextExample();

            fire.CreateResources();

            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    using (var ds = _swapchain.CreateDrawingSession(Colors.Transparent))
                    {
                        // HACK - this should be time in seconds since animation start
                        ticks += .016f;

                        fire.Draw(ds, sizeSwapchain, ticks);
                    }
                    _swapchain.Present();
                }
            }, TaskCreationOptions.LongRunning);
        }
Beispiel #10
0
        void DrawLoop()
        {
            var canceled = drawLoopCancellationTokenSource.Token;

            try
            {
                // Tracking the previous pause state lets us draw once even after becoming paused,
                // so the label text can change to indicate the paused state.

                bool wasPaused = false;

                while (!canceled.IsCancellationRequested)
                {
                    bool isPaused = false; //= paused;
                                           //if (!isPaused || isPaused != wasPaused)
                    if (!isPaused || isPaused != wasPaused)
                    {
                        DrawSwapChain(swapChain, isPaused);
                        swapChain.Present();
                    }
                    else
                    {
                        // A more sophisticated implementation might want to exit the draw loop when paused,
                        // but to keep things simple we just wait on vblank to yield CPU.

                        swapChain.WaitForVerticalBlank();
                    }
                    wasPaused = isPaused;
                }
                swapChain.Dispose();
            }
            catch (Exception e) when(swapChain.Device.IsDeviceLost(e.HResult))
            {
                swapChain.Device.RaiseDeviceLost();
            }
        }
Beispiel #11
0
        void DrawStuff(CanvasDrawingSession ds)
        {
            int horizontalLimit = (int)m_canvasControl.ActualWidth;
            int verticalLimit   = (int)m_canvasControl.ActualHeight;

            DrawnContentType drawnContentType = (DrawnContentType)m_drawnContentTypeCombo.SelectedValue;

            ds.Clear(NextRandomColor());

            Vector2 point;
            float   radiusX;
            float   radiusY;

            Random random = new Random();

            m_canvasSwapChainPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

            switch (drawnContentType)
            {
            case DrawnContentType.Clear_Only:
                break;

            case DrawnContentType.Bitmap:
                if (m_bitmap_tiger != null)
                {
                    ds.DrawImage(m_bitmap_tiger, NextRandomPoint(horizontalLimit, verticalLimit).ToVector2());
                }
                else
                {
                    DrawNoBitmapErrorMessage(ds, horizontalLimit / 2, verticalLimit / 2);
                }
                break;

            case DrawnContentType.Rectangle_Filled:
                Point bound0 = NextRandomPoint(horizontalLimit, verticalLimit);
                Point bound1 = NextRandomPoint(horizontalLimit, verticalLimit);

                m_linearGradientBrush.StartPoint = bound0.ToVector2();
                m_linearGradientBrush.EndPoint   = bound1.ToVector2();

                ds.FillRectangle(
                    new Rect(bound0, bound1),
                    m_linearGradientBrush);

                break;

            case DrawnContentType.Ellipse_Fill:
                NextRandomEllipse(horizontalLimit, verticalLimit, out point, out radiusX, out radiusY);
                ds.FillEllipse(
                    point, radiusX, radiusY,
                    NextRandomColor());
                break;

            case DrawnContentType.Circle_Fill:
                ds.FillCircle(
                    NextRandomPoint(horizontalLimit, verticalLimit).ToVector2(),
                    100,
                    NextRandomColor());
                break;

            case DrawnContentType.Text:
                var p     = NextRandomPoint(horizontalLimit, verticalLimit);
                var x     = (float)p.X;
                var y     = (float)p.Y;
                var color = NextRandomColor();
                ds.DrawLine(new Vector2(x, 0), new Vector2(x, verticalLimit), color);
                ds.DrawLine(new Vector2(0, y), new Vector2(horizontalLimit, y), color);
                ds.DrawText(
                    "Centered",
                    p.ToVector2(),
                    color,
                    new CanvasTextFormat()
                {
                    FontSize           = 18,
                    VerticalAlignment  = CanvasVerticalAlignment.Center,
                    ParagraphAlignment = ParagraphAlignment.Center
                });

                var r = NextRandomRect(horizontalLimit, verticalLimit);
                ds.DrawRectangle(r, color);
                ds.DrawText(
                    m_quiteLongText,
                    r,
                    NextRandomColor(),
                    new CanvasTextFormat()
                {
                    FontFamily         = "Comic Sans MS",
                    FontSize           = 18,
                    ParagraphAlignment = ParagraphAlignment.Justify,
                    Options            = CanvasDrawTextOptions.Clip
                });
                break;

            case DrawnContentType.ImageBrush:
                if (m_bitmap_tiger != null)
                {
                    m_imageBrush.Image   = m_bitmap_tiger;
                    m_imageBrush.ExtendX = (CanvasEdgeBehavior)(m_random.Next(3));
                    m_imageBrush.ExtendY = (CanvasEdgeBehavior)(m_random.Next(3));
                    ds.FillRectangle(new Rect(0, 0, horizontalLimit, verticalLimit), m_imageBrush);
                }
                else
                {
                    DrawNoBitmapErrorMessage(ds, horizontalLimit / 2, verticalLimit / 2);
                }
                break;

            case DrawnContentType.OffscreenTarget:
                m_imageBrush.Image   = m_offscreenTarget;
                m_imageBrush.ExtendX = (CanvasEdgeBehavior)(m_random.Next(3));
                m_imageBrush.ExtendY = (CanvasEdgeBehavior)(m_random.Next(3));
                ds.FillRectangle(new Rect(0, 0, horizontalLimit, verticalLimit), m_imageBrush);
                break;

            case DrawnContentType.Gradients:
                Vector2 center = NextRandomPoint(horizontalLimit, verticalLimit).ToVector2();
                m_radialGradientBrush.Center = center;
                float radius = m_random.Next(horizontalLimit / 2);
                m_radialGradientBrush.OriginOffset = new Vector2(radius, radius);
                m_radialGradientBrush.RadiusX      = radius;
                m_radialGradientBrush.RadiusY      = radius;
                ds.FillCircle(center, radius, m_radialGradientBrush);

                Vector2 line0 = NextRandomPoint(horizontalLimit, verticalLimit).ToVector2();
                Vector2 line1 = NextRandomPoint(horizontalLimit, verticalLimit).ToVector2();
                m_linearGradientBrush.StartPoint = line0;
                m_linearGradientBrush.EndPoint   = line1;
                float thickness = m_random.Next(horizontalLimit / 2);
                ds.DrawLine(line0, line1, m_linearGradientBrush, thickness);
                break;

            case DrawnContentType.AlternateBitmapLoading:
                if (m_bitmap_colorGrids != null)
                {
                    Matrix3x2 scale = Matrix3x2.CreateScale(20);

                    ds.Transform = scale;
                    ds.DrawImage(m_bitmap_colorGrids[0]);

                    ds.Transform = scale * Matrix3x2.CreateTranslation(200, 0);
                    ds.DrawImage(m_bitmap_colorGrids[1]);

                    ds.Transform = scale * Matrix3x2.CreateTranslation(0, 200);
                    ds.DrawImage(m_bitmap_colorGrids[2]);
                }
                else
                {
                    DrawNoBitmapErrorMessage(ds, horizontalLimit / 2, verticalLimit / 2);
                }
                break;

            case DrawnContentType.SwapChainPanel:
                m_canvasSwapChainPanel.Visibility = Windows.UI.Xaml.Visibility.Visible;

                float swapChainWidth  = horizontalLimit * ((float)random.NextDouble() / 2 + 0.5f);
                float swapChainHeight = verticalLimit * ((float)random.NextDouble() / 2 + 0.5f);

                if (m_swapChain == null)
                {
                    m_swapChain = new CanvasSwapChain(ds, swapChainWidth, swapChainHeight);
                    m_canvasSwapChainPanel.SwapChain = m_swapChain;
                }
                else
                {
                    m_swapChain.ResizeBuffers(swapChainWidth, swapChainHeight);
                }

                using (CanvasDrawingSession panelDS = m_swapChain.CreateDrawingSession(NextRandomColor()))
                {
                    panelDS.DrawCircle(swapChainWidth / 2.0f, swapChainHeight / 2.0f, 100.0f, NextRandomColor(), 20.0f);
                }
                m_swapChain.Present();
                break;

            case DrawnContentType.Test_Scene0_Default:
                GeometryTestScene0.DrawGeometryTestScene(ds, TestSceneRenderingType.Default);
                break;

            case DrawnContentType.Test_Scene0_Wireframe:
                GeometryTestScene0.DrawGeometryTestScene(ds, TestSceneRenderingType.Wireframe);
                break;

            case DrawnContentType.Test_Scene1_Default:
                GeometryTestScene1.DrawGeometryTestScene(ds, TestSceneRenderingType.Default);
                break;

            case DrawnContentType.Test_Scene1_Randomized:
                GeometryTestScene1.DrawGeometryTestScene(ds, TestSceneRenderingType.Randomized);
                break;

            case DrawnContentType.Test_Scene1_Wireframe:
                GeometryTestScene1.DrawGeometryTestScene(ds, TestSceneRenderingType.Wireframe);
                break;

            default:
                System.Diagnostics.Debug.Assert(false);     // Unexpected
                break;
            }
        }
Beispiel #12
0
        const float peakMeterFallTime = 0.003f;     // Slow fall time

        void DrawSwapChain(CanvasSwapChain swapChain)
        {
            using (var ds = swapChain.CreateDrawingSession(Colors.Transparent))
            {
                var size   = swapChain.Size.ToVector2();
                var radius = (Math.Min(size.X, size.Y) / 2.0f) - 4.0f;

                var center = size / 2;

                if (PlayerService.Current.ReferencePropertySet?.ContainsKey("samplegrabber") == true)
                {
                    SampleGrabber.IMyInterface mft = (SampleGrabber.IMyInterface)PlayerService.Current.ReferencePropertySet["samplegrabber"];

                    var     dataFrame           = mft.GetFrame();
                    Vector2 visualizationOffset = new Vector2(200, 20);
                    Vector2 barSize             = new Vector2(5, 150);
                    DrawVisualization(dataFrame, ds, true, true, true, barSize, visualizationOffset);
                    // yuck
                    //lock (PassthroughEffect.GetBadLock())

                    /*
                     * {
                     *  if (PlayerService.Current.ReferencePropertySet != null &&
                     *      PlayerService.Current.ReferencePropertySet.ContainsKey("dataQueue") &&
                     *      this.dataQueue == null)
                     *  {
                     *      this.dataQueue = PlayerService.Current.ReferencePropertySet["dataQueue"] as Queue<Tuple<double, double>>;
                     *  }
                     *
                     *  if (this.dataQueue != null && !hasNextValue && this.dataQueue.Count>0)
                     *  {
                     *      nextvalue = this.dataQueue.Dequeue();
                     *      hasNextValue = true;
                     *  } else if (this.dataQueue != null && this.dataQueue.Count == 0)
                     *  {
                     *      hasNextValue = false;
                     *
                     *  }
                     * }
                     *
                     * if (dataQueue != null)
                     * {
                     *  Debug.WriteLine(dataQueue.Count);
                     * }
                     *
                     * if (!weAreVisualizing && hasNextValue)
                     * {
                     *  weAreVisualizing = true;
                     *  delayStart = true;
                     * }
                     *
                     * if (weAreVisualizing && delayStart && delayCurrent < delayTotal)
                     * {
                     *  delayCurrent++;
                     * }
                     * else
                     * {
                     *  delayStart = false;
                     *  delayCurrent = 0;
                     * }
                     *
                     * if (weAreVisualizing && delayStart)
                     * {
                     *  DrawVU(ds, -100, -100);
                     * } else if (weAreVisualizing && !delayStart && nextvalue != null)
                     * {
                     *  DrawVU(ds, nextvalue.Item1, nextvalue.Item2);
                     *  hasNextValue = false;
                     * } else
                     * {
                     *  Debug.WriteLine("miss");
                     *  DrawVU(ds, -100, -100);
                     * }*/
                }

                swapChain.Present();
            }
        }
Beispiel #13
0
        void DrawSwapChain(CanvasSwapChain swapChain, bool isPaused)
        {
            ++drawCount;

            using (var ds = swapChain.CreateDrawingSession(Colors.Transparent))
            {
                var size = swapChain.Size.ToVector2();
                var radius = (Math.Min(size.X, size.Y) / 2.0f) - 4.0f;

                var center = size / 2;

                ds.FillCircle(center, radius, Colors.LightGoldenrodYellow);
                ds.DrawCircle(center, radius, Colors.LightGray);

                double mu = (-drawCount / 50.0f);

                for (int i =0; i < 16; ++i)
                {
                    double a = mu + (i / 16.0) * Math.PI * 2;
                    var x = (float)Math.Sin(a);
                    var y = (float)Math.Cos(a);
                    ds.DrawLine(center, center + new Vector2(x, y) * radius, Colors.Black, 5);
                }

                var rectLength = Math.Sqrt(radius * radius * 2);

                ds.FillCircle(center, (float)rectLength / 2, Colors.LightGoldenrodYellow);

                var rect = new Rect(center.X - rectLength / 2, center.Y - rectLength / 2, rectLength, rectLength);

                ds.DrawText("This is a swap chain",
                    rect,
                    Colors.Black,
                    new CanvasTextFormat()
                    {
                        FontFamily = "Comic Sans MS",
                        FontSize = 24,
                        VerticalAlignment = CanvasVerticalAlignment.Center,
                        HorizontalAlignment = CanvasHorizontalAlignment.Center,
                        WordWrapping = CanvasWordWrapping.WholeWord,
                    });

                var label = string.Format("Draws: {0}\nDevices: {1}\nTap to {2}", drawCount, deviceCount, isPaused ? "unpause" : "pause");

                ds.DrawText(label, rect, Colors.Black, new CanvasTextFormat()
                {
                    FontSize = 10,
                    VerticalAlignment = CanvasVerticalAlignment.Bottom,
                    HorizontalAlignment = CanvasHorizontalAlignment.Center
                });
            }

            swapChain.Present();
        }
Beispiel #14
0
        private void ProcessFrame(Direct3D11CaptureFrame frame)
        {
            // Resize and device-lost leverage the same function on the
            // Direct3D11CaptureFramePool. Refactoring it this way avoids
            // throwing in the catch block below (device creation could always
            // fail) along with ensuring that resize completes successfully and
            // isn’t vulnerable to device-lost.
            bool needsReset     = false;
            bool recreateDevice = false;

            if ((frame.ContentSize.Width != _lastSize.Width) ||
                (frame.ContentSize.Height != _lastSize.Height))
            {
                needsReset = true;
                _lastSize  = frame.ContentSize;
                _swapChain.ResizeBuffers(_lastSize.Width, _lastSize.Height);
            }
            Direct3D11CaptureFrame direct = frame;

            try
            {
                // Take the D3D11 surface and draw it into a
                // Composition surface.
                if (direct.SystemRelativeTime - lastFrameTime < TimeSpan.FromSeconds(1))
                {
                    //F**k Microsoft🤬
                    MediaClip mediaClip = MediaClip.CreateFromSurface(direct.Surface, direct.SystemRelativeTime - lastFrameTime);
                    composition.Clips.Add(mediaClip);
                }
                lastFrameTime = direct.SystemRelativeTime;

                // Convert our D3D11 surface into a Win2D object.
                canvasBitmap = CanvasBitmap.CreateFromDirect3D11Surface(
                    _canvasDevice,
                    direct.Surface);

                using (var drawingSession = _swapChain.CreateDrawingSession(Colors.Transparent))
                {
                    //drawingSession.DrawCircle(400, 300, 100, Colors.Red, 20);
                    ScaleEffect effect = new ScaleEffect()
                    {
                        Source = canvasBitmap,
                        Scale  = new Vector2((float)swapChain.ActualWidth / _item.Size.Width)
                    };

                    drawingSession.DrawImage(effect);
                }

                _swapChain.Present();

                //canvasControl.Invalidate();
                // Helper that handles the drawing for us, not shown.
            }
            // This is the device-lost convention for Win2D.
            catch (Exception e) when(_canvasDevice.IsDeviceLost(e.HResult))
            {
                // We lost our graphics device. Recreate it and reset
                // our Direct3D11CaptureFramePool.
                needsReset     = true;
                recreateDevice = true;
            }
            if (needsReset)
            {
                ResetFramePool(direct.ContentSize, recreateDevice);
            }
        }