void OnPostRender()
        {
            // Unused in scriptable render pipelines
            if (RuntimeUtilities.scriptableRenderPipelineActive)
            {
                return;
            }

            if (m_CurrentContext.IsTemporalAntialiasingActive() && !m_IsRenderingInSceneView)
            {
                m_Camera.ResetProjectionMatrix();
            }
        }
        // Renders everything not opaque-only
        //
        // Current order of operation is as following:
        //     1. Pre-stack
        //     2. Built-in stack
        //     3. Post-stack
        //     4. Built-in final pass
        //
        // Final pass should be skipped when outputting to a HDR display.
        public void Render(PostProcessRenderContext context)
        {
            SetupContext(context);
            TextureLerper.instance.BeginFrame(context);

            // Update & override layer settings first (volume blending) if the opaque only pass
            // hasn't been called this frame.
            UpdateSettingsIfNeeded(context);

            // Do temporal anti-aliasing first
            int lastTarget = -1;

            if (context.IsTemporalAntialiasingActive() && !context.isSceneView)
            {
                temporalAntialiasing.SetProjectionMatrix(context.camera);

                lastTarget = m_TargetPool.Get();
                var finalDestination = context.destination;
                context.command.GetTemporaryRT(lastTarget, context.width, context.height, 24, FilterMode.Bilinear, context.sourceFormat);
                context.destination = lastTarget;
                temporalAntialiasing.Render(context);
                context.source      = lastTarget;
                context.destination = finalDestination;
            }

            // Right before the builtin stack
            lastTarget = RenderInjectionPoint(PostProcessEvent.BeforeStack, context, "BeforeStack", lastTarget);

            // Builtin stack
            lastTarget = RenderBuiltins(context, lastTarget);

            // After the builtin stack but before the final pass (before FXAA & Dithering)
            if (!breakBeforeColorGrading)
            {
                lastTarget = RenderInjectionPoint(PostProcessEvent.AfterStack, context, "AfterStack", lastTarget);
            }

            // And close with the final pass
            RenderFinalPass(context, lastTarget);

            TextureLerper.instance.EndFrame();
            m_SettingsUpdateNeeded = true;
        }
        // In the legacy render loop you have to explicitely set flags on camera to tell that you
        // need depth, depth+normals or motion vectors... This won't have any effect with most
        // scriptable render pipelines.
        void SetLegacyCameraFlags(PostProcessRenderContext context)
        {
            var flags = DepthTextureMode.None;

            foreach (var bundle in m_Bundles)
            {
                if (bundle.Value.settings.IsEnabledAndSupported())
                {
                    flags |= bundle.Value.renderer.GetLegacyCameraFlags();
                }
            }

            // Special case for TAA
            if (context.IsTemporalAntialiasingActive())
            {
                flags |= temporalAntialiasing.GetLegacyCameraFlags();
            }

            context.camera.depthTextureMode = flags;
        }
Exemple #4
0
        public override void Render(PostProcessRenderContext context)
        {
            var colorFormat = RenderTextureFormat.ARGBHalf;
            var cocFormat   = SelectFormat(RenderTextureFormat.R8, RenderTextureFormat.RHalf);

            // Avoid using R8 on OSX with Metal. #896121, https://goo.gl/MgKqu6
            #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
            if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
            {
                cocFormat = SelectFormat(RenderTextureFormat.RHalf, RenderTextureFormat.Default);
            }
            #endif

            // Material setup
            var f      = settings.focalLength.value / 1000f;
            var s1     = Mathf.Max(settings.focusDistance.value, f);
            var aspect = (float)context.width / (float)context.height;
            var coeff  = f * f / (settings.aperture.value * (s1 - f) * k_FilmHeight * 2);
            var maxCoC = CalculateMaxCoCRadius(context.height);

            var sheet = context.propertySheets.Get("Hidden/PostProcessing/DepthOfField");
            sheet.properties.Clear();
            sheet.properties.SetFloat(Uniforms._Distance, s1);
            sheet.properties.SetFloat(Uniforms._LensCoeff, coeff);
            sheet.properties.SetFloat(Uniforms._MaxCoC, maxCoC);
            sheet.properties.SetFloat(Uniforms._RcpMaxCoC, 1f / maxCoC);
            sheet.properties.SetFloat(Uniforms._RcpAspect, 1f / aspect);

            var cmd = context.command;
            cmd.BeginSample("DepthOfField");

            // CoC calculation pass
            cmd.GetTemporaryRT(Uniforms._CoCTex, context.width, context.height, 0, FilterMode.Bilinear, cocFormat);
            cmd.BlitFullscreenTriangle((Texture)null, Uniforms._CoCTex, sheet, (int)Pass.CoCCalculation);

            // CoC temporal filter pass when TAA is enabled
            if (context.IsTemporalAntialiasingActive())
            {
                float motionBlending = context.temporalAntialiasing.motionBlending;
                float blend          = m_ResetHistory ? 0f : motionBlending; // Handles first frame blending
                var   jitter         = context.temporalAntialiasing.jitter;

                sheet.properties.SetVector(Uniforms._TaaParams, new Vector3(jitter.x, jitter.y, blend));

                int pp           = m_HistoryPingPong;
                var historyRead  = CheckHistory(++pp % 2, context.width, context.height, cocFormat);
                var historyWrite = CheckHistory(++pp % 2, context.width, context.height, cocFormat);
                m_HistoryPingPong = ++pp % 2;

                cmd.BlitFullscreenTriangle(historyRead, historyWrite, sheet, (int)Pass.CoCTemporalFilter);
                cmd.ReleaseTemporaryRT(Uniforms._CoCTex);
                cmd.SetGlobalTexture(Uniforms._CoCTex, historyWrite);
            }

            // Downsampling and prefiltering pass
            cmd.GetTemporaryRT(Uniforms._DepthOfFieldTex, context.width / 2, context.height / 2, 0, FilterMode.Bilinear, colorFormat);
            cmd.BlitFullscreenTriangle(context.source, Uniforms._DepthOfFieldTex, sheet, (int)Pass.DownsampleAndPrefilter);

            // Bokeh simulation pass
            cmd.GetTemporaryRT(Uniforms._DepthOfFieldTemp, context.width / 2, context.height / 2, 0, FilterMode.Bilinear, colorFormat);
            cmd.BlitFullscreenTriangle(Uniforms._DepthOfFieldTex, Uniforms._DepthOfFieldTemp, sheet, (int)Pass.BokehSmallKernel + (int)settings.kernelSize.value);

            // Postfilter pass
            cmd.BlitFullscreenTriangle(Uniforms._DepthOfFieldTemp, Uniforms._DepthOfFieldTex, sheet, (int)Pass.PostFilter);
            cmd.ReleaseTemporaryRT(Uniforms._DepthOfFieldTemp);

            var uberSheet = context.uberSheet;
            uberSheet.EnableKeyword("DEPTH_OF_FIELD");
            uberSheet.properties.SetVector(Uniforms._DepthOfFieldParams, new Vector3(s1, coeff, maxCoC));

            cmd.EndSample("DepthOfField");

            m_ResetHistory = false;
        }
