Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions SceneDepthPointCloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -131,8 +131,9 @@
E73DD12524636FEF00D77039 /* Project object */ = {
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = YES;
LastSwiftUpdateCheck = 1140;
LastUpgradeCheck = 1200;
LastUpgradeCheck = 2610;
ORGANIZATIONNAME = Apple;
TargetAttributes = {
E73DD12C24636FEF00D77039 = {
Expand Down Expand Up @@ -245,8 +246,10 @@
CODE_SIGN_STYLE = Automatic;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = 58R9CYQKK2;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand All @@ -266,6 +269,7 @@
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos.internal;
STRING_CATALOG_GENERATE_SYMBOLS = YES;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
Expand Down Expand Up @@ -308,8 +312,10 @@
CODE_SIGN_STYLE = Automatic;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = 58R9CYQKK2;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand All @@ -322,6 +328,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos.internal;
STRING_CATALOG_GENERATE_SYMBOLS = YES;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
VALIDATE_PRODUCT = YES;
Expand All @@ -337,8 +344,8 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 52N826U726;
INFOPLIST_FILE = SceneDepthPointCloud/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 26.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -349,7 +356,7 @@
PROVISIONING_PROFILE_SPECIFIER = "";
SDKROOT = iphoneos;
SWIFT_OBJC_BRIDGING_HEADER = "SceneDepthPointCloud/SceneDepthPointCloud-Bridging-Header.h";
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -363,8 +370,8 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 52N826U726;
INFOPLIST_FILE = SceneDepthPointCloud/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 26.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -375,7 +382,7 @@
PROVISIONING_PROFILE_SPECIFIER = "";
SDKROOT = iphoneos;
SWIFT_OBJC_BRIDGING_HEADER = "SceneDepthPointCloud/SceneDepthPointCloud-Bridging-Header.h";
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1200"
LastUpgradeVersion = "2610"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion SceneDepthPointCloud/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Contains the application's delegate.
import UIKit
import ARKit

@UIApplicationMain
@main
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
Expand Down
30 changes: 27 additions & 3 deletions SceneDepthPointCloud/MetalBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ struct MetalBuffer<Element>: Resource {
/// Initializes the buffer with the contents of the provided array.
init(device: MTLDevice, array: [Element], index: UInt32, options: MTLResourceOptions = []) {

guard let buffer = device.makeBuffer(bytes: array, length: MemoryLayout<Element>.stride * array.count, options: .storageModeShared) else {
// Safely create a buffer from the array's raw bytes. This avoids forming an UnsafeRawPointer
// directly from `[Element]` which may contain references.
let length = MemoryLayout<Element>.stride * array.count
let created: MTLBuffer? = array.withUnsafeBytes { rawBuffer in
guard let base = rawBuffer.baseAddress, rawBuffer.count == length else {
return device.makeBuffer(length: length, options: options)
}
return device.makeBuffer(bytes: base, length: rawBuffer.count, options: options)
}
guard let buffer = created else {
fatalError("Failed to create MTLBuffer")
}
self.buffer = buffer
Expand All @@ -60,10 +69,25 @@ struct MetalBuffer<Element>: Resource {
}

/// Replaces the buffer's memory with the values in the array.
func assign<Element>(with array: [Element]) {
func assign(with array: [Element]) {
let byteCount = array.count * stride
precondition(byteCount == buffer.length, "Mismatch between the byte count of the array's contents and the MTLBuffer length.")
buffer.contents().copyMemory(from: array, byteCount: byteCount)

// Safely copy from the array's raw bytes. Avoid forming an UnsafeRawPointer directly from `[Element]`
// because `Element` may contain references.
array.withUnsafeBytes { rawBuffer in
if let base = rawBuffer.baseAddress, rawBuffer.count == byteCount {
buffer.contents().copyMemory(from: base, byteCount: byteCount)
} else {
// If we cannot get a valid raw representation (e.g., Element contains references),
// fall back to per-element assignment to avoid undefined behavior.
for i in 0..<array.count {
withUnsafePointer(to: array[i]) { ptr in
buffer.contents().advanced(by: i * stride).copyMemory(from: ptr, byteCount: stride)
}
}
}
}
}

/// Returns a copy of the value at the specified element index in the buffer.
Expand Down
2 changes: 2 additions & 0 deletions SceneDepthPointCloud/Renderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ARKit
import Foundation
import UIKit

@MainActor
final class Renderer {
// Whether recording is on
public var isRecording = false;
Expand Down Expand Up @@ -504,3 +505,4 @@ private extension Renderer {
return flipYZ * matrix_float4x4(simd_quaternion(rotationAngle, Float3(0, 0, 1)))
}
}

8 changes: 6 additions & 2 deletions SceneDepthPointCloud/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import Metal
import MetalKit
import ARKit

final class ViewController: UIViewController, ARSessionDelegate {
@MainActor
final class ViewController: UIViewController, @MainActor ARSessionDelegate {
private let isUIEnabled = true
private let confidenceControl = UISegmentedControl(items: ["Low", "Medium", "High"])
private let rgbRadiusSlider = UISlider()
Expand Down Expand Up @@ -215,7 +216,7 @@ final class ViewController: UIViewController, ARSessionDelegate {
}

// update textlabel on tasks start/finish
extension ViewController: TaskDelegate {
extension ViewController: @MainActor TaskDelegate {
func didStartTask() {
self.taskNum += 1
updateTextLabel()
Expand All @@ -226,6 +227,7 @@ extension ViewController: TaskDelegate {
updateTextLabel()
}

@MainActor
private func updateTextLabel() {
let text = " 1/\(self.renderer.pickFrames) of new frames \n Files saved \(self.completedTaskNum)/\(self.taskNum) "
DispatchQueue.main.async {
Expand All @@ -250,6 +252,7 @@ extension ViewController: MTKViewDelegate {

// MARK: - RenderDestinationProvider

@MainActor
protocol RenderDestinationProvider {
var currentRenderPassDescriptor: MTLRenderPassDescriptor? { get }
var currentDrawable: CAMetalDrawable? { get }
Expand All @@ -258,6 +261,7 @@ protocol RenderDestinationProvider {
var sampleCount: Int { get set }
}

@MainActor
extension MTKView: RenderDestinationProvider {

}