Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Dynamic Texture Lookups - Sanity check

Discussion in 'Shaders' started by naked_chicken, Apr 12, 2017.

  1. naked_chicken

    naked_chicken

    Joined:
    Sep 10, 2012
    Posts:
    186
    So reading through this article, I wanted to double check on the following blurb:

    Note: It may not seem obvious, but any calculation on the texture coordinates counts as a dependent texture read. For example, packing multiple sets of texture coordinates into a single varying parameter and using a swizzle command to extract the coordinates still causes a dependent texture read.

    So that means that instead of having a single float4 texcoord like this:

    Code (CSharp):
    1. struct v2f {
    2.          float4 vertex : SV_POSITION;
    3.          float4 texcoord : TEXCOORD0;
    4.        };
    5.        
    6.        v2f vert (appdata_t v) {
    7.          v2f o;
    8.          UNITY_INITIALIZE_OUTPUT(v2f,o);
    9.          V_CW_TransformPoint(v.vertex);
    10.          o.vertex = UnityObjectToClipPos(vert);
    11.  
    12.          o.texcoord.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    13.          o.texcoord.zw = TRANSFORM_TEX(v.texcoord, _Tex2);
    14.  
    15.          return o;
    16.        }
    17.  
    18.        fixed4 frag (v2f i) : SV_Target {
    19.          fixed4 mainTex = tex2D(_MainTex, i.texcoord.xy);
    20.          fixed4 tex2 = tex2D(_Tex2, i.texcoord.zw);
    21.  
    22.          return mainTex * tex2;
    23.        }
    I should break out the float4 texcoord into 2 separate values like this:

    Code (CSharp):
    1. struct v2f {
    2.          float4 vertex : SV_POSITION;
    3.          float2 texcoord : TEXCOORD0;
    4.          float2 texcoord2 : TEXCOORD1;
    5.        };
    6.        
    7.        v2f vert (appdata_t v) {
    8.          v2f o;
    9.          UNITY_INITIALIZE_OUTPUT(v2f,o);
    10.          V_CW_TransformPoint(v.vertex);
    11.          o.vertex = UnityObjectToClipPos(vert);
    12.  
    13.          o.texcoord= TRANSFORM_TEX(v.texcoord, _MainTex);
    14.          o.texcoord2 = TRANSFORM_TEX(v.texcoord, _Tex2);
    15.  
    16.          return o;
    17.        }
    18.  
    19.        fixed4 frag (v2f i) : SV_Target {
    20.          fixed4 mainTex = tex2D(_MainTex, i.texcoord);
    21.          fixed4 tex2 = tex2D(_Tex2, i.texcoord2);
    22.  
    23.          return mainTex * tex2;
    24.        }


    Am I understanding that right?
     
  2. naked_chicken

    naked_chicken

    Joined:
    Sep 10, 2012
    Posts:
    186
    The flip side to this issue that I just discussed with someone, two float2's at the hardware level mean basically two float4's because the min size is a float4. So does the extra footprint of double the float4's outweigh the performance cost of the dynamic texture lookup?

    I know this is variable based on the application so there's probably no right answer. It's not even really something I'm concerned about for our project, more just me wanting a better understanding and some people's experiences.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    It would appear you are. I was about to say "no, that's ridiculous", but it is what their documentation says. I think some old desktop hardware had special ".zwxy" swizzles that were fine to use, but mobile may not. It also notes that dependent texture reads aren't a problem for "OpenGL ES 3.0" capable hardware, so iPhone 5s, iPad Air, and iPad Mini 2 or newer, ie: any Apple hardware made in the last 4 years. As best I understand dependent texture reads aren't an issue for OpenGL ES 3.0 devices in general.

    So for OpenGL ES 2.0 devices the cost of the dependent texture reads is absolutely more than the cost of the wasted space in the float4, I'm not even sure you pay that cost if you don't use the texture coordinates in the shader anywhere but for the texture read anyway.

    In OpenGL ES 3.0, pack them.
     
  4. naked_chicken

    naked_chicken

    Joined:
    Sep 10, 2012
    Posts:
    186
    awesome, thank you bgolus