What's error with this Metal Compute Function in Apple Silicon?

I'm developing AI APPs with Metal Compute Function.

As shown in the following picture captured from Metal Debuger: The preview picutre is wrong while the picture in Attachment View is correct. The final Result is the same as the preview picutre which is not expected.

This error is not appeared in x86_64 Macbook, but appeared in M1 Macbook.

I think there may be some issue(like concurrent read) mentioned in Bring your Metal app to Apple silicon Macs.

Can you give me some idea how to debug it? Thanks!

The bug is fixed.

Reason: Use untracked MTLHeaps, but not using MTLFence to protect the GPU could execute both filters in parallel, and thus read uninitialized dynamic texture data allocated from the heap.

Solution1: set MTLFence, see https://developer.apple.com/documentation/metal/synchronization/implementing_a_multistage_image_filter_using_heaps_and_events

Solution2: turn untracked MTLHeap to tracked MTLHeap, that it is, let heapDescriptor.hazarTrackingMode = MTLHazardTrackingModeTracked, see https://developer.apple.com/documentation/metal/mtlheapdescriptor/3131686-hazardtrackingmode

What's error with this Metal Compute Function in Apple Silicon?
 
 
Q