moltenvk/Demos/Cube/macOS/DemoViewController.m
Bill Hollings 9f64faadbc Improve behavior of swapchain image presentation stalls caused by Metal regression.
In a recent Metal regression, Metal sometimes does not trigger the
[CAMetalDrawable addPresentedHandler:] callback on the final few (1-3)
CAMetalDrawable presentations, and retains internal memory associated
with these CAMetalDrawables. This does not occur for any CAMetalDrawable
presentations prior to those final few.

Most apps typically don't care much what happens after the last few
CAMetalDrawables are presented, and typically end shortly after that.

However, for some apps, such as Vulkan CTS WSI tests, which serially create
potentially hundreds, or thousands, of CAMetalLayers and MTLDevices,these
retained device memory allocations can pile up and cause the CTS WSI tests
to stall, block, or crash.

This issue has proven very difficult to debug, or replicate in incrementally
controlled environments. It appears consistently in some scenarios, and never
in other, almost identical scenarios.

For example, the MoltenVK Cube demo consistently runs without encountering
this issue, but CTS WSI test dEQP-VK.wsi.macos.swapchain.render.basic
consistently triggers the issue. Both apps run almost identical Vulkan
command paths, and identical swapchain image presentation paths, and
result in GPU captures that have identical swapchain image presentations.

We may ultimately have to wait for Apple to fix the core issue, but this
update includes workarounds that helps in some cases. During vkQueueWaitIdle()
and vkDeviceWaitIdle(), wait a short while for any in-flight swapchain image
presentations to finish, and attempt to force completion by calling
MVKPresentableSwapchainImage::forcePresentationCompletion(), which releases
the current CAMetalDrawable, and attempts to retrieve a new one, to trigger
the callback on the current CAMetalDrawable.

In exploring possible work-arounds for this issue, this update adds significant
structural improvements in the handling of swapchains, and quite a bit of new
performance and logging functionality that is useful for debugging purposes.

- Add several additional performance trackers, available via logging,
  or the mvk_private_api.h API.
- Rename MVKPerformanceTracker members, and refactor performance result
  collection, to support tracking and logging memory use, or other measurements,
  in addition to just durations.
- Redefine MVKQueuePerformance to add tracking separate performance metrics for
  MTLCommandBuffer retrieval, encoding, and execution, plus swapchain presentation.
- Add MVKDevicePerformance as part of MVKPerformanceStatistics to track device
  information, including GPU device memory allocated, and update device memory
  results whenever performance content is requested.
- Add MVKConfigActivityPerformanceLoggingStyle::
  MVK_CONFIG_ACTIVITY_PERFORMANCE_LOGGING_STYLE_DEVICE_LIFETIME_ACCUMULATE
  to accumulate performance and memory results across multiple serial
  invocations of VkDevices, during the lifetime of the app process. This
  is useful for accumulating performance results across multiple CTS tests.
- Log destruction of VkDevice, VkPhysicalDevice, and VkInstance, to bookend
  the corresponding logs performed upon their creation.
- Include consumed GPU memory in log when VkPhysicalDevice is destroyed.
- Add mvkGetAvailableMTLDevicesArray() to support consistency when retrieving
  MTLDevices available on the system.
- Add mvkVkCommandName() to generically map command use to a command name.
- MVKDevice:
    - Support MTLPhysicalDevice.recommendedMaxWorkingSetSize on iOS & tvOS.
    - Include available and consumed GPU memory in log of GPU device at
      VkInstance creation time.
- MVKQueue:
    - Add handleMTLCommandBufferError() to handle errors for all
      MTLCommandBuffer executions.
    - Track time to retrieve a MTLCommandBuffer.
    - If MTLCommandBuffer could not be retrieved during queue submission,
      report error, signal queue submission completion, and return
      VK_ERROR_OUT_OF_POOL_MEMORY.
    - waitIdle() simplify to use [MTLCommandBuffer waitUntilCompleted],
      plus also wait for in-flight presentations to complete, and attempt
      to force them to complete if they are stuck.
- MVKPresentableSwapchainImage:
    - Don't track presenting MTLCommandBuffer.
    - Add limit on number of attempts to retrieve a drawable, and report
      VK_ERROR_OUT_OF_POOL_MEMORY if drawable cannot be retrieved.
    - Return VkResult from acquireAndSignalWhenAvailable() to notify upstream
      if MTLCommandBuffer could not be created.
    - Track presentation time.
	- Notify MVKQueue when presentation has completed.
	- Add forcePresentationCompletion(), which releases the current
	  CAMetalDrawable, and attempts to retrieve a new one, to trigger the
	  callback on the current CAMetalDrawable. Called when a swapchain is
	  destroyed, or by queue if waiting for presentation to complete stalls,
	- If destroyed while in flight, stop tracking swapchain and
	  don't notify when presentation completes.
- MVKSwapchain:
    - Track active swapchain in MVKSurface to check oldSwapchain
    - Track MVKSurface to access layer and detect lost surface.
    - Don't track layer and layer observer, since MVKSurface handles these.
    - On destruction, wait until all in-flight presentable images have returned.
    - Remove empty and unused releaseUndisplayedSurfaces() function.
