Пример #1
0
        void CheckInternalLogLut()
        {
            // Check internal lut state, (re)create it if needed
            if (m_InternalLogLut == null || !m_InternalLogLut.IsCreated())
            {
                RuntimeUtilities.Destroy(m_InternalLogLut);

                var format = GetLutFormat();
                m_InternalLogLut = new RenderTexture(k_Lut3DSize, k_Lut3DSize, 0, format, RenderTextureReadWrite.Linear)
                {
                    name              = "Color Grading Log Lut",
                    hideFlags         = HideFlags.DontSave,
                    filterMode        = FilterMode.Bilinear,
                    wrapMode          = TextureWrapMode.Clamp,
                    anisoLevel        = 0,
                    enableRandomWrite = true,
                    volumeDepth       = k_Lut3DSize,
                    dimension         = TextureDimension.Tex3D,
                    autoGenerateMips  = false,
                    useMipMap         = false
                };
                m_InternalLogLut.Create();
            }
        }
Пример #2
0
        public override void Render(PostProcessRenderContext context)
        {
            var cmd = context.command;

            cmd.BeginSample("AutoExposureLookup");

            var sheet = context.propertySheets.Get(context.resources.shaders.autoExposure);

            sheet.ClearKeywords();

            // Prepare autoExpo texture pool
            CheckTexture(0);
            CheckTexture(1);

            // Make sure filtering values are correct to avoid apocalyptic consequences
            float       lowPercent  = settings.filtering.value.x;
            float       highPercent = settings.filtering.value.y;
            const float kMinDelta   = 1e-2f;

            highPercent = Mathf.Clamp(highPercent, 1f + kMinDelta, 99f);
            lowPercent  = Mathf.Clamp(lowPercent, 1f, highPercent - kMinDelta);

            // Compute auto exposure
            sheet.properties.SetBuffer(Uniforms._HistogramBuffer, context.logHistogram.data);
            sheet.properties.SetVector(Uniforms._Params, new Vector4(lowPercent * 0.01f, highPercent * 0.01f, RuntimeUtilities.Exp2(settings.minLuminance.value), RuntimeUtilities.Exp2(settings.maxLuminance.value)));
            sheet.properties.SetVector(Uniforms._Speed, new Vector2(settings.speedDown.value, settings.speedUp.value));
            sheet.properties.SetVector(Uniforms._ScaleOffsetRes, context.logHistogram.GetHistogramScaleOffsetRes(context));
            sheet.properties.SetFloat(Uniforms._ExposureCompensation, settings.keyValue.value);

            if (m_FirstFrame || !Application.isPlaying)
            {
                // We don't want eye adaptation when not in play mode because the GameView isn't
                // animated, thus making it harder to tweak. Just use the final audo exposure value.
                m_CurrentAutoExposure = m_AutoExposurePool[0];
                cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, m_CurrentAutoExposure, sheet, (int)EyeAdaptation.Fixed);

                // Copy current exposure to the other pingpong target to avoid adapting from black
                RuntimeUtilities.CopyTexture(cmd, m_AutoExposurePool[0], m_AutoExposurePool[1]);
            }
            else
            {
                int pp  = m_AutoExposurePingPong;
                var src = m_AutoExposurePool[++pp % 2];
                var dst = m_AutoExposurePool[++pp % 2];
                cmd.BlitFullscreenTriangle(src, dst, sheet, (int)settings.eyeAdaptation.value);
                m_AutoExposurePingPong = ++pp % 2;
                m_CurrentAutoExposure  = dst;
            }

            cmd.EndSample("AutoExposureLookup");

            context.autoExposureTexture = m_CurrentAutoExposure;
            context.autoExposure        = settings;
            m_FirstFrame = false;
        }
