/* * 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 #include #include "../../Vulkan-Tools/cube/cube.c" #pragma mark - #pragma mark DemoViewController @implementation DemoViewController { CVDisplayLinkRef _displayLink; struct demo demo; } -(void) dealloc { demo_cleanup(&demo); CVDisplayLinkRelease(_displayLink); [super dealloc]; } /** Since this is a single-view app, initialize Vulkan during view loading. */ -(void) viewDidLoad { [super viewDidLoad]; 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 (60 fps). // Disabling this will allow the rendering loop to run flat out, limited only by the rendering speed. bool useDisplayLink = true; 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); if (useDisplayLink) { CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink); CVDisplayLinkSetOutputCallback(_displayLink, &DisplayLinkCallback, &demo); CVDisplayLinkStart(_displayLink); } else { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ while(true) { demo_draw(&demo); } }); } } #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) { demo_draw((struct demo*)target); 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