Exemplo n.º 1
0
        internal unsafe static void RunForwardPostprocess(IRtvBindable rt, ISrvBindable depth, ref Matrix viewMatrix, uint? atmosphereId)
        {
            MyGpuProfiler.IC_BeginBlock("Postprocess");
            var transpose = Matrix.Transpose(viewMatrix);
            var mapping = MyMapping.MapDiscard(RC, MyCommon.ProjectionConstants);
            mapping.WriteAndPosition(ref transpose);
            mapping.Unmap();

            RC.AllShaderStages.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
            RC.AllShaderStages.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);

            RC.SetDepthStencilState(MyDepthStencilStateManager.IgnoreDepthStencil);
            RC.SetRtv(rt);
            RC.PixelShader.SetSrv(0, depth);
            MyFileTextureManager texManager = MyManagers.FileTextures;
            RC.PixelShader.SetSrv(MyCommon.SKYBOX_SLOT, texManager.GetTexture(MyRender11.Environment.Data.Skybox, MyFileTextureEnum.CUBEMAP, true));
            RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers);
            RC.PixelShader.Set(m_ps);

            MyScreenPass.DrawFullscreenQuad(new MyViewport(m_viewportSize, m_viewportSize));
            MyGpuProfiler.IC_EndBlock();

            MyGpuProfiler.IC_BeginBlock("Atmosphere");
            if (atmosphereId != null)
            {
                var atmosphere = MyAtmosphereRenderer.GetAtmosphere(atmosphereId.Value);
                var constants = new AtmosphereConstants();
                //TODO(AF) These values are computed in MyAtmosphere as well. Find a way to remove the duplication
                var worldMatrix = atmosphere.WorldMatrix;
                worldMatrix.Translation -= MyRender11.Environment.Matrices.CameraPosition;

                double distance = worldMatrix.Translation.Length();
                double atmosphereTop = atmosphere.AtmosphereRadius * atmosphere.Settings.AtmosphereTopModifier * atmosphere.PlanetScaleFactor * atmosphere.Settings.RayleighTransitionModifier;
                float rayleighHeight = atmosphere.Settings.RayleighHeight;
                float t = 0.0f;
                if (distance > atmosphereTop)
                {
                    if (distance > atmosphereTop * 2.0f)
                    {
                        t = 1.0f;
                    }
                    else
                    {
                        t = (float)((distance - atmosphereTop) / atmosphereTop);
                    }
                }
                rayleighHeight = MathHelper.Lerp(atmosphere.Settings.RayleighHeight, atmosphere.Settings.RayleighHeightSpace, t);


                constants.PlanetCentre = (Vector3)worldMatrix.Translation;
                constants.AtmosphereRadius = atmosphere.AtmosphereRadius * atmosphere.Settings.AtmosphereTopModifier;
                constants.GroundRadius = atmosphere.PlanetRadius * 1.01f * atmosphere.Settings.SeaLevelModifier;
                constants.BetaRayleighScattering = atmosphere.BetaRayleighScattering / atmosphere.Settings.RayleighScattering;
                constants.BetaMieScattering = atmosphere.BetaMieScattering / atmosphere.Settings.MieColorScattering;
                constants.HeightScaleRayleighMie = atmosphere.HeightScaleRayleighMie * new Vector2(rayleighHeight, atmosphere.Settings.MieHeight);
                constants.MieG = atmosphere.Settings.MieG;
                constants.PlanetScaleFactor = atmosphere.PlanetScaleFactor;
                constants.AtmosphereScaleFactor = atmosphere.AtmosphereScaleFactor;
                constants.Intensity = atmosphere.Settings.Intensity;
                constants.FogIntensity = atmosphere.Settings.FogIntensity;
            
                var cb = MyCommon.GetObjectCB(sizeof(AtmosphereConstants));
            
                mapping = MyMapping.MapDiscard(RC, cb);
                mapping.WriteAndPosition(ref constants);
                mapping.Unmap();

                RC.SetBlendState(MyBlendStateManager.BlendAdditive);
                RC.PixelShader.SetConstantBuffer(2, cb);
                RC.PixelShader.SetSrv(2, MyAtmosphereRenderer.GetAtmosphereLuts(atmosphereId.Value).TransmittanceLut);
                RC.PixelShader.Set(m_atmosphere);

                MyScreenPass.DrawFullscreenQuad(new MyViewport(MyEnvironmentProbe.CubeMapResolution, MyEnvironmentProbe.CubeMapResolution));
            }
            MyGpuProfiler.IC_EndBlock();

            RC.SetRtv(null);

        }
