Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug shader error in 'universal render pipeline/lit': maximum ps_4_0 sampler register index (16) exceeded

Discussion in 'Editor & General Support' started by swantonb, Mar 12, 2022.

  1. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
    Hi, will the unity team ever fix this issue that stops me from building? This error shouldnt happen in the Lit shader ever. I've noticed that it may happen in shadergraphs that use many textures after a lot of google searches. But the Lit shader doesnt use that many textures. I've had a bug report on this for more than 6 months. When will it get fixed? I somehow managed to build until now by reimporting the URP assets every time i tried to build but that doesnt work anymore. I just tested in 2021.2.3,4,5,6,7,8,9,10 and 14, and in 2022.1.0b11. I am stuck because of this and it doesnt seem like it will be fixed soon. I've been promised in my bug report that it will get fixed after the christmas vacantions, but it's 12 march and this stuff is still going on. It's somewhat outrageous if you ask me.

    upload_2022-3-12_12-22-29.png


    I made this thread a while ago too: https://forum.unity.com/threads/shader-error-in-universal-render-pipeline-lit-urp-12.1194580/

    Nobody seems to care about this problem. It may not happen for everybody because usually people dont have big projects but im definitely not the only person with a big project here. I think the cause is many lit materials and not everybody has this many lit shader materials in the project.
     
    Last edited: Mar 12, 2022
  2. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
    Well my assumption seems correct. So i added 3 more maps in my game and all of those environment assets had urp/lit shader. Before adding those maps i could bypass the error by reimporting the urp asset and forward renderer before building. After adding those maps i couldnt. I made all those materials simple lit and now i can build again.

    There are weird issues going on with the lit shader since urp 12. Hope they will be fixed soon. Simple lit doesnt look the way i want, i need the lit shader.
     
  3. iMer

    iMer

    Joined:
    May 21, 2013
    Posts:
    29
    Kurt-Dekker and spikezart like this.
  4. iMer

    iMer

    Joined:
    May 21, 2013
    Posts:
    29
    Further questioning:
     
  5. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
    Hi there, I must've missed this reply, this is a game-breaking bug but it wont be fixed anytime soon? I just upgraded to 2021.3 and i cannot build again. Wow.
     
  6. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
    Was this helpful for you? Can you give me more details please?
     
  7. iMer

    iMer

    Joined:
    May 21, 2013
    Posts:
    29
    Just popping this in here as well in case anyone else needs this info (swontonb and I had a little chat via dms)

    > how do you even modify the lit shader? It comes back to default code after compiling, since its in packages folder.

    You just copy the URP package from Library/PackageCache/com.unity.render-pipelines.universal@12.1.6 to the Packages folder (so you end up with Packages/com.unity.render-pipelines.universal@12.1.6) and that makes unity use the local folder instead of re-downloading it

    As for what features to remove.. if you dont use light cookies for example you should be able to just edit the Lit shader and comment out all the "#pragma multi_compile _ _LIGHT_COOKIES"
    If you don't use Decals you can comment out the "#pragma multi_compile _ _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3" etc
    (there's gonna be multiple of that in the shader)

    For me taking out light cookies solved the sampler issues, but your project may have different needs

    If you use the shadergraph you'll also have to modify the keywords shadergraph adds (which was easier than expected, code is quite tidy), you want to look at the URP/Editor/ShaderGraph/Targets/UniversalTarget.cs there
    For light cookies, thats on line 1783 in 12.1.6, you can now look at the uses via your IDE of choice or, if you're just using lit targets the exact spot where its referenced is in UniversalLitSubTarget.cs line 814 onwards - just remove it from there and shader graph shaders wont reference it either anymore
    For example:


    As a side effect, removing multicompile keywords will also reduce your shader build time, which is also a huge issue with 2021 lts :)
     
  8. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
    Dang man you saved my life! Allthough i was using light cookies i choose the ability to build. All i did was saying #define light cookies instead of #pragma multi_compile, as you mentioned in the 2nd post, in the urp lit shader code and the variants went from 1500M to 250M. Unity URP team messed up the light cookies, that was the cause of the sampler limit. I did that because i read your other post here:


    https://gyazo.com/faa9b141bdfa9afdc52e403f994a00f6

    Btw if you want to use decals it's safe to do it, light cookies seem to be the main problem. I use decals for bullet marks and blood splatters.

    Regarding huge build times.. i kinda got used to it, the first build takes ages, the second is faster. Without cookies its a lot of times faster (from 1500M variants to 250M).

    Thanks a lot man! If it wasnt for you to have this issue and be able to report it I'd be stuck forever. My project is 90 gb, kinda hard to send a bug report with it.
     
    eli_123 and iMer like this.
  9. ChangFaseR

    ChangFaseR

    Joined:
    Feb 20, 2019
    Posts:
    7
    OMG! Thank you so much! After I commented out the cookies, the installation package was reduced from 1GB to 700MB, the build time was reduced from 5 hours to 2 hours, and the RAM usage was also reduced...
     
    iMer and swantonb like this.
  10. AshyB

    AshyB

    Joined:
    Aug 9, 2012
    Posts:
    191
    Your sir, have saved my life.

    Linux 2021.3.16f1 building to Android (Quest 2).

    com.unity.render-pipelines.universal@12.1.8

    All I had to do was comment out lit.shader lines 125 and 435 which are the //#pragma multi_compile_fragment _ _LIGHT_COOKIES

    Now, nothing is ever easy so I suspect this will come back to haunt me one day.

    But in the meantime, thank you very much!

    P.S issue only occurred in the scene after i used lightmapping.

    In my non-lightmapped scene there is no problem.
     
  11. Vagabond_

    Vagabond_

    Joined:
    Aug 26, 2014
    Posts:
    1,148
    Don't worry, this is still the case even in latest beta... to which i decided to switch just to see if i will be able to build... but guess what... i can't... latest 2022.x fails to build with some errors that can not be even traced... and latest 2023.beta just gives me the same error as original post... so guess how much you can rely on URP
     
  12. fendercodes

    fendercodes

    Joined:
    Feb 4, 2019
    Posts:
    192
    Getting this on 2022.3.0. I use light cookies, but disabling the feature in the URP pipeline asset files didn't prevent the error anyway. Suddenly unable to build my project.. what gives!?

    Update: After digging through my recent commit history, it seems that this error is being caused by enabling LOD Cross Fade on my URP pipeline asset. The build actually still goes ahead but this error is logged in the background:

    Code (CSharp):
    1. Shader error in 'Universal Render Pipeline/Lit': maximum ps_4_0 sampler register index (16) exceeded at /Users/Joe/Documents/Workspace/Hobble/Library/PackageCache/com.unity.render-pipelines.universal@14.0.7/ShaderLibrary/LightCookie/LightCookieInput.hlsl(12) (on d3d11)
    2.  
    3. Compiling Subshader: 0, Pass: ForwardLit, Fragment program with LIGHTMAP_ON LOD_FADE_CROSSFADE _ADDITIONAL_LIGHTS _ADDITIONAL_LIGHT_SHADOWS _DETAIL_MULX2 _EMISSION _LIGHT_COOKIES _MAIN_LIGHT_SHADOWS _METALLICSPECGLOSSMAP _NORMALMAP _OCCLUSIONMAP _REFLECTION_PROBE_BLENDING _REFLECTION_PROBE_BOX_PROJECTION _SCREEN_SPACE_OCCLUSION _SPECULAR_SETUP
    4. Platform defines: SHADER_API_DESKTOP UNITY_ENABLE_DETAIL_NORMALMAP UNITY_ENABLE_REFLECTION_BUFFERS UNITY_LIGHTMAP_FULL_HDR UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BLENDING UNITY_SPECCUBE_BOX_PROJECTION UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS
    5. Disabled keywords: DEBUG_DISPLAY DIRLIGHTMAP_COMBINED DOTS_INSTANCING_ON DYNAMICLIGHTMAP_ON EVALUATE_SH_MIXED EVALUATE_SH_VERTEX FOG_EXP FOG_EXP2 FOG_LINEAR INSTANCING_ON LIGHTMAP_SHADOW_MIXING SHADER_API_GLES30 SHADOWS_SHADOWMASK UNITY_ASTC_NORMALMAP_ENCODING UNITY_COLORSPACE_GAMMA UNITY_FRAMEBUFFER_FETCH_AVAILABLE UNITY_HALF_PRECISION_FRAGMENT_SHADER_REGISTERS UNITY_HARDWARE_TIER1 UNITY_HARDWARE_TIER2 UNITY_HARDWARE_TIER3 UNITY_LIGHTMAP_DLDR_ENCODING UNITY_LIGHTMAP_RGBM_ENCODING UNITY_METAL_SHADOWS_USE_POINT_FILTERING UNITY_NO_DXT5nm UNITY_NO_FULL_STANDARD_SHADER UNITY_NO_SCREENSPACE_SHADOWS UNITY_PBS_USE_BRDF2 UNITY_PBS_USE_BRDF3 UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION UNITY_UNIFIED_SHADER_PRECISION_MODEL UNITY_VIRTUAL_TEXTURING _ADDITIONAL_LIGHTS_VERTEX _ALPHAMODULATE_ON _ALPHAPREMULTIPLY_ON _ALPHATEST_ON _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3 _DETAIL_SCALED _ENVIRONMENTREFLECTIONS_OFF _FORWARD_PLUS _LIGHT_LAYERS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN _PARALLAXMAP _RECEIVE_SHADOWS_OFF _SHADOWS_SOFT _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A _SPECULARHIGHLIGHTS_OFF _SURFACE_TYPE_TRANSPARENT _WRITE_RENDERING_LAYERS
    6.  
     
    Last edited: Jul 11, 2023
  13. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
    same fix as above, take the URP package from library>package cache, paste it in packages folder, right near the manifest. This way you can edit the lit shader and comment light cookies. That's the only way to get rid of maximum sampler error

    For me 2022.3 brought whole new bunch of errors, like 10-15 errors in all URP shaders. The merging shaders into one was a big fail and i basically cannot send a bug report. It only happens when the project is big and has a lot of materials I believe, just like the maximum sampler error. It cant be reproduced in a small project, you must have thousands of materials.
     
  14. fendercodes

    fendercodes

    Joined:
    Feb 4, 2019
    Posts:
    192
    Your tweak means we cannot use Light Cookies right?
     
  15. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
    yes but, well, its either use cookies or build your project. Which one would you choose? hehe
     
  16. fendercodes

    fendercodes

    Joined:
    Feb 4, 2019
    Posts:
    192
    Neither. :D I'll try sending a repro bug to Unity.
     
  17. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
  18. djarcas

    djarcas

    Joined:
    Nov 15, 2012
    Posts:
    248
  19. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
    well that won't work, I tested it before finding the solution that actually worked (commenting cookies in the shader directly)
     
  20. djarcas

    djarcas

    Joined:
    Nov 15, 2012
    Posts:
    248
    Right. To why can't Unity supply a proper fix rather than us relying on this hack?
     
  21. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
  22. Waz

    Waz

    Joined:
    May 1, 2010
    Posts:
    287
    If we can fix it by commenting out 1 line of code, they can "fix" it with a compile-time option. I need some moderately complex shaders, but I don't need _LIGHT_COOKIES.

    Edit: hey, wait, there IS an option to disable Light Cookies in the URP settings asset, at least in 2023.3.2. And it seems to solve this.
     
    Last edited: Sep 12, 2023
    trombonaut likes this.
  23. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
    Oh good to know! It didn't work before

    Edit: yup, seems to work now. Disabling cookies in URP asset will save you the trouble of commenting stuff yourself in the shader
     
    Last edited: Sep 14, 2023
  24. gcrockenberg_unity

    gcrockenberg_unity

    Joined:
    Sep 6, 2023
    Posts:
    3
    Thanks for posting the work around! Worked for me.
     
  25. Erzsebeth

    Erzsebeth

    Joined:
    Feb 24, 2022
    Posts:
    1
    What if I need cookies in my project? Is there a plan B for solving this issue?

    (I currently have a project where some clues are triggered and shown on the walls with light cookies using the symbols, I've really liked the effect of that, and wouldn't like to remove it.)

    I thank in advance for the help and apologize for eventual english mistakes (I'm brazilian).
     
  26. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    174
    I'd try and fake a cookie with the decal projector maybe
     
  27. eHaka

    eHaka

    Unity Technologies

    Joined:
    Jan 23, 2020
    Posts:
    11
    Hello.

    The issues is that the (Lit) shader has too many textures in use. Reduce the amount of active textures/features in the shader.

    Some APIs like D3D11 has max 16 active samplers in a shader. A hard GFX API Limit out of our control.
    You can have more textures and reuse the samplers. You can use "inline samplers" in Unity for sampler reuse.
    https://docs.unity3d.com/2023.3/Documentation/Manual/SL-SamplerStates.html

    We have done some of this inline sampler reuse work for the Lit shader. Cookies, LOD and shadows should already be using the inline sampler in current Unity versions. More work needs to be done, but there are limits to how much sharing we can do.
    The features mentioned in the error are just a side effect. There is nothing wrong with the features. They just happened to be the last thing in the list when the sampler count went over the limit.

    Unity API is based on OpenGL style coupled texture unit (sampler + texture) for historical reasons. You set the filtering options in the texture. It automatically creates a sampler + texture and there is no way to create and set samplers directly from C#. Only alternative is to use the inline samplers in the shader.
    You can also reuse the samplers from other textures. However the sampler might be discarded by the shader compiler if the texture isn't used. A shader variant that doesn't use the texture might break the other textures reusing its sampler.

    Inline samplers won't help OpenGL. The samplers can't be shared. OpenGL (even the latest) still uses (sampler + texture) units. Inline samplers for GLES are just minimally emulated at the moment. I think the inline sampling options are ignored, and you just get a single default implementation. Texture unit counts can be low on GLES2 and GLES3 (see GL_MAX_TEXTURE_IMAGE_UNITS) and those depend on the API minimum and the device.

    You can also workaround the sampler limitation from the other direction by using texture arrays or texture atlases. This way you are using one sampler + texture object which contains multiple textures.
     
    Beauque and swantonb like this.
  28. bendtner34

    bendtner34

    Joined:
    Jun 17, 2023
    Posts:
    1
    Hi! What if editor throws such errors in console but build ends up being successful is there something to worry about?
     
  29. mathieso

    mathieso

    Joined:
    May 17, 2020
    Posts:
    9
    To see the setting, go to Preferences > Core Render Pipeline, and set Additional Properties Visibility to All Visible.
     
    Yumenoki likes this.
  30. luxichor

    luxichor

    Joined:
    May 24, 2023
    Posts:
    2
    well, I ended up here after getting this error in a brand-new project using only a couple of materials. 2021.3.25, 2022.3.15, and 2023.3.2. i mean it when i say there is almost NOTHING in this project, just a couple of objects and a script for some movement mechanics that i wanted to test in an actual build rather than the editor.

    found another thread suggesting it's an issue with caching? someone was able to fix an instance of the bug by building without any shaders and then building again with all shaders enabled: maximum ps_5_0 sampler register index - Unity Forum. though this was a custom shader and not the URP Lit shader, so not exactly the same.

    in the end, i resolved the issue by just... making a new project and manually copying over what i needed, since it was just a small testing project. bam! no more bug. must have been a weird one-off in my case, despite everything else being the same, which does indicate some kind of caching problem. that or i mistakenly flipped some important switch in the original... which seems unlikely, since i didn't touch any shader settings.
     
  31. adri3105

    adri3105

    Joined:
    Nov 23, 2021
    Posts:
    3
    My mistake was about a material that I had exported from an fbx model that came from 3dmax. The only thing I had to do was change this material for another URP material and it let me do the build.