Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

maximum ps_5_0 sampler register index

Discussion in 'Shaders' started by francois85, Sep 30, 2018.

  1. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    469
    so seeing this error in my shadergraph using LWRP,I’m guessing I’ve hit a limit on how many textures I can use in my shader. I’m just staring to work with shaders so my understanding is next to noting but I’ve seen shader that can take 256 textures , how can I use more than 16 textures in my shadergraph,please help
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,667
    You’re limited to 16 sampler states per shader, not 16 textures. By default each texture gets its own sampler state, which means you hit the 16 sampler limit after 16 textures. You can work around this by defining a sampler state and reusing it to sample multiple textures. I believe Shader Graph has the Sampler State Node for this. However that only gets you to 128 textures. The real solution to get past that is using a texture array, which is how those shaders that advertise 256 textures work. And those shaders really have a limit of 256 texture sets, which includes the diffuse, normal, and smoothness textures. A texture array is a special kind of texture which the shader sees as a single texture, and the 256 limit has more to do with the way the texture stores which texture layer to sample. Really you could have 65536 textures in a single shader by using 128 texture arrays with 512 layers each, but that’s overkill.

    If all you need is to get a few more textures, a shared sampler state is the easier option.
     
  3. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    160
    Code (CSharp):
    1. // https://github.com/przemyslawzaworski/Unity3D-CG-programming/blob/master/texture_mapping.shader
    2.  
    3. //Shader shows configuration of using 24 textures per shader in single pass.
    4.  
    5. Shader "Texture Mapping"
    6. {
    7.     Properties
    8.     {
    9.         _Texture01 ("Texture 01", 2D) = "black" {}
    10.         _Texture02 ("Texture 02", 2D) = "black" {}
    11.         _Texture03 ("Texture 03", 2D) = "black" {}
    12.         _Texture04 ("Texture 04", 2D) = "black" {}
    13.         _Texture05 ("Texture 05", 2D) = "black" {}
    14.         _Texture06 ("Texture 06", 2D) = "black" {}
    15.         _Texture07 ("Texture 07", 2D) = "black" {}
    16.         _Texture08 ("Texture 08", 2D) = "black" {}
    17.         _Texture09 ("Texture 09", 2D) = "black" {}
    18.         _Texture10 ("Texture 10", 2D) = "black" {}
    19.         _Texture11 ("Texture 11", 2D) = "black" {}
    20.         _Texture12 ("Texture 12", 2D) = "black" {}
    21.         _Texture13 ("Texture 13", 2D) = "black" {}
    22.         _Texture14 ("Texture 14", 2D) = "black" {}
    23.         _Texture15 ("Texture 15", 2D) = "black" {}
    24.         _Texture16 ("Texture 16", 2D) = "black" {}
    25.         _Texture17 ("Texture 17", 2D) = "black" {}
    26.         _Texture18 ("Texture 18", 2D) = "black" {}
    27.         _Texture19 ("Texture 19", 2D) = "black" {}
    28.         _Texture20 ("Texture 20", 2D) = "black" {}
    29.         _Texture21 ("Texture 21", 2D) = "black" {}
    30.         _Texture22 ("Texture 22", 2D) = "black" {}
    31.         _Texture23 ("Texture 23", 2D) = "black" {}
    32.         _Texture24 ("Texture 24", 2D) = "black" {}      
    33.     }
    34.     Subshader
    35.     {
    36.         Pass
    37.         {
    38.             CGPROGRAM
    39.             #pragma vertex vertex_shader
    40.             #pragma fragment pixel_shader
    41.             #pragma target 5.0
    42.  
    43.             Texture2D _Texture01,_Texture02,_Texture03,_Texture04,_Texture05,_Texture06,_Texture07,_Texture08;
    44.             Texture2D _Texture09,_Texture10,_Texture11,_Texture12,_Texture13,_Texture14,_Texture15,_Texture16;
    45.             Texture2D _Texture17,_Texture18,_Texture19,_Texture20,_Texture21,_Texture22,_Texture23,_Texture24;
    46.  
    47.             SamplerState sampler_linear_repeat;
    48.  
    49.             struct structure
    50.             {
    51.                 float4 vertex : SV_POSITION;
    52.                 float2 uv : TEXCOORD0;
    53.             };
    54.  
    55.             structure vertex_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0)
    56.             {
    57.                 structure vs;
    58.                 vs.vertex = UnityObjectToClipPos (vertex);
    59.                 vs.uv = uv;
    60.                 return vs;
    61.             }
    62.  
    63.             float4 pixel_shader (structure ps ) : SV_TARGET
    64.             {
    65.                 float2 uv = ps.uv.xy;
    66.                 float2 scale = uv/float2(0.25,0.1666);
    67.                 if (uv.x<0.25 && uv.y<0.1666)
    68.                     return  _Texture01.Sample(sampler_linear_repeat, scale);
    69.                 else
    70.                 if (uv.x<0.25 && uv.y<0.3333)
    71.                     return  _Texture02.Sample(sampler_linear_repeat, scale);
    72.                 else
    73.                 if (uv.x<0.25 && uv.y<0.4999)
    74.                     return  _Texture03.Sample(sampler_linear_repeat, scale);
    75.                 else
    76.                 if (uv.x<0.25 && uv.y<0.6666)
    77.                     return  _Texture04.Sample(sampler_linear_repeat, scale);
    78.                 else
    79.                 if (uv.x<0.25 && uv.y<0.8333)
    80.                     return  _Texture05.Sample(sampler_linear_repeat, scale);
    81.                 else
    82.                 if (uv.x<0.25 && uv.y<=1.0)
    83.                     return  _Texture06.Sample(sampler_linear_repeat, scale);
    84.                 else
    85.                 if (uv.x<0.5 && uv.y<0.1666)
    86.                     return  _Texture07.Sample(sampler_linear_repeat, scale);
    87.                 else
    88.                 if (uv.x<0.5 && uv.y<0.3333)
    89.                     return  _Texture08.Sample(sampler_linear_repeat, scale);
    90.                 else
    91.                 if (uv.x<0.5 && uv.y<0.4999)
    92.                     return  _Texture09.Sample(sampler_linear_repeat, scale);
    93.                 else
    94.                 if (uv.x<0.5 && uv.y<0.6666)
    95.                     return  _Texture10.Sample(sampler_linear_repeat, scale);
    96.                 else
    97.                 if (uv.x<0.5 && uv.y<0.8333)
    98.                     return  _Texture11.Sample(sampler_linear_repeat, scale);
    99.                 else
    100.                 if (uv.x<0.5 && uv.y<=1.0)
    101.                     return  _Texture12.Sample(sampler_linear_repeat, scale);
    102.                 else
    103.                 if (uv.x<0.75 && uv.y<0.1666)
    104.                     return  _Texture13.Sample(sampler_linear_repeat, scale);
    105.                 else
    106.                 if (uv.x<0.75 && uv.y<0.3333)
    107.                     return  _Texture14.Sample(sampler_linear_repeat, scale);
    108.                 else
    109.                 if (uv.x<0.75 && uv.y<0.4999)
    110.                     return  _Texture15.Sample(sampler_linear_repeat, scale);
    111.                 else
    112.                 if (uv.x<0.75 && uv.y<0.6666)
    113.                     return  _Texture16.Sample(sampler_linear_repeat, scale);
    114.                 else
    115.                 if (uv.x<0.75 && uv.y<0.8333)
    116.                     return  _Texture17.Sample(sampler_linear_repeat, scale);
    117.                 else
    118.                 if (uv.x<0.75 && uv.y<=1.0)
    119.                     return  _Texture18.Sample(sampler_linear_repeat, scale);
    120.                 else
    121.                 if (uv.x<=1.0 && uv.y<0.1666)
    122.                     return  _Texture19.Sample(sampler_linear_repeat, scale);
    123.                 else
    124.                 if (uv.x<=1.0 && uv.y<0.3333)
    125.                     return  _Texture20.Sample(sampler_linear_repeat, scale);
    126.                 else
    127.                 if (uv.x<=1.0 && uv.y<0.4999)
    128.                     return  _Texture21.Sample(sampler_linear_repeat, scale);
    129.                 else
    130.                 if (uv.x<=1.0 && uv.y<0.6666)
    131.                     return  _Texture22.Sample(sampler_linear_repeat, scale);
    132.                 else
    133.                 if (uv.x<=1.0 && uv.y<0.8333)
    134.                     return  _Texture23.Sample(sampler_linear_repeat, scale);
    135.                 else
    136.                 if (uv.x<=1.0 && uv.y<=1.0)
    137.                     return  _Texture24.Sample(sampler_linear_repeat, scale);
    138.                 else
    139.                     return 1;
    140.             }
    141.             ENDCG
    142.         }
    143.     }
    144. }
     
  4. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    469
    Thanks for the info, looks like I will need to use a texture array. Have you done texture arrays in shadergraph I would know where to start, so I need to create a texture array or can I just plug textures in as I go
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,667
    Unity's editor doesn't help with creating texture arrays at all. You have to construct them entirely on your own from script, or download something from the asset store which can help. Once they've been setup, they can be plugged into a sample 2d array node.
     
  6. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    469
    Ok , thanks again for all your help
     
  7. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    469
  8. Beauque

    Beauque

    Joined:
    Mar 7, 2017
    Posts:
    14
    Hi there, I have the same problem but I am using ASE. I am doing a terrain shader and I just need a few more than 16 textures, 20 in fact. Is there an equivalent of this Sampler State Node in Amplify?
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,667
  10. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    469
    Ive used a textureArray in Amplify, seems like this is what you need. I used over 50 textures that way in a single sampler