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. Dismiss Notice

Question Different sampler states for ShaderGraph in HDRP

Discussion in 'Shader Graph' started by Micz84, Sep 26, 2023.

  1. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    436
    In the documentation for the shaders, there are mentioned different SamplerStates.
    https://docs.unity3d.com/Manual/SL-SamplerStates.html
    Shader graph Sampler State node has fewer options. Can I create a custom function node to get all those other sampler states?
     
  2. Remy_Unity

    Remy_Unity

    Unity Technologies

    Joined:
    Oct 3, 2017
    Posts:
    614
    Micz84 likes this.
  3. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    436
  4. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    436
    I have one more question, I have noticed that I can also use SamplerState as a shader property.
    Is there a way to set the modes listed below from the code? I haven't seen anything in the documentation.

    Smp_Repeat_Linear,
    Smp_Clamp_Linear,
    Smp_ClampU_RepeatV_Linear,
    Smp_RepeatU_ClampV_Linear

    Because right now I have it like this

    Code (CSharp):
    1. void SampleTexture_float(UnityTexture2D tex, float2 uv, float mode, out float4 color)
    2. {
    3. if(mode > 2) // 3
    4. color = SAMPLE_TEXTURE2D(tex, UnityBuildSamplerStateStructInternal(Smp_ClampU_RepeatV_Linear), uv);
    5. else if(mode > 1) // 2
    6. color = SAMPLE_TEXTURE2D(tex,UnityBuildSamplerStateStructInternal(Smp_RepeatU_ClampV_Linear), uv);
    7. else if(mode > 0) // 1
    8. color = SAMPLE_TEXTURE2D(tex,UnityBuildSamplerStateStructInternal(Smp_Repeat_Linear), uv);
    9. else // 0
    10. color = SAMPLE_TEXTURE2D(tex,UnityBuildSamplerStateStructInternal(Smp_Clamp_Linear), uv);
    11.  
    12. }
    And I would like to avoid these ifs.
    Why those other modes are not available in SamplerStateNode?
     
  5. Remy_Unity

    Remy_Unity

    Unity Technologies

    Joined:
    Oct 3, 2017
    Posts:
    614
    I'm not sure, but probably to not make the sampler state node enormous.

    If the mode input is uniform (constant on the full screen), only the required branch will be evaluated, and the other ones are fully ignored, so if it is a performance consideration, you should be safe.

    If you want even more options, you can use the HLSL syntax to declare sampler states : https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-sampler

    But I'm not sure it would be possible to do the ifs before the sampling operation. If there is a way to tell the compiler that the mode variable is a constant (maybe through some attribute) and won't change in a thread group, it should be doable, else you'll have to duplicate a lot of the code. Or use macros.