Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Editor crash while implementation RenderPipeline has something wrong

Discussion in 'Graphics Experimental Previews' started by kakashi8841, Nov 8, 2018.

  1. kakashi8841

    kakashi8841

    Joined:
    Apr 27, 2018
    Posts:
    10
    First, I implement the RenderPipeline in C#.

    The following code is what I used :
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Experimental.Rendering;
    3. using UnityEngine.Rendering;
    4.  
    5. public class BasicPipeInstance : RenderPipeline
    6. {
    7.     private Color _ClearColor;
    8.  
    9.     public BasicPipeInstance(Color clearColor) {
    10.         _ClearColor = clearColor;
    11.     }
    12.  
    13.     public override void Render(ScriptableRenderContext context, Camera[] cameras)
    14.     {
    15.         base.Render(context, cameras);
    16.  
    17.         var cmd = new CommandBuffer();
    18.  
    19.         cmd.ClearRenderTarget(true, true, _ClearColor);
    20.         context.ExecuteCommandBuffer(cmd);
    21.  
    22.         // I only have one camera in my scene.
    23.         var camera = cameras[0];
    24.  
    25.         ScriptableCullingParameters cullingParameters;
    26.         if (!CullResults.GetCullingParameters(camera, out cullingParameters))
    27.         {
    28.             return;
    29.         }
    30.         CullResults cullResults = CullResults.Cull(ref cullingParameters, context);
    31.  
    32.         var opaqueRange = new FilterRenderersSettings();
    33.         opaqueRange.renderQueueRange = new RenderQueueRange()
    34.         {
    35.             min = 0,
    36.             max = (int)RenderQueue.GeometryLast
    37.         };
    38.         opaqueRange.layerMask = ~0;
    39.  
    40.         cmd.Release();
    41.         context.Submit();
    42.     }
    43. }
    And I already implement a RenderPipelineAsset, such as :
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Experimental.Rendering;
    3.  
    4. [ExecuteInEditMode]
    5. public class BasicAssetPipe : RenderPipelineAsset
    6. {
    7.     public Color ClearColor;
    8.  
    9. #if UNITY_EDITOR
    10.     [UnityEditor.MenuItem("SRP-Demo/01 - Create Basic Asset Pipeline")]
    11.     static void CreateBasicAssetPipeline()
    12.     {
    13.         var instance = ScriptableObject.CreateInstance<BasicAssetPipe>();
    14.         UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/BasicAssetPipe.asset");
    15.     }
    16. #endif
    17.  
    18.     protected override IRenderPipeline InternalCreatePipeline()
    19.     {
    20.         return new BasicPipeInstance(ClearColor);
    21.     }
    22. }
    Then I use the menu "SRP-Demo/01 - Create Basic Asset Pipeline" to create a "BasicAssetPipe.asset" and assign it to the graphics settings.

    Here, It works well. And I can change the screen background by changing the ClearColor in "BasicAssetPipe.asset".

    Then I continue add some code to the BasicPipeInstance.cs
    Now it looks like :
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Experimental.Rendering;
    3. using UnityEngine.Rendering;
    4.  
    5. public class BasicPipeInstance : RenderPipeline
    6. {
    7.     private Color _ClearColor;
    8.  
    9.     public BasicPipeInstance(Color clearColor) {
    10.         _ClearColor = clearColor;
    11.     }
    12.  
    13.     public override void Render(ScriptableRenderContext context, Camera[] cameras)
    14.     {
    15.         base.Render(context, cameras);
    16.  
    17.         var cmd = new CommandBuffer();
    18.  
    19.         cmd.ClearRenderTarget(true, true, _ClearColor);
    20.         context.ExecuteCommandBuffer(cmd);
    21.  
    22.         // I only have one camera in my scene.
    23.         var camera = cameras[0];
    24.  
    25.         ScriptableCullingParameters cullingParameters;
    26.         if (!CullResults.GetCullingParameters(camera, out cullingParameters))
    27.         {
    28.             return;
    29.         }
    30.         CullResults cullResults = CullResults.Cull(ref cullingParameters, context);
    31.  
    32.         var opaqueRange = new FilterRenderersSettings();
    33.         opaqueRange.renderQueueRange = new RenderQueueRange()
    34.         {
    35.             min = 0,
    36.             max = (int)RenderQueue.GeometryLast
    37.         };
    38.         opaqueRange.layerMask = ~0;
    39.  
    40.         // ----  new problem code below
    41.         var drs = new DrawRendererSettings(Camera.current, new ShaderPassName("Qpaque"));
    42.         // enable instancing for the draw call
    43.         drs.flags = DrawRendererFlags.EnableInstancing;
    44.  
    45.         // pass light probe and lightmap data to each renderer
    46.         drs.rendererConfiguration = RendererConfiguration.PerObjectLightProbe | RendererConfiguration.PerObjectLightmaps;
    47.  
    48.         // sort the objects like normal opaque objects
    49.         drs.sorting.flags = SortFlags.CommonOpaque;
    50.         // ----  new problem code above
    51.  
    52.         cmd.Release();
    53.         context.Submit();
    54.     }
    55. }
    I have commented what I changed.

    And then the editor will crash.
    Once it crash no matter how many times you restart the Unity it always crash even if you remove the problem code which we add later.

    Now the only way to open the project is :
    remove the problem code > close unity > remove the `Library` folder which under your project into `recycle bin`> open unity
     
    Last edited: Nov 8, 2018
  2. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    Instead of Camera.current, you need to for-loop the list of cameras received in the function.
     
  3. kakashi8841

    kakashi8841

    Joined:
    Apr 27, 2018
    Posts:
    10
    Yeah, I agree with you. The code in this thread is what I get from the SRP wiki.
    I have indicated the problem on the other thread :
    https://forum.unity.com/threads/error-code-on-wiki-of-srp.580669/

    And I post this thread to suggest DrawRendererSettings constructor can be more fault-tolerant. If a null camera is given in the Editor Mode, throw a exception and prevent the crash.