Skip to content

Commit

Permalink
start MetalKit
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Oct 28, 2024
1 parent 701ca58 commit 5feda5b
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cidre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ full = [
"sc",
"mc",
"mtl",
"mtk",
"mlc",
"cv",
"objc",
Expand Down Expand Up @@ -108,7 +109,8 @@ ut = ["ns"]
un = ["ns"]
ct = ["cf", "cg"]
mc = ["ns"]
mtl = ["ns"]
mtl = ["ns", "blocks"]
mtk = ["mtl"]
mlc = ["mtl"]
mps = ["mtl"]
mpsg = ["mps"]
Expand Down
4 changes: 3 additions & 1 deletion cidre/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ fn main() {
if sdk != "watchos" && sdk != "watchsimulator" {
add_xc_target_args_from_features(
&mut xc_target_args,
&["ca", "vn", "mps", "mpsg", "mc", "mtl", "ci", "gc", "av_kit"],
&[
"ca", "vn", "mps", "mpsg", "mc", "mtl", "mtk", "ci", "gc", "av_kit",
],
);
if sdk != "xros" && sdk != "xrsimulator" {
add_xc_target_args_from_features(&mut xc_target_args, &["mlc"]);
Expand Down
26 changes: 26 additions & 0 deletions cidre/pomace/mtk/mtk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// mtk.h
// mtk
//
// Created by Yury Korolev on 10/28/24.
//

#import <MetalKit/MetalKit.h>

NS_ASSUME_NONNULL_BEGIN

Class MTK_TEXTURE_LOADER;

__attribute__((constructor))
static void mtk_initializer(void)
{
static int initialized = 0;
if (!initialized) {
initialized = 1;

MTK_TEXTURE_LOADER = [MTKTextureLoader class];
}
}


NS_ASSUME_NONNULL_END
8 changes: 8 additions & 0 deletions cidre/pomace/mtk/mtk.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// mtk.m
// mtk
//
// Created by Yury Korolev on 10/28/24.
//

#import "mtk.h"
112 changes: 112 additions & 0 deletions cidre/pomace/pomace.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
D25387712C906096002122EB /* cl.m in Sources */ = {isa = PBXBuildFile; fileRef = D253876F2C906096002122EB /* cl.m */; };
D25387722C906096002122EB /* cl.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = D253876E2C906096002122EB /* cl.h */; };
D27864ED2A0A2BBC00E64871 /* MLCompute.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D27864EB2A0A2A8600E64871 /* MLCompute.framework */; };
D2C1FA3B2CCFAA7C008EFEDC /* mtk.m in Sources */ = {isa = PBXBuildFile; fileRef = D2C1FA392CCFAA7C008EFEDC /* mtk.m */; };
D2C1FA3C2CCFAA7C008EFEDC /* mtk.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = D2C1FA382CCFAA7C008EFEDC /* mtk.h */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -237,6 +239,16 @@
);
runOnlyForDeploymentPostprocessing = 1;
};
D2C1FA2C2CCFAA6E008EFEDC /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "include/$(PRODUCT_NAME)";
dstSubfolderSpec = 16;
files = (
D2C1FA3C2CCFAA7C008EFEDC /* mtk.h in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
};
D2DC5D20295870DB007E2B9D /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 8;
Expand Down Expand Up @@ -362,6 +374,9 @@
D2A52939283A1F5800E7B4C6 /* libca.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libca.a; sourceTree = BUILT_PRODUCTS_DIR; };
D2A52946283A1F6A00E7B4C6 /* libci.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libci.a; sourceTree = BUILT_PRODUCTS_DIR; };
D2A52950283A26B800E7B4C6 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
D2C1FA2E2CCFAA6E008EFEDC /* libmtk.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libmtk.a; sourceTree = BUILT_PRODUCTS_DIR; };
D2C1FA382CCFAA7C008EFEDC /* mtk.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = mtk.h; sourceTree = "<group>"; };
D2C1FA392CCFAA7C008EFEDC /* mtk.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = mtk.m; sourceTree = "<group>"; };
D2DC5D22295870DB007E2B9D /* libsn.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libsn.a; sourceTree = BUILT_PRODUCTS_DIR; };
D2DC5D34295B5064007E2B9D /* libmps.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libmps.a; sourceTree = BUILT_PRODUCTS_DIR; };
D2DC5D41295B506F007E2B9D /* libmpsg.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libmpsg.a; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -497,6 +512,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
D2C1FA2B2CCFAA6E008EFEDC /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
D2DC5D1F295870DB007E2B9D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
Expand Down Expand Up @@ -757,9 +779,19 @@
name = Frameworks;
sourceTree = "<group>";
};
D2C1FA3A2CCFAA7C008EFEDC /* mtk */ = {
isa = PBXGroup;
children = (
D2C1FA382CCFAA7C008EFEDC /* mtk.h */,
D2C1FA392CCFAA7C008EFEDC /* mtk.m */,
);
path = mtk;
sourceTree = "<group>";
};
D2FBEBEE27CB653200FD974A = {
isa = PBXGroup;
children = (
D2C1FA3A2CCFAA7C008EFEDC /* mtk */,
D25386F72C906079002122EB /* mtl */,
D25386FC2C90607C002122EB /* sc */,
D25387012C90607F002122EB /* av */,
Expand Down Expand Up @@ -814,6 +846,7 @@
D22A550C2B78E27300114BDE /* libwc.a */,
D28CF5EC2BCD201B00ED6381 /* libnl.a */,
D25386522C9019D4002122EB /* libav_kit.a */,
D2C1FA2E2CCFAA6E008EFEDC /* libmtk.a */,
);
name = Products;
sourceTree = "<group>";
Expand Down Expand Up @@ -1129,6 +1162,25 @@
productReference = D2A52946283A1F6A00E7B4C6 /* libci.a */;
productType = "com.apple.product-type.library.static";
};
D2C1FA2D2CCFAA6E008EFEDC /* mtk */ = {
isa = PBXNativeTarget;
buildConfigurationList = D2C1FA372CCFAA6E008EFEDC /* Build configuration list for PBXNativeTarget "mtk" */;
buildPhases = (
D2C1FA2A2CCFAA6E008EFEDC /* Sources */,
D2C1FA2B2CCFAA6E008EFEDC /* Frameworks */,
D2C1FA2C2CCFAA6E008EFEDC /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = mtk;
packageProductDependencies = (
);
productName = mtk;
productReference = D2C1FA2E2CCFAA6E008EFEDC /* libmtk.a */;
productType = "com.apple.product-type.library.static";
};
D2DC5D21295870DB007E2B9D /* sn */ = {
isa = PBXNativeTarget;
buildConfigurationList = D2DC5D2A295870DB007E2B9D /* Build configuration list for PBXNativeTarget "sn" */;
Expand Down Expand Up @@ -1277,6 +1329,9 @@
D2A52945283A1F6A00E7B4C6 = {
CreatedOnToolsVersion = 13.4;
};
D2C1FA2D2CCFAA6E008EFEDC = {
CreatedOnToolsVersion = 16.2;
};
D2DC5D21295870DB007E2B9D = {
CreatedOnToolsVersion = 14.2;
};
Expand Down Expand Up @@ -1330,6 +1385,7 @@
D22A550B2B78E27300114BDE /* wc */,
D28CF5EB2BCD201B00ED6381 /* nl */,
D25386512C9019D4002122EB /* av_kit */,
D2C1FA2D2CCFAA6E008EFEDC /* mtk */,
);
};
/* End PBXProject section */
Expand Down Expand Up @@ -1479,6 +1535,14 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
D2C1FA2A2CCFAA6E008EFEDC /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D2C1FA3B2CCFAA7C008EFEDC /* mtk.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
D2DC5D1E295870DB007E2B9D /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
Expand Down Expand Up @@ -2135,6 +2199,45 @@
};
name = Release;
};
D2C1FA342CCFAA6E008EFEDC /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = FD6ZML48V9;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu17;
IPHONEOS_DEPLOYMENT_TARGET = 18.2;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
D2C1FA352CCFAA6E008EFEDC /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CODE_SIGN_STYLE = Automatic;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = FD6ZML48V9;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu17;
IPHONEOS_DEPLOYMENT_TARGET = 18.2;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
D2DC5D28295870DB007E2B9D /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
Expand Down Expand Up @@ -2561,6 +2664,15 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
D2C1FA372CCFAA6E008EFEDC /* Build configuration list for PBXNativeTarget "mtk" */ = {
isa = XCConfigurationList;
buildConfigurations = (
D2C1FA342CCFAA6E008EFEDC /* Debug */,
D2C1FA352CCFAA6E008EFEDC /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
D2DC5D2A295870DB007E2B9D /* Build configuration list for PBXNativeTarget "sn" */ = {
isa = XCConfigurationList;
buildConfigurations = (
Expand Down
3 changes: 3 additions & 0 deletions cidre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ pub mod mc;
#[cfg(feature = "mtl")]
pub mod mtl;

#[cfg(feature = "mtk")]
pub mod mtk;

/// MLCompute
#[cfg(feature = "mlc")]
pub mod mlc;
Expand Down
5 changes: 5 additions & 0 deletions cidre/src/mtk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod texture_loader;
pub use texture_loader::TextureLoader;
pub use texture_loader::TextureLoaderArrayCb;
pub use texture_loader::TextureLoaderCb;
pub use texture_loader::TextureLoaderOpt;
70 changes: 70 additions & 0 deletions cidre/src/mtk/texture_loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::{arc, blocks, define_cls, define_obj_type, mtl, ns, objc};

define_obj_type!(
#[doc(alias = "MTKTextureLoader")]
pub TextureLoader(ns::Id)
);

define_obj_type!(
#[doc(alias = "MTKTextureLoaderOption")]
pub TextureLoaderOpt(ns::String)
);

#[doc(alias = "MTKTextureLoaderCallback")]
pub type TextureLoaderCb = blocks::ResultCompletionHandler<mtl::Texture>;

#[doc(alias = "MTKTextureLoaderArrayCallback")]
pub type TextureLoaderArrayCb = blocks::ResultCompletionHandler<ns::Array<mtl::Texture>>;

impl arc::A<TextureLoader> {
#[objc::msg_send(initWithDevice:)]
pub fn init_with_device(self, device: &mtl::Device) -> arc::R<TextureLoader>;
}

impl TextureLoader {
define_cls!(MTK_TEXTURE_LOADER);

#[objc::msg_send(device)]
pub fn device(&self) -> arc::R<mtl::Device>;

pub fn with_device(device: &mtl::Device) -> arc::R<Self> {
Self::alloc().init_with_device(device)
}

#[objc::msg_send(newTextureWithContentsOfURL:options:error:)]
pub fn new_texture_with_url_err<'ear>(
&self,
url: &ns::Url,
options: Option<&ns::Dictionary<TextureLoaderOpt, ns::Id>>,
error: *mut Option<&'ear ns::Error>,
) -> Option<arc::R<mtl::Texture>>;

pub fn new_texture_with_url<'ear>(
&self,
url: &ns::Url,
options: Option<&ns::Dictionary<TextureLoaderOpt, ns::Id>>,
) -> ns::Result<arc::R<mtl::Texture>> {
ns::if_none(|err| self.new_texture_with_url_err(url, options, err))
}
}

#[link(name = "mtk", kind = "static")]
extern "C" {
static MTK_TEXTURE_LOADER: &'static objc::Class<TextureLoader>;
}

#[cfg(test)]
mod tests {
use crate::{mtk, mtl, ns};

#[test]
fn basics() {
let device = mtl::Device::sys_default().unwrap();
let loader = mtk::TextureLoader::with_device(&device);

let url = ns::Url::with_fs_path_str("unknown.png", false);
let _err = loader
.new_texture_with_url(&url, None)
.expect_err("Should be err");
}
}

0 comments on commit 5feda5b

Please sign in to comment.