internal static void ApplySetup(MyRenderSetup setup)
        {
            if (setup.CameraPosition.HasValue)
            {
                MyRenderCamera.SetPosition(setup.CameraPosition.Value);
            }
            if (setup.AspectRatio.HasValue)
            {
                MyRenderCamera.AspectRatio = setup.AspectRatio.Value;
            }
            if (setup.Fov.HasValue && MyRenderCamera.FieldOfView != setup.Fov.Value)
            {
                MyRenderCamera.FieldOfView = setup.Fov.Value;
            }
            if (setup.ViewMatrix.HasValue && setup.ViewMatrix != MatrixD.Zero)
            {
                MyRenderCamera.SetViewMatrix(setup.ViewMatrix.Value, null);
            }
            if (setup.Viewport.HasValue)
            {
                MyRenderCamera.Viewport = setup.Viewport.Value;
            }
            if (setup.Fov.HasValue && MyRenderCamera.FieldOfView != setup.Fov.Value)
            {
                // When custom FOV set, zoom will be disabled
                MyRenderCamera.ChangeFov(setup.Fov.Value);
            }
            if (setup.ProjectionMatrix.HasValue)
            {
                MyRenderCamera.SetCustomProjection(setup.ProjectionMatrix.Value);
            }

            if (setup.RenderTargets != null && setup.RenderTargets.Length > 0)
            {
                Texture rt = setup.RenderTargets[0];
                if (rt != null)
                {
                    MyRender.SetRenderTarget(rt, setup.DepthTarget);
                }
                else
                    MyRender.SetRenderTarget(null, null);
            }
            else
                MyRender.SetRenderTarget(null, null);

            m_currentSetup.RenderTargets = setup.RenderTargets;

            MyRenderCamera.UpdateCamera();
            MyRender.SetDeviceViewport(MyRenderCamera.Viewport);
        }
 internal static void PushRenderSetup(MyRenderSetup setup)
 {
     m_renderSetupStack.Add(setup);
 }
 internal static void PopRenderSetupAndRevert(MyRenderSetup previousSetup)
 {
     PopRenderSetup();
     ApplySetup(previousSetup);
 }
 internal static void PushRenderSetupAndApply(MyRenderSetup setup, ref MyRenderSetup storePreviousSetup)
 {
     PushRenderSetup(setup);
     ApplySetupStack(storePreviousSetup);
 }
        private static void AggregateSetup(MyRenderSetup setup)
        {
            if (setup.CallerID != null)
            {
                m_currentSetup.CallerID = setup.CallerID;
            }
            else
            {
                Debug.Assert(false, "CallerID has to be set in render setup.");
            }

            if (setup.RenderTargets != null)
            {
                m_currentSetup.RenderTargets = setup.RenderTargets;
            }

            if (setup.CameraPosition.HasValue)
            {
                m_currentSetup.CameraPosition = setup.CameraPosition;
            }

            if (setup.ViewMatrix.HasValue)
            {
                m_currentSetup.ViewMatrix = setup.ViewMatrix;
            }

            if (setup.ProjectionMatrix.HasValue)
            {
                m_currentSetup.ProjectionMatrix = setup.ProjectionMatrix;
            }

            if (setup.Fov.HasValue)
            {
                m_currentSetup.Fov = setup.Fov;
            }

            if (setup.AspectRatio.HasValue)
            {
                m_currentSetup.AspectRatio = setup.AspectRatio;
            }

            if (setup.Viewport.HasValue)
            {
                m_currentSetup.Viewport = setup.Viewport;
            }

            if (setup.EnableHDR.HasValue)
            {
                m_currentSetup.EnableHDR = setup.EnableHDR;
            }

            if (setup.EnableLights.HasValue)
            {
                m_currentSetup.EnableLights = setup.EnableLights;
            }

            if (setup.EnableSun.HasValue)
            {
                m_currentSetup.EnableSun = setup.EnableSun;
            }

            // Special case...when no shadow render specified, no shadows are rendered
            m_currentSetup.ShadowRenderer = setup.ShadowRenderer;
            m_currentSetup.FogMultiplierMult = setup.FogMultiplierMult;
            m_currentSetup.DepthToAlpha = setup.DepthToAlpha;
            m_currentSetup.DepthCopy = setup.DepthCopy;

            if (setup.EnableShadowInterleaving.HasValue)
            {
                m_currentSetup.EnableShadowInterleaving = setup.EnableShadowInterleaving;
            }

            if (setup.EnableSmallLights.HasValue)
            {
                m_currentSetup.EnableSmallLights = setup.EnableSmallLights;
            }

            if (setup.EnableSmallLightShadows.HasValue)
            {
                m_currentSetup.EnableSmallLightShadows = setup.EnableSmallLightShadows;
            }

            if (setup.EnableDebugHelpers.HasValue)
            {
                m_currentSetup.EnableDebugHelpers = setup.EnableDebugHelpers;
            }

            if (setup.EnableEnvironmentMapping.HasValue)
            {
                m_currentSetup.EnableEnvironmentMapping = setup.EnableEnvironmentMapping;
            }

            if (setup.EnableNear.HasValue)
            {
                m_currentSetup.EnableNear = setup.EnableNear;
            }

            if (setup.BackgroundColor.HasValue)
            {
                m_currentSetup.BackgroundColor = setup.BackgroundColor;
            }

            if (setup.RenderElementsToDraw != null)
            {
                m_currentSetup.RenderElementsToDraw = setup.RenderElementsToDraw;
            }

            if (setup.TransparentRenderElementsToDraw != null)
            {
                m_currentSetup.TransparentRenderElementsToDraw = setup.TransparentRenderElementsToDraw;
            }

            m_currentSetup.EnableOcclusionQueries = setup.EnableOcclusionQueries;

            if (setup.EnabledModules != null)
            {
                if (m_currentSetup.EnabledModules == null)
                {
                    m_currentSetup.EnabledModules = setup.EnabledModules;
                }
                else
                {
                    m_currentSetup.EnabledModules.IntersectWith(setup.EnabledModules);
                }
            }

            if (setup.EnabledPostprocesses != null)
            {
                if (m_currentSetup.EnabledPostprocesses == null)
                {
                    m_currentSetup.EnabledPostprocesses = setup.EnabledPostprocesses;
                }
                else
                {
                    m_currentSetup.EnabledPostprocesses.IntersectWith(setup.EnabledPostprocesses);
                }
            }

            if (setup.EnabledRenderStages != null)
            {
                if (m_currentSetup.EnabledRenderStages == null)
                {
                    m_currentSetup.EnabledRenderStages = setup.EnabledRenderStages;
                }
                else
                {
                    m_currentSetup.EnabledRenderStages.IntersectWith(setup.EnabledRenderStages);
                }
            }

            //m_currentSetup.RenderTargets = setup.RenderTargets;
        }
        private static void ApplySetupStack(MyRenderSetup storeBackup)
        {
            if (storeBackup != null)
            {
                if (MyRenderCamera.ViewMatrix.Left != Vector3D.Zero)
                {
                    storeBackup.CameraPosition = MyRenderCamera.Position;
                    storeBackup.ViewMatrix = MyRenderCamera.ViewMatrix;
                }

                storeBackup.ProjectionMatrix = MyRenderCamera.ProjectionMatrix;
                storeBackup.AspectRatio = MyRenderCamera.AspectRatio;
                storeBackup.Fov = MyRenderCamera.FieldOfView;
                storeBackup.RenderTargets = m_currentSetup.RenderTargets;
                storeBackup.Viewport = MyRenderCamera.Viewport;
            }

            if (MyRenderCamera.ViewMatrix.Left != Vector3D.Zero)
            {
                m_currentSetup.ViewMatrix = MyRenderCamera.ViewMatrix;
            }

            // Set default values
            m_currentSetup.CallerID = MyRenderCallerEnum.Main;

            m_currentSetup.RenderTargets = null;

            m_currentSetup.CameraPosition = MyRenderCamera.Position;
            m_currentSetup.AspectRatio = MyRenderCamera.AspectRatio;
            m_currentSetup.Fov = null;
            m_currentSetup.Viewport = MyRenderCamera.Viewport;
            m_currentSetup.ProjectionMatrix = null;
            m_currentSetup.FogMultiplierMult = 1;
            m_currentSetup.DepthToAlpha = false;
            m_currentSetup.DepthCopy = false;

            m_currentSetup.EnableHDR = true;
            m_currentSetup.EnableLights = true;
            m_currentSetup.EnableSun = true;
            m_currentSetup.ShadowRenderer = m_shadowRenderer; // Default shadow render
            m_currentSetup.EnableShadowInterleaving = Settings.ShadowInterleaving;
            m_currentSetup.EnableSmallLights = true;
            m_currentSetup.EnableSmallLightShadows = true;
            m_currentSetup.EnableDebugHelpers = true;
            m_currentSetup.EnableEnvironmentMapping = true;
            m_currentSetup.EnableNear = true;
            m_currentSetup.EnableOcclusionQueries = true;

            m_currentSetup.BackgroundColor = null;

            m_currentSetup.EnabledModules = null;
            m_currentSetup.EnabledPostprocesses = null;
            m_currentSetup.EnabledRenderStages = null;

            //m_currentSetup.LightsToUse = null;
            m_currentSetup.RenderElementsToDraw = null;
            m_currentSetup.TransparentRenderElementsToDraw = null;

            foreach (var setup in m_renderSetupStack)
            {
                AggregateSetup(setup);
            }

            ApplySetup(m_currentSetup);
        }