2018-01-08 21:44:46 -05:00
|
|
|
/*
|
2019-03-22 19:36:21 -04:00
|
|
|
* OSSupport.mm
|
2018-01-08 21:44:46 -05:00
|
|
|
*
|
2024-01-04 14:51:53 -05:00
|
|
|
* Copyright (c) 2015-2024 The Brenwill Workshop Ltd. (http://www.brenwill.com)
|
2018-01-08 21:44:46 -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.
|
|
|
|
*/
|
|
|
|
|
2019-03-22 19:36:21 -04:00
|
|
|
#include "OSSupport.h"
|
2018-01-08 21:44:46 -05:00
|
|
|
#include "FileSupport.h"
|
|
|
|
#include "MoltenVKShaderConverterTool.h"
|
2020-06-11 12:43:38 -04:00
|
|
|
#include "MVKOSExtensions.h"
|
2018-01-08 21:44:46 -05:00
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
2019-03-22 19:36:21 -04:00
|
|
|
#import <Metal/Metal.h>
|
2018-01-08 21:44:46 -05:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace mvk;
|
|
|
|
|
|
|
|
template <typename FileProcessor>
|
|
|
|
bool mvk::iterateDirectory(const string& dirPath,
|
2019-01-16 13:36:43 -05:00
|
|
|
FileProcessor& fileProcessor,
|
|
|
|
bool isRecursive,
|
|
|
|
string& errMsg) {
|
2018-01-08 21:44:46 -05:00
|
|
|
NSString* nsAbsDirPath = @(absolutePath(dirPath).data());
|
|
|
|
NSFileManager* fileMgr = NSFileManager.defaultManager;
|
|
|
|
BOOL isDir = false;
|
|
|
|
BOOL exists = [fileMgr fileExistsAtPath: nsAbsDirPath isDirectory: &isDir];
|
|
|
|
if ( !exists ) {
|
|
|
|
errMsg = "Could not locate directory: " + absolutePath(dirPath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( !isDir ) {
|
|
|
|
errMsg = absolutePath(dirPath) + " is not a directory.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:36:43 -05:00
|
|
|
bool success = true;
|
2018-01-08 21:44:46 -05:00
|
|
|
NSDirectoryEnumerator* dirEnum = [fileMgr enumeratorAtPath: nsAbsDirPath];
|
|
|
|
NSString* filePath;
|
|
|
|
while ((filePath = dirEnum.nextObject)) {
|
|
|
|
if ( !isRecursive ) { [dirEnum skipDescendants]; }
|
|
|
|
NSString* absFilePath = [nsAbsDirPath stringByAppendingPathComponent: filePath];
|
2019-01-16 13:36:43 -05:00
|
|
|
if( !fileProcessor.processFile(absFilePath.UTF8String) ) { success = false; }
|
2018-01-08 21:44:46 -05:00
|
|
|
}
|
2019-01-16 13:36:43 -05:00
|
|
|
return success;
|
2018-01-08 21:44:46 -05:00
|
|
|
}
|
|
|
|
|
2019-04-17 16:09:07 -04:00
|
|
|
// Concrete template implementation to allow MoltenVKShaderConverterTool to iterate the files in a directory.
|
2018-01-08 21:44:46 -05:00
|
|
|
template bool mvk::iterateDirectory<MoltenVKShaderConverterTool>(const string& dirPath,
|
|
|
|
MoltenVKShaderConverterTool& fileProcessor,
|
|
|
|
bool isRecursive,
|
|
|
|
string& errMsg);
|
|
|
|
|
2019-04-17 16:09:07 -04:00
|
|
|
bool mvk::compile(const string& mslSourceCode,
|
|
|
|
string& errMsg,
|
|
|
|
uint32_t mslVersionMajor,
|
|
|
|
uint32_t mslVersionMinor,
|
|
|
|
uint32_t mslVersionPoint) {
|
|
|
|
|
|
|
|
#define mslVer(MJ, MN, PT) mslVersionMajor == MJ && mslVersionMinor == MN && mslVersionPoint == PT
|
|
|
|
|
|
|
|
MTLLanguageVersion mslVerEnum = (MTLLanguageVersion)0;
|
2023-07-05 13:20:59 -04:00
|
|
|
#if MVK_XCODE_15
|
|
|
|
if (mslVer(3, 1, 0)) {
|
|
|
|
mslVerEnum = MTLLanguageVersion3_1;
|
|
|
|
} else
|
|
|
|
#endif
|
Support Xcode 14, macOS 13, and iOS/tvOS 16.
- Update minimum Xcode deployment targets to macOS 10.13, iOS 11, and tvOS 11,
to avoid Xcode build warnings.
- Add support for MTLLanguageVersion3_0 enumeration.
- Build efficiencies:
- Build scripts create_dylib.sh and gen_moltenvk_rev_hdr.sh
only run if build dependencies require it.
- Packaging and copy_to_staging.sh scripts are too complex to define dependencies,
and are fast, so configured to run every time, to avoid build warning.
- Replace use of deprecated sprintf() with snprintf().
- Replace use of deprecated kIOMasterPortDefault with kIOMainPortDefault.
- Support old-style GPU debug capture only if building for earlier minimum
deployment targets, to avoid deprecation warning.
- Update minimum Xcode deployment targets of Cube demo to macOS 10.14, iOS 12,
and tvOS 12, to avoid Xcode build warning regarding MTLSharedEvent in .
- Update README.md document regarding minimum Xcode deployment targets.
2022-07-06 18:15:10 -04:00
|
|
|
#if MVK_XCODE_14
|
|
|
|
if (mslVer(3, 0, 0)) {
|
|
|
|
mslVerEnum = MTLLanguageVersion3_0;
|
|
|
|
} else
|
|
|
|
#endif
|
2021-12-01 18:14:07 -05:00
|
|
|
#if MVK_XCODE_13
|
|
|
|
if (mslVer(2, 4, 0)) {
|
|
|
|
mslVerEnum = MTLLanguageVersion2_4;
|
|
|
|
} else
|
|
|
|
#endif
|
2021-01-28 22:16:04 -06:00
|
|
|
#if MVK_XCODE_12
|
2021-01-25 13:08:41 -05:00
|
|
|
if (mslVer(2, 3, 0)) {
|
2021-01-25 21:40:29 -05:00
|
|
|
mslVerEnum = MTLLanguageVersion2_3;
|
2021-01-28 22:16:04 -06:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (mslVer(2, 2, 0)) {
|
2021-01-25 21:40:29 -05:00
|
|
|
mslVerEnum = MTLLanguageVersion2_2;
|
2021-01-25 13:08:41 -05:00
|
|
|
} else if (mslVer(2, 1, 0)) {
|
2020-06-11 12:43:38 -04:00
|
|
|
mslVerEnum = MTLLanguageVersion2_1;
|
2019-04-17 16:09:07 -04:00
|
|
|
} else if (mslVer(2, 0, 0)) {
|
2020-06-11 12:43:38 -04:00
|
|
|
mslVerEnum = MTLLanguageVersion2_0;
|
2019-04-17 16:09:07 -04:00
|
|
|
} else if (mslVer(1, 2, 0)) {
|
|
|
|
mslVerEnum = MTLLanguageVersion1_2;
|
|
|
|
} else if (mslVer(1, 1, 0)) {
|
|
|
|
mslVerEnum = MTLLanguageVersion1_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !mslVerEnum ) {
|
|
|
|
errMsg = [NSString stringWithFormat: @"%d.%d.%d is not a valid MSL version number on this device",
|
|
|
|
mslVersionMajor, mslVersionMinor, mslVersionPoint].UTF8String;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-22 19:36:21 -04:00
|
|
|
@autoreleasepool {
|
2022-01-18 12:37:59 -05:00
|
|
|
NSArray* mtlDevs = [MTLCopyAllDevices() autorelease];
|
|
|
|
if (mtlDevs.count == 0) {
|
|
|
|
errMsg = "Could not retrieve MTLDevice to compile shader.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-17 16:09:07 -04:00
|
|
|
MTLCompileOptions* mtlCompileOptions = [[MTLCompileOptions new] autorelease];
|
|
|
|
mtlCompileOptions.languageVersion = mslVerEnum;
|
2019-03-22 19:36:21 -04:00
|
|
|
NSError* err = nil;
|
2022-01-18 12:37:59 -05:00
|
|
|
id<MTLLibrary> mtlLib = [[mtlDevs[0] newLibraryWithSource: @(mslSourceCode.c_str())
|
|
|
|
options: mtlCompileOptions
|
|
|
|
error: &err] autorelease];
|
2019-04-17 16:09:07 -04:00
|
|
|
errMsg = err ? [NSString stringWithFormat: @"(Error code %li):\n%@", (long)err.code, err.localizedDescription].UTF8String : "";
|
2019-03-22 19:36:21 -04:00
|
|
|
return !!mtlLib;
|
|
|
|
}
|
|
|
|
}
|