Skip to content

Commit

Permalink
Merge branch 'main' into khr-acceleration-structures
Browse files Browse the repository at this point in the history
  • Loading branch information
AntarticCoder committed Jan 2, 2024
2 parents 3937822 + ad8b963 commit c688f5e
Show file tree
Hide file tree
Showing 55 changed files with 2,114 additions and 1,903 deletions.
7 changes: 5 additions & 2 deletions Common/MVKCommonEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ extern "C" {
* Compiler build setting that ensures a definite value for whether this
* build is a debug build or not.
*
* If the standard DEBUG build setting is defined, MVK_DEBUG is set to true,
* otherwise, it is set to false.
* If the standard DEBUG build setting is defined, MVK_CONFIG_DEBUG is
* set to true, otherwise, it is set to false.
*/
#ifndef MVK_DEBUG
# ifdef DEBUG
Expand All @@ -41,6 +41,9 @@ extern "C" {
# define MVK_DEBUG 0
# endif
#endif
#ifndef MVK_CONFIG_DEBUG
# define MVK_CONFIG_DEBUG MVK_DEBUG
#endif

/** Building for macOS. */
#ifndef MVK_MACOS
Expand Down
42 changes: 27 additions & 15 deletions Common/MVKOSExtensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <limits>


#pragma mark -
#pragma mark Operating System versions

typedef float MVKOSVersion;

/*** Constant indicating unsupported functionality in an OS. */
Expand Down Expand Up @@ -66,20 +69,31 @@ static inline bool mvkOSVersionIsAtLeast(MVKOSVersion macOSMinVer,
#endif
}


#pragma mark -
#pragma mark Timestamps

/**
* Returns a monotonic timestamp value for use in Vulkan and performance timestamping.
* Returns a monotonic tick value for use in Vulkan and performance timestamping.
*
* The returned value corresponds to the number of CPU "ticks" since the app was initialized.
*
* Calling this value twice, subtracting the first value from the second, and then multiplying
* the result by the value returned by mvkGetTimestampPeriod() will provide an indication of the
* number of nanoseconds between the two calls. The convenience function mvkGetElapsedMilliseconds()
* can be used to perform this calculation.
* The returned value corresponds to the number of CPU ticks since an arbitrary
* point in the past, and does not increment while the system is asleep.
*/
uint64_t mvkGetTimestamp();

/** Returns the number of nanoseconds between each increment of the value returned by mvkGetTimestamp(). */
double mvkGetTimestampPeriod();
/**
* Returns the number of runtime nanoseconds since an arbitrary point in the past,
* excluding any time spent while the system is asleep.
*
* This value corresponds to the timestamps returned by Metal presentation timings.
*/
uint64_t mvkGetRuntimeNanoseconds();

/**
* Returns the number of nanoseconds since an arbitrary point in the past,
* including any time spent while the system is asleep.
*/
uint64_t mvkGetContinuousNanoseconds();

/**
* Returns the number of nanoseconds elapsed between startTimestamp and endTimestamp,
Expand All @@ -97,12 +111,6 @@ uint64_t mvkGetElapsedNanoseconds(uint64_t startTimestamp = 0, uint64_t endTimes
*/
double mvkGetElapsedMilliseconds(uint64_t startTimestamp = 0, uint64_t endTimestamp = 0);

/** Returns the current absolute time in nanoseconds. */
uint64_t mvkGetAbsoluteTime();

/** Ensures the block is executed on the main thread. */
void mvkDispatchToMainAndWait(dispatch_block_t block);


#pragma mark -
#pragma mark Process environment
Expand Down Expand Up @@ -141,8 +149,12 @@ uint64_t mvkGetUsedMemorySize();
/** Returns the size of a page of host memory on this platform. */
uint64_t mvkGetHostMemoryPageSize();


#pragma mark -
#pragma mark Threading

/** Returns the amount of avaliable CPU cores. */
uint32_t mvkGetAvaliableCPUCores();

/** Ensures the block is executed on the main thread. */
void mvkDispatchToMainAndWait(dispatch_block_t block);
65 changes: 36 additions & 29 deletions Common/MVKOSExtensions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
#include <mach/mach_time.h>
#include <mach/task.h>
#include <os/proc.h>
#include <sys/sysctl.h>

#import <Foundation/Foundation.h>


using namespace std;


#pragma mark -
#pragma mark Operating System versions

MVKOSVersion mvkOSVersion() {
static MVKOSVersion _mvkOSVersion = 0;
if ( !_mvkOSVersion ) {
Expand All @@ -38,43 +43,35 @@ MVKOSVersion mvkOSVersion() {
return _mvkOSVersion;
}

static uint64_t _mvkTimestampBase;
static double _mvkTimestampPeriod;

#pragma mark -
#pragma mark Timestamps

static mach_timebase_info_data_t _mvkMachTimebase;

uint64_t mvkGetTimestamp() { return mach_absolute_time() - _mvkTimestampBase; }
uint64_t mvkGetTimestamp() { return mach_absolute_time(); }

double mvkGetTimestampPeriod() { return _mvkTimestampPeriod; }
uint64_t mvkGetRuntimeNanoseconds() { return mach_absolute_time() * _mvkMachTimebase.numer / _mvkMachTimebase.denom; }

uint64_t mvkGetContinuousNanoseconds() { return mach_continuous_time() * _mvkMachTimebase.numer / _mvkMachTimebase.denom; }

uint64_t mvkGetElapsedNanoseconds(uint64_t startTimestamp, uint64_t endTimestamp) {
if (endTimestamp == 0) { endTimestamp = mvkGetTimestamp(); }
return (endTimestamp - startTimestamp) * _mvkTimestampPeriod;
return (endTimestamp - startTimestamp) * _mvkMachTimebase.numer / _mvkMachTimebase.denom;
}

double mvkGetElapsedMilliseconds(uint64_t startTimestamp, uint64_t endTimestamp) {
return mvkGetElapsedNanoseconds(startTimestamp, endTimestamp) / 1e6;
}

uint64_t mvkGetAbsoluteTime() { return mach_continuous_time() * _mvkMachTimebase.numer / _mvkMachTimebase.denom; }

// Initialize timestamping capabilities on app startup.
//Called automatically when the framework is loaded and initialized.
// Initialize timestamp capabilities on app startup.
// Called automatically when the framework is loaded and initialized.
static bool _mvkTimestampsInitialized = false;
__attribute__((constructor)) static void MVKInitTimestamps() {
if (_mvkTimestampsInitialized ) { return; }
_mvkTimestampsInitialized = true;

_mvkTimestampBase = mach_absolute_time();
mach_timebase_info(&_mvkMachTimebase);
_mvkTimestampPeriod = (double)_mvkMachTimebase.numer / (double)_mvkMachTimebase.denom;
}

void mvkDispatchToMainAndWait(dispatch_block_t block) {
if (NSThread.isMainThread) {
block();
} else {
dispatch_sync(dispatch_get_main_queue(), block);
}
}


Expand Down Expand Up @@ -104,17 +101,12 @@ bool mvkGetEnvVar(const char* varName, string& evStr) {
#pragma mark System memory

uint64_t mvkGetSystemMemorySize() {
#if MVK_MACOS_OR_IOS_OR_VISIONOS
mach_msg_type_number_t host_size = HOST_BASIC_INFO_COUNT;
host_basic_info_data_t info;
if (host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&info, &host_size) == KERN_SUCCESS) {
return info.max_mem;
uint64_t host_memsize = 0;
size_t size = sizeof(host_memsize);
if (sysctlbyname("hw.memsize", &host_memsize, &size, NULL, 0) == KERN_SUCCESS) {
return host_memsize;
}
return 0;
#endif
#if MVK_TVOS
return 0;
#endif
}

uint64_t mvkGetAvailableMemorySize() {
Expand All @@ -138,17 +130,32 @@ uint64_t mvkGetUsedMemorySize() {
task_vm_info_data_t task_vm_info;
mach_msg_type_number_t task_size = TASK_VM_INFO_COUNT;
if (task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&task_vm_info, &task_size) == KERN_SUCCESS) {
return task_vm_info.phys_footprint;
#ifdef TASK_VM_INFO_REV3_COUNT // check for rev3 version of task_vm_info
if (task_size >= TASK_VM_INFO_REV3_COUNT) {
return task_vm_info.ledger_tag_graphics_footprint;
}
else
#endif
return task_vm_info.phys_footprint;
}
return 0;
}

uint64_t mvkGetHostMemoryPageSize() { return sysconf(_SC_PAGESIZE); }


#pragma mark -
#pragma mark Threading

/** Returns the amount of avaliable CPU cores. */
uint32_t mvkGetAvaliableCPUCores() {
return (uint32_t)[[NSProcessInfo processInfo] activeProcessorCount];
}

void mvkDispatchToMainAndWait(dispatch_block_t block) {
if (NSThread.isMainThread) {
block();
} else {
dispatch_sync(dispatch_get_main_queue(), block);
}
}
12 changes: 6 additions & 6 deletions Demos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ Table of Contents
*Cube*
------

The basic canonical *Cube* sample app from the
[*Vulkan-Tools* repository](https://github.com/KhronosGroup/Vulkan-Tools)
The basic canonical *Cube* sample app from the
[*Vulkan-Tools* repository](https://github.com/KhronosGroup/Vulkan-Tools)
is included in this **MoltenVK** package.

This demo renders a basic textured cube that spins in place.

The demo can be found in the `Cube` folder, and in the `Cube` group in the
The demo can be found in the `Cube` folder, and in the `Cube` group in the
*Xcode Project Navigator* in the `Demos.xcworkspace` *Xcode* workspace.

To run this demo, run the `Cube-iOS`, `Cube-tvOS`, or `Cube-macOS` *Scheme* from within *Xcode*.
To run this demo, run the `Cube-iOS`, `Cube-tvOS`, or `Cube-macOS` *Scheme* from within *Xcode*.
In addition to devices, this demo will also run on the `iOS Simulator` or `tvOS Simulator` destinations.

The `Cube` demo is a simple example of installing **MoltenVK** as an `XCFramework` that is
The `Cube` demo is a simple example of installing **MoltenVK** as an `XCFramework` that is
statically linked to the application. It supports all platforms, including _Mac Catalyst_, _iOS
Simulator_ and _tvOS Simulator_, and all architectures including _Apple Silicon_.

Expand All @@ -46,5 +46,5 @@ Simulator_ and _tvOS Simulator_, and all architectures including _Apple Silicon_
*Khronos Vulkan Samples*
----------------------

*Khronos Group* provides a [repository](https://github.com/KhronosGroup/Vulkan-Samples)
*Khronos Group* provides a [repository](https://github.com/KhronosGroup/Vulkan-Samples)
containing a full suite of standard *Vulkan* samples that run on **MoltenVK** on *macOS*.
Loading

0 comments on commit c688f5e

Please sign in to comment.