Autodesk: WaitUntilCompleted on Metal #2794
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of Change(s)
Correct the behavior when we want to submit a command. This will also make WaitTypeWaitUntilCompleted work properly.
The issue was introduced in change 28116f1. Before the change, the command buffer is committed in HgiMetal::_SubmitCmds(), and before the buffer commit, HgiMetal::_workToFlush was set to indicate if there is dispatching work in compute encoder or drawing work in graphics encoder. So when command buffer is committed, if _workToFlush is true, it will commit the commands to GPU. But in change 28116f1, because they want to introduce the secondary command buffer, the commit of the command buffer is moved to HgiCmds::_Submit (in _hgi->CommitPrimaryCommandBuffer(waitType)), which is before the set of _workToFlush. So even if the encoder contains dispatching work or drawing work, the command buffer will not be committed to GPU. This has two drawbacks. For HgiSubmitWaitTypeNoWait waitType, we push the work to GPU too late. This didn't make the most use of GPU. For HgiSubmitWaitTypeWaitUntilCompleted waitType, it doesn't wait at all.
As you can see, the problem is that _workToFlush is set after the commit of command buffer. So in this change, I add an API HgiMetal::HasWork(). It will set _workToFlush to true. When graphics encoder encodes the draw command or the compute encoder encodes the dispatch command, it can call the HasWork API to indicate there is already work command. So when command buffer is committed, _workToFlush is correctly set.
Fixes Issue(s)