Exemplo n.º 1
0
        public void ApplyOpaqueOnlyPostProcess(CommandBuffer cmd, RGCamera rgCam, RTHandleSystem.RTHandle colorBuffer, RTHandleSystem.RTHandle depthBuffer, RTHandleSystem.RTHandle r8Buffer)
        {
            if (m_FogandAOShader)
            {
                rgCam.SetupComputeShader(m_FogandAOShader, cmd);
                int actualKernel = m_FogandAOKernel;
                int threadZ      = 1;
                if (rgCam.StereoEnabled && rgCam.RenderTextureDimension == TextureDimension.Tex2DArray)
                {
                    actualKernel = m_FogandAOStereoKernel;
                    cmd.SetComputeTextureParam(m_FogandAOShader, actualKernel, ClusterShaderIDs._MainColorBufferArray, colorBuffer);
                    cmd.SetComputeTextureParam(m_FogandAOShader, actualKernel, ClusterShaderIDs._MainDepthBufferArray, depthBuffer);
                    threadZ = 2;
                }
                else
                {
                    cmd.SetComputeTextureParam(m_FogandAOShader, actualKernel, ClusterShaderIDs._MainColorBuffer, colorBuffer);
                    cmd.SetComputeTextureParam(m_FogandAOShader, actualKernel, ClusterShaderIDs._MainDepthBuffer, depthBuffer);
                }

                cmd.SetComputeTextureParam(m_FogandAOShader, actualKernel, ClusterShaderIDs._R8Buffer, r8Buffer);
                cmd.SetComputeTextureParam(m_FogandAOShader, actualKernel, ClusterShaderIDs._VolumetricFogTexture, ClusterPass.LightManager.GetFogTexture());

                int threadX = (int)Mathf.Ceil((float)rgCam.CameraWidth / 8.0f);
                int threadY = (int)Mathf.Ceil((float)rgCam.CameraHeight / 8.0f);

                cmd.DispatchCompute(m_FogandAOShader, actualKernel, threadX, threadY, threadZ);
            }
        }
Exemplo n.º 2
0
 public static void DrawFullScreen(CommandBuffer commandBuffer, RGCamera camera, Material material,
                                   RenderTargetIdentifier colorBuffer,
                                   MaterialPropertyBlock properties = null, int shaderPassId = 0)
 {
     CoreUtils.SetRenderTarget(commandBuffer, colorBuffer);
     commandBuffer.SetGlobalVector(ClusterShaderIDs._ScreenToTargetScale, camera.doubleBufferedViewportScale);
     commandBuffer.DrawProcedural(Matrix4x4.identity, material, shaderPassId, MeshTopology.Triangles, 3, 1, properties);
 }
Exemplo n.º 3
0
 // Scaling viewport is done for auto-scaling render targets.
 // In the context of HDRP, every auto-scaled RT is scaled against the maximum RTHandles reference size (that can only grow).
 // When we render using a camera whose viewport is smaller than the RTHandles reference size (and thus smaller than the RT actual size), we need to set it explicitly (otherwise, native code will set the viewport at the size of the RT)
 // For auto-scaled RTs (like for example a half-resolution RT), we need to scale this viewport accordingly.
 // For non scaled RTs we just do nothing, the native code will set the viewport at the size of the RT anyway.
 public static void SetViewport(CommandBuffer cmd, RGCamera camera, RTHandleSystem.RTHandle target)
 {
     if (target.useScaling)
     {
         Debug.Assert(camera != null, "Missing HDCamera when setting up Render Target with auto-scale and Viewport.");
         Vector2Int scaledViewportSize = target.GetScaledSize(new Vector2Int(camera.CameraWidth, camera.CameraHeight));
         cmd.SetViewport(new Rect(0.0f, 0.0f, scaledViewportSize.x, scaledViewportSize.y));
     }
 }
Exemplo n.º 4
0
        public static RGCamera Get(Camera camera, PostProcessLayer postProcessLayer, int alignedClusterSize, bool enablePostProcess)
        {
            RGCamera rgCam;

            if (!s_Cameras.TryGetValue(camera, out rgCam))
            {
                rgCam = new RGCamera(camera);
                s_Cameras.Add(camera, rgCam);
            }

            rgCam.Update(postProcessLayer, alignedClusterSize, enablePostProcess);

            return(rgCam);
        }