Exemplo n.º 2
0
        internal unsafe static void RunForwardPostprocess(RenderTargetView rt, ShaderResourceView depth, ref Matrix viewMatrix, uint?atmosphereId)
        {
            var transpose = Matrix.Transpose(viewMatrix);
            var mapping   = MyMapping.MapDiscard(RC.DeviceContext, MyCommon.ProjectionConstants);

            mapping.WriteAndPosition(ref transpose);
            mapping.Unmap();

            RC.SetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);

            RC.DeviceContext.OutputMerger.SetTargets(rt);
            RC.DeviceContext.PixelShader.SetShaderResource(0, depth);
            RC.DeviceContext.PixelShader.SetShaderResource(MyCommon.SKYBOX_SLOT, MyTextures.GetView(MyTextures.GetTexture(MyEnvironment.DaySkybox, MyTextureEnum.CUBEMAP, true)));
            RC.DeviceContext.PixelShader.SetShaderResource(MyCommon.SKYBOX2_SLOT, MyTextures.GetView(MyTextures.GetTexture(MyEnvironment.NightSkybox, MyTextureEnum.CUBEMAP, true)));
            RC.DeviceContext.PixelShader.SetSamplers(0, MyRender11.StandardSamplers);
            RC.DeviceContext.PixelShader.Set(m_ps);

            MyScreenPass.DrawFullscreenQuad(new MyViewport(256, 256));

            if (atmosphereId != null)
            {
                var atmosphere = MyAtmosphereRenderer.GetAtmosphere(atmosphereId.Value);
                var constants  = new AtmosphereConstants();
                //TODO(AF) These values are computed in MyAtmosphere as well. Find a way to remove the duplication
                var worldMatrix = atmosphere.WorldMatrix;
                worldMatrix.Translation -= MyEnvironment.CameraPosition;

                double distance       = worldMatrix.Translation.Length();
                double atmosphereTop  = atmosphere.AtmosphereRadius * atmosphere.Settings.AtmosphereTopModifier * atmosphere.PlanetScaleFactor * atmosphere.Settings.RayleighTransitionModifier;
                float  rayleighHeight = atmosphere.Settings.RayleighHeight;
                float  t = 0.0f;
                if (distance > atmosphereTop)
                {
                    if (distance > atmosphereTop * 2.0f)
                    {
                        t = 1.0f;
                    }
                    else
                    {
                        t = (float)((distance - atmosphereTop) / atmosphereTop);
                    }
                }
                rayleighHeight = MathHelper.Lerp(atmosphere.Settings.RayleighHeight, atmosphere.Settings.RayleighHeightSpace, t);


                constants.PlanetCentre           = (Vector3)worldMatrix.Translation;
                constants.AtmosphereRadius       = atmosphere.AtmosphereRadius * atmosphere.Settings.AtmosphereTopModifier;
                constants.GroundRadius           = atmosphere.PlanetRadius * 1.01f * atmosphere.Settings.SeaLevelModifier;
                constants.BetaRayleighScattering = atmosphere.BetaRayleighScattering / atmosphere.Settings.RayleighScattering;
                constants.BetaMieScattering      = atmosphere.BetaMieScattering / atmosphere.Settings.MieColorScattering;
                constants.HeightScaleRayleighMie = atmosphere.HeightScaleRayleighMie * new Vector2(rayleighHeight, atmosphere.Settings.MieHeight);
                constants.MieG = atmosphere.Settings.MieG;
                constants.PlanetScaleFactor     = atmosphere.PlanetScaleFactor;
                constants.AtmosphereScaleFactor = atmosphere.AtmosphereScaleFactor;
                constants.Intensity             = atmosphere.Settings.Intensity;
                constants.FogIntensity          = atmosphere.Settings.FogIntensity;

                var cb = MyCommon.GetObjectCB(sizeof(AtmosphereConstants));

                mapping = MyMapping.MapDiscard(RC.DeviceContext, cb);
                mapping.WriteAndPosition(ref constants);
                mapping.Unmap();

                RC.SetBS(MyRender11.BlendAdditive);
                RC.SetCB(2, cb);
                RC.DeviceContext.PixelShader.SetShaderResource(2, MyAtmosphereRenderer.GetAtmosphereLuts(atmosphereId.Value).TransmittanceLut.ShaderView);
                RC.DeviceContext.PixelShader.Set(m_atmosphere);

                MyScreenPass.DrawFullscreenQuad(new MyViewport(MyEnvironmentProbe.CubeMapResolution, MyEnvironmentProbe.CubeMapResolution));
            }

            RC.DeviceContext.OutputMerger.SetTargets(null as RenderTargetView);
        }
