Пример #1
0
        internal static IConstantBuffer GetCB_blur(MyStereoRegion region, Vector2I uavSize)
        {
            int offX = 0;
            int maxX = uavSize.X - 1;

            if (region == MyStereoRegion.LEFT)
            {
                maxX = uavSize.X / 2 - 1;
            }
            else if (region == MyStereoRegion.RIGHT)
            {
                offX = uavSize.X / 2;
                maxX = uavSize.X / 2 - 1;
            }

            var buffer  = MyCommon.GetObjectCB(16);
            var mapping = MyMapping.MapDiscard(buffer);

            mapping.WriteAndPosition(ref offX);
            mapping.WriteAndPosition(ref maxX);
            var size = new Vector2(uavSize.X, uavSize.Y);

            mapping.WriteAndPosition(ref size);
            mapping.Unmap();
            return(buffer);
        }
Пример #2
0
        internal static MyBindableResource Run(MyBindableResource src, MyBindableResource avgLum)
        {
            RC.CSSetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);

            RC.BindUAV(0, MyRender11.HalfScreenUavHDR);
            RC.BindSRVs(0, src, avgLum);

            RC.SetCS(m_bloomShader);

            var size = MyRender11.HalfScreenUavHDR.GetSize();

            VRageMath.Vector2I threadGroups = new VRageMath.Vector2I((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads);
            RC.DeviceContext.Dispatch(threadGroups.X, threadGroups.Y, 1);

            RC.SetCS(m_downscaleShader);

            size         = MyRender11.QuarterScreenUavHDR.GetSize();
            threadGroups = new VRageMath.Vector2I((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads);
            RC.BindUAV(0, MyRender11.QuarterScreenUavHDR);
            RC.BindSRV(0, MyRender11.HalfScreenUavHDR);
            RC.DeviceContext.Dispatch(threadGroups.X, threadGroups.Y, 1);

            size         = MyRender11.EighthScreenUavHDR.GetSize();
            threadGroups = new VRageMath.Vector2I((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads);
            RC.BindUAV(0, MyRender11.EighthScreenUavHDR);
            RC.BindSRV(0, MyRender11.QuarterScreenUavHDR);
            RC.DeviceContext.Dispatch(threadGroups.X, threadGroups.Y, 1);

            RC.SetCS(m_blurV);
            RC.BindUAV(0, MyRender11.EighthScreenUavHDRHelper);
            RC.BindSRV(0, MyRender11.EighthScreenUavHDR);
            RC.DeviceContext.Dispatch(threadGroups.X, threadGroups.Y, 1);

            RC.SetCS(m_blurH);
            RC.BindUAV(0, MyRender11.EighthScreenUavHDR);
            RC.BindSRV(0, MyRender11.EighthScreenUavHDRHelper);

            int nPasses = 1;

            if (MyStereoRender.Enable)
            {
                threadGroups.X /= 2;
                nPasses         = 2;
            }
            for (int nPass = 0; nPass < nPasses; nPass++)
            {
                MyStereoRegion region = MyStereoRegion.FULLSCREEN;
                if (MyStereoRender.Enable)
                {
                    region = nPass == 0 ? MyStereoRegion.LEFT : MyStereoRegion.RIGHT;
                }
                RC.CSSetCB(1, GetCB_blurH(region, size));
                RC.DeviceContext.Dispatch(threadGroups.X, threadGroups.Y, 1);
            }

            return(MyRender11.EighthScreenUavHDR);
        }
Пример #3
0
 public static void SetViewport(MyRenderContext RC, MyStereoRegion region)
 {
     SharpDX.ViewportF viewport = new SharpDX.ViewportF(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y);
     if (region == MyStereoRegion.LEFT)
     {
         viewport = new SharpDX.ViewportF(viewport.X, viewport.Y, viewport.Width / 2, viewport.Height);
     }
     else if (region == MyStereoRegion.RIGHT)
     {
         viewport = new SharpDX.ViewportF(viewport.X + viewport.Width / 2, viewport.Y, viewport.Width / 2, viewport.Height);
     }
     RC.DeviceContext.Rasterizer.SetViewport(viewport);
 }
Пример #4
0
        internal static ConstantsBufferId GetCB_blurH(MyStereoRegion region, Vector3I uavSize)
        {
            int offX = 0;
            int maxX = uavSize.X - 1;
            if (region == MyStereoRegion.LEFT)
                maxX = uavSize.X / 2 - 1;
            else if (region == MyStereoRegion.RIGHT)
            {
                offX = uavSize.X / 2;
                maxX = uavSize.X / 2 - 1;
            }

            var buffer = MyCommon.GetObjectCB(8);
            var mapping = MyMapping.MapDiscard(buffer);
            mapping.WriteAndPosition(ref offX);
            mapping.WriteAndPosition(ref maxX);
            mapping.Unmap();
            return buffer;
        }
Пример #5
0
        internal static IConstantBuffer GetCB_blur(MyStereoRegion region, Vector2I uavSize)
        {
            int offX = 0;
            int maxX = uavSize.X - 1;
            if (region == MyStereoRegion.LEFT)
                maxX = uavSize.X / 2 - 1;
            else if (region == MyStereoRegion.RIGHT)
            {
                offX = uavSize.X / 2;
                maxX = uavSize.X / 2 - 1;
            }

            var buffer = MyCommon.GetObjectCB(16);
            var mapping = MyMapping.MapDiscard(buffer);
            mapping.WriteAndPosition(ref offX);
            mapping.WriteAndPosition(ref maxX);
            var size = new Vector2(uavSize.X, uavSize.Y);
            mapping.WriteAndPosition(ref size);
            mapping.Unmap();
            return buffer;
        }
Пример #6
0
        internal static ConstantsBufferId GetCB_blurH(MyStereoRegion region, Vector3I uavSize)
        {
            int offX = 0;
            int maxX = uavSize.X - 1;

            if (region == MyStereoRegion.LEFT)
            {
                maxX = uavSize.X / 2 - 1;
            }
            else if (region == MyStereoRegion.RIGHT)
            {
                offX = uavSize.X / 2;
                maxX = uavSize.X / 2 - 1;
            }

            var buffer  = MyCommon.GetObjectCB(8);
            var mapping = MyMapping.MapDiscard(buffer);

            mapping.WriteAndPosition(ref offX);
            mapping.WriteAndPosition(ref maxX);
            mapping.Unmap();
            return(buffer);
        }
 public static void SetViewport(MyRenderContext RC, MyStereoRegion region)
 {
     SharpDX.ViewportF viewport = new SharpDX.ViewportF(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y);
     if (region == MyStereoRegion.LEFT)
         viewport = new SharpDX.ViewportF(viewport.X, viewport.Y, viewport.Width / 2, viewport.Height);
     else if (region == MyStereoRegion.RIGHT)
         viewport = new SharpDX.ViewportF(viewport.X + viewport.Width / 2, viewport.Y, viewport.Width / 2, viewport.Height);
     RC.DeviceContext.Rasterizer.SetViewport(viewport);
 }
Пример #8
0
        private static void SetupCameraMatricesInternal(MyRenderMessageSetCameraViewMatrix message, MyEnvironmentMatrices envMatrices, MyStereoRegion typeofEnv)
        {//uses m_leftEye to handle HMD images
            var viewMatrix     = message.ViewMatrix;
            var cameraPosition = message.CameraPosition;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null && message.LastMomentUpdateIndex != 0)
                {
                    MatrixD origin = MatrixD.Identity;
                    MyOpenVR.LMUMatrixGetOrigin(ref origin, message.LastMomentUpdateIndex);
                    viewMatrix = MatrixD.Invert(origin);
                }
            }

            var viewMatrixAt0 = viewMatrix;

            viewMatrixAt0.M14 = 0;
            viewMatrixAt0.M24 = 0;
            viewMatrixAt0.M34 = 0;
            viewMatrixAt0.M41 = 0;
            viewMatrixAt0.M42 = 0;
            viewMatrixAt0.M43 = 0;
            viewMatrixAt0.M44 = 1;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null)
                {
                    if (message.LastMomentUpdateIndex != 0)
                    {
                        var tViewMatrix = Matrix.Transpose(viewMatrix);
                        var viewHMDat0  = MyOpenVR.ViewHMD;
                        viewHMDat0.M14 = 0;
                        viewHMDat0.M24 = 0;
                        viewHMDat0.M34 = 0;
                        viewHMDat0.M41 = 0;
                        viewHMDat0.M42 = 0;
                        viewHMDat0.M43 = 0;
                        viewHMDat0.M44 = 1;

                        //cameraPosition += tViewMatrix.Up * MyOpenVR.ViewHMD.Translation.Y;
                        //cameraPosition += tViewMatrix.Backward * MyOpenVR.ViewHMD.Translation.X;
                        //cameraPosition += tViewMatrix.Right * MyOpenVR.ViewHMD.Translation.Z;

                        viewMatrixAt0 = viewMatrixAt0 * viewHMDat0;
                        viewMatrix    = viewMatrix * viewHMDat0;

                        if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix    = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                        }
                        else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix    = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                        }
                    }
                }
                else
                {
                    if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix    = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                    }
                    else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix    = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                    }
                }
            }

            var originalProjection = message.ProjectionMatrix;
            //var invOriginalProjection = Matrix.CreatePerspectiveFovRhInverse(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane, message.FarPlane);

            float aspectRatio = MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y;

            if (typeofEnv != MyStereoRegion.FULLSCREEN)
            {
                aspectRatio /= 2;
            }
            var renderProjection = Matrix.CreatePerspectiveFieldOfView(message.FOV, aspectRatio, message.FarPlane, message.NearPlane);
            var invProj          = Matrix.CreatePerspectiveFovRhInverse(message.FOV, aspectRatio, message.FarPlane, message.NearPlane);

            renderProjection = Matrix.CreatePerspectiveFovRhInfiniteComplementary(message.FOV, aspectRatio, message.NearPlane);
            invProj          = Matrix.CreatePerspectiveFovRhInfiniteComplementaryInverse(message.FOV, aspectRatio, message.NearPlane);

            var invView = Matrix.Transpose(viewMatrixAt0);

            invView.M41 = (float)cameraPosition.X;
            invView.M42 = (float)cameraPosition.Y;
            invView.M43 = (float)cameraPosition.Z;

            envMatrices.ViewAt0              = viewMatrixAt0;
            envMatrices.InvViewAt0           = Matrix.Transpose(viewMatrixAt0);
            envMatrices.ViewProjectionAt0    = viewMatrixAt0 * renderProjection;
            envMatrices.InvViewProjectionAt0 = invProj * Matrix.Transpose(viewMatrixAt0);
            cameraPosition.AssertIsValid();
            envMatrices.CameraPosition      = cameraPosition;
            envMatrices.View                = viewMatrix;
            envMatrices.ViewD               = viewMatrix;
            envMatrices.OriginalProjectionD = originalProjection;
            envMatrices.InvView             = invView;
            envMatrices.ViewProjection      = viewMatrix * renderProjection;
            envMatrices.InvViewProjection   = invProj * invView;
            envMatrices.Projection          = renderProjection;
            envMatrices.InvProjection       = invProj;

            envMatrices.ViewProjectionD = envMatrices.ViewD * (MatrixD)renderProjection;

            envMatrices.NearClipping             = message.NearPlane;
            envMatrices.FarClipping              = message.FarPlane;
            envMatrices.LargeDistanceFarClipping = message.FarPlane * 500.0f;
            envMatrices.FovY = message.FOV;

            MyUtils.Init(ref envMatrices.ViewFrustumD);
            envMatrices.ViewFrustumD.Matrix = envMatrices.ViewProjectionD;

            MyUtils.Init(ref envMatrices.ViewFrustumClippedD);
            envMatrices.ViewFrustumClippedD.Matrix = envMatrices.ViewD * envMatrices.OriginalProjectionD;
        }
