Flickering in Draw Immediate in 3.1.1
Note: This topic is just to track what was already discussed on the discord server.
In a scene with multiple ImmediateShapeDrawer components that each draw several Lines I noticed that even after the 3.1.1 update sometimes some of the scene would start to flicker.
Specifically it would look as though halfway through a Draw.Command, later Draw.Line calls would be using a different transformation matrix as earlier Draw.Line calls, where lines that should definitely be connected appeared offset by distances that seemed to correspond to the world-position of cameras in the scene.
The issue was somewhat reproducible by starting the project clean, opening the scene, and then enabling/disabling a scene camera that was rendering the Draw calls several dozen times. Doing this, as well as moving the camera around and potentially also savling/loading and entering play mode would _eventually_ result in this flickering/corruption occurring.
Screen capture: Desktop 2021.03.18 - 03.45.27.03.DVR_Trim.mp4 - the test scene is from this post.
Since the issue was similar in appearance and behavior to the issue that 3.1.1 was intended to fix (https://shapes.userecho.com/en/communities/1/topics/216-flickering-in-draw-immediate), Freya suggested that the fix might be related as well, and suggested to try modifying ShapeDrawState.AddToCommandBuffer and ShapeDrawState.Cleanup as follows:
public void AddToCommandBuffer( CommandBuffer cmd ) {
if( instanced ) {
cmd.DrawMeshInstanced( drawState.mesh, drawState.submesh, drawState.mat, 0, matrices, count, mpb );
} else {
cmd.DrawMesh( drawState.mesh, matrix, drawState.mat, drawState.submesh, 0, mpb );
}
}
public void Cleanup() { // called after this draw call has rendered
mpb.Clear();
ObjectPool<MaterialPropertyBlock>.Free( mpb );
if( instanced )
ArrayPool<Matrix4x4>.Free( matrices );
}
Basically moving the matrices Free call to the Cleanup.
Initial testing seems to confirm that this may resolve the issue, but getting it to reproduce initially was frustratingly difficult, since performing the exact same actions would frequently not trigger the issue, and sometimes not for many minutes of trying.
This fix has now been applied in 3.2.0, I'll reopen this in case it keeps happening in other cases!