Exemplo n.º 3
0
        internal unsafe static void Render()
        {
            var RC = MyImmediateRC.RC;

            if (Atmospheres.Count == 0 || !Enabled)
            {
                return;
            }

            var sphereMesh = MyMeshes.GetMeshId(X.TEXT("Models/Debug/Sphere.mwm"));
            var buffers    = MyMeshes.GetLodMesh(sphereMesh, 0).Buffers;

            RC.SetVB(0, buffers.VB0.Buffer, buffers.VB0.Stride);
            RC.SetIB(buffers.IB.Buffer, buffers.IB.Format);

            RC.SetVS(m_proxyVs);
            RC.SetIL(m_proxyIL);


            RC.SetRS(null);
            RC.SetBS(MyRender11.BlendAtmosphere);

            var cb = MyCommon.GetObjectCB(sizeof(AtmosphereConstants));

            RC.SetCB(1, cb);
            RC.DeviceContext.PixelShader.SetSampler(MyCommon.SHADOW_SAMPLER_SLOT, MyRender11.m_shadowmapSamplerState);

            // depth,
            RC.BindGBufferForRead(0, MyGBuffer.Main);

            RC.BindDepthRT(MyGBuffer.Main.Get(MyGbufferSlot.DepthStencil), DepthStencilAccess.ReadOnly, MyGBuffer.Main.Get(MyGbufferSlot.LBuffer));


            var indicesNum = MyMeshes.GetLodMesh(sphereMesh, 0).Info.IndicesNum;

            // sort by distance
            int i = Atmospheres.Count;

            foreach (var atmosphere in Atmospheres.OrderByDescending(x => (x.Value.WorldMatrix.Translation - MyEnvironment.CameraPosition).LengthSquared()))
            {
                var worldMatrix = atmosphere.Value.WorldMatrix;
                worldMatrix.Translation -= MyEnvironment.CameraPosition;

                var worldViewProj = ((Matrix)worldMatrix) * MyEnvironment.ViewProjectionAt0;

                double distance       = worldMatrix.Translation.Length();
                double atmosphereTop  = atmosphere.Value.AtmosphereRadius * atmosphere.Value.Settings.AtmosphereTopModifier * atmosphere.Value.PlanetScaleFactor * atmosphere.Value.Settings.RayleighTransitionModifier;
                float  rayleighHeight = atmosphere.Value.Settings.RayleighHeight;
                float  t = 0.0f;
                if (distance > atmosphereTop)
                {
                    if (distance > atmosphereTop * 2.0f)
                    {
                        t = 1.0f;
                    }
                    else
                    {
                        t = (float)((distance - atmosphereTop) / atmosphereTop);
                    }
                }
                rayleighHeight = MathHelper.Lerp(atmosphere.Value.Settings.RayleighHeight, atmosphere.Value.Settings.RayleighHeightSpace, t);

                AtmosphereConstants constants = new AtmosphereConstants();
                constants.WorldViewProj    = Matrix.Transpose(worldViewProj);
                constants.PlanetCentre     = (Vector3)worldMatrix.Translation;
                constants.AtmosphereRadius = atmosphere.Value.AtmosphereRadius * atmosphere.Value.Settings.AtmosphereTopModifier;
                // Raise the ground a bit for better sunsets
                constants.GroundRadius           = atmosphere.Value.PlanetRadius * 1.01f * atmosphere.Value.Settings.SeaLevelModifier;
                constants.BetaRayleighScattering = atmosphere.Value.BetaRayleighScattering / atmosphere.Value.Settings.RayleighScattering;
                constants.BetaMieScattering      = atmosphere.Value.BetaMieScattering / atmosphere.Value.Settings.MieColorScattering;
                constants.HeightScaleRayleighMie = atmosphere.Value.HeightScaleRayleighMie * new Vector2(rayleighHeight, atmosphere.Value.Settings.MieHeight);
                constants.MieG = atmosphere.Value.Settings.MieG;
                constants.PlanetScaleFactor     = atmosphere.Value.PlanetScaleFactor;
                constants.AtmosphereScaleFactor = atmosphere.Value.AtmosphereScaleFactor;
                constants.Intensity             = atmosphere.Value.Settings.Intensity;
                constants.FogIntensity          = atmosphere.Value.Settings.FogIntensity;

                var mapping = MyMapping.MapDiscard(cb);
                mapping.WriteAndPosition(ref constants);
                mapping.Unmap();

                var luts = AtmosphereLUT[atmosphere.Key];
                RC.DeviceContext.PixelShader.SetShaderResources(5, luts.TransmittanceLut.ShaderView);

                bool inside = worldMatrix.Translation.Length() < atmosphere.Value.AtmosphereRadius * atmosphere.Value.PlanetScaleFactor;

                if (inside)
                {
                    RC.SetRS(MyRender11.m_invTriRasterizerState);

                    if (MyRender11.MultisamplingEnabled)
                    {
                        RC.SetDS(MyDepthStencilState.TestEdgeStencil, 0);
                    }
                    else
                    {
                        RC.SetDS(MyDepthStencilState.IgnoreDepthStencil);
                    }
                }
                else
                {
                    RC.SetRS(null);

                    if (MyRender11.MultisamplingEnabled)
                    {
                        RC.SetDS(MyDepthStencilState.TestDepthAndEdgeStencil, 0);
                    }
                    else
                    {
                        RC.SetDS(MyDepthStencilState.DepthTest);
                    }
                }

                RC.SetPS(m_ps);

                RC.DeviceContext.DrawIndexed(indicesNum, 0, 0);

                if (MyRender11.MultisamplingEnabled)
                {
                    if (inside)
                    {
                        RC.SetDS(MyDepthStencilState.TestEdgeStencil, 0x80);
                    }
                    else
                    {
                        RC.SetDS(MyDepthStencilState.TestDepthAndEdgeStencil, 0x80);
                    }

                    RC.SetPS(m_psPerSample);
                    RC.DeviceContext.DrawIndexed(indicesNum, 0, 0);
                }

                i--;
            }

            RC.DeviceContext.PixelShader.SetShaderResources(5, null, null);
            RC.SetRS(null);
            RC.SetDS(null);
        }
