Search Unity

Feedback Sample State Node disables Anisotropic Filtering

Discussion in 'Shader Graph' started by Korindian, Mar 3, 2020.

  1. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    In HDRP, using a Sample Texture2D node with no connections to the "Sampler(SS)" input results in correct anisotropic filtering.

    If you add a Sampler State node to the "Sampler(SS)" input, anistropic filtering gets disabled regardless of the Quality settings, texture import settings, or Sampler State node setttings.

    What's the reason for this? Shouldn't there be an anisotropic filtering option in the Sampler State filter dropdown?
     
    Last edited: Mar 3, 2020
    theCodeHermit likes this.
  2. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    So this really is a problem.

    Even if you want to use Texture Arrays, you'd need to use a "Sampler State" node as the input to all the "Sample Texture Array" nodes to take advantage of the minimized sampler count, but then you won't have anisotropic filtering on those textures.

    Here are some other links I found where people are wondering the same thing:
    https://forum.unity.com/threads/expose-samplerstate-from-texture.838213/
    https://forum.unity.com/threads/where-is-the-anisotropic-sampler.783095/
    https://www.reddit.com/r/Unity3D/comments/aolj9z/why_there_isnt_an_anisotropic_texture_filter_in/

    From the following article from the forum's very own @bgolus :
    https://medium.com/@bgolus/sharper-mipmapping-using-shader-based-supersampling-ed7aadb47bec

    "...The closest is the Sample Texture 2D LOD node, which uses the equivalent of tex2Dlod(), but that requires the user calculate the mip level manually. It also disables Anisotropic filtering. You can use a Custom node and write the HLSL manually, but there’s currently no way to use the sampler state of the texture (what you control when setting the filter settings on a texture asset) in a Custom node. So you have to use a Sampler State node, and that means you again can’t use Anisotropic filtering since they don’t offer that option for that node. Hopefully at some point they’ll add a Sample Texture 2D Bias node, or the option of using a texture’s existing sample state. There’s no reason either of those couldn’t be added."

    I tried to create a bug report about this multiple times, but the Bug Reporter hangs at "Uploading the report..." 52% no matter how many times I restart and create a new bug report.
     

    Attached Files:

  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    The trick for Shader Graph when using texture arrays is to use the built in Sample Texture 2D Array node and don't plug anything into the Sampler input like you started with. This will use the texture array's sampler state, meaning no increased sampler count even if you use multiple Sample Texture 2D Array nodes sampling the array. Understand as far as the rendering systems are concerned, a texture array is still a "single texture", just one that potentially has multiple layers.

    If you have multiple textures you're sampling from (not in a single texture array), then yes, each one will use it's own and you cannot have both anisotropic filtering and a shared sampler between different textures.

    The one hacky work around for this I've found is using a Custom Function node and passing in a hardcoded reference to a texture's sampler.

    For example:
    upload_2020-3-4_14-30-37.png
    This particular example doesn't make a lot of sense to do since the built in sample texture node will do the same thing, but the trick is I'm using the name of the sampler from the texture explicitly in the function rather than relying on any sampler node or default sampler state being passed along. Since Shader Graph always has a unique sampler state for (most) textures, as long as the name of the sampler in the function matches the generated one (based on the texture's reference prefixed with
    sampler
    ) then it should work.

    It might break if you build for mobile though, but that can't use the shared sampler trick anyway.
     
    Korindian likes this.
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Having the Sample State node takes an optional Texture input ala the Texel Size node would be super useful though and would solve all of this.

    I suspect the lack of Anisotropic filtering options on the Sampler State come from them inheriting the inline sampler system that was originally expected to be used mainly for post processing, 2D, and intentionally using "worse" sampler states than the texture's own rather than a general replacement for texture settings which it currently gets used as for Shader Graph. It's missing a lot of features that texture settings are capable of beyond just anisotropic filtering.
     
    Korindian likes this.
  5. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    Thanks for the Sample Texture 2D Array clarification. I did a test and noticed, as you mentioned, that there is no increased sampler count when using multiple Sample Texture 2D Array nodes, so Sampler State nodes are not needed in that case.

    I also noticed that in HDRP Lit Shader Graphs at least, I can use default Sampler States for 11 textures before hitting the maximum sampler count of 16, due to what I'm guessing is some hidden built-in usage.

    I appreciate the hacky work around you mentioned. If Unity decides not to include anisotropic filtering among other texture settings in the Sampler State node, then I'll try that. It would be great if Unity devs could weigh in on this request... @alexandral_unity perhaps?

    Just wanted to say I really appreciate you @bgolus for replying to so many shader related threads throughout the years, imparting your knowledge so freely. I've learned a lot about shaders from reading your replies to other posts. You add such immense value to these forums.
     
    patrickb2022 likes this.
  6. sqallpl

    sqallpl

    Joined:
    Oct 22, 2013
    Posts:
    384
    Hi,

    I'm wondering if there is any update on this. Is Anisotropic Filtering planned for Sampler State node?

    Thanks.
     
  7. Ziflin

    Ziflin

    Joined:
    Mar 12, 2013
    Posts:
    132
    Same issue here. There needs to be a way to control anisotropic filtering inside the shader graph. We're blending multiple texture 'layers' together in a custom function and there does not appear to be a way to get anisotropic filtering working on our textures because of this issue (we don't want to use the built-in Texture Sampling node).
     
  8. Ziflin

    Ziflin

    Joined:
    Mar 12, 2013
    Posts:
    132
    Can we please get an update on this? We're trying to make decisions on how to proceed and this seems like a feature that should have been available from the start in Unity's Shader Graph.
     
  9. projectorgames_unity

    projectorgames_unity

    Joined:
    Oct 15, 2018
    Posts:
    107
    Going to weigh in on this. Finally got the correct-sized art from the art team, and the lack of anisotropic on my texturearrays isn't of acceptable quality.
     
    theCodeHermit likes this.
  10. sqallpl

    sqallpl

    Joined:
    Oct 22, 2013
    Posts:
    384
    Bump.

    Any update on this? Please let us know if you have any plans.

    Thanks.
     
  11. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    Just realize this today, I was wondering why all my sample texture 2d looks quite blurry on the edge of a sphere compared to default URP Lit shader or just a Sample Texture 2D with Sampler unplugged.

    I guess my question is, as long as sampler state limit is not reached, is it really worth the trouble to to share sampler states? I always do out of my old habit, but perhaps it's not necessary, what's your take? @bgolus
     
  12. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
  13. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    I see the main benefit here is really for Triplanar or Terrain shader, without anisotropic it does look pretty bad considering the view angle, on the other hand, sharing sampler would make the most impact exactly in cases where Triplanar is used...

    shader graph, shrug.

    Screenshot 2021-12-06 164537.jpg Screenshot 2021-12-06 164517.jpg
     
  14. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    If you've not run out of sampler states, it's almost always going to be slower to reuse a sampler state than to have unique ones per texture. It should only be done if you're looking to sample a single texture with different sampler states, or in cases where you are likely to run out.

    Because the documentation is wrong. But also it was added. The documentation shows the aniso inline sampler state being available since 2019.4, but it was not added until Unity 2021.2 (the current beta release), and Shader Graph 13.0 did add support for it at the same time.

    Moveover, since Shader Graph 10 or so, if you do not specify a sampler state override the Sample Texture nodes should all properly use the texture's sampler state even in sub graphs (which was not always the case before that). And it's even possible to access the texture's sampler state from custom functions. So this issue should already be fixed if your texture is set to use anisotropic filtering and you're not using a sampler state node.
     
    Peter-Bailey, Kasperrr and bitinn like this.
  15. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    Actually I did not know that. I went searching for some literature on this, looks like this is highly hardware dependent.

    https://answers.unrealengine.com/qu...mpler-source-why-not-use-shared-by-defau.html

    Interesting, I am using Unity 2021.2 (it's stable release now) and SG 12.1 didn't have this feature.

    But I haven't reached the 16 samplers limit of D3D11 yet, so sharing samplers makes little sense, I will complain when that comes.
     
  16. sqallpl

    sqallpl

    Joined:
    Oct 22, 2013
    Posts:
    384
    @bitinn , @bgolus

    I don't see Anisotropic Filtering in the Sampler State node (Unity 2021.2.15, ShaderGraph 12.1.5).

    Can someone confirm if it's available in Unity 2022.1 (ShaderGraph 13) or later please?

    Thank you.

    There is no word about anisotropic filtering in the Sampler State node documentation (ShaderGraph 13, 14).


    But there is a changelog entry:
    [13.0.0] - 2021-09-01
    • Adding control of anisotropic settings on inline Sampler state nodes in ShaderGraph.
     
    Last edited: Apr 13, 2022
  17. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Yes, it's there for Shader Graph 13.
     
    sqallpl likes this.
  18. sqallpl

    sqallpl

    Joined:
    Oct 22, 2013
    Posts:
    384
    Thanks!