Flickering in Draw Immediate in 3.1.1

Avatar
  • updated
  • Fixed

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.

Reporting a bug? please specify Unity version:
2020.3.0f1
Reporting a bug? please specify Shapes version:
3.1.1
Reporting a bug? please specify Render Pipeline:
Built-in render pipeline
Pinned replies
Avatar
Freya Holmér creator
  • Answer
  • Fixed

This fix has now been applied in 3.2.0, I'll reopen this in case it keeps happening in other cases!

Avatar
JohannesMP

While I've not been able to isolate a reliable repro, it still seems as though with this 3.1.1 occasionally I get artifacts similar to the original issue.

It's generally in a scene with lots of ImmediateModeShapeDrawer components that use Draw.Line and Draw.Polyline calls. It seems to become more likely to happen the more draw calls have occurred, either by waiting longer or by having more than one scene view open along with the game view. What is consistent though is its inconsistency - when you first launch and run the project it will be fine, but after a few dozen recompiles and toggling edit/play mode it becomes more likely.

I've so far only noticed it in a fairly large project and so have not been able to isolate a repro.

Avatar
JuDelCo

This happened to me last night. I was using the same scene as this animated gif and wanted to crank up the stars numbers from 13.300 to about 176.320 discs (everything is made with Draw.Disc calls) and then started happening.

Oddly enough, I can't reproduce it today again, I guess is a bug/quirk in the Unity URP side?

Avatar
JohannesMP
Quote from JuDelCo

This happened to me last night. I was using the same scene as this animated gif and wanted to crank up the stars numbers from 13.300 to about 176.320 discs (everything is made with Draw.Disc calls) and then started happening.

Oddly enough, I can't reproduce it today again, I guess is a bug/quirk in the Unity URP side?

Hmm, in my case I was not using URP, just the built-in render pipeline.

is there enough of an overlap that an issue in Unity would explain it in both?

Avatar
Freya Holmér creator
  • Answer
  • Fixed

This fix has now been applied in 3.2.0, I'll reopen this in case it keeps happening in other cases!

Avatar
Daniele

Ahoy Freya!

I'm posting here since you mentioned you wanted to reopen this in case it happened again, and sorry to be a bringer of bad news but it happened to me (but at least it's reproducible).

But first of all, Shapes is awesome, thank you for doing it ^_^

And now, second of all, the flickering. It happens for me on Unity 2021.2.7f1, built-in render pipeline, Shapes 4.1.3, and is reproducible. The immediate-mode shapes only start flickering if I force Unity's framerate to a given target (using Application.targetFrameRate), in which case both the Shapes and the command buffer in the Inspector (see pic) start flickering, as if they can't sync with the rendering.

P.S. This is nothing game-breaking for me (I use immediate mode only for debugging, so I can live with deactivating targetFrameRate when I need it), but I thought I'd report it.

Avatar
Freya Holmér creator
Quote from Daniele

Ahoy Freya!

I'm posting here since you mentioned you wanted to reopen this in case it happened again, and sorry to be a bringer of bad news but it happened to me (but at least it's reproducible).

But first of all, Shapes is awesome, thank you for doing it ^_^

And now, second of all, the flickering. It happens for me on Unity 2021.2.7f1, built-in render pipeline, Shapes 4.1.3, and is reproducible. The immediate-mode shapes only start flickering if I force Unity's framerate to a given target (using Application.targetFrameRate), in which case both the Shapes and the command buffer in the Inspector (see pic) start flickering, as if they can't sync with the rendering.

P.S. This is nothing game-breaking for me (I use immediate mode only for debugging, so I can live with deactivating targetFrameRate when I need it), but I thought I'd report it.

does it actually flicker the rendering too or just the camera UI?

Avatar
Daniele

The rendering too (of the Shapes, not the whole rendering).

Avatar
Freya Holmér creator

this one is deeply cursed and I don't know if I will fix it anytime soon :c
the issue with command buffers in the built-in RP is that there's no proper callback when it's done rendering, so I've used some heuristics to get an approximation of when that is. I'm guessing, something, somewhere, has a mismatch

Avatar
Daniele

I feel very much for deeply cursed issues (and ouch for Unity not having a proper callback for that). If it's only me bothering you about this fringe case don't worry, now that I understood the reason I will deal with the no-target-framerate workaround and be happy all the same :)

P.S. if it can help in the future, this happens when ANY target framerate is set, and never happens if no target framerate is set (regardless if v-sync is on).