- MVKSurface:
	- Consolidate constructors into initLayer() function.
    - Update logic to test for valid layer and to set up layer observer.
- MVKSemaphoreImpl:
    - Add getReservationCount()
- MVKBaseObject:
    - Add reportResult() and reportWarning() functions to support logging
      and reporting Vulkan results that are not actual errors.
- Rename MVKCommandUse::kMVKCommandUseEndCommandBuffer to
  kMVKCommandUseBeginCommandBuffer, since that's where it is used.
- Update MVK_CONFIGURATION_API_VERSION and MVK_PRIVATE_API_VERSION to 38.
- Cube Demo support running a maximum number of frames.
2023-09-02 08:51:36 -04:00

146 lines
4.7 KiB
Objective-C

/*
* DemoViewController.m
*
* Copyright (c) 2015-2023 The Brenwill Workshop Ltd. (http://www.brenwill.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "DemoViewController.h"
#import <QuartzCore/CAMetalLayer.h>
#import <CoreVideo/CVDisplayLink.h>
#include <MoltenVK/mvk_vulkan.h>
#include "../../Vulkan-Tools/cube/cube.c"
#pragma mark -
#pragma mark DemoViewController
@implementation DemoViewController {
CVDisplayLinkRef _displayLink;
struct demo demo;
uint32_t _maxFrameCount;
uint64_t _frameCount;
BOOL _stop;
BOOL _useDisplayLink;
}
/** Since this is a single-view app, initialize Vulkan as view is appearing. */
-(void) viewWillAppear {
[super viewWillAppear];
self.view.wantsLayer = YES; // Back the view with a layer created by the makeBackingLayer method.
// Enabling this will sync the rendering loop with the natural display link
// (monitor refresh rate, typically 60 fps). Disabling this will allow the
// rendering loop to run flat out, limited only by the rendering speed.
_useDisplayLink = YES;
// If this value is set to zero, the demo will render frames until the window is closed.
// If this value is not zero, it establishes a maximum number of frames that will be
// rendered, and once this count has been reached, the demo will stop rendering.
// Once rendering is finished, if _useDisplayLink is false, the demo will immediately
// clean up the Vulkan objects, or if _useDisplayLink is true, the demo will delay
// cleaning up Vulkan objects until the window is closed.
_maxFrameCount = 0;
VkPresentModeKHR vkPresentMode = _useDisplayLink ? VK_PRESENT_MODE_FIFO_KHR : VK_PRESENT_MODE_IMMEDIATE_KHR;
char vkPresentModeStr[64];
sprintf(vkPresentModeStr, "%d", vkPresentMode);
const char* argv[] = { "cube", "--present_mode", vkPresentModeStr };
int argc = sizeof(argv)/sizeof(char*);
demo_main(&demo, self.view.layer, argc, argv);
_stop = NO;
_frameCount = 0;
if (_useDisplayLink) {
CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
CVDisplayLinkSetOutputCallback(_displayLink, &DisplayLinkCallback, self);
CVDisplayLinkStart(_displayLink);
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
do {
demo_draw(&demo);
_stop = _stop || (_maxFrameCount && ++_frameCount >= _maxFrameCount);
} while( !_stop );
demo_cleanup(&demo);
});
}
}
-(void) viewDidDisappear {
_stop = YES;
if (_useDisplayLink) {
CVDisplayLinkRelease(_displayLink);
demo_cleanup(&demo);
}
[super viewDidDisappear];
}
#pragma mark Display loop callback function
/** Rendering loop callback function for use with a CVDisplayLink. */
static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink,
const CVTimeStamp* now,
const CVTimeStamp* outputTime,
CVOptionFlags flagsIn,
CVOptionFlags* flagsOut,
void* target) {
DemoViewController* demoVC =(DemoViewController*)target;
if ( !demoVC->_stop ) {
demo_draw(&demoVC->demo);
demoVC->_stop = (demoVC->_maxFrameCount && ++demoVC->_frameCount >= demoVC->_maxFrameCount);
}
return kCVReturnSuccess;
}
@end
#pragma mark -
#pragma mark DemoView
@implementation DemoView
/** Indicates that the view wants to draw using the backing layer instead of using drawRect:. */
-(BOOL) wantsUpdateLayer { return YES; }
/** Returns a Metal-compatible layer. */
+(Class) layerClass { return [CAMetalLayer class]; }
/** If the wantsLayer property is set to YES, this method will be invoked to return a layer instance. */
-(CALayer*) makeBackingLayer {
CALayer* layer = [self.class.layerClass layer];
CGSize viewScale = [self convertSizeToBacking: CGSizeMake(1.0, 1.0)];
layer.contentsScale = MIN(viewScale.width, viewScale.height);
return layer;
}
/**
* If this view moves to a screen that has a different resolution scale (eg. Standard <=> Retina),
* update the contentsScale of the layer, which will trigger a Vulkan VK_SUBOPTIMAL_KHR result, which
* causes this demo to replace the swapchain, in order to optimize rendering for the new resolution.
*/
-(BOOL) layer: (CALayer *)layer shouldInheritContentsScale: (CGFloat)newScale fromWindow: (NSWindow *)window {
if (newScale == layer.contentsScale) { return NO; }
layer.contentsScale = newScale;
return YES;
}
@end