Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Not using vertex position for calculating clip space position breaks Dynamic batching

Discussion in 'Shaders' started by Kekec, Mar 17, 2019.

  1. Kekec

    Kekec

    Joined:
    Jul 12, 2015
    Posts:
    23
    Hi,

    Not sure if I should post this thread under General graphics since it is connected with dynamic batching.

    Anyway the problem I'm having is that dynamic batching does not work on shaders that do not use vertex position (object space position) for calculating the clip space position. Here is the most basic example shader:

    Code (CSharp):
    1. Shader "DynamicBatchingTest"
    2. {
    3.   SubShader
    4.   {
    5.     Tags { "RenderType"="Opaque" }
    6.  
    7.     Pass
    8.     {
    9.       CGPROGRAM
    10.  
    11.       #pragma vertex vertex_shader
    12.       #pragma fragment fragment_shader
    13.  
    14.       struct VS_Input //vertex shader input
    15.       {
    16.         float4 pos   : POSITION;
    17.         float2 uv    : TEXCOORD0;
    18.       };
    19.  
    20.       struct VS_Output //vertex shader output
    21.       {
    22.         float4 pos  : SV_POSITION;
    23.       };
    24.  
    25.       VS_Output vertex_shader(VS_Input input)
    26.       {
    27.         VS_Output output;
    28.  
    29.         output.pos = UnityObjectToClipPos(input.pos); //OK
    30.         //output.pos = UnityObjectToClipPos(float4(input.uv,0,1)); //NOT OK
    31.  
    32.         return output;
    33.       }
    34.  
    35.       fixed4 fragment_shader(VS_Output input) : COLOR
    36.       {
    37.         return float4(1,0,0,1);
    38.       }
    39.  
    40.       ENDCG
    41.     }
    42.   }
    43. }
    I created a simple scene using 2 quads with same material and this are the results (please ignore the fact that using uv as position does not result in exact same output result ... just focus on the fact that it is not dynamically batched).

    Code (CSharp):
    1. output.pos = UnityObjectToClipPos(input.pos); //BATCHED
    DynamicBatchingTest_batched.PNG

    Code (CSharp):
    1. output.pos = UnityObjectToClipPos(float4(input.uv,0,1)); //NOT BATCHED
    DynamicBatchingTest_not_batched.PNG


    Can someone explain to me why this breaks dynamic batching?
     
unityunity