-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
68 additions
and
77 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Sources/GaussianSplatSupport/Sorting/AsyncSortManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import AsyncAlgorithms | ||
import BaseSupport | ||
import GaussianSplatShaders | ||
@preconcurrency import Metal | ||
import MetalSupport | ||
import os | ||
import simd | ||
import SIMDSupport | ||
|
||
internal actor AsyncSortManager <Splat> where Splat: SplatProtocol { | ||
private var splatCloud: SplatCloud<Splat> | ||
private var _sortRequestChannel: AsyncChannel<SortState> = .init() | ||
private var _sortedIndicesChannel: AsyncChannel<SplatIndices> = .init() | ||
private var logger: Logger? = Logger() | ||
private var sorter: CPUSplatRadixSorter<Splat> | ||
|
||
internal init(device: MTLDevice, splatCloud: SplatCloud<Splat>, capacity: Int) throws { | ||
self.sorter = .init(device: device, capacity: capacity) | ||
self.splatCloud = splatCloud | ||
Task(priority: .high) { | ||
do { | ||
try await self.sort() | ||
} | ||
catch is CancellationError { | ||
} | ||
catch { | ||
await logger?.log("Failed to sort splats: \(error)") | ||
} | ||
} | ||
} | ||
|
||
internal func sortedIndicesChannel() -> AsyncChannel<SplatIndices> { | ||
_sortedIndicesChannel | ||
} | ||
|
||
nonisolated | ||
internal func requestSort(camera: simd_float4x4, model: simd_float4x4, count: Int) { | ||
Task { | ||
await _sortRequestChannel.send(.init(camera: camera, model: model, count: count)) | ||
} | ||
} | ||
|
||
internal func sort() async throws { | ||
// swiftlint:disable:next empty_count | ||
for await state in _sortRequestChannel.removeDuplicates() where state.count > 0 { | ||
let currentIndexedDistances = try sorter.sort(splats: splatCloud.splats, camera: state.camera, model: state.model) | ||
await self._sortedIndicesChannel.send(.init(state: state, indices: currentIndexedDistances)) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - | ||
|
||
internal extension AsyncSortManager { | ||
static func sort(device: MTLDevice, splats: TypedMTLBuffer<Splat>, camera: simd_float4x4, model: simd_float4x4) throws -> SplatIndices { | ||
let sorter = CPUSplatRadixSorter<Splat>(device: device, capacity: splats.capacity) | ||
let indices = try sorter.sort(splats: splats, camera: camera, model: model) | ||
return .init(state: .init(camera: camera, model: model, count: splats.count), indices: indices) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters