How to tell if SRP is active in script or shaders

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?

2 Likes

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

namespace MadGoat.Core.Utils {
    public static class RenderPipelineUtils {

        public enum PipelineType {
            Unsupported,
            BuiltInPipeline,
            UniversalPipeline,
            HDPipeline
        }

        /// <summary>
        /// Returns the type of renderpipeline that is currently running
        /// </summary>
        /// <returns></returns>
        public static PipelineType DetectPipeline() {
#if UNITY_2019_1_OR_NEWER
            if (GraphicsSettings.renderPipelineAsset != null) {
                // SRP
                var srpType = GraphicsSettings.renderPipelineAsset.GetType().ToString();
                if (srpType.Contains("HDRenderPipelineAsset")) {
                    return PipelineType.HDPipeline;
                }
                else if (srpType.Contains("UniversalRenderPipelineAsset") || srpType.Contains("LightweightRenderPipelineAsset")) {
                    return PipelineType.UniversalPipeline;
                }
                else return PipelineType.Unsupported;
            }
#elif UNITY_2017_1_OR_NEWER
            if (GraphicsSettings.renderPipelineAsset != null) {
                // SRP not supported before 2019
                return PipelineType.Unsupported;
            }
#endif
            // no SRP
            return PipelineType.BuiltInPipeline;
        }
    }
}

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.

4 Likes