Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I use D3D12 shader features like NonUniformResourceIndex?

Discussion in 'Shaders' started by Zenorbi, Jan 4, 2023.

  1. Zenorbi

    Zenorbi

    Joined:
    Oct 1, 2019
    Posts:
    3
    In our project we require indexing into a Texture3D array because we are doing multi volume raymarching. Our current workaround is using a switch around every dynamic texture read, emulating non uniform resource indexes.

    We were looking forward to unity releasing DX12 from the experimental phase and having the ability to use the NonUniformResourceIndex DX12 feature but we are unable to force unity to use shader model 5 and DX12 to compile our shader.

    Even though we are using Unity 2022.2.1f1 and only have DX12 enabled as the graphics API, we get the following error message:
    'NonUniformResourceIndex' requires shader model 5 or higher at line 55 (on d3d11)


    Seeing how the error message has "(on d3d11)" my guess is that Unity's shader compiler still compiles only to D11. Can we somehow force Unity to compile the shader targeting D12?

    Here is a small repro of this issue:

    Code (CSharp):
    1.  
    2. Shader "Unlit/NonUniformResourceIndex"
    3. {
    4.    Properties
    5.    {
    6.       _InputTexture0("Input1", 2D) = "white" {}
    7.       _InputTexture1("Input2", 2D) = "gray" {}
    8.       _InputTexture2("Input3", 2D) = "black" {}
    9.    }
    10.    SubShader
    11.    {
    12.       Tags { "RenderType"="Opaque" }
    13.       LOD 100
    14.  
    15.       Pass
    16.       {
    17.          CGPROGRAM
    18.          #pragma vertex vert
    19.          #pragma fragment frag
    20.          #pragma target 5.0
    21.  
    22.          #include "UnityCG.cginc"
    23.  
    24.          struct appdata
    25.          {
    26.             float4 vertex : POSITION;
    27.             float2 uv : TEXCOORD0;
    28.          };
    29.  
    30.          struct v2f
    31.          {
    32.             float2 uv : TEXCOORD0;
    33.             float4 vertex : SV_POSITION;
    34.          };
    35.          Texture2D<float4> _InputTexture0;
    36.          Texture2D<float4> _InputTexture1;
    37.          Texture2D<float4> _InputTexture2;
    38.  
    39.          Texture2D<float4> _InputTextures[3];
    40.          SamplerState LinearClampSampler;
    41.  
    42.          v2f vert (appdata v)
    43.          {
    44.             v2f o;
    45.             o.vertex = UnityObjectToClipPos(v.vertex);
    46.             o.uv = v.uv;
    47.             return o;
    48.          }
    49.  
    50.          // === CANNOT ENABLE THIS ===
    51.          // #define USE_NON_UNIFORM_RESOURCE_INDEX
    52.  
    53.          float4 ReadTexture(int index, float2 uv)
    54.          {
    55.             #ifdef USE_NON_UNIFORM_RESOURCE_INDEX
    56.             return _InputTextures[NonUniformResourceIndex(index)].Sample(LinearClampSampler, uv);
    57.             #else
    58.             switch (index)
    59.             {
    60.                case 0:
    61.                   return _InputTextures[0].Sample(LinearClampSampler, uv);
    62.                case 1:
    63.                   return _InputTextures[1].Sample(LinearClampSampler, uv);
    64.                case 2:
    65.                   return _InputTextures[2].Sample(LinearClampSampler, uv);
    66.                default:
    67.                   return float4(0, 0, 0, 0);
    68.             }
    69.             #endif
    70.          }
    71.  
    72.          fixed4 frag (v2f i) : SV_Target
    73.          {
    74.             _InputTextures[0] = _InputTexture0;
    75.             _InputTextures[1] = _InputTexture1;
    76.             _InputTextures[2] = _InputTexture2;
    77.  
    78.             int index = i.uv.x * 3 % 3;
    79.             float4 col = ReadTexture(index, i.uv);
    80.             #ifdef USE_NON_UNIFORM_RESOURCE_INDEX
    81.             return lerp(col, float4(0, 1, 0, 1), 0.2);
    82.             #else
    83.             return lerp(col, float4(1, 0, 0, 1), 0.2);
    84.             #endif
    85.          }
    86.          ENDCG
    87.       }
    88.    }
    89. }
    90.  
     
  2. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    #pragma use_dxc