Пример #9
0
        private static void UpdateFrameConstantsInternal(MyEnvironmentMatrices envMatrices, ref MyFrameConstantsLayout constants, MyStereoRegion typeofFC)
        {
            constants.Environment.View                = Matrix.Transpose(envMatrices.ViewAt0);
            constants.Environment.Projection          = Matrix.Transpose(envMatrices.Projection);
            constants.Environment.ViewProjection      = Matrix.Transpose(envMatrices.ViewProjectionAt0);
            constants.Environment.InvView             = Matrix.Transpose(envMatrices.InvViewAt0);
            constants.Environment.InvProjection       = Matrix.Transpose(envMatrices.InvProjection);
            constants.Environment.InvViewProjection   = Matrix.Transpose(envMatrices.InvViewProjectionAt0);
            constants.Environment.ViewProjectionWorld = Matrix.Transpose(envMatrices.ViewProjection);
            constants.Environment.WorldOffset         = new Vector4(envMatrices.CameraPosition, 0);

            constants.Screen.Resolution = MyRender11.ResolutionF;
            if (typeofFC != MyStereoRegion.FULLSCREEN)
            {
                constants.Screen.Resolution.X /= 2;

                Vector3 eyeOffset        = new Vector3(envMatrices.ViewAt0.M41, envMatrices.ViewAt0.M42, envMatrices.ViewAt0.M43);
                Vector3 eyeOffsetInWorld = Vector3.Transform(eyeOffset, Matrix.Transpose(MyRender11.Environment.Matrices.ViewAt0));
                constants.Environment.EyeOffsetInWorld = eyeOffsetInWorld;
            }

            constants.Screen.GBufferOffset = new Vector2I(0, 0);
            if (typeofFC == MyStereoRegion.RIGHT)
            {
                constants.Screen.GBufferOffset.X = MyRender11.ResolutionI.X / 2;
            }

            constants.Screen.ResolutionOfGBuffer = MyRender11.ResolutionI;
        }
