Repository navigation
Set OBR's head rotation and enable flags without the render mutex #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
116 changes: 116 additions & 0 deletions
116
src/renderer/obr/obr_capi/obr/obr/common/atomic_rotation.h
This file contains hidden or 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,116 @@ | ||
| /* | ||
| * Copyright (c) 2026, Alliance for Open Media. All rights reserved | ||
| * | ||
| * This source code is subject to the terms of the BSD 3-Clause Clear License | ||
| * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear | ||
| * License was not distributed with this source code in the LICENSE file, you | ||
| * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the | ||
| * Alliance for Open Media Patent License 1.0 was not distributed with this | ||
| * source code in the PATENTS file, you can obtain it at | ||
| * www.aomedia.org/license/patent. | ||
| */ | ||
|
|
||
| #ifndef OBR_COMMON_ATOMIC_ROTATION_H_ | ||
| #define OBR_COMMON_ATOMIC_ROTATION_H_ | ||
|
|
||
| #include <atomic> | ||
| #include <cstdint> | ||
|
|
||
| #include "obr/common/misc_math.h" | ||
|
|
||
| namespace obr { | ||
|
|
||
| /*!\brief A `WorldRotation` written by one or more threads and read by | ||
| * another without a lock. | ||
| * | ||
| * The four quaternion components are atomic floats behind a sequence | ||
| * counter that is odd while a write is in progress. `std::atomic` of the | ||
| * whole 16-byte quaternion is not guaranteed to be lock-free on every | ||
| * platform OBR targets. | ||
| * `Store()` waits for a concurrent `Store()` to finish; `Load()` never waits: | ||
| * after `kMaxLoadAttempts` attempts that overlap a write it returns the | ||
| * caller's previous value. | ||
| */ | ||
| class AtomicWorldRotation { | ||
| public: | ||
| /*!\brief The number of times `Load()` reads the components before it | ||
| * returns the fallback. | ||
| * | ||
| * `Load()` runs on the render thread, so it must not wait for a writer: a | ||
| * writer preempted while the sequence is odd would keep an unbounded retry | ||
| * loop spinning until that writer runs again. An attempt fails only when a | ||
| * write overlaps it, so with 4 the fallback needs writes to overlap four | ||
| * consecutive attempts. `ObrImpl` passes the rotation it rendered the | ||
| * previous block with as the fallback. Any small bound behaves the same | ||
| * way; 4 is not tuned against a measurement. | ||
| */ | ||
| static constexpr int kMaxLoadAttempts = 4; | ||
|
|
||
| /*!\brief Constructs an identity rotation. */ | ||
| AtomicWorldRotation() : AtomicWorldRotation(WorldRotation()) {} | ||
|
|
||
| /*!\brief Constructs a rotation holding `rotation`. */ | ||
| explicit AtomicWorldRotation(const WorldRotation& rotation) | ||
| : w_(rotation.w()), | ||
| x_(rotation.x()), | ||
| y_(rotation.y()), | ||
| z_(rotation.z()) {} | ||
|
|
||
| AtomicWorldRotation(const AtomicWorldRotation&) = delete; | ||
| AtomicWorldRotation& operator=(const AtomicWorldRotation&) = delete; | ||
|
|
||
| /*!\brief Publishes `rotation`. Safe to call from several threads. */ | ||
| void Store(const WorldRotation& rotation) { | ||
| // Make the sequence odd. Another writer holding it odd is storing four | ||
| // floats, so the wait is short while that writer runs; it spins for as | ||
| // long as that writer is preempted. The acquire on success orders this | ||
| // store after the previous writer's, whose final sequence store it reads. | ||
| uint32_t sequence = sequence_.load(std::memory_order_relaxed); | ||
| do { | ||
| while (sequence & 1u) { | ||
| sequence = sequence_.load(std::memory_order_relaxed); | ||
| } | ||
| } while (!sequence_.compare_exchange_weak(sequence, sequence + 1, | ||
| std::memory_order_acquire, | ||
| std::memory_order_relaxed)); | ||
| // Orders the odd sequence before the component stores, so a reader that | ||
| // sees a new component also sees a changed sequence. | ||
| std::atomic_thread_fence(std::memory_order_release); | ||
| w_.store(rotation.w(), std::memory_order_relaxed); | ||
| x_.store(rotation.x(), std::memory_order_relaxed); | ||
| y_.store(rotation.y(), std::memory_order_relaxed); | ||
| z_.store(rotation.z(), std::memory_order_relaxed); | ||
| sequence_.store(sequence + 2, std::memory_order_release); | ||
| } | ||
|
|
||
| /*!\brief Returns the last stored rotation, or `fallback` when every one of | ||
| * `kMaxLoadAttempts` reads overlapped a `Store()`. | ||
| */ | ||
| WorldRotation Load(const WorldRotation& fallback) const { | ||
| for (int attempt = 0; attempt < kMaxLoadAttempts; ++attempt) { | ||
| const uint32_t before = sequence_.load(std::memory_order_acquire); | ||
| if (before & 1u) continue; | ||
| const float w = w_.load(std::memory_order_relaxed); | ||
| const float x = x_.load(std::memory_order_relaxed); | ||
| const float y = y_.load(std::memory_order_relaxed); | ||
| const float z = z_.load(std::memory_order_relaxed); | ||
| // Orders the component loads before the second sequence load. | ||
| std::atomic_thread_fence(std::memory_order_acquire); | ||
| if (sequence_.load(std::memory_order_relaxed) == before) { | ||
| return WorldRotation(w, x, y, z); | ||
| } | ||
| } | ||
| return fallback; | ||
| } | ||
|
|
||
| private: | ||
| std::atomic<uint32_t> sequence_{0}; | ||
| std::atomic<float> w_; | ||
| std::atomic<float> x_; | ||
| std::atomic<float> y_; | ||
| std::atomic<float> z_; | ||
| }; | ||
|
|
||
| } // namespace obr | ||
|
|
||
| #endif // OBR_COMMON_ATOMIC_ROTATION_H_ | ||
This file contains hidden or 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
131 changes: 131 additions & 0 deletions
131
src/renderer/obr/obr_capi/obr/obr/common/tests/atomic_rotation_test.cc
This file contains hidden or 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,131 @@ | ||
| /* | ||
| * Copyright (c) 2026, Alliance for Open Media. All rights reserved | ||
| * | ||
| * This source code is subject to the terms of the BSD 3-Clause Clear License | ||
| * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear | ||
| * License was not distributed with this source code in the LICENSE file, you | ||
| * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the | ||
| * Alliance for Open Media Patent License 1.0 was not distributed with this | ||
| * source code in the PATENTS file, you can obtain it at | ||
| * www.aomedia.org/license/patent. | ||
| */ | ||
|
|
||
| #include "obr/common/atomic_rotation.h" | ||
|
|
||
| #include <atomic> | ||
| #include <thread> | ||
| #include <vector> | ||
|
|
||
| #include "gtest/gtest.h" | ||
| #include "obr/common/misc_math.h" | ||
|
|
||
| namespace obr { | ||
| namespace { | ||
|
|
||
| // A rotation whose components are `k`, `k + 1`, `k + 2` and `k + 3`, so a | ||
| // load that mixes two stores shows as unequal steps. Integers below 2^24 are | ||
| // exact in a float. | ||
| WorldRotation Numbered(int k) { | ||
| return WorldRotation(static_cast<float>(k), static_cast<float>(k + 1), | ||
| static_cast<float>(k + 2), static_cast<float>(k + 3)); | ||
| } | ||
|
|
||
| bool IsNumbered(const WorldRotation& r) { | ||
| return r.x() - r.w() == 1.0f && r.y() - r.w() == 2.0f && | ||
| r.z() - r.w() == 3.0f; | ||
| } | ||
|
|
||
| const WorldRotation kFallback(-7.0f, -7.0f, -7.0f, -7.0f); | ||
|
|
||
| bool IsFallback(const WorldRotation& r) { | ||
| return r.w() == kFallback.w() && r.x() == kFallback.x() && | ||
| r.y() == kFallback.y() && r.z() == kFallback.z(); | ||
| } | ||
|
|
||
| TEST(AtomicWorldRotationTest, DefaultIsIdentity) { | ||
| const AtomicWorldRotation rotation; | ||
| const WorldRotation loaded = rotation.Load(kFallback); | ||
| EXPECT_EQ(loaded.w(), 1.0f); | ||
| EXPECT_EQ(loaded.x(), 0.0f); | ||
| EXPECT_EQ(loaded.y(), 0.0f); | ||
| EXPECT_EQ(loaded.z(), 0.0f); | ||
| } | ||
|
|
||
| TEST(AtomicWorldRotationTest, LoadReturnsTheLastStore) { | ||
| AtomicWorldRotation rotation; | ||
| rotation.Store(WorldRotation(0.5f, -0.5f, 0.25f, 0.75f)); | ||
| rotation.Store(Numbered(10)); | ||
| const WorldRotation loaded = rotation.Load(kFallback); | ||
| EXPECT_EQ(loaded.w(), 10.0f); | ||
| EXPECT_EQ(loaded.x(), 11.0f); | ||
| EXPECT_EQ(loaded.y(), 12.0f); | ||
| EXPECT_EQ(loaded.z(), 13.0f); | ||
| } | ||
|
|
||
| // One writer stores increasing numbered rotations while the test thread | ||
| // loads. Every load returns a whole store or the fallback, and the stores it | ||
| // returns never go back in time. | ||
| TEST(AtomicWorldRotationTest, NoTornLoadsUnderAConcurrentWriter) { | ||
| AtomicWorldRotation rotation(Numbered(0)); | ||
| std::atomic<bool> stop{false}; | ||
| std::thread writer([&] { | ||
| for (int k = 1; !stop.load(std::memory_order_relaxed); ++k) { | ||
| rotation.Store(Numbered(k & ((1 << 20) - 1))); | ||
| } | ||
| }); | ||
|
|
||
| float last_w = 0.0f; | ||
| int torn = 0; | ||
| int backwards = 0; | ||
| for (int i = 0; i < 200000; ++i) { | ||
| const WorldRotation loaded = rotation.Load(kFallback); | ||
| if (IsFallback(loaded)) continue; | ||
| if (!IsNumbered(loaded)) { | ||
| ++torn; | ||
| continue; | ||
| } | ||
| // The writer wraps at 2^20; a smaller value after a large one is a wrap. | ||
| if (loaded.w() < last_w && last_w - loaded.w() < (1 << 19)) ++backwards; | ||
| last_w = loaded.w(); | ||
| } | ||
| stop.store(true, std::memory_order_relaxed); | ||
| writer.join(); | ||
|
|
||
| EXPECT_EQ(torn, 0); | ||
| EXPECT_EQ(backwards, 0); | ||
| } | ||
|
|
||
| // Several writers store as fast as they can. `Load()` returns after at most | ||
| // `kMaxLoadAttempts` attempts each time, with a whole store or the fallback. | ||
| TEST(AtomicWorldRotationTest, LoadIsBoundedUnderAWriteStorm) { | ||
| AtomicWorldRotation rotation(Numbered(0)); | ||
| std::atomic<bool> stop{false}; | ||
| std::vector<std::thread> writers; | ||
| for (int t = 0; t < 4; ++t) { | ||
| writers.emplace_back([&rotation, &stop, t] { | ||
| for (int k = t; !stop.load(std::memory_order_relaxed); k += 4) { | ||
| rotation.Store(Numbered(k & ((1 << 20) - 1))); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| int torn = 0; | ||
| int fallbacks = 0; | ||
| for (int i = 0; i < 100000; ++i) { | ||
| const WorldRotation loaded = rotation.Load(kFallback); | ||
| if (IsFallback(loaded)) { | ||
| ++fallbacks; | ||
| } else if (!IsNumbered(loaded)) { | ||
| ++torn; | ||
| } | ||
| } | ||
| stop.store(true, std::memory_order_relaxed); | ||
| for (auto& writer : writers) writer.join(); | ||
|
|
||
| EXPECT_EQ(torn, 0); | ||
| // Whether a load fell back depends on scheduling; record it. | ||
| RecordProperty("fallbacks", fallbacks); | ||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace obr |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick question: why choose 4 times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Load()runs on the render thread once per block, so it must not wait for a writer. A writer that is preempted while the sequence is odd would keep an unbounded retry loop spinning until it runs again. The bound caps the render thread's work at four reads; after that,Process()uses the previous block's rotation, so a new rotation applies one block later.A
Store()is four relaxed float stores between two sequence updates, so a retry after an overlapping write normally succeeds on the next attempt. With 4, a fallback needs writes to overlap four consecutive reads. Any small bound would behave the same way; 4 is not tuned against a measurement.Added to the
kMaxLoadAttemptscomment in 340dc49.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, thanks!