Search Unity

Shader error: "Incompatible texture type [MTLTextureType2D]..."

Discussion in 'Shaders' started by ricarious, Sep 9, 2019.

  1. ricarious

    ricarious

    Joined:
    Nov 1, 2017
    Posts:
    21
    The shader below generates the following error:

    Metal: Shader[Custom/Terrain]: Incompatible texture type [MTLTextureType2D] of texture bound at index 3, expected [MTLTextureType2DArray]

    The code looks fine to me. I believe that the three calls to UNITY_SAMPLE_TEX2DARRAY() are throwing the error, but the argument is clearly a TEX2DARRAY... so I'm confused.

    [Running Unity 2019.1.8f1 on a 2015 MacBook Pro w/o a GPU (Intel Iris Pro 1536 MB).]

    Code (CSharp):
    1. Shader "Custom/Terrain" {
    2.     Properties {
    3.         testTexture("Texture", 2D) = "white"{}
    4.         testScale("Scale", Float) = 1
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8.         LOD 200
    9.        
    10.         CGPROGRAM
    11.  
    12.         #pragma surface surf Standard fullforwardshadows
    13.         #pragma target 3.0
    14.  
    15.         const static int maxLayerCount = 8;
    16.         const static float epsilon = 1E-4;
    17.  
    18.         int layerCount;
    19.         float3 baseColours[maxLayerCount];
    20.         float baseStartHeights[maxLayerCount];
    21.         float baseBlends[maxLayerCount];
    22.         float baseColourStrength[maxLayerCount];
    23.         float baseTextureScales[maxLayerCount];
    24.  
    25.         float minHeight;
    26.         float maxHeight;
    27.  
    28.         sampler2D testTexture;
    29.         float testScale;
    30.  
    31.         UNITY_DECLARE_TEX2DARRAY(texArray);
    32.  
    33.         struct Input {
    34.             float3 worldPos;
    35.             float3 worldNormal;
    36.         };
    37.  
    38.         float inverseLerp(float a, float b, float value) {
    39.             return saturate((value-a)/(b-a));
    40.         }
    41.  
    42.         float3 triplanar(float3 pos, float scale, float3 axes, int index) {
    43.             float3 sPos = pos / scale;
    44.  
    45.             // The following code is producing the error.
    46.             float3 xProjection = UNITY_SAMPLE_TEX2DARRAY(texArray, float3(sPos.y, sPos.z, index)) * axes.x;
    47.             float3 yProjection = UNITY_SAMPLE_TEX2DARRAY(texArray, float3(sPos.x, sPos.z, index)) * axes.y;
    48.             float3 zProjection = UNITY_SAMPLE_TEX2DARRAY(texArray, float3(sPos.x, sPos.y, index)) * axes.z;
    49.             return xProjection + yProjection + zProjection;
    50.         }
    51.  
    52.         void surf (Input IN, inout SurfaceOutputStandard o) {
    53.              ...
    54.          }
    55.  
    56.         }
    57.         ENDCG
    58.     }
    59.     FallBack "Diffuse"
    60. }
     
  2. ricarious

    ricarious

    Joined:
    Nov 1, 2017
    Posts:
    21
    Adding some detail:
    The code declares a texture array: UNITY_DECLARE_TEX2DARRAY(texArray);
    And later samples it: UNITY_SAMPLE_TEX2DARRAY(texArray, float3(sPos.y, sPos.z, index)) * axes.x;

    But for some strange reason, it generates an error, implying the texture array is not an array...?...

    Metal: Shader[Custom/Terrain]: Incompatible texture type [MTLTextureType2D] of texture bound at index 3, expected [MTLTextureType2DArray]
     
  3. jtate5

    jtate5

    Joined:
    Jan 13, 2014
    Posts:
    24
    Did you ever find a solution to this? I'm having the same exact error.
     
  4. erikbethke

    erikbethke

    Joined:
    Aug 11, 2018
    Posts:
    4
    There is a race condition here. To solve it I simply used Invoke and created the terrain 0.1 seconds later:

    TerrainGenerator.cs

    void Start()
    {
    Invoke("CreateTerrain", 0.1f);
    }

    public void CreateTerrain()
    {
    textureSettings.ApplyToMaterial(mapMaterial);
    textureSettings.UpdateMeshHeights(mapMaterial, heightMapSettings.minHeight, heightMapSettings.maxHeight);

    float maxViewDst = detailLevels[detailLevels.Length - 1].visibleDstThreshold;
    meshWorldSize = meshSettings.meshWorldSize;
    chunksVisibleInViewDst = Mathf.RoundToInt(maxViewDst / meshWorldSize);

    UpdateVisibleChunks();
    }
     
  5. erikbethke

    erikbethke

    Joined:
    Aug 11, 2018
    Posts:
    4
    A follow-up - here was the true solution for me:

    Was to call CreateTerrain() while a modal scene was in full screen view waiting for the player. Buy the time they finished up the modal actions, the terrain was nicely generated.
     
  6. wescoatt

    wescoatt

    Joined:
    Dec 1, 2022
    Posts:
    1
    Since this was the first thread that popped up when I encountered this similar error:

    Metal: Shader[Universal Render Pipeline/Lit]: Incompatible texture type [MTLTextureType2D] of texture bound at index 1, expected [MTLTextureTypeCube]

    If it's helpful to anyone, I found the the solution by walking through top level game objects in the scene and deactivating until the warning stopped - then drilling down through child objects in the same way, and determining I had applied a material to an image (whoops).