Exemplo n.º 4
0
        internal unsafe static void Render()
        {
            var RC = MyImmediateRC.RC;

            if (Atmospheres.Count == 0)
            {
                return;
            }

            var sphereMesh = MyMeshes.GetMeshId(X.TEXT("Models/Debug/Sphere.mwm"));
            var buffers    = MyMeshes.GetLodMesh(sphereMesh, 0).Buffers;

            RC.SetVB(0, buffers.VB0.Buffer, buffers.VB0.Stride);
            RC.SetIB(buffers.IB.Buffer, buffers.IB.Format);

            RC.SetVS(m_proxyVs);
            RC.SetIL(m_proxyIL);


            RC.SetRS(null);
            RC.SetBS(MyRender11.BlendAdditive);

            var cb = MyCommon.GetObjectCB(sizeof(AtmosphereConstants));

            RC.SetCB(1, cb);
            RC.Context.PixelShader.SetSampler(MyCommon.SHADOW_SAMPLER_SLOT, MyRender11.m_shadowmapSamplerState);

            // depth,
            RC.BindGBufferForRead(0, MyGBuffer.Main);

            RC.BindDepthRT(MyGBuffer.Main.Get(MyGbufferSlot.DepthStencil), DepthStencilAccess.ReadOnly, MyGBuffer.Main.Get(MyGbufferSlot.LBuffer));


            var indicesNum = MyMeshes.GetLodMesh(sphereMesh, 0).Info.IndicesNum;

            // sort by distance
            int i = Atmospheres.Count;

            foreach (var atmosphere in Atmospheres.OrderByDescending(x => (x.Value.WorldMatrix.Translation - MyEnvironment.CameraPosition).LengthSquared()))
            {
                var worldMatrix = atmosphere.Value.WorldMatrix;
                worldMatrix.Translation -= MyEnvironment.CameraPosition;

                var worldViewProj = ((Matrix)worldMatrix) * MyEnvironment.ViewProjectionAt0;

                AtmosphereConstants constants = new AtmosphereConstants();
                constants.WorldViewProj          = Matrix.Transpose(worldViewProj);
                constants.PlanetCentre           = (Vector3)worldMatrix.Translation;
                constants.AtmosphereRadius       = atmosphere.Value.AtmosphereRadius;
                constants.GroundRadius           = atmosphere.Value.PlanetRadius;
                constants.BetaRayleighScattering = atmosphere.Value.BetaRayleighScattering;
                constants.BetaMieScattering      = atmosphere.Value.BetaMieScattering;
                constants.HeightScaleRayleighMie = atmosphere.Value.HeightScaleRayleighMie;
                constants.RadiusLimit            = constants.AtmosphereRadius + 1;

                var mapping = MyMapping.MapDiscard(cb);
                mapping.stream.Write(constants);
                mapping.Unmap();

                var luts = AtmosphereLUT[atmosphere.Key];
                RC.Context.PixelShader.SetShaderResources(5, luts.TransmittanceLut.ShaderView, luts.InscatterLut.ShaderView);

                bool inside = worldMatrix.Translation.Length() < constants.AtmosphereRadius;

                if (inside)
                {
                    RC.SetRS(MyRender11.m_invTriRasterizerState);

                    if (MyRender11.MultisamplingEnabled)
                    {
                        RC.SetDS(MyDepthStencilState.TestAAEdge, 0);
                    }
                    else
                    {
                        RC.SetDS(MyDepthStencilState.IgnoreDepthStencil);
                    }
                }
                else
                {
                    RC.SetRS(null);

                    if (MyRender11.MultisamplingEnabled)
                    {
                        RC.SetDS(MyDepthStencilState.TestDepthAndAAEdge, 0);
                    }
                    else
                    {
                        RC.SetDS(MyDepthStencilState.DepthTest);
                    }
                }

                if (i == 1)
                {
                    RC.SetBS(MyRender11.BlendOutscatter);

                    RC.SetPS(m_psT);
                    RC.Context.DrawIndexed(indicesNum, 0, 0);

                    RC.SetBS(MyRender11.BlendAdditive);
                }
                RC.SetPS(m_ps);

                RC.Context.DrawIndexed(indicesNum, 0, 0);

                if (MyRender11.MultisamplingEnabled)
                {
                    if (inside)
                    {
                        RC.SetDS(MyDepthStencilState.TestAAEdge, 0x80);
                    }
                    else
                    {
                        RC.SetDS(MyDepthStencilState.TestDepthAndAAEdge, 0x80);
                    }

                    if (i == 1)
                    {
                        RC.SetBS(MyRender11.BlendOutscatter);

                        RC.SetPS(m_psTPerSample);
                        RC.Context.DrawIndexed(indicesNum, 0, 0);

                        RC.SetBS(MyRender11.BlendAdditive);
                    }
                    RC.SetPS(m_psPerSample);
                    RC.Context.DrawIndexed(indicesNum, 0, 0);
                }

                i--;
            }

            RC.Context.PixelShader.SetShaderResources(5, null, null);
            RC.SetRS(null);
            RC.SetDS(null);
        }
