Exemplo n.º 1
0
        void BuildCommandBuffers()
        {
            var context      = m_CurrentContext;
            var sourceFormat = m_Camera.allowHDR ? RuntimeUtilities.defaultHDRRenderTextureFormat : RenderTextureFormat.Default;

            if (!RuntimeUtilities.isFloatingPointFormat(sourceFormat))
            {
                m_NaNKilled = true;
            }

            context.Reset();
            context.camera       = m_Camera;
            context.sourceFormat = sourceFormat;

            // TODO: Investigate retaining command buffers on XR multi-pass right eye
            m_LegacyCmdBufferBeforeReflections.Clear();
            m_LegacyCmdBufferBeforeLighting.Clear();
            m_LegacyCmdBufferOpaque.Clear();
            m_LegacyCmdBuffer.Clear();

            SetupContext(context);

            context.command = m_LegacyCmdBufferOpaque;
            UpdateSettingsIfNeeded(context);

            // Lighting & opaque-only effects
            var aoBundle   = GetBundle <AmbientOcclusion>();
            var aoSettings = aoBundle.CastSettings <AmbientOcclusion>();
            var aoRenderer = aoBundle.CastRenderer <AmbientOcclusionRenderer>();

            bool aoSupported   = aoSettings.IsEnabledAndSupported(context);
            bool aoAmbientOnly = aoRenderer.IsAmbientOnly(context);
            bool isAmbientOcclusionDeferred = aoSupported && aoAmbientOnly;
            bool isAmbientOcclusionOpaque   = aoSupported && !aoAmbientOnly;

            var  ssrBundle   = GetBundle <ScreenSpaceReflections>();
            var  ssrSettings = ssrBundle.settings;
            var  ssrRenderer = ssrBundle.renderer;
            bool isScreenSpaceReflectionsActive = ssrSettings.IsEnabledAndSupported(context);

            // Ambient-only AO is a special case and has to be done in separate command buffers
            if (isAmbientOcclusionDeferred)
            {
                var ao = aoRenderer.Get();

                // Render as soon as possible - should be done async in SRPs when available
                context.command = m_LegacyCmdBufferBeforeReflections;
                ao.RenderAmbientOnly(context);

                // Composite with GBuffer right before the lighting pass
                context.command = m_LegacyCmdBufferBeforeLighting;
                ao.CompositeAmbientOnly(context);
            }
            else if (isAmbientOcclusionOpaque)
            {
                context.command = m_LegacyCmdBufferOpaque;
                aoRenderer.Get().RenderAfterOpaque(context);
            }

            bool isFogActive = fog.IsEnabledAndSupported(context);
            bool hasCustomOpaqueOnlyEffects = HasOpaqueOnlyEffects(context);
            int  opaqueOnlyEffects          = 0;

            opaqueOnlyEffects += isScreenSpaceReflectionsActive ? 1 : 0;
            opaqueOnlyEffects += isFogActive ? 1 : 0;
            opaqueOnlyEffects += hasCustomOpaqueOnlyEffects ? 1 : 0;

            // This works on right eye because it is resolved/populated at runtime
            var cameraTarget = new RenderTargetIdentifier(BuiltinRenderTextureType.CameraTarget);

            if (opaqueOnlyEffects > 0)
            {
                var cmd = m_LegacyCmdBufferOpaque;
                context.command = cmd;

                // We need to use the internal Blit method to copy the camera target or it'll fail
                // on tiled GPU as it won't be able to resolve
                int tempTarget0 = m_TargetPool.Get();
                context.GetScreenSpaceTemporaryRT(cmd, tempTarget0, 0, sourceFormat);
                cmd.Blit(cameraTarget, tempTarget0);
                context.source = tempTarget0;

                int tempTarget1 = -1;

                if (opaqueOnlyEffects > 1)
                {
                    tempTarget1 = m_TargetPool.Get();
                    context.GetScreenSpaceTemporaryRT(cmd, tempTarget1, 0, sourceFormat);
                    context.destination = tempTarget1;
                }
                else
                {
                    context.destination = cameraTarget;
                }

                if (isScreenSpaceReflectionsActive)
                {
                    ssrRenderer.Render(context);
                    opaqueOnlyEffects--;
                    var prevSource = context.source;
                    context.source      = context.destination;
                    context.destination = opaqueOnlyEffects == 1 ? cameraTarget : prevSource;
                }

                if (isFogActive)
                {
                    fog.Render(context);
                    opaqueOnlyEffects--;
                    var prevSource = context.source;
                    context.source      = context.destination;
                    context.destination = opaqueOnlyEffects == 1 ? cameraTarget : prevSource;
                }

                if (hasCustomOpaqueOnlyEffects)
                {
                    RenderOpaqueOnly(context);
                }

                if (opaqueOnlyEffects > 1)
                {
                    cmd.ReleaseTemporaryRT(tempTarget1);
                }

                cmd.ReleaseTemporaryRT(tempTarget0);
            }

            // Post-transparency stack
            // Same as before, first blit needs to use the builtin Blit command to properly handle
            // tiled GPUs
            int tempRt = m_TargetPool.Get();

            context.GetScreenSpaceTemporaryRT(m_LegacyCmdBuffer, tempRt, 0, sourceFormat, RenderTextureReadWrite.sRGB);
            m_LegacyCmdBuffer.Blit(cameraTarget, tempRt, RuntimeUtilities.copyStdMaterial, stopNaNPropagation ? 1 : 0);
            if (!m_NaNKilled)
            {
                m_NaNKilled = stopNaNPropagation;
            }

            context.command     = m_LegacyCmdBuffer;
            context.source      = tempRt;
            context.destination = cameraTarget;
            Render(context);
            m_LegacyCmdBuffer.ReleaseTemporaryRT(tempRt);
        }
