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.

maximum ps_5_0 sampler register index

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

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,684
    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:
    11,702
    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:
    278
    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. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,684
    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:
    11,702
    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. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,684
    Ok , thanks again for all your help
     
  7. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,684
  8. Beauque

    Beauque

    Joined:
    Mar 7, 2017
    Posts:
    50
    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:
    11,702
  10. RoughSpaghetti3211

    RoughSpaghetti3211

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

    Horus_Sungod42

    Joined:
    Oct 30, 2014
    Posts:
    99
    Thanks Bgolus, using the Sampler State node worked perfectly to have more than 16 textures at once in a shader. Now is it optimized? Time will tell... :)
     
    theCodeHermit likes this.
  12. SinDeSiecle

    SinDeSiecle

    Joined:
    Sep 30, 2017
    Posts:
    98
    @Horus_Sungod42 How did you use SamplerState node? Just attaching its output to sampler input ok?
     
    theCodeHermit likes this.
  13. Horus_Sungod42

    Horus_Sungod42

    Joined:
    Oct 30, 2014
    Posts:
    99
    Yes
     
    stellamocha and theCodeHermit like this.
  14. claudiocho

    claudiocho

    Joined:
    Aug 19, 2019
    Posts:
    3
    @Horus_Sungod42 I don´t know how using the samplerState node resolve this issue. I´m using 12 textures and one SamplerState node and Unity is having problem to render preview for material. You can post the schemas of how you work with this issue? Thanks
     
  15. Liderangel

    Liderangel

    Joined:
    Jul 8, 2018
    Posts:
    87
    I'm getting this error on Shaders made by Unity URP, only while building, which is extremely weird (Unity 2021.2.7f1 - URP 12.1.2):

    upload_2022-4-12_16-35-45.png


    Does anybody know why? And the solution to it? The build somehow does get completed but the errors worry me a great deal.
     
    Last edited: Apr 21, 2022
  16. bfaliuperspectives

    bfaliuperspectives

    Joined:
    Feb 10, 2021
    Posts:
    5
    I have exact same errors when I build my project or my addressables groups. I am using Unity 2021.2.14.
     

    Attached Files:

  17. Liderangel

    Liderangel

    Joined:
    Jul 8, 2018
    Posts:
    87
    I'm not 100% sure about this, but if you work for Unity perhaps you have a way of testing this.

    We managed to build the game by removing all the shaders (we added them all into a Shader Variant) and then build again with all the shaders.

    It makes no sense but we figure (because we tried this in a couple of PCs) it's something related to a Unity cache bug (not project cache because we re-generated the project like 5 times and even updated it).


    That's it.

    Our weird process was like:

    Build with all the shaders - Fail.
    Build with 0 shaders - Success.
    Build with all the shaders - Success.


    ¯\_(ツ)_/¯
     
  18. RSH1

    RSH1

    Joined:
    Jul 9, 2012
    Posts:
    224
    Getting this in 2021.3.2f1 with built-in terrain shader:

    Code (CSharp):
    1. Shader error in 'Nature/Terrain/Standard': maximum ps_5_0 sampler register index (16) exceeded at Files/Unity/Hub/Editor/2021.3.2f1/Editor/Data/CGIncludes/TerrainSplatmapCommon.cginc(62) (on gles3)
    2.  
    3. Compiling Subshader: 0, Pass: FORWARD, Vertex program with DIRECTIONAL DIRLIGHTMAP_COMBINED DYNAMICLIGHTMAP_ON INSTANCING_ON LIGHTMAP_ON SHADOWS_SCREEN SHADOWS_SHADOWMASK _ALPHATEST_ON _NORMALMAP
    4. Platform defines: SHADER_API_GLES30 SHADER_API_MOBILE UNITY_COLORSPACE_GAMMA UNITY_ENABLE_REFLECTION_BUFFERS UNITY_FRAMEBUFFER_FETCH_AVAILABLE UNITY_HARDWARE_TIER1 UNITY_LIGHTMAP_DLDR_ENCODING UNITY_NO_CUBEMAP_ARRAY UNITY_NO_DXT5nm UNITY_NO_FULL_STANDARD_SHADER UNITY_NO_RGBM UNITY_NO_SCREENSPACE_SHADOWS UNITY_PASS_FORWARDBASE UNITY_PBS_USE_BRDF3
    5. Disabled keywords: FOG_EXP FOG_EXP2 FOG_LINEAR LIGHTMAP_SHADOW_MIXING LIGHTPROBE_SH UNITY_ASTC_NORMALMAP_ENCODING UNITY_ENABLE_DETAIL_NORMALMAP UNITY_ENABLE_NATIVE_SHADOW_LOOKUPS UNITY_HALF_PRECISION_FRAGMENT_SHADER_REGISTERS UNITY_HARDWARE_TIER2 UNITY_HARDWARE_TIER3 UNITY_LIGHTMAP_FULL_HDR UNITY_LIGHTMAP_RGBM_ENCODING UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_METAL_SHADOWS_USE_POINT_FILTERING UNITY_PBS_USE_BRDF1 UNITY_PBS_USE_BRDF2 UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION UNITY_SPECCUBE_BLENDING UNITY_SPECCUBE_BOX_PROJECTION UNITY_UNIFIED_SHADER_PRECISION_MODEL UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_VIRTUAL_TEXTURING VERTEXLIGHT_ON
    6.  
     
    DEEnvironment likes this.
  19. azganmtgo

    azganmtgo

    Joined:
    May 3, 2020
    Posts:
    11
    Same - got it after upgrading to 2021.3.2 looks like we got LTS release with a breaking bug... I think if the tech bubble bursts we might lose Unity altogether if they keep working like this.
     
  20. gillemp

    gillemp

    Joined:
    Nov 23, 2015
    Posts:
    81
    How do you build without the shaders? I do not understand what you did
     
  21. sacb0y

    sacb0y

    Joined:
    May 9, 2016
    Posts:
    469
    I'm also having this problem, a shader I've used for a long time keeps getting this error suddenly. I thought it was an amplify error.
     
  22. martin-kucer

    martin-kucer

    Joined:
    Sep 29, 2016
    Posts:
    3
    I have the same error. Any workarounds yet ???
     
  23. Liderangel

    Liderangel

    Joined:
    Jul 8, 2018
    Posts:
    87
    It appears to be an actual Unity error, and the temporary workaround until a proper fix from them was to move URP to the Packages folder and edit all the ShaderGraph shaders and leave them empty (helps with project size at least...).
     
  24. RSH1

    RSH1

    Joined:
    Jul 9, 2012
    Posts:
    224
    Still getting this with 2021.3.6f1
     
  25. Charlicopter

    Charlicopter

    Joined:
    May 15, 2017
    Posts:
    93
    I'm getting this with 2021.3.7f1
    Built my Addressables Groups for the game yesterday and everything was fine. This just suddenly popped up today after I made some script changes (unrelated to shaders).
     
  26. RSH1

    RSH1

    Joined:
    Jul 9, 2012
    Posts:
    224
    If I delete 'library' this works initially, then after some time the error comes back again. What's going on? This is preventing building for Android.
     
unityunity