Search Unity

Shader Overload Methods? Best Practices?

Discussion in 'Shaders' started by sgoodrow, Apr 11, 2014.

  1. sgoodrow

    sgoodrow

    Joined:
    Sep 28, 2012
    Posts:
    150
    Can anyone chime in on the performance differences between these two fixed2-returning methods? The methods are applying bias to a value in line with traditional bias/gain techniques.

    Here's the base algorithm for a single-component fixed variable:
    Code (csharp):
    1. // Bias
    2. // p = pixel
    3. // b = bias
    4. fixed bias(fixed p, fixed b)
    5. {
    6.         return pow(p, log(b) * -1.44269504089); // 1/log(0.5f)
    7. }
    8.  
    The two implementations for fixed2:
    Code (csharp):
    1.  
    2. fixed2 bias2(fixed2 p, fixed2 b)
    3. {
    4.         return fixed2(bias(p.r, b.r), bias(p.g, b.g));
    5. }
    6.  
    Code (csharp):
    1.  
    2. fixed2 bias2Alt(fixed2 p, fixed2 b)
    3. {
    4.         return pow(p, log(b) * -1.44269504089); // 1/log(0.5f)
    5. }
    Bonus question: When should inline be used? When should it not be used? Does inline improves performance but worsens compile time?
     
    Last edited: Apr 11, 2014
  2. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    I'll jump in on the bonus question..
    Unity uses CG, but CG and HLSL are very similar. All functions in HLSL are inline by default, regardless of if you declare it explicitly. A copy of the functions body gets generated inline at compile time.

    Like I mentioned, Unity uses CG, but I would imagine the above applies for Unity. I'm happy to be corrected tho.
     
  3. sgoodrow

    sgoodrow

    Joined:
    Sep 28, 2012
    Posts:
    150
    I appreciate the reply spraycanmansam, but you didn't really answer my question regarding inline. I understand what it does at that level (its documented as such), but what are the actual implications? What does it actually do, performance wise? When should it be used or when should it not be used?
     
  4. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    I believe I did ;) You asked ---

    Let me rephrase my answer, all functions are inline, regardless of whether you specific the inline keyword. Or in other words, you can't specify a function as not inline.

    So..
    When should inline be used? All functions are inline.
    When should it not be used? All functions are inline.
    Does inline improve performance but worsen compile time? From the answers to 1 and 2 this question becomes a little irrelevant but, yes and yes, along with a few other compiler optimisations. The extra compile time would hardly even be measurable.
     
    Last edited: Apr 12, 2014
  5. sgoodrow

    sgoodrow

    Joined:
    Sep 28, 2012
    Posts:
    150
    Ah, I see. Sorry, I didn't understand. So really, the keyword doesn't need to be included as it is automatically included. A relic of old versions, or something?
     
  6. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    No worries. Like I mentioned, HLSL is definitely wired this way, but not 100% sure on CG. CG is very very similar to HLSL. I've always worked on the assumption that they are, but like I said, happy to be proven wrong :)

    <EDIT> Just had a quick look at the CG compiler specifications ---
    http://http.developer.nvidia.com/Cg/cgc.html
    ..unless Unity is specifying a different compiler directive for inline, it seems to confirm that all functions in CG are inline by default, same as HLSL.

    Hope that helps :)
     
    Last edited: Apr 12, 2014