Fix CMakeLists.txt for vulkanExamples.xcodeproj build, fix macOS storyboard for resizable window, set animation rate based on display refresh period

This commit is contained in:
Stephen Saunders 2022-05-04 00:43:14 -04:00
parent d1975e91ee
commit b1f10d7393
7 changed files with 71 additions and 47 deletions

View file

@ -130,11 +130,11 @@ endif()
IF(MSVC) IF(MSVC)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
ELSEIF(APPLE) ELSEIF(APPLE)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") #if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fobjc-arc -ObjC++") # SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fobjc-arc -ObjC++")
ELSE() #ELSE()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fobjc-arc -xobjective-c++") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fobjc-arc -xobjective-c++")
ENDIF() #ENDIF()
ENDIF(MSVC) ENDIF(MSVC)
IF(WIN32) IF(WIN32)

View file

@ -243,7 +243,7 @@ void VulkanExampleBase::nextFrame()
auto tEnd = std::chrono::high_resolution_clock::now(); auto tEnd = std::chrono::high_resolution_clock::now();
auto tDiff = std::chrono::duration<double, std::milli>(tEnd - tStart).count(); auto tDiff = std::chrono::duration<double, std::milli>(tEnd - tStart).count();
#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)) #if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
frameTimer = (float)tDiff * refreshPeriod; // SRS - Multiply by the display refresh period due to fixed vsync rendering on iOS and macOS frameTimer = (float)tDiff * refreshPeriod; // SRS - Multiply by refresh period due to displayLink callback rendering on iOS and macOS
#else #else
frameTimer = (float)tDiff / 1000.0f; frameTimer = (float)tDiff / 1000.0f;
#endif #endif
@ -1515,6 +1515,10 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd)
@end @end
const std::string getAssetPath() {
return [NSBundle.mainBundle.resourcePath stringByAppendingString: @"/../../data/"].UTF8String;
}
static CVReturn displayLinkOutputCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow, static CVReturn displayLinkOutputCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow,
const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut,
void *displayLinkContext) void *displayLinkContext)
@ -1556,6 +1560,9 @@ static CVReturn displayLinkOutputCallback(CVDisplayLinkRef displayLink, const CV
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink); CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
CVDisplayLinkSetOutputCallback(displayLink, &displayLinkOutputCallback, vulkanExample); CVDisplayLinkSetOutputCallback(displayLink, &displayLinkOutputCallback, vulkanExample);
CVDisplayLinkStart(displayLink); CVDisplayLinkStart(displayLink);
// SRS - Pause 1 ms for displayLink startup then get the actual refresh period of the display
usleep(1000);
vulkanExample->refreshPeriod = CVDisplayLinkGetActualOutputVideoRefreshPeriod(displayLink);
} }
- (BOOL)acceptsFirstResponder - (BOOL)acceptsFirstResponder
@ -1633,22 +1640,44 @@ static CVReturn displayLinkOutputCallback(CVDisplayLinkRef displayLink, const CV
vulkanExample->mouseButtons.left = false; vulkanExample->mouseButtons.left = false;
} }
- (void)otherMouseDown:(NSEvent *)event - (void)rightMouseDown:(NSEvent *)event
{ {
vulkanExample->mouseButtons.right = true; vulkanExample->mouseButtons.right = true;
} }
- (void)otherMouseUp:(NSEvent *)event - (void)rightMouseUp:(NSEvent *)event
{ {
vulkanExample->mouseButtons.right = false; vulkanExample->mouseButtons.right = false;
} }
- (void)otherMouseDown:(NSEvent *)event
{
vulkanExample->mouseButtons.middle = true;
}
- (void)otherMouseUp:(NSEvent *)event
{
vulkanExample->mouseButtons.middle = false;
}
- (void)mouseDragged:(NSEvent *)event - (void)mouseDragged:(NSEvent *)event
{ {
auto point = [self getMouseLocalPoint:event]; auto point = [self getMouseLocalPoint:event];
vulkanExample->mouseDragged(point.x, point.y); vulkanExample->mouseDragged(point.x, point.y);
} }
- (void)rightMouseDragged:(NSEvent *)event
{
auto point = [self getMouseLocalPoint:event];
vulkanExample->mouseDragged(point.x, point.y);
}
- (void)otherMouseDragged:(NSEvent *)event
{
auto point = [self getMouseLocalPoint:event];
vulkanExample->mouseDragged(point.x, point.y);
}
- (void)mouseMoved:(NSEvent *)event - (void)mouseMoved:(NSEvent *)event
{ {
auto point = [self getMouseLocalPoint:event]; auto point = [self getMouseLocalPoint:event];

View file

@ -11,6 +11,18 @@ function(buildExample EXAMPLE_NAME)
if(EXISTS ${EXAMPLE_FOLDER}/${EXAMPLE_NAME}.h) if(EXISTS ${EXAMPLE_FOLDER}/${EXAMPLE_NAME}.h)
SET(MAIN_HEADER ${EXAMPLE_FOLDER}/${EXAMPLE_NAME}.h) SET(MAIN_HEADER ${EXAMPLE_FOLDER}/${EXAMPLE_NAME}.h)
ENDIF() ENDIF()
if(APPLE)
if(CMAKE_C_COMPILER_ID MATCHES "Clang\$")
set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp")
set(OpenMP_C_LIB_NAMES "omp")
set(OpenMP_omp_LIBRARY /opt/local/lib/libomp/libomp.dylib)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang\$")
set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp")
set(OpenMP_CXX_LIB_NAMES "omp")
set(OpenMP_omp_LIBRARY /opt/local/lib/libomp/libomp.dylib)
endif()
endif()
find_package(OpenMP) find_package(OpenMP)
# imgui example requires additional source files # imgui example requires additional source files
IF(${EXAMPLE_NAME} STREQUAL "imgui") IF(${EXAMPLE_NAME} STREQUAL "imgui")

View file

@ -10,28 +10,14 @@
#include "MVKExample.h" #include "MVKExample.h"
#include "examples.h" #include "examples.h"
/*
void MVKExample::renderFrame() { // SRS - don't need to expose VulkanExampleBase::renderFrame() to DemoViewController
_vulkanExample->renderFrame();
}
*/
void MVKExample::displayLinkOutputCb() { // SRS - expose VulkanExampleBase::displayLinkOutputCb() to DemoViewController void MVKExample::displayLinkOutputCb() { // SRS - expose VulkanExampleBase::displayLinkOutputCb() to DemoViewController
_vulkanExample->displayLinkOutputCb(); _vulkanExample->displayLinkOutputCb();
} }
void MVKExample::getRefreshPeriod(double refreshPeriod) { // SRS - get the actual refresh period of the display void MVKExample::setRefreshPeriod(double refreshPeriod) { // SRS - set VulkanExampleBase::refreshPeriod from DemoViewController displayLink
_vulkanExample->refreshPeriod = refreshPeriod; _vulkanExample->refreshPeriod = refreshPeriod;
} }
void MVKExample::windowWillResize(float x, float y) {
_vulkanExample->windowWillResize(x, y);
}
void MVKExample::windowDidResize() {
_vulkanExample->windowDidResize();
}
void MVKExample::keyPressed(uint32_t keyCode) { void MVKExample::keyPressed(uint32_t keyCode) {
_vulkanExample->keyPressed(keyCode); _vulkanExample->keyPressed(keyCode);
} }
@ -119,10 +105,12 @@ void MVKExample::scrollWheel(short wheelDelta) {
MVKExample::MVKExample(void* view) { MVKExample::MVKExample(void* view) {
_vulkanExample = new VulkanExample(); _vulkanExample = new VulkanExample();
width = _vulkanExample->width; // SRS - Get desired window size from VulkanExampleBase instance
height = _vulkanExample->height;
_vulkanExample->initVulkan(); _vulkanExample->initVulkan();
_vulkanExample->setupWindow(view); _vulkanExample->setupWindow(view);
//_vulkanExample->initSwapchain(); // SRS - initSwapchain() is now part of VulkanExampleBase::prepare()
_vulkanExample->prepare(); _vulkanExample->prepare();
_vulkanExample->renderLoop(); // SRS - Init destWidth & destHeight, then fall through and return
} }
MVKExample::~MVKExample() { MVKExample::~MVKExample() {

View file

@ -13,12 +13,11 @@
class MVKExample { class MVKExample {
public: public:
//void renderFrame(); // SRS - don't need to expose VulkanExampleBase::renderFrame() to DemoViewController uint32_t width; // SRS - expose VulkanExampleBase initial window size to DemoViewController
void displayLinkOutputCb(); // SRS - expose VulkanExampleBase::displayLinkOutputCb() to DemoViewController uint32_t height;
void getRefreshPeriod(double refreshPeriod); // SRS - get the actual refresh period of the display
void windowWillResize(float x, float y); // SRS - expose window resize events to DemoViewController void displayLinkOutputCb(); // SRS - expose VulkanExampleBase::displayLinkOutputCb() to DemoViewController
void windowDidResize(); void setRefreshPeriod(double refreshPeriod); // SRS - set VulkanExampleBase::refreshPeriod from DemoViewController displayLink
void keyPressed(uint32_t keyCode); // SRS - expose keyboard events to DemoViewController void keyPressed(uint32_t keyCode); // SRS - expose keyboard events to DemoViewController
void keyDown(uint32_t keyCode); void keyDown(uint32_t keyCode);

View file

@ -22,7 +22,7 @@ static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink,
CVOptionFlags* flagsOut, CVOptionFlags* flagsOut,
void* target) { void* target) {
//((MVKExample*)target)->renderFrame(); //((MVKExample*)target)->renderFrame();
((MVKExample*)target)->displayLinkOutputCb(); // SRS - Call MVKExample::displayLinkOutputCb() to animate frames vs. MVKExample::renderFrame() for static image ((MVKExample*)target)->displayLinkOutputCb(); // SRS - Call displayLinkOutputCb() to animate frames vs. renderFrame() for static image
return kCVReturnSuccess; return kCVReturnSuccess;
} }
@ -48,23 +48,18 @@ MVKExample* _mvkExample;
CVDisplayLinkStart(_displayLink); CVDisplayLinkStart(_displayLink);
} }
// SRS - get the actual refresh period of the display // SRS - Set window's initial content size and set VulkanExampleBase::refreshPeriod from the displayLink
-(void) viewWillAppear { -(void) viewWillAppear {
[super viewWillAppear]; [super viewWillAppear];
_mvkExample->getRefreshPeriod(CVDisplayLinkGetActualOutputVideoRefreshPeriod(_displayLink)); NSWindow* window = self.view.window;
} NSSize viewSize;
viewSize.width = _mvkExample->width;
viewSize.height = _mvkExample->height;
[window setContentSize:viewSize];
[window center];
// SRS - Handle window resize events (FIXME: resizeable window not yet supported at init) _mvkExample->setRefreshPeriod(CVDisplayLinkGetActualOutputVideoRefreshPeriod(_displayLink));
-(NSSize) windowWillResize:(NSWindow*) sender toSize:(NSSize)frameSize {
CVDisplayLinkStop(_displayLink);
_mvkExample->windowWillResize(frameSize.width, frameSize.height);
return frameSize;
}
-(void) windowDidResize:(NSNotification*) notification {
_mvkExample->windowDidResize();
CVDisplayLinkStart(_displayLink);
} }
-(void) dealloc { -(void) dealloc {

View file

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS"> <document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="19529" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies> <dependencies>
<deployment identifier="macosx"/> <deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12118"/> <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="19529"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies> </dependencies>
<scenes> <scenes>
@ -101,12 +101,13 @@
<scene sceneID="R2V-B0-nI4"> <scene sceneID="R2V-B0-nI4">
<objects> <objects>
<windowController id="B8D-0N-5wS" sceneMemberID="viewController"> <windowController id="B8D-0N-5wS" sceneMemberID="viewController">
<window key="window" title="MoltenVK Vulkan Example" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" animationBehavior="default" id="IQv-IB-iLA"> <window key="window" title="MoltenVK Vulkan Example" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="IQv-IB-iLA">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/> <windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<rect key="contentRect" x="1051" y="656" width="1024" height="768"/> <rect key="contentRect" x="1051" y="656" width="1024" height="768"/>
<rect key="screenRect" x="0.0" y="0.0" width="2560" height="1417"/> <rect key="screenRect" x="0.0" y="0.0" width="2560" height="1417"/>
<value key="minSize" type="size" width="1024" height="768"/> <connections>
<value key="maxSize" type="size" width="1024" height="768"/> <outlet property="delegate" destination="B8D-0N-5wS" id="ayw-fB-DBj"/>
</connections>
</window> </window>
<connections> <connections>
<segue destination="XfG-lQ-9wD" kind="relationship" relationship="window.shadowedContentViewController" id="cq2-FE-JQM"/> <segue destination="XfG-lQ-9wD" kind="relationship" relationship="window.shadowedContentViewController" id="cq2-FE-JQM"/>