예제 #1
0
        private void Bloom(Renderer renderer, Box <Vector2> resolution, GraphicsDevice device, Texture2D lightBuffer)
        {
            var screenResolution  = resolution.Value;
            var halfResolution    = screenResolution / 2;
            var quarterResolution = halfResolution / 2;

            // downsample the light buffer to half resolution, and threshold at the same time
            var thresholded = RenderTargetManager.GetTarget(device, (int)halfResolution.X, (int)halfResolution.Y, SurfaceFormat.Rgba64, name: "bloom thresholded");

            device.SetRenderTarget(thresholded);
            bloom.Parameters["Resolution"].SetValue(halfResolution);
            bloom.Parameters["Threshold"].SetValue(renderer.Data.Get <float>("hdr_bloomthreshold").Value);
            bloom.Parameters["MinExposure"].SetValue(renderer.Data.Get <float>("hdr_minexposure").Value);
            bloom.Parameters["MaxExposure"].SetValue(renderer.Data.Get <float>("hdr_maxexposure").Value);
            bloom.Parameters["Texture"].SetValue(lightBuffer);
            bloom.Parameters["Luminance"].SetValue(adaptedLuminance[current]);
            bloom.CurrentTechnique = bloom.Techniques["ThresholdDownsample2X"];
            quad.Draw(bloom);

            // downsample again to quarter resolution
            var downsample = RenderTargetManager.GetTarget(device, (int)quarterResolution.X, (int)quarterResolution.Y, SurfaceFormat.Rgba64, name: "bloom downsampled");

            device.SetRenderTarget(downsample);
            bloom.Parameters["Resolution"].SetValue(quarterResolution);
            bloom.Parameters["Texture"].SetValue(thresholded);
            bloom.CurrentTechnique = bloom.Techniques["Scale"];
            quad.Draw(bloom);

            // blur the target
            var blurred = RenderTargetManager.GetTarget(device, (int)quarterResolution.X, (int)quarterResolution.Y, SurfaceFormat.Rgba64, name: "bloom blurred");

            gaussian.Blur(downsample, blurred, renderer.Data.Get <float>("hdr_bloomblurammount").Value);

            // upscale back to half resolution
            device.SetRenderTarget(thresholded);
            bloom.Parameters["Resolution"].SetValue(halfResolution);
            bloom.Parameters["Texture"].SetValue(blurred);
            quad.Draw(bloom);

            // output result
            Output("bloom", thresholded);

            // cleanup temp render targets
            RenderTargetManager.RecycleTarget(downsample);
            RenderTargetManager.RecycleTarget(blurred);
        }