Пример #1
0
        protected override void DrawCore()
        {
            var input  = GetSafeInput(0);
            var output = GetSafeOutput(0);

            var blurTextureSize   = output.Size.Down2(UpscaleCount);
            var outputTextureDown = NewScopedRenderTarget2D(blurTextureSize.Width, blurTextureSize.Height, luminanceFormat, 1);

            var luminanceMap = NewScopedRenderTarget2D(input.ViewWidth, input.ViewHeight, luminanceFormat, 1);

            // Calculate the first luminance map
            luminanceLogEffect.SetInput(input);
            luminanceLogEffect.SetOutput(luminanceMap);
            luminanceLogEffect.Draw();

            // Downscales luminance up to BlurTexture (optional) and 1x1
            multiScaler.SetInput(luminanceMap);
            multiScaler.SetOutput(outputTextureDown, luminance1x1);
            multiScaler.Draw();

            // If we have an output texture
            if (outputTextureDown != null)
            {
                // Blur x2 the intermediate output texture
                blur.SetInput(outputTextureDown);
                blur.SetOutput(outputTextureDown);
                blur.Draw();
                blur.Draw();

                // Upscale from intermediate to output
                multiScaler.SetInput(outputTextureDown);
                multiScaler.SetOutput(output);
                multiScaler.Draw();
            }

            // Calculate average luminance only if needed
            if (EnableAverageLuminanceReadback)
            {
                readback.Draw();
                var rawLogValue = readback.Result[0];
                AverageLuminance = (float)Math.Pow(2.0, rawLogValue);
            }
        }
Пример #2
0
        protected override void DrawCore()
        {
            var input  = GetInput(0);
            var output = GetOutput(0) ?? input;

            if (input == null)
            {
                return;
            }

            // ----------------------------------------
            // Downscale / 2
            // ----------------------------------------
            var nextSize          = input.Size.Down2();
            var startRenderTarget = NewScopedRenderTarget2D(nextSize.Width, nextSize.Height, input.Format);

            Scaler.SetInput(input);
            Scaler.SetOutput(startRenderTarget);
            Scaler.Draw("Down/2");

            // ----------------------------------------
            // Downscale / 4 up to Downscale / xx
            // ----------------------------------------
            var previousRenderTarget = startRenderTarget;

            // Create other rendertargets upto lastMinSize max
            resultList.Clear();
            var upscaleSize = nextSize; //nextSize.Down2();

            //var radius = (float)power;
            var maxInputSize = Math.Max(input.Size.Width, input.Size.Height);
            var maxLevel     = (int)(Math.Max(1, Math.Floor(Math.Log(maxInputSize, 2.0))) - 2);

            for (int mip = 0; mip < DownScale; mip++)
            {
                nextSize = nextSize.Down2();
                var nextRenderTarget = NewScopedRenderTarget2D(nextSize.Width, nextSize.Height, input.Format);

                // Downscale
                Scaler.SetInput(previousRenderTarget);
                Scaler.SetOutput(nextRenderTarget);
                Scaler.Draw("Down/2");

                // Blur it
                blur.Radius = Radius;
                blur.SetInput(nextRenderTarget);
                blur.SetOutput(nextRenderTarget);
                blur.Draw();

                // TODO: Use the MultiScaler for this part instead of recoding it here
                // Only blur after 2nd downscale
                var renderTargetToCombine = nextRenderTarget;
                if (mip > 0)
                {
                    renderTargetToCombine = NewScopedRenderTarget2D(upscaleSize.Width, upscaleSize.Height, input.Format);
                    multiScaler.SetInput(nextRenderTarget);
                    multiScaler.SetOutput(renderTargetToCombine);
                    multiScaler.Draw();
                }
                resultList.Add(renderTargetToCombine);
                previousRenderTarget = nextRenderTarget;
            }

            MaxMip = DownScale - 1;

            // Copy the input texture to the output
            if (ShowOnlyMip || ShowOnlyBloom)
            {
                GraphicsDevice.Clear(output, Color.Black);
            }

            // Switch to additive
            GraphicsDevice.SetBlendState(GraphicsDevice.BlendStates.Additive);
            if (resultList.Count == 1)
            {
                Scaler.SetInput(resultList[0]);
                Scaler.SetOutput(output);
                Scaler.Draw();
            }
            else if (resultList.Count > 1)
            {
                // Combine the blurred mips
                blurCombine.Reset();
                for (int i = 0; i < resultList.Count; i++)
                {
                    var result = resultList[i];
                    blurCombine.SetInput(i, result);
                    var exponent = (float)Math.Max(0, i) - 4.0f;
                    var level    = !ShowOnlyMip || i == MipIndex ? (float)Math.Pow(2.0f, exponent) : 0.0f;
                    level *= Amount;
                    blurCombine.Factors[i] = level;
                }

                blurCombine.SetOutput(output);
                blurCombine.Draw();
            }
            GraphicsDevice.SetBlendState(GraphicsDevice.BlendStates.Default);
        }