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

news/2024/5/19 9:47:02/文章来源:https://blog.csdn.net/ithot/article/details/127023362

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 PCF5ENDHLSL}}

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

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


SubShader {Pass {HLSLPROGRAM  #pragma multi_compile PCF PCF3 PCF5 PCF7ENDHLSL}}

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


SubShader {Pass {HLSLPROGRAM  #pragma multi_compile __ PCF3 PCF5 PCF7ENDHLSL}}

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  //控制预乘AlphaENDHLSL}}

在这里插入图片描述

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_PCF7ENDHLSL}}

#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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_13100.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

sklearn包使用Extra-Trees和GridSearchCV完成成人死亡率预测

成年人死亡率指的是每一千人中 15 岁至 60 岁死亡的概率&#xff08;数学期望&#xff09;。这里我们给出了世界卫生组织&#xff08;WHO&#xff09;下属的全球卫生观察站&#xff08;GHO&#xff09;数据存储库跟踪的所有国家健康状况以及许多其他相关因素。要求利用训练数据…

Fiddler使用

最近老是使用fiddler工具&#xff0c;用了忘所以特此来记录。先说说fiddler吧&#xff0c;这是一款功能强大的抓包工具&#xff0c;平时可以拿来抓抓小程序的包&#xff0c;直接通关羊了个羊&#xff0c;安装就不说了&#xff0c;都是无脑下一步&#xff0c;提供下地址&#xf…

阿里巴巴面试题- - -多线程并发篇(三十八)

前言:七月末八月初的时候,秋招正式打响,公司会放出大量的全职和实习岗位。为了帮助秋招的小伙伴们,学长这里整理了一系列的秋招面试题给大家,所以小伙伴们不用太过焦虑,相信你们一定能超常发挥,收到心仪公司的Offer~~ 内容涵盖:Java、MyBatis、ZooKeeper、Dubbo、Elast…

商汤AI象棋机器人到底谁在买?北大象棋大师已签收

金磊 衡宇 发自 凹非寺量子位 | 公众号 QbitAI郭晶晶家的象棋家教——没错&#xff0c;就是商汤AI象棋机器人“元萝卜”&#xff08;SenseRobot&#xff09;&#xff0c;近日正式现货发售。从近2个月前开始预售的那刻起&#xff0c;各种讨论就萦绕在它周围&#xff1a;AI象棋机…

非零基础自学Java (老师:韩顺平) 第21章 网络编程 21.3 Socket 21.4 TCP网络通信编程

非零基础自学Java (老师&#xff1a;韩顺平) ✈【【零基础 快速学Java】韩顺平 零基础30天学会Java】 第21章 网络编程 文章目录非零基础自学Java (老师&#xff1a;韩顺平)第21章 网络编程21.3 Socket21.3.1 基本介绍21.4 TCP网络通信编程21.4.1 基本介绍21.4.2 应用案例1 -…

网络法规——知识产权及侵权鉴别学习笔记

一、知识产权概念 《中华人民共和国民法通则》规定&#xff0c;知识产权是指民事权利主体&#xff08;公民、法人&#xff09;基于创造性的智力成果。 1、知识产权分类 知识产权可分为工业产权和著作权。 工业产权 工业产权包括专利、实用新型、工业品外观设计、商标、服务…

随机生成植物生长及舞动算法

几年前写过一套随机树木的生成算法&#xff0c;其中使用了分形和放样建模。那时候还不知道有speedtree这款软件&#xff0c;写的比较粗糙&#xff0c;最近看了speedtree的演示把原算法改进了一下&#xff0c;算是一个speedtree的简化版本。 重构主要是把原先使用递归函…

使用Consul做注册中心简示

基地版同一个地点 不同的风景

JAMA子刊:孕妈妈每天喝半杯咖啡,可能让胎儿发育迟缓

爱心提示本文不仅适用于喜欢咖啡的孕妇&#xff0c;也适用于喜欢茶&#xff08;包括奶茶&#xff09;、巧克力、能量饮料、可乐和其他含有咖啡因的食物或饮料的孕妇。核心要点题目及新发现的相关背景&#xff1b;已知各权威机构对孕期咖啡因摄入量的建议&#xff1b;业主的个人…

Java数据结构之二叉树的基本操作

二叉树的基本操作1 二叉树的基本概念2 二叉树的遍历3 代码实现二叉树的遍历4 代码实现前序、中序、后序查找5 代码实现二叉树指定节点的删除1 二叉树的基本概念 &#xff08;1&#xff09;树有很多种&#xff0c;每个节点最多只能有两个子节点的树就是二叉树。 &#xff08;2&…

[CISCN 2019 初赛]Love Math

<?php error_reporting(0); //听说你很喜欢数学&#xff0c;不知道你是否爱它胜过爱flag if(!isset($_GET[c])){show_source(__FILE__); }else{//例子 c20-1$content $_GET[c];if (strlen($content) > 80) {die("太长了不会算");}$blacklist [ , \t, \r, \n…

【C++11新特性】类的新功能,可变模板参数,包装器

文章目录一、类的新功能1.default2.delete二、可变参数模板1.参数包2.参数包的插入与解析(1)参数包的个数(2)添加参数解析(3)逗号表达式展开(4)emplace_back三、包装器1.function(封装)2.bind(绑定)一、类的新功能 1.default 在继承与多态中&#xff0c;我们介绍了final与ove…

Feign的简单介绍及配置参数

contextId用于区分实例,类似beanName

mysql存储过程的写法

示例表 area_code_2022 &#xff1a; DROP TABLE IF EXISTS area_code_2022; CREATE TABLE area_code_2022 ( code bigint(12) unsigned NOT NULL COMMENT 区划代码, name varchar(128) NOT NULL DEFAULT COMMENT 名称, level tinyint(1) NOT NULL COMMENT 级别1-5,省市…

python识别选中文本

目标&#xff1a;识别鼠标选中区域的文本 be like : 这是我在模拟键鼠操作时遇到的情况&#xff0c;我需要根据某个位置返回的值进行判断&#xff0c;但是只是依赖pyautogui是做不到的。 方法一 经过上网冲浪寻找答案&#xff0c;被告知了此方法&#xff0c;经测试可行 impor…

Django项目想要在 Python Console里面进行操作 报错找不到对应模块

Django项目想要在 Python Console里面进行操作 报错找不到对应模块 问题描述 ModuleNotFoundError: No module named django ’ 问题原因 在进行对 Python console操作 进行管理查询要导入对应的模块&#xff0c;但是和项目中的models.py文件中的 导包引入 冲突了 导致在Py…

可持久化Trie

可持久化指的是可以记录所有的历史版本&#xff0c;即记录下来每一步操作后的状态 下图模拟过程 题目&#xff1a; 最大异或和 给定一个非负整数序列 a&#xff0c;初始长度为 N。 有 M 个操作&#xff0c;有以下两种操作类型&#xff1a; A x&#xff1a;添加操作&#xff0…

uniapp自用速查表 - 我的常用组件

纯私人class&#xff0c;公开文章是方便置顶.... <!-- 自定义导航栏 占位空间 --> <view class"navigationStyleCustomHeight alwaysOnTopBox0 bgColorForTheme"></view> <!-- 自定义导航栏 --> <view class"alwaysOnTopBox1 myNav…

java正则表达式简单使用

String email = "13072558368"; email = email.replaceAll("(\\d{3})\\d{6}(\\d{2})", "$1****$2"); System.out.println("email=" + email); email=130****68从第三个开始,计算6个数字,其中计算的6个数字用*替换String mobile = &q…

java public、protected、default、private

java 的访问控制符为了控制类还有类对应方法的访问做限制。如上的图表总结了类方法的访问控制范围,其实类的访问控制范围也是类似的情况。声明为public则不管外部包还是内部都能够访问,如果为default则只能本包内能够访问 关于类方法的访问范围,我们比较熟悉的是private还有…