Search Unity

Compute Shader SamplerState confusion

Discussion in 'Shaders' started by jesta, Dec 23, 2012.

  1. jesta

    jesta

    Joined:
    Jun 19, 2010
    Posts:
    294
    The Unity docs say that compute shaders are written in basic HLSL, but I have some problems declaring samplers.

    Example:
    Code (csharp):
    1.  
    2. Texture3D<float4> myTex : register(t0);
    3. RWTexture3D<float4> myUAV : register (u0);
    4. SamplerState linearSamp : register(s0);
    and then using:
    Code (csharp):
    1.  
    2. float4 c = myTex.SampleLevel(linearSamp, myTexCoord, 0);
    3. myUAV[dtID] = c;
    gives the following error:
    Should I use a built-in sampler or initialize it in some specific way?

    Thanks for any suggestions.
     
    Last edited: Dec 23, 2012
  2. jesta

    jesta

    Joined:
    Jun 19, 2010
    Posts:
    294
    Reading the DirectCompute documentation further, I came accross the "mips" operator. Would it be equivalent to the SampleLevel operation?
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yeah, for samplers you have to follow some special rules, the reason being that "textures" and "samplers" aren't separated in Unity. I'll add this to the docs, but the rules for compute shaders are:

    * Either use same as texture name, with "sampler" in front (e.g. Texture2D MyTex; SamplerState samplerMyTex). In this case, sampler will be initialized to that texture's filter/wrap/aniso settings.

    * Or use one of "predefined" samplers; name has to have "Linear" or "Point" (for filter mode) and "Clamp" or "Repeat" (for wrap mode). Example, "MyLinearClampSampler" - this will have linear filter and clamp wrap mode.
     
  4. scone

    scone

    Joined:
    May 21, 2008
    Posts:
    244
    Hey Aras,

    None of those samplers are working for me. I have:

    Texture3D<float4> NoiseTex;

    and samplerNoiseTex isn't working, nor is MyLinearClampSampler, etc.

    Do I still have to declare them?
     
  5. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    In case you're still having trouble sampling in compute shaders (and for future travellers of this thread): make sure you specify which mipmap level should be used (LOD). (So in compute shaders you'll want to use SampleLevel, in stead of Sample.)
     
  6. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yes you do have to declare them, i.e.
    Texture3D<float4> NoiseTex;
    SamplerState samplerNoiseTex;
     
  7. GeorgeAdamon

    GeorgeAdamon

    Joined:
    May 31, 2017
    Posts:
    48
    Would have never figured this out. 7 years later, thanks Aras!

    [EDIT] : I found this in the documentation
     
    Last edited: Sep 28, 2019
    PrimalCoder likes this.
  8. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    Still relevant in URP it seems...

    I'm hitting a
    Code (CSharp):
    1. Fragment program 'frag': Unrecognized sampler 'sampler_repeatedtexture' - does not match any texture and is not a recognized inline name (should contain filter and wrap modes).
    and don't know why.

    There's
    Code (CSharp):
    1. TEXTURE2D(_ClampedTexture);
    2. SAMPLER(sampler_ClampedTexture);
    3. TEXTURE2D(_RepeatedTexture);
    4. SAMPLER(sampler_RepeatedTexture);
    in place as it should, and I'm trying to call
    Code (CSharp):
    1. SAMPLE_TEXTURE2D(_ClampedTexture, sampler_RepeatedTexture, IN.uv0.xy);
    to sample the texture with another sampler...

    Is the shader compiler doing "magic" in the background and removing samplers in the actual compiled code? From the generated code it looks alright...
     
    Last edited: Jan 14, 2021
  9. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    The error shows it written in all lowercase though. Maybe that's just an error quirk but maybe you had it written like that at first and it hasn't recompiled?
    This should be working fine, this is exactly how it's used in their shaders too. Have you restarted Unity and reimported the shader as a test? And if so the error repeats itself on reimport?
     
  10. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    Yep, that error stays around. My first instinct was also to rename the property to lowercase (_repeatedtexture) instead, but the error persists, also after restarts and library clear.

    It seems that the actual reason for this failing is that the shader compiler strips out the texture when it's not accessed, and when doing so also strips out the sampler, thus resulting in "totally ok code but the compilation result is still broken". Not sure if that's a bug or "by design"; accessing the sampler but not the texture is surely an edge case.
     
    Beauque likes this.
  11. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    I'm not sure how you're both accessing the texture to get the error, but also supposedly it isn't being accessed when the shader compiler looks at the code... Are you saying you have the sampling of it inside of keyword/shader_feature block, and you're only turning that keyword on at runtime in build? Because that would make sense as unused variants are stripped, which is what multi_compile avoids.
     
  12. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    If you are not using the _RepeatedTexture texture, then yes it will be removed as part of compilation. So effectively you're left with a sampler that has no idea which texture it should get the sampling settings from, since the texture is gone.
     
    fherbst likes this.