Пример #3
0
        public override void Render(PostProcessRenderContext context)
        {
            // Setup compute
            if (m_EyeCompute == null)
            {
                m_EyeCompute = Resources.Load <ComputeShader>("Shaders/Builtins/ExposureHistogram");
            }

            var cmd = context.command;

            cmd.BeginSample("AutoExposureLookup");

            var sheet = context.propertySheets.Get("Hidden/PostProcessing/AutoExposure");

            sheet.ClearKeywords();

            if (m_HistogramBuffer == null)
            {
                m_HistogramBuffer = new ComputeBuffer(k_HistogramBins, sizeof(uint));
            }

            // Downscale the framebuffer, we don't need an absolute precision for auto exposure and it
            // helps making it more stable
            var scaleOffsetRes = GetHistogramScaleOffsetRes(context);

            cmd.GetTemporaryRT(Uniforms._AutoExposureCopyTex,
                               (int)scaleOffsetRes.z,
                               (int)scaleOffsetRes.w,
                               0,
                               FilterMode.Bilinear,
                               context.sourceFormat);
            cmd.BlitFullscreenTriangle(context.source, Uniforms._AutoExposureCopyTex);

            // Prepare autoExpo texture pool
            CheckTexture(0);
            CheckTexture(1);

            // Clear the buffer on every frame as we use it to accumulate luminance values on each frame
            m_HistogramBuffer.SetData(s_EmptyHistogramBuffer);

            // Get a log histogram
            int kernel = m_EyeCompute.FindKernel("KEyeHistogram");

            cmd.SetComputeBufferParam(m_EyeCompute, kernel, "_HistogramBuffer", m_HistogramBuffer);
            cmd.SetComputeTextureParam(m_EyeCompute, kernel, "_Source", Uniforms._AutoExposureCopyTex);
            cmd.SetComputeVectorParam(m_EyeCompute, "_ScaleOffsetRes", scaleOffsetRes);
            cmd.DispatchCompute(m_EyeCompute, kernel,
                                Mathf.CeilToInt(scaleOffsetRes.z / (float)k_HistogramThreadX),
                                Mathf.CeilToInt(scaleOffsetRes.w / (float)k_HistogramThreadY),
                                1);

            // Cleanup
            cmd.ReleaseTemporaryRT(Uniforms._AutoExposureCopyTex);

            // Make sure filtering values are correct to avoid apocalyptic consequences
            float       lowPercent  = settings.filtering.value.x;
            float       highPercent = settings.filtering.value.y;
            const float kMinDelta   = 1e-2f;

            highPercent = Mathf.Clamp(highPercent, 1f + kMinDelta, 99f);
            lowPercent  = Mathf.Clamp(lowPercent, 1f, highPercent - kMinDelta);

            // Compute auto exposure
            sheet.properties.SetBuffer(Uniforms._HistogramBuffer, m_HistogramBuffer);
            sheet.properties.SetVector(Uniforms._Params, new Vector4(lowPercent * 0.01f, highPercent * 0.01f, RuntimeUtilities.Exp2(settings.minLuminance.value), RuntimeUtilities.Exp2(settings.maxLuminance.value)));
            sheet.properties.SetVector(Uniforms._Speed, new Vector2(settings.speedDown.value, settings.speedUp.value));
            sheet.properties.SetVector(Uniforms._ScaleOffsetRes, scaleOffsetRes);
            sheet.properties.SetFloat(Uniforms._ExposureCompensation, settings.keyValue.value);

            if (settings.dynamicKeyValue)
            {
                sheet.EnableKeyword("AUTO_KEY_VALUE");
            }

            if (m_FirstFrame || !Application.isPlaying)
            {
                // We don't want eye adaptation when not in play mode because the GameView isn't
                // animated, thus making it harder to tweak. Just use the final audo exposure value.
                m_CurrentAutoExposure = m_AutoExposurePool[0];
                cmd.BlitFullscreenTriangle((Texture)null, m_CurrentAutoExposure, sheet, (int)EyeAdaptation.Fixed);

                // Copy current exposure to the other pingpong target to avoid adapting from black
                RuntimeUtilities.CopyTexture(cmd, m_AutoExposurePool[0], m_AutoExposurePool[1]);
            }
            else
            {
                int pp  = m_AutoExposurePingPong;
                var src = m_AutoExposurePool[++pp % 2];
                var dst = m_AutoExposurePool[++pp % 2];
                cmd.BlitFullscreenTriangle(src, dst, sheet, (int)settings.eyeAdaptation.value);
                m_AutoExposurePingPong = ++pp % 2;
                m_CurrentAutoExposure  = dst;
            }

            cmd.EndSample("AutoExposureLookup");

            context.autoExposureTexture = m_CurrentAutoExposure;
            m_FirstFrame = false;
        }
