其他分享
首页 > 其他分享> > UnityInstancing头文件分析

UnityInstancing头文件分析

作者:互联网

Unity Instancing.hlsl

A.平台适配

UNITY_SUPPORT_INSTANCING

UNITY_SUPPORT_STEREO_INSTANCING

UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE

undef UNITY_SUPPORT_INSTANCING

defined(SHADER_TARGET_SURFACE_ANALYSIS) && defined(UNITY_SUPPORT_INSTANCING)

B.instancing paths (根据绘制指令选择不同的Instance路径)

    #if defined(UNITY_SUPPORT_INSTANCING) && defined(INSTANCING_ON)
        #define UNITY_INSTANCING_ENABLED
    #endif
    #if defined(UNITY_SUPPORT_INSTANCING) && defined(PROCEDURAL_INSTANCING_ON)
        #define UNITY_PROCEDURAL_INSTANCING_ENABLED
    #endif
    #if defined(UNITY_SUPPORT_INSTANCING) && defined(DOTS_INSTANCING_ON)
        #define UNITY_DOTS_INSTANCING_ENABLED
    #endif
    #if defined(UNITY_SUPPORT_STEREO_INSTANCING) && defined(STEREO_INSTANCING_ON)
        #define UNITY_STEREO_INSTANCING_ENABLED
    #endif

    #if defined(UNITY_INSTANCING_ENABLED) || defined(UNITY_PROCEDURAL_INSTANCING_ENABLED) || defined(UNITY_DOTS_INSTANCING_ENABLED) || defined(UNITY_STEREO_INSTANCING_ENABLED)
        #define UNITY_ANY_INSTANCING_ENABLED 1
    #else
        #define UNITY_ANY_INSTANCING_ENABLED 0
    #endif

C.Instance的CBUFFER_SCOPE Begin/End

D.基本的instancing宏定义函数设置

    #if UNITY_ANY_INSTANCING_ENABLED

        // A global instance ID variable that functions can directly access.
        static uint unity_InstanceID;

        // Don't make UnityDrawCallInfo an actual CB on GL
        #if !defined(SHADER_API_GLES3) && !defined(SHADER_API_GLCORE)
            UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(UnityDrawCallInfo)
        #endif
                int unity_BaseInstanceID;
                int unity_InstanceCount;
        #if !defined(SHADER_API_GLES3) && !defined(SHADER_API_GLCORE)
            UNITY_INSTANCING_CBUFFER_SCOPE_END
        #endif

        #ifdef SHADER_API_PSSL
            #define DEFAULT_UNITY_VERTEX_INPUT_INSTANCE_ID uint instanceID;
            #define UNITY_GET_INSTANCE_ID(input)    _GETINSTANCEID(input)
        #else
            #define DEFAULT_UNITY_VERTEX_INPUT_INSTANCE_ID uint instanceID : SV_InstanceID;
            #define UNITY_GET_INSTANCE_ID(input)    input.instanceID
        #endif

    #else
        #define DEFAULT_UNITY_VERTEX_INPUT_INSTANCE_ID
    #endif // UNITY_INSTANCING_ENABLED || UNITY_PROCEDURAL_INSTANCING_ENABLED || UNITY_STEREO_INSTANCING_ENABLED

    #if !defined(UNITY_VERTEX_INPUT_INSTANCE_0ID)
    #   define UNITY_VERTEX_INPUT_INSTANCE_ID DEFAULT_UNITY_VERTEX_INPUT_INSTANCE_ID
    #endif

E.basic stereo instancing setups 跳过

F.UNITY_SETUP_INSTANCE_ID和UNITY_TRANSFER_INSTANCE_ID

////////////////////////////////////////////////////////
// - UNITY_SETUP_INSTANCE_ID        Should be used at the very beginning of the vertex shader / fragment shader,
//                                  so that succeeding code can have access to the global unity_InstanceID.
//                                  Also procedural function is called to setup instance data.
// - UNITY_TRANSFER_INSTANCE_ID     Copy instance ID from input struct to output struct. Used in vertex shader.

G.instanced property arrays

H.One more thing

UNITY_INSTANCING_BUFFER_START(buf)
     UNITY_DEFINE_INSTANCED_PROP(type, var)
     UNITY_DEFINE_INSTANCED_PROP(type, var)
UNITY_INSTANCING_BUFFER_END(arr)

UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(UnityInstancing_##buf) struct{
    type var;
    type var;
} arr##Array[UNITY_INSTANCED_ARRAY_SIZE]; UNITY_INSTANCING_CBUFFER_SCOPE_END

[非GL平台]
CBUFFER_START(UnityInstancing_##buf) 

struct{
    type var;
    type var;
} arr##Array[UNITY_INSTANCED_ARRAY_SIZE]; 

CBUFFER_END

例子:
UNITY_INSTANCING_BUFFER_START(Props)
    UNITY_DEFINE_INSTANCED_PROP(float4, _TestColor)
UNITY_INSTANCING_BUFFER_END(Props)

name of arr=buf=Props

[非GL平台]
CBUFFER_START(UnityInstancing_Props) 

struct{
    float4 _TestColor;
} PropsArray[UNITY_INSTANCED_ARRAY_SIZE];

CBUFFER_END
UNITY_ACCESS_INSTANCED_PROP(arr, var)  =>arrArray[unity_InstanceID].var
例子:
UNITY_ACCESS_INSTANCED_PROP(Props, _TestColor) =>PropsArray[unity_InstanceID]._TestColor
- UNITY_SETUP_INSTANCE_ID(input)
    => DEFAULT_UNITY_SETUP_INSTANCE_ID 
        =>1.Procedural 
        UnitySetupInstanceID(UNITY_GET_INSTANCE_ID(input)); 
        UNITY_INSTANCING_PROCEDURAL_FUNC();
        =>2.非Procedural 
        UnitySetupInstanceID(UNITY_GET_INSTANCE_ID(input));
        [非PS平台] UnitySetupInstanceID(input.instanceID); 
        unity_InstanceID = input.instanceID + unity_BaseInstanceID;

- UNITY_TRANSFER_INSTANCE_ID(input, output) 
    =>output.instanceID = UNITY_GET_INSTANCE_ID(input)
    =>output.instanceID = input.instanceID

标签:分析,头文件,INSTANCED,UnityInstancing,unity,INSTANCE,UNITY,INSTANCING,define
来源: https://www.cnblogs.com/OneStargazer/p/15170931.html