Exemplo n.º 5
0
        internal unsafe static void Render()
        {
            var RC = MyImmediateRC.RC;

            if(Atmospheres.Count == 0 || !Enabled) {
                return;
            }

            var sphereMesh = MyMeshes.GetMeshId(X.TEXT_("Models/Debug/Sphere.mwm"), 1.0f);
            var buffers = MyMeshes.GetLodMesh(sphereMesh, 0).Buffers;
            RC.SetVertexBuffer(0, buffers.VB0.Buffer, buffers.VB0.Stride);
            RC.SetIndexBuffer(buffers.IB.Buffer, buffers.IB.Format);

            RC.VertexShader.Set(m_proxyVs);
            RC.SetInputLayout(m_proxyIL);
            

            RC.SetRasterizerState(null);
            RC.SetBlendState(MyBlendStateManager.BlendAtmosphere);

            RC.AllShaderStages.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);

            var cb = MyCommon.GetObjectCB(sizeof(AtmosphereConstants));
            RC.AllShaderStages.SetConstantBuffer(1, cb);
            RC.PixelShader.SetSampler(MyCommon.SHADOW_SAMPLER_SLOT, MySamplerStateManager.Shadowmap);

            // depth, 
            RC.PixelShader.SetSrvs(0, MyGBuffer.Main);

            RC.SetRtv(MyGBuffer.Main.DepthStencil, MyDepthStencilAccess.ReadOnly, MyGBuffer.Main.LBuffer);


            var indicesNum = MyMeshes.GetLodMesh(sphereMesh, 0).Info.IndicesNum;

            // sort by distance
            int i = Atmospheres.Count;

            foreach (var atmosphere in Atmospheres.OrderByDescending(x => (x.Value.WorldMatrix.Translation - MyRender11.Environment.Matrices.CameraPosition).LengthSquared()))
            {
                var worldMatrix = atmosphere.Value.WorldMatrix;
                worldMatrix.Translation -= MyRender11.Environment.Matrices.CameraPosition;
                
                var worldViewProj = ((Matrix) worldMatrix) * MyRender11.Environment.Matrices.ViewProjectionAt0;

                double distance = worldMatrix.Translation.Length();
                double atmosphereTop = atmosphere.Value.AtmosphereRadius * atmosphere.Value.Settings.AtmosphereTopModifier * atmosphere.Value.PlanetScaleFactor * atmosphere.Value.Settings.RayleighTransitionModifier;
                float rayleighHeight = atmosphere.Value.Settings.RayleighHeight;
                float t = 0.0f;
                if (distance > atmosphereTop)
                {
                    if (distance > atmosphereTop * 2.0f)
                    {
                        t = 1.0f;
                    }
                    else
                    {
                        t = (float)((distance - atmosphereTop) / atmosphereTop);
                    }
                }
                rayleighHeight = MathHelper.Lerp(atmosphere.Value.Settings.RayleighHeight, atmosphere.Value.Settings.RayleighHeightSpace, t);

                AtmosphereConstants constants = new AtmosphereConstants();
                constants.WorldViewProj = Matrix.Transpose(worldViewProj);
                constants.PlanetCentre = (Vector3)worldMatrix.Translation;
                constants.AtmosphereRadius = atmosphere.Value.AtmosphereRadius * atmosphere.Value.Settings.AtmosphereTopModifier;
                // Raise the ground a bit for better sunsets
                constants.GroundRadius = atmosphere.Value.PlanetRadius * 1.01f * atmosphere.Value.Settings.SeaLevelModifier;
                constants.BetaRayleighScattering = atmosphere.Value.BetaRayleighScattering / atmosphere.Value.Settings.RayleighScattering;
                constants.BetaMieScattering = atmosphere.Value.BetaMieScattering / atmosphere.Value.Settings.MieColorScattering;
                constants.HeightScaleRayleighMie = atmosphere.Value.HeightScaleRayleighMie * new Vector2(rayleighHeight, atmosphere.Value.Settings.MieHeight);
                constants.MieG = atmosphere.Value.Settings.MieG;
                constants.PlanetScaleFactor = atmosphere.Value.PlanetScaleFactor;
                constants.AtmosphereScaleFactor = atmosphere.Value.AtmosphereScaleFactor;
                constants.Intensity = atmosphere.Value.Settings.Intensity;
                constants.FogIntensity = atmosphere.Value.Settings.FogIntensity;

                var mapping = MyMapping.MapDiscard(cb);
                mapping.WriteAndPosition(ref constants);
                mapping.Unmap();

                var luts = AtmosphereLUT[atmosphere.Key];
                RC.PixelShader.SetSrvs(5, luts.TransmittanceLut);

                bool inside = worldMatrix.Translation.Length() < atmosphere.Value.AtmosphereRadius * atmosphere.Value.PlanetScaleFactor;

                if (inside)
                {
                    RC.SetRasterizerState(MyRasterizerStateManager.InvTriRasterizerState);

                    if (MyRender11.MultisamplingEnabled)
                    {
                        RC.SetDepthStencilState(MyDepthStencilStateManager.TestEdgeStencil, 0);
                    }
                    else
                    {
                        RC.SetDepthStencilState(MyDepthStencilStateManager.IgnoreDepthStencil);
                    }                    
                }
                else
                {
                    RC.SetRasterizerState(null);

                    if (MyRender11.MultisamplingEnabled)
                    {
                        RC.SetDepthStencilState(MyDepthStencilStateManager.TestDepthAndEdgeStencil, 0);
                    }
                    else
                    {
                        RC.SetDepthStencilState(MyDepthStencilStateManager.DepthTestReadOnly);
                    }
                }

                RC.PixelShader.Set(m_ps);

                RC.DrawIndexed(indicesNum, 0, 0);

                if (MyRender11.MultisamplingEnabled)
                {
                    if (inside)
                    {
                        RC.SetDepthStencilState(MyDepthStencilStateManager.TestEdgeStencil, 0x80);
                    }
                    else
                    {
                        RC.SetDepthStencilState(MyDepthStencilStateManager.TestDepthAndEdgeStencil, 0x80);
                    }

                    RC.PixelShader.Set(m_psPerSample);
                    RC.DrawIndexed(indicesNum, 0, 0);
                    
                }

                i--;
            }

            RC.PixelShader.SetSrvs(5, null, null);
            RC.SetRasterizerState(null);
            RC.SetDepthStencilState(null);
            RC.SetRtv(null);
        }
        internal unsafe static void RunForwardPostprocess(RenderTargetView rt, ShaderResourceView depth, ref Matrix viewMatrix, uint? atmosphereId)
        {
            var transpose = Matrix.Transpose(viewMatrix);
            var mapping = MyMapping.MapDiscard(RC.DeviceContext, MyCommon.ProjectionConstants);
            mapping.WriteAndPosition(ref transpose);
            mapping.Unmap();

            RC.SetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);

            RC.DeviceContext.OutputMerger.SetTargets(rt);
            RC.DeviceContext.PixelShader.SetShaderResource(0, depth);
            RC.DeviceContext.PixelShader.SetShaderResource(MyCommon.SKYBOX_SLOT, MyTextures.GetView(MyTextures.GetTexture(MyEnvironment.DaySkybox, MyTextureEnum.CUBEMAP, true)));
            RC.DeviceContext.PixelShader.SetShaderResource(MyCommon.SKYBOX2_SLOT, MyTextures.GetView(MyTextures.GetTexture(MyEnvironment.NightSkybox, MyTextureEnum.CUBEMAP, true)));
            RC.DeviceContext.PixelShader.SetSamplers(0, MyRender11.StandardSamplers);
            RC.DeviceContext.PixelShader.Set(m_ps);

            MyScreenPass.DrawFullscreenQuad(new MyViewport(256, 256));

            if (atmosphereId != null)
            {
                var atmosphere = MyAtmosphereRenderer.GetAtmosphere(atmosphereId.Value);
                var constants = new AtmosphereConstants();
                //TODO(AF) These values are computed in MyAtmosphere as well. Find a way to remove the duplication
                var worldMatrix = atmosphere.WorldMatrix;
                worldMatrix.Translation -= MyEnvironment.CameraPosition;

                double distance = worldMatrix.Translation.Length();
                double atmosphereTop = atmosphere.AtmosphereRadius * atmosphere.Settings.AtmosphereTopModifier * atmosphere.PlanetScaleFactor * atmosphere.Settings.RayleighTransitionModifier;
                float rayleighHeight = atmosphere.Settings.RayleighHeight;
                float t = 0.0f;
                if (distance > atmosphereTop)
                {
                    if (distance > atmosphereTop * 2.0f)
                    {
                        t = 1.0f;
                    }
                    else
                    {
                        t = (float)((distance - atmosphereTop) / atmosphereTop);
                    }
                }
                rayleighHeight = MathHelper.Lerp(atmosphere.Settings.RayleighHeight, atmosphere.Settings.RayleighHeightSpace, t);


                constants.PlanetCentre = (Vector3)worldMatrix.Translation;
                constants.AtmosphereRadius = atmosphere.AtmosphereRadius * atmosphere.Settings.AtmosphereTopModifier;
                constants.GroundRadius = atmosphere.PlanetRadius * 1.01f * atmosphere.Settings.SeaLevelModifier;
                constants.BetaRayleighScattering = atmosphere.BetaRayleighScattering / atmosphere.Settings.RayleighScattering;
                constants.BetaMieScattering = atmosphere.BetaMieScattering / atmosphere.Settings.MieColorScattering;
                constants.HeightScaleRayleighMie = atmosphere.HeightScaleRayleighMie * new Vector2(rayleighHeight, atmosphere.Settings.MieHeight);
                constants.MieG = atmosphere.Settings.MieG;
                constants.PlanetScaleFactor = atmosphere.PlanetScaleFactor;
                constants.AtmosphereScaleFactor = atmosphere.AtmosphereScaleFactor;
                constants.Intensity = atmosphere.Settings.Intensity;
                constants.FogIntensity = atmosphere.Settings.FogIntensity;
            
                var cb = MyCommon.GetObjectCB(sizeof(AtmosphereConstants));
            
                mapping = MyMapping.MapDiscard(RC.DeviceContext, cb);
                mapping.WriteAndPosition(ref constants);
                mapping.Unmap();
            
                RC.SetBS(MyRender11.BlendAdditive);
                RC.SetCB(2, cb);
                RC.DeviceContext.PixelShader.SetShaderResource(2, MyAtmosphereRenderer.GetAtmosphereLuts(atmosphereId.Value).TransmittanceLut.ShaderView);
                RC.DeviceContext.PixelShader.Set(m_atmosphere);

                MyScreenPass.DrawFullscreenQuad(new MyViewport(MyEnvironmentProbe.CubeMapResolution, MyEnvironmentProbe.CubeMapResolution));
            }

            RC.DeviceContext.OutputMerger.SetTargets(null as RenderTargetView);

        }
        internal unsafe static void RunForwardPostprocess(IRtvBindable rt, ISrvBindable depth, ref Matrix viewMatrix, uint?atmosphereId)
        {
            MyGpuProfiler.IC_BeginBlock("Postprocess");
            var transpose = Matrix.Transpose(viewMatrix);
            var mapping   = MyMapping.MapDiscard(RC, MyCommon.ProjectionConstants);

            mapping.WriteAndPosition(ref transpose);
            mapping.Unmap();

            RC.AllShaderStages.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
            RC.AllShaderStages.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);

            RC.SetDepthStencilState(MyDepthStencilStateManager.IgnoreDepthStencil);
            RC.SetRtv(rt);
            RC.PixelShader.SetSrv(0, depth);
            MyFileTextureManager texManager = MyManagers.FileTextures;

            RC.PixelShader.SetSrv(MyCommon.SKYBOX_SLOT, texManager.GetTexture(MyRender11.Environment.Data.Skybox, MyFileTextureEnum.CUBEMAP, true));
            RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers);
            RC.PixelShader.Set(m_ps);

            MyScreenPass.DrawFullscreenQuad(new MyViewport(m_viewportSize, m_viewportSize));
            MyGpuProfiler.IC_EndBlock();

            MyGpuProfiler.IC_BeginBlock("Atmosphere");
            if (atmosphereId != null)
            {
                var atmosphere = MyAtmosphereRenderer.GetAtmosphere(atmosphereId.Value);
                var constants  = new AtmosphereConstants();
                //TODO(AF) These values are computed in MyAtmosphere as well. Find a way to remove the duplication
                var worldMatrix = atmosphere.WorldMatrix;
                worldMatrix.Translation -= MyRender11.Environment.Matrices.CameraPosition;

                double distance       = worldMatrix.Translation.Length();
                double atmosphereTop  = atmosphere.AtmosphereRadius * atmosphere.Settings.AtmosphereTopModifier * atmosphere.PlanetScaleFactor * atmosphere.Settings.RayleighTransitionModifier;
                float  rayleighHeight = atmosphere.Settings.RayleighHeight;
                float  t = 0.0f;
                if (distance > atmosphereTop)
                {
                    if (distance > atmosphereTop * 2.0f)
                    {
                        t = 1.0f;
                    }
                    else
                    {
                        t = (float)((distance - atmosphereTop) / atmosphereTop);
                    }
                }
                rayleighHeight = MathHelper.Lerp(atmosphere.Settings.RayleighHeight, atmosphere.Settings.RayleighHeightSpace, t);


                constants.PlanetCentre           = (Vector3)worldMatrix.Translation;
                constants.AtmosphereRadius       = atmosphere.AtmosphereRadius * atmosphere.Settings.AtmosphereTopModifier;
                constants.GroundRadius           = atmosphere.PlanetRadius * 1.01f * atmosphere.Settings.SeaLevelModifier;
                constants.BetaRayleighScattering = atmosphere.BetaRayleighScattering / atmosphere.Settings.RayleighScattering;
                constants.BetaMieScattering      = atmosphere.BetaMieScattering / atmosphere.Settings.MieColorScattering;
                constants.HeightScaleRayleighMie = atmosphere.HeightScaleRayleighMie * new Vector2(rayleighHeight, atmosphere.Settings.MieHeight);
                constants.MieG = atmosphere.Settings.MieG;
                constants.PlanetScaleFactor     = atmosphere.PlanetScaleFactor;
                constants.AtmosphereScaleFactor = atmosphere.AtmosphereScaleFactor;
                constants.Intensity             = atmosphere.Settings.Intensity;
                constants.FogIntensity          = atmosphere.Settings.FogIntensity;

                var cb = MyCommon.GetObjectCB(sizeof(AtmosphereConstants));

                mapping = MyMapping.MapDiscard(RC, cb);
                mapping.WriteAndPosition(ref constants);
                mapping.Unmap();

                RC.SetBlendState(MyBlendStateManager.BlendAdditive);
                RC.PixelShader.SetConstantBuffer(2, cb);
                RC.PixelShader.SetSrv(2, MyAtmosphereRenderer.GetAtmosphereLuts(atmosphereId.Value).TransmittanceLut);
                RC.PixelShader.Set(m_atmosphere);

                MyScreenPass.DrawFullscreenQuad(new MyViewport(MyEnvironmentProbe.CubeMapResolution, MyEnvironmentProbe.CubeMapResolution));
            }
            MyGpuProfiler.IC_EndBlock();

            RC.SetRtv(null);
        }
