From 036a5c8da0d0c595bc0cc53e11496160e4c9ad63 Mon Sep 17 00:00:00 2001 From: Jake Turner Date: Sat, 12 Mar 2022 05:49:49 +0000 Subject: [PATCH] Added wrapped MTLLibrary & MTLFunction Not currently created or used --- renderdoc/driver/metal/CMakeLists.txt | 6 + renderdoc/driver/metal/metal_function.cpp | 33 ++++ renderdoc/driver/metal/metal_function.h | 41 +++++ .../driver/metal/metal_function_bridge.mm | 118 ++++++++++++++ renderdoc/driver/metal/metal_library.cpp | 34 ++++ renderdoc/driver/metal/metal_library.h | 41 +++++ .../driver/metal/metal_library_bridge.mm | 152 ++++++++++++++++++ renderdoc/driver/metal/metal_types.h | 7 +- renderdoc/driver/metal/metal_types_bridge.h | 1 + renderdoc/driver/metal/metal_types_bridge.mm | 1 + 10 files changed, 432 insertions(+), 2 deletions(-) create mode 100644 renderdoc/driver/metal/metal_function.cpp create mode 100644 renderdoc/driver/metal/metal_function.h create mode 100644 renderdoc/driver/metal/metal_function_bridge.mm create mode 100644 renderdoc/driver/metal/metal_library.cpp create mode 100644 renderdoc/driver/metal/metal_library.h create mode 100644 renderdoc/driver/metal/metal_library_bridge.mm diff --git a/renderdoc/driver/metal/CMakeLists.txt b/renderdoc/driver/metal/CMakeLists.txt index fb7e1e38e..30577cad1 100644 --- a/renderdoc/driver/metal/CMakeLists.txt +++ b/renderdoc/driver/metal/CMakeLists.txt @@ -12,6 +12,12 @@ set(sources metal_types_bridge.mm metal_types_bridge.h metal_types.h + metal_library.cpp + metal_library.h + metal_library_bridge.mm + metal_function.cpp + metal_function.h + metal_function_bridge.mm metal_common.h official/metal-cpp.h official/metal-cpp.cpp) diff --git a/renderdoc/driver/metal/metal_function.cpp b/renderdoc/driver/metal/metal_function.cpp new file mode 100644 index 000000000..b08bc1a92 --- /dev/null +++ b/renderdoc/driver/metal/metal_function.cpp @@ -0,0 +1,33 @@ +/****************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2022 Baldur Karlsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ + +#include "metal_function.h" +#include "metal_device.h" + +WrappedMTLFunction::WrappedMTLFunction(MTL::Function *realMTLFunction, ResourceId objId, + WrappedMTLDevice *wrappedMTLDevice) + : WrappedMTLObject(realMTLFunction, objId, wrappedMTLDevice, wrappedMTLDevice->GetStateRef()) +{ + wrappedObjC = AllocateObjCWrapper(this); +} diff --git a/renderdoc/driver/metal/metal_function.h b/renderdoc/driver/metal/metal_function.h new file mode 100644 index 000000000..d13ababed --- /dev/null +++ b/renderdoc/driver/metal/metal_function.h @@ -0,0 +1,41 @@ +/****************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2022 Baldur Karlsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ + +#pragma once + +#include "metal_common.h" + +class WrappedMTLFunction : public WrappedMTLObject +{ +public: + WrappedMTLFunction(MTL::Function *realMTLFunction, ResourceId objId, + WrappedMTLDevice *wrappedMTLDevice); + + enum + { + TypeEnum = eResFunction + }; + +private: +}; diff --git a/renderdoc/driver/metal/metal_function_bridge.mm b/renderdoc/driver/metal/metal_function_bridge.mm new file mode 100644 index 000000000..49b71c772 --- /dev/null +++ b/renderdoc/driver/metal/metal_function_bridge.mm @@ -0,0 +1,118 @@ +/****************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2022 Baldur Karlsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ + +#include "metal_function.h" +#include "metal_types_bridge.h" + +// Wrapper for MTLFunction +@implementation ObjCWrappedMTLFunction + +// ObjCWrappedMTLFunction specific +- (id)real +{ + MTL::Function *real = Unwrap(self.wrappedCPP); + return id(real); +} + +- (void)dealloc +{ + self.wrappedCPP->Dealloc(); + [super dealloc]; +} + +// MTLFunction : based on the protocol defined in +// Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/Metal.framework/Headers/MTLLibrrary.h + +- (NSString *)label API_AVAILABLE(macos(10.12), ios(10.0)) +{ + return self.real.label; +} + +- (void)setLabel:value +{ + self.real.label = value; +} + +- (id)device +{ + return id(self.wrappedCPP->GetObjCWrappedMTLDevice()); +} + +- (MTLFunctionType)functionType +{ + return self.real.functionType; +} + +- (MTLPatchType)patchType API_AVAILABLE(macos(10.12), ios(10.0)) +{ + return self.real.patchType; +} + +- (NSInteger)patchControlPointCount API_AVAILABLE(macos(10.12), ios(10.0)) +{ + return self.real.patchControlPointCount; +} + +- (NSArray *)vertexAttributes +{ + return self.real.vertexAttributes; +} + +- (NSArray *)stageInputAttributes API_AVAILABLE(macos(10.12), ios(10.0)) +{ + return self.real.stageInputAttributes; +} + +- (NSString *)name +{ + return self.real.name; +} + +- (NSDictionary *)functionConstantsDictionary + API_AVAILABLE(macos(10.12), ios(10.0)) +{ + return self.real.functionConstantsDictionary; +} + +- (id)newArgumentEncoderWithBufferIndex:(NSUInteger)bufferIndex + API_AVAILABLE(macos(10.13), ios(11.0)) +{ + METAL_NOT_HOOKED(); + return [self.real newArgumentEncoderWithBufferIndex:bufferIndex]; +} + +- (id)newArgumentEncoderWithBufferIndex:(NSUInteger)bufferIndex + reflection:(MTLAutoreleasedArgument *__nullable)reflection + API_AVAILABLE(macos(10.13), ios(11.0)) +{ + METAL_NOT_HOOKED(); + return [self.real newArgumentEncoderWithBufferIndex:bufferIndex reflection:reflection]; +} + +- (MTLFunctionOptions)options API_AVAILABLE(macos(11.0), ios(14.0)) +{ + return self.real.options; +} + +@end diff --git a/renderdoc/driver/metal/metal_library.cpp b/renderdoc/driver/metal/metal_library.cpp new file mode 100644 index 000000000..5fde072d0 --- /dev/null +++ b/renderdoc/driver/metal/metal_library.cpp @@ -0,0 +1,34 @@ +/****************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2022 Baldur Karlsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ + +#include "metal_library.h" +#include "metal_device.h" +#include "metal_function.h" + +WrappedMTLLibrary::WrappedMTLLibrary(MTL::Library *realMTLLibrary, ResourceId objId, + WrappedMTLDevice *wrappedMTLDevice) + : WrappedMTLObject(realMTLLibrary, objId, wrappedMTLDevice, wrappedMTLDevice->GetStateRef()) +{ + wrappedObjC = AllocateObjCWrapper(this); +} diff --git a/renderdoc/driver/metal/metal_library.h b/renderdoc/driver/metal/metal_library.h new file mode 100644 index 000000000..fb4fe37e8 --- /dev/null +++ b/renderdoc/driver/metal/metal_library.h @@ -0,0 +1,41 @@ +/****************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2022 Baldur Karlsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ + +#pragma once + +#include "metal_common.h" + +class WrappedMTLLibrary : public WrappedMTLObject +{ +public: + WrappedMTLLibrary(MTL::Library *realMTLLibrary, ResourceId objId, + WrappedMTLDevice *wrappedMTLDevice); + + enum + { + TypeEnum = eResLibrary + }; + +private: +}; diff --git a/renderdoc/driver/metal/metal_library_bridge.mm b/renderdoc/driver/metal/metal_library_bridge.mm new file mode 100644 index 000000000..af8fb2ae1 --- /dev/null +++ b/renderdoc/driver/metal/metal_library_bridge.mm @@ -0,0 +1,152 @@ +/****************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2022 Baldur Karlsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ + +#include "metal_library.h" +#include "metal_types_bridge.h" + +// Wrapper for MTLLibrary +@implementation ObjCWrappedMTLLibrary + +// ObjCWrappedMTLLibrary specific +- (id)real +{ + MTL::Library *real = Unwrap(self.wrappedCPP); + return id(real); +} + +// Use the real MTLLibrary to find methods from messages +- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector +{ + id fwd = self.real; + return [fwd methodSignatureForSelector:aSelector]; +} + +// Forward any unknown messages to the real MTLLibrary +- (void)forwardInvocation:(NSInvocation *)invocation +{ + SEL aSelector = [invocation selector]; + + if([self.real respondsToSelector:aSelector]) + [invocation invokeWithTarget:self.real]; + else + [super forwardInvocation:invocation]; +} + +// MTLLibrary : based on the protocol defined in +// Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/Metal.framework/Headers/MTLLibrrary.h + +- (NSString *)label +{ + return self.real.label; +} + +- (void)setLabel:value +{ + self.real.label = value; +} + +- (id)device +{ + return id(self.wrappedCPP->GetObjCWrappedMTLDevice()); +} + +- (nullable id)newFunctionWithName:(NSString *)functionName +{ + METAL_NOT_HOOKED(); + return [self.real newFunctionWithName:functionName]; +} + +- (nullable id)newFunctionWithName:(NSString *)name + constantValues:(MTLFunctionConstantValues *)constantValues + error:(__autoreleasing NSError **)error + API_AVAILABLE(macos(10.12), ios(10.0)) +{ + METAL_NOT_HOOKED(); + return [self.real newFunctionWithName:name constantValues:constantValues error:error]; +} + +- (void)newFunctionWithName:(NSString *)name + constantValues:(MTLFunctionConstantValues *)constantValues + completionHandler:(void (^)(id __nullable function, + NSError *__nullable error))completionHandler + API_AVAILABLE(macos(10.12), ios(10.0)) +{ + METAL_NOT_HOOKED(); + return [self.real newFunctionWithName:name + constantValues:constantValues + completionHandler:completionHandler]; +} + +- (void)newFunctionWithDescriptor:(nonnull MTLFunctionDescriptor *)descriptor + completionHandler:(void (^)(id __nullable function, + NSError *__nullable error))completionHandler + API_AVAILABLE(macos(11.0), ios(14.0)) +{ + METAL_NOT_HOOKED(); + return [self.real newFunctionWithDescriptor:descriptor completionHandler:completionHandler]; +} + +- (nullable id)newFunctionWithDescriptor:(nonnull MTLFunctionDescriptor *)descriptor + error:(__autoreleasing NSError **)error + API_AVAILABLE(macos(11.0), ios(14.0)) +{ + METAL_NOT_HOOKED(); + return [self.real newFunctionWithDescriptor:descriptor error:error]; +} + +- (void)newIntersectionFunctionWithDescriptor:(nonnull MTLIntersectionFunctionDescriptor *)descriptor + completionHandler:(void (^)(id __nullable function, + NSError *__nullable error))completionHandler + API_AVAILABLE(macos(11.0), ios(14.0)) +{ + METAL_NOT_HOOKED(); + return [self.real newIntersectionFunctionWithDescriptor:descriptor + completionHandler:completionHandler]; +} + +- (nullable id)newIntersectionFunctionWithDescriptor: + (nonnull MTLIntersectionFunctionDescriptor *)descriptor + error:(__autoreleasing NSError **)error + API_AVAILABLE(macos(11.0), ios(14.0)) +{ + METAL_NOT_HOOKED(); + return [self.real newIntersectionFunctionWithDescriptor:descriptor error:error]; +} + +- (NSArray *)functionNames +{ + return self.real.functionNames; +} + +- (MTLLibraryType)type API_AVAILABLE(macos(11.0), ios(14.0)) +{ + return self.real.type; +} + +- (NSString *)installName API_AVAILABLE(macos(11.0), ios(14.0)) +{ + return self.real.installName; +} + +@end diff --git a/renderdoc/driver/metal/metal_types.h b/renderdoc/driver/metal/metal_types.h index 0fe3d51b0..3627cbffe 100644 --- a/renderdoc/driver/metal/metal_types.h +++ b/renderdoc/driver/metal/metal_types.h @@ -24,9 +24,12 @@ #pragma once -#include "official/metal-cpp.h" +#include "metal_common.h" -#define METALCPP_WRAPPED_PROTOCOLS(FUNC) FUNC(Device); +#define METALCPP_WRAPPED_PROTOCOLS(FUNC) \ + FUNC(Device); \ + FUNC(Function); \ + FUNC(Library); #define DECLARE_OBJC_HELPERS(CPPTYPE) \ class WrappedMTL##CPPTYPE; \ diff --git a/renderdoc/driver/metal/metal_types_bridge.h b/renderdoc/driver/metal/metal_types_bridge.h index 1c96effd9..8a081fbc7 100644 --- a/renderdoc/driver/metal/metal_types_bridge.h +++ b/renderdoc/driver/metal/metal_types_bridge.h @@ -27,6 +27,7 @@ #include "metal_types.h" #import +#import // clang-format off #define DECLARE_OBJC_WRAPPED_INTERFACES(CPPTYPE) \ diff --git a/renderdoc/driver/metal/metal_types_bridge.mm b/renderdoc/driver/metal/metal_types_bridge.mm index 6731079f6..bd072b80b 100644 --- a/renderdoc/driver/metal/metal_types_bridge.mm +++ b/renderdoc/driver/metal/metal_types_bridge.mm @@ -24,6 +24,7 @@ #include "metal_types_bridge.h" #include "metal_device.h" +#include "metal_library.h" #define DEFINE_OBJC_HELPERS(CPPTYPE) \ MTL::CPPTYPE *AllocateObjCWrapper(WrappedMTL##CPPTYPE *wrappedCPP) \