Unity Shader: multi_compile一个文件变幻多个shader

multi_compile

我们在写shader时,经常会出现同一个shader在面对不同的一些需求时,可能需要出现一些局部的变化,比如有些地方需要描边,有些地方不需要描边,由于在shader中不适合使用if,所以最好就是再写一份shader,但我们肯定不想复制一个shader改改,毕竟有很多共用的部分,万一修改了,岂不是两边都要改,而且shader多了也不方便使用。这个时候就需要用到shader变体了,只需要写一个shader,在编译的时候会自动生成多个对应shader使用,这样子就完美的解决了我们的问题。

拥有多种变体的shader一般称为"mega shaders"或者"uber shaders"
在unity里我们可以通过在Shader中(CG代码块或者HLSL代码块中)添加下面的指令来实现
#pragma multi_compile
或者
#pragma shader_feature

在运行时Unity会根据材质中的关键词(通过Material.EnableKeyword和Material.DisableKeyword开关)或者全局的shader关键词(通过Shader.EnableKeyword和Shader.DisableKeyword开关)来选择对应的Shader变体。

如何使用multi_compile

如下


SubShader {
    
    
        Pass {
    
    
              
            HLSLPROGRAM  

            #pragma multi_compile PCF3 PCF5
                        
            ENDHLSL
        }
    }

这里会生成两个shader变体,一个是定义了PCF3的,另外一个定义了PCF5,在运行时,根据材质或者全局的关键词,他们其中一个会被调用到,如果两个关键词都没有开启,则第一个PCF3的版本的shader变体会被使用。

当然我们可以在一个multi_compile中定义更多的关键词,如下会产生4个shader变体


SubShader {
    
    
        Pass {
    
    
              
            HLSLPROGRAM  

            #pragma multi_compile PCF PCF3 PCF5 PCF7
                        
            ENDHLSL
        }
    }

如果定义的名称都由下划线组成的话,这个名称也会产生一个shader变体,作为类似默认的shader变体,这样子我们可以少定义一个关键词(关键词总数会有限制,下面会讲),比如上面的shader我们可以改为如下,这样子同样会生成4个变体,但是只需要开关3个关键词了


SubShader {
    
    
        Pass {
    
    
              
            HLSLPROGRAM  

            #pragma multi_compile __ PCF3 PCF5 PCF7
                        
            ENDHLSL
        }
    }

C#中使用如下所示(材质球中设置同理)


    public enum FilterMode
    {
    
    
        PCF2x2, PCF3x3, PCF5x5, PCF7x7
    }


    // 定义Shader中FilterMode的关键词
    static string[] directionalFilterKeywords = {
    
    
        "PCF3",
        "PCF5",
        "PCF7",
    };


    // 设置Shader中的Keywords,目前主要设置使用的过滤模式(filter mode)
    void SetKeywords()
    {
    
    
        // 我们定义shader关键词的时候,不需要定义默认的PCF2x2,默认的只要是没有定义就可以
        int enabledIndex = (int)settings.directional.filter - 1;
        for (int i = 0; i < directionalFilterKeywords.Length; i++)
        {
    
    
            if (i == enabledIndex)
            {
    
    
                buffer.EnableShaderKeyword(directionalFilterKeywords[i]);
            }
            else
            {
    
    
                buffer.DisableShaderKeyword(directionalFilterKeywords[i]);
            }
        }
    }


在这里插入图片描述

如何使用shader_feature

如下所示,这样子会有两个变体,一个是没有开启,另外一个是开启了预乘alpha


Properties {
    
    
        [Toggle(_PREMULTIPLY_ALPHA)] _PremulAlpha("Premultiply Alpha", Float) = 0  //控制预乘Alpha
     }   


SubShader {
    
    
        Pass {
    
    
              
            HLSLPROGRAM  

            #pragma shader_feature _PREMULTIPLY_ALPHA  //控制预乘Alpha
                        
            ENDHLSL
        }
    }

在这里插入图片描述

multi_compile和shader_feature的区别

multi_compile和shader_feature功能很像,最大的区别是对于shader_feature,没有使用的变体不会在构建时包含进去,所以对于shader_feature最好是直接在材质上设置好关键词(选项选好,开关开好),不然可能在运行时设置可能无效,因为对应的shader变体可能没有包含进来,multi_compile则没有这个问题,全局设置也可以。


Properties {
    
    
        [Toggle(_PREMULTIPLY_ALPHA)] _PremulAlpha("Premultiply Alpha", Float) = 0  //控制预乘Alpha
     }   


SubShader {
    
    
        Pass {
    
    
              
            HLSLPROGRAM  

            #pragma shader_feature _PREMULTIPLY_ALPHA  //控制预乘Alpha
            #pragma multi_compile _ _DIRECTIONAL_PCF3 _DIRECTIONAL_PCF5 _DIRECTIONAL_PCF7
                        
            ENDHLSL
        }
    }

