找回密码
 立即注册
查看: 321|回复: 1

如何理解unity内置的UNITY_LOOP、UNITY_UNROLL等宏?

[复制链接]
发表于 2023-4-1 17:45 | 显示全部楼层 |阅读模式
在CGIncludes/HLSLSupport.cginc中,有这么一段:
// HLSL attributes
#if defined(UNITY_COMPILER_HLSL)
    #define UNITY_BRANCH    [branch]
    #define UNITY_FLATTEN   [flatten]
    #define UNITY_UNROLL    [unroll]
    #define UNITY_LOOP      [loop]
    #define UNITY_FASTOPT   [fastopt]
   ...

那么这5个宏分别是什么作用呢?
发表于 2023-4-1 17:46 | 显示全部楼层
在unity包中的CGIncludes\HLSLSupport.cginc中可以找到定义:
#if defined(UNITY_COMPILER_HLSL)
    #define UNITY_BRANCH    [branch]
    #define UNITY_FLATTEN   [flatten]
    #define UNITY_UNROLL    [unroll]
    #define UNITY_LOOP      [loop]
    #define UNITY_FASTOPT   [fastopt]
#else
    #define UNITY_BRANCH
    #define UNITY_FLATTEN
    #define UNITY_UNROLL
    #define UNITY_LOOP
    #define UNITY_FASTOPT
#endif
官方DX有具体文档的:
https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-while
UNITY_FLATTEN 执行分支的所有条件,并且在执行后选择结果。
UNITY_BRANCH 为在进行if条件语句时使用动态分支进行处理, 太多的话有时候会报错。

UNITY_LOOP 表示shader在编译时不进行循环展开检查。
UNITY_UNROLL(x) 表示编译时尝试展开循环,x为尝试展开循环的最大次数。
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-14 23:47 , Processed in 0.098479 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表