Exemplo n.º 5
0
        private static void SetViewportAndClear(CommandBuffer cmd, RGCamera camera, RTHandleSystem.RTHandle buffer, ClearFlag clearFlag, Color clearColor)
        {
            // Clearing a partial viewport currently does not go through the hardware clear.
            // Instead it goes through a quad rendered with a specific shader.
            // When enabling wireframe mode in the scene view, unfortunately it overrides this shader thus breaking every clears.
            // That's why in the editor we don't set the viewport before clearing (it's set to full screen by the previous SetRenderTarget) but AFTER so that we benefit from un-bugged hardware clear.
            // We consider that the small loss in performance is acceptable in the editor.
            // A refactor of wireframe is needed before we can fix this properly (with not doing anything!)
#if !UNITY_EDITOR
            SetViewport(cmd, camera, buffer);
#endif
            CoreUtils.ClearRenderTarget(cmd, clearFlag, clearColor);
#if UNITY_EDITOR
            SetViewport(cmd, camera, buffer);
#endif
        }
Exemplo n.º 6
0
        public static void GetFrameConfigration(RGCamera rgCam, PostProcessLayer postProcessLayer, VolumetricSettings volumetricSettings, ref FrameConfigration frameConfig)
        {
            AdditionalCameraData acd = rgCam.camera.GetComponent <AdditionalCameraData>();

            frameConfig.enableHDR         = true;
            frameConfig.enablePostprocess = CoreUtils.IsPostProcessingActive(postProcessLayer);
            if (acd)
            {
                switch (acd.m_RenderingType)
                {
                case AdditionalCameraData.RenderingType.ClusterLight:
                    frameConfig.enableSky               = true;
                    frameConfig.enableShadows           = true;
                    frameConfig.enableAsyncCompute      = true;
                    frameConfig.enableClusterLighting   = true;
                    frameConfig.enableVolumetricDisplay = acd.m_DisplayVolumetricFog;
                    break;

                case AdditionalCameraData.RenderingType.Unlit:
                    frameConfig.enableShadows           = false;
                    frameConfig.enableAsyncCompute      = false;
                    frameConfig.enableSky               = true;
                    frameConfig.enableDepthPrePass      = false;
                    frameConfig.enableClusterLighting   = false;
                    frameConfig.enableVolumetricDisplay = false;
                    break;

                case AdditionalCameraData.RenderingType.SimpleLight:
                    frameConfig.enableAsyncCompute      = false;
                    frameConfig.enableSky               = true;
                    frameConfig.enableDepthPrePass      = false;
                    frameConfig.enableVolumetricFog     = false;
                    frameConfig.enableClusterLighting   = false;
                    frameConfig.enableVolumetricDisplay = false;
                    break;

                case AdditionalCameraData.RenderingType.StaticShadow:
                    frameConfig.enableSky               = false;
                    frameConfig.enableShadows           = false;
                    frameConfig.enableAsyncCompute      = false;
                    frameConfig.enableDepthPrePass      = false;
                    frameConfig.enableVolumetricFog     = false;
                    frameConfig.enableClusterLighting   = false;
                    frameConfig.enableVolumetricDisplay = false;
                    break;

                default:
                    break;
                }
            }
#if UNITY_EDITOR
            else if (rgCam.camera.cameraType == CameraType.SceneView)
            {
                frameConfig.enableSky               = true;
                frameConfig.enableShadows           = true;
                frameConfig.enableAsyncCompute      = true;
                frameConfig.enableClusterLighting   = true;
                frameConfig.enableVolumetricDisplay = true;
            }
#endif

            bool msaaChanged    = false;
            var  commonSettings = VolumeManager.instance.stack.GetComponent <CommonSettings>();
            if (commonSettings)
            {
                frameConfig.enableAsyncCompute      = commonSettings.enableAsyncCompute;
                frameConfig.hasRefraction           = commonSettings.hasRefraction;
                frameConfig.enableScreenSpaceShadow = commonSettings.enableScreenSpaceShadow;
                frameConfig.enableDepthPrePass      = commonSettings.enableDepthPrepass;
                msaaChanged = frameConfig.rtConfig.msaaSamples != commonSettings.msaaSamples;
                frameConfig.enableLightCullingMask = commonSettings.enableLightCullingMask;
                frameConfig.rtConfig.msaaSamples   = commonSettings.msaaSamples;
            }

            frameConfig.enableGradientResolution = rgCam.camera.stereoEnabled && !frameConfig.hasRefraction;

            frameConfig.enableVolumetricLighting = false;
            frameConfig.enableVolumetricFog      = false;
            if (volumetricSettings /* && volumetricSettings.BaseDensity != 0*/)
            {
                frameConfig.enableVolumetricLighting = true;
                frameConfig.enableVolumetricFog      = true;
            }

#if UNITY_EDITOR
            if (rgCam.camera.cameraType == CameraType.Preview || rgCam.camera.name.Contains("Preview"))
            {
                frameConfig.enableDepthPrePass       = false;
                frameConfig.enablePostprocess        = false;
                frameConfig.enableClusterLighting    = true;
                frameConfig.enableVolumetricFog      = false;
                frameConfig.enableVolumetricLighting = false;
                frameConfig.enableHalfResParticle    = false;
                frameConfig.enableShadows            = false;
                frameConfig.enableSky               = false;
                frameConfig.enableAsyncCompute      = false;
                frameConfig.enableScreenSpaceShadow = false;
                frameConfig.enableStaticShadowmap   = false;
                frameConfig.rtConfig.msaaSamples    = MSAASamples.None;
            }
#endif

            if (rgCam.CameraWidth != frameConfig.rtConfig.frameWidth || rgCam.CameraHeight != frameConfig.rtConfig.frameHeight || rgCam.RenderTextureDimension != frameConfig.rtConfig.textureDimension || msaaChanged)
            {
                frameConfig.rtConfigChanged           = true;
                frameConfig.rtConfig.frameWidth       = rgCam.CameraWidth;
                frameConfig.rtConfig.frameHeight      = rgCam.CameraHeight;
                frameConfig.rtConfig.textureDimension = rgCam.RenderTextureDimension;
#if UNITY_EDITOR
                if (frameConfig.enableVolumetricFog || frameConfig.enableVolumetricLighting)
                {
                    if (frameConfig.rtConfig.volumetricWidth != rgCam.CameraWidth || frameConfig.rtConfig.volumetricHeight != rgCam.CameraHeight)
                    {
                        frameConfig.rtConfig.volumetricWidth          = rgCam.CameraWidth;
                        frameConfig.rtConfig.volumetricHeight         = rgCam.CameraHeight;
                        frameConfig.rtConfig.volumetricNeedReallocate = true;
                    }
                    else
                    {
                        frameConfig.rtConfig.volumetricNeedReallocate = false;
                    }
                }
#endif
            }
            else
            {
                frameConfig.rtConfigChanged = false;
            }

            if (frameConfig.rtConfigChanged)
            {
                frameConfig.clusterConfigChanged = true;
            }
            else
            {
                frameConfig.clusterConfigChanged = false;
            }
        }