#pragma shader_feature _PREMULTIPLY_ALPHA实际上是一种简化写法,和#pragma shader_feature _ _PREMULTIPLY_ALPHA是一样的,都是对应两种变体,一种定义了_PREMULTIPLY_ALPHA,另外一种没有定义

多个multi_compile指令

在一个CG或HLSL代码块中可以有多个multi_compile指令,如下
#pragma multi_compile A B C
#pragma multi_compile D E

这样子总共会产生6个变体,AD,AE,BD,BE,CD,CE,就是所有的排列组合,因此要注意不能使用太多的multi_compile不然会产生大量的shader变体,因为这是指数级增长的。

关键词的限制

unity中总共能用256个关键词,但是unity内部会大概用掉60个关键词,因此能用的数量就不超过196个,或者更少,所以要注意不要超过这个限制了。

内置的multi_compile快捷指令

对于灯光、阴影和光照贴图,unity有多种的内置multi_compile指令可以使用来快速生成对应的shader变体

multi_compile_fwdbase
编译ForwardBase pass类型的所有需要的变体,这些变体处理不同的光照贴图和主要的平行光的阴影开关

multi_compile_fwdadd
编译ForwardAdd pass类型的所有需要的变体,这些变体处理平行光、聚光灯、点光源类型和相应的贴图

multi_compile_fwdadd_fullshadows
和上面的multi_compile_fwdadd类似,但是还增加了灯光的实时阴影相关的处理

multi_compile_fog
编译不同类型的雾(off/linear/exp/exp2) 所需要的shader变体

跳过指定的关键词

要注意的是,大部分的内置multi_compile会导致产生大量的shader变体,如果有不需要的变体,我们可以使用下面的指令来跳过其中的一些shader变体
#pragma multi_compile_fwdadd
#prama skip_variants POINT POINT_COOKIE

这样子会跳过其中的POINT 或 POINT_COOKIE的变体,不会生成带这两种定义的变体,会减少一些变体的量

还有一些没搞懂的

Shader硬件变体?

Shader Hardware Variants
One common reason for using shader variants is to create fallbacks or simplified shaders that can run efficiently on both high and low end hardware within a single target platform - such as OpenGL ES. To provide a specially optimised set of variants for different levels of hardware capability, you can use shader hardware variants.
To enable the generation of shader hardware variants, add #pragma hardware_tier_variants renderer, where renderer is one of the available renderering platforms for shader program pragmas. With this #pragma 3 shader variants will be generated for each shader, regardless of any other keywords. Each variant will have one of the following defined:
UNITY_HARDWARE_TIER1
UNITY_HARDWARE_TIER2
UNITY_HARDWARE_TIER3
You can use these to write conditional fallbacks or extra features for lower or higher end. In the editor you can test any of the tiers by using the Graphics Emulation menu, which allows you to change between each of the tiers.
To help keep the impact of these variants as small as possible, only one set of shaders is ever loaded in the player. In addition, any shaders which end up identical - for example if you only write a specialised version for TIER1 and all others are the same - will not take up any extra space on disk.
At load time Unity will examine the GPU that it is using and auto-detect a tier value; it will default to the highest tier if the GPU is not auto-detected. You can override this tier value by setting Shader.globalShaderHardwareTier, but this must be done before any shaders you want to vary are loaded. Once the shaders are loaded they will have selected their set of variants and this value will have no effect. A good place to set this would be in a pre-load scene before you load your main scene.
Note that these shader hardware tiers are not related to the Quality settings of the player, they are purely detected from the relative capability of the GPU the player is running on.

来自 https://docs.unity3d.com/2018.4/Documentation/Manual/SL-MultipleProgramVariants.html

平台Shader设置

Platform Shader Settings
Apart from tweaking your shader code for different hardware tiers, you might want to tweak unity internal defines (e.g. you might want to force cascaded shadowmaps on mobiles). You can find details on this in the UnityEditor.Rendering.PlatformShaderSettings documentation, which provides a list of currently supported features for overriding per-tier. Use UnityEditor.Rendering.EditorGraphicsSettings.SetShaderSettingsForPlatform to tweak Platform Shader Settings per-platform per-tier.
Please note that if PlatformShaderSettings set to different tiers are not identical, then tier variants will be generated for the shader even if #pragma hardware_tier_variants is missing.

来自 https://docs.unity3d.com/2018.4/Documentation/Manual/SL-MultipleProgramVariants.html

参考

https://docs.unity3d.com/2018.4/Documentation/Manual/SL-MultipleProgramVariants.html

猜你喜欢

转载自blog.csdn.net/ithot/article/details/127023362