Seams between Texture2DArray pages in mip maps

I have a shader that combines a Texture2DArray grid into one big image.
Shader

Shader "Huge Texture/UI Array"
{
    Properties
    {
        [PerRendererData] _MainTex("Sprite Texture", 2DArray) = "white" {}
        _Color("Tint", Color) = (1,1,1,1)

        _StencilComp("Stencil Comparison", Float) = 8
        _Stencil("Stencil ID", Float) = 0
        _StencilOp("Stencil Operation", Float) = 0
        _StencilWriteMask("Stencil Write Mask", Float) = 255
        _StencilReadMask("Stencil Read Mask", Float) = 255

        _ColorMask("Color Mask", Float) = 15

        [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
        _Cols("Cols", Int) = 1
        _Rows("Rows", Int) = 1
    }

        SubShader
        {
            Tags
            {
                "Queue" = "Transparent"
                "IgnoreProjector" = "True"
                "RenderType" = "Transparent"
                "PreviewType" = "Plane"
                "CanUseSpriteAtlas" = "True"
            }

            Stencil
            {
                Ref[_Stencil]
                Comp[_StencilComp]
                Pass[_StencilOp]
                ReadMask[_StencilReadMask]
                WriteMask[_StencilWriteMask]
            }

            Cull Off
            Lighting Off
            ZWrite Off
            ZTest[unity_GUIZTestMode]
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMask[_ColorMask]

            Pass
            {
                Name "Default"
            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma target 3.5
                #pragma require 2darray

                #include "UnityCG.cginc"
                #include "UnityUI.cginc"

                #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
                #pragma multi_compile_local _ UNITY_UI_ALPHACLIP

                struct appdata_t
                {
                    float4 vertex   : POSITION;
                    float4 color    : COLOR;
                    float2 texcoord : TEXCOORD0;
                    UNITY_VERTEX_INPUT_INSTANCE_ID
                };

                struct v2f
                {
                    float4 vertex   : SV_POSITION;
                    fixed4 color : COLOR;
                    float2 texcoord  : TEXCOORD0;
                    float4 worldPosition : TEXCOORD1;
                    UNITY_VERTEX_OUTPUT_STEREO
                };

                UNITY_DECLARE_TEX2DARRAY(_MainTex);
                fixed4 _Color;
                fixed4 _TextureSampleAdd;
                float4 _ClipRect;
                float4 _MainTex_ST;
                half _Cols;
                half _Rows;

                v2f vert(appdata_t v)
                {
                    v2f OUT;
                    UNITY_SETUP_INSTANCE_ID(v);
                    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
                    OUT.worldPosition = v.vertex;
                    OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);

                    OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);

                    OUT.color = v.color * _Color;
                    return OUT;
                }

                fixed4 frag(v2f IN) : SV_Target
                {
                    float uvx = IN.texcoord.x * _Cols;
                    float uvy = IN.texcoord.y * _Rows;
                    float uvz = floor(uvy) * _Cols + floor(uvx);
                    uvx = frac(uvx);
                    uvy = frac(uvy);

                    half4 color = (UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(uvx, uvy, uvz)) + _TextureSampleAdd) * IN.color;

                    #ifdef UNITY_UI_CLIP_RECT
                    color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
                    #endif

                    #ifdef UNITY_UI_ALPHACLIP
                    clip(color.a - 0.001);
                    #endif

                    return color;
                }
            ENDCG
            }
        }
}

This shader works correctly when mip maps are disabled.
When I enable the mip maps, the seams are displayed between the pages.

Initially I thought it was a problem with mip levels generation or wrapping.
I’ve tried the following (everything doesn’t work):

  • Generate mip levels manually;
  • UNITY_SAMPLE_TEX2DARRAY_SAMPLER;
  • UNITY_SAMPLE_TEX2DARRAY_GRAD;
  • _MainTex_TexelSize. This doesn’t work at all, and contains 0 in all fields;
  • Make padding for pages.
    Padding
uvx = clamp(frac(uvx), 0.05, 0.95);
uvy = clamp(frac(uvy), 0.05, 0.95);

Is this some kind of bug in Unity or what?
Any help would be appreciated.

ddx ddy

Hi @Alex-Vertax , why do you not clamp from 0 to 1?

Neither padding not clamping will help here, as the problem is that frac(). GPUs calculate the mip level based on how much the UV changes between one pixel and the next. When you use frac() it sees the UV change from nearly 1.0 to 0.0 between two pixels, so it things it’s displaying the entire texture within the span of those pixels, and drops down to the smallest mip level available. @BattleAngelAlita 's cryptic comment of “ddx ddy” is indeed what you’re missing. The other two parts are you need to use something like UNITY_SAMPLE_TEX2DARRAY_GRAD, which Unity doesn’t provide but which I’m assuming you’ve copied from one of the old posts , and you need to use the ddx() and ddy() from the uv before using frac().

float uvx = IN.texcoord.x * _Cols;
float uvy = IN.texcoord.y * _Rows;
float uvz = floor(uvy) * _Cols + floor(uvx);
half4 color = (UNITY_SAMPLE_TEX2DARRAY_GRAD(_MainTex, float3(frac(uvx), frac(uvy), uvz), ddx(uvx), ddy(uvy)) + _TextureSampleAdd) * IN.color;
1 Like

@bgolus
It works. Thank you so much for your help.