2017-11-17 11:14:29 -05:00
|
|
|
/*
|
|
|
|
* DemoViewController.m
|
|
|
|
*
|
2023-01-13 12:19:37 -05:00
|
|
|
* Copyright (c) 2015-2023 The Brenwill Workshop Ltd. (http://www.brenwill.com)
|
2017-11-17 11:14:29 -05:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2019-01-27 19:52:59 -05:00
|
|
|
#include <MoltenVK/mvk_vulkan.h>
|
2018-07-31 13:10:44 -04:00
|
|
|
#include "../../Vulkan-Tools/cube/cube.c"
|
2017-11-17 11:14:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
#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.
|
2019-07-16 13:02:37 -04:00
|
|
|
|
2020-06-23 12:50:40 -04:00
|
|
|
const char* argv[] = { "cube" };
|
|
|
|
int argc = sizeof(argv)/sizeof(char*);
|
|
|
|
demo_main(&demo, self.view.layer, argc, argv);
|
2017-11-17 11:14:29 -05:00
|
|
|
|
|
|
|
CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
|
|
|
|
CVDisplayLinkSetOutputCallback(_displayLink, &DisplayLinkCallback, &demo);
|
|
|
|
CVDisplayLinkStart(_displayLink);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#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) {
|
2018-04-05 11:15:54 -04:00
|
|
|
demo_draw((struct demo*)target);
|
2017-11-17 11:14:29 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:33:10 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2017-11-17 11:14:29 -05:00
|
|
|
@end
|