Example #1
0
        static GlobalConstantBuffer InitConstantBuffer(MyViewport viewport)
        {
            GlobalConstantBuffer m_Data = new GlobalConstantBuffer();
            Matrix m = MyRender11.Environment.Matrices.Projection;

            // ProjectionMatrixInfo
            // In matrices generated with D3DXMatrixPerspectiveFovRH
            // A = zf/(zn-zf)
            // B = zn*zf/(zn-zf)
            // C = -1
            float A = m.M33;
            float B = m.M43;

            // Rely on INFs to be generated in case of any divisions by zero
            float zNear = B / A;
            float zFar = B / (A + 1);
            // Some matrices may use negative m00 or m11 terms to flip X/Y axises
            float tanHalfFovX = 1 / Math.Abs(m.M11);
            float tanHalfFovY = 1 / Math.Abs(m.M22);

            // SetDepthLinearizationConstants
            const float EPSILON = 1e-6f;
            float inverseZNear = Math.Max(1 / zNear, EPSILON);
            float inverseZFar = Math.Max(1 / zFar, EPSILON);
            m_Data.LinearizeDepthA = inverseZFar - inverseZNear;
            m_Data.LinearizeDepthB = inverseZNear;

            // SetViewportConstants
            m_Data.InverseDepthRangeA = 1f;
            m_Data.InverseDepthRangeB = 0f;

            m_Data.InputViewportTopLeft.X = viewport.OffsetX;
            m_Data.InputViewportTopLeft.Y = viewport.OffsetY;

            // SetProjectionConstants
            m_Data.UVToViewA.X = 2 * tanHalfFovX;
            m_Data.UVToViewA.Y = -2 * tanHalfFovY;
            m_Data.UVToViewB.X = -1 * tanHalfFovX;
            m_Data.UVToViewB.Y = 1 * tanHalfFovY;

            // SetResolutionConstants
            m_Data.InvFullResolution.X = 1.0f / viewport.Width;
            m_Data.InvFullResolution.Y = 1.0f / viewport.Height;
            m_Data.InvQuarterResolution.X = 1.0f / DivUp((int)viewport.Width, 4);
            m_Data.InvQuarterResolution.Y = 1.0f / DivUp((int)viewport.Height, 4);

            // SetNormalData
            m_Data.NormalMatrix = MyRender11.Environment.Matrices.ViewAt0;
            m_Data.NormalDecodeScale = 2;
            m_Data.NormalDecodeBias = -1;

            // SetAORadiusConstants
            float radiusInMeters = Math.Max(Params.Radius, EPSILON);
            float r = radiusInMeters * METERS_TO_VIEW_SPACE_UNITS;
            m_Data.R2 = r * r;
            m_Data.NegInvR2 = -1 / m_Data.R2;

            m_Data.RadiusToScreen = r * 0.5f / tanHalfFovY * viewport.Height;

            float backgroundViewDepth = Math.Max(Params.BackgroundViewDepth, EPSILON);
            if (Params.AdaptToFOV)
            {
                // use larger background view depth for low FOV values (less then 30 degrees)
                float factor = Math.Min(1.0f, MyRender11.Environment.Matrices.FovH / MathHelper.ToRadians(30));
                backgroundViewDepth = MathHelper.Lerp(6000, backgroundViewDepth, factor);
            }
            m_Data.BackgroundAORadiusPixels = m_Data.RadiusToScreen / backgroundViewDepth;

            float foregroundViewDepth = Math.Max(Params.ForegroundViewDepth, EPSILON);
            m_Data.ForegroundAORadiusPixels = m_Data.RadiusToScreen / foregroundViewDepth;

            // SetBlurConstants
            float BaseSharpness = Math.Max(Params.BlurSharpness, 0);
            BaseSharpness /= METERS_TO_VIEW_SPACE_UNITS;

            if (Params.BlurSharpnessFunctionEnable)
            {
                m_Data.BlurViewDepth0 = Math.Max(Params.BlurSharpnessFunctionForegroundViewDepth, 0);
                m_Data.BlurViewDepth1 = Math.Max(Params.BlurSharpnessFunctionBackgroundViewDepth, m_Data.BlurViewDepth0 + EPSILON);
                m_Data.BlurSharpness0 = BaseSharpness * Math.Max(Params.BlurSharpnessFunctionForegroundScale, 0);
                m_Data.BlurSharpness1 = BaseSharpness;
            }
            else
            {
                m_Data.BlurSharpness0 = BaseSharpness;
                m_Data.BlurSharpness1 = BaseSharpness;
                m_Data.BlurViewDepth0 = 0;
                m_Data.BlurViewDepth1 = 1;
            }

            // SetDepthThresholdConstants
            if (Params.DepthThresholdEnable)
            {
                m_Data.ViewDepthThresholdNegInv = -1 / Math.Max(Params.DepthThreshold, EPSILON);
                m_Data.ViewDepthThresholdSharpness = Math.Max(Params.DepthThresholdSharpness, 0);
            }
            else
            {
                m_Data.ViewDepthThresholdNegInv = 0;
                m_Data.ViewDepthThresholdSharpness = 1;
            }

            // SetAOParameters
            m_Data.PowExponent = Math.Min(Math.Max(Params.PowerExponent, 1), 8);
            m_Data.NDotVBias = Math.Min(Math.Max(Params.Bias, 0.0f), 0.5f);

            float aoAmountScaleFactor = 1 / (1 - m_Data.NDotVBias);
            m_Data.SmallScaleAOAmount = Math.Min(Math.Max(Params.SmallScaleAO, 0), 4) * aoAmountScaleFactor * 2;
            m_Data.LargeScaleAOAmount = Math.Min(Math.Max(Params.LargeScaleAO, 0), 4) * aoAmountScaleFactor;

            return m_Data;
        }