Exemplo n.º 2
0
        private void BuildCommandBuffers()
        {
            PostProcessRenderContext currentContext      = m_CurrentContext;
            RenderTextureFormat      renderTextureFormat = (!m_Camera.allowHDR) ? RenderTextureFormat.Default : RuntimeUtilities.defaultHDRRenderTextureFormat;

            if (!RuntimeUtilities.isFloatingPointFormat(renderTextureFormat))
            {
                m_NaNKilled = true;
            }
            currentContext.Reset();
            currentContext.camera       = m_Camera;
            currentContext.sourceFormat = renderTextureFormat;
            m_LegacyCmdBufferBeforeReflections.Clear();
            m_LegacyCmdBufferBeforeLighting.Clear();
            m_LegacyCmdBufferOpaque.Clear();
            m_LegacyCmdBuffer.Clear();
            SetupContext(currentContext);
            currentContext.command = m_LegacyCmdBufferOpaque;
            TextureLerper.instance.BeginFrame(currentContext);
            UpdateSettingsIfNeeded(currentContext);
            PostProcessBundle        bundle                   = GetBundle <AmbientOcclusion>();
            AmbientOcclusion         ambientOcclusion         = bundle.CastSettings <AmbientOcclusion>();
            AmbientOcclusionRenderer ambientOcclusionRenderer = bundle.CastRenderer <AmbientOcclusionRenderer>();
            bool flag  = ambientOcclusion.IsEnabledAndSupported(currentContext);
            bool flag2 = ambientOcclusionRenderer.IsAmbientOnly(currentContext);
            bool flag3 = flag && flag2;
            bool flag4 = flag && !flag2;
            PostProcessBundle         bundle2  = GetBundle <ScreenSpaceReflections>();
            PostProcessEffectSettings settings = bundle2.settings;
            PostProcessEffectRenderer renderer = bundle2.renderer;
            bool flag5 = settings.IsEnabledAndSupported(currentContext);

            if (flag3)
            {
                IAmbientOcclusionMethod ambientOcclusionMethod = ambientOcclusionRenderer.Get();
                currentContext.command = m_LegacyCmdBufferBeforeReflections;
                ambientOcclusionMethod.RenderAmbientOnly(currentContext);
                currentContext.command = m_LegacyCmdBufferBeforeLighting;
                ambientOcclusionMethod.CompositeAmbientOnly(currentContext);
            }
            else if (flag4)
            {
                currentContext.command = m_LegacyCmdBufferOpaque;
                ambientOcclusionRenderer.Get().RenderAfterOpaque(currentContext);
            }
            bool flag6 = fog.IsEnabledAndSupported(currentContext);
            bool flag7 = HasOpaqueOnlyEffects(currentContext);
            int  num   = 0;

            num += (flag5 ? 1 : 0);
            num += (flag6 ? 1 : 0);
            num += (flag7 ? 1 : 0);
            RenderTargetIdentifier renderTargetIdentifier = new RenderTargetIdentifier(BuiltinRenderTextureType.CameraTarget);

            if (num > 0)
            {
                CommandBuffer commandBuffer = currentContext.command = m_LegacyCmdBufferOpaque;
                int           nameID        = m_TargetPool.Get();
                currentContext.GetScreenSpaceTemporaryRT(commandBuffer, nameID, 0, renderTextureFormat);
                commandBuffer.BuiltinBlit(renderTargetIdentifier, nameID, RuntimeUtilities.copyStdMaterial, stopNaNPropagation ? 1 : 0);
                currentContext.source = nameID;
                int nameID2 = -1;
                if (num > 1)
                {
                    nameID2 = m_TargetPool.Get();
                    currentContext.GetScreenSpaceTemporaryRT(commandBuffer, nameID2, 0, renderTextureFormat);
                    currentContext.destination = nameID2;
                }
                else
                {
                    currentContext.destination = renderTargetIdentifier;
                }
                if (flag5)
                {
                    renderer.Render(currentContext);
                    num--;
                    RenderTargetIdentifier source = currentContext.source;
                    currentContext.source      = currentContext.destination;
                    currentContext.destination = ((num != 1) ? source : renderTargetIdentifier);
                }
                if (flag6)
                {
                    fog.Render(currentContext);
                    num--;
                    RenderTargetIdentifier source2 = currentContext.source;
                    currentContext.source      = currentContext.destination;
                    currentContext.destination = ((num != 1) ? source2 : renderTargetIdentifier);
                }
                if (flag7)
                {
                    RenderOpaqueOnly(currentContext);
                }
                if (num > 1)
                {
                    commandBuffer.ReleaseTemporaryRT(nameID2);
                }
                commandBuffer.ReleaseTemporaryRT(nameID);
            }
            int nameID3 = m_TargetPool.Get();

            currentContext.GetScreenSpaceTemporaryRT(m_LegacyCmdBuffer, nameID3, 0, renderTextureFormat, RenderTextureReadWrite.sRGB);
            m_LegacyCmdBuffer.BuiltinBlit(renderTargetIdentifier, nameID3, RuntimeUtilities.copyStdMaterial, stopNaNPropagation ? 1 : 0);
            if (!m_NaNKilled)
            {
                m_NaNKilled = stopNaNPropagation;
            }
            currentContext.command     = m_LegacyCmdBuffer;
            currentContext.source      = nameID3;
            currentContext.destination = renderTargetIdentifier;
            Render(currentContext);
            m_LegacyCmdBuffer.ReleaseTemporaryRT(nameID3);
        }