Пример #4
0
 public override void Release()
 {
     RuntimeUtilities.Destroy(m_GrainLookupRT);
     m_GrainLookupRT = null;
     m_SampleIndex   = 0;
 }
Пример #5
0
 public override void Release()
 {
     RuntimeUtilities.Destroy(m_InternalSpectralLut);
     m_InternalSpectralLut = null;
 }
Пример #6
0
        // HDR color pipeline is rendered to a 3D lut; it requires Texture3D & compute shaders
        // support - Desktop / Consoles / Some high-end mobiles
        void RenderHDRPipeline(PostProcessRenderContext context)
        {
            // Unfortunately because AnimationCurve doesn't implement GetHashCode and we don't have
            // any reliable way to figure out if a curve data is different from another one we can't
            // skip regenerating the Lut if nothing has changed. So it has to be done on every
            // frame...
            // It's not a very expensive operation anyway (we're talking about filling a 33x33x33
            // Lut on the GPU) but every little thing helps, especially on mobile.
            {
                CheckInternalLogLut();

                // Lut setup
                var compute = context.resources.computeShaders.lut3DBaker;
                int kernel  = 0;

                switch (settings.tonemapper.value)
                {
                case Tonemapper.None: kernel = compute.FindKernel("KGenLut3D_NoTonemap");
                    break;

                case Tonemapper.Neutral: kernel = compute.FindKernel("KGenLut3D_NeutralTonemap");
                    break;

                case Tonemapper.ACES: kernel = compute.FindKernel("KGenLut3D_AcesTonemap");
                    break;

                case Tonemapper.Custom: kernel = compute.FindKernel("KGenLut3D_CustomTonemap");
                    break;
                }

                int groupSize = Mathf.CeilToInt(k_Lut3DSize / 8f);
                var cmd       = context.command;
                cmd.SetComputeTextureParam(compute, kernel, "_Output", m_InternalLogLut);
                cmd.SetComputeVectorParam(compute, "_Size", new Vector4(k_Lut3DSize, 1f / (k_Lut3DSize - 1f), 0f, 0f));

                var colorBalance = ColorUtilities.ComputeColorBalance(settings.temperature.value, settings.tint.value);
                cmd.SetComputeVectorParam(compute, "_ColorBalance", colorBalance);
                cmd.SetComputeVectorParam(compute, "_ColorFilter", settings.colorFilter.value);

                float hue = settings.hueShift.value / 360f;         // Remap to [-0.5;0.5]
                float sat = settings.saturation.value / 100f + 1f;  // Remap to [0;2]
                float con = settings.contrast.value / 100f + 1f;    // Remap to [0;2]
                cmd.SetComputeVectorParam(compute, "_HueSatCon", new Vector4(hue, sat, con, 0f));

                var channelMixerR = new Vector4(settings.mixerRedOutRedIn, settings.mixerRedOutGreenIn, settings.mixerRedOutBlueIn, 0f);
                var channelMixerG = new Vector4(settings.mixerGreenOutRedIn, settings.mixerGreenOutGreenIn, settings.mixerGreenOutBlueIn, 0f);
                var channelMixerB = new Vector4(settings.mixerBlueOutRedIn, settings.mixerBlueOutGreenIn, settings.mixerBlueOutBlueIn, 0f);
                cmd.SetComputeVectorParam(compute, "_ChannelMixerRed", channelMixerR / 100f); // Remap to [-2;2]
                cmd.SetComputeVectorParam(compute, "_ChannelMixerGreen", channelMixerG / 100f);
                cmd.SetComputeVectorParam(compute, "_ChannelMixerBlue", channelMixerB / 100f);

                var lift     = ColorUtilities.ColorToLift(settings.lift.value * 0.2f);
                var gain     = ColorUtilities.ColorToGain(settings.gain.value * 0.8f);
                var invgamma = ColorUtilities.ColorToInverseGamma(settings.gamma.value * 0.8f);
                cmd.SetComputeVectorParam(compute, "_Lift", new Vector4(lift.x, lift.y, lift.z, 0f));
                cmd.SetComputeVectorParam(compute, "_InvGamma", new Vector4(invgamma.x, invgamma.y, invgamma.z, 0f));
                cmd.SetComputeVectorParam(compute, "_Gain", new Vector4(gain.x, gain.y, gain.z, 0f));

                cmd.SetComputeTextureParam(compute, kernel, "_Curves", GetCurveTexture(true));

                if (settings.tonemapper.value == Tonemapper.Custom)
                {
                    m_HableCurve.Init(
                        settings.toneCurveToeStrength.value,
                        settings.toneCurveToeLength.value,
                        settings.toneCurveShoulderStrength.value,
                        settings.toneCurveShoulderLength.value,
                        settings.toneCurveShoulderAngle.value,
                        settings.toneCurveGamma.value
                        );

                    var curve = new Vector4(m_HableCurve.inverseWhitePoint, m_HableCurve.x0, m_HableCurve.x1, 0f);
                    cmd.SetComputeVectorParam(compute, "_CustomToneCurve", curve);

                    var toe         = m_HableCurve.segments[0];
                    var mid         = m_HableCurve.segments[1];
                    var sho         = m_HableCurve.segments[2];
                    var toeSegmentA = new Vector4(toe.offsetX, toe.offsetY, toe.scaleX, toe.scaleY);
                    var toeSegmentB = new Vector4(toe.lnA, toe.B, 0f, 0f);
                    var midSegmentA = new Vector4(mid.offsetX, mid.offsetY, mid.scaleX, mid.scaleY);
                    var midSegmentB = new Vector4(mid.lnA, mid.B, 0f, 0f);
                    var shoSegmentA = new Vector4(sho.offsetX, sho.offsetY, sho.scaleX, sho.scaleY);
                    var shoSegmentB = new Vector4(sho.lnA, sho.B, 0f, 0f);
                    cmd.SetComputeVectorParam(compute, "_ToeSegmentA", toeSegmentA);
                    cmd.SetComputeVectorParam(compute, "_ToeSegmentB", toeSegmentB);
                    cmd.SetComputeVectorParam(compute, "_MidSegmentA", midSegmentA);
                    cmd.SetComputeVectorParam(compute, "_MidSegmentB", midSegmentB);
                    cmd.SetComputeVectorParam(compute, "_ShoSegmentA", shoSegmentA);
                    cmd.SetComputeVectorParam(compute, "_ShoSegmentB", shoSegmentB);
                }

                // Generate the lut
                context.command.BeginSample("HdrColorGradingLut");
                cmd.DispatchCompute(compute, kernel, groupSize, groupSize, groupSize);
                context.command.EndSample("HdrColorGradingLut");
            }

            var lut       = m_InternalLogLut;
            var uberSheet = context.uberSheet;

            uberSheet.EnableKeyword("COLOR_GRADING_HDR");
            uberSheet.properties.SetTexture(Uniforms._Lut3D, lut);
            uberSheet.properties.SetVector(Uniforms._Lut3D_Params, new Vector2(1f / lut.width, lut.width - 1f));
            uberSheet.properties.SetFloat(Uniforms._PostExposure, RuntimeUtilities.Exp2(settings.postExposure.value));

            context.logLut = lut;
        }