Пример #10
0
        private static void UpdateFrameConstantsInternal(MyEnvironmentMatrices envMatrices, ref MyFrameConstantsLayout constants, MyStereoRegion typeofFC)
        {
            constants.Environment.View = Matrix.Transpose(envMatrices.ViewAt0);
            constants.Environment.Projection = Matrix.Transpose(envMatrices.Projection);
            constants.Environment.ViewProjection = Matrix.Transpose(envMatrices.ViewProjectionAt0);
            constants.Environment.InvView = Matrix.Transpose(envMatrices.InvViewAt0);
            constants.Environment.InvProjection = Matrix.Transpose(envMatrices.InvProjection);
            constants.Environment.InvViewProjection = Matrix.Transpose(envMatrices.InvViewProjectionAt0);
            constants.Environment.ViewProjectionWorld = Matrix.Transpose(envMatrices.ViewProjection);
            constants.Environment.WorldOffset = new Vector4(envMatrices.CameraPosition, 0);

            constants.Screen.Resolution = MyRender11.ResolutionF;
            if (typeofFC != MyStereoRegion.FULLSCREEN)
            {
                constants.Screen.Resolution.X /= 2;

                Vector3 eyeOffset = new Vector3(envMatrices.ViewAt0.M41, envMatrices.ViewAt0.M42, envMatrices.ViewAt0.M43);
                Vector3 eyeOffsetInWorld = Vector3.Transform(eyeOffset, Matrix.Transpose(MyRender11.Environment.Matrices.ViewAt0));
                constants.Environment.EyeOffsetInWorld = eyeOffsetInWorld;
            }

            constants.Screen.GBufferOffset = new Vector2I(0, 0);
            if (typeofFC == MyStereoRegion.RIGHT)
                constants.Screen.GBufferOffset.X = MyRender11.ResolutionI.X / 2;

            constants.Screen.ResolutionOfGBuffer = MyRender11.ResolutionI;
        }
