Batching-optimized GPU instantiation in Unity (3)


Preface

In the previous article, we mainly analyzed the definition of GPU instantiation in Unity and what the instantiation ID step does.

In this article, we use the defined instance ID to make the rendered object coordinates contained in the synthesized model of a batch display correctly.


一、UNITY_SETUP_INSTANCE_ID(v);

UNITY_SETUP_INSTANCE_ID(v); Place it at the beginning of the vertex shader/fragment shader (optional) so that the global variable unity_InstanceID can be accessed.

  • Before modification (although the GPU instantiation batch was successful, the location of the object was wrong):

Please add image description

  • After modification: (GPU instantiation batch is successful and the position is correct)
    Please add image description

2. In the UnityInstancing.cginc file, take a look at what Unity does.

  • UNITY_SETUP_INSTANCE_ID(v);

1. After using .cginc, the function will be automatically predefined

Insert image description here

2. The GPU instantiation conditions need to be met before the corresponding statement will be executed.

  • This sentence must be used at the beginning of the vertex shader and fragment shader so that subsequent steps can correctly access the global variable unity_InstanceID.

Insert image description here

  • UNITY_INSTANCING_ENABLED: When GPU instantiation can be turned on

Insert image description here

  • UNITY_SUPPORT_INSTANCING: GPU instantiation hardware support

Insert image description here

3. After the GPU is instantiated, the following functions are mainly executed:

Insert image description here

  • UNITY_GET_INSTANCE_ID(input): Get the batch ID function of the point before batching

Insert image description here

  • UnitySetupInstanceID(UNITY_GET_INSTANCE_ID(input)): Convert to get the batch ID of the point after the batch is combined.

Insert image description here

  • UnitySetupCompoundMatrices();: Unity performs corresponding matrix transformation according to the global variable unity_InstanceID to make the vertex position correct

In this article, we solve the problem of incorrect object position after GPU instantiation. In the next article, we will implement how to make the material color of each object different.

Guess you like

Origin blog.csdn.net/qq_51603875/article/details/134859121