Example #1
0
        public override void Render(ScriptableRenderContext renderContext, Camera[] cameras)
        {
            base.Render(renderContext, cameras);

#if UNITY_EDITOR
            SupportedRenderingFeatures.active = s_NeededFeatures;
#endif

            GraphicsSettings.lightsUseLinearIntensity  = true;
            GraphicsSettings.lightsUseColorTemperature = true;

            m_SkyManager.Build();

            if (!m_LitRenderLoop.isInit)
            {
                m_LitRenderLoop.RenderInit(renderContext);
            }

            // Do anything we need to do upon a new frame.

            if (m_LightLoop != null)
            {
                m_LightLoop.NewFrame();
            }

            m_Owner.ApplyDebugParameters();
            m_Owner.UpdateCommonSettings();

            // Set Frame constant buffer
            // TODO...

            // we only want to render one camera for now
            // select the most main camera!

            Camera camera = cameras.OrderByDescending(x => x.tag == "MainCamera").FirstOrDefault();
            if (camera == null)
            {
                return;
            }

            // Set camera constant buffer
            // TODO...

            CullingParameters cullingParams;
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
            {
                return;
            }

            m_ShadowPass.UpdateCullingParameters(ref cullingParams);

            var cullResults = CullResults.Cull(ref cullingParams, renderContext);

            Resize(camera);

            renderContext.SetupCameraProperties(camera);

            HDCamera hdCamera = Utilities.GetHDCamera(camera);

            // TODO: Find a correct place to bind these material textures
            // We have to bind the material specific global parameters in this mode
            m_LitRenderLoop.Bind();

            InitAndClearBuffer(camera, renderContext);

            RenderDepthPrepass(cullResults, camera, renderContext);

            // Forward opaque with deferred/cluster tile require that we fill the depth buffer
            // correctly to build the light list.
            // TODO: avoid double lighting by tagging stencil or gbuffer that we must not lit.
            RenderForwardOnlyOpaqueDepthPrepass(cullResults, camera, renderContext);
            RenderGBuffer(cullResults, camera, renderContext);

            // 'm_CameraStencilBufferRT' is a temporary copy of the stencil buffer and should be removed
            // once we are able to read from the depth buffer and perform the stencil test simultaneously.
            using (new Utilities.ProfilingSample("Copy depth-stencil buffer", renderContext))
            {
                var cmd = new CommandBuffer();
                cmd.CopyTexture(m_CameraDepthStencilBufferRT, m_CameraStencilBufferRT);
                renderContext.ExecuteCommandBuffer(cmd);
                cmd.Dispose();
            }

            if (debugParameters.debugViewMaterial != 0)
            {
                RenderDebugViewMaterial(cullResults, hdCamera, renderContext);
            }
            else
            {
                using (new Utilities.ProfilingSample("Shadow Pass", renderContext))
                {
                    m_ShadowPass.Render(renderContext, cullResults, out m_ShadowsResult);
                }

                renderContext.SetupCameraProperties(camera); // Need to recall SetupCameraProperties after m_ShadowPass.Render

                if (m_LightLoop != null)
                {
                    using (new Utilities.ProfilingSample("Build Light list", renderContext))
                    {
                        m_LightLoop.PrepareLightsForGPU(m_Owner.shadowSettings, cullResults, camera, ref m_ShadowsResult);
                        m_LightLoop.BuildGPULightLists(camera, renderContext, m_CameraDepthStencilBufferRT); // TODO: Use async compute here to run light culling during shadow
                    }
                }

                PushGlobalParams(hdCamera, renderContext);

                // Caution: We require sun light here as some sky use the sun light to render, mean UpdateSkyEnvironment
                // must be call after BuildGPULightLists.
                // TODO: Try to arrange code so we can trigger this call earlier and use async compute here to run sky convolution during other passes (once we move convolution shader to compute).
                UpdateSkyEnvironment(hdCamera, renderContext);

                RenderDeferredLighting(hdCamera, renderContext);

                // We compute subsurface scattering here. Therefore, no objects rendered afterwards will exhibit SSS.
                // Currently, there is no efficient way to switch between SRT and MRT for the forward pass;
                // therefore, forward-rendered objects do not output split lighting required for the SSS pass.
                CombineSubsurfaceScattering(hdCamera, renderContext, m_Owner.sssParameters);

                // For opaque forward we have split rendering in two categories
                // Material that are always forward and material that can be deferred or forward depends on render pipeline options (like switch to rendering forward only mode)
                // Material that are always forward are unlit and complex (Like Hair) and don't require sorting, so it is ok to split them.
                RenderForward(cullResults, camera, renderContext, true); // Render deferred or forward opaque
                RenderForwardOnlyOpaque(cullResults, camera, renderContext);

                RenderSky(hdCamera, renderContext);

                // Render all type of transparent forward (unlit, lit, complex (hair...)) to keep the sorting between transparent objects.
                RenderForward(cullResults, camera, renderContext, false);

                RenderVelocity(cullResults, camera, renderContext); // Note we may have to render velocity earlier if we do temporalAO, temporal volumetric etc... Mean we will not take into account forward opaque in case of deferred rendering ?

                // TODO: Check with VFX team.
                // Rendering distortion here have off course lot of artifact.
                // But resolving at each objects that write in distortion is not possible (need to sort transparent, render those that do not distort, then resolve, then etc...)
                // Instead we chose to apply distortion at the end after we cumulate distortion vector and desired blurriness. This
                RenderDistortion(cullResults, camera, renderContext);

                FinalPass(camera, renderContext);
            }

            RenderDebugOverlay(camera, renderContext);

            // bind depth surface for editor grid/gizmo/selection rendering
            if (camera.cameraType == CameraType.SceneView)
            {
                var cmd = new CommandBuffer();
                cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, m_CameraDepthStencilBufferRT);
                renderContext.ExecuteCommandBuffer(cmd);
                cmd.Dispose();
            }

            renderContext.Submit();
        }