Пример #11
0
        private static void SetupCameraMatricesInternal(MyRenderMessageSetCameraViewMatrix message, MyEnvironmentMatrices envMatrices, MyStereoRegion typeofEnv)
        {//uses m_leftEye to handle HMD images

            var viewMatrix = message.ViewMatrix;
            var cameraPosition = message.CameraPosition;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null && message.LastMomentUpdateIndex != 0)
                {
                    MatrixD origin = MatrixD.Identity;
                    MyOpenVR.LMUMatrixGetOrigin(ref origin, message.LastMomentUpdateIndex);
                    viewMatrix = MatrixD.Invert(origin);
                }
            }

            var viewMatrixAt0 = viewMatrix;
            viewMatrixAt0.M14 = 0;
            viewMatrixAt0.M24 = 0;
            viewMatrixAt0.M34 = 0;
            viewMatrixAt0.M41 = 0;
            viewMatrixAt0.M42 = 0;
            viewMatrixAt0.M43 = 0;
            viewMatrixAt0.M44 = 1;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null)
                {
                    if (message.LastMomentUpdateIndex != 0)
                    {
                        var tViewMatrix = Matrix.Transpose(viewMatrix);
                        var viewHMDat0 = MyOpenVR.ViewHMD;
                        viewHMDat0.M14 = 0;
                        viewHMDat0.M24 = 0;
                        viewHMDat0.M34 = 0;
                        viewHMDat0.M41 = 0;
                        viewHMDat0.M42 = 0;
                        viewHMDat0.M43 = 0;
                        viewHMDat0.M44 = 1;

                        //cameraPosition += tViewMatrix.Up * MyOpenVR.ViewHMD.Translation.Y;
                        //cameraPosition += tViewMatrix.Backward * MyOpenVR.ViewHMD.Translation.X;
                        //cameraPosition += tViewMatrix.Right * MyOpenVR.ViewHMD.Translation.Z;

                        viewMatrixAt0 = viewMatrixAt0 * viewHMDat0;
                        viewMatrix = viewMatrix * viewHMDat0;

                        if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                        }
                        else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                        }
                    }
                }
                else
                {
                    if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                    }
                    else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                    }
                }
            }

            var originalProjection = message.ProjectionMatrix;
            //var invOriginalProjection = Matrix.CreatePerspectiveFovRhInverse(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane, message.FarPlane);

            float aspectRatio = MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y;
            if (typeofEnv != MyStereoRegion.FULLSCREEN)
                aspectRatio /= 2;
            var renderProjection = Matrix.CreatePerspectiveFieldOfView(message.FOV, aspectRatio, message.FarPlane, message.NearPlane);
            var invProj = Matrix.CreatePerspectiveFovRhInverse(message.FOV, aspectRatio, message.FarPlane, message.NearPlane);

            renderProjection = Matrix.CreatePerspectiveFovRhInfiniteComplementary(message.FOV, aspectRatio, message.NearPlane);
            invProj = Matrix.CreatePerspectiveFovRhInfiniteComplementaryInverse(message.FOV, aspectRatio, message.NearPlane);

            var invView = Matrix.Transpose(viewMatrixAt0);
            invView.M41 = (float)cameraPosition.X;
            invView.M42 = (float)cameraPosition.Y;
            invView.M43 = (float)cameraPosition.Z;

            envMatrices.ViewAt0 = viewMatrixAt0;
            envMatrices.InvViewAt0 = Matrix.Transpose(viewMatrixAt0);
            envMatrices.ViewProjectionAt0 = viewMatrixAt0 * renderProjection;
            envMatrices.InvViewProjectionAt0 = invProj * Matrix.Transpose(viewMatrixAt0);
            cameraPosition.AssertIsValid();
            envMatrices.CameraPosition = cameraPosition;
            envMatrices.View = viewMatrix;
            envMatrices.ViewD = viewMatrix;
            envMatrices.OriginalProjectionD = originalProjection;
            envMatrices.InvView = invView;
            envMatrices.ViewProjection = viewMatrix * renderProjection;
            envMatrices.InvViewProjection = invProj * invView;
            envMatrices.Projection = renderProjection;
            envMatrices.InvProjection = invProj;

            envMatrices.ViewProjectionD = envMatrices.ViewD * (MatrixD)renderProjection;
            
            envMatrices.NearClipping = message.NearPlane;
            envMatrices.FarClipping = message.FarPlane;
            envMatrices.LargeDistanceFarClipping = message.FarPlane*500.0f;
            envMatrices.FovY = message.FOV;

            MyUtils.Init(ref envMatrices.ViewFrustumD);
            envMatrices.ViewFrustumD.Matrix = envMatrices.ViewProjectionD;

            MyUtils.Init(ref envMatrices.ViewFrustumClippedD);
            envMatrices.ViewFrustumClippedD.Matrix = envMatrices.ViewD * envMatrices.OriginalProjectionD;
        }
