#Why does EntityCommandBufferSystem complete jobs before Playback?

1 messages · Page 1 of 1 (latest)

minor forge
#

Hello everyone, I’m currently struggling with ECBSystem. According to the documentation I’ve read, structural changes force the main thread to complete all scheduled jobs. However, with ECB systems, I see that Unity requires registering the dependency of the job with the ECB system.
I looked at Unity’s source code and saw that the OnUpdate of the ECB system is implemented roughly like this:

CompleteDependency();
m_ProducerHandle.Complete();
m_ProducerHandle = new JobHandle();
int length = PendingBuffers.Length;
for (int i = 0; i < length; ++i)
{
    var buffer = PendingBuffers[i];
    buffer.Playback(EntityManager);
}

What I don’t understand is whether the job completion step before playback is redundant, because when playback happens, all scheduled jobs should already have been completed.

In my mind, the flow would look something like this instead:

int length = PendingBuffers.Length;
for (int i = 0; i < length; ++i)
{
    var buffer = PendingBuffers[i];
    buffer.Playback(EntityManager);
    // Complete all jobs scheduled before Add/Remove/Create/...
}

Am I misunderstanding something here? sadok

rugged glade
#

What I don’t understand is whether the job completion step before playback is redundant, because when playback happens, all scheduled jobs should already have been completed.
without the complete, why do you think this would be the case?

#

the complete is what ensures all jobs have been completed

#

if all jobs had already been completed, then the complete does nothing

minor forge
#

without the complete, why do you think this would be the case?
I took a look at the internal implementation of EntityCommandBuffer.PlaybackInternal. During playback, PlaybackProcessor.Init calls EntityManager.BeginStructuralChanges(), which leads to DependencyManager.CompleteAllJobsAndInvalidateArrays() if there are still jobs. So it seems if any jobs are still running when playback starts, DependencyManager would force them to complete anyway before the structural changes are applied.

rugged glade
#

[though generally this would be a terrible idea]