Example #1
0
        /// <summary>
        /// Function to remember the current state of the graphics interface so that it can be reset after rendering.
        /// </summary>
        /// <param name="states">The states to record.</param>
        /// <remarks>
        /// <para>
        /// Applications should use this in order to remember the current set of render targets, depth/stencil or set of view ports on the <see cref="GorgonGraphics"/> interface prior to rendering a scene.
        /// When this method is called, it should be paired with a call to <see cref="RecallState"/> in order to restore the state.
        /// </para>
        /// <para>
        /// The <paramref name="states"/> parameter indicates which state to record based on the value from <see cref="RecordedState"/> enumeration. This enumeration is a bit flag, and the values can be
        /// combined via OR so that multiple states are stored.
        /// </para>
        /// </remarks>
        /// <seealso cref="RecordedState"/>
        /// <seealso cref="GorgonGraphics"/>
        public void RecordState(RecordedState states)
        {
            if (states == RecordedState.None)
            {
                RecordedStates       = RecordedState.None;
                RecordedDepthStencil = null;
                Array.Clear(_targets, 0, _targets.Length);
                Array.Clear(_viewports, 0, _viewports.Length);
                return;
            }

            if ((states & RecordedState.RenderTargets) == RecordedState.RenderTargets)
            {
                int targetCount = _targetEnumerator.Count();

                if (_targets.Length != targetCount)
                {
                    Array.Resize(ref _targets, targetCount);
                }

                for (int i = 0; i < targetCount; ++i)
                {
                    _targets[i] = Graphics.RenderTargets[i];
                }
            }

            if ((states & RecordedState.Viewports) == RecordedState.Viewports)
            {
                int viewportCount = _viewportEnumerator.Count();

                if (_viewports.Length != viewportCount)
                {
                    Array.Resize(ref _viewports, viewportCount);
                }

                for (int i = 0; i < viewportCount; i++)
                {
                    _viewports[i] = Graphics.Viewports[i];
                }
            }

            if ((states & RecordedState.DepthStencil) == RecordedState.DepthStencil)
            {
                RecordedDepthStencil = Graphics.DepthStencilView;
            }

            RecordedStates = states;
        }
Example #2
0
        /// <summary>
        /// Function to recall the previous state of the graphics interface so that it can be reset after rendering.
        /// </summary>
        /// <param name="state">[Optional] The state(s) to recall.</param>
        public void RecallState(RecordedState state = RecordedState.All)
        {
            if (RecordedStates == RecordedState.None)
            {
                return;
            }

            // Restore our states.
            bool resetDepthStencil = (((RecordedStates & RecordedState.DepthStencil) == RecordedState.DepthStencil) &&
                                      ((state & RecordedState.DepthStencil) == RecordedState.DepthStencil));

            if (((RecordedStates & RecordedState.RenderTargets) == RecordedState.RenderTargets) &&
                ((state & RecordedState.RenderTargets) == RecordedState.RenderTargets))
            {
                Graphics.SetRenderTargets(_targets, resetDepthStencil ? RecordedDepthStencil : Graphics.DepthStencilView);
                RecordedStates &= ~RecordedState.RenderTargets;

                if (resetDepthStencil)
                {
                    RecordedStates &= ~RecordedState.DepthStencil;
                }
            }
            else if (((RecordedStates & RecordedState.DepthStencil) == RecordedState.DepthStencil) &&
                     ((state & RecordedState.DepthStencil) == RecordedState.DepthStencil))
            {
                Graphics.SetDepthStencil(RecordedDepthStencil);
                RecordedStates &= ~RecordedState.DepthStencil;
            }

            if (((RecordedStates & RecordedState.Viewports) != RecordedState.Viewports) ||
                ((state & RecordedState.Viewports) != RecordedState.Viewports))
            {
                return;
            }

            Graphics.SetViewports(_viewports);
            RecordedStates &= ~RecordedState.Viewports;
        }