Example #1
0
        public override void Issue(DeviceManager manager)
        {
            manager.Device.SetRenderTarget(RenderTarget);
            RenderManager.ResetDeviceState(manager.Device);

            base.Issue(manager);
        }
Example #2
0
        private void ResetDeviceState(RenderTarget2D firstRenderTarget, bool setParams)
        {
            RenderManager.ResetDeviceState(Device);
            int w, h;

            if (firstRenderTarget != null)
            {
                w = firstRenderTarget.Width;
                h = firstRenderTarget.Height;
            }
            else
            {
                w = Device.PresentationParameters.BackBufferWidth;
                h = Device.PresentationParameters.BackBufferHeight;
            }
            Device.Viewport         = new Viewport(0, 0, w, h);
            Device.ScissorRectangle = new Rectangle(0, 0, w, h);
            UpdateTargetInfo(firstRenderTarget, true, setParams);
        }
        /// <summary>
        /// Synchronously renders a complete frame to the specified render target.
        /// Automatically sets up the device's viewport and the view transform of your materials and restores them afterwards.
        /// </summary>
        public bool SynchronousDrawToRenderTarget(RenderTarget2D renderTarget, DefaultMaterialSet materials, Action <Frame> drawBehavior)
        {
            if (renderTarget.IsDisposed)
            {
                return(false);
            }
            if (!SynchronousDrawsEnabled)
            {
                throw new InvalidOperationException("Synchronous draws not available inside of Game.Draw");
            }

            WaitForActiveDraw();

            var oldDrawIsActive = Interlocked.Exchange(ref _SynchronousDrawIsActive, 1);

            if (oldDrawIsActive != 0)
            {
                throw new InvalidOperationException("A synchronous draw is already in progress");
            }

            _SynchronousDrawFinishedSignal.Reset();

            WaitForActiveDraw();

            try {
                using (var frame = Manager.CreateFrame()) {
                    materials.PushViewTransform(ViewTransform.CreateOrthographic(renderTarget.Width, renderTarget.Height));

                    ClearBatch.AddNew(frame, int.MinValue, materials.Clear, clearColor: Color.Transparent);

                    drawBehavior(frame);

                    PrepareNextFrame(frame, false);

                    var oldRenderTargets = Device.GetRenderTargets();
                    var oldViewport      = Device.Viewport;
                    try {
                        Device.SetRenderTarget(renderTarget);
                        RenderManager.ResetDeviceState(Device);
                        Device.Viewport = new Viewport(0, 0, renderTarget.Width, renderTarget.Height);

                        RenderFrameToDraw(frame, false);
                    } finally {
                        Device.SetRenderTargets(oldRenderTargets);
                        materials.PopViewTransform();
                        Device.Viewport = oldViewport;
                    }
                }

                return(true);
            } finally {
                _SynchronousDrawFinishedSignal.Set();
                Interlocked.Exchange(ref _SynchronousDrawIsActive, 0);
            }
        }
Example #4
0
        public void PushRenderTargets(RenderTargetBinding[] newRenderTargets)
        {
            PushStates();

            var first = (RenderTarget2D)newRenderTargets[0].RenderTarget;

            RenderTargetStack.Push(Device.GetRenderTargets());
            ViewportStack.Push(Device.Viewport);
            Device.SetRenderTargets(newRenderTargets);
            RenderManager.ResetDeviceState(Device);
            Device.Viewport = new Viewport(0, 0, first.Width, first.Height);
        }
Example #5
0
        public void Finish()
        {
            if (CurrentMaterial != null)
            {
                CurrentMaterial.End(this);
                CurrentMaterial = null;
            }

            RenderManager.ResetDeviceState(Device);

            Device.SetRenderTargets();
            Device.SetVertexBuffer(null);
        }
Example #6
0
        public void PushRenderTarget(RenderTarget2D newRenderTarget)
        {
            PushStates();

            RenderTargetStack.Push(Device.GetRenderTargets());
            ViewportStack.Push(Device.Viewport);
            Device.SetRenderTarget(newRenderTarget);
            RenderManager.ResetDeviceState(Device);
            if (newRenderTarget != null)
            {
                Device.Viewport = new Viewport(0, 0, newRenderTarget.Width, newRenderTarget.Height);
            }
            else
            {
                Device.Viewport = new Viewport(0, 0, Device.PresentationParameters.BackBufferWidth, Device.PresentationParameters.BackBufferHeight);
            }
        }
        protected bool DefaultBeginDraw()
        {
            if (IsDisposed)
            {
                return(false);
            }

            if (Device.GraphicsDeviceStatus == GraphicsDeviceStatus.Normal)
            {
                RenderManager.ResetDeviceState(Device);
                return(true);
            }
            else if (!_Running)
            {
                return(false);
            }

            return(false);
        }
        private bool DoSynchronousDrawToRenderTarget(
            RenderTarget2D renderTarget,
            DefaultMaterialSet materials,
            Delegate drawBehavior, object userData,
            ref ViewTransform?viewTransform,
            string description
            )
        {
            var oldLazyState = materials.LazyViewTransformChanges;

            try {
                materials.LazyViewTransformChanges = false;
                materials.ApplyViewTransform(materials.ViewTransform, true);
                using (var frame = Manager.CreateFrame(this)) {
                    frame.ChangeRenderTargets = false;
                    frame.Label = description;
                    if (viewTransform.HasValue)
                    {
                        materials.PushViewTransform(in viewTransform);
                    }
                    else
                    {
                        materials.PushViewTransform(ViewTransform.CreateOrthographic(renderTarget.Width, renderTarget.Height));
                    }

                    try {
                        // FIXME: Should queued draws run here? They are probably meant to run in the next real frame

                        var singleBehavior = drawBehavior as Action <Frame>;
                        var multiBehavior  = drawBehavior as PendingDrawHandler;
                        if (singleBehavior != null)
                        {
                            singleBehavior(frame);
                        }
                        else if (multiBehavior != null)
                        {
                            multiBehavior(frame, materials, userData);
                        }
                        else
                        {
                            throw new ArgumentException("Draw behavior was not of a compatible type");
                        }

                        RunBeforePrepareHandlers();
                        PrepareNextFrame(frame, false);

                        Manager.DeviceManager.SetRenderTarget(renderTarget);
                        RenderManager.ResetDeviceState(Device);
                        Device.Clear(Color.Transparent);

                        RenderFrameToDraw(frame, false);
                        // We don't have to push/pop anymore because the stacks are cleared at the end of a frame.

                        return(true);
                    } finally {
                        materials.PopViewTransform();
                    }
                }
            } finally {
                materials.LazyViewTransformChanges = oldLazyState;
            }
        }