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
|
|
|
*
|
2019-01-01 21:13:25 -05:00
|
|
|
* Copyright (c) 2014-2019 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"
|
|
|
|
|
|
|
|
#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;
|
|
|
|
if (mslVer(2, 1, 0)) {
|
|
|
|
if (@available(macOS 10.14, *)) {
|
|
|
|
mslVerEnum = MTLLanguageVersion2_1;
|
|
|
|
}
|
|
|
|
} else if (mslVer(2, 0, 0)) {
|
|
|
|
if (@available(macOS 10.13, *)) {
|
|
|
|
mslVerEnum = MTLLanguageVersion2_0;
|
|
|
|
}
|
|
|
|
} 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 {
|
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;
|
|
|
|
id<MTLLibrary> mtlLib = [[MTLCreateSystemDefaultDevice() newLibraryWithSource: @(mslSourceCode.c_str())
|
2019-04-17 16:09:07 -04:00
|
|
|
options: mtlCompileOptions
|
2019-03-22 19:36:21 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|