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 why UNITY_ACCESS_INSTANCED_PROP get nothing?

Discussion in 'General Graphics' started by WestMorn, Mar 20, 2023.

  1. WestMorn

    WestMorn

    Joined:
    Jul 1, 2020
    Posts:
    9
    trimmed off some irrelevant block:
    Code (CSharp):
    1. for (int i = 0; i < 1000; i++)
    2. {
    3.     testColors[i]= new Vector4(Random.value, Random.value, Random.value,Random.Range(0.1f, 1f));
    4. }
    5.  
    6. MaterialPropertyBlock  matProps = new MaterialPropertyBlock();
    7. bounds = new Bounds(CenterVec, Vector3.one * boundScale);
    8. matProps.SetVectorArray("_TestColor", testColors);
    9. rp = new RenderParams(material);
    10. rp.matProps = matProps;
    11. rp.worldBounds = bounds;
    12.  
    13. Graphics.RenderMeshPrimitives(rp, mesh, 0, 1000);
    14. //Graphics.DrawMeshInstancedProcedural(mesh, 0, material, bounds, 1000, matProps);   same result
    trimmed off some irrelevant block from my view:
    Code (CSharp):
    1. Shader "lit/Test"
    2. {
    3.     Properties
    4.     {
    5.         _TestColor("test Color",Color)=(0,1,1)
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             Name "ForwardLight"
    15.             Tags {"LightMode" = "UniversalForward"}
    16.            HLSLPROGRAM
    17.             #pragma target 4.5
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.            #pragma multi_compile_instancing
    21.             #pragma instancing_options assumeuniformscaling procedural:RePos
    22.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    23.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    24.  
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    29.             };
    30.             struct v2f
    31.             {
    32.                 float4 vertex : SV_POSITION;
    33.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    34.             };
    35.  
    36.             #if defined(UNITY_PROCEDURAL_INSTANCING_ENABLED)
    37.                 StructuredBuffer<float3> _Centers;
    38.             #endif
    39.  
    40.             UNITY_INSTANCING_BUFFER_START(Props)
    41.                 UNITY_DEFINE_INSTANCED_PROP(float4, _TestColor)
    42.             UNITY_INSTANCING_BUFFER_END(Props)
    43.  
    44.             void RePos()
    45.             {
    46.                 #if defined(UNITY_PROCEDURAL_INSTANCING_ENABLED)
    47.                     float3 position = _Centers[unity_InstanceID];
    48.                     unity_ObjectToWorld = 0.0;
    49.                     unity_ObjectToWorld._m03_m13_m23_m33 = float4(position, 1.0);
    50.                     unity_ObjectToWorld._m00_m11_m22 = 1;
    51.                 #endif
    52.             }
    53.  
    54.             v2f vert (appdata v, uint v_index : SV_VertexID)
    55.             {          
    56.                 v2f o;
    57.                 UNITY_SETUP_INSTANCE_ID(v);
    58.                 UNITY_TRANSFER_INSTANCE_ID(v, o);
    59.                 #if defined(UNITY_PROCEDURAL_INSTANCING_ENABLED)
    60.                     //.... some verts manipulation using another ComputeBuffer
    61.                 #else
    62.                 o.vertex = mul(UNITY_MATRIX_MVP,v.vertex);
    63.                 #endif
    64.             }
    65.  
    66.             float4 frag (v2f i) : SV_Target
    67.             {
    68.                 UNITY_SETUP_INSTANCE_ID(i);
    69.                  float4 tscolor = UNITY_ACCESS_INSTANCED_PROP(Props, _TestColor);
    70.                  return tscolor;
    71.             }
    72.  
    73.             ENDHLSL
    74.         }
    75.     }
    76. }
    77.  
    it should be various colored meshes in the scene right? instead ,i got a pile of black ones which suggest instanced prop didn't pass in?
    tried GetVectorArray which told me the data transfer is fine, so i don't get it since the syntax of macros are strictly obey the document or examples.
    really need some help or clue, thx in advance
     
    Last edited: Mar 21, 2023
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    651
    Can't see anything blatantly wrong but this example uses a uniform array in the shader, instead of macros.

    UNITY_INSTANCING_BUFFER_START/END is defined as
    Code (CSharp):
    1.     #define UNITY_INSTANCING_BUFFER_START(buf)      UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(UnityInstancing_##buf) struct {
    2.     #define UNITY_INSTANCING_BUFFER_END(arr)        } arr##Array[UNITY_INSTANCED_ARRAY_SIZE]; UNITY_INSTANCING_CBUFFER_SCOPE_END
    3.  
    so you'd have to set the values as an array of structs. Not sure if that is even possible with MaterialPropertyBlock (in your case with just one struct member it might work if you just append "Array" to name but that's not a universal solution). When Unity does the instancing call for you, it has one MPB per instance.
     
    Last edited: Mar 21, 2023
  3. WestMorn

    WestMorn

    Joined:
    Jul 1, 2020
    Posts:
    9
    well, the problem seems solved after i stop goes in the macro(UNITY_INSTANCING_BUFFER_START) way and just using the array. in fragment shader ,directly use the array combining the directive:
    #if defined(UNITY_PROCEDURAL_INSTANCING_ENABLED)
    followed by : albedo = _TestColor[instanceID];

    one thing i noticed when using directive: #ifdef UNITY_INSTANCING_ENABLED
    it fails to get into that branch.

    i didn't dig into UnityInstancing.hlsl or related files, still my guess is : UNITY_INSTANCING_ENABLED and UNITY_INSTANCING_BUFFER_START is a package deal, and the UNITY_PROCEDURAL_INSTANCING_ENABLED is somehow in conflict with the former.

    furthermore, why should i choose to go the MPB approach in the first place instead of StructuredBuffer approach , result shows similarity, especially when no object involved.
     
    Last edited: Mar 21, 2023