Exemplo n.º 7
0
        public void GenerateGradientResStencilMask(CommandBuffer cmd, RTHandleSystem.RTHandle r8Buffer, RGCamera rgCam, bool gradiant)
        {
            if (!m_StencilMaskGenerated)
            {
                int actualKernal = m_StencilMaskGenShader.FindKernel("GradiantStencilMaskGen");
                if (!gradiant)
                {
                    actualKernal = m_StencilMaskGenShader.FindKernel("NonGradiantStencilMaskGen");
                }
                int stencilMaskIndex = Time.frameCount % 4;
                cmd.SetComputeIntParam(m_StencilMaskGenShader, ClusterShaderIDs._MaskIndex, stencilMaskIndex);
                cmd.SetComputeTextureParam(m_StencilMaskGenShader, actualKernal, ClusterShaderIDs._R8Buffer, r8Buffer);


                int threadX = (int)Mathf.Ceil((float)rgCam.CameraWidth / 8.0f);
                int threadY = (int)Mathf.Ceil((float)rgCam.CameraHeight / 8.0f);
                cmd.SetComputeVectorParam(m_StencilMaskGenShader, ClusterShaderIDs._GroupSize, new Vector4(threadX, threadY, 0, 0));
                cmd.DispatchCompute(m_StencilMaskGenShader, actualKernal, threadX, threadY, 1);
            }
        }
