From 64c10052337e4d3cd7f40de0f5a695d9d1272610 Mon Sep 17 00:00:00 2001 From: Saad Najmi Date: Tue, 15 Oct 2024 14:49:15 -0500 Subject: [PATCH 1/3] Update to latest React Native macOS 0.74 release --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index e2318b57f..7386a9d89 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10474,8 +10474,8 @@ __metadata: linkType: hard "react-native-macos@npm:^0.74.0": - version: 0.74.5 - resolution: "react-native-macos@npm:0.74.5" + version: 0.74.10 + resolution: "react-native-macos@npm:0.74.10" dependencies: "@jest/create-cache-key-function": ^29.6.3 "@react-native-community/cli": 13.6.9 @@ -10522,7 +10522,7 @@ __metadata: optional: true bin: react-native-macos: cli.js - checksum: 732f09c21be670188ef7b7d658bd849fe4c0ef78157cb31edf88a46912eb83dbbce93decde809a6bfffdfda42caf04faa62638475279c16390a4fdb8315d7794 + checksum: d94a78599cd5442e51f5c9cf5fc8cd94586e5c05c11ce6f09a725deb5b28722744f5c6b5a5912eddbf71718f999b845db24b1a408e306252563e4d2fd5528e9a languageName: node linkType: hard From 34de147fd80747233780e0f0bd05739bddf03aa6 Mon Sep 17 00:00:00 2001 From: Saad Najmi Date: Thu, 19 Sep 2024 12:20:29 -0700 Subject: [PATCH 2/3] feat: Add macOS support --- packages/webgpu/apple/ApplePlatformContext.mm | 8 ++++++++ packages/webgpu/apple/MetalView.h | 4 ++-- packages/webgpu/apple/MetalView.mm | 12 ++++++++++++ packages/webgpu/apple/RNWGUIKit.h | 11 +++++++++++ packages/webgpu/apple/WebGPUViewManager.mm | 5 +++-- packages/webgpu/react-native-wgpu.podspec | 2 +- 6 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 packages/webgpu/apple/RNWGUIKit.h diff --git a/packages/webgpu/apple/ApplePlatformContext.mm b/packages/webgpu/apple/ApplePlatformContext.mm index 57706a3ab..777e55bc9 100644 --- a/packages/webgpu/apple/ApplePlatformContext.mm +++ b/packages/webgpu/apple/ApplePlatformContext.mm @@ -32,12 +32,20 @@ throw std::runtime_error("Couldn't retrive blob data"); } +#if !TARGET_OS_OSX UIImage *image = [UIImage imageWithData:blobData]; +#else + NSImage *image = [[NSImage alloc] initWithData:blobData]; +#endif if (!image) { throw std::runtime_error("Couldn't decode image"); } +#if !TARGET_OS_OSX CGImageRef cgImage = image.CGImage; +#else + CGImageRef cgImage = [image CGImageForProposedRect:NULL context:NULL hints:NULL]; +#endif size_t width = CGImageGetWidth(cgImage); size_t height = CGImageGetHeight(cgImage); size_t bitsPerComponent = 8; diff --git a/packages/webgpu/apple/MetalView.h b/packages/webgpu/apple/MetalView.h index 373a30da2..4b1fe859b 100644 --- a/packages/webgpu/apple/MetalView.h +++ b/packages/webgpu/apple/MetalView.h @@ -1,9 +1,9 @@ #pragma once #import "WebGPUModule.h" -#import +#import "RNWGUIKit.h" -@interface MetalView : UIView +@interface MetalView : RNWGPlatformView @property NSNumber *contextId; diff --git a/packages/webgpu/apple/MetalView.mm b/packages/webgpu/apple/MetalView.mm index c2930e7d6..240dcc669 100644 --- a/packages/webgpu/apple/MetalView.mm +++ b/packages/webgpu/apple/MetalView.mm @@ -9,9 +9,21 @@ @implementation MetalView { BOOL _isConfigured; } +#if !TARGET_OS_OSX + (Class)layerClass { return [CAMetalLayer class]; } +#else // !TARGET_OS_OSX +- (instancetype)init +{ + self = [super init]; + if (self) { + self.wantsLayer = true; + self.layer = [CAMetalLayer layer]; + } + return self; +} +#endif // !TARGET_OS_OSX - (void)configure { diff --git a/packages/webgpu/apple/RNWGUIKit.h b/packages/webgpu/apple/RNWGUIKit.h new file mode 100644 index 000000000..32bca9f21 --- /dev/null +++ b/packages/webgpu/apple/RNWGUIKit.h @@ -0,0 +1,11 @@ +#if !TARGET_OS_OSX +#import +#else +#import +#endif + +#if !TARGET_OS_OSX +typedef UIView RNWGPlatformView; +#else +typedef NSView RNWGPlatformView; +#endif diff --git a/packages/webgpu/apple/WebGPUViewManager.mm b/packages/webgpu/apple/WebGPUViewManager.mm index 7a37bcf0f..ec0f60dc8 100644 --- a/packages/webgpu/apple/WebGPUViewManager.mm +++ b/packages/webgpu/apple/WebGPUViewManager.mm @@ -2,6 +2,7 @@ #import #import "MetalView.h" #import "RCTBridge.h" +#import "RNWGUIKit.h" #import "WebGPUModule.h" @interface WebGPUViewManager : RCTViewManager @@ -11,11 +12,11 @@ @implementation WebGPUViewManager RCT_EXPORT_MODULE(WebGPUView) -- (UIView *)view { +- (RNWGPlatformView *)view { return [MetalView new]; } -RCT_CUSTOM_VIEW_PROPERTY(contextId, NSNumber, UIView) { +RCT_CUSTOM_VIEW_PROPERTY(contextId, NSNumber, RNWGPlatformView) { NSNumber *contextId = [RCTConvert NSNumber:json]; [(MetalView *)view setContextId:contextId]; } diff --git a/packages/webgpu/react-native-wgpu.podspec b/packages/webgpu/react-native-wgpu.podspec index c318f5b62..98967596c 100644 --- a/packages/webgpu/react-native-wgpu.podspec +++ b/packages/webgpu/react-native-wgpu.podspec @@ -11,7 +11,7 @@ Pod::Spec.new do |s| s.license = package["license"] s.authors = package["author"] - s.platforms = { :ios => min_ios_version_supported, :visionos => "1.0" } + s.platforms = { :ios => min_ios_version_supported, :osx => "10.15", :visionos => "1.0" } s.source = { :git => "https://github.com/wcandillon/react-native-webgpu.git", :tag => "#{s.version}" } s.source_files = [ From 9c4e4ef80ae8ca6e22f467ee6260539f30069fb9 Mon Sep 17 00:00:00 2001 From: Saad Najmi Date: Tue, 15 Oct 2024 14:52:49 -0500 Subject: [PATCH 3/3] Add `build-macos` to CI --- .github/workflows/ci.yml | 33 +++++++++++++++++++++++++++++++++ apps/example/package.json | 4 +++- package.json | 1 + turbo.json | 28 +++++++++++++++++++++++++--- 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e1296580f..d5b562b16 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -164,3 +164,36 @@ jobs: - name: Build example for iOS run: | yarn turbo run build:ios --cache-dir="${{ env.TURBO_CACHE_DIR }}" + + build-macos: + runs-on: macos-latest + env: + TURBO_CACHE_DIR: .turbo/macos + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup + uses: ./.github/actions/setup + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + + - name: Cache turborepo for macOS + uses: actions/cache@v3 + with: + path: ${{ env.TURBO_CACHE_DIR }} + key: ${{ runner.os }}-turborepo-macos-${{ hashFiles('yarn.lock') }} + restore-keys: | + ${{ runner.os }}-turborepo-macos- + + - name: Check turborepo cache for macOS + run: | + TURBO_CACHE_STATUS=$(node -p "($(yarn turbo run build:macos --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:macos').cache.status") + + if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then + echo "turbo_cache_hit=1" >> $GITHUB_ENV + fi + + - name: Build example for macOS + run: | + yarn turbo run build:macos --cache-dir="${{ env.TURBO_CACHE_DIR }}" diff --git a/apps/example/package.json b/apps/example/package.json index a2722eb14..633206e20 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -8,9 +8,11 @@ "android": "react-native run-android", "ios": "react-native run-ios", "start": "react-native start", - "pod:install": "pod install --project-directory=ios", + "pod:install:ios": "pod install --project-directory=ios", + "pod:install:macos": "pod install --project-directory=macos", "build:android": "cd android && ./gradlew assembleDebug --warning-mode all", "build:ios": "react-native build-ios --scheme Example --mode Debug --extra-params \"-sdk iphonesimulator CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ GCC_OPTIMIZATION_LEVEL=0 GCC_PRECOMPILE_PREFIX_HEADER=YES ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO\"", + "build:macos": "react-native build-macos --scheme Example --mode Debug --extra-params \"-sdk iphonesimulator CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ GCC_OPTIMIZATION_LEVEL=0 GCC_PRECOMPILE_PREFIX_HEADER=YES ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO\"", "mkdist": "node -e \"require('node:fs').mkdirSync('dist', { recursive: true, mode: 0o755 })\"" }, "dependencies": { diff --git a/package.json b/package.json index 0fc7d581e..1b3829df7 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "lint": "turbo run lint", "tsc": "turbo run tsc", "build:ios": "turbo run build:ios", + "build:macos": "turbo run build:macos", "build:android": "turbo run build:android", "build": "turbo run build", "pod:install": "turbo run pod:install" diff --git a/turbo.json b/turbo.json index b11717250..852ee62d4 100644 --- a/turbo.json +++ b/turbo.json @@ -25,7 +25,7 @@ ] }, "build:ios": { - "dependsOn": ["pod:install"], + "dependsOn": ["pod:install:ios"], "outputs": [ "apps/*/ios/build", "apps/*/ios/Pods" @@ -34,14 +34,36 @@ "**/package.json", "**/*.podspec", "packages/webgpu/cpp/**", - "packages/webgpu/ios/**", + "packages/webgpu/apple/**", "apps/*/package.json", "apps/*/ios", "!apps/*/ios/build", "!apps/*/ios/Pods" ] }, - "pod:install": { + "build:macos": { + "dependsOn": ["pod:install:macos"], + "outputs": [ + "apps/*/macos/build", + "apps/*/macos/Pods" + ], + "inputs": [ + "**/package.json", + "**/*.podspec", + "packages/webgpu/cpp/**", + "packages/webgpu/apple/**", + "apps/*/package.json", + "apps/*/macos", + "!apps/*/macos/build", + "!apps/*/macos/Pods" + ] + }, + "pod:install:ios": { + "cache": false, + "inputs": ["**/ios/Podfile", "**/ios/Podfile.lock"], + "outputs": ["**/ios/Pods/**"] + }, + "pod:install:macos": { "cache": false, "inputs": ["**/ios/Podfile", "**/ios/Podfile.lock"], "outputs": ["**/ios/Pods/**"]