Exemplo n.º 8
0
        internal unsafe static void Render()
        {
            var RC = MyImmediateRC.RC;

            if(Atmospheres.Count == 0) {
                return;
            }

            var sphereMesh = MyMeshes.GetMeshId(X.TEXT("Models/Debug/Sphere.mwm"));
            var buffers = MyMeshes.GetLodMesh(sphereMesh, 0).Buffers;
            RC.SetVB(0, buffers.VB0.Buffer, buffers.VB0.Stride);
            RC.SetIB(buffers.IB.Buffer, buffers.IB.Format);

            RC.SetVS(m_proxyVs);
            RC.SetIL(m_proxyIL);
            

            RC.SetRS(null);
            RC.SetBS(MyRender11.BlendAdditive);

            var cb = MyCommon.GetObjectCB(sizeof(AtmosphereConstants));
            RC.SetCB(1, cb);
            RC.Context.PixelShader.SetSampler(MyCommon.SHADOW_SAMPLER_SLOT, MyRender11.m_shadowmapSamplerState);

            // depth, 
            RC.BindGBufferForRead(0, MyGBuffer.Main);

            RC.BindDepthRT(MyGBuffer.Main.Get(MyGbufferSlot.DepthStencil), DepthStencilAccess.ReadOnly, MyGBuffer.Main.Get(MyGbufferSlot.LBuffer));


            var indicesNum = MyMeshes.GetLodMesh(sphereMesh, 0).Info.IndicesNum;

            // sort by distance
            int i = Atmospheres.Count;

            foreach (var atmosphere in Atmospheres.OrderByDescending(x => (x.Value.WorldMatrix.Translation - MyEnvironment.CameraPosition).LengthSquared()))
            {
                var worldMatrix = atmosphere.Value.WorldMatrix;
                worldMatrix.Translation -= MyEnvironment.CameraPosition;
                
                var worldViewProj = ((Matrix) worldMatrix) * MyEnvironment.ViewProjectionAt0;

                AtmosphereConstants constants = new AtmosphereConstants();
                constants.WorldViewProj = Matrix.Transpose(worldViewProj);
                constants.PlanetCentre = (Vector3)worldMatrix.Translation;
                constants.AtmosphereRadius = atmosphere.Value.AtmosphereRadius;
                constants.GroundRadius = atmosphere.Value.PlanetRadius;
                constants.BetaRayleighScattering = atmosphere.Value.BetaRayleighScattering;
                constants.BetaMieScattering = atmosphere.Value.BetaMieScattering;
                constants.HeightScaleRayleighMie = atmosphere.Value.HeightScaleRayleighMie;
                constants.RadiusLimit = constants.AtmosphereRadius + 1;

                var mapping = MyMapping.MapDiscard(cb);
                mapping.stream.Write(constants);
                mapping.Unmap();

                var luts = AtmosphereLUT[atmosphere.Key];
                RC.Context.PixelShader.SetShaderResources(5, luts.TransmittanceLut.ShaderView, luts.InscatterLut.ShaderView);

                bool inside = worldMatrix.Translation.Length() < constants.AtmosphereRadius;

                if (inside)
                {
                    RC.SetRS(MyRender11.m_invTriRasterizerState);

                    if (MyRender11.MultisamplingEnabled)
                    {
                        RC.SetDS(MyDepthStencilState.TestEdgeStencil, 0);
                    }
                    else
                    {
                        RC.SetDS(MyDepthStencilState.IgnoreDepthStencil);
                    }                    
                }
                else
                {
                    RC.SetRS(null);

                    if (MyRender11.MultisamplingEnabled)
                    {
                        RC.SetDS(MyDepthStencilState.TestDepthAndEdgeStencil, 0);
                    }
                    else
                    {
                        RC.SetDS(MyDepthStencilState.DepthTest);
                    }
                }

                if (i == 1)
                {
                    RC.SetBS(MyRender11.BlendOutscatter);

                    RC.SetPS(m_psT);
                    RC.Context.DrawIndexed(indicesNum, 0, 0);

                    RC.SetBS(MyRender11.BlendAdditive);
                }
                RC.SetPS(m_ps);

                RC.Context.DrawIndexed(indicesNum, 0, 0);

                if (MyRender11.MultisamplingEnabled)
                {
                    if (inside)
                    {
                        RC.SetDS(MyDepthStencilState.TestEdgeStencil, 0x80);
                    }
                    else
                    {
                        RC.SetDS(MyDepthStencilState.TestDepthAndEdgeStencil, 0x80);
                    }

                    if (i == 1)
                    {
                        RC.SetBS(MyRender11.BlendOutscatter);

                        RC.SetPS(m_psTPerSample);
                        RC.Context.DrawIndexed(indicesNum, 0, 0);

                        RC.SetBS(MyRender11.BlendAdditive);
                    }
                    RC.SetPS(m_psPerSample);
                    RC.Context.DrawIndexed(indicesNum, 0, 0);
                    
                }

                i--;
            }

            RC.Context.PixelShader.SetShaderResources(5, null, null);
            RC.SetRS(null);
            RC.SetDS(null);
        }