Пример #12
0
        private static void SetupCameraMatricesInternal(MyRenderMessageSetCameraViewMatrix message, MyEnvironmentMatrices envMatrices, MyStereoRegion typeofEnv)
        {
            var originalProjection = message.ProjectionMatrix;
            var viewMatrix = message.ViewMatrix;
            var cameraPosition = message.CameraPosition;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null && message.LastMomentUpdateIndex != 0)
                {
                    MatrixD origin = MatrixD.Identity;
                    MyOpenVR.LMUMatrixGetOrigin(ref origin, message.LastMomentUpdateIndex);
                    viewMatrix = MatrixD.Invert(origin);
                }
            }

            var viewMatrixAt0 = viewMatrix;
            viewMatrixAt0.M14 = 0;
            viewMatrixAt0.M24 = 0;
            viewMatrixAt0.M34 = 0;
            viewMatrixAt0.M41 = 0;
            viewMatrixAt0.M42 = 0;
            viewMatrixAt0.M43 = 0;
            viewMatrixAt0.M44 = 1;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null)
                {
                    if (message.LastMomentUpdateIndex != 0)
                    {
                        var tViewMatrix = Matrix.Transpose(viewMatrix);
                        var viewHMDat0 = MyOpenVR.ViewHMD;
                        viewHMDat0.M14 = 0;
                        viewHMDat0.M24 = 0;
                        viewHMDat0.M34 = 0;
                        viewHMDat0.M41 = 0;
                        viewHMDat0.M42 = 0;
                        viewHMDat0.M43 = 0;
                        viewHMDat0.M44 = 1;

                        //cameraPosition += tViewMatrix.Up * MyOpenVR.ViewHMD.Translation.Y;
                        //cameraPosition += tViewMatrix.Backward * MyOpenVR.ViewHMD.Translation.X;
                        //cameraPosition += tViewMatrix.Right * MyOpenVR.ViewHMD.Translation.Z;

                        viewMatrixAt0 = viewMatrixAt0 * viewHMDat0;
                        viewMatrix = viewMatrix * viewHMDat0;

                        if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                        }
                        else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                        }
                    }
                }
                else
                {
                    if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                    }
                    else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                    }
                }
            }

            float aspectRatio = MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y;
            if (typeofEnv != MyStereoRegion.FULLSCREEN)
                aspectRatio /= 2;
            Matrix projMatrix = Matrix.CreatePerspectiveFovRhInfiniteComplementary(message.FOV, aspectRatio, message.NearPlane);

            cameraPosition.AssertIsValid();

            envMatrices.ViewAt0 = viewMatrixAt0;
            envMatrices.InvViewAt0 = Matrix.Invert(viewMatrixAt0);
            envMatrices.ViewProjectionAt0 = viewMatrixAt0 * projMatrix;
            envMatrices.InvViewProjectionAt0 = Matrix.Invert(viewMatrixAt0 * projMatrix);
            envMatrices.CameraPosition = cameraPosition;
            envMatrices.View = viewMatrix;
            envMatrices.ViewD = viewMatrix;
            envMatrices.OriginalProjectionD = originalProjection;
            envMatrices.InvView = Matrix.Invert(viewMatrix);
            envMatrices.ViewProjection = viewMatrix * projMatrix;
            envMatrices.InvViewProjection = Matrix.Invert(viewMatrix * projMatrix);
            envMatrices.Projection = projMatrix;
            envMatrices.InvProjection = Matrix.Invert(projMatrix);
            envMatrices.ViewProjectionD = envMatrices.ViewD * (MatrixD)projMatrix;
            envMatrices.NearClipping = message.NearPlane;
            envMatrices.FarClipping = message.FarPlane;
            envMatrices.LargeDistanceFarClipping = message.FarPlane * 500.0f;

            int width = MyRender11.ViewportResolution.X;
            int height = MyRender11.ViewportResolution.Y;
            float fovH = message.FOV;
            envMatrices.FovH = fovH;
            envMatrices.FovV = (float)(2 * Math.Atan(Math.Tan(fovH / 2.0) * (width / (double)height)));

            MyUtils.Init(ref envMatrices.ViewFrustumD);
            envMatrices.ViewFrustumD.Matrix = envMatrices.ViewProjectionD;

            MyUtils.Init(ref envMatrices.ViewFrustumClippedD);
            envMatrices.ViewFrustumClippedD.Matrix = envMatrices.ViewD * envMatrices.OriginalProjectionD;
        }
