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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

LUT post process add to every shader? (for mobile performance)

Discussion in 'Shaders' started by iamfeyre, May 22, 2021.

  1. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    56
    So I want to apply color grading to my game and was thinking about performance implications for mobile. I basically have 2 questions -

    1. A 3D LUT isn't compatible with mobile right gles2.0 so i was thinking of using a 2D or 2.5D LUT. But I read that post processing is heavy on mobile especially because the tile based rendering. So I thought that shouldn't I just add the LUT to every shader? would that be more performant than using a post processing effect LUT instead?

    2. If I'm willing to trade the 3D ie 2/2.5D LUT for a 1D LUT which would give better performance obviously, but would it also give better performance as a post processing effect LUT or just adding it to every shader? (basically same as question 1 but using 1D LUT)

    Any help is really appreciated!
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,638
    2D LUT isn't that slow. I can run our color correction on an iPad 2 and Samsung Galaxy S6 (potentially even older devices, but I don't have any that are still working to check out) with ease.
     
  3. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    56
    okay so technically there's no 2D LUT. There's 1D and 3D. However 3D LUT doesn't work with mobile platforms so are you talking about 1D LUTs?

    Also if you are indeed talking about 1D then I know it doesn't use that kuch resources but I would still like to know if it's better to add it to each shader or use as a seperate post effect?

    Thank you!
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    11,840
    By "2D LUT" usually that means a 3D LUT packed into a 2D texture, like you might see when you export or import a custom user LUT.

    Like this:

    Then you use a function that samples that 2D texture twice to emulate a filtered 3D texture sample. This function from Unity's HDRP/URP post processing is a nice example implementation of that.
    https://github.com/Unity-Technologi...ssing/PostProcessing/Shaders/Colors.hlsl#L465
    Code (csharp):
    1. //
    2. // 2D LUT grading
    3. // scaleOffset = (1 / lut_width, 1 / lut_height, lut_height - 1)
    4. //
    5. half3 ApplyLut2D(TEXTURE2D_ARGS(tex, samplerTex), float3 uvw, float3 scaleOffset)
    6. {
    7.     // Strip format where `height = sqrt(width)`
    8.     uvw.z *= scaleOffset.z;
    9.     float shift = floor(uvw.z);
    10.     uvw.xy = uvw.xy * scaleOffset.z * scaleOffset.xy + scaleOffset.xy * 0.5;
    11.     uvw.x += shift * scaleOffset.y;
    12.     uvw.xyz = lerp(
    13.         SAMPLE_TEXTURE2D(tex, samplerTex, uvw.xy).rgb,
    14.         SAMPLE_TEXTURE2D(tex, samplerTex, uvw.xy + float2(scaleOffset.y, 0.0)).rgb,
    15.         uvw.z - shift
    16.     );
    17.     return uvw;
    18. }
     
  5. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    56
    Thank you I knew about that already from when I read the documentation for it :p

    But can you help me with the actual question, ie, is it better to use LUT as post effect or add it to every shader for better mobile performance?
     
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,638
    If you’re doing other post effects, doing LUT color correction in the final pass of your post effects is fast.

    If color correction is the only post effect and by doing it in shader you have zero post fx then, maybe it’s worth it? I don’t think there are easy answers, you probably have to try it and see what happens on a low end device.

    But as I said, it’s a non issue, since doing it in post is pretty fast anyway.
     
  7. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    56
    Yes color correction is the only thing I'm doing, there's no else post processing
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    11,840
    The most performant way to do it is as part of your object shader rather than as a post process. Just be mindful that it may not look as you expect on transparent objects as the color correction obviously is happening before blend.

    On mobile VR this is basically the only option as any post processing is too expensive to do and still keep framerate (at least on the original Quest and earlier HMDs).

    For a normal mobile game where you don’t need to be running at full resolution or extremely high frame rate, it’s totally fine to keep it as a post process.
     
  9. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    56
    what's object shader? you mean each and every shader for each object?
     
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    11,840
    Yes.

    The thing that's expensive is running a second pass over the entire screen. Transparency is expensive on mobile, and post processing is equivalent to a full screen transparent surface. It can be significantly faster to run the same post processing code in the per object shader ok the final output color. Also avoids some precision issues.
     
    iamfeyre likes this.