I’m using RenderPrimitivesIndexedIndirect to draw a buffer of indexed matrices I access from a buffer of IDs I cull in realtime with a compute shader.
All works perfectly, I can draw (100)thousands of diffrerent meshes in realtime,
I’m having troubles using unity internal functions to create the shadows in a custom shadow caster pass.
The shadows are flickering, like if the shadow casting pass is not drawing always the same, always all or all in the same order? I defined the buffers separately in the two shaders, I guess that’s the way, I have no problems in the drawing pass.
For drawing:
v2f vert(uint svVertexID: SV_VertexID, uint svInstanceID : SV_InstanceID)
{
InitIndirectDrawArgs(0);
v2f o;
uint cmdID = GetCommandID(0);
uint instanceID = GetIndirectInstanceID_Base(svInstanceID);
uint meshID = Visible_ID_Buffer[instanceID];
int vertexID = GetIndirectVertexID(svVertexID);
float4 positionOS = float4(_Positions[ vertexID], 1.0f);
o.worldPos = mul(_TransformMatrices[meshID]._matrix, float4(positionOS.xyz ,1));
... }
In the shadow caster pass:
struct v2f {
V2F_SHADOW_CASTER;
//float4 pos : TEXCOORD0;
float4 _ShadowCoord : TEXCOORD1;
float3 normal : NORMAL;
};
v2f vert(uint svVertexID: SV_VertexID, uint svInstanceID : SV_InstanceID)
{
v2f o;
uint cmdID = GetCommandID(0);
uint instanceID = GetIndirectInstanceID_Base(svInstanceID);
uint meshID = Visible_ID_Buffer[instanceID];
int vertexID = GetIndirectVertexID(svVertexID);
float4 positionOS = float4(_Positions[ vertexID], 1.0f);
o.pos = mul(_TransformMatrices[meshID]._matrix, float4(positionOS.xyz ,1));
o.pos = mul(UNITY_MATRIX_VP,o.pos);
o.normal = float3(_Normals[vertexID]);
//TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
return o;
}
float4 frag(v2f i) : SV_Target
{
//return 0;
SHADOW_CASTER_FRAGMENT(i)
}
I believe internally unity needs to have set at least unity_ObjectToWorld and unity_WorldToObject, because they are not correctly initialized in indirect calls, same for the other parameters expected by :
TRANSFER_SHADOW_CASTER_NORMALOFFSET()
SHADOW_CASTER_FRAGMENT(i)
I looked into UnityCG.cginc but I could not sort it out.
Could someone tell what must be set in V2F_SHADOW_CASTER (couldn’t fin the definition).
Is UNITY_MATRIX_VP always the same for a single indirect draw call?
thanks.