Пример #13
0
        // IMPORTANT: The returned object needs to be returned to MyManagers.RwTexturePool after the usage
        internal static IBorrowedUavTexture Run(ISrvBindable src, ISrvBindable srcGBuffer2, ISrvBindable srcDepth)
        {
            RC.ComputeShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
            RC.ComputeShader.SetSampler(0, MySamplerStateManager.Default);

            int    screenX       = MyRender11.ResolutionI.X;
            int    screenY       = MyRender11.ResolutionI.Y;
            Format formatLBuffer = MyGBuffer.LBufferFormat;
            MyBorrowedRwTextureManager rwTexturePool       = MyManagers.RwTexturesPool;
            IBorrowedUavTexture        uavHalfScreen       = rwTexturePool.BorrowUav("MyBloom.UavHalfScreen", screenX / 2, screenY / 2, formatLBuffer);
            IBorrowedUavTexture        uavBlurScreen       = rwTexturePool.BorrowUav("MyBloom.UavBlurScreen", screenX / BLOOM_TARGET_SIZE_DIVIDER, screenY / BLOOM_TARGET_SIZE_DIVIDER, formatLBuffer);
            IBorrowedUavTexture        uavBlurScreenHelper = rwTexturePool.BorrowUav("MyBloom.UavBlurScreenHelper", screenX / BLOOM_TARGET_SIZE_DIVIDER, screenY / BLOOM_TARGET_SIZE_DIVIDER, formatLBuffer);

            RC.ComputeShader.SetUav(0, uavHalfScreen);
            RC.ComputeShader.SetSrv(0, src);
            RC.ComputeShader.SetSrv(1, srcGBuffer2);
            RC.ComputeShader.SetSrv(2, srcDepth);

            RC.ComputeShader.Set(m_bloomShader);

            var size = uavHalfScreen.Size;

            VRageMath.Vector2I threadGroups = new VRageMath.Vector2I((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads);
            RC.Dispatch(threadGroups.X, threadGroups.Y, 1);

            bool skipDownScale = false;

            switch (BLOOM_TARGET_SIZE_DIVIDER)
            {
            case 2:
                skipDownScale = true;
                break;

            case 4:
                RC.ComputeShader.Set(m_downscale2Shader);
                break;

            case 8:
                RC.ComputeShader.Set(m_downscale4Shader);
                break;

            default:
                MyRenderProxy.Assert(false, "Invalid bloom target size divider");
                break;
            }
            size         = uavBlurScreen.Size;
            threadGroups = new VRageMath.Vector2I((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads);
            if (!skipDownScale)
            {
                RC.ComputeShader.SetConstantBuffer(1, GetCBSize(uavHalfScreen.Size.X, uavHalfScreen.Size.Y));

                RC.ComputeShader.SetUav(0, uavBlurScreen);
                RC.ComputeShader.SetSrv(0, uavHalfScreen);
                RC.Dispatch(threadGroups.X, threadGroups.Y, 1);
            }

            RC.ComputeShader.SetConstantBuffer(1, GetCB_blur(MyStereoRegion.FULLSCREEN, size));
            RC.ComputeShader.Set(m_blurV[MyRender11.Postprocess.BloomSize]);
            RC.ComputeShader.SetUav(0, uavBlurScreenHelper);
            RC.ComputeShader.SetSrv(0, uavBlurScreen);
            RC.Dispatch(threadGroups.X, threadGroups.Y, 1);
            RC.ComputeShader.SetSrv(0, null);
            RC.ComputeShader.SetUav(0, null);

            RC.ComputeShader.Set(m_blurH[MyRender11.Postprocess.BloomSize]);
            RC.ComputeShader.SetUav(0, uavBlurScreen);
            RC.ComputeShader.SetSrv(0, uavBlurScreenHelper);

            int nPasses = 1;

            if (MyStereoRender.Enable)
            {
                threadGroups.X /= 2;
                nPasses         = 2;
            }
            for (int nPass = 0; nPass < nPasses; nPass++)
            {
                MyStereoRegion region = MyStereoRegion.FULLSCREEN;
                if (MyStereoRender.Enable)
                {
                    region = nPass == 0 ? MyStereoRegion.LEFT : MyStereoRegion.RIGHT;
                }

                RC.ComputeShader.SetConstantBuffer(1, GetCB_blur(region, size));
                RC.Dispatch(threadGroups.X, threadGroups.Y, 1);
            }

            if (MyRender11.Settings.DisplayBloomFilter)
            {
                MyDebugTextureDisplay.Select(uavHalfScreen);
            }
            else if (MyRender11.Settings.DisplayBloomMin)
            {
                MyDebugTextureDisplay.Select(uavBlurScreen);
            }

            RC.ComputeShader.SetUav(0, null);
            RC.ComputeShader.SetSrv(0, null);
            uavHalfScreen.Release();
            uavBlurScreenHelper.Release();

            return(uavBlurScreen);
        }
Пример #14
0
        private static void SetupCameraMatricesInternal(MyRenderMessageSetCameraViewMatrix message, MyEnvironmentMatrices envMatrices, MyStereoRegion typeofEnv)
        {
            var originalProjection = message.ProjectionMatrix;
            var viewMatrix         = message.ViewMatrix;
            var cameraPosition     = message.CameraPosition;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null && message.LastMomentUpdateIndex != 0)
                {
                    MatrixD origin = MatrixD.Identity;
                    MyOpenVR.LMUMatrixGetOrigin(ref origin, message.LastMomentUpdateIndex);
                    viewMatrix = MatrixD.Invert(origin);
                }
            }

            var viewMatrixAt0 = viewMatrix;

            viewMatrixAt0.M14 = 0;
            viewMatrixAt0.M24 = 0;
            viewMatrixAt0.M34 = 0;
            viewMatrixAt0.M41 = 0;
            viewMatrixAt0.M42 = 0;
            viewMatrixAt0.M43 = 0;
            viewMatrixAt0.M44 = 1;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null)
                {
                    if (message.LastMomentUpdateIndex != 0)
                    {
                        var tViewMatrix = Matrix.Transpose(viewMatrix);
                        var viewHMDat0  = MyOpenVR.ViewHMD;
                        viewHMDat0.M14 = 0;
                        viewHMDat0.M24 = 0;
                        viewHMDat0.M34 = 0;
                        viewHMDat0.M41 = 0;
                        viewHMDat0.M42 = 0;
                        viewHMDat0.M43 = 0;
                        viewHMDat0.M44 = 1;

                        //cameraPosition += tViewMatrix.Up * MyOpenVR.ViewHMD.Translation.Y;
                        //cameraPosition += tViewMatrix.Backward * MyOpenVR.ViewHMD.Translation.X;
                        //cameraPosition += tViewMatrix.Right * MyOpenVR.ViewHMD.Translation.Z;

                        viewMatrixAt0 = viewMatrixAt0 * viewHMDat0;
                        viewMatrix    = viewMatrix * viewHMDat0;

                        if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix    = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                        }
                        else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix    = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                        }
                    }
                }
                else
                {
                    if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix    = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                    }
                    else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix    = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                    }
                }
            }

            float aspectRatio = MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y;

            if (typeofEnv != MyStereoRegion.FULLSCREEN)
            {
                aspectRatio /= 2;
            }
            Matrix projMatrix = Matrix.CreatePerspectiveFovRhInfiniteComplementary(message.FOV, aspectRatio, message.NearPlane);

            cameraPosition.AssertIsValid();

            envMatrices.ViewAt0              = viewMatrixAt0;
            envMatrices.InvViewAt0           = Matrix.Invert(viewMatrixAt0);
            envMatrices.ViewProjectionAt0    = viewMatrixAt0 * projMatrix;
            envMatrices.InvViewProjectionAt0 = Matrix.Invert(viewMatrixAt0 * projMatrix);
            envMatrices.CameraPosition       = cameraPosition;
            envMatrices.View  = viewMatrix;
            envMatrices.ViewD = viewMatrix;
            envMatrices.OriginalProjectionD      = originalProjection;
            envMatrices.InvView                  = Matrix.Invert(viewMatrix);
            envMatrices.ViewProjection           = viewMatrix * projMatrix;
            envMatrices.InvViewProjection        = Matrix.Invert(viewMatrix * projMatrix);
            envMatrices.Projection               = projMatrix;
            envMatrices.InvProjection            = Matrix.Invert(projMatrix);
            envMatrices.ViewProjectionD          = envMatrices.ViewD * (MatrixD)projMatrix;
            envMatrices.NearClipping             = message.NearPlane;
            envMatrices.FarClipping              = message.FarPlane;
            envMatrices.LargeDistanceFarClipping = message.FarPlane * 500.0f;

            int   width  = MyRender11.ViewportResolution.X;
            int   height = MyRender11.ViewportResolution.Y;
            float fovH   = message.FOV;

            envMatrices.FovH = fovH;
            envMatrices.FovV = (float)(2 * Math.Atan(Math.Tan(fovH / 2.0) * (height / (double)width)));

            MyUtils.Init(ref envMatrices.ViewFrustumD);
            envMatrices.ViewFrustumD.Matrix = envMatrices.ViewProjectionD;

            MyUtils.Init(ref envMatrices.ViewFrustumClippedD);
            envMatrices.ViewFrustumClippedD.Matrix = envMatrices.ViewD * envMatrices.OriginalProjectionD;
        }