justingardner / mgl

A suite of mex/m files for displaying psychophysics stimuli
http://justingardner.net/mgl
Other
18 stars 22 forks source link

Time to acquire system "drawable" resource each frame #68

Closed benjamin-heasly closed 1 year ago

benjamin-heasly commented 2 years ago

This issue is to capture some observations and research about frame timing. Specifically, we've seen some long delays when our Metal app tries to acquire a system "drawable" resource during each frame -- sometimes as much as 7ms or 14ms. For now, this is just documentation of what we've seen, with no obvious action to take next.

Observations started with our render pipeline test, for example testing how long it takes to a blt an arbitrary full-screen-sized texture to the screen on each frame. This can be tested with

mglTestRenderPipeline('runTests', 4)

Here are some sample results from an M1 iMac (24-inch, 2021, 8GB memory) with macOS Monterey 12.4 and Matlab R2021b.

Screen Shot 2022-06-20 at 9 23 19 AM

The timing of "Draw commands Processed" is unexpected -- ie the green dots are too high up. In these tests the textures are pre-loaded ahead of time, so the command to draw / blt the texture should be very small. Why then does it take some 7ms for the Metal app to report that the drawing command has been issued, each frame?

Digging in, it seems this time is spent when our Metal app is blocked, waiting to acquire a system "drawable" resource, which is required for onscreen rendering. Our Metal code starts here.

It's not clear yet why we end up blocking on this call. We think we're following the relevant guidance from Apple about how to work with the drawable:

Specifically, we are avoiding accessing the drawable as long as we can, until we actually start issuing drawing commands. And, we are allowing the drawable to be released as soon as we're done, using the recommended autoreleasepool {} pattern.

Here are a few more informal observations about the behavior we've seen:

So -- is this simply what to expect on a relatively non-powerful iMac using >10% of its total memory for MGL textures? Or, is there something more we can learn / do to address this blocking time?

justingardner commented 1 year ago

Not sure if the specific observations have been resolved or not, but going to close this out and come back to it if issues arise over frame timing in the future.