Beispiel #1
0
        private void MensureLuminance(GraphicDevice device, IShaderStage stage)
        {
            effect.Technique = HdrEffect.SampleAvgLum;

            RenderTexture2D dest   = _rtToneMap[Tonemaptextures - 1];
            RenderTexture2D source = _rtHdrScene;

            dest.SetTarget(device);
            source.SetTexture(0, device);

            stage.SetSampler(0, _linearSampler);

            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
            _quadRender.DrawQuad(effect);

            effect.Technique = HdrEffect.ResampleAvgLum;
            stage.SetSampler(0, _pointSampler);

            for (int i = Tonemaptextures - 2; i > 0; i--)
            {
                dest   = _rtToneMap[i];
                source = _rtToneMap[i + 1];

                dest.SetTarget(device);
                source.SetTexture(0, device);
                device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
                _quadRender.DrawQuad(effect);
            }

            effect.Technique = HdrEffect.ResampleAvgLumExp;
            source           = _rtToneMap[1];
            dest             = _rtToneMap[0];

            dest.SetTarget(device);
            source.SetTexture(0, device);

            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
            _quadRender.DrawQuad(effect);
        }
Beispiel #2
0
 private void DownSample(GraphicDevice graphic, RenderTexture2D src, RenderTexture2D dest)
 {
     dest.SetTarget(graphic);
     src.SetTexture(0, graphic);
     _sprite.DrawQuad(_effect);
 }
Beispiel #3
0
        public override void Apply()
        {
            if (_gaussianMean != _state.GaussainMean ||
                _gaussianDev != _state.GaussianDeviation ||
                _gaussainMultiplier != _state.GaussianMultiplier)
            {
                ComputeSamples();
            }

            var device = GraphicDeviceFactory.Device;

            device.SaveRenderTarget();

            RenderTexture2D dest;
            RenderTexture2D source;
            RenderTexture2D lumen;
            var             stage = device.PS;
            var             scene = Engine.Scene;

            #region Render To HDRTexture

            _rtHdrScene.SetTarget(device);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Engine.BackColor, 1, 0);

            RenderManager.PopTechnique();
            RenderManager.ApplyTechnique();
            RenderManager.PushTechnique(this);

            #endregion

            _quadRender.Begin();
            _quadRender.SetFullScreenTransform(_input);

            stage.SetSampler(0, _pointSampler);
            stage.SetSampler(1, _pointSampler);

            MensureLuminance(device, stage);

            #region CalculateAdaptation

            if (_state.CalculateEyeAdaptation)
            {
                effect.Technique = HdrEffect.CalculateAdaptedLum;

                //swap last width current
                var rtTemp = _rtLastAdaptedLuminance;
                _rtLastAdaptedLuminance    = _rtCurrentAdaptedLuminance;
                _rtCurrentAdaptedLuminance = rtTemp;

                _rtCurrentAdaptedLuminance.SetTarget(device);

                _rtLastAdaptedLuminance.SetTexture(0, device);
                _rtToneMap[0].SetTexture(1, device);

                _map.ElapsedTime = Engine.ElapsedTime;
                _quadRender.DrawQuad(effect);

                lumen = _rtCurrentAdaptedLuminance;
            }
            else
            {
                lumen = _rtToneMap[0];
            }
            #endregion

            #region BrightPassFilter

            effect.Technique = HdrEffect.BrightPassFilter;
            _rtBrightPassFilter.SetTarget(device);

            _rtHdrScene.SetTexture(0, device);
            lumen.SetTexture(1, device);

            _map.MiddleGray          = _state.MiddleGray;
            _map.BrightPassThreshold = _state.BrightThreshold;

            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
            _quadRender.DrawQuad(effect);

            #endregion

            stage.SetSampler(0, _linearSampler);

            #region BloomFilter

            #region DownSample 4x4

            effect.Technique = HdrEffect.DownSampler4X4;
            dest             = _rtBloom[2];
            source           = _rtBrightPassFilter;

            dest.SetTarget(device);
            source.SetTexture(0, device);
            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);

            _quadRender.DrawQuad(effect);

            #endregion

            #region Horizontal Blur

            effect.Technique = HdrEffect.Bloom;
            //Horizontal Blur
            source = _rtBloom[2];
            dest   = _rtBloom[1];

            dest.SetTarget(device);
            source.SetTexture(0, device);
            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);

            _map.SampleOffsets = _sampleOffsetsH;
            _map.SampleWeights = _sampleWeights;

            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
            _quadRender.DrawQuad(effect);

            #endregion

            #region Vertical Blur
            source = _rtBloom[1];
            dest   = _rtBloom[0];

            dest.SetTarget(device);
            source.SetTexture(0, device);

            _map.SampleOffsets = _sampleOffsetsV;
            _map.SampleWeights = _sampleWeights;

            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
            _quadRender.DrawQuad(effect);

            #endregion

            #endregion

            #region StarFilter

            RenderStar(device);

            #endregion

            #region FinalPass

            effect.Technique = HdrEffect.FinalScenePass;

            device.RestoreRenderTarget();

            stage.SetSampler(0, _pointSampler);
            stage.SetSampler(1, _pointSampler);
            stage.SetSampler(2, _linearSampler);
            stage.SetSampler(3, _linearSampler);

            _rtHdrScene.SetTexture(0, device);
            lumen.SetTexture(1, device);
            _rtBloom[0].SetTexture(2, device);
            _rtStarFinal.SetTexture(3, device);

            _map.MiddleGray = _state.MiddleGray;

            var glare = _state.Glare;
            if (glare == null || glare.glareLuminance <= 0.0f || glare.starLuminance <= 0.0f)
            {
                _map.GaussianScalar = _state.GaussianMultiplier;
            }
            else
            {
                _map.GaussianScalar = 2 * _state.GaussianMultiplier;
            }

            _map.EnableBlueShift = _state.EnableBlueShift;
            _map.StarBlend       = _state.StarBlendFactor;
            _map.BloomBlend      = _state.BloomBlendFactor;

            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
            _quadRender.DrawQuad(effect);

            stage.SetResource(0, null);
            stage.SetResource(1, null);
            stage.SetResource(2, null);
            stage.SetResource(3, null);

            #endregion

            _quadRender.End();
            //device.PopGraphicState<BlendState>();
        }