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

Structured Buffer on DrawMeshInstancedIndirect with Surface Shader Oculus Quest

Discussion in 'Shaders' started by icormier, Oct 20, 2021.

  1. icormier

    icormier

    Joined:
    Aug 26, 2021
    Posts:
    16
    Hi, I am trying for a while now to use DrawMeshInstancedIndirect to render object with textureArray on Android device. I am unable to get the textureID in structuredBuffer to work properly.

    Here is a small snippet of code I use to send the buffer:

    Code (CSharp):
    1. m_positionBuffer = new ComputeBuffer(instance.Count, sizeof(float) * 3);
    2. m_textureBuffer = new ComputeBuffer(instance.Count, sizeof(float));
    3.  
    4. Vector3[] positions = new Vector3[instance.Count];
    5. float[] textures = new float[instance.Count];
    6.  
    7. // in a for loop
    8. positions[index] = new Vector3(pair.Value.Pos.x, pair.Value.Pos.y, pair.Value.Pos.z);
    9. textures[index] = 1.0;
    10.  
    11.  
    12. m_positionBuffer.SetData(positions);
    13. m_textureBuffer.SetData(textures);
    14. instanceMaterial.SetBuffer("positionBuffer", m_positionBuffer);
    15. instanceMaterial.SetBuffer("textureBuffer", m_textureBuffer);
    16.  
    17.  
    18. //later on update
    19. Graphics.DrawMeshInstancedIndirect(instanceMesh, subMeshIndex, instanceMaterial, new Bounds(Vector3.zero, new Vector3(100.0f, 100.0f, 100.0f)), m_argsBuffer);

    Here is a version of the shader that work fine when using a predefined textureId:

    Code (CSharp):
    1. Shader"Custom/GPUInstanced" {
    2.     Properties {
    3.         _MainTexArray("Base Color Array", 2DArray) = "" {}
    4.         _TextureIdx("Texture Idx", float) = 0.0
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8. LOD 200
    9.  
    10.         CGPROGRAM
    11.         #pragma surface surf Standard addshadow fullforwardshadows
    12.         #pragma multi_compile_instancing
    13.         #pragma instancing_options procedural:setup
    14.  
    15.  
    16. #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    17.         StructuredBuffer<float3> positionBuffer;
    18. #endif
    19.  
    20.  
    21. sampler2D _MainTex;
    22. UNITY_DECLARE_TEX2DARRAY(_MainTexArray);
    23.  
    24.  
    25. UNITY_INSTANCING_BUFFER_START(Props)
    26.     UNITY_DEFINE_INSTANCED_PROP(float, _TextureIdx)
    27. UNITY_INSTANCING_BUFFER_END(Props)
    28.  
    29. struct Input
    30. {
    31.     float2 uv_MainTexArray;
    32. };
    33.  
    34. void setup()
    35. {
    36. #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    37.             float3 data = positionBuffer[unity_InstanceID];
    38.  
    39.             unity_ObjectToWorld._11_21_31_41 = float4(1.0f, 0, 0, 0);
    40.             unity_ObjectToWorld._12_22_32_42 = float4(0, 1.0f, 0, 0);
    41.             unity_ObjectToWorld._13_23_33_43 = float4(0, 0, 1.0f, 0);
    42.             unity_ObjectToWorld._14_24_34_44 = float4(data.xyz, 1);
    43.             unity_WorldToObject = unity_ObjectToWorld;
    44.             unity_WorldToObject._14_24_34 *= -1;
    45.             unity_WorldToObject._11_22_33 = 1.0f / unity_WorldToObject._11_22_33;
    46. #endif
    47.  
    48.  
    49. void surf(Input IN, inout SurfaceOutputStandard o)
    50. {
    51.  
    52.     fixed4 c = UNITY_SAMPLE_TEX2DARRAY(
    53.                            _MainTexArray,
    54.                            float3(IN.uv_MainTexArray, UNITY_ACCESS_INSTANCED_PROP(Props, _TextureIdx))
    55.                        );
    56.  
    57.     o.Albedo = c.rgb;
    58.     o.Alpha = 1.0f;
    59. }
    60.     ENDCG
    61.     }
    62.     FallBack "Diffuse"
    63. }

    The moment I am trying to sample the textureId buffer then the object stop rendering:

    Code (CSharp):
    1. Shader"Custom/GPUInstanced" {
    2.     Properties {
    3.         _MainTexArray("Base Color Array", 2DArray) = "" {}
    4.         _TextureIdx("Texture Idx", float) = 0.0
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8. LOD 200
    9.  
    10.         CGPROGRAM
    11.         #pragma surface surf Standard addshadow fullforwardshadows
    12.         #pragma multi_compile_instancing
    13.         #pragma instancing_options procedural:setup
    14.  
    15.  
    16. #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    17.         StructuredBuffer<float3> positionBuffer;
    18.         StructuredBuffer<float> textureBuffer;
    19. #endif
    20.  
    21.  
    22. sampler2D _MainTex;
    23. UNITY_DECLARE_TEX2DARRAY(_MainTexArray);
    24.  
    25.  
    26. UNITY_INSTANCING_BUFFER_START(Props)
    27.     UNITY_DEFINE_INSTANCED_PROP(float, _TextureIdx)
    28. UNITY_INSTANCING_BUFFER_END(Props)
    29.  
    30. struct Input
    31. {
    32.     float2 uv_MainTexArray;
    33. };
    34.  
    35. void setup()
    36. {
    37. #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    38.             float3 data = positionBuffer[unity_InstanceID];
    39.  
    40.             unity_ObjectToWorld._11_21_31_41 = float4(1.0f, 0, 0, 0);
    41.             unity_ObjectToWorld._12_22_32_42 = float4(0, 1.0f, 0, 0);
    42.             unity_ObjectToWorld._13_23_33_43 = float4(0, 0, 1.0f, 0);
    43.             unity_ObjectToWorld._14_24_34_44 = float4(data.xyz, 1);
    44.             unity_WorldToObject = unity_ObjectToWorld;
    45.             unity_WorldToObject._14_24_34 *= -1;
    46.             unity_WorldToObject._11_22_33 = 1.0f / unity_WorldToObject._11_22_33;
    47. #endif
    48.  
    49.  
    50. void surf(Input IN, inout SurfaceOutputStandard o)
    51. {
    52.     float texture_array_id = 0.0f;
    53. #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    54.             texture_array_id = textureBuffer[unity_InstanceID];    
    55.  
    56. #endif
    57.  
    58.     fixed4 c = UNITY_SAMPLE_TEX2DARRAY(
    59.                            _MainTexArray,
    60.                            float3(IN.uv_MainTexArray, texture_array_id)
    61.                        );
    62.  
    63.     o.Albedo = c.rgb;
    64.     o.Alpha = 1.0f;
    65. }
    66.     ENDCG
    67.     }
    68.     FallBack "Diffuse"
    69. }
    We are using Unity 2019.4.22 Any help would be very appreciated.
     
  2. Estemyr

    Estemyr

    Joined:
    Oct 9, 2018
    Posts:
    8
    Did you find a solution for this?