Example #2
0
        static GlobalConstantBuffer InitConstantBuffer(MyViewport viewport)
        {
            GlobalConstantBuffer m_Data = new GlobalConstantBuffer();
            Matrix m = MyRender11.Environment.Matrices.Projection;

            // ProjectionMatrixInfo
            // In matrices generated with D3DXMatrixPerspectiveFovRH
            // A = zf/(zn-zf)
            // B = zn*zf/(zn-zf)
            // C = -1
            float A = m.M33;
            float B = m.M43;

            // Rely on INFs to be generated in case of any divisions by zero
            float zNear = B / A;
            float zFar  = B / (A + 1);
            // Some matrices may use negative m00 or m11 terms to flip X/Y axises
            float tanHalfFovX = 1 / Math.Abs(m.M11);
            float tanHalfFovY = 1 / Math.Abs(m.M22);

            // SetDepthLinearizationConstants
            const float EPSILON      = 1e-6f;
            float       inverseZNear = Math.Max(1 / zNear, EPSILON);
            float       inverseZFar  = Math.Max(1 / zFar, EPSILON);

            m_Data.LinearizeDepthA = inverseZFar - inverseZNear;
            m_Data.LinearizeDepthB = inverseZNear;

            // SetViewportConstants
            m_Data.InverseDepthRangeA = 1f;
            m_Data.InverseDepthRangeB = 0f;

            m_Data.InputViewportTopLeft.X = viewport.OffsetX;
            m_Data.InputViewportTopLeft.Y = viewport.OffsetY;

            // SetProjectionConstants
            m_Data.UVToViewA.X = 2 * tanHalfFovX;
            m_Data.UVToViewA.Y = -2 * tanHalfFovY;
            m_Data.UVToViewB.X = -1 * tanHalfFovX;
            m_Data.UVToViewB.Y = 1 * tanHalfFovY;

            // SetResolutionConstants
            m_Data.InvFullResolution.X    = 1.0f / viewport.Width;
            m_Data.InvFullResolution.Y    = 1.0f / viewport.Height;
            m_Data.InvQuarterResolution.X = 1.0f / DivUp((int)viewport.Width, 4);
            m_Data.InvQuarterResolution.Y = 1.0f / DivUp((int)viewport.Height, 4);

            // SetNormalData
            m_Data.NormalMatrix      = MyRender11.Environment.Matrices.ViewAt0;
            m_Data.NormalDecodeScale = 2;
            m_Data.NormalDecodeBias  = -1;

            // SetAORadiusConstants
            float radiusInMeters = Math.Max(Params.Radius, EPSILON);
            float r = radiusInMeters * METERS_TO_VIEW_SPACE_UNITS;

            m_Data.R2       = r * r;
            m_Data.NegInvR2 = -1 / m_Data.R2;

            m_Data.RadiusToScreen = r * 0.5f / tanHalfFovY * viewport.Height;

            float backgroundViewDepth = Math.Max(Params.BackgroundViewDepth, EPSILON);

            m_Data.BackgroundAORadiusPixels = m_Data.RadiusToScreen / backgroundViewDepth;

            float foregroundViewDepth = Math.Max(Params.ForegroundViewDepth, EPSILON);

            m_Data.ForegroundAORadiusPixels = m_Data.RadiusToScreen / foregroundViewDepth;

            // SetBlurConstants
            float BaseSharpness = Math.Max(Params.BlurSharpness, 0);

            BaseSharpness /= METERS_TO_VIEW_SPACE_UNITS;

            if (Params.BlurSharpnessFunctionEnable)
            {
                m_Data.BlurViewDepth0 = Math.Max(Params.BlurSharpnessFunctionForegroundViewDepth, 0);
                m_Data.BlurViewDepth1 = Math.Max(Params.BlurSharpnessFunctionBackgroundViewDepth, m_Data.BlurViewDepth0 + EPSILON);
                m_Data.BlurSharpness0 = BaseSharpness * Math.Max(Params.BlurSharpnessFunctionForegroundScale, 0);
                m_Data.BlurSharpness1 = BaseSharpness;
            }
            else
            {
                m_Data.BlurSharpness0 = BaseSharpness;
                m_Data.BlurSharpness1 = BaseSharpness;
                m_Data.BlurViewDepth0 = 0;
                m_Data.BlurViewDepth1 = 1;
            }

            // SetDepthThresholdConstants
            if (Params.DepthThresholdEnable)
            {
                m_Data.ViewDepthThresholdNegInv    = -1 / Math.Max(Params.DepthThreshold, EPSILON);
                m_Data.ViewDepthThresholdSharpness = Math.Max(Params.DepthThresholdSharpness, 0);
            }
            else
            {
                m_Data.ViewDepthThresholdNegInv    = 0;
                m_Data.ViewDepthThresholdSharpness = 1;
            }

            // SetAOParameters
            m_Data.PowExponent = Math.Min(Math.Max(Params.PowerExponent, 1), 8);
            m_Data.NDotVBias   = Math.Min(Math.Max(Params.Bias, 0.0f), 0.5f);

            float aoAmountScaleFactor = 1 / (1 - m_Data.NDotVBias);

            m_Data.SmallScaleAOAmount = Math.Min(Math.Max(Params.SmallScaleAO, 0), 4) * aoAmountScaleFactor * 2;
            m_Data.LargeScaleAOAmount = Math.Min(Math.Max(Params.LargeScaleAO, 0), 4) * aoAmountScaleFactor;

            return(m_Data);
        }