Пример #7
0
        public override void Render(PostProcessRenderContext context)
        {
            var cmd = context.command;

            cmd.BeginSample("BloomPyramid");

            var sheet = context.propertySheets.Get(context.resources.shaders.bloom);

            // Apply auto exposure adjustment in the prefiltering pass
            sheet.properties.SetTexture(Uniforms._AutoExposureTex, context.autoExposureTexture);

            // Determine the iteration count
            float logh        = Mathf.Log(context.height, 2f) + settings.diffusion.value - 10f;
            int   logh_i      = Mathf.FloorToInt(logh);
            int   iterations  = Mathf.Clamp(logh_i, 1, k_MaxPyramidSize);
            float sampleScale = 0.5f + logh - logh_i;

            sheet.properties.SetFloat(Uniforms._SampleScale, sampleScale);

            // Do bloom on a half-res buffer, full-res doesn't bring much and kills performances on
            // fillrate limited platforms
            int tw = context.width / 2;
            int th = context.height / 2;

            // Prefiltering parameters
            float lthresh   = Mathf.GammaToLinearSpace(settings.threshold.value);
            float knee      = lthresh * settings.softKnee.value + 1e-5f;
            var   threshold = new Vector4(lthresh, lthresh - knee, knee * 2f, 0.25f / knee);

            sheet.properties.SetVector(Uniforms._Threshold, threshold);

            int qualityOffset = settings.mobileOptimized ? 1 : 0;

            // Downsample
            var last = context.source;

            for (int i = 0; i < iterations; i++)
            {
                int mipDown = m_Pyramid[i].down;
                int mipUp   = m_Pyramid[i].up;
                int pass    = i == 0
                    ? (int)Pass.Prefilter13 + qualityOffset
                    : (int)Pass.Downsample13 + qualityOffset;

                cmd.GetTemporaryRT(mipDown, tw, th, 0, FilterMode.Bilinear, context.sourceFormat);
                cmd.GetTemporaryRT(mipUp, tw, th, 0, FilterMode.Bilinear, context.sourceFormat);
                cmd.BlitFullscreenTriangle(last, mipDown, sheet, pass);

                last = mipDown;
                tw  /= 2; th /= 2;
            }

            // Upsample
            last = m_Pyramid[iterations - 1].down;
            for (int i = iterations - 2; i >= 0; i--)
            {
                int mipDown = m_Pyramid[i].down;
                int mipUp   = m_Pyramid[i].up;
                cmd.SetGlobalTexture(Uniforms._BloomTex, mipDown);
                cmd.BlitFullscreenTriangle(last, mipUp, sheet, (int)Pass.UpsampleTent + qualityOffset);
                last = mipUp;
            }

            var shaderSettings = new Vector4(
                sampleScale,
                RuntimeUtilities.Exp2(settings.intensity.value / 10f) - 1f,
                settings.lensIntensity.value,
                iterations
                );

            var dirtTexture = settings.lensTexture.value == null
                ? RuntimeUtilities.blackTexture
                : settings.lensTexture.value;

            var uberSheet = context.uberSheet;

            uberSheet.EnableKeyword("BLOOM");
            uberSheet.properties.SetVector(Uniforms._Bloom_Settings, shaderSettings);
            uberSheet.properties.SetColor(Uniforms._Bloom_Color, settings.color.value.linear);
            uberSheet.properties.SetTexture(Uniforms._Bloom_DirtTex, dirtTexture);
            cmd.SetGlobalTexture(Uniforms._BloomTex, m_Pyramid[0].up);

            // Cleanup
            for (int i = 0; i < iterations; i++)
            {
                cmd.ReleaseTemporaryRT(m_Pyramid[i].down);
                cmd.ReleaseTemporaryRT(m_Pyramid[i].up);
            }

            cmd.EndSample("BloomPyramid");
        }