Exemplo n.º 8
0
        public void ExpandMaskedBuffer(CommandBuffer cmd, RTHandleSystem.RTHandle r8Buffer, RTHandleSystem.RTHandle colorBuffer, RTHandleSystem.RTHandle velocityBuffer, RTHandleSystem.RTHandle depthBuffer, RGCamera rgCam)
        {
            if (m_ExpandMaskedBufferShader)
            {
                int threadX = (int)Mathf.Ceil((float)rgCam.CameraWidth / 8.0f);
                int threadY = (int)Mathf.Ceil((float)rgCam.CameraHeight / 8.0f);

                int actualKernel = m_ExpandMaskedBufferKernal;
                int threadZ      = 1;
                if (rgCam.StereoEnabled && rgCam.RenderTextureDimension == TextureDimension.Tex2DArray)
                {
                    actualKernel = m_ExpandMaskedBufferShader.FindKernel("ExpandMaskedBufferStereo");
                    cmd.SetComputeTextureParam(m_ExpandMaskedBufferShader, actualKernel, ClusterShaderIDs._MainColorBufferArray, colorBuffer);
                    cmd.SetComputeTextureParam(m_ExpandMaskedBufferShader, actualKernel, ClusterShaderIDs._MainDepthBufferArray, depthBuffer);
                    cmd.SetComputeTextureParam(m_ExpandMaskedBufferShader, actualKernel, ClusterShaderIDs._MainVelocityBufferArray, velocityBuffer);
                    cmd.SetComputeVectorParam(m_ExpandMaskedBufferShader, ClusterShaderIDs._GroupSize, new Vector4(threadX, threadY, 0, 0));
                    threadZ = 2;
                }
                else
                {
                    cmd.SetComputeTextureParam(m_ExpandMaskedBufferShader, actualKernel, ClusterShaderIDs._MainColorBuffer, colorBuffer);
                    cmd.SetComputeTextureParam(m_ExpandMaskedBufferShader, actualKernel, ClusterShaderIDs._MainDepthBuffer, depthBuffer);
                    cmd.SetComputeTextureParam(m_ExpandMaskedBufferShader, actualKernel, ClusterShaderIDs._MainVelocityBuffer, velocityBuffer);
                }

                cmd.SetComputeTextureParam(m_ExpandMaskedBufferShader, actualKernel, ClusterShaderIDs._R8Buffer, r8Buffer);

                cmd.DispatchCompute(m_ExpandMaskedBufferShader, actualKernel, threadX, threadY, threadZ);
            }
        }
Exemplo n.º 9
0
 public static void SetRenderTarget(CommandBuffer cmd, RGCamera camera, RTHandleSystem.RTHandle buffer, ClearFlag clearFlag = ClearFlag.None, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = 0)
 {
     SetRenderTarget(cmd, camera, buffer, clearFlag, CoreUtils.clearColorAllBlack, miplevel, cubemapFace, depthSlice);
 }
Exemplo n.º 10
0
 // This set of RenderTarget management methods is supposed to be used when rendering into a camera dependent render texture.
 // This will automatically set the viewport based on the camera size and the RTHandle scaling info.
 public static void SetRenderTarget(CommandBuffer cmd, RGCamera camera, RTHandleSystem.RTHandle buffer, ClearFlag clearFlag, Color clearColor, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = 0)
 {
     cmd.SetRenderTarget(buffer, miplevel, cubemapFace, depthSlice);
     SetViewportAndClear(cmd, camera, buffer, clearFlag, clearColor);
 }