Exemple #5
0
        // Renders before-stack, builtin stack and after-stack effects
        // TODO: Refactor this, it's a mess and it's hard to maintain
        public void Render(PostProcessRenderContext context)
        {
            // Update & override layer settings first (volume blending) if the opaque only pass
            // hasn't been called this frame.
            UpdateSettingsIfNeeded(context);

            SetupContext(context);
            var finalDestination = context.destination;
            var cmd = context.command;

            // Do temporal anti-aliasing first
            if (context.IsTemporalAntialiasingActive() && !m_IsRenderingInSceneView)
            {
                temporalAntialiasing.SetProjectionMatrix(context.camera);

                cmd.GetTemporaryRT(Uniforms._AATemp, context.width, context.height, 24, FilterMode.Bilinear, context.sourceFormat);
                context.destination = Uniforms._AATemp;
                temporalAntialiasing.Render(context);
                context.source      = Uniforms._AATemp;
                context.destination = Uniforms._TempTargetPool[4];
            }

            bool hasBeforeStack = HasActiveEffects(m_SortedBundles[PostProcessEvent.BeforeStack]);
            bool hasAfterStack  = HasActiveEffects(m_SortedBundles[PostProcessEvent.AfterStack]);

            // Render builtin stack and user effects
            if (hasBeforeStack)
            {
                cmd.GetTemporaryRT(Uniforms._TempTargetPool[2], context.width, context.height, 24, FilterMode.Bilinear, context.sourceFormat);
                context.destination = Uniforms._TempTargetPool[2];
                RenderList(m_SortedBundles[PostProcessEvent.BeforeStack], context, "BeforeStack");
                context.source      = Uniforms._TempTargetPool[2];
                context.destination = Uniforms._TempTargetPool[4];
            }

            if (hasAfterStack)
            {
                cmd.GetTemporaryRT(Uniforms._TempTargetPool[3], context.width, context.height, 24, FilterMode.Bilinear, context.sourceFormat);
                context.destination = Uniforms._TempTargetPool[3];
                RenderBuiltins(context);

                if (hasBeforeStack)
                {
                    cmd.ReleaseTemporaryRT(Uniforms._TempTargetPool[2]);
                }

                context.source      = Uniforms._TempTargetPool[3];
                context.destination = Uniforms._TempTargetPool[4];
                cmd.GetTemporaryRT(Uniforms._TempTargetPool[4], context.width, context.height, 24, FilterMode.Bilinear, context.sourceFormat);
                RenderList(m_SortedBundles[PostProcessEvent.AfterStack], context, "AfterStack");
                cmd.ReleaseTemporaryRT(Uniforms._TempTargetPool[3]);
            }
            else // Should be skippable if nothing's enabled in builtins
            {
                context.destination = Uniforms._TempTargetPool[4];
                cmd.GetTemporaryRT(Uniforms._TempTargetPool[4], context.width, context.height, 24, FilterMode.Bilinear, context.sourceFormat);
                RenderBuiltins(context);

                if (hasBeforeStack)
                {
                    cmd.ReleaseTemporaryRT(Uniforms._TempTargetPool[2]);
                }
            }

            context.source      = Uniforms._TempTargetPool[4];
            context.destination = finalDestination;
            RenderFinalPass(context);
            cmd.ReleaseTemporaryRT(Uniforms._TempTargetPool[4]);

            m_SettingsUpdateNeeded = true;
        }