Occlusion culling not working if you call it in Custom pass

Before Unity 6 I used ctx.renderContext.Cull() in Custom passes and everything works for me.
But in Unity 6 I get the error if I have Terrain in the scene (Draw Instanced option doesn’t affected, it could be enable or disable):

Unable to add Renderer to the Scene after Culling.
Possible cause: A Camera callback, such as OnPreRender, called Graphics.DrawMesh.
Solution: If this is the cause, move the callback to earlier in the frame, to in OnPreCull, for example.
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Is this correct work and now it is impossible to use culling in custom pass? Because as far as I understand any injection point happens after OnPreCull. Or is it a bug? Thanks!

Hello,

We added a new protection to avoid crashes related to the late addition of renderers when the rendering already started, see this API for more info: Unity - Scripting API: Rendering.ScriptableRenderContext.PushDisableApiRenderers

I believe you are calling the Cull of the ScriptableRenderContext inside the Execute function of the custom pass which is too late for that now. Instead, can you try to override the AggregateCullingParameters function in the custom pass?
https://docs.unity3d.com/Packages/com.unity.render-pipelines.high-definition@17.0/api/UnityEngine.Rendering.HighDefinition.CustomPass.html

You can then get your culling result from the CustomPassContext.cullingResults field and the original camera culling will be inside CustomPassContext.cameraCullingResults.

1 Like

I looked and both methods will really solve the problem.
Thank you very much!

I would like to note that in the custom pass I called culling on not main camera that is disabled in the scene and is used by me only for custom culling of specific objects, I understand that rendering on the main camera is already happening and if I called culling on the main camera it could be an error. But in my case, using ScriptableRenderContext.PushDisableApiRenderers() solves the problem.

I have the same problem with this error. As far as I understand I can’t use AggregateCullingParameters. For my use case I have a CustomPass that draws certain Renderers that are in specific Layer with params from another Camera. What would be the recommendation here?


            if (overrideCameraValues.TryGetCullingParameters(out ScriptableCullingParameters cullingParameters))
            {
                var renderContext = ctx.renderContext;


                cullingParameters.cullingOptions &= ~CullingOptions.OcclusionCull;
                // cullingParameters.cullingMatrix = overrideCameraValues.cullingMatrix;
                cullingParameters.cullingMask |= (uint)(int)layerMask;

                CullingResults cullingResults = renderContext.Cull(ref cullingParameters);


                // // Use these culling results to render objects with the specified layer mask
                // var drawingSettings = new DrawingSettings();
                // var filteringSettings = new FilteringSettings(RenderQueueRange.all, itemOverUILayerMask);

                var result = new RendererListDesc(shaderPasses, cullingResults, overrideCameraValues)
                {
                    rendererConfiguration = renderConfig,
                    renderQueueRange = GetRenderQueueRange(renderQueueType),
                    sortingCriteria = sortingCriteria,
                    excludeObjectMotionVectors = false,
                    overrideShader = overrideMode == OverrideMaterialMode.Shader ? overrideShader : null,
                    overrideMaterial = overrideMode == OverrideMaterialMode.Material ? overrideMaterial : null,
                    overrideMaterialPassIndex = (overrideMaterial != null) ? overrideMaterial.FindPass(overrideMaterialPassName) : 0,
                    overrideShaderPassIndex = (overrideShader != null) ? overrideShaderMaterial.FindPass(overrideShaderPassName) : 0,
                    stateBlock = stateBlock,
                    layerMask = layerMask,
                };

                Object.DestroyImmediate(overrideShaderMaterial);
                var renderCtx = ctx.renderContext;
                var rendererList = renderCtx.CreateRendererList(result);
                bool opaque = renderQueueType == RenderQueueType.AllOpaque || renderQueueType == RenderQueueType.OpaqueAlphaTest || renderQueueType == RenderQueueType.OpaqueNoAlphaTest;
            
                
                using (new CustomPassUtils.OverrideCameraRendering(ctx, overrideCameraValues))
                {
                    RenderForwardRendererList(ctx.hdCamera.frameSettings, rendererList, opaque, ctx.renderContext, ctx.cmd);
                }
            }

Hi, i am using the camera.render() in BiRP pipeline in OnWillRenderObject and get the error spamming in the console, while everything seem to be working, in Unity 6000.0.25

Is there an option to remove the error, as seems like is not relevant in this case ?

Thanks

It is the original reflections script from Unity water that uses this method and worked fine in 2022.3 and in Unity 6 also works, but gives this error.