Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Optimization and minimal Pixel or Fragment shader logic

Discussion in 'Shaders' started by eco_bach, Dec 23, 2015.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Hi
    In my attempts at optimizing a Vertex Fragment shader I have moved all the logic in my Fragment shader (except for texture calculations) to my Vertex shader. So my Fragment shader is simply

    Code (csharp):
    1.  
    2. float4 frag(vertexOutput input) : COLOR
    3. {
    4.  
    5. //Texture Maps if using textures
    6. float4 tex = tex2D(_MainTex, input.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);
    7.  
    8. returnfloat4(tex.xyz * input.col * _Color.xyz, 1.0);
    9. //return input.col;
    10. }
    11.  
    My question >
    Is there ever a case when your Fragment shader MUST contain more logic than the above?
     
    Last edited: Dec 23, 2015
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,248
    Sure, whenever you need to do something at a higher frequency than the vertices, ie: something based off of a texture mask, or specular highlights, or normal mapping, etc. etc.
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yeah. If you're fine computing something per-vertex and linearly interpolating the result, then of course, move as much as you can to vertex shader.
     
  4. Flailer

    Flailer

    Joined:
    Apr 1, 2014
    Posts:
    66
    If you wanna go even faster
    in this line:
    float4 tex = tex2D(_MainTex, input.tex.xy* _MainTex_ST.xy+ _MainTex_ST.zw);

    do the * + of MainTex_ST on the vertex unless you're doing some crazy stuff the emulates 3D on a camera facing quad, you should be fine :p

    Cos atm your causin a dependant read, not super bad on desktop, but not great for GPU (especially if your doing multiple texture fetches)