Example #2
0
        public HDRenderPipelineInstance(HDRenderPipeline owner)
        {
            m_Owner = owner;

            m_CameraColorBuffer            = Shader.PropertyToID("_CameraColorTexture");
            m_CameraSubsurfaceBuffer       = Shader.PropertyToID("_CameraSubsurfaceTexture");
            m_CameraFilteringBuffer        = Shader.PropertyToID("_CameraFilteringBuffer");
            m_CameraDepthStencilBuffer     = Shader.PropertyToID("_CameraDepthTexture");
            m_CameraDepthStencilBufferCopy = Shader.PropertyToID("_CameraDepthTextureCopy");
            m_CameraStencilBuffer          = Shader.PropertyToID("_CameraStencilTexture");

            m_CameraColorBufferRT            = new RenderTargetIdentifier(m_CameraColorBuffer);
            m_CameraSubsurfaceBufferRT       = new RenderTargetIdentifier(m_CameraSubsurfaceBuffer);
            m_CameraFilteringBufferRT        = new RenderTargetIdentifier(m_CameraFilteringBuffer);
            m_CameraDepthStencilBufferRT     = new RenderTargetIdentifier(m_CameraDepthStencilBuffer);
            m_CameraDepthStencilBufferCopyRT = new RenderTargetIdentifier(m_CameraDepthStencilBufferCopy);
            m_CameraStencilBufferRT          = new RenderTargetIdentifier(m_CameraStencilBuffer);

            m_DebugViewMaterialGBuffer = Utilities.CreateEngineMaterial("Hidden/HDRenderPipeline/DebugViewMaterialGBuffer");

            m_FilterSubsurfaceScattering = Utilities.CreateEngineMaterial("Hidden/HDRenderPipeline/CombineSubsurfaceScattering");
            m_FilterSubsurfaceScattering.DisableKeyword("FILTER_HORIZONTAL");
            m_FilterSubsurfaceScattering.SetFloat("_DstBlend", (float)BlendMode.Zero);

            m_FilterAndCombineSubsurfaceScattering = Utilities.CreateEngineMaterial("Hidden/HDRenderPipeline/CombineSubsurfaceScattering");
            m_FilterSubsurfaceScattering.EnableKeyword("FILTER_HORIZONTAL");
            m_FilterAndCombineSubsurfaceScattering.SetFloat("_DstBlend", (float)BlendMode.One);

            m_DebugDisplayShadowMap = Utilities.CreateEngineMaterial("Hidden/HDRenderPipeline/DebugDisplayShadowMap");

            m_ShadowPass = new ShadowRenderPass(owner.shadowSettings);

            // Init Gbuffer description
            m_gbufferManager.gbufferCount = m_LitRenderLoop.GetMaterialGBufferCount();
            RenderTextureFormat[]    RTFormat;
            RenderTextureReadWrite[] RTReadWrite;
            m_LitRenderLoop.GetMaterialGBufferDescription(out RTFormat, out RTReadWrite);

            for (int gbufferIndex = 0; gbufferIndex < m_gbufferManager.gbufferCount; ++gbufferIndex)
            {
                m_gbufferManager.SetBufferDescription(gbufferIndex, "_GBufferTexture" + gbufferIndex, RTFormat[gbufferIndex], RTReadWrite[gbufferIndex]);
            }

            m_VelocityBuffer = Shader.PropertyToID("_VelocityTexture");
            if (ShaderConfig.s_VelocityInGbuffer == 1)
            {
                // If velocity is in GBuffer then it is in the last RT. Assign a different name to it.
                m_gbufferManager.SetBufferDescription(m_gbufferManager.gbufferCount, "_VelocityTexture", Builtin.RenderLoop.GetVelocityBufferFormat(), Builtin.RenderLoop.GetVelocityBufferReadWrite());
                m_gbufferManager.gbufferCount++;
            }
            m_VelocityBufferRT = new RenderTargetIdentifier(m_VelocityBuffer);

            m_DistortionBuffer   = Shader.PropertyToID("_DistortionTexture");
            m_DistortionBufferRT = new RenderTargetIdentifier(m_DistortionBuffer);

            m_LitRenderLoop.Build();

            if (owner.lightLoopProducer)
            {
                m_LightLoop = owner.lightLoopProducer.CreateLightLoop();
            }

            if (m_LightLoop != null)
            {
                m_LightLoop.Build(owner.textureSettings);
            }

            m_SkyManager.Build();
            m_SkyManager.skySettings = owner.skySettingsToUse;
        }