Exemplo n.º 11
0
 public static void BlitCameraTexture(CommandBuffer cmd, RGCamera rgCam, RTHandleSystem.RTHandle source, RTHandleSystem.RTHandle destination, float mipLevel = 0.0f, bool bilinear = false)
 {
     SetRenderTarget(cmd, rgCam, destination);
     BlitTexture(cmd, source, destination, rgCam.viewportScaleBias, mipLevel, bilinear);
 }
Exemplo n.º 12
0
 public static void SetRenderTarget(CommandBuffer cmd, RGCamera camera, RenderTargetIdentifier[] colorBuffers, RTHandleSystem.RTHandle depthBuffer, ClearFlag clearFlag, Color clearColor)
 {
     cmd.SetRenderTarget(colorBuffers, depthBuffer);
     SetViewport(cmd, camera, depthBuffer);
     CoreUtils.ClearRenderTarget(cmd, clearFlag, clearColor);
 }
Exemplo n.º 13
0
 public static void SetRenderTarget(CommandBuffer cmd, RGCamera camera, RenderTargetIdentifier[] colorBuffers, RTHandleSystem.RTHandle depthBuffer, ClearFlag clearFlag = ClearFlag.None)
 {
     CoreUtils.SetRenderTarget(cmd, colorBuffers, depthBuffer); // Don't clear here, viewport needs to be set before we do.
     SetViewportAndClear(cmd, camera, depthBuffer, clearFlag, CoreUtils.clearColorAllBlack);
 }
Exemplo n.º 14
0
 public static void SetRenderTarget(CommandBuffer cmd, RGCamera camera, RenderTargetIdentifier[] colorBuffers, RTHandleSystem.RTHandle depthBuffer)
 {
     CoreUtils.SetRenderTarget(cmd, colorBuffers, depthBuffer, ClearFlag.None, CoreUtils.clearColorAllBlack);
     SetViewport(cmd, camera, depthBuffer);
 }
