Search Unity

Can someone help me with using macros/UNITY_SAMPLE_TEX2D in a method?

Discussion in 'Shaders' started by markashburner, May 22, 2020.

  1. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Hi

    I was wondering if anyone could help me with this shader that uses macros

    I use a whole bunch of samplers in my shader
    UNITY_DECLARE_TEX2D(_Tex1);
    UNITY_DECLARE_TEX2D(_Tex2);
    UNITY_DECLARE_TEX2D(_Tex3);
    UNITY_DECLARE_TEX2D(_Tex4);
    UNITY_DECLARE_TEX2D(_Tex5);
    UNITY_DECLARE_TEX2D(_Tex6);

    then basically in my surface method

    Code (CSharp):
    1.         void surf (Input IN, inout SurfaceOutputStandard o) {
    2. fixed4 c1 = lerp(UNITY_SAMPLE_TEX2D(_Tex1, IN.uv_Tex1 * _TexScale1), UNITY_SAMPLE_TEX2D(_Tex1Fade, IN.uv_Tex1 * _TexScale1Fade), IN.fade)* _Color1 * _Brightness1;
    3.             fixed4 c2 = lerp(UNITY_SAMPLE_TEX2D(_Tex2, IN.uv_Tex1 * _TexScale2), UNITY_SAMPLE_TEX2D(_Tex2Fade, IN.uv_Tex1 * _TexScale2Fade), IN.fade)* _Color2 * _Brightness2;
    4.             fixed4 c3 = lerp(UNITY_SAMPLE_TEX2D(_Tex3, IN.uv_Tex1 * _TexScale3),
    5.  
    6. }
    etc

    However I have this method in the shader that I want to use

    Code (CSharp):
    1. fixed4 tex2DNoTile(sampler2D samp, in fixed2 uv, fixed v)
    2.         {
    3.  
    4.             fixed2 p = floor(uv);
    5.             fixed2 f = frac(uv);
    6.  
    7.             // derivatives (for correct mipmapping)
    8.             fixed2 _ddx = ddx(uv);
    9.             fixed2 _ddy = ddy(uv);
    10.  
    11.             fixed4 va = fixed4(0.0, 0.0, 0.0, 0.0);
    12.             fixed w1 = 0.0;
    13.             fixed w2 = 0.0;
    14.  
    15.             [unroll(3)]
    16.             for (int j = -1; j <= 1; j++)
    17.             {
    18.                 [unroll(3)]
    19.                 for (int i = -1; i <= 1; i++)
    20.                 {
    21.                     fixed2 g = fixed2(fixed(i), fixed(j));
    22.                     fixed4 o = hash4(p + g);
    23.                     fixed2 r = g - f + o.xy;
    24.                     fixed d = dot(r, r);
    25.                     fixed w = exp(-5.0*d);
    26.                     fixed4 c = tex2D(samp, uv + v * o.zw, _ddx, _ddy)* _Color1;
    27.                     va += w * c;
    28.                     w1 += w;
    29.                     w2 += w * w;
    30.                 }
    31.             }
    32.             // normal averaging --> lowers contrasts
    33.             //return va/w1;
    34.             // contrast preserving average
    35.             fixed mean = 0;// tex2DGrad( samp, uv, ddx*16.0, ddy*16.0 ).x;
    36.             fixed4 res = mean + (va - w1 * mean) / sqrt(w2);
    37.             return lerp(va / w1, res, v);
    38.         }
    And to use it I have to go in the surface method

    fixed4 c1 = lerp(tex2DNoTile(_Tex1, IN.uv_Tex1 * _TexScale1, 1),

    but I get an error: cannot implicitly convert from 'Texture2D<float4>' to 'sampler2D' at line 302 (on d3d11)

    Is it not possible to use the tex2DNoTile?

    I have uploaded the shader

    Would thoroughly appreciate it if you could help me out.

    Thanks
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Why are you using this macro?

    Replace those lines with
    sampler2D _Tex1;
    and change all of the
    UNITY_SAMPLE_TEX2D
    to
    tex2D
    and everything will just work, including that no tile function.

    The problem is the macros you're using are generating Direct3D 11 style HLSL, which doesn't use
    sampler2D
    for textures, but instead
    Texture2D
    and
    SamplerState
    . That macro is generating declarations for both of those, and both would need to be passed to the no tile function, and the
    UNITY_SAMPLE_TEX2D
    macro would need to be used. The later is an alias for
    _TexName.Sampler(sampler_TexName, uv)
    , which is the D3D11 equivalent of
    tex2D(_TexName, uv)
    .

    Unity has macros for defining both the texture and sampler state in a function declaration, as well as for passing them to that function, but only for texture arrays and cubemaps, not for normal 2D textures. So if for some reason you wanted to keep using the original D3D11 style texture declaration and sampling, you'd probably want to define your own
    UNITY_ARGS_TEX2D
    and
    UNITY_PASS_TEX2D
    macros. See the file that defines the
    UNITY_ARGS_TEX2DARRAY
    here:
    https://github.com/TwoTailsGames/Unity-Built-in-Shaders/blob/master/CGIncludes/HLSLSupport.cginc
     
    Last edited: May 23, 2020
  3. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    The reason why I am using macros is because I am using more than 16 textures


    SubShader {

    // ----------------------------------------------------
    // --- STANDARD PASS ----------------------------------
    // ----------------------------------------------------
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    // #define USING_6_TEXTURES

    #pragma surface surf Standard fullforwardshadows vertex:vert
    // #pragma surface surf Standard fullforwardshadows
    #pragma target 3.0


    #include "UnityStandardCore.cginc"




    UNITY_DECLARE_TEX2D(_Tex1);
    UNITY_DECLARE_TEX2D(_Tex2);
    UNITY_DECLARE_TEX2D(_Tex3);
    UNITY_DECLARE_TEX2D(_Tex4);
    UNITY_DECLARE_TEX2D(_Tex5);
    UNITY_DECLARE_TEX2D(_Tex6);

    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor1);
    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor2);
    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor3);
    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor4);
    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor5);
    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor6);

    UNITY_DECLARE_TEX2D(_Tex1Fade);
    UNITY_DECLARE_TEX2D(_Tex2Fade);
    UNITY_DECLARE_TEX2D(_Tex3Fade);
    UNITY_DECLARE_TEX2D(_Tex4Fade);
    UNITY_DECLARE_TEX2D(_Tex5Fade);
    UNITY_DECLARE_TEX2D(_Tex6Fade);

    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor1Fade);
    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor2Fade);
    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor3Fade);
    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor4Fade);
    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor5Fade);
    UNITY_DECLARE_TEX2D_NOSAMPLER(_Nor6Fade);

    float _TexScale1;
    float _TexScale2;
    float _TexScale3;
    float _TexScale4;
    float _TexScale5;
    float _TexScale6;

    float _TexScale1Fade;
    float _TexScale2Fade;
    float _TexScale3Fade;
    float _TexScale4Fade;
    float _TexScale5Fade;
    float _TexScale6Fade;

    float _FadeRangeMin;
    float _FadeRangeMax;


    struct Input {

    float2 uv_Tex1;
    float2 uv4_Tex2;
    float4 color: COLOR;
    float4 screenPos;
    half fade;
    };

    half _Glossiness1;
    half _Metallic1;
    half _NorPow1;
    half _Brightness1;
    fixed4 _Color1;

    half _Glossiness2;
    half _Metallic2;
    half _NorPow2;
    half _Brightness2;
    fixed4 _Color2;

    half _Glossiness3;
    half _Metallic3;
    half _NorPow3;
    half _Brightness3;
    fixed4 _Color3;

    half _Glossiness4;
    half _Metallic4;
    half _NorPow4;
    half _Brightness4;
    fixed4 _Color4;

    half _Glossiness5;
    half _Metallic5;
    half _NorPow5;
    half _Brightness5;
    fixed4 _Color5;

    half _Glossiness6;
    half _Metallic6;
    half _NorPow6;
    half _Brightness6;
    fixed4 _Color6;

    float _FractalDivergance;


    fixed4 hash4(fixed2 p) {
    return frac(sin(fixed4(1.0 + dot(p, fixed2(37.0, 17.0)),
    2.0 + dot(p, fixed2(11.0, 47.0)),
    3.0 + dot(p, fixed2(41.0, 29.0)),
    4.0 + dot(p, fixed2(23.0, 31.0)))) * _FractalDivergance);
    }

    fixed4 tex2DNoTile(sampler2D samp, in fixed2 uv, fixed v)
    {

    fixed2 p = floor(uv);
    fixed2 f = frac(uv);

    // derivatives (for correct mipmapping)
    fixed2 _ddx = ddx(uv);
    fixed2 _ddy = ddy(uv);

    fixed4 va = fixed4(0.0, 0.0, 0.0, 0.0);
    fixed w1 = 0.0;
    fixed w2 = 0.0;

    [unroll(3)]
    for (int j = -1; j <= 1; j++)
    {
    [unroll(3)]
    for (int i = -1; i <= 1; i++)
    {
    fixed2 g = fixed2(fixed(i), fixed(j));
    fixed4 o = hash4(p + g);
    fixed2 r = g - f + o.xy;
    fixed d = dot(r, r);
    fixed w = exp(-5.0*d);
    fixed4 c = tex2D(samp, uv + v * o.zw, _ddx, _ddy)* _Color1;
    va += w * c;
    w1 += w;
    w2 += w * w;
    }
    }
    // normal averaging --> lowers contrasts
    //return va/w1;
    // contrast preserving average
    fixed mean = 0;// tex2DGrad( samp, uv, ddx*16.0, ddy*16.0 ).x;
    fixed4 res = mean + (va - w1 * mean) / sqrt(w2);
    return lerp(va / w1, res, v);
    }


    void vert (inout appdata_full v, out Input data) {

    UNITY_INITIALIZE_OUTPUT(Input,data);
    float pos = length(UnityObjectToViewPos(v.vertex).xyz);
    float diff = _FadeRangeMax - _FadeRangeMin;
    float invDiff = -1.0 / diff;
    data.fade = clamp ((_FadeRangeMax - pos) * invDiff, 0.0, 1.0);
    }

    void surf (Input IN, inout SurfaceOutputStandard o) {

    fixed4 c1 = lerp(tex2DNoTile(_Tex1, IN.uv_Tex1 * _TexScale1, 1), UNITY_SAMPLE_TEX2D(_Tex1Fade, IN.uv_Tex1 * _TexScale1Fade), IN.fade)* _Color1 * _Brightness1;
    fixed4 c2 = lerp(tex2DNoTile(_Tex2, IN.uv_Tex1 * _TexScale2, 1), UNITY_SAMPLE_TEX2D(_Tex2Fade, IN.uv_Tex1 * _TexScale2Fade), IN.fade)* _Color2 * _Brightness2;
    fixed4 c3 = lerp(tex2DNoTile(_Tex3, IN.uv_Tex1 * _TexScale3, 1), UNITY_SAMPLE_TEX2D(_Tex3Fade, IN.uv_Tex1 * _TexScale3Fade), IN.fade)* _Color3 * _Brightness3;
    fixed4 c4 = lerp(tex2DNoTile(_Tex4, IN.uv_Tex1 * _TexScale4, 1), UNITY_SAMPLE_TEX2D(_Tex4Fade, IN.uv_Tex1 * _TexScale4Fade), IN.fade)* _Color4 * _Brightness4;
    fixed4 c5 = lerp(tex2DNoTile(_Tex5, IN.uv_Tex1 * _TexScale5, 1), UNITY_SAMPLE_TEX2D(_Tex5Fade, IN.uv_Tex1 * _TexScale5Fade), IN.fade)* _Color5 * _Brightness5;
    fixed4 c6 = lerp(tex2DNoTile(_Tex6, IN.uv_Tex1 * _TexScale6, 1), UNITY_SAMPLE_TEX2D(_Tex6Fade, IN.uv_Tex1 * _TexScale6Fade), IN.fade)* _Color6 * _Brightness6;

    o.Albedo = IN.color.r * c1 + IN.color.g * c2 + IN.color.b * c3 + IN.color.a * c4 + IN.uv4_Tex2.x * c5 + IN.uv4_Tex2.y * c6;

    o.Metallic = IN.color.r * _Metallic1 + IN.color.g * _Metallic2 + IN.color.b * _Metallic3 + IN.color.a * _Metallic4 + IN.uv4_Tex2.x * _Metallic5 + IN.uv4_Tex2.y * _Metallic6;
    o.Smoothness = IN.color.r * _Glossiness1 + IN.color.g * _Glossiness2 + IN.color.b * _Glossiness3 + IN.color.a * _Glossiness4 + IN.uv4_Tex2.x * _Glossiness5 + IN.uv4_Tex2.y * _Glossiness6;

    fixed3 n1 = lerp(UnpackScaleNormal(tex2DNoTile(_Nor1, IN.uv_Tex1 * _TexScale1, 1), _NorPow1), UnpackNormal(UNITY_SAMPLE_TEX2D_SAMPLER(_Nor1Fade, _Tex1Fade, IN.uv_Tex1 * _TexScale1Fade)), IN.fade);
    fixed3 n2 = lerp(UnpackScaleNormal(tex2DNoTile(_Nor2, IN.uv_Tex1 * _TexScale2, 1), _NorPow2), UnpackNormal(UNITY_SAMPLE_TEX2D_SAMPLER(_Nor2Fade, _Tex2Fade, IN.uv_Tex1 * _TexScale2Fade)), IN.fade);
    fixed3 n3 = lerp(UnpackScaleNormal(tex2DNoTile(_Nor3, IN.uv_Tex1 * _TexScale3, 1), _NorPow3), UnpackNormal(UNITY_SAMPLE_TEX2D_SAMPLER(_Nor3Fade, _Tex3Fade, IN.uv_Tex1 * _TexScale3Fade)), IN.fade);
    fixed3 n4 = lerp(UnpackScaleNormal(tex2DNoTile(_Nor4, IN.uv_Tex1 * _TexScale4, 1), _NorPow4), UnpackNormal(UNITY_SAMPLE_TEX2D_SAMPLER(_Nor4Fade, _Tex4Fade, IN.uv_Tex1 * _TexScale4Fade)), IN.fade);
    fixed3 n5 = lerp(UnpackScaleNormal(tex2DNoTile(_Nor5, IN.uv_Tex1 * _TexScale5, 1), _NorPow5), UnpackNormal(UNITY_SAMPLE_TEX2D_SAMPLER(_Nor5Fade, _Tex5Fade, IN.uv_Tex1 * _TexScale5Fade)), IN.fade);
    fixed3 n6 = lerp(UnpackScaleNormal(tex2DNoTile(_Nor6, IN.uv_Tex1 * _TexScale6, 1), _NorPow6), UnpackNormal(UNITY_SAMPLE_TEX2D_SAMPLER(_Nor6Fade, _Tex6Fade, IN.uv_Tex1 * _TexScale6Fade)), IN.fade);

    o.Normal = IN.color.r * n1 + IN.color.g * n2 + IN.color.b * n3 + IN.color.a * n4 + IN.uv4_Tex2.x * n5 + IN.uv4_Tex2.y * n6;

    }
    ENDCG
     
  4. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    @bgolus

    Would I have to use the tex2DNoTile in that cginc shader?
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    I’m not sure I understand your question.

    If you want to use a no tiling function like the one you posted and keep your split split texture and sampler states to avoid the sampler limits, you’ll have to update it to use the appropriate macros, as well as define new macros that Unity didn’t include. Like I detailed above.