Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

How to tell if SRP is active in script or shaders

Discussion in 'High Definition Render Pipeline' started by jbooth, Nov 13, 2019.

  1. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    So, when you install a shader designed for an SRP you get compile errors if the SRP is not active. I would love to have a way to do conditional compilation in a shader based on the SRP, and I believe Unity may have added this at some point but I can't seem to find any info on it. I'd also like to know which SRP is active in script so I can generate new shaders with that SRP's code. Any ideas?
     
    nfynt-zap and Raul_T like this.
  2. Raul_T

    Raul_T

    Joined:
    Jan 10, 2015
    Posts:
    363
    Also interested in how this would be handled at shader level.

    Regarding detection in scripts, for our SSAA asset we currently use this for SRP detection:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5.  
    6. namespace MadGoat.Core.Utils {
    7.     public static class RenderPipelineUtils {
    8.  
    9.         public enum PipelineType {
    10.             Unsupported,
    11.             BuiltInPipeline,
    12.             UniversalPipeline,
    13.             HDPipeline
    14.         }
    15.  
    16.         /// <summary>
    17.         /// Returns the type of renderpipeline that is currently running
    18.         /// </summary>
    19.         /// <returns></returns>
    20.         public static PipelineType DetectPipeline() {
    21. #if UNITY_2019_1_OR_NEWER
    22.             if (GraphicsSettings.renderPipelineAsset != null) {
    23.                 // SRP
    24.                 var srpType = GraphicsSettings.renderPipelineAsset.GetType().ToString();
    25.                 if (srpType.Contains("HDRenderPipelineAsset")) {
    26.                     return PipelineType.HDPipeline;
    27.                 }
    28.                 else if (srpType.Contains("UniversalRenderPipelineAsset") || srpType.Contains("LightweightRenderPipelineAsset")) {
    29.                     return PipelineType.UniversalPipeline;
    30.                 }
    31.                 else return PipelineType.Unsupported;
    32.             }
    33. #elif UNITY_2017_1_OR_NEWER
    34.             if (GraphicsSettings.renderPipelineAsset != null) {
    35.                 // SRP not supported before 2019
    36.                 return PipelineType.Unsupported;
    37.             }
    38. #endif
    39.             // no SRP
    40.             return PipelineType.BuiltInPipeline;
    41.         }
    42.     }
    43. }
    It's definitely not perfect and we only account for SRPS under unity 2019 (we feel like using older pipelines is pointless at this moment.)

    Also in our implementation LWRP and URP are merged into a single pipeline since it doesn't affect our shaders, but its easy to modify if they are needed separately (we have the same shaders working on both since there were no major changes between LWRP and URP for what we needed.)

    I went with a different approach regarding the shaders and the compilation errors - based on the detected pipeline our script actually renames the shader files to *.shader if the pipeline is correct or to some other extension like .hd/.lw if the shader shouldn't be used with the current pipeline. Really not ideal but this is handled automatically by script and is better than having the end user to import separate shader packages depending on the pipeline.