Exemplo n.º 15
0
        public void Render(ScriptableRenderContext renderContext, CommandBuffer cmd)
        {
            if (needRender || isDynamic)
            {
                needRender = false;

                GameObject probeCamObj = new GameObject("Probe Camera");
                probeCamObj.hideFlags = HideFlags.HideAndDontSave;
                Camera probeCamera = probeCamObj.AddComponent <Camera>();
                probeCamera.enabled          = false;
                probeCamera.renderingPath    = RenderingPath.Forward;
                probeCamera.nearClipPlane    = NearPlane;
                probeCamera.farClipPlane     = FarPlane;
                probeCamera.depthTextureMode = DepthTextureMode.None;
                probeCamera.clearFlags       = CameraClearFlags.SolidColor | CameraClearFlags.Depth;
                probeCamera.backgroundColor  = Color.black;
                probeCamera.orthographic     = false;
                probeCamera.hideFlags        = HideFlags.HideAndDontSave;
                probeCamera.allowMSAA        = false;
                probeCamera.stereoTargetEye  = StereoTargetEyeMask.None;
                probeCamera.fieldOfView      = 90.0f;
                probeCamera.aspect           = 1;
                probeCamObj.SetActive(false);


                for (int i = 0; i < Probes.Count; i++)
                {
                    probeCamObj.transform.position = Probes[i].position;
                    for (int j = 0; j < 6; j++)
                    {
                        probeCamObj.transform.rotation = rotations[j];
                        m_ProbeLightManager.NewFrame();
                        RGCamera rgCam = RGCamera.Get(probeCamera, null, 128, false);

                        ScriptableCullingParameters cullingParams;

                        if (!CullResults.GetCullingParameters(probeCamera, false, out cullingParams))
                        {
                            continue;
                        }

                        CullResults.Cull(ref cullingParams, renderContext, ref m_cullResults);
                        m_ProbeLightManager.UpdateCullingParameters(ref cullingParams, false);
                        m_ProbeLightManager.PrepareLightsDataForGPU(cmd, m_ShadowSettings, m_cullResults, probeCamera, false);

                        bool enableStaticShadowmap = false;
                        if (m_ShadowSettings.StaticShadowmap)
                        {
                            enableStaticShadowmap = true;
                            cmd.SetGlobalTexture(ClusterShaderIDs._StaticShadowmapExp, m_ShadowSettings.StaticShadowmap);
                        }

                        var clusterShadowSettings = VolumeManager.instance.stack.GetComponent <ClusterShadowSettings>();
                        if (clusterShadowSettings)
                        {
                            m_ShadowSettings.MaxShadowDistance  = clusterShadowSettings.MaxShadowDistance;
                            m_ShadowSettings.MaxShadowCasters   = clusterShadowSettings.MaxShadowCasters;
                            m_ShadowSettings.ShadowmapRes       = clusterShadowSettings.ShadowMapResolution;
                            m_ShadowSettings.StaticShadowmapRes = clusterShadowSettings.StaticShadowMapResolution;
                            m_ShadowSettings.StaticShadowmap    = clusterShadowSettings.staticShadowmap.value;
                        }
                        else
                        {
                            m_ShadowSettings.MaxShadowDistance  = 1000.0f;
                            m_ShadowSettings.MaxShadowCasters   = 5;
                            m_ShadowSettings.ShadowmapRes       = new Vector2Int(m_ShadowInitParams.shadowAtlasWidth, m_ShadowInitParams.shadowAtlasHeight);
                            m_ShadowSettings.StaticShadowmapRes = Vector2Int.one;
                            m_ShadowSettings.StaticShadowmap    = null;
                        }

                        m_ProbeLightManager.RenderShadows(renderContext, cmd, m_cullResults);
                        m_ProbeLightManager.PostBlurExpShadows(cmd, 1);

                        m_ProbeLightManager.ClusterLightCompute(rgCam, cmd);
                        rgCam.SetupGlobalParams(cmd, 0, 0);

                        renderContext.ExecuteCommandBuffer(cmd);
                        cmd.Clear();
                        renderContext.SetupCameraProperties(probeCamera, rgCam.StereoEnabled); // Need to recall SetupCameraProperties after m_ShadowPass.Render

                        //RenderTargetIdentifier[] rendertargets = { Probes[i].RadianceTexture, Probes[i].NormalTexture };
                        cmd.SetRenderTarget(radianceCubeArray, probeDepth, 0, CubemapFace.Unknown, i * 6 + (int)faces[j]);
                        cmd.ClearRenderTarget(true, true, probeCamera.backgroundColor.linear);
                        RendererConfiguration settings = RendererConfiguration.PerObjectReflectionProbes | RendererConfiguration.PerObjectLightmaps | RendererConfiguration.PerObjectLightProbe;
                        RenderRendererList(m_cullResults, rgCam.camera, renderContext, cmd, m_RadiancePassNames, RGRenderQueue.k_RenderQueue_AllOpaque, settings);
                        renderContext.DrawSkybox(probeCamera);

                        //RenderTargetIdentifier[] rendertargets = { Probes[i].RadianceTexture, Probes[i].NormalTexture };
                        cmd.SetRenderTarget(normalMapArray, probeDepth, 0, CubemapFace.Unknown, i * 6 + (int)faces[j]);
                        cmd.ClearRenderTarget(true, true, new Color(0, 0, 0));
                        RenderRendererList(m_cullResults, rgCam.camera, renderContext, cmd, m_NormalPassNames, RGRenderQueue.k_RenderQueue_AllOpaque);

                        //RenderTargetIdentifier[] rendertargets = { Probes[i].RadianceTexture, Probes[i].NormalTexture };
                        cmd.SetRenderTarget(depthMapArray, probeDepth, 0, CubemapFace.Unknown, i * 6 + (int)faces[j]);
                        cmd.ClearRenderTarget(true, true, new Color(1, 1, 1));
                        RenderRendererList(m_cullResults, rgCam.camera, renderContext, cmd, m_DepthPassNames, RGRenderQueue.k_RenderQueue_AllOpaque);

                        renderContext.Submit();
                    }
                }

                ReprojectCubeToOctan(renderContext, cmd);

                UnityEngine.Object.DestroyImmediate(probeCamera);
                UnityEngine.Object.DestroyImmediate(probeCamObj);
            }
        }