mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-14 15:46:28 +00:00
val: Use the metal API to validate MSL shaders, if available
Roughly 4x faster than validating with the MSL executable. Change-Id: I6566fa29622475c459eb3a988a842a9e19d4be6f Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/53680 Kokoro: Kokoro <noreply+kokoro@google.com> Reviewed-by: Antonio Maiorano <amaiorano@google.com> Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
committed by
Tint LUCI CQ
parent
4634c8b736
commit
8abe369bc5
@@ -89,45 +89,5 @@ Result Hlsl(const std::string& dxc_path,
|
||||
return result;
|
||||
}
|
||||
|
||||
Result Msl(const std::string& xcrun_path, const std::string& source) {
|
||||
Result result;
|
||||
|
||||
auto xcrun = utils::Command(xcrun_path);
|
||||
if (!xcrun.Found()) {
|
||||
result.output = "xcrun not found at '" + std::string(xcrun_path) + "'";
|
||||
result.failed = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
result.source = source;
|
||||
|
||||
utils::TmpFile file(".metal");
|
||||
file << result.source;
|
||||
|
||||
#ifdef _WIN32
|
||||
// On Windows, we should actually be running metal.exe from the Metal
|
||||
// Developer Tools for Windows
|
||||
auto res = xcrun("-x", "metal", "-c", "-o", "NUL", file.Path());
|
||||
#else
|
||||
auto res =
|
||||
xcrun("-sdk", "macosx", "metal", "-o", "/dev/null", "-c", file.Path());
|
||||
#endif
|
||||
if (!res.out.empty()) {
|
||||
if (!result.output.empty()) {
|
||||
result.output += "\n";
|
||||
}
|
||||
result.output += res.out;
|
||||
}
|
||||
if (!res.err.empty()) {
|
||||
if (!result.output.empty()) {
|
||||
result.output += "\n";
|
||||
}
|
||||
result.output += res.err;
|
||||
}
|
||||
result.failed = (res.error_code != 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace val
|
||||
} // namespace tint
|
||||
66
src/val/msl.cc
Normal file
66
src/val/msl.cc
Normal file
@@ -0,0 +1,66 @@
|
||||
// Copyright 2021 The Tint Authors.
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include "src/val/val.h"
|
||||
|
||||
#include "src/ast/module.h"
|
||||
#include "src/program.h"
|
||||
#include "src/utils/io/command.h"
|
||||
#include "src/utils/io/tmpfile.h"
|
||||
|
||||
namespace tint {
|
||||
namespace val {
|
||||
|
||||
Result Msl(const std::string& xcrun_path, const std::string& source) {
|
||||
Result result;
|
||||
|
||||
auto xcrun = utils::Command(xcrun_path);
|
||||
if (!xcrun.Found()) {
|
||||
result.output = "xcrun not found at '" + std::string(xcrun_path) + "'";
|
||||
result.failed = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
result.source = source;
|
||||
|
||||
utils::TmpFile file(".metal");
|
||||
file << result.source;
|
||||
|
||||
#ifdef _WIN32
|
||||
// On Windows, we should actually be running metal.exe from the Metal
|
||||
// Developer Tools for Windows
|
||||
auto res = xcrun("-x", "metal", "-c", "-o", "NUL", file.Path());
|
||||
#else
|
||||
auto res =
|
||||
xcrun("-sdk", "macosx", "metal", "-o", "/dev/null", "-c", file.Path());
|
||||
#endif
|
||||
if (!res.out.empty()) {
|
||||
if (!result.output.empty()) {
|
||||
result.output += "\n";
|
||||
}
|
||||
result.output += res.out;
|
||||
}
|
||||
if (!res.err.empty()) {
|
||||
if (!result.output.empty()) {
|
||||
result.output += "\n";
|
||||
}
|
||||
result.output += res.err;
|
||||
}
|
||||
result.failed = (res.error_code != 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace val
|
||||
} // namespace tint
|
||||
58
src/val/msl_metal.mm
Normal file
58
src/val/msl_metal.mm
Normal file
@@ -0,0 +1,58 @@
|
||||
// Copyright 2021 The Tint Authors.
|
||||
//
|
||||
// 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.
|
||||
|
||||
#ifdef TINT_ENABLE_MSL_VALIDATION_USING_METAL_API
|
||||
|
||||
@import Metal;
|
||||
|
||||
// Disable: error: treating #include as an import of module 'std.string'
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wauto-import"
|
||||
#include "src/val/val.h"
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
namespace tint {
|
||||
namespace val {
|
||||
|
||||
Result MslUsingMetalAPI(const std::string& src) {
|
||||
tint::val::Result result;
|
||||
|
||||
NSError* error = nil;
|
||||
|
||||
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
|
||||
if (!device) {
|
||||
result.output = "MTLCreateSystemDefaultDevice returned null";
|
||||
result.failed = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
NSString* source = [NSString stringWithCString:src.c_str()
|
||||
encoding:NSUTF8StringEncoding];
|
||||
|
||||
id<MTLLibrary> library = [device newLibraryWithSource:source
|
||||
options:nil
|
||||
error:&error];
|
||||
if (!library) {
|
||||
NSString* output = [error localizedDescription];
|
||||
result.output = [output UTF8String];
|
||||
result.failed = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace val
|
||||
} // namespace tint
|
||||
|
||||
#endif // TINT_ENABLE_MSL_VALIDATION_USING_METAL_API
|
||||
@@ -52,6 +52,14 @@ Result Hlsl(const std::string& dxc_path,
|
||||
/// @return the result of the compile
|
||||
Result Msl(const std::string& xcrun_path, const std::string& source);
|
||||
|
||||
#ifdef TINT_ENABLE_MSL_VALIDATION_USING_METAL_API
|
||||
/// Msl attempts to compile the shader with the runtime Metal Shader Compiler
|
||||
/// API, verifying that the shader compiles successfully.
|
||||
/// @param source the generated MSL source
|
||||
/// @return the result of the compile
|
||||
Result MslUsingMetalAPI(const std::string& source);
|
||||
#endif // TINT_ENABLE_MSL_VALIDATION_USING_METAL_API
|
||||
|
||||
} // namespace val
|
||||
} // namespace tint
|
||||
|
||||
|
||||
Reference in New Issue
Block a user