Пример #8
0
        void OnLightMeterGUI(PostProcessRenderContext context)
        {
            if (m_TickLabelStyle == null)
            {
                m_TickLabelStyle = new GUIStyle("Label")
                {
                    fontStyle = FontStyle.Bold,
                    fontSize  = 10,
                    alignment = TextAnchor.MiddleCenter
                };
            }

            var histogram = context.logHistogram;
            int kMargin   = 8;
            int x         = kMargin;
            int w         = (int)(context.width * (3 / 5f) - kMargin * 2);
            int h         = context.height / 4;
            int y         = context.height - h - kMargin - 30;
            var rect      = new Rect(x, y, w, h);

            if (Event.current.type == EventType.Repaint)
            {
                CheckTexture(ref m_LightMeterRT, (int)rect.width, (int)rect.height);

                var material = context.propertySheets.Get(context.resources.shaders.lightMeter).material;
                material.shaderKeywords = null;
                material.SetBuffer(Uniforms._HistogramBuffer, histogram.data);

                var scaleOffsetRes = histogram.GetHistogramScaleOffsetRes(context);
                scaleOffsetRes.z = 1f / rect.width;
                scaleOffsetRes.w = 1f / rect.height;

                material.SetVector(Uniforms._ScaleOffsetRes, scaleOffsetRes);

                if (context.logLut != null)
                {
                    material.EnableKeyword("COLOR_GRADING_HDR");
                    material.SetTexture(Uniforms._Lut3D, context.logLut);
                }

                if (context.autoExposure != null)
                {
                    var settings = context.autoExposure;

                    // Make sure filtering values are correct to avoid apocalyptic consequences
                    float       lowPercent  = settings.filtering.value.x;
                    float       highPercent = settings.filtering.value.y;
                    const float kMinDelta   = 1e-2f;
                    highPercent = Mathf.Clamp(highPercent, 1f + kMinDelta, 99f);
                    lowPercent  = Mathf.Clamp(lowPercent, 1f, highPercent - kMinDelta);

                    material.EnableKeyword("AUTO_EXPOSURE");
                    material.SetVector(Uniforms._Params, new Vector4(lowPercent * 0.01f, highPercent * 0.01f, RuntimeUtilities.Exp2(settings.minLuminance.value), RuntimeUtilities.Exp2(settings.maxLuminance.value)));
                }

                RuntimeUtilities.BlitFullscreenTriangle(null, m_LightMeterRT, material, 0);
                GUI.DrawTexture(rect, m_LightMeterRT);
            }

            // Labels
            rect.y     += rect.height;
            rect.height = 30;
            int maxSize = Mathf.FloorToInt(rect.width / (LogHistogram.rangeMax - LogHistogram.rangeMin + 2));

            GUI.DrawTexture(rect, RuntimeUtilities.blackTexture);

            GUILayout.BeginArea(rect);
            {
                GUILayout.BeginHorizontal();
                {
                    GUILayout.Space(4);

                    for (int i = LogHistogram.rangeMin; i < LogHistogram.rangeMax; i++)
                    {
                        GUILayout.Label(i + "\n" + RuntimeUtilities.Exp2(i).ToString("0.###"), m_TickLabelStyle, GUILayout.Width(maxSize));
                        GUILayout.FlexibleSpace();
                    }

                    GUILayout.Label(LogHistogram.rangeMax + "\n" + RuntimeUtilities.Exp2(LogHistogram.rangeMax).ToString("0.###"), m_TickLabelStyle, GUILayout.Width(maxSize));
                    GUILayout.Space(4);
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndArea();
        }
Пример #9
0
 internal void Release()
 {
     RuntimeUtilities.Destroy(m_LightMeterRT);
     m_LightMeterRT = null;
 }